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