blob: e8bc0805ca59661585f89be62967f087e3a79020 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
zeertzjq7a5115c2025-03-19 20:29:58 +010018static void set_context_for_wildcard_arg(exarg_T *eap, char_u *arg, int usefilter, expand_T *xp, int *complp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000020static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020021static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020023#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000024static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000025static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020026#endif
27
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000028// "compl_match_array" points the currently displayed list of entries in the
29// popup menu. It is NULL when there is no popup menu.
30static pumitem_T *compl_match_array = NULL;
31static int compl_match_arraysize;
32// First column in cmdline of the matched item for completion.
33static int compl_startcol;
34static int compl_selected;
Girish Palya92f68e22025-04-21 11:12:41 +020035// cmdline before expansion
36static char_u *cmdline_orig = NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000037
zeertzjqc51a3762022-12-10 10:22:29 +000038#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000039
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000040/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000041 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
42 * context.
43 */
44 static int
45cmdline_fuzzy_completion_supported(expand_T *xp)
46{
47 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
48 && xp->xp_context != EXPAND_BOOL_SETTINGS
49 && xp->xp_context != EXPAND_COLORS
50 && xp->xp_context != EXPAND_COMPILER
51 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020052 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000053 && xp->xp_context != EXPAND_FILES
54 && xp->xp_context != EXPAND_FILES_IN_PATH
55 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010056 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000057 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010058 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020060 && xp->xp_context != EXPAND_STRING_SETTING
61 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_OWNSYNTAX
63 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000064 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000065 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020066 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000067 && xp->xp_context != EXPAND_TAGS
68 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000069 && xp->xp_context != EXPAND_USER_LIST);
70}
71
72/*
73 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000074 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
75 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000076 */
77 int
78cmdline_fuzzy_complete(char_u *fuzzystr)
79{
80 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
81}
82
83/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000084 * sort function for the completion matches.
85 * <SNR> functions should be sorted to the end.
86 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020087 static int
88sort_func_compare(const void *s1, const void *s2)
89{
90 char_u *p1 = *(char_u **)s1;
91 char_u *p2 = *(char_u **)s2;
92
93 if (*p1 != '<' && *p2 == '<') return -1;
94 if (*p1 == '<' && *p2 != '<') return 1;
95 return STRCMP(p1, p2);
96}
Bram Moolenaar66b51422019-08-18 21:44:12 +020097
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000098/*
99 * Escape special characters in the cmdline completion matches.
100 */
Bram Moolenaar66b51422019-08-18 21:44:12 +0200101 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000102wildescape(
103 expand_T *xp,
104 char_u *str,
105 int numfiles,
106 char_u **files)
107{
108 char_u *p;
109 int vse_what = xp->xp_context == EXPAND_BUFFERS
110 ? VSE_BUFFER : VSE_NONE;
111
112 if (xp->xp_context == EXPAND_FILES
113 || xp->xp_context == EXPAND_FILES_IN_PATH
114 || xp->xp_context == EXPAND_SHELLCMD
115 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200116 || xp->xp_context == EXPAND_DIRECTORIES
117 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000118 {
119 // Insert a backslash into a file name before a space, \, %, #
120 // and wildmatch characters, except '~'.
121 for (int i = 0; i < numfiles; ++i)
122 {
123 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200124 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200126 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
127 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000128 if (p != NULL)
129 {
130 vim_free(files[i]);
131 files[i] = p;
132#if defined(BACKSLASH_IN_FILENAME)
133 p = vim_strsave_escaped(files[i], (char_u *)" ");
134 if (p != NULL)
135 {
136 vim_free(files[i]);
137 files[i] = p;
138 }
139#endif
140 }
141 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200142 else if (xp->xp_backslash & XP_BS_COMMA)
143 {
144 if (vim_strchr(files[i], ',') != NULL)
145 {
146 p = vim_strsave_escaped(files[i], (char_u *)",");
147 if (p != NULL)
148 {
149 vim_free(files[i]);
150 files[i] = p;
151 }
152 }
153 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000154#ifdef BACKSLASH_IN_FILENAME
155 p = vim_strsave_fnameescape(files[i], vse_what);
156#else
157 p = vim_strsave_fnameescape(files[i],
158 xp->xp_shell ? VSE_SHELL : vse_what);
159#endif
160 if (p != NULL)
161 {
162 vim_free(files[i]);
163 files[i] = p;
164 }
165
166 // If 'str' starts with "\~", replace "~" at start of
167 // files[i] with "\~".
168 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
169 escape_fname(&files[i]);
170 }
171 xp->xp_backslash = XP_BS_NONE;
172
173 // If the first file starts with a '+' escape it. Otherwise it
174 // could be seen as "+cmd".
175 if (*files[0] == '+')
176 escape_fname(&files[0]);
177 }
178 else if (xp->xp_context == EXPAND_TAGS)
179 {
180 // Insert a backslash before characters in a tag name that
181 // would terminate the ":tag" command.
182 for (int i = 0; i < numfiles; ++i)
183 {
184 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
185 if (p != NULL)
186 {
187 vim_free(files[i]);
188 files[i] = p;
189 }
190 }
191 }
192}
193
194/*
195 * Escape special characters in the cmdline completion matches.
196 */
197 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200198ExpandEscape(
199 expand_T *xp,
200 char_u *str,
201 int numfiles,
202 char_u **files,
203 int options)
204{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200205 // May change home directory back to "~"
206 if (options & WILD_HOME_REPLACE)
207 tilde_replace(str, numfiles, files);
208
209 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000210 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200211}
212
213/*
214 * Return FAIL if this is not an appropriate context in which to do
215 * completion of anything, return OK if it is (even if there are no matches).
216 * For the caller, this means that the character is just passed through like a
217 * normal character (instead of being expanded). This allows :s/^I^D etc.
218 */
219 int
220nextwild(
221 expand_T *xp,
222 int type,
223 int options, // extra options for ExpandOne()
224 int escape) // if TRUE, escape the returned matches
225{
226 cmdline_info_T *ccline = get_cmdline_info();
227 int i, j;
228 char_u *p1;
229 char_u *p2;
230 int difflen;
231 int v;
232
233 if (xp->xp_numfiles == -1)
234 {
Jim Zhou3255af82025-02-27 19:29:50 +0100235#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100236 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100237 {
238 // Expand commands typed in input() function
239 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100240 }
241 else
Jim Zhou3255af82025-02-27 19:29:50 +0100242#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100243 {
Jim Zhou3255af82025-02-27 19:29:50 +0100244 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100245 }
246 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200247 }
248
249 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
250 {
251 beep_flush();
252 return OK; // Something illegal on command line
253 }
254 if (xp->xp_context == EXPAND_NOTHING)
255 {
256 // Caller can use the character as a normal char instead
257 return FAIL;
258 }
259
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000260 // If cmd_silent is set then don't show the dots, because redrawcmd() below
261 // won't remove them.
262 if (!cmd_silent)
263 {
264 msg_puts("..."); // show that we are busy
265 out_flush();
266 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200267
268 i = (int)(xp->xp_pattern - ccline->cmdbuff);
269 xp->xp_pattern_len = ccline->cmdpos - i;
270
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000271 if (type == WILD_NEXT || type == WILD_PREV
272 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200273 {
274 // Get next/previous match for a previous expanded pattern.
275 p2 = ExpandOne(xp, NULL, NULL, 0, type);
276 }
277 else
278 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000279 if (cmdline_fuzzy_completion_supported(xp))
280 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200281 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000282 else
283 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
284
Bram Moolenaar66b51422019-08-18 21:44:12 +0200285 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000286 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200287 p2 = NULL;
288 else
289 {
290 int use_options = options |
291 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100292 if (use_options & WILD_KEEP_SOLE_ITEM)
293 use_options &= ~WILD_KEEP_SOLE_ITEM;
294
Bram Moolenaar66b51422019-08-18 21:44:12 +0200295 if (escape)
296 use_options |= WILD_ESCAPE;
297
298 if (p_wic)
299 use_options += WILD_ICASE;
300 p2 = ExpandOne(xp, p1,
301 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
302 use_options, type);
303 vim_free(p1);
304 // longest match: make sure it is not shorter, happens with :help
305 if (p2 != NULL && type == WILD_LONGEST)
306 {
307 for (j = 0; j < xp->xp_pattern_len; ++j)
308 if (ccline->cmdbuff[i + j] == '*'
309 || ccline->cmdbuff[i + j] == '?')
310 break;
311 if ((int)STRLEN(p2) < j)
312 VIM_CLEAR(p2);
313 }
314 }
315 }
316
317 if (p2 != NULL && !got_int)
318 {
319 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
320 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
321 {
322 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
323 xp->xp_pattern = ccline->cmdbuff + i;
324 }
325 else
326 v = OK;
327 if (v == OK)
328 {
329 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
330 &ccline->cmdbuff[ccline->cmdpos],
331 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
332 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
333 ccline->cmdlen += difflen;
334 ccline->cmdpos += difflen;
335 }
336 }
337 vim_free(p2);
338
339 redrawcmd();
340 cursorcmd();
341
342 // When expanding a ":map" command and no matches are found, assume that
343 // the key is supposed to be inserted literally
344 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
345 return FAIL;
346
347 if (xp->xp_numfiles <= 0 && p2 == NULL)
348 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100349 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200350 // free expanded pattern
351 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
352
353 return OK;
354}
355
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356/*
357 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000358 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 */
360 static int
361cmdline_pum_create(
362 cmdline_info_T *ccline,
363 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000364 char_u **matches,
365 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366 int showtail)
367{
368 int i;
369 int columns;
370
371 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000374 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 {
zeertzjqc51a3762022-12-10 10:22:29 +0000376 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000377 compl_match_array[i].pum_info = NULL;
378 compl_match_array[i].pum_extra = NULL;
379 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200380 compl_match_array[i].pum_user_abbr_hlattr = -1;
381 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000382 }
383
384 // Compute the popup menu starting column
Christian Brabandt362be6b2025-04-22 20:06:53 +0200385 compl_startcol = ccline == NULL ? 0 : vim_strsize(ccline->cmdbuff) + 1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000386 columns = vim_strsize(xp->xp_pattern);
387 if (showtail)
388 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000389 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000390 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000391 }
glepnir977561a2025-02-13 20:48:56 +0100392 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000393
394 // no default selection
395 compl_selected = -1;
396
397 cmdline_pum_display();
398
399 return EXPAND_OK;
400}
401
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402/*
403 * Display the cmdline completion matches in a popup menu
404 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000405 void
406cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407{
408 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
409}
410
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000411/*
412 * Returns TRUE if the cmdline completion popup menu is being displayed.
413 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000414 int
415cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100417 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000418}
419
420/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000421 * Remove the cmdline completion popup menu (if present), free the list of
422 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000424 void
zeertzjq1830e782025-03-13 20:29:13 +0100425cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000427 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100428 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100429#ifdef FEAT_EVAL
430 int save_RedrawingDisabled = RedrawingDisabled;
431 if (cclp->input_fn)
432 RedrawingDisabled = 0;
433#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000434
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435 pum_undisplay();
436 VIM_CLEAR(compl_match_array);
Girish Palya92f68e22025-04-21 11:12:41 +0200437 compl_match_arraysize = 0;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000438 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000440 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000441 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100442
443 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
444 // as a side effect.
445 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100446#ifdef FEAT_EVAL
447 if (cclp->input_fn)
448 RedrawingDisabled = save_RedrawingDisabled;
449#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000450}
451
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000452 void
453cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000454{
zeertzjq1830e782025-03-13 20:29:13 +0100455 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000456 wildmenu_cleanup(cclp);
457}
458
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000459/*
460 * Returns the starting column number to use for the cmdline completion popup
461 * menu.
462 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000463 int
464cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000465{
466 return compl_startcol;
467}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000468
Bram Moolenaar66b51422019-08-18 21:44:12 +0200469/*
zeertzjqd8c93402024-06-17 18:25:32 +0200470 * Returns the current cmdline completion pattern.
471 */
472 char_u *
473cmdline_compl_pattern(void)
474{
475 expand_T *xp = get_cmdline_info()->xpc;
476
477 return xp == NULL ? NULL : xp->xp_orig;
478}
479
480/*
481 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
482 */
483 int
484cmdline_compl_is_fuzzy(void)
485{
486 expand_T *xp = get_cmdline_info()->xpc;
487
488 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
489}
490
491/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000492 * Return the number of characters that should be skipped in a status match.
493 * These are backslashes used for escaping. Do show backslashes in help tags.
494 */
495 static int
496skip_status_match_char(expand_T *xp, char_u *s)
497{
498 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
499#ifdef FEAT_MENU
500 || ((xp->xp_context == EXPAND_MENUS
501 || xp->xp_context == EXPAND_MENUNAMES)
502 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
503#endif
504 )
505 {
506#ifndef BACKSLASH_IN_FILENAME
507 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
508 return 2;
509#endif
510 return 1;
511 }
512 return 0;
513}
514
515/*
516 * Get the length of an item as it will be shown in the status line.
517 */
518 static int
519status_match_len(expand_T *xp, char_u *s)
520{
521 int len = 0;
522
523#ifdef FEAT_MENU
524 int emenu = xp->xp_context == EXPAND_MENUS
525 || xp->xp_context == EXPAND_MENUNAMES;
526
527 // Check for menu separators - replace with '|'.
528 if (emenu && menu_is_separator(s))
529 return 1;
530#endif
531
532 while (*s != NUL)
533 {
534 s += skip_status_match_char(xp, s);
535 len += ptr2cells(s);
536 MB_PTR_ADV(s);
537 }
538
539 return len;
540}
541
542/*
543 * Show wildchar matches in the status line.
544 * Show at least the "match" item.
545 * We start at item 'first_match' in the list and show all matches that fit.
546 *
547 * If inversion is possible we use it. Else '=' characters are used.
548 */
549 static void
550win_redr_status_matches(
551 expand_T *xp,
552 int num_matches,
553 char_u **matches, // list of matches
554 int match,
555 int showtail)
556{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000557 int row;
558 char_u *buf;
559 int len;
560 int clen; // length in screen cells
561 int fillchar;
562 int attr;
563 int i;
564 int highlight = TRUE;
565 char_u *selstart = NULL;
566 int selstart_col = 0;
567 char_u *selend = NULL;
568 static int first_match = 0;
569 int add_left = FALSE;
570 char_u *s;
571#ifdef FEAT_MENU
572 int emenu;
573#endif
574 int l;
575
576 if (matches == NULL) // interrupted completion?
577 return;
578
579 if (has_mbyte)
580 buf = alloc(Columns * MB_MAXBYTES + 1);
581 else
582 buf = alloc(Columns + 1);
583 if (buf == NULL)
584 return;
585
586 if (match == -1) // don't show match but original text
587 {
588 match = 0;
589 highlight = FALSE;
590 }
591 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000592 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 if (match == 0)
594 first_match = 0;
595 else if (match < first_match)
596 {
597 // jumping left, as far as we can go
598 first_match = match;
599 add_left = TRUE;
600 }
601 else
602 {
603 // check if match fits on the screen
604 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000605 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000606 if (first_match > 0)
607 clen += 2;
608 // jumping right, put match at the left
609 if ((long)clen > Columns)
610 {
611 first_match = match;
612 // if showing the last match, we can add some on the left
613 clen = 2;
614 for (i = match; i < num_matches; ++i)
615 {
zeertzjqc51a3762022-12-10 10:22:29 +0000616 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000617 if ((long)clen >= Columns)
618 break;
619 }
620 if (i == num_matches)
621 add_left = TRUE;
622 }
623 }
624 if (add_left)
625 while (first_match > 0)
626 {
zeertzjqc51a3762022-12-10 10:22:29 +0000627 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000628 if ((long)clen >= Columns)
629 break;
630 --first_match;
631 }
632
633 fillchar = fillchar_status(&attr, curwin);
634
635 if (first_match == 0)
636 {
637 *buf = NUL;
638 len = 0;
639 }
640 else
641 {
642 STRCPY(buf, "< ");
643 len = 2;
644 }
645 clen = len;
646
647 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000648 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000649 {
650 if (i == match)
651 {
652 selstart = buf + len;
653 selstart_col = clen;
654 }
655
zeertzjqc51a3762022-12-10 10:22:29 +0000656 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000657 // Check for menu separators - replace with '|'
658#ifdef FEAT_MENU
659 emenu = (xp->xp_context == EXPAND_MENUS
660 || xp->xp_context == EXPAND_MENUNAMES);
661 if (emenu && menu_is_separator(s))
662 {
663 STRCPY(buf + len, transchar('|'));
664 l = (int)STRLEN(buf + len);
665 len += l;
666 clen += l;
667 }
668 else
669#endif
670 for ( ; *s != NUL; ++s)
671 {
672 s += skip_status_match_char(xp, s);
673 clen += ptr2cells(s);
674 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
675 {
676 STRNCPY(buf + len, s, l);
677 s += l - 1;
678 len += l;
679 }
680 else
681 {
682 STRCPY(buf + len, transchar_byte(*s));
683 len += (int)STRLEN(buf + len);
684 }
685 }
686 if (i == match)
687 selend = buf + len;
688
689 *(buf + len++) = ' ';
690 *(buf + len++) = ' ';
691 clen += 2;
692 if (++i == num_matches)
693 break;
694 }
695
696 if (i != num_matches)
697 {
698 *(buf + len++) = '>';
699 ++clen;
700 }
701
702 buf[len] = NUL;
703
704 row = cmdline_row - 1;
705 if (row >= 0)
706 {
707 if (wild_menu_showing == 0)
708 {
709 if (msg_scrolled > 0)
710 {
711 // Put the wildmenu just above the command line. If there is
712 // no room, scroll the screen one line up.
713 if (cmdline_row == Rows - 1)
714 {
715 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
716 ++msg_scrolled;
717 }
718 else
719 {
720 ++cmdline_row;
721 ++row;
722 }
723 wild_menu_showing = WM_SCROLLED;
724 }
725 else
726 {
727 // Create status line if needed by setting 'laststatus' to 2.
728 // Set 'winminheight' to zero to avoid that the window is
729 // resized.
730 if (lastwin->w_status_height == 0)
731 {
732 save_p_ls = p_ls;
733 save_p_wmh = p_wmh;
734 p_ls = 2;
735 p_wmh = 0;
736 last_status(FALSE);
737 }
738 wild_menu_showing = WM_SHOWN;
739 }
740 }
741
742 screen_puts(buf, row, 0, attr);
743 if (selstart != NULL && highlight)
744 {
745 *selend = NUL;
746 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
747 }
748
749 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
750 }
751
752 win_redraw_last_status(topframe);
753 vim_free(buf);
754}
755
756/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200758 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759 */
760 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100761get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000762{
glepnir977561a2025-02-13 20:48:56 +0100763 int findex = xp->xp_selected;
764 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000765
zeertzjqb6c900b2025-02-14 17:59:31 +0100766 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000767 if (xp->xp_numfiles <= 0)
768 return NULL;
769
770 if (mode == WILD_PREV)
771 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100772 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 if (findex == -1)
774 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100775 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000776 --findex;
777 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000778 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000779 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100780 // Select the next entry
781 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000782 }
glepnir977561a2025-02-13 20:48:56 +0100783 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000784 {
glepnir977561a2025-02-13 20:48:56 +0100785 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
786 ht = pum_get_height();
787 if (ht > 3)
788 ht -= 2;
789
790 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000791 {
glepnir977561a2025-02-13 20:48:56 +0100792 if (findex == 0)
793 // at the first entry, don't select any entries
794 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100795 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100796 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000797 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100798 else
799 // go up by the pum height
800 findex = MAX(findex - ht, 0);
801 }
802 else // mode == WILD_PAGEDOWN
803 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100804 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100805 // at the last entry, don't select any entries
806 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100807 else if (findex < 0)
808 // no entry is selected, select the first entry
809 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100810 else
811 // go down by the pum height
812 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000813 }
814 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000815
glepnir977561a2025-02-13 20:48:56 +0100816 // Handle wrapping around
817 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100819 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100820 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822 else
glepnir977561a2025-02-13 20:48:56 +0100823 // Wrap around to opposite end
824 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000825 }
glepnir977561a2025-02-13 20:48:56 +0100826
827 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000828 if (compl_match_array)
829 {
830 compl_selected = findex;
831 cmdline_pum_display();
832 }
833 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100834 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
835 cmd_showtail);
836
zeertzjqe9ef3472023-08-17 23:57:05 +0200837 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100838 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100839 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000840}
841
842/*
843 * Start the command-line expansion and get the matches.
844 */
845 static char_u *
846ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
847{
848 int non_suf_match; // number without matching suffix
849 int i;
850 char_u *ss = NULL;
851
852 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000853 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000854 options) == FAIL)
855 {
856#ifdef FNAME_ILLEGAL
857 // Illegal file name has been silently skipped. But when there
858 // are wildcards, the real problem is that there was no match,
859 // causing the pattern to be added, which has illegal characters.
860 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
861 semsg(_(e_no_match_str_2), str);
862#endif
863 }
864 else if (xp->xp_numfiles == 0)
865 {
866 if (!(options & WILD_SILENT))
867 semsg(_(e_no_match_str_2), str);
868 }
869 else
870 {
871 // Escape the matches for use on the command line.
872 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
873
874 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000875 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000876 {
877 if (xp->xp_numfiles)
878 non_suf_match = xp->xp_numfiles;
879 else
880 non_suf_match = 1;
881 if ((xp->xp_context == EXPAND_FILES
882 || xp->xp_context == EXPAND_DIRECTORIES)
883 && xp->xp_numfiles > 1)
884 {
885 // More than one match; check suffix.
886 // The files will have been sorted on matching suffix in
887 // expand_wildcards, only need to check the first two.
888 non_suf_match = 0;
889 for (i = 0; i < 2; ++i)
890 if (match_suffix(xp->xp_files[i]))
891 ++non_suf_match;
892 }
893 if (non_suf_match != 1)
894 {
895 // Can we ever get here unless it's while expanding
896 // interactively? If not, we can get rid of this all
897 // together. Don't really want to wait for this message
898 // (and possibly have to hit return to continue!).
899 if (!(options & WILD_SILENT))
900 emsg(_(e_too_many_file_names));
901 else if (!(options & WILD_NO_BEEP))
902 beep_flush();
903 }
904 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
905 ss = vim_strsave(xp->xp_files[0]);
906 }
907 }
908
909 return ss;
910}
911
912/*
913 * Return the longest common part in the list of cmdline completion matches.
914 */
915 static char_u *
916find_longest_match(expand_T *xp, int options)
917{
918 long_u len;
919 int mb_len = 1;
920 int c0, ci;
921 int i;
922 char_u *ss;
923
924 for (len = 0; xp->xp_files[0][len]; len += mb_len)
925 {
926 if (has_mbyte)
927 {
928 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
929 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
930 }
931 else
932 c0 = xp->xp_files[0][len];
933 for (i = 1; i < xp->xp_numfiles; ++i)
934 {
935 if (has_mbyte)
936 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
937 else
938 ci = xp->xp_files[i][len];
939 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
940 || xp->xp_context == EXPAND_FILES
941 || xp->xp_context == EXPAND_SHELLCMD
942 || xp->xp_context == EXPAND_BUFFERS))
943 {
944 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
945 break;
946 }
947 else if (c0 != ci)
948 break;
949 }
950 if (i < xp->xp_numfiles)
951 {
952 if (!(options & WILD_NO_BEEP))
953 vim_beep(BO_WILD);
954 break;
955 }
956 }
957
958 ss = alloc(len + 1);
959 if (ss)
960 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
961
962 return ss;
963}
964
965/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000966 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200967 * Chars that should not be expanded must be preceded with a backslash.
968 * Return a pointer to allocated memory containing the new string.
969 * Return NULL for failure.
970 *
971 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200972 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
973 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 *
975 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
976 * is WILD_EXPAND_FREE or WILD_ALL.
977 *
978 * mode = WILD_FREE: just free previously expanded matches
979 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
980 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
981 * mode = WILD_NEXT: use next match in multiple match, wrap to first
982 * mode = WILD_PREV: use previous match in multiple match, wrap to first
983 * mode = WILD_ALL: return all matches concatenated
984 * mode = WILD_LONGEST: return longest matched part
985 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000986 * mode = WILD_APPLY: apply the item selected in the cmdline completion
987 * popup menu and close the menu.
988 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
989 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200990 *
991 * options = WILD_LIST_NOTFOUND: list entries without a match
992 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
993 * options = WILD_USE_NL: Use '\n' for WILD_ALL
994 * options = WILD_NO_BEEP: Don't beep for multiple matches
995 * options = WILD_ADD_SLASH: add a slash after directory names
996 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
997 * options = WILD_SILENT: don't print warning messages
998 * options = WILD_ESCAPE: put backslash before special chars
999 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +02001000 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +02001001 *
1002 * The variables xp->xp_context and xp->xp_backslash must have been set!
1003 */
1004 char_u *
1005ExpandOne(
1006 expand_T *xp,
1007 char_u *str,
1008 char_u *orig, // allocated copy of original of expanded string
1009 int options,
1010 int mode)
1011{
1012 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 int orig_saved = FALSE;
1014 int i;
1015 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001016
1017 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001018 if (mode == WILD_NEXT || mode == WILD_PREV
1019 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001020 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001021
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001022 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001023 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001024 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001025 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001026 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001027 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001028
Bram Moolenaar66b51422019-08-18 21:44:12 +02001029 // free old names
1030 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1031 {
1032 FreeWild(xp->xp_numfiles, xp->xp_files);
1033 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001034 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001035
1036 // The entries from xp_files may be used in the PUM, remove it.
1037 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001038 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001039 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001040 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001041
1042 if (mode == WILD_FREE) // only release file name
1043 return NULL;
1044
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001045 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 {
zeertzjq28a23602023-09-29 19:58:35 +02001047 vim_free(xp->xp_orig);
1048 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 orig_saved = TRUE;
1050
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001051 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001052 }
1053
1054 // Find longest common part
1055 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1056 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001057 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001058 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 }
1060
Bram Moolenaar57e95172022-08-20 19:26:14 +01001061 // Concatenate all matching names. Unless interrupted, this can be slow
1062 // and the result probably won't be used.
1063 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 {
1065 len = 0;
1066 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001067 {
1068 if (i > 0)
1069 {
1070 if (xp->xp_prefix == XP_PREFIX_NO)
1071 len += 2; // prefix "no"
1072 else if (xp->xp_prefix == XP_PREFIX_INV)
1073 len += 3; // prefix "inv"
1074 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001076 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077 ss = alloc(len);
1078 if (ss != NULL)
1079 {
1080 *ss = NUL;
1081 for (i = 0; i < xp->xp_numfiles; ++i)
1082 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001083 if (i > 0)
1084 {
1085 if (xp->xp_prefix == XP_PREFIX_NO)
1086 STRCAT(ss, "no");
1087 else if (xp->xp_prefix == XP_PREFIX_INV)
1088 STRCAT(ss, "inv");
1089 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001090 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001091
Bram Moolenaar66b51422019-08-18 21:44:12 +02001092 if (i != xp->xp_numfiles - 1)
1093 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1094 }
1095 }
1096 }
1097
1098 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1099 ExpandCleanup(xp);
1100
zeertzjq28a23602023-09-29 19:58:35 +02001101 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102 if (!orig_saved)
1103 vim_free(orig);
1104
1105 return ss;
1106}
1107
1108/*
1109 * Prepare an expand structure for use.
1110 */
1111 void
1112ExpandInit(expand_T *xp)
1113{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001114 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001115 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001116 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001117 xp->xp_numfiles = -1;
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)
Girish Palya5c3d1e32025-04-22 19:52:16 +02001247 {
1248 vim_free(cmdline_orig);
Girish Palya92f68e22025-04-21 11:12:41 +02001249 cmdline_orig = vim_strnsave(ccline->cmdbuff, ccline->cmdlen);
Girish Palya5c3d1e32025-04-22 19:52:16 +02001250 }
Girish Palya92f68e22025-04-21 11:12:41 +02001251
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 if (xp->xp_numfiles == -1)
1253 {
1254 set_expand_context(xp);
1255 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001256 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 showtail = expand_showtail(xp);
1258 if (i != EXPAND_OK)
1259 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001260 }
1261 else
1262 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001263 numMatches = xp->xp_numfiles;
1264 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001265 showtail = cmd_showtail;
1266 }
1267
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001268 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001269 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001270 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001271
Bram Moolenaar66b51422019-08-18 21:44:12 +02001272 if (!wildmenu)
1273 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 msg_didany = FALSE; // lines_left will be set
1275 msg_start(); // prepare for paging
1276 msg_putchar('\n');
1277 out_flush();
1278 cmdline_row = msg_row;
1279 msg_didany = FALSE; // lines_left will be set again
1280 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001281 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282
1283 if (got_int)
1284 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001285 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001286 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287 else
1288 {
1289 // find the length of the longest file name
1290 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001291 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292 {
1293 if (!showtail && (xp->xp_context == EXPAND_FILES
1294 || xp->xp_context == EXPAND_SHELLCMD
1295 || xp->xp_context == EXPAND_BUFFERS))
1296 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001297 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001298 j = vim_strsize(NameBuff);
1299 }
1300 else
zeertzjqc51a3762022-12-10 10:22:29 +00001301 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001302 if (j > maxlen)
1303 maxlen = j;
1304 }
1305
1306 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001307 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001308 else
1309 {
1310 // compute the number of columns and lines for the listing
1311 maxlen += 2; // two spaces between file names
1312 columns = ((int)Columns + 2) / maxlen;
1313 if (columns < 1)
1314 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001315 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001316 }
1317
1318 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1319
1320 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1321 {
1322 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1323 msg_clr_eos();
1324 msg_advance(maxlen - 3);
1325 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1326 }
1327
1328 // list the files line by line
1329 for (i = 0; i < lines; ++i)
1330 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001331 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001332 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001333 if (got_int)
1334 {
1335 got_int = FALSE;
1336 break;
1337 }
1338 }
1339
1340 // we redraw the command below the lines that we have just listed
1341 // This is a bit tricky, but it saves a lot of screen updating.
1342 cmdline_row = msg_row; // will put it back later
1343 }
1344
1345 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001346 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001347
1348 return EXPAND_OK;
1349}
1350
1351/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001352 * gettail() version for showmatches() and win_redr_status_matches():
1353 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001354 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001355 static char_u *
1356showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001357{
1358 char_u *p;
1359 char_u *t = s;
1360 int had_sep = FALSE;
1361
1362 for (p = s; *p != NUL; )
1363 {
1364 if (vim_ispathsep(*p)
1365#ifdef BACKSLASH_IN_FILENAME
1366 && !rem_backslash(p)
1367#endif
1368 )
1369 had_sep = TRUE;
1370 else if (had_sep)
1371 {
1372 t = p;
1373 had_sep = FALSE;
1374 }
1375 MB_PTR_ADV(p);
1376 }
1377 return t;
1378}
1379
1380/*
1381 * Return TRUE if we only need to show the tail of completion matches.
1382 * When not completing file names or there is a wildcard in the path FALSE is
1383 * returned.
1384 */
1385 static int
1386expand_showtail(expand_T *xp)
1387{
1388 char_u *s;
1389 char_u *end;
1390
1391 // When not completing file names a "/" may mean something different.
1392 if (xp->xp_context != EXPAND_FILES
1393 && xp->xp_context != EXPAND_SHELLCMD
1394 && xp->xp_context != EXPAND_DIRECTORIES)
1395 return FALSE;
1396
1397 end = gettail(xp->xp_pattern);
1398 if (end == xp->xp_pattern) // there is no path separator
1399 return FALSE;
1400
1401 for (s = xp->xp_pattern; s < end; s++)
1402 {
1403 // Skip escaped wildcards. Only when the backslash is not a path
1404 // separator, on DOS the '*' "path\*\file" must not be skipped.
1405 if (rem_backslash(s))
1406 ++s;
1407 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1408 return FALSE;
1409 }
1410 return TRUE;
1411}
1412
1413/*
1414 * Prepare a string for expansion.
1415 * When expanding file names: The string will be used with expand_wildcards().
1416 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1417 * When expanding other names: The string will be used with regcomp(). Copy
1418 * the name into allocated memory and prepend "^".
1419 */
1420 char_u *
1421addstar(
1422 char_u *fname,
1423 int len,
1424 int context) // EXPAND_FILES etc.
1425{
1426 char_u *retval;
1427 int i, j;
1428 int new_len;
1429 char_u *tail;
1430 int ends_in_star;
1431
1432 if (context != EXPAND_FILES
1433 && context != EXPAND_FILES_IN_PATH
1434 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001435 && context != EXPAND_DIRECTORIES
1436 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001437 {
1438 // Matching will be done internally (on something other than files).
1439 // So we convert the file-matching-type wildcards into our kind for
1440 // use with vim_regcomp(). First work out how long it will be:
1441
1442 // For help tags the translation is done in find_help_tags().
1443 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001444 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001445 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001446 || context == EXPAND_COLORS
1447 || context == EXPAND_COMPILER
1448 || context == EXPAND_OWNSYNTAX
1449 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001450 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001451 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001452 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001453 || ((context == EXPAND_TAGS_LISTFILES
1454 || context == EXPAND_TAGS)
1455 && fname[0] == '/'))
1456 retval = vim_strnsave(fname, len);
1457 else
1458 {
1459 new_len = len + 2; // +2 for '^' at start, NUL at end
1460 for (i = 0; i < len; i++)
1461 {
1462 if (fname[i] == '*' || fname[i] == '~')
1463 new_len++; // '*' needs to be replaced by ".*"
1464 // '~' needs to be replaced by "\~"
1465
1466 // Buffer names are like file names. "." should be literal
1467 if (context == EXPAND_BUFFERS && fname[i] == '.')
1468 new_len++; // "." becomes "\."
1469
1470 // Custom expansion takes care of special things, match
1471 // backslashes literally (perhaps also for other types?)
1472 if ((context == EXPAND_USER_DEFINED
1473 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1474 new_len++; // '\' becomes "\\"
1475 }
1476 retval = alloc(new_len);
1477 if (retval != NULL)
1478 {
1479 retval[0] = '^';
1480 j = 1;
1481 for (i = 0; i < len; i++, j++)
1482 {
1483 // Skip backslash. But why? At least keep it for custom
1484 // expansion.
1485 if (context != EXPAND_USER_DEFINED
1486 && context != EXPAND_USER_LIST
1487 && fname[i] == '\\'
1488 && ++i == len)
1489 break;
1490
1491 switch (fname[i])
1492 {
1493 case '*': retval[j++] = '.';
1494 break;
1495 case '~': retval[j++] = '\\';
1496 break;
1497 case '?': retval[j] = '.';
1498 continue;
1499 case '.': if (context == EXPAND_BUFFERS)
1500 retval[j++] = '\\';
1501 break;
1502 case '\\': if (context == EXPAND_USER_DEFINED
1503 || context == EXPAND_USER_LIST)
1504 retval[j++] = '\\';
1505 break;
1506 }
1507 retval[j] = fname[i];
1508 }
1509 retval[j] = NUL;
1510 }
1511 }
1512 }
1513 else
1514 {
1515 retval = alloc(len + 4);
1516 if (retval != NULL)
1517 {
1518 vim_strncpy(retval, fname, len);
1519
1520 // Don't add a star to *, ~, ~user, $var or `cmd`.
1521 // * would become **, which walks the whole tree.
1522 // ~ would be at the start of the file name, but not the tail.
1523 // $ could be anywhere in the tail.
1524 // ` could be anywhere in the file name.
1525 // When the name ends in '$' don't add a star, remove the '$'.
1526 tail = gettail(retval);
1527 ends_in_star = (len > 0 && retval[len - 1] == '*');
1528#ifndef BACKSLASH_IN_FILENAME
1529 for (i = len - 2; i >= 0; --i)
1530 {
1531 if (retval[i] != '\\')
1532 break;
1533 ends_in_star = !ends_in_star;
1534 }
1535#endif
1536 if ((*retval != '~' || tail != retval)
1537 && !ends_in_star
1538 && vim_strchr(tail, '$') == NULL
1539 && vim_strchr(retval, '`') == NULL)
1540 retval[len++] = '*';
1541 else if (len > 0 && retval[len - 1] == '$')
1542 --len;
1543 retval[len] = NUL;
1544 }
1545 }
1546 return retval;
1547}
1548
1549/*
1550 * Must parse the command line so far to work out what context we are in.
1551 * Completion can then be done based on that context.
1552 * This routine sets the variables:
1553 * xp->xp_pattern The start of the pattern to be expanded within
1554 * the command line (ends at the cursor).
1555 * xp->xp_context The type of thing to expand. Will be one of:
1556 *
1557 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1558 * the command line, like an unknown command. Caller
1559 * should beep.
1560 * EXPAND_NOTHING Unrecognised context for completion, use char like
1561 * a normal char, rather than for completion. eg
1562 * :s/^I/
1563 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1564 * it.
1565 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1566 * EXPAND_FILES After command with EX_XFILE set, or after setting
1567 * with P_EXPAND set. eg :e ^I, :w>>^I
1568 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001569 * when we know only directories are of interest.
1570 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001571 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1572 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1573 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1574 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1575 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1576 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1577 * EXPAND_EVENTS Complete event names
1578 * EXPAND_SYNTAX Complete :syntax command arguments
1579 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1580 * EXPAND_AUGROUP Complete autocommand group names
1581 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1582 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1583 * eg :unmap a^I , :cunab x^I
1584 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1585 * eg :call sub^I
1586 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1587 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1588 * names in expressions, eg :while s^I
1589 * EXPAND_ENV_VARS Complete environment variable names
1590 * EXPAND_USER Complete user names
1591 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001592 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001593set_expand_context(expand_T *xp)
1594{
1595 cmdline_info_T *ccline = get_cmdline_info();
1596
1597 // only expansion for ':', '>' and '=' command-lines
1598 if (ccline->cmdfirstc != ':'
1599#ifdef FEAT_EVAL
1600 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1601 && !ccline->input_fn
1602#endif
1603 )
1604 {
1605 xp->xp_context = EXPAND_NOTHING;
1606 return;
1607 }
1608 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1609}
1610
Bram Moolenaard0190392019-08-23 21:17:35 +02001611/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001612 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1613 * For user defined commands, the completion context is set in 'xp' and the
1614 * completion flags in 'complp'.
1615 *
1616 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001617 */
1618 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001619set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001620{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001621 char_u *p = NULL;
1622 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001623 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001624
1625 // Isolate the command and search for it in the command table.
1626 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001627 // - the 'k' command can directly be followed by any character, but do
1628 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1629 // find matches anywhere in the command name, do this only for command
1630 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001631 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001632 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001633 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001634 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001635 p = cmd + 1;
1636 }
1637 else
1638 {
1639 p = cmd;
1640 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1641 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001642 // A user command may contain digits.
1643 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1644 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001645 while (ASCII_ISALNUM(*p) || *p == '*')
1646 ++p;
1647 // for python 3.x: ":py3*" commands completion
1648 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1649 {
1650 ++p;
1651 while (ASCII_ISALPHA(*p) || *p == '*')
1652 ++p;
1653 }
1654 // check for non-alpha command
1655 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1656 ++p;
1657 len = (int)(p - cmd);
1658
1659 if (len == 0)
1660 {
1661 xp->xp_context = EXPAND_UNSUCCESSFUL;
1662 return NULL;
1663 }
1664
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001666
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001667 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001668 // Also when doing fuzzy expansion for non-shell commands, support
1669 // alphanumeric characters.
1670 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1671 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1673 ++p;
1674 }
1675
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001676 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001677 // character, complete the command name.
1678 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1679 return NULL;
1680
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 {
1683 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1684 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001686 p = cmd + 1;
1687 }
1688 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1689 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001690 eap->cmd = cmd;
1691 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001692 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001693 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001694 }
1695 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001696 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001697 {
1698 // Not still touching the command and it was an illegal one
1699 xp->xp_context = EXPAND_UNSUCCESSFUL;
1700 return NULL;
1701 }
1702
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 return p;
1704}
Bram Moolenaard0190392019-08-23 21:17:35 +02001705
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001706/*
1707 * Set the completion context for a command argument with wild card characters.
1708 */
1709 static void
1710set_context_for_wildcard_arg(
1711 exarg_T *eap,
1712 char_u *arg,
1713 int usefilter,
1714 expand_T *xp,
1715 int *complp)
1716{
1717 char_u *p;
1718 int c;
1719 int in_quote = FALSE;
1720 char_u *bow = NULL; // Beginning of word
1721 int len = 0;
1722
1723 // Allow spaces within back-quotes to count as part of the argument
1724 // being expanded.
1725 xp->xp_pattern = skipwhite(arg);
1726 p = xp->xp_pattern;
1727 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001728 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001729 if (has_mbyte)
1730 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001731 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 c = *p;
1733 if (c == '\\' && p[1] != NUL)
1734 ++p;
1735 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001736 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 xp->xp_pattern = p;
1740 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 in_quote = !in_quote;
1743 }
1744 // An argument can contain just about everything, except
1745 // characters that end the command and white space.
1746 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001747#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001748 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001749#endif
1750 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751 {
1752 len = 0; // avoid getting stuck when space is in 'isfname'
1753 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001754 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 if (has_mbyte)
1756 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001757 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001758 c = *p;
1759 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001760 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001761 if (has_mbyte)
1762 len = (*mb_ptr2len)(p);
1763 else
1764 len = 1;
1765 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001766 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001767 if (in_quote)
1768 bow = p;
1769 else
1770 xp->xp_pattern = p;
1771 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001772 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001773 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001774 }
1775
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001776 // If we are still inside the quotes, and we passed a space, just
1777 // expand from there.
1778 if (bow != NULL && in_quote)
1779 xp->xp_pattern = bow;
1780 xp->xp_context = EXPAND_FILES;
1781
1782 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001783 if (usefilter
1784 || (eap != NULL
1785 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1786 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001787 {
1788#ifndef BACKSLASH_IN_FILENAME
1789 xp->xp_shell = TRUE;
1790#endif
1791 // When still after the command name expand executables.
1792 if (xp->xp_pattern == skipwhite(arg))
1793 xp->xp_context = EXPAND_SHELLCMD;
1794 }
1795
1796 // Check for environment variable.
1797 if (*xp->xp_pattern == '$')
1798 {
1799 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1800 if (!vim_isIDc(*p))
1801 break;
1802 if (*p == NUL)
1803 {
1804 xp->xp_context = EXPAND_ENV_VARS;
1805 ++xp->xp_pattern;
1806 // Avoid that the assignment uses EXPAND_FILES again.
1807 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1808 *complp = EXPAND_ENV_VARS;
1809 }
1810 }
1811 // Check for user names.
1812 if (*xp->xp_pattern == '~')
1813 {
1814 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1815 ;
1816 // Complete ~user only if it partially matches a user name.
1817 // A full match ~user<Tab> will be replaced by user's home
1818 // directory i.e. something like ~user<Tab> -> /home/user/
1819 if (*p == NUL && p > xp->xp_pattern + 1
1820 && match_user(xp->xp_pattern + 1) >= 1)
1821 {
1822 xp->xp_context = EXPAND_USER;
1823 ++xp->xp_pattern;
1824 }
1825 }
1826}
1827
1828/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001829 * Set the completion context for the "++opt=arg" argument. Always returns
1830 * NULL.
1831 */
1832 static char_u *
1833set_context_in_argopt(expand_T *xp, char_u *arg)
1834{
1835 char_u *p;
1836
1837 p = vim_strchr(arg, '=');
1838 if (p == NULL)
1839 xp->xp_pattern = arg;
1840 else
1841 xp->xp_pattern = p + 1;
1842
1843 xp->xp_context = EXPAND_ARGOPT;
1844 return NULL;
1845}
1846
1847#ifdef FEAT_TERMINAL
1848/*
1849 * Set the completion context for :terminal's [options]. Always returns NULL.
1850 */
1851 static char_u *
1852set_context_in_terminalopt(expand_T *xp, char_u *arg)
1853{
1854 char_u *p;
1855
1856 p = vim_strchr(arg, '=');
1857 if (p == NULL)
1858 xp->xp_pattern = arg;
1859 else
1860 xp->xp_pattern = p + 1;
1861
1862 xp->xp_context = EXPAND_TERMINALOPT;
1863 return NULL;
1864}
1865#endif
1866
1867/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001868 * Set the completion context for the :filter command. Returns a pointer to the
1869 * next command after the :filter command.
1870 */
1871 static char_u *
1872set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1873{
1874 if (*arg != NUL)
1875 arg = skip_vimgrep_pat(arg, NULL, NULL);
1876 if (arg == NULL || *arg == NUL)
1877 {
1878 xp->xp_context = EXPAND_NOTHING;
1879 return NULL;
1880 }
1881 return skipwhite(arg);
1882}
1883
1884#ifdef FEAT_SEARCH_EXTRA
1885/*
1886 * Set the completion context for the :match command. Returns a pointer to the
1887 * next command after the :match command.
1888 */
1889 static char_u *
1890set_context_in_match_cmd(expand_T *xp, char_u *arg)
1891{
1892 if (*arg == NUL || !ends_excmd(*arg))
1893 {
1894 // also complete "None"
1895 set_context_in_echohl_cmd(xp, arg);
1896 arg = skipwhite(skiptowhite(arg));
1897 if (*arg != NUL)
1898 {
1899 xp->xp_context = EXPAND_NOTHING;
1900 arg = skip_regexp(arg + 1, *arg, magic_isset());
1901 }
1902 }
1903 return find_nextcmd(arg);
1904}
1905#endif
1906
1907/*
1908 * Returns a pointer to the next command after a :global or a :v command.
1909 * Returns NULL if there is no next command.
1910 */
1911 static char_u *
1912find_cmd_after_global_cmd(char_u *arg)
1913{
1914 int delim;
1915
1916 delim = *arg; // get the delimiter
1917 if (delim)
1918 ++arg; // skip delimiter if there is one
1919
1920 while (arg[0] != NUL && arg[0] != delim)
1921 {
1922 if (arg[0] == '\\' && arg[1] != NUL)
1923 ++arg;
1924 ++arg;
1925 }
1926 if (arg[0] != NUL)
1927 return arg + 1;
1928
1929 return NULL;
1930}
1931
1932/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001933 * Returns a pointer to the next command after a :substitute or a :& command.
1934 * Returns NULL if there is no next command.
1935 */
1936 static char_u *
1937find_cmd_after_substitute_cmd(char_u *arg)
1938{
1939 int delim;
1940
1941 delim = *arg;
1942 if (delim)
1943 {
1944 // skip "from" part
1945 ++arg;
1946 arg = skip_regexp(arg, delim, magic_isset());
1947
1948 if (arg[0] != NUL && arg[0] == delim)
1949 {
1950 // skip "to" part
1951 ++arg;
1952 while (arg[0] != NUL && arg[0] != delim)
1953 {
1954 if (arg[0] == '\\' && arg[1] != NUL)
1955 ++arg;
1956 ++arg;
1957 }
1958 if (arg[0] != NUL) // skip delimiter
1959 ++arg;
1960 }
1961 }
1962 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1963 ++arg;
1964 if (arg[0] != NUL)
1965 return arg;
1966
1967 return NULL;
1968}
1969
1970/*
1971 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1972 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1973 * Returns NULL if there is no next command.
1974 */
1975 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001976find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001977{
1978 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001979 if (*arg != '/')
1980 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001981
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001982 // Match regexp, not just whole words
1983 for (++arg; *arg && *arg != '/'; arg++)
1984 if (*arg == '\\' && arg[1] != NUL)
1985 arg++;
1986 if (*arg)
1987 {
1988 arg = skipwhite(arg + 1);
1989
1990 // Check for trailing illegal characters
1991 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1992 xp->xp_context = EXPAND_NOTHING;
1993 else
1994 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001995 }
1996
1997 return NULL;
1998}
1999
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002000#ifdef FEAT_EVAL
2001/*
2002 * Set the completion context for the :unlet command. Always returns NULL.
2003 */
2004 static char_u *
2005set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
2006{
2007 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2008 arg = xp->xp_pattern + 1;
2009
2010 xp->xp_context = EXPAND_USER_VARS;
2011 xp->xp_pattern = arg;
2012
2013 if (*xp->xp_pattern == '$')
2014 {
2015 xp->xp_context = EXPAND_ENV_VARS;
2016 ++xp->xp_pattern;
2017 }
2018
2019 return NULL;
2020}
2021#endif
2022
2023#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2024/*
2025 * Set the completion context for the :language command. Always returns NULL.
2026 */
2027 static char_u *
2028set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2029{
2030 char_u *p;
2031
2032 p = skiptowhite(arg);
2033 if (*p == NUL)
2034 {
2035 xp->xp_context = EXPAND_LANGUAGE;
2036 xp->xp_pattern = arg;
2037 }
2038 else
2039 {
2040 if ( STRNCMP(arg, "messages", p - arg) == 0
2041 || STRNCMP(arg, "ctype", p - arg) == 0
2042 || STRNCMP(arg, "time", p - arg) == 0
2043 || STRNCMP(arg, "collate", p - arg) == 0)
2044 {
2045 xp->xp_context = EXPAND_LOCALES;
2046 xp->xp_pattern = skipwhite(p);
2047 }
2048 else
2049 xp->xp_context = EXPAND_NOTHING;
2050 }
2051
2052 return NULL;
2053}
2054#endif
2055
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002056#ifdef FEAT_EVAL
2057static enum
2058{
2059 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002060 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2061 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002062} breakpt_expand_what;
2063
2064/*
2065 * Set the completion context for the :breakadd command. Always returns NULL.
2066 */
2067 static char_u *
2068set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2069{
2070 char_u *p;
2071 char_u *subcmd_start;
2072
2073 xp->xp_context = EXPAND_BREAKPOINT;
2074 xp->xp_pattern = arg;
2075
2076 if (cmdidx == CMD_breakadd)
2077 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002078 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002079 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002080 else
2081 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002082
2083 p = skipwhite(arg);
2084 if (*p == NUL)
2085 return NULL;
2086 subcmd_start = p;
2087
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002088 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002089 {
2090 // :breakadd file [lnum] <filename>
2091 // :breakadd func [lnum] <funcname>
2092 p += 4;
2093 p = skipwhite(p);
2094
2095 // skip line number (if specified)
2096 if (VIM_ISDIGIT(*p))
2097 {
2098 p = skipdigits(p);
2099 if (*p != ' ')
2100 {
2101 xp->xp_context = EXPAND_NOTHING;
2102 return NULL;
2103 }
2104 p = skipwhite(p);
2105 }
2106 if (STRNCMP("file", subcmd_start, 4) == 0)
2107 xp->xp_context = EXPAND_FILES;
2108 else
2109 xp->xp_context = EXPAND_USER_FUNC;
2110 xp->xp_pattern = p;
2111 }
2112 else if (STRNCMP("expr ", p, 5) == 0)
2113 {
2114 // :breakadd expr <expression>
2115 xp->xp_context = EXPAND_EXPRESSION;
2116 xp->xp_pattern = skipwhite(p + 5);
2117 }
2118
2119 return NULL;
2120}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002121
2122 static char_u *
2123set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2124{
2125 char_u *p;
2126
2127 xp->xp_context = EXPAND_NOTHING;
2128 xp->xp_pattern = NULL;
2129
2130 p = skipwhite(arg);
2131 if (VIM_ISDIGIT(*p))
2132 return NULL;
2133
2134 xp->xp_context = EXPAND_SCRIPTNAMES;
2135 xp->xp_pattern = p;
2136
2137 return NULL;
2138}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002139#endif
2140
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002141/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002142 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2143 * The argument to the command is 'arg' and the argument flags is 'argt'.
2144 * For user-defined commands and for environment variables, 'compl' has the
2145 * completion type.
2146 * Returns a pointer to the next command. Returns NULL if there is no next
2147 * command.
2148 */
2149 static char_u *
2150set_context_by_cmdname(
2151 char_u *cmd,
2152 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002153 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002154 char_u *arg,
2155 long argt,
2156 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002157 int forceit)
2158{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002159 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002160 {
2161 case CMD_find:
2162 case CMD_sfind:
2163 case CMD_tabfind:
2164 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002165 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002166 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002167 break;
2168 case CMD_cd:
2169 case CMD_chdir:
2170 case CMD_tcd:
2171 case CMD_tchdir:
2172 case CMD_lcd:
2173 case CMD_lchdir:
2174 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002175 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002176 break;
2177 case CMD_help:
2178 xp->xp_context = EXPAND_HELP;
2179 xp->xp_pattern = arg;
2180 break;
2181
2182 // Command modifiers: return the argument.
2183 // Also for commands with an argument that is a command.
2184 case CMD_aboveleft:
2185 case CMD_argdo:
2186 case CMD_belowright:
2187 case CMD_botright:
2188 case CMD_browse:
2189 case CMD_bufdo:
2190 case CMD_cdo:
2191 case CMD_cfdo:
2192 case CMD_confirm:
2193 case CMD_debug:
2194 case CMD_folddoclosed:
2195 case CMD_folddoopen:
2196 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002197 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002198 case CMD_keepalt:
2199 case CMD_keepjumps:
2200 case CMD_keepmarks:
2201 case CMD_keeppatterns:
2202 case CMD_ldo:
2203 case CMD_leftabove:
2204 case CMD_lfdo:
2205 case CMD_lockmarks:
2206 case CMD_noautocmd:
2207 case CMD_noswapfile:
2208 case CMD_rightbelow:
2209 case CMD_sandbox:
2210 case CMD_silent:
2211 case CMD_tab:
2212 case CMD_tabdo:
2213 case CMD_topleft:
2214 case CMD_verbose:
2215 case CMD_vertical:
2216 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002217 case CMD_vim9cmd:
2218 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002219 return arg;
2220
2221 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002222 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002223
2224#ifdef FEAT_SEARCH_EXTRA
2225 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002226 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002227#endif
2228
2229 // All completion for the +cmdline_compl feature goes here.
2230
2231 case CMD_command:
2232 return set_context_in_user_cmd(xp, arg);
2233
2234 case CMD_delcommand:
2235 xp->xp_context = EXPAND_USER_COMMANDS;
2236 xp->xp_pattern = arg;
2237 break;
2238
2239 case CMD_global:
2240 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002241 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002242 case CMD_and:
2243 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002244 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 case CMD_isearch:
2246 case CMD_dsearch:
2247 case CMD_ilist:
2248 case CMD_dlist:
2249 case CMD_ijump:
2250 case CMD_psearch:
2251 case CMD_djump:
2252 case CMD_isplit:
2253 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002254 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002255 case CMD_autocmd:
2256 return set_context_in_autocmd(xp, arg, FALSE);
2257 case CMD_doautocmd:
2258 case CMD_doautoall:
2259 return set_context_in_autocmd(xp, arg, TRUE);
2260 case CMD_set:
2261 set_context_in_set_cmd(xp, arg, 0);
2262 break;
2263 case CMD_setglobal:
2264 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2265 break;
2266 case CMD_setlocal:
2267 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2268 break;
2269 case CMD_tag:
2270 case CMD_stag:
2271 case CMD_ptag:
2272 case CMD_ltag:
2273 case CMD_tselect:
2274 case CMD_stselect:
2275 case CMD_ptselect:
2276 case CMD_tjump:
2277 case CMD_stjump:
2278 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002279 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002280 xp->xp_context = EXPAND_TAGS_LISTFILES;
2281 else
2282 xp->xp_context = EXPAND_TAGS;
2283 xp->xp_pattern = arg;
2284 break;
2285 case CMD_augroup:
2286 xp->xp_context = EXPAND_AUGROUP;
2287 xp->xp_pattern = arg;
2288 break;
2289#ifdef FEAT_SYN_HL
2290 case CMD_syntax:
2291 set_context_in_syntax_cmd(xp, arg);
2292 break;
2293#endif
2294#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002295 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002296 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002297 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002298 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002299 case CMD_if:
2300 case CMD_elseif:
2301 case CMD_while:
2302 case CMD_for:
2303 case CMD_echo:
2304 case CMD_echon:
2305 case CMD_execute:
2306 case CMD_echomsg:
2307 case CMD_echoerr:
2308 case CMD_call:
2309 case CMD_return:
2310 case CMD_cexpr:
2311 case CMD_caddexpr:
2312 case CMD_cgetexpr:
2313 case CMD_lexpr:
2314 case CMD_laddexpr:
2315 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002316 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002317 break;
2318
2319 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002320 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002321 case CMD_function:
2322 case CMD_delfunction:
2323 xp->xp_context = EXPAND_USER_FUNC;
2324 xp->xp_pattern = arg;
2325 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002326 case CMD_disassemble:
2327 set_context_in_disassemble_cmd(xp, arg);
2328 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002329
2330 case CMD_echohl:
2331 set_context_in_echohl_cmd(xp, arg);
2332 break;
2333#endif
2334 case CMD_highlight:
2335 set_context_in_highlight_cmd(xp, arg);
2336 break;
2337#ifdef FEAT_CSCOPE
2338 case CMD_cscope:
2339 case CMD_lcscope:
2340 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002341 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002342 break;
2343#endif
2344#ifdef FEAT_SIGNS
2345 case CMD_sign:
2346 set_context_in_sign_cmd(xp, arg);
2347 break;
2348#endif
2349 case CMD_bdelete:
2350 case CMD_bwipeout:
2351 case CMD_bunload:
2352 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2353 arg = xp->xp_pattern + 1;
2354 // FALLTHROUGH
2355 case CMD_buffer:
2356 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002357 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002358 case CMD_checktime:
2359 xp->xp_context = EXPAND_BUFFERS;
2360 xp->xp_pattern = arg;
2361 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002362#ifdef FEAT_DIFF
2363 case CMD_diffget:
2364 case CMD_diffput:
2365 // If current buffer is in diff mode, complete buffer names
2366 // which are in diff mode, and different than current buffer.
2367 xp->xp_context = EXPAND_DIFF_BUFFERS;
2368 xp->xp_pattern = arg;
2369 break;
2370#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002371 case CMD_USER:
2372 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002373 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2374 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002375
2376 case CMD_map: case CMD_noremap:
2377 case CMD_nmap: case CMD_nnoremap:
2378 case CMD_vmap: case CMD_vnoremap:
2379 case CMD_omap: case CMD_onoremap:
2380 case CMD_imap: case CMD_inoremap:
2381 case CMD_cmap: case CMD_cnoremap:
2382 case CMD_lmap: case CMD_lnoremap:
2383 case CMD_smap: case CMD_snoremap:
2384 case CMD_tmap: case CMD_tnoremap:
2385 case CMD_xmap: case CMD_xnoremap:
2386 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002387 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002388 case CMD_unmap:
2389 case CMD_nunmap:
2390 case CMD_vunmap:
2391 case CMD_ounmap:
2392 case CMD_iunmap:
2393 case CMD_cunmap:
2394 case CMD_lunmap:
2395 case CMD_sunmap:
2396 case CMD_tunmap:
2397 case CMD_xunmap:
2398 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002399 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002400 case CMD_mapclear:
2401 case CMD_nmapclear:
2402 case CMD_vmapclear:
2403 case CMD_omapclear:
2404 case CMD_imapclear:
2405 case CMD_cmapclear:
2406 case CMD_lmapclear:
2407 case CMD_smapclear:
2408 case CMD_tmapclear:
2409 case CMD_xmapclear:
2410 xp->xp_context = EXPAND_MAPCLEAR;
2411 xp->xp_pattern = arg;
2412 break;
2413
2414 case CMD_abbreviate: case CMD_noreabbrev:
2415 case CMD_cabbrev: case CMD_cnoreabbrev:
2416 case CMD_iabbrev: case CMD_inoreabbrev:
2417 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002418 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002419 case CMD_unabbreviate:
2420 case CMD_cunabbrev:
2421 case CMD_iunabbrev:
2422 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002423 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002424#ifdef FEAT_MENU
2425 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2426 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2427 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2428 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2429 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2430 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2431 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2432 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2433 case CMD_tmenu: case CMD_tunmenu:
2434 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2435 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2436#endif
2437
2438 case CMD_colorscheme:
2439 xp->xp_context = EXPAND_COLORS;
2440 xp->xp_pattern = arg;
2441 break;
2442
2443 case CMD_compiler:
2444 xp->xp_context = EXPAND_COMPILER;
2445 xp->xp_pattern = arg;
2446 break;
2447
2448 case CMD_ownsyntax:
2449 xp->xp_context = EXPAND_OWNSYNTAX;
2450 xp->xp_pattern = arg;
2451 break;
2452
2453 case CMD_setfiletype:
2454 xp->xp_context = EXPAND_FILETYPE;
2455 xp->xp_pattern = arg;
2456 break;
2457
2458 case CMD_packadd:
2459 xp->xp_context = EXPAND_PACKADD;
2460 xp->xp_pattern = arg;
2461 break;
2462
zeertzjqb0d45ec2023-01-25 15:04:22 +00002463 case CMD_runtime:
2464 set_context_in_runtime_cmd(xp, arg);
2465 break;
2466
Bram Moolenaard0190392019-08-23 21:17:35 +02002467#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2468 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002469 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002470#endif
2471#if defined(FEAT_PROFILE)
2472 case CMD_profile:
2473 set_context_in_profile_cmd(xp, arg);
2474 break;
2475#endif
2476 case CMD_behave:
2477 xp->xp_context = EXPAND_BEHAVE;
2478 xp->xp_pattern = arg;
2479 break;
2480
2481 case CMD_messages:
2482 xp->xp_context = EXPAND_MESSAGES;
2483 xp->xp_pattern = arg;
2484 break;
2485
2486 case CMD_history:
2487 xp->xp_context = EXPAND_HISTORY;
2488 xp->xp_pattern = arg;
2489 break;
2490#if defined(FEAT_PROFILE)
2491 case CMD_syntime:
2492 xp->xp_context = EXPAND_SYNTIME;
2493 xp->xp_pattern = arg;
2494 break;
2495#endif
2496
2497 case CMD_argdelete:
2498 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2499 arg = xp->xp_pattern + 1;
2500 xp->xp_context = EXPAND_ARGLIST;
2501 xp->xp_pattern = arg;
2502 break;
2503
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002504#ifdef FEAT_EVAL
2505 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002506 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002507 case CMD_breakdel:
2508 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002509
2510 case CMD_scriptnames:
2511 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002512#endif
2513
Bram Moolenaard0190392019-08-23 21:17:35 +02002514 default:
2515 break;
2516 }
2517 return NULL;
2518}
2519
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002520/*
2521 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2522 * we don't need/want deleted. Maybe this could be done better if we didn't
2523 * repeat all this stuff. The only problem is that they may not stay
2524 * perfectly compatible with each other, but then the command line syntax
2525 * probably won't change that much -- webb.
2526 */
2527 static char_u *
2528set_one_cmd_context(
2529 expand_T *xp,
2530 char_u *buff) // buffer for command string
2531{
2532 char_u *p;
2533 char_u *cmd, *arg;
2534 int len = 0;
2535 exarg_T ea;
2536 int compl = EXPAND_NOTHING;
2537 int forceit = FALSE;
2538 int usefilter = FALSE; // filter instead of file name
2539
2540 ExpandInit(xp);
2541 xp->xp_pattern = buff;
2542 xp->xp_line = buff;
2543 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2544 ea.argt = 0;
2545
2546 // 1. skip comment lines and leading space, colons or bars
2547 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2548 ;
2549 xp->xp_pattern = cmd;
2550
2551 if (*cmd == NUL)
2552 return NULL;
2553 if (*cmd == '"') // ignore comment lines
2554 {
2555 xp->xp_context = EXPAND_NOTHING;
2556 return NULL;
2557 }
2558
2559 // 3. Skip over the range to find the command.
2560 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2561 xp->xp_pattern = cmd;
2562 if (*cmd == NUL)
2563 return NULL;
2564 if (*cmd == '"')
2565 {
2566 xp->xp_context = EXPAND_NOTHING;
2567 return NULL;
2568 }
2569
2570 if (*cmd == '|' || *cmd == '\n')
2571 return cmd + 1; // There's another command
2572
2573 // Get the command index.
2574 p = set_cmd_index(cmd, &ea, xp, &compl);
2575 if (p == NULL)
2576 return NULL;
2577
2578 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2579
2580 if (*p == '!') // forced commands
2581 {
2582 forceit = TRUE;
2583 ++p;
2584 }
2585
2586 // 6. parse arguments
2587 if (!IS_USER_CMDIDX(ea.cmdidx))
2588 ea.argt = excmd_get_argt(ea.cmdidx);
2589
2590 arg = skipwhite(p);
2591
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002592 // Does command allow "++argopt" argument?
2593 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002594 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002595 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2596 {
2597 p = arg + 2;
2598 while (*p && !vim_isspace(*p))
2599 MB_PTR_ADV(p);
2600
2601 // Still touching the command after "++"?
2602 if (*p == NUL)
2603 {
2604 if (ea.argt & EX_ARGOPT)
2605 return set_context_in_argopt(xp, arg + 2);
2606#ifdef FEAT_TERMINAL
2607 if (ea.cmdidx == CMD_terminal)
2608 return set_context_in_terminalopt(xp, arg + 2);
2609#endif
2610 }
2611
2612 arg = skipwhite(p);
2613 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002614 }
2615
2616 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2617 {
2618 if (*arg == '>') // append
2619 {
2620 if (*++arg == '>')
2621 ++arg;
2622 arg = skipwhite(arg);
2623 }
2624 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2625 {
2626 ++arg;
2627 usefilter = TRUE;
2628 }
2629 }
2630
2631 if (ea.cmdidx == CMD_read)
2632 {
2633 usefilter = forceit; // :r! filter if forced
2634 if (*arg == '!') // :r !filter
2635 {
2636 ++arg;
2637 usefilter = TRUE;
2638 }
2639 }
2640
2641 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2642 {
2643 while (*arg == *cmd) // allow any number of '>' or '<'
2644 ++arg;
2645 arg = skipwhite(arg);
2646 }
2647
2648 // Does command allow "+command"?
2649 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2650 {
2651 // Check if we're in the +command
2652 p = arg + 1;
2653 arg = skip_cmd_arg(arg, FALSE);
2654
2655 // Still touching the command after '+'?
2656 if (*arg == NUL)
2657 return p;
2658
2659 // Skip space(s) after +command to get to the real argument
2660 arg = skipwhite(arg);
2661 }
2662
2663
2664 // Check for '|' to separate commands and '"' to start comments.
2665 // Don't do this for ":read !cmd" and ":write !cmd".
2666 if ((ea.argt & EX_TRLBAR) && !usefilter)
2667 {
2668 p = arg;
2669 // ":redir @" is not the start of a comment
2670 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2671 p += 2;
2672 while (*p)
2673 {
2674 if (*p == Ctrl_V)
2675 {
2676 if (p[1] != NUL)
2677 ++p;
2678 }
2679 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2680 || *p == '|' || *p == '\n')
2681 {
2682 if (*(p - 1) != '\\')
2683 {
2684 if (*p == '|' || *p == '\n')
2685 return p + 1;
2686 return NULL; // It's a comment
2687 }
2688 }
2689 MB_PTR_ADV(p);
2690 }
2691 }
2692
2693 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2694 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2695 // no arguments allowed but there is something
2696 return NULL;
2697
2698 // Find start of last argument (argument just before cursor):
2699 p = buff;
2700 xp->xp_pattern = p;
2701 len = (int)STRLEN(buff);
2702 while (*p && p < buff + len)
2703 {
2704 if (*p == ' ' || *p == TAB)
2705 {
2706 // argument starts after a space
2707 xp->xp_pattern = ++p;
2708 }
2709 else
2710 {
2711 if (*p == '\\' && *(p + 1) != NUL)
2712 ++p; // skip over escaped character
2713 MB_PTR_ADV(p);
2714 }
2715 }
2716
2717 if (ea.argt & EX_XFILE)
2718 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2719
2720 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002721 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002722 forceit);
2723}
2724
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002725/*
2726 * Set the completion context in 'xp' for command 'str'
2727 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 void
2729set_cmd_context(
2730 expand_T *xp,
2731 char_u *str, // start of command line
2732 int len, // length of command line (excl. NUL)
2733 int col, // position of cursor
2734 int use_ccline UNUSED) // use ccline for info
2735{
2736#ifdef FEAT_EVAL
2737 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002738 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002739#endif
2740 int old_char = NUL;
2741 char_u *nextcomm;
2742
2743 // Avoid a UMR warning from Purify, only save the character if it has been
2744 // written before.
2745 if (col < len)
2746 old_char = str[col];
2747 str[col] = NUL;
2748 nextcomm = str;
2749
2750#ifdef FEAT_EVAL
2751 if (use_ccline && ccline->cmdfirstc == '=')
2752 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002753 // pass CMD_SIZE because there is no real command
2754 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002755 }
2756 else if (use_ccline && ccline->input_fn)
2757 {
2758 xp->xp_context = ccline->xp_context;
2759 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002760 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002761 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2762 {
2763 context = xp->xp_context;
2764 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2765 &context);
2766 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002767 }
2768 else
2769#endif
2770 while (nextcomm != NULL)
2771 nextcomm = set_one_cmd_context(xp, nextcomm);
2772
2773 // Store the string here so that call_user_expand_func() can get to them
2774 // easily.
2775 xp->xp_line = str;
2776 xp->xp_col = col;
2777
2778 str[col] = old_char;
2779}
2780
2781/*
2782 * Expand the command line "str" from context "xp".
2783 * "xp" must have been set by set_cmd_context().
2784 * xp->xp_pattern points into "str", to where the text that is to be expanded
2785 * starts.
2786 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2787 * cursor.
2788 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2789 * key that triggered expansion literally.
2790 * Returns EXPAND_OK otherwise.
2791 */
2792 int
2793expand_cmdline(
2794 expand_T *xp,
2795 char_u *str, // start of command line
2796 int col, // position of cursor
2797 int *matchcount, // return: nr of matches
2798 char_u ***matches) // return: array of pointers to matches
2799{
2800 char_u *file_str = NULL;
2801 int options = WILD_ADD_SLASH|WILD_SILENT;
2802
2803 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2804 {
2805 beep_flush();
2806 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2807 }
2808 if (xp->xp_context == EXPAND_NOTHING)
2809 {
2810 // Caller can use the character as a normal char instead
2811 return EXPAND_NOTHING;
2812 }
2813
2814 // add star to file name, or convert to regexp if not exp. files.
2815 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002816 if (cmdline_fuzzy_completion_supported(xp))
2817 // If fuzzy matching, don't modify the search string
2818 file_str = vim_strsave(xp->xp_pattern);
2819 else
2820 {
2821 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2822 if (file_str == NULL)
2823 return EXPAND_UNSUCCESSFUL;
2824 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002825
2826 if (p_wic)
2827 options += WILD_ICASE;
2828
2829 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002830 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002831 {
2832 *matchcount = 0;
2833 *matches = NULL;
2834 }
2835 vim_free(file_str);
2836
2837 return EXPAND_OK;
2838}
2839
Bram Moolenaar66b51422019-08-18 21:44:12 +02002840/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002842 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002843 */
2844 static int
2845expand_files_and_dirs(
2846 expand_T *xp,
2847 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002848 char_u ***matches,
2849 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002850 int flags,
2851 int options)
2852{
2853 int free_pat = FALSE;
2854 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002855 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002856
2857 // for ":set path=" and ":set tags=" halve backslashes for escaped
2858 // space
2859 if (xp->xp_backslash != XP_BS_NONE)
2860 {
2861 free_pat = TRUE;
2862 pat = vim_strsave(pat);
2863 for (i = 0; pat[i]; ++i)
2864 if (pat[i] == '\\')
2865 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002866 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002867 && pat[i + 1] == '\\'
2868 && pat[i + 2] == '\\'
2869 && pat[i + 3] == ' ')
2870 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002871 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872 && pat[i + 1] == ' ')
2873 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002874 else if ((xp->xp_backslash & XP_BS_COMMA)
2875 && pat[i + 1] == '\\'
2876 && pat[i + 2] == ',')
2877 STRMOVE(pat + i, pat + i + 2);
2878#ifdef BACKSLASH_IN_FILENAME
2879 else if ((xp->xp_backslash & XP_BS_COMMA)
2880 && pat[i + 1] == ',')
2881 STRMOVE(pat + i, pat + i + 1);
2882#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002883 }
2884 }
2885
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002886 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002887 {
2888#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002889 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002890#endif
2891 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002892 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002893 {
2894 if (xp->xp_context == EXPAND_FILES)
2895 flags |= EW_FILE;
2896 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2897 flags |= (EW_FILE | EW_PATH);
2898 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2899 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2900 else
2901 flags = (flags | EW_DIR) & ~EW_FILE;
2902 if (options & WILD_ICASE)
2903 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002904
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002905 // Expand wildcards, supporting %:h and the like.
2906 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2907 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002908 if (free_pat)
2909 vim_free(pat);
2910#ifdef BACKSLASH_IN_FILENAME
2911 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2912 {
2913 int j;
2914
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002915 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002916 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002917 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002918
2919 while (*ptr != NUL)
2920 {
2921 if (p_csl[0] == 's' && *ptr == '\\')
2922 *ptr = '/';
2923 else if (p_csl[0] == 'b' && *ptr == '/')
2924 *ptr = '\\';
2925 ptr += (*mb_ptr2len)(ptr);
2926 }
2927 }
2928 }
2929#endif
2930 return ret;
2931}
2932
2933/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002934 * Function given to ExpandGeneric() to obtain the possible arguments of the
2935 * ":behave {mswin,xterm}" command.
2936 */
2937 static char_u *
2938get_behave_arg(expand_T *xp UNUSED, int idx)
2939{
2940 if (idx == 0)
2941 return (char_u *)"mswin";
2942 if (idx == 1)
2943 return (char_u *)"xterm";
2944 return NULL;
2945}
2946
K.Takata161b6ac2022-11-14 15:31:07 +00002947#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002948/*
2949 * Function given to ExpandGeneric() to obtain the possible arguments of the
2950 * ":breakadd {expr, file, func, here}" command.
2951 * ":breakdel {func, file, here}" command.
2952 */
2953 static char_u *
2954get_breakadd_arg(expand_T *xp UNUSED, int idx)
2955{
2956 char *opts[] = {"expr", "file", "func", "here"};
2957
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002958 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002959 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002960 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002961 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2962 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002963 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2964 {
2965 // breakdel {func, file, here}
2966 if (idx <= 2)
2967 return (char_u *)opts[idx + 1];
2968 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002969 else
2970 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002971 // profdel {func, file}
2972 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002973 return (char_u *)opts[idx + 1];
2974 }
2975 }
2976 return NULL;
2977}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002978
2979/*
2980 * Function given to ExpandGeneric() to obtain the possible arguments for the
2981 * ":scriptnames" command.
2982 */
2983 static char_u *
2984get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2985{
2986 scriptitem_T *si;
2987
2988 if (!SCRIPT_ID_VALID(idx + 1))
2989 return NULL;
2990
2991 si = SCRIPT_ITEM(idx + 1);
2992 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2993 return NameBuff;
2994}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002995#endif
2996
Bram Moolenaard0190392019-08-23 21:17:35 +02002997/*
2998 * Function given to ExpandGeneric() to obtain the possible arguments of the
2999 * ":messages {clear}" command.
3000 */
3001 static char_u *
3002get_messages_arg(expand_T *xp UNUSED, int idx)
3003{
3004 if (idx == 0)
3005 return (char_u *)"clear";
3006 return NULL;
3007}
3008
3009 static char_u *
3010get_mapclear_arg(expand_T *xp UNUSED, int idx)
3011{
3012 if (idx == 0)
3013 return (char_u *)"<buffer>";
3014 return NULL;
3015}
3016
3017/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003018 * Do the expansion based on xp->xp_context and 'rmp'.
3019 */
3020 static int
3021ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003022 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003023 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003024 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003025 char_u ***matches,
3026 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003027{
3028 static struct expgen
3029 {
3030 int context;
3031 char_u *((*func)(expand_T *, int));
3032 int ic;
3033 int escaped;
3034 } tab[] =
3035 {
3036 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3037 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3038 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3039 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3040 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3041 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3042 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3043 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3044 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3045 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003046#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003047 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3048 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3049 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3050 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3051 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003052#endif
3053#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003054 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3055 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003056#endif
3057#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003058 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003059#endif
3060#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003061 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003062#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003063 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3064 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3065 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003066#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003067 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003068#endif
3069#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003070 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003071#endif
3072#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003073 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003074#endif
3075#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003076 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3077 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003078#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003079 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3080 {EXPAND_USER, get_users, TRUE, FALSE},
3081 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003082#ifdef FEAT_EVAL
3083 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003084 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003085#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003086 };
3087 int i;
3088 int ret = FAIL;
3089
3090 // Find a context in the table and call the ExpandGeneric() with the
3091 // right function to do the expansion.
3092 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3093 {
3094 if (xp->xp_context == tab[i].context)
3095 {
3096 if (tab[i].ic)
3097 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003098 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3099 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003100 break;
3101 }
3102 }
3103
3104 return ret;
3105}
3106
3107/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003108 * Map wild expand options to flags for expand_wildcards()
3109 */
3110 static int
3111map_wildopts_to_ewflags(int options)
3112{
3113 int flags;
3114
3115 flags = EW_DIR; // include directories
3116 if (options & WILD_LIST_NOTFOUND)
3117 flags |= EW_NOTFOUND;
3118 if (options & WILD_ADD_SLASH)
3119 flags |= EW_ADDSLASH;
3120 if (options & WILD_KEEP_ALL)
3121 flags |= EW_KEEPALL;
3122 if (options & WILD_SILENT)
3123 flags |= EW_SILENT;
3124 if (options & WILD_NOERROR)
3125 flags |= EW_NOERROR;
3126 if (options & WILD_ALLLINKS)
3127 flags |= EW_ALLLINKS;
3128
3129 return flags;
3130}
3131
3132/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 * Do the expansion based on xp->xp_context and "pat".
3134 */
3135 static int
3136ExpandFromContext(
3137 expand_T *xp,
3138 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003139 char_u ***matches,
3140 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141 int options) // WILD_ flags
3142{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003144 int ret;
3145 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003146 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003147 int fuzzy = cmdline_fuzzy_complete(pat)
3148 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003150 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151
3152 if (xp->xp_context == EXPAND_FILES
3153 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003154 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003155 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003156 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003157 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3158 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003160 *matches = (char_u **)"";
3161 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162 if (xp->xp_context == EXPAND_HELP)
3163 {
3164 // With an empty argument we would get all the help tags, which is
3165 // very slow. Get matches for "help" instead.
3166 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 {
3169#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003170 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171#endif
3172 return OK;
3173 }
3174 return FAIL;
3175 }
3176
Bram Moolenaar66b51422019-08-18 21:44:12 +02003177 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003178 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003179 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003180 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003181 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003182 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003183#ifdef FEAT_DIFF
3184 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003185 return ExpandBufnames(pat, numMatches, matches,
3186 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003187#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 if (xp->xp_context == EXPAND_TAGS
3189 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003190 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3191 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192 if (xp->xp_context == EXPAND_COLORS)
3193 {
3194 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003195 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 directories);
3197 }
3198 if (xp->xp_context == EXPAND_COMPILER)
3199 {
3200 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003201 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202 }
3203 if (xp->xp_context == EXPAND_OWNSYNTAX)
3204 {
3205 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003206 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003207 }
3208 if (xp->xp_context == EXPAND_FILETYPE)
3209 {
3210 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003211 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212 }
Doug Kearns81642d92024-01-04 22:37:44 +01003213#ifdef FEAT_KEYMAP
3214 if (xp->xp_context == EXPAND_KEYMAP)
3215 {
3216 char *directories[] = {"keymap", NULL};
3217 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3218 }
3219#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003220#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003221 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003222 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003223#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003225 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003226 if (xp->xp_context == EXPAND_RUNTIME)
3227 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003228
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003229 // When expanding a function name starting with s:, match the <SNR>nr_
3230 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003231 if ((xp->xp_context == EXPAND_USER_FUNC
3232 || xp->xp_context == EXPAND_DISASSEMBLE)
3233 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003234 {
3235 int len = (int)STRLEN(pat) + 20;
3236
3237 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003238 if (tofree == NULL)
3239 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003240 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003241 pat = tofree;
3242 }
3243
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003244 if (!fuzzy)
3245 {
3246 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3247 if (regmatch.regprog == NULL)
3248 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003250 // set ignore-case according to p_ic, p_scs and pat
3251 regmatch.rm_ic = ignorecase(pat);
3252 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253
3254 if (xp->xp_context == EXPAND_SETTINGS
3255 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003256 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003257 else if (xp->xp_context == EXPAND_STRING_SETTING)
3258 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3259 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3260 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003262 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003263 else if (xp->xp_context == EXPAND_ARGOPT)
3264 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003265 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3266 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003267#if defined(FEAT_TERMINAL)
3268 else if (xp->xp_context == EXPAND_TERMINALOPT)
3269 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3270#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003271#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003273 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003274#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003275 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003276 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003278 if (!fuzzy)
3279 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003280 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281
3282 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003283}
3284
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003285 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003287 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003288 expand_T *xp,
3289 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003290 char_u ***matches,
3291 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003292 char_u *((*func)(expand_T *, int)),
3293 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003294 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003295{
Yee Cheng China7b81202025-02-23 09:32:47 +01003296 return ExpandGenericExt(
3297 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3298}
3299
3300/*
3301 * Expand a list of names.
3302 *
3303 * Generic function for command line completion. It calls a function to
3304 * obtain strings, one by one. The strings are matched against a regexp
3305 * program. Matching strings are copied into an array, which is returned.
3306 *
3307 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3308 * is used.
3309 *
3310 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3311 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3312 * sorting.
3313 *
3314 * Returns OK when no problems encountered, FAIL for error (out of memory).
3315 */
3316 int
3317ExpandGenericExt(
3318 char_u *pat,
3319 expand_T *xp,
3320 regmatch_T *regmatch,
3321 char_u ***matches,
3322 int *numMatches,
3323 char_u *((*func)(expand_T *, int)),
3324 // returns a string from the list
3325 int escaped,
3326 int sortStartIdx)
3327{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003328 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003329 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003330 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003331 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003332 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003333 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003334 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003335 int sort_matches = FALSE;
3336 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003337 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003338
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003339 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003340 *matches = NULL;
3341 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003342
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003343 if (!fuzzy)
3344 ga_init2(&ga, sizeof(char *), 30);
3345 else
3346 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3347
3348 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003349 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003350 str = (*func)(xp, i);
3351 if (str == NULL) // end of list
3352 break;
3353 if (*str == NUL) // skip empty strings
3354 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003355
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003356 if (xp->xp_pattern[0] != NUL)
3357 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003358 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003359 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003360 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003362 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003363 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003364 }
3365 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003366 else
3367 match = TRUE;
3368
3369 if (!match)
3370 continue;
3371
3372 if (escaped)
3373 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3374 else
3375 str = vim_strsave(str);
3376 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003377 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003378 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003379 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003380 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003381 return FAIL;
3382 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003383 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003384 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003385 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003386
3387 if (ga_grow(&ga, 1) == FAIL)
3388 {
3389 vim_free(str);
3390 break;
3391 }
3392
3393 if (fuzzy)
3394 {
3395 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3396 fuzmatch->idx = ga.ga_len;
3397 fuzmatch->str = str;
3398 fuzmatch->score = score;
3399 }
3400 else
3401 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3402
K.Takata161b6ac2022-11-14 15:31:07 +00003403#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003404 if (func == get_menu_names)
3405 {
3406 // test for separator added by get_menu_names()
3407 str += STRLEN(str) - 1;
3408 if (*str == '\001')
3409 *str = '.';
3410 }
K.Takata161b6ac2022-11-14 15:31:07 +00003411#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003412
Yee Cheng China7b81202025-02-23 09:32:47 +01003413 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3414 {
3415 // Found first item to start sorting from. This is usually 0.
3416 sortStartMatchIdx = ga.ga_len;
3417 }
3418
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003419 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003420 }
3421
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003422 if (ga.ga_len == 0)
3423 return OK;
3424
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003425 // sort the matches when using regular expression matching and sorting
3426 // applies to the completion context. Menus and scriptnames should be kept
3427 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003428 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003429 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003430 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003431 && xp->xp_context != EXPAND_SCRIPTNAMES
3432 && xp->xp_context != EXPAND_ARGOPT
3433 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003434 sort_matches = TRUE;
3435
3436 // <SNR> functions should be sorted to the end.
3437 if (xp->xp_context == EXPAND_EXPRESSION
3438 || xp->xp_context == EXPAND_FUNCTIONS
3439 || xp->xp_context == EXPAND_USER_FUNC
3440 || xp->xp_context == EXPAND_DISASSEMBLE)
3441 funcsort = TRUE;
3442
3443 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003444 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003445 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003446 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003447 // <SNR> functions should be sorted to the end.
3448 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3449 sort_func_compare);
3450 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003451 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003452 }
3453
3454 if (!fuzzy)
3455 {
3456 *matches = ga.ga_data;
3457 *numMatches = ga.ga_len;
3458 }
3459 else
3460 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003461 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3462 funcsort) == FAIL)
3463 return FAIL;
3464 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465 }
3466
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003467#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003468 // Reset the variables used for special highlight names expansion, so that
3469 // they don't show up when getting normal highlight names by ID.
3470 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003471#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003472
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 return OK;
3474}
3475
3476/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003477 * Expand shell command matches in one directory of $PATH.
3478 */
3479 static void
3480expand_shellcmd_onedir(
3481 char_u *buf,
3482 char_u *s,
3483 size_t l,
3484 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003485 char_u ***matches,
3486 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003487 int flags,
3488 hashtab_T *ht,
3489 garray_T *gap)
3490{
3491 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003492 hash_T hash;
3493 hashitem_T *hi;
3494
3495 vim_strncpy(buf, s, l);
3496 add_pathsep(buf);
3497 l = STRLEN(buf);
3498 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3499
3500 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003501 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003502 if (ret != OK)
3503 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003504
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003505 if (ga_grow(gap, *numMatches) == FAIL)
3506 {
3507 FreeWild(*numMatches, *matches);
3508 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003509 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003510
3511 for (int i = 0; i < *numMatches; ++i)
3512 {
3513 char_u *name = (*matches)[i];
3514
3515 if (STRLEN(name) > l)
3516 {
3517 // Check if this name was already found.
3518 hash = hash_hash(name + l);
3519 hi = hash_lookup(ht, name + l, hash);
3520 if (HASHITEM_EMPTY(hi))
3521 {
3522 // Remove the path that was prepended.
3523 STRMOVE(name, name + l);
3524 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3525 hash_add_item(ht, hi, name, hash);
3526 name = NULL;
3527 }
3528 }
3529 vim_free(name);
3530 }
3531 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003532}
3533
3534/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003535 * Complete a shell command.
3536 * Returns FAIL or OK;
3537 */
3538 static int
3539expand_shellcmd(
3540 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003541 char_u ***matches, // return: array with matches
3542 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543 int flagsarg) // EW_ flags
3544{
3545 char_u *pat;
3546 int i;
3547 char_u *path = NULL;
3548 int mustfree = FALSE;
3549 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003550 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003551 size_t l;
3552 char_u *s, *e;
3553 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003554 int did_curdir = FALSE;
3555 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003557 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003558 if (buf == NULL)
3559 return FAIL;
3560
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003561 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003562 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003563 if (pat == NULL)
3564 {
3565 vim_free(buf);
3566 return FAIL;
3567 }
3568
Bram Moolenaar66b51422019-08-18 21:44:12 +02003569 for (i = 0; pat[i]; ++i)
3570 if (pat[i] == '\\' && pat[i + 1] == ' ')
3571 STRMOVE(pat + i, pat + i + 1);
3572
3573 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3574
3575 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3576 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3577 path = (char_u *)".";
3578 else
3579 {
3580 // For an absolute name we don't use $PATH.
3581 if (!mch_isFullName(pat))
3582 path = vim_getenv((char_u *)"PATH", &mustfree);
3583 if (path == NULL)
3584 path = (char_u *)"";
3585 }
3586
3587 // Go over all directories in $PATH. Expand matches in that directory and
3588 // collect them in "ga". When "." is not in $PATH also expand for the
3589 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003590 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591 hash_init(&found_ht);
3592 for (s = path; ; s = e)
3593 {
K.Takata161b6ac2022-11-14 15:31:07 +00003594#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003595 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003596#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003597 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003598#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003599 if (e == NULL)
3600 e = s + STRLEN(s);
3601
3602 if (*s == NUL)
3603 {
3604 if (did_curdir)
3605 break;
3606 // Find directories in the current directory, path is empty.
3607 did_curdir = TRUE;
3608 flags |= EW_DIR;
3609 }
3610 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3611 {
3612 did_curdir = TRUE;
3613 flags |= EW_DIR;
3614 }
3615 else
3616 // Do not match directories inside a $PATH item.
3617 flags &= ~EW_DIR;
3618
3619 l = e - s;
3620 if (l > MAXPATHL - 5)
3621 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003623 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003624 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003625
Bram Moolenaar66b51422019-08-18 21:44:12 +02003626 if (*e != NUL)
3627 ++e;
3628 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003629 *matches = ga.ga_data;
3630 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003631
3632 vim_free(buf);
3633 vim_free(pat);
3634 if (mustfree)
3635 vim_free(path);
3636 hash_clear(&found_ht);
3637 return OK;
3638}
3639
K.Takata161b6ac2022-11-14 15:31:07 +00003640#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003641/*
3642 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003643 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 */
3645 static void *
3646call_user_expand_func(
3647 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003648 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003649{
3650 cmdline_info_T *ccline = get_cmdline_info();
3651 int keep = 0;
3652 typval_T args[4];
3653 sctx_T save_current_sctx = current_sctx;
3654 char_u *pat = NULL;
3655 void *ret;
3656
3657 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3658 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659
3660 if (ccline->cmdbuff != NULL)
3661 {
3662 keep = ccline->cmdbuff[ccline->cmdlen];
3663 ccline->cmdbuff[ccline->cmdlen] = 0;
3664 }
3665
3666 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3667
3668 args[0].v_type = VAR_STRING;
3669 args[0].vval.v_string = pat;
3670 args[1].v_type = VAR_STRING;
3671 args[1].vval.v_string = xp->xp_line;
3672 args[2].v_type = VAR_NUMBER;
3673 args[2].vval.v_number = xp->xp_col;
3674 args[3].v_type = VAR_UNKNOWN;
3675
3676 current_sctx = xp->xp_script_ctx;
3677
3678 ret = user_expand_func(xp->xp_arg, 3, args);
3679
3680 current_sctx = save_current_sctx;
3681 if (ccline->cmdbuff != NULL)
3682 ccline->cmdbuff[ccline->cmdlen] = keep;
3683
3684 vim_free(pat);
3685 return ret;
3686}
3687
3688/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003689 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3690 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003691 */
3692 static int
3693ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003694 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003695 expand_T *xp,
3696 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003697 char_u ***matches,
3698 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003699{
3700 char_u *retstr;
3701 char_u *s;
3702 char_u *e;
3703 int keep;
3704 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003705 int fuzzy;
3706 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003707 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003708
3709 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003710 *matches = NULL;
3711 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003712
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003713 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003714 if (retstr == NULL)
3715 return FAIL;
3716
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003717 if (!fuzzy)
3718 ga_init2(&ga, sizeof(char *), 3);
3719 else
3720 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3721
Bram Moolenaar66b51422019-08-18 21:44:12 +02003722 for (s = retstr; *s != NUL; s = e)
3723 {
3724 e = vim_strchr(s, '\n');
3725 if (e == NULL)
3726 e = s + STRLEN(s);
3727 keep = *e;
3728 *e = NUL;
3729
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003730 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003731 {
3732 if (!fuzzy)
3733 match = vim_regexec(regmatch, s, (colnr_T)0);
3734 else
3735 {
3736 score = fuzzy_match_str(s, pat);
3737 match = (score != 0);
3738 }
3739 }
3740 else
3741 match = TRUE; // match everything
3742
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 *e = keep;
3744
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003745 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003746 {
3747 if (ga_grow(&ga, 1) == FAIL)
3748 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003749 if (!fuzzy)
3750 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3751 else
3752 {
3753 fuzmatch_str_T *fuzmatch =
3754 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003755 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003756 fuzmatch->str = vim_strnsave(s, e - s);
3757 fuzmatch->score = score;
3758 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003759 ++ga.ga_len;
3760 }
3761
3762 if (*e != NUL)
3763 ++e;
3764 }
3765 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003766
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003767 if (ga.ga_len == 0)
3768 return OK;
3769
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003770 if (!fuzzy)
3771 {
3772 *matches = ga.ga_data;
3773 *numMatches = ga.ga_len;
3774 }
3775 else
3776 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003777 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3778 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003779 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003780 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003781 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782 return OK;
3783}
3784
3785/*
3786 * Expand names with a list returned by a function defined by the user.
3787 */
3788 static int
3789ExpandUserList(
3790 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003791 char_u ***matches,
3792 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003793{
3794 list_T *retlist;
3795 listitem_T *li;
3796 garray_T ga;
3797
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003798 *matches = NULL;
3799 *numMatches = 0;
3800 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003801 if (retlist == NULL)
3802 return FAIL;
3803
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003804 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003806 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 {
3808 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3809 continue; // Skip non-string items and empty strings
3810
3811 if (ga_grow(&ga, 1) == FAIL)
3812 break;
3813
3814 ((char_u **)ga.ga_data)[ga.ga_len] =
3815 vim_strsave(li->li_tv.vval.v_string);
3816 ++ga.ga_len;
3817 }
3818 list_unref(retlist);
3819
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003820 *matches = ga.ga_data;
3821 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003822 return OK;
3823}
K.Takata161b6ac2022-11-14 15:31:07 +00003824#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003825
3826/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003827 * Expand "file" for all comma-separated directories in "path".
3828 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003829 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003830 */
3831 void
3832globpath(
3833 char_u *path,
3834 char_u *file,
3835 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003836 int expand_options,
3837 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003838{
3839 expand_T xpc;
3840 char_u *buf;
3841 int i;
3842 int num_p;
3843 char_u **p;
3844
3845 buf = alloc(MAXPATHL);
3846 if (buf == NULL)
3847 return;
3848
3849 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003850 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003851
3852 // Loop over all entries in {path}.
3853 while (*path != NUL)
3854 {
3855 // Copy one item of the path to buf[] and concatenate the file name.
3856 copy_option_part(&path, buf, MAXPATHL, ",");
3857 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3858 {
K.Takata161b6ac2022-11-14 15:31:07 +00003859#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003860 // Using the platform's path separator (\) makes vim incorrectly
3861 // treat it as an escape character, use '/' instead.
3862 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3863 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003864#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003865 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003866#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003867 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003868 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003869 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3870 {
3871 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3872
3873 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003874 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003875 for (i = 0; i < num_p; ++i)
3876 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003877 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003878 ++ga->ga_len;
3879 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003880 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003881 }
3882 }
3883 }
3884
3885 vim_free(buf);
3886}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003887
Bram Moolenaareadee482020-09-04 15:37:31 +02003888/*
3889 * Translate some keys pressed when 'wildmenu' is used.
3890 */
3891 int
3892wildmenu_translate_key(
3893 cmdline_info_T *cclp,
3894 int key,
3895 expand_T *xp,
3896 int did_wild_list)
3897{
3898 int c = key;
3899
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003900 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003901 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003902 // When the popup menu is used for cmdline completion:
3903 // Up : go to the previous item in the menu
3904 // Down : go to the next item in the menu
3905 // Left : go to the parent directory
3906 // Right: list the files in the selected directory
3907 switch (c)
3908 {
3909 case K_UP: c = K_LEFT; break;
3910 case K_DOWN: c = K_RIGHT; break;
3911 case K_LEFT: c = K_UP; break;
3912 case K_RIGHT: c = K_DOWN; break;
3913 default: break;
3914 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003915 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003916
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003917 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003918 {
3919 if (c == K_LEFT)
3920 c = Ctrl_P;
3921 else if (c == K_RIGHT)
3922 c = Ctrl_N;
3923 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003924
Bram Moolenaareadee482020-09-04 15:37:31 +02003925 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003926 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003927 && cclp->cmdpos > 1
3928 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3929 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3930 && (c == '\n' || c == '\r' || c == K_KENTER))
3931 c = K_DOWN;
3932
3933 return c;
3934}
3935
3936/*
3937 * Delete characters on the command line, from "from" to the current
3938 * position.
3939 */
3940 static void
3941cmdline_del(cmdline_info_T *cclp, int from)
3942{
3943 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3944 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3945 cclp->cmdlen -= cclp->cmdpos - from;
3946 cclp->cmdpos = from;
3947}
3948
3949/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003950 * Handle a key pressed when the wild menu for the menu names
3951 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003952 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003953 static int
3954wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003955{
Bram Moolenaareadee482020-09-04 15:37:31 +02003956 int i;
3957 int j;
3958
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003959 // Hitting <Down> after "emenu Name.": complete submenu
3960 if (key == K_DOWN && cclp->cmdpos > 0
3961 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003962 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003963 key = p_wc;
3964 KeyTyped = TRUE; // in case the key was mapped
3965 }
3966 else if (key == K_UP)
3967 {
3968 // Hitting <Up>: Remove one submenu name in front of the
3969 // cursor
3970 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003971
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003972 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3973 i = 0;
3974 while (--j > 0)
3975 {
3976 // check for start of menu name
3977 if (cclp->cmdbuff[j] == ' '
3978 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003979 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003980 i = j + 1;
3981 break;
3982 }
3983 // check for start of submenu name
3984 if (cclp->cmdbuff[j] == '.'
3985 && cclp->cmdbuff[j - 1] != '\\')
3986 {
3987 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003988 {
3989 i = j + 1;
3990 break;
3991 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003992 else
3993 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003994 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003995 }
3996 if (i > 0)
3997 cmdline_del(cclp, i);
3998 key = p_wc;
3999 KeyTyped = TRUE; // in case the key was mapped
4000 xp->xp_context = EXPAND_NOTHING;
4001 }
4002
4003 return key;
4004}
4005
4006/*
4007 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
4008 * directory names (EXPAND_DIRECTORIES) or shell command names
4009 * (EXPAND_SHELLCMD) is displayed.
4010 */
4011 static int
4012wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4013{
4014 int i;
4015 int j;
4016 char_u upseg[5];
4017
4018 upseg[0] = PATHSEP;
4019 upseg[1] = '.';
4020 upseg[2] = '.';
4021 upseg[3] = PATHSEP;
4022 upseg[4] = NUL;
4023
4024 if (key == K_DOWN
4025 && cclp->cmdpos > 0
4026 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4027 && (cclp->cmdpos < 3
4028 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4029 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4030 {
4031 // go down a directory
4032 key = p_wc;
4033 KeyTyped = TRUE; // in case the key was mapped
4034 }
4035 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4036 {
4037 // If in a direct ancestor, strip off one ../ to go down
4038 int found = FALSE;
4039
4040 j = cclp->cmdpos;
4041 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4042 while (--j > i)
4043 {
4044 if (has_mbyte)
4045 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4046 if (vim_ispathsep(cclp->cmdbuff[j]))
4047 {
4048 found = TRUE;
4049 break;
4050 }
4051 }
4052 if (found
4053 && cclp->cmdbuff[j - 1] == '.'
4054 && cclp->cmdbuff[j - 2] == '.'
4055 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4056 {
4057 cmdline_del(cclp, j - 2);
4058 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004059 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004060 }
4061 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004062 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004063 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004064 // go up a directory
4065 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004066
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004067 j = cclp->cmdpos - 1;
4068 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4069 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004070 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004071 if (has_mbyte)
4072 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4073 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004074#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004075 && vim_strchr((char_u *)" *?[{`$%#",
4076 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004077#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004078 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004079 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004080 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004081 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004082 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004083 break;
4084 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004085 else
4086 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004087 }
4088 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004089
4090 if (!found)
4091 j = i;
4092 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4093 j += 4;
4094 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4095 && j == i)
4096 j += 3;
4097 else
4098 j = 0;
4099 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004100 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004101 // TODO this is only for DOS/UNIX systems - need to put in
4102 // machine-specific stuff here and in upseg init
4103 cmdline_del(cclp, j);
4104 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004105 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004106 else if (cclp->cmdpos > i)
4107 cmdline_del(cclp, i);
4108
4109 // Now complete in the new directory. Set KeyTyped in case the
4110 // Up key came from a mapping.
4111 key = p_wc;
4112 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004113 }
4114
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004115 return key;
4116}
4117
4118/*
4119 * Handle a key pressed when the wild menu is displayed
4120 */
4121 int
4122wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4123{
4124 if (xp->xp_context == EXPAND_MENUNAMES)
4125 return wildmenu_process_key_menunames(cclp, key, xp);
4126 else if ((xp->xp_context == EXPAND_FILES
4127 || xp->xp_context == EXPAND_DIRECTORIES
4128 || xp->xp_context == EXPAND_SHELLCMD))
4129 return wildmenu_process_key_filenames(cclp, key, xp);
4130
4131 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004132}
4133
4134/*
4135 * Free expanded names when finished walking through the matches
4136 */
4137 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004138wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004139{
4140 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004141
4142 if (!p_wmnu || wild_menu_showing == 0)
4143 return;
4144
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004145#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004146 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004147 if (cclp->input_fn)
4148 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004149#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004150
4151 if (wild_menu_showing == WM_SCROLLED)
4152 {
4153 // Entered command line, move it up
4154 cmdline_row--;
4155 redrawcmd();
4156 }
4157 else if (save_p_ls != -1)
4158 {
4159 // restore 'laststatus' and 'winminheight'
4160 p_ls = save_p_ls;
4161 p_wmh = save_p_wmh;
4162 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004163 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004164 redrawcmd();
4165 save_p_ls = -1;
4166 }
4167 else
4168 {
4169 win_redraw_last_status(topframe);
4170 redraw_statuslines();
4171 }
4172 KeyTyped = skt;
4173 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004174#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004175 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004176 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004177#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004178}
Bram Moolenaareadee482020-09-04 15:37:31 +02004179
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004180#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004181/*
4182 * "getcompletion()" function
4183 */
4184 void
4185f_getcompletion(typval_T *argvars, typval_T *rettv)
4186{
4187 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004188 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189 expand_T xpc;
4190 int filtered = FALSE;
4191 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004192 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004193
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004194 if (in_vim9script()
4195 && (check_for_string_arg(argvars, 0) == FAIL
4196 || check_for_string_arg(argvars, 1) == FAIL
4197 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4198 return;
4199
ii144785fe02021-11-21 12:13:56 +00004200 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004201 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004202 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004203 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004204
4205 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004206 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004207
4208 if (p_wic)
4209 options |= WILD_ICASE;
4210
4211 // For filtered results, 'wildignore' is used
4212 if (!filtered)
4213 options |= WILD_KEEP_ALL;
4214
4215 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004216 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004217 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004218 int cmdline_len = (int)STRLEN(pat);
4219 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004220 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004221 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004222 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004223 else
4224 {
ii144785fe02021-11-21 12:13:56 +00004225 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004226 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004227 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004228
4229 xpc.xp_context = cmdcomplete_str_to_type(type);
4230 if (xpc.xp_context == EXPAND_NOTHING)
4231 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004232 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004233 return;
4234 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004235
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004236 if (xpc.xp_context == EXPAND_USER_DEFINED)
4237 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004238 // Must be "custom,funcname" pattern
4239 if (STRNCMP(type, "custom,", 7) != 0)
4240 {
4241 semsg(_(e_invalid_argument_str), type);
4242 return;
4243 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004244
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004245 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004246 }
4247
4248 if (xpc.xp_context == EXPAND_USER_LIST)
4249 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004250 // Must be "customlist,funcname" pattern
4251 if (STRNCMP(type, "customlist,", 11) != 0)
4252 {
4253 semsg(_(e_invalid_argument_str), type);
4254 return;
4255 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004256
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004257 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004258 }
4259
Bram Moolenaar66b51422019-08-18 21:44:12 +02004260# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004261 if (xpc.xp_context == EXPAND_MENUS)
4262 {
4263 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4264 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4265 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004266# endif
4267# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004268 if (xpc.xp_context == EXPAND_CSCOPE)
4269 {
4270 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4271 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4272 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004273# endif
4274# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004275 if (xpc.xp_context == EXPAND_SIGN)
4276 {
4277 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4278 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4279 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004280# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004281 if (xpc.xp_context == EXPAND_RUNTIME)
4282 {
4283 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4284 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4285 }
zeertzjq85f36d62024-10-10 19:14:13 +02004286 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4287 {
4288 int context = EXPAND_SHELLCMDLINE;
4289 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4290 &context);
4291 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4292 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004293 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004294
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004295 if (cmdline_fuzzy_completion_supported(&xpc))
4296 // when fuzzy matching, don't modify the search string
4297 pat = vim_strsave(xpc.xp_pattern);
4298 else
4299 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4300
Bram Moolenaar93a10962022-06-16 11:42:09 +01004301 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004302 {
4303 int i;
4304
4305 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4306
4307 for (i = 0; i < xpc.xp_numfiles; i++)
4308 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4309 }
4310 vim_free(pat);
4311 ExpandCleanup(&xpc);
4312}
Girish Palya92f68e22025-04-21 11:12:41 +02004313
4314/*
4315 * "cmdcomplete_info()" function
4316 */
4317 void
4318f_cmdcomplete_info(typval_T *argvars UNUSED, typval_T *rettv)
4319{
4320 cmdline_info_T *ccline = get_cmdline_info();
4321 dict_T *retdict;
4322 list_T *li;
4323 int idx;
4324 int ret = OK;
4325
4326 if (rettv_dict_alloc(rettv) == FAIL || ccline == NULL
4327 || ccline->xpc == NULL || ccline->xpc->xp_files == NULL)
4328 return;
4329 retdict = rettv->vval.v_dict;
4330 ret = dict_add_string(retdict, "cmdline_orig", cmdline_orig);
4331 if (ret == OK)
4332 ret = dict_add_number(retdict, "pum_visible", pum_visible());
4333 if (ret == OK)
4334 ret = dict_add_number(retdict, "selected", ccline->xpc->xp_selected);
4335 if (ret == OK)
4336 {
4337 li = list_alloc();
4338 if (li == NULL)
4339 return;
4340 ret = dict_add_list(retdict, "matches", li);
4341 for (idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++)
4342 list_append_string(li, ccline->xpc->xp_files[idx], -1);
4343 }
4344}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004345#endif // FEAT_EVAL