blob: a6d8c8539b1d42ebff5c1c0969e7ed0859618dcb [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 }
338 vim_free(p2);
339
340 redrawcmd();
341 cursorcmd();
342
343 // When expanding a ":map" command and no matches are found, assume that
344 // the key is supposed to be inserted literally
345 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
346 return FAIL;
347
348 if (xp->xp_numfiles <= 0 && p2 == NULL)
349 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100350 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200351 // free expanded pattern
352 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
353
354 return OK;
355}
356
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357/*
358 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000359 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000360 */
361 static int
362cmdline_pum_create(
363 cmdline_info_T *ccline,
364 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000365 char_u **matches,
366 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000367 int showtail)
368{
369 int i;
370 int columns;
371
372 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000373 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000375 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000376 {
zeertzjqc51a3762022-12-10 10:22:29 +0000377 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000378 compl_match_array[i].pum_info = NULL;
379 compl_match_array[i].pum_extra = NULL;
380 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200381 compl_match_array[i].pum_user_abbr_hlattr = -1;
382 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000383 }
384
385 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200386 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000387 columns = vim_strsize(xp->xp_pattern);
388 if (showtail)
389 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000390 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000391 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000392 }
glepnir977561a2025-02-13 20:48:56 +0100393 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000394
395 // no default selection
396 compl_selected = -1;
397
398 cmdline_pum_display();
399
400 return EXPAND_OK;
401}
402
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000403/*
404 * Display the cmdline completion matches in a popup menu
405 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000406 void
407cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408{
409 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
410}
411
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000412/*
413 * Returns TRUE if the cmdline completion popup menu is being displayed.
414 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000415 int
416cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100418 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000419}
420
421/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000422 * Remove the cmdline completion popup menu (if present), free the list of
423 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000425 void
zeertzjq1830e782025-03-13 20:29:13 +0100426cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000427{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000428 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100429 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100430#ifdef FEAT_EVAL
431 int save_RedrawingDisabled = RedrawingDisabled;
432 if (cclp->input_fn)
433 RedrawingDisabled = 0;
434#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000435
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000436 pum_undisplay();
437 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200438 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000439 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000440 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000441 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000442 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100443
444 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
445 // as a side effect.
446 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100447#ifdef FEAT_EVAL
448 if (cclp->input_fn)
449 RedrawingDisabled = save_RedrawingDisabled;
450#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000451}
452
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000453 void
454cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000455{
zeertzjq1830e782025-03-13 20:29:13 +0100456 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000457 wildmenu_cleanup(cclp);
458}
459
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000460/*
461 * Returns the starting column number to use for the cmdline completion popup
462 * menu.
463 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000464 int
465cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000466{
467 return compl_startcol;
468}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000469
Bram Moolenaar66b51422019-08-18 21:44:12 +0200470/*
zeertzjqd8c93402024-06-17 18:25:32 +0200471 * Returns the current cmdline completion pattern.
472 */
473 char_u *
474cmdline_compl_pattern(void)
475{
476 expand_T *xp = get_cmdline_info()->xpc;
477
478 return xp == NULL ? NULL : xp->xp_orig;
479}
480
481/*
482 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
483 */
484 int
485cmdline_compl_is_fuzzy(void)
486{
487 expand_T *xp = get_cmdline_info()->xpc;
488
489 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
490}
491
492/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000493 * Return the number of characters that should be skipped in a status match.
494 * These are backslashes used for escaping. Do show backslashes in help tags.
495 */
496 static int
497skip_status_match_char(expand_T *xp, char_u *s)
498{
499 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
500#ifdef FEAT_MENU
501 || ((xp->xp_context == EXPAND_MENUS
502 || xp->xp_context == EXPAND_MENUNAMES)
503 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
504#endif
505 )
506 {
507#ifndef BACKSLASH_IN_FILENAME
508 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
509 return 2;
510#endif
511 return 1;
512 }
513 return 0;
514}
515
516/*
517 * Get the length of an item as it will be shown in the status line.
518 */
519 static int
520status_match_len(expand_T *xp, char_u *s)
521{
522 int len = 0;
523
524#ifdef FEAT_MENU
525 int emenu = xp->xp_context == EXPAND_MENUS
526 || xp->xp_context == EXPAND_MENUNAMES;
527
528 // Check for menu separators - replace with '|'.
529 if (emenu && menu_is_separator(s))
530 return 1;
531#endif
532
533 while (*s != NUL)
534 {
535 s += skip_status_match_char(xp, s);
536 len += ptr2cells(s);
537 MB_PTR_ADV(s);
538 }
539
540 return len;
541}
542
543/*
544 * Show wildchar matches in the status line.
545 * Show at least the "match" item.
546 * We start at item 'first_match' in the list and show all matches that fit.
547 *
548 * If inversion is possible we use it. Else '=' characters are used.
549 */
550 static void
551win_redr_status_matches(
552 expand_T *xp,
553 int num_matches,
554 char_u **matches, // list of matches
555 int match,
556 int showtail)
557{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000558 int row;
559 char_u *buf;
560 int len;
561 int clen; // length in screen cells
562 int fillchar;
563 int attr;
564 int i;
565 int highlight = TRUE;
566 char_u *selstart = NULL;
567 int selstart_col = 0;
568 char_u *selend = NULL;
569 static int first_match = 0;
570 int add_left = FALSE;
571 char_u *s;
572#ifdef FEAT_MENU
573 int emenu;
574#endif
575 int l;
576
577 if (matches == NULL) // interrupted completion?
578 return;
579
580 if (has_mbyte)
581 buf = alloc(Columns * MB_MAXBYTES + 1);
582 else
583 buf = alloc(Columns + 1);
584 if (buf == NULL)
585 return;
586
587 if (match == -1) // don't show match but original text
588 {
589 match = 0;
590 highlight = FALSE;
591 }
592 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000593 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000594 if (match == 0)
595 first_match = 0;
596 else if (match < first_match)
597 {
598 // jumping left, as far as we can go
599 first_match = match;
600 add_left = TRUE;
601 }
602 else
603 {
604 // check if match fits on the screen
605 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000606 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000607 if (first_match > 0)
608 clen += 2;
609 // jumping right, put match at the left
610 if ((long)clen > Columns)
611 {
612 first_match = match;
613 // if showing the last match, we can add some on the left
614 clen = 2;
615 for (i = match; i < num_matches; ++i)
616 {
zeertzjqc51a3762022-12-10 10:22:29 +0000617 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000618 if ((long)clen >= Columns)
619 break;
620 }
621 if (i == num_matches)
622 add_left = TRUE;
623 }
624 }
625 if (add_left)
626 while (first_match > 0)
627 {
zeertzjqc51a3762022-12-10 10:22:29 +0000628 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000629 if ((long)clen >= Columns)
630 break;
631 --first_match;
632 }
633
634 fillchar = fillchar_status(&attr, curwin);
635
636 if (first_match == 0)
637 {
638 *buf = NUL;
639 len = 0;
640 }
641 else
642 {
643 STRCPY(buf, "< ");
644 len = 2;
645 }
646 clen = len;
647
648 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000649 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000650 {
651 if (i == match)
652 {
653 selstart = buf + len;
654 selstart_col = clen;
655 }
656
zeertzjqc51a3762022-12-10 10:22:29 +0000657 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000658 // Check for menu separators - replace with '|'
659#ifdef FEAT_MENU
660 emenu = (xp->xp_context == EXPAND_MENUS
661 || xp->xp_context == EXPAND_MENUNAMES);
662 if (emenu && menu_is_separator(s))
663 {
664 STRCPY(buf + len, transchar('|'));
665 l = (int)STRLEN(buf + len);
666 len += l;
667 clen += l;
668 }
669 else
670#endif
671 for ( ; *s != NUL; ++s)
672 {
673 s += skip_status_match_char(xp, s);
674 clen += ptr2cells(s);
675 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
676 {
677 STRNCPY(buf + len, s, l);
678 s += l - 1;
679 len += l;
680 }
681 else
682 {
683 STRCPY(buf + len, transchar_byte(*s));
684 len += (int)STRLEN(buf + len);
685 }
686 }
687 if (i == match)
688 selend = buf + len;
689
690 *(buf + len++) = ' ';
691 *(buf + len++) = ' ';
692 clen += 2;
693 if (++i == num_matches)
694 break;
695 }
696
697 if (i != num_matches)
698 {
699 *(buf + len++) = '>';
700 ++clen;
701 }
702
703 buf[len] = NUL;
704
705 row = cmdline_row - 1;
706 if (row >= 0)
707 {
708 if (wild_menu_showing == 0)
709 {
710 if (msg_scrolled > 0)
711 {
712 // Put the wildmenu just above the command line. If there is
713 // no room, scroll the screen one line up.
714 if (cmdline_row == Rows - 1)
715 {
716 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
717 ++msg_scrolled;
718 }
719 else
720 {
721 ++cmdline_row;
722 ++row;
723 }
724 wild_menu_showing = WM_SCROLLED;
725 }
726 else
727 {
728 // Create status line if needed by setting 'laststatus' to 2.
729 // Set 'winminheight' to zero to avoid that the window is
730 // resized.
731 if (lastwin->w_status_height == 0)
732 {
733 save_p_ls = p_ls;
734 save_p_wmh = p_wmh;
735 p_ls = 2;
736 p_wmh = 0;
737 last_status(FALSE);
738 }
739 wild_menu_showing = WM_SHOWN;
740 }
741 }
742
743 screen_puts(buf, row, 0, attr);
744 if (selstart != NULL && highlight)
745 {
746 *selend = NUL;
747 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
748 }
749
750 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
751 }
752
753 win_redraw_last_status(topframe);
754 vim_free(buf);
755}
756
757/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000758 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200759 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000760 */
761 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100762get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000763{
glepnir977561a2025-02-13 20:48:56 +0100764 int findex = xp->xp_selected;
765 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000766
zeertzjqb6c900b2025-02-14 17:59:31 +0100767 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000768 if (xp->xp_numfiles <= 0)
769 return NULL;
770
771 if (mode == WILD_PREV)
772 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100773 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000774 if (findex == -1)
775 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100776 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000777 --findex;
778 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000779 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000780 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100781 // Select the next entry
782 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000783 }
glepnir977561a2025-02-13 20:48:56 +0100784 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000785 {
glepnir977561a2025-02-13 20:48:56 +0100786 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
787 ht = pum_get_height();
788 if (ht > 3)
789 ht -= 2;
790
791 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000792 {
glepnir977561a2025-02-13 20:48:56 +0100793 if (findex == 0)
794 // at the first entry, don't select any entries
795 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100796 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100797 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000798 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100799 else
800 // go up by the pum height
801 findex = MAX(findex - ht, 0);
802 }
803 else // mode == WILD_PAGEDOWN
804 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100805 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100806 // at the last entry, don't select any entries
807 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100808 else if (findex < 0)
809 // no entry is selected, select the first entry
810 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100811 else
812 // go down by the pum height
813 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000814 }
815 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000816
glepnir977561a2025-02-13 20:48:56 +0100817 // Handle wrapping around
818 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000819 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100820 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100821 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823 else
glepnir977561a2025-02-13 20:48:56 +0100824 // Wrap around to opposite end
825 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000826 }
glepnir977561a2025-02-13 20:48:56 +0100827
828 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000829 if (compl_match_array)
830 {
831 compl_selected = findex;
832 cmdline_pum_display();
833 }
834 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100835 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
836 cmd_showtail);
837
zeertzjqe9ef3472023-08-17 23:57:05 +0200838 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100839 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100840 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000841}
842
843/*
844 * Start the command-line expansion and get the matches.
845 */
846 static char_u *
847ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
848{
849 int non_suf_match; // number without matching suffix
850 int i;
851 char_u *ss = NULL;
852
853 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000854 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000855 options) == FAIL)
856 {
857#ifdef FNAME_ILLEGAL
858 // Illegal file name has been silently skipped. But when there
859 // are wildcards, the real problem is that there was no match,
860 // causing the pattern to be added, which has illegal characters.
861 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
862 semsg(_(e_no_match_str_2), str);
863#endif
864 }
865 else if (xp->xp_numfiles == 0)
866 {
867 if (!(options & WILD_SILENT))
868 semsg(_(e_no_match_str_2), str);
869 }
870 else
871 {
872 // Escape the matches for use on the command line.
873 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
874
875 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000876 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000877 {
878 if (xp->xp_numfiles)
879 non_suf_match = xp->xp_numfiles;
880 else
881 non_suf_match = 1;
882 if ((xp->xp_context == EXPAND_FILES
883 || xp->xp_context == EXPAND_DIRECTORIES)
884 && xp->xp_numfiles > 1)
885 {
886 // More than one match; check suffix.
887 // The files will have been sorted on matching suffix in
888 // expand_wildcards, only need to check the first two.
889 non_suf_match = 0;
890 for (i = 0; i < 2; ++i)
891 if (match_suffix(xp->xp_files[i]))
892 ++non_suf_match;
893 }
894 if (non_suf_match != 1)
895 {
896 // Can we ever get here unless it's while expanding
897 // interactively? If not, we can get rid of this all
898 // together. Don't really want to wait for this message
899 // (and possibly have to hit return to continue!).
900 if (!(options & WILD_SILENT))
901 emsg(_(e_too_many_file_names));
902 else if (!(options & WILD_NO_BEEP))
903 beep_flush();
904 }
905 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
906 ss = vim_strsave(xp->xp_files[0]);
907 }
908 }
909
910 return ss;
911}
912
913/*
914 * Return the longest common part in the list of cmdline completion matches.
915 */
916 static char_u *
917find_longest_match(expand_T *xp, int options)
918{
919 long_u len;
920 int mb_len = 1;
921 int c0, ci;
922 int i;
923 char_u *ss;
924
925 for (len = 0; xp->xp_files[0][len]; len += mb_len)
926 {
927 if (has_mbyte)
928 {
929 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
930 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
931 }
932 else
933 c0 = xp->xp_files[0][len];
934 for (i = 1; i < xp->xp_numfiles; ++i)
935 {
936 if (has_mbyte)
937 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
938 else
939 ci = xp->xp_files[i][len];
940 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
941 || xp->xp_context == EXPAND_FILES
942 || xp->xp_context == EXPAND_SHELLCMD
943 || xp->xp_context == EXPAND_BUFFERS))
944 {
945 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
946 break;
947 }
948 else if (c0 != ci)
949 break;
950 }
951 if (i < xp->xp_numfiles)
952 {
953 if (!(options & WILD_NO_BEEP))
954 vim_beep(BO_WILD);
955 break;
956 }
957 }
958
959 ss = alloc(len + 1);
960 if (ss)
961 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
962
963 return ss;
964}
965
966/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000967 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200968 * Chars that should not be expanded must be preceded with a backslash.
969 * Return a pointer to allocated memory containing the new string.
970 * Return NULL for failure.
971 *
972 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200973 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
974 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200975 *
976 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
977 * is WILD_EXPAND_FREE or WILD_ALL.
978 *
979 * mode = WILD_FREE: just free previously expanded matches
980 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
981 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
982 * mode = WILD_NEXT: use next match in multiple match, wrap to first
983 * mode = WILD_PREV: use previous match in multiple match, wrap to first
984 * mode = WILD_ALL: return all matches concatenated
985 * mode = WILD_LONGEST: return longest matched part
986 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000987 * mode = WILD_APPLY: apply the item selected in the cmdline completion
988 * popup menu and close the menu.
989 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
990 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200991 *
992 * options = WILD_LIST_NOTFOUND: list entries without a match
993 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
994 * options = WILD_USE_NL: Use '\n' for WILD_ALL
995 * options = WILD_NO_BEEP: Don't beep for multiple matches
996 * options = WILD_ADD_SLASH: add a slash after directory names
997 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
998 * options = WILD_SILENT: don't print warning messages
999 * options = WILD_ESCAPE: put backslash before special chars
1000 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001001 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001002 *
1003 * The variables xp->xp_context and xp->xp_backslash must have been set!
1004 */
1005 char_u *
1006ExpandOne(
1007 expand_T *xp,
1008 char_u *str,
1009 char_u *orig, // allocated copy of original of expanded string
1010 int options,
1011 int mode)
1012{
1013 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001014 int orig_saved = FALSE;
1015 int i;
1016 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001017
1018 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001019 if (mode == WILD_NEXT || mode == WILD_PREV
1020 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001021 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001023 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001024 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001025 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001026 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001027 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001028 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001029
Bram Moolenaar66b51422019-08-18 21:44:12 +02001030 // free old names
1031 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1032 {
1033 FreeWild(xp->xp_numfiles, xp->xp_files);
1034 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001035 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001036
1037 // The entries from xp_files may be used in the PUM, remove it.
1038 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001039 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001040 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001041 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001042
1043 if (mode == WILD_FREE) // only release file name
1044 return NULL;
1045
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001046 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001047 {
zeertzjq28a23602023-09-29 19:58:35 +02001048 vim_free(xp->xp_orig);
1049 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001050 orig_saved = TRUE;
1051
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001052 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001053 }
1054
1055 // Find longest common part
1056 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1057 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001058 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001059 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 }
1061
Bram Moolenaar57e95172022-08-20 19:26:14 +01001062 // Concatenate all matching names. Unless interrupted, this can be slow
1063 // and the result probably won't be used.
1064 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001065 {
1066 len = 0;
1067 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001068 {
1069 if (i > 0)
1070 {
1071 if (xp->xp_prefix == XP_PREFIX_NO)
1072 len += 2; // prefix "no"
1073 else if (xp->xp_prefix == XP_PREFIX_INV)
1074 len += 3; // prefix "inv"
1075 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001077 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001078 ss = alloc(len);
1079 if (ss != NULL)
1080 {
1081 *ss = NUL;
1082 for (i = 0; i < xp->xp_numfiles; ++i)
1083 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001084 if (i > 0)
1085 {
1086 if (xp->xp_prefix == XP_PREFIX_NO)
1087 STRCAT(ss, "no");
1088 else if (xp->xp_prefix == XP_PREFIX_INV)
1089 STRCAT(ss, "inv");
1090 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001091 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001092
Bram Moolenaar66b51422019-08-18 21:44:12 +02001093 if (i != xp->xp_numfiles - 1)
1094 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1095 }
1096 }
1097 }
1098
1099 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1100 ExpandCleanup(xp);
1101
zeertzjq28a23602023-09-29 19:58:35 +02001102 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001103 if (!orig_saved)
1104 vim_free(orig);
1105
1106 return ss;
1107}
1108
1109/*
1110 * Prepare an expand structure for use.
1111 */
1112 void
1113ExpandInit(expand_T *xp)
1114{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001115 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001116 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001117 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001118 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001119}
1120
1121/*
1122 * Cleanup an expand structure after use.
1123 */
1124 void
1125ExpandCleanup(expand_T *xp)
1126{
1127 if (xp->xp_numfiles >= 0)
1128 {
1129 FreeWild(xp->xp_numfiles, xp->xp_files);
1130 xp->xp_numfiles = -1;
1131 }
zeertzjq28a23602023-09-29 19:58:35 +02001132 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001133}
1134
zeertzjqec270a52025-04-23 20:50:23 +02001135 void
1136clear_cmdline_orig(void)
1137{
1138 VIM_CLEAR(cmdline_orig);
1139}
1140
Bram Moolenaar66b51422019-08-18 21:44:12 +02001141/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001142 * Display one line of completion matches. Multiple matches are displayed in
1143 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001144 * matches - list of completion match names
1145 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001146 * lines - number of output lines
1147 * linenr - line number of matches to display
1148 * maxlen - maximum number of characters in each line
1149 * showtail - display only the tail of the full path of a file name
1150 * dir_attr - highlight attribute to use for directory names
1151 */
1152 static void
1153showmatches_oneline(
1154 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001155 char_u **matches,
1156 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001157 int lines,
1158 int linenr,
1159 int maxlen,
1160 int showtail,
1161 int dir_attr)
1162{
1163 int i, j;
1164 int isdir;
1165 int lastlen;
1166 char_u *p;
1167
1168 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001169 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001170 {
1171 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1172 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001173 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1174 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001175 msg_advance(maxlen + 1);
1176 msg_puts((char *)p);
1177 msg_advance(maxlen + 3);
1178 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1179 break;
1180 }
1181 for (i = maxlen - lastlen; --i >= 0; )
1182 msg_putchar(' ');
1183 if (xp->xp_context == EXPAND_FILES
1184 || xp->xp_context == EXPAND_SHELLCMD
1185 || xp->xp_context == EXPAND_BUFFERS)
1186 {
1187 // highlight directories
1188 if (xp->xp_numfiles != -1)
1189 {
1190 char_u *halved_slash;
1191 char_u *exp_path;
1192 char_u *path;
1193
1194 // Expansion was done before and special characters
1195 // were escaped, need to halve backslashes. Also
1196 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001197 exp_path = expand_env_save_opt(matches[j], TRUE);
1198 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001199 halved_slash = backslash_halve_save(path);
1200 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001201 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001202 vim_free(exp_path);
1203 if (halved_slash != path)
1204 vim_free(halved_slash);
1205 }
1206 else
1207 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001208 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001209 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001210 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001211 else
1212 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001213 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001214 TRUE);
1215 p = NameBuff;
1216 }
1217 }
1218 else
1219 {
1220 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001221 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001222 }
1223 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1224 }
1225 if (msg_col > 0) // when not wrapped around
1226 {
1227 msg_clr_eos();
1228 msg_putchar('\n');
1229 }
1230 out_flush(); // show one line at a time
1231}
1232
1233/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 * Show all matches for completion on the command line.
1235 * Returns EXPAND_NOTHING when the character that triggered expansion should
1236 * be inserted like a normal character.
1237 */
1238 int
1239showmatches(expand_T *xp, int wildmenu UNUSED)
1240{
1241 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001242 int numMatches;
1243 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001244 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001245 int maxlen;
1246 int lines;
1247 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 int attr;
1249 int showtail;
1250
Girish Palya92f68e22025-04-21 11:12:41 +02001251 // Save cmdline before expansion
1252 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001253 {
1254 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001255 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001256 }
Girish Palya92f68e22025-04-21 11:12:41 +02001257
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 if (xp->xp_numfiles == -1)
1259 {
1260 set_expand_context(xp);
1261 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001262 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 showtail = expand_showtail(xp);
1264 if (i != EXPAND_OK)
1265 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001266 }
1267 else
1268 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001269 numMatches = xp->xp_numfiles;
1270 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271 showtail = cmd_showtail;
1272 }
1273
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001274 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001275 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001276 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001277
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278 if (!wildmenu)
1279 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001280 msg_didany = FALSE; // lines_left will be set
1281 msg_start(); // prepare for paging
1282 msg_putchar('\n');
1283 out_flush();
1284 cmdline_row = msg_row;
1285 msg_didany = FALSE; // lines_left will be set again
1286 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001288
1289 if (got_int)
1290 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001292 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001293 else
1294 {
1295 // find the length of the longest file name
1296 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001297 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001298 {
1299 if (!showtail && (xp->xp_context == EXPAND_FILES
1300 || xp->xp_context == EXPAND_SHELLCMD
1301 || xp->xp_context == EXPAND_BUFFERS))
1302 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001303 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001304 j = vim_strsize(NameBuff);
1305 }
1306 else
zeertzjqc51a3762022-12-10 10:22:29 +00001307 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001308 if (j > maxlen)
1309 maxlen = j;
1310 }
1311
1312 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001313 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001314 else
1315 {
1316 // compute the number of columns and lines for the listing
1317 maxlen += 2; // two spaces between file names
1318 columns = ((int)Columns + 2) / maxlen;
1319 if (columns < 1)
1320 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001321 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001322 }
1323
1324 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1325
1326 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1327 {
1328 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1329 msg_clr_eos();
1330 msg_advance(maxlen - 3);
1331 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1332 }
1333
1334 // list the files line by line
1335 for (i = 0; i < lines; ++i)
1336 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001337 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001338 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001339 if (got_int)
1340 {
1341 got_int = FALSE;
1342 break;
1343 }
1344 }
1345
1346 // we redraw the command below the lines that we have just listed
1347 // This is a bit tricky, but it saves a lot of screen updating.
1348 cmdline_row = msg_row; // will put it back later
1349 }
1350
1351 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001352 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001353
1354 return EXPAND_OK;
1355}
1356
1357/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001358 * gettail() version for showmatches() and win_redr_status_matches():
1359 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001360 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001361 static char_u *
1362showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001363{
1364 char_u *p;
1365 char_u *t = s;
1366 int had_sep = FALSE;
1367
1368 for (p = s; *p != NUL; )
1369 {
1370 if (vim_ispathsep(*p)
1371#ifdef BACKSLASH_IN_FILENAME
1372 && !rem_backslash(p)
1373#endif
1374 )
1375 had_sep = TRUE;
1376 else if (had_sep)
1377 {
1378 t = p;
1379 had_sep = FALSE;
1380 }
1381 MB_PTR_ADV(p);
1382 }
1383 return t;
1384}
1385
1386/*
1387 * Return TRUE if we only need to show the tail of completion matches.
1388 * When not completing file names or there is a wildcard in the path FALSE is
1389 * returned.
1390 */
1391 static int
1392expand_showtail(expand_T *xp)
1393{
1394 char_u *s;
1395 char_u *end;
1396
1397 // When not completing file names a "/" may mean something different.
1398 if (xp->xp_context != EXPAND_FILES
1399 && xp->xp_context != EXPAND_SHELLCMD
1400 && xp->xp_context != EXPAND_DIRECTORIES)
1401 return FALSE;
1402
1403 end = gettail(xp->xp_pattern);
1404 if (end == xp->xp_pattern) // there is no path separator
1405 return FALSE;
1406
1407 for (s = xp->xp_pattern; s < end; s++)
1408 {
1409 // Skip escaped wildcards. Only when the backslash is not a path
1410 // separator, on DOS the '*' "path\*\file" must not be skipped.
1411 if (rem_backslash(s))
1412 ++s;
1413 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1414 return FALSE;
1415 }
1416 return TRUE;
1417}
1418
1419/*
1420 * Prepare a string for expansion.
1421 * When expanding file names: The string will be used with expand_wildcards().
1422 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1423 * When expanding other names: The string will be used with regcomp(). Copy
1424 * the name into allocated memory and prepend "^".
1425 */
1426 char_u *
1427addstar(
1428 char_u *fname,
1429 int len,
1430 int context) // EXPAND_FILES etc.
1431{
1432 char_u *retval;
1433 int i, j;
1434 int new_len;
1435 char_u *tail;
1436 int ends_in_star;
1437
1438 if (context != EXPAND_FILES
1439 && context != EXPAND_FILES_IN_PATH
1440 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001441 && context != EXPAND_DIRECTORIES
1442 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001443 {
1444 // Matching will be done internally (on something other than files).
1445 // So we convert the file-matching-type wildcards into our kind for
1446 // use with vim_regcomp(). First work out how long it will be:
1447
1448 // For help tags the translation is done in find_help_tags().
1449 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001450 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001451 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001452 || context == EXPAND_COLORS
1453 || context == EXPAND_COMPILER
1454 || context == EXPAND_OWNSYNTAX
1455 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001456 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001457 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001458 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001459 || ((context == EXPAND_TAGS_LISTFILES
1460 || context == EXPAND_TAGS)
1461 && fname[0] == '/'))
1462 retval = vim_strnsave(fname, len);
1463 else
1464 {
1465 new_len = len + 2; // +2 for '^' at start, NUL at end
1466 for (i = 0; i < len; i++)
1467 {
1468 if (fname[i] == '*' || fname[i] == '~')
1469 new_len++; // '*' needs to be replaced by ".*"
1470 // '~' needs to be replaced by "\~"
1471
1472 // Buffer names are like file names. "." should be literal
1473 if (context == EXPAND_BUFFERS && fname[i] == '.')
1474 new_len++; // "." becomes "\."
1475
1476 // Custom expansion takes care of special things, match
1477 // backslashes literally (perhaps also for other types?)
1478 if ((context == EXPAND_USER_DEFINED
1479 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1480 new_len++; // '\' becomes "\\"
1481 }
1482 retval = alloc(new_len);
1483 if (retval != NULL)
1484 {
1485 retval[0] = '^';
1486 j = 1;
1487 for (i = 0; i < len; i++, j++)
1488 {
1489 // Skip backslash. But why? At least keep it for custom
1490 // expansion.
1491 if (context != EXPAND_USER_DEFINED
1492 && context != EXPAND_USER_LIST
1493 && fname[i] == '\\'
1494 && ++i == len)
1495 break;
1496
1497 switch (fname[i])
1498 {
1499 case '*': retval[j++] = '.';
1500 break;
1501 case '~': retval[j++] = '\\';
1502 break;
1503 case '?': retval[j] = '.';
1504 continue;
1505 case '.': if (context == EXPAND_BUFFERS)
1506 retval[j++] = '\\';
1507 break;
1508 case '\\': if (context == EXPAND_USER_DEFINED
1509 || context == EXPAND_USER_LIST)
1510 retval[j++] = '\\';
1511 break;
1512 }
1513 retval[j] = fname[i];
1514 }
1515 retval[j] = NUL;
1516 }
1517 }
1518 }
1519 else
1520 {
1521 retval = alloc(len + 4);
1522 if (retval != NULL)
1523 {
1524 vim_strncpy(retval, fname, len);
1525
1526 // Don't add a star to *, ~, ~user, $var or `cmd`.
1527 // * would become **, which walks the whole tree.
1528 // ~ would be at the start of the file name, but not the tail.
1529 // $ could be anywhere in the tail.
1530 // ` could be anywhere in the file name.
1531 // When the name ends in '$' don't add a star, remove the '$'.
1532 tail = gettail(retval);
1533 ends_in_star = (len > 0 && retval[len - 1] == '*');
1534#ifndef BACKSLASH_IN_FILENAME
1535 for (i = len - 2; i >= 0; --i)
1536 {
1537 if (retval[i] != '\\')
1538 break;
1539 ends_in_star = !ends_in_star;
1540 }
1541#endif
1542 if ((*retval != '~' || tail != retval)
1543 && !ends_in_star
1544 && vim_strchr(tail, '$') == NULL
1545 && vim_strchr(retval, '`') == NULL)
1546 retval[len++] = '*';
1547 else if (len > 0 && retval[len - 1] == '$')
1548 --len;
1549 retval[len] = NUL;
1550 }
1551 }
1552 return retval;
1553}
1554
1555/*
1556 * Must parse the command line so far to work out what context we are in.
1557 * Completion can then be done based on that context.
1558 * This routine sets the variables:
1559 * xp->xp_pattern The start of the pattern to be expanded within
1560 * the command line (ends at the cursor).
1561 * xp->xp_context The type of thing to expand. Will be one of:
1562 *
1563 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1564 * the command line, like an unknown command. Caller
1565 * should beep.
1566 * EXPAND_NOTHING Unrecognised context for completion, use char like
1567 * a normal char, rather than for completion. eg
1568 * :s/^I/
1569 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1570 * it.
1571 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1572 * EXPAND_FILES After command with EX_XFILE set, or after setting
1573 * with P_EXPAND set. eg :e ^I, :w>>^I
1574 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001575 * when we know only directories are of interest.
1576 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001577 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1578 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1579 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1580 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1581 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1582 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1583 * EXPAND_EVENTS Complete event names
1584 * EXPAND_SYNTAX Complete :syntax command arguments
1585 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1586 * EXPAND_AUGROUP Complete autocommand group names
1587 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1588 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1589 * eg :unmap a^I , :cunab x^I
1590 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1591 * eg :call sub^I
1592 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1593 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1594 * names in expressions, eg :while s^I
1595 * EXPAND_ENV_VARS Complete environment variable names
1596 * EXPAND_USER Complete user names
1597 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001598 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001599set_expand_context(expand_T *xp)
1600{
1601 cmdline_info_T *ccline = get_cmdline_info();
1602
1603 // only expansion for ':', '>' and '=' command-lines
1604 if (ccline->cmdfirstc != ':'
1605#ifdef FEAT_EVAL
1606 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1607 && !ccline->input_fn
1608#endif
1609 )
1610 {
1611 xp->xp_context = EXPAND_NOTHING;
1612 return;
1613 }
1614 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1615}
1616
Bram Moolenaard0190392019-08-23 21:17:35 +02001617/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1619 * For user defined commands, the completion context is set in 'xp' and the
1620 * completion flags in 'complp'.
1621 *
1622 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001623 */
1624 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001625set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001626{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001627 char_u *p = NULL;
1628 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001629 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001630
1631 // Isolate the command and search for it in the command table.
1632 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001633 // - the 'k' command can directly be followed by any character, but do
1634 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1635 // find matches anywhere in the command name, do this only for command
1636 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001637 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001638 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001639 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001640 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001641 p = cmd + 1;
1642 }
1643 else
1644 {
1645 p = cmd;
1646 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1647 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001648 // A user command may contain digits.
1649 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1650 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001651 while (ASCII_ISALNUM(*p) || *p == '*')
1652 ++p;
1653 // for python 3.x: ":py3*" commands completion
1654 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1655 {
1656 ++p;
1657 while (ASCII_ISALPHA(*p) || *p == '*')
1658 ++p;
1659 }
1660 // check for non-alpha command
1661 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1662 ++p;
1663 len = (int)(p - cmd);
1664
1665 if (len == 0)
1666 {
1667 xp->xp_context = EXPAND_UNSUCCESSFUL;
1668 return NULL;
1669 }
1670
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001672
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001673 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001674 // Also when doing fuzzy expansion for non-shell commands, support
1675 // alphanumeric characters.
1676 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1677 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001678 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1679 ++p;
1680 }
1681
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001682 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001683 // character, complete the command name.
1684 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1685 return NULL;
1686
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001687 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001688 {
1689 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1690 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 p = cmd + 1;
1693 }
1694 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1695 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001696 eap->cmd = cmd;
1697 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001698 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001699 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001700 }
1701 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001702 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001703 {
1704 // Not still touching the command and it was an illegal one
1705 xp->xp_context = EXPAND_UNSUCCESSFUL;
1706 return NULL;
1707 }
1708
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001709 return p;
1710}
Bram Moolenaard0190392019-08-23 21:17:35 +02001711
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712/*
1713 * Set the completion context for a command argument with wild card characters.
1714 */
1715 static void
1716set_context_for_wildcard_arg(
1717 exarg_T *eap,
1718 char_u *arg,
1719 int usefilter,
1720 expand_T *xp,
1721 int *complp)
1722{
1723 char_u *p;
1724 int c;
1725 int in_quote = FALSE;
1726 char_u *bow = NULL; // Beginning of word
1727 int len = 0;
1728
1729 // Allow spaces within back-quotes to count as part of the argument
1730 // being expanded.
1731 xp->xp_pattern = skipwhite(arg);
1732 p = xp->xp_pattern;
1733 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001734 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001735 if (has_mbyte)
1736 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001737 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001738 c = *p;
1739 if (c == '\\' && p[1] != NUL)
1740 ++p;
1741 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001744 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001745 xp->xp_pattern = p;
1746 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001747 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001748 in_quote = !in_quote;
1749 }
1750 // An argument can contain just about everything, except
1751 // characters that end the command and white space.
1752 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001753#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001754 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001755#endif
1756 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001757 {
1758 len = 0; // avoid getting stuck when space is in 'isfname'
1759 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001760 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001761 if (has_mbyte)
1762 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001763 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001764 c = *p;
1765 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001766 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001767 if (has_mbyte)
1768 len = (*mb_ptr2len)(p);
1769 else
1770 len = 1;
1771 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001772 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001773 if (in_quote)
1774 bow = p;
1775 else
1776 xp->xp_pattern = p;
1777 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001778 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001779 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001780 }
1781
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001782 // If we are still inside the quotes, and we passed a space, just
1783 // expand from there.
1784 if (bow != NULL && in_quote)
1785 xp->xp_pattern = bow;
1786 xp->xp_context = EXPAND_FILES;
1787
1788 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001789 if (usefilter
1790 || (eap != NULL
1791 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1792 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001793 {
1794#ifndef BACKSLASH_IN_FILENAME
1795 xp->xp_shell = TRUE;
1796#endif
1797 // When still after the command name expand executables.
1798 if (xp->xp_pattern == skipwhite(arg))
1799 xp->xp_context = EXPAND_SHELLCMD;
1800 }
1801
1802 // Check for environment variable.
1803 if (*xp->xp_pattern == '$')
1804 {
1805 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1806 if (!vim_isIDc(*p))
1807 break;
1808 if (*p == NUL)
1809 {
1810 xp->xp_context = EXPAND_ENV_VARS;
1811 ++xp->xp_pattern;
1812 // Avoid that the assignment uses EXPAND_FILES again.
1813 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1814 *complp = EXPAND_ENV_VARS;
1815 }
1816 }
1817 // Check for user names.
1818 if (*xp->xp_pattern == '~')
1819 {
1820 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1821 ;
1822 // Complete ~user only if it partially matches a user name.
1823 // A full match ~user<Tab> will be replaced by user's home
1824 // directory i.e. something like ~user<Tab> -> /home/user/
1825 if (*p == NUL && p > xp->xp_pattern + 1
1826 && match_user(xp->xp_pattern + 1) >= 1)
1827 {
1828 xp->xp_context = EXPAND_USER;
1829 ++xp->xp_pattern;
1830 }
1831 }
1832}
1833
1834/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001835 * Set the completion context for the "++opt=arg" argument. Always returns
1836 * NULL.
1837 */
1838 static char_u *
1839set_context_in_argopt(expand_T *xp, char_u *arg)
1840{
1841 char_u *p;
1842
1843 p = vim_strchr(arg, '=');
1844 if (p == NULL)
1845 xp->xp_pattern = arg;
1846 else
1847 xp->xp_pattern = p + 1;
1848
1849 xp->xp_context = EXPAND_ARGOPT;
1850 return NULL;
1851}
1852
1853#ifdef FEAT_TERMINAL
1854/*
1855 * Set the completion context for :terminal's [options]. Always returns NULL.
1856 */
1857 static char_u *
1858set_context_in_terminalopt(expand_T *xp, char_u *arg)
1859{
1860 char_u *p;
1861
1862 p = vim_strchr(arg, '=');
1863 if (p == NULL)
1864 xp->xp_pattern = arg;
1865 else
1866 xp->xp_pattern = p + 1;
1867
1868 xp->xp_context = EXPAND_TERMINALOPT;
1869 return NULL;
1870}
1871#endif
1872
1873/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001874 * Set the completion context for the :filter command. Returns a pointer to the
1875 * next command after the :filter command.
1876 */
1877 static char_u *
1878set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1879{
1880 if (*arg != NUL)
1881 arg = skip_vimgrep_pat(arg, NULL, NULL);
1882 if (arg == NULL || *arg == NUL)
1883 {
1884 xp->xp_context = EXPAND_NOTHING;
1885 return NULL;
1886 }
1887 return skipwhite(arg);
1888}
1889
1890#ifdef FEAT_SEARCH_EXTRA
1891/*
1892 * Set the completion context for the :match command. Returns a pointer to the
1893 * next command after the :match command.
1894 */
1895 static char_u *
1896set_context_in_match_cmd(expand_T *xp, char_u *arg)
1897{
1898 if (*arg == NUL || !ends_excmd(*arg))
1899 {
1900 // also complete "None"
1901 set_context_in_echohl_cmd(xp, arg);
1902 arg = skipwhite(skiptowhite(arg));
1903 if (*arg != NUL)
1904 {
1905 xp->xp_context = EXPAND_NOTHING;
1906 arg = skip_regexp(arg + 1, *arg, magic_isset());
1907 }
1908 }
1909 return find_nextcmd(arg);
1910}
1911#endif
1912
1913/*
1914 * Returns a pointer to the next command after a :global or a :v command.
1915 * Returns NULL if there is no next command.
1916 */
1917 static char_u *
1918find_cmd_after_global_cmd(char_u *arg)
1919{
1920 int delim;
1921
1922 delim = *arg; // get the delimiter
1923 if (delim)
1924 ++arg; // skip delimiter if there is one
1925
1926 while (arg[0] != NUL && arg[0] != delim)
1927 {
1928 if (arg[0] == '\\' && arg[1] != NUL)
1929 ++arg;
1930 ++arg;
1931 }
1932 if (arg[0] != NUL)
1933 return arg + 1;
1934
1935 return NULL;
1936}
1937
1938/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001939 * Returns a pointer to the next command after a :substitute or a :& command.
1940 * Returns NULL if there is no next command.
1941 */
1942 static char_u *
1943find_cmd_after_substitute_cmd(char_u *arg)
1944{
1945 int delim;
1946
1947 delim = *arg;
1948 if (delim)
1949 {
1950 // skip "from" part
1951 ++arg;
1952 arg = skip_regexp(arg, delim, magic_isset());
1953
1954 if (arg[0] != NUL && arg[0] == delim)
1955 {
1956 // skip "to" part
1957 ++arg;
1958 while (arg[0] != NUL && arg[0] != delim)
1959 {
1960 if (arg[0] == '\\' && arg[1] != NUL)
1961 ++arg;
1962 ++arg;
1963 }
1964 if (arg[0] != NUL) // skip delimiter
1965 ++arg;
1966 }
1967 }
1968 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1969 ++arg;
1970 if (arg[0] != NUL)
1971 return arg;
1972
1973 return NULL;
1974}
1975
1976/*
1977 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1978 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1979 * Returns NULL if there is no next command.
1980 */
1981 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001982find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001983{
1984 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001985 if (*arg != '/')
1986 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001987
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001988 // Match regexp, not just whole words
1989 for (++arg; *arg && *arg != '/'; arg++)
1990 if (*arg == '\\' && arg[1] != NUL)
1991 arg++;
1992 if (*arg)
1993 {
1994 arg = skipwhite(arg + 1);
1995
1996 // Check for trailing illegal characters
1997 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1998 xp->xp_context = EXPAND_NOTHING;
1999 else
2000 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002001 }
2002
2003 return NULL;
2004}
2005
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002006#ifdef FEAT_EVAL
2007/*
2008 * Set the completion context for the :unlet command. Always returns NULL.
2009 */
2010 static char_u *
2011set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2012{
2013 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2014 arg = xp->xp_pattern + 1;
2015
2016 xp->xp_context = EXPAND_USER_VARS;
2017 xp->xp_pattern = arg;
2018
2019 if (*xp->xp_pattern == '$')
2020 {
2021 xp->xp_context = EXPAND_ENV_VARS;
2022 ++xp->xp_pattern;
2023 }
2024
2025 return NULL;
2026}
2027#endif
2028
2029#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2030/*
2031 * Set the completion context for the :language command. Always returns NULL.
2032 */
2033 static char_u *
2034set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2035{
2036 char_u *p;
2037
2038 p = skiptowhite(arg);
2039 if (*p == NUL)
2040 {
2041 xp->xp_context = EXPAND_LANGUAGE;
2042 xp->xp_pattern = arg;
2043 }
2044 else
2045 {
2046 if ( STRNCMP(arg, "messages", p - arg) == 0
2047 || STRNCMP(arg, "ctype", p - arg) == 0
2048 || STRNCMP(arg, "time", p - arg) == 0
2049 || STRNCMP(arg, "collate", p - arg) == 0)
2050 {
2051 xp->xp_context = EXPAND_LOCALES;
2052 xp->xp_pattern = skipwhite(p);
2053 }
2054 else
2055 xp->xp_context = EXPAND_NOTHING;
2056 }
2057
2058 return NULL;
2059}
2060#endif
2061
Christian Brabandta3422aa2025-04-23 21:04:24 +02002062static enum
2063{
2064 EXP_FILETYPECMD_ALL, // expand all :filetype values
2065 EXP_FILETYPECMD_PLUGIN, // expand plugin on off
2066 EXP_FILETYPECMD_INDENT, // expand indent on off
2067 EXP_FILETYPECMD_ONOFF, // expand on off
2068} filetype_expand_what;
2069
2070#define EXPAND_FILETYPECMD_PLUGIN 0x01
2071#define EXPAND_FILETYPECMD_INDENT 0x02
2072#define EXPAND_FILETYPECMD_ONOFF 0x04
2073
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002074#ifdef FEAT_EVAL
2075static enum
2076{
2077 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002078 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2079 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002080} breakpt_expand_what;
2081
2082/*
2083 * Set the completion context for the :breakadd command. Always returns NULL.
2084 */
2085 static char_u *
2086set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2087{
2088 char_u *p;
2089 char_u *subcmd_start;
2090
2091 xp->xp_context = EXPAND_BREAKPOINT;
2092 xp->xp_pattern = arg;
2093
2094 if (cmdidx == CMD_breakadd)
2095 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002096 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002097 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002098 else
2099 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002100
2101 p = skipwhite(arg);
2102 if (*p == NUL)
2103 return NULL;
2104 subcmd_start = p;
2105
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002106 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002107 {
2108 // :breakadd file [lnum] <filename>
2109 // :breakadd func [lnum] <funcname>
2110 p += 4;
2111 p = skipwhite(p);
2112
2113 // skip line number (if specified)
2114 if (VIM_ISDIGIT(*p))
2115 {
2116 p = skipdigits(p);
2117 if (*p != ' ')
2118 {
2119 xp->xp_context = EXPAND_NOTHING;
2120 return NULL;
2121 }
2122 p = skipwhite(p);
2123 }
2124 if (STRNCMP("file", subcmd_start, 4) == 0)
2125 xp->xp_context = EXPAND_FILES;
2126 else
2127 xp->xp_context = EXPAND_USER_FUNC;
2128 xp->xp_pattern = p;
2129 }
2130 else if (STRNCMP("expr ", p, 5) == 0)
2131 {
2132 // :breakadd expr <expression>
2133 xp->xp_context = EXPAND_EXPRESSION;
2134 xp->xp_pattern = skipwhite(p + 5);
2135 }
2136
2137 return NULL;
2138}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002139
2140 static char_u *
2141set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2142{
2143 char_u *p;
2144
2145 xp->xp_context = EXPAND_NOTHING;
2146 xp->xp_pattern = NULL;
2147
2148 p = skipwhite(arg);
2149 if (VIM_ISDIGIT(*p))
2150 return NULL;
2151
2152 xp->xp_context = EXPAND_SCRIPTNAMES;
2153 xp->xp_pattern = p;
2154
2155 return NULL;
2156}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002157#endif
2158
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002159/*
Christian Brabandta3422aa2025-04-23 21:04:24 +02002160 * Set the completion context for the :filetype command. Always returns NULL.
2161 */
2162 static char_u *
2163set_context_in_filetype_cmd(expand_T *xp, char_u *arg)
2164{
2165 char_u *p;
2166 int val = 0;
2167
2168 xp->xp_context = EXPAND_FILETYPECMD;
2169 xp->xp_pattern = arg;
2170 filetype_expand_what = EXP_FILETYPECMD_ALL;
2171
2172 p = skipwhite(arg);
2173 if (*p == NUL)
2174 return NULL;
2175
2176 for (;;)
2177 {
2178 if (STRNCMP(p, "plugin", 6) == 0)
2179 {
2180 val |= EXPAND_FILETYPECMD_PLUGIN;
2181 p = skipwhite(p + 6);
2182 continue;
2183 }
2184 if (STRNCMP(p, "indent", 6) == 0)
2185 {
2186 val |= EXPAND_FILETYPECMD_INDENT;
2187 p = skipwhite(p + 6);
2188 continue;
2189 }
2190 break;
2191 }
2192
2193 if ((val & EXPAND_FILETYPECMD_PLUGIN) && (val & EXPAND_FILETYPECMD_INDENT))
2194 filetype_expand_what = EXP_FILETYPECMD_ONOFF;
2195 else if ((val & EXPAND_FILETYPECMD_PLUGIN))
2196 filetype_expand_what = EXP_FILETYPECMD_INDENT;
2197 else if ((val & EXPAND_FILETYPECMD_INDENT))
2198 filetype_expand_what = EXP_FILETYPECMD_PLUGIN;
2199
2200 xp->xp_pattern = p;
2201
2202 return NULL;
2203}
2204
2205
2206/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002207 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2208 * The argument to the command is 'arg' and the argument flags is 'argt'.
2209 * For user-defined commands and for environment variables, 'compl' has the
2210 * completion type.
2211 * Returns a pointer to the next command. Returns NULL if there is no next
2212 * command.
2213 */
2214 static char_u *
2215set_context_by_cmdname(
2216 char_u *cmd,
2217 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002218 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002219 char_u *arg,
2220 long argt,
2221 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002222 int forceit)
2223{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002224 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002225 {
2226 case CMD_find:
2227 case CMD_sfind:
2228 case CMD_tabfind:
2229 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002230 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002231 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002232 break;
2233 case CMD_cd:
2234 case CMD_chdir:
2235 case CMD_tcd:
2236 case CMD_tchdir:
2237 case CMD_lcd:
2238 case CMD_lchdir:
2239 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002240 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002241 break;
2242 case CMD_help:
2243 xp->xp_context = EXPAND_HELP;
2244 xp->xp_pattern = arg;
2245 break;
2246
2247 // Command modifiers: return the argument.
2248 // Also for commands with an argument that is a command.
2249 case CMD_aboveleft:
2250 case CMD_argdo:
2251 case CMD_belowright:
2252 case CMD_botright:
2253 case CMD_browse:
2254 case CMD_bufdo:
2255 case CMD_cdo:
2256 case CMD_cfdo:
2257 case CMD_confirm:
2258 case CMD_debug:
2259 case CMD_folddoclosed:
2260 case CMD_folddoopen:
2261 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002262 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_keepalt:
2264 case CMD_keepjumps:
2265 case CMD_keepmarks:
2266 case CMD_keeppatterns:
2267 case CMD_ldo:
2268 case CMD_leftabove:
2269 case CMD_lfdo:
2270 case CMD_lockmarks:
2271 case CMD_noautocmd:
2272 case CMD_noswapfile:
2273 case CMD_rightbelow:
2274 case CMD_sandbox:
2275 case CMD_silent:
2276 case CMD_tab:
2277 case CMD_tabdo:
2278 case CMD_topleft:
2279 case CMD_verbose:
2280 case CMD_vertical:
2281 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002282 case CMD_vim9cmd:
2283 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002284 return arg;
2285
2286 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002287 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002288
2289#ifdef FEAT_SEARCH_EXTRA
2290 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002291 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002292#endif
2293
2294 // All completion for the +cmdline_compl feature goes here.
2295
2296 case CMD_command:
2297 return set_context_in_user_cmd(xp, arg);
2298
2299 case CMD_delcommand:
2300 xp->xp_context = EXPAND_USER_COMMANDS;
2301 xp->xp_pattern = arg;
2302 break;
2303
2304 case CMD_global:
2305 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002306 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002307 case CMD_and:
2308 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002309 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002310 case CMD_isearch:
2311 case CMD_dsearch:
2312 case CMD_ilist:
2313 case CMD_dlist:
2314 case CMD_ijump:
2315 case CMD_psearch:
2316 case CMD_djump:
2317 case CMD_isplit:
2318 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002319 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002320 case CMD_autocmd:
2321 return set_context_in_autocmd(xp, arg, FALSE);
2322 case CMD_doautocmd:
2323 case CMD_doautoall:
2324 return set_context_in_autocmd(xp, arg, TRUE);
2325 case CMD_set:
2326 set_context_in_set_cmd(xp, arg, 0);
2327 break;
2328 case CMD_setglobal:
2329 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2330 break;
2331 case CMD_setlocal:
2332 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2333 break;
2334 case CMD_tag:
2335 case CMD_stag:
2336 case CMD_ptag:
2337 case CMD_ltag:
2338 case CMD_tselect:
2339 case CMD_stselect:
2340 case CMD_ptselect:
2341 case CMD_tjump:
2342 case CMD_stjump:
2343 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002344 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002345 xp->xp_context = EXPAND_TAGS_LISTFILES;
2346 else
2347 xp->xp_context = EXPAND_TAGS;
2348 xp->xp_pattern = arg;
2349 break;
2350 case CMD_augroup:
2351 xp->xp_context = EXPAND_AUGROUP;
2352 xp->xp_pattern = arg;
2353 break;
2354#ifdef FEAT_SYN_HL
2355 case CMD_syntax:
2356 set_context_in_syntax_cmd(xp, arg);
2357 break;
2358#endif
2359#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002360 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002361 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002362 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002363 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002364 case CMD_if:
2365 case CMD_elseif:
2366 case CMD_while:
2367 case CMD_for:
2368 case CMD_echo:
2369 case CMD_echon:
2370 case CMD_execute:
2371 case CMD_echomsg:
2372 case CMD_echoerr:
2373 case CMD_call:
2374 case CMD_return:
2375 case CMD_cexpr:
2376 case CMD_caddexpr:
2377 case CMD_cgetexpr:
2378 case CMD_lexpr:
2379 case CMD_laddexpr:
2380 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002381 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002382 break;
2383
2384 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002385 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002386 case CMD_function:
2387 case CMD_delfunction:
2388 xp->xp_context = EXPAND_USER_FUNC;
2389 xp->xp_pattern = arg;
2390 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002391 case CMD_disassemble:
2392 set_context_in_disassemble_cmd(xp, arg);
2393 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002394
2395 case CMD_echohl:
2396 set_context_in_echohl_cmd(xp, arg);
2397 break;
2398#endif
2399 case CMD_highlight:
2400 set_context_in_highlight_cmd(xp, arg);
2401 break;
2402#ifdef FEAT_CSCOPE
2403 case CMD_cscope:
2404 case CMD_lcscope:
2405 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002406 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002407 break;
2408#endif
2409#ifdef FEAT_SIGNS
2410 case CMD_sign:
2411 set_context_in_sign_cmd(xp, arg);
2412 break;
2413#endif
2414 case CMD_bdelete:
2415 case CMD_bwipeout:
2416 case CMD_bunload:
2417 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2418 arg = xp->xp_pattern + 1;
2419 // FALLTHROUGH
2420 case CMD_buffer:
2421 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002422 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002423 case CMD_checktime:
2424 xp->xp_context = EXPAND_BUFFERS;
2425 xp->xp_pattern = arg;
2426 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002427#ifdef FEAT_DIFF
2428 case CMD_diffget:
2429 case CMD_diffput:
2430 // If current buffer is in diff mode, complete buffer names
2431 // which are in diff mode, and different than current buffer.
2432 xp->xp_context = EXPAND_DIFF_BUFFERS;
2433 xp->xp_pattern = arg;
2434 break;
2435#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002436 case CMD_USER:
2437 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002438 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2439 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002440
2441 case CMD_map: case CMD_noremap:
2442 case CMD_nmap: case CMD_nnoremap:
2443 case CMD_vmap: case CMD_vnoremap:
2444 case CMD_omap: case CMD_onoremap:
2445 case CMD_imap: case CMD_inoremap:
2446 case CMD_cmap: case CMD_cnoremap:
2447 case CMD_lmap: case CMD_lnoremap:
2448 case CMD_smap: case CMD_snoremap:
2449 case CMD_tmap: case CMD_tnoremap:
2450 case CMD_xmap: case CMD_xnoremap:
2451 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002452 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002453 case CMD_unmap:
2454 case CMD_nunmap:
2455 case CMD_vunmap:
2456 case CMD_ounmap:
2457 case CMD_iunmap:
2458 case CMD_cunmap:
2459 case CMD_lunmap:
2460 case CMD_sunmap:
2461 case CMD_tunmap:
2462 case CMD_xunmap:
2463 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002464 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002465 case CMD_mapclear:
2466 case CMD_nmapclear:
2467 case CMD_vmapclear:
2468 case CMD_omapclear:
2469 case CMD_imapclear:
2470 case CMD_cmapclear:
2471 case CMD_lmapclear:
2472 case CMD_smapclear:
2473 case CMD_tmapclear:
2474 case CMD_xmapclear:
2475 xp->xp_context = EXPAND_MAPCLEAR;
2476 xp->xp_pattern = arg;
2477 break;
2478
2479 case CMD_abbreviate: case CMD_noreabbrev:
2480 case CMD_cabbrev: case CMD_cnoreabbrev:
2481 case CMD_iabbrev: case CMD_inoreabbrev:
2482 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002483 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002484 case CMD_unabbreviate:
2485 case CMD_cunabbrev:
2486 case CMD_iunabbrev:
2487 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002488 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002489#ifdef FEAT_MENU
2490 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2491 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2492 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2493 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2494 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2495 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2496 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2497 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2498 case CMD_tmenu: case CMD_tunmenu:
2499 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2500 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2501#endif
2502
2503 case CMD_colorscheme:
2504 xp->xp_context = EXPAND_COLORS;
2505 xp->xp_pattern = arg;
2506 break;
2507
2508 case CMD_compiler:
2509 xp->xp_context = EXPAND_COMPILER;
2510 xp->xp_pattern = arg;
2511 break;
2512
2513 case CMD_ownsyntax:
2514 xp->xp_context = EXPAND_OWNSYNTAX;
2515 xp->xp_pattern = arg;
2516 break;
2517
2518 case CMD_setfiletype:
2519 xp->xp_context = EXPAND_FILETYPE;
2520 xp->xp_pattern = arg;
2521 break;
2522
2523 case CMD_packadd:
2524 xp->xp_context = EXPAND_PACKADD;
2525 xp->xp_pattern = arg;
2526 break;
2527
zeertzjqb0d45ec2023-01-25 15:04:22 +00002528 case CMD_runtime:
2529 set_context_in_runtime_cmd(xp, arg);
2530 break;
2531
Bram Moolenaard0190392019-08-23 21:17:35 +02002532#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2533 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002534 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002535#endif
2536#if defined(FEAT_PROFILE)
2537 case CMD_profile:
2538 set_context_in_profile_cmd(xp, arg);
2539 break;
2540#endif
2541 case CMD_behave:
2542 xp->xp_context = EXPAND_BEHAVE;
2543 xp->xp_pattern = arg;
2544 break;
2545
2546 case CMD_messages:
2547 xp->xp_context = EXPAND_MESSAGES;
2548 xp->xp_pattern = arg;
2549 break;
2550
2551 case CMD_history:
2552 xp->xp_context = EXPAND_HISTORY;
2553 xp->xp_pattern = arg;
2554 break;
2555#if defined(FEAT_PROFILE)
2556 case CMD_syntime:
2557 xp->xp_context = EXPAND_SYNTIME;
2558 xp->xp_pattern = arg;
2559 break;
2560#endif
2561
2562 case CMD_argdelete:
2563 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2564 arg = xp->xp_pattern + 1;
2565 xp->xp_context = EXPAND_ARGLIST;
2566 xp->xp_pattern = arg;
2567 break;
2568
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002569#ifdef FEAT_EVAL
2570 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002571 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002572 case CMD_breakdel:
2573 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002574
2575 case CMD_scriptnames:
2576 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002577#endif
Christian Brabandta3422aa2025-04-23 21:04:24 +02002578 case CMD_filetype:
2579 return set_context_in_filetype_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002580
Bram Moolenaard0190392019-08-23 21:17:35 +02002581 default:
2582 break;
2583 }
2584 return NULL;
2585}
2586
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002587/*
2588 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2589 * we don't need/want deleted. Maybe this could be done better if we didn't
2590 * repeat all this stuff. The only problem is that they may not stay
2591 * perfectly compatible with each other, but then the command line syntax
2592 * probably won't change that much -- webb.
2593 */
2594 static char_u *
2595set_one_cmd_context(
2596 expand_T *xp,
2597 char_u *buff) // buffer for command string
2598{
2599 char_u *p;
2600 char_u *cmd, *arg;
2601 int len = 0;
2602 exarg_T ea;
2603 int compl = EXPAND_NOTHING;
2604 int forceit = FALSE;
2605 int usefilter = FALSE; // filter instead of file name
2606
2607 ExpandInit(xp);
2608 xp->xp_pattern = buff;
2609 xp->xp_line = buff;
2610 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2611 ea.argt = 0;
2612
2613 // 1. skip comment lines and leading space, colons or bars
2614 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2615 ;
2616 xp->xp_pattern = cmd;
2617
2618 if (*cmd == NUL)
2619 return NULL;
2620 if (*cmd == '"') // ignore comment lines
2621 {
2622 xp->xp_context = EXPAND_NOTHING;
2623 return NULL;
2624 }
2625
2626 // 3. Skip over the range to find the command.
2627 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2628 xp->xp_pattern = cmd;
2629 if (*cmd == NUL)
2630 return NULL;
2631 if (*cmd == '"')
2632 {
2633 xp->xp_context = EXPAND_NOTHING;
2634 return NULL;
2635 }
2636
2637 if (*cmd == '|' || *cmd == '\n')
2638 return cmd + 1; // There's another command
2639
2640 // Get the command index.
2641 p = set_cmd_index(cmd, &ea, xp, &compl);
2642 if (p == NULL)
2643 return NULL;
2644
2645 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2646
2647 if (*p == '!') // forced commands
2648 {
2649 forceit = TRUE;
2650 ++p;
2651 }
2652
2653 // 6. parse arguments
2654 if (!IS_USER_CMDIDX(ea.cmdidx))
2655 ea.argt = excmd_get_argt(ea.cmdidx);
2656
2657 arg = skipwhite(p);
2658
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002659 // Does command allow "++argopt" argument?
2660 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002661 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002662 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2663 {
2664 p = arg + 2;
2665 while (*p && !vim_isspace(*p))
2666 MB_PTR_ADV(p);
2667
2668 // Still touching the command after "++"?
2669 if (*p == NUL)
2670 {
2671 if (ea.argt & EX_ARGOPT)
2672 return set_context_in_argopt(xp, arg + 2);
2673#ifdef FEAT_TERMINAL
2674 if (ea.cmdidx == CMD_terminal)
2675 return set_context_in_terminalopt(xp, arg + 2);
2676#endif
2677 }
2678
2679 arg = skipwhite(p);
2680 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002681 }
2682
2683 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2684 {
2685 if (*arg == '>') // append
2686 {
2687 if (*++arg == '>')
2688 ++arg;
2689 arg = skipwhite(arg);
2690 }
2691 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2692 {
2693 ++arg;
2694 usefilter = TRUE;
2695 }
2696 }
2697
2698 if (ea.cmdidx == CMD_read)
2699 {
2700 usefilter = forceit; // :r! filter if forced
2701 if (*arg == '!') // :r !filter
2702 {
2703 ++arg;
2704 usefilter = TRUE;
2705 }
2706 }
2707
2708 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2709 {
2710 while (*arg == *cmd) // allow any number of '>' or '<'
2711 ++arg;
2712 arg = skipwhite(arg);
2713 }
2714
2715 // Does command allow "+command"?
2716 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2717 {
2718 // Check if we're in the +command
2719 p = arg + 1;
2720 arg = skip_cmd_arg(arg, FALSE);
2721
2722 // Still touching the command after '+'?
2723 if (*arg == NUL)
2724 return p;
2725
2726 // Skip space(s) after +command to get to the real argument
2727 arg = skipwhite(arg);
2728 }
2729
2730
2731 // Check for '|' to separate commands and '"' to start comments.
2732 // Don't do this for ":read !cmd" and ":write !cmd".
2733 if ((ea.argt & EX_TRLBAR) && !usefilter)
2734 {
2735 p = arg;
2736 // ":redir @" is not the start of a comment
2737 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2738 p += 2;
2739 while (*p)
2740 {
2741 if (*p == Ctrl_V)
2742 {
2743 if (p[1] != NUL)
2744 ++p;
2745 }
2746 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2747 || *p == '|' || *p == '\n')
2748 {
2749 if (*(p - 1) != '\\')
2750 {
2751 if (*p == '|' || *p == '\n')
2752 return p + 1;
2753 return NULL; // It's a comment
2754 }
2755 }
2756 MB_PTR_ADV(p);
2757 }
2758 }
2759
2760 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2761 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2762 // no arguments allowed but there is something
2763 return NULL;
2764
2765 // Find start of last argument (argument just before cursor):
2766 p = buff;
2767 xp->xp_pattern = p;
2768 len = (int)STRLEN(buff);
2769 while (*p && p < buff + len)
2770 {
2771 if (*p == ' ' || *p == TAB)
2772 {
2773 // argument starts after a space
2774 xp->xp_pattern = ++p;
2775 }
2776 else
2777 {
2778 if (*p == '\\' && *(p + 1) != NUL)
2779 ++p; // skip over escaped character
2780 MB_PTR_ADV(p);
2781 }
2782 }
2783
2784 if (ea.argt & EX_XFILE)
2785 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2786
2787 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002788 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002789 forceit);
2790}
2791
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002792/*
2793 * Set the completion context in 'xp' for command 'str'
2794 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795 void
2796set_cmd_context(
2797 expand_T *xp,
2798 char_u *str, // start of command line
2799 int len, // length of command line (excl. NUL)
2800 int col, // position of cursor
2801 int use_ccline UNUSED) // use ccline for info
2802{
2803#ifdef FEAT_EVAL
2804 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002805 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002806#endif
2807 int old_char = NUL;
2808 char_u *nextcomm;
2809
2810 // Avoid a UMR warning from Purify, only save the character if it has been
2811 // written before.
2812 if (col < len)
2813 old_char = str[col];
2814 str[col] = NUL;
2815 nextcomm = str;
2816
2817#ifdef FEAT_EVAL
2818 if (use_ccline && ccline->cmdfirstc == '=')
2819 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 // pass CMD_SIZE because there is no real command
2821 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002822 }
2823 else if (use_ccline && ccline->input_fn)
2824 {
2825 xp->xp_context = ccline->xp_context;
2826 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002827 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002828 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2829 {
2830 context = xp->xp_context;
2831 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2832 &context);
2833 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002834 }
2835 else
2836#endif
2837 while (nextcomm != NULL)
2838 nextcomm = set_one_cmd_context(xp, nextcomm);
2839
2840 // Store the string here so that call_user_expand_func() can get to them
2841 // easily.
2842 xp->xp_line = str;
2843 xp->xp_col = col;
2844
2845 str[col] = old_char;
2846}
2847
2848/*
2849 * Expand the command line "str" from context "xp".
2850 * "xp" must have been set by set_cmd_context().
2851 * xp->xp_pattern points into "str", to where the text that is to be expanded
2852 * starts.
2853 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2854 * cursor.
2855 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2856 * key that triggered expansion literally.
2857 * Returns EXPAND_OK otherwise.
2858 */
2859 int
2860expand_cmdline(
2861 expand_T *xp,
2862 char_u *str, // start of command line
2863 int col, // position of cursor
2864 int *matchcount, // return: nr of matches
2865 char_u ***matches) // return: array of pointers to matches
2866{
2867 char_u *file_str = NULL;
2868 int options = WILD_ADD_SLASH|WILD_SILENT;
2869
2870 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2871 {
2872 beep_flush();
2873 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2874 }
2875 if (xp->xp_context == EXPAND_NOTHING)
2876 {
2877 // Caller can use the character as a normal char instead
2878 return EXPAND_NOTHING;
2879 }
2880
2881 // add star to file name, or convert to regexp if not exp. files.
2882 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002883 if (cmdline_fuzzy_completion_supported(xp))
2884 // If fuzzy matching, don't modify the search string
2885 file_str = vim_strsave(xp->xp_pattern);
2886 else
2887 {
2888 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2889 if (file_str == NULL)
2890 return EXPAND_UNSUCCESSFUL;
2891 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002892
2893 if (p_wic)
2894 options += WILD_ICASE;
2895
2896 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002897 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002898 {
2899 *matchcount = 0;
2900 *matches = NULL;
2901 }
2902 vim_free(file_str);
2903
2904 return EXPAND_OK;
2905}
2906
Bram Moolenaar66b51422019-08-18 21:44:12 +02002907/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002908 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002909 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002910 */
2911 static int
2912expand_files_and_dirs(
2913 expand_T *xp,
2914 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002915 char_u ***matches,
2916 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002917 int flags,
2918 int options)
2919{
2920 int free_pat = FALSE;
2921 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002922 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923
2924 // for ":set path=" and ":set tags=" halve backslashes for escaped
2925 // space
2926 if (xp->xp_backslash != XP_BS_NONE)
2927 {
2928 free_pat = TRUE;
2929 pat = vim_strsave(pat);
2930 for (i = 0; pat[i]; ++i)
2931 if (pat[i] == '\\')
2932 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002933 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002934 && pat[i + 1] == '\\'
2935 && pat[i + 2] == '\\'
2936 && pat[i + 3] == ' ')
2937 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002938 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002939 && pat[i + 1] == ' ')
2940 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002941 else if ((xp->xp_backslash & XP_BS_COMMA)
2942 && pat[i + 1] == '\\'
2943 && pat[i + 2] == ',')
2944 STRMOVE(pat + i, pat + i + 2);
2945#ifdef BACKSLASH_IN_FILENAME
2946 else if ((xp->xp_backslash & XP_BS_COMMA)
2947 && pat[i + 1] == ',')
2948 STRMOVE(pat + i, pat + i + 1);
2949#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002950 }
2951 }
2952
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002953 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002954 {
2955#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002956 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002957#endif
2958 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002959 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002960 {
2961 if (xp->xp_context == EXPAND_FILES)
2962 flags |= EW_FILE;
2963 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2964 flags |= (EW_FILE | EW_PATH);
2965 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2966 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2967 else
2968 flags = (flags | EW_DIR) & ~EW_FILE;
2969 if (options & WILD_ICASE)
2970 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002971
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002972 // Expand wildcards, supporting %:h and the like.
2973 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2974 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002975 if (free_pat)
2976 vim_free(pat);
2977#ifdef BACKSLASH_IN_FILENAME
2978 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2979 {
2980 int j;
2981
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002982 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002983 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002984 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985
2986 while (*ptr != NUL)
2987 {
2988 if (p_csl[0] == 's' && *ptr == '\\')
2989 *ptr = '/';
2990 else if (p_csl[0] == 'b' && *ptr == '/')
2991 *ptr = '\\';
2992 ptr += (*mb_ptr2len)(ptr);
2993 }
2994 }
2995 }
2996#endif
2997 return ret;
2998}
2999
3000/*
Bram Moolenaard0190392019-08-23 21:17:35 +02003001 * Function given to ExpandGeneric() to obtain the possible arguments of the
3002 * ":behave {mswin,xterm}" command.
3003 */
3004 static char_u *
3005get_behave_arg(expand_T *xp UNUSED, int idx)
3006{
3007 if (idx == 0)
3008 return (char_u *)"mswin";
3009 if (idx == 1)
3010 return (char_u *)"xterm";
3011 return NULL;
3012}
3013
Christian Brabandta3422aa2025-04-23 21:04:24 +02003014/*
3015 * Function given to ExpandGeneric() to obtain the possible arguments of the
3016 * ":filetype {plugin,indent}" command.
3017 */
3018 static char_u *
3019get_filetypecmd_arg(expand_T *xp UNUSED, int idx)
3020{
3021 char *opts_all[] = {"indent", "plugin", "on", "off"};
3022 char *opts_plugin[] = {"plugin", "on", "off"};
3023 char *opts_indent[] = {"indent", "on", "off"};
3024 char *opts_onoff[] = {"on", "off"};
3025
3026 if (filetype_expand_what == EXP_FILETYPECMD_ALL && idx < 4)
3027 return (char_u *)opts_all[idx];
3028 if (filetype_expand_what == EXP_FILETYPECMD_PLUGIN && idx < 3)
3029 return (char_u *)opts_plugin[idx];
3030 if (filetype_expand_what == EXP_FILETYPECMD_INDENT && idx < 3)
3031 return (char_u *)opts_indent[idx];
3032 if (filetype_expand_what == EXP_FILETYPECMD_ONOFF && idx < 2)
3033 return (char_u *)opts_onoff[idx];
3034 return NULL;
3035}
3036
K.Takata161b6ac2022-11-14 15:31:07 +00003037#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003038/*
3039 * Function given to ExpandGeneric() to obtain the possible arguments of the
3040 * ":breakadd {expr, file, func, here}" command.
3041 * ":breakdel {func, file, here}" command.
3042 */
3043 static char_u *
3044get_breakadd_arg(expand_T *xp UNUSED, int idx)
3045{
3046 char *opts[] = {"expr", "file", "func", "here"};
3047
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003048 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003049 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003050 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003051 if (breakpt_expand_what == EXP_BREAKPT_ADD)
3052 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003053 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
3054 {
3055 // breakdel {func, file, here}
3056 if (idx <= 2)
3057 return (char_u *)opts[idx + 1];
3058 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003059 else
3060 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00003061 // profdel {func, file}
3062 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003063 return (char_u *)opts[idx + 1];
3064 }
3065 }
3066 return NULL;
3067}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003068
3069/*
3070 * Function given to ExpandGeneric() to obtain the possible arguments for the
3071 * ":scriptnames" command.
3072 */
3073 static char_u *
3074get_scriptnames_arg(expand_T *xp UNUSED, int idx)
3075{
3076 scriptitem_T *si;
3077
3078 if (!SCRIPT_ID_VALID(idx + 1))
3079 return NULL;
3080
3081 si = SCRIPT_ITEM(idx + 1);
3082 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
3083 return NameBuff;
3084}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003085#endif
3086
Bram Moolenaard0190392019-08-23 21:17:35 +02003087/*
3088 * Function given to ExpandGeneric() to obtain the possible arguments of the
3089 * ":messages {clear}" command.
3090 */
3091 static char_u *
3092get_messages_arg(expand_T *xp UNUSED, int idx)
3093{
3094 if (idx == 0)
3095 return (char_u *)"clear";
3096 return NULL;
3097}
3098
3099 static char_u *
3100get_mapclear_arg(expand_T *xp UNUSED, int idx)
3101{
3102 if (idx == 0)
3103 return (char_u *)"<buffer>";
3104 return NULL;
3105}
3106
3107/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003108 * Do the expansion based on xp->xp_context and 'rmp'.
3109 */
3110 static int
3111ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003112 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003113 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003114 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003115 char_u ***matches,
3116 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003117{
3118 static struct expgen
3119 {
3120 int context;
3121 char_u *((*func)(expand_T *, int));
3122 int ic;
3123 int escaped;
3124 } tab[] =
3125 {
3126 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3127 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Christian Brabandta3422aa2025-04-23 21:04:24 +02003128 {EXPAND_FILETYPECMD, get_filetypecmd_arg, TRUE, TRUE},
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003129 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3130 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3131 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3132 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3133 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3134 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3135 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3136 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003137#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003138 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3139 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3140 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3141 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3142 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003143#endif
3144#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003145 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3146 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003147#endif
3148#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003149 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003150#endif
3151#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003152 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003153#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003154 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3155 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3156 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003157#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003158 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003159#endif
3160#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003161 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003162#endif
3163#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003164 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003165#endif
3166#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003167 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3168 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003169#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003170 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3171 {EXPAND_USER, get_users, TRUE, FALSE},
3172 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003173#ifdef FEAT_EVAL
3174 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003175 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003176#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003177 };
3178 int i;
3179 int ret = FAIL;
3180
3181 // Find a context in the table and call the ExpandGeneric() with the
3182 // right function to do the expansion.
3183 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3184 {
3185 if (xp->xp_context == tab[i].context)
3186 {
3187 if (tab[i].ic)
3188 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003189 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3190 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003191 break;
3192 }
3193 }
3194
3195 return ret;
3196}
3197
3198/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003199 * Map wild expand options to flags for expand_wildcards()
3200 */
3201 static int
3202map_wildopts_to_ewflags(int options)
3203{
3204 int flags;
3205
3206 flags = EW_DIR; // include directories
3207 if (options & WILD_LIST_NOTFOUND)
3208 flags |= EW_NOTFOUND;
3209 if (options & WILD_ADD_SLASH)
3210 flags |= EW_ADDSLASH;
3211 if (options & WILD_KEEP_ALL)
3212 flags |= EW_KEEPALL;
3213 if (options & WILD_SILENT)
3214 flags |= EW_SILENT;
3215 if (options & WILD_NOERROR)
3216 flags |= EW_NOERROR;
3217 if (options & WILD_ALLLINKS)
3218 flags |= EW_ALLLINKS;
3219
3220 return flags;
3221}
3222
3223/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224 * Do the expansion based on xp->xp_context and "pat".
3225 */
3226 static int
3227ExpandFromContext(
3228 expand_T *xp,
3229 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003230 char_u ***matches,
3231 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232 int options) // WILD_ flags
3233{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003235 int ret;
3236 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003237 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003238 int fuzzy = cmdline_fuzzy_complete(pat)
3239 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003241 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242
3243 if (xp->xp_context == EXPAND_FILES
3244 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003245 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003246 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003247 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003248 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3249 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003251 *matches = (char_u **)"";
3252 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 if (xp->xp_context == EXPAND_HELP)
3254 {
3255 // With an empty argument we would get all the help tags, which is
3256 // very slow. Get matches for "help" instead.
3257 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003258 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003259 {
3260#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003261 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003262#endif
3263 return OK;
3264 }
3265 return FAIL;
3266 }
3267
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003269 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003273 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003274#ifdef FEAT_DIFF
3275 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003276 return ExpandBufnames(pat, numMatches, matches,
3277 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003278#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003279 if (xp->xp_context == EXPAND_TAGS
3280 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003281 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3282 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283 if (xp->xp_context == EXPAND_COLORS)
3284 {
3285 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003286 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003287 directories);
3288 }
3289 if (xp->xp_context == EXPAND_COMPILER)
3290 {
3291 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003292 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003293 }
3294 if (xp->xp_context == EXPAND_OWNSYNTAX)
3295 {
3296 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003297 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003298 }
3299 if (xp->xp_context == EXPAND_FILETYPE)
3300 {
3301 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003302 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003303 }
Doug Kearns81642d92024-01-04 22:37:44 +01003304#ifdef FEAT_KEYMAP
3305 if (xp->xp_context == EXPAND_KEYMAP)
3306 {
3307 char *directories[] = {"keymap", NULL};
3308 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3309 }
3310#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003311#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003312 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003313 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003314#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003316 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003317 if (xp->xp_context == EXPAND_RUNTIME)
3318 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003319
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003320 // When expanding a function name starting with s:, match the <SNR>nr_
3321 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003322 if ((xp->xp_context == EXPAND_USER_FUNC
3323 || xp->xp_context == EXPAND_DISASSEMBLE)
3324 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003325 {
3326 int len = (int)STRLEN(pat) + 20;
3327
3328 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003329 if (tofree == NULL)
3330 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003331 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003332 pat = tofree;
3333 }
3334
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003335 if (!fuzzy)
3336 {
3337 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3338 if (regmatch.regprog == NULL)
3339 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003340
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003341 // set ignore-case according to p_ic, p_scs and pat
3342 regmatch.rm_ic = ignorecase(pat);
3343 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344
3345 if (xp->xp_context == EXPAND_SETTINGS
3346 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003347 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003348 else if (xp->xp_context == EXPAND_STRING_SETTING)
3349 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3350 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3351 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003353 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003354 else if (xp->xp_context == EXPAND_ARGOPT)
3355 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003356 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3357 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003358#if defined(FEAT_TERMINAL)
3359 else if (xp->xp_context == EXPAND_TERMINALOPT)
3360 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3361#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003362#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003363 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003364 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003365#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003367 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003369 if (!fuzzy)
3370 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003371 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003372
3373 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374}
3375
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003376 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003377ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003378 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379 expand_T *xp,
3380 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003381 char_u ***matches,
3382 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003383 char_u *((*func)(expand_T *, int)),
3384 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003385 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003386{
Yee Cheng China7b81202025-02-23 09:32:47 +01003387 return ExpandGenericExt(
3388 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3389}
3390
3391/*
3392 * Expand a list of names.
3393 *
3394 * Generic function for command line completion. It calls a function to
3395 * obtain strings, one by one. The strings are matched against a regexp
3396 * program. Matching strings are copied into an array, which is returned.
3397 *
3398 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3399 * is used.
3400 *
3401 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3402 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3403 * sorting.
3404 *
3405 * Returns OK when no problems encountered, FAIL for error (out of memory).
3406 */
3407 int
3408ExpandGenericExt(
3409 char_u *pat,
3410 expand_T *xp,
3411 regmatch_T *regmatch,
3412 char_u ***matches,
3413 int *numMatches,
3414 char_u *((*func)(expand_T *, int)),
3415 // returns a string from the list
3416 int escaped,
3417 int sortStartIdx)
3418{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003419 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003420 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003421 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003422 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003423 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003424 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003425 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003426 int sort_matches = FALSE;
3427 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003428 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003429
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003430 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003431 *matches = NULL;
3432 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003433
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003434 if (!fuzzy)
3435 ga_init2(&ga, sizeof(char *), 30);
3436 else
3437 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3438
3439 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003441 str = (*func)(xp, i);
3442 if (str == NULL) // end of list
3443 break;
3444 if (*str == NUL) // skip empty strings
3445 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003446
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003447 if (xp->xp_pattern[0] != NUL)
3448 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003449 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003450 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003451 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003452 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003453 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003454 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003455 }
3456 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003457 else
3458 match = TRUE;
3459
3460 if (!match)
3461 continue;
3462
3463 if (escaped)
3464 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3465 else
3466 str = vim_strsave(str);
3467 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003468 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003469 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003470 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003471 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 return FAIL;
3473 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003474 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003475 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003477
3478 if (ga_grow(&ga, 1) == FAIL)
3479 {
3480 vim_free(str);
3481 break;
3482 }
3483
3484 if (fuzzy)
3485 {
3486 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3487 fuzmatch->idx = ga.ga_len;
3488 fuzmatch->str = str;
3489 fuzmatch->score = score;
3490 }
3491 else
3492 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3493
K.Takata161b6ac2022-11-14 15:31:07 +00003494#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003495 if (func == get_menu_names)
3496 {
3497 // test for separator added by get_menu_names()
3498 str += STRLEN(str) - 1;
3499 if (*str == '\001')
3500 *str = '.';
3501 }
K.Takata161b6ac2022-11-14 15:31:07 +00003502#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003503
Yee Cheng China7b81202025-02-23 09:32:47 +01003504 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3505 {
3506 // Found first item to start sorting from. This is usually 0.
3507 sortStartMatchIdx = ga.ga_len;
3508 }
3509
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003510 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511 }
3512
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003513 if (ga.ga_len == 0)
3514 return OK;
3515
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003516 // sort the matches when using regular expression matching and sorting
3517 // applies to the completion context. Menus and scriptnames should be kept
3518 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003519 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003520 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003521 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003522 && xp->xp_context != EXPAND_SCRIPTNAMES
3523 && xp->xp_context != EXPAND_ARGOPT
3524 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003525 sort_matches = TRUE;
3526
3527 // <SNR> functions should be sorted to the end.
3528 if (xp->xp_context == EXPAND_EXPRESSION
3529 || xp->xp_context == EXPAND_FUNCTIONS
3530 || xp->xp_context == EXPAND_USER_FUNC
3531 || xp->xp_context == EXPAND_DISASSEMBLE)
3532 funcsort = TRUE;
3533
3534 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003535 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003536 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003537 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003538 // <SNR> functions should be sorted to the end.
3539 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3540 sort_func_compare);
3541 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003542 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003543 }
3544
3545 if (!fuzzy)
3546 {
3547 *matches = ga.ga_data;
3548 *numMatches = ga.ga_len;
3549 }
3550 else
3551 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003552 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3553 funcsort) == FAIL)
3554 return FAIL;
3555 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556 }
3557
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003558#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003559 // Reset the variables used for special highlight names expansion, so that
3560 // they don't show up when getting normal highlight names by ID.
3561 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003562#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003563
Bram Moolenaar66b51422019-08-18 21:44:12 +02003564 return OK;
3565}
3566
3567/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003568 * Expand shell command matches in one directory of $PATH.
3569 */
3570 static void
3571expand_shellcmd_onedir(
3572 char_u *buf,
3573 char_u *s,
3574 size_t l,
3575 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003576 char_u ***matches,
3577 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003578 int flags,
3579 hashtab_T *ht,
3580 garray_T *gap)
3581{
3582 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003583 hash_T hash;
3584 hashitem_T *hi;
3585
3586 vim_strncpy(buf, s, l);
3587 add_pathsep(buf);
3588 l = STRLEN(buf);
3589 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3590
3591 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003592 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003593 if (ret != OK)
3594 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003595
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003596 if (ga_grow(gap, *numMatches) == FAIL)
3597 {
3598 FreeWild(*numMatches, *matches);
3599 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003600 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003601
3602 for (int i = 0; i < *numMatches; ++i)
3603 {
3604 char_u *name = (*matches)[i];
3605
3606 if (STRLEN(name) > l)
3607 {
3608 // Check if this name was already found.
3609 hash = hash_hash(name + l);
3610 hi = hash_lookup(ht, name + l, hash);
3611 if (HASHITEM_EMPTY(hi))
3612 {
3613 // Remove the path that was prepended.
3614 STRMOVE(name, name + l);
3615 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3616 hash_add_item(ht, hi, name, hash);
3617 name = NULL;
3618 }
3619 }
3620 vim_free(name);
3621 }
3622 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003623}
3624
3625/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626 * Complete a shell command.
3627 * Returns FAIL or OK;
3628 */
3629 static int
3630expand_shellcmd(
3631 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003632 char_u ***matches, // return: array with matches
3633 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003634 int flagsarg) // EW_ flags
3635{
3636 char_u *pat;
3637 int i;
3638 char_u *path = NULL;
3639 int mustfree = FALSE;
3640 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003641 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003642 size_t l;
3643 char_u *s, *e;
3644 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003645 int did_curdir = FALSE;
3646 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003647
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003648 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649 if (buf == NULL)
3650 return FAIL;
3651
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003652 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003653 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003654 if (pat == NULL)
3655 {
3656 vim_free(buf);
3657 return FAIL;
3658 }
3659
Bram Moolenaar66b51422019-08-18 21:44:12 +02003660 for (i = 0; pat[i]; ++i)
3661 if (pat[i] == '\\' && pat[i + 1] == ' ')
3662 STRMOVE(pat + i, pat + i + 1);
3663
3664 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3665
3666 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3667 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3668 path = (char_u *)".";
3669 else
3670 {
3671 // For an absolute name we don't use $PATH.
3672 if (!mch_isFullName(pat))
3673 path = vim_getenv((char_u *)"PATH", &mustfree);
3674 if (path == NULL)
3675 path = (char_u *)"";
3676 }
3677
3678 // Go over all directories in $PATH. Expand matches in that directory and
3679 // collect them in "ga". When "." is not in $PATH also expand for the
3680 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003681 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003682 hash_init(&found_ht);
3683 for (s = path; ; s = e)
3684 {
K.Takata161b6ac2022-11-14 15:31:07 +00003685#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003687#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003689#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003690 if (e == NULL)
3691 e = s + STRLEN(s);
3692
3693 if (*s == NUL)
3694 {
3695 if (did_curdir)
3696 break;
3697 // Find directories in the current directory, path is empty.
3698 did_curdir = TRUE;
3699 flags |= EW_DIR;
3700 }
3701 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3702 {
3703 did_curdir = TRUE;
3704 flags |= EW_DIR;
3705 }
3706 else
3707 // Do not match directories inside a $PATH item.
3708 flags &= ~EW_DIR;
3709
3710 l = e - s;
3711 if (l > MAXPATHL - 5)
3712 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003713
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003714 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003715 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003716
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717 if (*e != NUL)
3718 ++e;
3719 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003720 *matches = ga.ga_data;
3721 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003722
3723 vim_free(buf);
3724 vim_free(pat);
3725 if (mustfree)
3726 vim_free(path);
3727 hash_clear(&found_ht);
3728 return OK;
3729}
3730
K.Takata161b6ac2022-11-14 15:31:07 +00003731#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003732/*
3733 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003734 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735 */
3736 static void *
3737call_user_expand_func(
3738 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003739 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003740{
3741 cmdline_info_T *ccline = get_cmdline_info();
3742 int keep = 0;
3743 typval_T args[4];
3744 sctx_T save_current_sctx = current_sctx;
3745 char_u *pat = NULL;
3746 void *ret;
3747
3748 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3749 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003750
3751 if (ccline->cmdbuff != NULL)
3752 {
3753 keep = ccline->cmdbuff[ccline->cmdlen];
3754 ccline->cmdbuff[ccline->cmdlen] = 0;
3755 }
3756
3757 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3758
3759 args[0].v_type = VAR_STRING;
3760 args[0].vval.v_string = pat;
3761 args[1].v_type = VAR_STRING;
3762 args[1].vval.v_string = xp->xp_line;
3763 args[2].v_type = VAR_NUMBER;
3764 args[2].vval.v_number = xp->xp_col;
3765 args[3].v_type = VAR_UNKNOWN;
3766
3767 current_sctx = xp->xp_script_ctx;
3768
3769 ret = user_expand_func(xp->xp_arg, 3, args);
3770
3771 current_sctx = save_current_sctx;
3772 if (ccline->cmdbuff != NULL)
3773 ccline->cmdbuff[ccline->cmdlen] = keep;
3774
3775 vim_free(pat);
3776 return ret;
3777}
3778
3779/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003780 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3781 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782 */
3783 static int
3784ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003785 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003786 expand_T *xp,
3787 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003788 char_u ***matches,
3789 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003790{
3791 char_u *retstr;
3792 char_u *s;
3793 char_u *e;
3794 int keep;
3795 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003796 int fuzzy;
3797 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003798 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003799
3800 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003801 *matches = NULL;
3802 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003803
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003804 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 if (retstr == NULL)
3806 return FAIL;
3807
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003808 if (!fuzzy)
3809 ga_init2(&ga, sizeof(char *), 3);
3810 else
3811 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3812
Bram Moolenaar66b51422019-08-18 21:44:12 +02003813 for (s = retstr; *s != NUL; s = e)
3814 {
3815 e = vim_strchr(s, '\n');
3816 if (e == NULL)
3817 e = s + STRLEN(s);
3818 keep = *e;
3819 *e = NUL;
3820
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003821 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003822 {
3823 if (!fuzzy)
3824 match = vim_regexec(regmatch, s, (colnr_T)0);
3825 else
3826 {
3827 score = fuzzy_match_str(s, pat);
3828 match = (score != 0);
3829 }
3830 }
3831 else
3832 match = TRUE; // match everything
3833
Bram Moolenaar66b51422019-08-18 21:44:12 +02003834 *e = keep;
3835
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003836 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003837 {
3838 if (ga_grow(&ga, 1) == FAIL)
3839 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003840 if (!fuzzy)
3841 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3842 else
3843 {
3844 fuzmatch_str_T *fuzmatch =
3845 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003846 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003847 fuzmatch->str = vim_strnsave(s, e - s);
3848 fuzmatch->score = score;
3849 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003850 ++ga.ga_len;
3851 }
3852
3853 if (*e != NUL)
3854 ++e;
3855 }
3856 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003857
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003858 if (ga.ga_len == 0)
3859 return OK;
3860
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003861 if (!fuzzy)
3862 {
3863 *matches = ga.ga_data;
3864 *numMatches = ga.ga_len;
3865 }
3866 else
3867 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003868 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3869 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003870 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003871 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003872 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003873 return OK;
3874}
3875
3876/*
3877 * Expand names with a list returned by a function defined by the user.
3878 */
3879 static int
3880ExpandUserList(
3881 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003882 char_u ***matches,
3883 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003884{
3885 list_T *retlist;
3886 listitem_T *li;
3887 garray_T ga;
3888
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003889 *matches = NULL;
3890 *numMatches = 0;
3891 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003892 if (retlist == NULL)
3893 return FAIL;
3894
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003895 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003896 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003897 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003898 {
3899 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3900 continue; // Skip non-string items and empty strings
3901
3902 if (ga_grow(&ga, 1) == FAIL)
3903 break;
3904
3905 ((char_u **)ga.ga_data)[ga.ga_len] =
3906 vim_strsave(li->li_tv.vval.v_string);
3907 ++ga.ga_len;
3908 }
3909 list_unref(retlist);
3910
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003911 *matches = ga.ga_data;
3912 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003913 return OK;
3914}
K.Takata161b6ac2022-11-14 15:31:07 +00003915#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003916
3917/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003918 * Expand "file" for all comma-separated directories in "path".
3919 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003920 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003921 */
3922 void
3923globpath(
3924 char_u *path,
3925 char_u *file,
3926 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003927 int expand_options,
3928 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003929{
3930 expand_T xpc;
3931 char_u *buf;
3932 int i;
3933 int num_p;
3934 char_u **p;
3935
3936 buf = alloc(MAXPATHL);
3937 if (buf == NULL)
3938 return;
3939
3940 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003941 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003942
3943 // Loop over all entries in {path}.
3944 while (*path != NUL)
3945 {
3946 // Copy one item of the path to buf[] and concatenate the file name.
3947 copy_option_part(&path, buf, MAXPATHL, ",");
3948 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3949 {
K.Takata161b6ac2022-11-14 15:31:07 +00003950#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003951 // Using the platform's path separator (\) makes vim incorrectly
3952 // treat it as an escape character, use '/' instead.
3953 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3954 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003955#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003956 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003957#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003958 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003959 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003960 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3961 {
3962 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3963
3964 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003965 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003966 for (i = 0; i < num_p; ++i)
3967 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003968 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003969 ++ga->ga_len;
3970 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003971 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003972 }
3973 }
3974 }
3975
3976 vim_free(buf);
3977}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003978
Bram Moolenaareadee482020-09-04 15:37:31 +02003979/*
3980 * Translate some keys pressed when 'wildmenu' is used.
3981 */
3982 int
3983wildmenu_translate_key(
3984 cmdline_info_T *cclp,
3985 int key,
3986 expand_T *xp,
3987 int did_wild_list)
3988{
3989 int c = key;
3990
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003991 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003992 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003993 // When the popup menu is used for cmdline completion:
3994 // Up : go to the previous item in the menu
3995 // Down : go to the next item in the menu
3996 // Left : go to the parent directory
3997 // Right: list the files in the selected directory
3998 switch (c)
3999 {
4000 case K_UP: c = K_LEFT; break;
4001 case K_DOWN: c = K_RIGHT; break;
4002 case K_LEFT: c = K_UP; break;
4003 case K_RIGHT: c = K_DOWN; break;
4004 default: break;
4005 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004006 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004007
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004008 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02004009 {
4010 if (c == K_LEFT)
4011 c = Ctrl_P;
4012 else if (c == K_RIGHT)
4013 c = Ctrl_N;
4014 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00004015
Bram Moolenaareadee482020-09-04 15:37:31 +02004016 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004017 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02004018 && cclp->cmdpos > 1
4019 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
4020 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
4021 && (c == '\n' || c == '\r' || c == K_KENTER))
4022 c = K_DOWN;
4023
4024 return c;
4025}
4026
4027/*
4028 * Delete characters on the command line, from "from" to the current
4029 * position.
4030 */
4031 static void
4032cmdline_del(cmdline_info_T *cclp, int from)
4033{
4034 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
4035 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
4036 cclp->cmdlen -= cclp->cmdpos - from;
4037 cclp->cmdpos = from;
4038}
4039
4040/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004041 * Handle a key pressed when the wild menu for the menu names
4042 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02004043 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004044 static int
4045wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02004046{
Bram Moolenaareadee482020-09-04 15:37:31 +02004047 int i;
4048 int j;
4049
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004050 // Hitting <Down> after "emenu Name.": complete submenu
4051 if (key == K_DOWN && cclp->cmdpos > 0
4052 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02004053 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004054 key = p_wc;
4055 KeyTyped = TRUE; // in case the key was mapped
4056 }
4057 else if (key == K_UP)
4058 {
4059 // Hitting <Up>: Remove one submenu name in front of the
4060 // cursor
4061 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004062
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004063 j = (int)(xp->xp_pattern - cclp->cmdbuff);
4064 i = 0;
4065 while (--j > 0)
4066 {
4067 // check for start of menu name
4068 if (cclp->cmdbuff[j] == ' '
4069 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02004070 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004071 i = j + 1;
4072 break;
4073 }
4074 // check for start of submenu name
4075 if (cclp->cmdbuff[j] == '.'
4076 && cclp->cmdbuff[j - 1] != '\\')
4077 {
4078 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004079 {
4080 i = j + 1;
4081 break;
4082 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004083 else
4084 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004085 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004086 }
4087 if (i > 0)
4088 cmdline_del(cclp, i);
4089 key = p_wc;
4090 KeyTyped = TRUE; // in case the key was mapped
4091 xp->xp_context = EXPAND_NOTHING;
4092 }
4093
4094 return key;
4095}
4096
4097/*
4098 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4099 * directory names (EXPAND_DIRECTORIES) or shell command names
4100 * (EXPAND_SHELLCMD) is displayed.
4101 */
4102 static int
4103wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4104{
4105 int i;
4106 int j;
4107 char_u upseg[5];
4108
4109 upseg[0] = PATHSEP;
4110 upseg[1] = '.';
4111 upseg[2] = '.';
4112 upseg[3] = PATHSEP;
4113 upseg[4] = NUL;
4114
4115 if (key == K_DOWN
4116 && cclp->cmdpos > 0
4117 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4118 && (cclp->cmdpos < 3
4119 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4120 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4121 {
4122 // go down a directory
4123 key = p_wc;
4124 KeyTyped = TRUE; // in case the key was mapped
4125 }
4126 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4127 {
4128 // If in a direct ancestor, strip off one ../ to go down
4129 int found = FALSE;
4130
4131 j = cclp->cmdpos;
4132 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4133 while (--j > i)
4134 {
4135 if (has_mbyte)
4136 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4137 if (vim_ispathsep(cclp->cmdbuff[j]))
4138 {
4139 found = TRUE;
4140 break;
4141 }
4142 }
4143 if (found
4144 && cclp->cmdbuff[j - 1] == '.'
4145 && cclp->cmdbuff[j - 2] == '.'
4146 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4147 {
4148 cmdline_del(cclp, j - 2);
4149 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004150 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004151 }
4152 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004153 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004154 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004155 // go up a directory
4156 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004157
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004158 j = cclp->cmdpos - 1;
4159 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4160 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004161 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004162 if (has_mbyte)
4163 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4164 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004165#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004166 && vim_strchr((char_u *)" *?[{`$%#",
4167 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004168#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004169 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004170 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004171 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004172 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004173 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004174 break;
4175 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004176 else
4177 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004178 }
4179 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004180
4181 if (!found)
4182 j = i;
4183 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4184 j += 4;
4185 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4186 && j == i)
4187 j += 3;
4188 else
4189 j = 0;
4190 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004191 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004192 // TODO this is only for DOS/UNIX systems - need to put in
4193 // machine-specific stuff here and in upseg init
4194 cmdline_del(cclp, j);
4195 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004196 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004197 else if (cclp->cmdpos > i)
4198 cmdline_del(cclp, i);
4199
4200 // Now complete in the new directory. Set KeyTyped in case the
4201 // Up key came from a mapping.
4202 key = p_wc;
4203 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004204 }
4205
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004206 return key;
4207}
4208
4209/*
4210 * Handle a key pressed when the wild menu is displayed
4211 */
4212 int
4213wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4214{
4215 if (xp->xp_context == EXPAND_MENUNAMES)
4216 return wildmenu_process_key_menunames(cclp, key, xp);
4217 else if ((xp->xp_context == EXPAND_FILES
4218 || xp->xp_context == EXPAND_DIRECTORIES
4219 || xp->xp_context == EXPAND_SHELLCMD))
4220 return wildmenu_process_key_filenames(cclp, key, xp);
4221
4222 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004223}
4224
4225/*
4226 * Free expanded names when finished walking through the matches
4227 */
4228 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004229wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004230{
4231 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004232
4233 if (!p_wmnu || wild_menu_showing == 0)
4234 return;
4235
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004236#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004237 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004238 if (cclp->input_fn)
4239 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004240#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004241
4242 if (wild_menu_showing == WM_SCROLLED)
4243 {
4244 // Entered command line, move it up
4245 cmdline_row--;
4246 redrawcmd();
4247 }
4248 else if (save_p_ls != -1)
4249 {
4250 // restore 'laststatus' and 'winminheight'
4251 p_ls = save_p_ls;
4252 p_wmh = save_p_wmh;
4253 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004254 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004255 redrawcmd();
4256 save_p_ls = -1;
4257 }
4258 else
4259 {
4260 win_redraw_last_status(topframe);
4261 redraw_statuslines();
4262 }
4263 KeyTyped = skt;
4264 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004265#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004266 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004267 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004268#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004269}
Bram Moolenaareadee482020-09-04 15:37:31 +02004270
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004271#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004272/*
4273 * "getcompletion()" function
4274 */
4275 void
4276f_getcompletion(typval_T *argvars, typval_T *rettv)
4277{
4278 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004279 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004280 expand_T xpc;
4281 int filtered = FALSE;
4282 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004283 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004284
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004285 if (in_vim9script()
4286 && (check_for_string_arg(argvars, 0) == FAIL
4287 || check_for_string_arg(argvars, 1) == FAIL
4288 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4289 return;
4290
ii144785fe02021-11-21 12:13:56 +00004291 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004292 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004293 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004294 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004295
4296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004297 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004298
4299 if (p_wic)
4300 options |= WILD_ICASE;
4301
4302 // For filtered results, 'wildignore' is used
4303 if (!filtered)
4304 options |= WILD_KEEP_ALL;
4305
4306 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004307 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004308 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004309 int cmdline_len = (int)STRLEN(pat);
4310 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004311 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004312 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004313 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004314 else
4315 {
ii144785fe02021-11-21 12:13:56 +00004316 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004317 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004318 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004319
4320 xpc.xp_context = cmdcomplete_str_to_type(type);
4321 if (xpc.xp_context == EXPAND_NOTHING)
4322 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004323 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004324 return;
4325 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004326
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004327 if (xpc.xp_context == EXPAND_USER_DEFINED)
4328 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004329 // Must be "custom,funcname" pattern
4330 if (STRNCMP(type, "custom,", 7) != 0)
4331 {
4332 semsg(_(e_invalid_argument_str), type);
4333 return;
4334 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004335
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004336 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004337 }
4338
4339 if (xpc.xp_context == EXPAND_USER_LIST)
4340 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004341 // Must be "customlist,funcname" pattern
4342 if (STRNCMP(type, "customlist,", 11) != 0)
4343 {
4344 semsg(_(e_invalid_argument_str), type);
4345 return;
4346 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004347
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004348 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004349 }
4350
Bram Moolenaar66b51422019-08-18 21:44:12 +02004351# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004352 if (xpc.xp_context == EXPAND_MENUS)
4353 {
4354 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4355 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4356 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004357# endif
4358# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004359 if (xpc.xp_context == EXPAND_CSCOPE)
4360 {
4361 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4362 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4363 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004364# endif
4365# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004366 if (xpc.xp_context == EXPAND_SIGN)
4367 {
4368 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4369 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4370 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004371# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004372 if (xpc.xp_context == EXPAND_RUNTIME)
4373 {
4374 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4375 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4376 }
zeertzjq85f36d62024-10-10 19:14:13 +02004377 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4378 {
4379 int context = EXPAND_SHELLCMDLINE;
4380 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4381 &context);
4382 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4383 }
Christian Brabandta3422aa2025-04-23 21:04:24 +02004384 if (xpc.xp_context == EXPAND_FILETYPECMD)
4385 filetype_expand_what = EXP_FILETYPECMD_ALL;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004386 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004387
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004388 if (cmdline_fuzzy_completion_supported(&xpc))
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004389 // when fuzzy matching, don't modify the search string
4390 pat = vim_strsave(xpc.xp_pattern);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004391 else
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02004392 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004393
Bram Moolenaar93a10962022-06-16 11:42:09 +01004394 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004395 {
4396 int i;
4397
4398 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4399
4400 for (i = 0; i < xpc.xp_numfiles; i++)
4401 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4402 }
4403 vim_free(pat);
4404 ExpandCleanup(&xpc);
4405}
Girish Palya92f68e22025-04-21 11:12:41 +02004406
4407/*
4408 * "cmdcomplete_info()" function
4409 */
4410 void
4411f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4412{
4413 cmdline_info_T *ccline = get_cmdline_info();
4414 dict_T *retdict;
4415 list_T *li;
4416 int idx;
4417 int ret = OK;
4418
4419 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4420 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4421 return;
4422 retdict = rettv->vval.v_dict;
4423 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4424 if (ret == OK)
4425 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4426 if (ret == OK)
4427 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4428 if (ret == OK)
4429 {
4430 li = list_alloc();
4431 if (li == NULL)
4432 return;
4433 ret = dict_add_list(retdict, "matches", li);
4434 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4435 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4436 }
4437}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004438#endif // FEAT_EVAL