blob: 75efe1c6236e88cf2c00328ea4273a4ecbe5222e [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
Girish Palya0cd7f352025-07-07 19:47:53 +0200414 pum_clear();
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000415 cmdline_pum_display();
416
417 return EXPAND_OK;
418}
419
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420/*
421 * Display the cmdline completion matches in a popup menu
422 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000423 void
424cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000425{
426 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
427}
428
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000429/*
430 * Returns TRUE if the cmdline completion popup menu is being displayed.
431 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000432 int
433cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000434{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100435 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000436}
437
438/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000439 * Remove the cmdline completion popup menu (if present), free the list of
440 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000441 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000442 void
zeertzjq1830e782025-03-13 20:29:13 +0100443cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000444{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000445 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100446 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100447#ifdef FEAT_EVAL
448 int save_RedrawingDisabled = RedrawingDisabled;
449 if (cclp->input_fn)
450 RedrawingDisabled = 0;
451#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000452
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000453 pum_undisplay();
454 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200455 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000456 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000457 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000458 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000459 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100460
461 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
462 // as a side effect.
463 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100464#ifdef FEAT_EVAL
465 if (cclp->input_fn)
466 RedrawingDisabled = save_RedrawingDisabled;
467#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000468}
469
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000470 void
471cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000472{
zeertzjq1830e782025-03-13 20:29:13 +0100473 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000474 wildmenu_cleanup(cclp);
475}
476
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000477/*
478 * Returns the starting column number to use for the cmdline completion popup
479 * menu.
480 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000481 int
482cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000483{
484 return compl_startcol;
485}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000486
Bram Moolenaar66b51422019-08-18 21:44:12 +0200487/*
zeertzjqd8c93402024-06-17 18:25:32 +0200488 * Returns the current cmdline completion pattern.
489 */
490 char_u *
491cmdline_compl_pattern(void)
492{
493 expand_T *xp = get_cmdline_info()->xpc;
494
495 return xp == NULL ? NULL : xp->xp_orig;
496}
497
498/*
499 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
500 */
501 int
502cmdline_compl_is_fuzzy(void)
503{
504 expand_T *xp = get_cmdline_info()->xpc;
505
506 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
507}
508
509/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000510 * Return the number of characters that should be skipped in a status match.
Girish Palya6b49fba2025-06-28 19:47:34 +0200511 * These are backslashes used for escaping. Do show backslashes in help tags
512 * and in search pattern completion matches.
Bram Moolenaard6e91382022-11-12 17:44:13 +0000513 */
514 static int
515skip_status_match_char(expand_T *xp, char_u *s)
516{
Girish Palya6b49fba2025-06-28 19:47:34 +0200517 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP
518 && xp->xp_context != EXPAND_PATTERN_IN_BUF)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000519#ifdef FEAT_MENU
520 || ((xp->xp_context == EXPAND_MENUS
521 || xp->xp_context == EXPAND_MENUNAMES)
522 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
523#endif
524 )
525 {
526#ifndef BACKSLASH_IN_FILENAME
527 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
528 return 2;
529#endif
530 return 1;
531 }
532 return 0;
533}
534
535/*
536 * Get the length of an item as it will be shown in the status line.
537 */
538 static int
539status_match_len(expand_T *xp, char_u *s)
540{
541 int len = 0;
542
543#ifdef FEAT_MENU
544 int emenu = xp->xp_context == EXPAND_MENUS
545 || xp->xp_context == EXPAND_MENUNAMES;
546
547 // Check for menu separators - replace with '|'.
548 if (emenu && menu_is_separator(s))
549 return 1;
550#endif
551
552 while (*s != NUL)
553 {
554 s += skip_status_match_char(xp, s);
555 len += ptr2cells(s);
556 MB_PTR_ADV(s);
557 }
558
559 return len;
560}
561
562/*
563 * Show wildchar matches in the status line.
564 * Show at least the "match" item.
565 * We start at item 'first_match' in the list and show all matches that fit.
566 *
567 * If inversion is possible we use it. Else '=' characters are used.
568 */
569 static void
570win_redr_status_matches(
571 expand_T *xp,
572 int num_matches,
573 char_u **matches, // list of matches
574 int match,
575 int showtail)
576{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000577 int row;
578 char_u *buf;
579 int len;
580 int clen; // length in screen cells
581 int fillchar;
582 int attr;
583 int i;
584 int highlight = TRUE;
585 char_u *selstart = NULL;
586 int selstart_col = 0;
587 char_u *selend = NULL;
588 static int first_match = 0;
589 int add_left = FALSE;
590 char_u *s;
591#ifdef FEAT_MENU
592 int emenu;
593#endif
594 int l;
595
596 if (matches == NULL) // interrupted completion?
597 return;
598
599 if (has_mbyte)
600 buf = alloc(Columns * MB_MAXBYTES + 1);
601 else
602 buf = alloc(Columns + 1);
603 if (buf == NULL)
604 return;
605
606 if (match == -1) // don't show match but original text
607 {
608 match = 0;
609 highlight = FALSE;
610 }
611 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000612 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000613 if (match == 0)
614 first_match = 0;
615 else if (match < first_match)
616 {
617 // jumping left, as far as we can go
618 first_match = match;
619 add_left = TRUE;
620 }
621 else
622 {
623 // check if match fits on the screen
624 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000625 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000626 if (first_match > 0)
627 clen += 2;
628 // jumping right, put match at the left
629 if ((long)clen > Columns)
630 {
631 first_match = match;
632 // if showing the last match, we can add some on the left
633 clen = 2;
634 for (i = match; i < num_matches; ++i)
635 {
zeertzjqc51a3762022-12-10 10:22:29 +0000636 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000637 if ((long)clen >= Columns)
638 break;
639 }
640 if (i == num_matches)
641 add_left = TRUE;
642 }
643 }
644 if (add_left)
645 while (first_match > 0)
646 {
zeertzjqc51a3762022-12-10 10:22:29 +0000647 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000648 if ((long)clen >= Columns)
649 break;
650 --first_match;
651 }
652
653 fillchar = fillchar_status(&attr, curwin);
654
655 if (first_match == 0)
656 {
657 *buf = NUL;
658 len = 0;
659 }
660 else
661 {
662 STRCPY(buf, "< ");
663 len = 2;
664 }
665 clen = len;
666
667 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000668 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000669 {
670 if (i == match)
671 {
672 selstart = buf + len;
673 selstart_col = clen;
674 }
675
zeertzjqc51a3762022-12-10 10:22:29 +0000676 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000677 // Check for menu separators - replace with '|'
678#ifdef FEAT_MENU
679 emenu = (xp->xp_context == EXPAND_MENUS
680 || xp->xp_context == EXPAND_MENUNAMES);
681 if (emenu && menu_is_separator(s))
682 {
683 STRCPY(buf + len, transchar('|'));
684 l = (int)STRLEN(buf + len);
685 len += l;
686 clen += l;
687 }
688 else
689#endif
John Marriotta494ce12025-07-03 21:28:50 +0200690 for ( ; *s != NUL; ++s)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000691 {
692 s += skip_status_match_char(xp, s);
693 clen += ptr2cells(s);
694 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
695 {
696 STRNCPY(buf + len, s, l);
697 s += l - 1;
698 len += l;
699 }
700 else
701 {
702 STRCPY(buf + len, transchar_byte(*s));
703 len += (int)STRLEN(buf + len);
704 }
705 }
706 if (i == match)
707 selend = buf + len;
708
709 *(buf + len++) = ' ';
710 *(buf + len++) = ' ';
711 clen += 2;
712 if (++i == num_matches)
713 break;
714 }
715
716 if (i != num_matches)
717 {
718 *(buf + len++) = '>';
719 ++clen;
720 }
721
722 buf[len] = NUL;
723
724 row = cmdline_row - 1;
725 if (row >= 0)
726 {
727 if (wild_menu_showing == 0)
728 {
729 if (msg_scrolled > 0)
730 {
731 // Put the wildmenu just above the command line. If there is
732 // no room, scroll the screen one line up.
733 if (cmdline_row == Rows - 1)
734 {
735 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
736 ++msg_scrolled;
737 }
738 else
739 {
740 ++cmdline_row;
741 ++row;
742 }
743 wild_menu_showing = WM_SCROLLED;
744 }
745 else
746 {
747 // Create status line if needed by setting 'laststatus' to 2.
748 // Set 'winminheight' to zero to avoid that the window is
749 // resized.
750 if (lastwin->w_status_height == 0)
751 {
752 save_p_ls = p_ls;
753 save_p_wmh = p_wmh;
754 p_ls = 2;
755 p_wmh = 0;
756 last_status(FALSE);
757 }
758 wild_menu_showing = WM_SHOWN;
759 }
760 }
761
762 screen_puts(buf, row, 0, attr);
763 if (selstart != NULL && highlight)
764 {
765 *selend = NUL;
766 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
767 }
768
769 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
770 }
771
772 win_redraw_last_status(topframe);
773 vim_free(buf);
774}
775
776/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000777 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200778 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000779 */
780 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100781get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000782{
glepnir977561a2025-02-13 20:48:56 +0100783 int findex = xp->xp_selected;
784 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000785
zeertzjqb6c900b2025-02-14 17:59:31 +0100786 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000787 if (xp->xp_numfiles <= 0)
788 return NULL;
789
790 if (mode == WILD_PREV)
791 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100792 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000793 if (findex == -1)
794 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100795 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796 --findex;
797 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000798 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000799 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100800 // Select the next entry
801 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000802 }
glepnir977561a2025-02-13 20:48:56 +0100803 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000804 {
glepnir977561a2025-02-13 20:48:56 +0100805 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
806 ht = pum_get_height();
807 if (ht > 3)
808 ht -= 2;
809
810 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000811 {
glepnir977561a2025-02-13 20:48:56 +0100812 if (findex == 0)
813 // at the first entry, don't select any entries
814 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100815 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100816 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000817 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100818 else
819 // go up by the pum height
820 findex = MAX(findex - ht, 0);
821 }
822 else // mode == WILD_PAGEDOWN
823 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100824 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100825 // at the last entry, don't select any entries
826 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100827 else if (findex < 0)
828 // no entry is selected, select the first entry
829 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100830 else
831 // go down by the pum height
832 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000833 }
834 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000835
glepnir977561a2025-02-13 20:48:56 +0100836 // Handle wrapping around
837 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000838 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100839 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100840 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000841 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000842 else
glepnir977561a2025-02-13 20:48:56 +0100843 // Wrap around to opposite end
844 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000845 }
glepnir977561a2025-02-13 20:48:56 +0100846
847 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000848 if (compl_match_array)
849 {
850 compl_selected = findex;
851 cmdline_pum_display();
852 }
853 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100854 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
855 cmd_showtail);
856
zeertzjqe9ef3472023-08-17 23:57:05 +0200857 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100858 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100859 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000860}
861
862/*
863 * Start the command-line expansion and get the matches.
864 */
865 static char_u *
866ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
867{
868 int non_suf_match; // number without matching suffix
869 int i;
870 char_u *ss = NULL;
871
872 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000873 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000874 options) == FAIL)
875 {
876#ifdef FNAME_ILLEGAL
877 // Illegal file name has been silently skipped. But when there
878 // are wildcards, the real problem is that there was no match,
879 // causing the pattern to be added, which has illegal characters.
880 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
881 semsg(_(e_no_match_str_2), str);
882#endif
883 }
884 else if (xp->xp_numfiles == 0)
885 {
886 if (!(options & WILD_SILENT))
887 semsg(_(e_no_match_str_2), str);
888 }
889 else
890 {
891 // Escape the matches for use on the command line.
892 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
893
894 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000895 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000896 {
897 if (xp->xp_numfiles)
898 non_suf_match = xp->xp_numfiles;
899 else
900 non_suf_match = 1;
901 if ((xp->xp_context == EXPAND_FILES
902 || xp->xp_context == EXPAND_DIRECTORIES)
903 && xp->xp_numfiles > 1)
904 {
905 // More than one match; check suffix.
906 // The files will have been sorted on matching suffix in
907 // expand_wildcards, only need to check the first two.
908 non_suf_match = 0;
909 for (i = 0; i < 2; ++i)
910 if (match_suffix(xp->xp_files[i]))
911 ++non_suf_match;
912 }
913 if (non_suf_match != 1)
914 {
915 // Can we ever get here unless it's while expanding
916 // interactively? If not, we can get rid of this all
917 // together. Don't really want to wait for this message
918 // (and possibly have to hit return to continue!).
919 if (!(options & WILD_SILENT))
920 emsg(_(e_too_many_file_names));
921 else if (!(options & WILD_NO_BEEP))
922 beep_flush();
923 }
924 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
925 ss = vim_strsave(xp->xp_files[0]);
926 }
927 }
928
929 return ss;
930}
931
932/*
933 * Return the longest common part in the list of cmdline completion matches.
934 */
935 static char_u *
936find_longest_match(expand_T *xp, int options)
937{
938 long_u len;
939 int mb_len = 1;
940 int c0, ci;
941 int i;
942 char_u *ss;
943
944 for (len = 0; xp->xp_files[0][len]; len += mb_len)
945 {
946 if (has_mbyte)
947 {
948 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
John Marriotta494ce12025-07-03 21:28:50 +0200949 c0 = (*mb_ptr2char)(&xp->xp_files[0][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000950 }
951 else
952 c0 = xp->xp_files[0][len];
953 for (i = 1; i < xp->xp_numfiles; ++i)
954 {
955 if (has_mbyte)
John Marriotta494ce12025-07-03 21:28:50 +0200956 ci = (*mb_ptr2char)(&xp->xp_files[i][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000957 else
958 ci = xp->xp_files[i][len];
959 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
960 || xp->xp_context == EXPAND_FILES
961 || xp->xp_context == EXPAND_SHELLCMD
962 || xp->xp_context == EXPAND_BUFFERS))
963 {
964 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
965 break;
966 }
967 else if (c0 != ci)
968 break;
969 }
970 if (i < xp->xp_numfiles)
971 {
972 if (!(options & WILD_NO_BEEP))
973 vim_beep(BO_WILD);
974 break;
975 }
976 }
977
978 ss = alloc(len + 1);
979 if (ss)
980 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
981
982 return ss;
983}
984
985/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000986 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 * Chars that should not be expanded must be preceded with a backslash.
988 * Return a pointer to allocated memory containing the new string.
989 * Return NULL for failure.
990 *
991 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200992 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
993 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200994 *
995 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
996 * is WILD_EXPAND_FREE or WILD_ALL.
997 *
998 * mode = WILD_FREE: just free previously expanded matches
999 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
1000 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
1001 * mode = WILD_NEXT: use next match in multiple match, wrap to first
1002 * mode = WILD_PREV: use previous match in multiple match, wrap to first
1003 * mode = WILD_ALL: return all matches concatenated
1004 * mode = WILD_LONGEST: return longest matched part
1005 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001006 * mode = WILD_APPLY: apply the item selected in the cmdline completion
1007 * popup menu and close the menu.
1008 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
1009 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001010 *
1011 * options = WILD_LIST_NOTFOUND: list entries without a match
1012 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
1013 * options = WILD_USE_NL: Use '\n' for WILD_ALL
1014 * options = WILD_NO_BEEP: Don't beep for multiple matches
1015 * options = WILD_ADD_SLASH: add a slash after directory names
1016 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
1017 * options = WILD_SILENT: don't print warning messages
1018 * options = WILD_ESCAPE: put backslash before special chars
1019 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001020 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021 *
1022 * The variables xp->xp_context and xp->xp_backslash must have been set!
1023 */
1024 char_u *
1025ExpandOne(
1026 expand_T *xp,
1027 char_u *str,
1028 char_u *orig, // allocated copy of original of expanded string
1029 int options,
1030 int mode)
1031{
1032 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 int orig_saved = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034
1035 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001036 if (mode == WILD_NEXT || mode == WILD_PREV
1037 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001038 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001040 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001041 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001042 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001043 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001044 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001045 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001046
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 // free old names
1048 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1049 {
1050 FreeWild(xp->xp_numfiles, xp->xp_files);
1051 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001052 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001053
1054 // The entries from xp_files may be used in the PUM, remove it.
1055 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001056 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001057 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001058 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059
1060 if (mode == WILD_FREE) // only release file name
1061 return NULL;
1062
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001063 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 {
zeertzjq28a23602023-09-29 19:58:35 +02001065 vim_free(xp->xp_orig);
1066 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001067 orig_saved = TRUE;
1068
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001069 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001070 }
1071
1072 // Find longest common part
1073 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1074 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001075 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001076 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077 }
1078
Bram Moolenaar57e95172022-08-20 19:26:14 +01001079 // Concatenate all matching names. Unless interrupted, this can be slow
1080 // and the result probably won't be used.
1081 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001082 {
John Marriotta494ce12025-07-03 21:28:50 +02001083 size_t ss_size = 0;
1084 char *prefix = "";
1085 char *suffix = (options & WILD_USE_NL) ? "\n" : " ";
1086 int n = xp->xp_numfiles - 1;
1087 int i;
1088
1089 if (xp->xp_prefix == XP_PREFIX_NO)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001090 {
John Marriotta494ce12025-07-03 21:28:50 +02001091 prefix = "no";
1092 ss_size = STRLEN_LITERAL("no") * n;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001093 }
John Marriotta494ce12025-07-03 21:28:50 +02001094 else if (xp->xp_prefix == XP_PREFIX_INV)
1095 {
1096 prefix = "inv";
1097 ss_size = STRLEN_LITERAL("inv") * n;
1098 }
1099
1100 for (i = 0; i < xp->xp_numfiles; ++i)
1101 ss_size += STRLEN(xp->xp_files[i]) + 1; // +1 for the suffix
1102 ++ss_size; // +1 for the NUL
1103
1104 ss = alloc(ss_size);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001105 if (ss != NULL)
1106 {
John Marriotta494ce12025-07-03 21:28:50 +02001107 size_t ss_len = 0;
1108
Bram Moolenaar66b51422019-08-18 21:44:12 +02001109 for (i = 0; i < xp->xp_numfiles; ++i)
1110 {
John Marriotta494ce12025-07-03 21:28:50 +02001111 ss_len += vim_snprintf_safelen(
1112 (char *)ss + ss_len,
1113 ss_size - ss_len,
1114 "%s%s%s",
1115 (i > 0) ? prefix : "",
1116 (char *)xp->xp_files[i],
1117 (i < n) ? suffix : "");
Bram Moolenaar66b51422019-08-18 21:44:12 +02001118 }
1119 }
1120 }
1121
1122 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1123 ExpandCleanup(xp);
1124
zeertzjq28a23602023-09-29 19:58:35 +02001125 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001126 if (!orig_saved)
1127 vim_free(orig);
1128
1129 return ss;
1130}
1131
1132/*
1133 * Prepare an expand structure for use.
1134 */
1135 void
1136ExpandInit(expand_T *xp)
1137{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001138 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001139 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001140 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001141 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001142}
1143
1144/*
1145 * Cleanup an expand structure after use.
1146 */
1147 void
1148ExpandCleanup(expand_T *xp)
1149{
1150 if (xp->xp_numfiles >= 0)
1151 {
1152 FreeWild(xp->xp_numfiles, xp->xp_files);
1153 xp->xp_numfiles = -1;
1154 }
zeertzjq28a23602023-09-29 19:58:35 +02001155 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001156}
1157
zeertzjqec270a52025-04-23 20:50:23 +02001158 void
1159clear_cmdline_orig(void)
1160{
1161 VIM_CLEAR(cmdline_orig);
1162}
1163
Bram Moolenaar66b51422019-08-18 21:44:12 +02001164/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001165 * Display one line of completion matches. Multiple matches are displayed in
1166 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001167 * matches - list of completion match names
1168 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001169 * lines - number of output lines
1170 * linenr - line number of matches to display
1171 * maxlen - maximum number of characters in each line
1172 * showtail - display only the tail of the full path of a file name
1173 * dir_attr - highlight attribute to use for directory names
1174 */
1175 static void
1176showmatches_oneline(
1177 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001178 char_u **matches,
1179 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001180 int lines,
1181 int linenr,
1182 int maxlen,
1183 int showtail,
1184 int dir_attr)
1185{
1186 int i, j;
1187 int isdir;
1188 int lastlen;
1189 char_u *p;
1190
1191 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001192 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001193 {
1194 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1195 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001196 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1197 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001198 msg_advance(maxlen + 1);
1199 msg_puts((char *)p);
1200 msg_advance(maxlen + 3);
1201 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1202 break;
1203 }
1204 for (i = maxlen - lastlen; --i >= 0; )
1205 msg_putchar(' ');
1206 if (xp->xp_context == EXPAND_FILES
1207 || xp->xp_context == EXPAND_SHELLCMD
1208 || xp->xp_context == EXPAND_BUFFERS)
1209 {
1210 // highlight directories
1211 if (xp->xp_numfiles != -1)
1212 {
1213 char_u *halved_slash;
1214 char_u *exp_path;
1215 char_u *path;
1216
1217 // Expansion was done before and special characters
1218 // were escaped, need to halve backslashes. Also
1219 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001220 exp_path = expand_env_save_opt(matches[j], TRUE);
1221 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001222 halved_slash = backslash_halve_save(path);
1223 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001224 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001225 vim_free(exp_path);
1226 if (halved_slash != path)
1227 vim_free(halved_slash);
1228 }
1229 else
1230 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001231 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001232 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001233 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001234 else
1235 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001237 TRUE);
1238 p = NameBuff;
1239 }
1240 }
1241 else
1242 {
1243 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001244 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001245 }
1246 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1247 }
1248 if (msg_col > 0) // when not wrapped around
1249 {
1250 msg_clr_eos();
1251 msg_putchar('\n');
1252 }
1253 out_flush(); // show one line at a time
1254}
1255
1256/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 * Show all matches for completion on the command line.
1258 * Returns EXPAND_NOTHING when the character that triggered expansion should
1259 * be inserted like a normal character.
1260 */
1261 int
1262showmatches(expand_T *xp, int wildmenu UNUSED)
1263{
1264 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001265 int numMatches;
1266 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001267 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268 int maxlen;
1269 int lines;
1270 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271 int attr;
1272 int showtail;
1273
Girish Palya92f68e22025-04-21 11:12:41 +02001274 // Save cmdline before expansion
1275 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001276 {
1277 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001278 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001279 }
Girish Palya92f68e22025-04-21 11:12:41 +02001280
Bram Moolenaar66b51422019-08-18 21:44:12 +02001281 if (xp->xp_numfiles == -1)
1282 {
1283 set_expand_context(xp);
1284 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001285 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001286 showtail = expand_showtail(xp);
1287 if (i != EXPAND_OK)
1288 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001289 }
1290 else
1291 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001292 numMatches = xp->xp_numfiles;
1293 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001294 showtail = cmd_showtail;
1295 }
1296
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001297 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001298 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001299 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001300
Bram Moolenaar66b51422019-08-18 21:44:12 +02001301 if (!wildmenu)
1302 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001303 msg_didany = FALSE; // lines_left will be set
1304 msg_start(); // prepare for paging
1305 msg_putchar('\n');
1306 out_flush();
1307 cmdline_row = msg_row;
1308 msg_didany = FALSE; // lines_left will be set again
1309 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001310 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001311
1312 if (got_int)
1313 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001314 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001315 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001316 else
1317 {
1318 // find the length of the longest file name
1319 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001320 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001321 {
1322 if (!showtail && (xp->xp_context == EXPAND_FILES
1323 || xp->xp_context == EXPAND_SHELLCMD
1324 || xp->xp_context == EXPAND_BUFFERS))
1325 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001326 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001327 j = vim_strsize(NameBuff);
1328 }
1329 else
zeertzjqc51a3762022-12-10 10:22:29 +00001330 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001331 if (j > maxlen)
1332 maxlen = j;
1333 }
1334
1335 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001336 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001337 else
1338 {
1339 // compute the number of columns and lines for the listing
1340 maxlen += 2; // two spaces between file names
1341 columns = ((int)Columns + 2) / maxlen;
1342 if (columns < 1)
1343 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001344 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001345 }
1346
1347 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1348
1349 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1350 {
1351 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1352 msg_clr_eos();
1353 msg_advance(maxlen - 3);
1354 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1355 }
1356
1357 // list the files line by line
1358 for (i = 0; i < lines; ++i)
1359 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001360 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001361 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001362 if (got_int)
1363 {
1364 got_int = FALSE;
1365 break;
1366 }
1367 }
1368
1369 // we redraw the command below the lines that we have just listed
1370 // This is a bit tricky, but it saves a lot of screen updating.
1371 cmdline_row = msg_row; // will put it back later
1372 }
1373
1374 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001375 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001376
1377 return EXPAND_OK;
1378}
1379
1380/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001381 * gettail() version for showmatches() and win_redr_status_matches():
1382 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001383 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001384 static char_u *
1385showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001386{
1387 char_u *p;
1388 char_u *t = s;
1389 int had_sep = FALSE;
1390
1391 for (p = s; *p != NUL; )
1392 {
1393 if (vim_ispathsep(*p)
1394#ifdef BACKSLASH_IN_FILENAME
1395 && !rem_backslash(p)
1396#endif
1397 )
1398 had_sep = TRUE;
1399 else if (had_sep)
1400 {
1401 t = p;
1402 had_sep = FALSE;
1403 }
1404 MB_PTR_ADV(p);
1405 }
1406 return t;
1407}
1408
1409/*
1410 * Return TRUE if we only need to show the tail of completion matches.
1411 * When not completing file names or there is a wildcard in the path FALSE is
1412 * returned.
1413 */
1414 static int
1415expand_showtail(expand_T *xp)
1416{
1417 char_u *s;
1418 char_u *end;
1419
1420 // When not completing file names a "/" may mean something different.
1421 if (xp->xp_context != EXPAND_FILES
1422 && xp->xp_context != EXPAND_SHELLCMD
1423 && xp->xp_context != EXPAND_DIRECTORIES)
1424 return FALSE;
1425
1426 end = gettail(xp->xp_pattern);
1427 if (end == xp->xp_pattern) // there is no path separator
1428 return FALSE;
1429
1430 for (s = xp->xp_pattern; s < end; s++)
1431 {
1432 // Skip escaped wildcards. Only when the backslash is not a path
1433 // separator, on DOS the '*' "path\*\file" must not be skipped.
1434 if (rem_backslash(s))
1435 ++s;
1436 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1437 return FALSE;
1438 }
1439 return TRUE;
1440}
1441
1442/*
1443 * Prepare a string for expansion.
1444 * When expanding file names: The string will be used with expand_wildcards().
1445 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1446 * When expanding other names: The string will be used with regcomp(). Copy
1447 * the name into allocated memory and prepend "^".
1448 */
1449 char_u *
1450addstar(
1451 char_u *fname,
1452 int len,
1453 int context) // EXPAND_FILES etc.
1454{
1455 char_u *retval;
1456 int i, j;
1457 int new_len;
1458 char_u *tail;
1459 int ends_in_star;
1460
1461 if (context != EXPAND_FILES
1462 && context != EXPAND_FILES_IN_PATH
1463 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001464 && context != EXPAND_DIRECTORIES
1465 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001466 {
1467 // Matching will be done internally (on something other than files).
1468 // So we convert the file-matching-type wildcards into our kind for
1469 // use with vim_regcomp(). First work out how long it will be:
1470
1471 // For help tags the translation is done in find_help_tags().
1472 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001473 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001474 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001475 || context == EXPAND_COLORS
1476 || context == EXPAND_COMPILER
1477 || context == EXPAND_OWNSYNTAX
1478 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001479 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001480 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001481 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001482 || ((context == EXPAND_TAGS_LISTFILES
1483 || context == EXPAND_TAGS)
1484 && fname[0] == '/'))
1485 retval = vim_strnsave(fname, len);
1486 else
1487 {
1488 new_len = len + 2; // +2 for '^' at start, NUL at end
1489 for (i = 0; i < len; i++)
1490 {
1491 if (fname[i] == '*' || fname[i] == '~')
1492 new_len++; // '*' needs to be replaced by ".*"
1493 // '~' needs to be replaced by "\~"
1494
1495 // Buffer names are like file names. "." should be literal
1496 if (context == EXPAND_BUFFERS && fname[i] == '.')
1497 new_len++; // "." becomes "\."
1498
1499 // Custom expansion takes care of special things, match
1500 // backslashes literally (perhaps also for other types?)
1501 if ((context == EXPAND_USER_DEFINED
1502 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1503 new_len++; // '\' becomes "\\"
1504 }
1505 retval = alloc(new_len);
1506 if (retval != NULL)
1507 {
1508 retval[0] = '^';
1509 j = 1;
1510 for (i = 0; i < len; i++, j++)
1511 {
1512 // Skip backslash. But why? At least keep it for custom
1513 // expansion.
1514 if (context != EXPAND_USER_DEFINED
1515 && context != EXPAND_USER_LIST
1516 && fname[i] == '\\'
1517 && ++i == len)
1518 break;
1519
1520 switch (fname[i])
1521 {
1522 case '*': retval[j++] = '.';
1523 break;
1524 case '~': retval[j++] = '\\';
1525 break;
1526 case '?': retval[j] = '.';
1527 continue;
1528 case '.': if (context == EXPAND_BUFFERS)
1529 retval[j++] = '\\';
1530 break;
1531 case '\\': if (context == EXPAND_USER_DEFINED
1532 || context == EXPAND_USER_LIST)
1533 retval[j++] = '\\';
1534 break;
1535 }
1536 retval[j] = fname[i];
1537 }
1538 retval[j] = NUL;
1539 }
1540 }
1541 }
1542 else
1543 {
1544 retval = alloc(len + 4);
1545 if (retval != NULL)
1546 {
1547 vim_strncpy(retval, fname, len);
1548
1549 // Don't add a star to *, ~, ~user, $var or `cmd`.
1550 // * would become **, which walks the whole tree.
1551 // ~ would be at the start of the file name, but not the tail.
1552 // $ could be anywhere in the tail.
1553 // ` could be anywhere in the file name.
1554 // When the name ends in '$' don't add a star, remove the '$'.
1555 tail = gettail(retval);
1556 ends_in_star = (len > 0 && retval[len - 1] == '*');
1557#ifndef BACKSLASH_IN_FILENAME
1558 for (i = len - 2; i >= 0; --i)
1559 {
1560 if (retval[i] != '\\')
1561 break;
1562 ends_in_star = !ends_in_star;
1563 }
1564#endif
1565 if ((*retval != '~' || tail != retval)
1566 && !ends_in_star
1567 && vim_strchr(tail, '$') == NULL
1568 && vim_strchr(retval, '`') == NULL)
1569 retval[len++] = '*';
1570 else if (len > 0 && retval[len - 1] == '$')
1571 --len;
1572 retval[len] = NUL;
1573 }
1574 }
1575 return retval;
1576}
1577
1578/*
1579 * Must parse the command line so far to work out what context we are in.
1580 * Completion can then be done based on that context.
1581 * This routine sets the variables:
1582 * xp->xp_pattern The start of the pattern to be expanded within
1583 * the command line (ends at the cursor).
1584 * xp->xp_context The type of thing to expand. Will be one of:
1585 *
1586 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1587 * the command line, like an unknown command. Caller
1588 * should beep.
1589 * EXPAND_NOTHING Unrecognised context for completion, use char like
1590 * a normal char, rather than for completion. eg
1591 * :s/^I/
1592 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1593 * it.
1594 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1595 * EXPAND_FILES After command with EX_XFILE set, or after setting
1596 * with P_EXPAND set. eg :e ^I, :w>>^I
1597 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001598 * when we know only directories are of interest.
1599 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001600 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1601 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1602 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1603 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1604 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1605 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1606 * EXPAND_EVENTS Complete event names
1607 * EXPAND_SYNTAX Complete :syntax command arguments
1608 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1609 * EXPAND_AUGROUP Complete autocommand group names
1610 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1611 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1612 * eg :unmap a^I , :cunab x^I
1613 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1614 * eg :call sub^I
1615 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1616 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1617 * names in expressions, eg :while s^I
1618 * EXPAND_ENV_VARS Complete environment variable names
1619 * EXPAND_USER Complete user names
Girish Palya6b49fba2025-06-28 19:47:34 +02001620 * EXPAND_PATTERN_IN_BUF Complete pattern in '/', '?', ':s', ':g', etc.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001621 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001622 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001623set_expand_context(expand_T *xp)
1624{
Girish Palya6b49fba2025-06-28 19:47:34 +02001625 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001626
Girish Palya6b49fba2025-06-28 19:47:34 +02001627 // Handle search commands: '/' or '?'
1628 if ((ccline->cmdfirstc == '/' || ccline->cmdfirstc == '?')
1629 && may_expand_pattern)
1630 {
1631 xp->xp_context = EXPAND_PATTERN_IN_BUF;
1632 xp->xp_search_dir = (ccline->cmdfirstc == '/') ? FORWARD : BACKWARD;
1633 xp->xp_pattern = ccline->cmdbuff;
1634 xp->xp_pattern_len = ccline->cmdpos;
1635#ifdef FEAT_SEARCH_EXTRA
1636 search_first_line = 0; // Search entire buffer
1637#endif
1638 return;
1639 }
1640
1641 // Only handle ':', '>', or '=' command-lines, or expression input
Bram Moolenaar66b51422019-08-18 21:44:12 +02001642 if (ccline->cmdfirstc != ':'
1643#ifdef FEAT_EVAL
1644 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1645 && !ccline->input_fn
1646#endif
Girish Palya6b49fba2025-06-28 19:47:34 +02001647 )
Bram Moolenaar66b51422019-08-18 21:44:12 +02001648 {
1649 xp->xp_context = EXPAND_NOTHING;
1650 return;
1651 }
Girish Palya6b49fba2025-06-28 19:47:34 +02001652
1653 // Fallback to command-line expansion
Bram Moolenaar66b51422019-08-18 21:44:12 +02001654 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1655}
1656
Bram Moolenaard0190392019-08-23 21:17:35 +02001657/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001658 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1659 * For user defined commands, the completion context is set in 'xp' and the
1660 * completion flags in 'complp'.
1661 *
1662 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001663 */
1664 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001666{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001667 char_u *p = NULL;
1668 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001669 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001670
1671 // Isolate the command and search for it in the command table.
1672 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001673 // - the 'k' command can directly be followed by any character, but do
1674 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1675 // find matches anywhere in the command name, do this only for command
1676 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001678 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001679 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001680 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001681 p = cmd + 1;
1682 }
1683 else
1684 {
1685 p = cmd;
1686 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1687 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001688 // A user command may contain digits.
1689 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1690 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 while (ASCII_ISALNUM(*p) || *p == '*')
1692 ++p;
1693 // for python 3.x: ":py3*" commands completion
1694 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1695 {
1696 ++p;
1697 while (ASCII_ISALPHA(*p) || *p == '*')
1698 ++p;
1699 }
1700 // check for non-alpha command
1701 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1702 ++p;
1703 len = (int)(p - cmd);
1704
1705 if (len == 0)
1706 {
1707 xp->xp_context = EXPAND_UNSUCCESSFUL;
1708 return NULL;
1709 }
1710
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001711 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001712
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001713 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001714 // Also when doing fuzzy expansion for non-shell commands, support
1715 // alphanumeric characters.
1716 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1717 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001718 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1719 ++p;
1720 }
1721
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001722 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001723 // character, complete the command name.
1724 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1725 return NULL;
1726
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001728 {
1729 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1730 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001732 p = cmd + 1;
1733 }
1734 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1735 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001736 eap->cmd = cmd;
1737 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001740 }
1741 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001743 {
1744 // Not still touching the command and it was an illegal one
1745 xp->xp_context = EXPAND_UNSUCCESSFUL;
1746 return NULL;
1747 }
1748
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 return p;
1750}
Bram Moolenaard0190392019-08-23 21:17:35 +02001751
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752/*
1753 * Set the completion context for a command argument with wild card characters.
1754 */
1755 static void
1756set_context_for_wildcard_arg(
1757 exarg_T *eap,
1758 char_u *arg,
1759 int usefilter,
1760 expand_T *xp,
1761 int *complp)
1762{
1763 char_u *p;
1764 int c;
1765 int in_quote = FALSE;
1766 char_u *bow = NULL; // Beginning of word
1767 int len = 0;
1768
1769 // Allow spaces within back-quotes to count as part of the argument
1770 // being expanded.
1771 xp->xp_pattern = skipwhite(arg);
1772 p = xp->xp_pattern;
1773 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001774 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001775 if (has_mbyte)
1776 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001777 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001778 c = *p;
1779 if (c == '\\' && p[1] != NUL)
1780 ++p;
1781 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001782 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001783 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001784 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001785 xp->xp_pattern = p;
1786 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001787 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001788 in_quote = !in_quote;
1789 }
1790 // An argument can contain just about everything, except
1791 // characters that end the command and white space.
1792 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001793#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001794 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001795#endif
1796 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001797 {
1798 len = 0; // avoid getting stuck when space is in 'isfname'
1799 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001800 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001801 if (has_mbyte)
1802 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001803 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001804 c = *p;
1805 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001806 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001807 if (has_mbyte)
1808 len = (*mb_ptr2len)(p);
1809 else
1810 len = 1;
1811 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001812 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001813 if (in_quote)
1814 bow = p;
1815 else
1816 xp->xp_pattern = p;
1817 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001818 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001819 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001820 }
1821
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001822 // If we are still inside the quotes, and we passed a space, just
1823 // expand from there.
1824 if (bow != NULL && in_quote)
1825 xp->xp_pattern = bow;
1826 xp->xp_context = EXPAND_FILES;
1827
1828 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001829 if (usefilter
1830 || (eap != NULL
1831 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1832 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001833 {
1834#ifndef BACKSLASH_IN_FILENAME
1835 xp->xp_shell = TRUE;
1836#endif
1837 // When still after the command name expand executables.
1838 if (xp->xp_pattern == skipwhite(arg))
1839 xp->xp_context = EXPAND_SHELLCMD;
1840 }
1841
1842 // Check for environment variable.
1843 if (*xp->xp_pattern == '$')
1844 {
1845 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1846 if (!vim_isIDc(*p))
1847 break;
1848 if (*p == NUL)
1849 {
1850 xp->xp_context = EXPAND_ENV_VARS;
1851 ++xp->xp_pattern;
1852 // Avoid that the assignment uses EXPAND_FILES again.
1853 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1854 *complp = EXPAND_ENV_VARS;
1855 }
1856 }
1857 // Check for user names.
1858 if (*xp->xp_pattern == '~')
1859 {
1860 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1861 ;
1862 // Complete ~user only if it partially matches a user name.
1863 // A full match ~user<Tab> will be replaced by user's home
1864 // directory i.e. something like ~user<Tab> -> /home/user/
1865 if (*p == NUL && p > xp->xp_pattern + 1
1866 && match_user(xp->xp_pattern + 1) >= 1)
1867 {
1868 xp->xp_context = EXPAND_USER;
1869 ++xp->xp_pattern;
1870 }
1871 }
1872}
1873
1874/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001875 * Set the completion context for the "++opt=arg" argument. Always returns
1876 * NULL.
1877 */
1878 static char_u *
1879set_context_in_argopt(expand_T *xp, char_u *arg)
1880{
1881 char_u *p;
1882
1883 p = vim_strchr(arg, '=');
1884 if (p == NULL)
1885 xp->xp_pattern = arg;
1886 else
1887 xp->xp_pattern = p + 1;
1888
1889 xp->xp_context = EXPAND_ARGOPT;
1890 return NULL;
1891}
1892
1893#ifdef FEAT_TERMINAL
1894/*
1895 * Set the completion context for :terminal's [options]. Always returns NULL.
1896 */
1897 static char_u *
1898set_context_in_terminalopt(expand_T *xp, char_u *arg)
1899{
1900 char_u *p;
1901
1902 p = vim_strchr(arg, '=');
1903 if (p == NULL)
1904 xp->xp_pattern = arg;
1905 else
1906 xp->xp_pattern = p + 1;
1907
1908 xp->xp_context = EXPAND_TERMINALOPT;
1909 return NULL;
1910}
1911#endif
1912
1913/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001914 * Set the completion context for the :filter command. Returns a pointer to the
1915 * next command after the :filter command.
1916 */
1917 static char_u *
1918set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1919{
1920 if (*arg != NUL)
1921 arg = skip_vimgrep_pat(arg, NULL, NULL);
1922 if (arg == NULL || *arg == NUL)
1923 {
1924 xp->xp_context = EXPAND_NOTHING;
1925 return NULL;
1926 }
1927 return skipwhite(arg);
1928}
1929
1930#ifdef FEAT_SEARCH_EXTRA
1931/*
1932 * Set the completion context for the :match command. Returns a pointer to the
1933 * next command after the :match command.
1934 */
1935 static char_u *
1936set_context_in_match_cmd(expand_T *xp, char_u *arg)
1937{
1938 if (*arg == NUL || !ends_excmd(*arg))
1939 {
1940 // also complete "None"
1941 set_context_in_echohl_cmd(xp, arg);
1942 arg = skipwhite(skiptowhite(arg));
1943 if (*arg != NUL)
1944 {
1945 xp->xp_context = EXPAND_NOTHING;
1946 arg = skip_regexp(arg + 1, *arg, magic_isset());
1947 }
1948 }
1949 return find_nextcmd(arg);
1950}
1951#endif
1952
1953/*
1954 * Returns a pointer to the next command after a :global or a :v command.
1955 * Returns NULL if there is no next command.
1956 */
1957 static char_u *
1958find_cmd_after_global_cmd(char_u *arg)
1959{
1960 int delim;
1961
1962 delim = *arg; // get the delimiter
1963 if (delim)
1964 ++arg; // skip delimiter if there is one
1965
1966 while (arg[0] != NUL && arg[0] != delim)
1967 {
1968 if (arg[0] == '\\' && arg[1] != NUL)
1969 ++arg;
1970 ++arg;
1971 }
1972 if (arg[0] != NUL)
1973 return arg + 1;
1974
1975 return NULL;
1976}
1977
1978/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001979 * Returns a pointer to the next command after a :substitute or a :& command.
1980 * Returns NULL if there is no next command.
1981 */
1982 static char_u *
1983find_cmd_after_substitute_cmd(char_u *arg)
1984{
1985 int delim;
1986
1987 delim = *arg;
1988 if (delim)
1989 {
1990 // skip "from" part
1991 ++arg;
1992 arg = skip_regexp(arg, delim, magic_isset());
1993
1994 if (arg[0] != NUL && arg[0] == delim)
1995 {
1996 // skip "to" part
1997 ++arg;
1998 while (arg[0] != NUL && arg[0] != delim)
1999 {
2000 if (arg[0] == '\\' && arg[1] != NUL)
2001 ++arg;
2002 ++arg;
2003 }
2004 if (arg[0] != NUL) // skip delimiter
2005 ++arg;
2006 }
2007 }
2008 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
2009 ++arg;
2010 if (arg[0] != NUL)
2011 return arg;
2012
2013 return NULL;
2014}
2015
2016/*
2017 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
2018 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
2019 * Returns NULL if there is no next command.
2020 */
2021 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002022find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002023{
2024 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002025 if (*arg != '/')
2026 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002027
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002028 // Match regexp, not just whole words
2029 for (++arg; *arg && *arg != '/'; arg++)
2030 if (*arg == '\\' && arg[1] != NUL)
2031 arg++;
2032 if (*arg)
2033 {
2034 arg = skipwhite(arg + 1);
2035
2036 // Check for trailing illegal characters
2037 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
2038 xp->xp_context = EXPAND_NOTHING;
2039 else
2040 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002041 }
2042
2043 return NULL;
2044}
2045
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002046#ifdef FEAT_EVAL
2047/*
2048 * Set the completion context for the :unlet command. Always returns NULL.
2049 */
2050 static char_u *
2051set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2052{
2053 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2054 arg = xp->xp_pattern + 1;
2055
2056 xp->xp_context = EXPAND_USER_VARS;
2057 xp->xp_pattern = arg;
2058
2059 if (*xp->xp_pattern == '$')
2060 {
2061 xp->xp_context = EXPAND_ENV_VARS;
2062 ++xp->xp_pattern;
2063 }
2064
2065 return NULL;
2066}
2067#endif
2068
2069#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2070/*
2071 * Set the completion context for the :language command. Always returns NULL.
2072 */
2073 static char_u *
2074set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2075{
2076 char_u *p;
2077
2078 p = skiptowhite(arg);
2079 if (*p == NUL)
2080 {
2081 xp->xp_context = EXPAND_LANGUAGE;
2082 xp->xp_pattern = arg;
2083 }
2084 else
2085 {
2086 if ( STRNCMP(arg, "messages", p - arg) == 0
2087 || STRNCMP(arg, "ctype", p - arg) == 0
2088 || STRNCMP(arg, "time", p - arg) == 0
2089 || STRNCMP(arg, "collate", p - arg) == 0)
2090 {
2091 xp->xp_context = EXPAND_LOCALES;
2092 xp->xp_pattern = skipwhite(p);
2093 }
2094 else
2095 xp->xp_context = EXPAND_NOTHING;
2096 }
2097
2098 return NULL;
2099}
2100#endif
2101
Christian Brabandta3422aa2025-04-23 21:04:24 +02002102static enum
2103{
2104 EXP_FILETYPECMD_ALL, // expand all :filetype values
2105 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2106 EXP_FILETYPECMD_INDENT, // expand indent on off
2107 EXP_FILETYPECMD_ONOFF, // expand on off
2108} filetype_expand_what;
2109
2110#define EXPAND_FILETYPECMD_PLUGIN 0x01
2111#define EXPAND_FILETYPECMD_INDENT 0x02
2112#define EXPAND_FILETYPECMD_ONOFF 0x04
2113
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002114#ifdef FEAT_EVAL
2115static enum
2116{
2117 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002118 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2119 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002120} breakpt_expand_what;
2121
2122/*
2123 * Set the completion context for the :breakadd command. Always returns NULL.
2124 */
2125 static char_u *
2126set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2127{
2128 char_u *p;
2129 char_u *subcmd_start;
2130
2131 xp->xp_context = EXPAND_BREAKPOINT;
2132 xp->xp_pattern = arg;
2133
2134 if (cmdidx == CMD_breakadd)
2135 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002136 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002137 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002138 else
2139 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002140
2141 p = skipwhite(arg);
2142 if (*p == NUL)
2143 return NULL;
2144 subcmd_start = p;
2145
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002146 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002147 {
2148 // :breakadd file [lnum] <filename>
2149 // :breakadd func [lnum] <funcname>
2150 p += 4;
2151 p = skipwhite(p);
2152
2153 // skip line number (if specified)
2154 if (VIM_ISDIGIT(*p))
2155 {
2156 p = skipdigits(p);
2157 if (*p != ' ')
2158 {
2159 xp->xp_context = EXPAND_NOTHING;
2160 return NULL;
2161 }
2162 p = skipwhite(p);
2163 }
2164 if (STRNCMP("file", subcmd_start, 4) == 0)
2165 xp->xp_context = EXPAND_FILES;
2166 else
2167 xp->xp_context = EXPAND_USER_FUNC;
2168 xp->xp_pattern = p;
2169 }
2170 else if (STRNCMP("expr ", p, 5) == 0)
2171 {
2172 // :breakadd expr <expression>
2173 xp->xp_context = EXPAND_EXPRESSION;
2174 xp->xp_pattern = skipwhite(p + 5);
2175 }
2176
2177 return NULL;
2178}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002179
2180 static char_u *
2181set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2182{
2183 char_u *p;
2184
2185 xp->xp_context = EXPAND_NOTHING;
2186 xp->xp_pattern = NULL;
2187
2188 p = skipwhite(arg);
2189 if (VIM_ISDIGIT(*p))
2190 return NULL;
2191
2192 xp->xp_context = EXPAND_SCRIPTNAMES;
2193 xp->xp_pattern = p;
2194
2195 return NULL;
2196}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002197#endif
2198
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002199/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002200 * Set the completion context for the :filetype command. Always returns NULL.
2201 */
2202 static char_u *
2203set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2204{
2205 char_u *p;
2206 int val = 0;
2207
2208 xp->xp_context = EXPAND_FILETYPECMD;
2209 xp->xp_pattern = arg;
2210 filetype_expand_what = EXP_FILETYPECMD_ALL;
2211
2212 p = skipwhite(arg);
2213 if (*p == NUL)
2214 return NULL;
2215
2216 for (;;)
2217 {
2218 if (STRNCMP(p, "plugin", 6) == 0)
2219 {
2220 val |= EXPAND_FILETYPECMD_PLUGIN;
2221 p = skipwhite(p + 6);
2222 continue;
2223 }
2224 if (STRNCMP(p, "indent", 6) == 0)
2225 {
2226 val |= EXPAND_FILETYPECMD_INDENT;
2227 p = skipwhite(p + 6);
2228 continue;
2229 }
2230 break;
2231 }
2232
2233 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2234 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2235 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2236 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2237 else if ((val & EXPAND_FILETYPECMD_INDENT))
2238 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2239
2240 xp->xp_pattern = p;
2241
2242 return NULL;
2243}
2244
Girish Palya6b49fba2025-06-28 19:47:34 +02002245/*
2246 * Sets the completion context for commands that involve a search pattern
2247 * and a line range (e.g., :s, :g, :v).
2248 */
2249 static void
2250set_context_with_pattern(expand_T *xp)
2251{
2252 int skiplen = 0;
2253 cmdline_info_T *ccline = get_cmdline_info();
2254#ifdef FEAT_SEARCH_EXTRA
2255 int dummy, patlen, retval;
2256
2257 ++emsg_off;
2258 retval = parse_pattern_and_range(&pre_incsearch_pos, &dummy, &skiplen,
2259 &patlen);
2260 --emsg_off;
2261
2262 // Check if cursor is within search pattern
2263 if (!retval || ccline->cmdpos <= skiplen
2264 || ccline->cmdpos > skiplen + patlen)
2265 return;
2266#endif
2267
2268 xp->xp_pattern = ccline->cmdbuff + skiplen;
2269 xp->xp_pattern_len = ccline->cmdpos - skiplen;
2270 xp->xp_context = EXPAND_PATTERN_IN_BUF;
2271 xp->xp_search_dir = FORWARD;
2272}
Christian Brabandta3422aa2025-04-23 21:04:24 +02002273
2274/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002275 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2276 * The argument to the command is 'arg' and the argument flags is 'argt'.
2277 * For user-defined commands and for environment variables, 'compl' has the
2278 * completion type.
2279 * Returns a pointer to the next command. Returns NULL if there is no next
2280 * command.
2281 */
2282 static char_u *
2283set_context_by_cmdname(
2284 char_u *cmd,
2285 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002286 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002287 char_u *arg,
2288 long argt,
2289 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002290 int forceit)
2291{
Girish Palya6b49fba2025-06-28 19:47:34 +02002292 char_u *nextcmd;
2293
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002294 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002295 {
2296 case CMD_find:
2297 case CMD_sfind:
2298 case CMD_tabfind:
2299 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002300 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002301 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002302 break;
2303 case CMD_cd:
2304 case CMD_chdir:
2305 case CMD_tcd:
2306 case CMD_tchdir:
2307 case CMD_lcd:
2308 case CMD_lchdir:
2309 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002310 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002311 break;
2312 case CMD_help:
2313 xp->xp_context = EXPAND_HELP;
2314 xp->xp_pattern = arg;
2315 break;
2316
2317 // Command modifiers: return the argument.
2318 // Also for commands with an argument that is a command.
2319 case CMD_aboveleft:
2320 case CMD_argdo:
2321 case CMD_belowright:
2322 case CMD_botright:
2323 case CMD_browse:
2324 case CMD_bufdo:
2325 case CMD_cdo:
2326 case CMD_cfdo:
2327 case CMD_confirm:
2328 case CMD_debug:
2329 case CMD_folddoclosed:
2330 case CMD_folddoopen:
2331 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002332 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002333 case CMD_keepalt:
2334 case CMD_keepjumps:
2335 case CMD_keepmarks:
2336 case CMD_keeppatterns:
2337 case CMD_ldo:
2338 case CMD_leftabove:
2339 case CMD_lfdo:
2340 case CMD_lockmarks:
2341 case CMD_noautocmd:
2342 case CMD_noswapfile:
2343 case CMD_rightbelow:
2344 case CMD_sandbox:
2345 case CMD_silent:
2346 case CMD_tab:
2347 case CMD_tabdo:
2348 case CMD_topleft:
2349 case CMD_verbose:
2350 case CMD_vertical:
2351 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002352 case CMD_vim9cmd:
2353 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002354 return arg;
2355
2356 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002357 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002358
2359#ifdef FEAT_SEARCH_EXTRA
2360 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002361 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002362#endif
2363
2364 // All completion for the +cmdline_compl feature goes here.
2365
2366 case CMD_command:
2367 return set_context_in_user_cmd(xp, arg);
2368
2369 case CMD_delcommand:
2370 xp->xp_context = EXPAND_USER_COMMANDS;
2371 xp->xp_pattern = arg;
2372 break;
2373
2374 case CMD_global:
2375 case CMD_vglobal:
Girish Palya6b49fba2025-06-28 19:47:34 +02002376 nextcmd = find_cmd_after_global_cmd(arg);
2377 if (!nextcmd && may_expand_pattern)
2378 set_context_with_pattern(xp);
2379 return nextcmd;
2380
Bram Moolenaard0190392019-08-23 21:17:35 +02002381 case CMD_and:
2382 case CMD_substitute:
Girish Palya6b49fba2025-06-28 19:47:34 +02002383 nextcmd = find_cmd_after_substitute_cmd(arg);
2384 if (!nextcmd && may_expand_pattern)
2385 set_context_with_pattern(xp);
2386 return nextcmd;
2387
Bram Moolenaard0190392019-08-23 21:17:35 +02002388 case CMD_isearch:
2389 case CMD_dsearch:
2390 case CMD_ilist:
2391 case CMD_dlist:
2392 case CMD_ijump:
2393 case CMD_psearch:
2394 case CMD_djump:
2395 case CMD_isplit:
2396 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002397 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002398 case CMD_autocmd:
2399 return set_context_in_autocmd(xp, arg, FALSE);
2400 case CMD_doautocmd:
2401 case CMD_doautoall:
2402 return set_context_in_autocmd(xp, arg, TRUE);
2403 case CMD_set:
2404 set_context_in_set_cmd(xp, arg, 0);
2405 break;
2406 case CMD_setglobal:
2407 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2408 break;
2409 case CMD_setlocal:
2410 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2411 break;
2412 case CMD_tag:
2413 case CMD_stag:
2414 case CMD_ptag:
2415 case CMD_ltag:
2416 case CMD_tselect:
2417 case CMD_stselect:
2418 case CMD_ptselect:
2419 case CMD_tjump:
2420 case CMD_stjump:
2421 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002422 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002423 xp->xp_context = EXPAND_TAGS_LISTFILES;
2424 else
2425 xp->xp_context = EXPAND_TAGS;
2426 xp->xp_pattern = arg;
2427 break;
2428 case CMD_augroup:
2429 xp->xp_context = EXPAND_AUGROUP;
2430 xp->xp_pattern = arg;
2431 break;
2432#ifdef FEAT_SYN_HL
2433 case CMD_syntax:
2434 set_context_in_syntax_cmd(xp, arg);
2435 break;
2436#endif
2437#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002438 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002439 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002440 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002441 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002442 case CMD_if:
2443 case CMD_elseif:
2444 case CMD_while:
2445 case CMD_for:
2446 case CMD_echo:
2447 case CMD_echon:
2448 case CMD_execute:
2449 case CMD_echomsg:
2450 case CMD_echoerr:
2451 case CMD_call:
2452 case CMD_return:
2453 case CMD_cexpr:
2454 case CMD_caddexpr:
2455 case CMD_cgetexpr:
2456 case CMD_lexpr:
2457 case CMD_laddexpr:
2458 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002459 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002460 break;
2461
2462 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002463 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002464 case CMD_function:
2465 case CMD_delfunction:
2466 xp->xp_context = EXPAND_USER_FUNC;
2467 xp->xp_pattern = arg;
2468 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002469 case CMD_disassemble:
2470 set_context_in_disassemble_cmd(xp, arg);
2471 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002472
2473 case CMD_echohl:
2474 set_context_in_echohl_cmd(xp, arg);
2475 break;
2476#endif
2477 case CMD_highlight:
2478 set_context_in_highlight_cmd(xp, arg);
2479 break;
2480#ifdef FEAT_CSCOPE
2481 case CMD_cscope:
2482 case CMD_lcscope:
2483 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002484 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002485 break;
2486#endif
2487#ifdef FEAT_SIGNS
2488 case CMD_sign:
2489 set_context_in_sign_cmd(xp, arg);
2490 break;
2491#endif
2492 case CMD_bdelete:
2493 case CMD_bwipeout:
2494 case CMD_bunload:
2495 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2496 arg = xp->xp_pattern + 1;
2497 // FALLTHROUGH
2498 case CMD_buffer:
2499 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002500 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002501 case CMD_checktime:
2502 xp->xp_context = EXPAND_BUFFERS;
2503 xp->xp_pattern = arg;
2504 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002505#ifdef FEAT_DIFF
2506 case CMD_diffget:
2507 case CMD_diffput:
2508 // If current buffer is in diff mode, complete buffer names
2509 // which are in diff mode, and different than current buffer.
2510 xp->xp_context = EXPAND_DIFF_BUFFERS;
2511 xp->xp_pattern = arg;
2512 break;
2513#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002514 case CMD_USER:
2515 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002516 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2517 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002518
2519 case CMD_map: case CMD_noremap:
2520 case CMD_nmap: case CMD_nnoremap:
2521 case CMD_vmap: case CMD_vnoremap:
2522 case CMD_omap: case CMD_onoremap:
2523 case CMD_imap: case CMD_inoremap:
2524 case CMD_cmap: case CMD_cnoremap:
2525 case CMD_lmap: case CMD_lnoremap:
2526 case CMD_smap: case CMD_snoremap:
2527 case CMD_tmap: case CMD_tnoremap:
2528 case CMD_xmap: case CMD_xnoremap:
2529 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002530 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002531 case CMD_unmap:
2532 case CMD_nunmap:
2533 case CMD_vunmap:
2534 case CMD_ounmap:
2535 case CMD_iunmap:
2536 case CMD_cunmap:
2537 case CMD_lunmap:
2538 case CMD_sunmap:
2539 case CMD_tunmap:
2540 case CMD_xunmap:
2541 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002542 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002543 case CMD_mapclear:
2544 case CMD_nmapclear:
2545 case CMD_vmapclear:
2546 case CMD_omapclear:
2547 case CMD_imapclear:
2548 case CMD_cmapclear:
2549 case CMD_lmapclear:
2550 case CMD_smapclear:
2551 case CMD_tmapclear:
2552 case CMD_xmapclear:
2553 xp->xp_context = EXPAND_MAPCLEAR;
2554 xp->xp_pattern = arg;
2555 break;
2556
2557 case CMD_abbreviate: case CMD_noreabbrev:
2558 case CMD_cabbrev: case CMD_cnoreabbrev:
2559 case CMD_iabbrev: case CMD_inoreabbrev:
2560 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002561 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002562 case CMD_unabbreviate:
2563 case CMD_cunabbrev:
2564 case CMD_iunabbrev:
2565 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002566 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002567#ifdef FEAT_MENU
2568 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2569 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2570 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2571 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2572 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2573 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2574 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2575 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2576 case CMD_tmenu: case CMD_tunmenu:
2577 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2578 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2579#endif
2580
2581 case CMD_colorscheme:
2582 xp->xp_context = EXPAND_COLORS;
2583 xp->xp_pattern = arg;
2584 break;
2585
2586 case CMD_compiler:
2587 xp->xp_context = EXPAND_COMPILER;
2588 xp->xp_pattern = arg;
2589 break;
2590
2591 case CMD_ownsyntax:
2592 xp->xp_context = EXPAND_OWNSYNTAX;
2593 xp->xp_pattern = arg;
2594 break;
2595
2596 case CMD_setfiletype:
2597 xp->xp_context = EXPAND_FILETYPE;
2598 xp->xp_pattern = arg;
2599 break;
2600
2601 case CMD_packadd:
2602 xp->xp_context = EXPAND_PACKADD;
2603 xp->xp_pattern = arg;
2604 break;
2605
zeertzjqb0d45ec2023-01-25 15:04:22 +00002606 case CMD_runtime:
2607 set_context_in_runtime_cmd(xp, arg);
2608 break;
2609
Bram Moolenaard0190392019-08-23 21:17:35 +02002610#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2611 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002612 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002613#endif
2614#if defined(FEAT_PROFILE)
2615 case CMD_profile:
2616 set_context_in_profile_cmd(xp, arg);
2617 break;
2618#endif
2619 case CMD_behave:
2620 xp->xp_context = EXPAND_BEHAVE;
2621 xp->xp_pattern = arg;
2622 break;
2623
2624 case CMD_messages:
2625 xp->xp_context = EXPAND_MESSAGES;
2626 xp->xp_pattern = arg;
2627 break;
2628
2629 case CMD_history:
2630 xp->xp_context = EXPAND_HISTORY;
2631 xp->xp_pattern = arg;
2632 break;
2633#if defined(FEAT_PROFILE)
2634 case CMD_syntime:
2635 xp->xp_context = EXPAND_SYNTIME;
2636 xp->xp_pattern = arg;
2637 break;
2638#endif
2639
2640 case CMD_argdelete:
2641 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2642 arg = xp->xp_pattern + 1;
2643 xp->xp_context = EXPAND_ARGLIST;
2644 xp->xp_pattern = arg;
2645 break;
2646
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002647#ifdef FEAT_EVAL
2648 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002649 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002650 case CMD_breakdel:
2651 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002652
2653 case CMD_scriptnames:
2654 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002655#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002656 case CMD_filetype:
2657 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002658
Bram Moolenaard0190392019-08-23 21:17:35 +02002659 default:
2660 break;
2661 }
2662 return NULL;
2663}
2664
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002665/*
2666 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2667 * we don't need/want deleted. Maybe this could be done better if we didn't
2668 * repeat all this stuff. The only problem is that they may not stay
2669 * perfectly compatible with each other, but then the command line syntax
2670 * probably won't change that much -- webb.
2671 */
2672 static char_u *
2673set_one_cmd_context(
2674 expand_T *xp,
2675 char_u *buff) // buffer for command string
2676{
2677 char_u *p;
2678 char_u *cmd, *arg;
2679 int len = 0;
2680 exarg_T ea;
2681 int compl = EXPAND_NOTHING;
2682 int forceit = FALSE;
2683 int usefilter = FALSE; // filter instead of file name
2684
2685 ExpandInit(xp);
2686 xp->xp_pattern = buff;
2687 xp->xp_line = buff;
2688 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2689 ea.argt = 0;
2690
2691 // 1. skip comment lines and leading space, colons or bars
2692 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2693 ;
2694 xp->xp_pattern = cmd;
2695
2696 if (*cmd == NUL)
2697 return NULL;
2698 if (*cmd == '"') // ignore comment lines
2699 {
2700 xp->xp_context = EXPAND_NOTHING;
2701 return NULL;
2702 }
2703
2704 // 3. Skip over the range to find the command.
2705 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2706 xp->xp_pattern = cmd;
2707 if (*cmd == NUL)
2708 return NULL;
2709 if (*cmd == '"')
2710 {
2711 xp->xp_context = EXPAND_NOTHING;
2712 return NULL;
2713 }
2714
2715 if (*cmd == '|' || *cmd == '\n')
2716 return cmd + 1; // There's another command
2717
2718 // Get the command index.
2719 p = set_cmd_index(cmd, &ea, xp, &compl);
2720 if (p == NULL)
2721 return NULL;
2722
2723 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2724
2725 if (*p == '!') // forced commands
2726 {
2727 forceit = TRUE;
2728 ++p;
2729 }
2730
2731 // 6. parse arguments
2732 if (!IS_USER_CMDIDX(ea.cmdidx))
2733 ea.argt = excmd_get_argt(ea.cmdidx);
2734
2735 arg = skipwhite(p);
2736
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002737 // Does command allow "++argopt" argument?
2738 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002739 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002740 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2741 {
2742 p = arg + 2;
2743 while (*p && !vim_isspace(*p))
2744 MB_PTR_ADV(p);
2745
2746 // Still touching the command after "++"?
2747 if (*p == NUL)
2748 {
2749 if (ea.argt & EX_ARGOPT)
2750 return set_context_in_argopt(xp, arg + 2);
2751#ifdef FEAT_TERMINAL
2752 if (ea.cmdidx == CMD_terminal)
2753 return set_context_in_terminalopt(xp, arg + 2);
2754#endif
2755 }
2756
2757 arg = skipwhite(p);
2758 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002759 }
2760
2761 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2762 {
2763 if (*arg == '>') // append
2764 {
2765 if (*++arg == '>')
2766 ++arg;
2767 arg = skipwhite(arg);
2768 }
2769 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2770 {
2771 ++arg;
2772 usefilter = TRUE;
2773 }
2774 }
2775
2776 if (ea.cmdidx == CMD_read)
2777 {
2778 usefilter = forceit; // :r! filter if forced
2779 if (*arg == '!') // :r !filter
2780 {
2781 ++arg;
2782 usefilter = TRUE;
2783 }
2784 }
2785
2786 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2787 {
2788 while (*arg == *cmd) // allow any number of '>' or '<'
2789 ++arg;
2790 arg = skipwhite(arg);
2791 }
2792
2793 // Does command allow "+command"?
2794 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2795 {
2796 // Check if we're in the +command
2797 p = arg + 1;
2798 arg = skip_cmd_arg(arg, FALSE);
2799
2800 // Still touching the command after '+'?
2801 if (*arg == NUL)
2802 return p;
2803
2804 // Skip space(s) after +command to get to the real argument
2805 arg = skipwhite(arg);
2806 }
2807
2808
2809 // Check for '|' to separate commands and '"' to start comments.
2810 // Don't do this for ":read !cmd" and ":write !cmd".
2811 if ((ea.argt & EX_TRLBAR) && !usefilter)
2812 {
2813 p = arg;
2814 // ":redir @" is not the start of a comment
2815 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2816 p += 2;
2817 while (*p)
2818 {
2819 if (*p == Ctrl_V)
2820 {
2821 if (p[1] != NUL)
2822 ++p;
2823 }
2824 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2825 || *p == '|' || *p == '\n')
2826 {
2827 if (*(p - 1) != '\\')
2828 {
2829 if (*p == '|' || *p == '\n')
2830 return p + 1;
2831 return NULL; // It's a comment
2832 }
2833 }
2834 MB_PTR_ADV(p);
2835 }
2836 }
2837
2838 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2839 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2840 // no arguments allowed but there is something
2841 return NULL;
2842
2843 // Find start of last argument (argument just before cursor):
2844 p = buff;
2845 xp->xp_pattern = p;
2846 len = (int)STRLEN(buff);
2847 while (*p && p < buff + len)
2848 {
2849 if (*p == ' ' || *p == TAB)
2850 {
2851 // argument starts after a space
2852 xp->xp_pattern = ++p;
2853 }
2854 else
2855 {
2856 if (*p == '\\' && *(p + 1) != NUL)
2857 ++p; // skip over escaped character
2858 MB_PTR_ADV(p);
2859 }
2860 }
2861
2862 if (ea.argt & EX_XFILE)
2863 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2864
2865 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002866 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002867 forceit);
2868}
2869
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002870/*
2871 * Set the completion context in 'xp' for command 'str'
2872 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002873 void
2874set_cmd_context(
2875 expand_T *xp,
2876 char_u *str, // start of command line
2877 int len, // length of command line (excl. NUL)
2878 int col, // position of cursor
2879 int use_ccline UNUSED) // use ccline for info
2880{
2881#ifdef FEAT_EVAL
2882 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002883 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002884#endif
2885 int old_char = NUL;
2886 char_u *nextcomm;
2887
2888 // Avoid a UMR warning from Purify, only save the character if it has been
2889 // written before.
2890 if (col < len)
2891 old_char = str[col];
2892 str[col] = NUL;
2893 nextcomm = str;
2894
2895#ifdef FEAT_EVAL
2896 if (use_ccline && ccline->cmdfirstc == '=')
2897 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002898 // pass CMD_SIZE because there is no real command
2899 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002900 }
2901 else if (use_ccline && ccline->input_fn)
2902 {
2903 xp->xp_context = ccline->xp_context;
2904 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002905 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002906 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2907 {
2908 context = xp->xp_context;
2909 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2910 &context);
2911 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002912 }
2913 else
2914#endif
2915 while (nextcomm != NULL)
2916 nextcomm = set_one_cmd_context(xp, nextcomm);
2917
2918 // Store the string here so that call_user_expand_func() can get to them
2919 // easily.
2920 xp->xp_line = str;
2921 xp->xp_col = col;
2922
2923 str[col] = old_char;
2924}
2925
2926/*
2927 * Expand the command line "str" from context "xp".
2928 * "xp" must have been set by set_cmd_context().
2929 * xp->xp_pattern points into "str", to where the text that is to be expanded
2930 * starts.
2931 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2932 * cursor.
2933 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2934 * key that triggered expansion literally.
2935 * Returns EXPAND_OK otherwise.
2936 */
2937 int
2938expand_cmdline(
2939 expand_T *xp,
2940 char_u *str, // start of command line
2941 int col, // position of cursor
2942 int *matchcount, // return: nr of matches
2943 char_u ***matches) // return: array of pointers to matches
2944{
2945 char_u *file_str = NULL;
2946 int options = WILD_ADD_SLASH|WILD_SILENT;
2947
2948 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2949 {
2950 beep_flush();
2951 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2952 }
2953 if (xp->xp_context == EXPAND_NOTHING)
2954 {
2955 // Caller can use the character as a normal char instead
2956 return EXPAND_NOTHING;
2957 }
2958
2959 // add star to file name, or convert to regexp if not exp. files.
2960 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002961 if (cmdline_fuzzy_completion_supported(xp))
2962 // If fuzzy matching, don't modify the search string
2963 file_str = vim_strsave(xp->xp_pattern);
2964 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002965 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
John Marriott1be5b372025-06-23 19:57:29 +02002966 if (file_str == NULL)
2967 return EXPAND_UNSUCCESSFUL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002968
2969 if (p_wic)
2970 options += WILD_ICASE;
2971
2972 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002973 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002974 {
2975 *matchcount = 0;
2976 *matches = NULL;
2977 }
2978 vim_free(file_str);
2979
2980 return EXPAND_OK;
2981}
2982
Bram Moolenaar66b51422019-08-18 21:44:12 +02002983/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002984 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002985 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002986 */
2987 static int
2988expand_files_and_dirs(
2989 expand_T *xp,
2990 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002991 char_u ***matches,
2992 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002993 int flags,
2994 int options)
2995{
2996 int free_pat = FALSE;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002997 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002998
2999 // for ":set path=" and ":set tags=" halve backslashes for escaped
3000 // space
3001 if (xp->xp_backslash != XP_BS_NONE)
3002 {
John Marriotta494ce12025-07-03 21:28:50 +02003003 size_t pat_len;
3004 char_u *pat_end;
3005 char_u *p;
3006
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003007 free_pat = TRUE;
John Marriotta494ce12025-07-03 21:28:50 +02003008
3009 pat_len = STRLEN(pat);
3010 pat = vim_strnsave(pat, pat_len);
John Marriott3b03b432025-06-28 20:41:54 +02003011 if (pat == NULL)
3012 return ret;
3013
John Marriotta494ce12025-07-03 21:28:50 +02003014 pat_end = pat + pat_len;
3015 for (p = pat; *p != NUL; ++p)
3016 {
3017 char_u *from;
3018
3019 if (*p != '\\')
3020 continue;
3021
3022 if (xp->xp_backslash & XP_BS_THREE
3023 && *(p + 1) == '\\'
3024 && *(p + 2) == '\\'
3025 && *(p + 3) == ' ')
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 {
John Marriotta494ce12025-07-03 21:28:50 +02003027 from = p + 3;
3028 mch_memmove(p, from,
3029 (size_t)(pat_end - from) + 1); // +1 for NUL
3030 pat_end -= 3;
3031 }
3032 else if (xp->xp_backslash & XP_BS_ONE
3033 && *(p + 1) == ' ')
3034 {
3035 from = p + 1;
3036 mch_memmove(p, from,
3037 (size_t)(pat_end - from) + 1); // +1 for NUL
3038 --pat_end;
3039 }
3040 else if (xp->xp_backslash & XP_BS_COMMA)
3041 {
3042 if (*(p + 1) == '\\' && *(p + 2) == ',')
3043 {
3044 from = p + 2;
3045 mch_memmove(p, from,
3046 (size_t)(pat_end - from) + 1); // +1 for NUL
3047 pat_end -= 2;
3048 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003049#ifdef BACKSLASH_IN_FILENAME
John Marriotta494ce12025-07-03 21:28:50 +02003050 else if (*(p + 1) == ',')
3051 {
3052 from = p + 1;
3053 mch_memmove(p, from,
3054 (size_t)(pat_end - from) + 1); // +1 for NUL
3055 --pat_end;
3056 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003057#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003058 }
John Marriotta494ce12025-07-03 21:28:50 +02003059 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003060 }
3061
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003062 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003063 {
3064#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003065 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003066#endif
3067 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003068 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003069 {
3070 if (xp->xp_context == EXPAND_FILES)
3071 flags |= EW_FILE;
3072 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
3073 flags |= (EW_FILE | EW_PATH);
3074 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
3075 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
3076 else
3077 flags = (flags | EW_DIR) & ~EW_FILE;
3078 if (options & WILD_ICASE)
3079 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003080
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003081 // Expand wildcards, supporting %:h and the like.
3082 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
3083 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003084 if (free_pat)
3085 vim_free(pat);
3086#ifdef BACKSLASH_IN_FILENAME
3087 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
3088 {
John Marriotta494ce12025-07-03 21:28:50 +02003089 int j;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003090
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003091 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003092 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003093 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003094
3095 while (*ptr != NUL)
3096 {
3097 if (p_csl[0] == 's' && *ptr == '\\')
3098 *ptr = '/';
3099 else if (p_csl[0] == 'b' && *ptr == '/')
3100 *ptr = '\\';
3101 ptr += (*mb_ptr2len)(ptr);
3102 }
3103 }
3104 }
3105#endif
3106 return ret;
3107}
3108
3109/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003110 * Function given to ExpandGeneric() to obtain the possible arguments of the
3111 * ":behave {mswin,xterm}" command.
3112 */
3113 static char_u *
3114get_behave_arg(expand_T *xp UNUSED, int idx)
3115{
3116 if (idx == 0)
3117 return (char_u *)"mswin";
3118 if (idx == 1)
3119 return (char_u *)"xterm";
3120 return NULL;
3121}
3122
Christian Brabandta3422aa2025-04-23 21:04:24 +02003123/*
3124 * Function given to ExpandGeneric() to obtain the possible arguments of the
3125 * ":filetype {plugin,indent}" command.
3126 */
3127 static char_u *
3128get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3129{
John Marriotta494ce12025-07-03 21:28:50 +02003130 if (idx < 0)
3131 return NULL;
Christian Brabandta3422aa2025-04-23 21:04:24 +02003132
3133 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
John Marriotta494ce12025-07-03 21:28:50 +02003134 {
3135 char *opts_all[] = {"indent", "plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003136 return (char_u *)opts_all[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003137 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003138 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003139 {
3140 char *opts_plugin[] = {"plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003141 return (char_u *)opts_plugin[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003142 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003143 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003144 {
3145 char *opts_indent[] = {"indent", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003146 return (char_u *)opts_indent[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003147 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003148 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
John Marriotta494ce12025-07-03 21:28:50 +02003149 {
3150 char *opts_onoff[] = {"on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003151 return (char_u *)opts_onoff[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003152 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003153 return NULL;
3154}
3155
K.Takata161b6ac2022-11-14 15:31:07 +00003156#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003157/*
3158 * Function given to ExpandGeneric() to obtain the possible arguments of the
3159 * ":breakadd {expr, file, func, here}" command.
3160 * ":breakdel {func, file, here}" command.
3161 */
3162 static char_u *
3163get_breakadd_arg(expand_T *xp UNUSED, int idx)
3164{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003165 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003166 {
John Marriotta494ce12025-07-03 21:28:50 +02003167 char *opts[] = {"expr", "file", "func", "here"};
3168
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003169 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003170 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3171 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003172 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3173 {
3174 // breakdel {func, file, here}
3175 if (idx <= 2)
3176 return (char_u *)opts[idx + 1];
3177 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003178 else
3179 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003180 // profdel {func, file}
3181 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003182 return (char_u *)opts[idx + 1];
3183 }
3184 }
3185 return NULL;
3186}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003187
3188/*
3189 * Function given to ExpandGeneric() to obtain the possible arguments for the
3190 * ":scriptnames" command.
3191 */
3192 static char_u *
3193get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3194{
3195 scriptitem_T *si;
3196
3197 if (!SCRIPT_ID_VALID(idx + 1))
3198 return NULL;
3199
3200 si = SCRIPT_ITEM(idx + 1);
3201 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3202 return NameBuff;
3203}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003204#endif
3205
Bram Moolenaard0190392019-08-23 21:17:35 +02003206/*
3207 * Function given to ExpandGeneric() to obtain the possible arguments of the
3208 * ":messages {clear}" command.
3209 */
3210 static char_u *
3211get_messages_arg(expand_T *xp UNUSED, int idx)
3212{
3213 if (idx == 0)
3214 return (char_u *)"clear";
3215 return NULL;
3216}
3217
3218 static char_u *
3219get_mapclear_arg(expand_T *xp UNUSED, int idx)
3220{
3221 if (idx == 0)
3222 return (char_u *)"<buffer>";
3223 return NULL;
3224}
3225
3226/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003227 * Do the expansion based on xp->xp_context and 'rmp'.
3228 */
3229 static int
3230ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003231 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003232 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003233 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003234 char_u ***matches,
3235 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003236{
3237 static struct expgen
3238 {
3239 int context;
3240 char_u *((*func)(expand_T *, int));
3241 int ic;
3242 int escaped;
3243 } tab[] =
3244 {
3245 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3246 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003247 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003248 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3249 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3250 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3251 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3252 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3253 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3254 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3255 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003256#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003257 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3258 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3259 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3260 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3261 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003262#endif
3263#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003264 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3265 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003266#endif
3267#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003268 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003269#endif
3270#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003271 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003272#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003273 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3274 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3275 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003276#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003277 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003278#endif
3279#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003280 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003281#endif
3282#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003283 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003284#endif
3285#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003286 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3287 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003288#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003289 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3290 {EXPAND_USER, get_users, TRUE, FALSE},
3291 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003292#ifdef FEAT_EVAL
3293 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003294 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003295#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003296 };
3297 int i;
3298 int ret = FAIL;
3299
3300 // Find a context in the table and call the ExpandGeneric() with the
3301 // right function to do the expansion.
3302 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3303 {
3304 if (xp->xp_context == tab[i].context)
3305 {
3306 if (tab[i].ic)
3307 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003308 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3309 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003310 break;
3311 }
3312 }
3313
3314 return ret;
3315}
3316
3317/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003318 * Map wild expand options to flags for expand_wildcards()
3319 */
3320 static int
3321map_wildopts_to_ewflags(int options)
3322{
3323 int flags;
3324
3325 flags = EW_DIR; // include directories
3326 if (options & WILD_LIST_NOTFOUND)
3327 flags |= EW_NOTFOUND;
3328 if (options & WILD_ADD_SLASH)
3329 flags |= EW_ADDSLASH;
3330 if (options & WILD_KEEP_ALL)
3331 flags |= EW_KEEPALL;
3332 if (options & WILD_SILENT)
3333 flags |= EW_SILENT;
3334 if (options & WILD_NOERROR)
3335 flags |= EW_NOERROR;
3336 if (options & WILD_ALLLINKS)
3337 flags |= EW_ALLLINKS;
3338
3339 return flags;
3340}
3341
3342/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003343 * Do the expansion based on xp->xp_context and "pat".
3344 */
3345 static int
3346ExpandFromContext(
3347 expand_T *xp,
3348 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003349 char_u ***matches,
3350 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003351 int options) // WILD_ flags
3352{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 int ret;
3355 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003356 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003357 int fuzzy = cmdline_fuzzy_complete(pat)
3358 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003359
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003360 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361
3362 if (xp->xp_context == EXPAND_FILES
3363 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003364 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003365 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003366 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003367 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3368 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003369
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003370 *matches = (char_u **)"";
3371 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003372 if (xp->xp_context == EXPAND_HELP)
3373 {
3374 // With an empty argument we would get all the help tags, which is
3375 // very slow. Get matches for "help" instead.
3376 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003377 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003378 {
3379#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003380 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381#endif
3382 return OK;
3383 }
3384 return FAIL;
3385 }
3386
Bram Moolenaar66b51422019-08-18 21:44:12 +02003387 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003388 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003390 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003391 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003392 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003393#ifdef FEAT_DIFF
3394 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003395 return ExpandBufnames(pat, numMatches, matches,
3396 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003397#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398 if (xp->xp_context == EXPAND_TAGS
3399 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003400 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3401 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003402 if (xp->xp_context == EXPAND_COLORS)
3403 {
3404 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003405 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003406 directories);
3407 }
3408 if (xp->xp_context == EXPAND_COMPILER)
3409 {
3410 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003411 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003412 }
3413 if (xp->xp_context == EXPAND_OWNSYNTAX)
3414 {
3415 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003416 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003417 }
3418 if (xp->xp_context == EXPAND_FILETYPE)
3419 {
3420 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003421 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003422 }
Doug Kearns81642d92024-01-04 22:37:44 +01003423#ifdef FEAT_KEYMAP
3424 if (xp->xp_context == EXPAND_KEYMAP)
3425 {
3426 char *directories[] = {"keymap", NULL};
3427 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3428 }
3429#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003430#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003431 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003432 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003433#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003434 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003435 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003436 if (xp->xp_context == EXPAND_RUNTIME)
3437 return expand_runtime_cmd(pat, numMatches, matches);
Girish Palya6b49fba2025-06-28 19:47:34 +02003438 if (xp->xp_context == EXPAND_PATTERN_IN_BUF)
3439 return expand_pattern_in_buf(pat, xp->xp_search_dir,
3440 matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003441
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003442 // When expanding a function name starting with s:, match the <SNR>nr_
3443 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003444 if ((xp->xp_context == EXPAND_USER_FUNC
3445 || xp->xp_context == EXPAND_DISASSEMBLE)
3446 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003447 {
3448 int len = (int)STRLEN(pat) + 20;
3449
3450 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003451 if (tofree == NULL)
3452 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003453 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003454 pat = tofree;
3455 }
3456
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003457 if (!fuzzy)
3458 {
3459 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3460 if (regmatch.regprog == NULL)
3461 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003462
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003463 // set ignore-case according to p_ic, p_scs and pat
3464 regmatch.rm_ic = ignorecase(pat);
3465 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003466
3467 if (xp->xp_context == EXPAND_SETTINGS
3468 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003469 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003470 else if (xp->xp_context == EXPAND_STRING_SETTING)
3471 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3472 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3473 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003474 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003475 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003476 else if (xp->xp_context == EXPAND_ARGOPT)
3477 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003478 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3479 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003480#if defined(FEAT_TERMINAL)
3481 else if (xp->xp_context == EXPAND_TERMINALOPT)
3482 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3483#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003484#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003486 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003487#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003489 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003490
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003491 if (!fuzzy)
3492 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003493 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003494
3495 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496}
3497
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003498 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003500 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003501 expand_T *xp,
3502 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003503 char_u ***matches,
3504 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003505 char_u *((*func)(expand_T *, int)),
3506 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003507 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003508{
Yee Cheng China7b81202025-02-23 09:32:47 +01003509 return ExpandGenericExt(
3510 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3511}
3512
3513/*
3514 * Expand a list of names.
3515 *
3516 * Generic function for command line completion. It calls a function to
3517 * obtain strings, one by one. The strings are matched against a regexp
3518 * program. Matching strings are copied into an array, which is returned.
3519 *
3520 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3521 * is used.
3522 *
3523 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3524 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3525 * sorting.
3526 *
3527 * Returns OK when no problems encountered, FAIL for error (out of memory).
3528 */
3529 int
3530ExpandGenericExt(
3531 char_u *pat,
3532 expand_T *xp,
3533 regmatch_T *regmatch,
3534 char_u ***matches,
3535 int *numMatches,
3536 char_u *((*func)(expand_T *, int)),
3537 // returns a string from the list
3538 int escaped,
3539 int sortStartIdx)
3540{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003542 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003544 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003545 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003546 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003547 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003548 int sort_matches = FALSE;
3549 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003550 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003551
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003552 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003553 *matches = NULL;
3554 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003555
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003556 if (!fuzzy)
3557 ga_init2(&ga, sizeof(char *), 30);
3558 else
3559 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3560
3561 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003562 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003563 str = (*func)(xp, i);
3564 if (str == NULL) // end of list
3565 break;
3566 if (*str == NUL) // skip empty strings
3567 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003569 if (xp->xp_pattern[0] != NUL)
3570 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003571 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003572 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003573 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003574 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003575 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003576 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003577 }
3578 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003579 else
3580 match = TRUE;
3581
3582 if (!match)
3583 continue;
3584
3585 if (escaped)
3586 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3587 else
3588 str = vim_strsave(str);
3589 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003590 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003591 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003592 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003593 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003594 return FAIL;
3595 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003596 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003597 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003598 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003599
3600 if (ga_grow(&ga, 1) == FAIL)
3601 {
3602 vim_free(str);
3603 break;
3604 }
3605
3606 if (fuzzy)
3607 {
3608 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3609 fuzmatch->idx = ga.ga_len;
3610 fuzmatch->str = str;
3611 fuzmatch->score = score;
3612 }
3613 else
3614 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3615
K.Takata161b6ac2022-11-14 15:31:07 +00003616#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003617 if (func == get_menu_names)
3618 {
3619 // test for separator added by get_menu_names()
3620 str += STRLEN(str) - 1;
3621 if (*str == '\001')
3622 *str = '.';
3623 }
K.Takata161b6ac2022-11-14 15:31:07 +00003624#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003625
Yee Cheng China7b81202025-02-23 09:32:47 +01003626 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3627 {
3628 // Found first item to start sorting from. This is usually 0.
3629 sortStartMatchIdx = ga.ga_len;
3630 }
3631
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003632 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003633 }
3634
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003635 if (ga.ga_len == 0)
3636 return OK;
3637
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003638 // sort the matches when using regular expression matching and sorting
3639 // applies to the completion context. Menus and scriptnames should be kept
3640 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003641 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003642 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003643 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003644 && xp->xp_context != EXPAND_SCRIPTNAMES
3645 && xp->xp_context != EXPAND_ARGOPT
3646 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003647 sort_matches = TRUE;
3648
3649 // <SNR> functions should be sorted to the end.
3650 if (xp->xp_context == EXPAND_EXPRESSION
3651 || xp->xp_context == EXPAND_FUNCTIONS
3652 || xp->xp_context == EXPAND_USER_FUNC
3653 || xp->xp_context == EXPAND_DISASSEMBLE)
3654 funcsort = TRUE;
3655
3656 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003657 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003658 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003659 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003660 // <SNR> functions should be sorted to the end.
3661 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3662 sort_func_compare);
3663 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003664 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003665 }
3666
3667 if (!fuzzy)
3668 {
3669 *matches = ga.ga_data;
3670 *numMatches = ga.ga_len;
3671 }
3672 else
3673 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003674 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3675 funcsort) == FAIL)
3676 return FAIL;
3677 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003678 }
3679
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003680#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003681 // Reset the variables used for special highlight names expansion, so that
3682 // they don't show up when getting normal highlight names by ID.
3683 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003684#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003685
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686 return OK;
3687}
3688
3689/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003690 * Expand shell command matches in one directory of $PATH.
3691 */
3692 static void
3693expand_shellcmd_onedir(
3694 char_u *buf,
3695 char_u *s,
3696 size_t l,
3697 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003698 char_u ***matches,
3699 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003700 int flags,
3701 hashtab_T *ht,
3702 garray_T *gap)
3703{
3704 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003705 hash_T hash;
3706 hashitem_T *hi;
3707
3708 vim_strncpy(buf, s, l);
3709 add_pathsep(buf);
3710 l = STRLEN(buf);
3711 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3712
3713 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003714 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003715 if (ret != OK)
3716 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003717
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003718 if (ga_grow(gap, *numMatches) == FAIL)
3719 {
3720 FreeWild(*numMatches, *matches);
3721 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003722 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003723
3724 for (int i = 0; i < *numMatches; ++i)
3725 {
3726 char_u *name = (*matches)[i];
3727
3728 if (STRLEN(name) > l)
3729 {
3730 // Check if this name was already found.
3731 hash = hash_hash(name + l);
3732 hi = hash_lookup(ht, name + l, hash);
3733 if (HASHITEM_EMPTY(hi))
3734 {
3735 // Remove the path that was prepended.
3736 STRMOVE(name, name + l);
3737 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3738 hash_add_item(ht, hi, name, hash);
3739 name = NULL;
3740 }
3741 }
3742 vim_free(name);
3743 }
3744 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003745}
3746
3747/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003748 * Complete a shell command.
3749 * Returns FAIL or OK;
3750 */
3751 static int
3752expand_shellcmd(
3753 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003754 char_u ***matches, // return: array with matches
3755 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003756 int flagsarg) // EW_ flags
3757{
3758 char_u *pat;
3759 int i;
3760 char_u *path = NULL;
3761 int mustfree = FALSE;
3762 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003763 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003764 size_t l;
3765 char_u *s, *e;
3766 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003767 int did_curdir = FALSE;
3768 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003769
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003770 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003771 if (buf == NULL)
3772 return FAIL;
3773
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003774 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003775 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003776 if (pat == NULL)
3777 {
3778 vim_free(buf);
3779 return FAIL;
3780 }
3781
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782 for (i = 0; pat[i]; ++i)
3783 if (pat[i] == '\\' && pat[i + 1] == ' ')
3784 STRMOVE(pat + i, pat + i + 1);
3785
3786 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3787
3788 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3789 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3790 path = (char_u *)".";
3791 else
3792 {
3793 // For an absolute name we don't use $PATH.
3794 if (!mch_isFullName(pat))
3795 path = vim_getenv((char_u *)"PATH", &mustfree);
3796 if (path == NULL)
3797 path = (char_u *)"";
3798 }
3799
3800 // Go over all directories in $PATH. Expand matches in that directory and
3801 // collect them in "ga". When "." is not in $PATH also expand for the
3802 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003803 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003804 hash_init(&found_ht);
3805 for (s = path; ; s = e)
3806 {
K.Takata161b6ac2022-11-14 15:31:07 +00003807#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003808 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003809#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003811#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003812 if (e == NULL)
3813 e = s + STRLEN(s);
3814
3815 if (*s == NUL)
3816 {
3817 if (did_curdir)
3818 break;
3819 // Find directories in the current directory, path is empty.
3820 did_curdir = TRUE;
3821 flags |= EW_DIR;
3822 }
3823 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3824 {
3825 did_curdir = TRUE;
3826 flags |= EW_DIR;
3827 }
3828 else
3829 // Do not match directories inside a $PATH item.
3830 flags &= ~EW_DIR;
3831
3832 l = e - s;
3833 if (l > MAXPATHL - 5)
3834 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003835
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003836 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003837 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003838
Bram Moolenaar66b51422019-08-18 21:44:12 +02003839 if (*e != NUL)
3840 ++e;
3841 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003842 *matches = ga.ga_data;
3843 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003844
3845 vim_free(buf);
3846 vim_free(pat);
3847 if (mustfree)
3848 vim_free(path);
3849 hash_clear(&found_ht);
3850 return OK;
3851}
3852
K.Takata161b6ac2022-11-14 15:31:07 +00003853#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003854/*
3855 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003856 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003857 */
3858 static void *
3859call_user_expand_func(
3860 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003861 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003862{
3863 cmdline_info_T *ccline = get_cmdline_info();
3864 int keep = 0;
3865 typval_T args[4];
3866 sctx_T save_current_sctx = current_sctx;
3867 char_u *pat = NULL;
3868 void *ret;
3869
3870 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3871 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003872
3873 if (ccline->cmdbuff != NULL)
3874 {
3875 keep = ccline->cmdbuff[ccline->cmdlen];
3876 ccline->cmdbuff[ccline->cmdlen] = 0;
3877 }
3878
3879 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3880
3881 args[0].v_type = VAR_STRING;
3882 args[0].vval.v_string = pat;
3883 args[1].v_type = VAR_STRING;
3884 args[1].vval.v_string = xp->xp_line;
3885 args[2].v_type = VAR_NUMBER;
3886 args[2].vval.v_number = xp->xp_col;
3887 args[3].v_type = VAR_UNKNOWN;
3888
3889 current_sctx = xp->xp_script_ctx;
3890
3891 ret = user_expand_func(xp->xp_arg, 3, args);
3892
3893 current_sctx = save_current_sctx;
3894 if (ccline->cmdbuff != NULL)
3895 ccline->cmdbuff[ccline->cmdlen] = keep;
3896
3897 vim_free(pat);
3898 return ret;
3899}
3900
3901/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003902 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3903 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003904 */
3905 static int
3906ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003907 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003908 expand_T *xp,
3909 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003910 char_u ***matches,
3911 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003912{
3913 char_u *retstr;
3914 char_u *s;
3915 char_u *e;
3916 int keep;
3917 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003918 int fuzzy;
3919 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003920 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003921
3922 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003923 *matches = NULL;
3924 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003925
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003926 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003927 if (retstr == NULL)
3928 return FAIL;
3929
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003930 if (!fuzzy)
3931 ga_init2(&ga, sizeof(char *), 3);
3932 else
3933 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3934
Bram Moolenaar66b51422019-08-18 21:44:12 +02003935 for (s = retstr; *s != NUL; s = e)
3936 {
3937 e = vim_strchr(s, '\n');
3938 if (e == NULL)
3939 e = s + STRLEN(s);
3940 keep = *e;
3941 *e = NUL;
3942
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003943 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003944 {
3945 if (!fuzzy)
3946 match = vim_regexec(regmatch, s, (colnr_T)0);
3947 else
3948 {
3949 score = fuzzy_match_str(s, pat);
3950 match = (score != 0);
3951 }
3952 }
3953 else
3954 match = TRUE; // match everything
3955
Bram Moolenaar66b51422019-08-18 21:44:12 +02003956 *e = keep;
3957
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003958 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003959 {
John Marriott3b03b432025-06-28 20:41:54 +02003960 char_u *p = vim_strnsave(s, (size_t)(e - s));
3961 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003962 break;
John Marriott3b03b432025-06-28 20:41:54 +02003963
3964 if (ga_grow(&ga, 1) == FAIL)
3965 {
3966 vim_free(p);
3967 break;
3968 }
3969
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003970 if (!fuzzy)
John Marriott3b03b432025-06-28 20:41:54 +02003971 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003972 else
3973 {
3974 fuzmatch_str_T *fuzmatch =
3975 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003976 fuzmatch->idx = ga.ga_len;
John Marriott3b03b432025-06-28 20:41:54 +02003977 fuzmatch->str = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003978 fuzmatch->score = score;
3979 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003980 ++ga.ga_len;
3981 }
3982
3983 if (*e != NUL)
3984 ++e;
3985 }
3986 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003987
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003988 if (ga.ga_len == 0)
3989 return OK;
3990
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003991 if (!fuzzy)
3992 {
3993 *matches = ga.ga_data;
3994 *numMatches = ga.ga_len;
3995 }
3996 else
3997 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003998 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3999 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00004000 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00004001 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00004002 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004003 return OK;
4004}
4005
4006/*
4007 * Expand names with a list returned by a function defined by the user.
4008 */
4009 static int
4010ExpandUserList(
4011 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004012 char_u ***matches,
4013 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004014{
4015 list_T *retlist;
4016 listitem_T *li;
4017 garray_T ga;
4018
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004019 *matches = NULL;
4020 *numMatches = 0;
4021 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004022 if (retlist == NULL)
4023 return FAIL;
4024
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004025 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004026 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004027 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004028 {
John Marriott3b03b432025-06-28 20:41:54 +02004029 char_u *p;
4030
Bram Moolenaar66b51422019-08-18 21:44:12 +02004031 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4032 continue; // Skip non-string items and empty strings
4033
John Marriott3b03b432025-06-28 20:41:54 +02004034 p = vim_strsave(li->li_tv.vval.v_string);
4035 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004036 break;
4037
John Marriott3b03b432025-06-28 20:41:54 +02004038 if (ga_grow(&ga, 1) == FAIL)
4039 {
4040 vim_free(p);
4041 break;
4042 }
4043
4044 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004045 ++ga.ga_len;
4046 }
4047 list_unref(retlist);
4048
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004049 *matches = ga.ga_data;
4050 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004051 return OK;
4052}
K.Takata161b6ac2022-11-14 15:31:07 +00004053#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02004054
4055/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02004056 * Expand "file" for all comma-separated directories in "path".
4057 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00004058 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02004059 */
4060 void
4061globpath(
4062 char_u *path,
4063 char_u *file,
4064 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00004065 int expand_options,
4066 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004067{
4068 expand_T xpc;
4069 char_u *buf;
John Marriotta494ce12025-07-03 21:28:50 +02004070 size_t buflen;
4071 size_t filelen;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004072
4073 buf = alloc(MAXPATHL);
4074 if (buf == NULL)
4075 return;
4076
4077 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00004078 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004079
John Marriotta494ce12025-07-03 21:28:50 +02004080 filelen = STRLEN(file);
4081
Bram Moolenaar66b51422019-08-18 21:44:12 +02004082 // Loop over all entries in {path}.
4083 while (*path != NUL)
4084 {
4085 // Copy one item of the path to buf[] and concatenate the file name.
John Marriotta494ce12025-07-03 21:28:50 +02004086 buflen = (size_t)copy_option_part(&path, buf, MAXPATHL, ",");
4087 if (buflen + filelen + 2 < MAXPATHL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004088 {
John Marriotta494ce12025-07-03 21:28:50 +02004089 int num_p;
4090 char_u **p;
4091
4092 if (*buf != NUL && !after_pathsep(buf, buf + buflen))
4093 {
K.Takata161b6ac2022-11-14 15:31:07 +00004094#if defined(MSWIN)
John Marriotta494ce12025-07-03 21:28:50 +02004095 // Using the platform's path separator (\) makes vim incorrectly
4096 // treat it as an escape character, use '/' instead.
4097 STRCPY(buf + buflen, "/");
4098 ++buflen;
K.Takata161b6ac2022-11-14 15:31:07 +00004099#else
John Marriotta494ce12025-07-03 21:28:50 +02004100 STRCPY(buf + buflen, PATHSEPSTR);
4101 buflen += STRLEN_LITERAL(PATHSEPSTR);
K.Takata161b6ac2022-11-14 15:31:07 +00004102#endif
John Marriotta494ce12025-07-03 21:28:50 +02004103 }
4104 STRCPY(buf + buflen, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004105 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02004106 WILD_SILENT|expand_options) != FAIL && num_p > 0)
4107 {
4108 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
4109
4110 if (ga_grow(ga, num_p) == OK)
John Marriotta494ce12025-07-03 21:28:50 +02004111 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004112 // take over the pointers and put them in "ga"
John Marriotta494ce12025-07-03 21:28:50 +02004113 for (int i = 0; i < num_p; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004114 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004115 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02004116 ++ga->ga_len;
4117 }
John Marriotta494ce12025-07-03 21:28:50 +02004118 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004119 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004120 }
4121 }
4122 }
4123
4124 vim_free(buf);
4125}
Bram Moolenaar66b51422019-08-18 21:44:12 +02004126
Bram Moolenaareadee482020-09-04 15:37:31 +02004127/*
4128 * Translate some keys pressed when 'wildmenu' is used.
4129 */
4130 int
4131wildmenu_translate_key(
4132 cmdline_info_T *cclp,
4133 int key,
4134 expand_T *xp,
4135 int did_wild_list)
4136{
4137 int c = key;
4138
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004139 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004140 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004141 // When the popup menu is used for cmdline completion:
4142 // Up : go to the previous item in the menu
4143 // Down : go to the next item in the menu
4144 // Left : go to the parent directory
4145 // Right: list the files in the selected directory
4146 switch (c)
4147 {
4148 case K_UP: c = K_LEFT; break;
4149 case K_DOWN: c = K_RIGHT; break;
4150 case K_LEFT: c = K_UP; break;
4151 case K_RIGHT: c = K_DOWN; break;
4152 default: break;
4153 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004154 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004155
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004156 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004157 {
4158 if (c == K_LEFT)
4159 c = Ctrl_P;
4160 else if (c == K_RIGHT)
4161 c = Ctrl_N;
4162 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004163
Bram Moolenaareadee482020-09-04 15:37:31 +02004164 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004165 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004166 && cclp->cmdpos > 1
4167 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4168 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4169 && (c == '\n' || c == '\r' || c == K_KENTER))
4170 c = K_DOWN;
4171
4172 return c;
4173}
4174
4175/*
4176 * Delete characters on the command line, from "from" to the current
4177 * position.
4178 */
4179 static void
4180cmdline_del(cmdline_info_T *cclp, int from)
4181{
4182 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4183 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4184 cclp->cmdlen -= cclp->cmdpos - from;
4185 cclp->cmdpos = from;
4186}
4187
4188/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004189 * Handle a key pressed when the wild menu for the menu names
4190 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004191 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004192 static int
4193wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004194{
Bram Moolenaareadee482020-09-04 15:37:31 +02004195 int i;
4196 int j;
4197
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004198 // Hitting <Down> after "emenu Name.": complete submenu
4199 if (key == K_DOWN && cclp->cmdpos > 0
4200 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004201 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004202 key = p_wc;
4203 KeyTyped = TRUE; // in case the key was mapped
4204 }
4205 else if (key == K_UP)
4206 {
4207 // Hitting <Up>: Remove one submenu name in front of the
4208 // cursor
4209 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004210
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004211 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4212 i = 0;
4213 while (--j > 0)
4214 {
4215 // check for start of menu name
4216 if (cclp->cmdbuff[j] == ' '
4217 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004218 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004219 i = j + 1;
4220 break;
4221 }
4222 // check for start of submenu name
4223 if (cclp->cmdbuff[j] == '.'
4224 && cclp->cmdbuff[j - 1] != '\\')
4225 {
4226 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004227 {
4228 i = j + 1;
4229 break;
4230 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004231 else
4232 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004233 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004234 }
4235 if (i > 0)
4236 cmdline_del(cclp, i);
4237 key = p_wc;
4238 KeyTyped = TRUE; // in case the key was mapped
4239 xp->xp_context = EXPAND_NOTHING;
4240 }
4241
4242 return key;
4243}
4244
4245/*
4246 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4247 * directory names (EXPAND_DIRECTORIES) or shell command names
4248 * (EXPAND_SHELLCMD) is displayed.
4249 */
4250 static int
4251wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4252{
4253 int i;
4254 int j;
4255 char_u upseg[5];
4256
4257 upseg[0] = PATHSEP;
4258 upseg[1] = '.';
4259 upseg[2] = '.';
4260 upseg[3] = PATHSEP;
4261 upseg[4] = NUL;
4262
4263 if (key == K_DOWN
4264 && cclp->cmdpos > 0
4265 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4266 && (cclp->cmdpos < 3
4267 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4268 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4269 {
4270 // go down a directory
4271 key = p_wc;
4272 KeyTyped = TRUE; // in case the key was mapped
4273 }
4274 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4275 {
4276 // If in a direct ancestor, strip off one ../ to go down
4277 int found = FALSE;
4278
4279 j = cclp->cmdpos;
4280 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4281 while (--j > i)
4282 {
4283 if (has_mbyte)
4284 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4285 if (vim_ispathsep(cclp->cmdbuff[j]))
4286 {
4287 found = TRUE;
4288 break;
4289 }
4290 }
4291 if (found
4292 && cclp->cmdbuff[j - 1] == '.'
4293 && cclp->cmdbuff[j - 2] == '.'
4294 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4295 {
4296 cmdline_del(cclp, j - 2);
4297 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004298 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004299 }
4300 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004301 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004302 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004303 // go up a directory
4304 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004305
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004306 j = cclp->cmdpos - 1;
4307 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4308 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004309 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004310 if (has_mbyte)
4311 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4312 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004313#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004314 && vim_strchr((char_u *)" *?[{`$%#",
4315 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004316#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004317 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004318 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004319 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004320 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004321 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004322 break;
4323 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004324 else
4325 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004326 }
4327 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004328
4329 if (!found)
4330 j = i;
4331 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4332 j += 4;
4333 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4334 && j == i)
4335 j += 3;
4336 else
4337 j = 0;
4338 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004339 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004340 // TODO this is only for DOS/UNIX systems - need to put in
4341 // machine-specific stuff here and in upseg init
4342 cmdline_del(cclp, j);
4343 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004344 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004345 else if (cclp->cmdpos > i)
4346 cmdline_del(cclp, i);
4347
4348 // Now complete in the new directory. Set KeyTyped in case the
4349 // Up key came from a mapping.
4350 key = p_wc;
4351 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004352 }
4353
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004354 return key;
4355}
4356
4357/*
4358 * Handle a key pressed when the wild menu is displayed
4359 */
4360 int
4361wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4362{
4363 if (xp->xp_context == EXPAND_MENUNAMES)
4364 return wildmenu_process_key_menunames(cclp, key, xp);
4365 else if ((xp->xp_context == EXPAND_FILES
4366 || xp->xp_context == EXPAND_DIRECTORIES
4367 || xp->xp_context == EXPAND_SHELLCMD))
4368 return wildmenu_process_key_filenames(cclp, key, xp);
4369
4370 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004371}
4372
4373/*
4374 * Free expanded names when finished walking through the matches
4375 */
4376 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004377wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004378{
4379 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004380
4381 if (!p_wmnu || wild_menu_showing == 0)
4382 return;
4383
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004384#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004385 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004386 if (cclp->input_fn)
4387 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004388#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004389
Girish Palya6b49fba2025-06-28 19:47:34 +02004390#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
4391 // Clear highlighting applied during wildmenu activity
4392 set_no_hlsearch(TRUE);
4393#endif
4394
Bram Moolenaareadee482020-09-04 15:37:31 +02004395 if (wild_menu_showing == WM_SCROLLED)
4396 {
4397 // Entered command line, move it up
4398 cmdline_row--;
4399 redrawcmd();
4400 }
4401 else if (save_p_ls != -1)
4402 {
4403 // restore 'laststatus' and 'winminheight'
4404 p_ls = save_p_ls;
4405 p_wmh = save_p_wmh;
4406 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004407 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004408 redrawcmd();
4409 save_p_ls = -1;
4410 }
4411 else
4412 {
4413 win_redraw_last_status(topframe);
4414 redraw_statuslines();
4415 }
4416 KeyTyped = skt;
4417 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004418#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004419 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004420 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004421#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004422}
Bram Moolenaareadee482020-09-04 15:37:31 +02004423
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004424#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004425/*
4426 * "getcompletion()" function
4427 */
4428 void
4429f_getcompletion(typval_T *argvars, typval_T *rettv)
4430{
4431 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004432 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004433 expand_T xpc;
4434 int filtered = FALSE;
4435 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004436 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004437
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004438 if (in_vim9script()
4439 && (check_for_string_arg(argvars, 0) == FAIL
4440 || check_for_string_arg(argvars, 1) == FAIL
4441 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4442 return;
4443
ii144785fe02021-11-21 12:13:56 +00004444 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004445 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004446 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004447 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004448
4449 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004450 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004451
4452 if (p_wic)
4453 options |= WILD_ICASE;
4454
4455 // For filtered results, 'wildignore' is used
4456 if (!filtered)
4457 options |= WILD_KEEP_ALL;
4458
4459 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004460 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004461 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004462 int cmdline_len = (int)STRLEN(pat);
4463 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004464 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004465 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004466 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004467 else
4468 {
ii144785fe02021-11-21 12:13:56 +00004469 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004470 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004471 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004472
4473 xpc.xp_context = cmdcomplete_str_to_type(type);
4474 if (xpc.xp_context == EXPAND_NOTHING)
4475 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004476 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004477 return;
4478 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004479
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004480 if (xpc.xp_context == EXPAND_USER_DEFINED)
4481 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004482 // Must be "custom,funcname" pattern
4483 if (STRNCMP(type, "custom,", 7) != 0)
4484 {
4485 semsg(_(e_invalid_argument_str), type);
4486 return;
4487 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004488
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004489 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004490 }
4491
4492 if (xpc.xp_context == EXPAND_USER_LIST)
4493 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004494 // Must be "customlist,funcname" pattern
4495 if (STRNCMP(type, "customlist,", 11) != 0)
4496 {
4497 semsg(_(e_invalid_argument_str), type);
4498 return;
4499 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004500
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004501 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004502 }
4503
Bram Moolenaar66b51422019-08-18 21:44:12 +02004504# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004505 if (xpc.xp_context == EXPAND_MENUS)
4506 {
4507 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4508 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4509 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004510# endif
4511# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004512 if (xpc.xp_context == EXPAND_CSCOPE)
4513 {
4514 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4515 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4516 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004517# endif
4518# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004519 if (xpc.xp_context == EXPAND_SIGN)
4520 {
4521 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4522 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4523 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004524# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004525 if (xpc.xp_context == EXPAND_RUNTIME)
4526 {
4527 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4528 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4529 }
zeertzjq85f36d62024-10-10 19:14:13 +02004530 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4531 {
4532 int context = EXPAND_SHELLCMDLINE;
4533 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4534 &context);
4535 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4536 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004537 if (xpc.xp_context == EXPAND_FILETYPECMD)
4538 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004539 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004540
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004541 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004542 // when fuzzy matching, don't modify the search string
4543 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004544 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004545 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004546
Bram Moolenaar93a10962022-06-16 11:42:09 +01004547 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004548 {
4549 int i;
4550
4551 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4552
4553 for (i = 0; i < xpc.xp_numfiles; i++)
4554 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4555 }
4556 vim_free(pat);
4557 ExpandCleanup(&xpc);
4558}
Girish Palya92f68e22025-04-21 11:12:41 +02004559
4560/*
Hirohito Higashi96b3ef22025-07-05 15:31:23 +02004561 * "getcompletiontype()" function
4562 */
4563 void
4564f_getcompletiontype(typval_T *argvars, typval_T *rettv)
4565{
4566 char_u *pat;
4567 expand_T xpc;
4568 int cmdline_len;
4569
4570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = NULL;
4572
4573 if (check_for_string_arg(argvars, 0) == FAIL)
4574 return;
4575
4576 pat = tv_get_string(&argvars[0]);
4577 ExpandInit(&xpc);
4578
4579 cmdline_len = (int)STRLEN(pat);
4580 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
zeertzjqe2c0f812025-07-06 20:26:56 +02004581 rettv->vval.v_string = cmdcomplete_type_to_str(xpc.xp_context, xpc.xp_arg);
Hirohito Higashi96b3ef22025-07-05 15:31:23 +02004582
4583 ExpandCleanup(&xpc);
4584}
4585
4586/*
Girish Palya92f68e22025-04-21 11:12:41 +02004587 * "cmdcomplete_info()" function
4588 */
4589 void
4590f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4591{
4592 cmdline_info_T *ccline = get_cmdline_info();
4593 dict_T *retdict;
4594 list_T *li;
4595 int idx;
4596 int ret = OK;
4597
4598 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4599 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4600 return;
4601 retdict = rettv->vval.v_dict;
4602 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4603 if (ret == OK)
4604 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4605 if (ret == OK)
4606 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4607 if (ret == OK)
4608 {
4609 li = list_alloc();
4610 if (li == NULL)
4611 return;
4612 ret = dict_add_list(retdict, "matches", li);
4613 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4614 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4615 }
4616}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004617#endif // FEAT_EVAL
Girish Palya6b49fba2025-06-28 19:47:34 +02004618
4619/*
4620 * Copy a substring from the current buffer (curbuf), spanning from the given
4621 * 'start' position to the word boundary after 'end' position.
4622 * The copied string is stored in '*match', and the actual end position of the
4623 * matched text is returned in '*match_end'.
4624 */
4625 static int
4626copy_substring_from_pos(pos_T *start, pos_T *end, char_u **match,
4627 pos_T *match_end)
4628{
4629 char_u *word_end;
4630 char_u *line, *start_line, *end_line;
4631 int segment_len;
4632 linenr_T lnum;
4633 garray_T ga;
Girish Palya93c2d5b2025-07-08 21:29:02 +02004634 int exacttext = vim_strchr(p_wop, WOP_EXACTTEXT) != NULL;
Girish Palya6b49fba2025-06-28 19:47:34 +02004635
4636 if (start->lnum > end->lnum
4637 || (start->lnum == end->lnum && start->col >= end->col))
4638 return FAIL; // invalid range
4639
Girish Palya6b49fba2025-06-28 19:47:34 +02004640 // Use a growable string (ga)
4641 ga_init2(&ga, 1, 128);
4642
4643 // Append start line from start->col to end
zeertzjq5e34eec2025-07-05 15:37:17 +02004644 start_line = ml_get(start->lnum);
Girish Palya6b49fba2025-06-28 19:47:34 +02004645 char_u *start_ptr = start_line + start->col;
4646 int is_single_line = start->lnum == end->lnum;
4647
4648 segment_len = is_single_line ? (end->col - start->col)
glepnir7cf35bc2025-06-29 16:51:40 +02004649 : (int)STRLEN(start_ptr);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004650 if (ga_grow(&ga, segment_len + 2) != OK)
Girish Palya6b49fba2025-06-28 19:47:34 +02004651 return FAIL;
glepnir7cf35bc2025-06-29 16:51:40 +02004652
Girish Palya6b49fba2025-06-28 19:47:34 +02004653 ga_concat_len(&ga, start_ptr, segment_len);
4654 if (!is_single_line)
Girish Palya93c2d5b2025-07-08 21:29:02 +02004655 {
4656 if (exacttext)
4657 ga_concat_len(&ga, (char_u *)"\\n", 2);
4658 else
4659 ga_append(&ga, '\n');
4660 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004661
4662 // Append full lines between start and end
4663 if (!is_single_line)
glepnir7cf35bc2025-06-29 16:51:40 +02004664 {
Girish Palya6b49fba2025-06-28 19:47:34 +02004665 for (lnum = start->lnum + 1; lnum < end->lnum; lnum++)
4666 {
4667 line = ml_get(lnum);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004668 if (ga_grow(&ga, ml_get_len(lnum) + 2) != OK)
Girish Palya6b49fba2025-06-28 19:47:34 +02004669 return FAIL;
4670 ga_concat(&ga, line);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004671 if (exacttext)
4672 ga_concat_len(&ga, (char_u *)"\\n", 2);
4673 else
4674 ga_append(&ga, '\n');
Girish Palya6b49fba2025-06-28 19:47:34 +02004675 }
glepnir7cf35bc2025-06-29 16:51:40 +02004676 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004677
4678 // Append partial end line (up to word end)
zeertzjq5e34eec2025-07-05 15:37:17 +02004679 end_line = ml_get(end->lnum);
Girish Palya6b49fba2025-06-28 19:47:34 +02004680 word_end = find_word_end(end_line + end->col);
4681 segment_len = (int)(word_end - end_line);
4682 if (ga_grow(&ga, segment_len) != OK)
4683 return FAIL;
4684 ga_concat_len(&ga, end_line + (is_single_line ? end->col : 0),
4685 segment_len - (is_single_line ? end->col : 0));
4686
4687 // Null-terminate
4688 if (ga_grow(&ga, 1) != OK)
4689 return FAIL;
4690 ga_append(&ga, NUL);
4691
4692 *match = (char_u *)ga.ga_data;
4693 match_end->lnum = end->lnum;
4694 match_end->col = segment_len;
4695
4696 return OK;
4697}
4698
4699/*
Girish Palyaaf220072025-07-07 19:42:10 +02004700 * Returns TRUE if the given string `str` matches the regex pattern `pat`.
4701 * Honors the 'ignorecase' (p_ic) and 'smartcase' (p_scs) settings to determine
4702 * case sensitivity.
4703 */
4704 static int
4705is_regex_match(char_u *pat, char_u *str)
4706{
4707 regmatch_T regmatch;
4708 int result;
4709
4710 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4711 if (regmatch.regprog == NULL)
4712 return FALSE;
4713 regmatch.rm_ic = p_ic;
4714 if (p_ic && p_scs)
4715 regmatch.rm_ic = !pat_has_uppercase(pat);
4716
4717 result = vim_regexec_nl(&regmatch, str, (colnr_T)0);
4718
4719 vim_regfree(regmatch.regprog);
4720 return result;
4721}
4722
4723/*
4724 * Constructs a new match string by appending text from the buffer (starting at
4725 * end_match_pos) to the given pattern `pat`. The result is a concatenation of
4726 * `pat` and the word following end_match_pos.
4727 * If 'lowercase' is TRUE, the appended text is converted to lowercase before
4728 * being combined. Returns the newly allocated match string, or NULL on failure.
4729 */
4730 static char_u *
4731concat_pattern_with_buffer_match(
4732 char_u *pat,
4733 int pat_len,
4734 pos_T *end_match_pos,
4735 int lowercase UNUSED)
4736{
4737 char_u *line = ml_get(end_match_pos->lnum);
4738 char_u *word_end = find_word_end(line + end_match_pos->col);
4739 int match_len = (int)(word_end - (line + end_match_pos->col));
4740 char_u *match = alloc(match_len + pat_len + 1); // +1 for NUL
4741
4742 if (match == NULL)
4743 return NULL;
4744 mch_memmove(match, pat, pat_len);
4745 if (match_len > 0)
4746 {
4747#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
4748 if (lowercase)
4749 {
4750 char_u *mword = vim_strnsave(line + end_match_pos->col,
4751 match_len);
4752 if (mword == NULL)
4753 goto cleanup;
4754 char_u *lower = strlow_save(mword);
4755 vim_free(mword);
4756 if (lower == NULL)
4757 goto cleanup;
4758 mch_memmove(match + pat_len, lower, match_len);
4759 vim_free(lower);
4760 }
4761 else
4762#endif
4763 mch_memmove(match + pat_len, line + end_match_pos->col, match_len);
4764 }
4765 match[pat_len + match_len] = NUL;
4766 return match;
4767
4768#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
4769cleanup:
4770 vim_free(match);
4771 return NULL;
4772#endif
4773}
4774
4775/*
Girish Palya6b49fba2025-06-28 19:47:34 +02004776 * Search for strings matching "pat" in the specified range and return them.
4777 * Returns OK on success, FAIL otherwise.
4778 */
4779 static int
4780expand_pattern_in_buf(
4781 char_u *pat, // pattern to match
4782 int dir, // direction: FORWARD or BACKWARD
4783 char_u ***matches, // return: array with matched strings
4784 int *numMatches) // return: number of matches
4785{
4786 pos_T cur_match_pos, prev_match_pos, end_match_pos, word_end_pos;
4787 garray_T ga;
4788 int found_new_match;
4789 int looped_around = FALSE;
Girish Palyaaf220072025-07-07 19:42:10 +02004790 int pat_len;
Girish Palya6b49fba2025-06-28 19:47:34 +02004791 int has_range = FALSE;
4792 int compl_started = FALSE;
glepnir7cf35bc2025-06-29 16:51:40 +02004793 int search_flags;
Girish Palyaaf220072025-07-07 19:42:10 +02004794 char_u *match, *full_match;
Girish Palya93c2d5b2025-07-08 21:29:02 +02004795 int exacttext = vim_strchr(p_wop, WOP_EXACTTEXT) != NULL;
Girish Palya6b49fba2025-06-28 19:47:34 +02004796
4797#ifdef FEAT_SEARCH_EXTRA
4798 has_range = search_first_line != 0;
4799#endif
4800
4801 *matches = NULL;
4802 *numMatches = 0;
4803
4804 if (pat == NULL || *pat == NUL)
4805 return FAIL;
4806
4807 pat_len = (int)STRLEN(pat);
4808 CLEAR_FIELD(cur_match_pos);
4809 CLEAR_FIELD(prev_match_pos);
4810#ifdef FEAT_SEARCH_EXTRA
4811 if (has_range)
4812 cur_match_pos.lnum = search_first_line;
4813 else
4814#endif
4815 cur_match_pos = pre_incsearch_pos;
4816
4817 search_flags = SEARCH_OPT | SEARCH_NOOF | SEARCH_PEEK | SEARCH_NFMSG
4818 | (has_range ? SEARCH_START : 0);
4819
Girish Palya6b49fba2025-06-28 19:47:34 +02004820 ga_init2(&ga, sizeof(char_u *), 10); // Use growable array of char_u*
4821
4822 for (;;)
4823 {
4824 ++emsg_off;
4825 ++msg_silent;
4826 found_new_match = searchit(NULL, curbuf, &cur_match_pos,
4827 &end_match_pos, dir, pat, pat_len, 1L,
4828 search_flags, RE_LAST, NULL);
4829 --msg_silent;
4830 --emsg_off;
4831
4832 if (found_new_match == FAIL)
4833 break;
4834
4835#ifdef FEAT_SEARCH_EXTRA
4836 // If in range mode, check if match is within the range
4837 if (has_range && (cur_match_pos.lnum < search_first_line
4838 || cur_match_pos.lnum > search_last_line))
4839 break;
4840#endif
4841
4842 if (compl_started)
4843 {
4844 // If we've looped back to an earlier match, stop
glepnir7cf35bc2025-06-29 16:51:40 +02004845 if ((dir == FORWARD && LTOREQ_POS(cur_match_pos, prev_match_pos)) ||
4846 (dir == BACKWARD && LTOREQ_POS(prev_match_pos, cur_match_pos)))
Girish Palya6b49fba2025-06-28 19:47:34 +02004847 {
4848 if (looped_around)
4849 break;
4850 else
4851 looped_around = TRUE;
4852 }
4853 }
4854
4855 compl_started = TRUE;
4856 prev_match_pos = cur_match_pos;
4857
4858 // Abort if user typed a character or interrupted
4859 if (char_avail() || got_int)
4860 {
4861 if (got_int)
4862 {
4863 (void)vpeekc(); // Remove <C-C> from input stream
4864 got_int = FALSE; // Don't abandon the command line
4865 }
4866 goto cleanup;
4867 }
4868
4869 // searchit() can return line number +1 past the last line when
4870 // searching for "foo\n" if "foo" is at end of buffer.
4871 if (end_match_pos.lnum > curbuf->b_ml.ml_line_count)
4872 {
4873 cur_match_pos.lnum = 1;
4874 cur_match_pos.col = 0;
4875 cur_match_pos.coladd = 0;
4876 continue;
4877 }
4878
4879 // Extract the matching text prepended to completed word
Girish Palyaaf220072025-07-07 19:42:10 +02004880 if (!copy_substring_from_pos(&cur_match_pos, &end_match_pos, &full_match,
Girish Palya6b49fba2025-06-28 19:47:34 +02004881 &word_end_pos))
4882 break;
4883
Girish Palya93c2d5b2025-07-08 21:29:02 +02004884 if (exacttext)
4885 match = full_match;
4886 else
Girish Palya6b49fba2025-06-28 19:47:34 +02004887 {
Girish Palya93c2d5b2025-07-08 21:29:02 +02004888 // Construct a new match from completed word appended to pattern itself
4889 match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos,
4890 FALSE);
4891
4892 // The regex pattern may include '\C' or '\c'. First, try matching the
4893 // buffer word as-is. If it doesn't match, try again with the lowercase
4894 // version of the word to handle smartcase behavior.
Girish Palyaaf220072025-07-07 19:42:10 +02004895 if (match == NULL || !is_regex_match(match, full_match))
4896 {
4897 vim_free(match);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004898 match = concat_pattern_with_buffer_match(pat, pat_len,
4899 &end_match_pos, TRUE);
4900 if (match == NULL || !is_regex_match(match, full_match))
4901 {
4902 vim_free(match);
4903 vim_free(full_match);
4904 continue;
4905 }
Girish Palyaaf220072025-07-07 19:42:10 +02004906 }
Girish Palya93c2d5b2025-07-08 21:29:02 +02004907 vim_free(full_match);
Girish Palya6b49fba2025-06-28 19:47:34 +02004908 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004909
4910 // Include this match if it is not a duplicate
glepnir7cf35bc2025-06-29 16:51:40 +02004911 for (int i = 0; i < ga.ga_len; ++i)
Girish Palya6b49fba2025-06-28 19:47:34 +02004912 {
4913 if (STRCMP(match, ((char_u **)ga.ga_data)[i]) == 0)
4914 {
4915 VIM_CLEAR(match);
4916 break;
4917 }
4918 }
4919 if (match != NULL)
4920 {
4921 if (ga_grow(&ga, 1) == FAIL)
4922 goto cleanup;
4923 ((char_u **)ga.ga_data)[ga.ga_len++] = match;
4924 if (ga.ga_len > TAG_MANY)
4925 break;
4926 }
4927 if (has_range)
4928 cur_match_pos = word_end_pos;
4929 }
4930
Girish Palya6b49fba2025-06-28 19:47:34 +02004931 *matches = (char_u **)ga.ga_data;
4932 *numMatches = ga.ga_len;
4933 return OK;
4934
4935cleanup:
Girish Palya6b49fba2025-06-28 19:47:34 +02004936 ga_clear_strings(&ga);
4937 return FAIL;
4938}