blob: bdfa67945788558192ead2efcaadcfb47c689fc5 [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 pre_incsearch_pos = xp->xp_pre_incsearch_pos;
Jim Zhou3255af82025-02-27 19:29:50 +0100237#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100238 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100239 {
240 // Expand commands typed in input() function
241 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100242 }
243 else
Jim Zhou3255af82025-02-27 19:29:50 +0100244#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100245 {
Christian Brabandtf2ec8d42025-07-08 22:04:10 +0200246 may_expand_pattern = options & WILD_MAY_EXPAND_PATTERN;
Jim Zhou3255af82025-02-27 19:29:50 +0100247 set_expand_context(xp);
Christian Brabandtf2ec8d42025-07-08 22:04:10 +0200248 may_expand_pattern = FALSE;
zeertzjq7a5115c2025-03-19 20:29:58 +0100249 }
250 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200251 }
252
253 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
254 {
255 beep_flush();
256 return OK; // Something illegal on command line
257 }
258 if (xp->xp_context == EXPAND_NOTHING)
259 {
260 // Caller can use the character as a normal char instead
261 return FAIL;
262 }
263
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000264 // If cmd_silent is set then don't show the dots, because redrawcmd() below
265 // won't remove them.
266 if (!cmd_silent)
267 {
268 msg_puts("..."); // show that we are busy
269 out_flush();
270 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200271
272 i = (int)(xp->xp_pattern - ccline->cmdbuff);
273 xp->xp_pattern_len = ccline->cmdpos - i;
274
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000275 if (type == WILD_NEXT || type == WILD_PREV
276 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200277 {
278 // Get next/previous match for a previous expanded pattern.
John Marriotta494ce12025-07-03 21:28:50 +0200279 p = ExpandOne(xp, NULL, NULL, 0, type);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200280 }
281 else
282 {
John Marriotta494ce12025-07-03 21:28:50 +0200283 char_u *tmp;
284
Girish Palya6b49fba2025-06-28 19:47:34 +0200285 if (cmdline_fuzzy_completion_supported(xp)
286 || xp->xp_context == EXPAND_PATTERN_IN_BUF)
287 // Don't modify the search string
John Marriotta494ce12025-07-03 21:28:50 +0200288 tmp = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000289 else
John Marriotta494ce12025-07-03 21:28:50 +0200290 tmp = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000291
Bram Moolenaar66b51422019-08-18 21:44:12 +0200292 // Translate string into pattern and expand it.
John Marriotta494ce12025-07-03 21:28:50 +0200293 if (tmp == NULL)
294 p = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200295 else
296 {
297 int use_options = options |
298 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100299 if (use_options & WILD_KEEP_SOLE_ITEM)
300 use_options &= ~WILD_KEEP_SOLE_ITEM;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200301 if (escape)
302 use_options |= WILD_ESCAPE;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200303 if (p_wic)
304 use_options += WILD_ICASE;
Girish Palya6b49fba2025-06-28 19:47:34 +0200305
John Marriotta494ce12025-07-03 21:28:50 +0200306 p = ExpandOne(xp, tmp,
Bram Moolenaar66b51422019-08-18 21:44:12 +0200307 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
308 use_options, type);
John Marriotta494ce12025-07-03 21:28:50 +0200309 vim_free(tmp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200310 // longest match: make sure it is not shorter, happens with :help
John Marriotta494ce12025-07-03 21:28:50 +0200311 if (p != NULL && type == WILD_LONGEST)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200312 {
John Marriotta494ce12025-07-03 21:28:50 +0200313 int j;
314
Bram Moolenaar66b51422019-08-18 21:44:12 +0200315 for (j = 0; j < xp->xp_pattern_len; ++j)
John Marriotta494ce12025-07-03 21:28:50 +0200316 {
317 char_u c = ccline->cmdbuff[i + j];
318 if (c == '*' || c == '?')
319 break;
320 }
321 if ((int)STRLEN(p) < j)
322 VIM_CLEAR(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200323 }
324 }
325 }
326
John Marriotta494ce12025-07-03 21:28:50 +0200327 if (p != NULL && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200328 {
John Marriotta494ce12025-07-03 21:28:50 +0200329 size_t plen = STRLEN(p);
330 int difflen;
331 int v;
332
333 difflen = (int)plen - xp->xp_pattern_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200334 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
335 {
336 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
337 xp->xp_pattern = ccline->cmdbuff + i;
338 }
339 else
340 v = OK;
341 if (v == OK)
342 {
343 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
344 &ccline->cmdbuff[ccline->cmdpos],
345 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
John Marriotta494ce12025-07-03 21:28:50 +0200346 mch_memmove(&ccline->cmdbuff[i], p, plen);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200347 ccline->cmdlen += difflen;
348 ccline->cmdpos += difflen;
349 }
350 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200351
352 redrawcmd();
353 cursorcmd();
354
355 // When expanding a ":map" command and no matches are found, assume that
356 // the key is supposed to be inserted literally
John Marriotta494ce12025-07-03 21:28:50 +0200357 if (xp->xp_context == EXPAND_MAPPINGS && p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200358 return FAIL;
359
John Marriotta494ce12025-07-03 21:28:50 +0200360 if (xp->xp_numfiles <= 0 && p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200361 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100362 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200363 // free expanded pattern
364 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
365
John Marriotta494ce12025-07-03 21:28:50 +0200366 vim_free(p);
John Marriott1be5b372025-06-23 19:57:29 +0200367
Bram Moolenaar66b51422019-08-18 21:44:12 +0200368 return OK;
369}
370
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000371/*
372 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000373 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 */
375 static int
376cmdline_pum_create(
377 cmdline_info_T *ccline,
378 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000379 char_u **matches,
380 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000381 int showtail)
382{
383 int i;
384 int columns;
385
386 // Add all the completion matches
John Marriott1be5b372025-06-23 19:57:29 +0200387 compl_match_array = ALLOC_MULT(pumitem_T, numMatches);
388 if (compl_match_array == NULL)
389 return EXPAND_UNSUCCESSFUL;
390
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000391 compl_match_arraysize = numMatches;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000392 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000393 {
zeertzjqc51a3762022-12-10 10:22:29 +0000394 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000395 compl_match_array[i].pum_info = NULL;
396 compl_match_array[i].pum_extra = NULL;
397 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200398 compl_match_array[i].pum_user_abbr_hlattr = -1;
399 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000400 }
401
402 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200403 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000404 columns = vim_strsize(xp->xp_pattern);
405 if (showtail)
406 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000407 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000408 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000409 }
glepnir977561a2025-02-13 20:48:56 +0100410 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000411
412 // no default selection
413 compl_selected = -1;
414
Girish Palya0cd7f352025-07-07 19:47:53 +0200415 pum_clear();
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000416 cmdline_pum_display();
417
418 return EXPAND_OK;
419}
420
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000421/*
422 * Display the cmdline completion matches in a popup menu
423 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000424 void
425cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426{
427 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
428}
429
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000430/*
431 * Returns TRUE if the cmdline completion popup menu is being displayed.
432 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000433 int
434cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100436 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000437}
438
439/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000440 * Remove the cmdline completion popup menu (if present), free the list of
441 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000442 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000443 void
zeertzjq1830e782025-03-13 20:29:13 +0100444cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000445{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000446 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100447 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100448#ifdef FEAT_EVAL
449 int save_RedrawingDisabled = RedrawingDisabled;
450 if (cclp->input_fn)
451 RedrawingDisabled = 0;
452#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000453
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000454 pum_undisplay();
455 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200456 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000457 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000458 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000459 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000460 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100461
462 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
463 // as a side effect.
464 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100465#ifdef FEAT_EVAL
466 if (cclp->input_fn)
467 RedrawingDisabled = save_RedrawingDisabled;
468#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000469}
470
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000471 void
472cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000473{
zeertzjq1830e782025-03-13 20:29:13 +0100474 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000475 wildmenu_cleanup(cclp);
476}
477
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000478/*
479 * Returns the starting column number to use for the cmdline completion popup
480 * menu.
481 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000482 int
483cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000484{
485 return compl_startcol;
486}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000487
Bram Moolenaar66b51422019-08-18 21:44:12 +0200488/*
zeertzjqd8c93402024-06-17 18:25:32 +0200489 * Returns the current cmdline completion pattern.
490 */
491 char_u *
492cmdline_compl_pattern(void)
493{
494 expand_T *xp = get_cmdline_info()->xpc;
495
496 return xp == NULL ? NULL : xp->xp_orig;
497}
498
499/*
500 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
501 */
502 int
503cmdline_compl_is_fuzzy(void)
504{
505 expand_T *xp = get_cmdline_info()->xpc;
506
507 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
508}
509
510/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000511 * Return the number of characters that should be skipped in a status match.
Girish Palya6b49fba2025-06-28 19:47:34 +0200512 * These are backslashes used for escaping. Do show backslashes in help tags
513 * and in search pattern completion matches.
Bram Moolenaard6e91382022-11-12 17:44:13 +0000514 */
515 static int
516skip_status_match_char(expand_T *xp, char_u *s)
517{
Girish Palya6b49fba2025-06-28 19:47:34 +0200518 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP
519 && xp->xp_context != EXPAND_PATTERN_IN_BUF)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000520#ifdef FEAT_MENU
521 || ((xp->xp_context == EXPAND_MENUS
522 || xp->xp_context == EXPAND_MENUNAMES)
523 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
524#endif
525 )
526 {
527#ifndef BACKSLASH_IN_FILENAME
528 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
529 return 2;
530#endif
531 return 1;
532 }
533 return 0;
534}
535
536/*
537 * Get the length of an item as it will be shown in the status line.
538 */
539 static int
540status_match_len(expand_T *xp, char_u *s)
541{
542 int len = 0;
543
544#ifdef FEAT_MENU
545 int emenu = xp->xp_context == EXPAND_MENUS
546 || xp->xp_context == EXPAND_MENUNAMES;
547
548 // Check for menu separators - replace with '|'.
549 if (emenu && menu_is_separator(s))
550 return 1;
551#endif
552
553 while (*s != NUL)
554 {
555 s += skip_status_match_char(xp, s);
556 len += ptr2cells(s);
557 MB_PTR_ADV(s);
558 }
559
560 return len;
561}
562
563/*
564 * Show wildchar matches in the status line.
565 * Show at least the "match" item.
566 * We start at item 'first_match' in the list and show all matches that fit.
567 *
568 * If inversion is possible we use it. Else '=' characters are used.
569 */
570 static void
571win_redr_status_matches(
572 expand_T *xp,
573 int num_matches,
574 char_u **matches, // list of matches
575 int match,
576 int showtail)
577{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000578 int row;
579 char_u *buf;
580 int len;
581 int clen; // length in screen cells
582 int fillchar;
583 int attr;
584 int i;
585 int highlight = TRUE;
586 char_u *selstart = NULL;
587 int selstart_col = 0;
588 char_u *selend = NULL;
589 static int first_match = 0;
590 int add_left = FALSE;
591 char_u *s;
592#ifdef FEAT_MENU
593 int emenu;
594#endif
595 int l;
596
597 if (matches == NULL) // interrupted completion?
598 return;
599
600 if (has_mbyte)
601 buf = alloc(Columns * MB_MAXBYTES + 1);
602 else
603 buf = alloc(Columns + 1);
604 if (buf == NULL)
605 return;
606
607 if (match == -1) // don't show match but original text
608 {
609 match = 0;
610 highlight = FALSE;
611 }
612 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000613 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000614 if (match == 0)
615 first_match = 0;
616 else if (match < first_match)
617 {
618 // jumping left, as far as we can go
619 first_match = match;
620 add_left = TRUE;
621 }
622 else
623 {
624 // check if match fits on the screen
625 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000626 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000627 if (first_match > 0)
628 clen += 2;
629 // jumping right, put match at the left
630 if ((long)clen > Columns)
631 {
632 first_match = match;
633 // if showing the last match, we can add some on the left
634 clen = 2;
635 for (i = match; i < num_matches; ++i)
636 {
zeertzjqc51a3762022-12-10 10:22:29 +0000637 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000638 if ((long)clen >= Columns)
639 break;
640 }
641 if (i == num_matches)
642 add_left = TRUE;
643 }
644 }
645 if (add_left)
646 while (first_match > 0)
647 {
zeertzjqc51a3762022-12-10 10:22:29 +0000648 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000649 if ((long)clen >= Columns)
650 break;
651 --first_match;
652 }
653
654 fillchar = fillchar_status(&attr, curwin);
655
656 if (first_match == 0)
657 {
658 *buf = NUL;
659 len = 0;
660 }
661 else
662 {
663 STRCPY(buf, "< ");
664 len = 2;
665 }
666 clen = len;
667
668 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000669 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000670 {
671 if (i == match)
672 {
673 selstart = buf + len;
674 selstart_col = clen;
675 }
676
zeertzjqc51a3762022-12-10 10:22:29 +0000677 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000678 // Check for menu separators - replace with '|'
679#ifdef FEAT_MENU
680 emenu = (xp->xp_context == EXPAND_MENUS
681 || xp->xp_context == EXPAND_MENUNAMES);
682 if (emenu && menu_is_separator(s))
683 {
684 STRCPY(buf + len, transchar('|'));
685 l = (int)STRLEN(buf + len);
686 len += l;
687 clen += l;
688 }
689 else
690#endif
John Marriotta494ce12025-07-03 21:28:50 +0200691 for ( ; *s != NUL; ++s)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000692 {
693 s += skip_status_match_char(xp, s);
694 clen += ptr2cells(s);
695 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
696 {
697 STRNCPY(buf + len, s, l);
698 s += l - 1;
699 len += l;
700 }
701 else
702 {
703 STRCPY(buf + len, transchar_byte(*s));
704 len += (int)STRLEN(buf + len);
705 }
706 }
707 if (i == match)
708 selend = buf + len;
709
710 *(buf + len++) = ' ';
711 *(buf + len++) = ' ';
712 clen += 2;
713 if (++i == num_matches)
714 break;
715 }
716
717 if (i != num_matches)
718 {
719 *(buf + len++) = '>';
720 ++clen;
721 }
722
723 buf[len] = NUL;
724
725 row = cmdline_row - 1;
726 if (row >= 0)
727 {
728 if (wild_menu_showing == 0)
729 {
730 if (msg_scrolled > 0)
731 {
732 // Put the wildmenu just above the command line. If there is
733 // no room, scroll the screen one line up.
734 if (cmdline_row == Rows - 1)
735 {
736 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
737 ++msg_scrolled;
738 }
739 else
740 {
741 ++cmdline_row;
742 ++row;
743 }
744 wild_menu_showing = WM_SCROLLED;
745 }
746 else
747 {
748 // Create status line if needed by setting 'laststatus' to 2.
749 // Set 'winminheight' to zero to avoid that the window is
750 // resized.
751 if (lastwin->w_status_height == 0)
752 {
753 save_p_ls = p_ls;
754 save_p_wmh = p_wmh;
755 p_ls = 2;
756 p_wmh = 0;
757 last_status(FALSE);
758 }
759 wild_menu_showing = WM_SHOWN;
760 }
761 }
762
763 screen_puts(buf, row, 0, attr);
764 if (selstart != NULL && highlight)
765 {
766 *selend = NUL;
767 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
768 }
769
770 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
771 }
772
773 win_redraw_last_status(topframe);
774 vim_free(buf);
775}
776
777/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000778 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200779 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000780 */
781 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100782get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000783{
glepnir977561a2025-02-13 20:48:56 +0100784 int findex = xp->xp_selected;
785 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000786
zeertzjqb6c900b2025-02-14 17:59:31 +0100787 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000788 if (xp->xp_numfiles <= 0)
789 return NULL;
790
791 if (mode == WILD_PREV)
792 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100793 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000794 if (findex == -1)
795 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100796 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000797 --findex;
798 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000799 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000800 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100801 // Select the next entry
802 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000803 }
glepnir977561a2025-02-13 20:48:56 +0100804 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000805 {
glepnir977561a2025-02-13 20:48:56 +0100806 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
807 ht = pum_get_height();
808 if (ht > 3)
809 ht -= 2;
810
811 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000812 {
glepnir977561a2025-02-13 20:48:56 +0100813 if (findex == 0)
814 // at the first entry, don't select any entries
815 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100816 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100817 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000818 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100819 else
820 // go up by the pum height
821 findex = MAX(findex - ht, 0);
822 }
823 else // mode == WILD_PAGEDOWN
824 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100825 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100826 // at the last entry, don't select any entries
827 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100828 else if (findex < 0)
829 // no entry is selected, select the first entry
830 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100831 else
832 // go down by the pum height
833 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000834 }
835 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000836
glepnir977561a2025-02-13 20:48:56 +0100837 // Handle wrapping around
838 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000839 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100840 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100841 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000842 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000843 else
glepnir977561a2025-02-13 20:48:56 +0100844 // Wrap around to opposite end
845 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000846 }
glepnir977561a2025-02-13 20:48:56 +0100847
848 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000849 if (compl_match_array)
850 {
851 compl_selected = findex;
852 cmdline_pum_display();
853 }
854 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100855 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
856 cmd_showtail);
857
zeertzjqe9ef3472023-08-17 23:57:05 +0200858 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100859 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100860 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000861}
862
863/*
864 * Start the command-line expansion and get the matches.
865 */
866 static char_u *
867ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
868{
869 int non_suf_match; // number without matching suffix
870 int i;
871 char_u *ss = NULL;
872
873 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000874 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000875 options) == FAIL)
876 {
877#ifdef FNAME_ILLEGAL
878 // Illegal file name has been silently skipped. But when there
879 // are wildcards, the real problem is that there was no match,
880 // causing the pattern to be added, which has illegal characters.
881 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
882 semsg(_(e_no_match_str_2), str);
883#endif
884 }
885 else if (xp->xp_numfiles == 0)
886 {
887 if (!(options & WILD_SILENT))
888 semsg(_(e_no_match_str_2), str);
889 }
890 else
891 {
892 // Escape the matches for use on the command line.
893 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
894
895 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000896 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000897 {
898 if (xp->xp_numfiles)
899 non_suf_match = xp->xp_numfiles;
900 else
901 non_suf_match = 1;
902 if ((xp->xp_context == EXPAND_FILES
903 || xp->xp_context == EXPAND_DIRECTORIES)
904 && xp->xp_numfiles > 1)
905 {
906 // More than one match; check suffix.
907 // The files will have been sorted on matching suffix in
908 // expand_wildcards, only need to check the first two.
909 non_suf_match = 0;
910 for (i = 0; i < 2; ++i)
911 if (match_suffix(xp->xp_files[i]))
912 ++non_suf_match;
913 }
914 if (non_suf_match != 1)
915 {
916 // Can we ever get here unless it's while expanding
917 // interactively? If not, we can get rid of this all
918 // together. Don't really want to wait for this message
919 // (and possibly have to hit return to continue!).
920 if (!(options & WILD_SILENT))
921 emsg(_(e_too_many_file_names));
922 else if (!(options & WILD_NO_BEEP))
923 beep_flush();
924 }
925 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
926 ss = vim_strsave(xp->xp_files[0]);
927 }
928 }
929
930 return ss;
931}
932
933/*
934 * Return the longest common part in the list of cmdline completion matches.
935 */
936 static char_u *
937find_longest_match(expand_T *xp, int options)
938{
939 long_u len;
940 int mb_len = 1;
941 int c0, ci;
942 int i;
943 char_u *ss;
944
945 for (len = 0; xp->xp_files[0][len]; len += mb_len)
946 {
947 if (has_mbyte)
948 {
949 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
John Marriotta494ce12025-07-03 21:28:50 +0200950 c0 = (*mb_ptr2char)(&xp->xp_files[0][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000951 }
952 else
953 c0 = xp->xp_files[0][len];
954 for (i = 1; i < xp->xp_numfiles; ++i)
955 {
956 if (has_mbyte)
John Marriotta494ce12025-07-03 21:28:50 +0200957 ci = (*mb_ptr2char)(&xp->xp_files[i][len]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000958 else
959 ci = xp->xp_files[i][len];
960 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
961 || xp->xp_context == EXPAND_FILES
962 || xp->xp_context == EXPAND_SHELLCMD
963 || xp->xp_context == EXPAND_BUFFERS))
964 {
965 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
966 break;
967 }
968 else if (c0 != ci)
969 break;
970 }
971 if (i < xp->xp_numfiles)
972 {
973 if (!(options & WILD_NO_BEEP))
974 vim_beep(BO_WILD);
975 break;
976 }
977 }
978
979 ss = alloc(len + 1);
980 if (ss)
981 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
982
983 return ss;
984}
985
986/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000987 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200988 * Chars that should not be expanded must be preceded with a backslash.
989 * Return a pointer to allocated memory containing the new string.
990 * Return NULL for failure.
991 *
992 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200993 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
994 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200995 *
996 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
997 * is WILD_EXPAND_FREE or WILD_ALL.
998 *
999 * mode = WILD_FREE: just free previously expanded matches
1000 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
1001 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
1002 * mode = WILD_NEXT: use next match in multiple match, wrap to first
1003 * mode = WILD_PREV: use previous match in multiple match, wrap to first
1004 * mode = WILD_ALL: return all matches concatenated
1005 * mode = WILD_LONGEST: return longest matched part
1006 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001007 * mode = WILD_APPLY: apply the item selected in the cmdline completion
1008 * popup menu and close the menu.
1009 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
1010 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001011 *
1012 * options = WILD_LIST_NOTFOUND: list entries without a match
1013 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
1014 * options = WILD_USE_NL: Use '\n' for WILD_ALL
1015 * options = WILD_NO_BEEP: Don't beep for multiple matches
1016 * options = WILD_ADD_SLASH: add a slash after directory names
1017 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
1018 * options = WILD_SILENT: don't print warning messages
1019 * options = WILD_ESCAPE: put backslash before special chars
1020 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001021 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 *
1023 * The variables xp->xp_context and xp->xp_backslash must have been set!
1024 */
1025 char_u *
1026ExpandOne(
1027 expand_T *xp,
1028 char_u *str,
1029 char_u *orig, // allocated copy of original of expanded string
1030 int options,
1031 int mode)
1032{
1033 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 int orig_saved = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035
1036 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001037 if (mode == WILD_NEXT || mode == WILD_PREV
1038 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001039 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001040
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001041 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001042 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001043 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001044 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001045 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001046 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001047
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 // free old names
1049 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1050 {
1051 FreeWild(xp->xp_numfiles, xp->xp_files);
1052 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001053 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001054
1055 // The entries from xp_files may be used in the PUM, remove it.
1056 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001057 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001058 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001059 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060
1061 if (mode == WILD_FREE) // only release file name
1062 return NULL;
1063
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001064 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001065 {
zeertzjq28a23602023-09-29 19:58:35 +02001066 vim_free(xp->xp_orig);
1067 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001068 orig_saved = TRUE;
1069
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001070 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001071 }
1072
1073 // Find longest common part
1074 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1075 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001076 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001077 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001078 }
1079
Bram Moolenaar57e95172022-08-20 19:26:14 +01001080 // Concatenate all matching names. Unless interrupted, this can be slow
1081 // and the result probably won't be used.
1082 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001083 {
John Marriotta494ce12025-07-03 21:28:50 +02001084 size_t ss_size = 0;
1085 char *prefix = "";
1086 char *suffix = (options & WILD_USE_NL) ? "\n" : " ";
1087 int n = xp->xp_numfiles - 1;
1088 int i;
1089
1090 if (xp->xp_prefix == XP_PREFIX_NO)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001091 {
John Marriotta494ce12025-07-03 21:28:50 +02001092 prefix = "no";
1093 ss_size = STRLEN_LITERAL("no") * n;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001094 }
John Marriotta494ce12025-07-03 21:28:50 +02001095 else if (xp->xp_prefix == XP_PREFIX_INV)
1096 {
1097 prefix = "inv";
1098 ss_size = STRLEN_LITERAL("inv") * n;
1099 }
1100
1101 for (i = 0; i < xp->xp_numfiles; ++i)
1102 ss_size += STRLEN(xp->xp_files[i]) + 1; // +1 for the suffix
1103 ++ss_size; // +1 for the NUL
1104
1105 ss = alloc(ss_size);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001106 if (ss != NULL)
1107 {
John Marriotta494ce12025-07-03 21:28:50 +02001108 size_t ss_len = 0;
1109
Bram Moolenaar66b51422019-08-18 21:44:12 +02001110 for (i = 0; i < xp->xp_numfiles; ++i)
1111 {
John Marriotta494ce12025-07-03 21:28:50 +02001112 ss_len += vim_snprintf_safelen(
1113 (char *)ss + ss_len,
1114 ss_size - ss_len,
1115 "%s%s%s",
1116 (i > 0) ? prefix : "",
1117 (char *)xp->xp_files[i],
1118 (i < n) ? suffix : "");
Bram Moolenaar66b51422019-08-18 21:44:12 +02001119 }
1120 }
1121 }
1122
1123 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1124 ExpandCleanup(xp);
1125
zeertzjq28a23602023-09-29 19:58:35 +02001126 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001127 if (!orig_saved)
1128 vim_free(orig);
1129
1130 return ss;
1131}
1132
1133/*
1134 * Prepare an expand structure for use.
1135 */
1136 void
1137ExpandInit(expand_T *xp)
1138{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001139 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001140 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001141 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001142 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001143}
1144
1145/*
1146 * Cleanup an expand structure after use.
1147 */
1148 void
1149ExpandCleanup(expand_T *xp)
1150{
1151 if (xp->xp_numfiles >= 0)
1152 {
1153 FreeWild(xp->xp_numfiles, xp->xp_files);
1154 xp->xp_numfiles = -1;
1155 }
zeertzjq28a23602023-09-29 19:58:35 +02001156 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001157}
1158
zeertzjqec270a52025-04-23 20:50:23 +02001159 void
1160clear_cmdline_orig(void)
1161{
1162 VIM_CLEAR(cmdline_orig);
1163}
1164
Bram Moolenaar66b51422019-08-18 21:44:12 +02001165/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001166 * Display one line of completion matches. Multiple matches are displayed in
1167 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001168 * matches - list of completion match names
1169 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001170 * lines - number of output lines
1171 * linenr - line number of matches to display
1172 * maxlen - maximum number of characters in each line
1173 * showtail - display only the tail of the full path of a file name
1174 * dir_attr - highlight attribute to use for directory names
1175 */
1176 static void
1177showmatches_oneline(
1178 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001179 char_u **matches,
1180 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001181 int lines,
1182 int linenr,
1183 int maxlen,
1184 int showtail,
1185 int dir_attr)
1186{
1187 int i, j;
1188 int isdir;
1189 int lastlen;
1190 char_u *p;
1191
1192 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001193 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001194 {
1195 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1196 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001197 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1198 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001199 msg_advance(maxlen + 1);
1200 msg_puts((char *)p);
1201 msg_advance(maxlen + 3);
1202 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1203 break;
1204 }
1205 for (i = maxlen - lastlen; --i >= 0; )
1206 msg_putchar(' ');
1207 if (xp->xp_context == EXPAND_FILES
1208 || xp->xp_context == EXPAND_SHELLCMD
1209 || xp->xp_context == EXPAND_BUFFERS)
1210 {
1211 // highlight directories
1212 if (xp->xp_numfiles != -1)
1213 {
1214 char_u *halved_slash;
1215 char_u *exp_path;
1216 char_u *path;
1217
1218 // Expansion was done before and special characters
1219 // were escaped, need to halve backslashes. Also
1220 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001221 exp_path = expand_env_save_opt(matches[j], TRUE);
1222 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001223 halved_slash = backslash_halve_save(path);
1224 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001225 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001226 vim_free(exp_path);
1227 if (halved_slash != path)
1228 vim_free(halved_slash);
1229 }
1230 else
1231 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001232 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001233 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001234 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001235 else
1236 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001237 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001238 TRUE);
1239 p = NameBuff;
1240 }
1241 }
1242 else
1243 {
1244 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001245 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001246 }
1247 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1248 }
1249 if (msg_col > 0) // when not wrapped around
1250 {
1251 msg_clr_eos();
1252 msg_putchar('\n');
1253 }
1254 out_flush(); // show one line at a time
1255}
1256
1257/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 * Show all matches for completion on the command line.
1259 * Returns EXPAND_NOTHING when the character that triggered expansion should
1260 * be inserted like a normal character.
1261 */
1262 int
1263showmatches(expand_T *xp, int wildmenu UNUSED)
1264{
1265 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001266 int numMatches;
1267 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001268 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 int maxlen;
1270 int lines;
1271 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001272 int attr;
1273 int showtail;
1274
Girish Palya92f68e22025-04-21 11:12:41 +02001275 // Save cmdline before expansion
1276 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001277 {
1278 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001279 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001280 }
Girish Palya92f68e22025-04-21 11:12:41 +02001281
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 if (xp->xp_numfiles == -1)
1283 {
1284 set_expand_context(xp);
1285 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001286 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287 showtail = expand_showtail(xp);
1288 if (i != EXPAND_OK)
1289 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001290 }
1291 else
1292 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001293 numMatches = xp->xp_numfiles;
1294 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001295 showtail = cmd_showtail;
1296 }
1297
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001298 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001299 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001300 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001301
Bram Moolenaar66b51422019-08-18 21:44:12 +02001302 if (!wildmenu)
1303 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001304 msg_didany = FALSE; // lines_left will be set
1305 msg_start(); // prepare for paging
1306 msg_putchar('\n');
1307 out_flush();
1308 cmdline_row = msg_row;
1309 msg_didany = FALSE; // lines_left will be set again
1310 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001311 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001312
1313 if (got_int)
1314 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001315 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001316 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001317 else
1318 {
1319 // find the length of the longest file name
1320 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001321 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001322 {
1323 if (!showtail && (xp->xp_context == EXPAND_FILES
1324 || xp->xp_context == EXPAND_SHELLCMD
1325 || xp->xp_context == EXPAND_BUFFERS))
1326 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001327 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001328 j = vim_strsize(NameBuff);
1329 }
1330 else
zeertzjqc51a3762022-12-10 10:22:29 +00001331 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001332 if (j > maxlen)
1333 maxlen = j;
1334 }
1335
1336 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001337 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001338 else
1339 {
1340 // compute the number of columns and lines for the listing
1341 maxlen += 2; // two spaces between file names
1342 columns = ((int)Columns + 2) / maxlen;
1343 if (columns < 1)
1344 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001345 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001346 }
1347
1348 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1349
1350 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1351 {
1352 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1353 msg_clr_eos();
1354 msg_advance(maxlen - 3);
1355 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1356 }
1357
1358 // list the files line by line
1359 for (i = 0; i < lines; ++i)
1360 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001361 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001362 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001363 if (got_int)
1364 {
1365 got_int = FALSE;
1366 break;
1367 }
1368 }
1369
1370 // we redraw the command below the lines that we have just listed
1371 // This is a bit tricky, but it saves a lot of screen updating.
1372 cmdline_row = msg_row; // will put it back later
1373 }
1374
1375 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001376 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001377
1378 return EXPAND_OK;
1379}
1380
1381/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001382 * gettail() version for showmatches() and win_redr_status_matches():
1383 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001384 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001385 static char_u *
1386showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001387{
1388 char_u *p;
1389 char_u *t = s;
1390 int had_sep = FALSE;
1391
1392 for (p = s; *p != NUL; )
1393 {
1394 if (vim_ispathsep(*p)
1395#ifdef BACKSLASH_IN_FILENAME
1396 && !rem_backslash(p)
1397#endif
1398 )
1399 had_sep = TRUE;
1400 else if (had_sep)
1401 {
1402 t = p;
1403 had_sep = FALSE;
1404 }
1405 MB_PTR_ADV(p);
1406 }
1407 return t;
1408}
1409
1410/*
1411 * Return TRUE if we only need to show the tail of completion matches.
1412 * When not completing file names or there is a wildcard in the path FALSE is
1413 * returned.
1414 */
1415 static int
1416expand_showtail(expand_T *xp)
1417{
1418 char_u *s;
1419 char_u *end;
1420
1421 // When not completing file names a "/" may mean something different.
1422 if (xp->xp_context != EXPAND_FILES
1423 && xp->xp_context != EXPAND_SHELLCMD
1424 && xp->xp_context != EXPAND_DIRECTORIES)
1425 return FALSE;
1426
1427 end = gettail(xp->xp_pattern);
1428 if (end == xp->xp_pattern) // there is no path separator
1429 return FALSE;
1430
1431 for (s = xp->xp_pattern; s < end; s++)
1432 {
1433 // Skip escaped wildcards. Only when the backslash is not a path
1434 // separator, on DOS the '*' "path\*\file" must not be skipped.
1435 if (rem_backslash(s))
1436 ++s;
1437 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1438 return FALSE;
1439 }
1440 return TRUE;
1441}
1442
1443/*
1444 * Prepare a string for expansion.
1445 * When expanding file names: The string will be used with expand_wildcards().
1446 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1447 * When expanding other names: The string will be used with regcomp(). Copy
1448 * the name into allocated memory and prepend "^".
1449 */
1450 char_u *
1451addstar(
1452 char_u *fname,
1453 int len,
1454 int context) // EXPAND_FILES etc.
1455{
1456 char_u *retval;
1457 int i, j;
1458 int new_len;
1459 char_u *tail;
1460 int ends_in_star;
1461
1462 if (context != EXPAND_FILES
1463 && context != EXPAND_FILES_IN_PATH
1464 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001465 && context != EXPAND_DIRECTORIES
1466 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001467 {
1468 // Matching will be done internally (on something other than files).
1469 // So we convert the file-matching-type wildcards into our kind for
1470 // use with vim_regcomp(). First work out how long it will be:
1471
1472 // For help tags the translation is done in find_help_tags().
1473 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001474 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001475 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001476 || context == EXPAND_COLORS
1477 || context == EXPAND_COMPILER
1478 || context == EXPAND_OWNSYNTAX
1479 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001480 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001481 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001482 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001483 || ((context == EXPAND_TAGS_LISTFILES
1484 || context == EXPAND_TAGS)
1485 && fname[0] == '/'))
1486 retval = vim_strnsave(fname, len);
1487 else
1488 {
1489 new_len = len + 2; // +2 for '^' at start, NUL at end
1490 for (i = 0; i < len; i++)
1491 {
1492 if (fname[i] == '*' || fname[i] == '~')
1493 new_len++; // '*' needs to be replaced by ".*"
1494 // '~' needs to be replaced by "\~"
1495
1496 // Buffer names are like file names. "." should be literal
1497 if (context == EXPAND_BUFFERS && fname[i] == '.')
1498 new_len++; // "." becomes "\."
1499
1500 // Custom expansion takes care of special things, match
1501 // backslashes literally (perhaps also for other types?)
1502 if ((context == EXPAND_USER_DEFINED
1503 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1504 new_len++; // '\' becomes "\\"
1505 }
1506 retval = alloc(new_len);
1507 if (retval != NULL)
1508 {
1509 retval[0] = '^';
1510 j = 1;
1511 for (i = 0; i < len; i++, j++)
1512 {
1513 // Skip backslash. But why? At least keep it for custom
1514 // expansion.
1515 if (context != EXPAND_USER_DEFINED
1516 && context != EXPAND_USER_LIST
1517 && fname[i] == '\\'
1518 && ++i == len)
1519 break;
1520
1521 switch (fname[i])
1522 {
1523 case '*': retval[j++] = '.';
1524 break;
1525 case '~': retval[j++] = '\\';
1526 break;
1527 case '?': retval[j] = '.';
1528 continue;
1529 case '.': if (context == EXPAND_BUFFERS)
1530 retval[j++] = '\\';
1531 break;
1532 case '\\': if (context == EXPAND_USER_DEFINED
1533 || context == EXPAND_USER_LIST)
1534 retval[j++] = '\\';
1535 break;
1536 }
1537 retval[j] = fname[i];
1538 }
1539 retval[j] = NUL;
1540 }
1541 }
1542 }
1543 else
1544 {
1545 retval = alloc(len + 4);
1546 if (retval != NULL)
1547 {
1548 vim_strncpy(retval, fname, len);
1549
1550 // Don't add a star to *, ~, ~user, $var or `cmd`.
1551 // * would become **, which walks the whole tree.
1552 // ~ would be at the start of the file name, but not the tail.
1553 // $ could be anywhere in the tail.
1554 // ` could be anywhere in the file name.
1555 // When the name ends in '$' don't add a star, remove the '$'.
1556 tail = gettail(retval);
1557 ends_in_star = (len > 0 && retval[len - 1] == '*');
1558#ifndef BACKSLASH_IN_FILENAME
1559 for (i = len - 2; i >= 0; --i)
1560 {
1561 if (retval[i] != '\\')
1562 break;
1563 ends_in_star = !ends_in_star;
1564 }
1565#endif
1566 if ((*retval != '~' || tail != retval)
1567 && !ends_in_star
1568 && vim_strchr(tail, '$') == NULL
1569 && vim_strchr(retval, '`') == NULL)
1570 retval[len++] = '*';
1571 else if (len > 0 && retval[len - 1] == '$')
1572 --len;
1573 retval[len] = NUL;
1574 }
1575 }
1576 return retval;
1577}
1578
1579/*
1580 * Must parse the command line so far to work out what context we are in.
1581 * Completion can then be done based on that context.
1582 * This routine sets the variables:
1583 * xp->xp_pattern The start of the pattern to be expanded within
1584 * the command line (ends at the cursor).
1585 * xp->xp_context The type of thing to expand. Will be one of:
1586 *
1587 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1588 * the command line, like an unknown command. Caller
1589 * should beep.
1590 * EXPAND_NOTHING Unrecognised context for completion, use char like
1591 * a normal char, rather than for completion. eg
1592 * :s/^I/
1593 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1594 * it.
1595 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1596 * EXPAND_FILES After command with EX_XFILE set, or after setting
1597 * with P_EXPAND set. eg :e ^I, :w>>^I
1598 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001599 * when we know only directories are of interest.
1600 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001601 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1602 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1603 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1604 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1605 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1606 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1607 * EXPAND_EVENTS Complete event names
1608 * EXPAND_SYNTAX Complete :syntax command arguments
1609 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1610 * EXPAND_AUGROUP Complete autocommand group names
1611 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1612 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1613 * eg :unmap a^I , :cunab x^I
1614 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1615 * eg :call sub^I
1616 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1617 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1618 * names in expressions, eg :while s^I
1619 * EXPAND_ENV_VARS Complete environment variable names
1620 * EXPAND_USER Complete user names
Girish Palya6b49fba2025-06-28 19:47:34 +02001621 * EXPAND_PATTERN_IN_BUF Complete pattern in '/', '?', ':s', ':g', etc.
Bram Moolenaar66b51422019-08-18 21:44:12 +02001622 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001623 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001624set_expand_context(expand_T *xp)
1625{
Girish Palya6b49fba2025-06-28 19:47:34 +02001626 cmdline_info_T *ccline = get_cmdline_info();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001627
Girish Palya6b49fba2025-06-28 19:47:34 +02001628 // Handle search commands: '/' or '?'
1629 if ((ccline->cmdfirstc == '/' || ccline->cmdfirstc == '?')
1630 && may_expand_pattern)
1631 {
1632 xp->xp_context = EXPAND_PATTERN_IN_BUF;
1633 xp->xp_search_dir = (ccline->cmdfirstc == '/') ? FORWARD : BACKWARD;
1634 xp->xp_pattern = ccline->cmdbuff;
1635 xp->xp_pattern_len = ccline->cmdpos;
1636#ifdef FEAT_SEARCH_EXTRA
1637 search_first_line = 0; // Search entire buffer
1638#endif
1639 return;
1640 }
1641
1642 // Only handle ':', '>', or '=' command-lines, or expression input
Bram Moolenaar66b51422019-08-18 21:44:12 +02001643 if (ccline->cmdfirstc != ':'
1644#ifdef FEAT_EVAL
1645 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1646 && !ccline->input_fn
1647#endif
Girish Palya6b49fba2025-06-28 19:47:34 +02001648 )
Bram Moolenaar66b51422019-08-18 21:44:12 +02001649 {
1650 xp->xp_context = EXPAND_NOTHING;
1651 return;
1652 }
Girish Palya6b49fba2025-06-28 19:47:34 +02001653
1654 // Fallback to command-line expansion
Bram Moolenaar66b51422019-08-18 21:44:12 +02001655 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1656}
1657
Bram Moolenaard0190392019-08-23 21:17:35 +02001658/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1660 * For user defined commands, the completion context is set in 'xp' and the
1661 * completion flags in 'complp'.
1662 *
1663 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001664 */
1665 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001667{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 char_u *p = NULL;
1669 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001670 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001671
1672 // Isolate the command and search for it in the command table.
1673 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001674 // - the 'k' command can directly be followed by any character, but do
1675 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1676 // find matches anywhere in the command name, do this only for command
1677 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001679 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001680 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 p = cmd + 1;
1683 }
1684 else
1685 {
1686 p = cmd;
1687 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1688 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001689 // A user command may contain digits.
1690 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1691 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 while (ASCII_ISALNUM(*p) || *p == '*')
1693 ++p;
1694 // for python 3.x: ":py3*" commands completion
1695 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1696 {
1697 ++p;
1698 while (ASCII_ISALPHA(*p) || *p == '*')
1699 ++p;
1700 }
1701 // check for non-alpha command
1702 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1703 ++p;
1704 len = (int)(p - cmd);
1705
1706 if (len == 0)
1707 {
1708 xp->xp_context = EXPAND_UNSUCCESSFUL;
1709 return NULL;
1710 }
1711
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001713
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001714 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001715 // Also when doing fuzzy expansion for non-shell commands, support
1716 // alphanumeric characters.
1717 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1718 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001719 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1720 ++p;
1721 }
1722
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001723 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001724 // character, complete the command name.
1725 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1726 return NULL;
1727
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001728 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001729 {
1730 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1731 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 p = cmd + 1;
1734 }
1735 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1736 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 eap->cmd = cmd;
1738 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 }
1742 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001744 {
1745 // Not still touching the command and it was an illegal one
1746 xp->xp_context = EXPAND_UNSUCCESSFUL;
1747 return NULL;
1748 }
1749
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001750 return p;
1751}
Bram Moolenaard0190392019-08-23 21:17:35 +02001752
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001753/*
1754 * Set the completion context for a command argument with wild card characters.
1755 */
1756 static void
1757set_context_for_wildcard_arg(
1758 exarg_T *eap,
1759 char_u *arg,
1760 int usefilter,
1761 expand_T *xp,
1762 int *complp)
1763{
1764 char_u *p;
1765 int c;
1766 int in_quote = FALSE;
1767 char_u *bow = NULL; // Beginning of word
1768 int len = 0;
1769
1770 // Allow spaces within back-quotes to count as part of the argument
1771 // being expanded.
1772 xp->xp_pattern = skipwhite(arg);
1773 p = xp->xp_pattern;
1774 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001775 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001776 if (has_mbyte)
1777 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001778 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001779 c = *p;
1780 if (c == '\\' && p[1] != NUL)
1781 ++p;
1782 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001783 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001784 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001785 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001786 xp->xp_pattern = p;
1787 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001788 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001789 in_quote = !in_quote;
1790 }
1791 // An argument can contain just about everything, except
1792 // characters that end the command and white space.
1793 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001794#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001795 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001796#endif
1797 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001798 {
1799 len = 0; // avoid getting stuck when space is in 'isfname'
1800 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001801 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001802 if (has_mbyte)
1803 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001804 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001805 c = *p;
1806 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001807 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001808 if (has_mbyte)
1809 len = (*mb_ptr2len)(p);
1810 else
1811 len = 1;
1812 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001813 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001814 if (in_quote)
1815 bow = p;
1816 else
1817 xp->xp_pattern = p;
1818 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001819 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001820 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001821 }
1822
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001823 // If we are still inside the quotes, and we passed a space, just
1824 // expand from there.
1825 if (bow != NULL && in_quote)
1826 xp->xp_pattern = bow;
1827 xp->xp_context = EXPAND_FILES;
1828
1829 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001830 if (usefilter
1831 || (eap != NULL
1832 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1833 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001834 {
1835#ifndef BACKSLASH_IN_FILENAME
1836 xp->xp_shell = TRUE;
1837#endif
1838 // When still after the command name expand executables.
1839 if (xp->xp_pattern == skipwhite(arg))
1840 xp->xp_context = EXPAND_SHELLCMD;
1841 }
1842
1843 // Check for environment variable.
1844 if (*xp->xp_pattern == '$')
1845 {
1846 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1847 if (!vim_isIDc(*p))
1848 break;
1849 if (*p == NUL)
1850 {
1851 xp->xp_context = EXPAND_ENV_VARS;
1852 ++xp->xp_pattern;
1853 // Avoid that the assignment uses EXPAND_FILES again.
1854 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1855 *complp = EXPAND_ENV_VARS;
1856 }
1857 }
1858 // Check for user names.
1859 if (*xp->xp_pattern == '~')
1860 {
1861 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1862 ;
1863 // Complete ~user only if it partially matches a user name.
1864 // A full match ~user<Tab> will be replaced by user's home
1865 // directory i.e. something like ~user<Tab> -> /home/user/
1866 if (*p == NUL && p > xp->xp_pattern + 1
1867 && match_user(xp->xp_pattern + 1) >= 1)
1868 {
1869 xp->xp_context = EXPAND_USER;
1870 ++xp->xp_pattern;
1871 }
1872 }
1873}
1874
1875/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001876 * Set the completion context for the "++opt=arg" argument. Always returns
1877 * NULL.
1878 */
1879 static char_u *
1880set_context_in_argopt(expand_T *xp, char_u *arg)
1881{
1882 char_u *p;
1883
1884 p = vim_strchr(arg, '=');
1885 if (p == NULL)
1886 xp->xp_pattern = arg;
1887 else
1888 xp->xp_pattern = p + 1;
1889
1890 xp->xp_context = EXPAND_ARGOPT;
1891 return NULL;
1892}
1893
1894#ifdef FEAT_TERMINAL
1895/*
1896 * Set the completion context for :terminal's [options]. Always returns NULL.
1897 */
1898 static char_u *
1899set_context_in_terminalopt(expand_T *xp, char_u *arg)
1900{
1901 char_u *p;
1902
1903 p = vim_strchr(arg, '=');
1904 if (p == NULL)
1905 xp->xp_pattern = arg;
1906 else
1907 xp->xp_pattern = p + 1;
1908
1909 xp->xp_context = EXPAND_TERMINALOPT;
1910 return NULL;
1911}
1912#endif
1913
1914/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001915 * Set the completion context for the :filter command. Returns a pointer to the
1916 * next command after the :filter command.
1917 */
1918 static char_u *
1919set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1920{
1921 if (*arg != NUL)
1922 arg = skip_vimgrep_pat(arg, NULL, NULL);
1923 if (arg == NULL || *arg == NUL)
1924 {
1925 xp->xp_context = EXPAND_NOTHING;
1926 return NULL;
1927 }
1928 return skipwhite(arg);
1929}
1930
1931#ifdef FEAT_SEARCH_EXTRA
1932/*
1933 * Set the completion context for the :match command. Returns a pointer to the
1934 * next command after the :match command.
1935 */
1936 static char_u *
1937set_context_in_match_cmd(expand_T *xp, char_u *arg)
1938{
1939 if (*arg == NUL || !ends_excmd(*arg))
1940 {
1941 // also complete "None"
1942 set_context_in_echohl_cmd(xp, arg);
1943 arg = skipwhite(skiptowhite(arg));
1944 if (*arg != NUL)
1945 {
1946 xp->xp_context = EXPAND_NOTHING;
1947 arg = skip_regexp(arg + 1, *arg, magic_isset());
1948 }
1949 }
1950 return find_nextcmd(arg);
1951}
1952#endif
1953
1954/*
1955 * Returns a pointer to the next command after a :global or a :v command.
1956 * Returns NULL if there is no next command.
1957 */
1958 static char_u *
1959find_cmd_after_global_cmd(char_u *arg)
1960{
1961 int delim;
1962
1963 delim = *arg; // get the delimiter
1964 if (delim)
1965 ++arg; // skip delimiter if there is one
1966
1967 while (arg[0] != NUL && arg[0] != delim)
1968 {
1969 if (arg[0] == '\\' && arg[1] != NUL)
1970 ++arg;
1971 ++arg;
1972 }
1973 if (arg[0] != NUL)
1974 return arg + 1;
1975
1976 return NULL;
1977}
1978
1979/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001980 * Returns a pointer to the next command after a :substitute or a :& command.
1981 * Returns NULL if there is no next command.
1982 */
1983 static char_u *
1984find_cmd_after_substitute_cmd(char_u *arg)
1985{
1986 int delim;
1987
1988 delim = *arg;
1989 if (delim)
1990 {
1991 // skip "from" part
1992 ++arg;
1993 arg = skip_regexp(arg, delim, magic_isset());
1994
1995 if (arg[0] != NUL && arg[0] == delim)
1996 {
1997 // skip "to" part
1998 ++arg;
1999 while (arg[0] != NUL && arg[0] != delim)
2000 {
2001 if (arg[0] == '\\' && arg[1] != NUL)
2002 ++arg;
2003 ++arg;
2004 }
2005 if (arg[0] != NUL) // skip delimiter
2006 ++arg;
2007 }
2008 }
2009 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
2010 ++arg;
2011 if (arg[0] != NUL)
2012 return arg;
2013
2014 return NULL;
2015}
2016
2017/*
2018 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
2019 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
2020 * Returns NULL if there is no next command.
2021 */
2022 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002023find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002024{
2025 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002026 if (*arg != '/')
2027 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002028
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002029 // Match regexp, not just whole words
2030 for (++arg; *arg && *arg != '/'; arg++)
2031 if (*arg == '\\' && arg[1] != NUL)
2032 arg++;
2033 if (*arg)
2034 {
2035 arg = skipwhite(arg + 1);
2036
2037 // Check for trailing illegal characters
2038 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
2039 xp->xp_context = EXPAND_NOTHING;
2040 else
2041 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002042 }
2043
2044 return NULL;
2045}
2046
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002047#ifdef FEAT_EVAL
2048/*
2049 * Set the completion context for the :unlet command. Always returns NULL.
2050 */
2051 static char_u *
2052set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2053{
2054 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2055 arg = xp->xp_pattern + 1;
2056
2057 xp->xp_context = EXPAND_USER_VARS;
2058 xp->xp_pattern = arg;
2059
2060 if (*xp->xp_pattern == '$')
2061 {
2062 xp->xp_context = EXPAND_ENV_VARS;
2063 ++xp->xp_pattern;
2064 }
2065
2066 return NULL;
2067}
2068#endif
2069
2070#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2071/*
2072 * Set the completion context for the :language command. Always returns NULL.
2073 */
2074 static char_u *
2075set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2076{
2077 char_u *p;
2078
2079 p = skiptowhite(arg);
2080 if (*p == NUL)
2081 {
2082 xp->xp_context = EXPAND_LANGUAGE;
2083 xp->xp_pattern = arg;
2084 }
2085 else
2086 {
2087 if ( STRNCMP(arg, "messages", p - arg) == 0
2088 || STRNCMP(arg, "ctype", p - arg) == 0
2089 || STRNCMP(arg, "time", p - arg) == 0
2090 || STRNCMP(arg, "collate", p - arg) == 0)
2091 {
2092 xp->xp_context = EXPAND_LOCALES;
2093 xp->xp_pattern = skipwhite(p);
2094 }
2095 else
2096 xp->xp_context = EXPAND_NOTHING;
2097 }
2098
2099 return NULL;
2100}
2101#endif
2102
Christian Brabandta3422aa2025-04-23 21:04:24 +02002103static enum
2104{
2105 EXP_FILETYPECMD_ALL, // expand all :filetype values
2106 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2107 EXP_FILETYPECMD_INDENT, // expand indent on off
2108 EXP_FILETYPECMD_ONOFF, // expand on off
2109} filetype_expand_what;
2110
2111#define EXPAND_FILETYPECMD_PLUGIN 0x01
2112#define EXPAND_FILETYPECMD_INDENT 0x02
2113#define EXPAND_FILETYPECMD_ONOFF 0x04
2114
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002115#ifdef FEAT_EVAL
2116static enum
2117{
2118 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002119 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2120 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002121} breakpt_expand_what;
2122
2123/*
2124 * Set the completion context for the :breakadd command. Always returns NULL.
2125 */
2126 static char_u *
2127set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2128{
2129 char_u *p;
2130 char_u *subcmd_start;
2131
2132 xp->xp_context = EXPAND_BREAKPOINT;
2133 xp->xp_pattern = arg;
2134
2135 if (cmdidx == CMD_breakadd)
2136 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002137 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002138 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002139 else
2140 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002141
2142 p = skipwhite(arg);
2143 if (*p == NUL)
2144 return NULL;
2145 subcmd_start = p;
2146
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002147 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002148 {
2149 // :breakadd file [lnum] <filename>
2150 // :breakadd func [lnum] <funcname>
2151 p += 4;
2152 p = skipwhite(p);
2153
2154 // skip line number (if specified)
2155 if (VIM_ISDIGIT(*p))
2156 {
2157 p = skipdigits(p);
2158 if (*p != ' ')
2159 {
2160 xp->xp_context = EXPAND_NOTHING;
2161 return NULL;
2162 }
2163 p = skipwhite(p);
2164 }
2165 if (STRNCMP("file", subcmd_start, 4) == 0)
2166 xp->xp_context = EXPAND_FILES;
2167 else
2168 xp->xp_context = EXPAND_USER_FUNC;
2169 xp->xp_pattern = p;
2170 }
2171 else if (STRNCMP("expr ", p, 5) == 0)
2172 {
2173 // :breakadd expr <expression>
2174 xp->xp_context = EXPAND_EXPRESSION;
2175 xp->xp_pattern = skipwhite(p + 5);
2176 }
2177
2178 return NULL;
2179}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002180
2181 static char_u *
2182set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2183{
2184 char_u *p;
2185
2186 xp->xp_context = EXPAND_NOTHING;
2187 xp->xp_pattern = NULL;
2188
2189 p = skipwhite(arg);
2190 if (VIM_ISDIGIT(*p))
2191 return NULL;
2192
2193 xp->xp_context = EXPAND_SCRIPTNAMES;
2194 xp->xp_pattern = p;
2195
2196 return NULL;
2197}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002198#endif
2199
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002200/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002201 * Set the completion context for the :filetype command. Always returns NULL.
2202 */
2203 static char_u *
2204set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2205{
2206 char_u *p;
2207 int val = 0;
2208
2209 xp->xp_context = EXPAND_FILETYPECMD;
2210 xp->xp_pattern = arg;
2211 filetype_expand_what = EXP_FILETYPECMD_ALL;
2212
2213 p = skipwhite(arg);
2214 if (*p == NUL)
2215 return NULL;
2216
2217 for (;;)
2218 {
2219 if (STRNCMP(p, "plugin", 6) == 0)
2220 {
2221 val |= EXPAND_FILETYPECMD_PLUGIN;
2222 p = skipwhite(p + 6);
2223 continue;
2224 }
2225 if (STRNCMP(p, "indent", 6) == 0)
2226 {
2227 val |= EXPAND_FILETYPECMD_INDENT;
2228 p = skipwhite(p + 6);
2229 continue;
2230 }
2231 break;
2232 }
2233
2234 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2235 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2236 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2237 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2238 else if ((val & EXPAND_FILETYPECMD_INDENT))
2239 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2240
2241 xp->xp_pattern = p;
2242
2243 return NULL;
2244}
2245
Girish Palya6b49fba2025-06-28 19:47:34 +02002246/*
2247 * Sets the completion context for commands that involve a search pattern
2248 * and a line range (e.g., :s, :g, :v).
2249 */
2250 static void
2251set_context_with_pattern(expand_T *xp)
2252{
2253 int skiplen = 0;
2254 cmdline_info_T *ccline = get_cmdline_info();
2255#ifdef FEAT_SEARCH_EXTRA
2256 int dummy, patlen, retval;
2257
2258 ++emsg_off;
2259 retval = parse_pattern_and_range(&pre_incsearch_pos, &dummy, &skiplen,
2260 &patlen);
2261 --emsg_off;
2262
2263 // Check if cursor is within search pattern
2264 if (!retval || ccline->cmdpos <= skiplen
2265 || ccline->cmdpos > skiplen + patlen)
2266 return;
2267#endif
2268
2269 xp->xp_pattern = ccline->cmdbuff + skiplen;
2270 xp->xp_pattern_len = ccline->cmdpos - skiplen;
2271 xp->xp_context = EXPAND_PATTERN_IN_BUF;
2272 xp->xp_search_dir = FORWARD;
2273}
Christian Brabandta3422aa2025-04-23 21:04:24 +02002274
2275/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002276 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2277 * The argument to the command is 'arg' and the argument flags is 'argt'.
2278 * For user-defined commands and for environment variables, 'compl' has the
2279 * completion type.
2280 * Returns a pointer to the next command. Returns NULL if there is no next
2281 * command.
2282 */
2283 static char_u *
2284set_context_by_cmdname(
2285 char_u *cmd,
2286 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002287 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002288 char_u *arg,
2289 long argt,
2290 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002291 int forceit)
2292{
Girish Palya6b49fba2025-06-28 19:47:34 +02002293 char_u *nextcmd;
2294
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002295 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002296 {
2297 case CMD_find:
2298 case CMD_sfind:
2299 case CMD_tabfind:
2300 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002301 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002302 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002303 break;
2304 case CMD_cd:
2305 case CMD_chdir:
2306 case CMD_tcd:
2307 case CMD_tchdir:
2308 case CMD_lcd:
2309 case CMD_lchdir:
2310 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002311 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002312 break;
2313 case CMD_help:
2314 xp->xp_context = EXPAND_HELP;
2315 xp->xp_pattern = arg;
2316 break;
2317
2318 // Command modifiers: return the argument.
2319 // Also for commands with an argument that is a command.
2320 case CMD_aboveleft:
2321 case CMD_argdo:
2322 case CMD_belowright:
2323 case CMD_botright:
2324 case CMD_browse:
2325 case CMD_bufdo:
2326 case CMD_cdo:
2327 case CMD_cfdo:
2328 case CMD_confirm:
2329 case CMD_debug:
2330 case CMD_folddoclosed:
2331 case CMD_folddoopen:
2332 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002333 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002334 case CMD_keepalt:
2335 case CMD_keepjumps:
2336 case CMD_keepmarks:
2337 case CMD_keeppatterns:
2338 case CMD_ldo:
2339 case CMD_leftabove:
2340 case CMD_lfdo:
2341 case CMD_lockmarks:
2342 case CMD_noautocmd:
2343 case CMD_noswapfile:
2344 case CMD_rightbelow:
2345 case CMD_sandbox:
2346 case CMD_silent:
2347 case CMD_tab:
2348 case CMD_tabdo:
2349 case CMD_topleft:
2350 case CMD_verbose:
2351 case CMD_vertical:
2352 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002353 case CMD_vim9cmd:
2354 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002355 return arg;
2356
2357 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002358 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002359
2360#ifdef FEAT_SEARCH_EXTRA
2361 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002362 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002363#endif
2364
2365 // All completion for the +cmdline_compl feature goes here.
2366
2367 case CMD_command:
2368 return set_context_in_user_cmd(xp, arg);
2369
2370 case CMD_delcommand:
2371 xp->xp_context = EXPAND_USER_COMMANDS;
2372 xp->xp_pattern = arg;
2373 break;
2374
2375 case CMD_global:
2376 case CMD_vglobal:
Girish Palya6b49fba2025-06-28 19:47:34 +02002377 nextcmd = find_cmd_after_global_cmd(arg);
2378 if (!nextcmd && may_expand_pattern)
2379 set_context_with_pattern(xp);
2380 return nextcmd;
2381
Bram Moolenaard0190392019-08-23 21:17:35 +02002382 case CMD_and:
2383 case CMD_substitute:
Girish Palya6b49fba2025-06-28 19:47:34 +02002384 nextcmd = find_cmd_after_substitute_cmd(arg);
2385 if (!nextcmd && may_expand_pattern)
2386 set_context_with_pattern(xp);
2387 return nextcmd;
2388
Bram Moolenaard0190392019-08-23 21:17:35 +02002389 case CMD_isearch:
2390 case CMD_dsearch:
2391 case CMD_ilist:
2392 case CMD_dlist:
2393 case CMD_ijump:
2394 case CMD_psearch:
2395 case CMD_djump:
2396 case CMD_isplit:
2397 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002398 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002399 case CMD_autocmd:
2400 return set_context_in_autocmd(xp, arg, FALSE);
2401 case CMD_doautocmd:
2402 case CMD_doautoall:
2403 return set_context_in_autocmd(xp, arg, TRUE);
2404 case CMD_set:
2405 set_context_in_set_cmd(xp, arg, 0);
2406 break;
2407 case CMD_setglobal:
2408 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2409 break;
2410 case CMD_setlocal:
2411 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2412 break;
2413 case CMD_tag:
2414 case CMD_stag:
2415 case CMD_ptag:
2416 case CMD_ltag:
2417 case CMD_tselect:
2418 case CMD_stselect:
2419 case CMD_ptselect:
2420 case CMD_tjump:
2421 case CMD_stjump:
2422 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002423 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002424 xp->xp_context = EXPAND_TAGS_LISTFILES;
2425 else
2426 xp->xp_context = EXPAND_TAGS;
2427 xp->xp_pattern = arg;
2428 break;
2429 case CMD_augroup:
2430 xp->xp_context = EXPAND_AUGROUP;
2431 xp->xp_pattern = arg;
2432 break;
2433#ifdef FEAT_SYN_HL
2434 case CMD_syntax:
2435 set_context_in_syntax_cmd(xp, arg);
2436 break;
2437#endif
2438#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002439 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002440 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002441 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002442 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002443 case CMD_if:
2444 case CMD_elseif:
2445 case CMD_while:
2446 case CMD_for:
2447 case CMD_echo:
2448 case CMD_echon:
2449 case CMD_execute:
2450 case CMD_echomsg:
2451 case CMD_echoerr:
2452 case CMD_call:
2453 case CMD_return:
2454 case CMD_cexpr:
2455 case CMD_caddexpr:
2456 case CMD_cgetexpr:
2457 case CMD_lexpr:
2458 case CMD_laddexpr:
2459 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002460 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002461 break;
2462
2463 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002464 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002465 case CMD_function:
2466 case CMD_delfunction:
2467 xp->xp_context = EXPAND_USER_FUNC;
2468 xp->xp_pattern = arg;
2469 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002470 case CMD_disassemble:
2471 set_context_in_disassemble_cmd(xp, arg);
2472 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002473
2474 case CMD_echohl:
2475 set_context_in_echohl_cmd(xp, arg);
2476 break;
2477#endif
2478 case CMD_highlight:
2479 set_context_in_highlight_cmd(xp, arg);
2480 break;
2481#ifdef FEAT_CSCOPE
2482 case CMD_cscope:
2483 case CMD_lcscope:
2484 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002485 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002486 break;
2487#endif
2488#ifdef FEAT_SIGNS
2489 case CMD_sign:
2490 set_context_in_sign_cmd(xp, arg);
2491 break;
2492#endif
2493 case CMD_bdelete:
2494 case CMD_bwipeout:
2495 case CMD_bunload:
2496 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2497 arg = xp->xp_pattern + 1;
2498 // FALLTHROUGH
2499 case CMD_buffer:
2500 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002501 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002502 case CMD_checktime:
2503 xp->xp_context = EXPAND_BUFFERS;
2504 xp->xp_pattern = arg;
2505 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002506#ifdef FEAT_DIFF
2507 case CMD_diffget:
2508 case CMD_diffput:
2509 // If current buffer is in diff mode, complete buffer names
2510 // which are in diff mode, and different than current buffer.
2511 xp->xp_context = EXPAND_DIFF_BUFFERS;
2512 xp->xp_pattern = arg;
2513 break;
2514#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002515 case CMD_USER:
2516 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002517 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2518 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002519
2520 case CMD_map: case CMD_noremap:
2521 case CMD_nmap: case CMD_nnoremap:
2522 case CMD_vmap: case CMD_vnoremap:
2523 case CMD_omap: case CMD_onoremap:
2524 case CMD_imap: case CMD_inoremap:
2525 case CMD_cmap: case CMD_cnoremap:
2526 case CMD_lmap: case CMD_lnoremap:
2527 case CMD_smap: case CMD_snoremap:
2528 case CMD_tmap: case CMD_tnoremap:
2529 case CMD_xmap: case CMD_xnoremap:
2530 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002531 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002532 case CMD_unmap:
2533 case CMD_nunmap:
2534 case CMD_vunmap:
2535 case CMD_ounmap:
2536 case CMD_iunmap:
2537 case CMD_cunmap:
2538 case CMD_lunmap:
2539 case CMD_sunmap:
2540 case CMD_tunmap:
2541 case CMD_xunmap:
2542 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002543 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002544 case CMD_mapclear:
2545 case CMD_nmapclear:
2546 case CMD_vmapclear:
2547 case CMD_omapclear:
2548 case CMD_imapclear:
2549 case CMD_cmapclear:
2550 case CMD_lmapclear:
2551 case CMD_smapclear:
2552 case CMD_tmapclear:
2553 case CMD_xmapclear:
2554 xp->xp_context = EXPAND_MAPCLEAR;
2555 xp->xp_pattern = arg;
2556 break;
2557
2558 case CMD_abbreviate: case CMD_noreabbrev:
2559 case CMD_cabbrev: case CMD_cnoreabbrev:
2560 case CMD_iabbrev: case CMD_inoreabbrev:
2561 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002562 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002563 case CMD_unabbreviate:
2564 case CMD_cunabbrev:
2565 case CMD_iunabbrev:
2566 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002567 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002568#ifdef FEAT_MENU
2569 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2570 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2571 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2572 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2573 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2574 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2575 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2576 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2577 case CMD_tmenu: case CMD_tunmenu:
2578 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2579 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2580#endif
2581
2582 case CMD_colorscheme:
2583 xp->xp_context = EXPAND_COLORS;
2584 xp->xp_pattern = arg;
2585 break;
2586
2587 case CMD_compiler:
2588 xp->xp_context = EXPAND_COMPILER;
2589 xp->xp_pattern = arg;
2590 break;
2591
2592 case CMD_ownsyntax:
2593 xp->xp_context = EXPAND_OWNSYNTAX;
2594 xp->xp_pattern = arg;
2595 break;
2596
2597 case CMD_setfiletype:
2598 xp->xp_context = EXPAND_FILETYPE;
2599 xp->xp_pattern = arg;
2600 break;
2601
2602 case CMD_packadd:
2603 xp->xp_context = EXPAND_PACKADD;
2604 xp->xp_pattern = arg;
2605 break;
2606
zeertzjqb0d45ec2023-01-25 15:04:22 +00002607 case CMD_runtime:
2608 set_context_in_runtime_cmd(xp, arg);
2609 break;
2610
Bram Moolenaard0190392019-08-23 21:17:35 +02002611#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2612 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002613 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002614#endif
2615#if defined(FEAT_PROFILE)
2616 case CMD_profile:
2617 set_context_in_profile_cmd(xp, arg);
2618 break;
2619#endif
2620 case CMD_behave:
2621 xp->xp_context = EXPAND_BEHAVE;
2622 xp->xp_pattern = arg;
2623 break;
2624
2625 case CMD_messages:
2626 xp->xp_context = EXPAND_MESSAGES;
2627 xp->xp_pattern = arg;
2628 break;
2629
2630 case CMD_history:
2631 xp->xp_context = EXPAND_HISTORY;
2632 xp->xp_pattern = arg;
2633 break;
2634#if defined(FEAT_PROFILE)
2635 case CMD_syntime:
2636 xp->xp_context = EXPAND_SYNTIME;
2637 xp->xp_pattern = arg;
2638 break;
2639#endif
2640
2641 case CMD_argdelete:
2642 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2643 arg = xp->xp_pattern + 1;
2644 xp->xp_context = EXPAND_ARGLIST;
2645 xp->xp_pattern = arg;
2646 break;
2647
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002648#ifdef FEAT_EVAL
2649 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002650 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002651 case CMD_breakdel:
2652 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002653
2654 case CMD_scriptnames:
2655 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002656#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002657 case CMD_filetype:
2658 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002659
Bram Moolenaard0190392019-08-23 21:17:35 +02002660 default:
2661 break;
2662 }
2663 return NULL;
2664}
2665
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002666/*
2667 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2668 * we don't need/want deleted. Maybe this could be done better if we didn't
2669 * repeat all this stuff. The only problem is that they may not stay
2670 * perfectly compatible with each other, but then the command line syntax
2671 * probably won't change that much -- webb.
2672 */
2673 static char_u *
2674set_one_cmd_context(
2675 expand_T *xp,
2676 char_u *buff) // buffer for command string
2677{
2678 char_u *p;
2679 char_u *cmd, *arg;
2680 int len = 0;
2681 exarg_T ea;
2682 int compl = EXPAND_NOTHING;
2683 int forceit = FALSE;
2684 int usefilter = FALSE; // filter instead of file name
2685
2686 ExpandInit(xp);
2687 xp->xp_pattern = buff;
2688 xp->xp_line = buff;
2689 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2690 ea.argt = 0;
2691
2692 // 1. skip comment lines and leading space, colons or bars
2693 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2694 ;
2695 xp->xp_pattern = cmd;
2696
2697 if (*cmd == NUL)
2698 return NULL;
2699 if (*cmd == '"') // ignore comment lines
2700 {
2701 xp->xp_context = EXPAND_NOTHING;
2702 return NULL;
2703 }
2704
2705 // 3. Skip over the range to find the command.
2706 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2707 xp->xp_pattern = cmd;
2708 if (*cmd == NUL)
2709 return NULL;
2710 if (*cmd == '"')
2711 {
2712 xp->xp_context = EXPAND_NOTHING;
2713 return NULL;
2714 }
2715
2716 if (*cmd == '|' || *cmd == '\n')
2717 return cmd + 1; // There's another command
2718
2719 // Get the command index.
2720 p = set_cmd_index(cmd, &ea, xp, &compl);
2721 if (p == NULL)
2722 return NULL;
2723
2724 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2725
2726 if (*p == '!') // forced commands
2727 {
2728 forceit = TRUE;
2729 ++p;
2730 }
2731
2732 // 6. parse arguments
2733 if (!IS_USER_CMDIDX(ea.cmdidx))
2734 ea.argt = excmd_get_argt(ea.cmdidx);
2735
2736 arg = skipwhite(p);
2737
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002738 // Does command allow "++argopt" argument?
2739 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002740 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002741 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2742 {
2743 p = arg + 2;
2744 while (*p && !vim_isspace(*p))
2745 MB_PTR_ADV(p);
2746
2747 // Still touching the command after "++"?
2748 if (*p == NUL)
2749 {
2750 if (ea.argt & EX_ARGOPT)
2751 return set_context_in_argopt(xp, arg + 2);
2752#ifdef FEAT_TERMINAL
2753 if (ea.cmdidx == CMD_terminal)
2754 return set_context_in_terminalopt(xp, arg + 2);
2755#endif
2756 }
2757
2758 arg = skipwhite(p);
2759 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002760 }
2761
2762 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2763 {
2764 if (*arg == '>') // append
2765 {
2766 if (*++arg == '>')
2767 ++arg;
2768 arg = skipwhite(arg);
2769 }
2770 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2771 {
2772 ++arg;
2773 usefilter = TRUE;
2774 }
2775 }
2776
2777 if (ea.cmdidx == CMD_read)
2778 {
2779 usefilter = forceit; // :r! filter if forced
2780 if (*arg == '!') // :r !filter
2781 {
2782 ++arg;
2783 usefilter = TRUE;
2784 }
2785 }
2786
2787 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2788 {
2789 while (*arg == *cmd) // allow any number of '>' or '<'
2790 ++arg;
2791 arg = skipwhite(arg);
2792 }
2793
2794 // Does command allow "+command"?
2795 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2796 {
2797 // Check if we're in the +command
2798 p = arg + 1;
2799 arg = skip_cmd_arg(arg, FALSE);
2800
2801 // Still touching the command after '+'?
2802 if (*arg == NUL)
2803 return p;
2804
2805 // Skip space(s) after +command to get to the real argument
2806 arg = skipwhite(arg);
2807 }
2808
2809
2810 // Check for '|' to separate commands and '"' to start comments.
2811 // Don't do this for ":read !cmd" and ":write !cmd".
2812 if ((ea.argt & EX_TRLBAR) && !usefilter)
2813 {
2814 p = arg;
2815 // ":redir @" is not the start of a comment
2816 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2817 p += 2;
2818 while (*p)
2819 {
2820 if (*p == Ctrl_V)
2821 {
2822 if (p[1] != NUL)
2823 ++p;
2824 }
2825 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2826 || *p == '|' || *p == '\n')
2827 {
2828 if (*(p - 1) != '\\')
2829 {
2830 if (*p == '|' || *p == '\n')
2831 return p + 1;
2832 return NULL; // It's a comment
2833 }
2834 }
2835 MB_PTR_ADV(p);
2836 }
2837 }
2838
2839 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2840 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2841 // no arguments allowed but there is something
2842 return NULL;
2843
2844 // Find start of last argument (argument just before cursor):
2845 p = buff;
2846 xp->xp_pattern = p;
2847 len = (int)STRLEN(buff);
2848 while (*p && p < buff + len)
2849 {
2850 if (*p == ' ' || *p == TAB)
2851 {
2852 // argument starts after a space
2853 xp->xp_pattern = ++p;
2854 }
2855 else
2856 {
2857 if (*p == '\\' && *(p + 1) != NUL)
2858 ++p; // skip over escaped character
2859 MB_PTR_ADV(p);
2860 }
2861 }
2862
2863 if (ea.argt & EX_XFILE)
2864 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2865
2866 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002867 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002868 forceit);
2869}
2870
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002871/*
2872 * Set the completion context in 'xp' for command 'str'
2873 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002874 void
2875set_cmd_context(
2876 expand_T *xp,
2877 char_u *str, // start of command line
2878 int len, // length of command line (excl. NUL)
2879 int col, // position of cursor
2880 int use_ccline UNUSED) // use ccline for info
2881{
2882#ifdef FEAT_EVAL
2883 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002884 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002885#endif
2886 int old_char = NUL;
2887 char_u *nextcomm;
2888
2889 // Avoid a UMR warning from Purify, only save the character if it has been
2890 // written before.
2891 if (col < len)
2892 old_char = str[col];
2893 str[col] = NUL;
2894 nextcomm = str;
2895
2896#ifdef FEAT_EVAL
2897 if (use_ccline && ccline->cmdfirstc == '=')
2898 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002899 // pass CMD_SIZE because there is no real command
2900 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002901 }
2902 else if (use_ccline && ccline->input_fn)
2903 {
2904 xp->xp_context = ccline->xp_context;
2905 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002906 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002907 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2908 {
2909 context = xp->xp_context;
2910 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2911 &context);
2912 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002913 }
2914 else
2915#endif
2916 while (nextcomm != NULL)
2917 nextcomm = set_one_cmd_context(xp, nextcomm);
2918
2919 // Store the string here so that call_user_expand_func() can get to them
2920 // easily.
2921 xp->xp_line = str;
2922 xp->xp_col = col;
2923
2924 str[col] = old_char;
2925}
2926
2927/*
2928 * Expand the command line "str" from context "xp".
2929 * "xp" must have been set by set_cmd_context().
2930 * xp->xp_pattern points into "str", to where the text that is to be expanded
2931 * starts.
2932 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2933 * cursor.
2934 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2935 * key that triggered expansion literally.
2936 * Returns EXPAND_OK otherwise.
2937 */
2938 int
2939expand_cmdline(
2940 expand_T *xp,
2941 char_u *str, // start of command line
2942 int col, // position of cursor
2943 int *matchcount, // return: nr of matches
2944 char_u ***matches) // return: array of pointers to matches
2945{
2946 char_u *file_str = NULL;
2947 int options = WILD_ADD_SLASH|WILD_SILENT;
2948
2949 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2950 {
2951 beep_flush();
2952 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2953 }
2954 if (xp->xp_context == EXPAND_NOTHING)
2955 {
2956 // Caller can use the character as a normal char instead
2957 return EXPAND_NOTHING;
2958 }
2959
2960 // add star to file name, or convert to regexp if not exp. files.
2961 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002962 if (cmdline_fuzzy_completion_supported(xp))
2963 // If fuzzy matching, don't modify the search string
2964 file_str = vim_strsave(xp->xp_pattern);
2965 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002966 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
John Marriott1be5b372025-06-23 19:57:29 +02002967 if (file_str == NULL)
2968 return EXPAND_UNSUCCESSFUL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002969
2970 if (p_wic)
2971 options += WILD_ICASE;
2972
2973 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002974 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002975 {
2976 *matchcount = 0;
2977 *matches = NULL;
2978 }
2979 vim_free(file_str);
2980
2981 return EXPAND_OK;
2982}
2983
Bram Moolenaar66b51422019-08-18 21:44:12 +02002984/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002986 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002987 */
2988 static int
2989expand_files_and_dirs(
2990 expand_T *xp,
2991 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 char_u ***matches,
2993 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002994 int flags,
2995 int options)
2996{
2997 int free_pat = FALSE;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002998 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002999
3000 // for ":set path=" and ":set tags=" halve backslashes for escaped
3001 // space
3002 if (xp->xp_backslash != XP_BS_NONE)
3003 {
John Marriotta494ce12025-07-03 21:28:50 +02003004 size_t pat_len;
3005 char_u *pat_end;
3006 char_u *p;
3007
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003008 free_pat = TRUE;
John Marriotta494ce12025-07-03 21:28:50 +02003009
3010 pat_len = STRLEN(pat);
3011 pat = vim_strnsave(pat, pat_len);
John Marriott3b03b432025-06-28 20:41:54 +02003012 if (pat == NULL)
3013 return ret;
3014
John Marriotta494ce12025-07-03 21:28:50 +02003015 pat_end = pat + pat_len;
3016 for (p = pat; *p != NUL; ++p)
3017 {
3018 char_u *from;
3019
3020 if (*p != '\\')
3021 continue;
3022
3023 if (xp->xp_backslash & XP_BS_THREE
3024 && *(p + 1) == '\\'
3025 && *(p + 2) == '\\'
3026 && *(p + 3) == ' ')
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003027 {
John Marriotta494ce12025-07-03 21:28:50 +02003028 from = p + 3;
3029 mch_memmove(p, from,
3030 (size_t)(pat_end - from) + 1); // +1 for NUL
3031 pat_end -= 3;
3032 }
3033 else if (xp->xp_backslash & XP_BS_ONE
3034 && *(p + 1) == ' ')
3035 {
3036 from = p + 1;
3037 mch_memmove(p, from,
3038 (size_t)(pat_end - from) + 1); // +1 for NUL
3039 --pat_end;
3040 }
3041 else if (xp->xp_backslash & XP_BS_COMMA)
3042 {
3043 if (*(p + 1) == '\\' && *(p + 2) == ',')
3044 {
3045 from = p + 2;
3046 mch_memmove(p, from,
3047 (size_t)(pat_end - from) + 1); // +1 for NUL
3048 pat_end -= 2;
3049 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003050#ifdef BACKSLASH_IN_FILENAME
John Marriotta494ce12025-07-03 21:28:50 +02003051 else if (*(p + 1) == ',')
3052 {
3053 from = p + 1;
3054 mch_memmove(p, from,
3055 (size_t)(pat_end - from) + 1); // +1 for NUL
3056 --pat_end;
3057 }
Yee Cheng Chin54844852023-10-09 18:12:31 +02003058#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003059 }
John Marriotta494ce12025-07-03 21:28:50 +02003060 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003061 }
3062
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003063 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003064 {
3065#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003066 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003067#endif
3068 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003069 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003070 {
3071 if (xp->xp_context == EXPAND_FILES)
3072 flags |= EW_FILE;
3073 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
3074 flags |= (EW_FILE | EW_PATH);
3075 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
3076 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
3077 else
3078 flags = (flags | EW_DIR) & ~EW_FILE;
3079 if (options & WILD_ICASE)
3080 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003081
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02003082 // Expand wildcards, supporting %:h and the like.
3083 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
3084 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003085 if (free_pat)
3086 vim_free(pat);
3087#ifdef BACKSLASH_IN_FILENAME
3088 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
3089 {
John Marriotta494ce12025-07-03 21:28:50 +02003090 int j;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003091
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003092 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003093 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003094 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003095
3096 while (*ptr != NUL)
3097 {
3098 if (p_csl[0] == 's' && *ptr == '\\')
3099 *ptr = '/';
3100 else if (p_csl[0] == 'b' && *ptr == '/')
3101 *ptr = '\\';
3102 ptr += (*mb_ptr2len)(ptr);
3103 }
3104 }
3105 }
3106#endif
3107 return ret;
3108}
3109
3110/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003111 * Function given to ExpandGeneric() to obtain the possible arguments of the
3112 * ":behave {mswin,xterm}" command.
3113 */
3114 static char_u *
3115get_behave_arg(expand_T *xp UNUSED, int idx)
3116{
3117 if (idx == 0)
3118 return (char_u *)"mswin";
3119 if (idx == 1)
3120 return (char_u *)"xterm";
3121 return NULL;
3122}
3123
Christian Brabandta3422aa2025-04-23 21:04:24 +02003124/*
3125 * Function given to ExpandGeneric() to obtain the possible arguments of the
3126 * ":filetype {plugin,indent}" command.
3127 */
3128 static char_u *
3129get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3130{
John Marriotta494ce12025-07-03 21:28:50 +02003131 if (idx < 0)
3132 return NULL;
Christian Brabandta3422aa2025-04-23 21:04:24 +02003133
3134 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
John Marriotta494ce12025-07-03 21:28:50 +02003135 {
3136 char *opts_all[] = {"indent", "plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003137 return (char_u *)opts_all[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003138 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003139 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003140 {
3141 char *opts_plugin[] = {"plugin", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003142 return (char_u *)opts_plugin[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003143 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003144 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
John Marriotta494ce12025-07-03 21:28:50 +02003145 {
3146 char *opts_indent[] = {"indent", "on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003147 return (char_u *)opts_indent[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003148 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003149 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
John Marriotta494ce12025-07-03 21:28:50 +02003150 {
3151 char *opts_onoff[] = {"on", "off"};
Christian Brabandta3422aa2025-04-23 21:04:24 +02003152 return (char_u *)opts_onoff[idx];
John Marriotta494ce12025-07-03 21:28:50 +02003153 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02003154 return NULL;
3155}
3156
K.Takata161b6ac2022-11-14 15:31:07 +00003157#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003158/*
3159 * Function given to ExpandGeneric() to obtain the possible arguments of the
3160 * ":breakadd {expr, file, func, here}" command.
3161 * ":breakdel {func, file, here}" command.
3162 */
3163 static char_u *
3164get_breakadd_arg(expand_T *xp UNUSED, int idx)
3165{
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003166 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003167 {
John Marriotta494ce12025-07-03 21:28:50 +02003168 char *opts[] = {"expr", "file", "func", "here"};
3169
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003170 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003171 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3172 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003173 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3174 {
3175 // breakdel {func, file, here}
3176 if (idx <= 2)
3177 return (char_u *)opts[idx + 1];
3178 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003179 else
3180 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003181 // profdel {func, file}
3182 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003183 return (char_u *)opts[idx + 1];
3184 }
3185 }
3186 return NULL;
3187}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003188
3189/*
3190 * Function given to ExpandGeneric() to obtain the possible arguments for the
3191 * ":scriptnames" command.
3192 */
3193 static char_u *
3194get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3195{
3196 scriptitem_T *si;
3197
3198 if (!SCRIPT_ID_VALID(idx + 1))
3199 return NULL;
3200
3201 si = SCRIPT_ITEM(idx + 1);
3202 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3203 return NameBuff;
3204}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003205#endif
3206
Bram Moolenaard0190392019-08-23 21:17:35 +02003207/*
3208 * Function given to ExpandGeneric() to obtain the possible arguments of the
3209 * ":messages {clear}" command.
3210 */
3211 static char_u *
3212get_messages_arg(expand_T *xp UNUSED, int idx)
3213{
3214 if (idx == 0)
3215 return (char_u *)"clear";
3216 return NULL;
3217}
3218
3219 static char_u *
3220get_mapclear_arg(expand_T *xp UNUSED, int idx)
3221{
3222 if (idx == 0)
3223 return (char_u *)"<buffer>";
3224 return NULL;
3225}
3226
3227/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003228 * Do the expansion based on xp->xp_context and 'rmp'.
3229 */
3230 static int
3231ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003232 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003233 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003234 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003235 char_u ***matches,
3236 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003237{
3238 static struct expgen
3239 {
3240 int context;
3241 char_u *((*func)(expand_T *, int));
3242 int ic;
3243 int escaped;
3244 } tab[] =
3245 {
3246 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3247 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003248 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003249 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3250 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3251 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3252 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3253 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3254 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3255 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3256 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003257#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003258 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3259 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3260 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3261 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3262 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003263#endif
3264#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003265 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3266 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003267#endif
3268#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003269 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003270#endif
3271#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003272 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003273#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003274 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3275 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3276 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003277#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003278 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003279#endif
3280#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003281 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003282#endif
3283#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003284 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003285#endif
3286#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003287 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3288 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003289#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003290 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3291 {EXPAND_USER, get_users, TRUE, FALSE},
3292 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003293#ifdef FEAT_EVAL
3294 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003295 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003296#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003297 };
3298 int i;
3299 int ret = FAIL;
3300
3301 // Find a context in the table and call the ExpandGeneric() with the
3302 // right function to do the expansion.
3303 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3304 {
3305 if (xp->xp_context == tab[i].context)
3306 {
3307 if (tab[i].ic)
3308 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003309 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3310 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003311 break;
3312 }
3313 }
3314
3315 return ret;
3316}
3317
3318/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003319 * Map wild expand options to flags for expand_wildcards()
3320 */
3321 static int
3322map_wildopts_to_ewflags(int options)
3323{
3324 int flags;
3325
3326 flags = EW_DIR; // include directories
3327 if (options & WILD_LIST_NOTFOUND)
3328 flags |= EW_NOTFOUND;
3329 if (options & WILD_ADD_SLASH)
3330 flags |= EW_ADDSLASH;
3331 if (options & WILD_KEEP_ALL)
3332 flags |= EW_KEEPALL;
3333 if (options & WILD_SILENT)
3334 flags |= EW_SILENT;
3335 if (options & WILD_NOERROR)
3336 flags |= EW_NOERROR;
3337 if (options & WILD_ALLLINKS)
3338 flags |= EW_ALLLINKS;
3339
3340 return flags;
3341}
3342
3343/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344 * Do the expansion based on xp->xp_context and "pat".
3345 */
3346 static int
3347ExpandFromContext(
3348 expand_T *xp,
3349 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003350 char_u ***matches,
3351 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352 int options) // WILD_ flags
3353{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355 int ret;
3356 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003357 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003358 int fuzzy = cmdline_fuzzy_complete(pat)
3359 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003361 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003362
3363 if (xp->xp_context == EXPAND_FILES
3364 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003365 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003366 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003367 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003368 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3369 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003371 *matches = (char_u **)"";
3372 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003373 if (xp->xp_context == EXPAND_HELP)
3374 {
3375 // With an empty argument we would get all the help tags, which is
3376 // very slow. Get matches for "help" instead.
3377 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003378 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379 {
3380#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003381 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003382#endif
3383 return OK;
3384 }
3385 return FAIL;
3386 }
3387
Bram Moolenaar66b51422019-08-18 21:44:12 +02003388 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003389 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003391 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003393 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003394#ifdef FEAT_DIFF
3395 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003396 return ExpandBufnames(pat, numMatches, matches,
3397 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003398#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003399 if (xp->xp_context == EXPAND_TAGS
3400 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003401 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3402 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003403 if (xp->xp_context == EXPAND_COLORS)
3404 {
3405 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003406 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003407 directories);
3408 }
3409 if (xp->xp_context == EXPAND_COMPILER)
3410 {
3411 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003412 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003413 }
3414 if (xp->xp_context == EXPAND_OWNSYNTAX)
3415 {
3416 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003417 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003418 }
3419 if (xp->xp_context == EXPAND_FILETYPE)
3420 {
3421 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003422 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423 }
Doug Kearns81642d92024-01-04 22:37:44 +01003424#ifdef FEAT_KEYMAP
3425 if (xp->xp_context == EXPAND_KEYMAP)
3426 {
3427 char *directories[] = {"keymap", NULL};
3428 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3429 }
3430#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003431#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003432 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003433 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003434#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003435 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003436 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003437 if (xp->xp_context == EXPAND_RUNTIME)
3438 return expand_runtime_cmd(pat, numMatches, matches);
Girish Palya6b49fba2025-06-28 19:47:34 +02003439 if (xp->xp_context == EXPAND_PATTERN_IN_BUF)
3440 return expand_pattern_in_buf(pat, xp->xp_search_dir,
3441 matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003443 // When expanding a function name starting with s:, match the <SNR>nr_
3444 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003445 if ((xp->xp_context == EXPAND_USER_FUNC
3446 || xp->xp_context == EXPAND_DISASSEMBLE)
3447 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003448 {
3449 int len = (int)STRLEN(pat) + 20;
3450
3451 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003452 if (tofree == NULL)
3453 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003454 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003455 pat = tofree;
3456 }
3457
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003458 if (!fuzzy)
3459 {
3460 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3461 if (regmatch.regprog == NULL)
3462 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003463
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003464 // set ignore-case according to p_ic, p_scs and pat
3465 regmatch.rm_ic = ignorecase(pat);
3466 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003467
3468 if (xp->xp_context == EXPAND_SETTINGS
3469 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003470 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003471 else if (xp->xp_context == EXPAND_STRING_SETTING)
3472 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3473 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3474 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003475 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003476 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003477 else if (xp->xp_context == EXPAND_ARGOPT)
3478 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003479 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3480 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003481#if defined(FEAT_TERMINAL)
3482 else if (xp->xp_context == EXPAND_TERMINALOPT)
3483 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3484#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003485#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003487 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003488#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003489 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003490 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003492 if (!fuzzy)
3493 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003494 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495
3496 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497}
3498
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003499 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003501 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003502 expand_T *xp,
3503 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003504 char_u ***matches,
3505 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003506 char_u *((*func)(expand_T *, int)),
3507 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003508 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003509{
Yee Cheng China7b81202025-02-23 09:32:47 +01003510 return ExpandGenericExt(
3511 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3512}
3513
3514/*
3515 * Expand a list of names.
3516 *
3517 * Generic function for command line completion. It calls a function to
3518 * obtain strings, one by one. The strings are matched against a regexp
3519 * program. Matching strings are copied into an array, which is returned.
3520 *
3521 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3522 * is used.
3523 *
3524 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3525 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3526 * sorting.
3527 *
3528 * Returns OK when no problems encountered, FAIL for error (out of memory).
3529 */
3530 int
3531ExpandGenericExt(
3532 char_u *pat,
3533 expand_T *xp,
3534 regmatch_T *regmatch,
3535 char_u ***matches,
3536 int *numMatches,
3537 char_u *((*func)(expand_T *, int)),
3538 // returns a string from the list
3539 int escaped,
3540 int sortStartIdx)
3541{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003542 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003543 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003544 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003545 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003546 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003547 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003548 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003549 int sort_matches = FALSE;
3550 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003551 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003552
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003553 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003554 *matches = NULL;
3555 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003556
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003557 if (!fuzzy)
3558 ga_init2(&ga, sizeof(char *), 30);
3559 else
3560 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3561
3562 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003564 str = (*func)(xp, i);
3565 if (str == NULL) // end of list
3566 break;
3567 if (*str == NUL) // skip empty strings
3568 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003569
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003570 if (xp->xp_pattern[0] != NUL)
3571 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003572 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003573 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003574 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003575 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003576 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003577 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578 }
3579 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003580 else
3581 match = TRUE;
3582
3583 if (!match)
3584 continue;
3585
3586 if (escaped)
3587 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3588 else
3589 str = vim_strsave(str);
3590 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003592 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003593 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003594 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003595 return FAIL;
3596 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003597 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003598 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003599 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003600
3601 if (ga_grow(&ga, 1) == FAIL)
3602 {
3603 vim_free(str);
3604 break;
3605 }
3606
3607 if (fuzzy)
3608 {
3609 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3610 fuzmatch->idx = ga.ga_len;
3611 fuzmatch->str = str;
3612 fuzmatch->score = score;
3613 }
3614 else
3615 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3616
K.Takata161b6ac2022-11-14 15:31:07 +00003617#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003618 if (func == get_menu_names)
3619 {
3620 // test for separator added by get_menu_names()
3621 str += STRLEN(str) - 1;
3622 if (*str == '\001')
3623 *str = '.';
3624 }
K.Takata161b6ac2022-11-14 15:31:07 +00003625#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003626
Yee Cheng China7b81202025-02-23 09:32:47 +01003627 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3628 {
3629 // Found first item to start sorting from. This is usually 0.
3630 sortStartMatchIdx = ga.ga_len;
3631 }
3632
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003633 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003634 }
3635
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003636 if (ga.ga_len == 0)
3637 return OK;
3638
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003639 // sort the matches when using regular expression matching and sorting
3640 // applies to the completion context. Menus and scriptnames should be kept
3641 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003642 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003643 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003644 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003645 && xp->xp_context != EXPAND_SCRIPTNAMES
3646 && xp->xp_context != EXPAND_ARGOPT
3647 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003648 sort_matches = TRUE;
3649
3650 // <SNR> functions should be sorted to the end.
3651 if (xp->xp_context == EXPAND_EXPRESSION
3652 || xp->xp_context == EXPAND_FUNCTIONS
3653 || xp->xp_context == EXPAND_USER_FUNC
3654 || xp->xp_context == EXPAND_DISASSEMBLE)
3655 funcsort = TRUE;
3656
3657 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003658 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003660 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003661 // <SNR> functions should be sorted to the end.
3662 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3663 sort_func_compare);
3664 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003665 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003666 }
3667
3668 if (!fuzzy)
3669 {
3670 *matches = ga.ga_data;
3671 *numMatches = ga.ga_len;
3672 }
3673 else
3674 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003675 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3676 funcsort) == FAIL)
3677 return FAIL;
3678 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003679 }
3680
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003681#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 // Reset the variables used for special highlight names expansion, so that
3683 // they don't show up when getting normal highlight names by ID.
3684 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003685#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003686
Bram Moolenaar66b51422019-08-18 21:44:12 +02003687 return OK;
3688}
3689
3690/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003691 * Expand shell command matches in one directory of $PATH.
3692 */
3693 static void
3694expand_shellcmd_onedir(
3695 char_u *buf,
3696 char_u *s,
3697 size_t l,
3698 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003699 char_u ***matches,
3700 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003701 int flags,
3702 hashtab_T *ht,
3703 garray_T *gap)
3704{
3705 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003706 hash_T hash;
3707 hashitem_T *hi;
3708
3709 vim_strncpy(buf, s, l);
3710 add_pathsep(buf);
3711 l = STRLEN(buf);
3712 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3713
3714 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003715 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003716 if (ret != OK)
3717 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003718
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003719 if (ga_grow(gap, *numMatches) == FAIL)
3720 {
3721 FreeWild(*numMatches, *matches);
3722 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003723 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003724
3725 for (int i = 0; i < *numMatches; ++i)
3726 {
3727 char_u *name = (*matches)[i];
3728
3729 if (STRLEN(name) > l)
3730 {
3731 // Check if this name was already found.
3732 hash = hash_hash(name + l);
3733 hi = hash_lookup(ht, name + l, hash);
3734 if (HASHITEM_EMPTY(hi))
3735 {
3736 // Remove the path that was prepended.
3737 STRMOVE(name, name + l);
3738 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3739 hash_add_item(ht, hi, name, hash);
3740 name = NULL;
3741 }
3742 }
3743 vim_free(name);
3744 }
3745 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003746}
3747
3748/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749 * Complete a shell command.
3750 * Returns FAIL or OK;
3751 */
3752 static int
3753expand_shellcmd(
3754 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003755 char_u ***matches, // return: array with matches
3756 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003757 int flagsarg) // EW_ flags
3758{
3759 char_u *pat;
3760 int i;
3761 char_u *path = NULL;
3762 int mustfree = FALSE;
3763 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003764 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003765 size_t l;
3766 char_u *s, *e;
3767 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003768 int did_curdir = FALSE;
3769 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003770
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003771 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003772 if (buf == NULL)
3773 return FAIL;
3774
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003775 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003776 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003777 if (pat == NULL)
3778 {
3779 vim_free(buf);
3780 return FAIL;
3781 }
3782
Bram Moolenaar66b51422019-08-18 21:44:12 +02003783 for (i = 0; pat[i]; ++i)
3784 if (pat[i] == '\\' && pat[i + 1] == ' ')
3785 STRMOVE(pat + i, pat + i + 1);
3786
3787 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3788
3789 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3790 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3791 path = (char_u *)".";
3792 else
3793 {
3794 // For an absolute name we don't use $PATH.
3795 if (!mch_isFullName(pat))
3796 path = vim_getenv((char_u *)"PATH", &mustfree);
3797 if (path == NULL)
3798 path = (char_u *)"";
3799 }
3800
3801 // Go over all directories in $PATH. Expand matches in that directory and
3802 // collect them in "ga". When "." is not in $PATH also expand for the
3803 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003804 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 hash_init(&found_ht);
3806 for (s = path; ; s = e)
3807 {
K.Takata161b6ac2022-11-14 15:31:07 +00003808#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003809 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003810#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003812#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003813 if (e == NULL)
3814 e = s + STRLEN(s);
3815
3816 if (*s == NUL)
3817 {
3818 if (did_curdir)
3819 break;
3820 // Find directories in the current directory, path is empty.
3821 did_curdir = TRUE;
3822 flags |= EW_DIR;
3823 }
3824 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3825 {
3826 did_curdir = TRUE;
3827 flags |= EW_DIR;
3828 }
3829 else
3830 // Do not match directories inside a $PATH item.
3831 flags &= ~EW_DIR;
3832
3833 l = e - s;
3834 if (l > MAXPATHL - 5)
3835 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003836
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003837 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003838 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003839
Bram Moolenaar66b51422019-08-18 21:44:12 +02003840 if (*e != NUL)
3841 ++e;
3842 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003843 *matches = ga.ga_data;
3844 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003845
3846 vim_free(buf);
3847 vim_free(pat);
3848 if (mustfree)
3849 vim_free(path);
3850 hash_clear(&found_ht);
3851 return OK;
3852}
3853
K.Takata161b6ac2022-11-14 15:31:07 +00003854#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003855/*
3856 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003857 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003858 */
3859 static void *
3860call_user_expand_func(
3861 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003862 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003863{
3864 cmdline_info_T *ccline = get_cmdline_info();
3865 int keep = 0;
3866 typval_T args[4];
3867 sctx_T save_current_sctx = current_sctx;
3868 char_u *pat = NULL;
3869 void *ret;
3870
3871 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3872 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003873
3874 if (ccline->cmdbuff != NULL)
3875 {
3876 keep = ccline->cmdbuff[ccline->cmdlen];
3877 ccline->cmdbuff[ccline->cmdlen] = 0;
3878 }
3879
3880 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3881
3882 args[0].v_type = VAR_STRING;
3883 args[0].vval.v_string = pat;
3884 args[1].v_type = VAR_STRING;
3885 args[1].vval.v_string = xp->xp_line;
3886 args[2].v_type = VAR_NUMBER;
3887 args[2].vval.v_number = xp->xp_col;
3888 args[3].v_type = VAR_UNKNOWN;
3889
3890 current_sctx = xp->xp_script_ctx;
3891
3892 ret = user_expand_func(xp->xp_arg, 3, args);
3893
3894 current_sctx = save_current_sctx;
3895 if (ccline->cmdbuff != NULL)
3896 ccline->cmdbuff[ccline->cmdlen] = keep;
3897
3898 vim_free(pat);
3899 return ret;
3900}
3901
3902/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003903 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3904 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003905 */
3906 static int
3907ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003908 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003909 expand_T *xp,
3910 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003911 char_u ***matches,
3912 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003913{
3914 char_u *retstr;
3915 char_u *s;
3916 char_u *e;
3917 int keep;
3918 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003919 int fuzzy;
3920 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003921 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003922
3923 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003924 *matches = NULL;
3925 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003926
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003927 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003928 if (retstr == NULL)
3929 return FAIL;
3930
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003931 if (!fuzzy)
3932 ga_init2(&ga, sizeof(char *), 3);
3933 else
3934 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3935
Bram Moolenaar66b51422019-08-18 21:44:12 +02003936 for (s = retstr; *s != NUL; s = e)
3937 {
3938 e = vim_strchr(s, '\n');
3939 if (e == NULL)
3940 e = s + STRLEN(s);
3941 keep = *e;
3942 *e = NUL;
3943
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003944 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003945 {
3946 if (!fuzzy)
3947 match = vim_regexec(regmatch, s, (colnr_T)0);
3948 else
3949 {
3950 score = fuzzy_match_str(s, pat);
3951 match = (score != 0);
3952 }
3953 }
3954 else
3955 match = TRUE; // match everything
3956
Bram Moolenaar66b51422019-08-18 21:44:12 +02003957 *e = keep;
3958
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003959 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003960 {
John Marriott3b03b432025-06-28 20:41:54 +02003961 char_u *p = vim_strnsave(s, (size_t)(e - s));
3962 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003963 break;
John Marriott3b03b432025-06-28 20:41:54 +02003964
3965 if (ga_grow(&ga, 1) == FAIL)
3966 {
3967 vim_free(p);
3968 break;
3969 }
3970
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003971 if (!fuzzy)
John Marriott3b03b432025-06-28 20:41:54 +02003972 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003973 else
3974 {
3975 fuzmatch_str_T *fuzmatch =
3976 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003977 fuzmatch->idx = ga.ga_len;
John Marriott3b03b432025-06-28 20:41:54 +02003978 fuzmatch->str = p;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003979 fuzmatch->score = score;
3980 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003981 ++ga.ga_len;
3982 }
3983
3984 if (*e != NUL)
3985 ++e;
3986 }
3987 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003988
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003989 if (ga.ga_len == 0)
3990 return OK;
3991
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003992 if (!fuzzy)
3993 {
3994 *matches = ga.ga_data;
3995 *numMatches = ga.ga_len;
3996 }
3997 else
3998 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003999 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
4000 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00004001 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00004002 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00004003 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004004 return OK;
4005}
4006
4007/*
4008 * Expand names with a list returned by a function defined by the user.
4009 */
4010 static int
4011ExpandUserList(
4012 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004013 char_u ***matches,
4014 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004015{
4016 list_T *retlist;
4017 listitem_T *li;
4018 garray_T ga;
4019
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004020 *matches = NULL;
4021 *numMatches = 0;
4022 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004023 if (retlist == NULL)
4024 return FAIL;
4025
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004026 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004027 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004028 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004029 {
John Marriott3b03b432025-06-28 20:41:54 +02004030 char_u *p;
4031
Bram Moolenaar66b51422019-08-18 21:44:12 +02004032 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4033 continue; // Skip non-string items and empty strings
4034
John Marriott3b03b432025-06-28 20:41:54 +02004035 p = vim_strsave(li->li_tv.vval.v_string);
4036 if (p == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004037 break;
4038
John Marriott3b03b432025-06-28 20:41:54 +02004039 if (ga_grow(&ga, 1) == FAIL)
4040 {
4041 vim_free(p);
4042 break;
4043 }
4044
4045 ((char_u **)ga.ga_data)[ga.ga_len] = p;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004046 ++ga.ga_len;
4047 }
4048 list_unref(retlist);
4049
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004050 *matches = ga.ga_data;
4051 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004052 return OK;
4053}
K.Takata161b6ac2022-11-14 15:31:07 +00004054#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02004055
4056/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02004057 * Expand "file" for all comma-separated directories in "path".
4058 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00004059 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02004060 */
4061 void
4062globpath(
4063 char_u *path,
4064 char_u *file,
4065 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00004066 int expand_options,
4067 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004068{
4069 expand_T xpc;
4070 char_u *buf;
John Marriotta494ce12025-07-03 21:28:50 +02004071 size_t buflen;
4072 size_t filelen;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004073
4074 buf = alloc(MAXPATHL);
4075 if (buf == NULL)
4076 return;
4077
4078 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00004079 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004080
John Marriotta494ce12025-07-03 21:28:50 +02004081 filelen = STRLEN(file);
4082
Bram Moolenaar66b51422019-08-18 21:44:12 +02004083 // Loop over all entries in {path}.
4084 while (*path != NUL)
4085 {
4086 // Copy one item of the path to buf[] and concatenate the file name.
John Marriotta494ce12025-07-03 21:28:50 +02004087 buflen = (size_t)copy_option_part(&path, buf, MAXPATHL, ",");
4088 if (buflen + filelen + 2 < MAXPATHL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004089 {
John Marriotta494ce12025-07-03 21:28:50 +02004090 int num_p;
4091 char_u **p;
4092
4093 if (*buf != NUL && !after_pathsep(buf, buf + buflen))
4094 {
K.Takata161b6ac2022-11-14 15:31:07 +00004095#if defined(MSWIN)
John Marriotta494ce12025-07-03 21:28:50 +02004096 // Using the platform's path separator (\) makes vim incorrectly
4097 // treat it as an escape character, use '/' instead.
4098 STRCPY(buf + buflen, "/");
4099 ++buflen;
K.Takata161b6ac2022-11-14 15:31:07 +00004100#else
John Marriotta494ce12025-07-03 21:28:50 +02004101 STRCPY(buf + buflen, PATHSEPSTR);
4102 buflen += STRLEN_LITERAL(PATHSEPSTR);
K.Takata161b6ac2022-11-14 15:31:07 +00004103#endif
John Marriotta494ce12025-07-03 21:28:50 +02004104 }
4105 STRCPY(buf + buflen, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00004106 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02004107 WILD_SILENT|expand_options) != FAIL && num_p > 0)
4108 {
4109 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
4110
4111 if (ga_grow(ga, num_p) == OK)
John Marriotta494ce12025-07-03 21:28:50 +02004112 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004113 // take over the pointers and put them in "ga"
John Marriotta494ce12025-07-03 21:28:50 +02004114 for (int i = 0; i < num_p; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004115 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004116 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02004117 ++ga->ga_len;
4118 }
John Marriotta494ce12025-07-03 21:28:50 +02004119 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01004120 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004121 }
4122 }
4123 }
4124
4125 vim_free(buf);
4126}
Bram Moolenaar66b51422019-08-18 21:44:12 +02004127
Bram Moolenaareadee482020-09-04 15:37:31 +02004128/*
4129 * Translate some keys pressed when 'wildmenu' is used.
4130 */
4131 int
4132wildmenu_translate_key(
4133 cmdline_info_T *cclp,
4134 int key,
4135 expand_T *xp,
4136 int did_wild_list)
4137{
4138 int c = key;
4139
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004140 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004141 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00004142 // When the popup menu is used for cmdline completion:
4143 // Up : go to the previous item in the menu
4144 // Down : go to the next item in the menu
4145 // Left : go to the parent directory
4146 // Right: list the files in the selected directory
4147 switch (c)
4148 {
4149 case K_UP: c = K_LEFT; break;
4150 case K_DOWN: c = K_RIGHT; break;
4151 case K_LEFT: c = K_UP; break;
4152 case K_RIGHT: c = K_DOWN; break;
4153 default: break;
4154 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004155 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004156
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004157 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004158 {
4159 if (c == K_LEFT)
4160 c = Ctrl_P;
4161 else if (c == K_RIGHT)
4162 c = Ctrl_N;
4163 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004164
Bram Moolenaareadee482020-09-04 15:37:31 +02004165 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004166 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004167 && cclp->cmdpos > 1
4168 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4169 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4170 && (c == '\n' || c == '\r' || c == K_KENTER))
4171 c = K_DOWN;
4172
4173 return c;
4174}
4175
4176/*
4177 * Delete characters on the command line, from "from" to the current
4178 * position.
4179 */
4180 static void
4181cmdline_del(cmdline_info_T *cclp, int from)
4182{
4183 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4184 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4185 cclp->cmdlen -= cclp->cmdpos - from;
4186 cclp->cmdpos = from;
4187}
4188
4189/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004190 * Handle a key pressed when the wild menu for the menu names
4191 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004192 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004193 static int
4194wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004195{
Bram Moolenaareadee482020-09-04 15:37:31 +02004196 int i;
4197 int j;
4198
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004199 // Hitting <Down> after "emenu Name.": complete submenu
4200 if (key == K_DOWN && cclp->cmdpos > 0
4201 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004202 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004203 key = p_wc;
4204 KeyTyped = TRUE; // in case the key was mapped
4205 }
4206 else if (key == K_UP)
4207 {
4208 // Hitting <Up>: Remove one submenu name in front of the
4209 // cursor
4210 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004211
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004212 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4213 i = 0;
4214 while (--j > 0)
4215 {
4216 // check for start of menu name
4217 if (cclp->cmdbuff[j] == ' '
4218 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004219 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004220 i = j + 1;
4221 break;
4222 }
4223 // check for start of submenu name
4224 if (cclp->cmdbuff[j] == '.'
4225 && cclp->cmdbuff[j - 1] != '\\')
4226 {
4227 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004228 {
4229 i = j + 1;
4230 break;
4231 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004232 else
4233 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004234 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004235 }
4236 if (i > 0)
4237 cmdline_del(cclp, i);
4238 key = p_wc;
4239 KeyTyped = TRUE; // in case the key was mapped
4240 xp->xp_context = EXPAND_NOTHING;
4241 }
4242
4243 return key;
4244}
4245
4246/*
4247 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4248 * directory names (EXPAND_DIRECTORIES) or shell command names
4249 * (EXPAND_SHELLCMD) is displayed.
4250 */
4251 static int
4252wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4253{
4254 int i;
4255 int j;
4256 char_u upseg[5];
4257
4258 upseg[0] = PATHSEP;
4259 upseg[1] = '.';
4260 upseg[2] = '.';
4261 upseg[3] = PATHSEP;
4262 upseg[4] = NUL;
4263
4264 if (key == K_DOWN
4265 && cclp->cmdpos > 0
4266 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4267 && (cclp->cmdpos < 3
4268 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4269 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4270 {
4271 // go down a directory
4272 key = p_wc;
4273 KeyTyped = TRUE; // in case the key was mapped
4274 }
4275 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4276 {
4277 // If in a direct ancestor, strip off one ../ to go down
4278 int found = FALSE;
4279
4280 j = cclp->cmdpos;
4281 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4282 while (--j > i)
4283 {
4284 if (has_mbyte)
4285 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4286 if (vim_ispathsep(cclp->cmdbuff[j]))
4287 {
4288 found = TRUE;
4289 break;
4290 }
4291 }
4292 if (found
4293 && cclp->cmdbuff[j - 1] == '.'
4294 && cclp->cmdbuff[j - 2] == '.'
4295 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4296 {
4297 cmdline_del(cclp, j - 2);
4298 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004299 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004300 }
4301 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004302 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004303 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004304 // go up a directory
4305 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004306
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004307 j = cclp->cmdpos - 1;
4308 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4309 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004310 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004311 if (has_mbyte)
4312 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4313 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004314#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004315 && vim_strchr((char_u *)" *?[{`$%#",
4316 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004317#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004318 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004319 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004320 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004321 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004322 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004323 break;
4324 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004325 else
4326 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004327 }
4328 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004329
4330 if (!found)
4331 j = i;
4332 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4333 j += 4;
4334 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4335 && j == i)
4336 j += 3;
4337 else
4338 j = 0;
4339 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004340 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004341 // TODO this is only for DOS/UNIX systems - need to put in
4342 // machine-specific stuff here and in upseg init
4343 cmdline_del(cclp, j);
4344 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004345 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004346 else if (cclp->cmdpos > i)
4347 cmdline_del(cclp, i);
4348
4349 // Now complete in the new directory. Set KeyTyped in case the
4350 // Up key came from a mapping.
4351 key = p_wc;
4352 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004353 }
4354
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004355 return key;
4356}
4357
4358/*
4359 * Handle a key pressed when the wild menu is displayed
4360 */
4361 int
4362wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4363{
4364 if (xp->xp_context == EXPAND_MENUNAMES)
4365 return wildmenu_process_key_menunames(cclp, key, xp);
4366 else if ((xp->xp_context == EXPAND_FILES
4367 || xp->xp_context == EXPAND_DIRECTORIES
4368 || xp->xp_context == EXPAND_SHELLCMD))
4369 return wildmenu_process_key_filenames(cclp, key, xp);
4370
4371 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004372}
4373
4374/*
4375 * Free expanded names when finished walking through the matches
4376 */
4377 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004378wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004379{
4380 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004381
4382 if (!p_wmnu || wild_menu_showing == 0)
4383 return;
4384
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004385#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004386 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004387 if (cclp->input_fn)
4388 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004389#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004390
Girish Palya6b49fba2025-06-28 19:47:34 +02004391#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
4392 // Clear highlighting applied during wildmenu activity
4393 set_no_hlsearch(TRUE);
4394#endif
4395
Bram Moolenaareadee482020-09-04 15:37:31 +02004396 if (wild_menu_showing == WM_SCROLLED)
4397 {
4398 // Entered command line, move it up
4399 cmdline_row--;
4400 redrawcmd();
4401 }
4402 else if (save_p_ls != -1)
4403 {
4404 // restore 'laststatus' and 'winminheight'
4405 p_ls = save_p_ls;
4406 p_wmh = save_p_wmh;
4407 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004408 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004409 redrawcmd();
4410 save_p_ls = -1;
4411 }
4412 else
4413 {
4414 win_redraw_last_status(topframe);
4415 redraw_statuslines();
4416 }
4417 KeyTyped = skt;
4418 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004419#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004420 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004421 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004422#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004423}
Bram Moolenaareadee482020-09-04 15:37:31 +02004424
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004425#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004426/*
4427 * "getcompletion()" function
4428 */
4429 void
4430f_getcompletion(typval_T *argvars, typval_T *rettv)
4431{
4432 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004433 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004434 expand_T xpc;
4435 int filtered = FALSE;
4436 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004437 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004438
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004439 if (in_vim9script()
4440 && (check_for_string_arg(argvars, 0) == FAIL
4441 || check_for_string_arg(argvars, 1) == FAIL
4442 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4443 return;
4444
ii144785fe02021-11-21 12:13:56 +00004445 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004446 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004447 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004448 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004449
4450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004451 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004452
4453 if (p_wic)
4454 options |= WILD_ICASE;
4455
4456 // For filtered results, 'wildignore' is used
4457 if (!filtered)
4458 options |= WILD_KEEP_ALL;
4459
4460 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004461 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004462 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004463 int cmdline_len = (int)STRLEN(pat);
4464 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004465 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004466 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004467 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004468 else
4469 {
ii144785fe02021-11-21 12:13:56 +00004470 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004471 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004472 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004473
4474 xpc.xp_context = cmdcomplete_str_to_type(type);
4475 if (xpc.xp_context == EXPAND_NOTHING)
4476 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004477 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004478 return;
4479 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004480
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004481 if (xpc.xp_context == EXPAND_USER_DEFINED)
4482 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004483 // Must be "custom,funcname" pattern
4484 if (STRNCMP(type, "custom,", 7) != 0)
4485 {
4486 semsg(_(e_invalid_argument_str), type);
4487 return;
4488 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004489
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004490 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004491 }
4492
4493 if (xpc.xp_context == EXPAND_USER_LIST)
4494 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004495 // Must be "customlist,funcname" pattern
4496 if (STRNCMP(type, "customlist,", 11) != 0)
4497 {
4498 semsg(_(e_invalid_argument_str), type);
4499 return;
4500 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004501
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004502 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004503 }
4504
Bram Moolenaar66b51422019-08-18 21:44:12 +02004505# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004506 if (xpc.xp_context == EXPAND_MENUS)
4507 {
4508 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4509 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4510 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004511# endif
4512# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004513 if (xpc.xp_context == EXPAND_CSCOPE)
4514 {
4515 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4516 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4517 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004518# endif
4519# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004520 if (xpc.xp_context == EXPAND_SIGN)
4521 {
4522 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4523 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4524 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004525# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004526 if (xpc.xp_context == EXPAND_RUNTIME)
4527 {
4528 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4529 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4530 }
zeertzjq85f36d62024-10-10 19:14:13 +02004531 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4532 {
4533 int context = EXPAND_SHELLCMDLINE;
4534 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4535 &context);
4536 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4537 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004538 if (xpc.xp_context == EXPAND_FILETYPECMD)
4539 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004540 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004541
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004542 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004543 // when fuzzy matching, don't modify the search string
4544 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004545 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004546 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004547
Bram Moolenaar93a10962022-06-16 11:42:09 +01004548 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004549 {
4550 int i;
4551
4552 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4553
4554 for (i = 0; i < xpc.xp_numfiles; i++)
4555 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4556 }
4557 vim_free(pat);
4558 ExpandCleanup(&xpc);
4559}
Girish Palya92f68e22025-04-21 11:12:41 +02004560
4561/*
Hirohito Higashi96b3ef22025-07-05 15:31:23 +02004562 * "getcompletiontype()" function
4563 */
4564 void
4565f_getcompletiontype(typval_T *argvars, typval_T *rettv)
4566{
4567 char_u *pat;
4568 expand_T xpc;
4569 int cmdline_len;
4570
4571 rettv->v_type = VAR_STRING;
4572 rettv->vval.v_string = NULL;
4573
4574 if (check_for_string_arg(argvars, 0) == FAIL)
4575 return;
4576
4577 pat = tv_get_string(&argvars[0]);
4578 ExpandInit(&xpc);
4579
4580 cmdline_len = (int)STRLEN(pat);
4581 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
zeertzjqe2c0f812025-07-06 20:26:56 +02004582 rettv->vval.v_string = cmdcomplete_type_to_str(xpc.xp_context, xpc.xp_arg);
Hirohito Higashi96b3ef22025-07-05 15:31:23 +02004583
4584 ExpandCleanup(&xpc);
4585}
4586
4587/*
Girish Palya92f68e22025-04-21 11:12:41 +02004588 * "cmdcomplete_info()" function
4589 */
4590 void
4591f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4592{
4593 cmdline_info_T *ccline = get_cmdline_info();
4594 dict_T *retdict;
4595 list_T *li;
4596 int idx;
4597 int ret = OK;
4598
4599 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4600 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4601 return;
4602 retdict = rettv->vval.v_dict;
4603 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4604 if (ret == OK)
4605 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4606 if (ret == OK)
4607 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4608 if (ret == OK)
4609 {
4610 li = list_alloc();
4611 if (li == NULL)
4612 return;
4613 ret = dict_add_list(retdict, "matches", li);
4614 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4615 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4616 }
4617}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004618#endif // FEAT_EVAL
Girish Palya6b49fba2025-06-28 19:47:34 +02004619
4620/*
4621 * Copy a substring from the current buffer (curbuf), spanning from the given
4622 * 'start' position to the word boundary after 'end' position.
4623 * The copied string is stored in '*match', and the actual end position of the
4624 * matched text is returned in '*match_end'.
4625 */
4626 static int
4627copy_substring_from_pos(pos_T *start, pos_T *end, char_u **match,
4628 pos_T *match_end)
4629{
4630 char_u *word_end;
4631 char_u *line, *start_line, *end_line;
4632 int segment_len;
4633 linenr_T lnum;
4634 garray_T ga;
Girish Palya93c2d5b2025-07-08 21:29:02 +02004635 int exacttext = vim_strchr(p_wop, WOP_EXACTTEXT) != NULL;
Girish Palya6b49fba2025-06-28 19:47:34 +02004636
4637 if (start->lnum > end->lnum
4638 || (start->lnum == end->lnum && start->col >= end->col))
4639 return FAIL; // invalid range
4640
Girish Palya6b49fba2025-06-28 19:47:34 +02004641 // Use a growable string (ga)
4642 ga_init2(&ga, 1, 128);
4643
4644 // Append start line from start->col to end
zeertzjq5e34eec2025-07-05 15:37:17 +02004645 start_line = ml_get(start->lnum);
Girish Palya6b49fba2025-06-28 19:47:34 +02004646 char_u *start_ptr = start_line + start->col;
4647 int is_single_line = start->lnum == end->lnum;
4648
4649 segment_len = is_single_line ? (end->col - start->col)
glepnir7cf35bc2025-06-29 16:51:40 +02004650 : (int)STRLEN(start_ptr);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004651 if (ga_grow(&ga, segment_len + 2) != OK)
Girish Palya6b49fba2025-06-28 19:47:34 +02004652 return FAIL;
glepnir7cf35bc2025-06-29 16:51:40 +02004653
Girish Palya6b49fba2025-06-28 19:47:34 +02004654 ga_concat_len(&ga, start_ptr, segment_len);
4655 if (!is_single_line)
Girish Palya93c2d5b2025-07-08 21:29:02 +02004656 {
4657 if (exacttext)
4658 ga_concat_len(&ga, (char_u *)"\\n", 2);
4659 else
4660 ga_append(&ga, '\n');
4661 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004662
4663 // Append full lines between start and end
4664 if (!is_single_line)
glepnir7cf35bc2025-06-29 16:51:40 +02004665 {
Girish Palya6b49fba2025-06-28 19:47:34 +02004666 for (lnum = start->lnum + 1; lnum < end->lnum; lnum++)
4667 {
4668 line = ml_get(lnum);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004669 if (ga_grow(&ga, ml_get_len(lnum) + 2) != OK)
Girish Palya6b49fba2025-06-28 19:47:34 +02004670 return FAIL;
4671 ga_concat(&ga, line);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004672 if (exacttext)
4673 ga_concat_len(&ga, (char_u *)"\\n", 2);
4674 else
4675 ga_append(&ga, '\n');
Girish Palya6b49fba2025-06-28 19:47:34 +02004676 }
glepnir7cf35bc2025-06-29 16:51:40 +02004677 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004678
4679 // Append partial end line (up to word end)
zeertzjq5e34eec2025-07-05 15:37:17 +02004680 end_line = ml_get(end->lnum);
Girish Palya6b49fba2025-06-28 19:47:34 +02004681 word_end = find_word_end(end_line + end->col);
4682 segment_len = (int)(word_end - end_line);
4683 if (ga_grow(&ga, segment_len) != OK)
4684 return FAIL;
4685 ga_concat_len(&ga, end_line + (is_single_line ? end->col : 0),
4686 segment_len - (is_single_line ? end->col : 0));
4687
4688 // Null-terminate
4689 if (ga_grow(&ga, 1) != OK)
4690 return FAIL;
4691 ga_append(&ga, NUL);
4692
4693 *match = (char_u *)ga.ga_data;
4694 match_end->lnum = end->lnum;
4695 match_end->col = segment_len;
4696
4697 return OK;
4698}
4699
4700/*
Girish Palyaaf220072025-07-07 19:42:10 +02004701 * Returns TRUE if the given string `str` matches the regex pattern `pat`.
4702 * Honors the 'ignorecase' (p_ic) and 'smartcase' (p_scs) settings to determine
4703 * case sensitivity.
4704 */
4705 static int
4706is_regex_match(char_u *pat, char_u *str)
4707{
4708 regmatch_T regmatch;
4709 int result;
4710
4711 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4712 if (regmatch.regprog == NULL)
4713 return FALSE;
4714 regmatch.rm_ic = p_ic;
4715 if (p_ic && p_scs)
4716 regmatch.rm_ic = !pat_has_uppercase(pat);
4717
4718 result = vim_regexec_nl(&regmatch, str, (colnr_T)0);
4719
4720 vim_regfree(regmatch.regprog);
4721 return result;
4722}
4723
4724/*
4725 * Constructs a new match string by appending text from the buffer (starting at
4726 * end_match_pos) to the given pattern `pat`. The result is a concatenation of
4727 * `pat` and the word following end_match_pos.
4728 * If 'lowercase' is TRUE, the appended text is converted to lowercase before
4729 * being combined. Returns the newly allocated match string, or NULL on failure.
4730 */
4731 static char_u *
4732concat_pattern_with_buffer_match(
4733 char_u *pat,
4734 int pat_len,
4735 pos_T *end_match_pos,
4736 int lowercase UNUSED)
4737{
4738 char_u *line = ml_get(end_match_pos->lnum);
4739 char_u *word_end = find_word_end(line + end_match_pos->col);
4740 int match_len = (int)(word_end - (line + end_match_pos->col));
4741 char_u *match = alloc(match_len + pat_len + 1); // +1 for NUL
4742
4743 if (match == NULL)
4744 return NULL;
4745 mch_memmove(match, pat, pat_len);
4746 if (match_len > 0)
4747 {
4748#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
4749 if (lowercase)
4750 {
4751 char_u *mword = vim_strnsave(line + end_match_pos->col,
4752 match_len);
4753 if (mword == NULL)
4754 goto cleanup;
4755 char_u *lower = strlow_save(mword);
4756 vim_free(mword);
4757 if (lower == NULL)
4758 goto cleanup;
4759 mch_memmove(match + pat_len, lower, match_len);
4760 vim_free(lower);
4761 }
4762 else
4763#endif
4764 mch_memmove(match + pat_len, line + end_match_pos->col, match_len);
4765 }
4766 match[pat_len + match_len] = NUL;
4767 return match;
4768
4769#if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
4770cleanup:
4771 vim_free(match);
4772 return NULL;
4773#endif
4774}
4775
4776/*
Girish Palya6b49fba2025-06-28 19:47:34 +02004777 * Search for strings matching "pat" in the specified range and return them.
4778 * Returns OK on success, FAIL otherwise.
4779 */
4780 static int
4781expand_pattern_in_buf(
4782 char_u *pat, // pattern to match
4783 int dir, // direction: FORWARD or BACKWARD
4784 char_u ***matches, // return: array with matched strings
4785 int *numMatches) // return: number of matches
4786{
4787 pos_T cur_match_pos, prev_match_pos, end_match_pos, word_end_pos;
4788 garray_T ga;
4789 int found_new_match;
4790 int looped_around = FALSE;
Girish Palyaaf220072025-07-07 19:42:10 +02004791 int pat_len;
Girish Palya6b49fba2025-06-28 19:47:34 +02004792 int has_range = FALSE;
4793 int compl_started = FALSE;
glepnir7cf35bc2025-06-29 16:51:40 +02004794 int search_flags;
Girish Palyaaf220072025-07-07 19:42:10 +02004795 char_u *match, *full_match;
Girish Palya93c2d5b2025-07-08 21:29:02 +02004796 int exacttext = vim_strchr(p_wop, WOP_EXACTTEXT) != NULL;
Girish Palya6b49fba2025-06-28 19:47:34 +02004797
4798#ifdef FEAT_SEARCH_EXTRA
4799 has_range = search_first_line != 0;
4800#endif
4801
4802 *matches = NULL;
4803 *numMatches = 0;
4804
4805 if (pat == NULL || *pat == NUL)
4806 return FAIL;
4807
4808 pat_len = (int)STRLEN(pat);
4809 CLEAR_FIELD(cur_match_pos);
4810 CLEAR_FIELD(prev_match_pos);
4811#ifdef FEAT_SEARCH_EXTRA
4812 if (has_range)
4813 cur_match_pos.lnum = search_first_line;
4814 else
4815#endif
4816 cur_match_pos = pre_incsearch_pos;
4817
4818 search_flags = SEARCH_OPT | SEARCH_NOOF | SEARCH_PEEK | SEARCH_NFMSG
4819 | (has_range ? SEARCH_START : 0);
4820
Girish Palya6b49fba2025-06-28 19:47:34 +02004821 ga_init2(&ga, sizeof(char_u *), 10); // Use growable array of char_u*
4822
4823 for (;;)
4824 {
4825 ++emsg_off;
4826 ++msg_silent;
4827 found_new_match = searchit(NULL, curbuf, &cur_match_pos,
4828 &end_match_pos, dir, pat, pat_len, 1L,
4829 search_flags, RE_LAST, NULL);
4830 --msg_silent;
4831 --emsg_off;
4832
4833 if (found_new_match == FAIL)
4834 break;
4835
4836#ifdef FEAT_SEARCH_EXTRA
4837 // If in range mode, check if match is within the range
4838 if (has_range && (cur_match_pos.lnum < search_first_line
4839 || cur_match_pos.lnum > search_last_line))
4840 break;
4841#endif
4842
4843 if (compl_started)
4844 {
4845 // If we've looped back to an earlier match, stop
glepnir7cf35bc2025-06-29 16:51:40 +02004846 if ((dir == FORWARD && LTOREQ_POS(cur_match_pos, prev_match_pos)) ||
4847 (dir == BACKWARD && LTOREQ_POS(prev_match_pos, cur_match_pos)))
Girish Palya6b49fba2025-06-28 19:47:34 +02004848 {
4849 if (looped_around)
4850 break;
4851 else
4852 looped_around = TRUE;
4853 }
4854 }
4855
4856 compl_started = TRUE;
4857 prev_match_pos = cur_match_pos;
4858
4859 // Abort if user typed a character or interrupted
4860 if (char_avail() || got_int)
4861 {
4862 if (got_int)
4863 {
4864 (void)vpeekc(); // Remove <C-C> from input stream
4865 got_int = FALSE; // Don't abandon the command line
4866 }
4867 goto cleanup;
4868 }
4869
4870 // searchit() can return line number +1 past the last line when
4871 // searching for "foo\n" if "foo" is at end of buffer.
4872 if (end_match_pos.lnum > curbuf->b_ml.ml_line_count)
4873 {
4874 cur_match_pos.lnum = 1;
4875 cur_match_pos.col = 0;
4876 cur_match_pos.coladd = 0;
4877 continue;
4878 }
4879
4880 // Extract the matching text prepended to completed word
Girish Palyaaf220072025-07-07 19:42:10 +02004881 if (!copy_substring_from_pos(&cur_match_pos, &end_match_pos, &full_match,
Girish Palya6b49fba2025-06-28 19:47:34 +02004882 &word_end_pos))
4883 break;
4884
Girish Palya93c2d5b2025-07-08 21:29:02 +02004885 if (exacttext)
4886 match = full_match;
4887 else
Girish Palya6b49fba2025-06-28 19:47:34 +02004888 {
Girish Palya93c2d5b2025-07-08 21:29:02 +02004889 // Construct a new match from completed word appended to pattern itself
4890 match = concat_pattern_with_buffer_match(pat, pat_len, &end_match_pos,
4891 FALSE);
4892
4893 // The regex pattern may include '\C' or '\c'. First, try matching the
4894 // buffer word as-is. If it doesn't match, try again with the lowercase
4895 // version of the word to handle smartcase behavior.
Girish Palyaaf220072025-07-07 19:42:10 +02004896 if (match == NULL || !is_regex_match(match, full_match))
4897 {
4898 vim_free(match);
Girish Palya93c2d5b2025-07-08 21:29:02 +02004899 match = concat_pattern_with_buffer_match(pat, pat_len,
4900 &end_match_pos, TRUE);
4901 if (match == NULL || !is_regex_match(match, full_match))
4902 {
4903 vim_free(match);
4904 vim_free(full_match);
4905 continue;
4906 }
Girish Palyaaf220072025-07-07 19:42:10 +02004907 }
Girish Palya93c2d5b2025-07-08 21:29:02 +02004908 vim_free(full_match);
Girish Palya6b49fba2025-06-28 19:47:34 +02004909 }
Girish Palya6b49fba2025-06-28 19:47:34 +02004910
4911 // Include this match if it is not a duplicate
glepnir7cf35bc2025-06-29 16:51:40 +02004912 for (int i = 0; i < ga.ga_len; ++i)
Girish Palya6b49fba2025-06-28 19:47:34 +02004913 {
4914 if (STRCMP(match, ((char_u **)ga.ga_data)[i]) == 0)
4915 {
4916 VIM_CLEAR(match);
4917 break;
4918 }
4919 }
4920 if (match != NULL)
4921 {
4922 if (ga_grow(&ga, 1) == FAIL)
4923 goto cleanup;
4924 ((char_u **)ga.ga_data)[ga.ga_len++] = match;
4925 if (ga.ga_len > TAG_MANY)
4926 break;
4927 }
4928 if (has_range)
4929 cur_match_pos = word_end_pos;
4930 }
4931
Girish Palya6b49fba2025-06-28 19:47:34 +02004932 *matches = (char_u **)ga.ga_data;
4933 *numMatches = ga.ga_len;
4934 return OK;
4935
4936cleanup:
Girish Palya6b49fba2025-06-28 19:47:34 +02004937 ga_clear_strings(&ga);
4938 return FAIL;
4939}