blob: b1c9ed67ab7f3c9fb99d1f74411df82d051481ed [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();
John Marriotta494ce12025-07-03 21:28:50 +0200231 int i;
232 char_u *p;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200233
234 if (xp->xp_numfiles == -1)
235 {
Girish Palya6b49fba2025-06-28 19:47:34 +0200236 may_expand_pattern = options & WILD_MAY_EXPAND_PATTERN;
237 pre_incsearch_pos = xp->xp_pre_incsearch_pos;
Jim Zhou3255af82025-02-27 19:29:50 +0100238#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100239 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100240 {
241 // Expand commands typed in input() function
242 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100243 }
244 else
Jim Zhou3255af82025-02-27 19:29:50 +0100245#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100246 {
Jim Zhou3255af82025-02-27 19:29:50 +0100247 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100248 }
249 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200250 }
251
252 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
253 {
254 beep_flush();
255 return OK; // Something illegal on command line
256 }
257 if (xp->xp_context == EXPAND_NOTHING)
258 {
259 // Caller can use the character as a normal char instead
260 return FAIL;
261 }
262
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000263 // If cmd_silent is set then don't show the dots, because redrawcmd() below
264 // won't remove them.
265 if (!cmd_silent)
266 {
267 msg_puts("..."); // show that we are busy
268 out_flush();
269 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270
271 i = (int)(xp->xp_pattern - ccline->cmdbuff);
272 xp->xp_pattern_len = ccline->cmdpos - i;
273
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000274 if (type == WILD_NEXT || type == WILD_PREV
275 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200276 {
277 // Get next/previous match for a previous expanded pattern.
John Marriotta494ce12025-07-03 21:28:50 +0200278 p = ExpandOne(xp, NULL, NULL, 0, type);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200279 }
280 else
281 {
John Marriotta494ce12025-07-03 21:28:50 +0200282 char_u *tmp;
283
Girish Palya6b49fba2025-06-28 19:47:34 +0200284 if (cmdline_fuzzy_completion_supported(xp)
285 || xp->xp_context == EXPAND_PATTERN_IN_BUF)
286 // Don't modify the search string
John Marriotta494ce12025-07-03 21:28:50 +0200287 tmp = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000288 else
John Marriotta494ce12025-07-03 21:28:50 +0200289 tmp = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000290
Bram Moolenaar66b51422019-08-18 21:44:12 +0200291 // Translate string into pattern and expand it.
John Marriotta494ce12025-07-03 21:28:50 +0200292 if (tmp == NULL)
293 p = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200294 else
295 {
296 int use_options = options |
297 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100298 if (use_options & WILD_KEEP_SOLE_ITEM)
299 use_options &= ~WILD_KEEP_SOLE_ITEM;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200300 if (escape)
301 use_options |= WILD_ESCAPE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200302 if (p_wic)
303 use_options += WILD_ICASE;
Girish Palya6b49fba2025-06-28 19:47:34 +0200304
John Marriotta494ce12025-07-03 21:28:50 +0200305 p = ExpandOne(xp, tmp,
Bram Moolenaar66b51422019-08-18 21:44:12 +0200306 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
307 use_options, type);
John Marriotta494ce12025-07-03 21:28:50 +0200308 vim_free(tmp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200309 // longest match: make sure it is not shorter, happens with :help
John Marriotta494ce12025-07-03 21:28:50 +0200310 if (p != NULL && type == WILD_LONGEST)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200311 {
John Marriotta494ce12025-07-03 21:28:50 +0200312 int j;
313
Bram Moolenaar66b51422019-08-18 21:44:12 +0200314 for (j = 0; j < xp->xp_pattern_len; ++j)
John Marriotta494ce12025-07-03 21:28:50 +0200315 {
316 char_u c = ccline->cmdbuff[i + j];
317 if (c == '*' || c == '?')
318 break;
319 }
320 if ((int)STRLEN(p) < j)
321 VIM_CLEAR(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200322 }
323 }
324 }
325
John Marriotta494ce12025-07-03 21:28:50 +0200326 if (p != NULL && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200327 {
John Marriotta494ce12025-07-03 21:28:50 +0200328 size_t plen = STRLEN(p);
329 int difflen;
330 int v;
331
332 difflen = (int)plen - xp->xp_pattern_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200333 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
334 {
335 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
336 xp->xp_pattern = ccline->cmdbuff + i;
337 }
338 else
339 v = OK;
340 if (v == OK)
341 {
342 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
343 &ccline->cmdbuff[ccline->cmdpos],
344 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
John Marriotta494ce12025-07-03 21:28:50 +0200345 mch_memmove(&ccline->cmdbuff[i], p, plen);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200346 ccline->cmdlen += difflen;
347 ccline->cmdpos += difflen;
348 }
349 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200350
351 redrawcmd();
352 cursorcmd();
353
354 // When expanding a ":map" command and no matches are found, assume that
355 // the key is supposed to be inserted literally
John Marriotta494ce12025-07-03 21:28:50 +0200356 if (xp->xp_context == EXPAND_MAPPINGS && p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200357 return FAIL;
358
John Marriotta494ce12025-07-03 21:28:50 +0200359 if (xp->xp_numfiles <= 0 && p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200360 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100361 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200362 // free expanded pattern
363 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
364
John Marriotta494ce12025-07-03 21:28:50 +0200365 vim_free(p);
John Marriott1be5b372025-06-23 19:57:29 +0200366
Bram Moolenaar66b51422019-08-18 21:44:12 +0200367 return OK;
368}
369
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000370/*
371 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 */
374 static int
375cmdline_pum_create(
376 cmdline_info_T *ccline,
377 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000378 char_u **matches,
379 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000380 int showtail)
381{
382 int i;
383 int columns;
384
385 // Add all the completion matches
John Marriott1be5b372025-06-23 19:57:29 +0200386 compl_match_array = ALLOC_MULT(pumitem_T, numMatches);
387 if (compl_match_array == NULL)
388 return EXPAND_UNSUCCESSFUL;
389
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000390 compl_match_arraysize = numMatches;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000391 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000392 {
zeertzjqc51a3762022-12-10 10:22:29 +0000393 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000394 compl_match_array[i].pum_info = NULL;
395 compl_match_array[i].pum_extra = NULL;
396 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200397 compl_match_array[i].pum_user_abbr_hlattr = -1;
398 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000399 }
400
401 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200402 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000403 columns = vim_strsize(xp->xp_pattern);
404 if (showtail)
405 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000406 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000407 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000408 }
glepnir977561a2025-02-13 20:48:56 +0100409 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000410
411 // no default selection
412 compl_selected = -1;
413
414 cmdline_pum_display();
415
416 return EXPAND_OK;
417}
418
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000419/*
420 * Display the cmdline completion matches in a popup menu
421 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000422 void
423cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424{
425 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
426}
427
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000428/*
429 * Returns TRUE if the cmdline completion popup menu is being displayed.
430 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000431 int
432cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000433{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100434 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435}
436
437/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000438 * Remove the cmdline completion popup menu (if present), free the list of
439 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000440 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000441 void
zeertzjq1830e782025-03-13 20:29:13 +0100442cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000443{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000444 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100445 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100446#ifdef FEAT_EVAL
447 int save_RedrawingDisabled = RedrawingDisabled;
448 if (cclp->input_fn)
449 RedrawingDisabled = 0;
450#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000451
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000452 pum_undisplay();
453 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200454 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000455 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000456 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000457 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000458 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100459
460 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
461 // as a side effect.
462 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100463#ifdef FEAT_EVAL
464 if (cclp->input_fn)
465 RedrawingDisabled = save_RedrawingDisabled;
466#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000467}
468
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000469 void
470cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000471{
zeertzjq1830e782025-03-13 20:29:13 +0100472 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000473 wildmenu_cleanup(cclp);
474}
475
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000476/*
477 * Returns the starting column number to use for the cmdline completion popup
478 * menu.
479 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000480 int
481cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000482{
483 return compl_startcol;
484}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000485
Bram Moolenaar66b51422019-08-18 21:44:12 +0200486/*
zeertzjqd8c93402024-06-17 18:25:32 +0200487 * Returns the current cmdline completion pattern.
488 */
489 char_u *
490cmdline_compl_pattern(void)
491{
492 expand_T *xp = get_cmdline_info()->xpc;
493
494 return xp == NULL ? NULL : xp->xp_orig;
495}
496
497/*
498 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
499 */
500 int
501cmdline_compl_is_fuzzy(void)
502{
503 expand_T *xp = get_cmdline_info()->xpc;
504
505 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
506}
507
508/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000509 * Return the number of characters that should be skipped in a status match.
Girish Palya6b49fba2025-06-28 19:47:34 +0200510 * These are backslashes used for escaping. Do show backslashes in help tags
511 * and in search pattern completion matches.
Bram Moolenaard6e91382022-11-12 17:44:13 +0000512 */
513 static int
514skip_status_match_char(expand_T *xp, char_u *s)
515{
Girish Palya6b49fba2025-06-28 19:47:34 +0200516 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP
517 && xp->xp_context != EXPAND_PATTERN_IN_BUF)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000518#ifdef FEAT_MENU
519 || ((xp->xp_context == EXPAND_MENUS
520 || xp->xp_context == EXPAND_MENUNAMES)
521 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
522#endif
523 )
524 {
525#ifndef BACKSLASH_IN_FILENAME
526 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
527 return 2;
528#endif
529 return 1;
530 }
531 return 0;
532}
533
534/*
535 * Get the length of an item as it will be shown in the status line.
536 */
537 static int
538status_match_len(expand_T *xp, char_u *s)
539{
540 int len = 0;
541
542#ifdef FEAT_MENU
543 int emenu = xp->xp_context == EXPAND_MENUS
544 || xp->xp_context == EXPAND_MENUNAMES;
545
546 // Check for menu separators - replace with '|'.
547 if (emenu && menu_is_separator(s))
548 return 1;
549#endif
550
551 while (*s != NUL)
552 {
553 s += skip_status_match_char(xp, s);
554 len += ptr2cells(s);
555 MB_PTR_ADV(s);
556 }
557
558 return len;
559}
560
561/*
562 * Show wildchar matches in the status line.
563 * Show at least the "match" item.
564 * We start at item 'first_match' in the list and show all matches that fit.
565 *
566 * If inversion is possible we use it. Else '=' characters are used.
567 */
568 static void
569win_redr_status_matches(
570 expand_T *xp,
571 int num_matches,
572 char_u **matches, // list of matches
573 int match,
574 int showtail)
575{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000576 int row;
577 char_u *buf;
578 int len;
579 int clen; // length in screen cells
580 int fillchar;
581 int attr;
582 int i;
583 int highlight = TRUE;
584 char_u *selstart = NULL;
585 int selstart_col = 0;
586 char_u *selend = NULL;
587 static int first_match = 0;
588 int add_left = FALSE;
589 char_u *s;
590#ifdef FEAT_MENU
591 int emenu;
592#endif
593 int l;
594
595 if (matches == NULL) // interrupted completion?
596 return;
597
598 if (has_mbyte)
599 buf = alloc(Columns * MB_MAXBYTES + 1);
600 else
601 buf = alloc(Columns + 1);
602 if (buf == NULL)
603 return;
604
605 if (match == -1) // don't show match but original text
606 {
607 match = 0;
608 highlight = FALSE;
609 }
610 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000611 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000612 if (match == 0)
613 first_match = 0;
614 else if (match < first_match)
615 {
616 // jumping left, as far as we can go
617 first_match = match;
618 add_left = TRUE;
619 }
620 else
621 {
622 // check if match fits on the screen
623 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000624 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000625 if (first_match > 0)
626 clen += 2;
627 // jumping right, put match at the left
628 if ((long)clen > Columns)
629 {
630 first_match = match;
631 // if showing the last match, we can add some on the left
632 clen = 2;
633 for (i = match; i < num_matches; ++i)
634 {
zeertzjqc51a3762022-12-10 10:22:29 +0000635 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000636 if ((long)clen >= Columns)
637 break;
638 }
639 if (i == num_matches)
640 add_left = TRUE;
641 }
642 }
643 if (add_left)
644 while (first_match > 0)
645 {
zeertzjqc51a3762022-12-10 10:22:29 +0000646 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000647 if ((long)clen >= Columns)
648 break;
649 --first_match;
650 }
651
652 fillchar = fillchar_status(&attr, curwin);
653
654 if (first_match == 0)
655 {
656 *buf = NUL;
657 len = 0;
658 }
659 else
660 {
661 STRCPY(buf, "< ");
662 len = 2;
663 }
664 clen = len;
665
666 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000667 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000668 {
669 if (i == match)
670 {
671 selstart = buf + len;
672 selstart_col = clen;
673 }
674
zeertzjqc51a3762022-12-10 10:22:29 +0000675 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000676 // Check for menu separators - replace with '|'
677#ifdef FEAT_MENU
678 emenu = (xp->xp_context == EXPAND_MENUS
679 || xp->xp_context == EXPAND_MENUNAMES);
680 if (emenu && menu_is_separator(s))
681 {
682 STRCPY(buf + len, transchar('|'));
683 l = (int)STRLEN(buf + len);
684 len += l;
685 clen += l;
686 }
687 else
688#endif
John Marriotta494ce12025-07-03 21:28:50 +0200689 for ( ; *s != NUL; ++s)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000690 {
691 s += skip_status_match_char(xp, s);
692 clen += ptr2cells(s);
693 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
694 {
695 STRNCPY(buf + len, s, l);
696 s += l - 1;
697 len += l;
698 }
699 else
700 {
701 STRCPY(buf + len, transchar_byte(*s));
702 len += (int)STRLEN(buf + len);
703 }
704 }
705 if (i == match)
706 selend = buf + len;
707
708 *(buf + len++) = ' ';
709 *(buf + len++) = ' ';
710 clen += 2;
711 if (++i == num_matches)
712 break;
713 }
714
715 if (i != num_matches)
716 {
717 *(buf + len++) = '>';
718 ++clen;
719 }
720
721 buf[len] = NUL;
722
723 row = cmdline_row - 1;
724 if (row >= 0)
725 {
726 if (wild_menu_showing == 0)
727 {
728 if (msg_scrolled > 0)
729 {
730 // Put the wildmenu just above the command line. If there is
731 // no room, scroll the screen one line up.
732 if (cmdline_row == Rows - 1)
733 {
734 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
735 ++msg_scrolled;
736 }
737 else
738 {
739 ++cmdline_row;
740 ++row;
741 }
742 wild_menu_showing = WM_SCROLLED;
743 }
744 else
745 {
746 // Create status line if needed by setting 'laststatus' to 2.
747 // Set 'winminheight' to zero to avoid that the window is
748 // resized.
749 if (lastwin->w_status_height == 0)
750 {
751 save_p_ls = p_ls;
752 save_p_wmh = p_wmh;
753 p_ls = 2;
754 p_wmh = 0;
755 last_status(FALSE);
756 }
757 wild_menu_showing = WM_SHOWN;
758 }
759 }
760
761 screen_puts(buf, row, 0, attr);
762 if (selstart != NULL && highlight)
763 {
764 *selend = NUL;
765 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
766 }
767
768 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
769 }
770
771 win_redraw_last_status(topframe);
772 vim_free(buf);
773}
774
775/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000776 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200777 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000778 */
779 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100780get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781{
glepnir977561a2025-02-13 20:48:56 +0100782 int findex = xp->xp_selected;
783 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000784
zeertzjqb6c900b2025-02-14 17:59:31 +0100785 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000786 if (xp->xp_numfiles <= 0)
787 return NULL;
788
789 if (mode == WILD_PREV)
790 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100791 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000792 if (findex == -1)
793 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100794 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795 --findex;
796 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000797 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000798 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100799 // Select the next entry
800 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000801 }
glepnir977561a2025-02-13 20:48:56 +0100802 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000803 {
glepnir977561a2025-02-13 20:48:56 +0100804 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
805 ht = pum_get_height();
806 if (ht > 3)
807 ht -= 2;
808
809 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000810 {
glepnir977561a2025-02-13 20:48:56 +0100811 if (findex == 0)
812 // at the first entry, don't select any entries
813 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100814 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100815 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000816 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100817 else
818 // go up by the pum height
819 findex = MAX(findex - ht, 0);
820 }
821 else // mode == WILD_PAGEDOWN
822 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100823 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100824 // at the last entry, don't select any entries
825 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100826 else if (findex < 0)
827 // no entry is selected, select the first entry
828 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100829 else
830 // go down by the pum height
831 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000832 }
833 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000834
glepnir977561a2025-02-13 20:48:56 +0100835 // Handle wrapping around
836 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000837 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100838 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100839 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000840 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000841 else
glepnir977561a2025-02-13 20:48:56 +0100842 // Wrap around to opposite end
843 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000844 }
glepnir977561a2025-02-13 20:48:56 +0100845
846 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000847 if (compl_match_array)
848 {
849 compl_selected = findex;
850 cmdline_pum_display();
851 }
852 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100853 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
854 cmd_showtail);
855
zeertzjqe9ef3472023-08-17 23:57:05 +0200856 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100857 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100858 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000859}
860
861/*
862 * Start the command-line expansion and get the matches.
863 */
864 static char_u *
865ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
866{
867 int non_suf_match; // number without matching suffix
868 int i;
869 char_u *ss = NULL;
870
871 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000872 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000873 options) == FAIL)
874 {
875#ifdef FNAME_ILLEGAL
876 // Illegal file name has been silently skipped. But when there
877 // are wildcards, the real problem is that there was no match,
878 // causing the pattern to be added, which has illegal characters.
879 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
880 semsg(_(e_no_match_str_2), str);
881#endif
882 }
883 else if (xp->xp_numfiles == 0)
884 {
885 if (!(options & WILD_SILENT))
886 semsg(_(e_no_match_str_2), str);
887 }
888 else
889 {
890 // Escape the matches for use on the command line.
891 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
892
893 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000894 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000895 {
896 if (xp->xp_numfiles)
897 non_suf_match = xp->xp_numfiles;
898 else
899 non_suf_match = 1;
900 if ((xp->xp_context == EXPAND_FILES
901 || xp->xp_context == EXPAND_DIRECTORIES)
902 && xp->xp_numfiles > 1)
903 {
904 // More than one match; check suffix.
905 // The files will have been sorted on matching suffix in
906 // expand_wildcards, only need to check the first two.
907 non_suf_match = 0;
908 for (i = 0; i < 2; ++i)
909 if (match_suffix(xp->xp_files[i]))
910 ++non_suf_match;
911 }
912 if (non_suf_match != 1)
913 {
914 // Can we ever get here unless it's while expanding
915 // interactively? If not, we can get rid of this all
916 // together. Don't really want to wait for this message
917 // (and possibly have to hit return to continue!).
918 if (!(options & WILD_SILENT))
919 emsg(_(e_too_many_file_names));
920 else if (!(options & WILD_NO_BEEP))
921 beep_flush();
922 }
923 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
924 ss = vim_strsave(xp->xp_files[0]);
925 }
926 }
927
928 return ss;
929}
930
931/*
932 * Return the longest common part in the list of cmdline completion matches.
933 */
934 static char_u *
935find_longest_match(expand_T *xp, int options)
936{
937 long_u len;
938 int mb_len = 1;
939 int c0, ci;
940 int i;
941 char_u *ss;
942
943 for (len = 0; xp->xp_files[0][len]; len += mb_len)
944 {
945 if (has_mbyte)
946 {
947 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
John Marriotta494ce12025-07-03 21:28:50 +0200948 c0 = (*mb_ptr2char)(&xp->xp_files[0][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000949 }
950 else
951 c0 = xp->xp_files[0][len];
952 for (i = 1; i < xp->xp_numfiles; ++i)
953 {
954 if (has_mbyte)
John Marriotta494ce12025-07-03 21:28:50 +0200955 ci = (*mb_ptr2char)(&xp->xp_files[i][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000956 else
957 ci = xp->xp_files[i][len];
958 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
959 || xp->xp_context == EXPAND_FILES
960 || xp->xp_context == EXPAND_SHELLCMD
961 || xp->xp_context == EXPAND_BUFFERS))
962 {
963 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
964 break;
965 }
966 else if (c0 != ci)
967 break;
968 }
969 if (i < xp->xp_numfiles)
970 {
971 if (!(options & WILD_NO_BEEP))
972 vim_beep(BO_WILD);
973 break;
974 }
975 }
976
977 ss = alloc(len + 1);
978 if (ss)
979 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
980
981 return ss;
982}
983
984/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000985 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 * Chars that should not be expanded must be preceded with a backslash.
987 * Return a pointer to allocated memory containing the new string.
988 * Return NULL for failure.
989 *
990 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200991 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
992 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200993 *
994 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
995 * is WILD_EXPAND_FREE or WILD_ALL.
996 *
997 * mode = WILD_FREE: just free previously expanded matches
998 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
999 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
1000 * mode = WILD_NEXT: use next match in multiple match, wrap to first
1001 * mode = WILD_PREV: use previous match in multiple match, wrap to first
1002 * mode = WILD_ALL: return all matches concatenated
1003 * mode = WILD_LONGEST: return longest matched part
1004 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001005 * mode = WILD_APPLY: apply the item selected in the cmdline completion
1006 * popup menu and close the menu.
1007 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
1008 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 *
1010 * options = WILD_LIST_NOTFOUND: list entries without a match
1011 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
1012 * options = WILD_USE_NL: Use '\n' for WILD_ALL
1013 * options = WILD_NO_BEEP: Don't beep for multiple matches
1014 * options = WILD_ADD_SLASH: add a slash after directory names
1015 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
1016 * options = WILD_SILENT: don't print warning messages
1017 * options = WILD_ESCAPE: put backslash before special chars
1018 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001019 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001020 *
1021 * The variables xp->xp_context and xp->xp_backslash must have been set!
1022 */
1023 char_u *
1024ExpandOne(
1025 expand_T *xp,
1026 char_u *str,
1027 char_u *orig, // allocated copy of original of expanded string
1028 int options,
1029 int mode)
1030{
1031 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 int orig_saved = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033
1034 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001035 if (mode == WILD_NEXT || mode == WILD_PREV
1036 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001037 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001039 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001040 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001041 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001042 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001043 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001044 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001045
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 // free old names
1047 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1048 {
1049 FreeWild(xp->xp_numfiles, xp->xp_files);
1050 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001051 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001052
1053 // The entries from xp_files may be used in the PUM, remove it.
1054 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001055 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001056 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001057 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001058
1059 if (mode == WILD_FREE) // only release file name
1060 return NULL;
1061
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001062 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001063 {
zeertzjq28a23602023-09-29 19:58:35 +02001064 vim_free(xp->xp_orig);
1065 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001066 orig_saved = TRUE;
1067
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001068 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001069 }
1070
1071 // Find longest common part
1072 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1073 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001074 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001075 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 }
1077
Bram Moolenaar57e95172022-08-20 19:26:14 +01001078 // Concatenate all matching names. Unless interrupted, this can be slow
1079 // and the result probably won't be used.
1080 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001081 {
John Marriotta494ce12025-07-03 21:28:50 +02001082 size_t ss_size = 0;
1083 char *prefix = "";
1084 char *suffix = (options & WILD_USE_NL) ? "\n" : " ";
1085 int n = xp->xp_numfiles - 1;
1086 int i;
1087
1088 if (xp->xp_prefix == XP_PREFIX_NO)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001089 {
John Marriotta494ce12025-07-03 21:28:50 +02001090 prefix = "no";
1091 ss_size = STRLEN_LITERAL("no") * n;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001092 }
John Marriotta494ce12025-07-03 21:28:50 +02001093 else if (xp->xp_prefix == XP_PREFIX_INV)
1094 {
1095 prefix = "inv";
1096 ss_size = STRLEN_LITERAL("inv") * n;
1097 }
1098
1099 for (i = 0; i < xp->xp_numfiles; ++i)
1100 ss_size += STRLEN(xp->xp_files[i]) + 1; // +1 for the suffix
1101 ++ss_size; // +1 for the NUL
1102
1103 ss = alloc(ss_size);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001104 if (ss != NULL)
1105 {
John Marriotta494ce12025-07-03 21:28:50 +02001106 size_t ss_len = 0;
1107
Bram Moolenaar66b51422019-08-18 21:44:12 +02001108 for (i = 0; i < xp->xp_numfiles; ++i)
1109 {
John Marriotta494ce12025-07-03 21:28:50 +02001110 ss_len += vim_snprintf_safelen(
1111 (char *)ss + ss_len,
1112 ss_size - ss_len,
1113 "%s%s%s",
1114 (i > 0) ? prefix : "",
1115 (char *)xp->xp_files[i],
1116 (i < n) ? suffix : "");
Bram Moolenaar66b51422019-08-18 21:44:12 +02001117 }
1118 }
1119 }
1120
1121 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1122 ExpandCleanup(xp);
1123
zeertzjq28a23602023-09-29 19:58:35 +02001124 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001125 if (!orig_saved)
1126 vim_free(orig);
1127
1128 return ss;
1129}
1130
1131/*
1132 * Prepare an expand structure for use.
1133 */
1134 void
1135ExpandInit(expand_T *xp)
1136{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001137 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001138 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001139 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001140 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001141}
1142
1143/*
1144 * Cleanup an expand structure after use.
1145 */
1146 void
1147ExpandCleanup(expand_T *xp)
1148{
1149 if (xp->xp_numfiles >= 0)
1150 {
1151 FreeWild(xp->xp_numfiles, xp->xp_files);
1152 xp->xp_numfiles = -1;
1153 }
zeertzjq28a23602023-09-29 19:58:35 +02001154 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001155}
1156
zeertzjqec270a52025-04-23 20:50:23 +02001157 void
1158clear_cmdline_orig(void)
1159{
1160 VIM_CLEAR(cmdline_orig);
1161}
1162
Bram Moolenaar66b51422019-08-18 21:44:12 +02001163/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001164 * Display one line of completion matches. Multiple matches are displayed in
1165 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001166 * matches - list of completion match names
1167 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001168 * lines - number of output lines
1169 * linenr - line number of matches to display
1170 * maxlen - maximum number of characters in each line
1171 * showtail - display only the tail of the full path of a file name
1172 * dir_attr - highlight attribute to use for directory names
1173 */
1174 static void
1175showmatches_oneline(
1176 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 char_u **matches,
1178 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001179 int lines,
1180 int linenr,
1181 int maxlen,
1182 int showtail,
1183 int dir_attr)
1184{
1185 int i, j;
1186 int isdir;
1187 int lastlen;
1188 char_u *p;
1189
1190 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001192 {
1193 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1194 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001195 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1196 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001197 msg_advance(maxlen + 1);
1198 msg_puts((char *)p);
1199 msg_advance(maxlen + 3);
1200 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1201 break;
1202 }
1203 for (i = maxlen - lastlen; --i >= 0; )
1204 msg_putchar(' ');
1205 if (xp->xp_context == EXPAND_FILES
1206 || xp->xp_context == EXPAND_SHELLCMD
1207 || xp->xp_context == EXPAND_BUFFERS)
1208 {
1209 // highlight directories
1210 if (xp->xp_numfiles != -1)
1211 {
1212 char_u *halved_slash;
1213 char_u *exp_path;
1214 char_u *path;
1215
1216 // Expansion was done before and special characters
1217 // were escaped, need to halve backslashes. Also
1218 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 exp_path = expand_env_save_opt(matches[j], TRUE);
1220 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001221 halved_slash = backslash_halve_save(path);
1222 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001223 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001224 vim_free(exp_path);
1225 if (halved_slash != path)
1226 vim_free(halved_slash);
1227 }
1228 else
1229 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001230 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001231 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001232 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001233 else
1234 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001236 TRUE);
1237 p = NameBuff;
1238 }
1239 }
1240 else
1241 {
1242 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001243 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001244 }
1245 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1246 }
1247 if (msg_col > 0) // when not wrapped around
1248 {
1249 msg_clr_eos();
1250 msg_putchar('\n');
1251 }
1252 out_flush(); // show one line at a time
1253}
1254
1255/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001256 * Show all matches for completion on the command line.
1257 * Returns EXPAND_NOTHING when the character that triggered expansion should
1258 * be inserted like a normal character.
1259 */
1260 int
1261showmatches(expand_T *xp, int wildmenu UNUSED)
1262{
1263 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001264 int numMatches;
1265 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001266 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001267 int maxlen;
1268 int lines;
1269 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 int attr;
1271 int showtail;
1272
Girish Palya92f68e22025-04-21 11:12:41 +02001273 // Save cmdline before expansion
1274 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001275 {
1276 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001277 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001278 }
Girish Palya92f68e22025-04-21 11:12:41 +02001279
Bram Moolenaar66b51422019-08-18 21:44:12 +02001280 if (xp->xp_numfiles == -1)
1281 {
1282 set_expand_context(xp);
1283 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001284 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001285 showtail = expand_showtail(xp);
1286 if (i != EXPAND_OK)
1287 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001288 }
1289 else
1290 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001291 numMatches = xp->xp_numfiles;
1292 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001293 showtail = cmd_showtail;
1294 }
1295
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001296 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001297 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001298 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001299
Bram Moolenaar66b51422019-08-18 21:44:12 +02001300 if (!wildmenu)
1301 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001302 msg_didany = FALSE; // lines_left will be set
1303 msg_start(); // prepare for paging
1304 msg_putchar('\n');
1305 out_flush();
1306 cmdline_row = msg_row;
1307 msg_didany = FALSE; // lines_left will be set again
1308 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001309 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001310
1311 if (got_int)
1312 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001313 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001314 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001315 else
1316 {
1317 // find the length of the longest file name
1318 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001319 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001320 {
1321 if (!showtail && (xp->xp_context == EXPAND_FILES
1322 || xp->xp_context == EXPAND_SHELLCMD
1323 || xp->xp_context == EXPAND_BUFFERS))
1324 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001325 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001326 j = vim_strsize(NameBuff);
1327 }
1328 else
zeertzjqc51a3762022-12-10 10:22:29 +00001329 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001330 if (j > maxlen)
1331 maxlen = j;
1332 }
1333
1334 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001335 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001336 else
1337 {
1338 // compute the number of columns and lines for the listing
1339 maxlen += 2; // two spaces between file names
1340 columns = ((int)Columns + 2) / maxlen;
1341 if (columns < 1)
1342 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001343 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001344 }
1345
1346 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1347
1348 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1349 {
1350 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1351 msg_clr_eos();
1352 msg_advance(maxlen - 3);
1353 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1354 }
1355
1356 // list the files line by line
1357 for (i = 0; i < lines; ++i)
1358 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001359 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001360 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001361 if (got_int)
1362 {
1363 got_int = FALSE;
1364 break;
1365 }
1366 }
1367
1368 // we redraw the command below the lines that we have just listed
1369 // This is a bit tricky, but it saves a lot of screen updating.
1370 cmdline_row = msg_row; // will put it back later
1371 }
1372
1373 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001374 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001375
1376 return EXPAND_OK;
1377}
1378
1379/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001380 * gettail() version for showmatches() and win_redr_status_matches():
1381 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001382 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001383 static char_u *
1384showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001385{
1386 char_u *p;
1387 char_u *t = s;
1388 int had_sep = FALSE;
1389
1390 for (p = s; *p != NUL; )
1391 {
1392 if (vim_ispathsep(*p)
1393#ifdef BACKSLASH_IN_FILENAME
1394 && !rem_backslash(p)
1395#endif
1396 )
1397 had_sep = TRUE;
1398 else if (had_sep)
1399 {
1400 t = p;
1401 had_sep = FALSE;
1402 }
1403 MB_PTR_ADV(p);
1404 }
1405 return t;
1406}
1407
1408/*
1409 * Return TRUE if we only need to show the tail of completion matches.
1410 * When not completing file names or there is a wildcard in the path FALSE is
1411 * returned.
1412 */
1413 static int
1414expand_showtail(expand_T *xp)
1415{
1416 char_u *s;
1417 char_u *end;
1418
1419 // When not completing file names a "/" may mean something different.
1420 if (xp->xp_context != EXPAND_FILES
1421 && xp->xp_context != EXPAND_SHELLCMD
1422 && xp->xp_context != EXPAND_DIRECTORIES)
1423 return FALSE;
1424
1425 end = gettail(xp->xp_pattern);
1426 if (end == xp->xp_pattern) // there is no path separator
1427 return FALSE;
1428
1429 for (s = xp->xp_pattern; s < end; s++)
1430 {
1431 // Skip escaped wildcards. Only when the backslash is not a path
1432 // separator, on DOS the '*' "path\*\file" must not be skipped.
1433 if (rem_backslash(s))
1434 ++s;
1435 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1436 return FALSE;
1437 }
1438 return TRUE;
1439}
1440
1441/*
1442 * Prepare a string for expansion.
1443 * When expanding file names: The string will be used with expand_wildcards().
1444 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1445 * When expanding other names: The string will be used with regcomp(). Copy
1446 * the name into allocated memory and prepend "^".
1447 */
1448 char_u *
1449addstar(
1450 char_u *fname,
1451 int len,
1452 int context) // EXPAND_FILES etc.
1453{
1454 char_u *retval;
1455 int i, j;
1456 int new_len;
1457 char_u *tail;
1458 int ends_in_star;
1459
1460 if (context != EXPAND_FILES
1461 && context != EXPAND_FILES_IN_PATH
1462 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001463 && context != EXPAND_DIRECTORIES
1464 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001465 {
1466 // Matching will be done internally (on something other than files).
1467 // So we convert the file-matching-type wildcards into our kind for
1468 // use with vim_regcomp(). First work out how long it will be:
1469
1470 // For help tags the translation is done in find_help_tags().
1471 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001472 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001473 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001474 || context == EXPAND_COLORS
1475 || context == EXPAND_COMPILER
1476 || context == EXPAND_OWNSYNTAX
1477 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001478 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001479 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001480 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001481 || ((context == EXPAND_TAGS_LISTFILES
1482 || context == EXPAND_TAGS)
1483 && fname[0] == '/'))
1484 retval = vim_strnsave(fname, len);
1485 else
1486 {
1487 new_len = len + 2; // +2 for '^' at start, NUL at end
1488 for (i = 0; i < len; i++)
1489 {
1490 if (fname[i] == '*' || fname[i] == '~')
1491 new_len++; // '*' needs to be replaced by ".*"
1492 // '~' needs to be replaced by "\~"
1493
1494 // Buffer names are like file names. "." should be literal
1495 if (context == EXPAND_BUFFERS && fname[i] == '.')
1496 new_len++; // "." becomes "\."
1497
1498 // Custom expansion takes care of special things, match
1499 // backslashes literally (perhaps also for other types?)
1500 if ((context == EXPAND_USER_DEFINED
1501 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1502 new_len++; // '\' becomes "\\"
1503 }
1504 retval = alloc(new_len);
1505 if (retval != NULL)
1506 {
1507 retval[0] = '^';
1508 j = 1;
1509 for (i = 0; i < len; i++, j++)
1510 {
1511 // Skip backslash. But why? At least keep it for custom
1512 // expansion.
1513 if (context != EXPAND_USER_DEFINED
1514 && context != EXPAND_USER_LIST
1515 && fname[i] == '\\'
1516 && ++i == len)
1517 break;
1518
1519 switch (fname[i])
1520 {
1521 case '*': retval[j++] = '.';
1522 break;
1523 case '~': retval[j++] = '\\';
1524 break;
1525 case '?': retval[j] = '.';
1526 continue;
1527 case '.': if (context == EXPAND_BUFFERS)
1528 retval[j++] = '\\';
1529 break;
1530 case '\\': if (context == EXPAND_USER_DEFINED
1531 || context == EXPAND_USER_LIST)
1532 retval[j++] = '\\';
1533 break;
1534 }
1535 retval[j] = fname[i];
1536 }
1537 retval[j] = NUL;
1538 }
1539 }
1540 }
1541 else
1542 {
1543 retval = alloc(len + 4);
1544 if (retval != NULL)
1545 {
1546 vim_strncpy(retval, fname, len);
1547
1548 // Don't add a star to *, ~, ~user, $var or `cmd`.
1549 // * would become **, which walks the whole tree.
1550 // ~ would be at the start of the file name, but not the tail.
1551 // $ could be anywhere in the tail.
1552 // ` could be anywhere in the file name.
1553 // When the name ends in '$' don't add a star, remove the '$'.
1554 tail = gettail(retval);
1555 ends_in_star = (len > 0 && retval[len - 1] == '*');
1556#ifndef BACKSLASH_IN_FILENAME
1557 for (i = len - 2; i >= 0; --i)
1558 {
1559 if (retval[i] != '\\')
1560 break;
1561 ends_in_star = !ends_in_star;
1562 }
1563#endif
1564 if ((*retval != '~' || tail != retval)
1565 && !ends_in_star
1566 && vim_strchr(tail, '$') == NULL
1567 && vim_strchr(retval, '`') == NULL)
1568 retval[len++] = '*';
1569 else if (len > 0 && retval[len - 1] == '$')
1570 --len;
1571 retval[len] = NUL;
1572 }
1573 }
1574 return retval;
1575}
1576
1577/*
1578 * Must parse the command line so far to work out what context we are in.
1579 * Completion can then be done based on that context.
1580 * This routine sets the variables:
1581 * xp->xp_pattern The start of the pattern to be expanded within
1582 * the command line (ends at the cursor).
1583 * xp->xp_context The type of thing to expand. Will be one of:
1584 *
1585 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1586 * the command line, like an unknown command. Caller
1587 * should beep.
1588 * EXPAND_NOTHING Unrecognised context for completion, use char like
1589 * a normal char, rather than for completion. eg
1590 * :s/^I/
1591 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1592 * it.
1593 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1594 * EXPAND_FILES After command with EX_XFILE set, or after setting
1595 * with P_EXPAND set. eg :e ^I, :w>>^I
1596 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001597 * when we know only directories are of interest.
1598 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001599 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1600 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1601 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1602 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1603 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1604 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1605 * EXPAND_EVENTS Complete event names
1606 * EXPAND_SYNTAX Complete :syntax command arguments
1607 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1608 * EXPAND_AUGROUP Complete autocommand group names
1609 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1610 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1611 * eg :unmap a^I , :cunab x^I
1612 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1613 * eg :call sub^I
1614 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1615 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1616 * names in expressions, eg :while s^I
1617 * EXPAND_ENV_VARS Complete environment variable names
1618 * EXPAND_USER Complete user names
Girish Palya6b49fba2025-06-28 19:47:34 +02001619 * EXPAND_PATTERN_IN_BUF Complete pattern in '/', '?', ':s', ':g', etc.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001620 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001621 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001622set_expand_context(expand_T *xp)
1623{
Girish Palya6b49fba2025-06-28 19:47:34 +02001624 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001625
Girish Palya6b49fba2025-06-28 19:47:34 +02001626 // Handle search commands: '/' or '?'
1627 if ((ccline->cmdfirstc == '/' || ccline->cmdfirstc == '?')
1628 && may_expand_pattern)
1629 {
1630 xp->xp_context = EXPAND_PATTERN_IN_BUF;
1631 xp->xp_search_dir = (ccline->cmdfirstc == '/') ? FORWARD : BACKWARD;
1632 xp->xp_pattern = ccline->cmdbuff;
1633 xp->xp_pattern_len = ccline->cmdpos;
1634#ifdef FEAT_SEARCH_EXTRA
1635 search_first_line = 0; // Search entire buffer
1636#endif
1637 return;
1638 }
1639
1640 // Only handle ':', '>', or '=' command-lines, or expression input
Bram Moolenaar66b51422019-08-18 21:44:12 +02001641 if (ccline->cmdfirstc != ':'
1642#ifdef FEAT_EVAL
1643 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1644 && !ccline->input_fn
1645#endif
Girish Palya6b49fba2025-06-28 19:47:34 +02001646 )
Bram Moolenaar66b51422019-08-18 21:44:12 +02001647 {
1648 xp->xp_context = EXPAND_NOTHING;
1649 return;
1650 }
Girish Palya6b49fba2025-06-28 19:47:34 +02001651
1652 // Fallback to command-line expansion
Bram Moolenaar66b51422019-08-18 21:44:12 +02001653 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1654}
1655
Bram Moolenaard0190392019-08-23 21:17:35 +02001656/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001657 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1658 * For user defined commands, the completion context is set in 'xp' and the
1659 * completion flags in 'complp'.
1660 *
1661 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 */
1663 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001664set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001665{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 char_u *p = NULL;
1667 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001668 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001669
1670 // Isolate the command and search for it in the command table.
1671 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001672 // - the 'k' command can directly be followed by any character, but do
1673 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1674 // find matches anywhere in the command name, do this only for command
1675 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001676 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001677 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001680 p = cmd + 1;
1681 }
1682 else
1683 {
1684 p = cmd;
1685 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1686 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001687 // A user command may contain digits.
1688 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1689 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001690 while (ASCII_ISALNUM(*p) || *p == '*')
1691 ++p;
1692 // for python 3.x: ":py3*" commands completion
1693 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1694 {
1695 ++p;
1696 while (ASCII_ISALPHA(*p) || *p == '*')
1697 ++p;
1698 }
1699 // check for non-alpha command
1700 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1701 ++p;
1702 len = (int)(p - cmd);
1703
1704 if (len == 0)
1705 {
1706 xp->xp_context = EXPAND_UNSUCCESSFUL;
1707 return NULL;
1708 }
1709
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001710 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001711
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001712 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001713 // Also when doing fuzzy expansion for non-shell commands, support
1714 // alphanumeric characters.
1715 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1716 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1718 ++p;
1719 }
1720
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001721 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001722 // character, complete the command name.
1723 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1724 return NULL;
1725
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001726 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001727 {
1728 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1729 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001731 p = cmd + 1;
1732 }
1733 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1734 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001735 eap->cmd = cmd;
1736 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001737 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001738 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 }
1740 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001741 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 {
1743 // Not still touching the command and it was an illegal one
1744 xp->xp_context = EXPAND_UNSUCCESSFUL;
1745 return NULL;
1746 }
1747
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001748 return p;
1749}
Bram Moolenaard0190392019-08-23 21:17:35 +02001750
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751/*
1752 * Set the completion context for a command argument with wild card characters.
1753 */
1754 static void
1755set_context_for_wildcard_arg(
1756 exarg_T *eap,
1757 char_u *arg,
1758 int usefilter,
1759 expand_T *xp,
1760 int *complp)
1761{
1762 char_u *p;
1763 int c;
1764 int in_quote = FALSE;
1765 char_u *bow = NULL; // Beginning of word
1766 int len = 0;
1767
1768 // Allow spaces within back-quotes to count as part of the argument
1769 // being expanded.
1770 xp->xp_pattern = skipwhite(arg);
1771 p = xp->xp_pattern;
1772 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001773 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001774 if (has_mbyte)
1775 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001776 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001777 c = *p;
1778 if (c == '\\' && p[1] != NUL)
1779 ++p;
1780 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001781 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001782 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001783 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001784 xp->xp_pattern = p;
1785 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001786 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001787 in_quote = !in_quote;
1788 }
1789 // An argument can contain just about everything, except
1790 // characters that end the command and white space.
1791 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001792#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001793 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001794#endif
1795 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001796 {
1797 len = 0; // avoid getting stuck when space is in 'isfname'
1798 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001799 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001800 if (has_mbyte)
1801 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001802 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001803 c = *p;
1804 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001805 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001806 if (has_mbyte)
1807 len = (*mb_ptr2len)(p);
1808 else
1809 len = 1;
1810 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001811 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001812 if (in_quote)
1813 bow = p;
1814 else
1815 xp->xp_pattern = p;
1816 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001817 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001818 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001819 }
1820
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001821 // If we are still inside the quotes, and we passed a space, just
1822 // expand from there.
1823 if (bow != NULL && in_quote)
1824 xp->xp_pattern = bow;
1825 xp->xp_context = EXPAND_FILES;
1826
1827 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001828 if (usefilter
1829 || (eap != NULL
1830 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1831 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001832 {
1833#ifndef BACKSLASH_IN_FILENAME
1834 xp->xp_shell = TRUE;
1835#endif
1836 // When still after the command name expand executables.
1837 if (xp->xp_pattern == skipwhite(arg))
1838 xp->xp_context = EXPAND_SHELLCMD;
1839 }
1840
1841 // Check for environment variable.
1842 if (*xp->xp_pattern == '$')
1843 {
1844 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1845 if (!vim_isIDc(*p))
1846 break;
1847 if (*p == NUL)
1848 {
1849 xp->xp_context = EXPAND_ENV_VARS;
1850 ++xp->xp_pattern;
1851 // Avoid that the assignment uses EXPAND_FILES again.
1852 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1853 *complp = EXPAND_ENV_VARS;
1854 }
1855 }
1856 // Check for user names.
1857 if (*xp->xp_pattern == '~')
1858 {
1859 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1860 ;
1861 // Complete ~user only if it partially matches a user name.
1862 // A full match ~user<Tab> will be replaced by user's home
1863 // directory i.e. something like ~user<Tab> -> /home/user/
1864 if (*p == NUL && p > xp->xp_pattern + 1
1865 && match_user(xp->xp_pattern + 1) >= 1)
1866 {
1867 xp->xp_context = EXPAND_USER;
1868 ++xp->xp_pattern;
1869 }
1870 }
1871}
1872
1873/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001874 * Set the completion context for the "++opt=arg" argument. Always returns
1875 * NULL.
1876 */
1877 static char_u *
1878set_context_in_argopt(expand_T *xp, char_u *arg)
1879{
1880 char_u *p;
1881
1882 p = vim_strchr(arg, '=');
1883 if (p == NULL)
1884 xp->xp_pattern = arg;
1885 else
1886 xp->xp_pattern = p + 1;
1887
1888 xp->xp_context = EXPAND_ARGOPT;
1889 return NULL;
1890}
1891
1892#ifdef FEAT_TERMINAL
1893/*
1894 * Set the completion context for :terminal's [options]. Always returns NULL.
1895 */
1896 static char_u *
1897set_context_in_terminalopt(expand_T *xp, char_u *arg)
1898{
1899 char_u *p;
1900
1901 p = vim_strchr(arg, '=');
1902 if (p == NULL)
1903 xp->xp_pattern = arg;
1904 else
1905 xp->xp_pattern = p + 1;
1906
1907 xp->xp_context = EXPAND_TERMINALOPT;
1908 return NULL;
1909}
1910#endif
1911
1912/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001913 * Set the completion context for the :filter command. Returns a pointer to the
1914 * next command after the :filter command.
1915 */
1916 static char_u *
1917set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1918{
1919 if (*arg != NUL)
1920 arg = skip_vimgrep_pat(arg, NULL, NULL);
1921 if (arg == NULL || *arg == NUL)
1922 {
1923 xp->xp_context = EXPAND_NOTHING;
1924 return NULL;
1925 }
1926 return skipwhite(arg);
1927}
1928
1929#ifdef FEAT_SEARCH_EXTRA
1930/*
1931 * Set the completion context for the :match command. Returns a pointer to the
1932 * next command after the :match command.
1933 */
1934 static char_u *
1935set_context_in_match_cmd(expand_T *xp, char_u *arg)
1936{
1937 if (*arg == NUL || !ends_excmd(*arg))
1938 {
1939 // also complete "None"
1940 set_context_in_echohl_cmd(xp, arg);
1941 arg = skipwhite(skiptowhite(arg));
1942 if (*arg != NUL)
1943 {
1944 xp->xp_context = EXPAND_NOTHING;
1945 arg = skip_regexp(arg + 1, *arg, magic_isset());
1946 }
1947 }
1948 return find_nextcmd(arg);
1949}
1950#endif
1951
1952/*
1953 * Returns a pointer to the next command after a :global or a :v command.
1954 * Returns NULL if there is no next command.
1955 */
1956 static char_u *
1957find_cmd_after_global_cmd(char_u *arg)
1958{
1959 int delim;
1960
1961 delim = *arg; // get the delimiter
1962 if (delim)
1963 ++arg; // skip delimiter if there is one
1964
1965 while (arg[0] != NUL && arg[0] != delim)
1966 {
1967 if (arg[0] == '\\' && arg[1] != NUL)
1968 ++arg;
1969 ++arg;
1970 }
1971 if (arg[0] != NUL)
1972 return arg + 1;
1973
1974 return NULL;
1975}
1976
1977/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001978 * Returns a pointer to the next command after a :substitute or a :& command.
1979 * Returns NULL if there is no next command.
1980 */
1981 static char_u *
1982find_cmd_after_substitute_cmd(char_u *arg)
1983{
1984 int delim;
1985
1986 delim = *arg;
1987 if (delim)
1988 {
1989 // skip "from" part
1990 ++arg;
1991 arg = skip_regexp(arg, delim, magic_isset());
1992
1993 if (arg[0] != NUL && arg[0] == delim)
1994 {
1995 // skip "to" part
1996 ++arg;
1997 while (arg[0] != NUL && arg[0] != delim)
1998 {
1999 if (arg[0] == '\\' && arg[1] != NUL)
2000 ++arg;
2001 ++arg;
2002 }
2003 if (arg[0] != NUL) // skip delimiter
2004 ++arg;
2005 }
2006 }
2007 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
2008 ++arg;
2009 if (arg[0] != NUL)
2010 return arg;
2011
2012 return NULL;
2013}
2014
2015/*
2016 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
2017 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
2018 * Returns NULL if there is no next command.
2019 */
2020 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002021find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002022{
2023 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002024 if (*arg != '/')
2025 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002026
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002027 // Match regexp, not just whole words
2028 for (++arg; *arg && *arg != '/'; arg++)
2029 if (*arg == '\\' && arg[1] != NUL)
2030 arg++;
2031 if (*arg)
2032 {
2033 arg = skipwhite(arg + 1);
2034
2035 // Check for trailing illegal characters
2036 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
2037 xp->xp_context = EXPAND_NOTHING;
2038 else
2039 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002040 }
2041
2042 return NULL;
2043}
2044
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002045#ifdef FEAT_EVAL
2046/*
2047 * Set the completion context for the :unlet command. Always returns NULL.
2048 */
2049 static char_u *
2050set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2051{
2052 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2053 arg = xp->xp_pattern + 1;
2054
2055 xp->xp_context = EXPAND_USER_VARS;
2056 xp->xp_pattern = arg;
2057
2058 if (*xp->xp_pattern == '$')
2059 {
2060 xp->xp_context = EXPAND_ENV_VARS;
2061 ++xp->xp_pattern;
2062 }
2063
2064 return NULL;
2065}
2066#endif
2067
2068#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2069/*
2070 * Set the completion context for the :language command. Always returns NULL.
2071 */
2072 static char_u *
2073set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2074{
2075 char_u *p;
2076
2077 p = skiptowhite(arg);
2078 if (*p == NUL)
2079 {
2080 xp->xp_context = EXPAND_LANGUAGE;
2081 xp->xp_pattern = arg;
2082 }
2083 else
2084 {
2085 if ( STRNCMP(arg, "messages", p - arg) == 0
2086 || STRNCMP(arg, "ctype", p - arg) == 0
2087 || STRNCMP(arg, "time", p - arg) == 0
2088 || STRNCMP(arg, "collate", p - arg) == 0)
2089 {
2090 xp->xp_context = EXPAND_LOCALES;
2091 xp->xp_pattern = skipwhite(p);
2092 }
2093 else
2094 xp->xp_context = EXPAND_NOTHING;
2095 }
2096
2097 return NULL;
2098}
2099#endif
2100
Christian Brabandta3422aa2025-04-23 21:04:24 +02002101static enum
2102{
2103 EXP_FILETYPECMD_ALL, // expand all :filetype values
2104 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2105 EXP_FILETYPECMD_INDENT, // expand indent on off
2106 EXP_FILETYPECMD_ONOFF, // expand on off
2107} filetype_expand_what;
2108
2109#define EXPAND_FILETYPECMD_PLUGIN 0x01
2110#define EXPAND_FILETYPECMD_INDENT 0x02
2111#define EXPAND_FILETYPECMD_ONOFF 0x04
2112
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002113#ifdef FEAT_EVAL
2114static enum
2115{
2116 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002117 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2118 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002119} breakpt_expand_what;
2120
2121/*
2122 * Set the completion context for the :breakadd command. Always returns NULL.
2123 */
2124 static char_u *
2125set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2126{
2127 char_u *p;
2128 char_u *subcmd_start;
2129
2130 xp->xp_context = EXPAND_BREAKPOINT;
2131 xp->xp_pattern = arg;
2132
2133 if (cmdidx == CMD_breakadd)
2134 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002135 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002136 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002137 else
2138 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002139
2140 p = skipwhite(arg);
2141 if (*p == NUL)
2142 return NULL;
2143 subcmd_start = p;
2144
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002145 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002146 {
2147 // :breakadd file [lnum] <filename>
2148 // :breakadd func [lnum] <funcname>
2149 p += 4;
2150 p = skipwhite(p);
2151
2152 // skip line number (if specified)
2153 if (VIM_ISDIGIT(*p))
2154 {
2155 p = skipdigits(p);
2156 if (*p != ' ')
2157 {
2158 xp->xp_context = EXPAND_NOTHING;
2159 return NULL;
2160 }
2161 p = skipwhite(p);
2162 }
2163 if (STRNCMP("file", subcmd_start, 4) == 0)
2164 xp->xp_context = EXPAND_FILES;
2165 else
2166 xp->xp_context = EXPAND_USER_FUNC;
2167 xp->xp_pattern = p;
2168 }
2169 else if (STRNCMP("expr ", p, 5) == 0)
2170 {
2171 // :breakadd expr <expression>
2172 xp->xp_context = EXPAND_EXPRESSION;
2173 xp->xp_pattern = skipwhite(p + 5);
2174 }
2175
2176 return NULL;
2177}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002178
2179 static char_u *
2180set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2181{
2182 char_u *p;
2183
2184 xp->xp_context = EXPAND_NOTHING;
2185 xp->xp_pattern = NULL;
2186
2187 p = skipwhite(arg);
2188 if (VIM_ISDIGIT(*p))
2189 return NULL;
2190
2191 xp->xp_context = EXPAND_SCRIPTNAMES;
2192 xp->xp_pattern = p;
2193
2194 return NULL;
2195}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002196#endif
2197
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002198/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002199 * Set the completion context for the :filetype command. Always returns NULL.
2200 */
2201 static char_u *
2202set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2203{
2204 char_u *p;
2205 int val = 0;
2206
2207 xp->xp_context = EXPAND_FILETYPECMD;
2208 xp->xp_pattern = arg;
2209 filetype_expand_what = EXP_FILETYPECMD_ALL;
2210
2211 p = skipwhite(arg);
2212 if (*p == NUL)
2213 return NULL;
2214
2215 for (;;)
2216 {
2217 if (STRNCMP(p, "plugin", 6) == 0)
2218 {
2219 val |= EXPAND_FILETYPECMD_PLUGIN;
2220 p = skipwhite(p + 6);
2221 continue;
2222 }
2223 if (STRNCMP(p, "indent", 6) == 0)
2224 {
2225 val |= EXPAND_FILETYPECMD_INDENT;
2226 p = skipwhite(p + 6);
2227 continue;
2228 }
2229 break;
2230 }
2231
2232 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2233 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2234 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2235 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2236 else if ((val & EXPAND_FILETYPECMD_INDENT))
2237 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2238
2239 xp->xp_pattern = p;
2240
2241 return NULL;
2242}
2243
Girish Palya6b49fba2025-06-28 19:47:34 +02002244/*
2245 * Sets the completion context for commands that involve a search pattern
2246 * and a line range (e.g., :s, :g, :v).
2247 */
2248 static void
2249set_context_with_pattern(expand_T *xp)
2250{
2251 int skiplen = 0;
2252 cmdline_info_T *ccline = get_cmdline_info();
2253#ifdef FEAT_SEARCH_EXTRA
2254 int dummy, patlen, retval;
2255
2256 ++emsg_off;
2257 retval = parse_pattern_and_range(&pre_incsearch_pos, &dummy, &skiplen,
2258 &patlen);
2259 --emsg_off;
2260
2261 // Check if cursor is within search pattern
2262 if (!retval || ccline->cmdpos <= skiplen
2263 || ccline->cmdpos > skiplen + patlen)
2264 return;
2265#endif
2266
2267 xp->xp_pattern = ccline->cmdbuff + skiplen;
2268 xp->xp_pattern_len = ccline->cmdpos - skiplen;
2269 xp->xp_context = EXPAND_PATTERN_IN_BUF;
2270 xp->xp_search_dir = FORWARD;
2271}
Christian Brabandta3422aa2025-04-23 21:04:24 +02002272
2273/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002274 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2275 * The argument to the command is 'arg' and the argument flags is 'argt'.
2276 * For user-defined commands and for environment variables, 'compl' has the
2277 * completion type.
2278 * Returns a pointer to the next command. Returns NULL if there is no next
2279 * command.
2280 */
2281 static char_u *
2282set_context_by_cmdname(
2283 char_u *cmd,
2284 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002285 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002286 char_u *arg,
2287 long argt,
2288 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002289 int forceit)
2290{
Girish Palya6b49fba2025-06-28 19:47:34 +02002291 char_u *nextcmd;
2292
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002293 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002294 {
2295 case CMD_find:
2296 case CMD_sfind:
2297 case CMD_tabfind:
2298 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002299 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002300 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002301 break;
2302 case CMD_cd:
2303 case CMD_chdir:
2304 case CMD_tcd:
2305 case CMD_tchdir:
2306 case CMD_lcd:
2307 case CMD_lchdir:
2308 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002309 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002310 break;
2311 case CMD_help:
2312 xp->xp_context = EXPAND_HELP;
2313 xp->xp_pattern = arg;
2314 break;
2315
2316 // Command modifiers: return the argument.
2317 // Also for commands with an argument that is a command.
2318 case CMD_aboveleft:
2319 case CMD_argdo:
2320 case CMD_belowright:
2321 case CMD_botright:
2322 case CMD_browse:
2323 case CMD_bufdo:
2324 case CMD_cdo:
2325 case CMD_cfdo:
2326 case CMD_confirm:
2327 case CMD_debug:
2328 case CMD_folddoclosed:
2329 case CMD_folddoopen:
2330 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002331 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002332 case CMD_keepalt:
2333 case CMD_keepjumps:
2334 case CMD_keepmarks:
2335 case CMD_keeppatterns:
2336 case CMD_ldo:
2337 case CMD_leftabove:
2338 case CMD_lfdo:
2339 case CMD_lockmarks:
2340 case CMD_noautocmd:
2341 case CMD_noswapfile:
2342 case CMD_rightbelow:
2343 case CMD_sandbox:
2344 case CMD_silent:
2345 case CMD_tab:
2346 case CMD_tabdo:
2347 case CMD_topleft:
2348 case CMD_verbose:
2349 case CMD_vertical:
2350 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002351 case CMD_vim9cmd:
2352 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002353 return arg;
2354
2355 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002356 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002357
2358#ifdef FEAT_SEARCH_EXTRA
2359 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002360 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002361#endif
2362
2363 // All completion for the +cmdline_compl feature goes here.
2364
2365 case CMD_command:
2366 return set_context_in_user_cmd(xp, arg);
2367
2368 case CMD_delcommand:
2369 xp->xp_context = EXPAND_USER_COMMANDS;
2370 xp->xp_pattern = arg;
2371 break;
2372
2373 case CMD_global:
2374 case CMD_vglobal:
Girish Palya6b49fba2025-06-28 19:47:34 +02002375 nextcmd = find_cmd_after_global_cmd(arg);
2376 if (!nextcmd && may_expand_pattern)
2377 set_context_with_pattern(xp);
2378 return nextcmd;
2379
Bram Moolenaard0190392019-08-23 21:17:35 +02002380 case CMD_and:
2381 case CMD_substitute:
Girish Palya6b49fba2025-06-28 19:47:34 +02002382 nextcmd = find_cmd_after_substitute_cmd(arg);
2383 if (!nextcmd && may_expand_pattern)
2384 set_context_with_pattern(xp);
2385 return nextcmd;
2386
Bram Moolenaard0190392019-08-23 21:17:35 +02002387 case CMD_isearch:
2388 case CMD_dsearch:
2389 case CMD_ilist:
2390 case CMD_dlist:
2391 case CMD_ijump:
2392 case CMD_psearch:
2393 case CMD_djump:
2394 case CMD_isplit:
2395 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002396 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002397 case CMD_autocmd:
2398 return set_context_in_autocmd(xp, arg, FALSE);
2399 case CMD_doautocmd:
2400 case CMD_doautoall:
2401 return set_context_in_autocmd(xp, arg, TRUE);
2402 case CMD_set:
2403 set_context_in_set_cmd(xp, arg, 0);
2404 break;
2405 case CMD_setglobal:
2406 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2407 break;
2408 case CMD_setlocal:
2409 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2410 break;
2411 case CMD_tag:
2412 case CMD_stag:
2413 case CMD_ptag:
2414 case CMD_ltag:
2415 case CMD_tselect:
2416 case CMD_stselect:
2417 case CMD_ptselect:
2418 case CMD_tjump:
2419 case CMD_stjump:
2420 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002421 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002422 xp->xp_context = EXPAND_TAGS_LISTFILES;
2423 else
2424 xp->xp_context = EXPAND_TAGS;
2425 xp->xp_pattern = arg;
2426 break;
2427 case CMD_augroup:
2428 xp->xp_context = EXPAND_AUGROUP;
2429 xp->xp_pattern = arg;
2430 break;
2431#ifdef FEAT_SYN_HL
2432 case CMD_syntax:
2433 set_context_in_syntax_cmd(xp, arg);
2434 break;
2435#endif
2436#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002437 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002438 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002439 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002440 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002441 case CMD_if:
2442 case CMD_elseif:
2443 case CMD_while:
2444 case CMD_for:
2445 case CMD_echo:
2446 case CMD_echon:
2447 case CMD_execute:
2448 case CMD_echomsg:
2449 case CMD_echoerr:
2450 case CMD_call:
2451 case CMD_return:
2452 case CMD_cexpr:
2453 case CMD_caddexpr:
2454 case CMD_cgetexpr:
2455 case CMD_lexpr:
2456 case CMD_laddexpr:
2457 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002458 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002459 break;
2460
2461 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002462 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002463 case CMD_function:
2464 case CMD_delfunction:
2465 xp->xp_context = EXPAND_USER_FUNC;
2466 xp->xp_pattern = arg;
2467 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002468 case CMD_disassemble:
2469 set_context_in_disassemble_cmd(xp, arg);
2470 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002471
2472 case CMD_echohl:
2473 set_context_in_echohl_cmd(xp, arg);
2474 break;
2475#endif
2476 case CMD_highlight:
2477 set_context_in_highlight_cmd(xp, arg);
2478 break;
2479#ifdef FEAT_CSCOPE
2480 case CMD_cscope:
2481 case CMD_lcscope:
2482 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002483 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002484 break;
2485#endif
2486#ifdef FEAT_SIGNS
2487 case CMD_sign:
2488 set_context_in_sign_cmd(xp, arg);
2489 break;
2490#endif
2491 case CMD_bdelete:
2492 case CMD_bwipeout:
2493 case CMD_bunload:
2494 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2495 arg = xp->xp_pattern + 1;
2496 // FALLTHROUGH
2497 case CMD_buffer:
2498 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002499 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002500 case CMD_checktime:
2501 xp->xp_context = EXPAND_BUFFERS;
2502 xp->xp_pattern = arg;
2503 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002504#ifdef FEAT_DIFF
2505 case CMD_diffget:
2506 case CMD_diffput:
2507 // If current buffer is in diff mode, complete buffer names
2508 // which are in diff mode, and different than current buffer.
2509 xp->xp_context = EXPAND_DIFF_BUFFERS;
2510 xp->xp_pattern = arg;
2511 break;
2512#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002513 case CMD_USER:
2514 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002515 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2516 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002517
2518 case CMD_map: case CMD_noremap:
2519 case CMD_nmap: case CMD_nnoremap:
2520 case CMD_vmap: case CMD_vnoremap:
2521 case CMD_omap: case CMD_onoremap:
2522 case CMD_imap: case CMD_inoremap:
2523 case CMD_cmap: case CMD_cnoremap:
2524 case CMD_lmap: case CMD_lnoremap:
2525 case CMD_smap: case CMD_snoremap:
2526 case CMD_tmap: case CMD_tnoremap:
2527 case CMD_xmap: case CMD_xnoremap:
2528 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002529 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002530 case CMD_unmap:
2531 case CMD_nunmap:
2532 case CMD_vunmap:
2533 case CMD_ounmap:
2534 case CMD_iunmap:
2535 case CMD_cunmap:
2536 case CMD_lunmap:
2537 case CMD_sunmap:
2538 case CMD_tunmap:
2539 case CMD_xunmap:
2540 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002541 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002542 case CMD_mapclear:
2543 case CMD_nmapclear:
2544 case CMD_vmapclear:
2545 case CMD_omapclear:
2546 case CMD_imapclear:
2547 case CMD_cmapclear:
2548 case CMD_lmapclear:
2549 case CMD_smapclear:
2550 case CMD_tmapclear:
2551 case CMD_xmapclear:
2552 xp->xp_context = EXPAND_MAPCLEAR;
2553 xp->xp_pattern = arg;
2554 break;
2555
2556 case CMD_abbreviate: case CMD_noreabbrev:
2557 case CMD_cabbrev: case CMD_cnoreabbrev:
2558 case CMD_iabbrev: case CMD_inoreabbrev:
2559 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002560 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002561 case CMD_unabbreviate:
2562 case CMD_cunabbrev:
2563 case CMD_iunabbrev:
2564 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002565 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002566#ifdef FEAT_MENU
2567 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2568 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2569 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2570 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2571 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2572 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2573 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2574 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2575 case CMD_tmenu: case CMD_tunmenu:
2576 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2577 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2578#endif
2579
2580 case CMD_colorscheme:
2581 xp->xp_context = EXPAND_COLORS;
2582 xp->xp_pattern = arg;
2583 break;
2584
2585 case CMD_compiler:
2586 xp->xp_context = EXPAND_COMPILER;
2587 xp->xp_pattern = arg;
2588 break;
2589
2590 case CMD_ownsyntax:
2591 xp->xp_context = EXPAND_OWNSYNTAX;
2592 xp->xp_pattern = arg;
2593 break;
2594
2595 case CMD_setfiletype:
2596 xp->xp_context = EXPAND_FILETYPE;
2597 xp->xp_pattern = arg;
2598 break;
2599
2600 case CMD_packadd:
2601 xp->xp_context = EXPAND_PACKADD;
2602 xp->xp_pattern = arg;
2603 break;
2604
zeertzjqb0d45ec2023-01-25 15:04:22 +00002605 case CMD_runtime:
2606 set_context_in_runtime_cmd(xp, arg);
2607 break;
2608
Bram Moolenaard0190392019-08-23 21:17:35 +02002609#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2610 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002611 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002612#endif
2613#if defined(FEAT_PROFILE)
2614 case CMD_profile:
2615 set_context_in_profile_cmd(xp, arg);
2616 break;
2617#endif
2618 case CMD_behave:
2619 xp->xp_context = EXPAND_BEHAVE;
2620 xp->xp_pattern = arg;
2621 break;
2622
2623 case CMD_messages:
2624 xp->xp_context = EXPAND_MESSAGES;
2625 xp->xp_pattern = arg;
2626 break;
2627
2628 case CMD_history:
2629 xp->xp_context = EXPAND_HISTORY;
2630 xp->xp_pattern = arg;
2631 break;
2632#if defined(FEAT_PROFILE)
2633 case CMD_syntime:
2634 xp->xp_context = EXPAND_SYNTIME;
2635 xp->xp_pattern = arg;
2636 break;
2637#endif
2638
2639 case CMD_argdelete:
2640 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2641 arg = xp->xp_pattern + 1;
2642 xp->xp_context = EXPAND_ARGLIST;
2643 xp->xp_pattern = arg;
2644 break;
2645
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002646#ifdef FEAT_EVAL
2647 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002648 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002649 case CMD_breakdel:
2650 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002651
2652 case CMD_scriptnames:
2653 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002654#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002655 case CMD_filetype:
2656 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002657
Bram Moolenaard0190392019-08-23 21:17:35 +02002658 default:
2659 break;
2660 }
2661 return NULL;
2662}
2663
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002664/*
2665 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2666 * we don't need/want deleted. Maybe this could be done better if we didn't
2667 * repeat all this stuff. The only problem is that they may not stay
2668 * perfectly compatible with each other, but then the command line syntax
2669 * probably won't change that much -- webb.
2670 */
2671 static char_u *
2672set_one_cmd_context(
2673 expand_T *xp,
2674 char_u *buff) // buffer for command string
2675{
2676 char_u *p;
2677 char_u *cmd, *arg;
2678 int len = 0;
2679 exarg_T ea;
2680 int compl = EXPAND_NOTHING;
2681 int forceit = FALSE;
2682 int usefilter = FALSE; // filter instead of file name
2683
2684 ExpandInit(xp);
2685 xp->xp_pattern = buff;
2686 xp->xp_line = buff;
2687 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2688 ea.argt = 0;
2689
2690 // 1. skip comment lines and leading space, colons or bars
2691 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2692 ;
2693 xp->xp_pattern = cmd;
2694
2695 if (*cmd == NUL)
2696 return NULL;
2697 if (*cmd == '"') // ignore comment lines
2698 {
2699 xp->xp_context = EXPAND_NOTHING;
2700 return NULL;
2701 }
2702
2703 // 3. Skip over the range to find the command.
2704 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2705 xp->xp_pattern = cmd;
2706 if (*cmd == NUL)
2707 return NULL;
2708 if (*cmd == '"')
2709 {
2710 xp->xp_context = EXPAND_NOTHING;
2711 return NULL;
2712 }
2713
2714 if (*cmd == '|' || *cmd == '\n')
2715 return cmd + 1; // There's another command
2716
2717 // Get the command index.
2718 p = set_cmd_index(cmd, &ea, xp, &compl);
2719 if (p == NULL)
2720 return NULL;
2721
2722 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2723
2724 if (*p == '!') // forced commands
2725 {
2726 forceit = TRUE;
2727 ++p;
2728 }
2729
2730 // 6. parse arguments
2731 if (!IS_USER_CMDIDX(ea.cmdidx))
2732 ea.argt = excmd_get_argt(ea.cmdidx);
2733
2734 arg = skipwhite(p);
2735
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002736 // Does command allow "++argopt" argument?
2737 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002738 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002739 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2740 {
2741 p = arg + 2;
2742 while (*p && !vim_isspace(*p))
2743 MB_PTR_ADV(p);
2744
2745 // Still touching the command after "++"?
2746 if (*p == NUL)
2747 {
2748 if (ea.argt & EX_ARGOPT)
2749 return set_context_in_argopt(xp, arg + 2);
2750#ifdef FEAT_TERMINAL
2751 if (ea.cmdidx == CMD_terminal)
2752 return set_context_in_terminalopt(xp, arg + 2);
2753#endif
2754 }
2755
2756 arg = skipwhite(p);
2757 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002758 }
2759
2760 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2761 {
2762 if (*arg == '>') // append
2763 {
2764 if (*++arg == '>')
2765 ++arg;
2766 arg = skipwhite(arg);
2767 }
2768 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2769 {
2770 ++arg;
2771 usefilter = TRUE;
2772 }
2773 }
2774
2775 if (ea.cmdidx == CMD_read)
2776 {
2777 usefilter = forceit; // :r! filter if forced
2778 if (*arg == '!') // :r !filter
2779 {
2780 ++arg;
2781 usefilter = TRUE;
2782 }
2783 }
2784
2785 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2786 {
2787 while (*arg == *cmd) // allow any number of '>' or '<'
2788 ++arg;
2789 arg = skipwhite(arg);
2790 }
2791
2792 // Does command allow "+command"?
2793 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2794 {
2795 // Check if we're in the +command
2796 p = arg + 1;
2797 arg = skip_cmd_arg(arg, FALSE);
2798
2799 // Still touching the command after '+'?
2800 if (*arg == NUL)
2801 return p;
2802
2803 // Skip space(s) after +command to get to the real argument
2804 arg = skipwhite(arg);
2805 }
2806
2807
2808 // Check for '|' to separate commands and '"' to start comments.
2809 // Don't do this for ":read !cmd" and ":write !cmd".
2810 if ((ea.argt & EX_TRLBAR) && !usefilter)
2811 {
2812 p = arg;
2813 // ":redir @" is not the start of a comment
2814 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2815 p += 2;
2816 while (*p)
2817 {
2818 if (*p == Ctrl_V)
2819 {
2820 if (p[1] != NUL)
2821 ++p;
2822 }
2823 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2824 || *p == '|' || *p == '\n')
2825 {
2826 if (*(p - 1) != '\\')
2827 {
2828 if (*p == '|' || *p == '\n')
2829 return p + 1;
2830 return NULL; // It's a comment
2831 }
2832 }
2833 MB_PTR_ADV(p);
2834 }
2835 }
2836
2837 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2838 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2839 // no arguments allowed but there is something
2840 return NULL;
2841
2842 // Find start of last argument (argument just before cursor):
2843 p = buff;
2844 xp->xp_pattern = p;
2845 len = (int)STRLEN(buff);
2846 while (*p && p < buff + len)
2847 {
2848 if (*p == ' ' || *p == TAB)
2849 {
2850 // argument starts after a space
2851 xp->xp_pattern = ++p;
2852 }
2853 else
2854 {
2855 if (*p == '\\' && *(p + 1) != NUL)
2856 ++p; // skip over escaped character
2857 MB_PTR_ADV(p);
2858 }
2859 }
2860
2861 if (ea.argt & EX_XFILE)
2862 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2863
2864 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002865 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002866 forceit);
2867}
2868
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002869/*
2870 * Set the completion context in 'xp' for command 'str'
2871 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002872 void
2873set_cmd_context(
2874 expand_T *xp,
2875 char_u *str, // start of command line
2876 int len, // length of command line (excl. NUL)
2877 int col, // position of cursor
2878 int use_ccline UNUSED) // use ccline for info
2879{
2880#ifdef FEAT_EVAL
2881 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002882 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002883#endif
2884 int old_char = NUL;
2885 char_u *nextcomm;
2886
2887 // Avoid a UMR warning from Purify, only save the character if it has been
2888 // written before.
2889 if (col < len)
2890 old_char = str[col];
2891 str[col] = NUL;
2892 nextcomm = str;
2893
2894#ifdef FEAT_EVAL
2895 if (use_ccline && ccline->cmdfirstc == '=')
2896 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002897 // pass CMD_SIZE because there is no real command
2898 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002899 }
2900 else if (use_ccline && ccline->input_fn)
2901 {
2902 xp->xp_context = ccline->xp_context;
2903 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002904 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002905 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2906 {
2907 context = xp->xp_context;
2908 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2909 &context);
2910 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002911 }
2912 else
2913#endif
2914 while (nextcomm != NULL)
2915 nextcomm = set_one_cmd_context(xp, nextcomm);
2916
2917 // Store the string here so that call_user_expand_func() can get to them
2918 // easily.
2919 xp->xp_line = str;
2920 xp->xp_col = col;
2921
2922 str[col] = old_char;
2923}
2924
2925/*
2926 * Expand the command line "str" from context "xp".
2927 * "xp" must have been set by set_cmd_context().
2928 * xp->xp_pattern points into "str", to where the text that is to be expanded
2929 * starts.
2930 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2931 * cursor.
2932 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2933 * key that triggered expansion literally.
2934 * Returns EXPAND_OK otherwise.
2935 */
2936 int
2937expand_cmdline(
2938 expand_T *xp,
2939 char_u *str, // start of command line
2940 int col, // position of cursor
2941 int *matchcount, // return: nr of matches
2942 char_u ***matches) // return: array of pointers to matches
2943{
2944 char_u *file_str = NULL;
2945 int options = WILD_ADD_SLASH|WILD_SILENT;
2946
2947 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2948 {
2949 beep_flush();
2950 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2951 }
2952 if (xp->xp_context == EXPAND_NOTHING)
2953 {
2954 // Caller can use the character as a normal char instead
2955 return EXPAND_NOTHING;
2956 }
2957
2958 // add star to file name, or convert to regexp if not exp. files.
2959 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002960 if (cmdline_fuzzy_completion_supported(xp))
2961 // If fuzzy matching, don't modify the search string
2962 file_str = vim_strsave(xp->xp_pattern);
2963 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002964 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
John Marriott1be5b372025-06-23 19:57:29 +02002965 if (file_str == NULL)
2966 return EXPAND_UNSUCCESSFUL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002967
2968 if (p_wic)
2969 options += WILD_ICASE;
2970
2971 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002972 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002973 {
2974 *matchcount = 0;
2975 *matches = NULL;
2976 }
2977 vim_free(file_str);
2978
2979 return EXPAND_OK;
2980}
2981
Bram Moolenaar66b51422019-08-18 21:44:12 +02002982/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002983 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002984 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985 */
2986 static int
2987expand_files_and_dirs(
2988 expand_T *xp,
2989 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002990 char_u ***matches,
2991 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002992 int flags,
2993 int options)
2994{
2995 int free_pat = FALSE;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002996 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002997
2998 // for ":set path=" and ":set tags=" halve backslashes for escaped
2999 // space
3000 if (xp->xp_backslash != XP_BS_NONE)
3001 {
John Marriotta494ce12025-07-03 21:28:50 +02003002 size_t pat_len;
3003 char_u *pat_end;
3004 char_u *p;
3005
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003006 free_pat = TRUE;
John Marriotta494ce12025-07-03 21:28:50 +02003007
3008 pat_len = STRLEN(pat);
3009 pat = vim_strnsave(pat, pat_len);
John Marriott3b03b432025-06-28 20:41:54 +02003010 if (pat == NULL)
3011 return ret;
3012
John Marriotta494ce12025-07-03 21:28:50 +02003013 pat_end = pat + pat_len;
3014 for (p = pat; *p != NUL; ++p)
3015 {
3016 char_u *from;
3017
3018 if (*p != '\\')
3019 continue;
3020
3021 if (xp->xp_backslash & XP_BS_THREE
3022 && *(p + 1) == '\\'
3023 && *(p + 2) == '\\'
3024 && *(p + 3) == ' ')
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 {
John Marriotta494ce12025-07-03 21:28:50 +02003026 from = p + 3;
3027 mch_memmove(p, from,
3028 (size_t)(pat_end - from) + 1); // +1 for NUL
3029 pat_end -= 3;
3030 }
3031 else if (xp->xp_backslash & XP_BS_ONE
3032 && *(p + 1) == ' ')
3033 {
3034 from = p + 1;
3035 mch_memmove(p, from,
3036 (size_t)(pat_end - from) + 1); // +1 for NUL
3037 --pat_end;
3038 }
3039 else if (xp->xp_backslash & XP_BS_COMMA)
3040 {
3041 if (*(p + 1) == '\\' && *(p + 2) == ',')
3042 {
3043 from = p + 2;
3044 mch_memmove(p, from,
3045 (size_t)(pat_end - from) + 1); // +1 for NUL
3046 pat_end -= 2;
3047 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003048#ifdef BACKSLASH_IN_FILENAME
John Marriotta494ce12025-07-03 21:28:50 +02003049 else if (*(p + 1) == ',')
3050 {
3051 from = p + 1;
3052 mch_memmove(p, from,
3053 (size_t)(pat_end - from) + 1); // +1 for NUL
3054 --pat_end;
3055 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003056#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003057 }
John Marriotta494ce12025-07-03 21:28:50 +02003058 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003059 }
3060
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003061 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003062 {
3063#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003064 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003065#endif
3066 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003067 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003068 {
3069 if (xp->xp_context == EXPAND_FILES)
3070 flags |= EW_FILE;
3071 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
3072 flags |= (EW_FILE | EW_PATH);
3073 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
3074 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
3075 else
3076 flags = (flags | EW_DIR) & ~EW_FILE;
3077 if (options & WILD_ICASE)
3078 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003079
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003080 // Expand wildcards, supporting %:h and the like.
3081 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
3082 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003083 if (free_pat)
3084 vim_free(pat);
3085#ifdef BACKSLASH_IN_FILENAME
3086 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
3087 {
John Marriotta494ce12025-07-03 21:28:50 +02003088 int j;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003089
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003090 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003091 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003092 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003093
3094 while (*ptr != NUL)
3095 {
3096 if (p_csl[0] == 's' && *ptr == '\\')
3097 *ptr = '/';
3098 else if (p_csl[0] == 'b' && *ptr == '/')
3099 *ptr = '\\';
3100 ptr += (*mb_ptr2len)(ptr);
3101 }
3102 }
3103 }
3104#endif
3105 return ret;
3106}
3107
3108/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003109 * Function given to ExpandGeneric() to obtain the possible arguments of the
3110 * ":behave {mswin,xterm}" command.
3111 */
3112 static char_u *
3113get_behave_arg(expand_T *xp UNUSED, int idx)
3114{
3115 if (idx == 0)
3116 return (char_u *)"mswin";
3117 if (idx == 1)
3118 return (char_u *)"xterm";
3119 return NULL;
3120}
3121
Christian Brabandta3422aa2025-04-23 21:04:24 +02003122/*
3123 * Function given to ExpandGeneric() to obtain the possible arguments of the
3124 * ":filetype {plugin,indent}" command.
3125 */
3126 static char_u *
3127get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3128{
John Marriotta494ce12025-07-03 21:28:50 +02003129 if (idx < 0)
3130 return NULL;
Christian Brabandta3422aa2025-04-23 21:04:24 +02003131
3132 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
John Marriotta494ce12025-07-03 21:28:50 +02003133 {
3134 char *opts_all[] = {"indent", "plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003135 return (char_u *)opts_all[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003136 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003137 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003138 {
3139 char *opts_plugin[] = {"plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003140 return (char_u *)opts_plugin[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003141 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003142 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003143 {
3144 char *opts_indent[] = {"indent", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003145 return (char_u *)opts_indent[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003146 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003147 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
John Marriotta494ce12025-07-03 21:28:50 +02003148 {
3149 char *opts_onoff[] = {"on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003150 return (char_u *)opts_onoff[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003151 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003152 return NULL;
3153}
3154
K.Takata161b6ac2022-11-14 15:31:07 +00003155#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003156/*
3157 * Function given to ExpandGeneric() to obtain the possible arguments of the
3158 * ":breakadd {expr, file, func, here}" command.
3159 * ":breakdel {func, file, here}" command.
3160 */
3161 static char_u *
3162get_breakadd_arg(expand_T *xp UNUSED, int idx)
3163{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003164 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003165 {
John Marriotta494ce12025-07-03 21:28:50 +02003166 char *opts[] = {"expr", "file", "func", "here"};
3167
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003168 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003169 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3170 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003171 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3172 {
3173 // breakdel {func, file, here}
3174 if (idx <= 2)
3175 return (char_u *)opts[idx + 1];
3176 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003177 else
3178 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003179 // profdel {func, file}
3180 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003181 return (char_u *)opts[idx + 1];
3182 }
3183 }
3184 return NULL;
3185}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003186
3187/*
3188 * Function given to ExpandGeneric() to obtain the possible arguments for the
3189 * ":scriptnames" command.
3190 */
3191 static char_u *
3192get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3193{
3194 scriptitem_T *si;
3195
3196 if (!SCRIPT_ID_VALID(idx + 1))
3197 return NULL;
3198
3199 si = SCRIPT_ITEM(idx + 1);
3200 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3201 return NameBuff;
3202}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003203#endif
3204
Bram Moolenaard0190392019-08-23 21:17:35 +02003205/*
3206 * Function given to ExpandGeneric() to obtain the possible arguments of the
3207 * ":messages {clear}" command.
3208 */
3209 static char_u *
3210get_messages_arg(expand_T *xp UNUSED, int idx)
3211{
3212 if (idx == 0)
3213 return (char_u *)"clear";
3214 return NULL;
3215}
3216
3217 static char_u *
3218get_mapclear_arg(expand_T *xp UNUSED, int idx)
3219{
3220 if (idx == 0)
3221 return (char_u *)"<buffer>";
3222 return NULL;
3223}
3224
3225/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003226 * Do the expansion based on xp->xp_context and 'rmp'.
3227 */
3228 static int
3229ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003230 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003231 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003232 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003233 char_u ***matches,
3234 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003235{
3236 static struct expgen
3237 {
3238 int context;
3239 char_u *((*func)(expand_T *, int));
3240 int ic;
3241 int escaped;
3242 } tab[] =
3243 {
3244 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3245 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003246 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003247 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3248 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3249 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3250 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3251 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3252 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3253 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3254 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003255#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003256 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3257 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3258 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3259 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3260 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003261#endif
3262#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003263 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3264 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003265#endif
3266#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003267 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003268#endif
3269#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003270 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003271#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003272 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3273 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3274 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003275#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003276 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003277#endif
3278#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003279 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003280#endif
3281#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003282 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003283#endif
3284#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003285 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3286 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003287#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003288 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3289 {EXPAND_USER, get_users, TRUE, FALSE},
3290 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003291#ifdef FEAT_EVAL
3292 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003293 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003294#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003295 };
3296 int i;
3297 int ret = FAIL;
3298
3299 // Find a context in the table and call the ExpandGeneric() with the
3300 // right function to do the expansion.
3301 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3302 {
3303 if (xp->xp_context == tab[i].context)
3304 {
3305 if (tab[i].ic)
3306 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003307 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3308 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003309 break;
3310 }
3311 }
3312
3313 return ret;
3314}
3315
3316/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003317 * Map wild expand options to flags for expand_wildcards()
3318 */
3319 static int
3320map_wildopts_to_ewflags(int options)
3321{
3322 int flags;
3323
3324 flags = EW_DIR; // include directories
3325 if (options & WILD_LIST_NOTFOUND)
3326 flags |= EW_NOTFOUND;
3327 if (options & WILD_ADD_SLASH)
3328 flags |= EW_ADDSLASH;
3329 if (options & WILD_KEEP_ALL)
3330 flags |= EW_KEEPALL;
3331 if (options & WILD_SILENT)
3332 flags |= EW_SILENT;
3333 if (options & WILD_NOERROR)
3334 flags |= EW_NOERROR;
3335 if (options & WILD_ALLLINKS)
3336 flags |= EW_ALLLINKS;
3337
3338 return flags;
3339}
3340
3341/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342 * Do the expansion based on xp->xp_context and "pat".
3343 */
3344 static int
3345ExpandFromContext(
3346 expand_T *xp,
3347 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003348 char_u ***matches,
3349 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 int options) // WILD_ flags
3351{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 int ret;
3354 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003355 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003356 int fuzzy = cmdline_fuzzy_complete(pat)
3357 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003359 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360
3361 if (xp->xp_context == EXPAND_FILES
3362 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003363 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003364 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003365 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003366 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3367 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003369 *matches = (char_u **)"";
3370 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003371 if (xp->xp_context == EXPAND_HELP)
3372 {
3373 // With an empty argument we would get all the help tags, which is
3374 // very slow. Get matches for "help" instead.
3375 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003376 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003377 {
3378#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003379 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003380#endif
3381 return OK;
3382 }
3383 return FAIL;
3384 }
3385
Bram Moolenaar66b51422019-08-18 21:44:12 +02003386 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003387 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003388 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003389 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003391 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003392#ifdef FEAT_DIFF
3393 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003394 return ExpandBufnames(pat, numMatches, matches,
3395 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003396#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003397 if (xp->xp_context == EXPAND_TAGS
3398 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003399 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3400 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003401 if (xp->xp_context == EXPAND_COLORS)
3402 {
3403 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003404 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003405 directories);
3406 }
3407 if (xp->xp_context == EXPAND_COMPILER)
3408 {
3409 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003410 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003411 }
3412 if (xp->xp_context == EXPAND_OWNSYNTAX)
3413 {
3414 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003415 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003416 }
3417 if (xp->xp_context == EXPAND_FILETYPE)
3418 {
3419 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003420 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003421 }
Doug Kearns81642d92024-01-04 22:37:44 +01003422#ifdef FEAT_KEYMAP
3423 if (xp->xp_context == EXPAND_KEYMAP)
3424 {
3425 char *directories[] = {"keymap", NULL};
3426 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3427 }
3428#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003429#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003430 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003431 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003432#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003433 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003434 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003435 if (xp->xp_context == EXPAND_RUNTIME)
3436 return expand_runtime_cmd(pat, numMatches, matches);
Girish Palya6b49fba2025-06-28 19:47:34 +02003437 if (xp->xp_context == EXPAND_PATTERN_IN_BUF)
3438 return expand_pattern_in_buf(pat, xp->xp_search_dir,
3439 matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003441 // When expanding a function name starting with s:, match the <SNR>nr_
3442 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003443 if ((xp->xp_context == EXPAND_USER_FUNC
3444 || xp->xp_context == EXPAND_DISASSEMBLE)
3445 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003446 {
3447 int len = (int)STRLEN(pat) + 20;
3448
3449 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003450 if (tofree == NULL)
3451 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003452 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003453 pat = tofree;
3454 }
3455
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003456 if (!fuzzy)
3457 {
3458 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3459 if (regmatch.regprog == NULL)
3460 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003461
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003462 // set ignore-case according to p_ic, p_scs and pat
3463 regmatch.rm_ic = ignorecase(pat);
3464 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465
3466 if (xp->xp_context == EXPAND_SETTINGS
3467 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003468 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003469 else if (xp->xp_context == EXPAND_STRING_SETTING)
3470 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3471 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3472 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003474 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003475 else if (xp->xp_context == EXPAND_ARGOPT)
3476 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003477 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3478 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003479#if defined(FEAT_TERMINAL)
3480 else if (xp->xp_context == EXPAND_TERMINALOPT)
3481 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3482#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003483#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003485 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003486#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003488 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003489
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003490 if (!fuzzy)
3491 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003492 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493
3494 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495}
3496
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003497 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003498ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003499 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500 expand_T *xp,
3501 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003502 char_u ***matches,
3503 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003504 char_u *((*func)(expand_T *, int)),
3505 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003506 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507{
Yee Cheng China7b81202025-02-23 09:32:47 +01003508 return ExpandGenericExt(
3509 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3510}
3511
3512/*
3513 * Expand a list of names.
3514 *
3515 * Generic function for command line completion. It calls a function to
3516 * obtain strings, one by one. The strings are matched against a regexp
3517 * program. Matching strings are copied into an array, which is returned.
3518 *
3519 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3520 * is used.
3521 *
3522 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3523 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3524 * sorting.
3525 *
3526 * Returns OK when no problems encountered, FAIL for error (out of memory).
3527 */
3528 int
3529ExpandGenericExt(
3530 char_u *pat,
3531 expand_T *xp,
3532 regmatch_T *regmatch,
3533 char_u ***matches,
3534 int *numMatches,
3535 char_u *((*func)(expand_T *, int)),
3536 // returns a string from the list
3537 int escaped,
3538 int sortStartIdx)
3539{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003541 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003543 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003544 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003545 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003546 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003547 int sort_matches = FALSE;
3548 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003549 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003550
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003551 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003552 *matches = NULL;
3553 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003554
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003555 if (!fuzzy)
3556 ga_init2(&ga, sizeof(char *), 30);
3557 else
3558 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3559
3560 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003561 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003562 str = (*func)(xp, i);
3563 if (str == NULL) // end of list
3564 break;
3565 if (*str == NUL) // skip empty strings
3566 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003568 if (xp->xp_pattern[0] != NUL)
3569 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003570 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003571 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003572 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003573 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003574 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003575 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003576 }
3577 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003578 else
3579 match = TRUE;
3580
3581 if (!match)
3582 continue;
3583
3584 if (escaped)
3585 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3586 else
3587 str = vim_strsave(str);
3588 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003589 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003590 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003592 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003593 return FAIL;
3594 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003595 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003596 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003597 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003598
3599 if (ga_grow(&ga, 1) == FAIL)
3600 {
3601 vim_free(str);
3602 break;
3603 }
3604
3605 if (fuzzy)
3606 {
3607 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3608 fuzmatch->idx = ga.ga_len;
3609 fuzmatch->str = str;
3610 fuzmatch->score = score;
3611 }
3612 else
3613 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3614
K.Takata161b6ac2022-11-14 15:31:07 +00003615#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003616 if (func == get_menu_names)
3617 {
3618 // test for separator added by get_menu_names()
3619 str += STRLEN(str) - 1;
3620 if (*str == '\001')
3621 *str = '.';
3622 }
K.Takata161b6ac2022-11-14 15:31:07 +00003623#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003624
Yee Cheng China7b81202025-02-23 09:32:47 +01003625 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3626 {
3627 // Found first item to start sorting from. This is usually 0.
3628 sortStartMatchIdx = ga.ga_len;
3629 }
3630
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003631 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 }
3633
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003634 if (ga.ga_len == 0)
3635 return OK;
3636
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003637 // sort the matches when using regular expression matching and sorting
3638 // applies to the completion context. Menus and scriptnames should be kept
3639 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003640 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003641 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003642 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003643 && xp->xp_context != EXPAND_SCRIPTNAMES
3644 && xp->xp_context != EXPAND_ARGOPT
3645 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003646 sort_matches = TRUE;
3647
3648 // <SNR> functions should be sorted to the end.
3649 if (xp->xp_context == EXPAND_EXPRESSION
3650 || xp->xp_context == EXPAND_FUNCTIONS
3651 || xp->xp_context == EXPAND_USER_FUNC
3652 || xp->xp_context == EXPAND_DISASSEMBLE)
3653 funcsort = TRUE;
3654
3655 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003656 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003657 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003658 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003659 // <SNR> functions should be sorted to the end.
3660 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3661 sort_func_compare);
3662 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003663 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003664 }
3665
3666 if (!fuzzy)
3667 {
3668 *matches = ga.ga_data;
3669 *numMatches = ga.ga_len;
3670 }
3671 else
3672 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003673 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3674 funcsort) == FAIL)
3675 return FAIL;
3676 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003677 }
3678
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003679#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680 // Reset the variables used for special highlight names expansion, so that
3681 // they don't show up when getting normal highlight names by ID.
3682 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003683#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003684
Bram Moolenaar66b51422019-08-18 21:44:12 +02003685 return OK;
3686}
3687
3688/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003689 * Expand shell command matches in one directory of $PATH.
3690 */
3691 static void
3692expand_shellcmd_onedir(
3693 char_u *buf,
3694 char_u *s,
3695 size_t l,
3696 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003697 char_u ***matches,
3698 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003699 int flags,
3700 hashtab_T *ht,
3701 garray_T *gap)
3702{
3703 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003704 hash_T hash;
3705 hashitem_T *hi;
3706
3707 vim_strncpy(buf, s, l);
3708 add_pathsep(buf);
3709 l = STRLEN(buf);
3710 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3711
3712 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003713 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003714 if (ret != OK)
3715 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003716
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003717 if (ga_grow(gap, *numMatches) == FAIL)
3718 {
3719 FreeWild(*numMatches, *matches);
3720 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003721 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003722
3723 for (int i = 0; i < *numMatches; ++i)
3724 {
3725 char_u *name = (*matches)[i];
3726
3727 if (STRLEN(name) > l)
3728 {
3729 // Check if this name was already found.
3730 hash = hash_hash(name + l);
3731 hi = hash_lookup(ht, name + l, hash);
3732 if (HASHITEM_EMPTY(hi))
3733 {
3734 // Remove the path that was prepended.
3735 STRMOVE(name, name + l);
3736 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3737 hash_add_item(ht, hi, name, hash);
3738 name = NULL;
3739 }
3740 }
3741 vim_free(name);
3742 }
3743 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003744}
3745
3746/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003747 * Complete a shell command.
3748 * Returns FAIL or OK;
3749 */
3750 static int
3751expand_shellcmd(
3752 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003753 char_u ***matches, // return: array with matches
3754 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003755 int flagsarg) // EW_ flags
3756{
3757 char_u *pat;
3758 int i;
3759 char_u *path = NULL;
3760 int mustfree = FALSE;
3761 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003762 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003763 size_t l;
3764 char_u *s, *e;
3765 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003766 int did_curdir = FALSE;
3767 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003768
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003769 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003770 if (buf == NULL)
3771 return FAIL;
3772
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003773 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003774 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003775 if (pat == NULL)
3776 {
3777 vim_free(buf);
3778 return FAIL;
3779 }
3780
Bram Moolenaar66b51422019-08-18 21:44:12 +02003781 for (i = 0; pat[i]; ++i)
3782 if (pat[i] == '\\' && pat[i + 1] == ' ')
3783 STRMOVE(pat + i, pat + i + 1);
3784
3785 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3786
3787 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3788 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3789 path = (char_u *)".";
3790 else
3791 {
3792 // For an absolute name we don't use $PATH.
3793 if (!mch_isFullName(pat))
3794 path = vim_getenv((char_u *)"PATH", &mustfree);
3795 if (path == NULL)
3796 path = (char_u *)"";
3797 }
3798
3799 // Go over all directories in $PATH. Expand matches in that directory and
3800 // collect them in "ga". When "." is not in $PATH also expand for the
3801 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003802 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003803 hash_init(&found_ht);
3804 for (s = path; ; s = e)
3805 {
K.Takata161b6ac2022-11-14 15:31:07 +00003806#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003808#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003809 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003810#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 if (e == NULL)
3812 e = s + STRLEN(s);
3813
3814 if (*s == NUL)
3815 {
3816 if (did_curdir)
3817 break;
3818 // Find directories in the current directory, path is empty.
3819 did_curdir = TRUE;
3820 flags |= EW_DIR;
3821 }
3822 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3823 {
3824 did_curdir = TRUE;
3825 flags |= EW_DIR;
3826 }
3827 else
3828 // Do not match directories inside a $PATH item.
3829 flags &= ~EW_DIR;
3830
3831 l = e - s;
3832 if (l > MAXPATHL - 5)
3833 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003834
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003835 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003836 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003837
Bram Moolenaar66b51422019-08-18 21:44:12 +02003838 if (*e != NUL)
3839 ++e;
3840 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003841 *matches = ga.ga_data;
3842 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003843
3844 vim_free(buf);
3845 vim_free(pat);
3846 if (mustfree)
3847 vim_free(path);
3848 hash_clear(&found_ht);
3849 return OK;
3850}
3851
K.Takata161b6ac2022-11-14 15:31:07 +00003852#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003853/*
3854 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003855 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856 */
3857 static void *
3858call_user_expand_func(
3859 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003860 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003861{
3862 cmdline_info_T *ccline = get_cmdline_info();
3863 int keep = 0;
3864 typval_T args[4];
3865 sctx_T save_current_sctx = current_sctx;
3866 char_u *pat = NULL;
3867 void *ret;
3868
3869 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3870 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003871
3872 if (ccline->cmdbuff != NULL)
3873 {
3874 keep = ccline->cmdbuff[ccline->cmdlen];
3875 ccline->cmdbuff[ccline->cmdlen] = 0;
3876 }
3877
3878 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3879
3880 args[0].v_type = VAR_STRING;
3881 args[0].vval.v_string = pat;
3882 args[1].v_type = VAR_STRING;
3883 args[1].vval.v_string = xp->xp_line;
3884 args[2].v_type = VAR_NUMBER;
3885 args[2].vval.v_number = xp->xp_col;
3886 args[3].v_type = VAR_UNKNOWN;
3887
3888 current_sctx = xp->xp_script_ctx;
3889
3890 ret = user_expand_func(xp->xp_arg, 3, args);
3891
3892 current_sctx = save_current_sctx;
3893 if (ccline->cmdbuff != NULL)
3894 ccline->cmdbuff[ccline->cmdlen] = keep;
3895
3896 vim_free(pat);
3897 return ret;
3898}
3899
3900/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003901 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3902 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003903 */
3904 static int
3905ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003906 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003907 expand_T *xp,
3908 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003909 char_u ***matches,
3910 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003911{
3912 char_u *retstr;
3913 char_u *s;
3914 char_u *e;
3915 int keep;
3916 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003917 int fuzzy;
3918 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003919 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003920
3921 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003922 *matches = NULL;
3923 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003924
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003925 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003926 if (retstr == NULL)
3927 return FAIL;
3928
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003929 if (!fuzzy)
3930 ga_init2(&ga, sizeof(char *), 3);
3931 else
3932 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3933
Bram Moolenaar66b51422019-08-18 21:44:12 +02003934 for (s = retstr; *s != NUL; s = e)
3935 {
3936 e = vim_strchr(s, '\n');
3937 if (e == NULL)
3938 e = s + STRLEN(s);
3939 keep = *e;
3940 *e = NUL;
3941
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003942 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003943 {
3944 if (!fuzzy)
3945 match = vim_regexec(regmatch, s, (colnr_T)0);
3946 else
3947 {
3948 score = fuzzy_match_str(s, pat);
3949 match = (score != 0);
3950 }
3951 }
3952 else
3953 match = TRUE; // match everything
3954
Bram Moolenaar66b51422019-08-18 21:44:12 +02003955 *e = keep;
3956
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003957 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003958 {
John Marriott3b03b432025-06-28 20:41:54 +02003959 char_u *p = vim_strnsave(s, (size_t)(e - s));
3960 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003961 break;
John Marriott3b03b432025-06-28 20:41:54 +02003962
3963 if (ga_grow(&ga, 1) == FAIL)
3964 {
3965 vim_free(p);
3966 break;
3967 }
3968
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003969 if (!fuzzy)
John Marriott3b03b432025-06-28 20:41:54 +02003970 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003971 else
3972 {
3973 fuzmatch_str_T *fuzmatch =
3974 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003975 fuzmatch->idx = ga.ga_len;
John Marriott3b03b432025-06-28 20:41:54 +02003976 fuzmatch->str = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003977 fuzmatch->score = score;
3978 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003979 ++ga.ga_len;
3980 }
3981
3982 if (*e != NUL)
3983 ++e;
3984 }
3985 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003986
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003987 if (ga.ga_len == 0)
3988 return OK;
3989
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003990 if (!fuzzy)
3991 {
3992 *matches = ga.ga_data;
3993 *numMatches = ga.ga_len;
3994 }
3995 else
3996 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003997 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3998 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003999 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00004000 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00004001 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004002 return OK;
4003}
4004
4005/*
4006 * Expand names with a list returned by a function defined by the user.
4007 */
4008 static int
4009ExpandUserList(
4010 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004011 char_u ***matches,
4012 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004013{
4014 list_T *retlist;
4015 listitem_T *li;
4016 garray_T ga;
4017
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004018 *matches = NULL;
4019 *numMatches = 0;
4020 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004021 if (retlist == NULL)
4022 return FAIL;
4023
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004024 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004025 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004026 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004027 {
John Marriott3b03b432025-06-28 20:41:54 +02004028 char_u *p;
4029
Bram Moolenaar66b51422019-08-18 21:44:12 +02004030 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4031 continue; // Skip non-string items and empty strings
4032
John Marriott3b03b432025-06-28 20:41:54 +02004033 p = vim_strsave(li->li_tv.vval.v_string);
4034 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004035 break;
4036
John Marriott3b03b432025-06-28 20:41:54 +02004037 if (ga_grow(&ga, 1) == FAIL)
4038 {
4039 vim_free(p);
4040 break;
4041 }
4042
4043 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004044 ++ga.ga_len;
4045 }
4046 list_unref(retlist);
4047
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004048 *matches = ga.ga_data;
4049 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004050 return OK;
4051}
K.Takata161b6ac2022-11-14 15:31:07 +00004052#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02004053
4054/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02004055 * Expand "file" for all comma-separated directories in "path".
4056 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00004057 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02004058 */
4059 void
4060globpath(
4061 char_u *path,
4062 char_u *file,
4063 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00004064 int expand_options,
4065 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004066{
4067 expand_T xpc;
4068 char_u *buf;
John Marriotta494ce12025-07-03 21:28:50 +02004069 size_t buflen;
4070 size_t filelen;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004071
4072 buf = alloc(MAXPATHL);
4073 if (buf == NULL)
4074 return;
4075
4076 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00004077 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004078
John Marriotta494ce12025-07-03 21:28:50 +02004079 filelen = STRLEN(file);
4080
Bram Moolenaar66b51422019-08-18 21:44:12 +02004081 // Loop over all entries in {path}.
4082 while (*path != NUL)
4083 {
4084 // Copy one item of the path to buf[] and concatenate the file name.
John Marriotta494ce12025-07-03 21:28:50 +02004085 buflen = (size_t)copy_option_part(&path, buf, MAXPATHL, ",");
4086 if (buflen + filelen + 2 < MAXPATHL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004087 {
John Marriotta494ce12025-07-03 21:28:50 +02004088 int num_p;
4089 char_u **p;
4090
4091 if (*buf != NUL && !after_pathsep(buf, buf + buflen))
4092 {
K.Takata161b6ac2022-11-14 15:31:07 +00004093#if defined(MSWIN)
John Marriotta494ce12025-07-03 21:28:50 +02004094 // Using the platform's path separator (\) makes vim incorrectly
4095 // treat it as an escape character, use '/' instead.
4096 STRCPY(buf + buflen, "/");
4097 ++buflen;
K.Takata161b6ac2022-11-14 15:31:07 +00004098#else
John Marriotta494ce12025-07-03 21:28:50 +02004099 STRCPY(buf + buflen, PATHSEPSTR);
4100 buflen += STRLEN_LITERAL(PATHSEPSTR);
K.Takata161b6ac2022-11-14 15:31:07 +00004101#endif
John Marriotta494ce12025-07-03 21:28:50 +02004102 }
4103 STRCPY(buf + buflen, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004104 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02004105 WILD_SILENT|expand_options) != FAIL && num_p > 0)
4106 {
4107 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
4108
4109 if (ga_grow(ga, num_p) == OK)
John Marriotta494ce12025-07-03 21:28:50 +02004110 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004111 // take over the pointers and put them in "ga"
John Marriotta494ce12025-07-03 21:28:50 +02004112 for (int i = 0; i < num_p; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004113 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004114 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02004115 ++ga->ga_len;
4116 }
John Marriotta494ce12025-07-03 21:28:50 +02004117 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004118 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004119 }
4120 }
4121 }
4122
4123 vim_free(buf);
4124}
Bram Moolenaar66b51422019-08-18 21:44:12 +02004125
Bram Moolenaareadee482020-09-04 15:37:31 +02004126/*
4127 * Translate some keys pressed when 'wildmenu' is used.
4128 */
4129 int
4130wildmenu_translate_key(
4131 cmdline_info_T *cclp,
4132 int key,
4133 expand_T *xp,
4134 int did_wild_list)
4135{
4136 int c = key;
4137
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004138 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004139 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004140 // When the popup menu is used for cmdline completion:
4141 // Up : go to the previous item in the menu
4142 // Down : go to the next item in the menu
4143 // Left : go to the parent directory
4144 // Right: list the files in the selected directory
4145 switch (c)
4146 {
4147 case K_UP: c = K_LEFT; break;
4148 case K_DOWN: c = K_RIGHT; break;
4149 case K_LEFT: c = K_UP; break;
4150 case K_RIGHT: c = K_DOWN; break;
4151 default: break;
4152 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004153 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004154
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004155 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004156 {
4157 if (c == K_LEFT)
4158 c = Ctrl_P;
4159 else if (c == K_RIGHT)
4160 c = Ctrl_N;
4161 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004162
Bram Moolenaareadee482020-09-04 15:37:31 +02004163 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004164 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004165 && cclp->cmdpos > 1
4166 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4167 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4168 && (c == '\n' || c == '\r' || c == K_KENTER))
4169 c = K_DOWN;
4170
4171 return c;
4172}
4173
4174/*
4175 * Delete characters on the command line, from "from" to the current
4176 * position.
4177 */
4178 static void
4179cmdline_del(cmdline_info_T *cclp, int from)
4180{
4181 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4182 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4183 cclp->cmdlen -= cclp->cmdpos - from;
4184 cclp->cmdpos = from;
4185}
4186
4187/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004188 * Handle a key pressed when the wild menu for the menu names
4189 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004190 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004191 static int
4192wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004193{
Bram Moolenaareadee482020-09-04 15:37:31 +02004194 int i;
4195 int j;
4196
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004197 // Hitting <Down> after "emenu Name.": complete submenu
4198 if (key == K_DOWN && cclp->cmdpos > 0
4199 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004200 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004201 key = p_wc;
4202 KeyTyped = TRUE; // in case the key was mapped
4203 }
4204 else if (key == K_UP)
4205 {
4206 // Hitting <Up>: Remove one submenu name in front of the
4207 // cursor
4208 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004209
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004210 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4211 i = 0;
4212 while (--j > 0)
4213 {
4214 // check for start of menu name
4215 if (cclp->cmdbuff[j] == ' '
4216 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004217 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004218 i = j + 1;
4219 break;
4220 }
4221 // check for start of submenu name
4222 if (cclp->cmdbuff[j] == '.'
4223 && cclp->cmdbuff[j - 1] != '\\')
4224 {
4225 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004226 {
4227 i = j + 1;
4228 break;
4229 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004230 else
4231 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004232 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004233 }
4234 if (i > 0)
4235 cmdline_del(cclp, i);
4236 key = p_wc;
4237 KeyTyped = TRUE; // in case the key was mapped
4238 xp->xp_context = EXPAND_NOTHING;
4239 }
4240
4241 return key;
4242}
4243
4244/*
4245 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4246 * directory names (EXPAND_DIRECTORIES) or shell command names
4247 * (EXPAND_SHELLCMD) is displayed.
4248 */
4249 static int
4250wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4251{
4252 int i;
4253 int j;
4254 char_u upseg[5];
4255
4256 upseg[0] = PATHSEP;
4257 upseg[1] = '.';
4258 upseg[2] = '.';
4259 upseg[3] = PATHSEP;
4260 upseg[4] = NUL;
4261
4262 if (key == K_DOWN
4263 && cclp->cmdpos > 0
4264 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4265 && (cclp->cmdpos < 3
4266 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4267 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4268 {
4269 // go down a directory
4270 key = p_wc;
4271 KeyTyped = TRUE; // in case the key was mapped
4272 }
4273 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4274 {
4275 // If in a direct ancestor, strip off one ../ to go down
4276 int found = FALSE;
4277
4278 j = cclp->cmdpos;
4279 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4280 while (--j > i)
4281 {
4282 if (has_mbyte)
4283 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4284 if (vim_ispathsep(cclp->cmdbuff[j]))
4285 {
4286 found = TRUE;
4287 break;
4288 }
4289 }
4290 if (found
4291 && cclp->cmdbuff[j - 1] == '.'
4292 && cclp->cmdbuff[j - 2] == '.'
4293 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4294 {
4295 cmdline_del(cclp, j - 2);
4296 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004297 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004298 }
4299 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004300 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004301 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004302 // go up a directory
4303 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004304
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004305 j = cclp->cmdpos - 1;
4306 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4307 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004308 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004309 if (has_mbyte)
4310 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4311 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004312#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004313 && vim_strchr((char_u *)" *?[{`$%#",
4314 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004315#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004316 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004317 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004318 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004319 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004320 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004321 break;
4322 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004323 else
4324 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004325 }
4326 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004327
4328 if (!found)
4329 j = i;
4330 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4331 j += 4;
4332 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4333 && j == i)
4334 j += 3;
4335 else
4336 j = 0;
4337 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004338 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004339 // TODO this is only for DOS/UNIX systems - need to put in
4340 // machine-specific stuff here and in upseg init
4341 cmdline_del(cclp, j);
4342 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004343 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004344 else if (cclp->cmdpos > i)
4345 cmdline_del(cclp, i);
4346
4347 // Now complete in the new directory. Set KeyTyped in case the
4348 // Up key came from a mapping.
4349 key = p_wc;
4350 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004351 }
4352
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004353 return key;
4354}
4355
4356/*
4357 * Handle a key pressed when the wild menu is displayed
4358 */
4359 int
4360wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4361{
4362 if (xp->xp_context == EXPAND_MENUNAMES)
4363 return wildmenu_process_key_menunames(cclp, key, xp);
4364 else if ((xp->xp_context == EXPAND_FILES
4365 || xp->xp_context == EXPAND_DIRECTORIES
4366 || xp->xp_context == EXPAND_SHELLCMD))
4367 return wildmenu_process_key_filenames(cclp, key, xp);
4368
4369 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004370}
4371
4372/*
4373 * Free expanded names when finished walking through the matches
4374 */
4375 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004376wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004377{
4378 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004379
4380 if (!p_wmnu || wild_menu_showing == 0)
4381 return;
4382
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004383#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004384 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004385 if (cclp->input_fn)
4386 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004387#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004388
Girish Palya6b49fba2025-06-28 19:47:34 +02004389#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
4390 // Clear highlighting applied during wildmenu activity
4391 set_no_hlsearch(TRUE);
4392#endif
4393
Bram Moolenaareadee482020-09-04 15:37:31 +02004394 if (wild_menu_showing == WM_SCROLLED)
4395 {
4396 // Entered command line, move it up
4397 cmdline_row--;
4398 redrawcmd();
4399 }
4400 else if (save_p_ls != -1)
4401 {
4402 // restore 'laststatus' and 'winminheight'
4403 p_ls = save_p_ls;
4404 p_wmh = save_p_wmh;
4405 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004406 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004407 redrawcmd();
4408 save_p_ls = -1;
4409 }
4410 else
4411 {
4412 win_redraw_last_status(topframe);
4413 redraw_statuslines();
4414 }
4415 KeyTyped = skt;
4416 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004417#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004418 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004419 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004420#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004421}
Bram Moolenaareadee482020-09-04 15:37:31 +02004422
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004423#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004424/*
4425 * "getcompletion()" function
4426 */
4427 void
4428f_getcompletion(typval_T *argvars, typval_T *rettv)
4429{
4430 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004431 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004432 expand_T xpc;
4433 int filtered = FALSE;
4434 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004435 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004436
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004437 if (in_vim9script()
4438 && (check_for_string_arg(argvars, 0) == FAIL
4439 || check_for_string_arg(argvars, 1) == FAIL
4440 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4441 return;
4442
ii144785fe02021-11-21 12:13:56 +00004443 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004444 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004445 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004446 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004447
4448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004449 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004450
4451 if (p_wic)
4452 options |= WILD_ICASE;
4453
4454 // For filtered results, 'wildignore' is used
4455 if (!filtered)
4456 options |= WILD_KEEP_ALL;
4457
4458 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004459 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004460 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004461 int cmdline_len = (int)STRLEN(pat);
4462 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004463 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004464 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004465 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004466 else
4467 {
ii144785fe02021-11-21 12:13:56 +00004468 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004469 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004470 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004471
4472 xpc.xp_context = cmdcomplete_str_to_type(type);
4473 if (xpc.xp_context == EXPAND_NOTHING)
4474 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004475 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004476 return;
4477 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004478
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004479 if (xpc.xp_context == EXPAND_USER_DEFINED)
4480 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004481 // Must be "custom,funcname" pattern
4482 if (STRNCMP(type, "custom,", 7) != 0)
4483 {
4484 semsg(_(e_invalid_argument_str), type);
4485 return;
4486 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004487
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004488 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004489 }
4490
4491 if (xpc.xp_context == EXPAND_USER_LIST)
4492 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004493 // Must be "customlist,funcname" pattern
4494 if (STRNCMP(type, "customlist,", 11) != 0)
4495 {
4496 semsg(_(e_invalid_argument_str), type);
4497 return;
4498 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004499
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004500 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004501 }
4502
Bram Moolenaar66b51422019-08-18 21:44:12 +02004503# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004504 if (xpc.xp_context == EXPAND_MENUS)
4505 {
4506 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4507 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4508 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004509# endif
4510# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004511 if (xpc.xp_context == EXPAND_CSCOPE)
4512 {
4513 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4514 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4515 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004516# endif
4517# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004518 if (xpc.xp_context == EXPAND_SIGN)
4519 {
4520 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4521 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4522 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004523# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004524 if (xpc.xp_context == EXPAND_RUNTIME)
4525 {
4526 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4527 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4528 }
zeertzjq85f36d62024-10-10 19:14:13 +02004529 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4530 {
4531 int context = EXPAND_SHELLCMDLINE;
4532 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4533 &context);
4534 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4535 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004536 if (xpc.xp_context == EXPAND_FILETYPECMD)
4537 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004538 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004539
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004540 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004541 // when fuzzy matching, don't modify the search string
4542 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004543 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004544 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004545
Bram Moolenaar93a10962022-06-16 11:42:09 +01004546 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004547 {
4548 int i;
4549
4550 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4551
4552 for (i = 0; i < xpc.xp_numfiles; i++)
4553 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4554 }
4555 vim_free(pat);
4556 ExpandCleanup(&xpc);
4557}
Girish Palya92f68e22025-04-21 11:12:41 +02004558
4559/*
Hirohito Higashi96b3ef22025-07-05 15:31:23 +02004560 * "getcompletiontype()" function
4561 */
4562 void
4563f_getcompletiontype(typval_T *argvars, typval_T *rettv)
4564{
4565 char_u *pat;
4566 expand_T xpc;
4567 int cmdline_len;
4568
4569 rettv->v_type = VAR_STRING;
4570 rettv->vval.v_string = NULL;
4571
4572 if (check_for_string_arg(argvars, 0) == FAIL)
4573 return;
4574
4575 pat = tv_get_string(&argvars[0]);
4576 ExpandInit(&xpc);
4577
4578 cmdline_len = (int)STRLEN(pat);
4579 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
4580 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4581 xpc.xp_col = cmdline_len;
4582
4583 rettv->vval.v_string = get_cmdline_completion(&xpc);
4584
4585 ExpandCleanup(&xpc);
4586}
4587
4588/*
Girish Palya92f68e22025-04-21 11:12:41 +02004589 * "cmdcomplete_info()" function
4590 */
4591 void
4592f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4593{
4594 cmdline_info_T *ccline = get_cmdline_info();
4595 dict_T *retdict;
4596 list_T *li;
4597 int idx;
4598 int ret = OK;
4599
4600 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4601 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4602 return;
4603 retdict = rettv->vval.v_dict;
4604 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4605 if (ret == OK)
4606 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4607 if (ret == OK)
4608 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4609 if (ret == OK)
4610 {
4611 li = list_alloc();
4612 if (li == NULL)
4613 return;
4614 ret = dict_add_list(retdict, "matches", li);
4615 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4616 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4617 }
4618}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004619#endif // FEAT_EVAL
Girish Palya6b49fba2025-06-28 19:47:34 +02004620
4621/*
4622 * Copy a substring from the current buffer (curbuf), spanning from the given
4623 * 'start' position to the word boundary after 'end' position.
4624 * The copied string is stored in '*match', and the actual end position of the
4625 * matched text is returned in '*match_end'.
4626 */
4627 static int
4628copy_substring_from_pos(pos_T *start, pos_T *end, char_u **match,
4629 pos_T *match_end)
4630{
4631 char_u *word_end;
4632 char_u *line, *start_line, *end_line;
4633 int segment_len;
4634 linenr_T lnum;
4635 garray_T ga;
4636
4637 if (start->lnum > end->lnum
4638 || (start->lnum == end->lnum && start->col >= end->col))
4639 return FAIL; // invalid range
4640
4641 // Get line pointers
4642 start_line = ml_get(start->lnum);
4643 end_line = ml_get(end->lnum);
4644
4645 // Use a growable string (ga)
4646 ga_init2(&ga, 1, 128);
4647
4648 // Append start line from start->col to end
4649 char_u *start_ptr = start_line + start->col;
4650 int is_single_line = start->lnum == end->lnum;
4651
4652 segment_len = is_single_line ? (end->col - start->col)
glepnir7cf35bc2025-06-29 16:51:40 +02004653 : (int)STRLEN(start_ptr);
Girish Palya6b49fba2025-06-28 19:47:34 +02004654 if (ga_grow(&ga, segment_len + 1) != OK)
4655 return FAIL;
glepnir7cf35bc2025-06-29 16:51:40 +02004656
Girish Palya6b49fba2025-06-28 19:47:34 +02004657 ga_concat_len(&ga, start_ptr, segment_len);
4658 if (!is_single_line)
4659 ga_append(&ga, '\n');
4660
4661 // Append full lines between start and end
4662 if (!is_single_line)
glepnir7cf35bc2025-06-29 16:51:40 +02004663 {
Girish Palya6b49fba2025-06-28 19:47:34 +02004664 for (lnum = start->lnum + 1; lnum < end->lnum; lnum++)
4665 {
4666 line = ml_get(lnum);
4667 if (ga_grow(&ga, ml_get_len(lnum) + 1) != OK)
4668 return FAIL;
4669 ga_concat(&ga, line);
4670 ga_append(&ga, '\n');
4671 }
glepnir7cf35bc2025-06-29 16:51:40 +02004672 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004673
4674 // Append partial end line (up to word end)
4675 word_end = find_word_end(end_line + end->col);
4676 segment_len = (int)(word_end - end_line);
4677 if (ga_grow(&ga, segment_len) != OK)
4678 return FAIL;
4679 ga_concat_len(&ga, end_line + (is_single_line ? end->col : 0),
4680 segment_len - (is_single_line ? end->col : 0));
4681
4682 // Null-terminate
4683 if (ga_grow(&ga, 1) != OK)
4684 return FAIL;
4685 ga_append(&ga, NUL);
4686
4687 *match = (char_u *)ga.ga_data;
4688 match_end->lnum = end->lnum;
4689 match_end->col = segment_len;
4690
4691 return OK;
4692}
4693
4694/*
4695 * Search for strings matching "pat" in the specified range and return them.
4696 * Returns OK on success, FAIL otherwise.
4697 */
4698 static int
4699expand_pattern_in_buf(
4700 char_u *pat, // pattern to match
4701 int dir, // direction: FORWARD or BACKWARD
4702 char_u ***matches, // return: array with matched strings
4703 int *numMatches) // return: number of matches
4704{
4705 pos_T cur_match_pos, prev_match_pos, end_match_pos, word_end_pos;
4706 garray_T ga;
4707 int found_new_match;
4708 int looped_around = FALSE;
4709 int pat_len, match_len;
4710 int has_range = FALSE;
4711 int compl_started = FALSE;
glepnir7cf35bc2025-06-29 16:51:40 +02004712 int search_flags;
Girish Palya6b49fba2025-06-28 19:47:34 +02004713 char_u *match, *line, *word_end;
4714 regmatch_T regmatch;
4715
4716#ifdef FEAT_SEARCH_EXTRA
4717 has_range = search_first_line != 0;
4718#endif
4719
4720 *matches = NULL;
4721 *numMatches = 0;
4722
4723 if (pat == NULL || *pat == NUL)
4724 return FAIL;
4725
4726 pat_len = (int)STRLEN(pat);
4727 CLEAR_FIELD(cur_match_pos);
4728 CLEAR_FIELD(prev_match_pos);
4729#ifdef FEAT_SEARCH_EXTRA
4730 if (has_range)
4731 cur_match_pos.lnum = search_first_line;
4732 else
4733#endif
4734 cur_match_pos = pre_incsearch_pos;
4735
4736 search_flags = SEARCH_OPT | SEARCH_NOOF | SEARCH_PEEK | SEARCH_NFMSG
4737 | (has_range ? SEARCH_START : 0);
4738
4739 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4740 if (regmatch.regprog == NULL)
4741 return FAIL;
4742 regmatch.rm_ic = p_ic;
4743
4744 ga_init2(&ga, sizeof(char_u *), 10); // Use growable array of char_u*
4745
4746 for (;;)
4747 {
4748 ++emsg_off;
4749 ++msg_silent;
4750 found_new_match = searchit(NULL, curbuf, &cur_match_pos,
4751 &end_match_pos, dir, pat, pat_len, 1L,
4752 search_flags, RE_LAST, NULL);
4753 --msg_silent;
4754 --emsg_off;
4755
4756 if (found_new_match == FAIL)
4757 break;
4758
4759#ifdef FEAT_SEARCH_EXTRA
4760 // If in range mode, check if match is within the range
4761 if (has_range && (cur_match_pos.lnum < search_first_line
4762 || cur_match_pos.lnum > search_last_line))
4763 break;
4764#endif
4765
4766 if (compl_started)
4767 {
4768 // If we've looped back to an earlier match, stop
glepnir7cf35bc2025-06-29 16:51:40 +02004769 if ((dir == FORWARD && LTOREQ_POS(cur_match_pos, prev_match_pos)) ||
4770 (dir == BACKWARD && LTOREQ_POS(prev_match_pos, cur_match_pos)))
Girish Palya6b49fba2025-06-28 19:47:34 +02004771 {
4772 if (looped_around)
4773 break;
4774 else
4775 looped_around = TRUE;
4776 }
4777 }
4778
4779 compl_started = TRUE;
4780 prev_match_pos = cur_match_pos;
4781
4782 // Abort if user typed a character or interrupted
4783 if (char_avail() || got_int)
4784 {
4785 if (got_int)
4786 {
4787 (void)vpeekc(); // Remove <C-C> from input stream
4788 got_int = FALSE; // Don't abandon the command line
4789 }
4790 goto cleanup;
4791 }
4792
4793 // searchit() can return line number +1 past the last line when
4794 // searching for "foo\n" if "foo" is at end of buffer.
4795 if (end_match_pos.lnum > curbuf->b_ml.ml_line_count)
4796 {
4797 cur_match_pos.lnum = 1;
4798 cur_match_pos.col = 0;
4799 cur_match_pos.coladd = 0;
4800 continue;
4801 }
4802
4803 // Extract the matching text prepended to completed word
4804 if (!copy_substring_from_pos(&cur_match_pos, &end_match_pos, &match,
4805 &word_end_pos))
4806 break;
4807
4808 // Verify that the constructed match actually matches the pattern with
4809 // correct case sensitivity
4810 if (!vim_regexec_nl(&regmatch, match, (colnr_T)0))
4811 {
4812 vim_free(match);
4813 continue;
4814 }
4815 vim_free(match);
4816
4817 // Construct a new match from completed word appended to pattern itself
4818 line = ml_get(end_match_pos.lnum);
4819 word_end = find_word_end(line + end_match_pos.col); // col starts from 0
4820 match_len = (int)(word_end - (line + end_match_pos.col));
4821 match = alloc(match_len + pat_len + 1); // +1 for NUL
4822 if (match == NULL)
4823 goto cleanup;
4824 mch_memmove(match, pat, pat_len);
4825 if (match_len > 0)
4826 mch_memmove(match + pat_len, line + end_match_pos.col, match_len);
4827 match[pat_len + match_len] = NUL;
4828
4829 // Include this match if it is not a duplicate
glepnir7cf35bc2025-06-29 16:51:40 +02004830 for (int i = 0; i < ga.ga_len; ++i)
Girish Palya6b49fba2025-06-28 19:47:34 +02004831 {
4832 if (STRCMP(match, ((char_u **)ga.ga_data)[i]) == 0)
4833 {
4834 VIM_CLEAR(match);
4835 break;
4836 }
4837 }
4838 if (match != NULL)
4839 {
4840 if (ga_grow(&ga, 1) == FAIL)
4841 goto cleanup;
4842 ((char_u **)ga.ga_data)[ga.ga_len++] = match;
4843 if (ga.ga_len > TAG_MANY)
4844 break;
4845 }
4846 if (has_range)
4847 cur_match_pos = word_end_pos;
4848 }
4849
4850 vim_regfree(regmatch.regprog);
4851
4852 *matches = (char_u **)ga.ga_data;
4853 *numMatches = ga.ga_len;
4854 return OK;
4855
4856cleanup:
4857 vim_regfree(regmatch.regprog);
4858 ga_clear_strings(&ga);
4859 return FAIL;
4860}