blob: 45f69bb2fe9287f6ed9508bb272e53eadcbb92a9 [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
385 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
386 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;
Girish Palya92f68e22025-04-21 11:12:41 +02001118 VIM_CLEAR(cmdline_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001119}
1120
1121/*
1122 * Cleanup an expand structure after use.
1123 */
1124 void
1125ExpandCleanup(expand_T *xp)
1126{
1127 if (xp->xp_numfiles >= 0)
1128 {
1129 FreeWild(xp->xp_numfiles, xp->xp_files);
1130 xp->xp_numfiles = -1;
1131 }
zeertzjq28a23602023-09-29 19:58:35 +02001132 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001133}
1134
1135/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 * Display one line of completion matches. Multiple matches are displayed in
1137 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001138 * matches - list of completion match names
1139 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001140 * lines - number of output lines
1141 * linenr - line number of matches to display
1142 * maxlen - maximum number of characters in each line
1143 * showtail - display only the tail of the full path of a file name
1144 * dir_attr - highlight attribute to use for directory names
1145 */
1146 static void
1147showmatches_oneline(
1148 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001149 char_u **matches,
1150 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001151 int lines,
1152 int linenr,
1153 int maxlen,
1154 int showtail,
1155 int dir_attr)
1156{
1157 int i, j;
1158 int isdir;
1159 int lastlen;
1160 char_u *p;
1161
1162 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001163 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001164 {
1165 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1166 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001167 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1168 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001169 msg_advance(maxlen + 1);
1170 msg_puts((char *)p);
1171 msg_advance(maxlen + 3);
1172 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1173 break;
1174 }
1175 for (i = maxlen - lastlen; --i >= 0; )
1176 msg_putchar(' ');
1177 if (xp->xp_context == EXPAND_FILES
1178 || xp->xp_context == EXPAND_SHELLCMD
1179 || xp->xp_context == EXPAND_BUFFERS)
1180 {
1181 // highlight directories
1182 if (xp->xp_numfiles != -1)
1183 {
1184 char_u *halved_slash;
1185 char_u *exp_path;
1186 char_u *path;
1187
1188 // Expansion was done before and special characters
1189 // were escaped, need to halve backslashes. Also
1190 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 exp_path = expand_env_save_opt(matches[j], TRUE);
1192 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001193 halved_slash = backslash_halve_save(path);
1194 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001195 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001196 vim_free(exp_path);
1197 if (halved_slash != path)
1198 vim_free(halved_slash);
1199 }
1200 else
1201 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001202 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001203 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001204 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001205 else
1206 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001207 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001208 TRUE);
1209 p = NameBuff;
1210 }
1211 }
1212 else
1213 {
1214 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001215 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001216 }
1217 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1218 }
1219 if (msg_col > 0) // when not wrapped around
1220 {
1221 msg_clr_eos();
1222 msg_putchar('\n');
1223 }
1224 out_flush(); // show one line at a time
1225}
1226
1227/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001228 * Show all matches for completion on the command line.
1229 * Returns EXPAND_NOTHING when the character that triggered expansion should
1230 * be inserted like a normal character.
1231 */
1232 int
1233showmatches(expand_T *xp, int wildmenu UNUSED)
1234{
1235 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 int numMatches;
1237 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001238 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001239 int maxlen;
1240 int lines;
1241 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001242 int attr;
1243 int showtail;
1244
Girish Palya92f68e22025-04-21 11:12:41 +02001245 // Save cmdline before expansion
1246 if (ccline->cmdbuff != NULL)
1247 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
1248
Bram Moolenaar66b51422019-08-18 21:44:12 +02001249 if (xp->xp_numfiles == -1)
1250 {
1251 set_expand_context(xp);
1252 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001253 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254 showtail = expand_showtail(xp);
1255 if (i != EXPAND_OK)
1256 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 }
1258 else
1259 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001260 numMatches = xp->xp_numfiles;
1261 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 showtail = cmd_showtail;
1263 }
1264
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001265 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001266 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001268
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 if (!wildmenu)
1270 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271 msg_didany = FALSE; // lines_left will be set
1272 msg_start(); // prepare for paging
1273 msg_putchar('\n');
1274 out_flush();
1275 cmdline_row = msg_row;
1276 msg_didany = FALSE; // lines_left will be set again
1277 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001279
1280 if (got_int)
1281 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001283 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001284 else
1285 {
1286 // find the length of the longest file name
1287 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001288 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001289 {
1290 if (!showtail && (xp->xp_context == EXPAND_FILES
1291 || xp->xp_context == EXPAND_SHELLCMD
1292 || xp->xp_context == EXPAND_BUFFERS))
1293 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001294 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001295 j = vim_strsize(NameBuff);
1296 }
1297 else
zeertzjqc51a3762022-12-10 10:22:29 +00001298 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001299 if (j > maxlen)
1300 maxlen = j;
1301 }
1302
1303 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001304 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001305 else
1306 {
1307 // compute the number of columns and lines for the listing
1308 maxlen += 2; // two spaces between file names
1309 columns = ((int)Columns + 2) / maxlen;
1310 if (columns < 1)
1311 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001312 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001313 }
1314
1315 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1316
1317 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1318 {
1319 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1320 msg_clr_eos();
1321 msg_advance(maxlen - 3);
1322 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1323 }
1324
1325 // list the files line by line
1326 for (i = 0; i < lines; ++i)
1327 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001328 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001329 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001330 if (got_int)
1331 {
1332 got_int = FALSE;
1333 break;
1334 }
1335 }
1336
1337 // we redraw the command below the lines that we have just listed
1338 // This is a bit tricky, but it saves a lot of screen updating.
1339 cmdline_row = msg_row; // will put it back later
1340 }
1341
1342 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001343 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001344
1345 return EXPAND_OK;
1346}
1347
1348/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001349 * gettail() version for showmatches() and win_redr_status_matches():
1350 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001351 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001352 static char_u *
1353showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001354{
1355 char_u *p;
1356 char_u *t = s;
1357 int had_sep = FALSE;
1358
1359 for (p = s; *p != NUL; )
1360 {
1361 if (vim_ispathsep(*p)
1362#ifdef BACKSLASH_IN_FILENAME
1363 && !rem_backslash(p)
1364#endif
1365 )
1366 had_sep = TRUE;
1367 else if (had_sep)
1368 {
1369 t = p;
1370 had_sep = FALSE;
1371 }
1372 MB_PTR_ADV(p);
1373 }
1374 return t;
1375}
1376
1377/*
1378 * Return TRUE if we only need to show the tail of completion matches.
1379 * When not completing file names or there is a wildcard in the path FALSE is
1380 * returned.
1381 */
1382 static int
1383expand_showtail(expand_T *xp)
1384{
1385 char_u *s;
1386 char_u *end;
1387
1388 // When not completing file names a "/" may mean something different.
1389 if (xp->xp_context != EXPAND_FILES
1390 && xp->xp_context != EXPAND_SHELLCMD
1391 && xp->xp_context != EXPAND_DIRECTORIES)
1392 return FALSE;
1393
1394 end = gettail(xp->xp_pattern);
1395 if (end == xp->xp_pattern) // there is no path separator
1396 return FALSE;
1397
1398 for (s = xp->xp_pattern; s < end; s++)
1399 {
1400 // Skip escaped wildcards. Only when the backslash is not a path
1401 // separator, on DOS the '*' "path\*\file" must not be skipped.
1402 if (rem_backslash(s))
1403 ++s;
1404 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1405 return FALSE;
1406 }
1407 return TRUE;
1408}
1409
1410/*
1411 * Prepare a string for expansion.
1412 * When expanding file names: The string will be used with expand_wildcards().
1413 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1414 * When expanding other names: The string will be used with regcomp(). Copy
1415 * the name into allocated memory and prepend "^".
1416 */
1417 char_u *
1418addstar(
1419 char_u *fname,
1420 int len,
1421 int context) // EXPAND_FILES etc.
1422{
1423 char_u *retval;
1424 int i, j;
1425 int new_len;
1426 char_u *tail;
1427 int ends_in_star;
1428
1429 if (context != EXPAND_FILES
1430 && context != EXPAND_FILES_IN_PATH
1431 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001432 && context != EXPAND_DIRECTORIES
1433 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001434 {
1435 // Matching will be done internally (on something other than files).
1436 // So we convert the file-matching-type wildcards into our kind for
1437 // use with vim_regcomp(). First work out how long it will be:
1438
1439 // For help tags the translation is done in find_help_tags().
1440 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001441 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001442 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001443 || context == EXPAND_COLORS
1444 || context == EXPAND_COMPILER
1445 || context == EXPAND_OWNSYNTAX
1446 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001447 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001448 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001449 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001450 || ((context == EXPAND_TAGS_LISTFILES
1451 || context == EXPAND_TAGS)
1452 && fname[0] == '/'))
1453 retval = vim_strnsave(fname, len);
1454 else
1455 {
1456 new_len = len + 2; // +2 for '^' at start, NUL at end
1457 for (i = 0; i < len; i++)
1458 {
1459 if (fname[i] == '*' || fname[i] == '~')
1460 new_len++; // '*' needs to be replaced by ".*"
1461 // '~' needs to be replaced by "\~"
1462
1463 // Buffer names are like file names. "." should be literal
1464 if (context == EXPAND_BUFFERS && fname[i] == '.')
1465 new_len++; // "." becomes "\."
1466
1467 // Custom expansion takes care of special things, match
1468 // backslashes literally (perhaps also for other types?)
1469 if ((context == EXPAND_USER_DEFINED
1470 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1471 new_len++; // '\' becomes "\\"
1472 }
1473 retval = alloc(new_len);
1474 if (retval != NULL)
1475 {
1476 retval[0] = '^';
1477 j = 1;
1478 for (i = 0; i < len; i++, j++)
1479 {
1480 // Skip backslash. But why? At least keep it for custom
1481 // expansion.
1482 if (context != EXPAND_USER_DEFINED
1483 && context != EXPAND_USER_LIST
1484 && fname[i] == '\\'
1485 && ++i == len)
1486 break;
1487
1488 switch (fname[i])
1489 {
1490 case '*': retval[j++] = '.';
1491 break;
1492 case '~': retval[j++] = '\\';
1493 break;
1494 case '?': retval[j] = '.';
1495 continue;
1496 case '.': if (context == EXPAND_BUFFERS)
1497 retval[j++] = '\\';
1498 break;
1499 case '\\': if (context == EXPAND_USER_DEFINED
1500 || context == EXPAND_USER_LIST)
1501 retval[j++] = '\\';
1502 break;
1503 }
1504 retval[j] = fname[i];
1505 }
1506 retval[j] = NUL;
1507 }
1508 }
1509 }
1510 else
1511 {
1512 retval = alloc(len + 4);
1513 if (retval != NULL)
1514 {
1515 vim_strncpy(retval, fname, len);
1516
1517 // Don't add a star to *, ~, ~user, $var or `cmd`.
1518 // * would become **, which walks the whole tree.
1519 // ~ would be at the start of the file name, but not the tail.
1520 // $ could be anywhere in the tail.
1521 // ` could be anywhere in the file name.
1522 // When the name ends in '$' don't add a star, remove the '$'.
1523 tail = gettail(retval);
1524 ends_in_star = (len > 0 && retval[len - 1] == '*');
1525#ifndef BACKSLASH_IN_FILENAME
1526 for (i = len - 2; i >= 0; --i)
1527 {
1528 if (retval[i] != '\\')
1529 break;
1530 ends_in_star = !ends_in_star;
1531 }
1532#endif
1533 if ((*retval != '~' || tail != retval)
1534 && !ends_in_star
1535 && vim_strchr(tail, '$') == NULL
1536 && vim_strchr(retval, '`') == NULL)
1537 retval[len++] = '*';
1538 else if (len > 0 && retval[len - 1] == '$')
1539 --len;
1540 retval[len] = NUL;
1541 }
1542 }
1543 return retval;
1544}
1545
1546/*
1547 * Must parse the command line so far to work out what context we are in.
1548 * Completion can then be done based on that context.
1549 * This routine sets the variables:
1550 * xp->xp_pattern The start of the pattern to be expanded within
1551 * the command line (ends at the cursor).
1552 * xp->xp_context The type of thing to expand. Will be one of:
1553 *
1554 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1555 * the command line, like an unknown command. Caller
1556 * should beep.
1557 * EXPAND_NOTHING Unrecognised context for completion, use char like
1558 * a normal char, rather than for completion. eg
1559 * :s/^I/
1560 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1561 * it.
1562 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1563 * EXPAND_FILES After command with EX_XFILE set, or after setting
1564 * with P_EXPAND set. eg :e ^I, :w>>^I
1565 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001566 * when we know only directories are of interest.
1567 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001568 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1569 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1570 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1571 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1572 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1573 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1574 * EXPAND_EVENTS Complete event names
1575 * EXPAND_SYNTAX Complete :syntax command arguments
1576 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1577 * EXPAND_AUGROUP Complete autocommand group names
1578 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1579 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1580 * eg :unmap a^I , :cunab x^I
1581 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1582 * eg :call sub^I
1583 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1584 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1585 * names in expressions, eg :while s^I
1586 * EXPAND_ENV_VARS Complete environment variable names
1587 * EXPAND_USER Complete user names
1588 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001589 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001590set_expand_context(expand_T *xp)
1591{
1592 cmdline_info_T *ccline = get_cmdline_info();
1593
1594 // only expansion for ':', '>' and '=' command-lines
1595 if (ccline->cmdfirstc != ':'
1596#ifdef FEAT_EVAL
1597 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1598 && !ccline->input_fn
1599#endif
1600 )
1601 {
1602 xp->xp_context = EXPAND_NOTHING;
1603 return;
1604 }
1605 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1606}
1607
Bram Moolenaard0190392019-08-23 21:17:35 +02001608/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001609 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1610 * For user defined commands, the completion context is set in 'xp' and the
1611 * completion flags in 'complp'.
1612 *
1613 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001614 */
1615 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001616set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001617{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001618 char_u *p = NULL;
1619 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001620 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001621
1622 // Isolate the command and search for it in the command table.
1623 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001624 // - the 'k' command can directly be followed by any character, but do
1625 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1626 // find matches anywhere in the command name, do this only for command
1627 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001628 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001629 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001630 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001631 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001632 p = cmd + 1;
1633 }
1634 else
1635 {
1636 p = cmd;
1637 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1638 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001639 // A user command may contain digits.
1640 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1641 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001642 while (ASCII_ISALNUM(*p) || *p == '*')
1643 ++p;
1644 // for python 3.x: ":py3*" commands completion
1645 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1646 {
1647 ++p;
1648 while (ASCII_ISALPHA(*p) || *p == '*')
1649 ++p;
1650 }
1651 // check for non-alpha command
1652 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1653 ++p;
1654 len = (int)(p - cmd);
1655
1656 if (len == 0)
1657 {
1658 xp->xp_context = EXPAND_UNSUCCESSFUL;
1659 return NULL;
1660 }
1661
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001662 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001663
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001664 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001665 // Also when doing fuzzy expansion for non-shell commands, support
1666 // alphanumeric characters.
1667 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1668 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001669 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1670 ++p;
1671 }
1672
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001673 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 // character, complete the command name.
1675 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1676 return NULL;
1677
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001679 {
1680 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1681 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001683 p = cmd + 1;
1684 }
1685 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1686 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001687 eap->cmd = cmd;
1688 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001689 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001691 }
1692 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001693 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001694 {
1695 // Not still touching the command and it was an illegal one
1696 xp->xp_context = EXPAND_UNSUCCESSFUL;
1697 return NULL;
1698 }
1699
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001700 return p;
1701}
Bram Moolenaard0190392019-08-23 21:17:35 +02001702
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703/*
1704 * Set the completion context for a command argument with wild card characters.
1705 */
1706 static void
1707set_context_for_wildcard_arg(
1708 exarg_T *eap,
1709 char_u *arg,
1710 int usefilter,
1711 expand_T *xp,
1712 int *complp)
1713{
1714 char_u *p;
1715 int c;
1716 int in_quote = FALSE;
1717 char_u *bow = NULL; // Beginning of word
1718 int len = 0;
1719
1720 // Allow spaces within back-quotes to count as part of the argument
1721 // being expanded.
1722 xp->xp_pattern = skipwhite(arg);
1723 p = xp->xp_pattern;
1724 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001725 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001726 if (has_mbyte)
1727 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001728 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001729 c = *p;
1730 if (c == '\\' && p[1] != NUL)
1731 ++p;
1732 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001734 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001735 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001736 xp->xp_pattern = p;
1737 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 in_quote = !in_quote;
1740 }
1741 // An argument can contain just about everything, except
1742 // characters that end the command and white space.
1743 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001744#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001745 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001746#endif
1747 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001748 {
1749 len = 0; // avoid getting stuck when space is in 'isfname'
1750 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001751 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 if (has_mbyte)
1753 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001754 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 c = *p;
1756 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001757 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001758 if (has_mbyte)
1759 len = (*mb_ptr2len)(p);
1760 else
1761 len = 1;
1762 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001763 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001764 if (in_quote)
1765 bow = p;
1766 else
1767 xp->xp_pattern = p;
1768 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001769 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001770 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001771 }
1772
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001773 // If we are still inside the quotes, and we passed a space, just
1774 // expand from there.
1775 if (bow != NULL && in_quote)
1776 xp->xp_pattern = bow;
1777 xp->xp_context = EXPAND_FILES;
1778
1779 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001780 if (usefilter
1781 || (eap != NULL
1782 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1783 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001784 {
1785#ifndef BACKSLASH_IN_FILENAME
1786 xp->xp_shell = TRUE;
1787#endif
1788 // When still after the command name expand executables.
1789 if (xp->xp_pattern == skipwhite(arg))
1790 xp->xp_context = EXPAND_SHELLCMD;
1791 }
1792
1793 // Check for environment variable.
1794 if (*xp->xp_pattern == '$')
1795 {
1796 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1797 if (!vim_isIDc(*p))
1798 break;
1799 if (*p == NUL)
1800 {
1801 xp->xp_context = EXPAND_ENV_VARS;
1802 ++xp->xp_pattern;
1803 // Avoid that the assignment uses EXPAND_FILES again.
1804 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1805 *complp = EXPAND_ENV_VARS;
1806 }
1807 }
1808 // Check for user names.
1809 if (*xp->xp_pattern == '~')
1810 {
1811 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1812 ;
1813 // Complete ~user only if it partially matches a user name.
1814 // A full match ~user<Tab> will be replaced by user's home
1815 // directory i.e. something like ~user<Tab> -> /home/user/
1816 if (*p == NUL && p > xp->xp_pattern + 1
1817 && match_user(xp->xp_pattern + 1) >= 1)
1818 {
1819 xp->xp_context = EXPAND_USER;
1820 ++xp->xp_pattern;
1821 }
1822 }
1823}
1824
1825/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001826 * Set the completion context for the "++opt=arg" argument. Always returns
1827 * NULL.
1828 */
1829 static char_u *
1830set_context_in_argopt(expand_T *xp, char_u *arg)
1831{
1832 char_u *p;
1833
1834 p = vim_strchr(arg, '=');
1835 if (p == NULL)
1836 xp->xp_pattern = arg;
1837 else
1838 xp->xp_pattern = p + 1;
1839
1840 xp->xp_context = EXPAND_ARGOPT;
1841 return NULL;
1842}
1843
1844#ifdef FEAT_TERMINAL
1845/*
1846 * Set the completion context for :terminal's [options]. Always returns NULL.
1847 */
1848 static char_u *
1849set_context_in_terminalopt(expand_T *xp, char_u *arg)
1850{
1851 char_u *p;
1852
1853 p = vim_strchr(arg, '=');
1854 if (p == NULL)
1855 xp->xp_pattern = arg;
1856 else
1857 xp->xp_pattern = p + 1;
1858
1859 xp->xp_context = EXPAND_TERMINALOPT;
1860 return NULL;
1861}
1862#endif
1863
1864/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001865 * Set the completion context for the :filter command. Returns a pointer to the
1866 * next command after the :filter command.
1867 */
1868 static char_u *
1869set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1870{
1871 if (*arg != NUL)
1872 arg = skip_vimgrep_pat(arg, NULL, NULL);
1873 if (arg == NULL || *arg == NUL)
1874 {
1875 xp->xp_context = EXPAND_NOTHING;
1876 return NULL;
1877 }
1878 return skipwhite(arg);
1879}
1880
1881#ifdef FEAT_SEARCH_EXTRA
1882/*
1883 * Set the completion context for the :match command. Returns a pointer to the
1884 * next command after the :match command.
1885 */
1886 static char_u *
1887set_context_in_match_cmd(expand_T *xp, char_u *arg)
1888{
1889 if (*arg == NUL || !ends_excmd(*arg))
1890 {
1891 // also complete "None"
1892 set_context_in_echohl_cmd(xp, arg);
1893 arg = skipwhite(skiptowhite(arg));
1894 if (*arg != NUL)
1895 {
1896 xp->xp_context = EXPAND_NOTHING;
1897 arg = skip_regexp(arg + 1, *arg, magic_isset());
1898 }
1899 }
1900 return find_nextcmd(arg);
1901}
1902#endif
1903
1904/*
1905 * Returns a pointer to the next command after a :global or a :v command.
1906 * Returns NULL if there is no next command.
1907 */
1908 static char_u *
1909find_cmd_after_global_cmd(char_u *arg)
1910{
1911 int delim;
1912
1913 delim = *arg; // get the delimiter
1914 if (delim)
1915 ++arg; // skip delimiter if there is one
1916
1917 while (arg[0] != NUL && arg[0] != delim)
1918 {
1919 if (arg[0] == '\\' && arg[1] != NUL)
1920 ++arg;
1921 ++arg;
1922 }
1923 if (arg[0] != NUL)
1924 return arg + 1;
1925
1926 return NULL;
1927}
1928
1929/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001930 * Returns a pointer to the next command after a :substitute or a :& command.
1931 * Returns NULL if there is no next command.
1932 */
1933 static char_u *
1934find_cmd_after_substitute_cmd(char_u *arg)
1935{
1936 int delim;
1937
1938 delim = *arg;
1939 if (delim)
1940 {
1941 // skip "from" part
1942 ++arg;
1943 arg = skip_regexp(arg, delim, magic_isset());
1944
1945 if (arg[0] != NUL && arg[0] == delim)
1946 {
1947 // skip "to" part
1948 ++arg;
1949 while (arg[0] != NUL && arg[0] != delim)
1950 {
1951 if (arg[0] == '\\' && arg[1] != NUL)
1952 ++arg;
1953 ++arg;
1954 }
1955 if (arg[0] != NUL) // skip delimiter
1956 ++arg;
1957 }
1958 }
1959 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1960 ++arg;
1961 if (arg[0] != NUL)
1962 return arg;
1963
1964 return NULL;
1965}
1966
1967/*
1968 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1969 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1970 * Returns NULL if there is no next command.
1971 */
1972 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001973find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001974{
1975 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001976 if (*arg != '/')
1977 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001978
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001979 // Match regexp, not just whole words
1980 for (++arg; *arg && *arg != '/'; arg++)
1981 if (*arg == '\\' && arg[1] != NUL)
1982 arg++;
1983 if (*arg)
1984 {
1985 arg = skipwhite(arg + 1);
1986
1987 // Check for trailing illegal characters
1988 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1989 xp->xp_context = EXPAND_NOTHING;
1990 else
1991 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001992 }
1993
1994 return NULL;
1995}
1996
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001997#ifdef FEAT_EVAL
1998/*
1999 * Set the completion context for the :unlet command. Always returns NULL.
2000 */
2001 static char_u *
2002set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2003{
2004 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2005 arg = xp->xp_pattern + 1;
2006
2007 xp->xp_context = EXPAND_USER_VARS;
2008 xp->xp_pattern = arg;
2009
2010 if (*xp->xp_pattern == '$')
2011 {
2012 xp->xp_context = EXPAND_ENV_VARS;
2013 ++xp->xp_pattern;
2014 }
2015
2016 return NULL;
2017}
2018#endif
2019
2020#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2021/*
2022 * Set the completion context for the :language command. Always returns NULL.
2023 */
2024 static char_u *
2025set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2026{
2027 char_u *p;
2028
2029 p = skiptowhite(arg);
2030 if (*p == NUL)
2031 {
2032 xp->xp_context = EXPAND_LANGUAGE;
2033 xp->xp_pattern = arg;
2034 }
2035 else
2036 {
2037 if ( STRNCMP(arg, "messages", p - arg) == 0
2038 || STRNCMP(arg, "ctype", p - arg) == 0
2039 || STRNCMP(arg, "time", p - arg) == 0
2040 || STRNCMP(arg, "collate", p - arg) == 0)
2041 {
2042 xp->xp_context = EXPAND_LOCALES;
2043 xp->xp_pattern = skipwhite(p);
2044 }
2045 else
2046 xp->xp_context = EXPAND_NOTHING;
2047 }
2048
2049 return NULL;
2050}
2051#endif
2052
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002053#ifdef FEAT_EVAL
2054static enum
2055{
2056 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002057 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2058 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002059} breakpt_expand_what;
2060
2061/*
2062 * Set the completion context for the :breakadd command. Always returns NULL.
2063 */
2064 static char_u *
2065set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2066{
2067 char_u *p;
2068 char_u *subcmd_start;
2069
2070 xp->xp_context = EXPAND_BREAKPOINT;
2071 xp->xp_pattern = arg;
2072
2073 if (cmdidx == CMD_breakadd)
2074 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002075 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002076 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002077 else
2078 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002079
2080 p = skipwhite(arg);
2081 if (*p == NUL)
2082 return NULL;
2083 subcmd_start = p;
2084
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002085 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002086 {
2087 // :breakadd file [lnum] <filename>
2088 // :breakadd func [lnum] <funcname>
2089 p += 4;
2090 p = skipwhite(p);
2091
2092 // skip line number (if specified)
2093 if (VIM_ISDIGIT(*p))
2094 {
2095 p = skipdigits(p);
2096 if (*p != ' ')
2097 {
2098 xp->xp_context = EXPAND_NOTHING;
2099 return NULL;
2100 }
2101 p = skipwhite(p);
2102 }
2103 if (STRNCMP("file", subcmd_start, 4) == 0)
2104 xp->xp_context = EXPAND_FILES;
2105 else
2106 xp->xp_context = EXPAND_USER_FUNC;
2107 xp->xp_pattern = p;
2108 }
2109 else if (STRNCMP("expr ", p, 5) == 0)
2110 {
2111 // :breakadd expr <expression>
2112 xp->xp_context = EXPAND_EXPRESSION;
2113 xp->xp_pattern = skipwhite(p + 5);
2114 }
2115
2116 return NULL;
2117}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002118
2119 static char_u *
2120set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2121{
2122 char_u *p;
2123
2124 xp->xp_context = EXPAND_NOTHING;
2125 xp->xp_pattern = NULL;
2126
2127 p = skipwhite(arg);
2128 if (VIM_ISDIGIT(*p))
2129 return NULL;
2130
2131 xp->xp_context = EXPAND_SCRIPTNAMES;
2132 xp->xp_pattern = p;
2133
2134 return NULL;
2135}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002136#endif
2137
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002138/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002139 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2140 * The argument to the command is 'arg' and the argument flags is 'argt'.
2141 * For user-defined commands and for environment variables, 'compl' has the
2142 * completion type.
2143 * Returns a pointer to the next command. Returns NULL if there is no next
2144 * command.
2145 */
2146 static char_u *
2147set_context_by_cmdname(
2148 char_u *cmd,
2149 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002150 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002151 char_u *arg,
2152 long argt,
2153 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002154 int forceit)
2155{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002156 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002157 {
2158 case CMD_find:
2159 case CMD_sfind:
2160 case CMD_tabfind:
2161 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002162 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002163 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002164 break;
2165 case CMD_cd:
2166 case CMD_chdir:
2167 case CMD_tcd:
2168 case CMD_tchdir:
2169 case CMD_lcd:
2170 case CMD_lchdir:
2171 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002172 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002173 break;
2174 case CMD_help:
2175 xp->xp_context = EXPAND_HELP;
2176 xp->xp_pattern = arg;
2177 break;
2178
2179 // Command modifiers: return the argument.
2180 // Also for commands with an argument that is a command.
2181 case CMD_aboveleft:
2182 case CMD_argdo:
2183 case CMD_belowright:
2184 case CMD_botright:
2185 case CMD_browse:
2186 case CMD_bufdo:
2187 case CMD_cdo:
2188 case CMD_cfdo:
2189 case CMD_confirm:
2190 case CMD_debug:
2191 case CMD_folddoclosed:
2192 case CMD_folddoopen:
2193 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002194 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002195 case CMD_keepalt:
2196 case CMD_keepjumps:
2197 case CMD_keepmarks:
2198 case CMD_keeppatterns:
2199 case CMD_ldo:
2200 case CMD_leftabove:
2201 case CMD_lfdo:
2202 case CMD_lockmarks:
2203 case CMD_noautocmd:
2204 case CMD_noswapfile:
2205 case CMD_rightbelow:
2206 case CMD_sandbox:
2207 case CMD_silent:
2208 case CMD_tab:
2209 case CMD_tabdo:
2210 case CMD_topleft:
2211 case CMD_verbose:
2212 case CMD_vertical:
2213 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002214 case CMD_vim9cmd:
2215 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002216 return arg;
2217
2218 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002219 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002220
2221#ifdef FEAT_SEARCH_EXTRA
2222 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002223 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002224#endif
2225
2226 // All completion for the +cmdline_compl feature goes here.
2227
2228 case CMD_command:
2229 return set_context_in_user_cmd(xp, arg);
2230
2231 case CMD_delcommand:
2232 xp->xp_context = EXPAND_USER_COMMANDS;
2233 xp->xp_pattern = arg;
2234 break;
2235
2236 case CMD_global:
2237 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002238 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002239 case CMD_and:
2240 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002241 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002242 case CMD_isearch:
2243 case CMD_dsearch:
2244 case CMD_ilist:
2245 case CMD_dlist:
2246 case CMD_ijump:
2247 case CMD_psearch:
2248 case CMD_djump:
2249 case CMD_isplit:
2250 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002251 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002252 case CMD_autocmd:
2253 return set_context_in_autocmd(xp, arg, FALSE);
2254 case CMD_doautocmd:
2255 case CMD_doautoall:
2256 return set_context_in_autocmd(xp, arg, TRUE);
2257 case CMD_set:
2258 set_context_in_set_cmd(xp, arg, 0);
2259 break;
2260 case CMD_setglobal:
2261 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2262 break;
2263 case CMD_setlocal:
2264 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2265 break;
2266 case CMD_tag:
2267 case CMD_stag:
2268 case CMD_ptag:
2269 case CMD_ltag:
2270 case CMD_tselect:
2271 case CMD_stselect:
2272 case CMD_ptselect:
2273 case CMD_tjump:
2274 case CMD_stjump:
2275 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002276 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002277 xp->xp_context = EXPAND_TAGS_LISTFILES;
2278 else
2279 xp->xp_context = EXPAND_TAGS;
2280 xp->xp_pattern = arg;
2281 break;
2282 case CMD_augroup:
2283 xp->xp_context = EXPAND_AUGROUP;
2284 xp->xp_pattern = arg;
2285 break;
2286#ifdef FEAT_SYN_HL
2287 case CMD_syntax:
2288 set_context_in_syntax_cmd(xp, arg);
2289 break;
2290#endif
2291#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002292 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002293 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002294 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002295 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002296 case CMD_if:
2297 case CMD_elseif:
2298 case CMD_while:
2299 case CMD_for:
2300 case CMD_echo:
2301 case CMD_echon:
2302 case CMD_execute:
2303 case CMD_echomsg:
2304 case CMD_echoerr:
2305 case CMD_call:
2306 case CMD_return:
2307 case CMD_cexpr:
2308 case CMD_caddexpr:
2309 case CMD_cgetexpr:
2310 case CMD_lexpr:
2311 case CMD_laddexpr:
2312 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002313 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002314 break;
2315
2316 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002317 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002318 case CMD_function:
2319 case CMD_delfunction:
2320 xp->xp_context = EXPAND_USER_FUNC;
2321 xp->xp_pattern = arg;
2322 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002323 case CMD_disassemble:
2324 set_context_in_disassemble_cmd(xp, arg);
2325 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002326
2327 case CMD_echohl:
2328 set_context_in_echohl_cmd(xp, arg);
2329 break;
2330#endif
2331 case CMD_highlight:
2332 set_context_in_highlight_cmd(xp, arg);
2333 break;
2334#ifdef FEAT_CSCOPE
2335 case CMD_cscope:
2336 case CMD_lcscope:
2337 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002338 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002339 break;
2340#endif
2341#ifdef FEAT_SIGNS
2342 case CMD_sign:
2343 set_context_in_sign_cmd(xp, arg);
2344 break;
2345#endif
2346 case CMD_bdelete:
2347 case CMD_bwipeout:
2348 case CMD_bunload:
2349 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2350 arg = xp->xp_pattern + 1;
2351 // FALLTHROUGH
2352 case CMD_buffer:
2353 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002354 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002355 case CMD_checktime:
2356 xp->xp_context = EXPAND_BUFFERS;
2357 xp->xp_pattern = arg;
2358 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002359#ifdef FEAT_DIFF
2360 case CMD_diffget:
2361 case CMD_diffput:
2362 // If current buffer is in diff mode, complete buffer names
2363 // which are in diff mode, and different than current buffer.
2364 xp->xp_context = EXPAND_DIFF_BUFFERS;
2365 xp->xp_pattern = arg;
2366 break;
2367#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002368 case CMD_USER:
2369 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002370 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2371 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002372
2373 case CMD_map: case CMD_noremap:
2374 case CMD_nmap: case CMD_nnoremap:
2375 case CMD_vmap: case CMD_vnoremap:
2376 case CMD_omap: case CMD_onoremap:
2377 case CMD_imap: case CMD_inoremap:
2378 case CMD_cmap: case CMD_cnoremap:
2379 case CMD_lmap: case CMD_lnoremap:
2380 case CMD_smap: case CMD_snoremap:
2381 case CMD_tmap: case CMD_tnoremap:
2382 case CMD_xmap: case CMD_xnoremap:
2383 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002384 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002385 case CMD_unmap:
2386 case CMD_nunmap:
2387 case CMD_vunmap:
2388 case CMD_ounmap:
2389 case CMD_iunmap:
2390 case CMD_cunmap:
2391 case CMD_lunmap:
2392 case CMD_sunmap:
2393 case CMD_tunmap:
2394 case CMD_xunmap:
2395 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002396 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002397 case CMD_mapclear:
2398 case CMD_nmapclear:
2399 case CMD_vmapclear:
2400 case CMD_omapclear:
2401 case CMD_imapclear:
2402 case CMD_cmapclear:
2403 case CMD_lmapclear:
2404 case CMD_smapclear:
2405 case CMD_tmapclear:
2406 case CMD_xmapclear:
2407 xp->xp_context = EXPAND_MAPCLEAR;
2408 xp->xp_pattern = arg;
2409 break;
2410
2411 case CMD_abbreviate: case CMD_noreabbrev:
2412 case CMD_cabbrev: case CMD_cnoreabbrev:
2413 case CMD_iabbrev: case CMD_inoreabbrev:
2414 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002415 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002416 case CMD_unabbreviate:
2417 case CMD_cunabbrev:
2418 case CMD_iunabbrev:
2419 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002420 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002421#ifdef FEAT_MENU
2422 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2423 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2424 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2425 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2426 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2427 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2428 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2429 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2430 case CMD_tmenu: case CMD_tunmenu:
2431 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2432 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2433#endif
2434
2435 case CMD_colorscheme:
2436 xp->xp_context = EXPAND_COLORS;
2437 xp->xp_pattern = arg;
2438 break;
2439
2440 case CMD_compiler:
2441 xp->xp_context = EXPAND_COMPILER;
2442 xp->xp_pattern = arg;
2443 break;
2444
2445 case CMD_ownsyntax:
2446 xp->xp_context = EXPAND_OWNSYNTAX;
2447 xp->xp_pattern = arg;
2448 break;
2449
2450 case CMD_setfiletype:
2451 xp->xp_context = EXPAND_FILETYPE;
2452 xp->xp_pattern = arg;
2453 break;
2454
2455 case CMD_packadd:
2456 xp->xp_context = EXPAND_PACKADD;
2457 xp->xp_pattern = arg;
2458 break;
2459
zeertzjqb0d45ec2023-01-25 15:04:22 +00002460 case CMD_runtime:
2461 set_context_in_runtime_cmd(xp, arg);
2462 break;
2463
Bram Moolenaard0190392019-08-23 21:17:35 +02002464#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2465 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002466 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002467#endif
2468#if defined(FEAT_PROFILE)
2469 case CMD_profile:
2470 set_context_in_profile_cmd(xp, arg);
2471 break;
2472#endif
2473 case CMD_behave:
2474 xp->xp_context = EXPAND_BEHAVE;
2475 xp->xp_pattern = arg;
2476 break;
2477
2478 case CMD_messages:
2479 xp->xp_context = EXPAND_MESSAGES;
2480 xp->xp_pattern = arg;
2481 break;
2482
2483 case CMD_history:
2484 xp->xp_context = EXPAND_HISTORY;
2485 xp->xp_pattern = arg;
2486 break;
2487#if defined(FEAT_PROFILE)
2488 case CMD_syntime:
2489 xp->xp_context = EXPAND_SYNTIME;
2490 xp->xp_pattern = arg;
2491 break;
2492#endif
2493
2494 case CMD_argdelete:
2495 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2496 arg = xp->xp_pattern + 1;
2497 xp->xp_context = EXPAND_ARGLIST;
2498 xp->xp_pattern = arg;
2499 break;
2500
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002501#ifdef FEAT_EVAL
2502 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002503 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002504 case CMD_breakdel:
2505 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002506
2507 case CMD_scriptnames:
2508 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002509#endif
2510
Bram Moolenaard0190392019-08-23 21:17:35 +02002511 default:
2512 break;
2513 }
2514 return NULL;
2515}
2516
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002517/*
2518 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2519 * we don't need/want deleted. Maybe this could be done better if we didn't
2520 * repeat all this stuff. The only problem is that they may not stay
2521 * perfectly compatible with each other, but then the command line syntax
2522 * probably won't change that much -- webb.
2523 */
2524 static char_u *
2525set_one_cmd_context(
2526 expand_T *xp,
2527 char_u *buff) // buffer for command string
2528{
2529 char_u *p;
2530 char_u *cmd, *arg;
2531 int len = 0;
2532 exarg_T ea;
2533 int compl = EXPAND_NOTHING;
2534 int forceit = FALSE;
2535 int usefilter = FALSE; // filter instead of file name
2536
2537 ExpandInit(xp);
2538 xp->xp_pattern = buff;
2539 xp->xp_line = buff;
2540 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2541 ea.argt = 0;
2542
2543 // 1. skip comment lines and leading space, colons or bars
2544 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2545 ;
2546 xp->xp_pattern = cmd;
2547
2548 if (*cmd == NUL)
2549 return NULL;
2550 if (*cmd == '"') // ignore comment lines
2551 {
2552 xp->xp_context = EXPAND_NOTHING;
2553 return NULL;
2554 }
2555
2556 // 3. Skip over the range to find the command.
2557 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2558 xp->xp_pattern = cmd;
2559 if (*cmd == NUL)
2560 return NULL;
2561 if (*cmd == '"')
2562 {
2563 xp->xp_context = EXPAND_NOTHING;
2564 return NULL;
2565 }
2566
2567 if (*cmd == '|' || *cmd == '\n')
2568 return cmd + 1; // There's another command
2569
2570 // Get the command index.
2571 p = set_cmd_index(cmd, &ea, xp, &compl);
2572 if (p == NULL)
2573 return NULL;
2574
2575 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2576
2577 if (*p == '!') // forced commands
2578 {
2579 forceit = TRUE;
2580 ++p;
2581 }
2582
2583 // 6. parse arguments
2584 if (!IS_USER_CMDIDX(ea.cmdidx))
2585 ea.argt = excmd_get_argt(ea.cmdidx);
2586
2587 arg = skipwhite(p);
2588
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002589 // Does command allow "++argopt" argument?
2590 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002591 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002592 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2593 {
2594 p = arg + 2;
2595 while (*p && !vim_isspace(*p))
2596 MB_PTR_ADV(p);
2597
2598 // Still touching the command after "++"?
2599 if (*p == NUL)
2600 {
2601 if (ea.argt & EX_ARGOPT)
2602 return set_context_in_argopt(xp, arg + 2);
2603#ifdef FEAT_TERMINAL
2604 if (ea.cmdidx == CMD_terminal)
2605 return set_context_in_terminalopt(xp, arg + 2);
2606#endif
2607 }
2608
2609 arg = skipwhite(p);
2610 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002611 }
2612
2613 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2614 {
2615 if (*arg == '>') // append
2616 {
2617 if (*++arg == '>')
2618 ++arg;
2619 arg = skipwhite(arg);
2620 }
2621 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2622 {
2623 ++arg;
2624 usefilter = TRUE;
2625 }
2626 }
2627
2628 if (ea.cmdidx == CMD_read)
2629 {
2630 usefilter = forceit; // :r! filter if forced
2631 if (*arg == '!') // :r !filter
2632 {
2633 ++arg;
2634 usefilter = TRUE;
2635 }
2636 }
2637
2638 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2639 {
2640 while (*arg == *cmd) // allow any number of '>' or '<'
2641 ++arg;
2642 arg = skipwhite(arg);
2643 }
2644
2645 // Does command allow "+command"?
2646 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2647 {
2648 // Check if we're in the +command
2649 p = arg + 1;
2650 arg = skip_cmd_arg(arg, FALSE);
2651
2652 // Still touching the command after '+'?
2653 if (*arg == NUL)
2654 return p;
2655
2656 // Skip space(s) after +command to get to the real argument
2657 arg = skipwhite(arg);
2658 }
2659
2660
2661 // Check for '|' to separate commands and '"' to start comments.
2662 // Don't do this for ":read !cmd" and ":write !cmd".
2663 if ((ea.argt & EX_TRLBAR) && !usefilter)
2664 {
2665 p = arg;
2666 // ":redir @" is not the start of a comment
2667 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2668 p += 2;
2669 while (*p)
2670 {
2671 if (*p == Ctrl_V)
2672 {
2673 if (p[1] != NUL)
2674 ++p;
2675 }
2676 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2677 || *p == '|' || *p == '\n')
2678 {
2679 if (*(p - 1) != '\\')
2680 {
2681 if (*p == '|' || *p == '\n')
2682 return p + 1;
2683 return NULL; // It's a comment
2684 }
2685 }
2686 MB_PTR_ADV(p);
2687 }
2688 }
2689
2690 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2691 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2692 // no arguments allowed but there is something
2693 return NULL;
2694
2695 // Find start of last argument (argument just before cursor):
2696 p = buff;
2697 xp->xp_pattern = p;
2698 len = (int)STRLEN(buff);
2699 while (*p && p < buff + len)
2700 {
2701 if (*p == ' ' || *p == TAB)
2702 {
2703 // argument starts after a space
2704 xp->xp_pattern = ++p;
2705 }
2706 else
2707 {
2708 if (*p == '\\' && *(p + 1) != NUL)
2709 ++p; // skip over escaped character
2710 MB_PTR_ADV(p);
2711 }
2712 }
2713
2714 if (ea.argt & EX_XFILE)
2715 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2716
2717 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002718 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002719 forceit);
2720}
2721
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002722/*
2723 * Set the completion context in 'xp' for command 'str'
2724 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 void
2726set_cmd_context(
2727 expand_T *xp,
2728 char_u *str, // start of command line
2729 int len, // length of command line (excl. NUL)
2730 int col, // position of cursor
2731 int use_ccline UNUSED) // use ccline for info
2732{
2733#ifdef FEAT_EVAL
2734 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002735 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736#endif
2737 int old_char = NUL;
2738 char_u *nextcomm;
2739
2740 // Avoid a UMR warning from Purify, only save the character if it has been
2741 // written before.
2742 if (col < len)
2743 old_char = str[col];
2744 str[col] = NUL;
2745 nextcomm = str;
2746
2747#ifdef FEAT_EVAL
2748 if (use_ccline && ccline->cmdfirstc == '=')
2749 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002750 // pass CMD_SIZE because there is no real command
2751 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002752 }
2753 else if (use_ccline && ccline->input_fn)
2754 {
2755 xp->xp_context = ccline->xp_context;
2756 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002757 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002758 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2759 {
2760 context = xp->xp_context;
2761 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2762 &context);
2763 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002764 }
2765 else
2766#endif
2767 while (nextcomm != NULL)
2768 nextcomm = set_one_cmd_context(xp, nextcomm);
2769
2770 // Store the string here so that call_user_expand_func() can get to them
2771 // easily.
2772 xp->xp_line = str;
2773 xp->xp_col = col;
2774
2775 str[col] = old_char;
2776}
2777
2778/*
2779 * Expand the command line "str" from context "xp".
2780 * "xp" must have been set by set_cmd_context().
2781 * xp->xp_pattern points into "str", to where the text that is to be expanded
2782 * starts.
2783 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2784 * cursor.
2785 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2786 * key that triggered expansion literally.
2787 * Returns EXPAND_OK otherwise.
2788 */
2789 int
2790expand_cmdline(
2791 expand_T *xp,
2792 char_u *str, // start of command line
2793 int col, // position of cursor
2794 int *matchcount, // return: nr of matches
2795 char_u ***matches) // return: array of pointers to matches
2796{
2797 char_u *file_str = NULL;
2798 int options = WILD_ADD_SLASH|WILD_SILENT;
2799
2800 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2801 {
2802 beep_flush();
2803 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2804 }
2805 if (xp->xp_context == EXPAND_NOTHING)
2806 {
2807 // Caller can use the character as a normal char instead
2808 return EXPAND_NOTHING;
2809 }
2810
2811 // add star to file name, or convert to regexp if not exp. files.
2812 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002813 if (cmdline_fuzzy_completion_supported(xp))
2814 // If fuzzy matching, don't modify the search string
2815 file_str = vim_strsave(xp->xp_pattern);
2816 else
2817 {
2818 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2819 if (file_str == NULL)
2820 return EXPAND_UNSUCCESSFUL;
2821 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002822
2823 if (p_wic)
2824 options += WILD_ICASE;
2825
2826 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002827 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002828 {
2829 *matchcount = 0;
2830 *matches = NULL;
2831 }
2832 vim_free(file_str);
2833
2834 return EXPAND_OK;
2835}
2836
Bram Moolenaar66b51422019-08-18 21:44:12 +02002837/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002838 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002839 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002840 */
2841 static int
2842expand_files_and_dirs(
2843 expand_T *xp,
2844 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002845 char_u ***matches,
2846 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002847 int flags,
2848 int options)
2849{
2850 int free_pat = FALSE;
2851 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002852 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002853
2854 // for ":set path=" and ":set tags=" halve backslashes for escaped
2855 // space
2856 if (xp->xp_backslash != XP_BS_NONE)
2857 {
2858 free_pat = TRUE;
2859 pat = vim_strsave(pat);
2860 for (i = 0; pat[i]; ++i)
2861 if (pat[i] == '\\')
2862 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002863 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002864 && pat[i + 1] == '\\'
2865 && pat[i + 2] == '\\'
2866 && pat[i + 3] == ' ')
2867 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002868 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002869 && pat[i + 1] == ' ')
2870 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002871 else if ((xp->xp_backslash & XP_BS_COMMA)
2872 && pat[i + 1] == '\\'
2873 && pat[i + 2] == ',')
2874 STRMOVE(pat + i, pat + i + 2);
2875#ifdef BACKSLASH_IN_FILENAME
2876 else if ((xp->xp_backslash & XP_BS_COMMA)
2877 && pat[i + 1] == ',')
2878 STRMOVE(pat + i, pat + i + 1);
2879#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002880 }
2881 }
2882
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002883 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002884 {
2885#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002886 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002887#endif
2888 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002889 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002890 {
2891 if (xp->xp_context == EXPAND_FILES)
2892 flags |= EW_FILE;
2893 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2894 flags |= (EW_FILE | EW_PATH);
2895 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2896 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2897 else
2898 flags = (flags | EW_DIR) & ~EW_FILE;
2899 if (options & WILD_ICASE)
2900 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002901
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002902 // Expand wildcards, supporting %:h and the like.
2903 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2904 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002905 if (free_pat)
2906 vim_free(pat);
2907#ifdef BACKSLASH_IN_FILENAME
2908 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2909 {
2910 int j;
2911
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002912 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002913 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002914 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002915
2916 while (*ptr != NUL)
2917 {
2918 if (p_csl[0] == 's' && *ptr == '\\')
2919 *ptr = '/';
2920 else if (p_csl[0] == 'b' && *ptr == '/')
2921 *ptr = '\\';
2922 ptr += (*mb_ptr2len)(ptr);
2923 }
2924 }
2925 }
2926#endif
2927 return ret;
2928}
2929
2930/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002931 * Function given to ExpandGeneric() to obtain the possible arguments of the
2932 * ":behave {mswin,xterm}" command.
2933 */
2934 static char_u *
2935get_behave_arg(expand_T *xp UNUSED, int idx)
2936{
2937 if (idx == 0)
2938 return (char_u *)"mswin";
2939 if (idx == 1)
2940 return (char_u *)"xterm";
2941 return NULL;
2942}
2943
K.Takata161b6ac2022-11-14 15:31:07 +00002944#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002945/*
2946 * Function given to ExpandGeneric() to obtain the possible arguments of the
2947 * ":breakadd {expr, file, func, here}" command.
2948 * ":breakdel {func, file, here}" command.
2949 */
2950 static char_u *
2951get_breakadd_arg(expand_T *xp UNUSED, int idx)
2952{
2953 char *opts[] = {"expr", "file", "func", "here"};
2954
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002955 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002956 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002957 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002958 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2959 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002960 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2961 {
2962 // breakdel {func, file, here}
2963 if (idx <= 2)
2964 return (char_u *)opts[idx + 1];
2965 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002966 else
2967 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002968 // profdel {func, file}
2969 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002970 return (char_u *)opts[idx + 1];
2971 }
2972 }
2973 return NULL;
2974}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002975
2976/*
2977 * Function given to ExpandGeneric() to obtain the possible arguments for the
2978 * ":scriptnames" command.
2979 */
2980 static char_u *
2981get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2982{
2983 scriptitem_T *si;
2984
2985 if (!SCRIPT_ID_VALID(idx + 1))
2986 return NULL;
2987
2988 si = SCRIPT_ITEM(idx + 1);
2989 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2990 return NameBuff;
2991}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002992#endif
2993
Bram Moolenaard0190392019-08-23 21:17:35 +02002994/*
2995 * Function given to ExpandGeneric() to obtain the possible arguments of the
2996 * ":messages {clear}" command.
2997 */
2998 static char_u *
2999get_messages_arg(expand_T *xp UNUSED, int idx)
3000{
3001 if (idx == 0)
3002 return (char_u *)"clear";
3003 return NULL;
3004}
3005
3006 static char_u *
3007get_mapclear_arg(expand_T *xp UNUSED, int idx)
3008{
3009 if (idx == 0)
3010 return (char_u *)"<buffer>";
3011 return NULL;
3012}
3013
3014/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003015 * Do the expansion based on xp->xp_context and 'rmp'.
3016 */
3017 static int
3018ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003019 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003020 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003021 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003022 char_u ***matches,
3023 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003024{
3025 static struct expgen
3026 {
3027 int context;
3028 char_u *((*func)(expand_T *, int));
3029 int ic;
3030 int escaped;
3031 } tab[] =
3032 {
3033 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3034 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3035 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3036 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3037 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3038 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3039 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3040 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3041 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3042 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003043#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003044 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3045 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3046 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3047 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3048 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003049#endif
3050#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003051 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3052 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003053#endif
3054#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003055 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003056#endif
3057#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003058 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003059#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003060 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3061 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3062 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003063#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003064 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003065#endif
3066#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003067 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003068#endif
3069#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003070 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003071#endif
3072#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003073 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3074 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003075#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003076 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3077 {EXPAND_USER, get_users, TRUE, FALSE},
3078 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003079#ifdef FEAT_EVAL
3080 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003081 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003082#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003083 };
3084 int i;
3085 int ret = FAIL;
3086
3087 // Find a context in the table and call the ExpandGeneric() with the
3088 // right function to do the expansion.
3089 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3090 {
3091 if (xp->xp_context == tab[i].context)
3092 {
3093 if (tab[i].ic)
3094 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003095 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3096 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003097 break;
3098 }
3099 }
3100
3101 return ret;
3102}
3103
3104/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003105 * Map wild expand options to flags for expand_wildcards()
3106 */
3107 static int
3108map_wildopts_to_ewflags(int options)
3109{
3110 int flags;
3111
3112 flags = EW_DIR; // include directories
3113 if (options & WILD_LIST_NOTFOUND)
3114 flags |= EW_NOTFOUND;
3115 if (options & WILD_ADD_SLASH)
3116 flags |= EW_ADDSLASH;
3117 if (options & WILD_KEEP_ALL)
3118 flags |= EW_KEEPALL;
3119 if (options & WILD_SILENT)
3120 flags |= EW_SILENT;
3121 if (options & WILD_NOERROR)
3122 flags |= EW_NOERROR;
3123 if (options & WILD_ALLLINKS)
3124 flags |= EW_ALLLINKS;
3125
3126 return flags;
3127}
3128
3129/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 * Do the expansion based on xp->xp_context and "pat".
3131 */
3132 static int
3133ExpandFromContext(
3134 expand_T *xp,
3135 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 char_u ***matches,
3137 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 int options) // WILD_ flags
3139{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141 int ret;
3142 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003143 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003144 int fuzzy = cmdline_fuzzy_complete(pat)
3145 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003147 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148
3149 if (xp->xp_context == EXPAND_FILES
3150 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003151 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003152 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003153 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3155 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003156
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003157 *matches = (char_u **)"";
3158 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159 if (xp->xp_context == EXPAND_HELP)
3160 {
3161 // With an empty argument we would get all the help tags, which is
3162 // very slow. Get matches for "help" instead.
3163 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003164 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003165 {
3166#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168#endif
3169 return OK;
3170 }
3171 return FAIL;
3172 }
3173
Bram Moolenaar66b51422019-08-18 21:44:12 +02003174 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003175 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003177 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003178 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003179 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003180#ifdef FEAT_DIFF
3181 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003182 return ExpandBufnames(pat, numMatches, matches,
3183 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003184#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 if (xp->xp_context == EXPAND_TAGS
3186 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003187 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3188 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003189 if (xp->xp_context == EXPAND_COLORS)
3190 {
3191 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003192 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 directories);
3194 }
3195 if (xp->xp_context == EXPAND_COMPILER)
3196 {
3197 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003198 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003199 }
3200 if (xp->xp_context == EXPAND_OWNSYNTAX)
3201 {
3202 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003203 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003204 }
3205 if (xp->xp_context == EXPAND_FILETYPE)
3206 {
3207 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003208 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209 }
Doug Kearns81642d92024-01-04 22:37:44 +01003210#ifdef FEAT_KEYMAP
3211 if (xp->xp_context == EXPAND_KEYMAP)
3212 {
3213 char *directories[] = {"keymap", NULL};
3214 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3215 }
3216#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003217#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003218 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003219 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003220#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003222 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003223 if (xp->xp_context == EXPAND_RUNTIME)
3224 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003225
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003226 // When expanding a function name starting with s:, match the <SNR>nr_
3227 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003228 if ((xp->xp_context == EXPAND_USER_FUNC
3229 || xp->xp_context == EXPAND_DISASSEMBLE)
3230 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003231 {
3232 int len = (int)STRLEN(pat) + 20;
3233
3234 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003235 if (tofree == NULL)
3236 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003237 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003238 pat = tofree;
3239 }
3240
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003241 if (!fuzzy)
3242 {
3243 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3244 if (regmatch.regprog == NULL)
3245 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003247 // set ignore-case according to p_ic, p_scs and pat
3248 regmatch.rm_ic = ignorecase(pat);
3249 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250
3251 if (xp->xp_context == EXPAND_SETTINGS
3252 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003253 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254 else if (xp->xp_context == EXPAND_STRING_SETTING)
3255 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3256 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3257 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003259 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003260 else if (xp->xp_context == EXPAND_ARGOPT)
3261 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003262 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3263 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003264#if defined(FEAT_TERMINAL)
3265 else if (xp->xp_context == EXPAND_TERMINALOPT)
3266 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3267#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003268#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003269 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003270 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003271#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003273 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003275 if (!fuzzy)
3276 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003277 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003278
3279 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003280}
3281
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003282 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003284 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 expand_T *xp,
3286 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003287 char_u ***matches,
3288 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003289 char_u *((*func)(expand_T *, int)),
3290 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003291 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003292{
Yee Cheng China7b81202025-02-23 09:32:47 +01003293 return ExpandGenericExt(
3294 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3295}
3296
3297/*
3298 * Expand a list of names.
3299 *
3300 * Generic function for command line completion. It calls a function to
3301 * obtain strings, one by one. The strings are matched against a regexp
3302 * program. Matching strings are copied into an array, which is returned.
3303 *
3304 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3305 * is used.
3306 *
3307 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3308 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3309 * sorting.
3310 *
3311 * Returns OK when no problems encountered, FAIL for error (out of memory).
3312 */
3313 int
3314ExpandGenericExt(
3315 char_u *pat,
3316 expand_T *xp,
3317 regmatch_T *regmatch,
3318 char_u ***matches,
3319 int *numMatches,
3320 char_u *((*func)(expand_T *, int)),
3321 // returns a string from the list
3322 int escaped,
3323 int sortStartIdx)
3324{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003325 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003326 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003328 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003329 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003330 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003331 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003332 int sort_matches = FALSE;
3333 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003334 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003335
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003336 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003337 *matches = NULL;
3338 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003339
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003340 if (!fuzzy)
3341 ga_init2(&ga, sizeof(char *), 30);
3342 else
3343 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3344
3345 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003346 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003347 str = (*func)(xp, i);
3348 if (str == NULL) // end of list
3349 break;
3350 if (*str == NUL) // skip empty strings
3351 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003352
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003353 if (xp->xp_pattern[0] != NUL)
3354 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003355 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003356 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003357 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003359 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003360 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 }
3362 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003363 else
3364 match = TRUE;
3365
3366 if (!match)
3367 continue;
3368
3369 if (escaped)
3370 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3371 else
3372 str = vim_strsave(str);
3373 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003375 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003376 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003377 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003378 return FAIL;
3379 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003380 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003381 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003382 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003383
3384 if (ga_grow(&ga, 1) == FAIL)
3385 {
3386 vim_free(str);
3387 break;
3388 }
3389
3390 if (fuzzy)
3391 {
3392 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3393 fuzmatch->idx = ga.ga_len;
3394 fuzmatch->str = str;
3395 fuzmatch->score = score;
3396 }
3397 else
3398 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3399
K.Takata161b6ac2022-11-14 15:31:07 +00003400#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003401 if (func == get_menu_names)
3402 {
3403 // test for separator added by get_menu_names()
3404 str += STRLEN(str) - 1;
3405 if (*str == '\001')
3406 *str = '.';
3407 }
K.Takata161b6ac2022-11-14 15:31:07 +00003408#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003409
Yee Cheng China7b81202025-02-23 09:32:47 +01003410 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3411 {
3412 // Found first item to start sorting from. This is usually 0.
3413 sortStartMatchIdx = ga.ga_len;
3414 }
3415
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003416 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003417 }
3418
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003419 if (ga.ga_len == 0)
3420 return OK;
3421
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003422 // sort the matches when using regular expression matching and sorting
3423 // applies to the completion context. Menus and scriptnames should be kept
3424 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003425 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003426 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003427 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003428 && xp->xp_context != EXPAND_SCRIPTNAMES
3429 && xp->xp_context != EXPAND_ARGOPT
3430 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003431 sort_matches = TRUE;
3432
3433 // <SNR> functions should be sorted to the end.
3434 if (xp->xp_context == EXPAND_EXPRESSION
3435 || xp->xp_context == EXPAND_FUNCTIONS
3436 || xp->xp_context == EXPAND_USER_FUNC
3437 || xp->xp_context == EXPAND_DISASSEMBLE)
3438 funcsort = TRUE;
3439
3440 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003441 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003443 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003444 // <SNR> functions should be sorted to the end.
3445 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3446 sort_func_compare);
3447 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003448 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003449 }
3450
3451 if (!fuzzy)
3452 {
3453 *matches = ga.ga_data;
3454 *numMatches = ga.ga_len;
3455 }
3456 else
3457 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003458 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3459 funcsort) == FAIL)
3460 return FAIL;
3461 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003462 }
3463
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003464#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465 // Reset the variables used for special highlight names expansion, so that
3466 // they don't show up when getting normal highlight names by ID.
3467 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003468#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003469
Bram Moolenaar66b51422019-08-18 21:44:12 +02003470 return OK;
3471}
3472
3473/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003474 * Expand shell command matches in one directory of $PATH.
3475 */
3476 static void
3477expand_shellcmd_onedir(
3478 char_u *buf,
3479 char_u *s,
3480 size_t l,
3481 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003482 char_u ***matches,
3483 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003484 int flags,
3485 hashtab_T *ht,
3486 garray_T *gap)
3487{
3488 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003489 hash_T hash;
3490 hashitem_T *hi;
3491
3492 vim_strncpy(buf, s, l);
3493 add_pathsep(buf);
3494 l = STRLEN(buf);
3495 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3496
3497 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003498 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003499 if (ret != OK)
3500 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003501
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003502 if (ga_grow(gap, *numMatches) == FAIL)
3503 {
3504 FreeWild(*numMatches, *matches);
3505 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003506 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003507
3508 for (int i = 0; i < *numMatches; ++i)
3509 {
3510 char_u *name = (*matches)[i];
3511
3512 if (STRLEN(name) > l)
3513 {
3514 // Check if this name was already found.
3515 hash = hash_hash(name + l);
3516 hi = hash_lookup(ht, name + l, hash);
3517 if (HASHITEM_EMPTY(hi))
3518 {
3519 // Remove the path that was prepended.
3520 STRMOVE(name, name + l);
3521 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3522 hash_add_item(ht, hi, name, hash);
3523 name = NULL;
3524 }
3525 }
3526 vim_free(name);
3527 }
3528 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003529}
3530
3531/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 * Complete a shell command.
3533 * Returns FAIL or OK;
3534 */
3535 static int
3536expand_shellcmd(
3537 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003538 char_u ***matches, // return: array with matches
3539 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 int flagsarg) // EW_ flags
3541{
3542 char_u *pat;
3543 int i;
3544 char_u *path = NULL;
3545 int mustfree = FALSE;
3546 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003547 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003548 size_t l;
3549 char_u *s, *e;
3550 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003551 int did_curdir = FALSE;
3552 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003553
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003554 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003555 if (buf == NULL)
3556 return FAIL;
3557
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003558 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003559 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003560 if (pat == NULL)
3561 {
3562 vim_free(buf);
3563 return FAIL;
3564 }
3565
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 for (i = 0; pat[i]; ++i)
3567 if (pat[i] == '\\' && pat[i + 1] == ' ')
3568 STRMOVE(pat + i, pat + i + 1);
3569
3570 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3571
3572 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3573 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3574 path = (char_u *)".";
3575 else
3576 {
3577 // For an absolute name we don't use $PATH.
3578 if (!mch_isFullName(pat))
3579 path = vim_getenv((char_u *)"PATH", &mustfree);
3580 if (path == NULL)
3581 path = (char_u *)"";
3582 }
3583
3584 // Go over all directories in $PATH. Expand matches in that directory and
3585 // collect them in "ga". When "." is not in $PATH also expand for the
3586 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003587 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003588 hash_init(&found_ht);
3589 for (s = path; ; s = e)
3590 {
K.Takata161b6ac2022-11-14 15:31:07 +00003591#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003592 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003593#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003594 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003595#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003596 if (e == NULL)
3597 e = s + STRLEN(s);
3598
3599 if (*s == NUL)
3600 {
3601 if (did_curdir)
3602 break;
3603 // Find directories in the current directory, path is empty.
3604 did_curdir = TRUE;
3605 flags |= EW_DIR;
3606 }
3607 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3608 {
3609 did_curdir = TRUE;
3610 flags |= EW_DIR;
3611 }
3612 else
3613 // Do not match directories inside a $PATH item.
3614 flags &= ~EW_DIR;
3615
3616 l = e - s;
3617 if (l > MAXPATHL - 5)
3618 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003619
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003620 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003621 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622
Bram Moolenaar66b51422019-08-18 21:44:12 +02003623 if (*e != NUL)
3624 ++e;
3625 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003626 *matches = ga.ga_data;
3627 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628
3629 vim_free(buf);
3630 vim_free(pat);
3631 if (mustfree)
3632 vim_free(path);
3633 hash_clear(&found_ht);
3634 return OK;
3635}
3636
K.Takata161b6ac2022-11-14 15:31:07 +00003637#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638/*
3639 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003640 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003641 */
3642 static void *
3643call_user_expand_func(
3644 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003645 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003646{
3647 cmdline_info_T *ccline = get_cmdline_info();
3648 int keep = 0;
3649 typval_T args[4];
3650 sctx_T save_current_sctx = current_sctx;
3651 char_u *pat = NULL;
3652 void *ret;
3653
3654 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3655 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003656
3657 if (ccline->cmdbuff != NULL)
3658 {
3659 keep = ccline->cmdbuff[ccline->cmdlen];
3660 ccline->cmdbuff[ccline->cmdlen] = 0;
3661 }
3662
3663 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3664
3665 args[0].v_type = VAR_STRING;
3666 args[0].vval.v_string = pat;
3667 args[1].v_type = VAR_STRING;
3668 args[1].vval.v_string = xp->xp_line;
3669 args[2].v_type = VAR_NUMBER;
3670 args[2].vval.v_number = xp->xp_col;
3671 args[3].v_type = VAR_UNKNOWN;
3672
3673 current_sctx = xp->xp_script_ctx;
3674
3675 ret = user_expand_func(xp->xp_arg, 3, args);
3676
3677 current_sctx = save_current_sctx;
3678 if (ccline->cmdbuff != NULL)
3679 ccline->cmdbuff[ccline->cmdlen] = keep;
3680
3681 vim_free(pat);
3682 return ret;
3683}
3684
3685/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3687 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 */
3689 static int
3690ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003691 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003692 expand_T *xp,
3693 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003694 char_u ***matches,
3695 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003696{
3697 char_u *retstr;
3698 char_u *s;
3699 char_u *e;
3700 int keep;
3701 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003702 int fuzzy;
3703 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003704 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003705
3706 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003707 *matches = NULL;
3708 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003709
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003710 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003711 if (retstr == NULL)
3712 return FAIL;
3713
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003714 if (!fuzzy)
3715 ga_init2(&ga, sizeof(char *), 3);
3716 else
3717 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3718
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719 for (s = retstr; *s != NUL; s = e)
3720 {
3721 e = vim_strchr(s, '\n');
3722 if (e == NULL)
3723 e = s + STRLEN(s);
3724 keep = *e;
3725 *e = NUL;
3726
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003727 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003728 {
3729 if (!fuzzy)
3730 match = vim_regexec(regmatch, s, (colnr_T)0);
3731 else
3732 {
3733 score = fuzzy_match_str(s, pat);
3734 match = (score != 0);
3735 }
3736 }
3737 else
3738 match = TRUE; // match everything
3739
Bram Moolenaar66b51422019-08-18 21:44:12 +02003740 *e = keep;
3741
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003742 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 {
3744 if (ga_grow(&ga, 1) == FAIL)
3745 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003746 if (!fuzzy)
3747 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3748 else
3749 {
3750 fuzmatch_str_T *fuzmatch =
3751 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003752 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003753 fuzmatch->str = vim_strnsave(s, e - s);
3754 fuzmatch->score = score;
3755 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003756 ++ga.ga_len;
3757 }
3758
3759 if (*e != NUL)
3760 ++e;
3761 }
3762 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003763
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003764 if (ga.ga_len == 0)
3765 return OK;
3766
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003767 if (!fuzzy)
3768 {
3769 *matches = ga.ga_data;
3770 *numMatches = ga.ga_len;
3771 }
3772 else
3773 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003774 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3775 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003776 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003777 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003778 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003779 return OK;
3780}
3781
3782/*
3783 * Expand names with a list returned by a function defined by the user.
3784 */
3785 static int
3786ExpandUserList(
3787 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003788 char_u ***matches,
3789 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003790{
3791 list_T *retlist;
3792 listitem_T *li;
3793 garray_T ga;
3794
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003795 *matches = NULL;
3796 *numMatches = 0;
3797 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003798 if (retlist == NULL)
3799 return FAIL;
3800
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003801 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003803 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003804 {
3805 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3806 continue; // Skip non-string items and empty strings
3807
3808 if (ga_grow(&ga, 1) == FAIL)
3809 break;
3810
3811 ((char_u **)ga.ga_data)[ga.ga_len] =
3812 vim_strsave(li->li_tv.vval.v_string);
3813 ++ga.ga_len;
3814 }
3815 list_unref(retlist);
3816
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003817 *matches = ga.ga_data;
3818 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003819 return OK;
3820}
K.Takata161b6ac2022-11-14 15:31:07 +00003821#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003822
3823/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003824 * Expand "file" for all comma-separated directories in "path".
3825 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003826 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003827 */
3828 void
3829globpath(
3830 char_u *path,
3831 char_u *file,
3832 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003833 int expand_options,
3834 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003835{
3836 expand_T xpc;
3837 char_u *buf;
3838 int i;
3839 int num_p;
3840 char_u **p;
3841
3842 buf = alloc(MAXPATHL);
3843 if (buf == NULL)
3844 return;
3845
3846 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003847 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003848
3849 // Loop over all entries in {path}.
3850 while (*path != NUL)
3851 {
3852 // Copy one item of the path to buf[] and concatenate the file name.
3853 copy_option_part(&path, buf, MAXPATHL, ",");
3854 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3855 {
K.Takata161b6ac2022-11-14 15:31:07 +00003856#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003857 // Using the platform's path separator (\) makes vim incorrectly
3858 // treat it as an escape character, use '/' instead.
3859 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3860 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003861#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003862 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003863#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003864 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003865 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003866 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3867 {
3868 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3869
3870 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003871 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003872 for (i = 0; i < num_p; ++i)
3873 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003874 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003875 ++ga->ga_len;
3876 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003877 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003878 }
3879 }
3880 }
3881
3882 vim_free(buf);
3883}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003884
Bram Moolenaareadee482020-09-04 15:37:31 +02003885/*
3886 * Translate some keys pressed when 'wildmenu' is used.
3887 */
3888 int
3889wildmenu_translate_key(
3890 cmdline_info_T *cclp,
3891 int key,
3892 expand_T *xp,
3893 int did_wild_list)
3894{
3895 int c = key;
3896
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003897 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003898 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003899 // When the popup menu is used for cmdline completion:
3900 // Up : go to the previous item in the menu
3901 // Down : go to the next item in the menu
3902 // Left : go to the parent directory
3903 // Right: list the files in the selected directory
3904 switch (c)
3905 {
3906 case K_UP: c = K_LEFT; break;
3907 case K_DOWN: c = K_RIGHT; break;
3908 case K_LEFT: c = K_UP; break;
3909 case K_RIGHT: c = K_DOWN; break;
3910 default: break;
3911 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003912 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003913
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003914 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003915 {
3916 if (c == K_LEFT)
3917 c = Ctrl_P;
3918 else if (c == K_RIGHT)
3919 c = Ctrl_N;
3920 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003921
Bram Moolenaareadee482020-09-04 15:37:31 +02003922 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003923 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003924 && cclp->cmdpos > 1
3925 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3926 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3927 && (c == '\n' || c == '\r' || c == K_KENTER))
3928 c = K_DOWN;
3929
3930 return c;
3931}
3932
3933/*
3934 * Delete characters on the command line, from "from" to the current
3935 * position.
3936 */
3937 static void
3938cmdline_del(cmdline_info_T *cclp, int from)
3939{
3940 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3941 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3942 cclp->cmdlen -= cclp->cmdpos - from;
3943 cclp->cmdpos = from;
3944}
3945
3946/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003947 * Handle a key pressed when the wild menu for the menu names
3948 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003949 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003950 static int
3951wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003952{
Bram Moolenaareadee482020-09-04 15:37:31 +02003953 int i;
3954 int j;
3955
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003956 // Hitting <Down> after "emenu Name.": complete submenu
3957 if (key == K_DOWN && cclp->cmdpos > 0
3958 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003959 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003960 key = p_wc;
3961 KeyTyped = TRUE; // in case the key was mapped
3962 }
3963 else if (key == K_UP)
3964 {
3965 // Hitting <Up>: Remove one submenu name in front of the
3966 // cursor
3967 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003968
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003969 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3970 i = 0;
3971 while (--j > 0)
3972 {
3973 // check for start of menu name
3974 if (cclp->cmdbuff[j] == ' '
3975 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003976 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003977 i = j + 1;
3978 break;
3979 }
3980 // check for start of submenu name
3981 if (cclp->cmdbuff[j] == '.'
3982 && cclp->cmdbuff[j - 1] != '\\')
3983 {
3984 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003985 {
3986 i = j + 1;
3987 break;
3988 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003989 else
3990 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003991 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003992 }
3993 if (i > 0)
3994 cmdline_del(cclp, i);
3995 key = p_wc;
3996 KeyTyped = TRUE; // in case the key was mapped
3997 xp->xp_context = EXPAND_NOTHING;
3998 }
3999
4000 return key;
4001}
4002
4003/*
4004 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4005 * directory names (EXPAND_DIRECTORIES) or shell command names
4006 * (EXPAND_SHELLCMD) is displayed.
4007 */
4008 static int
4009wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4010{
4011 int i;
4012 int j;
4013 char_u upseg[5];
4014
4015 upseg[0] = PATHSEP;
4016 upseg[1] = '.';
4017 upseg[2] = '.';
4018 upseg[3] = PATHSEP;
4019 upseg[4] = NUL;
4020
4021 if (key == K_DOWN
4022 && cclp->cmdpos > 0
4023 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4024 && (cclp->cmdpos < 3
4025 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4026 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4027 {
4028 // go down a directory
4029 key = p_wc;
4030 KeyTyped = TRUE; // in case the key was mapped
4031 }
4032 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4033 {
4034 // If in a direct ancestor, strip off one ../ to go down
4035 int found = FALSE;
4036
4037 j = cclp->cmdpos;
4038 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4039 while (--j > i)
4040 {
4041 if (has_mbyte)
4042 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4043 if (vim_ispathsep(cclp->cmdbuff[j]))
4044 {
4045 found = TRUE;
4046 break;
4047 }
4048 }
4049 if (found
4050 && cclp->cmdbuff[j - 1] == '.'
4051 && cclp->cmdbuff[j - 2] == '.'
4052 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4053 {
4054 cmdline_del(cclp, j - 2);
4055 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004056 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004057 }
4058 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004059 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004060 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004061 // go up a directory
4062 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004063
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004064 j = cclp->cmdpos - 1;
4065 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4066 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004067 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004068 if (has_mbyte)
4069 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4070 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004071#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004072 && vim_strchr((char_u *)" *?[{`$%#",
4073 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004074#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004075 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004076 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004077 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004078 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004079 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004080 break;
4081 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004082 else
4083 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004084 }
4085 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004086
4087 if (!found)
4088 j = i;
4089 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4090 j += 4;
4091 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4092 && j == i)
4093 j += 3;
4094 else
4095 j = 0;
4096 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004097 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004098 // TODO this is only for DOS/UNIX systems - need to put in
4099 // machine-specific stuff here and in upseg init
4100 cmdline_del(cclp, j);
4101 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004102 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004103 else if (cclp->cmdpos > i)
4104 cmdline_del(cclp, i);
4105
4106 // Now complete in the new directory. Set KeyTyped in case the
4107 // Up key came from a mapping.
4108 key = p_wc;
4109 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004110 }
4111
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004112 return key;
4113}
4114
4115/*
4116 * Handle a key pressed when the wild menu is displayed
4117 */
4118 int
4119wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4120{
4121 if (xp->xp_context == EXPAND_MENUNAMES)
4122 return wildmenu_process_key_menunames(cclp, key, xp);
4123 else if ((xp->xp_context == EXPAND_FILES
4124 || xp->xp_context == EXPAND_DIRECTORIES
4125 || xp->xp_context == EXPAND_SHELLCMD))
4126 return wildmenu_process_key_filenames(cclp, key, xp);
4127
4128 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004129}
4130
4131/*
4132 * Free expanded names when finished walking through the matches
4133 */
4134 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004135wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004136{
4137 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004138
4139 if (!p_wmnu || wild_menu_showing == 0)
4140 return;
4141
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004142#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004143 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004144 if (cclp->input_fn)
4145 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004146#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004147
4148 if (wild_menu_showing == WM_SCROLLED)
4149 {
4150 // Entered command line, move it up
4151 cmdline_row--;
4152 redrawcmd();
4153 }
4154 else if (save_p_ls != -1)
4155 {
4156 // restore 'laststatus' and 'winminheight'
4157 p_ls = save_p_ls;
4158 p_wmh = save_p_wmh;
4159 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004160 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004161 redrawcmd();
4162 save_p_ls = -1;
4163 }
4164 else
4165 {
4166 win_redraw_last_status(topframe);
4167 redraw_statuslines();
4168 }
4169 KeyTyped = skt;
4170 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004171#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004172 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004173 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004174#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004175}
Bram Moolenaareadee482020-09-04 15:37:31 +02004176
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004177#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004178/*
4179 * "getcompletion()" function
4180 */
4181 void
4182f_getcompletion(typval_T *argvars, typval_T *rettv)
4183{
4184 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004185 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004186 expand_T xpc;
4187 int filtered = FALSE;
4188 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004189 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004190
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004191 if (in_vim9script()
4192 && (check_for_string_arg(argvars, 0) == FAIL
4193 || check_for_string_arg(argvars, 1) == FAIL
4194 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4195 return;
4196
ii144785fe02021-11-21 12:13:56 +00004197 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004198 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004199 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004200 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004201
4202 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004203 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004204
4205 if (p_wic)
4206 options |= WILD_ICASE;
4207
4208 // For filtered results, 'wildignore' is used
4209 if (!filtered)
4210 options |= WILD_KEEP_ALL;
4211
4212 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004213 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004214 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004215 int cmdline_len = (int)STRLEN(pat);
4216 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004217 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004218 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004219 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004220 else
4221 {
ii144785fe02021-11-21 12:13:56 +00004222 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004223 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004224 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004225
4226 xpc.xp_context = cmdcomplete_str_to_type(type);
4227 if (xpc.xp_context == EXPAND_NOTHING)
4228 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004229 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004230 return;
4231 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004232
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004233 if (xpc.xp_context == EXPAND_USER_DEFINED)
4234 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004235 // Must be "custom,funcname" pattern
4236 if (STRNCMP(type, "custom,", 7) != 0)
4237 {
4238 semsg(_(e_invalid_argument_str), type);
4239 return;
4240 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004241
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004242 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004243 }
4244
4245 if (xpc.xp_context == EXPAND_USER_LIST)
4246 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004247 // Must be "customlist,funcname" pattern
4248 if (STRNCMP(type, "customlist,", 11) != 0)
4249 {
4250 semsg(_(e_invalid_argument_str), type);
4251 return;
4252 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004253
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004254 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004255 }
4256
Bram Moolenaar66b51422019-08-18 21:44:12 +02004257# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004258 if (xpc.xp_context == EXPAND_MENUS)
4259 {
4260 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4261 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4262 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004263# endif
4264# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004265 if (xpc.xp_context == EXPAND_CSCOPE)
4266 {
4267 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4268 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4269 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004270# endif
4271# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004272 if (xpc.xp_context == EXPAND_SIGN)
4273 {
4274 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4275 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4276 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004277# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004278 if (xpc.xp_context == EXPAND_RUNTIME)
4279 {
4280 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4281 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4282 }
zeertzjq85f36d62024-10-10 19:14:13 +02004283 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4284 {
4285 int context = EXPAND_SHELLCMDLINE;
4286 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4287 &context);
4288 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4289 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004290 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004291
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004292 if (cmdline_fuzzy_completion_supported(&xpc))
4293 // when fuzzy matching, don't modify the search string
4294 pat = vim_strsave(xpc.xp_pattern);
4295 else
4296 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4297
Bram Moolenaar93a10962022-06-16 11:42:09 +01004298 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004299 {
4300 int i;
4301
4302 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4303
4304 for (i = 0; i < xpc.xp_numfiles; i++)
4305 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4306 }
4307 vim_free(pat);
4308 ExpandCleanup(&xpc);
4309}
Girish Palya92f68e22025-04-21 11:12:41 +02004310
4311/*
4312 * "cmdcomplete_info()" function
4313 */
4314 void
4315f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4316{
4317 cmdline_info_T *ccline = get_cmdline_info();
4318 dict_T *retdict;
4319 list_T *li;
4320 int idx;
4321 int ret = OK;
4322
4323 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4324 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4325 return;
4326 retdict = rettv->vval.v_dict;
4327 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4328 if (ret == OK)
4329 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4330 if (ret == OK)
4331 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4332 if (ret == OK)
4333 {
4334 li = list_alloc();
4335 if (li == NULL)
4336 return;
4337 ret = dict_add_list(retdict, "matches", li);
4338 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4339 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4340 }
4341}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004342#endif // FEAT_EVAL