blob: 597f78da2e9b1b4a19ddf6269da4bfd7064a278f [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
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010053 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000054 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010055 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020057 && xp->xp_context != EXPAND_STRING_SETTING
58 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000061 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020063 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_TAGS
65 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_USER_LIST);
67}
68
69/*
70 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000071 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
72 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000073 */
74 int
75cmdline_fuzzy_complete(char_u *fuzzystr)
76{
77 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
78}
79
80/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000081 * sort function for the completion matches.
82 * <SNR> functions should be sorted to the end.
83 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020084 static int
85sort_func_compare(const void *s1, const void *s2)
86{
87 char_u *p1 = *(char_u **)s1;
88 char_u *p2 = *(char_u **)s2;
89
90 if (*p1 != '<' && *p2 == '<') return -1;
91 if (*p1 == '<' && *p2 != '<') return 1;
92 return STRCMP(p1, p2);
93}
Bram Moolenaar66b51422019-08-18 21:44:12 +020094
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000095/*
96 * Escape special characters in the cmdline completion matches.
97 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020098 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000099wildescape(
100 expand_T *xp,
101 char_u *str,
102 int numfiles,
103 char_u **files)
104{
105 char_u *p;
106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
108
109 if (xp->xp_context == EXPAND_FILES
110 || xp->xp_context == EXPAND_FILES_IN_PATH
111 || xp->xp_context == EXPAND_SHELLCMD
112 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200113 || xp->xp_context == EXPAND_DIRECTORIES
114 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000115 {
116 // Insert a backslash into a file name before a space, \, %, #
117 // and wildmatch characters, except '~'.
118 for (int i = 0; i < numfiles; ++i)
119 {
120 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000122 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200123 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
124 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129#if defined(BACKSLASH_IN_FILENAME)
130 p = vim_strsave_escaped(files[i], (char_u *)" ");
131 if (p != NULL)
132 {
133 vim_free(files[i]);
134 files[i] = p;
135 }
136#endif
137 }
138 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200139 else if (xp->xp_backslash & XP_BS_COMMA)
140 {
141 if (vim_strchr(files[i], ',') != NULL)
142 {
143 p = vim_strsave_escaped(files[i], (char_u *)",");
144 if (p != NULL)
145 {
146 vim_free(files[i]);
147 files[i] = p;
148 }
149 }
150 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000151#ifdef BACKSLASH_IN_FILENAME
152 p = vim_strsave_fnameescape(files[i], vse_what);
153#else
154 p = vim_strsave_fnameescape(files[i],
155 xp->xp_shell ? VSE_SHELL : vse_what);
156#endif
157 if (p != NULL)
158 {
159 vim_free(files[i]);
160 files[i] = p;
161 }
162
163 // If 'str' starts with "\~", replace "~" at start of
164 // files[i] with "\~".
165 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
166 escape_fname(&files[i]);
167 }
168 xp->xp_backslash = XP_BS_NONE;
169
170 // If the first file starts with a '+' escape it. Otherwise it
171 // could be seen as "+cmd".
172 if (*files[0] == '+')
173 escape_fname(&files[0]);
174 }
175 else if (xp->xp_context == EXPAND_TAGS)
176 {
177 // Insert a backslash before characters in a tag name that
178 // would terminate the ":tag" command.
179 for (int i = 0; i < numfiles; ++i)
180 {
181 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
182 if (p != NULL)
183 {
184 vim_free(files[i]);
185 files[i] = p;
186 }
187 }
188 }
189}
190
191/*
192 * Escape special characters in the cmdline completion matches.
193 */
194 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200195ExpandEscape(
196 expand_T *xp,
197 char_u *str,
198 int numfiles,
199 char_u **files,
200 int options)
201{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200202 // May change home directory back to "~"
203 if (options & WILD_HOME_REPLACE)
204 tilde_replace(str, numfiles, files);
205
206 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000207 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200208}
209
210/*
211 * Return FAIL if this is not an appropriate context in which to do
212 * completion of anything, return OK if it is (even if there are no matches).
213 * For the caller, this means that the character is just passed through like a
214 * normal character (instead of being expanded). This allows :s/^I^D etc.
215 */
216 int
217nextwild(
218 expand_T *xp,
219 int type,
220 int options, // extra options for ExpandOne()
221 int escape) // if TRUE, escape the returned matches
222{
223 cmdline_info_T *ccline = get_cmdline_info();
224 int i, j;
225 char_u *p1;
226 char_u *p2;
227 int difflen;
228 int v;
229
230 if (xp->xp_numfiles == -1)
231 {
Jim Zhou3255af82025-02-27 19:29:50 +0100232#ifdef FEAT_EVAL
233 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
234 {
235 // Expand commands typed in input() function
236 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
237 }
238 else
239#endif
240 {
241 set_expand_context(xp);
242 }
243 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200244 }
245
246 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
247 {
248 beep_flush();
249 return OK; // Something illegal on command line
250 }
251 if (xp->xp_context == EXPAND_NOTHING)
252 {
253 // Caller can use the character as a normal char instead
254 return FAIL;
255 }
256
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000257 // If cmd_silent is set then don't show the dots, because redrawcmd() below
258 // won't remove them.
259 if (!cmd_silent)
260 {
261 msg_puts("..."); // show that we are busy
262 out_flush();
263 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200264
265 i = (int)(xp->xp_pattern - ccline->cmdbuff);
266 xp->xp_pattern_len = ccline->cmdpos - i;
267
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000268 if (type == WILD_NEXT || type == WILD_PREV
269 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 {
271 // Get next/previous match for a previous expanded pattern.
272 p2 = ExpandOne(xp, NULL, NULL, 0, type);
273 }
274 else
275 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000276 if (cmdline_fuzzy_completion_supported(xp))
277 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200278 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000279 else
280 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
281
Bram Moolenaar66b51422019-08-18 21:44:12 +0200282 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000283 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200284 p2 = NULL;
285 else
286 {
287 int use_options = options |
288 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100289 if (use_options & WILD_KEEP_SOLE_ITEM)
290 use_options &= ~WILD_KEEP_SOLE_ITEM;
291
Bram Moolenaar66b51422019-08-18 21:44:12 +0200292 if (escape)
293 use_options |= WILD_ESCAPE;
294
295 if (p_wic)
296 use_options += WILD_ICASE;
297 p2 = ExpandOne(xp, p1,
298 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
299 use_options, type);
300 vim_free(p1);
301 // longest match: make sure it is not shorter, happens with :help
302 if (p2 != NULL && type == WILD_LONGEST)
303 {
304 for (j = 0; j < xp->xp_pattern_len; ++j)
305 if (ccline->cmdbuff[i + j] == '*'
306 || ccline->cmdbuff[i + j] == '?')
307 break;
308 if ((int)STRLEN(p2) < j)
309 VIM_CLEAR(p2);
310 }
311 }
312 }
313
314 if (p2 != NULL && !got_int)
315 {
316 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
317 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
318 {
319 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
320 xp->xp_pattern = ccline->cmdbuff + i;
321 }
322 else
323 v = OK;
324 if (v == OK)
325 {
326 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
327 &ccline->cmdbuff[ccline->cmdpos],
328 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
329 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
330 ccline->cmdlen += difflen;
331 ccline->cmdpos += difflen;
332 }
333 }
334 vim_free(p2);
335
336 redrawcmd();
337 cursorcmd();
338
339 // When expanding a ":map" command and no matches are found, assume that
340 // the key is supposed to be inserted literally
341 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
342 return FAIL;
343
344 if (xp->xp_numfiles <= 0 && p2 == NULL)
345 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100346 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200347 // free expanded pattern
348 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
349
350 return OK;
351}
352
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000353/*
354 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 */
357 static int
358cmdline_pum_create(
359 cmdline_info_T *ccline,
360 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000361 char_u **matches,
362 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000363 int showtail)
364{
365 int i;
366 int columns;
367
368 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000369 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000370 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000371 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000372 {
zeertzjqc51a3762022-12-10 10:22:29 +0000373 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 compl_match_array[i].pum_info = NULL;
375 compl_match_array[i].pum_extra = NULL;
376 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200377 compl_match_array[i].pum_user_abbr_hlattr = -1;
378 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379 }
380
381 // Compute the popup menu starting column
382 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
383 columns = vim_strsize(xp->xp_pattern);
384 if (showtail)
385 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000386 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000387 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000388 }
glepnir977561a2025-02-13 20:48:56 +0100389 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000390
391 // no default selection
392 compl_selected = -1;
393
394 cmdline_pum_display();
395
396 return EXPAND_OK;
397}
398
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399/*
400 * Display the cmdline completion matches in a popup menu
401 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000402 void
403cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404{
405 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
406}
407
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000408/*
409 * Returns TRUE if the cmdline completion popup menu is being displayed.
410 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000411 int
412cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000413{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100414 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000415}
416
417/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000418 * Remove the cmdline completion popup menu (if present), free the list of
419 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 void
422cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000424 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100425 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000426
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000427 pum_undisplay();
428 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000429 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000430 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000431 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000432 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100433
434 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
435 // as a side effect.
436 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000437}
438
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000439 void
440cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000441{
442 cmdline_pum_remove();
443 wildmenu_cleanup(cclp);
444}
445
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000446/*
447 * Returns the starting column number to use for the cmdline completion popup
448 * menu.
449 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000450 int
451cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000452{
453 return compl_startcol;
454}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000455
Bram Moolenaar66b51422019-08-18 21:44:12 +0200456/*
zeertzjqd8c93402024-06-17 18:25:32 +0200457 * Returns the current cmdline completion pattern.
458 */
459 char_u *
460cmdline_compl_pattern(void)
461{
462 expand_T *xp = get_cmdline_info()->xpc;
463
464 return xp == NULL ? NULL : xp->xp_orig;
465}
466
467/*
468 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
469 */
470 int
471cmdline_compl_is_fuzzy(void)
472{
473 expand_T *xp = get_cmdline_info()->xpc;
474
475 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
476}
477
478/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000479 * Return the number of characters that should be skipped in a status match.
480 * These are backslashes used for escaping. Do show backslashes in help tags.
481 */
482 static int
483skip_status_match_char(expand_T *xp, char_u *s)
484{
485 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
486#ifdef FEAT_MENU
487 || ((xp->xp_context == EXPAND_MENUS
488 || xp->xp_context == EXPAND_MENUNAMES)
489 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
490#endif
491 )
492 {
493#ifndef BACKSLASH_IN_FILENAME
494 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
495 return 2;
496#endif
497 return 1;
498 }
499 return 0;
500}
501
502/*
503 * Get the length of an item as it will be shown in the status line.
504 */
505 static int
506status_match_len(expand_T *xp, char_u *s)
507{
508 int len = 0;
509
510#ifdef FEAT_MENU
511 int emenu = xp->xp_context == EXPAND_MENUS
512 || xp->xp_context == EXPAND_MENUNAMES;
513
514 // Check for menu separators - replace with '|'.
515 if (emenu && menu_is_separator(s))
516 return 1;
517#endif
518
519 while (*s != NUL)
520 {
521 s += skip_status_match_char(xp, s);
522 len += ptr2cells(s);
523 MB_PTR_ADV(s);
524 }
525
526 return len;
527}
528
529/*
530 * Show wildchar matches in the status line.
531 * Show at least the "match" item.
532 * We start at item 'first_match' in the list and show all matches that fit.
533 *
534 * If inversion is possible we use it. Else '=' characters are used.
535 */
536 static void
537win_redr_status_matches(
538 expand_T *xp,
539 int num_matches,
540 char_u **matches, // list of matches
541 int match,
542 int showtail)
543{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000544 int row;
545 char_u *buf;
546 int len;
547 int clen; // length in screen cells
548 int fillchar;
549 int attr;
550 int i;
551 int highlight = TRUE;
552 char_u *selstart = NULL;
553 int selstart_col = 0;
554 char_u *selend = NULL;
555 static int first_match = 0;
556 int add_left = FALSE;
557 char_u *s;
558#ifdef FEAT_MENU
559 int emenu;
560#endif
561 int l;
562
563 if (matches == NULL) // interrupted completion?
564 return;
565
566 if (has_mbyte)
567 buf = alloc(Columns * MB_MAXBYTES + 1);
568 else
569 buf = alloc(Columns + 1);
570 if (buf == NULL)
571 return;
572
573 if (match == -1) // don't show match but original text
574 {
575 match = 0;
576 highlight = FALSE;
577 }
578 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000579 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000580 if (match == 0)
581 first_match = 0;
582 else if (match < first_match)
583 {
584 // jumping left, as far as we can go
585 first_match = match;
586 add_left = TRUE;
587 }
588 else
589 {
590 // check if match fits on the screen
591 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000592 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 if (first_match > 0)
594 clen += 2;
595 // jumping right, put match at the left
596 if ((long)clen > Columns)
597 {
598 first_match = match;
599 // if showing the last match, we can add some on the left
600 clen = 2;
601 for (i = match; i < num_matches; ++i)
602 {
zeertzjqc51a3762022-12-10 10:22:29 +0000603 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000604 if ((long)clen >= Columns)
605 break;
606 }
607 if (i == num_matches)
608 add_left = TRUE;
609 }
610 }
611 if (add_left)
612 while (first_match > 0)
613 {
zeertzjqc51a3762022-12-10 10:22:29 +0000614 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000615 if ((long)clen >= Columns)
616 break;
617 --first_match;
618 }
619
620 fillchar = fillchar_status(&attr, curwin);
621
622 if (first_match == 0)
623 {
624 *buf = NUL;
625 len = 0;
626 }
627 else
628 {
629 STRCPY(buf, "< ");
630 len = 2;
631 }
632 clen = len;
633
634 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000635 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000636 {
637 if (i == match)
638 {
639 selstart = buf + len;
640 selstart_col = clen;
641 }
642
zeertzjqc51a3762022-12-10 10:22:29 +0000643 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000644 // Check for menu separators - replace with '|'
645#ifdef FEAT_MENU
646 emenu = (xp->xp_context == EXPAND_MENUS
647 || xp->xp_context == EXPAND_MENUNAMES);
648 if (emenu && menu_is_separator(s))
649 {
650 STRCPY(buf + len, transchar('|'));
651 l = (int)STRLEN(buf + len);
652 len += l;
653 clen += l;
654 }
655 else
656#endif
657 for ( ; *s != NUL; ++s)
658 {
659 s += skip_status_match_char(xp, s);
660 clen += ptr2cells(s);
661 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
662 {
663 STRNCPY(buf + len, s, l);
664 s += l - 1;
665 len += l;
666 }
667 else
668 {
669 STRCPY(buf + len, transchar_byte(*s));
670 len += (int)STRLEN(buf + len);
671 }
672 }
673 if (i == match)
674 selend = buf + len;
675
676 *(buf + len++) = ' ';
677 *(buf + len++) = ' ';
678 clen += 2;
679 if (++i == num_matches)
680 break;
681 }
682
683 if (i != num_matches)
684 {
685 *(buf + len++) = '>';
686 ++clen;
687 }
688
689 buf[len] = NUL;
690
691 row = cmdline_row - 1;
692 if (row >= 0)
693 {
694 if (wild_menu_showing == 0)
695 {
696 if (msg_scrolled > 0)
697 {
698 // Put the wildmenu just above the command line. If there is
699 // no room, scroll the screen one line up.
700 if (cmdline_row == Rows - 1)
701 {
702 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
703 ++msg_scrolled;
704 }
705 else
706 {
707 ++cmdline_row;
708 ++row;
709 }
710 wild_menu_showing = WM_SCROLLED;
711 }
712 else
713 {
714 // Create status line if needed by setting 'laststatus' to 2.
715 // Set 'winminheight' to zero to avoid that the window is
716 // resized.
717 if (lastwin->w_status_height == 0)
718 {
719 save_p_ls = p_ls;
720 save_p_wmh = p_wmh;
721 p_ls = 2;
722 p_wmh = 0;
723 last_status(FALSE);
724 }
725 wild_menu_showing = WM_SHOWN;
726 }
727 }
728
729 screen_puts(buf, row, 0, attr);
730 if (selstart != NULL && highlight)
731 {
732 *selend = NUL;
733 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
734 }
735
736 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
737 }
738
739 win_redraw_last_status(topframe);
740 vim_free(buf);
741}
742
743/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000744 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200745 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000746 */
747 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100748get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000749{
glepnir977561a2025-02-13 20:48:56 +0100750 int findex = xp->xp_selected;
751 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000752
zeertzjqb6c900b2025-02-14 17:59:31 +0100753 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000754 if (xp->xp_numfiles <= 0)
755 return NULL;
756
757 if (mode == WILD_PREV)
758 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100759 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000760 if (findex == -1)
761 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100762 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000763 --findex;
764 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000765 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000766 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100767 // Select the next entry
768 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000769 }
glepnir977561a2025-02-13 20:48:56 +0100770 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000771 {
glepnir977561a2025-02-13 20:48:56 +0100772 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
773 ht = pum_get_height();
774 if (ht > 3)
775 ht -= 2;
776
777 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000778 {
glepnir977561a2025-02-13 20:48:56 +0100779 if (findex == 0)
780 // at the first entry, don't select any entries
781 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100782 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100783 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000784 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100785 else
786 // go up by the pum height
787 findex = MAX(findex - ht, 0);
788 }
789 else // mode == WILD_PAGEDOWN
790 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100791 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100792 // at the last entry, don't select any entries
793 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100794 else if (findex < 0)
795 // no entry is selected, select the first entry
796 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100797 else
798 // go down by the pum height
799 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000800 }
801 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000802
glepnir977561a2025-02-13 20:48:56 +0100803 // Handle wrapping around
804 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000805 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100806 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100807 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000808 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000809 else
glepnir977561a2025-02-13 20:48:56 +0100810 // Wrap around to opposite end
811 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000812 }
glepnir977561a2025-02-13 20:48:56 +0100813
814 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000815 if (compl_match_array)
816 {
817 compl_selected = findex;
818 cmdline_pum_display();
819 }
820 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100821 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
822 cmd_showtail);
823
zeertzjqe9ef3472023-08-17 23:57:05 +0200824 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100825 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100826 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000827}
828
829/*
830 * Start the command-line expansion and get the matches.
831 */
832 static char_u *
833ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
834{
835 int non_suf_match; // number without matching suffix
836 int i;
837 char_u *ss = NULL;
838
839 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000840 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000841 options) == FAIL)
842 {
843#ifdef FNAME_ILLEGAL
844 // Illegal file name has been silently skipped. But when there
845 // are wildcards, the real problem is that there was no match,
846 // causing the pattern to be added, which has illegal characters.
847 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
848 semsg(_(e_no_match_str_2), str);
849#endif
850 }
851 else if (xp->xp_numfiles == 0)
852 {
853 if (!(options & WILD_SILENT))
854 semsg(_(e_no_match_str_2), str);
855 }
856 else
857 {
858 // Escape the matches for use on the command line.
859 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
860
861 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000862 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000863 {
864 if (xp->xp_numfiles)
865 non_suf_match = xp->xp_numfiles;
866 else
867 non_suf_match = 1;
868 if ((xp->xp_context == EXPAND_FILES
869 || xp->xp_context == EXPAND_DIRECTORIES)
870 && xp->xp_numfiles > 1)
871 {
872 // More than one match; check suffix.
873 // The files will have been sorted on matching suffix in
874 // expand_wildcards, only need to check the first two.
875 non_suf_match = 0;
876 for (i = 0; i < 2; ++i)
877 if (match_suffix(xp->xp_files[i]))
878 ++non_suf_match;
879 }
880 if (non_suf_match != 1)
881 {
882 // Can we ever get here unless it's while expanding
883 // interactively? If not, we can get rid of this all
884 // together. Don't really want to wait for this message
885 // (and possibly have to hit return to continue!).
886 if (!(options & WILD_SILENT))
887 emsg(_(e_too_many_file_names));
888 else if (!(options & WILD_NO_BEEP))
889 beep_flush();
890 }
891 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
892 ss = vim_strsave(xp->xp_files[0]);
893 }
894 }
895
896 return ss;
897}
898
899/*
900 * Return the longest common part in the list of cmdline completion matches.
901 */
902 static char_u *
903find_longest_match(expand_T *xp, int options)
904{
905 long_u len;
906 int mb_len = 1;
907 int c0, ci;
908 int i;
909 char_u *ss;
910
911 for (len = 0; xp->xp_files[0][len]; len += mb_len)
912 {
913 if (has_mbyte)
914 {
915 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
916 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
917 }
918 else
919 c0 = xp->xp_files[0][len];
920 for (i = 1; i < xp->xp_numfiles; ++i)
921 {
922 if (has_mbyte)
923 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
924 else
925 ci = xp->xp_files[i][len];
926 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
927 || xp->xp_context == EXPAND_FILES
928 || xp->xp_context == EXPAND_SHELLCMD
929 || xp->xp_context == EXPAND_BUFFERS))
930 {
931 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
932 break;
933 }
934 else if (c0 != ci)
935 break;
936 }
937 if (i < xp->xp_numfiles)
938 {
939 if (!(options & WILD_NO_BEEP))
940 vim_beep(BO_WILD);
941 break;
942 }
943 }
944
945 ss = alloc(len + 1);
946 if (ss)
947 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
948
949 return ss;
950}
951
952/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000953 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200954 * Chars that should not be expanded must be preceded with a backslash.
955 * Return a pointer to allocated memory containing the new string.
956 * Return NULL for failure.
957 *
958 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200959 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
960 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200961 *
962 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
963 * is WILD_EXPAND_FREE or WILD_ALL.
964 *
965 * mode = WILD_FREE: just free previously expanded matches
966 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
967 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
968 * mode = WILD_NEXT: use next match in multiple match, wrap to first
969 * mode = WILD_PREV: use previous match in multiple match, wrap to first
970 * mode = WILD_ALL: return all matches concatenated
971 * mode = WILD_LONGEST: return longest matched part
972 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000973 * mode = WILD_APPLY: apply the item selected in the cmdline completion
974 * popup menu and close the menu.
975 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
976 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200977 *
978 * options = WILD_LIST_NOTFOUND: list entries without a match
979 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
980 * options = WILD_USE_NL: Use '\n' for WILD_ALL
981 * options = WILD_NO_BEEP: Don't beep for multiple matches
982 * options = WILD_ADD_SLASH: add a slash after directory names
983 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
984 * options = WILD_SILENT: don't print warning messages
985 * options = WILD_ESCAPE: put backslash before special chars
986 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200987 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200988 *
989 * The variables xp->xp_context and xp->xp_backslash must have been set!
990 */
991 char_u *
992ExpandOne(
993 expand_T *xp,
994 char_u *str,
995 char_u *orig, // allocated copy of original of expanded string
996 int options,
997 int mode)
998{
999 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001000 int orig_saved = FALSE;
1001 int i;
1002 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003
1004 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001005 if (mode == WILD_NEXT || mode == WILD_PREV
1006 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001007 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001008
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001009 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001010 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001011 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001012 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001013 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001014 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001015
Bram Moolenaar66b51422019-08-18 21:44:12 +02001016 // free old names
1017 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1018 {
1019 FreeWild(xp->xp_numfiles, xp->xp_files);
1020 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001021 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001022
1023 // The entries from xp_files may be used in the PUM, remove it.
1024 if (compl_match_array != NULL)
1025 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001027 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001028
1029 if (mode == WILD_FREE) // only release file name
1030 return NULL;
1031
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001032 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 {
zeertzjq28a23602023-09-29 19:58:35 +02001034 vim_free(xp->xp_orig);
1035 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001036 orig_saved = TRUE;
1037
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001038 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039 }
1040
1041 // Find longest common part
1042 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1043 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001044 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001045 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 }
1047
Bram Moolenaar57e95172022-08-20 19:26:14 +01001048 // Concatenate all matching names. Unless interrupted, this can be slow
1049 // and the result probably won't be used.
1050 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001051 {
1052 len = 0;
1053 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001054 {
1055 if (i > 0)
1056 {
1057 if (xp->xp_prefix == XP_PREFIX_NO)
1058 len += 2; // prefix "no"
1059 else if (xp->xp_prefix == XP_PREFIX_INV)
1060 len += 3; // prefix "inv"
1061 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001062 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001063 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 ss = alloc(len);
1065 if (ss != NULL)
1066 {
1067 *ss = NUL;
1068 for (i = 0; i < xp->xp_numfiles; ++i)
1069 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001070 if (i > 0)
1071 {
1072 if (xp->xp_prefix == XP_PREFIX_NO)
1073 STRCAT(ss, "no");
1074 else if (xp->xp_prefix == XP_PREFIX_INV)
1075 STRCAT(ss, "inv");
1076 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001078
Bram Moolenaar66b51422019-08-18 21:44:12 +02001079 if (i != xp->xp_numfiles - 1)
1080 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1081 }
1082 }
1083 }
1084
1085 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1086 ExpandCleanup(xp);
1087
zeertzjq28a23602023-09-29 19:58:35 +02001088 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001089 if (!orig_saved)
1090 vim_free(orig);
1091
1092 return ss;
1093}
1094
1095/*
1096 * Prepare an expand structure for use.
1097 */
1098 void
1099ExpandInit(expand_T *xp)
1100{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001101 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001103 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001104 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001105}
1106
1107/*
1108 * Cleanup an expand structure after use.
1109 */
1110 void
1111ExpandCleanup(expand_T *xp)
1112{
1113 if (xp->xp_numfiles >= 0)
1114 {
1115 FreeWild(xp->xp_numfiles, xp->xp_files);
1116 xp->xp_numfiles = -1;
1117 }
zeertzjq28a23602023-09-29 19:58:35 +02001118 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001119}
1120
1121/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001122 * Display one line of completion matches. Multiple matches are displayed in
1123 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001124 * matches - list of completion match names
1125 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001126 * lines - number of output lines
1127 * linenr - line number of matches to display
1128 * maxlen - maximum number of characters in each line
1129 * showtail - display only the tail of the full path of a file name
1130 * dir_attr - highlight attribute to use for directory names
1131 */
1132 static void
1133showmatches_oneline(
1134 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001135 char_u **matches,
1136 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001137 int lines,
1138 int linenr,
1139 int maxlen,
1140 int showtail,
1141 int dir_attr)
1142{
1143 int i, j;
1144 int isdir;
1145 int lastlen;
1146 char_u *p;
1147
1148 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001149 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001150 {
1151 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1152 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001153 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1154 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001155 msg_advance(maxlen + 1);
1156 msg_puts((char *)p);
1157 msg_advance(maxlen + 3);
1158 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1159 break;
1160 }
1161 for (i = maxlen - lastlen; --i >= 0; )
1162 msg_putchar(' ');
1163 if (xp->xp_context == EXPAND_FILES
1164 || xp->xp_context == EXPAND_SHELLCMD
1165 || xp->xp_context == EXPAND_BUFFERS)
1166 {
1167 // highlight directories
1168 if (xp->xp_numfiles != -1)
1169 {
1170 char_u *halved_slash;
1171 char_u *exp_path;
1172 char_u *path;
1173
1174 // Expansion was done before and special characters
1175 // were escaped, need to halve backslashes. Also
1176 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001177 exp_path = expand_env_save_opt(matches[j], TRUE);
1178 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001179 halved_slash = backslash_halve_save(path);
1180 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001181 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001182 vim_free(exp_path);
1183 if (halved_slash != path)
1184 vim_free(halved_slash);
1185 }
1186 else
1187 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001188 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001189 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001190 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001191 else
1192 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001193 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001194 TRUE);
1195 p = NameBuff;
1196 }
1197 }
1198 else
1199 {
1200 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001201 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001202 }
1203 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1204 }
1205 if (msg_col > 0) // when not wrapped around
1206 {
1207 msg_clr_eos();
1208 msg_putchar('\n');
1209 }
1210 out_flush(); // show one line at a time
1211}
1212
1213/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001214 * Show all matches for completion on the command line.
1215 * Returns EXPAND_NOTHING when the character that triggered expansion should
1216 * be inserted like a normal character.
1217 */
1218 int
1219showmatches(expand_T *xp, int wildmenu UNUSED)
1220{
1221 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001222 int numMatches;
1223 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001224 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001225 int maxlen;
1226 int lines;
1227 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001228 int attr;
1229 int showtail;
1230
1231 if (xp->xp_numfiles == -1)
1232 {
1233 set_expand_context(xp);
1234 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001236 showtail = expand_showtail(xp);
1237 if (i != EXPAND_OK)
1238 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001239 }
1240 else
1241 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001242 numMatches = xp->xp_numfiles;
1243 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244 showtail = cmd_showtail;
1245 }
1246
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001247 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001248 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001249 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001250
Bram Moolenaar66b51422019-08-18 21:44:12 +02001251 if (!wildmenu)
1252 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001253 msg_didany = FALSE; // lines_left will be set
1254 msg_start(); // prepare for paging
1255 msg_putchar('\n');
1256 out_flush();
1257 cmdline_row = msg_row;
1258 msg_didany = FALSE; // lines_left will be set again
1259 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001260 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261
1262 if (got_int)
1263 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001264 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001265 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001266 else
1267 {
1268 // find the length of the longest file name
1269 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001270 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271 {
1272 if (!showtail && (xp->xp_context == EXPAND_FILES
1273 || xp->xp_context == EXPAND_SHELLCMD
1274 || xp->xp_context == EXPAND_BUFFERS))
1275 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001276 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001277 j = vim_strsize(NameBuff);
1278 }
1279 else
zeertzjqc51a3762022-12-10 10:22:29 +00001280 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001281 if (j > maxlen)
1282 maxlen = j;
1283 }
1284
1285 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001286 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287 else
1288 {
1289 // compute the number of columns and lines for the listing
1290 maxlen += 2; // two spaces between file names
1291 columns = ((int)Columns + 2) / maxlen;
1292 if (columns < 1)
1293 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001294 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001295 }
1296
1297 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1298
1299 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1300 {
1301 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1302 msg_clr_eos();
1303 msg_advance(maxlen - 3);
1304 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1305 }
1306
1307 // list the files line by line
1308 for (i = 0; i < lines; ++i)
1309 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001310 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001311 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001312 if (got_int)
1313 {
1314 got_int = FALSE;
1315 break;
1316 }
1317 }
1318
1319 // we redraw the command below the lines that we have just listed
1320 // This is a bit tricky, but it saves a lot of screen updating.
1321 cmdline_row = msg_row; // will put it back later
1322 }
1323
1324 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001325 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001326
1327 return EXPAND_OK;
1328}
1329
1330/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001331 * gettail() version for showmatches() and win_redr_status_matches():
1332 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001333 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001334 static char_u *
1335showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001336{
1337 char_u *p;
1338 char_u *t = s;
1339 int had_sep = FALSE;
1340
1341 for (p = s; *p != NUL; )
1342 {
1343 if (vim_ispathsep(*p)
1344#ifdef BACKSLASH_IN_FILENAME
1345 && !rem_backslash(p)
1346#endif
1347 )
1348 had_sep = TRUE;
1349 else if (had_sep)
1350 {
1351 t = p;
1352 had_sep = FALSE;
1353 }
1354 MB_PTR_ADV(p);
1355 }
1356 return t;
1357}
1358
1359/*
1360 * Return TRUE if we only need to show the tail of completion matches.
1361 * When not completing file names or there is a wildcard in the path FALSE is
1362 * returned.
1363 */
1364 static int
1365expand_showtail(expand_T *xp)
1366{
1367 char_u *s;
1368 char_u *end;
1369
1370 // When not completing file names a "/" may mean something different.
1371 if (xp->xp_context != EXPAND_FILES
1372 && xp->xp_context != EXPAND_SHELLCMD
1373 && xp->xp_context != EXPAND_DIRECTORIES)
1374 return FALSE;
1375
1376 end = gettail(xp->xp_pattern);
1377 if (end == xp->xp_pattern) // there is no path separator
1378 return FALSE;
1379
1380 for (s = xp->xp_pattern; s < end; s++)
1381 {
1382 // Skip escaped wildcards. Only when the backslash is not a path
1383 // separator, on DOS the '*' "path\*\file" must not be skipped.
1384 if (rem_backslash(s))
1385 ++s;
1386 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1387 return FALSE;
1388 }
1389 return TRUE;
1390}
1391
1392/*
1393 * Prepare a string for expansion.
1394 * When expanding file names: The string will be used with expand_wildcards().
1395 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1396 * When expanding other names: The string will be used with regcomp(). Copy
1397 * the name into allocated memory and prepend "^".
1398 */
1399 char_u *
1400addstar(
1401 char_u *fname,
1402 int len,
1403 int context) // EXPAND_FILES etc.
1404{
1405 char_u *retval;
1406 int i, j;
1407 int new_len;
1408 char_u *tail;
1409 int ends_in_star;
1410
1411 if (context != EXPAND_FILES
1412 && context != EXPAND_FILES_IN_PATH
1413 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001414 && context != EXPAND_DIRECTORIES
1415 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001416 {
1417 // Matching will be done internally (on something other than files).
1418 // So we convert the file-matching-type wildcards into our kind for
1419 // use with vim_regcomp(). First work out how long it will be:
1420
1421 // For help tags the translation is done in find_help_tags().
1422 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001423 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001424 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001425 || context == EXPAND_COLORS
1426 || context == EXPAND_COMPILER
1427 || context == EXPAND_OWNSYNTAX
1428 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001429 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001430 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001431 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001432 || ((context == EXPAND_TAGS_LISTFILES
1433 || context == EXPAND_TAGS)
1434 && fname[0] == '/'))
1435 retval = vim_strnsave(fname, len);
1436 else
1437 {
1438 new_len = len + 2; // +2 for '^' at start, NUL at end
1439 for (i = 0; i < len; i++)
1440 {
1441 if (fname[i] == '*' || fname[i] == '~')
1442 new_len++; // '*' needs to be replaced by ".*"
1443 // '~' needs to be replaced by "\~"
1444
1445 // Buffer names are like file names. "." should be literal
1446 if (context == EXPAND_BUFFERS && fname[i] == '.')
1447 new_len++; // "." becomes "\."
1448
1449 // Custom expansion takes care of special things, match
1450 // backslashes literally (perhaps also for other types?)
1451 if ((context == EXPAND_USER_DEFINED
1452 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1453 new_len++; // '\' becomes "\\"
1454 }
1455 retval = alloc(new_len);
1456 if (retval != NULL)
1457 {
1458 retval[0] = '^';
1459 j = 1;
1460 for (i = 0; i < len; i++, j++)
1461 {
1462 // Skip backslash. But why? At least keep it for custom
1463 // expansion.
1464 if (context != EXPAND_USER_DEFINED
1465 && context != EXPAND_USER_LIST
1466 && fname[i] == '\\'
1467 && ++i == len)
1468 break;
1469
1470 switch (fname[i])
1471 {
1472 case '*': retval[j++] = '.';
1473 break;
1474 case '~': retval[j++] = '\\';
1475 break;
1476 case '?': retval[j] = '.';
1477 continue;
1478 case '.': if (context == EXPAND_BUFFERS)
1479 retval[j++] = '\\';
1480 break;
1481 case '\\': if (context == EXPAND_USER_DEFINED
1482 || context == EXPAND_USER_LIST)
1483 retval[j++] = '\\';
1484 break;
1485 }
1486 retval[j] = fname[i];
1487 }
1488 retval[j] = NUL;
1489 }
1490 }
1491 }
1492 else
1493 {
1494 retval = alloc(len + 4);
1495 if (retval != NULL)
1496 {
1497 vim_strncpy(retval, fname, len);
1498
1499 // Don't add a star to *, ~, ~user, $var or `cmd`.
1500 // * would become **, which walks the whole tree.
1501 // ~ would be at the start of the file name, but not the tail.
1502 // $ could be anywhere in the tail.
1503 // ` could be anywhere in the file name.
1504 // When the name ends in '$' don't add a star, remove the '$'.
1505 tail = gettail(retval);
1506 ends_in_star = (len > 0 && retval[len - 1] == '*');
1507#ifndef BACKSLASH_IN_FILENAME
1508 for (i = len - 2; i >= 0; --i)
1509 {
1510 if (retval[i] != '\\')
1511 break;
1512 ends_in_star = !ends_in_star;
1513 }
1514#endif
1515 if ((*retval != '~' || tail != retval)
1516 && !ends_in_star
1517 && vim_strchr(tail, '$') == NULL
1518 && vim_strchr(retval, '`') == NULL)
1519 retval[len++] = '*';
1520 else if (len > 0 && retval[len - 1] == '$')
1521 --len;
1522 retval[len] = NUL;
1523 }
1524 }
1525 return retval;
1526}
1527
1528/*
1529 * Must parse the command line so far to work out what context we are in.
1530 * Completion can then be done based on that context.
1531 * This routine sets the variables:
1532 * xp->xp_pattern The start of the pattern to be expanded within
1533 * the command line (ends at the cursor).
1534 * xp->xp_context The type of thing to expand. Will be one of:
1535 *
1536 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1537 * the command line, like an unknown command. Caller
1538 * should beep.
1539 * EXPAND_NOTHING Unrecognised context for completion, use char like
1540 * a normal char, rather than for completion. eg
1541 * :s/^I/
1542 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1543 * it.
1544 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1545 * EXPAND_FILES After command with EX_XFILE set, or after setting
1546 * with P_EXPAND set. eg :e ^I, :w>>^I
1547 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001548 * when we know only directories are of interest.
1549 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001550 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1551 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1552 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1553 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1554 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1555 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1556 * EXPAND_EVENTS Complete event names
1557 * EXPAND_SYNTAX Complete :syntax command arguments
1558 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1559 * EXPAND_AUGROUP Complete autocommand group names
1560 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1561 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1562 * eg :unmap a^I , :cunab x^I
1563 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1564 * eg :call sub^I
1565 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1566 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1567 * names in expressions, eg :while s^I
1568 * EXPAND_ENV_VARS Complete environment variable names
1569 * EXPAND_USER Complete user names
1570 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001571 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001572set_expand_context(expand_T *xp)
1573{
1574 cmdline_info_T *ccline = get_cmdline_info();
1575
1576 // only expansion for ':', '>' and '=' command-lines
1577 if (ccline->cmdfirstc != ':'
1578#ifdef FEAT_EVAL
1579 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1580 && !ccline->input_fn
1581#endif
1582 )
1583 {
1584 xp->xp_context = EXPAND_NOTHING;
1585 return;
1586 }
1587 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1588}
1589
Bram Moolenaard0190392019-08-23 21:17:35 +02001590/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001591 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1592 * For user defined commands, the completion context is set in 'xp' and the
1593 * completion flags in 'complp'.
1594 *
1595 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001596 */
1597 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001598set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001599{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001600 char_u *p = NULL;
1601 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001602 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001603
1604 // Isolate the command and search for it in the command table.
1605 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001606 // - the 'k' command can directly be followed by any character, but do
1607 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1608 // find matches anywhere in the command name, do this only for command
1609 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001611 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001612 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001613 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001614 p = cmd + 1;
1615 }
1616 else
1617 {
1618 p = cmd;
1619 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1620 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001621 // A user command may contain digits.
1622 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1623 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001624 while (ASCII_ISALNUM(*p) || *p == '*')
1625 ++p;
1626 // for python 3.x: ":py3*" commands completion
1627 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1628 {
1629 ++p;
1630 while (ASCII_ISALPHA(*p) || *p == '*')
1631 ++p;
1632 }
1633 // check for non-alpha command
1634 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1635 ++p;
1636 len = (int)(p - cmd);
1637
1638 if (len == 0)
1639 {
1640 xp->xp_context = EXPAND_UNSUCCESSFUL;
1641 return NULL;
1642 }
1643
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001644 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001645
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001646 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001647 // Also when doing fuzzy expansion for non-shell commands, support
1648 // alphanumeric characters.
1649 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1650 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001651 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1652 ++p;
1653 }
1654
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001655 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001656 // character, complete the command name.
1657 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1658 return NULL;
1659
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001661 {
1662 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1663 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001664 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 p = cmd + 1;
1666 }
1667 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1668 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 eap->cmd = cmd;
1670 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001673 }
1674 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001675 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001676 {
1677 // Not still touching the command and it was an illegal one
1678 xp->xp_context = EXPAND_UNSUCCESSFUL;
1679 return NULL;
1680 }
1681
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682 return p;
1683}
Bram Moolenaard0190392019-08-23 21:17:35 +02001684
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685/*
1686 * Set the completion context for a command argument with wild card characters.
1687 */
1688 static void
1689set_context_for_wildcard_arg(
1690 exarg_T *eap,
1691 char_u *arg,
1692 int usefilter,
1693 expand_T *xp,
1694 int *complp)
1695{
1696 char_u *p;
1697 int c;
1698 int in_quote = FALSE;
1699 char_u *bow = NULL; // Beginning of word
1700 int len = 0;
1701
1702 // Allow spaces within back-quotes to count as part of the argument
1703 // being expanded.
1704 xp->xp_pattern = skipwhite(arg);
1705 p = xp->xp_pattern;
1706 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 if (has_mbyte)
1709 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001710 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001711 c = *p;
1712 if (c == '\\' && p[1] != NUL)
1713 ++p;
1714 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001715 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001716 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001718 xp->xp_pattern = p;
1719 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 in_quote = !in_quote;
1722 }
1723 // An argument can contain just about everything, except
1724 // characters that end the command and white space.
1725 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001726#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001727 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001728#endif
1729 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 {
1731 len = 0; // avoid getting stuck when space is in 'isfname'
1732 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001734 if (has_mbyte)
1735 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001736 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 c = *p;
1738 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 if (has_mbyte)
1741 len = (*mb_ptr2len)(p);
1742 else
1743 len = 1;
1744 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001745 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001746 if (in_quote)
1747 bow = p;
1748 else
1749 xp->xp_pattern = p;
1750 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001751 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001753 }
1754
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 // If we are still inside the quotes, and we passed a space, just
1756 // expand from there.
1757 if (bow != NULL && in_quote)
1758 xp->xp_pattern = bow;
1759 xp->xp_context = EXPAND_FILES;
1760
1761 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001762 if (usefilter
1763 || (eap != NULL
1764 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1765 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001766 {
1767#ifndef BACKSLASH_IN_FILENAME
1768 xp->xp_shell = TRUE;
1769#endif
1770 // When still after the command name expand executables.
1771 if (xp->xp_pattern == skipwhite(arg))
1772 xp->xp_context = EXPAND_SHELLCMD;
1773 }
1774
1775 // Check for environment variable.
1776 if (*xp->xp_pattern == '$')
1777 {
1778 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1779 if (!vim_isIDc(*p))
1780 break;
1781 if (*p == NUL)
1782 {
1783 xp->xp_context = EXPAND_ENV_VARS;
1784 ++xp->xp_pattern;
1785 // Avoid that the assignment uses EXPAND_FILES again.
1786 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1787 *complp = EXPAND_ENV_VARS;
1788 }
1789 }
1790 // Check for user names.
1791 if (*xp->xp_pattern == '~')
1792 {
1793 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1794 ;
1795 // Complete ~user only if it partially matches a user name.
1796 // A full match ~user<Tab> will be replaced by user's home
1797 // directory i.e. something like ~user<Tab> -> /home/user/
1798 if (*p == NUL && p > xp->xp_pattern + 1
1799 && match_user(xp->xp_pattern + 1) >= 1)
1800 {
1801 xp->xp_context = EXPAND_USER;
1802 ++xp->xp_pattern;
1803 }
1804 }
1805}
1806
1807/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001808 * Set the completion context for the "++opt=arg" argument. Always returns
1809 * NULL.
1810 */
1811 static char_u *
1812set_context_in_argopt(expand_T *xp, char_u *arg)
1813{
1814 char_u *p;
1815
1816 p = vim_strchr(arg, '=');
1817 if (p == NULL)
1818 xp->xp_pattern = arg;
1819 else
1820 xp->xp_pattern = p + 1;
1821
1822 xp->xp_context = EXPAND_ARGOPT;
1823 return NULL;
1824}
1825
1826#ifdef FEAT_TERMINAL
1827/*
1828 * Set the completion context for :terminal's [options]. Always returns NULL.
1829 */
1830 static char_u *
1831set_context_in_terminalopt(expand_T *xp, char_u *arg)
1832{
1833 char_u *p;
1834
1835 p = vim_strchr(arg, '=');
1836 if (p == NULL)
1837 xp->xp_pattern = arg;
1838 else
1839 xp->xp_pattern = p + 1;
1840
1841 xp->xp_context = EXPAND_TERMINALOPT;
1842 return NULL;
1843}
1844#endif
1845
1846/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001847 * Set the completion context for the :filter command. Returns a pointer to the
1848 * next command after the :filter command.
1849 */
1850 static char_u *
1851set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1852{
1853 if (*arg != NUL)
1854 arg = skip_vimgrep_pat(arg, NULL, NULL);
1855 if (arg == NULL || *arg == NUL)
1856 {
1857 xp->xp_context = EXPAND_NOTHING;
1858 return NULL;
1859 }
1860 return skipwhite(arg);
1861}
1862
1863#ifdef FEAT_SEARCH_EXTRA
1864/*
1865 * Set the completion context for the :match command. Returns a pointer to the
1866 * next command after the :match command.
1867 */
1868 static char_u *
1869set_context_in_match_cmd(expand_T *xp, char_u *arg)
1870{
1871 if (*arg == NUL || !ends_excmd(*arg))
1872 {
1873 // also complete "None"
1874 set_context_in_echohl_cmd(xp, arg);
1875 arg = skipwhite(skiptowhite(arg));
1876 if (*arg != NUL)
1877 {
1878 xp->xp_context = EXPAND_NOTHING;
1879 arg = skip_regexp(arg + 1, *arg, magic_isset());
1880 }
1881 }
1882 return find_nextcmd(arg);
1883}
1884#endif
1885
1886/*
1887 * Returns a pointer to the next command after a :global or a :v command.
1888 * Returns NULL if there is no next command.
1889 */
1890 static char_u *
1891find_cmd_after_global_cmd(char_u *arg)
1892{
1893 int delim;
1894
1895 delim = *arg; // get the delimiter
1896 if (delim)
1897 ++arg; // skip delimiter if there is one
1898
1899 while (arg[0] != NUL && arg[0] != delim)
1900 {
1901 if (arg[0] == '\\' && arg[1] != NUL)
1902 ++arg;
1903 ++arg;
1904 }
1905 if (arg[0] != NUL)
1906 return arg + 1;
1907
1908 return NULL;
1909}
1910
1911/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001912 * Returns a pointer to the next command after a :substitute or a :& command.
1913 * Returns NULL if there is no next command.
1914 */
1915 static char_u *
1916find_cmd_after_substitute_cmd(char_u *arg)
1917{
1918 int delim;
1919
1920 delim = *arg;
1921 if (delim)
1922 {
1923 // skip "from" part
1924 ++arg;
1925 arg = skip_regexp(arg, delim, magic_isset());
1926
1927 if (arg[0] != NUL && arg[0] == delim)
1928 {
1929 // skip "to" part
1930 ++arg;
1931 while (arg[0] != NUL && arg[0] != delim)
1932 {
1933 if (arg[0] == '\\' && arg[1] != NUL)
1934 ++arg;
1935 ++arg;
1936 }
1937 if (arg[0] != NUL) // skip delimiter
1938 ++arg;
1939 }
1940 }
1941 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1942 ++arg;
1943 if (arg[0] != NUL)
1944 return arg;
1945
1946 return NULL;
1947}
1948
1949/*
1950 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1951 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1952 * Returns NULL if there is no next command.
1953 */
1954 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001955find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001956{
1957 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001958 if (*arg != '/')
1959 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001960
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001961 // Match regexp, not just whole words
1962 for (++arg; *arg && *arg != '/'; arg++)
1963 if (*arg == '\\' && arg[1] != NUL)
1964 arg++;
1965 if (*arg)
1966 {
1967 arg = skipwhite(arg + 1);
1968
1969 // Check for trailing illegal characters
1970 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1971 xp->xp_context = EXPAND_NOTHING;
1972 else
1973 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001974 }
1975
1976 return NULL;
1977}
1978
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001979#ifdef FEAT_EVAL
1980/*
1981 * Set the completion context for the :unlet command. Always returns NULL.
1982 */
1983 static char_u *
1984set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1985{
1986 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1987 arg = xp->xp_pattern + 1;
1988
1989 xp->xp_context = EXPAND_USER_VARS;
1990 xp->xp_pattern = arg;
1991
1992 if (*xp->xp_pattern == '$')
1993 {
1994 xp->xp_context = EXPAND_ENV_VARS;
1995 ++xp->xp_pattern;
1996 }
1997
1998 return NULL;
1999}
2000#endif
2001
2002#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2003/*
2004 * Set the completion context for the :language command. Always returns NULL.
2005 */
2006 static char_u *
2007set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2008{
2009 char_u *p;
2010
2011 p = skiptowhite(arg);
2012 if (*p == NUL)
2013 {
2014 xp->xp_context = EXPAND_LANGUAGE;
2015 xp->xp_pattern = arg;
2016 }
2017 else
2018 {
2019 if ( STRNCMP(arg, "messages", p - arg) == 0
2020 || STRNCMP(arg, "ctype", p - arg) == 0
2021 || STRNCMP(arg, "time", p - arg) == 0
2022 || STRNCMP(arg, "collate", p - arg) == 0)
2023 {
2024 xp->xp_context = EXPAND_LOCALES;
2025 xp->xp_pattern = skipwhite(p);
2026 }
2027 else
2028 xp->xp_context = EXPAND_NOTHING;
2029 }
2030
2031 return NULL;
2032}
2033#endif
2034
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002035#ifdef FEAT_EVAL
2036static enum
2037{
2038 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002039 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2040 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002041} breakpt_expand_what;
2042
2043/*
2044 * Set the completion context for the :breakadd command. Always returns NULL.
2045 */
2046 static char_u *
2047set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2048{
2049 char_u *p;
2050 char_u *subcmd_start;
2051
2052 xp->xp_context = EXPAND_BREAKPOINT;
2053 xp->xp_pattern = arg;
2054
2055 if (cmdidx == CMD_breakadd)
2056 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002057 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002058 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002059 else
2060 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002061
2062 p = skipwhite(arg);
2063 if (*p == NUL)
2064 return NULL;
2065 subcmd_start = p;
2066
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002067 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002068 {
2069 // :breakadd file [lnum] <filename>
2070 // :breakadd func [lnum] <funcname>
2071 p += 4;
2072 p = skipwhite(p);
2073
2074 // skip line number (if specified)
2075 if (VIM_ISDIGIT(*p))
2076 {
2077 p = skipdigits(p);
2078 if (*p != ' ')
2079 {
2080 xp->xp_context = EXPAND_NOTHING;
2081 return NULL;
2082 }
2083 p = skipwhite(p);
2084 }
2085 if (STRNCMP("file", subcmd_start, 4) == 0)
2086 xp->xp_context = EXPAND_FILES;
2087 else
2088 xp->xp_context = EXPAND_USER_FUNC;
2089 xp->xp_pattern = p;
2090 }
2091 else if (STRNCMP("expr ", p, 5) == 0)
2092 {
2093 // :breakadd expr <expression>
2094 xp->xp_context = EXPAND_EXPRESSION;
2095 xp->xp_pattern = skipwhite(p + 5);
2096 }
2097
2098 return NULL;
2099}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002100
2101 static char_u *
2102set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2103{
2104 char_u *p;
2105
2106 xp->xp_context = EXPAND_NOTHING;
2107 xp->xp_pattern = NULL;
2108
2109 p = skipwhite(arg);
2110 if (VIM_ISDIGIT(*p))
2111 return NULL;
2112
2113 xp->xp_context = EXPAND_SCRIPTNAMES;
2114 xp->xp_pattern = p;
2115
2116 return NULL;
2117}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002118#endif
2119
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002120/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002121 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2122 * The argument to the command is 'arg' and the argument flags is 'argt'.
2123 * For user-defined commands and for environment variables, 'compl' has the
2124 * completion type.
2125 * Returns a pointer to the next command. Returns NULL if there is no next
2126 * command.
2127 */
2128 static char_u *
2129set_context_by_cmdname(
2130 char_u *cmd,
2131 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002132 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002133 char_u *arg,
2134 long argt,
2135 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002136 int forceit)
2137{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002138 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002139 {
2140 case CMD_find:
2141 case CMD_sfind:
2142 case CMD_tabfind:
2143 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002144 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002145 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002146 break;
2147 case CMD_cd:
2148 case CMD_chdir:
2149 case CMD_tcd:
2150 case CMD_tchdir:
2151 case CMD_lcd:
2152 case CMD_lchdir:
2153 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002154 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002155 break;
2156 case CMD_help:
2157 xp->xp_context = EXPAND_HELP;
2158 xp->xp_pattern = arg;
2159 break;
2160
2161 // Command modifiers: return the argument.
2162 // Also for commands with an argument that is a command.
2163 case CMD_aboveleft:
2164 case CMD_argdo:
2165 case CMD_belowright:
2166 case CMD_botright:
2167 case CMD_browse:
2168 case CMD_bufdo:
2169 case CMD_cdo:
2170 case CMD_cfdo:
2171 case CMD_confirm:
2172 case CMD_debug:
2173 case CMD_folddoclosed:
2174 case CMD_folddoopen:
2175 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002176 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002177 case CMD_keepalt:
2178 case CMD_keepjumps:
2179 case CMD_keepmarks:
2180 case CMD_keeppatterns:
2181 case CMD_ldo:
2182 case CMD_leftabove:
2183 case CMD_lfdo:
2184 case CMD_lockmarks:
2185 case CMD_noautocmd:
2186 case CMD_noswapfile:
2187 case CMD_rightbelow:
2188 case CMD_sandbox:
2189 case CMD_silent:
2190 case CMD_tab:
2191 case CMD_tabdo:
2192 case CMD_topleft:
2193 case CMD_verbose:
2194 case CMD_vertical:
2195 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002196 case CMD_vim9cmd:
2197 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002198 return arg;
2199
2200 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002201 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002202
2203#ifdef FEAT_SEARCH_EXTRA
2204 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002205 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002206#endif
2207
2208 // All completion for the +cmdline_compl feature goes here.
2209
2210 case CMD_command:
2211 return set_context_in_user_cmd(xp, arg);
2212
2213 case CMD_delcommand:
2214 xp->xp_context = EXPAND_USER_COMMANDS;
2215 xp->xp_pattern = arg;
2216 break;
2217
2218 case CMD_global:
2219 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002220 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002221 case CMD_and:
2222 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002223 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002224 case CMD_isearch:
2225 case CMD_dsearch:
2226 case CMD_ilist:
2227 case CMD_dlist:
2228 case CMD_ijump:
2229 case CMD_psearch:
2230 case CMD_djump:
2231 case CMD_isplit:
2232 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002233 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002234 case CMD_autocmd:
2235 return set_context_in_autocmd(xp, arg, FALSE);
2236 case CMD_doautocmd:
2237 case CMD_doautoall:
2238 return set_context_in_autocmd(xp, arg, TRUE);
2239 case CMD_set:
2240 set_context_in_set_cmd(xp, arg, 0);
2241 break;
2242 case CMD_setglobal:
2243 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2244 break;
2245 case CMD_setlocal:
2246 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2247 break;
2248 case CMD_tag:
2249 case CMD_stag:
2250 case CMD_ptag:
2251 case CMD_ltag:
2252 case CMD_tselect:
2253 case CMD_stselect:
2254 case CMD_ptselect:
2255 case CMD_tjump:
2256 case CMD_stjump:
2257 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002258 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002259 xp->xp_context = EXPAND_TAGS_LISTFILES;
2260 else
2261 xp->xp_context = EXPAND_TAGS;
2262 xp->xp_pattern = arg;
2263 break;
2264 case CMD_augroup:
2265 xp->xp_context = EXPAND_AUGROUP;
2266 xp->xp_pattern = arg;
2267 break;
2268#ifdef FEAT_SYN_HL
2269 case CMD_syntax:
2270 set_context_in_syntax_cmd(xp, arg);
2271 break;
2272#endif
2273#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002274 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002275 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002276 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002277 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002278 case CMD_if:
2279 case CMD_elseif:
2280 case CMD_while:
2281 case CMD_for:
2282 case CMD_echo:
2283 case CMD_echon:
2284 case CMD_execute:
2285 case CMD_echomsg:
2286 case CMD_echoerr:
2287 case CMD_call:
2288 case CMD_return:
2289 case CMD_cexpr:
2290 case CMD_caddexpr:
2291 case CMD_cgetexpr:
2292 case CMD_lexpr:
2293 case CMD_laddexpr:
2294 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002295 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002296 break;
2297
2298 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002299 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002300 case CMD_function:
2301 case CMD_delfunction:
2302 xp->xp_context = EXPAND_USER_FUNC;
2303 xp->xp_pattern = arg;
2304 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002305 case CMD_disassemble:
2306 set_context_in_disassemble_cmd(xp, arg);
2307 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002308
2309 case CMD_echohl:
2310 set_context_in_echohl_cmd(xp, arg);
2311 break;
2312#endif
2313 case CMD_highlight:
2314 set_context_in_highlight_cmd(xp, arg);
2315 break;
2316#ifdef FEAT_CSCOPE
2317 case CMD_cscope:
2318 case CMD_lcscope:
2319 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002320 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002321 break;
2322#endif
2323#ifdef FEAT_SIGNS
2324 case CMD_sign:
2325 set_context_in_sign_cmd(xp, arg);
2326 break;
2327#endif
2328 case CMD_bdelete:
2329 case CMD_bwipeout:
2330 case CMD_bunload:
2331 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2332 arg = xp->xp_pattern + 1;
2333 // FALLTHROUGH
2334 case CMD_buffer:
2335 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002336 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002337 case CMD_checktime:
2338 xp->xp_context = EXPAND_BUFFERS;
2339 xp->xp_pattern = arg;
2340 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002341#ifdef FEAT_DIFF
2342 case CMD_diffget:
2343 case CMD_diffput:
2344 // If current buffer is in diff mode, complete buffer names
2345 // which are in diff mode, and different than current buffer.
2346 xp->xp_context = EXPAND_DIFF_BUFFERS;
2347 xp->xp_pattern = arg;
2348 break;
2349#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002350 case CMD_USER:
2351 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002352 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2353 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002354
2355 case CMD_map: case CMD_noremap:
2356 case CMD_nmap: case CMD_nnoremap:
2357 case CMD_vmap: case CMD_vnoremap:
2358 case CMD_omap: case CMD_onoremap:
2359 case CMD_imap: case CMD_inoremap:
2360 case CMD_cmap: case CMD_cnoremap:
2361 case CMD_lmap: case CMD_lnoremap:
2362 case CMD_smap: case CMD_snoremap:
2363 case CMD_tmap: case CMD_tnoremap:
2364 case CMD_xmap: case CMD_xnoremap:
2365 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002366 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002367 case CMD_unmap:
2368 case CMD_nunmap:
2369 case CMD_vunmap:
2370 case CMD_ounmap:
2371 case CMD_iunmap:
2372 case CMD_cunmap:
2373 case CMD_lunmap:
2374 case CMD_sunmap:
2375 case CMD_tunmap:
2376 case CMD_xunmap:
2377 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002378 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002379 case CMD_mapclear:
2380 case CMD_nmapclear:
2381 case CMD_vmapclear:
2382 case CMD_omapclear:
2383 case CMD_imapclear:
2384 case CMD_cmapclear:
2385 case CMD_lmapclear:
2386 case CMD_smapclear:
2387 case CMD_tmapclear:
2388 case CMD_xmapclear:
2389 xp->xp_context = EXPAND_MAPCLEAR;
2390 xp->xp_pattern = arg;
2391 break;
2392
2393 case CMD_abbreviate: case CMD_noreabbrev:
2394 case CMD_cabbrev: case CMD_cnoreabbrev:
2395 case CMD_iabbrev: case CMD_inoreabbrev:
2396 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002397 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002398 case CMD_unabbreviate:
2399 case CMD_cunabbrev:
2400 case CMD_iunabbrev:
2401 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002402 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002403#ifdef FEAT_MENU
2404 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2405 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2406 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2407 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2408 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2409 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2410 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2411 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2412 case CMD_tmenu: case CMD_tunmenu:
2413 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2414 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2415#endif
2416
2417 case CMD_colorscheme:
2418 xp->xp_context = EXPAND_COLORS;
2419 xp->xp_pattern = arg;
2420 break;
2421
2422 case CMD_compiler:
2423 xp->xp_context = EXPAND_COMPILER;
2424 xp->xp_pattern = arg;
2425 break;
2426
2427 case CMD_ownsyntax:
2428 xp->xp_context = EXPAND_OWNSYNTAX;
2429 xp->xp_pattern = arg;
2430 break;
2431
2432 case CMD_setfiletype:
2433 xp->xp_context = EXPAND_FILETYPE;
2434 xp->xp_pattern = arg;
2435 break;
2436
2437 case CMD_packadd:
2438 xp->xp_context = EXPAND_PACKADD;
2439 xp->xp_pattern = arg;
2440 break;
2441
zeertzjqb0d45ec2023-01-25 15:04:22 +00002442 case CMD_runtime:
2443 set_context_in_runtime_cmd(xp, arg);
2444 break;
2445
Bram Moolenaard0190392019-08-23 21:17:35 +02002446#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2447 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002448 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002449#endif
2450#if defined(FEAT_PROFILE)
2451 case CMD_profile:
2452 set_context_in_profile_cmd(xp, arg);
2453 break;
2454#endif
2455 case CMD_behave:
2456 xp->xp_context = EXPAND_BEHAVE;
2457 xp->xp_pattern = arg;
2458 break;
2459
2460 case CMD_messages:
2461 xp->xp_context = EXPAND_MESSAGES;
2462 xp->xp_pattern = arg;
2463 break;
2464
2465 case CMD_history:
2466 xp->xp_context = EXPAND_HISTORY;
2467 xp->xp_pattern = arg;
2468 break;
2469#if defined(FEAT_PROFILE)
2470 case CMD_syntime:
2471 xp->xp_context = EXPAND_SYNTIME;
2472 xp->xp_pattern = arg;
2473 break;
2474#endif
2475
2476 case CMD_argdelete:
2477 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2478 arg = xp->xp_pattern + 1;
2479 xp->xp_context = EXPAND_ARGLIST;
2480 xp->xp_pattern = arg;
2481 break;
2482
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002483#ifdef FEAT_EVAL
2484 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002485 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002486 case CMD_breakdel:
2487 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002488
2489 case CMD_scriptnames:
2490 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002491#endif
2492
Bram Moolenaard0190392019-08-23 21:17:35 +02002493 default:
2494 break;
2495 }
2496 return NULL;
2497}
2498
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002499/*
2500 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2501 * we don't need/want deleted. Maybe this could be done better if we didn't
2502 * repeat all this stuff. The only problem is that they may not stay
2503 * perfectly compatible with each other, but then the command line syntax
2504 * probably won't change that much -- webb.
2505 */
2506 static char_u *
2507set_one_cmd_context(
2508 expand_T *xp,
2509 char_u *buff) // buffer for command string
2510{
2511 char_u *p;
2512 char_u *cmd, *arg;
2513 int len = 0;
2514 exarg_T ea;
2515 int compl = EXPAND_NOTHING;
2516 int forceit = FALSE;
2517 int usefilter = FALSE; // filter instead of file name
2518
2519 ExpandInit(xp);
2520 xp->xp_pattern = buff;
2521 xp->xp_line = buff;
2522 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2523 ea.argt = 0;
2524
2525 // 1. skip comment lines and leading space, colons or bars
2526 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2527 ;
2528 xp->xp_pattern = cmd;
2529
2530 if (*cmd == NUL)
2531 return NULL;
2532 if (*cmd == '"') // ignore comment lines
2533 {
2534 xp->xp_context = EXPAND_NOTHING;
2535 return NULL;
2536 }
2537
2538 // 3. Skip over the range to find the command.
2539 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2540 xp->xp_pattern = cmd;
2541 if (*cmd == NUL)
2542 return NULL;
2543 if (*cmd == '"')
2544 {
2545 xp->xp_context = EXPAND_NOTHING;
2546 return NULL;
2547 }
2548
2549 if (*cmd == '|' || *cmd == '\n')
2550 return cmd + 1; // There's another command
2551
2552 // Get the command index.
2553 p = set_cmd_index(cmd, &ea, xp, &compl);
2554 if (p == NULL)
2555 return NULL;
2556
2557 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2558
2559 if (*p == '!') // forced commands
2560 {
2561 forceit = TRUE;
2562 ++p;
2563 }
2564
2565 // 6. parse arguments
2566 if (!IS_USER_CMDIDX(ea.cmdidx))
2567 ea.argt = excmd_get_argt(ea.cmdidx);
2568
2569 arg = skipwhite(p);
2570
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002571 // Does command allow "++argopt" argument?
2572 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002573 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002574 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2575 {
2576 p = arg + 2;
2577 while (*p && !vim_isspace(*p))
2578 MB_PTR_ADV(p);
2579
2580 // Still touching the command after "++"?
2581 if (*p == NUL)
2582 {
2583 if (ea.argt & EX_ARGOPT)
2584 return set_context_in_argopt(xp, arg + 2);
2585#ifdef FEAT_TERMINAL
2586 if (ea.cmdidx == CMD_terminal)
2587 return set_context_in_terminalopt(xp, arg + 2);
2588#endif
2589 }
2590
2591 arg = skipwhite(p);
2592 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002593 }
2594
2595 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2596 {
2597 if (*arg == '>') // append
2598 {
2599 if (*++arg == '>')
2600 ++arg;
2601 arg = skipwhite(arg);
2602 }
2603 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2604 {
2605 ++arg;
2606 usefilter = TRUE;
2607 }
2608 }
2609
2610 if (ea.cmdidx == CMD_read)
2611 {
2612 usefilter = forceit; // :r! filter if forced
2613 if (*arg == '!') // :r !filter
2614 {
2615 ++arg;
2616 usefilter = TRUE;
2617 }
2618 }
2619
2620 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2621 {
2622 while (*arg == *cmd) // allow any number of '>' or '<'
2623 ++arg;
2624 arg = skipwhite(arg);
2625 }
2626
2627 // Does command allow "+command"?
2628 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2629 {
2630 // Check if we're in the +command
2631 p = arg + 1;
2632 arg = skip_cmd_arg(arg, FALSE);
2633
2634 // Still touching the command after '+'?
2635 if (*arg == NUL)
2636 return p;
2637
2638 // Skip space(s) after +command to get to the real argument
2639 arg = skipwhite(arg);
2640 }
2641
2642
2643 // Check for '|' to separate commands and '"' to start comments.
2644 // Don't do this for ":read !cmd" and ":write !cmd".
2645 if ((ea.argt & EX_TRLBAR) && !usefilter)
2646 {
2647 p = arg;
2648 // ":redir @" is not the start of a comment
2649 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2650 p += 2;
2651 while (*p)
2652 {
2653 if (*p == Ctrl_V)
2654 {
2655 if (p[1] != NUL)
2656 ++p;
2657 }
2658 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2659 || *p == '|' || *p == '\n')
2660 {
2661 if (*(p - 1) != '\\')
2662 {
2663 if (*p == '|' || *p == '\n')
2664 return p + 1;
2665 return NULL; // It's a comment
2666 }
2667 }
2668 MB_PTR_ADV(p);
2669 }
2670 }
2671
2672 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2673 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2674 // no arguments allowed but there is something
2675 return NULL;
2676
2677 // Find start of last argument (argument just before cursor):
2678 p = buff;
2679 xp->xp_pattern = p;
2680 len = (int)STRLEN(buff);
2681 while (*p && p < buff + len)
2682 {
2683 if (*p == ' ' || *p == TAB)
2684 {
2685 // argument starts after a space
2686 xp->xp_pattern = ++p;
2687 }
2688 else
2689 {
2690 if (*p == '\\' && *(p + 1) != NUL)
2691 ++p; // skip over escaped character
2692 MB_PTR_ADV(p);
2693 }
2694 }
2695
2696 if (ea.argt & EX_XFILE)
2697 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2698
2699 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002700 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002701 forceit);
2702}
2703
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002704/*
2705 * Set the completion context in 'xp' for command 'str'
2706 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002707 void
2708set_cmd_context(
2709 expand_T *xp,
2710 char_u *str, // start of command line
2711 int len, // length of command line (excl. NUL)
2712 int col, // position of cursor
2713 int use_ccline UNUSED) // use ccline for info
2714{
2715#ifdef FEAT_EVAL
2716 cmdline_info_T *ccline = get_cmdline_info();
2717#endif
2718 int old_char = NUL;
2719 char_u *nextcomm;
2720
2721 // Avoid a UMR warning from Purify, only save the character if it has been
2722 // written before.
2723 if (col < len)
2724 old_char = str[col];
2725 str[col] = NUL;
2726 nextcomm = str;
2727
2728#ifdef FEAT_EVAL
2729 if (use_ccline && ccline->cmdfirstc == '=')
2730 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002731 // pass CMD_SIZE because there is no real command
2732 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 }
2734 else if (use_ccline && ccline->input_fn)
2735 {
2736 xp->xp_context = ccline->xp_context;
2737 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002738 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739 }
2740 else
2741#endif
2742 while (nextcomm != NULL)
2743 nextcomm = set_one_cmd_context(xp, nextcomm);
2744
2745 // Store the string here so that call_user_expand_func() can get to them
2746 // easily.
2747 xp->xp_line = str;
2748 xp->xp_col = col;
2749
2750 str[col] = old_char;
2751}
2752
2753/*
2754 * Expand the command line "str" from context "xp".
2755 * "xp" must have been set by set_cmd_context().
2756 * xp->xp_pattern points into "str", to where the text that is to be expanded
2757 * starts.
2758 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2759 * cursor.
2760 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2761 * key that triggered expansion literally.
2762 * Returns EXPAND_OK otherwise.
2763 */
2764 int
2765expand_cmdline(
2766 expand_T *xp,
2767 char_u *str, // start of command line
2768 int col, // position of cursor
2769 int *matchcount, // return: nr of matches
2770 char_u ***matches) // return: array of pointers to matches
2771{
2772 char_u *file_str = NULL;
2773 int options = WILD_ADD_SLASH|WILD_SILENT;
2774
2775 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2776 {
2777 beep_flush();
2778 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2779 }
2780 if (xp->xp_context == EXPAND_NOTHING)
2781 {
2782 // Caller can use the character as a normal char instead
2783 return EXPAND_NOTHING;
2784 }
2785
2786 // add star to file name, or convert to regexp if not exp. files.
2787 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002788 if (cmdline_fuzzy_completion_supported(xp))
2789 // If fuzzy matching, don't modify the search string
2790 file_str = vim_strsave(xp->xp_pattern);
2791 else
2792 {
2793 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2794 if (file_str == NULL)
2795 return EXPAND_UNSUCCESSFUL;
2796 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002797
2798 if (p_wic)
2799 options += WILD_ICASE;
2800
2801 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002802 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002803 {
2804 *matchcount = 0;
2805 *matches = NULL;
2806 }
2807 vim_free(file_str);
2808
2809 return EXPAND_OK;
2810}
2811
Bram Moolenaar66b51422019-08-18 21:44:12 +02002812/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002813 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002814 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002815 */
2816 static int
2817expand_files_and_dirs(
2818 expand_T *xp,
2819 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002820 char_u ***matches,
2821 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002822 int flags,
2823 int options)
2824{
2825 int free_pat = FALSE;
2826 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002827 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002828
2829 // for ":set path=" and ":set tags=" halve backslashes for escaped
2830 // space
2831 if (xp->xp_backslash != XP_BS_NONE)
2832 {
2833 free_pat = TRUE;
2834 pat = vim_strsave(pat);
2835 for (i = 0; pat[i]; ++i)
2836 if (pat[i] == '\\')
2837 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002838 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002839 && pat[i + 1] == '\\'
2840 && pat[i + 2] == '\\'
2841 && pat[i + 3] == ' ')
2842 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002843 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002844 && pat[i + 1] == ' ')
2845 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002846 else if ((xp->xp_backslash & XP_BS_COMMA)
2847 && pat[i + 1] == '\\'
2848 && pat[i + 2] == ',')
2849 STRMOVE(pat + i, pat + i + 2);
2850#ifdef BACKSLASH_IN_FILENAME
2851 else if ((xp->xp_backslash & XP_BS_COMMA)
2852 && pat[i + 1] == ',')
2853 STRMOVE(pat + i, pat + i + 1);
2854#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002855 }
2856 }
2857
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002858 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002859 {
2860#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002861 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002862#endif
2863 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002864 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002865 {
2866 if (xp->xp_context == EXPAND_FILES)
2867 flags |= EW_FILE;
2868 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2869 flags |= (EW_FILE | EW_PATH);
2870 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2871 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2872 else
2873 flags = (flags | EW_DIR) & ~EW_FILE;
2874 if (options & WILD_ICASE)
2875 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002876
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002877 // Expand wildcards, supporting %:h and the like.
2878 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2879 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880 if (free_pat)
2881 vim_free(pat);
2882#ifdef BACKSLASH_IN_FILENAME
2883 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2884 {
2885 int j;
2886
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002887 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002888 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002889 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002890
2891 while (*ptr != NUL)
2892 {
2893 if (p_csl[0] == 's' && *ptr == '\\')
2894 *ptr = '/';
2895 else if (p_csl[0] == 'b' && *ptr == '/')
2896 *ptr = '\\';
2897 ptr += (*mb_ptr2len)(ptr);
2898 }
2899 }
2900 }
2901#endif
2902 return ret;
2903}
2904
2905/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002906 * Function given to ExpandGeneric() to obtain the possible arguments of the
2907 * ":behave {mswin,xterm}" command.
2908 */
2909 static char_u *
2910get_behave_arg(expand_T *xp UNUSED, int idx)
2911{
2912 if (idx == 0)
2913 return (char_u *)"mswin";
2914 if (idx == 1)
2915 return (char_u *)"xterm";
2916 return NULL;
2917}
2918
K.Takata161b6ac2022-11-14 15:31:07 +00002919#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002920/*
2921 * Function given to ExpandGeneric() to obtain the possible arguments of the
2922 * ":breakadd {expr, file, func, here}" command.
2923 * ":breakdel {func, file, here}" command.
2924 */
2925 static char_u *
2926get_breakadd_arg(expand_T *xp UNUSED, int idx)
2927{
2928 char *opts[] = {"expr", "file", "func", "here"};
2929
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002930 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002932 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002933 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2934 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002935 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2936 {
2937 // breakdel {func, file, here}
2938 if (idx <= 2)
2939 return (char_u *)opts[idx + 1];
2940 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002941 else
2942 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002943 // profdel {func, file}
2944 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002945 return (char_u *)opts[idx + 1];
2946 }
2947 }
2948 return NULL;
2949}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002950
2951/*
2952 * Function given to ExpandGeneric() to obtain the possible arguments for the
2953 * ":scriptnames" command.
2954 */
2955 static char_u *
2956get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2957{
2958 scriptitem_T *si;
2959
2960 if (!SCRIPT_ID_VALID(idx + 1))
2961 return NULL;
2962
2963 si = SCRIPT_ITEM(idx + 1);
2964 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2965 return NameBuff;
2966}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002967#endif
2968
Bram Moolenaard0190392019-08-23 21:17:35 +02002969/*
2970 * Function given to ExpandGeneric() to obtain the possible arguments of the
2971 * ":messages {clear}" command.
2972 */
2973 static char_u *
2974get_messages_arg(expand_T *xp UNUSED, int idx)
2975{
2976 if (idx == 0)
2977 return (char_u *)"clear";
2978 return NULL;
2979}
2980
2981 static char_u *
2982get_mapclear_arg(expand_T *xp UNUSED, int idx)
2983{
2984 if (idx == 0)
2985 return (char_u *)"<buffer>";
2986 return NULL;
2987}
2988
2989/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002990 * Do the expansion based on xp->xp_context and 'rmp'.
2991 */
2992 static int
2993ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002994 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002995 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002996 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002997 char_u ***matches,
2998 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002999{
3000 static struct expgen
3001 {
3002 int context;
3003 char_u *((*func)(expand_T *, int));
3004 int ic;
3005 int escaped;
3006 } tab[] =
3007 {
3008 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3009 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3010 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3011 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3012 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3013 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3014 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3015 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3016 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3017 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003018#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003019 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3020 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3021 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3022 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3023 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003024#endif
3025#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3027 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003028#endif
3029#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003030 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003031#endif
3032#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003033 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003034#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003035 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3036 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3037 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003038#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003039 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003040#endif
3041#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003042 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003043#endif
3044#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003045 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003046#endif
3047#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003048 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3049 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003050#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003051 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3052 {EXPAND_USER, get_users, TRUE, FALSE},
3053 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003054#ifdef FEAT_EVAL
3055 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003056 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003057#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003058 };
3059 int i;
3060 int ret = FAIL;
3061
3062 // Find a context in the table and call the ExpandGeneric() with the
3063 // right function to do the expansion.
3064 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3065 {
3066 if (xp->xp_context == tab[i].context)
3067 {
3068 if (tab[i].ic)
3069 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003070 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3071 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003072 break;
3073 }
3074 }
3075
3076 return ret;
3077}
3078
3079/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003080 * Map wild expand options to flags for expand_wildcards()
3081 */
3082 static int
3083map_wildopts_to_ewflags(int options)
3084{
3085 int flags;
3086
3087 flags = EW_DIR; // include directories
3088 if (options & WILD_LIST_NOTFOUND)
3089 flags |= EW_NOTFOUND;
3090 if (options & WILD_ADD_SLASH)
3091 flags |= EW_ADDSLASH;
3092 if (options & WILD_KEEP_ALL)
3093 flags |= EW_KEEPALL;
3094 if (options & WILD_SILENT)
3095 flags |= EW_SILENT;
3096 if (options & WILD_NOERROR)
3097 flags |= EW_NOERROR;
3098 if (options & WILD_ALLLINKS)
3099 flags |= EW_ALLLINKS;
3100
3101 return flags;
3102}
3103
3104/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105 * Do the expansion based on xp->xp_context and "pat".
3106 */
3107 static int
3108ExpandFromContext(
3109 expand_T *xp,
3110 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003111 char_u ***matches,
3112 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 int options) // WILD_ flags
3114{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116 int ret;
3117 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003118 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003119 int fuzzy = cmdline_fuzzy_complete(pat)
3120 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003122 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003123
3124 if (xp->xp_context == EXPAND_FILES
3125 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003126 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003127 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003128 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3130 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003132 *matches = (char_u **)"";
3133 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 if (xp->xp_context == EXPAND_HELP)
3135 {
3136 // With an empty argument we would get all the help tags, which is
3137 // very slow. Get matches for "help" instead.
3138 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003139 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140 {
3141#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003142 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143#endif
3144 return OK;
3145 }
3146 return FAIL;
3147 }
3148
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003150 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003152 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003153 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003155#ifdef FEAT_DIFF
3156 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003157 return ExpandBufnames(pat, numMatches, matches,
3158 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003159#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160 if (xp->xp_context == EXPAND_TAGS
3161 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003162 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3163 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003164 if (xp->xp_context == EXPAND_COLORS)
3165 {
3166 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 directories);
3169 }
3170 if (xp->xp_context == EXPAND_COMPILER)
3171 {
3172 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003173 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003174 }
3175 if (xp->xp_context == EXPAND_OWNSYNTAX)
3176 {
3177 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003178 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003179 }
3180 if (xp->xp_context == EXPAND_FILETYPE)
3181 {
3182 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003183 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 }
Doug Kearns81642d92024-01-04 22:37:44 +01003185#ifdef FEAT_KEYMAP
3186 if (xp->xp_context == EXPAND_KEYMAP)
3187 {
3188 char *directories[] = {"keymap", NULL};
3189 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3190 }
3191#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003192#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003194 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003195#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003197 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003198 if (xp->xp_context == EXPAND_RUNTIME)
3199 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003200
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003201 // When expanding a function name starting with s:, match the <SNR>nr_
3202 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003203 if ((xp->xp_context == EXPAND_USER_FUNC
3204 || xp->xp_context == EXPAND_DISASSEMBLE)
3205 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003206 {
3207 int len = (int)STRLEN(pat) + 20;
3208
3209 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003210 if (tofree == NULL)
3211 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003212 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003213 pat = tofree;
3214 }
3215
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003216 if (!fuzzy)
3217 {
3218 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3219 if (regmatch.regprog == NULL)
3220 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003222 // set ignore-case according to p_ic, p_scs and pat
3223 regmatch.rm_ic = ignorecase(pat);
3224 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225
3226 if (xp->xp_context == EXPAND_SETTINGS
3227 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003228 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003229 else if (xp->xp_context == EXPAND_STRING_SETTING)
3230 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3231 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3232 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003233 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003234 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003235 else if (xp->xp_context == EXPAND_ARGOPT)
3236 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003237 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3238 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003239#if defined(FEAT_TERMINAL)
3240 else if (xp->xp_context == EXPAND_TERMINALOPT)
3241 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3242#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003243#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003245 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003246#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003248 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003250 if (!fuzzy)
3251 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003252 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253
3254 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255}
3256
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003257 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003259 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003260 expand_T *xp,
3261 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003262 char_u ***matches,
3263 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 char_u *((*func)(expand_T *, int)),
3265 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003266 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267{
Yee Cheng China7b81202025-02-23 09:32:47 +01003268 return ExpandGenericExt(
3269 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3270}
3271
3272/*
3273 * Expand a list of names.
3274 *
3275 * Generic function for command line completion. It calls a function to
3276 * obtain strings, one by one. The strings are matched against a regexp
3277 * program. Matching strings are copied into an array, which is returned.
3278 *
3279 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3280 * is used.
3281 *
3282 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3283 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3284 * sorting.
3285 *
3286 * Returns OK when no problems encountered, FAIL for error (out of memory).
3287 */
3288 int
3289ExpandGenericExt(
3290 char_u *pat,
3291 expand_T *xp,
3292 regmatch_T *regmatch,
3293 char_u ***matches,
3294 int *numMatches,
3295 char_u *((*func)(expand_T *, int)),
3296 // returns a string from the list
3297 int escaped,
3298 int sortStartIdx)
3299{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003301 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003303 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003304 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003305 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003306 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003307 int sort_matches = FALSE;
3308 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003309 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003310
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003311 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003312 *matches = NULL;
3313 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003314
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003315 if (!fuzzy)
3316 ga_init2(&ga, sizeof(char *), 30);
3317 else
3318 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3319
3320 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003322 str = (*func)(xp, i);
3323 if (str == NULL) // end of list
3324 break;
3325 if (*str == NUL) // skip empty strings
3326 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003328 if (xp->xp_pattern[0] != NUL)
3329 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003330 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003331 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003332 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003334 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003335 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003336 }
3337 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003338 else
3339 match = TRUE;
3340
3341 if (!match)
3342 continue;
3343
3344 if (escaped)
3345 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3346 else
3347 str = vim_strsave(str);
3348 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003349 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003350 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003351 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003352 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 return FAIL;
3354 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003355 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003356 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003357 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003358
3359 if (ga_grow(&ga, 1) == FAIL)
3360 {
3361 vim_free(str);
3362 break;
3363 }
3364
3365 if (fuzzy)
3366 {
3367 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3368 fuzmatch->idx = ga.ga_len;
3369 fuzmatch->str = str;
3370 fuzmatch->score = score;
3371 }
3372 else
3373 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3374
K.Takata161b6ac2022-11-14 15:31:07 +00003375#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003376 if (func == get_menu_names)
3377 {
3378 // test for separator added by get_menu_names()
3379 str += STRLEN(str) - 1;
3380 if (*str == '\001')
3381 *str = '.';
3382 }
K.Takata161b6ac2022-11-14 15:31:07 +00003383#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003384
Yee Cheng China7b81202025-02-23 09:32:47 +01003385 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3386 {
3387 // Found first item to start sorting from. This is usually 0.
3388 sortStartMatchIdx = ga.ga_len;
3389 }
3390
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003391 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 }
3393
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003394 if (ga.ga_len == 0)
3395 return OK;
3396
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003397 // sort the matches when using regular expression matching and sorting
3398 // applies to the completion context. Menus and scriptnames should be kept
3399 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003400 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003401 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003402 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003403 && xp->xp_context != EXPAND_SCRIPTNAMES
3404 && xp->xp_context != EXPAND_ARGOPT
3405 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003406 sort_matches = TRUE;
3407
3408 // <SNR> functions should be sorted to the end.
3409 if (xp->xp_context == EXPAND_EXPRESSION
3410 || xp->xp_context == EXPAND_FUNCTIONS
3411 || xp->xp_context == EXPAND_USER_FUNC
3412 || xp->xp_context == EXPAND_DISASSEMBLE)
3413 funcsort = TRUE;
3414
3415 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003416 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003417 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003418 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003419 // <SNR> functions should be sorted to the end.
3420 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3421 sort_func_compare);
3422 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003423 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003424 }
3425
3426 if (!fuzzy)
3427 {
3428 *matches = ga.ga_data;
3429 *numMatches = ga.ga_len;
3430 }
3431 else
3432 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003433 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3434 funcsort) == FAIL)
3435 return FAIL;
3436 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003437 }
3438
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003439#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003440 // Reset the variables used for special highlight names expansion, so that
3441 // they don't show up when getting normal highlight names by ID.
3442 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003443#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003444
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445 return OK;
3446}
3447
3448/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003449 * Expand shell command matches in one directory of $PATH.
3450 */
3451 static void
3452expand_shellcmd_onedir(
3453 char_u *buf,
3454 char_u *s,
3455 size_t l,
3456 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003457 char_u ***matches,
3458 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003459 int flags,
3460 hashtab_T *ht,
3461 garray_T *gap)
3462{
3463 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003464 hash_T hash;
3465 hashitem_T *hi;
3466
3467 vim_strncpy(buf, s, l);
3468 add_pathsep(buf);
3469 l = STRLEN(buf);
3470 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3471
3472 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003473 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003474 if (ret != OK)
3475 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003476
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003477 if (ga_grow(gap, *numMatches) == FAIL)
3478 {
3479 FreeWild(*numMatches, *matches);
3480 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003481 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003482
3483 for (int i = 0; i < *numMatches; ++i)
3484 {
3485 char_u *name = (*matches)[i];
3486
3487 if (STRLEN(name) > l)
3488 {
3489 // Check if this name was already found.
3490 hash = hash_hash(name + l);
3491 hi = hash_lookup(ht, name + l, hash);
3492 if (HASHITEM_EMPTY(hi))
3493 {
3494 // Remove the path that was prepended.
3495 STRMOVE(name, name + l);
3496 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3497 hash_add_item(ht, hi, name, hash);
3498 name = NULL;
3499 }
3500 }
3501 vim_free(name);
3502 }
3503 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003504}
3505
3506/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003507 * Complete a shell command.
3508 * Returns FAIL or OK;
3509 */
3510 static int
3511expand_shellcmd(
3512 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003513 char_u ***matches, // return: array with matches
3514 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003515 int flagsarg) // EW_ flags
3516{
3517 char_u *pat;
3518 int i;
3519 char_u *path = NULL;
3520 int mustfree = FALSE;
3521 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003522 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003523 size_t l;
3524 char_u *s, *e;
3525 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003526 int did_curdir = FALSE;
3527 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003528
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003529 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003530 if (buf == NULL)
3531 return FAIL;
3532
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003533 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003534 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003535 if (pat == NULL)
3536 {
3537 vim_free(buf);
3538 return FAIL;
3539 }
3540
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541 for (i = 0; pat[i]; ++i)
3542 if (pat[i] == '\\' && pat[i + 1] == ' ')
3543 STRMOVE(pat + i, pat + i + 1);
3544
3545 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3546
3547 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3548 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3549 path = (char_u *)".";
3550 else
3551 {
3552 // For an absolute name we don't use $PATH.
3553 if (!mch_isFullName(pat))
3554 path = vim_getenv((char_u *)"PATH", &mustfree);
3555 if (path == NULL)
3556 path = (char_u *)"";
3557 }
3558
3559 // Go over all directories in $PATH. Expand matches in that directory and
3560 // collect them in "ga". When "." is not in $PATH also expand for the
3561 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003562 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563 hash_init(&found_ht);
3564 for (s = path; ; s = e)
3565 {
K.Takata161b6ac2022-11-14 15:31:07 +00003566#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003568#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003569 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003570#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003571 if (e == NULL)
3572 e = s + STRLEN(s);
3573
3574 if (*s == NUL)
3575 {
3576 if (did_curdir)
3577 break;
3578 // Find directories in the current directory, path is empty.
3579 did_curdir = TRUE;
3580 flags |= EW_DIR;
3581 }
3582 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3583 {
3584 did_curdir = TRUE;
3585 flags |= EW_DIR;
3586 }
3587 else
3588 // Do not match directories inside a $PATH item.
3589 flags &= ~EW_DIR;
3590
3591 l = e - s;
3592 if (l > MAXPATHL - 5)
3593 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003594
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003595 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003596 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003597
Bram Moolenaar66b51422019-08-18 21:44:12 +02003598 if (*e != NUL)
3599 ++e;
3600 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003601 *matches = ga.ga_data;
3602 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003603
3604 vim_free(buf);
3605 vim_free(pat);
3606 if (mustfree)
3607 vim_free(path);
3608 hash_clear(&found_ht);
3609 return OK;
3610}
3611
K.Takata161b6ac2022-11-14 15:31:07 +00003612#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003613/*
3614 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003615 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003616 */
3617 static void *
3618call_user_expand_func(
3619 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003620 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003621{
3622 cmdline_info_T *ccline = get_cmdline_info();
3623 int keep = 0;
3624 typval_T args[4];
3625 sctx_T save_current_sctx = current_sctx;
3626 char_u *pat = NULL;
3627 void *ret;
3628
3629 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3630 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631
3632 if (ccline->cmdbuff != NULL)
3633 {
3634 keep = ccline->cmdbuff[ccline->cmdlen];
3635 ccline->cmdbuff[ccline->cmdlen] = 0;
3636 }
3637
3638 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3639
3640 args[0].v_type = VAR_STRING;
3641 args[0].vval.v_string = pat;
3642 args[1].v_type = VAR_STRING;
3643 args[1].vval.v_string = xp->xp_line;
3644 args[2].v_type = VAR_NUMBER;
3645 args[2].vval.v_number = xp->xp_col;
3646 args[3].v_type = VAR_UNKNOWN;
3647
3648 current_sctx = xp->xp_script_ctx;
3649
3650 ret = user_expand_func(xp->xp_arg, 3, args);
3651
3652 current_sctx = save_current_sctx;
3653 if (ccline->cmdbuff != NULL)
3654 ccline->cmdbuff[ccline->cmdlen] = keep;
3655
3656 vim_free(pat);
3657 return ret;
3658}
3659
3660/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003661 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3662 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003663 */
3664 static int
3665ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003666 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667 expand_T *xp,
3668 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003669 char_u ***matches,
3670 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003671{
3672 char_u *retstr;
3673 char_u *s;
3674 char_u *e;
3675 int keep;
3676 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003677 int fuzzy;
3678 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003679 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003680
3681 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003682 *matches = NULL;
3683 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003684
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003685 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003686 if (retstr == NULL)
3687 return FAIL;
3688
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003689 if (!fuzzy)
3690 ga_init2(&ga, sizeof(char *), 3);
3691 else
3692 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3693
Bram Moolenaar66b51422019-08-18 21:44:12 +02003694 for (s = retstr; *s != NUL; s = e)
3695 {
3696 e = vim_strchr(s, '\n');
3697 if (e == NULL)
3698 e = s + STRLEN(s);
3699 keep = *e;
3700 *e = NUL;
3701
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003702 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003703 {
3704 if (!fuzzy)
3705 match = vim_regexec(regmatch, s, (colnr_T)0);
3706 else
3707 {
3708 score = fuzzy_match_str(s, pat);
3709 match = (score != 0);
3710 }
3711 }
3712 else
3713 match = TRUE; // match everything
3714
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715 *e = keep;
3716
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003717 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003718 {
3719 if (ga_grow(&ga, 1) == FAIL)
3720 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003721 if (!fuzzy)
3722 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3723 else
3724 {
3725 fuzmatch_str_T *fuzmatch =
3726 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003727 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003728 fuzmatch->str = vim_strnsave(s, e - s);
3729 fuzmatch->score = score;
3730 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003731 ++ga.ga_len;
3732 }
3733
3734 if (*e != NUL)
3735 ++e;
3736 }
3737 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003738
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003739 if (ga.ga_len == 0)
3740 return OK;
3741
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003742 if (!fuzzy)
3743 {
3744 *matches = ga.ga_data;
3745 *numMatches = ga.ga_len;
3746 }
3747 else
3748 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003749 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3750 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003751 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003752 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003753 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754 return OK;
3755}
3756
3757/*
3758 * Expand names with a list returned by a function defined by the user.
3759 */
3760 static int
3761ExpandUserList(
3762 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003763 char_u ***matches,
3764 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003765{
3766 list_T *retlist;
3767 listitem_T *li;
3768 garray_T ga;
3769
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003770 *matches = NULL;
3771 *numMatches = 0;
3772 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003773 if (retlist == NULL)
3774 return FAIL;
3775
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003776 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003777 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003778 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003779 {
3780 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3781 continue; // Skip non-string items and empty strings
3782
3783 if (ga_grow(&ga, 1) == FAIL)
3784 break;
3785
3786 ((char_u **)ga.ga_data)[ga.ga_len] =
3787 vim_strsave(li->li_tv.vval.v_string);
3788 ++ga.ga_len;
3789 }
3790 list_unref(retlist);
3791
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003792 *matches = ga.ga_data;
3793 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003794 return OK;
3795}
K.Takata161b6ac2022-11-14 15:31:07 +00003796#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003797
3798/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003799 * Expand "file" for all comma-separated directories in "path".
3800 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003801 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802 */
3803 void
3804globpath(
3805 char_u *path,
3806 char_u *file,
3807 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003808 int expand_options,
3809 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810{
3811 expand_T xpc;
3812 char_u *buf;
3813 int i;
3814 int num_p;
3815 char_u **p;
3816
3817 buf = alloc(MAXPATHL);
3818 if (buf == NULL)
3819 return;
3820
3821 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003822 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003823
3824 // Loop over all entries in {path}.
3825 while (*path != NUL)
3826 {
3827 // Copy one item of the path to buf[] and concatenate the file name.
3828 copy_option_part(&path, buf, MAXPATHL, ",");
3829 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3830 {
K.Takata161b6ac2022-11-14 15:31:07 +00003831#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003832 // Using the platform's path separator (\) makes vim incorrectly
3833 // treat it as an escape character, use '/' instead.
3834 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3835 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003836#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003837 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003838#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003839 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003840 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003841 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3842 {
3843 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3844
3845 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003846 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003847 for (i = 0; i < num_p; ++i)
3848 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003849 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003850 ++ga->ga_len;
3851 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003852 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003853 }
3854 }
3855 }
3856
3857 vim_free(buf);
3858}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003859
Bram Moolenaareadee482020-09-04 15:37:31 +02003860/*
3861 * Translate some keys pressed when 'wildmenu' is used.
3862 */
3863 int
3864wildmenu_translate_key(
3865 cmdline_info_T *cclp,
3866 int key,
3867 expand_T *xp,
3868 int did_wild_list)
3869{
3870 int c = key;
3871
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003872 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003873 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003874 // When the popup menu is used for cmdline completion:
3875 // Up : go to the previous item in the menu
3876 // Down : go to the next item in the menu
3877 // Left : go to the parent directory
3878 // Right: list the files in the selected directory
3879 switch (c)
3880 {
3881 case K_UP: c = K_LEFT; break;
3882 case K_DOWN: c = K_RIGHT; break;
3883 case K_LEFT: c = K_UP; break;
3884 case K_RIGHT: c = K_DOWN; break;
3885 default: break;
3886 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003887 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003888
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003889 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003890 {
3891 if (c == K_LEFT)
3892 c = Ctrl_P;
3893 else if (c == K_RIGHT)
3894 c = Ctrl_N;
3895 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003896
Bram Moolenaareadee482020-09-04 15:37:31 +02003897 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003898 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003899 && cclp->cmdpos > 1
3900 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3901 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3902 && (c == '\n' || c == '\r' || c == K_KENTER))
3903 c = K_DOWN;
3904
3905 return c;
3906}
3907
3908/*
3909 * Delete characters on the command line, from "from" to the current
3910 * position.
3911 */
3912 static void
3913cmdline_del(cmdline_info_T *cclp, int from)
3914{
3915 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3916 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3917 cclp->cmdlen -= cclp->cmdpos - from;
3918 cclp->cmdpos = from;
3919}
3920
3921/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003922 * Handle a key pressed when the wild menu for the menu names
3923 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003924 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003925 static int
3926wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003927{
Bram Moolenaareadee482020-09-04 15:37:31 +02003928 int i;
3929 int j;
3930
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003931 // Hitting <Down> after "emenu Name.": complete submenu
3932 if (key == K_DOWN && cclp->cmdpos > 0
3933 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003934 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003935 key = p_wc;
3936 KeyTyped = TRUE; // in case the key was mapped
3937 }
3938 else if (key == K_UP)
3939 {
3940 // Hitting <Up>: Remove one submenu name in front of the
3941 // cursor
3942 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003943
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003944 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3945 i = 0;
3946 while (--j > 0)
3947 {
3948 // check for start of menu name
3949 if (cclp->cmdbuff[j] == ' '
3950 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003951 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003952 i = j + 1;
3953 break;
3954 }
3955 // check for start of submenu name
3956 if (cclp->cmdbuff[j] == '.'
3957 && cclp->cmdbuff[j - 1] != '\\')
3958 {
3959 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003960 {
3961 i = j + 1;
3962 break;
3963 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003964 else
3965 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003966 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003967 }
3968 if (i > 0)
3969 cmdline_del(cclp, i);
3970 key = p_wc;
3971 KeyTyped = TRUE; // in case the key was mapped
3972 xp->xp_context = EXPAND_NOTHING;
3973 }
3974
3975 return key;
3976}
3977
3978/*
3979 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3980 * directory names (EXPAND_DIRECTORIES) or shell command names
3981 * (EXPAND_SHELLCMD) is displayed.
3982 */
3983 static int
3984wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3985{
3986 int i;
3987 int j;
3988 char_u upseg[5];
3989
3990 upseg[0] = PATHSEP;
3991 upseg[1] = '.';
3992 upseg[2] = '.';
3993 upseg[3] = PATHSEP;
3994 upseg[4] = NUL;
3995
3996 if (key == K_DOWN
3997 && cclp->cmdpos > 0
3998 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3999 && (cclp->cmdpos < 3
4000 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4001 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4002 {
4003 // go down a directory
4004 key = p_wc;
4005 KeyTyped = TRUE; // in case the key was mapped
4006 }
4007 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4008 {
4009 // If in a direct ancestor, strip off one ../ to go down
4010 int found = FALSE;
4011
4012 j = cclp->cmdpos;
4013 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4014 while (--j > i)
4015 {
4016 if (has_mbyte)
4017 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4018 if (vim_ispathsep(cclp->cmdbuff[j]))
4019 {
4020 found = TRUE;
4021 break;
4022 }
4023 }
4024 if (found
4025 && cclp->cmdbuff[j - 1] == '.'
4026 && cclp->cmdbuff[j - 2] == '.'
4027 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4028 {
4029 cmdline_del(cclp, j - 2);
4030 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004031 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004032 }
4033 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004034 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004035 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004036 // go up a directory
4037 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004038
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004039 j = cclp->cmdpos - 1;
4040 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4041 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004042 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004043 if (has_mbyte)
4044 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4045 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004046#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004047 && vim_strchr((char_u *)" *?[{`$%#",
4048 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004049#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004050 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004051 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004052 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004053 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004054 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004055 break;
4056 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004057 else
4058 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004059 }
4060 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004061
4062 if (!found)
4063 j = i;
4064 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4065 j += 4;
4066 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4067 && j == i)
4068 j += 3;
4069 else
4070 j = 0;
4071 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004072 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004073 // TODO this is only for DOS/UNIX systems - need to put in
4074 // machine-specific stuff here and in upseg init
4075 cmdline_del(cclp, j);
4076 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004077 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004078 else if (cclp->cmdpos > i)
4079 cmdline_del(cclp, i);
4080
4081 // Now complete in the new directory. Set KeyTyped in case the
4082 // Up key came from a mapping.
4083 key = p_wc;
4084 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004085 }
4086
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004087 return key;
4088}
4089
4090/*
4091 * Handle a key pressed when the wild menu is displayed
4092 */
4093 int
4094wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4095{
4096 if (xp->xp_context == EXPAND_MENUNAMES)
4097 return wildmenu_process_key_menunames(cclp, key, xp);
4098 else if ((xp->xp_context == EXPAND_FILES
4099 || xp->xp_context == EXPAND_DIRECTORIES
4100 || xp->xp_context == EXPAND_SHELLCMD))
4101 return wildmenu_process_key_filenames(cclp, key, xp);
4102
4103 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004104}
4105
4106/*
4107 * Free expanded names when finished walking through the matches
4108 */
4109 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004110wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004111{
4112 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004113
4114 if (!p_wmnu || wild_menu_showing == 0)
4115 return;
4116
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004117#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004118 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004119 if (cclp->input_fn)
4120 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004121#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004122
4123 if (wild_menu_showing == WM_SCROLLED)
4124 {
4125 // Entered command line, move it up
4126 cmdline_row--;
4127 redrawcmd();
4128 }
4129 else if (save_p_ls != -1)
4130 {
4131 // restore 'laststatus' and 'winminheight'
4132 p_ls = save_p_ls;
4133 p_wmh = save_p_wmh;
4134 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004135 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004136 redrawcmd();
4137 save_p_ls = -1;
4138 }
4139 else
4140 {
4141 win_redraw_last_status(topframe);
4142 redraw_statuslines();
4143 }
4144 KeyTyped = skt;
4145 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004146#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004147 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004148 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004149#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004150}
Bram Moolenaareadee482020-09-04 15:37:31 +02004151
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004152#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004153/*
4154 * "getcompletion()" function
4155 */
4156 void
4157f_getcompletion(typval_T *argvars, typval_T *rettv)
4158{
4159 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004160 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004161 expand_T xpc;
4162 int filtered = FALSE;
4163 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004164 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004165
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004166 if (in_vim9script()
4167 && (check_for_string_arg(argvars, 0) == FAIL
4168 || check_for_string_arg(argvars, 1) == FAIL
4169 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4170 return;
4171
ii144785fe02021-11-21 12:13:56 +00004172 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004173 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004174 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004175 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004176
4177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004178 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004179
4180 if (p_wic)
4181 options |= WILD_ICASE;
4182
4183 // For filtered results, 'wildignore' is used
4184 if (!filtered)
4185 options |= WILD_KEEP_ALL;
4186
4187 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004188 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004190 int cmdline_len = (int)STRLEN(pat);
4191 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004192 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004193 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004194 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004195 else
4196 {
ii144785fe02021-11-21 12:13:56 +00004197 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004198 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004199 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004200
4201 xpc.xp_context = cmdcomplete_str_to_type(type);
4202 if (xpc.xp_context == EXPAND_NOTHING)
4203 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004204 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004205 return;
4206 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004207
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004208 if (xpc.xp_context == EXPAND_USER_DEFINED)
4209 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004210 // Must be "custom,funcname" pattern
4211 if (STRNCMP(type, "custom,", 7) != 0)
4212 {
4213 semsg(_(e_invalid_argument_str), type);
4214 return;
4215 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004216
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004217 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004218 }
4219
4220 if (xpc.xp_context == EXPAND_USER_LIST)
4221 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004222 // Must be "customlist,funcname" pattern
4223 if (STRNCMP(type, "customlist,", 11) != 0)
4224 {
4225 semsg(_(e_invalid_argument_str), type);
4226 return;
4227 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004228
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004229 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004230 }
4231
Bram Moolenaar66b51422019-08-18 21:44:12 +02004232# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004233 if (xpc.xp_context == EXPAND_MENUS)
4234 {
4235 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4236 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4237 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004238# endif
4239# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004240 if (xpc.xp_context == EXPAND_CSCOPE)
4241 {
4242 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4243 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4244 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004245# endif
4246# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004247 if (xpc.xp_context == EXPAND_SIGN)
4248 {
4249 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4250 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4251 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004252# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004253 if (xpc.xp_context == EXPAND_RUNTIME)
4254 {
4255 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4256 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4257 }
zeertzjq85f36d62024-10-10 19:14:13 +02004258 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4259 {
4260 int context = EXPAND_SHELLCMDLINE;
4261 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4262 &context);
4263 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4264 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004265 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004266
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004267 if (cmdline_fuzzy_completion_supported(&xpc))
4268 // when fuzzy matching, don't modify the search string
4269 pat = vim_strsave(xpc.xp_pattern);
4270 else
4271 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4272
Bram Moolenaar93a10962022-06-16 11:42:09 +01004273 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004274 {
4275 int i;
4276
4277 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4278
4279 for (i = 0; i < xpc.xp_numfiles; i++)
4280 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4281 }
4282 vim_free(pat);
4283 ExpandCleanup(&xpc);
4284}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004285#endif // FEAT_EVAL