blob: 4077fb341626f08beb8899d95bcda2b2cf793237 [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 ?
17
zeertzjq7a5115c2025-03-19 20:29:58 +010018static 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 +000019static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000020static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020021static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020023#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000024static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000025static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020026#endif
27
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000028// "compl_match_array" points the currently displayed list of entries in the
29// popup menu. It is NULL when there is no popup menu.
30static pumitem_T *compl_match_array = NULL;
31static int compl_match_arraysize;
32// First column in cmdline of the matched item for completion.
33static int compl_startcol;
34static int compl_selected;
Girish Palya92f68e22025-04-21 11:12:41 +020035// cmdline before expansion
36static char_u *cmdline_orig = NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020052 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000053 && xp->xp_context != EXPAND_FILES
54 && xp->xp_context != EXPAND_FILES_IN_PATH
55 && xp->xp_context != EXPAND_FILETYPE
Christian Brabandta3422aa2025-04-23 21:04:24 +020056 && xp->xp_context != EXPAND_FILETYPECMD
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010057 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010059 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020061 && xp->xp_context != EXPAND_STRING_SETTING
62 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_OWNSYNTAX
64 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000065 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020067 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000068 && xp->xp_context != EXPAND_TAGS
69 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000070 && xp->xp_context != EXPAND_USER_LIST);
71}
72
73/*
74 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000075 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
76 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000077 */
78 int
79cmdline_fuzzy_complete(char_u *fuzzystr)
80{
81 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
82}
83
84/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000085 * sort function for the completion matches.
86 * <SNR> functions should be sorted to the end.
87 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020088 static int
89sort_func_compare(const void *s1, const void *s2)
90{
91 char_u *p1 = *(char_u **)s1;
92 char_u *p2 = *(char_u **)s2;
93
94 if (*p1 != '<' && *p2 == '<') return -1;
95 if (*p1 == '<' && *p2 != '<') return 1;
96 return STRCMP(p1, p2);
97}
Bram Moolenaar66b51422019-08-18 21:44:12 +020098
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000099/*
100 * Escape special characters in the cmdline completion matches.
101 */
Bram Moolenaar66b51422019-08-18 21:44:12 +0200102 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000103wildescape(
104 expand_T *xp,
105 char_u *str,
106 int numfiles,
107 char_u **files)
108{
109 char_u *p;
110 int vse_what = xp->xp_context == EXPAND_BUFFERS
111 ? VSE_BUFFER : VSE_NONE;
112
113 if (xp->xp_context == EXPAND_FILES
114 || xp->xp_context == EXPAND_FILES_IN_PATH
115 || xp->xp_context == EXPAND_SHELLCMD
116 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200117 || xp->xp_context == EXPAND_DIRECTORIES
118 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000119 {
120 // Insert a backslash into a file name before a space, \, %, #
121 // and wildmatch characters, except '~'.
122 for (int i = 0; i < numfiles; ++i)
123 {
124 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200125 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000126 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200127 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
128 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000129 if (p != NULL)
130 {
131 vim_free(files[i]);
132 files[i] = p;
133#if defined(BACKSLASH_IN_FILENAME)
134 p = vim_strsave_escaped(files[i], (char_u *)" ");
135 if (p != NULL)
136 {
137 vim_free(files[i]);
138 files[i] = p;
139 }
140#endif
141 }
142 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200143 else if (xp->xp_backslash & XP_BS_COMMA)
144 {
145 if (vim_strchr(files[i], ',') != NULL)
146 {
147 p = vim_strsave_escaped(files[i], (char_u *)",");
148 if (p != NULL)
149 {
150 vim_free(files[i]);
151 files[i] = p;
152 }
153 }
154 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000155#ifdef BACKSLASH_IN_FILENAME
156 p = vim_strsave_fnameescape(files[i], vse_what);
157#else
158 p = vim_strsave_fnameescape(files[i],
159 xp->xp_shell ? VSE_SHELL : vse_what);
160#endif
161 if (p != NULL)
162 {
163 vim_free(files[i]);
164 files[i] = p;
165 }
166
167 // If 'str' starts with "\~", replace "~" at start of
168 // files[i] with "\~".
169 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
170 escape_fname(&files[i]);
171 }
172 xp->xp_backslash = XP_BS_NONE;
173
174 // If the first file starts with a '+' escape it. Otherwise it
175 // could be seen as "+cmd".
176 if (*files[0] == '+')
177 escape_fname(&files[0]);
178 }
179 else if (xp->xp_context == EXPAND_TAGS)
180 {
181 // Insert a backslash before characters in a tag name that
182 // would terminate the ":tag" command.
183 for (int i = 0; i < numfiles; ++i)
184 {
185 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
186 if (p != NULL)
187 {
188 vim_free(files[i]);
189 files[i] = p;
190 }
191 }
192 }
193}
194
195/*
196 * Escape special characters in the cmdline completion matches.
197 */
198 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200199ExpandEscape(
200 expand_T *xp,
201 char_u *str,
202 int numfiles,
203 char_u **files,
204 int options)
205{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200206 // May change home directory back to "~"
207 if (options & WILD_HOME_REPLACE)
208 tilde_replace(str, numfiles, files);
209
210 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000211 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200212}
213
214/*
215 * Return FAIL if this is not an appropriate context in which to do
216 * completion of anything, return OK if it is (even if there are no matches).
217 * For the caller, this means that the character is just passed through like a
218 * normal character (instead of being expanded). This allows :s/^I^D etc.
219 */
220 int
221nextwild(
222 expand_T *xp,
223 int type,
224 int options, // extra options for ExpandOne()
225 int escape) // if TRUE, escape the returned matches
226{
227 cmdline_info_T *ccline = get_cmdline_info();
228 int i, j;
229 char_u *p1;
230 char_u *p2;
231 int difflen;
232 int v;
233
234 if (xp->xp_numfiles == -1)
235 {
Jim Zhou3255af82025-02-27 19:29:50 +0100236#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100237 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100238 {
239 // Expand commands typed in input() function
240 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100241 }
242 else
Jim Zhou3255af82025-02-27 19:29:50 +0100243#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100244 {
Jim Zhou3255af82025-02-27 19:29:50 +0100245 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100246 }
247 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200248 }
249
250 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
251 {
252 beep_flush();
253 return OK; // Something illegal on command line
254 }
255 if (xp->xp_context == EXPAND_NOTHING)
256 {
257 // Caller can use the character as a normal char instead
258 return FAIL;
259 }
260
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000261 // If cmd_silent is set then don't show the dots, because redrawcmd() below
262 // won't remove them.
263 if (!cmd_silent)
264 {
265 msg_puts("..."); // show that we are busy
266 out_flush();
267 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200268
269 i = (int)(xp->xp_pattern - ccline->cmdbuff);
270 xp->xp_pattern_len = ccline->cmdpos - i;
271
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000272 if (type == WILD_NEXT || type == WILD_PREV
273 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200274 {
275 // Get next/previous match for a previous expanded pattern.
276 p2 = ExpandOne(xp, NULL, NULL, 0, type);
277 }
278 else
279 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000280 if (cmdline_fuzzy_completion_supported(xp))
281 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200282 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000283 else
284 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
285
Bram Moolenaar66b51422019-08-18 21:44:12 +0200286 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000287 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200288 p2 = NULL;
289 else
290 {
291 int use_options = options |
292 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100293 if (use_options & WILD_KEEP_SOLE_ITEM)
294 use_options &= ~WILD_KEEP_SOLE_ITEM;
295
Bram Moolenaar66b51422019-08-18 21:44:12 +0200296 if (escape)
297 use_options |= WILD_ESCAPE;
298
299 if (p_wic)
300 use_options += WILD_ICASE;
301 p2 = ExpandOne(xp, p1,
302 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
303 use_options, type);
304 vim_free(p1);
305 // longest match: make sure it is not shorter, happens with :help
306 if (p2 != NULL && type == WILD_LONGEST)
307 {
308 for (j = 0; j < xp->xp_pattern_len; ++j)
309 if (ccline->cmdbuff[i + j] == '*'
310 || ccline->cmdbuff[i + j] == '?')
311 break;
312 if ((int)STRLEN(p2) < j)
313 VIM_CLEAR(p2);
314 }
315 }
316 }
317
318 if (p2 != NULL && !got_int)
319 {
320 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
321 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
322 {
323 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
324 xp->xp_pattern = ccline->cmdbuff + i;
325 }
326 else
327 v = OK;
328 if (v == OK)
329 {
330 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
331 &ccline->cmdbuff[ccline->cmdpos],
332 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
333 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
334 ccline->cmdlen += difflen;
335 ccline->cmdpos += difflen;
336 }
337 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200338
339 redrawcmd();
340 cursorcmd();
341
342 // When expanding a ":map" command and no matches are found, assume that
343 // the key is supposed to be inserted literally
344 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
345 return FAIL;
346
347 if (xp->xp_numfiles <= 0 && p2 == NULL)
348 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100349 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200350 // free expanded pattern
351 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
352
John Marriott1be5b372025-06-23 19:57:29 +0200353 vim_free(p2);
354
Bram Moolenaar66b51422019-08-18 21:44:12 +0200355 return OK;
356}
357
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000358/*
359 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000360 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000361 */
362 static int
363cmdline_pum_create(
364 cmdline_info_T *ccline,
365 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000366 char_u **matches,
367 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000368 int showtail)
369{
370 int i;
371 int columns;
372
373 // Add all the completion matches
John Marriott1be5b372025-06-23 19:57:29 +0200374 compl_match_array = ALLOC_MULT(pumitem_T, numMatches);
375 if (compl_match_array == NULL)
376 return EXPAND_UNSUCCESSFUL;
377
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000378 compl_match_arraysize = numMatches;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000379 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000380 {
zeertzjqc51a3762022-12-10 10:22:29 +0000381 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000382 compl_match_array[i].pum_info = NULL;
383 compl_match_array[i].pum_extra = NULL;
384 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200385 compl_match_array[i].pum_user_abbr_hlattr = -1;
386 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000387 }
388
389 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200390 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000391 columns = vim_strsize(xp->xp_pattern);
392 if (showtail)
393 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000394 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000395 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396 }
glepnir977561a2025-02-13 20:48:56 +0100397 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000398
399 // no default selection
400 compl_selected = -1;
401
402 cmdline_pum_display();
403
404 return EXPAND_OK;
405}
406
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407/*
408 * Display the cmdline completion matches in a popup menu
409 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
413 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
414}
415
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000416/*
417 * Returns TRUE if the cmdline completion popup menu is being displayed.
418 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000419 int
420cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000421{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100422 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423}
424
425/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000426 * Remove the cmdline completion popup menu (if present), free the list of
427 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000428 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000429 void
zeertzjq1830e782025-03-13 20:29:13 +0100430cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000431{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000432 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100433 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100434#ifdef FEAT_EVAL
435 int save_RedrawingDisabled = RedrawingDisabled;
436 if (cclp->input_fn)
437 RedrawingDisabled = 0;
438#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000439
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000440 pum_undisplay();
441 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200442 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000443 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000444 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000445 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000446 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100447
448 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
449 // as a side effect.
450 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100451#ifdef FEAT_EVAL
452 if (cclp->input_fn)
453 RedrawingDisabled = save_RedrawingDisabled;
454#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000455}
456
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000457 void
458cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000459{
zeertzjq1830e782025-03-13 20:29:13 +0100460 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000461 wildmenu_cleanup(cclp);
462}
463
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000464/*
465 * Returns the starting column number to use for the cmdline completion popup
466 * menu.
467 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000468 int
469cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000470{
471 return compl_startcol;
472}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000473
Bram Moolenaar66b51422019-08-18 21:44:12 +0200474/*
zeertzjqd8c93402024-06-17 18:25:32 +0200475 * Returns the current cmdline completion pattern.
476 */
477 char_u *
478cmdline_compl_pattern(void)
479{
480 expand_T *xp = get_cmdline_info()->xpc;
481
482 return xp == NULL ? NULL : xp->xp_orig;
483}
484
485/*
486 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
487 */
488 int
489cmdline_compl_is_fuzzy(void)
490{
491 expand_T *xp = get_cmdline_info()->xpc;
492
493 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
494}
495
496/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000497 * Return the number of characters that should be skipped in a status match.
498 * These are backslashes used for escaping. Do show backslashes in help tags.
499 */
500 static int
501skip_status_match_char(expand_T *xp, char_u *s)
502{
503 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
504#ifdef FEAT_MENU
505 || ((xp->xp_context == EXPAND_MENUS
506 || xp->xp_context == EXPAND_MENUNAMES)
507 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
508#endif
509 )
510 {
511#ifndef BACKSLASH_IN_FILENAME
512 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
513 return 2;
514#endif
515 return 1;
516 }
517 return 0;
518}
519
520/*
521 * Get the length of an item as it will be shown in the status line.
522 */
523 static int
524status_match_len(expand_T *xp, char_u *s)
525{
526 int len = 0;
527
528#ifdef FEAT_MENU
529 int emenu = xp->xp_context == EXPAND_MENUS
530 || xp->xp_context == EXPAND_MENUNAMES;
531
532 // Check for menu separators - replace with '|'.
533 if (emenu && menu_is_separator(s))
534 return 1;
535#endif
536
537 while (*s != NUL)
538 {
539 s += skip_status_match_char(xp, s);
540 len += ptr2cells(s);
541 MB_PTR_ADV(s);
542 }
543
544 return len;
545}
546
547/*
548 * Show wildchar matches in the status line.
549 * Show at least the "match" item.
550 * We start at item 'first_match' in the list and show all matches that fit.
551 *
552 * If inversion is possible we use it. Else '=' characters are used.
553 */
554 static void
555win_redr_status_matches(
556 expand_T *xp,
557 int num_matches,
558 char_u **matches, // list of matches
559 int match,
560 int showtail)
561{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000562 int row;
563 char_u *buf;
564 int len;
565 int clen; // length in screen cells
566 int fillchar;
567 int attr;
568 int i;
569 int highlight = TRUE;
570 char_u *selstart = NULL;
571 int selstart_col = 0;
572 char_u *selend = NULL;
573 static int first_match = 0;
574 int add_left = FALSE;
575 char_u *s;
576#ifdef FEAT_MENU
577 int emenu;
578#endif
579 int l;
580
581 if (matches == NULL) // interrupted completion?
582 return;
583
584 if (has_mbyte)
585 buf = alloc(Columns * MB_MAXBYTES + 1);
586 else
587 buf = alloc(Columns + 1);
588 if (buf == NULL)
589 return;
590
591 if (match == -1) // don't show match but original text
592 {
593 match = 0;
594 highlight = FALSE;
595 }
596 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000597 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000598 if (match == 0)
599 first_match = 0;
600 else if (match < first_match)
601 {
602 // jumping left, as far as we can go
603 first_match = match;
604 add_left = TRUE;
605 }
606 else
607 {
608 // check if match fits on the screen
609 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000610 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000611 if (first_match > 0)
612 clen += 2;
613 // jumping right, put match at the left
614 if ((long)clen > Columns)
615 {
616 first_match = match;
617 // if showing the last match, we can add some on the left
618 clen = 2;
619 for (i = match; i < num_matches; ++i)
620 {
zeertzjqc51a3762022-12-10 10:22:29 +0000621 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000622 if ((long)clen >= Columns)
623 break;
624 }
625 if (i == num_matches)
626 add_left = TRUE;
627 }
628 }
629 if (add_left)
630 while (first_match > 0)
631 {
zeertzjqc51a3762022-12-10 10:22:29 +0000632 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000633 if ((long)clen >= Columns)
634 break;
635 --first_match;
636 }
637
638 fillchar = fillchar_status(&attr, curwin);
639
640 if (first_match == 0)
641 {
642 *buf = NUL;
643 len = 0;
644 }
645 else
646 {
647 STRCPY(buf, "< ");
648 len = 2;
649 }
650 clen = len;
651
652 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000653 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000654 {
655 if (i == match)
656 {
657 selstart = buf + len;
658 selstart_col = clen;
659 }
660
zeertzjqc51a3762022-12-10 10:22:29 +0000661 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000662 // Check for menu separators - replace with '|'
663#ifdef FEAT_MENU
664 emenu = (xp->xp_context == EXPAND_MENUS
665 || xp->xp_context == EXPAND_MENUNAMES);
666 if (emenu && menu_is_separator(s))
667 {
668 STRCPY(buf + len, transchar('|'));
669 l = (int)STRLEN(buf + len);
670 len += l;
671 clen += l;
672 }
673 else
674#endif
675 for ( ; *s != NUL; ++s)
676 {
677 s += skip_status_match_char(xp, s);
678 clen += ptr2cells(s);
679 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
680 {
681 STRNCPY(buf + len, s, l);
682 s += l - 1;
683 len += l;
684 }
685 else
686 {
687 STRCPY(buf + len, transchar_byte(*s));
688 len += (int)STRLEN(buf + len);
689 }
690 }
691 if (i == match)
692 selend = buf + len;
693
694 *(buf + len++) = ' ';
695 *(buf + len++) = ' ';
696 clen += 2;
697 if (++i == num_matches)
698 break;
699 }
700
701 if (i != num_matches)
702 {
703 *(buf + len++) = '>';
704 ++clen;
705 }
706
707 buf[len] = NUL;
708
709 row = cmdline_row - 1;
710 if (row >= 0)
711 {
712 if (wild_menu_showing == 0)
713 {
714 if (msg_scrolled > 0)
715 {
716 // Put the wildmenu just above the command line. If there is
717 // no room, scroll the screen one line up.
718 if (cmdline_row == Rows - 1)
719 {
720 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
721 ++msg_scrolled;
722 }
723 else
724 {
725 ++cmdline_row;
726 ++row;
727 }
728 wild_menu_showing = WM_SCROLLED;
729 }
730 else
731 {
732 // Create status line if needed by setting 'laststatus' to 2.
733 // Set 'winminheight' to zero to avoid that the window is
734 // resized.
735 if (lastwin->w_status_height == 0)
736 {
737 save_p_ls = p_ls;
738 save_p_wmh = p_wmh;
739 p_ls = 2;
740 p_wmh = 0;
741 last_status(FALSE);
742 }
743 wild_menu_showing = WM_SHOWN;
744 }
745 }
746
747 screen_puts(buf, row, 0, attr);
748 if (selstart != NULL && highlight)
749 {
750 *selend = NUL;
751 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
752 }
753
754 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
755 }
756
757 win_redraw_last_status(topframe);
758 vim_free(buf);
759}
760
761/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000762 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200763 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000764 */
765 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100766get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000767{
glepnir977561a2025-02-13 20:48:56 +0100768 int findex = xp->xp_selected;
769 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000770
zeertzjqb6c900b2025-02-14 17:59:31 +0100771 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000772 if (xp->xp_numfiles <= 0)
773 return NULL;
774
775 if (mode == WILD_PREV)
776 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100777 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000778 if (findex == -1)
779 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100780 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000781 --findex;
782 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000783 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000784 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100785 // Select the next entry
786 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000787 }
glepnir977561a2025-02-13 20:48:56 +0100788 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000789 {
glepnir977561a2025-02-13 20:48:56 +0100790 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
791 ht = pum_get_height();
792 if (ht > 3)
793 ht -= 2;
794
795 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000796 {
glepnir977561a2025-02-13 20:48:56 +0100797 if (findex == 0)
798 // at the first entry, don't select any entries
799 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100800 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100801 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000802 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100803 else
804 // go up by the pum height
805 findex = MAX(findex - ht, 0);
806 }
807 else // mode == WILD_PAGEDOWN
808 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100809 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100810 // at the last entry, don't select any entries
811 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100812 else if (findex < 0)
813 // no entry is selected, select the first entry
814 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100815 else
816 // go down by the pum height
817 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000818 }
819 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000820
glepnir977561a2025-02-13 20:48:56 +0100821 // Handle wrapping around
822 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100824 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100825 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000826 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000827 else
glepnir977561a2025-02-13 20:48:56 +0100828 // Wrap around to opposite end
829 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000830 }
glepnir977561a2025-02-13 20:48:56 +0100831
832 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000833 if (compl_match_array)
834 {
835 compl_selected = findex;
836 cmdline_pum_display();
837 }
838 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100839 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
840 cmd_showtail);
841
zeertzjqe9ef3472023-08-17 23:57:05 +0200842 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100843 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100844 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000845}
846
847/*
848 * Start the command-line expansion and get the matches.
849 */
850 static char_u *
851ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
852{
853 int non_suf_match; // number without matching suffix
854 int i;
855 char_u *ss = NULL;
856
857 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000858 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000859 options) == FAIL)
860 {
861#ifdef FNAME_ILLEGAL
862 // Illegal file name has been silently skipped. But when there
863 // are wildcards, the real problem is that there was no match,
864 // causing the pattern to be added, which has illegal characters.
865 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
866 semsg(_(e_no_match_str_2), str);
867#endif
868 }
869 else if (xp->xp_numfiles == 0)
870 {
871 if (!(options & WILD_SILENT))
872 semsg(_(e_no_match_str_2), str);
873 }
874 else
875 {
876 // Escape the matches for use on the command line.
877 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
878
879 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000880 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000881 {
882 if (xp->xp_numfiles)
883 non_suf_match = xp->xp_numfiles;
884 else
885 non_suf_match = 1;
886 if ((xp->xp_context == EXPAND_FILES
887 || xp->xp_context == EXPAND_DIRECTORIES)
888 && xp->xp_numfiles > 1)
889 {
890 // More than one match; check suffix.
891 // The files will have been sorted on matching suffix in
892 // expand_wildcards, only need to check the first two.
893 non_suf_match = 0;
894 for (i = 0; i < 2; ++i)
895 if (match_suffix(xp->xp_files[i]))
896 ++non_suf_match;
897 }
898 if (non_suf_match != 1)
899 {
900 // Can we ever get here unless it's while expanding
901 // interactively? If not, we can get rid of this all
902 // together. Don't really want to wait for this message
903 // (and possibly have to hit return to continue!).
904 if (!(options & WILD_SILENT))
905 emsg(_(e_too_many_file_names));
906 else if (!(options & WILD_NO_BEEP))
907 beep_flush();
908 }
909 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
910 ss = vim_strsave(xp->xp_files[0]);
911 }
912 }
913
914 return ss;
915}
916
917/*
918 * Return the longest common part in the list of cmdline completion matches.
919 */
920 static char_u *
921find_longest_match(expand_T *xp, int options)
922{
923 long_u len;
924 int mb_len = 1;
925 int c0, ci;
926 int i;
927 char_u *ss;
928
929 for (len = 0; xp->xp_files[0][len]; len += mb_len)
930 {
931 if (has_mbyte)
932 {
933 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
934 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
935 }
936 else
937 c0 = xp->xp_files[0][len];
938 for (i = 1; i < xp->xp_numfiles; ++i)
939 {
940 if (has_mbyte)
941 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
942 else
943 ci = xp->xp_files[i][len];
944 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
945 || xp->xp_context == EXPAND_FILES
946 || xp->xp_context == EXPAND_SHELLCMD
947 || xp->xp_context == EXPAND_BUFFERS))
948 {
949 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
950 break;
951 }
952 else if (c0 != ci)
953 break;
954 }
955 if (i < xp->xp_numfiles)
956 {
957 if (!(options & WILD_NO_BEEP))
958 vim_beep(BO_WILD);
959 break;
960 }
961 }
962
963 ss = alloc(len + 1);
964 if (ss)
965 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
966
967 return ss;
968}
969
970/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000971 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200972 * Chars that should not be expanded must be preceded with a backslash.
973 * Return a pointer to allocated memory containing the new string.
974 * Return NULL for failure.
975 *
976 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200977 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
978 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200979 *
980 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
981 * is WILD_EXPAND_FREE or WILD_ALL.
982 *
983 * mode = WILD_FREE: just free previously expanded matches
984 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
985 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
986 * mode = WILD_NEXT: use next match in multiple match, wrap to first
987 * mode = WILD_PREV: use previous match in multiple match, wrap to first
988 * mode = WILD_ALL: return all matches concatenated
989 * mode = WILD_LONGEST: return longest matched part
990 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000991 * mode = WILD_APPLY: apply the item selected in the cmdline completion
992 * popup menu and close the menu.
993 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
994 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200995 *
996 * options = WILD_LIST_NOTFOUND: list entries without a match
997 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
998 * options = WILD_USE_NL: Use '\n' for WILD_ALL
999 * options = WILD_NO_BEEP: Don't beep for multiple matches
1000 * options = WILD_ADD_SLASH: add a slash after directory names
1001 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
1002 * options = WILD_SILENT: don't print warning messages
1003 * options = WILD_ESCAPE: put backslash before special chars
1004 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001005 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006 *
1007 * The variables xp->xp_context and xp->xp_backslash must have been set!
1008 */
1009 char_u *
1010ExpandOne(
1011 expand_T *xp,
1012 char_u *str,
1013 char_u *orig, // allocated copy of original of expanded string
1014 int options,
1015 int mode)
1016{
1017 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001018 int orig_saved = FALSE;
1019 int i;
1020 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021
1022 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001023 if (mode == WILD_NEXT || mode == WILD_PREV
1024 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001025 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001027 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001028 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001029 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001030 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001031 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001032 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001033
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 // free old names
1035 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1036 {
1037 FreeWild(xp->xp_numfiles, xp->xp_files);
1038 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001039 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001040
1041 // The entries from xp_files may be used in the PUM, remove it.
1042 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001043 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001044 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001045 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046
1047 if (mode == WILD_FREE) // only release file name
1048 return NULL;
1049
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001050 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001051 {
zeertzjq28a23602023-09-29 19:58:35 +02001052 vim_free(xp->xp_orig);
1053 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001054 orig_saved = TRUE;
1055
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001056 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001057 }
1058
1059 // Find longest common part
1060 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1061 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001062 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001063 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 }
1065
Bram Moolenaar57e95172022-08-20 19:26:14 +01001066 // Concatenate all matching names. Unless interrupted, this can be slow
1067 // and the result probably won't be used.
1068 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001069 {
1070 len = 0;
1071 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001072 {
1073 if (i > 0)
1074 {
1075 if (xp->xp_prefix == XP_PREFIX_NO)
1076 len += 2; // prefix "no"
1077 else if (xp->xp_prefix == XP_PREFIX_INV)
1078 len += 3; // prefix "inv"
1079 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001080 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001081 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001082 ss = alloc(len);
1083 if (ss != NULL)
1084 {
1085 *ss = NUL;
1086 for (i = 0; i < xp->xp_numfiles; ++i)
1087 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001088 if (i > 0)
1089 {
1090 if (xp->xp_prefix == XP_PREFIX_NO)
1091 STRCAT(ss, "no");
1092 else if (xp->xp_prefix == XP_PREFIX_INV)
1093 STRCAT(ss, "inv");
1094 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001095 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001096
Bram Moolenaar66b51422019-08-18 21:44:12 +02001097 if (i != xp->xp_numfiles - 1)
1098 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1099 }
1100 }
1101 }
1102
1103 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1104 ExpandCleanup(xp);
1105
zeertzjq28a23602023-09-29 19:58:35 +02001106 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001107 if (!orig_saved)
1108 vim_free(orig);
1109
1110 return ss;
1111}
1112
1113/*
1114 * Prepare an expand structure for use.
1115 */
1116 void
1117ExpandInit(expand_T *xp)
1118{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001119 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001120 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001121 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001122 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001123}
1124
1125/*
1126 * Cleanup an expand structure after use.
1127 */
1128 void
1129ExpandCleanup(expand_T *xp)
1130{
1131 if (xp->xp_numfiles >= 0)
1132 {
1133 FreeWild(xp->xp_numfiles, xp->xp_files);
1134 xp->xp_numfiles = -1;
1135 }
zeertzjq28a23602023-09-29 19:58:35 +02001136 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001137}
1138
zeertzjqec270a52025-04-23 20:50:23 +02001139 void
1140clear_cmdline_orig(void)
1141{
1142 VIM_CLEAR(cmdline_orig);
1143}
1144
Bram Moolenaar66b51422019-08-18 21:44:12 +02001145/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001146 * Display one line of completion matches. Multiple matches are displayed in
1147 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001148 * matches - list of completion match names
1149 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001150 * lines - number of output lines
1151 * linenr - line number of matches to display
1152 * maxlen - maximum number of characters in each line
1153 * showtail - display only the tail of the full path of a file name
1154 * dir_attr - highlight attribute to use for directory names
1155 */
1156 static void
1157showmatches_oneline(
1158 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001159 char_u **matches,
1160 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001161 int lines,
1162 int linenr,
1163 int maxlen,
1164 int showtail,
1165 int dir_attr)
1166{
1167 int i, j;
1168 int isdir;
1169 int lastlen;
1170 char_u *p;
1171
1172 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001173 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001174 {
1175 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1176 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1178 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001179 msg_advance(maxlen + 1);
1180 msg_puts((char *)p);
1181 msg_advance(maxlen + 3);
1182 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1183 break;
1184 }
1185 for (i = maxlen - lastlen; --i >= 0; )
1186 msg_putchar(' ');
1187 if (xp->xp_context == EXPAND_FILES
1188 || xp->xp_context == EXPAND_SHELLCMD
1189 || xp->xp_context == EXPAND_BUFFERS)
1190 {
1191 // highlight directories
1192 if (xp->xp_numfiles != -1)
1193 {
1194 char_u *halved_slash;
1195 char_u *exp_path;
1196 char_u *path;
1197
1198 // Expansion was done before and special characters
1199 // were escaped, need to halve backslashes. Also
1200 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001201 exp_path = expand_env_save_opt(matches[j], TRUE);
1202 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001203 halved_slash = backslash_halve_save(path);
1204 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001205 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001206 vim_free(exp_path);
1207 if (halved_slash != path)
1208 vim_free(halved_slash);
1209 }
1210 else
1211 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001213 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001214 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001215 else
1216 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001217 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001218 TRUE);
1219 p = NameBuff;
1220 }
1221 }
1222 else
1223 {
1224 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001225 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001226 }
1227 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1228 }
1229 if (msg_col > 0) // when not wrapped around
1230 {
1231 msg_clr_eos();
1232 msg_putchar('\n');
1233 }
1234 out_flush(); // show one line at a time
1235}
1236
1237/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001238 * Show all matches for completion on the command line.
1239 * Returns EXPAND_NOTHING when the character that triggered expansion should
1240 * be inserted like a normal character.
1241 */
1242 int
1243showmatches(expand_T *xp, int wildmenu UNUSED)
1244{
1245 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001246 int numMatches;
1247 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001248 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001249 int maxlen;
1250 int lines;
1251 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 int attr;
1253 int showtail;
1254
Girish Palya92f68e22025-04-21 11:12:41 +02001255 // Save cmdline before expansion
1256 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001257 {
1258 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001259 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001260 }
Girish Palya92f68e22025-04-21 11:12:41 +02001261
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 if (xp->xp_numfiles == -1)
1263 {
1264 set_expand_context(xp);
1265 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001266 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001267 showtail = expand_showtail(xp);
1268 if (i != EXPAND_OK)
1269 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 }
1271 else
1272 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 numMatches = xp->xp_numfiles;
1274 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 showtail = cmd_showtail;
1276 }
1277
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001278 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001279 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001280 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001281
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 if (!wildmenu)
1283 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001284 msg_didany = FALSE; // lines_left will be set
1285 msg_start(); // prepare for paging
1286 msg_putchar('\n');
1287 out_flush();
1288 cmdline_row = msg_row;
1289 msg_didany = FALSE; // lines_left will be set again
1290 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292
1293 if (got_int)
1294 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001295 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001296 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001297 else
1298 {
1299 // find the length of the longest file name
1300 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001301 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001302 {
1303 if (!showtail && (xp->xp_context == EXPAND_FILES
1304 || xp->xp_context == EXPAND_SHELLCMD
1305 || xp->xp_context == EXPAND_BUFFERS))
1306 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001307 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001308 j = vim_strsize(NameBuff);
1309 }
1310 else
zeertzjqc51a3762022-12-10 10:22:29 +00001311 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001312 if (j > maxlen)
1313 maxlen = j;
1314 }
1315
1316 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001317 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001318 else
1319 {
1320 // compute the number of columns and lines for the listing
1321 maxlen += 2; // two spaces between file names
1322 columns = ((int)Columns + 2) / maxlen;
1323 if (columns < 1)
1324 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001325 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001326 }
1327
1328 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1329
1330 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1331 {
1332 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1333 msg_clr_eos();
1334 msg_advance(maxlen - 3);
1335 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1336 }
1337
1338 // list the files line by line
1339 for (i = 0; i < lines; ++i)
1340 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001341 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001342 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001343 if (got_int)
1344 {
1345 got_int = FALSE;
1346 break;
1347 }
1348 }
1349
1350 // we redraw the command below the lines that we have just listed
1351 // This is a bit tricky, but it saves a lot of screen updating.
1352 cmdline_row = msg_row; // will put it back later
1353 }
1354
1355 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001356 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001357
1358 return EXPAND_OK;
1359}
1360
1361/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001362 * gettail() version for showmatches() and win_redr_status_matches():
1363 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001364 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001365 static char_u *
1366showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001367{
1368 char_u *p;
1369 char_u *t = s;
1370 int had_sep = FALSE;
1371
1372 for (p = s; *p != NUL; )
1373 {
1374 if (vim_ispathsep(*p)
1375#ifdef BACKSLASH_IN_FILENAME
1376 && !rem_backslash(p)
1377#endif
1378 )
1379 had_sep = TRUE;
1380 else if (had_sep)
1381 {
1382 t = p;
1383 had_sep = FALSE;
1384 }
1385 MB_PTR_ADV(p);
1386 }
1387 return t;
1388}
1389
1390/*
1391 * Return TRUE if we only need to show the tail of completion matches.
1392 * When not completing file names or there is a wildcard in the path FALSE is
1393 * returned.
1394 */
1395 static int
1396expand_showtail(expand_T *xp)
1397{
1398 char_u *s;
1399 char_u *end;
1400
1401 // When not completing file names a "/" may mean something different.
1402 if (xp->xp_context != EXPAND_FILES
1403 && xp->xp_context != EXPAND_SHELLCMD
1404 && xp->xp_context != EXPAND_DIRECTORIES)
1405 return FALSE;
1406
1407 end = gettail(xp->xp_pattern);
1408 if (end == xp->xp_pattern) // there is no path separator
1409 return FALSE;
1410
1411 for (s = xp->xp_pattern; s < end; s++)
1412 {
1413 // Skip escaped wildcards. Only when the backslash is not a path
1414 // separator, on DOS the '*' "path\*\file" must not be skipped.
1415 if (rem_backslash(s))
1416 ++s;
1417 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1418 return FALSE;
1419 }
1420 return TRUE;
1421}
1422
1423/*
1424 * Prepare a string for expansion.
1425 * When expanding file names: The string will be used with expand_wildcards().
1426 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1427 * When expanding other names: The string will be used with regcomp(). Copy
1428 * the name into allocated memory and prepend "^".
1429 */
1430 char_u *
1431addstar(
1432 char_u *fname,
1433 int len,
1434 int context) // EXPAND_FILES etc.
1435{
1436 char_u *retval;
1437 int i, j;
1438 int new_len;
1439 char_u *tail;
1440 int ends_in_star;
1441
1442 if (context != EXPAND_FILES
1443 && context != EXPAND_FILES_IN_PATH
1444 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001445 && context != EXPAND_DIRECTORIES
1446 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001447 {
1448 // Matching will be done internally (on something other than files).
1449 // So we convert the file-matching-type wildcards into our kind for
1450 // use with vim_regcomp(). First work out how long it will be:
1451
1452 // For help tags the translation is done in find_help_tags().
1453 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001454 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001455 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001456 || context == EXPAND_COLORS
1457 || context == EXPAND_COMPILER
1458 || context == EXPAND_OWNSYNTAX
1459 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001460 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001461 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001462 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001463 || ((context == EXPAND_TAGS_LISTFILES
1464 || context == EXPAND_TAGS)
1465 && fname[0] == '/'))
1466 retval = vim_strnsave(fname, len);
1467 else
1468 {
1469 new_len = len + 2; // +2 for '^' at start, NUL at end
1470 for (i = 0; i < len; i++)
1471 {
1472 if (fname[i] == '*' || fname[i] == '~')
1473 new_len++; // '*' needs to be replaced by ".*"
1474 // '~' needs to be replaced by "\~"
1475
1476 // Buffer names are like file names. "." should be literal
1477 if (context == EXPAND_BUFFERS && fname[i] == '.')
1478 new_len++; // "." becomes "\."
1479
1480 // Custom expansion takes care of special things, match
1481 // backslashes literally (perhaps also for other types?)
1482 if ((context == EXPAND_USER_DEFINED
1483 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1484 new_len++; // '\' becomes "\\"
1485 }
1486 retval = alloc(new_len);
1487 if (retval != NULL)
1488 {
1489 retval[0] = '^';
1490 j = 1;
1491 for (i = 0; i < len; i++, j++)
1492 {
1493 // Skip backslash. But why? At least keep it for custom
1494 // expansion.
1495 if (context != EXPAND_USER_DEFINED
1496 && context != EXPAND_USER_LIST
1497 && fname[i] == '\\'
1498 && ++i == len)
1499 break;
1500
1501 switch (fname[i])
1502 {
1503 case '*': retval[j++] = '.';
1504 break;
1505 case '~': retval[j++] = '\\';
1506 break;
1507 case '?': retval[j] = '.';
1508 continue;
1509 case '.': if (context == EXPAND_BUFFERS)
1510 retval[j++] = '\\';
1511 break;
1512 case '\\': if (context == EXPAND_USER_DEFINED
1513 || context == EXPAND_USER_LIST)
1514 retval[j++] = '\\';
1515 break;
1516 }
1517 retval[j] = fname[i];
1518 }
1519 retval[j] = NUL;
1520 }
1521 }
1522 }
1523 else
1524 {
1525 retval = alloc(len + 4);
1526 if (retval != NULL)
1527 {
1528 vim_strncpy(retval, fname, len);
1529
1530 // Don't add a star to *, ~, ~user, $var or `cmd`.
1531 // * would become **, which walks the whole tree.
1532 // ~ would be at the start of the file name, but not the tail.
1533 // $ could be anywhere in the tail.
1534 // ` could be anywhere in the file name.
1535 // When the name ends in '$' don't add a star, remove the '$'.
1536 tail = gettail(retval);
1537 ends_in_star = (len > 0 && retval[len - 1] == '*');
1538#ifndef BACKSLASH_IN_FILENAME
1539 for (i = len - 2; i >= 0; --i)
1540 {
1541 if (retval[i] != '\\')
1542 break;
1543 ends_in_star = !ends_in_star;
1544 }
1545#endif
1546 if ((*retval != '~' || tail != retval)
1547 && !ends_in_star
1548 && vim_strchr(tail, '$') == NULL
1549 && vim_strchr(retval, '`') == NULL)
1550 retval[len++] = '*';
1551 else if (len > 0 && retval[len - 1] == '$')
1552 --len;
1553 retval[len] = NUL;
1554 }
1555 }
1556 return retval;
1557}
1558
1559/*
1560 * Must parse the command line so far to work out what context we are in.
1561 * Completion can then be done based on that context.
1562 * This routine sets the variables:
1563 * xp->xp_pattern The start of the pattern to be expanded within
1564 * the command line (ends at the cursor).
1565 * xp->xp_context The type of thing to expand. Will be one of:
1566 *
1567 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1568 * the command line, like an unknown command. Caller
1569 * should beep.
1570 * EXPAND_NOTHING Unrecognised context for completion, use char like
1571 * a normal char, rather than for completion. eg
1572 * :s/^I/
1573 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1574 * it.
1575 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1576 * EXPAND_FILES After command with EX_XFILE set, or after setting
1577 * with P_EXPAND set. eg :e ^I, :w>>^I
1578 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001579 * when we know only directories are of interest.
1580 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001581 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1582 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1583 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1584 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1585 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1586 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1587 * EXPAND_EVENTS Complete event names
1588 * EXPAND_SYNTAX Complete :syntax command arguments
1589 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1590 * EXPAND_AUGROUP Complete autocommand group names
1591 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1592 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1593 * eg :unmap a^I , :cunab x^I
1594 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1595 * eg :call sub^I
1596 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1597 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1598 * names in expressions, eg :while s^I
1599 * EXPAND_ENV_VARS Complete environment variable names
1600 * EXPAND_USER Complete user names
1601 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001602 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001603set_expand_context(expand_T *xp)
1604{
1605 cmdline_info_T *ccline = get_cmdline_info();
1606
1607 // only expansion for ':', '>' and '=' command-lines
1608 if (ccline->cmdfirstc != ':'
1609#ifdef FEAT_EVAL
1610 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1611 && !ccline->input_fn
1612#endif
1613 )
1614 {
1615 xp->xp_context = EXPAND_NOTHING;
1616 return;
1617 }
1618 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1619}
1620
Bram Moolenaard0190392019-08-23 21:17:35 +02001621/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001622 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1623 * For user defined commands, the completion context is set in 'xp' and the
1624 * completion flags in 'complp'.
1625 *
1626 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001627 */
1628 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001629set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001630{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001631 char_u *p = NULL;
1632 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001633 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001634
1635 // Isolate the command and search for it in the command table.
1636 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001637 // - the 'k' command can directly be followed by any character, but do
1638 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1639 // find matches anywhere in the command name, do this only for command
1640 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001641 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001642 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001643 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001644 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001645 p = cmd + 1;
1646 }
1647 else
1648 {
1649 p = cmd;
1650 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1651 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001652 // A user command may contain digits.
1653 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1654 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001655 while (ASCII_ISALNUM(*p) || *p == '*')
1656 ++p;
1657 // for python 3.x: ":py3*" commands completion
1658 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1659 {
1660 ++p;
1661 while (ASCII_ISALPHA(*p) || *p == '*')
1662 ++p;
1663 }
1664 // check for non-alpha command
1665 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1666 ++p;
1667 len = (int)(p - cmd);
1668
1669 if (len == 0)
1670 {
1671 xp->xp_context = EXPAND_UNSUCCESSFUL;
1672 return NULL;
1673 }
1674
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001676
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001677 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001678 // Also when doing fuzzy expansion for non-shell commands, support
1679 // alphanumeric characters.
1680 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1681 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1683 ++p;
1684 }
1685
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001686 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001687 // character, complete the command name.
1688 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1689 return NULL;
1690
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 {
1693 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1694 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001695 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001696 p = cmd + 1;
1697 }
1698 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1699 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001700 eap->cmd = cmd;
1701 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 }
1705 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 {
1708 // Not still touching the command and it was an illegal one
1709 xp->xp_context = EXPAND_UNSUCCESSFUL;
1710 return NULL;
1711 }
1712
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001713 return p;
1714}
Bram Moolenaard0190392019-08-23 21:17:35 +02001715
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001716/*
1717 * Set the completion context for a command argument with wild card characters.
1718 */
1719 static void
1720set_context_for_wildcard_arg(
1721 exarg_T *eap,
1722 char_u *arg,
1723 int usefilter,
1724 expand_T *xp,
1725 int *complp)
1726{
1727 char_u *p;
1728 int c;
1729 int in_quote = FALSE;
1730 char_u *bow = NULL; // Beginning of word
1731 int len = 0;
1732
1733 // Allow spaces within back-quotes to count as part of the argument
1734 // being expanded.
1735 xp->xp_pattern = skipwhite(arg);
1736 p = xp->xp_pattern;
1737 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 if (has_mbyte)
1740 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 c = *p;
1743 if (c == '\\' && p[1] != NUL)
1744 ++p;
1745 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001746 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001747 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 xp->xp_pattern = p;
1750 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001751 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 in_quote = !in_quote;
1753 }
1754 // An argument can contain just about everything, except
1755 // characters that end the command and white space.
1756 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001757#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001758 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001759#endif
1760 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001761 {
1762 len = 0; // avoid getting stuck when space is in 'isfname'
1763 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001764 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001765 if (has_mbyte)
1766 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001767 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001768 c = *p;
1769 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001770 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001771 if (has_mbyte)
1772 len = (*mb_ptr2len)(p);
1773 else
1774 len = 1;
1775 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001776 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001777 if (in_quote)
1778 bow = p;
1779 else
1780 xp->xp_pattern = p;
1781 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001782 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001783 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001784 }
1785
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001786 // If we are still inside the quotes, and we passed a space, just
1787 // expand from there.
1788 if (bow != NULL && in_quote)
1789 xp->xp_pattern = bow;
1790 xp->xp_context = EXPAND_FILES;
1791
1792 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001793 if (usefilter
1794 || (eap != NULL
1795 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1796 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001797 {
1798#ifndef BACKSLASH_IN_FILENAME
1799 xp->xp_shell = TRUE;
1800#endif
1801 // When still after the command name expand executables.
1802 if (xp->xp_pattern == skipwhite(arg))
1803 xp->xp_context = EXPAND_SHELLCMD;
1804 }
1805
1806 // Check for environment variable.
1807 if (*xp->xp_pattern == '$')
1808 {
1809 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1810 if (!vim_isIDc(*p))
1811 break;
1812 if (*p == NUL)
1813 {
1814 xp->xp_context = EXPAND_ENV_VARS;
1815 ++xp->xp_pattern;
1816 // Avoid that the assignment uses EXPAND_FILES again.
1817 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1818 *complp = EXPAND_ENV_VARS;
1819 }
1820 }
1821 // Check for user names.
1822 if (*xp->xp_pattern == '~')
1823 {
1824 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1825 ;
1826 // Complete ~user only if it partially matches a user name.
1827 // A full match ~user<Tab> will be replaced by user's home
1828 // directory i.e. something like ~user<Tab> -> /home/user/
1829 if (*p == NUL && p > xp->xp_pattern + 1
1830 && match_user(xp->xp_pattern + 1) >= 1)
1831 {
1832 xp->xp_context = EXPAND_USER;
1833 ++xp->xp_pattern;
1834 }
1835 }
1836}
1837
1838/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001839 * Set the completion context for the "++opt=arg" argument. Always returns
1840 * NULL.
1841 */
1842 static char_u *
1843set_context_in_argopt(expand_T *xp, char_u *arg)
1844{
1845 char_u *p;
1846
1847 p = vim_strchr(arg, '=');
1848 if (p == NULL)
1849 xp->xp_pattern = arg;
1850 else
1851 xp->xp_pattern = p + 1;
1852
1853 xp->xp_context = EXPAND_ARGOPT;
1854 return NULL;
1855}
1856
1857#ifdef FEAT_TERMINAL
1858/*
1859 * Set the completion context for :terminal's [options]. Always returns NULL.
1860 */
1861 static char_u *
1862set_context_in_terminalopt(expand_T *xp, char_u *arg)
1863{
1864 char_u *p;
1865
1866 p = vim_strchr(arg, '=');
1867 if (p == NULL)
1868 xp->xp_pattern = arg;
1869 else
1870 xp->xp_pattern = p + 1;
1871
1872 xp->xp_context = EXPAND_TERMINALOPT;
1873 return NULL;
1874}
1875#endif
1876
1877/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001878 * Set the completion context for the :filter command. Returns a pointer to the
1879 * next command after the :filter command.
1880 */
1881 static char_u *
1882set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1883{
1884 if (*arg != NUL)
1885 arg = skip_vimgrep_pat(arg, NULL, NULL);
1886 if (arg == NULL || *arg == NUL)
1887 {
1888 xp->xp_context = EXPAND_NOTHING;
1889 return NULL;
1890 }
1891 return skipwhite(arg);
1892}
1893
1894#ifdef FEAT_SEARCH_EXTRA
1895/*
1896 * Set the completion context for the :match command. Returns a pointer to the
1897 * next command after the :match command.
1898 */
1899 static char_u *
1900set_context_in_match_cmd(expand_T *xp, char_u *arg)
1901{
1902 if (*arg == NUL || !ends_excmd(*arg))
1903 {
1904 // also complete "None"
1905 set_context_in_echohl_cmd(xp, arg);
1906 arg = skipwhite(skiptowhite(arg));
1907 if (*arg != NUL)
1908 {
1909 xp->xp_context = EXPAND_NOTHING;
1910 arg = skip_regexp(arg + 1, *arg, magic_isset());
1911 }
1912 }
1913 return find_nextcmd(arg);
1914}
1915#endif
1916
1917/*
1918 * Returns a pointer to the next command after a :global or a :v command.
1919 * Returns NULL if there is no next command.
1920 */
1921 static char_u *
1922find_cmd_after_global_cmd(char_u *arg)
1923{
1924 int delim;
1925
1926 delim = *arg; // get the delimiter
1927 if (delim)
1928 ++arg; // skip delimiter if there is one
1929
1930 while (arg[0] != NUL && arg[0] != delim)
1931 {
1932 if (arg[0] == '\\' && arg[1] != NUL)
1933 ++arg;
1934 ++arg;
1935 }
1936 if (arg[0] != NUL)
1937 return arg + 1;
1938
1939 return NULL;
1940}
1941
1942/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001943 * Returns a pointer to the next command after a :substitute or a :& command.
1944 * Returns NULL if there is no next command.
1945 */
1946 static char_u *
1947find_cmd_after_substitute_cmd(char_u *arg)
1948{
1949 int delim;
1950
1951 delim = *arg;
1952 if (delim)
1953 {
1954 // skip "from" part
1955 ++arg;
1956 arg = skip_regexp(arg, delim, magic_isset());
1957
1958 if (arg[0] != NUL && arg[0] == delim)
1959 {
1960 // skip "to" part
1961 ++arg;
1962 while (arg[0] != NUL && arg[0] != delim)
1963 {
1964 if (arg[0] == '\\' && arg[1] != NUL)
1965 ++arg;
1966 ++arg;
1967 }
1968 if (arg[0] != NUL) // skip delimiter
1969 ++arg;
1970 }
1971 }
1972 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1973 ++arg;
1974 if (arg[0] != NUL)
1975 return arg;
1976
1977 return NULL;
1978}
1979
1980/*
1981 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1982 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1983 * Returns NULL if there is no next command.
1984 */
1985 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001986find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001987{
1988 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001989 if (*arg != '/')
1990 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001991
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001992 // Match regexp, not just whole words
1993 for (++arg; *arg && *arg != '/'; arg++)
1994 if (*arg == '\\' && arg[1] != NUL)
1995 arg++;
1996 if (*arg)
1997 {
1998 arg = skipwhite(arg + 1);
1999
2000 // Check for trailing illegal characters
2001 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
2002 xp->xp_context = EXPAND_NOTHING;
2003 else
2004 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002005 }
2006
2007 return NULL;
2008}
2009
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002010#ifdef FEAT_EVAL
2011/*
2012 * Set the completion context for the :unlet command. Always returns NULL.
2013 */
2014 static char_u *
2015set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2016{
2017 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2018 arg = xp->xp_pattern + 1;
2019
2020 xp->xp_context = EXPAND_USER_VARS;
2021 xp->xp_pattern = arg;
2022
2023 if (*xp->xp_pattern == '$')
2024 {
2025 xp->xp_context = EXPAND_ENV_VARS;
2026 ++xp->xp_pattern;
2027 }
2028
2029 return NULL;
2030}
2031#endif
2032
2033#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2034/*
2035 * Set the completion context for the :language command. Always returns NULL.
2036 */
2037 static char_u *
2038set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2039{
2040 char_u *p;
2041
2042 p = skiptowhite(arg);
2043 if (*p == NUL)
2044 {
2045 xp->xp_context = EXPAND_LANGUAGE;
2046 xp->xp_pattern = arg;
2047 }
2048 else
2049 {
2050 if ( STRNCMP(arg, "messages", p - arg) == 0
2051 || STRNCMP(arg, "ctype", p - arg) == 0
2052 || STRNCMP(arg, "time", p - arg) == 0
2053 || STRNCMP(arg, "collate", p - arg) == 0)
2054 {
2055 xp->xp_context = EXPAND_LOCALES;
2056 xp->xp_pattern = skipwhite(p);
2057 }
2058 else
2059 xp->xp_context = EXPAND_NOTHING;
2060 }
2061
2062 return NULL;
2063}
2064#endif
2065
Christian Brabandta3422aa2025-04-23 21:04:24 +02002066static enum
2067{
2068 EXP_FILETYPECMD_ALL, // expand all :filetype values
2069 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2070 EXP_FILETYPECMD_INDENT, // expand indent on off
2071 EXP_FILETYPECMD_ONOFF, // expand on off
2072} filetype_expand_what;
2073
2074#define EXPAND_FILETYPECMD_PLUGIN 0x01
2075#define EXPAND_FILETYPECMD_INDENT 0x02
2076#define EXPAND_FILETYPECMD_ONOFF 0x04
2077
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002078#ifdef FEAT_EVAL
2079static enum
2080{
2081 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002082 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2083 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002084} breakpt_expand_what;
2085
2086/*
2087 * Set the completion context for the :breakadd command. Always returns NULL.
2088 */
2089 static char_u *
2090set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2091{
2092 char_u *p;
2093 char_u *subcmd_start;
2094
2095 xp->xp_context = EXPAND_BREAKPOINT;
2096 xp->xp_pattern = arg;
2097
2098 if (cmdidx == CMD_breakadd)
2099 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002100 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002101 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002102 else
2103 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002104
2105 p = skipwhite(arg);
2106 if (*p == NUL)
2107 return NULL;
2108 subcmd_start = p;
2109
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002110 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002111 {
2112 // :breakadd file [lnum] <filename>
2113 // :breakadd func [lnum] <funcname>
2114 p += 4;
2115 p = skipwhite(p);
2116
2117 // skip line number (if specified)
2118 if (VIM_ISDIGIT(*p))
2119 {
2120 p = skipdigits(p);
2121 if (*p != ' ')
2122 {
2123 xp->xp_context = EXPAND_NOTHING;
2124 return NULL;
2125 }
2126 p = skipwhite(p);
2127 }
2128 if (STRNCMP("file", subcmd_start, 4) == 0)
2129 xp->xp_context = EXPAND_FILES;
2130 else
2131 xp->xp_context = EXPAND_USER_FUNC;
2132 xp->xp_pattern = p;
2133 }
2134 else if (STRNCMP("expr ", p, 5) == 0)
2135 {
2136 // :breakadd expr <expression>
2137 xp->xp_context = EXPAND_EXPRESSION;
2138 xp->xp_pattern = skipwhite(p + 5);
2139 }
2140
2141 return NULL;
2142}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002143
2144 static char_u *
2145set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2146{
2147 char_u *p;
2148
2149 xp->xp_context = EXPAND_NOTHING;
2150 xp->xp_pattern = NULL;
2151
2152 p = skipwhite(arg);
2153 if (VIM_ISDIGIT(*p))
2154 return NULL;
2155
2156 xp->xp_context = EXPAND_SCRIPTNAMES;
2157 xp->xp_pattern = p;
2158
2159 return NULL;
2160}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002161#endif
2162
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002163/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002164 * Set the completion context for the :filetype command. Always returns NULL.
2165 */
2166 static char_u *
2167set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2168{
2169 char_u *p;
2170 int val = 0;
2171
2172 xp->xp_context = EXPAND_FILETYPECMD;
2173 xp->xp_pattern = arg;
2174 filetype_expand_what = EXP_FILETYPECMD_ALL;
2175
2176 p = skipwhite(arg);
2177 if (*p == NUL)
2178 return NULL;
2179
2180 for (;;)
2181 {
2182 if (STRNCMP(p, "plugin", 6) == 0)
2183 {
2184 val |= EXPAND_FILETYPECMD_PLUGIN;
2185 p = skipwhite(p + 6);
2186 continue;
2187 }
2188 if (STRNCMP(p, "indent", 6) == 0)
2189 {
2190 val |= EXPAND_FILETYPECMD_INDENT;
2191 p = skipwhite(p + 6);
2192 continue;
2193 }
2194 break;
2195 }
2196
2197 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2198 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2199 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2200 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2201 else if ((val & EXPAND_FILETYPECMD_INDENT))
2202 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2203
2204 xp->xp_pattern = p;
2205
2206 return NULL;
2207}
2208
2209
2210/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002211 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2212 * The argument to the command is 'arg' and the argument flags is 'argt'.
2213 * For user-defined commands and for environment variables, 'compl' has the
2214 * completion type.
2215 * Returns a pointer to the next command. Returns NULL if there is no next
2216 * command.
2217 */
2218 static char_u *
2219set_context_by_cmdname(
2220 char_u *cmd,
2221 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002222 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002223 char_u *arg,
2224 long argt,
2225 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002226 int forceit)
2227{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002228 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002229 {
2230 case CMD_find:
2231 case CMD_sfind:
2232 case CMD_tabfind:
2233 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002234 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002235 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002236 break;
2237 case CMD_cd:
2238 case CMD_chdir:
2239 case CMD_tcd:
2240 case CMD_tchdir:
2241 case CMD_lcd:
2242 case CMD_lchdir:
2243 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002244 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 break;
2246 case CMD_help:
2247 xp->xp_context = EXPAND_HELP;
2248 xp->xp_pattern = arg;
2249 break;
2250
2251 // Command modifiers: return the argument.
2252 // Also for commands with an argument that is a command.
2253 case CMD_aboveleft:
2254 case CMD_argdo:
2255 case CMD_belowright:
2256 case CMD_botright:
2257 case CMD_browse:
2258 case CMD_bufdo:
2259 case CMD_cdo:
2260 case CMD_cfdo:
2261 case CMD_confirm:
2262 case CMD_debug:
2263 case CMD_folddoclosed:
2264 case CMD_folddoopen:
2265 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002266 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002267 case CMD_keepalt:
2268 case CMD_keepjumps:
2269 case CMD_keepmarks:
2270 case CMD_keeppatterns:
2271 case CMD_ldo:
2272 case CMD_leftabove:
2273 case CMD_lfdo:
2274 case CMD_lockmarks:
2275 case CMD_noautocmd:
2276 case CMD_noswapfile:
2277 case CMD_rightbelow:
2278 case CMD_sandbox:
2279 case CMD_silent:
2280 case CMD_tab:
2281 case CMD_tabdo:
2282 case CMD_topleft:
2283 case CMD_verbose:
2284 case CMD_vertical:
2285 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002286 case CMD_vim9cmd:
2287 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002288 return arg;
2289
2290 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002291 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002292
2293#ifdef FEAT_SEARCH_EXTRA
2294 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002295 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002296#endif
2297
2298 // All completion for the +cmdline_compl feature goes here.
2299
2300 case CMD_command:
2301 return set_context_in_user_cmd(xp, arg);
2302
2303 case CMD_delcommand:
2304 xp->xp_context = EXPAND_USER_COMMANDS;
2305 xp->xp_pattern = arg;
2306 break;
2307
2308 case CMD_global:
2309 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002310 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002311 case CMD_and:
2312 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002313 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002314 case CMD_isearch:
2315 case CMD_dsearch:
2316 case CMD_ilist:
2317 case CMD_dlist:
2318 case CMD_ijump:
2319 case CMD_psearch:
2320 case CMD_djump:
2321 case CMD_isplit:
2322 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002323 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002324 case CMD_autocmd:
2325 return set_context_in_autocmd(xp, arg, FALSE);
2326 case CMD_doautocmd:
2327 case CMD_doautoall:
2328 return set_context_in_autocmd(xp, arg, TRUE);
2329 case CMD_set:
2330 set_context_in_set_cmd(xp, arg, 0);
2331 break;
2332 case CMD_setglobal:
2333 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2334 break;
2335 case CMD_setlocal:
2336 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2337 break;
2338 case CMD_tag:
2339 case CMD_stag:
2340 case CMD_ptag:
2341 case CMD_ltag:
2342 case CMD_tselect:
2343 case CMD_stselect:
2344 case CMD_ptselect:
2345 case CMD_tjump:
2346 case CMD_stjump:
2347 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002348 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002349 xp->xp_context = EXPAND_TAGS_LISTFILES;
2350 else
2351 xp->xp_context = EXPAND_TAGS;
2352 xp->xp_pattern = arg;
2353 break;
2354 case CMD_augroup:
2355 xp->xp_context = EXPAND_AUGROUP;
2356 xp->xp_pattern = arg;
2357 break;
2358#ifdef FEAT_SYN_HL
2359 case CMD_syntax:
2360 set_context_in_syntax_cmd(xp, arg);
2361 break;
2362#endif
2363#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002364 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002365 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002366 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002367 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002368 case CMD_if:
2369 case CMD_elseif:
2370 case CMD_while:
2371 case CMD_for:
2372 case CMD_echo:
2373 case CMD_echon:
2374 case CMD_execute:
2375 case CMD_echomsg:
2376 case CMD_echoerr:
2377 case CMD_call:
2378 case CMD_return:
2379 case CMD_cexpr:
2380 case CMD_caddexpr:
2381 case CMD_cgetexpr:
2382 case CMD_lexpr:
2383 case CMD_laddexpr:
2384 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002385 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002386 break;
2387
2388 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002389 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002390 case CMD_function:
2391 case CMD_delfunction:
2392 xp->xp_context = EXPAND_USER_FUNC;
2393 xp->xp_pattern = arg;
2394 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002395 case CMD_disassemble:
2396 set_context_in_disassemble_cmd(xp, arg);
2397 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002398
2399 case CMD_echohl:
2400 set_context_in_echohl_cmd(xp, arg);
2401 break;
2402#endif
2403 case CMD_highlight:
2404 set_context_in_highlight_cmd(xp, arg);
2405 break;
2406#ifdef FEAT_CSCOPE
2407 case CMD_cscope:
2408 case CMD_lcscope:
2409 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002410 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002411 break;
2412#endif
2413#ifdef FEAT_SIGNS
2414 case CMD_sign:
2415 set_context_in_sign_cmd(xp, arg);
2416 break;
2417#endif
2418 case CMD_bdelete:
2419 case CMD_bwipeout:
2420 case CMD_bunload:
2421 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2422 arg = xp->xp_pattern + 1;
2423 // FALLTHROUGH
2424 case CMD_buffer:
2425 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002426 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002427 case CMD_checktime:
2428 xp->xp_context = EXPAND_BUFFERS;
2429 xp->xp_pattern = arg;
2430 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002431#ifdef FEAT_DIFF
2432 case CMD_diffget:
2433 case CMD_diffput:
2434 // If current buffer is in diff mode, complete buffer names
2435 // which are in diff mode, and different than current buffer.
2436 xp->xp_context = EXPAND_DIFF_BUFFERS;
2437 xp->xp_pattern = arg;
2438 break;
2439#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002440 case CMD_USER:
2441 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002442 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2443 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002444
2445 case CMD_map: case CMD_noremap:
2446 case CMD_nmap: case CMD_nnoremap:
2447 case CMD_vmap: case CMD_vnoremap:
2448 case CMD_omap: case CMD_onoremap:
2449 case CMD_imap: case CMD_inoremap:
2450 case CMD_cmap: case CMD_cnoremap:
2451 case CMD_lmap: case CMD_lnoremap:
2452 case CMD_smap: case CMD_snoremap:
2453 case CMD_tmap: case CMD_tnoremap:
2454 case CMD_xmap: case CMD_xnoremap:
2455 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002456 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002457 case CMD_unmap:
2458 case CMD_nunmap:
2459 case CMD_vunmap:
2460 case CMD_ounmap:
2461 case CMD_iunmap:
2462 case CMD_cunmap:
2463 case CMD_lunmap:
2464 case CMD_sunmap:
2465 case CMD_tunmap:
2466 case CMD_xunmap:
2467 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002468 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002469 case CMD_mapclear:
2470 case CMD_nmapclear:
2471 case CMD_vmapclear:
2472 case CMD_omapclear:
2473 case CMD_imapclear:
2474 case CMD_cmapclear:
2475 case CMD_lmapclear:
2476 case CMD_smapclear:
2477 case CMD_tmapclear:
2478 case CMD_xmapclear:
2479 xp->xp_context = EXPAND_MAPCLEAR;
2480 xp->xp_pattern = arg;
2481 break;
2482
2483 case CMD_abbreviate: case CMD_noreabbrev:
2484 case CMD_cabbrev: case CMD_cnoreabbrev:
2485 case CMD_iabbrev: case CMD_inoreabbrev:
2486 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002487 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002488 case CMD_unabbreviate:
2489 case CMD_cunabbrev:
2490 case CMD_iunabbrev:
2491 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002492 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002493#ifdef FEAT_MENU
2494 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2495 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2496 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2497 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2498 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2499 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2500 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2501 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2502 case CMD_tmenu: case CMD_tunmenu:
2503 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2504 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2505#endif
2506
2507 case CMD_colorscheme:
2508 xp->xp_context = EXPAND_COLORS;
2509 xp->xp_pattern = arg;
2510 break;
2511
2512 case CMD_compiler:
2513 xp->xp_context = EXPAND_COMPILER;
2514 xp->xp_pattern = arg;
2515 break;
2516
2517 case CMD_ownsyntax:
2518 xp->xp_context = EXPAND_OWNSYNTAX;
2519 xp->xp_pattern = arg;
2520 break;
2521
2522 case CMD_setfiletype:
2523 xp->xp_context = EXPAND_FILETYPE;
2524 xp->xp_pattern = arg;
2525 break;
2526
2527 case CMD_packadd:
2528 xp->xp_context = EXPAND_PACKADD;
2529 xp->xp_pattern = arg;
2530 break;
2531
zeertzjqb0d45ec2023-01-25 15:04:22 +00002532 case CMD_runtime:
2533 set_context_in_runtime_cmd(xp, arg);
2534 break;
2535
Bram Moolenaard0190392019-08-23 21:17:35 +02002536#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2537 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002538 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002539#endif
2540#if defined(FEAT_PROFILE)
2541 case CMD_profile:
2542 set_context_in_profile_cmd(xp, arg);
2543 break;
2544#endif
2545 case CMD_behave:
2546 xp->xp_context = EXPAND_BEHAVE;
2547 xp->xp_pattern = arg;
2548 break;
2549
2550 case CMD_messages:
2551 xp->xp_context = EXPAND_MESSAGES;
2552 xp->xp_pattern = arg;
2553 break;
2554
2555 case CMD_history:
2556 xp->xp_context = EXPAND_HISTORY;
2557 xp->xp_pattern = arg;
2558 break;
2559#if defined(FEAT_PROFILE)
2560 case CMD_syntime:
2561 xp->xp_context = EXPAND_SYNTIME;
2562 xp->xp_pattern = arg;
2563 break;
2564#endif
2565
2566 case CMD_argdelete:
2567 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2568 arg = xp->xp_pattern + 1;
2569 xp->xp_context = EXPAND_ARGLIST;
2570 xp->xp_pattern = arg;
2571 break;
2572
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002573#ifdef FEAT_EVAL
2574 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002575 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002576 case CMD_breakdel:
2577 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002578
2579 case CMD_scriptnames:
2580 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002581#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002582 case CMD_filetype:
2583 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002584
Bram Moolenaard0190392019-08-23 21:17:35 +02002585 default:
2586 break;
2587 }
2588 return NULL;
2589}
2590
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002591/*
2592 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2593 * we don't need/want deleted. Maybe this could be done better if we didn't
2594 * repeat all this stuff. The only problem is that they may not stay
2595 * perfectly compatible with each other, but then the command line syntax
2596 * probably won't change that much -- webb.
2597 */
2598 static char_u *
2599set_one_cmd_context(
2600 expand_T *xp,
2601 char_u *buff) // buffer for command string
2602{
2603 char_u *p;
2604 char_u *cmd, *arg;
2605 int len = 0;
2606 exarg_T ea;
2607 int compl = EXPAND_NOTHING;
2608 int forceit = FALSE;
2609 int usefilter = FALSE; // filter instead of file name
2610
2611 ExpandInit(xp);
2612 xp->xp_pattern = buff;
2613 xp->xp_line = buff;
2614 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2615 ea.argt = 0;
2616
2617 // 1. skip comment lines and leading space, colons or bars
2618 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2619 ;
2620 xp->xp_pattern = cmd;
2621
2622 if (*cmd == NUL)
2623 return NULL;
2624 if (*cmd == '"') // ignore comment lines
2625 {
2626 xp->xp_context = EXPAND_NOTHING;
2627 return NULL;
2628 }
2629
2630 // 3. Skip over the range to find the command.
2631 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2632 xp->xp_pattern = cmd;
2633 if (*cmd == NUL)
2634 return NULL;
2635 if (*cmd == '"')
2636 {
2637 xp->xp_context = EXPAND_NOTHING;
2638 return NULL;
2639 }
2640
2641 if (*cmd == '|' || *cmd == '\n')
2642 return cmd + 1; // There's another command
2643
2644 // Get the command index.
2645 p = set_cmd_index(cmd, &ea, xp, &compl);
2646 if (p == NULL)
2647 return NULL;
2648
2649 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2650
2651 if (*p == '!') // forced commands
2652 {
2653 forceit = TRUE;
2654 ++p;
2655 }
2656
2657 // 6. parse arguments
2658 if (!IS_USER_CMDIDX(ea.cmdidx))
2659 ea.argt = excmd_get_argt(ea.cmdidx);
2660
2661 arg = skipwhite(p);
2662
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002663 // Does command allow "++argopt" argument?
2664 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002665 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002666 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2667 {
2668 p = arg + 2;
2669 while (*p && !vim_isspace(*p))
2670 MB_PTR_ADV(p);
2671
2672 // Still touching the command after "++"?
2673 if (*p == NUL)
2674 {
2675 if (ea.argt & EX_ARGOPT)
2676 return set_context_in_argopt(xp, arg + 2);
2677#ifdef FEAT_TERMINAL
2678 if (ea.cmdidx == CMD_terminal)
2679 return set_context_in_terminalopt(xp, arg + 2);
2680#endif
2681 }
2682
2683 arg = skipwhite(p);
2684 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002685 }
2686
2687 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2688 {
2689 if (*arg == '>') // append
2690 {
2691 if (*++arg == '>')
2692 ++arg;
2693 arg = skipwhite(arg);
2694 }
2695 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2696 {
2697 ++arg;
2698 usefilter = TRUE;
2699 }
2700 }
2701
2702 if (ea.cmdidx == CMD_read)
2703 {
2704 usefilter = forceit; // :r! filter if forced
2705 if (*arg == '!') // :r !filter
2706 {
2707 ++arg;
2708 usefilter = TRUE;
2709 }
2710 }
2711
2712 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2713 {
2714 while (*arg == *cmd) // allow any number of '>' or '<'
2715 ++arg;
2716 arg = skipwhite(arg);
2717 }
2718
2719 // Does command allow "+command"?
2720 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2721 {
2722 // Check if we're in the +command
2723 p = arg + 1;
2724 arg = skip_cmd_arg(arg, FALSE);
2725
2726 // Still touching the command after '+'?
2727 if (*arg == NUL)
2728 return p;
2729
2730 // Skip space(s) after +command to get to the real argument
2731 arg = skipwhite(arg);
2732 }
2733
2734
2735 // Check for '|' to separate commands and '"' to start comments.
2736 // Don't do this for ":read !cmd" and ":write !cmd".
2737 if ((ea.argt & EX_TRLBAR) && !usefilter)
2738 {
2739 p = arg;
2740 // ":redir @" is not the start of a comment
2741 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2742 p += 2;
2743 while (*p)
2744 {
2745 if (*p == Ctrl_V)
2746 {
2747 if (p[1] != NUL)
2748 ++p;
2749 }
2750 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2751 || *p == '|' || *p == '\n')
2752 {
2753 if (*(p - 1) != '\\')
2754 {
2755 if (*p == '|' || *p == '\n')
2756 return p + 1;
2757 return NULL; // It's a comment
2758 }
2759 }
2760 MB_PTR_ADV(p);
2761 }
2762 }
2763
2764 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2765 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2766 // no arguments allowed but there is something
2767 return NULL;
2768
2769 // Find start of last argument (argument just before cursor):
2770 p = buff;
2771 xp->xp_pattern = p;
2772 len = (int)STRLEN(buff);
2773 while (*p && p < buff + len)
2774 {
2775 if (*p == ' ' || *p == TAB)
2776 {
2777 // argument starts after a space
2778 xp->xp_pattern = ++p;
2779 }
2780 else
2781 {
2782 if (*p == '\\' && *(p + 1) != NUL)
2783 ++p; // skip over escaped character
2784 MB_PTR_ADV(p);
2785 }
2786 }
2787
2788 if (ea.argt & EX_XFILE)
2789 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2790
2791 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002792 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002793 forceit);
2794}
2795
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002796/*
2797 * Set the completion context in 'xp' for command 'str'
2798 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002799 void
2800set_cmd_context(
2801 expand_T *xp,
2802 char_u *str, // start of command line
2803 int len, // length of command line (excl. NUL)
2804 int col, // position of cursor
2805 int use_ccline UNUSED) // use ccline for info
2806{
2807#ifdef FEAT_EVAL
2808 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002809 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002810#endif
2811 int old_char = NUL;
2812 char_u *nextcomm;
2813
2814 // Avoid a UMR warning from Purify, only save the character if it has been
2815 // written before.
2816 if (col < len)
2817 old_char = str[col];
2818 str[col] = NUL;
2819 nextcomm = str;
2820
2821#ifdef FEAT_EVAL
2822 if (use_ccline && ccline->cmdfirstc == '=')
2823 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002824 // pass CMD_SIZE because there is no real command
2825 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002826 }
2827 else if (use_ccline && ccline->input_fn)
2828 {
2829 xp->xp_context = ccline->xp_context;
2830 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002831 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002832 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2833 {
2834 context = xp->xp_context;
2835 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2836 &context);
2837 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002838 }
2839 else
2840#endif
2841 while (nextcomm != NULL)
2842 nextcomm = set_one_cmd_context(xp, nextcomm);
2843
2844 // Store the string here so that call_user_expand_func() can get to them
2845 // easily.
2846 xp->xp_line = str;
2847 xp->xp_col = col;
2848
2849 str[col] = old_char;
2850}
2851
2852/*
2853 * Expand the command line "str" from context "xp".
2854 * "xp" must have been set by set_cmd_context().
2855 * xp->xp_pattern points into "str", to where the text that is to be expanded
2856 * starts.
2857 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2858 * cursor.
2859 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2860 * key that triggered expansion literally.
2861 * Returns EXPAND_OK otherwise.
2862 */
2863 int
2864expand_cmdline(
2865 expand_T *xp,
2866 char_u *str, // start of command line
2867 int col, // position of cursor
2868 int *matchcount, // return: nr of matches
2869 char_u ***matches) // return: array of pointers to matches
2870{
2871 char_u *file_str = NULL;
2872 int options = WILD_ADD_SLASH|WILD_SILENT;
2873
2874 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2875 {
2876 beep_flush();
2877 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2878 }
2879 if (xp->xp_context == EXPAND_NOTHING)
2880 {
2881 // Caller can use the character as a normal char instead
2882 return EXPAND_NOTHING;
2883 }
2884
2885 // add star to file name, or convert to regexp if not exp. files.
2886 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002887 if (cmdline_fuzzy_completion_supported(xp))
2888 // If fuzzy matching, don't modify the search string
2889 file_str = vim_strsave(xp->xp_pattern);
2890 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002891 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
John Marriott1be5b372025-06-23 19:57:29 +02002892 if (file_str == NULL)
2893 return EXPAND_UNSUCCESSFUL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002894
2895 if (p_wic)
2896 options += WILD_ICASE;
2897
2898 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002899 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002900 {
2901 *matchcount = 0;
2902 *matches = NULL;
2903 }
2904 vim_free(file_str);
2905
2906 return EXPAND_OK;
2907}
2908
Bram Moolenaar66b51422019-08-18 21:44:12 +02002909/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002910 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002911 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002912 */
2913 static int
2914expand_files_and_dirs(
2915 expand_T *xp,
2916 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002917 char_u ***matches,
2918 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002919 int flags,
2920 int options)
2921{
2922 int free_pat = FALSE;
2923 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002924 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002925
2926 // for ":set path=" and ":set tags=" halve backslashes for escaped
2927 // space
2928 if (xp->xp_backslash != XP_BS_NONE)
2929 {
2930 free_pat = TRUE;
2931 pat = vim_strsave(pat);
2932 for (i = 0; pat[i]; ++i)
2933 if (pat[i] == '\\')
2934 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002935 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002936 && pat[i + 1] == '\\'
2937 && pat[i + 2] == '\\'
2938 && pat[i + 3] == ' ')
2939 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002940 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002941 && pat[i + 1] == ' ')
2942 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002943 else if ((xp->xp_backslash & XP_BS_COMMA)
2944 && pat[i + 1] == '\\'
2945 && pat[i + 2] == ',')
2946 STRMOVE(pat + i, pat + i + 2);
2947#ifdef BACKSLASH_IN_FILENAME
2948 else if ((xp->xp_backslash & XP_BS_COMMA)
2949 && pat[i + 1] == ',')
2950 STRMOVE(pat + i, pat + i + 1);
2951#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002952 }
2953 }
2954
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002955 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002956 {
2957#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002958 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002959#endif
2960 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002961 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002962 {
2963 if (xp->xp_context == EXPAND_FILES)
2964 flags |= EW_FILE;
2965 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2966 flags |= (EW_FILE | EW_PATH);
2967 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2968 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2969 else
2970 flags = (flags | EW_DIR) & ~EW_FILE;
2971 if (options & WILD_ICASE)
2972 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002973
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002974 // Expand wildcards, supporting %:h and the like.
2975 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2976 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002977 if (free_pat)
2978 vim_free(pat);
2979#ifdef BACKSLASH_IN_FILENAME
2980 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2981 {
2982 int j;
2983
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002984 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002986 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002987
2988 while (*ptr != NUL)
2989 {
2990 if (p_csl[0] == 's' && *ptr == '\\')
2991 *ptr = '/';
2992 else if (p_csl[0] == 'b' && *ptr == '/')
2993 *ptr = '\\';
2994 ptr += (*mb_ptr2len)(ptr);
2995 }
2996 }
2997 }
2998#endif
2999 return ret;
3000}
3001
3002/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003003 * Function given to ExpandGeneric() to obtain the possible arguments of the
3004 * ":behave {mswin,xterm}" command.
3005 */
3006 static char_u *
3007get_behave_arg(expand_T *xp UNUSED, int idx)
3008{
3009 if (idx == 0)
3010 return (char_u *)"mswin";
3011 if (idx == 1)
3012 return (char_u *)"xterm";
3013 return NULL;
3014}
3015
Christian Brabandta3422aa2025-04-23 21:04:24 +02003016/*
3017 * Function given to ExpandGeneric() to obtain the possible arguments of the
3018 * ":filetype {plugin,indent}" command.
3019 */
3020 static char_u *
3021get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3022{
3023 char *opts_all[] = {"indent", "plugin", "on", "off"};
3024 char *opts_plugin[] = {"plugin", "on", "off"};
3025 char *opts_indent[] = {"indent", "on", "off"};
3026 char *opts_onoff[] = {"on", "off"};
3027
3028 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
3029 return (char_u *)opts_all[idx];
3030 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
3031 return (char_u *)opts_plugin[idx];
3032 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
3033 return (char_u *)opts_indent[idx];
3034 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
3035 return (char_u *)opts_onoff[idx];
3036 return NULL;
3037}
3038
K.Takata161b6ac2022-11-14 15:31:07 +00003039#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003040/*
3041 * Function given to ExpandGeneric() to obtain the possible arguments of the
3042 * ":breakadd {expr, file, func, here}" command.
3043 * ":breakdel {func, file, here}" command.
3044 */
3045 static char_u *
3046get_breakadd_arg(expand_T *xp UNUSED, int idx)
3047{
3048 char *opts[] = {"expr", "file", "func", "here"};
3049
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003050 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003051 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003052 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003053 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3054 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003055 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3056 {
3057 // breakdel {func, file, here}
3058 if (idx <= 2)
3059 return (char_u *)opts[idx + 1];
3060 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003061 else
3062 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003063 // profdel {func, file}
3064 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003065 return (char_u *)opts[idx + 1];
3066 }
3067 }
3068 return NULL;
3069}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003070
3071/*
3072 * Function given to ExpandGeneric() to obtain the possible arguments for the
3073 * ":scriptnames" command.
3074 */
3075 static char_u *
3076get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3077{
3078 scriptitem_T *si;
3079
3080 if (!SCRIPT_ID_VALID(idx + 1))
3081 return NULL;
3082
3083 si = SCRIPT_ITEM(idx + 1);
3084 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3085 return NameBuff;
3086}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003087#endif
3088
Bram Moolenaard0190392019-08-23 21:17:35 +02003089/*
3090 * Function given to ExpandGeneric() to obtain the possible arguments of the
3091 * ":messages {clear}" command.
3092 */
3093 static char_u *
3094get_messages_arg(expand_T *xp UNUSED, int idx)
3095{
3096 if (idx == 0)
3097 return (char_u *)"clear";
3098 return NULL;
3099}
3100
3101 static char_u *
3102get_mapclear_arg(expand_T *xp UNUSED, int idx)
3103{
3104 if (idx == 0)
3105 return (char_u *)"<buffer>";
3106 return NULL;
3107}
3108
3109/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003110 * Do the expansion based on xp->xp_context and 'rmp'.
3111 */
3112 static int
3113ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003114 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003115 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003116 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003117 char_u ***matches,
3118 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003119{
3120 static struct expgen
3121 {
3122 int context;
3123 char_u *((*func)(expand_T *, int));
3124 int ic;
3125 int escaped;
3126 } tab[] =
3127 {
3128 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3129 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003130 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003131 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3132 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3133 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3134 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3135 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3136 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3137 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3138 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003139#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003140 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3141 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3142 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3143 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3144 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003145#endif
3146#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003147 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3148 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003149#endif
3150#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003151 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003152#endif
3153#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003154 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003155#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003156 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3157 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3158 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003159#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003160 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003161#endif
3162#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003163 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003164#endif
3165#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003166 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003167#endif
3168#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003169 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3170 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003171#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003172 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3173 {EXPAND_USER, get_users, TRUE, FALSE},
3174 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003175#ifdef FEAT_EVAL
3176 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003177 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003178#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003179 };
3180 int i;
3181 int ret = FAIL;
3182
3183 // Find a context in the table and call the ExpandGeneric() with the
3184 // right function to do the expansion.
3185 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3186 {
3187 if (xp->xp_context == tab[i].context)
3188 {
3189 if (tab[i].ic)
3190 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003191 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3192 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003193 break;
3194 }
3195 }
3196
3197 return ret;
3198}
3199
3200/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003201 * Map wild expand options to flags for expand_wildcards()
3202 */
3203 static int
3204map_wildopts_to_ewflags(int options)
3205{
3206 int flags;
3207
3208 flags = EW_DIR; // include directories
3209 if (options & WILD_LIST_NOTFOUND)
3210 flags |= EW_NOTFOUND;
3211 if (options & WILD_ADD_SLASH)
3212 flags |= EW_ADDSLASH;
3213 if (options & WILD_KEEP_ALL)
3214 flags |= EW_KEEPALL;
3215 if (options & WILD_SILENT)
3216 flags |= EW_SILENT;
3217 if (options & WILD_NOERROR)
3218 flags |= EW_NOERROR;
3219 if (options & WILD_ALLLINKS)
3220 flags |= EW_ALLLINKS;
3221
3222 return flags;
3223}
3224
3225/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226 * Do the expansion based on xp->xp_context and "pat".
3227 */
3228 static int
3229ExpandFromContext(
3230 expand_T *xp,
3231 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003232 char_u ***matches,
3233 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234 int options) // WILD_ flags
3235{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003236 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003237 int ret;
3238 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003239 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003240 int fuzzy = cmdline_fuzzy_complete(pat)
3241 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003243 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244
3245 if (xp->xp_context == EXPAND_FILES
3246 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003247 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003248 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003249 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003250 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3251 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003253 *matches = (char_u **)"";
3254 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255 if (xp->xp_context == EXPAND_HELP)
3256 {
3257 // With an empty argument we would get all the help tags, which is
3258 // very slow. Get matches for "help" instead.
3259 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003260 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 {
3262#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003263 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264#endif
3265 return OK;
3266 }
3267 return FAIL;
3268 }
3269
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003273 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003275 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003276#ifdef FEAT_DIFF
3277 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003278 return ExpandBufnames(pat, numMatches, matches,
3279 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003280#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281 if (xp->xp_context == EXPAND_TAGS
3282 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003283 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3284 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 if (xp->xp_context == EXPAND_COLORS)
3286 {
3287 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003288 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003289 directories);
3290 }
3291 if (xp->xp_context == EXPAND_COMPILER)
3292 {
3293 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003294 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003295 }
3296 if (xp->xp_context == EXPAND_OWNSYNTAX)
3297 {
3298 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003299 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 }
3301 if (xp->xp_context == EXPAND_FILETYPE)
3302 {
3303 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003304 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003305 }
Doug Kearns81642d92024-01-04 22:37:44 +01003306#ifdef FEAT_KEYMAP
3307 if (xp->xp_context == EXPAND_KEYMAP)
3308 {
3309 char *directories[] = {"keymap", NULL};
3310 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3311 }
3312#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003313#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003314 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003315 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003316#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003318 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003319 if (xp->xp_context == EXPAND_RUNTIME)
3320 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003322 // When expanding a function name starting with s:, match the <SNR>nr_
3323 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003324 if ((xp->xp_context == EXPAND_USER_FUNC
3325 || xp->xp_context == EXPAND_DISASSEMBLE)
3326 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003327 {
3328 int len = (int)STRLEN(pat) + 20;
3329
3330 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003331 if (tofree == NULL)
3332 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003333 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003334 pat = tofree;
3335 }
3336
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003337 if (!fuzzy)
3338 {
3339 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3340 if (regmatch.regprog == NULL)
3341 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003343 // set ignore-case according to p_ic, p_scs and pat
3344 regmatch.rm_ic = ignorecase(pat);
3345 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003346
3347 if (xp->xp_context == EXPAND_SETTINGS
3348 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003349 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003350 else if (xp->xp_context == EXPAND_STRING_SETTING)
3351 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3352 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3353 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003355 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003356 else if (xp->xp_context == EXPAND_ARGOPT)
3357 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003358 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3359 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003360#if defined(FEAT_TERMINAL)
3361 else if (xp->xp_context == EXPAND_TERMINALOPT)
3362 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3363#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003364#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003365 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003366 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003367#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003369 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003371 if (!fuzzy)
3372 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003373 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374
3375 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003376}
3377
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003378 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003380 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381 expand_T *xp,
3382 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003383 char_u ***matches,
3384 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003385 char_u *((*func)(expand_T *, int)),
3386 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003387 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003388{
Yee Cheng China7b81202025-02-23 09:32:47 +01003389 return ExpandGenericExt(
3390 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3391}
3392
3393/*
3394 * Expand a list of names.
3395 *
3396 * Generic function for command line completion. It calls a function to
3397 * obtain strings, one by one. The strings are matched against a regexp
3398 * program. Matching strings are copied into an array, which is returned.
3399 *
3400 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3401 * is used.
3402 *
3403 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3404 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3405 * sorting.
3406 *
3407 * Returns OK when no problems encountered, FAIL for error (out of memory).
3408 */
3409 int
3410ExpandGenericExt(
3411 char_u *pat,
3412 expand_T *xp,
3413 regmatch_T *regmatch,
3414 char_u ***matches,
3415 int *numMatches,
3416 char_u *((*func)(expand_T *, int)),
3417 // returns a string from the list
3418 int escaped,
3419 int sortStartIdx)
3420{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003421 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003422 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003423 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003424 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003425 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003426 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003427 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003428 int sort_matches = FALSE;
3429 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003430 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003431
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003432 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003433 *matches = NULL;
3434 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003435
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003436 if (!fuzzy)
3437 ga_init2(&ga, sizeof(char *), 30);
3438 else
3439 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3440
3441 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003443 str = (*func)(xp, i);
3444 if (str == NULL) // end of list
3445 break;
3446 if (*str == NUL) // skip empty strings
3447 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003448
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003449 if (xp->xp_pattern[0] != NUL)
3450 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003451 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003452 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003453 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003455 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003456 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003457 }
3458 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003459 else
3460 match = TRUE;
3461
3462 if (!match)
3463 continue;
3464
3465 if (escaped)
3466 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3467 else
3468 str = vim_strsave(str);
3469 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003470 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003471 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003473 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003474 return FAIL;
3475 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003476 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003477 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003479
3480 if (ga_grow(&ga, 1) == FAIL)
3481 {
3482 vim_free(str);
3483 break;
3484 }
3485
3486 if (fuzzy)
3487 {
3488 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3489 fuzmatch->idx = ga.ga_len;
3490 fuzmatch->str = str;
3491 fuzmatch->score = score;
3492 }
3493 else
3494 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3495
K.Takata161b6ac2022-11-14 15:31:07 +00003496#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003497 if (func == get_menu_names)
3498 {
3499 // test for separator added by get_menu_names()
3500 str += STRLEN(str) - 1;
3501 if (*str == '\001')
3502 *str = '.';
3503 }
K.Takata161b6ac2022-11-14 15:31:07 +00003504#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003505
Yee Cheng China7b81202025-02-23 09:32:47 +01003506 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3507 {
3508 // Found first item to start sorting from. This is usually 0.
3509 sortStartMatchIdx = ga.ga_len;
3510 }
3511
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003512 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003513 }
3514
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003515 if (ga.ga_len == 0)
3516 return OK;
3517
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003518 // sort the matches when using regular expression matching and sorting
3519 // applies to the completion context. Menus and scriptnames should be kept
3520 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003521 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003522 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003523 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003524 && xp->xp_context != EXPAND_SCRIPTNAMES
3525 && xp->xp_context != EXPAND_ARGOPT
3526 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003527 sort_matches = TRUE;
3528
3529 // <SNR> functions should be sorted to the end.
3530 if (xp->xp_context == EXPAND_EXPRESSION
3531 || xp->xp_context == EXPAND_FUNCTIONS
3532 || xp->xp_context == EXPAND_USER_FUNC
3533 || xp->xp_context == EXPAND_DISASSEMBLE)
3534 funcsort = TRUE;
3535
3536 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003537 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003539 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003540 // <SNR> functions should be sorted to the end.
3541 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3542 sort_func_compare);
3543 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003544 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003545 }
3546
3547 if (!fuzzy)
3548 {
3549 *matches = ga.ga_data;
3550 *numMatches = ga.ga_len;
3551 }
3552 else
3553 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003554 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3555 funcsort) == FAIL)
3556 return FAIL;
3557 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003558 }
3559
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003560#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003561 // Reset the variables used for special highlight names expansion, so that
3562 // they don't show up when getting normal highlight names by ID.
3563 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003564#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003565
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 return OK;
3567}
3568
3569/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003570 * Expand shell command matches in one directory of $PATH.
3571 */
3572 static void
3573expand_shellcmd_onedir(
3574 char_u *buf,
3575 char_u *s,
3576 size_t l,
3577 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003578 char_u ***matches,
3579 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003580 int flags,
3581 hashtab_T *ht,
3582 garray_T *gap)
3583{
3584 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003585 hash_T hash;
3586 hashitem_T *hi;
3587
3588 vim_strncpy(buf, s, l);
3589 add_pathsep(buf);
3590 l = STRLEN(buf);
3591 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3592
3593 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003594 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003595 if (ret != OK)
3596 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003597
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003598 if (ga_grow(gap, *numMatches) == FAIL)
3599 {
3600 FreeWild(*numMatches, *matches);
3601 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003602 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003603
3604 for (int i = 0; i < *numMatches; ++i)
3605 {
3606 char_u *name = (*matches)[i];
3607
3608 if (STRLEN(name) > l)
3609 {
3610 // Check if this name was already found.
3611 hash = hash_hash(name + l);
3612 hi = hash_lookup(ht, name + l, hash);
3613 if (HASHITEM_EMPTY(hi))
3614 {
3615 // Remove the path that was prepended.
3616 STRMOVE(name, name + l);
3617 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3618 hash_add_item(ht, hi, name, hash);
3619 name = NULL;
3620 }
3621 }
3622 vim_free(name);
3623 }
3624 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003625}
3626
3627/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 * Complete a shell command.
3629 * Returns FAIL or OK;
3630 */
3631 static int
3632expand_shellcmd(
3633 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003634 char_u ***matches, // return: array with matches
3635 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636 int flagsarg) // EW_ flags
3637{
3638 char_u *pat;
3639 int i;
3640 char_u *path = NULL;
3641 int mustfree = FALSE;
3642 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003643 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 size_t l;
3645 char_u *s, *e;
3646 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003647 int did_curdir = FALSE;
3648 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003650 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003651 if (buf == NULL)
3652 return FAIL;
3653
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003654 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003655 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003656 if (pat == NULL)
3657 {
3658 vim_free(buf);
3659 return FAIL;
3660 }
3661
Bram Moolenaar66b51422019-08-18 21:44:12 +02003662 for (i = 0; pat[i]; ++i)
3663 if (pat[i] == '\\' && pat[i + 1] == ' ')
3664 STRMOVE(pat + i, pat + i + 1);
3665
3666 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3667
3668 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3669 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3670 path = (char_u *)".";
3671 else
3672 {
3673 // For an absolute name we don't use $PATH.
3674 if (!mch_isFullName(pat))
3675 path = vim_getenv((char_u *)"PATH", &mustfree);
3676 if (path == NULL)
3677 path = (char_u *)"";
3678 }
3679
3680 // Go over all directories in $PATH. Expand matches in that directory and
3681 // collect them in "ga". When "." is not in $PATH also expand for the
3682 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003683 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003684 hash_init(&found_ht);
3685 for (s = path; ; s = e)
3686 {
K.Takata161b6ac2022-11-14 15:31:07 +00003687#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003689#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003690 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003691#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003692 if (e == NULL)
3693 e = s + STRLEN(s);
3694
3695 if (*s == NUL)
3696 {
3697 if (did_curdir)
3698 break;
3699 // Find directories in the current directory, path is empty.
3700 did_curdir = TRUE;
3701 flags |= EW_DIR;
3702 }
3703 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3704 {
3705 did_curdir = TRUE;
3706 flags |= EW_DIR;
3707 }
3708 else
3709 // Do not match directories inside a $PATH item.
3710 flags &= ~EW_DIR;
3711
3712 l = e - s;
3713 if (l > MAXPATHL - 5)
3714 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003716 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003717 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003718
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719 if (*e != NUL)
3720 ++e;
3721 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003722 *matches = ga.ga_data;
3723 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724
3725 vim_free(buf);
3726 vim_free(pat);
3727 if (mustfree)
3728 vim_free(path);
3729 hash_clear(&found_ht);
3730 return OK;
3731}
3732
K.Takata161b6ac2022-11-14 15:31:07 +00003733#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003734/*
3735 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003736 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003737 */
3738 static void *
3739call_user_expand_func(
3740 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003741 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003742{
3743 cmdline_info_T *ccline = get_cmdline_info();
3744 int keep = 0;
3745 typval_T args[4];
3746 sctx_T save_current_sctx = current_sctx;
3747 char_u *pat = NULL;
3748 void *ret;
3749
3750 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3751 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752
3753 if (ccline->cmdbuff != NULL)
3754 {
3755 keep = ccline->cmdbuff[ccline->cmdlen];
3756 ccline->cmdbuff[ccline->cmdlen] = 0;
3757 }
3758
3759 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3760
3761 args[0].v_type = VAR_STRING;
3762 args[0].vval.v_string = pat;
3763 args[1].v_type = VAR_STRING;
3764 args[1].vval.v_string = xp->xp_line;
3765 args[2].v_type = VAR_NUMBER;
3766 args[2].vval.v_number = xp->xp_col;
3767 args[3].v_type = VAR_UNKNOWN;
3768
3769 current_sctx = xp->xp_script_ctx;
3770
3771 ret = user_expand_func(xp->xp_arg, 3, args);
3772
3773 current_sctx = save_current_sctx;
3774 if (ccline->cmdbuff != NULL)
3775 ccline->cmdbuff[ccline->cmdlen] = keep;
3776
3777 vim_free(pat);
3778 return ret;
3779}
3780
3781/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003782 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3783 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003784 */
3785 static int
3786ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003787 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003788 expand_T *xp,
3789 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003790 char_u ***matches,
3791 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003792{
3793 char_u *retstr;
3794 char_u *s;
3795 char_u *e;
3796 int keep;
3797 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003798 int fuzzy;
3799 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003800 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003801
3802 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003803 *matches = NULL;
3804 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003805
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003806 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 if (retstr == NULL)
3808 return FAIL;
3809
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003810 if (!fuzzy)
3811 ga_init2(&ga, sizeof(char *), 3);
3812 else
3813 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3814
Bram Moolenaar66b51422019-08-18 21:44:12 +02003815 for (s = retstr; *s != NUL; s = e)
3816 {
3817 e = vim_strchr(s, '\n');
3818 if (e == NULL)
3819 e = s + STRLEN(s);
3820 keep = *e;
3821 *e = NUL;
3822
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003823 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003824 {
3825 if (!fuzzy)
3826 match = vim_regexec(regmatch, s, (colnr_T)0);
3827 else
3828 {
3829 score = fuzzy_match_str(s, pat);
3830 match = (score != 0);
3831 }
3832 }
3833 else
3834 match = TRUE; // match everything
3835
Bram Moolenaar66b51422019-08-18 21:44:12 +02003836 *e = keep;
3837
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003838 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003839 {
3840 if (ga_grow(&ga, 1) == FAIL)
3841 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003842 if (!fuzzy)
3843 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3844 else
3845 {
3846 fuzmatch_str_T *fuzmatch =
3847 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003848 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003849 fuzmatch->str = vim_strnsave(s, e - s);
3850 fuzmatch->score = score;
3851 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003852 ++ga.ga_len;
3853 }
3854
3855 if (*e != NUL)
3856 ++e;
3857 }
3858 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003859
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003860 if (ga.ga_len == 0)
3861 return OK;
3862
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003863 if (!fuzzy)
3864 {
3865 *matches = ga.ga_data;
3866 *numMatches = ga.ga_len;
3867 }
3868 else
3869 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003870 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3871 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003872 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003873 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003874 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003875 return OK;
3876}
3877
3878/*
3879 * Expand names with a list returned by a function defined by the user.
3880 */
3881 static int
3882ExpandUserList(
3883 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003884 char_u ***matches,
3885 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003886{
3887 list_T *retlist;
3888 listitem_T *li;
3889 garray_T ga;
3890
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003891 *matches = NULL;
3892 *numMatches = 0;
3893 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003894 if (retlist == NULL)
3895 return FAIL;
3896
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003897 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003898 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003899 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003900 {
3901 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3902 continue; // Skip non-string items and empty strings
3903
3904 if (ga_grow(&ga, 1) == FAIL)
3905 break;
3906
3907 ((char_u **)ga.ga_data)[ga.ga_len] =
3908 vim_strsave(li->li_tv.vval.v_string);
3909 ++ga.ga_len;
3910 }
3911 list_unref(retlist);
3912
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003913 *matches = ga.ga_data;
3914 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003915 return OK;
3916}
K.Takata161b6ac2022-11-14 15:31:07 +00003917#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003918
3919/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003920 * Expand "file" for all comma-separated directories in "path".
3921 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003922 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003923 */
3924 void
3925globpath(
3926 char_u *path,
3927 char_u *file,
3928 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003929 int expand_options,
3930 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003931{
3932 expand_T xpc;
3933 char_u *buf;
3934 int i;
3935 int num_p;
3936 char_u **p;
3937
3938 buf = alloc(MAXPATHL);
3939 if (buf == NULL)
3940 return;
3941
3942 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003943 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003944
3945 // Loop over all entries in {path}.
3946 while (*path != NUL)
3947 {
3948 // Copy one item of the path to buf[] and concatenate the file name.
3949 copy_option_part(&path, buf, MAXPATHL, ",");
3950 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3951 {
K.Takata161b6ac2022-11-14 15:31:07 +00003952#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003953 // Using the platform's path separator (\) makes vim incorrectly
3954 // treat it as an escape character, use '/' instead.
3955 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3956 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003957#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003958 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003959#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003960 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003961 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003962 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3963 {
3964 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3965
3966 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003967 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003968 for (i = 0; i < num_p; ++i)
3969 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003970 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003971 ++ga->ga_len;
3972 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003973 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003974 }
3975 }
3976 }
3977
3978 vim_free(buf);
3979}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003980
Bram Moolenaareadee482020-09-04 15:37:31 +02003981/*
3982 * Translate some keys pressed when 'wildmenu' is used.
3983 */
3984 int
3985wildmenu_translate_key(
3986 cmdline_info_T *cclp,
3987 int key,
3988 expand_T *xp,
3989 int did_wild_list)
3990{
3991 int c = key;
3992
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003993 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003994 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003995 // When the popup menu is used for cmdline completion:
3996 // Up : go to the previous item in the menu
3997 // Down : go to the next item in the menu
3998 // Left : go to the parent directory
3999 // Right: list the files in the selected directory
4000 switch (c)
4001 {
4002 case K_UP: c = K_LEFT; break;
4003 case K_DOWN: c = K_RIGHT; break;
4004 case K_LEFT: c = K_UP; break;
4005 case K_RIGHT: c = K_DOWN; break;
4006 default: break;
4007 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004008 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004009
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004010 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004011 {
4012 if (c == K_LEFT)
4013 c = Ctrl_P;
4014 else if (c == K_RIGHT)
4015 c = Ctrl_N;
4016 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004017
Bram Moolenaareadee482020-09-04 15:37:31 +02004018 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004019 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004020 && cclp->cmdpos > 1
4021 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4022 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4023 && (c == '\n' || c == '\r' || c == K_KENTER))
4024 c = K_DOWN;
4025
4026 return c;
4027}
4028
4029/*
4030 * Delete characters on the command line, from "from" to the current
4031 * position.
4032 */
4033 static void
4034cmdline_del(cmdline_info_T *cclp, int from)
4035{
4036 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4037 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4038 cclp->cmdlen -= cclp->cmdpos - from;
4039 cclp->cmdpos = from;
4040}
4041
4042/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004043 * Handle a key pressed when the wild menu for the menu names
4044 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004045 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004046 static int
4047wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004048{
Bram Moolenaareadee482020-09-04 15:37:31 +02004049 int i;
4050 int j;
4051
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004052 // Hitting <Down> after "emenu Name.": complete submenu
4053 if (key == K_DOWN && cclp->cmdpos > 0
4054 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004055 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004056 key = p_wc;
4057 KeyTyped = TRUE; // in case the key was mapped
4058 }
4059 else if (key == K_UP)
4060 {
4061 // Hitting <Up>: Remove one submenu name in front of the
4062 // cursor
4063 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004064
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004065 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4066 i = 0;
4067 while (--j > 0)
4068 {
4069 // check for start of menu name
4070 if (cclp->cmdbuff[j] == ' '
4071 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004072 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004073 i = j + 1;
4074 break;
4075 }
4076 // check for start of submenu name
4077 if (cclp->cmdbuff[j] == '.'
4078 && cclp->cmdbuff[j - 1] != '\\')
4079 {
4080 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004081 {
4082 i = j + 1;
4083 break;
4084 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004085 else
4086 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004087 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004088 }
4089 if (i > 0)
4090 cmdline_del(cclp, i);
4091 key = p_wc;
4092 KeyTyped = TRUE; // in case the key was mapped
4093 xp->xp_context = EXPAND_NOTHING;
4094 }
4095
4096 return key;
4097}
4098
4099/*
4100 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4101 * directory names (EXPAND_DIRECTORIES) or shell command names
4102 * (EXPAND_SHELLCMD) is displayed.
4103 */
4104 static int
4105wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4106{
4107 int i;
4108 int j;
4109 char_u upseg[5];
4110
4111 upseg[0] = PATHSEP;
4112 upseg[1] = '.';
4113 upseg[2] = '.';
4114 upseg[3] = PATHSEP;
4115 upseg[4] = NUL;
4116
4117 if (key == K_DOWN
4118 && cclp->cmdpos > 0
4119 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4120 && (cclp->cmdpos < 3
4121 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4122 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4123 {
4124 // go down a directory
4125 key = p_wc;
4126 KeyTyped = TRUE; // in case the key was mapped
4127 }
4128 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4129 {
4130 // If in a direct ancestor, strip off one ../ to go down
4131 int found = FALSE;
4132
4133 j = cclp->cmdpos;
4134 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4135 while (--j > i)
4136 {
4137 if (has_mbyte)
4138 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4139 if (vim_ispathsep(cclp->cmdbuff[j]))
4140 {
4141 found = TRUE;
4142 break;
4143 }
4144 }
4145 if (found
4146 && cclp->cmdbuff[j - 1] == '.'
4147 && cclp->cmdbuff[j - 2] == '.'
4148 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4149 {
4150 cmdline_del(cclp, j - 2);
4151 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004152 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004153 }
4154 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004155 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004156 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004157 // go up a directory
4158 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004159
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004160 j = cclp->cmdpos - 1;
4161 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4162 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004163 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004164 if (has_mbyte)
4165 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4166 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004167#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004168 && vim_strchr((char_u *)" *?[{`$%#",
4169 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004170#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004171 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004172 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004173 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004174 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004175 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004176 break;
4177 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004178 else
4179 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004180 }
4181 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004182
4183 if (!found)
4184 j = i;
4185 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4186 j += 4;
4187 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4188 && j == i)
4189 j += 3;
4190 else
4191 j = 0;
4192 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004193 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004194 // TODO this is only for DOS/UNIX systems - need to put in
4195 // machine-specific stuff here and in upseg init
4196 cmdline_del(cclp, j);
4197 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004198 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004199 else if (cclp->cmdpos > i)
4200 cmdline_del(cclp, i);
4201
4202 // Now complete in the new directory. Set KeyTyped in case the
4203 // Up key came from a mapping.
4204 key = p_wc;
4205 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004206 }
4207
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004208 return key;
4209}
4210
4211/*
4212 * Handle a key pressed when the wild menu is displayed
4213 */
4214 int
4215wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4216{
4217 if (xp->xp_context == EXPAND_MENUNAMES)
4218 return wildmenu_process_key_menunames(cclp, key, xp);
4219 else if ((xp->xp_context == EXPAND_FILES
4220 || xp->xp_context == EXPAND_DIRECTORIES
4221 || xp->xp_context == EXPAND_SHELLCMD))
4222 return wildmenu_process_key_filenames(cclp, key, xp);
4223
4224 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004225}
4226
4227/*
4228 * Free expanded names when finished walking through the matches
4229 */
4230 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004231wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004232{
4233 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004234
4235 if (!p_wmnu || wild_menu_showing == 0)
4236 return;
4237
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004238#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004239 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004240 if (cclp->input_fn)
4241 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004242#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004243
4244 if (wild_menu_showing == WM_SCROLLED)
4245 {
4246 // Entered command line, move it up
4247 cmdline_row--;
4248 redrawcmd();
4249 }
4250 else if (save_p_ls != -1)
4251 {
4252 // restore 'laststatus' and 'winminheight'
4253 p_ls = save_p_ls;
4254 p_wmh = save_p_wmh;
4255 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004256 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004257 redrawcmd();
4258 save_p_ls = -1;
4259 }
4260 else
4261 {
4262 win_redraw_last_status(topframe);
4263 redraw_statuslines();
4264 }
4265 KeyTyped = skt;
4266 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004267#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004268 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004269 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004270#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004271}
Bram Moolenaareadee482020-09-04 15:37:31 +02004272
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004273#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004274/*
4275 * "getcompletion()" function
4276 */
4277 void
4278f_getcompletion(typval_T *argvars, typval_T *rettv)
4279{
4280 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004281 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004282 expand_T xpc;
4283 int filtered = FALSE;
4284 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004285 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004286
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004287 if (in_vim9script()
4288 && (check_for_string_arg(argvars, 0) == FAIL
4289 || check_for_string_arg(argvars, 1) == FAIL
4290 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4291 return;
4292
ii144785fe02021-11-21 12:13:56 +00004293 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004294 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004295 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004296 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004297
4298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004299 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004300
4301 if (p_wic)
4302 options |= WILD_ICASE;
4303
4304 // For filtered results, 'wildignore' is used
4305 if (!filtered)
4306 options |= WILD_KEEP_ALL;
4307
4308 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004309 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004310 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004311 int cmdline_len = (int)STRLEN(pat);
4312 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004313 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004314 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004315 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004316 else
4317 {
ii144785fe02021-11-21 12:13:56 +00004318 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004319 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004320 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004321
4322 xpc.xp_context = cmdcomplete_str_to_type(type);
4323 if (xpc.xp_context == EXPAND_NOTHING)
4324 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004325 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004326 return;
4327 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004328
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004329 if (xpc.xp_context == EXPAND_USER_DEFINED)
4330 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004331 // Must be "custom,funcname" pattern
4332 if (STRNCMP(type, "custom,", 7) != 0)
4333 {
4334 semsg(_(e_invalid_argument_str), type);
4335 return;
4336 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004337
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004338 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004339 }
4340
4341 if (xpc.xp_context == EXPAND_USER_LIST)
4342 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004343 // Must be "customlist,funcname" pattern
4344 if (STRNCMP(type, "customlist,", 11) != 0)
4345 {
4346 semsg(_(e_invalid_argument_str), type);
4347 return;
4348 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004349
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004350 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004351 }
4352
Bram Moolenaar66b51422019-08-18 21:44:12 +02004353# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004354 if (xpc.xp_context == EXPAND_MENUS)
4355 {
4356 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4357 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4358 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004359# endif
4360# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004361 if (xpc.xp_context == EXPAND_CSCOPE)
4362 {
4363 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4364 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4365 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004366# endif
4367# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004368 if (xpc.xp_context == EXPAND_SIGN)
4369 {
4370 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4371 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4372 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004373# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004374 if (xpc.xp_context == EXPAND_RUNTIME)
4375 {
4376 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4377 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4378 }
zeertzjq85f36d62024-10-10 19:14:13 +02004379 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4380 {
4381 int context = EXPAND_SHELLCMDLINE;
4382 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4383 &context);
4384 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4385 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004386 if (xpc.xp_context == EXPAND_FILETYPECMD)
4387 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004388 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004389
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004390 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004391 // when fuzzy matching, don't modify the search string
4392 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004393 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004394 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004395
Bram Moolenaar93a10962022-06-16 11:42:09 +01004396 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004397 {
4398 int i;
4399
4400 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4401
4402 for (i = 0; i < xpc.xp_numfiles; i++)
4403 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4404 }
4405 vim_free(pat);
4406 ExpandCleanup(&xpc);
4407}
Girish Palya92f68e22025-04-21 11:12:41 +02004408
4409/*
4410 * "cmdcomplete_info()" function
4411 */
4412 void
4413f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4414{
4415 cmdline_info_T *ccline = get_cmdline_info();
4416 dict_T *retdict;
4417 list_T *li;
4418 int idx;
4419 int ret = OK;
4420
4421 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4422 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4423 return;
4424 retdict = rettv->vval.v_dict;
4425 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4426 if (ret == OK)
4427 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4428 if (ret == OK)
4429 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4430 if (ret == OK)
4431 {
4432 li = list_alloc();
4433 if (li == NULL)
4434 return;
4435 ret = dict_add_list(retdict, "matches", li);
4436 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4437 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4438 }
4439}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004440#endif // FEAT_EVAL