blob: 4dbd10ee8a7ab4e6871ce6d8a1cb88f0e3787e92 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
zeertzjq7a5115c2025-03-19 20:29:58 +010018static void set_context_for_wildcard_arg(exarg_T *eap, char_u *arg, int usefilter, expand_T *xp, int *complp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000020static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020021static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020023#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000024static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000025static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020026#endif
27
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000028// "compl_match_array" points the currently displayed list of entries in the
29// popup menu. It is NULL when there is no popup menu.
30static pumitem_T *compl_match_array = NULL;
31static int compl_match_arraysize;
32// First column in cmdline of the matched item for completion.
33static int compl_startcol;
34static int compl_selected;
Girish Palya92f68e22025-04-21 11:12:41 +020035// cmdline before expansion
36static char_u *cmdline_orig = NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020052 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000053 && xp->xp_context != EXPAND_FILES
54 && xp->xp_context != EXPAND_FILES_IN_PATH
55 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010056 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000057 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010058 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020060 && xp->xp_context != EXPAND_STRING_SETTING
61 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_OWNSYNTAX
63 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000064 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000065 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020066 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000067 && xp->xp_context != EXPAND_TAGS
68 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 && xp->xp_context != EXPAND_USER_LIST);
70}
71
72/*
73 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000074 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
75 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000076 */
77 int
78cmdline_fuzzy_complete(char_u *fuzzystr)
79{
80 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
81}
82
83/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000084 * sort function for the completion matches.
85 * <SNR> functions should be sorted to the end.
86 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020087 static int
88sort_func_compare(const void *s1, const void *s2)
89{
90 char_u *p1 = *(char_u **)s1;
91 char_u *p2 = *(char_u **)s2;
92
93 if (*p1 != '<' && *p2 == '<') return -1;
94 if (*p1 == '<' && *p2 != '<') return 1;
95 return STRCMP(p1, p2);
96}
Bram Moolenaar66b51422019-08-18 21:44:12 +020097
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000098/*
99 * Escape special characters in the cmdline completion matches.
100 */
Bram Moolenaar66b51422019-08-18 21:44:12 +0200101 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000102wildescape(
103 expand_T *xp,
104 char_u *str,
105 int numfiles,
106 char_u **files)
107{
108 char_u *p;
109 int vse_what = xp->xp_context == EXPAND_BUFFERS
110 ? VSE_BUFFER : VSE_NONE;
111
112 if (xp->xp_context == EXPAND_FILES
113 || xp->xp_context == EXPAND_FILES_IN_PATH
114 || xp->xp_context == EXPAND_SHELLCMD
115 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200116 || xp->xp_context == EXPAND_DIRECTORIES
117 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000118 {
119 // Insert a backslash into a file name before a space, \, %, #
120 // and wildmatch characters, except '~'.
121 for (int i = 0; i < numfiles; ++i)
122 {
123 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200124 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200126 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
127 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000128 if (p != NULL)
129 {
130 vim_free(files[i]);
131 files[i] = p;
132#if defined(BACKSLASH_IN_FILENAME)
133 p = vim_strsave_escaped(files[i], (char_u *)" ");
134 if (p != NULL)
135 {
136 vim_free(files[i]);
137 files[i] = p;
138 }
139#endif
140 }
141 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200142 else if (xp->xp_backslash & XP_BS_COMMA)
143 {
144 if (vim_strchr(files[i], ',') != NULL)
145 {
146 p = vim_strsave_escaped(files[i], (char_u *)",");
147 if (p != NULL)
148 {
149 vim_free(files[i]);
150 files[i] = p;
151 }
152 }
153 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000154#ifdef BACKSLASH_IN_FILENAME
155 p = vim_strsave_fnameescape(files[i], vse_what);
156#else
157 p = vim_strsave_fnameescape(files[i],
158 xp->xp_shell ? VSE_SHELL : vse_what);
159#endif
160 if (p != NULL)
161 {
162 vim_free(files[i]);
163 files[i] = p;
164 }
165
166 // If 'str' starts with "\~", replace "~" at start of
167 // files[i] with "\~".
168 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
169 escape_fname(&files[i]);
170 }
171 xp->xp_backslash = XP_BS_NONE;
172
173 // If the first file starts with a '+' escape it. Otherwise it
174 // could be seen as "+cmd".
175 if (*files[0] == '+')
176 escape_fname(&files[0]);
177 }
178 else if (xp->xp_context == EXPAND_TAGS)
179 {
180 // Insert a backslash before characters in a tag name that
181 // would terminate the ":tag" command.
182 for (int i = 0; i < numfiles; ++i)
183 {
184 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
185 if (p != NULL)
186 {
187 vim_free(files[i]);
188 files[i] = p;
189 }
190 }
191 }
192}
193
194/*
195 * Escape special characters in the cmdline completion matches.
196 */
197 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200198ExpandEscape(
199 expand_T *xp,
200 char_u *str,
201 int numfiles,
202 char_u **files,
203 int options)
204{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200205 // May change home directory back to "~"
206 if (options & WILD_HOME_REPLACE)
207 tilde_replace(str, numfiles, files);
208
209 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000210 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200211}
212
213/*
214 * Return FAIL if this is not an appropriate context in which to do
215 * completion of anything, return OK if it is (even if there are no matches).
216 * For the caller, this means that the character is just passed through like a
217 * normal character (instead of being expanded). This allows :s/^I^D etc.
218 */
219 int
220nextwild(
221 expand_T *xp,
222 int type,
223 int options, // extra options for ExpandOne()
224 int escape) // if TRUE, escape the returned matches
225{
226 cmdline_info_T *ccline = get_cmdline_info();
227 int i, j;
228 char_u *p1;
229 char_u *p2;
230 int difflen;
231 int v;
232
233 if (xp->xp_numfiles == -1)
234 {
Jim Zhou3255af82025-02-27 19:29:50 +0100235#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100236 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100237 {
238 // Expand commands typed in input() function
239 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100240 }
241 else
Jim Zhou3255af82025-02-27 19:29:50 +0100242#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100243 {
Jim Zhou3255af82025-02-27 19:29:50 +0100244 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100245 }
246 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200247 }
248
249 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
250 {
251 beep_flush();
252 return OK; // Something illegal on command line
253 }
254 if (xp->xp_context == EXPAND_NOTHING)
255 {
256 // Caller can use the character as a normal char instead
257 return FAIL;
258 }
259
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000260 // If cmd_silent is set then don't show the dots, because redrawcmd() below
261 // won't remove them.
262 if (!cmd_silent)
263 {
264 msg_puts("..."); // show that we are busy
265 out_flush();
266 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200267
268 i = (int)(xp->xp_pattern - ccline->cmdbuff);
269 xp->xp_pattern_len = ccline->cmdpos - i;
270
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000271 if (type == WILD_NEXT || type == WILD_PREV
272 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200273 {
274 // Get next/previous match for a previous expanded pattern.
275 p2 = ExpandOne(xp, NULL, NULL, 0, type);
276 }
277 else
278 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000279 if (cmdline_fuzzy_completion_supported(xp))
280 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200281 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000282 else
283 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
284
Bram Moolenaar66b51422019-08-18 21:44:12 +0200285 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000286 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200287 p2 = NULL;
288 else
289 {
290 int use_options = options |
291 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100292 if (use_options & WILD_KEEP_SOLE_ITEM)
293 use_options &= ~WILD_KEEP_SOLE_ITEM;
294
Bram Moolenaar66b51422019-08-18 21:44:12 +0200295 if (escape)
296 use_options |= WILD_ESCAPE;
297
298 if (p_wic)
299 use_options += WILD_ICASE;
300 p2 = ExpandOne(xp, p1,
301 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
302 use_options, type);
303 vim_free(p1);
304 // longest match: make sure it is not shorter, happens with :help
305 if (p2 != NULL && type == WILD_LONGEST)
306 {
307 for (j = 0; j < xp->xp_pattern_len; ++j)
308 if (ccline->cmdbuff[i + j] == '*'
309 || ccline->cmdbuff[i + j] == '?')
310 break;
311 if ((int)STRLEN(p2) < j)
312 VIM_CLEAR(p2);
313 }
314 }
315 }
316
317 if (p2 != NULL && !got_int)
318 {
319 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
320 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
321 {
322 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
323 xp->xp_pattern = ccline->cmdbuff + i;
324 }
325 else
326 v = OK;
327 if (v == OK)
328 {
329 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
330 &ccline->cmdbuff[ccline->cmdpos],
331 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
332 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
333 ccline->cmdlen += difflen;
334 ccline->cmdpos += difflen;
335 }
336 }
337 vim_free(p2);
338
339 redrawcmd();
340 cursorcmd();
341
342 // When expanding a ":map" command and no matches are found, assume that
343 // the key is supposed to be inserted literally
344 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
345 return FAIL;
346
347 if (xp->xp_numfiles <= 0 && p2 == NULL)
348 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100349 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200350 // free expanded pattern
351 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
352
353 return OK;
354}
355
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356/*
357 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000358 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 */
360 static int
361cmdline_pum_create(
362 cmdline_info_T *ccline,
363 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000364 char_u **matches,
365 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366 int showtail)
367{
368 int i;
369 int columns;
370
371 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000374 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 {
zeertzjqc51a3762022-12-10 10:22:29 +0000376 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000377 compl_match_array[i].pum_info = NULL;
378 compl_match_array[i].pum_extra = NULL;
379 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200380 compl_match_array[i].pum_user_abbr_hlattr = -1;
381 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000382 }
383
384 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200385 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000386 columns = vim_strsize(xp->xp_pattern);
387 if (showtail)
388 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000389 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000390 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000391 }
glepnir977561a2025-02-13 20:48:56 +0100392 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000393
394 // no default selection
395 compl_selected = -1;
396
397 cmdline_pum_display();
398
399 return EXPAND_OK;
400}
401
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402/*
403 * Display the cmdline completion matches in a popup menu
404 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000405 void
406cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407{
408 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
409}
410
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000411/*
412 * Returns TRUE if the cmdline completion popup menu is being displayed.
413 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000414 int
415cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100417 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000418}
419
420/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000421 * Remove the cmdline completion popup menu (if present), free the list of
422 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000424 void
zeertzjq1830e782025-03-13 20:29:13 +0100425cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000427 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100428 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100429#ifdef FEAT_EVAL
430 int save_RedrawingDisabled = RedrawingDisabled;
431 if (cclp->input_fn)
432 RedrawingDisabled = 0;
433#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000434
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435 pum_undisplay();
436 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200437 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000438 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000440 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000441 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100442
443 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
444 // as a side effect.
445 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100446#ifdef FEAT_EVAL
447 if (cclp->input_fn)
448 RedrawingDisabled = save_RedrawingDisabled;
449#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000450}
451
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000452 void
453cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000454{
zeertzjq1830e782025-03-13 20:29:13 +0100455 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000456 wildmenu_cleanup(cclp);
457}
458
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000459/*
460 * Returns the starting column number to use for the cmdline completion popup
461 * menu.
462 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000463 int
464cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000465{
466 return compl_startcol;
467}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000468
Bram Moolenaar66b51422019-08-18 21:44:12 +0200469/*
zeertzjqd8c93402024-06-17 18:25:32 +0200470 * Returns the current cmdline completion pattern.
471 */
472 char_u *
473cmdline_compl_pattern(void)
474{
475 expand_T *xp = get_cmdline_info()->xpc;
476
477 return xp == NULL ? NULL : xp->xp_orig;
478}
479
480/*
481 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
482 */
483 int
484cmdline_compl_is_fuzzy(void)
485{
486 expand_T *xp = get_cmdline_info()->xpc;
487
488 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
489}
490
491/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000492 * Return the number of characters that should be skipped in a status match.
493 * These are backslashes used for escaping. Do show backslashes in help tags.
494 */
495 static int
496skip_status_match_char(expand_T *xp, char_u *s)
497{
498 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
499#ifdef FEAT_MENU
500 || ((xp->xp_context == EXPAND_MENUS
501 || xp->xp_context == EXPAND_MENUNAMES)
502 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
503#endif
504 )
505 {
506#ifndef BACKSLASH_IN_FILENAME
507 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
508 return 2;
509#endif
510 return 1;
511 }
512 return 0;
513}
514
515/*
516 * Get the length of an item as it will be shown in the status line.
517 */
518 static int
519status_match_len(expand_T *xp, char_u *s)
520{
521 int len = 0;
522
523#ifdef FEAT_MENU
524 int emenu = xp->xp_context == EXPAND_MENUS
525 || xp->xp_context == EXPAND_MENUNAMES;
526
527 // Check for menu separators - replace with '|'.
528 if (emenu && menu_is_separator(s))
529 return 1;
530#endif
531
532 while (*s != NUL)
533 {
534 s += skip_status_match_char(xp, s);
535 len += ptr2cells(s);
536 MB_PTR_ADV(s);
537 }
538
539 return len;
540}
541
542/*
543 * Show wildchar matches in the status line.
544 * Show at least the "match" item.
545 * We start at item 'first_match' in the list and show all matches that fit.
546 *
547 * If inversion is possible we use it. Else '=' characters are used.
548 */
549 static void
550win_redr_status_matches(
551 expand_T *xp,
552 int num_matches,
553 char_u **matches, // list of matches
554 int match,
555 int showtail)
556{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000557 int row;
558 char_u *buf;
559 int len;
560 int clen; // length in screen cells
561 int fillchar;
562 int attr;
563 int i;
564 int highlight = TRUE;
565 char_u *selstart = NULL;
566 int selstart_col = 0;
567 char_u *selend = NULL;
568 static int first_match = 0;
569 int add_left = FALSE;
570 char_u *s;
571#ifdef FEAT_MENU
572 int emenu;
573#endif
574 int l;
575
576 if (matches == NULL) // interrupted completion?
577 return;
578
579 if (has_mbyte)
580 buf = alloc(Columns * MB_MAXBYTES + 1);
581 else
582 buf = alloc(Columns + 1);
583 if (buf == NULL)
584 return;
585
586 if (match == -1) // don't show match but original text
587 {
588 match = 0;
589 highlight = FALSE;
590 }
591 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000592 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 if (match == 0)
594 first_match = 0;
595 else if (match < first_match)
596 {
597 // jumping left, as far as we can go
598 first_match = match;
599 add_left = TRUE;
600 }
601 else
602 {
603 // check if match fits on the screen
604 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000605 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000606 if (first_match > 0)
607 clen += 2;
608 // jumping right, put match at the left
609 if ((long)clen > Columns)
610 {
611 first_match = match;
612 // if showing the last match, we can add some on the left
613 clen = 2;
614 for (i = match; i < num_matches; ++i)
615 {
zeertzjqc51a3762022-12-10 10:22:29 +0000616 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000617 if ((long)clen >= Columns)
618 break;
619 }
620 if (i == num_matches)
621 add_left = TRUE;
622 }
623 }
624 if (add_left)
625 while (first_match > 0)
626 {
zeertzjqc51a3762022-12-10 10:22:29 +0000627 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000628 if ((long)clen >= Columns)
629 break;
630 --first_match;
631 }
632
633 fillchar = fillchar_status(&attr, curwin);
634
635 if (first_match == 0)
636 {
637 *buf = NUL;
638 len = 0;
639 }
640 else
641 {
642 STRCPY(buf, "< ");
643 len = 2;
644 }
645 clen = len;
646
647 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000648 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000649 {
650 if (i == match)
651 {
652 selstart = buf + len;
653 selstart_col = clen;
654 }
655
zeertzjqc51a3762022-12-10 10:22:29 +0000656 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000657 // Check for menu separators - replace with '|'
658#ifdef FEAT_MENU
659 emenu = (xp->xp_context == EXPAND_MENUS
660 || xp->xp_context == EXPAND_MENUNAMES);
661 if (emenu && menu_is_separator(s))
662 {
663 STRCPY(buf + len, transchar('|'));
664 l = (int)STRLEN(buf + len);
665 len += l;
666 clen += l;
667 }
668 else
669#endif
670 for ( ; *s != NUL; ++s)
671 {
672 s += skip_status_match_char(xp, s);
673 clen += ptr2cells(s);
674 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
675 {
676 STRNCPY(buf + len, s, l);
677 s += l - 1;
678 len += l;
679 }
680 else
681 {
682 STRCPY(buf + len, transchar_byte(*s));
683 len += (int)STRLEN(buf + len);
684 }
685 }
686 if (i == match)
687 selend = buf + len;
688
689 *(buf + len++) = ' ';
690 *(buf + len++) = ' ';
691 clen += 2;
692 if (++i == num_matches)
693 break;
694 }
695
696 if (i != num_matches)
697 {
698 *(buf + len++) = '>';
699 ++clen;
700 }
701
702 buf[len] = NUL;
703
704 row = cmdline_row - 1;
705 if (row >= 0)
706 {
707 if (wild_menu_showing == 0)
708 {
709 if (msg_scrolled > 0)
710 {
711 // Put the wildmenu just above the command line. If there is
712 // no room, scroll the screen one line up.
713 if (cmdline_row == Rows - 1)
714 {
715 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
716 ++msg_scrolled;
717 }
718 else
719 {
720 ++cmdline_row;
721 ++row;
722 }
723 wild_menu_showing = WM_SCROLLED;
724 }
725 else
726 {
727 // Create status line if needed by setting 'laststatus' to 2.
728 // Set 'winminheight' to zero to avoid that the window is
729 // resized.
730 if (lastwin->w_status_height == 0)
731 {
732 save_p_ls = p_ls;
733 save_p_wmh = p_wmh;
734 p_ls = 2;
735 p_wmh = 0;
736 last_status(FALSE);
737 }
738 wild_menu_showing = WM_SHOWN;
739 }
740 }
741
742 screen_puts(buf, row, 0, attr);
743 if (selstart != NULL && highlight)
744 {
745 *selend = NUL;
746 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
747 }
748
749 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
750 }
751
752 win_redraw_last_status(topframe);
753 vim_free(buf);
754}
755
756/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200758 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 */
760 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100761get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000762{
glepnir977561a2025-02-13 20:48:56 +0100763 int findex = xp->xp_selected;
764 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000765
zeertzjqb6c900b2025-02-14 17:59:31 +0100766 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000767 if (xp->xp_numfiles <= 0)
768 return NULL;
769
770 if (mode == WILD_PREV)
771 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100772 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 if (findex == -1)
774 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100775 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000776 --findex;
777 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000778 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000779 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100780 // Select the next entry
781 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000782 }
glepnir977561a2025-02-13 20:48:56 +0100783 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000784 {
glepnir977561a2025-02-13 20:48:56 +0100785 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
786 ht = pum_get_height();
787 if (ht > 3)
788 ht -= 2;
789
790 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000791 {
glepnir977561a2025-02-13 20:48:56 +0100792 if (findex == 0)
793 // at the first entry, don't select any entries
794 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100795 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100796 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000797 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100798 else
799 // go up by the pum height
800 findex = MAX(findex - ht, 0);
801 }
802 else // mode == WILD_PAGEDOWN
803 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100804 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100805 // at the last entry, don't select any entries
806 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100807 else if (findex < 0)
808 // no entry is selected, select the first entry
809 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100810 else
811 // go down by the pum height
812 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000813 }
814 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000815
glepnir977561a2025-02-13 20:48:56 +0100816 // Handle wrapping around
817 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100819 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100820 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822 else
glepnir977561a2025-02-13 20:48:56 +0100823 // Wrap around to opposite end
824 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000825 }
glepnir977561a2025-02-13 20:48:56 +0100826
827 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000828 if (compl_match_array)
829 {
830 compl_selected = findex;
831 cmdline_pum_display();
832 }
833 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100834 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
835 cmd_showtail);
836
zeertzjqe9ef3472023-08-17 23:57:05 +0200837 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100838 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100839 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000840}
841
842/*
843 * Start the command-line expansion and get the matches.
844 */
845 static char_u *
846ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
847{
848 int non_suf_match; // number without matching suffix
849 int i;
850 char_u *ss = NULL;
851
852 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000853 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000854 options) == FAIL)
855 {
856#ifdef FNAME_ILLEGAL
857 // Illegal file name has been silently skipped. But when there
858 // are wildcards, the real problem is that there was no match,
859 // causing the pattern to be added, which has illegal characters.
860 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
861 semsg(_(e_no_match_str_2), str);
862#endif
863 }
864 else if (xp->xp_numfiles == 0)
865 {
866 if (!(options & WILD_SILENT))
867 semsg(_(e_no_match_str_2), str);
868 }
869 else
870 {
871 // Escape the matches for use on the command line.
872 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
873
874 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000875 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000876 {
877 if (xp->xp_numfiles)
878 non_suf_match = xp->xp_numfiles;
879 else
880 non_suf_match = 1;
881 if ((xp->xp_context == EXPAND_FILES
882 || xp->xp_context == EXPAND_DIRECTORIES)
883 && xp->xp_numfiles > 1)
884 {
885 // More than one match; check suffix.
886 // The files will have been sorted on matching suffix in
887 // expand_wildcards, only need to check the first two.
888 non_suf_match = 0;
889 for (i = 0; i < 2; ++i)
890 if (match_suffix(xp->xp_files[i]))
891 ++non_suf_match;
892 }
893 if (non_suf_match != 1)
894 {
895 // Can we ever get here unless it's while expanding
896 // interactively? If not, we can get rid of this all
897 // together. Don't really want to wait for this message
898 // (and possibly have to hit return to continue!).
899 if (!(options & WILD_SILENT))
900 emsg(_(e_too_many_file_names));
901 else if (!(options & WILD_NO_BEEP))
902 beep_flush();
903 }
904 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
905 ss = vim_strsave(xp->xp_files[0]);
906 }
907 }
908
909 return ss;
910}
911
912/*
913 * Return the longest common part in the list of cmdline completion matches.
914 */
915 static char_u *
916find_longest_match(expand_T *xp, int options)
917{
918 long_u len;
919 int mb_len = 1;
920 int c0, ci;
921 int i;
922 char_u *ss;
923
924 for (len = 0; xp->xp_files[0][len]; len += mb_len)
925 {
926 if (has_mbyte)
927 {
928 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
929 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
930 }
931 else
932 c0 = xp->xp_files[0][len];
933 for (i = 1; i < xp->xp_numfiles; ++i)
934 {
935 if (has_mbyte)
936 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
937 else
938 ci = xp->xp_files[i][len];
939 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
940 || xp->xp_context == EXPAND_FILES
941 || xp->xp_context == EXPAND_SHELLCMD
942 || xp->xp_context == EXPAND_BUFFERS))
943 {
944 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
945 break;
946 }
947 else if (c0 != ci)
948 break;
949 }
950 if (i < xp->xp_numfiles)
951 {
952 if (!(options & WILD_NO_BEEP))
953 vim_beep(BO_WILD);
954 break;
955 }
956 }
957
958 ss = alloc(len + 1);
959 if (ss)
960 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
961
962 return ss;
963}
964
965/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000966 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200967 * Chars that should not be expanded must be preceded with a backslash.
968 * Return a pointer to allocated memory containing the new string.
969 * Return NULL for failure.
970 *
971 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200972 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
973 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 *
975 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
976 * is WILD_EXPAND_FREE or WILD_ALL.
977 *
978 * mode = WILD_FREE: just free previously expanded matches
979 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
980 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
981 * mode = WILD_NEXT: use next match in multiple match, wrap to first
982 * mode = WILD_PREV: use previous match in multiple match, wrap to first
983 * mode = WILD_ALL: return all matches concatenated
984 * mode = WILD_LONGEST: return longest matched part
985 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000986 * mode = WILD_APPLY: apply the item selected in the cmdline completion
987 * popup menu and close the menu.
988 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
989 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200990 *
991 * options = WILD_LIST_NOTFOUND: list entries without a match
992 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
993 * options = WILD_USE_NL: Use '\n' for WILD_ALL
994 * options = WILD_NO_BEEP: Don't beep for multiple matches
995 * options = WILD_ADD_SLASH: add a slash after directory names
996 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
997 * options = WILD_SILENT: don't print warning messages
998 * options = WILD_ESCAPE: put backslash before special chars
999 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001000 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001001 *
1002 * The variables xp->xp_context and xp->xp_backslash must have been set!
1003 */
1004 char_u *
1005ExpandOne(
1006 expand_T *xp,
1007 char_u *str,
1008 char_u *orig, // allocated copy of original of expanded string
1009 int options,
1010 int mode)
1011{
1012 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 int orig_saved = FALSE;
1014 int i;
1015 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001016
1017 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001018 if (mode == WILD_NEXT || mode == WILD_PREV
1019 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001020 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001022 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001023 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001024 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001025 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001026 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001027 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001028
Bram Moolenaar66b51422019-08-18 21:44:12 +02001029 // free old names
1030 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1031 {
1032 FreeWild(xp->xp_numfiles, xp->xp_files);
1033 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001034 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001035
1036 // The entries from xp_files may be used in the PUM, remove it.
1037 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001038 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001040 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001041
1042 if (mode == WILD_FREE) // only release file name
1043 return NULL;
1044
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001045 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 {
zeertzjq28a23602023-09-29 19:58:35 +02001047 vim_free(xp->xp_orig);
1048 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 orig_saved = TRUE;
1050
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001051 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001052 }
1053
1054 // Find longest common part
1055 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1056 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001057 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001058 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 }
1060
Bram Moolenaar57e95172022-08-20 19:26:14 +01001061 // Concatenate all matching names. Unless interrupted, this can be slow
1062 // and the result probably won't be used.
1063 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 {
1065 len = 0;
1066 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001067 {
1068 if (i > 0)
1069 {
1070 if (xp->xp_prefix == XP_PREFIX_NO)
1071 len += 2; // prefix "no"
1072 else if (xp->xp_prefix == XP_PREFIX_INV)
1073 len += 3; // prefix "inv"
1074 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001076 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077 ss = alloc(len);
1078 if (ss != NULL)
1079 {
1080 *ss = NUL;
1081 for (i = 0; i < xp->xp_numfiles; ++i)
1082 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001083 if (i > 0)
1084 {
1085 if (xp->xp_prefix == XP_PREFIX_NO)
1086 STRCAT(ss, "no");
1087 else if (xp->xp_prefix == XP_PREFIX_INV)
1088 STRCAT(ss, "inv");
1089 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001090 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001091
Bram Moolenaar66b51422019-08-18 21:44:12 +02001092 if (i != xp->xp_numfiles - 1)
1093 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1094 }
1095 }
1096 }
1097
1098 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1099 ExpandCleanup(xp);
1100
zeertzjq28a23602023-09-29 19:58:35 +02001101 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102 if (!orig_saved)
1103 vim_free(orig);
1104
1105 return ss;
1106}
1107
1108/*
1109 * Prepare an expand structure for use.
1110 */
1111 void
1112ExpandInit(expand_T *xp)
1113{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001114 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001115 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001116 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001117 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001118}
1119
1120/*
1121 * Cleanup an expand structure after use.
1122 */
1123 void
1124ExpandCleanup(expand_T *xp)
1125{
1126 if (xp->xp_numfiles >= 0)
1127 {
1128 FreeWild(xp->xp_numfiles, xp->xp_files);
1129 xp->xp_numfiles = -1;
1130 }
zeertzjq28a23602023-09-29 19:58:35 +02001131 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001132}
1133
zeertzjqec270a52025-04-23 20:50:23 +02001134 void
1135clear_cmdline_orig(void)
1136{
1137 VIM_CLEAR(cmdline_orig);
1138}
1139
Bram Moolenaar66b51422019-08-18 21:44:12 +02001140/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001141 * Display one line of completion matches. Multiple matches are displayed in
1142 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001143 * matches - list of completion match names
1144 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001145 * lines - number of output lines
1146 * linenr - line number of matches to display
1147 * maxlen - maximum number of characters in each line
1148 * showtail - display only the tail of the full path of a file name
1149 * dir_attr - highlight attribute to use for directory names
1150 */
1151 static void
1152showmatches_oneline(
1153 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001154 char_u **matches,
1155 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001156 int lines,
1157 int linenr,
1158 int maxlen,
1159 int showtail,
1160 int dir_attr)
1161{
1162 int i, j;
1163 int isdir;
1164 int lastlen;
1165 char_u *p;
1166
1167 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001168 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001169 {
1170 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1171 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001172 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1173 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001174 msg_advance(maxlen + 1);
1175 msg_puts((char *)p);
1176 msg_advance(maxlen + 3);
1177 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1178 break;
1179 }
1180 for (i = maxlen - lastlen; --i >= 0; )
1181 msg_putchar(' ');
1182 if (xp->xp_context == EXPAND_FILES
1183 || xp->xp_context == EXPAND_SHELLCMD
1184 || xp->xp_context == EXPAND_BUFFERS)
1185 {
1186 // highlight directories
1187 if (xp->xp_numfiles != -1)
1188 {
1189 char_u *halved_slash;
1190 char_u *exp_path;
1191 char_u *path;
1192
1193 // Expansion was done before and special characters
1194 // were escaped, need to halve backslashes. Also
1195 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001196 exp_path = expand_env_save_opt(matches[j], TRUE);
1197 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001198 halved_slash = backslash_halve_save(path);
1199 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001200 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001201 vim_free(exp_path);
1202 if (halved_slash != path)
1203 vim_free(halved_slash);
1204 }
1205 else
1206 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001207 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001208 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001209 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001210 else
1211 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001212 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001213 TRUE);
1214 p = NameBuff;
1215 }
1216 }
1217 else
1218 {
1219 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001220 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001221 }
1222 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1223 }
1224 if (msg_col > 0) // when not wrapped around
1225 {
1226 msg_clr_eos();
1227 msg_putchar('\n');
1228 }
1229 out_flush(); // show one line at a time
1230}
1231
1232/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001233 * Show all matches for completion on the command line.
1234 * Returns EXPAND_NOTHING when the character that triggered expansion should
1235 * be inserted like a normal character.
1236 */
1237 int
1238showmatches(expand_T *xp, int wildmenu UNUSED)
1239{
1240 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001241 int numMatches;
1242 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001243 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001244 int maxlen;
1245 int lines;
1246 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247 int attr;
1248 int showtail;
1249
Girish Palya92f68e22025-04-21 11:12:41 +02001250 // Save cmdline before expansion
1251 if (ccline->cmdbuff != NULL)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001252 {
1253 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001254 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001255 }
Girish Palya92f68e22025-04-21 11:12:41 +02001256
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 if (xp->xp_numfiles == -1)
1258 {
1259 set_expand_context(xp);
1260 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001261 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 showtail = expand_showtail(xp);
1263 if (i != EXPAND_OK)
1264 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001265 }
1266 else
1267 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001268 numMatches = xp->xp_numfiles;
1269 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 showtail = cmd_showtail;
1271 }
1272
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001273 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001274 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001275 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001276
Bram Moolenaar66b51422019-08-18 21:44:12 +02001277 if (!wildmenu)
1278 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001279 msg_didany = FALSE; // lines_left will be set
1280 msg_start(); // prepare for paging
1281 msg_putchar('\n');
1282 out_flush();
1283 cmdline_row = msg_row;
1284 msg_didany = FALSE; // lines_left will be set again
1285 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001286 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287
1288 if (got_int)
1289 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001290 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001291 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292 else
1293 {
1294 // find the length of the longest file name
1295 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001296 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001297 {
1298 if (!showtail && (xp->xp_context == EXPAND_FILES
1299 || xp->xp_context == EXPAND_SHELLCMD
1300 || xp->xp_context == EXPAND_BUFFERS))
1301 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001302 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001303 j = vim_strsize(NameBuff);
1304 }
1305 else
zeertzjqc51a3762022-12-10 10:22:29 +00001306 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001307 if (j > maxlen)
1308 maxlen = j;
1309 }
1310
1311 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001312 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001313 else
1314 {
1315 // compute the number of columns and lines for the listing
1316 maxlen += 2; // two spaces between file names
1317 columns = ((int)Columns + 2) / maxlen;
1318 if (columns < 1)
1319 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001320 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001321 }
1322
1323 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1324
1325 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1326 {
1327 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1328 msg_clr_eos();
1329 msg_advance(maxlen - 3);
1330 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1331 }
1332
1333 // list the files line by line
1334 for (i = 0; i < lines; ++i)
1335 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001336 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001337 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001338 if (got_int)
1339 {
1340 got_int = FALSE;
1341 break;
1342 }
1343 }
1344
1345 // we redraw the command below the lines that we have just listed
1346 // This is a bit tricky, but it saves a lot of screen updating.
1347 cmdline_row = msg_row; // will put it back later
1348 }
1349
1350 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001351 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001352
1353 return EXPAND_OK;
1354}
1355
1356/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001357 * gettail() version for showmatches() and win_redr_status_matches():
1358 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001359 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001360 static char_u *
1361showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001362{
1363 char_u *p;
1364 char_u *t = s;
1365 int had_sep = FALSE;
1366
1367 for (p = s; *p != NUL; )
1368 {
1369 if (vim_ispathsep(*p)
1370#ifdef BACKSLASH_IN_FILENAME
1371 && !rem_backslash(p)
1372#endif
1373 )
1374 had_sep = TRUE;
1375 else if (had_sep)
1376 {
1377 t = p;
1378 had_sep = FALSE;
1379 }
1380 MB_PTR_ADV(p);
1381 }
1382 return t;
1383}
1384
1385/*
1386 * Return TRUE if we only need to show the tail of completion matches.
1387 * When not completing file names or there is a wildcard in the path FALSE is
1388 * returned.
1389 */
1390 static int
1391expand_showtail(expand_T *xp)
1392{
1393 char_u *s;
1394 char_u *end;
1395
1396 // When not completing file names a "/" may mean something different.
1397 if (xp->xp_context != EXPAND_FILES
1398 && xp->xp_context != EXPAND_SHELLCMD
1399 && xp->xp_context != EXPAND_DIRECTORIES)
1400 return FALSE;
1401
1402 end = gettail(xp->xp_pattern);
1403 if (end == xp->xp_pattern) // there is no path separator
1404 return FALSE;
1405
1406 for (s = xp->xp_pattern; s < end; s++)
1407 {
1408 // Skip escaped wildcards. Only when the backslash is not a path
1409 // separator, on DOS the '*' "path\*\file" must not be skipped.
1410 if (rem_backslash(s))
1411 ++s;
1412 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1413 return FALSE;
1414 }
1415 return TRUE;
1416}
1417
1418/*
1419 * Prepare a string for expansion.
1420 * When expanding file names: The string will be used with expand_wildcards().
1421 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1422 * When expanding other names: The string will be used with regcomp(). Copy
1423 * the name into allocated memory and prepend "^".
1424 */
1425 char_u *
1426addstar(
1427 char_u *fname,
1428 int len,
1429 int context) // EXPAND_FILES etc.
1430{
1431 char_u *retval;
1432 int i, j;
1433 int new_len;
1434 char_u *tail;
1435 int ends_in_star;
1436
1437 if (context != EXPAND_FILES
1438 && context != EXPAND_FILES_IN_PATH
1439 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001440 && context != EXPAND_DIRECTORIES
1441 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001442 {
1443 // Matching will be done internally (on something other than files).
1444 // So we convert the file-matching-type wildcards into our kind for
1445 // use with vim_regcomp(). First work out how long it will be:
1446
1447 // For help tags the translation is done in find_help_tags().
1448 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001449 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001450 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001451 || context == EXPAND_COLORS
1452 || context == EXPAND_COMPILER
1453 || context == EXPAND_OWNSYNTAX
1454 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001455 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001456 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001457 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001458 || ((context == EXPAND_TAGS_LISTFILES
1459 || context == EXPAND_TAGS)
1460 && fname[0] == '/'))
1461 retval = vim_strnsave(fname, len);
1462 else
1463 {
1464 new_len = len + 2; // +2 for '^' at start, NUL at end
1465 for (i = 0; i < len; i++)
1466 {
1467 if (fname[i] == '*' || fname[i] == '~')
1468 new_len++; // '*' needs to be replaced by ".*"
1469 // '~' needs to be replaced by "\~"
1470
1471 // Buffer names are like file names. "." should be literal
1472 if (context == EXPAND_BUFFERS && fname[i] == '.')
1473 new_len++; // "." becomes "\."
1474
1475 // Custom expansion takes care of special things, match
1476 // backslashes literally (perhaps also for other types?)
1477 if ((context == EXPAND_USER_DEFINED
1478 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1479 new_len++; // '\' becomes "\\"
1480 }
1481 retval = alloc(new_len);
1482 if (retval != NULL)
1483 {
1484 retval[0] = '^';
1485 j = 1;
1486 for (i = 0; i < len; i++, j++)
1487 {
1488 // Skip backslash. But why? At least keep it for custom
1489 // expansion.
1490 if (context != EXPAND_USER_DEFINED
1491 && context != EXPAND_USER_LIST
1492 && fname[i] == '\\'
1493 && ++i == len)
1494 break;
1495
1496 switch (fname[i])
1497 {
1498 case '*': retval[j++] = '.';
1499 break;
1500 case '~': retval[j++] = '\\';
1501 break;
1502 case '?': retval[j] = '.';
1503 continue;
1504 case '.': if (context == EXPAND_BUFFERS)
1505 retval[j++] = '\\';
1506 break;
1507 case '\\': if (context == EXPAND_USER_DEFINED
1508 || context == EXPAND_USER_LIST)
1509 retval[j++] = '\\';
1510 break;
1511 }
1512 retval[j] = fname[i];
1513 }
1514 retval[j] = NUL;
1515 }
1516 }
1517 }
1518 else
1519 {
1520 retval = alloc(len + 4);
1521 if (retval != NULL)
1522 {
1523 vim_strncpy(retval, fname, len);
1524
1525 // Don't add a star to *, ~, ~user, $var or `cmd`.
1526 // * would become **, which walks the whole tree.
1527 // ~ would be at the start of the file name, but not the tail.
1528 // $ could be anywhere in the tail.
1529 // ` could be anywhere in the file name.
1530 // When the name ends in '$' don't add a star, remove the '$'.
1531 tail = gettail(retval);
1532 ends_in_star = (len > 0 && retval[len - 1] == '*');
1533#ifndef BACKSLASH_IN_FILENAME
1534 for (i = len - 2; i >= 0; --i)
1535 {
1536 if (retval[i] != '\\')
1537 break;
1538 ends_in_star = !ends_in_star;
1539 }
1540#endif
1541 if ((*retval != '~' || tail != retval)
1542 && !ends_in_star
1543 && vim_strchr(tail, '$') == NULL
1544 && vim_strchr(retval, '`') == NULL)
1545 retval[len++] = '*';
1546 else if (len > 0 && retval[len - 1] == '$')
1547 --len;
1548 retval[len] = NUL;
1549 }
1550 }
1551 return retval;
1552}
1553
1554/*
1555 * Must parse the command line so far to work out what context we are in.
1556 * Completion can then be done based on that context.
1557 * This routine sets the variables:
1558 * xp->xp_pattern The start of the pattern to be expanded within
1559 * the command line (ends at the cursor).
1560 * xp->xp_context The type of thing to expand. Will be one of:
1561 *
1562 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1563 * the command line, like an unknown command. Caller
1564 * should beep.
1565 * EXPAND_NOTHING Unrecognised context for completion, use char like
1566 * a normal char, rather than for completion. eg
1567 * :s/^I/
1568 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1569 * it.
1570 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1571 * EXPAND_FILES After command with EX_XFILE set, or after setting
1572 * with P_EXPAND set. eg :e ^I, :w>>^I
1573 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001574 * when we know only directories are of interest.
1575 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001576 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1577 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1578 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1579 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1580 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1581 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1582 * EXPAND_EVENTS Complete event names
1583 * EXPAND_SYNTAX Complete :syntax command arguments
1584 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1585 * EXPAND_AUGROUP Complete autocommand group names
1586 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1587 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1588 * eg :unmap a^I , :cunab x^I
1589 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1590 * eg :call sub^I
1591 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1592 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1593 * names in expressions, eg :while s^I
1594 * EXPAND_ENV_VARS Complete environment variable names
1595 * EXPAND_USER Complete user names
1596 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001597 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001598set_expand_context(expand_T *xp)
1599{
1600 cmdline_info_T *ccline = get_cmdline_info();
1601
1602 // only expansion for ':', '>' and '=' command-lines
1603 if (ccline->cmdfirstc != ':'
1604#ifdef FEAT_EVAL
1605 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1606 && !ccline->input_fn
1607#endif
1608 )
1609 {
1610 xp->xp_context = EXPAND_NOTHING;
1611 return;
1612 }
1613 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1614}
1615
Bram Moolenaard0190392019-08-23 21:17:35 +02001616/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001617 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1618 * For user defined commands, the completion context is set in 'xp' and the
1619 * completion flags in 'complp'.
1620 *
1621 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001622 */
1623 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001624set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001625{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001626 char_u *p = NULL;
1627 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001628 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001629
1630 // Isolate the command and search for it in the command table.
1631 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001632 // - the 'k' command can directly be followed by any character, but do
1633 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1634 // find matches anywhere in the command name, do this only for command
1635 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001636 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001637 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001638 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001639 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001640 p = cmd + 1;
1641 }
1642 else
1643 {
1644 p = cmd;
1645 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1646 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001647 // A user command may contain digits.
1648 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1649 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001650 while (ASCII_ISALNUM(*p) || *p == '*')
1651 ++p;
1652 // for python 3.x: ":py3*" commands completion
1653 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1654 {
1655 ++p;
1656 while (ASCII_ISALPHA(*p) || *p == '*')
1657 ++p;
1658 }
1659 // check for non-alpha command
1660 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1661 ++p;
1662 len = (int)(p - cmd);
1663
1664 if (len == 0)
1665 {
1666 xp->xp_context = EXPAND_UNSUCCESSFUL;
1667 return NULL;
1668 }
1669
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001670 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001671
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001672 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001673 // Also when doing fuzzy expansion for non-shell commands, support
1674 // alphanumeric characters.
1675 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1676 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1678 ++p;
1679 }
1680
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001681 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 // character, complete the command name.
1683 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1684 return NULL;
1685
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001686 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001687 {
1688 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1689 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 p = cmd + 1;
1692 }
1693 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1694 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001695 eap->cmd = cmd;
1696 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001697 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001698 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001699 }
1700 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001701 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 {
1703 // Not still touching the command and it was an illegal one
1704 xp->xp_context = EXPAND_UNSUCCESSFUL;
1705 return NULL;
1706 }
1707
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 return p;
1709}
Bram Moolenaard0190392019-08-23 21:17:35 +02001710
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001711/*
1712 * Set the completion context for a command argument with wild card characters.
1713 */
1714 static void
1715set_context_for_wildcard_arg(
1716 exarg_T *eap,
1717 char_u *arg,
1718 int usefilter,
1719 expand_T *xp,
1720 int *complp)
1721{
1722 char_u *p;
1723 int c;
1724 int in_quote = FALSE;
1725 char_u *bow = NULL; // Beginning of word
1726 int len = 0;
1727
1728 // Allow spaces within back-quotes to count as part of the argument
1729 // being expanded.
1730 xp->xp_pattern = skipwhite(arg);
1731 p = xp->xp_pattern;
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 == '\\' && p[1] != NUL)
1739 ++p;
1740 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001743 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001744 xp->xp_pattern = p;
1745 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001746 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001747 in_quote = !in_quote;
1748 }
1749 // An argument can contain just about everything, except
1750 // characters that end the command and white space.
1751 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001752#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001753 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001754#endif
1755 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001756 {
1757 len = 0; // avoid getting stuck when space is in 'isfname'
1758 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001759 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001760 if (has_mbyte)
1761 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001762 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001763 c = *p;
1764 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001765 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001766 if (has_mbyte)
1767 len = (*mb_ptr2len)(p);
1768 else
1769 len = 1;
1770 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001771 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001772 if (in_quote)
1773 bow = p;
1774 else
1775 xp->xp_pattern = p;
1776 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001777 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001778 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001779 }
1780
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001781 // If we are still inside the quotes, and we passed a space, just
1782 // expand from there.
1783 if (bow != NULL && in_quote)
1784 xp->xp_pattern = bow;
1785 xp->xp_context = EXPAND_FILES;
1786
1787 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001788 if (usefilter
1789 || (eap != NULL
1790 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1791 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001792 {
1793#ifndef BACKSLASH_IN_FILENAME
1794 xp->xp_shell = TRUE;
1795#endif
1796 // When still after the command name expand executables.
1797 if (xp->xp_pattern == skipwhite(arg))
1798 xp->xp_context = EXPAND_SHELLCMD;
1799 }
1800
1801 // Check for environment variable.
1802 if (*xp->xp_pattern == '$')
1803 {
1804 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1805 if (!vim_isIDc(*p))
1806 break;
1807 if (*p == NUL)
1808 {
1809 xp->xp_context = EXPAND_ENV_VARS;
1810 ++xp->xp_pattern;
1811 // Avoid that the assignment uses EXPAND_FILES again.
1812 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1813 *complp = EXPAND_ENV_VARS;
1814 }
1815 }
1816 // Check for user names.
1817 if (*xp->xp_pattern == '~')
1818 {
1819 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1820 ;
1821 // Complete ~user only if it partially matches a user name.
1822 // A full match ~user<Tab> will be replaced by user's home
1823 // directory i.e. something like ~user<Tab> -> /home/user/
1824 if (*p == NUL && p > xp->xp_pattern + 1
1825 && match_user(xp->xp_pattern + 1) >= 1)
1826 {
1827 xp->xp_context = EXPAND_USER;
1828 ++xp->xp_pattern;
1829 }
1830 }
1831}
1832
1833/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001834 * Set the completion context for the "++opt=arg" argument. Always returns
1835 * NULL.
1836 */
1837 static char_u *
1838set_context_in_argopt(expand_T *xp, char_u *arg)
1839{
1840 char_u *p;
1841
1842 p = vim_strchr(arg, '=');
1843 if (p == NULL)
1844 xp->xp_pattern = arg;
1845 else
1846 xp->xp_pattern = p + 1;
1847
1848 xp->xp_context = EXPAND_ARGOPT;
1849 return NULL;
1850}
1851
1852#ifdef FEAT_TERMINAL
1853/*
1854 * Set the completion context for :terminal's [options]. Always returns NULL.
1855 */
1856 static char_u *
1857set_context_in_terminalopt(expand_T *xp, char_u *arg)
1858{
1859 char_u *p;
1860
1861 p = vim_strchr(arg, '=');
1862 if (p == NULL)
1863 xp->xp_pattern = arg;
1864 else
1865 xp->xp_pattern = p + 1;
1866
1867 xp->xp_context = EXPAND_TERMINALOPT;
1868 return NULL;
1869}
1870#endif
1871
1872/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001873 * Set the completion context for the :filter command. Returns a pointer to the
1874 * next command after the :filter command.
1875 */
1876 static char_u *
1877set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1878{
1879 if (*arg != NUL)
1880 arg = skip_vimgrep_pat(arg, NULL, NULL);
1881 if (arg == NULL || *arg == NUL)
1882 {
1883 xp->xp_context = EXPAND_NOTHING;
1884 return NULL;
1885 }
1886 return skipwhite(arg);
1887}
1888
1889#ifdef FEAT_SEARCH_EXTRA
1890/*
1891 * Set the completion context for the :match command. Returns a pointer to the
1892 * next command after the :match command.
1893 */
1894 static char_u *
1895set_context_in_match_cmd(expand_T *xp, char_u *arg)
1896{
1897 if (*arg == NUL || !ends_excmd(*arg))
1898 {
1899 // also complete "None"
1900 set_context_in_echohl_cmd(xp, arg);
1901 arg = skipwhite(skiptowhite(arg));
1902 if (*arg != NUL)
1903 {
1904 xp->xp_context = EXPAND_NOTHING;
1905 arg = skip_regexp(arg + 1, *arg, magic_isset());
1906 }
1907 }
1908 return find_nextcmd(arg);
1909}
1910#endif
1911
1912/*
1913 * Returns a pointer to the next command after a :global or a :v command.
1914 * Returns NULL if there is no next command.
1915 */
1916 static char_u *
1917find_cmd_after_global_cmd(char_u *arg)
1918{
1919 int delim;
1920
1921 delim = *arg; // get the delimiter
1922 if (delim)
1923 ++arg; // skip delimiter if there is one
1924
1925 while (arg[0] != NUL && arg[0] != delim)
1926 {
1927 if (arg[0] == '\\' && arg[1] != NUL)
1928 ++arg;
1929 ++arg;
1930 }
1931 if (arg[0] != NUL)
1932 return arg + 1;
1933
1934 return NULL;
1935}
1936
1937/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001938 * Returns a pointer to the next command after a :substitute or a :& command.
1939 * Returns NULL if there is no next command.
1940 */
1941 static char_u *
1942find_cmd_after_substitute_cmd(char_u *arg)
1943{
1944 int delim;
1945
1946 delim = *arg;
1947 if (delim)
1948 {
1949 // skip "from" part
1950 ++arg;
1951 arg = skip_regexp(arg, delim, magic_isset());
1952
1953 if (arg[0] != NUL && arg[0] == delim)
1954 {
1955 // skip "to" part
1956 ++arg;
1957 while (arg[0] != NUL && arg[0] != delim)
1958 {
1959 if (arg[0] == '\\' && arg[1] != NUL)
1960 ++arg;
1961 ++arg;
1962 }
1963 if (arg[0] != NUL) // skip delimiter
1964 ++arg;
1965 }
1966 }
1967 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1968 ++arg;
1969 if (arg[0] != NUL)
1970 return arg;
1971
1972 return NULL;
1973}
1974
1975/*
1976 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1977 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1978 * Returns NULL if there is no next command.
1979 */
1980 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001981find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001982{
1983 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001984 if (*arg != '/')
1985 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001986
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001987 // Match regexp, not just whole words
1988 for (++arg; *arg && *arg != '/'; arg++)
1989 if (*arg == '\\' && arg[1] != NUL)
1990 arg++;
1991 if (*arg)
1992 {
1993 arg = skipwhite(arg + 1);
1994
1995 // Check for trailing illegal characters
1996 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1997 xp->xp_context = EXPAND_NOTHING;
1998 else
1999 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002000 }
2001
2002 return NULL;
2003}
2004
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002005#ifdef FEAT_EVAL
2006/*
2007 * Set the completion context for the :unlet command. Always returns NULL.
2008 */
2009 static char_u *
2010set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2011{
2012 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2013 arg = xp->xp_pattern + 1;
2014
2015 xp->xp_context = EXPAND_USER_VARS;
2016 xp->xp_pattern = arg;
2017
2018 if (*xp->xp_pattern == '$')
2019 {
2020 xp->xp_context = EXPAND_ENV_VARS;
2021 ++xp->xp_pattern;
2022 }
2023
2024 return NULL;
2025}
2026#endif
2027
2028#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2029/*
2030 * Set the completion context for the :language command. Always returns NULL.
2031 */
2032 static char_u *
2033set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2034{
2035 char_u *p;
2036
2037 p = skiptowhite(arg);
2038 if (*p == NUL)
2039 {
2040 xp->xp_context = EXPAND_LANGUAGE;
2041 xp->xp_pattern = arg;
2042 }
2043 else
2044 {
2045 if ( STRNCMP(arg, "messages", p - arg) == 0
2046 || STRNCMP(arg, "ctype", p - arg) == 0
2047 || STRNCMP(arg, "time", p - arg) == 0
2048 || STRNCMP(arg, "collate", p - arg) == 0)
2049 {
2050 xp->xp_context = EXPAND_LOCALES;
2051 xp->xp_pattern = skipwhite(p);
2052 }
2053 else
2054 xp->xp_context = EXPAND_NOTHING;
2055 }
2056
2057 return NULL;
2058}
2059#endif
2060
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002061#ifdef FEAT_EVAL
2062static enum
2063{
2064 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002065 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2066 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002067} breakpt_expand_what;
2068
2069/*
2070 * Set the completion context for the :breakadd command. Always returns NULL.
2071 */
2072 static char_u *
2073set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2074{
2075 char_u *p;
2076 char_u *subcmd_start;
2077
2078 xp->xp_context = EXPAND_BREAKPOINT;
2079 xp->xp_pattern = arg;
2080
2081 if (cmdidx == CMD_breakadd)
2082 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002083 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002084 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002085 else
2086 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002087
2088 p = skipwhite(arg);
2089 if (*p == NUL)
2090 return NULL;
2091 subcmd_start = p;
2092
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002093 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002094 {
2095 // :breakadd file [lnum] <filename>
2096 // :breakadd func [lnum] <funcname>
2097 p += 4;
2098 p = skipwhite(p);
2099
2100 // skip line number (if specified)
2101 if (VIM_ISDIGIT(*p))
2102 {
2103 p = skipdigits(p);
2104 if (*p != ' ')
2105 {
2106 xp->xp_context = EXPAND_NOTHING;
2107 return NULL;
2108 }
2109 p = skipwhite(p);
2110 }
2111 if (STRNCMP("file", subcmd_start, 4) == 0)
2112 xp->xp_context = EXPAND_FILES;
2113 else
2114 xp->xp_context = EXPAND_USER_FUNC;
2115 xp->xp_pattern = p;
2116 }
2117 else if (STRNCMP("expr ", p, 5) == 0)
2118 {
2119 // :breakadd expr <expression>
2120 xp->xp_context = EXPAND_EXPRESSION;
2121 xp->xp_pattern = skipwhite(p + 5);
2122 }
2123
2124 return NULL;
2125}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002126
2127 static char_u *
2128set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2129{
2130 char_u *p;
2131
2132 xp->xp_context = EXPAND_NOTHING;
2133 xp->xp_pattern = NULL;
2134
2135 p = skipwhite(arg);
2136 if (VIM_ISDIGIT(*p))
2137 return NULL;
2138
2139 xp->xp_context = EXPAND_SCRIPTNAMES;
2140 xp->xp_pattern = p;
2141
2142 return NULL;
2143}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002144#endif
2145
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002146/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002147 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2148 * The argument to the command is 'arg' and the argument flags is 'argt'.
2149 * For user-defined commands and for environment variables, 'compl' has the
2150 * completion type.
2151 * Returns a pointer to the next command. Returns NULL if there is no next
2152 * command.
2153 */
2154 static char_u *
2155set_context_by_cmdname(
2156 char_u *cmd,
2157 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002158 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002159 char_u *arg,
2160 long argt,
2161 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002162 int forceit)
2163{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002164 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002165 {
2166 case CMD_find:
2167 case CMD_sfind:
2168 case CMD_tabfind:
2169 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002170 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002171 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002172 break;
2173 case CMD_cd:
2174 case CMD_chdir:
2175 case CMD_tcd:
2176 case CMD_tchdir:
2177 case CMD_lcd:
2178 case CMD_lchdir:
2179 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002180 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002181 break;
2182 case CMD_help:
2183 xp->xp_context = EXPAND_HELP;
2184 xp->xp_pattern = arg;
2185 break;
2186
2187 // Command modifiers: return the argument.
2188 // Also for commands with an argument that is a command.
2189 case CMD_aboveleft:
2190 case CMD_argdo:
2191 case CMD_belowright:
2192 case CMD_botright:
2193 case CMD_browse:
2194 case CMD_bufdo:
2195 case CMD_cdo:
2196 case CMD_cfdo:
2197 case CMD_confirm:
2198 case CMD_debug:
2199 case CMD_folddoclosed:
2200 case CMD_folddoopen:
2201 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002202 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002203 case CMD_keepalt:
2204 case CMD_keepjumps:
2205 case CMD_keepmarks:
2206 case CMD_keeppatterns:
2207 case CMD_ldo:
2208 case CMD_leftabove:
2209 case CMD_lfdo:
2210 case CMD_lockmarks:
2211 case CMD_noautocmd:
2212 case CMD_noswapfile:
2213 case CMD_rightbelow:
2214 case CMD_sandbox:
2215 case CMD_silent:
2216 case CMD_tab:
2217 case CMD_tabdo:
2218 case CMD_topleft:
2219 case CMD_verbose:
2220 case CMD_vertical:
2221 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002222 case CMD_vim9cmd:
2223 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002224 return arg;
2225
2226 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002227 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002228
2229#ifdef FEAT_SEARCH_EXTRA
2230 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002231 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002232#endif
2233
2234 // All completion for the +cmdline_compl feature goes here.
2235
2236 case CMD_command:
2237 return set_context_in_user_cmd(xp, arg);
2238
2239 case CMD_delcommand:
2240 xp->xp_context = EXPAND_USER_COMMANDS;
2241 xp->xp_pattern = arg;
2242 break;
2243
2244 case CMD_global:
2245 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002246 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002247 case CMD_and:
2248 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002249 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002250 case CMD_isearch:
2251 case CMD_dsearch:
2252 case CMD_ilist:
2253 case CMD_dlist:
2254 case CMD_ijump:
2255 case CMD_psearch:
2256 case CMD_djump:
2257 case CMD_isplit:
2258 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002259 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002260 case CMD_autocmd:
2261 return set_context_in_autocmd(xp, arg, FALSE);
2262 case CMD_doautocmd:
2263 case CMD_doautoall:
2264 return set_context_in_autocmd(xp, arg, TRUE);
2265 case CMD_set:
2266 set_context_in_set_cmd(xp, arg, 0);
2267 break;
2268 case CMD_setglobal:
2269 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2270 break;
2271 case CMD_setlocal:
2272 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2273 break;
2274 case CMD_tag:
2275 case CMD_stag:
2276 case CMD_ptag:
2277 case CMD_ltag:
2278 case CMD_tselect:
2279 case CMD_stselect:
2280 case CMD_ptselect:
2281 case CMD_tjump:
2282 case CMD_stjump:
2283 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002284 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002285 xp->xp_context = EXPAND_TAGS_LISTFILES;
2286 else
2287 xp->xp_context = EXPAND_TAGS;
2288 xp->xp_pattern = arg;
2289 break;
2290 case CMD_augroup:
2291 xp->xp_context = EXPAND_AUGROUP;
2292 xp->xp_pattern = arg;
2293 break;
2294#ifdef FEAT_SYN_HL
2295 case CMD_syntax:
2296 set_context_in_syntax_cmd(xp, arg);
2297 break;
2298#endif
2299#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002300 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002301 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002302 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002303 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002304 case CMD_if:
2305 case CMD_elseif:
2306 case CMD_while:
2307 case CMD_for:
2308 case CMD_echo:
2309 case CMD_echon:
2310 case CMD_execute:
2311 case CMD_echomsg:
2312 case CMD_echoerr:
2313 case CMD_call:
2314 case CMD_return:
2315 case CMD_cexpr:
2316 case CMD_caddexpr:
2317 case CMD_cgetexpr:
2318 case CMD_lexpr:
2319 case CMD_laddexpr:
2320 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002321 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002322 break;
2323
2324 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002325 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002326 case CMD_function:
2327 case CMD_delfunction:
2328 xp->xp_context = EXPAND_USER_FUNC;
2329 xp->xp_pattern = arg;
2330 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002331 case CMD_disassemble:
2332 set_context_in_disassemble_cmd(xp, arg);
2333 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002334
2335 case CMD_echohl:
2336 set_context_in_echohl_cmd(xp, arg);
2337 break;
2338#endif
2339 case CMD_highlight:
2340 set_context_in_highlight_cmd(xp, arg);
2341 break;
2342#ifdef FEAT_CSCOPE
2343 case CMD_cscope:
2344 case CMD_lcscope:
2345 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002346 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002347 break;
2348#endif
2349#ifdef FEAT_SIGNS
2350 case CMD_sign:
2351 set_context_in_sign_cmd(xp, arg);
2352 break;
2353#endif
2354 case CMD_bdelete:
2355 case CMD_bwipeout:
2356 case CMD_bunload:
2357 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2358 arg = xp->xp_pattern + 1;
2359 // FALLTHROUGH
2360 case CMD_buffer:
2361 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002362 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002363 case CMD_checktime:
2364 xp->xp_context = EXPAND_BUFFERS;
2365 xp->xp_pattern = arg;
2366 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002367#ifdef FEAT_DIFF
2368 case CMD_diffget:
2369 case CMD_diffput:
2370 // If current buffer is in diff mode, complete buffer names
2371 // which are in diff mode, and different than current buffer.
2372 xp->xp_context = EXPAND_DIFF_BUFFERS;
2373 xp->xp_pattern = arg;
2374 break;
2375#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002376 case CMD_USER:
2377 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002378 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2379 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002380
2381 case CMD_map: case CMD_noremap:
2382 case CMD_nmap: case CMD_nnoremap:
2383 case CMD_vmap: case CMD_vnoremap:
2384 case CMD_omap: case CMD_onoremap:
2385 case CMD_imap: case CMD_inoremap:
2386 case CMD_cmap: case CMD_cnoremap:
2387 case CMD_lmap: case CMD_lnoremap:
2388 case CMD_smap: case CMD_snoremap:
2389 case CMD_tmap: case CMD_tnoremap:
2390 case CMD_xmap: case CMD_xnoremap:
2391 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002392 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002393 case CMD_unmap:
2394 case CMD_nunmap:
2395 case CMD_vunmap:
2396 case CMD_ounmap:
2397 case CMD_iunmap:
2398 case CMD_cunmap:
2399 case CMD_lunmap:
2400 case CMD_sunmap:
2401 case CMD_tunmap:
2402 case CMD_xunmap:
2403 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002404 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002405 case CMD_mapclear:
2406 case CMD_nmapclear:
2407 case CMD_vmapclear:
2408 case CMD_omapclear:
2409 case CMD_imapclear:
2410 case CMD_cmapclear:
2411 case CMD_lmapclear:
2412 case CMD_smapclear:
2413 case CMD_tmapclear:
2414 case CMD_xmapclear:
2415 xp->xp_context = EXPAND_MAPCLEAR;
2416 xp->xp_pattern = arg;
2417 break;
2418
2419 case CMD_abbreviate: case CMD_noreabbrev:
2420 case CMD_cabbrev: case CMD_cnoreabbrev:
2421 case CMD_iabbrev: case CMD_inoreabbrev:
2422 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002423 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002424 case CMD_unabbreviate:
2425 case CMD_cunabbrev:
2426 case CMD_iunabbrev:
2427 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002428 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002429#ifdef FEAT_MENU
2430 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2431 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2432 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2433 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2434 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2435 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2436 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2437 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2438 case CMD_tmenu: case CMD_tunmenu:
2439 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2440 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2441#endif
2442
2443 case CMD_colorscheme:
2444 xp->xp_context = EXPAND_COLORS;
2445 xp->xp_pattern = arg;
2446 break;
2447
2448 case CMD_compiler:
2449 xp->xp_context = EXPAND_COMPILER;
2450 xp->xp_pattern = arg;
2451 break;
2452
2453 case CMD_ownsyntax:
2454 xp->xp_context = EXPAND_OWNSYNTAX;
2455 xp->xp_pattern = arg;
2456 break;
2457
2458 case CMD_setfiletype:
2459 xp->xp_context = EXPAND_FILETYPE;
2460 xp->xp_pattern = arg;
2461 break;
2462
2463 case CMD_packadd:
2464 xp->xp_context = EXPAND_PACKADD;
2465 xp->xp_pattern = arg;
2466 break;
2467
zeertzjqb0d45ec2023-01-25 15:04:22 +00002468 case CMD_runtime:
2469 set_context_in_runtime_cmd(xp, arg);
2470 break;
2471
Bram Moolenaard0190392019-08-23 21:17:35 +02002472#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2473 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002474 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002475#endif
2476#if defined(FEAT_PROFILE)
2477 case CMD_profile:
2478 set_context_in_profile_cmd(xp, arg);
2479 break;
2480#endif
2481 case CMD_behave:
2482 xp->xp_context = EXPAND_BEHAVE;
2483 xp->xp_pattern = arg;
2484 break;
2485
2486 case CMD_messages:
2487 xp->xp_context = EXPAND_MESSAGES;
2488 xp->xp_pattern = arg;
2489 break;
2490
2491 case CMD_history:
2492 xp->xp_context = EXPAND_HISTORY;
2493 xp->xp_pattern = arg;
2494 break;
2495#if defined(FEAT_PROFILE)
2496 case CMD_syntime:
2497 xp->xp_context = EXPAND_SYNTIME;
2498 xp->xp_pattern = arg;
2499 break;
2500#endif
2501
2502 case CMD_argdelete:
2503 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2504 arg = xp->xp_pattern + 1;
2505 xp->xp_context = EXPAND_ARGLIST;
2506 xp->xp_pattern = arg;
2507 break;
2508
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002509#ifdef FEAT_EVAL
2510 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002511 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002512 case CMD_breakdel:
2513 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002514
2515 case CMD_scriptnames:
2516 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002517#endif
2518
Bram Moolenaard0190392019-08-23 21:17:35 +02002519 default:
2520 break;
2521 }
2522 return NULL;
2523}
2524
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002525/*
2526 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2527 * we don't need/want deleted. Maybe this could be done better if we didn't
2528 * repeat all this stuff. The only problem is that they may not stay
2529 * perfectly compatible with each other, but then the command line syntax
2530 * probably won't change that much -- webb.
2531 */
2532 static char_u *
2533set_one_cmd_context(
2534 expand_T *xp,
2535 char_u *buff) // buffer for command string
2536{
2537 char_u *p;
2538 char_u *cmd, *arg;
2539 int len = 0;
2540 exarg_T ea;
2541 int compl = EXPAND_NOTHING;
2542 int forceit = FALSE;
2543 int usefilter = FALSE; // filter instead of file name
2544
2545 ExpandInit(xp);
2546 xp->xp_pattern = buff;
2547 xp->xp_line = buff;
2548 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2549 ea.argt = 0;
2550
2551 // 1. skip comment lines and leading space, colons or bars
2552 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2553 ;
2554 xp->xp_pattern = cmd;
2555
2556 if (*cmd == NUL)
2557 return NULL;
2558 if (*cmd == '"') // ignore comment lines
2559 {
2560 xp->xp_context = EXPAND_NOTHING;
2561 return NULL;
2562 }
2563
2564 // 3. Skip over the range to find the command.
2565 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2566 xp->xp_pattern = cmd;
2567 if (*cmd == NUL)
2568 return NULL;
2569 if (*cmd == '"')
2570 {
2571 xp->xp_context = EXPAND_NOTHING;
2572 return NULL;
2573 }
2574
2575 if (*cmd == '|' || *cmd == '\n')
2576 return cmd + 1; // There's another command
2577
2578 // Get the command index.
2579 p = set_cmd_index(cmd, &ea, xp, &compl);
2580 if (p == NULL)
2581 return NULL;
2582
2583 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2584
2585 if (*p == '!') // forced commands
2586 {
2587 forceit = TRUE;
2588 ++p;
2589 }
2590
2591 // 6. parse arguments
2592 if (!IS_USER_CMDIDX(ea.cmdidx))
2593 ea.argt = excmd_get_argt(ea.cmdidx);
2594
2595 arg = skipwhite(p);
2596
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002597 // Does command allow "++argopt" argument?
2598 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002599 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002600 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2601 {
2602 p = arg + 2;
2603 while (*p && !vim_isspace(*p))
2604 MB_PTR_ADV(p);
2605
2606 // Still touching the command after "++"?
2607 if (*p == NUL)
2608 {
2609 if (ea.argt & EX_ARGOPT)
2610 return set_context_in_argopt(xp, arg + 2);
2611#ifdef FEAT_TERMINAL
2612 if (ea.cmdidx == CMD_terminal)
2613 return set_context_in_terminalopt(xp, arg + 2);
2614#endif
2615 }
2616
2617 arg = skipwhite(p);
2618 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002619 }
2620
2621 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2622 {
2623 if (*arg == '>') // append
2624 {
2625 if (*++arg == '>')
2626 ++arg;
2627 arg = skipwhite(arg);
2628 }
2629 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2630 {
2631 ++arg;
2632 usefilter = TRUE;
2633 }
2634 }
2635
2636 if (ea.cmdidx == CMD_read)
2637 {
2638 usefilter = forceit; // :r! filter if forced
2639 if (*arg == '!') // :r !filter
2640 {
2641 ++arg;
2642 usefilter = TRUE;
2643 }
2644 }
2645
2646 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2647 {
2648 while (*arg == *cmd) // allow any number of '>' or '<'
2649 ++arg;
2650 arg = skipwhite(arg);
2651 }
2652
2653 // Does command allow "+command"?
2654 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2655 {
2656 // Check if we're in the +command
2657 p = arg + 1;
2658 arg = skip_cmd_arg(arg, FALSE);
2659
2660 // Still touching the command after '+'?
2661 if (*arg == NUL)
2662 return p;
2663
2664 // Skip space(s) after +command to get to the real argument
2665 arg = skipwhite(arg);
2666 }
2667
2668
2669 // Check for '|' to separate commands and '"' to start comments.
2670 // Don't do this for ":read !cmd" and ":write !cmd".
2671 if ((ea.argt & EX_TRLBAR) && !usefilter)
2672 {
2673 p = arg;
2674 // ":redir @" is not the start of a comment
2675 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2676 p += 2;
2677 while (*p)
2678 {
2679 if (*p == Ctrl_V)
2680 {
2681 if (p[1] != NUL)
2682 ++p;
2683 }
2684 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2685 || *p == '|' || *p == '\n')
2686 {
2687 if (*(p - 1) != '\\')
2688 {
2689 if (*p == '|' || *p == '\n')
2690 return p + 1;
2691 return NULL; // It's a comment
2692 }
2693 }
2694 MB_PTR_ADV(p);
2695 }
2696 }
2697
2698 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2699 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2700 // no arguments allowed but there is something
2701 return NULL;
2702
2703 // Find start of last argument (argument just before cursor):
2704 p = buff;
2705 xp->xp_pattern = p;
2706 len = (int)STRLEN(buff);
2707 while (*p && p < buff + len)
2708 {
2709 if (*p == ' ' || *p == TAB)
2710 {
2711 // argument starts after a space
2712 xp->xp_pattern = ++p;
2713 }
2714 else
2715 {
2716 if (*p == '\\' && *(p + 1) != NUL)
2717 ++p; // skip over escaped character
2718 MB_PTR_ADV(p);
2719 }
2720 }
2721
2722 if (ea.argt & EX_XFILE)
2723 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2724
2725 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002726 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002727 forceit);
2728}
2729
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002730/*
2731 * Set the completion context in 'xp' for command 'str'
2732 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 void
2734set_cmd_context(
2735 expand_T *xp,
2736 char_u *str, // start of command line
2737 int len, // length of command line (excl. NUL)
2738 int col, // position of cursor
2739 int use_ccline UNUSED) // use ccline for info
2740{
2741#ifdef FEAT_EVAL
2742 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002743 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744#endif
2745 int old_char = NUL;
2746 char_u *nextcomm;
2747
2748 // Avoid a UMR warning from Purify, only save the character if it has been
2749 // written before.
2750 if (col < len)
2751 old_char = str[col];
2752 str[col] = NUL;
2753 nextcomm = str;
2754
2755#ifdef FEAT_EVAL
2756 if (use_ccline && ccline->cmdfirstc == '=')
2757 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002758 // pass CMD_SIZE because there is no real command
2759 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002760 }
2761 else if (use_ccline && ccline->input_fn)
2762 {
2763 xp->xp_context = ccline->xp_context;
2764 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002765 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002766 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2767 {
2768 context = xp->xp_context;
2769 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2770 &context);
2771 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002772 }
2773 else
2774#endif
2775 while (nextcomm != NULL)
2776 nextcomm = set_one_cmd_context(xp, nextcomm);
2777
2778 // Store the string here so that call_user_expand_func() can get to them
2779 // easily.
2780 xp->xp_line = str;
2781 xp->xp_col = col;
2782
2783 str[col] = old_char;
2784}
2785
2786/*
2787 * Expand the command line "str" from context "xp".
2788 * "xp" must have been set by set_cmd_context().
2789 * xp->xp_pattern points into "str", to where the text that is to be expanded
2790 * starts.
2791 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2792 * cursor.
2793 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2794 * key that triggered expansion literally.
2795 * Returns EXPAND_OK otherwise.
2796 */
2797 int
2798expand_cmdline(
2799 expand_T *xp,
2800 char_u *str, // start of command line
2801 int col, // position of cursor
2802 int *matchcount, // return: nr of matches
2803 char_u ***matches) // return: array of pointers to matches
2804{
2805 char_u *file_str = NULL;
2806 int options = WILD_ADD_SLASH|WILD_SILENT;
2807
2808 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2809 {
2810 beep_flush();
2811 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2812 }
2813 if (xp->xp_context == EXPAND_NOTHING)
2814 {
2815 // Caller can use the character as a normal char instead
2816 return EXPAND_NOTHING;
2817 }
2818
2819 // add star to file name, or convert to regexp if not exp. files.
2820 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002821 if (cmdline_fuzzy_completion_supported(xp))
2822 // If fuzzy matching, don't modify the search string
2823 file_str = vim_strsave(xp->xp_pattern);
2824 else
2825 {
2826 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2827 if (file_str == NULL)
2828 return EXPAND_UNSUCCESSFUL;
2829 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002830
2831 if (p_wic)
2832 options += WILD_ICASE;
2833
2834 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002835 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002836 {
2837 *matchcount = 0;
2838 *matches = NULL;
2839 }
2840 vim_free(file_str);
2841
2842 return EXPAND_OK;
2843}
2844
Bram Moolenaar66b51422019-08-18 21:44:12 +02002845/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002846 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002847 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002848 */
2849 static int
2850expand_files_and_dirs(
2851 expand_T *xp,
2852 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002853 char_u ***matches,
2854 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002855 int flags,
2856 int options)
2857{
2858 int free_pat = FALSE;
2859 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002860 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002861
2862 // for ":set path=" and ":set tags=" halve backslashes for escaped
2863 // space
2864 if (xp->xp_backslash != XP_BS_NONE)
2865 {
2866 free_pat = TRUE;
2867 pat = vim_strsave(pat);
2868 for (i = 0; pat[i]; ++i)
2869 if (pat[i] == '\\')
2870 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002871 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872 && pat[i + 1] == '\\'
2873 && pat[i + 2] == '\\'
2874 && pat[i + 3] == ' ')
2875 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002876 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002877 && pat[i + 1] == ' ')
2878 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002879 else if ((xp->xp_backslash & XP_BS_COMMA)
2880 && pat[i + 1] == '\\'
2881 && pat[i + 2] == ',')
2882 STRMOVE(pat + i, pat + i + 2);
2883#ifdef BACKSLASH_IN_FILENAME
2884 else if ((xp->xp_backslash & XP_BS_COMMA)
2885 && pat[i + 1] == ',')
2886 STRMOVE(pat + i, pat + i + 1);
2887#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002888 }
2889 }
2890
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002891 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002892 {
2893#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002894 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002895#endif
2896 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002897 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002898 {
2899 if (xp->xp_context == EXPAND_FILES)
2900 flags |= EW_FILE;
2901 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2902 flags |= (EW_FILE | EW_PATH);
2903 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2904 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2905 else
2906 flags = (flags | EW_DIR) & ~EW_FILE;
2907 if (options & WILD_ICASE)
2908 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002909
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002910 // Expand wildcards, supporting %:h and the like.
2911 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2912 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002913 if (free_pat)
2914 vim_free(pat);
2915#ifdef BACKSLASH_IN_FILENAME
2916 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2917 {
2918 int j;
2919
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002920 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002921 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002922 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002923
2924 while (*ptr != NUL)
2925 {
2926 if (p_csl[0] == 's' && *ptr == '\\')
2927 *ptr = '/';
2928 else if (p_csl[0] == 'b' && *ptr == '/')
2929 *ptr = '\\';
2930 ptr += (*mb_ptr2len)(ptr);
2931 }
2932 }
2933 }
2934#endif
2935 return ret;
2936}
2937
2938/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002939 * Function given to ExpandGeneric() to obtain the possible arguments of the
2940 * ":behave {mswin,xterm}" command.
2941 */
2942 static char_u *
2943get_behave_arg(expand_T *xp UNUSED, int idx)
2944{
2945 if (idx == 0)
2946 return (char_u *)"mswin";
2947 if (idx == 1)
2948 return (char_u *)"xterm";
2949 return NULL;
2950}
2951
K.Takata161b6ac2022-11-14 15:31:07 +00002952#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002953/*
2954 * Function given to ExpandGeneric() to obtain the possible arguments of the
2955 * ":breakadd {expr, file, func, here}" command.
2956 * ":breakdel {func, file, here}" command.
2957 */
2958 static char_u *
2959get_breakadd_arg(expand_T *xp UNUSED, int idx)
2960{
2961 char *opts[] = {"expr", "file", "func", "here"};
2962
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002963 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002964 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002965 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002966 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2967 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002968 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2969 {
2970 // breakdel {func, file, here}
2971 if (idx <= 2)
2972 return (char_u *)opts[idx + 1];
2973 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002974 else
2975 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002976 // profdel {func, file}
2977 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002978 return (char_u *)opts[idx + 1];
2979 }
2980 }
2981 return NULL;
2982}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002983
2984/*
2985 * Function given to ExpandGeneric() to obtain the possible arguments for the
2986 * ":scriptnames" command.
2987 */
2988 static char_u *
2989get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2990{
2991 scriptitem_T *si;
2992
2993 if (!SCRIPT_ID_VALID(idx + 1))
2994 return NULL;
2995
2996 si = SCRIPT_ITEM(idx + 1);
2997 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2998 return NameBuff;
2999}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003000#endif
3001
Bram Moolenaard0190392019-08-23 21:17:35 +02003002/*
3003 * Function given to ExpandGeneric() to obtain the possible arguments of the
3004 * ":messages {clear}" command.
3005 */
3006 static char_u *
3007get_messages_arg(expand_T *xp UNUSED, int idx)
3008{
3009 if (idx == 0)
3010 return (char_u *)"clear";
3011 return NULL;
3012}
3013
3014 static char_u *
3015get_mapclear_arg(expand_T *xp UNUSED, int idx)
3016{
3017 if (idx == 0)
3018 return (char_u *)"<buffer>";
3019 return NULL;
3020}
3021
3022/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003023 * Do the expansion based on xp->xp_context and 'rmp'.
3024 */
3025 static int
3026ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003027 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003028 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003029 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003030 char_u ***matches,
3031 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003032{
3033 static struct expgen
3034 {
3035 int context;
3036 char_u *((*func)(expand_T *, int));
3037 int ic;
3038 int escaped;
3039 } tab[] =
3040 {
3041 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3042 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3043 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3044 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3045 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3046 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3047 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3048 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3049 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3050 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003051#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003052 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3053 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3054 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3055 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3056 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003057#endif
3058#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003059 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3060 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003061#endif
3062#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003063 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003064#endif
3065#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003066 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003067#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003068 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3069 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3070 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003071#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003072 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003073#endif
3074#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003075 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003076#endif
3077#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003078 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003079#endif
3080#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003081 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3082 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003083#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003084 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3085 {EXPAND_USER, get_users, TRUE, FALSE},
3086 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003087#ifdef FEAT_EVAL
3088 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003089 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003090#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003091 };
3092 int i;
3093 int ret = FAIL;
3094
3095 // Find a context in the table and call the ExpandGeneric() with the
3096 // right function to do the expansion.
3097 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3098 {
3099 if (xp->xp_context == tab[i].context)
3100 {
3101 if (tab[i].ic)
3102 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003103 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3104 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003105 break;
3106 }
3107 }
3108
3109 return ret;
3110}
3111
3112/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003113 * Map wild expand options to flags for expand_wildcards()
3114 */
3115 static int
3116map_wildopts_to_ewflags(int options)
3117{
3118 int flags;
3119
3120 flags = EW_DIR; // include directories
3121 if (options & WILD_LIST_NOTFOUND)
3122 flags |= EW_NOTFOUND;
3123 if (options & WILD_ADD_SLASH)
3124 flags |= EW_ADDSLASH;
3125 if (options & WILD_KEEP_ALL)
3126 flags |= EW_KEEPALL;
3127 if (options & WILD_SILENT)
3128 flags |= EW_SILENT;
3129 if (options & WILD_NOERROR)
3130 flags |= EW_NOERROR;
3131 if (options & WILD_ALLLINKS)
3132 flags |= EW_ALLLINKS;
3133
3134 return flags;
3135}
3136
3137/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 * Do the expansion based on xp->xp_context and "pat".
3139 */
3140 static int
3141ExpandFromContext(
3142 expand_T *xp,
3143 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003144 char_u ***matches,
3145 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146 int options) // WILD_ flags
3147{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 int ret;
3150 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003151 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003152 int fuzzy = cmdline_fuzzy_complete(pat)
3153 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003155 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003156
3157 if (xp->xp_context == EXPAND_FILES
3158 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003159 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003160 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003161 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003162 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3163 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003164
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003165 *matches = (char_u **)"";
3166 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167 if (xp->xp_context == EXPAND_HELP)
3168 {
3169 // With an empty argument we would get all the help tags, which is
3170 // very slow. Get matches for "help" instead.
3171 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003172 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 {
3174#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003175 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176#endif
3177 return OK;
3178 }
3179 return FAIL;
3180 }
3181
Bram Moolenaar66b51422019-08-18 21:44:12 +02003182 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003183 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003184 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003185 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003187 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003188#ifdef FEAT_DIFF
3189 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003190 return ExpandBufnames(pat, numMatches, matches,
3191 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003192#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 if (xp->xp_context == EXPAND_TAGS
3194 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003195 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3196 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003197 if (xp->xp_context == EXPAND_COLORS)
3198 {
3199 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003200 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003201 directories);
3202 }
3203 if (xp->xp_context == EXPAND_COMPILER)
3204 {
3205 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003206 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003207 }
3208 if (xp->xp_context == EXPAND_OWNSYNTAX)
3209 {
3210 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003211 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212 }
3213 if (xp->xp_context == EXPAND_FILETYPE)
3214 {
3215 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003216 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003217 }
Doug Kearns81642d92024-01-04 22:37:44 +01003218#ifdef FEAT_KEYMAP
3219 if (xp->xp_context == EXPAND_KEYMAP)
3220 {
3221 char *directories[] = {"keymap", NULL};
3222 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3223 }
3224#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003225#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003227 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003228#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003229 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003230 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003231 if (xp->xp_context == EXPAND_RUNTIME)
3232 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003233
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003234 // When expanding a function name starting with s:, match the <SNR>nr_
3235 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003236 if ((xp->xp_context == EXPAND_USER_FUNC
3237 || xp->xp_context == EXPAND_DISASSEMBLE)
3238 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003239 {
3240 int len = (int)STRLEN(pat) + 20;
3241
3242 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003243 if (tofree == NULL)
3244 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003245 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003246 pat = tofree;
3247 }
3248
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003249 if (!fuzzy)
3250 {
3251 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3252 if (regmatch.regprog == NULL)
3253 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003255 // set ignore-case according to p_ic, p_scs and pat
3256 regmatch.rm_ic = ignorecase(pat);
3257 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258
3259 if (xp->xp_context == EXPAND_SETTINGS
3260 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003261 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003262 else if (xp->xp_context == EXPAND_STRING_SETTING)
3263 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3264 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3265 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003267 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003268 else if (xp->xp_context == EXPAND_ARGOPT)
3269 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003270 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3271 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003272#if defined(FEAT_TERMINAL)
3273 else if (xp->xp_context == EXPAND_TERMINALOPT)
3274 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3275#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003276#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003278 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003279#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003280 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003281 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003282
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003283 if (!fuzzy)
3284 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003285 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286
3287 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288}
3289
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003290 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003291ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003292 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003293 expand_T *xp,
3294 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003295 char_u ***matches,
3296 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003297 char_u *((*func)(expand_T *, int)),
3298 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003299 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300{
Yee Cheng China7b81202025-02-23 09:32:47 +01003301 return ExpandGenericExt(
3302 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3303}
3304
3305/*
3306 * Expand a list of names.
3307 *
3308 * Generic function for command line completion. It calls a function to
3309 * obtain strings, one by one. The strings are matched against a regexp
3310 * program. Matching strings are copied into an array, which is returned.
3311 *
3312 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3313 * is used.
3314 *
3315 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3316 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3317 * sorting.
3318 *
3319 * Returns OK when no problems encountered, FAIL for error (out of memory).
3320 */
3321 int
3322ExpandGenericExt(
3323 char_u *pat,
3324 expand_T *xp,
3325 regmatch_T *regmatch,
3326 char_u ***matches,
3327 int *numMatches,
3328 char_u *((*func)(expand_T *, int)),
3329 // returns a string from the list
3330 int escaped,
3331 int sortStartIdx)
3332{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003334 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003335 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003336 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003337 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003338 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003339 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003340 int sort_matches = FALSE;
3341 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003342 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003343
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003344 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003345 *matches = NULL;
3346 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003347
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003348 if (!fuzzy)
3349 ga_init2(&ga, sizeof(char *), 30);
3350 else
3351 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3352
3353 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003355 str = (*func)(xp, i);
3356 if (str == NULL) // end of list
3357 break;
3358 if (*str == NUL) // skip empty strings
3359 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003361 if (xp->xp_pattern[0] != NUL)
3362 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003363 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003364 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003365 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003367 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003368 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003369 }
3370 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003371 else
3372 match = TRUE;
3373
3374 if (!match)
3375 continue;
3376
3377 if (escaped)
3378 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3379 else
3380 str = vim_strsave(str);
3381 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003382 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003383 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003384 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003385 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003386 return FAIL;
3387 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003388 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003389 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003390 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003391
3392 if (ga_grow(&ga, 1) == FAIL)
3393 {
3394 vim_free(str);
3395 break;
3396 }
3397
3398 if (fuzzy)
3399 {
3400 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3401 fuzmatch->idx = ga.ga_len;
3402 fuzmatch->str = str;
3403 fuzmatch->score = score;
3404 }
3405 else
3406 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3407
K.Takata161b6ac2022-11-14 15:31:07 +00003408#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003409 if (func == get_menu_names)
3410 {
3411 // test for separator added by get_menu_names()
3412 str += STRLEN(str) - 1;
3413 if (*str == '\001')
3414 *str = '.';
3415 }
K.Takata161b6ac2022-11-14 15:31:07 +00003416#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003417
Yee Cheng China7b81202025-02-23 09:32:47 +01003418 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3419 {
3420 // Found first item to start sorting from. This is usually 0.
3421 sortStartMatchIdx = ga.ga_len;
3422 }
3423
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003424 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003425 }
3426
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003427 if (ga.ga_len == 0)
3428 return OK;
3429
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003430 // sort the matches when using regular expression matching and sorting
3431 // applies to the completion context. Menus and scriptnames should be kept
3432 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003433 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003434 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003435 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003436 && xp->xp_context != EXPAND_SCRIPTNAMES
3437 && xp->xp_context != EXPAND_ARGOPT
3438 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003439 sort_matches = TRUE;
3440
3441 // <SNR> functions should be sorted to the end.
3442 if (xp->xp_context == EXPAND_EXPRESSION
3443 || xp->xp_context == EXPAND_FUNCTIONS
3444 || xp->xp_context == EXPAND_USER_FUNC
3445 || xp->xp_context == EXPAND_DISASSEMBLE)
3446 funcsort = TRUE;
3447
3448 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003449 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003450 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003451 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003452 // <SNR> functions should be sorted to the end.
3453 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3454 sort_func_compare);
3455 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003456 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003457 }
3458
3459 if (!fuzzy)
3460 {
3461 *matches = ga.ga_data;
3462 *numMatches = ga.ga_len;
3463 }
3464 else
3465 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003466 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3467 funcsort) == FAIL)
3468 return FAIL;
3469 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003470 }
3471
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003472#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 // Reset the variables used for special highlight names expansion, so that
3474 // they don't show up when getting normal highlight names by ID.
3475 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003476#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003477
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478 return OK;
3479}
3480
3481/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003482 * Expand shell command matches in one directory of $PATH.
3483 */
3484 static void
3485expand_shellcmd_onedir(
3486 char_u *buf,
3487 char_u *s,
3488 size_t l,
3489 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003490 char_u ***matches,
3491 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003492 int flags,
3493 hashtab_T *ht,
3494 garray_T *gap)
3495{
3496 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003497 hash_T hash;
3498 hashitem_T *hi;
3499
3500 vim_strncpy(buf, s, l);
3501 add_pathsep(buf);
3502 l = STRLEN(buf);
3503 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3504
3505 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003506 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003507 if (ret != OK)
3508 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003509
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003510 if (ga_grow(gap, *numMatches) == FAIL)
3511 {
3512 FreeWild(*numMatches, *matches);
3513 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003514 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003515
3516 for (int i = 0; i < *numMatches; ++i)
3517 {
3518 char_u *name = (*matches)[i];
3519
3520 if (STRLEN(name) > l)
3521 {
3522 // Check if this name was already found.
3523 hash = hash_hash(name + l);
3524 hi = hash_lookup(ht, name + l, hash);
3525 if (HASHITEM_EMPTY(hi))
3526 {
3527 // Remove the path that was prepended.
3528 STRMOVE(name, name + l);
3529 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3530 hash_add_item(ht, hi, name, hash);
3531 name = NULL;
3532 }
3533 }
3534 vim_free(name);
3535 }
3536 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003537}
3538
3539/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 * Complete a shell command.
3541 * Returns FAIL or OK;
3542 */
3543 static int
3544expand_shellcmd(
3545 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003546 char_u ***matches, // return: array with matches
3547 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003548 int flagsarg) // EW_ flags
3549{
3550 char_u *pat;
3551 int i;
3552 char_u *path = NULL;
3553 int mustfree = FALSE;
3554 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003555 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556 size_t l;
3557 char_u *s, *e;
3558 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003559 int did_curdir = FALSE;
3560 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003561
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003562 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563 if (buf == NULL)
3564 return FAIL;
3565
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003566 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003568 if (pat == NULL)
3569 {
3570 vim_free(buf);
3571 return FAIL;
3572 }
3573
Bram Moolenaar66b51422019-08-18 21:44:12 +02003574 for (i = 0; pat[i]; ++i)
3575 if (pat[i] == '\\' && pat[i + 1] == ' ')
3576 STRMOVE(pat + i, pat + i + 1);
3577
3578 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3579
3580 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3581 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3582 path = (char_u *)".";
3583 else
3584 {
3585 // For an absolute name we don't use $PATH.
3586 if (!mch_isFullName(pat))
3587 path = vim_getenv((char_u *)"PATH", &mustfree);
3588 if (path == NULL)
3589 path = (char_u *)"";
3590 }
3591
3592 // Go over all directories in $PATH. Expand matches in that directory and
3593 // collect them in "ga". When "." is not in $PATH also expand for the
3594 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003595 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003596 hash_init(&found_ht);
3597 for (s = path; ; s = e)
3598 {
K.Takata161b6ac2022-11-14 15:31:07 +00003599#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003600 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003601#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003602 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003603#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003604 if (e == NULL)
3605 e = s + STRLEN(s);
3606
3607 if (*s == NUL)
3608 {
3609 if (did_curdir)
3610 break;
3611 // Find directories in the current directory, path is empty.
3612 did_curdir = TRUE;
3613 flags |= EW_DIR;
3614 }
3615 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3616 {
3617 did_curdir = TRUE;
3618 flags |= EW_DIR;
3619 }
3620 else
3621 // Do not match directories inside a $PATH item.
3622 flags &= ~EW_DIR;
3623
3624 l = e - s;
3625 if (l > MAXPATHL - 5)
3626 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003627
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003628 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003629 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003630
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631 if (*e != NUL)
3632 ++e;
3633 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003634 *matches = ga.ga_data;
3635 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636
3637 vim_free(buf);
3638 vim_free(pat);
3639 if (mustfree)
3640 vim_free(path);
3641 hash_clear(&found_ht);
3642 return OK;
3643}
3644
K.Takata161b6ac2022-11-14 15:31:07 +00003645#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003646/*
3647 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003648 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649 */
3650 static void *
3651call_user_expand_func(
3652 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003653 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003654{
3655 cmdline_info_T *ccline = get_cmdline_info();
3656 int keep = 0;
3657 typval_T args[4];
3658 sctx_T save_current_sctx = current_sctx;
3659 char_u *pat = NULL;
3660 void *ret;
3661
3662 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3663 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664
3665 if (ccline->cmdbuff != NULL)
3666 {
3667 keep = ccline->cmdbuff[ccline->cmdlen];
3668 ccline->cmdbuff[ccline->cmdlen] = 0;
3669 }
3670
3671 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3672
3673 args[0].v_type = VAR_STRING;
3674 args[0].vval.v_string = pat;
3675 args[1].v_type = VAR_STRING;
3676 args[1].vval.v_string = xp->xp_line;
3677 args[2].v_type = VAR_NUMBER;
3678 args[2].vval.v_number = xp->xp_col;
3679 args[3].v_type = VAR_UNKNOWN;
3680
3681 current_sctx = xp->xp_script_ctx;
3682
3683 ret = user_expand_func(xp->xp_arg, 3, args);
3684
3685 current_sctx = save_current_sctx;
3686 if (ccline->cmdbuff != NULL)
3687 ccline->cmdbuff[ccline->cmdlen] = keep;
3688
3689 vim_free(pat);
3690 return ret;
3691}
3692
3693/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003694 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3695 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003696 */
3697 static int
3698ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003699 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003700 expand_T *xp,
3701 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003702 char_u ***matches,
3703 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003704{
3705 char_u *retstr;
3706 char_u *s;
3707 char_u *e;
3708 int keep;
3709 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003710 int fuzzy;
3711 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003712 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003713
3714 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003715 *matches = NULL;
3716 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003717
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003718 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719 if (retstr == NULL)
3720 return FAIL;
3721
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003722 if (!fuzzy)
3723 ga_init2(&ga, sizeof(char *), 3);
3724 else
3725 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3726
Bram Moolenaar66b51422019-08-18 21:44:12 +02003727 for (s = retstr; *s != NUL; s = e)
3728 {
3729 e = vim_strchr(s, '\n');
3730 if (e == NULL)
3731 e = s + STRLEN(s);
3732 keep = *e;
3733 *e = NUL;
3734
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003735 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003736 {
3737 if (!fuzzy)
3738 match = vim_regexec(regmatch, s, (colnr_T)0);
3739 else
3740 {
3741 score = fuzzy_match_str(s, pat);
3742 match = (score != 0);
3743 }
3744 }
3745 else
3746 match = TRUE; // match everything
3747
Bram Moolenaar66b51422019-08-18 21:44:12 +02003748 *e = keep;
3749
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003750 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751 {
3752 if (ga_grow(&ga, 1) == FAIL)
3753 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003754 if (!fuzzy)
3755 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3756 else
3757 {
3758 fuzmatch_str_T *fuzmatch =
3759 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003760 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003761 fuzmatch->str = vim_strnsave(s, e - s);
3762 fuzmatch->score = score;
3763 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003764 ++ga.ga_len;
3765 }
3766
3767 if (*e != NUL)
3768 ++e;
3769 }
3770 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003771
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003772 if (ga.ga_len == 0)
3773 return OK;
3774
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003775 if (!fuzzy)
3776 {
3777 *matches = ga.ga_data;
3778 *numMatches = ga.ga_len;
3779 }
3780 else
3781 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003782 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3783 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003784 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003785 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003786 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003787 return OK;
3788}
3789
3790/*
3791 * Expand names with a list returned by a function defined by the user.
3792 */
3793 static int
3794ExpandUserList(
3795 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003796 char_u ***matches,
3797 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003798{
3799 list_T *retlist;
3800 listitem_T *li;
3801 garray_T ga;
3802
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003803 *matches = NULL;
3804 *numMatches = 0;
3805 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003806 if (retlist == NULL)
3807 return FAIL;
3808
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003809 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003811 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003812 {
3813 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3814 continue; // Skip non-string items and empty strings
3815
3816 if (ga_grow(&ga, 1) == FAIL)
3817 break;
3818
3819 ((char_u **)ga.ga_data)[ga.ga_len] =
3820 vim_strsave(li->li_tv.vval.v_string);
3821 ++ga.ga_len;
3822 }
3823 list_unref(retlist);
3824
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003825 *matches = ga.ga_data;
3826 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003827 return OK;
3828}
K.Takata161b6ac2022-11-14 15:31:07 +00003829#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003830
3831/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003832 * Expand "file" for all comma-separated directories in "path".
3833 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003834 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003835 */
3836 void
3837globpath(
3838 char_u *path,
3839 char_u *file,
3840 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003841 int expand_options,
3842 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003843{
3844 expand_T xpc;
3845 char_u *buf;
3846 int i;
3847 int num_p;
3848 char_u **p;
3849
3850 buf = alloc(MAXPATHL);
3851 if (buf == NULL)
3852 return;
3853
3854 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003855 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856
3857 // Loop over all entries in {path}.
3858 while (*path != NUL)
3859 {
3860 // Copy one item of the path to buf[] and concatenate the file name.
3861 copy_option_part(&path, buf, MAXPATHL, ",");
3862 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3863 {
K.Takata161b6ac2022-11-14 15:31:07 +00003864#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003865 // Using the platform's path separator (\) makes vim incorrectly
3866 // treat it as an escape character, use '/' instead.
3867 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3868 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003869#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003870 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003871#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003872 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003873 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003874 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3875 {
3876 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3877
3878 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003879 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003880 for (i = 0; i < num_p; ++i)
3881 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003882 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003883 ++ga->ga_len;
3884 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003885 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003886 }
3887 }
3888 }
3889
3890 vim_free(buf);
3891}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003892
Bram Moolenaareadee482020-09-04 15:37:31 +02003893/*
3894 * Translate some keys pressed when 'wildmenu' is used.
3895 */
3896 int
3897wildmenu_translate_key(
3898 cmdline_info_T *cclp,
3899 int key,
3900 expand_T *xp,
3901 int did_wild_list)
3902{
3903 int c = key;
3904
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003905 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003906 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003907 // When the popup menu is used for cmdline completion:
3908 // Up : go to the previous item in the menu
3909 // Down : go to the next item in the menu
3910 // Left : go to the parent directory
3911 // Right: list the files in the selected directory
3912 switch (c)
3913 {
3914 case K_UP: c = K_LEFT; break;
3915 case K_DOWN: c = K_RIGHT; break;
3916 case K_LEFT: c = K_UP; break;
3917 case K_RIGHT: c = K_DOWN; break;
3918 default: break;
3919 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003920 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003921
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003922 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003923 {
3924 if (c == K_LEFT)
3925 c = Ctrl_P;
3926 else if (c == K_RIGHT)
3927 c = Ctrl_N;
3928 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003929
Bram Moolenaareadee482020-09-04 15:37:31 +02003930 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003931 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003932 && cclp->cmdpos > 1
3933 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3934 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3935 && (c == '\n' || c == '\r' || c == K_KENTER))
3936 c = K_DOWN;
3937
3938 return c;
3939}
3940
3941/*
3942 * Delete characters on the command line, from "from" to the current
3943 * position.
3944 */
3945 static void
3946cmdline_del(cmdline_info_T *cclp, int from)
3947{
3948 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3949 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3950 cclp->cmdlen -= cclp->cmdpos - from;
3951 cclp->cmdpos = from;
3952}
3953
3954/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003955 * Handle a key pressed when the wild menu for the menu names
3956 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003957 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003958 static int
3959wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003960{
Bram Moolenaareadee482020-09-04 15:37:31 +02003961 int i;
3962 int j;
3963
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003964 // Hitting <Down> after "emenu Name.": complete submenu
3965 if (key == K_DOWN && cclp->cmdpos > 0
3966 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003967 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003968 key = p_wc;
3969 KeyTyped = TRUE; // in case the key was mapped
3970 }
3971 else if (key == K_UP)
3972 {
3973 // Hitting <Up>: Remove one submenu name in front of the
3974 // cursor
3975 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003976
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003977 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3978 i = 0;
3979 while (--j > 0)
3980 {
3981 // check for start of menu name
3982 if (cclp->cmdbuff[j] == ' '
3983 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003984 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003985 i = j + 1;
3986 break;
3987 }
3988 // check for start of submenu name
3989 if (cclp->cmdbuff[j] == '.'
3990 && cclp->cmdbuff[j - 1] != '\\')
3991 {
3992 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003993 {
3994 i = j + 1;
3995 break;
3996 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003997 else
3998 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003999 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004000 }
4001 if (i > 0)
4002 cmdline_del(cclp, i);
4003 key = p_wc;
4004 KeyTyped = TRUE; // in case the key was mapped
4005 xp->xp_context = EXPAND_NOTHING;
4006 }
4007
4008 return key;
4009}
4010
4011/*
4012 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4013 * directory names (EXPAND_DIRECTORIES) or shell command names
4014 * (EXPAND_SHELLCMD) is displayed.
4015 */
4016 static int
4017wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4018{
4019 int i;
4020 int j;
4021 char_u upseg[5];
4022
4023 upseg[0] = PATHSEP;
4024 upseg[1] = '.';
4025 upseg[2] = '.';
4026 upseg[3] = PATHSEP;
4027 upseg[4] = NUL;
4028
4029 if (key == K_DOWN
4030 && cclp->cmdpos > 0
4031 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4032 && (cclp->cmdpos < 3
4033 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4034 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4035 {
4036 // go down a directory
4037 key = p_wc;
4038 KeyTyped = TRUE; // in case the key was mapped
4039 }
4040 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4041 {
4042 // If in a direct ancestor, strip off one ../ to go down
4043 int found = FALSE;
4044
4045 j = cclp->cmdpos;
4046 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4047 while (--j > i)
4048 {
4049 if (has_mbyte)
4050 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4051 if (vim_ispathsep(cclp->cmdbuff[j]))
4052 {
4053 found = TRUE;
4054 break;
4055 }
4056 }
4057 if (found
4058 && cclp->cmdbuff[j - 1] == '.'
4059 && cclp->cmdbuff[j - 2] == '.'
4060 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4061 {
4062 cmdline_del(cclp, j - 2);
4063 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004064 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004065 }
4066 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004067 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004068 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004069 // go up a directory
4070 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004071
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004072 j = cclp->cmdpos - 1;
4073 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4074 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004075 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004076 if (has_mbyte)
4077 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4078 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004079#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004080 && vim_strchr((char_u *)" *?[{`$%#",
4081 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004082#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004083 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004084 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004085 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004086 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004087 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004088 break;
4089 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004090 else
4091 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004092 }
4093 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004094
4095 if (!found)
4096 j = i;
4097 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4098 j += 4;
4099 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4100 && j == i)
4101 j += 3;
4102 else
4103 j = 0;
4104 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004105 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004106 // TODO this is only for DOS/UNIX systems - need to put in
4107 // machine-specific stuff here and in upseg init
4108 cmdline_del(cclp, j);
4109 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004110 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004111 else if (cclp->cmdpos > i)
4112 cmdline_del(cclp, i);
4113
4114 // Now complete in the new directory. Set KeyTyped in case the
4115 // Up key came from a mapping.
4116 key = p_wc;
4117 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004118 }
4119
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004120 return key;
4121}
4122
4123/*
4124 * Handle a key pressed when the wild menu is displayed
4125 */
4126 int
4127wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4128{
4129 if (xp->xp_context == EXPAND_MENUNAMES)
4130 return wildmenu_process_key_menunames(cclp, key, xp);
4131 else if ((xp->xp_context == EXPAND_FILES
4132 || xp->xp_context == EXPAND_DIRECTORIES
4133 || xp->xp_context == EXPAND_SHELLCMD))
4134 return wildmenu_process_key_filenames(cclp, key, xp);
4135
4136 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004137}
4138
4139/*
4140 * Free expanded names when finished walking through the matches
4141 */
4142 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004143wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004144{
4145 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004146
4147 if (!p_wmnu || wild_menu_showing == 0)
4148 return;
4149
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004150#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004151 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004152 if (cclp->input_fn)
4153 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004154#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004155
4156 if (wild_menu_showing == WM_SCROLLED)
4157 {
4158 // Entered command line, move it up
4159 cmdline_row--;
4160 redrawcmd();
4161 }
4162 else if (save_p_ls != -1)
4163 {
4164 // restore 'laststatus' and 'winminheight'
4165 p_ls = save_p_ls;
4166 p_wmh = save_p_wmh;
4167 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004168 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004169 redrawcmd();
4170 save_p_ls = -1;
4171 }
4172 else
4173 {
4174 win_redraw_last_status(topframe);
4175 redraw_statuslines();
4176 }
4177 KeyTyped = skt;
4178 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004179#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004180 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004181 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004182#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004183}
Bram Moolenaareadee482020-09-04 15:37:31 +02004184
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004185#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004186/*
4187 * "getcompletion()" function
4188 */
4189 void
4190f_getcompletion(typval_T *argvars, typval_T *rettv)
4191{
4192 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004193 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004194 expand_T xpc;
4195 int filtered = FALSE;
4196 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004197 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004198
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004199 if (in_vim9script()
4200 && (check_for_string_arg(argvars, 0) == FAIL
4201 || check_for_string_arg(argvars, 1) == FAIL
4202 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4203 return;
4204
ii144785fe02021-11-21 12:13:56 +00004205 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004206 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004207 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004208 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004209
4210 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004211 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004212
4213 if (p_wic)
4214 options |= WILD_ICASE;
4215
4216 // For filtered results, 'wildignore' is used
4217 if (!filtered)
4218 options |= WILD_KEEP_ALL;
4219
4220 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004221 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004222 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004223 int cmdline_len = (int)STRLEN(pat);
4224 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004225 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004226 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004227 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004228 else
4229 {
ii144785fe02021-11-21 12:13:56 +00004230 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004231 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004232 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004233
4234 xpc.xp_context = cmdcomplete_str_to_type(type);
4235 if (xpc.xp_context == EXPAND_NOTHING)
4236 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004237 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004238 return;
4239 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004240
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004241 if (xpc.xp_context == EXPAND_USER_DEFINED)
4242 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004243 // Must be "custom,funcname" pattern
4244 if (STRNCMP(type, "custom,", 7) != 0)
4245 {
4246 semsg(_(e_invalid_argument_str), type);
4247 return;
4248 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004249
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004250 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004251 }
4252
4253 if (xpc.xp_context == EXPAND_USER_LIST)
4254 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004255 // Must be "customlist,funcname" pattern
4256 if (STRNCMP(type, "customlist,", 11) != 0)
4257 {
4258 semsg(_(e_invalid_argument_str), type);
4259 return;
4260 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004261
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004262 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004263 }
4264
Bram Moolenaar66b51422019-08-18 21:44:12 +02004265# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004266 if (xpc.xp_context == EXPAND_MENUS)
4267 {
4268 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4269 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4270 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004271# endif
4272# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004273 if (xpc.xp_context == EXPAND_CSCOPE)
4274 {
4275 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4276 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4277 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004278# endif
4279# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004280 if (xpc.xp_context == EXPAND_SIGN)
4281 {
4282 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4283 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4284 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004285# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004286 if (xpc.xp_context == EXPAND_RUNTIME)
4287 {
4288 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4289 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4290 }
zeertzjq85f36d62024-10-10 19:14:13 +02004291 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4292 {
4293 int context = EXPAND_SHELLCMDLINE;
4294 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4295 &context);
4296 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4297 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004298 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004299
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004300 if (cmdline_fuzzy_completion_supported(&xpc))
4301 // when fuzzy matching, don't modify the search string
4302 pat = vim_strsave(xpc.xp_pattern);
4303 else
4304 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4305
Bram Moolenaar93a10962022-06-16 11:42:09 +01004306 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004307 {
4308 int i;
4309
4310 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4311
4312 for (i = 0; i < xpc.xp_numfiles; i++)
4313 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4314 }
4315 vim_free(pat);
4316 ExpandCleanup(&xpc);
4317}
Girish Palya92f68e22025-04-21 11:12:41 +02004318
4319/*
4320 * "cmdcomplete_info()" function
4321 */
4322 void
4323f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4324{
4325 cmdline_info_T *ccline = get_cmdline_info();
4326 dict_T *retdict;
4327 list_T *li;
4328 int idx;
4329 int ret = OK;
4330
4331 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4332 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4333 return;
4334 retdict = rettv->vval.v_dict;
4335 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4336 if (ret == OK)
4337 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4338 if (ret == OK)
4339 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4340 if (ret == OK)
4341 {
4342 li = list_alloc();
4343 if (li == NULL)
4344 return;
4345 ret = dict_add_list(retdict, "matches", li);
4346 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4347 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4348 }
4349}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004350#endif // FEAT_EVAL