blob: d218277338505d81307b75b1b651cc8e8e117abf [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010053 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000054 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010055 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020057 && xp->xp_context != EXPAND_STRING_SETTING
58 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000061 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020063 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_TAGS
65 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_USER_LIST);
67}
68
69/*
70 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000071 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
72 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000073 */
74 int
75cmdline_fuzzy_complete(char_u *fuzzystr)
76{
77 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
78}
79
80/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000081 * sort function for the completion matches.
82 * <SNR> functions should be sorted to the end.
83 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020084 static int
85sort_func_compare(const void *s1, const void *s2)
86{
87 char_u *p1 = *(char_u **)s1;
88 char_u *p2 = *(char_u **)s2;
89
90 if (*p1 != '<' && *p2 == '<') return -1;
91 if (*p1 == '<' && *p2 != '<') return 1;
92 return STRCMP(p1, p2);
93}
Bram Moolenaar66b51422019-08-18 21:44:12 +020094
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000095/*
96 * Escape special characters in the cmdline completion matches.
97 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020098 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000099wildescape(
100 expand_T *xp,
101 char_u *str,
102 int numfiles,
103 char_u **files)
104{
105 char_u *p;
106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
108
109 if (xp->xp_context == EXPAND_FILES
110 || xp->xp_context == EXPAND_FILES_IN_PATH
111 || xp->xp_context == EXPAND_SHELLCMD
112 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200113 || xp->xp_context == EXPAND_DIRECTORIES
114 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000115 {
116 // Insert a backslash into a file name before a space, \, %, #
117 // and wildmatch characters, except '~'.
118 for (int i = 0; i < numfiles; ++i)
119 {
120 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000122 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200123 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
124 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129#if defined(BACKSLASH_IN_FILENAME)
130 p = vim_strsave_escaped(files[i], (char_u *)" ");
131 if (p != NULL)
132 {
133 vim_free(files[i]);
134 files[i] = p;
135 }
136#endif
137 }
138 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200139 else if (xp->xp_backslash & XP_BS_COMMA)
140 {
141 if (vim_strchr(files[i], ',') != NULL)
142 {
143 p = vim_strsave_escaped(files[i], (char_u *)",");
144 if (p != NULL)
145 {
146 vim_free(files[i]);
147 files[i] = p;
148 }
149 }
150 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000151#ifdef BACKSLASH_IN_FILENAME
152 p = vim_strsave_fnameescape(files[i], vse_what);
153#else
154 p = vim_strsave_fnameescape(files[i],
155 xp->xp_shell ? VSE_SHELL : vse_what);
156#endif
157 if (p != NULL)
158 {
159 vim_free(files[i]);
160 files[i] = p;
161 }
162
163 // If 'str' starts with "\~", replace "~" at start of
164 // files[i] with "\~".
165 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
166 escape_fname(&files[i]);
167 }
168 xp->xp_backslash = XP_BS_NONE;
169
170 // If the first file starts with a '+' escape it. Otherwise it
171 // could be seen as "+cmd".
172 if (*files[0] == '+')
173 escape_fname(&files[0]);
174 }
175 else if (xp->xp_context == EXPAND_TAGS)
176 {
177 // Insert a backslash before characters in a tag name that
178 // would terminate the ":tag" command.
179 for (int i = 0; i < numfiles; ++i)
180 {
181 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
182 if (p != NULL)
183 {
184 vim_free(files[i]);
185 files[i] = p;
186 }
187 }
188 }
189}
190
191/*
192 * Escape special characters in the cmdline completion matches.
193 */
194 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200195ExpandEscape(
196 expand_T *xp,
197 char_u *str,
198 int numfiles,
199 char_u **files,
200 int options)
201{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200202 // May change home directory back to "~"
203 if (options & WILD_HOME_REPLACE)
204 tilde_replace(str, numfiles, files);
205
206 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000207 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200208}
209
210/*
211 * Return FAIL if this is not an appropriate context in which to do
212 * completion of anything, return OK if it is (even if there are no matches).
213 * For the caller, this means that the character is just passed through like a
214 * normal character (instead of being expanded). This allows :s/^I^D etc.
215 */
216 int
217nextwild(
218 expand_T *xp,
219 int type,
220 int options, // extra options for ExpandOne()
221 int escape) // if TRUE, escape the returned matches
222{
223 cmdline_info_T *ccline = get_cmdline_info();
224 int i, j;
225 char_u *p1;
226 char_u *p2;
227 int difflen;
228 int v;
229
230 if (xp->xp_numfiles == -1)
231 {
Jim Zhou3255af82025-02-27 19:29:50 +0100232#ifdef FEAT_EVAL
233 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
234 {
235 // Expand commands typed in input() function
236 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
237 }
238 else
239#endif
240 {
241 set_expand_context(xp);
242 }
243 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200244 }
245
246 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
247 {
248 beep_flush();
249 return OK; // Something illegal on command line
250 }
251 if (xp->xp_context == EXPAND_NOTHING)
252 {
253 // Caller can use the character as a normal char instead
254 return FAIL;
255 }
256
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000257 // If cmd_silent is set then don't show the dots, because redrawcmd() below
258 // won't remove them.
259 if (!cmd_silent)
260 {
261 msg_puts("..."); // show that we are busy
262 out_flush();
263 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200264
265 i = (int)(xp->xp_pattern - ccline->cmdbuff);
266 xp->xp_pattern_len = ccline->cmdpos - i;
267
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000268 if (type == WILD_NEXT || type == WILD_PREV
269 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 {
271 // Get next/previous match for a previous expanded pattern.
272 p2 = ExpandOne(xp, NULL, NULL, 0, type);
273 }
274 else
275 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000276 if (cmdline_fuzzy_completion_supported(xp))
277 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200278 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000279 else
280 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
281
Bram Moolenaar66b51422019-08-18 21:44:12 +0200282 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000283 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200284 p2 = NULL;
285 else
286 {
287 int use_options = options |
288 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100289 if (use_options & WILD_KEEP_SOLE_ITEM)
290 use_options &= ~WILD_KEEP_SOLE_ITEM;
291
Bram Moolenaar66b51422019-08-18 21:44:12 +0200292 if (escape)
293 use_options |= WILD_ESCAPE;
294
295 if (p_wic)
296 use_options += WILD_ICASE;
297 p2 = ExpandOne(xp, p1,
298 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
299 use_options, type);
300 vim_free(p1);
301 // longest match: make sure it is not shorter, happens with :help
302 if (p2 != NULL && type == WILD_LONGEST)
303 {
304 for (j = 0; j < xp->xp_pattern_len; ++j)
305 if (ccline->cmdbuff[i + j] == '*'
306 || ccline->cmdbuff[i + j] == '?')
307 break;
308 if ((int)STRLEN(p2) < j)
309 VIM_CLEAR(p2);
310 }
311 }
312 }
313
314 if (p2 != NULL && !got_int)
315 {
316 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
317 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
318 {
319 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
320 xp->xp_pattern = ccline->cmdbuff + i;
321 }
322 else
323 v = OK;
324 if (v == OK)
325 {
326 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
327 &ccline->cmdbuff[ccline->cmdpos],
328 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
329 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
330 ccline->cmdlen += difflen;
331 ccline->cmdpos += difflen;
332 }
333 }
334 vim_free(p2);
335
336 redrawcmd();
337 cursorcmd();
338
339 // When expanding a ":map" command and no matches are found, assume that
340 // the key is supposed to be inserted literally
341 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
342 return FAIL;
343
344 if (xp->xp_numfiles <= 0 && p2 == NULL)
345 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100346 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200347 // free expanded pattern
348 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
349
350 return OK;
351}
352
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000353/*
354 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 */
357 static int
358cmdline_pum_create(
359 cmdline_info_T *ccline,
360 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000361 char_u **matches,
362 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000363 int showtail)
364{
365 int i;
366 int columns;
367
368 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000369 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000370 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000371 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000372 {
zeertzjqc51a3762022-12-10 10:22:29 +0000373 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 compl_match_array[i].pum_info = NULL;
375 compl_match_array[i].pum_extra = NULL;
376 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200377 compl_match_array[i].pum_user_abbr_hlattr = -1;
378 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000379 }
380
381 // Compute the popup menu starting column
382 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
383 columns = vim_strsize(xp->xp_pattern);
384 if (showtail)
385 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000386 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000387 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000388 }
glepnir977561a2025-02-13 20:48:56 +0100389 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000390
391 // no default selection
392 compl_selected = -1;
393
394 cmdline_pum_display();
395
396 return EXPAND_OK;
397}
398
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000399/*
400 * Display the cmdline completion matches in a popup menu
401 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000402 void
403cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404{
405 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
406}
407
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000408/*
409 * Returns TRUE if the cmdline completion popup menu is being displayed.
410 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000411 int
412cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000413{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100414 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000415}
416
417/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000418 * Remove the cmdline completion popup menu (if present), free the list of
419 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000421 void
zeertzjq1830e782025-03-13 20:29:13 +0100422cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000423{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000424 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100425 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100426#ifdef FEAT_EVAL
427 int save_RedrawingDisabled = RedrawingDisabled;
428 if (cclp->input_fn)
429 RedrawingDisabled = 0;
430#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000431
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000432 pum_undisplay();
433 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000434 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000435 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000436 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000437 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100438
439 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
440 // as a side effect.
441 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100442#ifdef FEAT_EVAL
443 if (cclp->input_fn)
444 RedrawingDisabled = save_RedrawingDisabled;
445#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000446}
447
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000448 void
449cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000450{
zeertzjq1830e782025-03-13 20:29:13 +0100451 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000452 wildmenu_cleanup(cclp);
453}
454
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000455/*
456 * Returns the starting column number to use for the cmdline completion popup
457 * menu.
458 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000459 int
460cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000461{
462 return compl_startcol;
463}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000464
Bram Moolenaar66b51422019-08-18 21:44:12 +0200465/*
zeertzjqd8c93402024-06-17 18:25:32 +0200466 * Returns the current cmdline completion pattern.
467 */
468 char_u *
469cmdline_compl_pattern(void)
470{
471 expand_T *xp = get_cmdline_info()->xpc;
472
473 return xp == NULL ? NULL : xp->xp_orig;
474}
475
476/*
477 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
478 */
479 int
480cmdline_compl_is_fuzzy(void)
481{
482 expand_T *xp = get_cmdline_info()->xpc;
483
484 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
485}
486
487/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000488 * Return the number of characters that should be skipped in a status match.
489 * These are backslashes used for escaping. Do show backslashes in help tags.
490 */
491 static int
492skip_status_match_char(expand_T *xp, char_u *s)
493{
494 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
495#ifdef FEAT_MENU
496 || ((xp->xp_context == EXPAND_MENUS
497 || xp->xp_context == EXPAND_MENUNAMES)
498 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
499#endif
500 )
501 {
502#ifndef BACKSLASH_IN_FILENAME
503 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
504 return 2;
505#endif
506 return 1;
507 }
508 return 0;
509}
510
511/*
512 * Get the length of an item as it will be shown in the status line.
513 */
514 static int
515status_match_len(expand_T *xp, char_u *s)
516{
517 int len = 0;
518
519#ifdef FEAT_MENU
520 int emenu = xp->xp_context == EXPAND_MENUS
521 || xp->xp_context == EXPAND_MENUNAMES;
522
523 // Check for menu separators - replace with '|'.
524 if (emenu && menu_is_separator(s))
525 return 1;
526#endif
527
528 while (*s != NUL)
529 {
530 s += skip_status_match_char(xp, s);
531 len += ptr2cells(s);
532 MB_PTR_ADV(s);
533 }
534
535 return len;
536}
537
538/*
539 * Show wildchar matches in the status line.
540 * Show at least the "match" item.
541 * We start at item 'first_match' in the list and show all matches that fit.
542 *
543 * If inversion is possible we use it. Else '=' characters are used.
544 */
545 static void
546win_redr_status_matches(
547 expand_T *xp,
548 int num_matches,
549 char_u **matches, // list of matches
550 int match,
551 int showtail)
552{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000553 int row;
554 char_u *buf;
555 int len;
556 int clen; // length in screen cells
557 int fillchar;
558 int attr;
559 int i;
560 int highlight = TRUE;
561 char_u *selstart = NULL;
562 int selstart_col = 0;
563 char_u *selend = NULL;
564 static int first_match = 0;
565 int add_left = FALSE;
566 char_u *s;
567#ifdef FEAT_MENU
568 int emenu;
569#endif
570 int l;
571
572 if (matches == NULL) // interrupted completion?
573 return;
574
575 if (has_mbyte)
576 buf = alloc(Columns * MB_MAXBYTES + 1);
577 else
578 buf = alloc(Columns + 1);
579 if (buf == NULL)
580 return;
581
582 if (match == -1) // don't show match but original text
583 {
584 match = 0;
585 highlight = FALSE;
586 }
587 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000588 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000589 if (match == 0)
590 first_match = 0;
591 else if (match < first_match)
592 {
593 // jumping left, as far as we can go
594 first_match = match;
595 add_left = TRUE;
596 }
597 else
598 {
599 // check if match fits on the screen
600 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000601 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000602 if (first_match > 0)
603 clen += 2;
604 // jumping right, put match at the left
605 if ((long)clen > Columns)
606 {
607 first_match = match;
608 // if showing the last match, we can add some on the left
609 clen = 2;
610 for (i = match; i < num_matches; ++i)
611 {
zeertzjqc51a3762022-12-10 10:22:29 +0000612 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000613 if ((long)clen >= Columns)
614 break;
615 }
616 if (i == num_matches)
617 add_left = TRUE;
618 }
619 }
620 if (add_left)
621 while (first_match > 0)
622 {
zeertzjqc51a3762022-12-10 10:22:29 +0000623 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000624 if ((long)clen >= Columns)
625 break;
626 --first_match;
627 }
628
629 fillchar = fillchar_status(&attr, curwin);
630
631 if (first_match == 0)
632 {
633 *buf = NUL;
634 len = 0;
635 }
636 else
637 {
638 STRCPY(buf, "< ");
639 len = 2;
640 }
641 clen = len;
642
643 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000644 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000645 {
646 if (i == match)
647 {
648 selstart = buf + len;
649 selstart_col = clen;
650 }
651
zeertzjqc51a3762022-12-10 10:22:29 +0000652 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000653 // Check for menu separators - replace with '|'
654#ifdef FEAT_MENU
655 emenu = (xp->xp_context == EXPAND_MENUS
656 || xp->xp_context == EXPAND_MENUNAMES);
657 if (emenu && menu_is_separator(s))
658 {
659 STRCPY(buf + len, transchar('|'));
660 l = (int)STRLEN(buf + len);
661 len += l;
662 clen += l;
663 }
664 else
665#endif
666 for ( ; *s != NUL; ++s)
667 {
668 s += skip_status_match_char(xp, s);
669 clen += ptr2cells(s);
670 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
671 {
672 STRNCPY(buf + len, s, l);
673 s += l - 1;
674 len += l;
675 }
676 else
677 {
678 STRCPY(buf + len, transchar_byte(*s));
679 len += (int)STRLEN(buf + len);
680 }
681 }
682 if (i == match)
683 selend = buf + len;
684
685 *(buf + len++) = ' ';
686 *(buf + len++) = ' ';
687 clen += 2;
688 if (++i == num_matches)
689 break;
690 }
691
692 if (i != num_matches)
693 {
694 *(buf + len++) = '>';
695 ++clen;
696 }
697
698 buf[len] = NUL;
699
700 row = cmdline_row - 1;
701 if (row >= 0)
702 {
703 if (wild_menu_showing == 0)
704 {
705 if (msg_scrolled > 0)
706 {
707 // Put the wildmenu just above the command line. If there is
708 // no room, scroll the screen one line up.
709 if (cmdline_row == Rows - 1)
710 {
711 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
712 ++msg_scrolled;
713 }
714 else
715 {
716 ++cmdline_row;
717 ++row;
718 }
719 wild_menu_showing = WM_SCROLLED;
720 }
721 else
722 {
723 // Create status line if needed by setting 'laststatus' to 2.
724 // Set 'winminheight' to zero to avoid that the window is
725 // resized.
726 if (lastwin->w_status_height == 0)
727 {
728 save_p_ls = p_ls;
729 save_p_wmh = p_wmh;
730 p_ls = 2;
731 p_wmh = 0;
732 last_status(FALSE);
733 }
734 wild_menu_showing = WM_SHOWN;
735 }
736 }
737
738 screen_puts(buf, row, 0, attr);
739 if (selstart != NULL && highlight)
740 {
741 *selend = NUL;
742 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
743 }
744
745 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
746 }
747
748 win_redraw_last_status(topframe);
749 vim_free(buf);
750}
751
752/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000753 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200754 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000755 */
756 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100757get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000758{
glepnir977561a2025-02-13 20:48:56 +0100759 int findex = xp->xp_selected;
760 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000761
zeertzjqb6c900b2025-02-14 17:59:31 +0100762 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000763 if (xp->xp_numfiles <= 0)
764 return NULL;
765
766 if (mode == WILD_PREV)
767 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100768 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000769 if (findex == -1)
770 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100771 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000772 --findex;
773 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000774 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000775 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100776 // Select the next entry
777 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000778 }
glepnir977561a2025-02-13 20:48:56 +0100779 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000780 {
glepnir977561a2025-02-13 20:48:56 +0100781 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
782 ht = pum_get_height();
783 if (ht > 3)
784 ht -= 2;
785
786 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000787 {
glepnir977561a2025-02-13 20:48:56 +0100788 if (findex == 0)
789 // at the first entry, don't select any entries
790 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100791 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100792 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000793 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100794 else
795 // go up by the pum height
796 findex = MAX(findex - ht, 0);
797 }
798 else // mode == WILD_PAGEDOWN
799 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100800 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100801 // at the last entry, don't select any entries
802 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100803 else if (findex < 0)
804 // no entry is selected, select the first entry
805 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100806 else
807 // go down by the pum height
808 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000809 }
810 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000811
glepnir977561a2025-02-13 20:48:56 +0100812 // Handle wrapping around
813 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000814 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100815 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100816 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000817 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818 else
glepnir977561a2025-02-13 20:48:56 +0100819 // Wrap around to opposite end
820 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821 }
glepnir977561a2025-02-13 20:48:56 +0100822
823 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000824 if (compl_match_array)
825 {
826 compl_selected = findex;
827 cmdline_pum_display();
828 }
829 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100830 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
831 cmd_showtail);
832
zeertzjqe9ef3472023-08-17 23:57:05 +0200833 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100834 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100835 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000836}
837
838/*
839 * Start the command-line expansion and get the matches.
840 */
841 static char_u *
842ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
843{
844 int non_suf_match; // number without matching suffix
845 int i;
846 char_u *ss = NULL;
847
848 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000849 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000850 options) == FAIL)
851 {
852#ifdef FNAME_ILLEGAL
853 // Illegal file name has been silently skipped. But when there
854 // are wildcards, the real problem is that there was no match,
855 // causing the pattern to be added, which has illegal characters.
856 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
857 semsg(_(e_no_match_str_2), str);
858#endif
859 }
860 else if (xp->xp_numfiles == 0)
861 {
862 if (!(options & WILD_SILENT))
863 semsg(_(e_no_match_str_2), str);
864 }
865 else
866 {
867 // Escape the matches for use on the command line.
868 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
869
870 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000871 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000872 {
873 if (xp->xp_numfiles)
874 non_suf_match = xp->xp_numfiles;
875 else
876 non_suf_match = 1;
877 if ((xp->xp_context == EXPAND_FILES
878 || xp->xp_context == EXPAND_DIRECTORIES)
879 && xp->xp_numfiles > 1)
880 {
881 // More than one match; check suffix.
882 // The files will have been sorted on matching suffix in
883 // expand_wildcards, only need to check the first two.
884 non_suf_match = 0;
885 for (i = 0; i < 2; ++i)
886 if (match_suffix(xp->xp_files[i]))
887 ++non_suf_match;
888 }
889 if (non_suf_match != 1)
890 {
891 // Can we ever get here unless it's while expanding
892 // interactively? If not, we can get rid of this all
893 // together. Don't really want to wait for this message
894 // (and possibly have to hit return to continue!).
895 if (!(options & WILD_SILENT))
896 emsg(_(e_too_many_file_names));
897 else if (!(options & WILD_NO_BEEP))
898 beep_flush();
899 }
900 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
901 ss = vim_strsave(xp->xp_files[0]);
902 }
903 }
904
905 return ss;
906}
907
908/*
909 * Return the longest common part in the list of cmdline completion matches.
910 */
911 static char_u *
912find_longest_match(expand_T *xp, int options)
913{
914 long_u len;
915 int mb_len = 1;
916 int c0, ci;
917 int i;
918 char_u *ss;
919
920 for (len = 0; xp->xp_files[0][len]; len += mb_len)
921 {
922 if (has_mbyte)
923 {
924 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
925 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
926 }
927 else
928 c0 = xp->xp_files[0][len];
929 for (i = 1; i < xp->xp_numfiles; ++i)
930 {
931 if (has_mbyte)
932 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
933 else
934 ci = xp->xp_files[i][len];
935 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
936 || xp->xp_context == EXPAND_FILES
937 || xp->xp_context == EXPAND_SHELLCMD
938 || xp->xp_context == EXPAND_BUFFERS))
939 {
940 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
941 break;
942 }
943 else if (c0 != ci)
944 break;
945 }
946 if (i < xp->xp_numfiles)
947 {
948 if (!(options & WILD_NO_BEEP))
949 vim_beep(BO_WILD);
950 break;
951 }
952 }
953
954 ss = alloc(len + 1);
955 if (ss)
956 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
957
958 return ss;
959}
960
961/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000962 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200963 * Chars that should not be expanded must be preceded with a backslash.
964 * Return a pointer to allocated memory containing the new string.
965 * Return NULL for failure.
966 *
967 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200968 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
969 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200970 *
971 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
972 * is WILD_EXPAND_FREE or WILD_ALL.
973 *
974 * mode = WILD_FREE: just free previously expanded matches
975 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
976 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
977 * mode = WILD_NEXT: use next match in multiple match, wrap to first
978 * mode = WILD_PREV: use previous match in multiple match, wrap to first
979 * mode = WILD_ALL: return all matches concatenated
980 * mode = WILD_LONGEST: return longest matched part
981 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000982 * mode = WILD_APPLY: apply the item selected in the cmdline completion
983 * popup menu and close the menu.
984 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
985 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 *
987 * options = WILD_LIST_NOTFOUND: list entries without a match
988 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
989 * options = WILD_USE_NL: Use '\n' for WILD_ALL
990 * options = WILD_NO_BEEP: Don't beep for multiple matches
991 * options = WILD_ADD_SLASH: add a slash after directory names
992 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
993 * options = WILD_SILENT: don't print warning messages
994 * options = WILD_ESCAPE: put backslash before special chars
995 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200996 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 *
998 * The variables xp->xp_context and xp->xp_backslash must have been set!
999 */
1000 char_u *
1001ExpandOne(
1002 expand_T *xp,
1003 char_u *str,
1004 char_u *orig, // allocated copy of original of expanded string
1005 int options,
1006 int mode)
1007{
1008 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001009 int orig_saved = FALSE;
1010 int i;
1011 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001012
1013 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001014 if (mode == WILD_NEXT || mode == WILD_PREV
1015 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001016 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001017
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001018 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001019 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001020 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001021 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001022 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001023 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001024
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025 // free old names
1026 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1027 {
1028 FreeWild(xp->xp_numfiles, xp->xp_files);
1029 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001030 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001031
1032 // The entries from xp_files may be used in the PUM, remove it.
1033 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001034 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001036 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037
1038 if (mode == WILD_FREE) // only release file name
1039 return NULL;
1040
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001041 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001042 {
zeertzjq28a23602023-09-29 19:58:35 +02001043 vim_free(xp->xp_orig);
1044 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001045 orig_saved = TRUE;
1046
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001047 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 }
1049
1050 // Find longest common part
1051 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1052 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001053 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001054 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001055 }
1056
Bram Moolenaar57e95172022-08-20 19:26:14 +01001057 // Concatenate all matching names. Unless interrupted, this can be slow
1058 // and the result probably won't be used.
1059 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 {
1061 len = 0;
1062 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001063 {
1064 if (i > 0)
1065 {
1066 if (xp->xp_prefix == XP_PREFIX_NO)
1067 len += 2; // prefix "no"
1068 else if (xp->xp_prefix == XP_PREFIX_INV)
1069 len += 3; // prefix "inv"
1070 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001071 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001072 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001073 ss = alloc(len);
1074 if (ss != NULL)
1075 {
1076 *ss = NUL;
1077 for (i = 0; i < xp->xp_numfiles; ++i)
1078 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001079 if (i > 0)
1080 {
1081 if (xp->xp_prefix == XP_PREFIX_NO)
1082 STRCAT(ss, "no");
1083 else if (xp->xp_prefix == XP_PREFIX_INV)
1084 STRCAT(ss, "inv");
1085 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001086 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001087
Bram Moolenaar66b51422019-08-18 21:44:12 +02001088 if (i != xp->xp_numfiles - 1)
1089 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1090 }
1091 }
1092 }
1093
1094 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1095 ExpandCleanup(xp);
1096
zeertzjq28a23602023-09-29 19:58:35 +02001097 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001098 if (!orig_saved)
1099 vim_free(orig);
1100
1101 return ss;
1102}
1103
1104/*
1105 * Prepare an expand structure for use.
1106 */
1107 void
1108ExpandInit(expand_T *xp)
1109{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001110 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001111 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001112 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001113 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001114}
1115
1116/*
1117 * Cleanup an expand structure after use.
1118 */
1119 void
1120ExpandCleanup(expand_T *xp)
1121{
1122 if (xp->xp_numfiles >= 0)
1123 {
1124 FreeWild(xp->xp_numfiles, xp->xp_files);
1125 xp->xp_numfiles = -1;
1126 }
zeertzjq28a23602023-09-29 19:58:35 +02001127 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001128}
1129
1130/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001131 * Display one line of completion matches. Multiple matches are displayed in
1132 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001133 * matches - list of completion match names
1134 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001135 * lines - number of output lines
1136 * linenr - line number of matches to display
1137 * maxlen - maximum number of characters in each line
1138 * showtail - display only the tail of the full path of a file name
1139 * dir_attr - highlight attribute to use for directory names
1140 */
1141 static void
1142showmatches_oneline(
1143 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001144 char_u **matches,
1145 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001146 int lines,
1147 int linenr,
1148 int maxlen,
1149 int showtail,
1150 int dir_attr)
1151{
1152 int i, j;
1153 int isdir;
1154 int lastlen;
1155 char_u *p;
1156
1157 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001158 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001159 {
1160 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1161 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001162 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1163 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001164 msg_advance(maxlen + 1);
1165 msg_puts((char *)p);
1166 msg_advance(maxlen + 3);
1167 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1168 break;
1169 }
1170 for (i = maxlen - lastlen; --i >= 0; )
1171 msg_putchar(' ');
1172 if (xp->xp_context == EXPAND_FILES
1173 || xp->xp_context == EXPAND_SHELLCMD
1174 || xp->xp_context == EXPAND_BUFFERS)
1175 {
1176 // highlight directories
1177 if (xp->xp_numfiles != -1)
1178 {
1179 char_u *halved_slash;
1180 char_u *exp_path;
1181 char_u *path;
1182
1183 // Expansion was done before and special characters
1184 // were escaped, need to halve backslashes. Also
1185 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001186 exp_path = expand_env_save_opt(matches[j], TRUE);
1187 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001188 halved_slash = backslash_halve_save(path);
1189 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001190 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001191 vim_free(exp_path);
1192 if (halved_slash != path)
1193 vim_free(halved_slash);
1194 }
1195 else
1196 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001197 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001198 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001199 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001200 else
1201 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001202 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001203 TRUE);
1204 p = NameBuff;
1205 }
1206 }
1207 else
1208 {
1209 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001210 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001211 }
1212 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1213 }
1214 if (msg_col > 0) // when not wrapped around
1215 {
1216 msg_clr_eos();
1217 msg_putchar('\n');
1218 }
1219 out_flush(); // show one line at a time
1220}
1221
1222/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 * Show all matches for completion on the command line.
1224 * Returns EXPAND_NOTHING when the character that triggered expansion should
1225 * be inserted like a normal character.
1226 */
1227 int
1228showmatches(expand_T *xp, int wildmenu UNUSED)
1229{
1230 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001231 int numMatches;
1232 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001233 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 int maxlen;
1235 int lines;
1236 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 int attr;
1238 int showtail;
1239
1240 if (xp->xp_numfiles == -1)
1241 {
1242 set_expand_context(xp);
1243 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001244 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001245 showtail = expand_showtail(xp);
1246 if (i != EXPAND_OK)
1247 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 }
1249 else
1250 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001251 numMatches = xp->xp_numfiles;
1252 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001253 showtail = cmd_showtail;
1254 }
1255
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001256 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001257 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001258 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001259
Bram Moolenaar66b51422019-08-18 21:44:12 +02001260 if (!wildmenu)
1261 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 msg_didany = FALSE; // lines_left will be set
1263 msg_start(); // prepare for paging
1264 msg_putchar('\n');
1265 out_flush();
1266 cmdline_row = msg_row;
1267 msg_didany = FALSE; // lines_left will be set again
1268 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270
1271 if (got_int)
1272 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001273 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001274 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 else
1276 {
1277 // find the length of the longest file name
1278 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001279 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001280 {
1281 if (!showtail && (xp->xp_context == EXPAND_FILES
1282 || xp->xp_context == EXPAND_SHELLCMD
1283 || xp->xp_context == EXPAND_BUFFERS))
1284 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001285 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001286 j = vim_strsize(NameBuff);
1287 }
1288 else
zeertzjqc51a3762022-12-10 10:22:29 +00001289 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001290 if (j > maxlen)
1291 maxlen = j;
1292 }
1293
1294 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001295 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001296 else
1297 {
1298 // compute the number of columns and lines for the listing
1299 maxlen += 2; // two spaces between file names
1300 columns = ((int)Columns + 2) / maxlen;
1301 if (columns < 1)
1302 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001303 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001304 }
1305
1306 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1307
1308 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1309 {
1310 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1311 msg_clr_eos();
1312 msg_advance(maxlen - 3);
1313 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1314 }
1315
1316 // list the files line by line
1317 for (i = 0; i < lines; ++i)
1318 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001319 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001320 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001321 if (got_int)
1322 {
1323 got_int = FALSE;
1324 break;
1325 }
1326 }
1327
1328 // we redraw the command below the lines that we have just listed
1329 // This is a bit tricky, but it saves a lot of screen updating.
1330 cmdline_row = msg_row; // will put it back later
1331 }
1332
1333 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001334 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001335
1336 return EXPAND_OK;
1337}
1338
1339/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001340 * gettail() version for showmatches() and win_redr_status_matches():
1341 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001342 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001343 static char_u *
1344showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001345{
1346 char_u *p;
1347 char_u *t = s;
1348 int had_sep = FALSE;
1349
1350 for (p = s; *p != NUL; )
1351 {
1352 if (vim_ispathsep(*p)
1353#ifdef BACKSLASH_IN_FILENAME
1354 && !rem_backslash(p)
1355#endif
1356 )
1357 had_sep = TRUE;
1358 else if (had_sep)
1359 {
1360 t = p;
1361 had_sep = FALSE;
1362 }
1363 MB_PTR_ADV(p);
1364 }
1365 return t;
1366}
1367
1368/*
1369 * Return TRUE if we only need to show the tail of completion matches.
1370 * When not completing file names or there is a wildcard in the path FALSE is
1371 * returned.
1372 */
1373 static int
1374expand_showtail(expand_T *xp)
1375{
1376 char_u *s;
1377 char_u *end;
1378
1379 // When not completing file names a "/" may mean something different.
1380 if (xp->xp_context != EXPAND_FILES
1381 && xp->xp_context != EXPAND_SHELLCMD
1382 && xp->xp_context != EXPAND_DIRECTORIES)
1383 return FALSE;
1384
1385 end = gettail(xp->xp_pattern);
1386 if (end == xp->xp_pattern) // there is no path separator
1387 return FALSE;
1388
1389 for (s = xp->xp_pattern; s < end; s++)
1390 {
1391 // Skip escaped wildcards. Only when the backslash is not a path
1392 // separator, on DOS the '*' "path\*\file" must not be skipped.
1393 if (rem_backslash(s))
1394 ++s;
1395 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1396 return FALSE;
1397 }
1398 return TRUE;
1399}
1400
1401/*
1402 * Prepare a string for expansion.
1403 * When expanding file names: The string will be used with expand_wildcards().
1404 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1405 * When expanding other names: The string will be used with regcomp(). Copy
1406 * the name into allocated memory and prepend "^".
1407 */
1408 char_u *
1409addstar(
1410 char_u *fname,
1411 int len,
1412 int context) // EXPAND_FILES etc.
1413{
1414 char_u *retval;
1415 int i, j;
1416 int new_len;
1417 char_u *tail;
1418 int ends_in_star;
1419
1420 if (context != EXPAND_FILES
1421 && context != EXPAND_FILES_IN_PATH
1422 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001423 && context != EXPAND_DIRECTORIES
1424 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001425 {
1426 // Matching will be done internally (on something other than files).
1427 // So we convert the file-matching-type wildcards into our kind for
1428 // use with vim_regcomp(). First work out how long it will be:
1429
1430 // For help tags the translation is done in find_help_tags().
1431 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001432 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001433 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001434 || context == EXPAND_COLORS
1435 || context == EXPAND_COMPILER
1436 || context == EXPAND_OWNSYNTAX
1437 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001438 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001439 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001440 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001441 || ((context == EXPAND_TAGS_LISTFILES
1442 || context == EXPAND_TAGS)
1443 && fname[0] == '/'))
1444 retval = vim_strnsave(fname, len);
1445 else
1446 {
1447 new_len = len + 2; // +2 for '^' at start, NUL at end
1448 for (i = 0; i < len; i++)
1449 {
1450 if (fname[i] == '*' || fname[i] == '~')
1451 new_len++; // '*' needs to be replaced by ".*"
1452 // '~' needs to be replaced by "\~"
1453
1454 // Buffer names are like file names. "." should be literal
1455 if (context == EXPAND_BUFFERS && fname[i] == '.')
1456 new_len++; // "." becomes "\."
1457
1458 // Custom expansion takes care of special things, match
1459 // backslashes literally (perhaps also for other types?)
1460 if ((context == EXPAND_USER_DEFINED
1461 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1462 new_len++; // '\' becomes "\\"
1463 }
1464 retval = alloc(new_len);
1465 if (retval != NULL)
1466 {
1467 retval[0] = '^';
1468 j = 1;
1469 for (i = 0; i < len; i++, j++)
1470 {
1471 // Skip backslash. But why? At least keep it for custom
1472 // expansion.
1473 if (context != EXPAND_USER_DEFINED
1474 && context != EXPAND_USER_LIST
1475 && fname[i] == '\\'
1476 && ++i == len)
1477 break;
1478
1479 switch (fname[i])
1480 {
1481 case '*': retval[j++] = '.';
1482 break;
1483 case '~': retval[j++] = '\\';
1484 break;
1485 case '?': retval[j] = '.';
1486 continue;
1487 case '.': if (context == EXPAND_BUFFERS)
1488 retval[j++] = '\\';
1489 break;
1490 case '\\': if (context == EXPAND_USER_DEFINED
1491 || context == EXPAND_USER_LIST)
1492 retval[j++] = '\\';
1493 break;
1494 }
1495 retval[j] = fname[i];
1496 }
1497 retval[j] = NUL;
1498 }
1499 }
1500 }
1501 else
1502 {
1503 retval = alloc(len + 4);
1504 if (retval != NULL)
1505 {
1506 vim_strncpy(retval, fname, len);
1507
1508 // Don't add a star to *, ~, ~user, $var or `cmd`.
1509 // * would become **, which walks the whole tree.
1510 // ~ would be at the start of the file name, but not the tail.
1511 // $ could be anywhere in the tail.
1512 // ` could be anywhere in the file name.
1513 // When the name ends in '$' don't add a star, remove the '$'.
1514 tail = gettail(retval);
1515 ends_in_star = (len > 0 && retval[len - 1] == '*');
1516#ifndef BACKSLASH_IN_FILENAME
1517 for (i = len - 2; i >= 0; --i)
1518 {
1519 if (retval[i] != '\\')
1520 break;
1521 ends_in_star = !ends_in_star;
1522 }
1523#endif
1524 if ((*retval != '~' || tail != retval)
1525 && !ends_in_star
1526 && vim_strchr(tail, '$') == NULL
1527 && vim_strchr(retval, '`') == NULL)
1528 retval[len++] = '*';
1529 else if (len > 0 && retval[len - 1] == '$')
1530 --len;
1531 retval[len] = NUL;
1532 }
1533 }
1534 return retval;
1535}
1536
1537/*
1538 * Must parse the command line so far to work out what context we are in.
1539 * Completion can then be done based on that context.
1540 * This routine sets the variables:
1541 * xp->xp_pattern The start of the pattern to be expanded within
1542 * the command line (ends at the cursor).
1543 * xp->xp_context The type of thing to expand. Will be one of:
1544 *
1545 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1546 * the command line, like an unknown command. Caller
1547 * should beep.
1548 * EXPAND_NOTHING Unrecognised context for completion, use char like
1549 * a normal char, rather than for completion. eg
1550 * :s/^I/
1551 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1552 * it.
1553 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1554 * EXPAND_FILES After command with EX_XFILE set, or after setting
1555 * with P_EXPAND set. eg :e ^I, :w>>^I
1556 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001557 * when we know only directories are of interest.
1558 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001559 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1560 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1561 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1562 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1563 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1564 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1565 * EXPAND_EVENTS Complete event names
1566 * EXPAND_SYNTAX Complete :syntax command arguments
1567 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1568 * EXPAND_AUGROUP Complete autocommand group names
1569 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1570 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1571 * eg :unmap a^I , :cunab x^I
1572 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1573 * eg :call sub^I
1574 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1575 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1576 * names in expressions, eg :while s^I
1577 * EXPAND_ENV_VARS Complete environment variable names
1578 * EXPAND_USER Complete user names
1579 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001580 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001581set_expand_context(expand_T *xp)
1582{
1583 cmdline_info_T *ccline = get_cmdline_info();
1584
1585 // only expansion for ':', '>' and '=' command-lines
1586 if (ccline->cmdfirstc != ':'
1587#ifdef FEAT_EVAL
1588 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1589 && !ccline->input_fn
1590#endif
1591 )
1592 {
1593 xp->xp_context = EXPAND_NOTHING;
1594 return;
1595 }
1596 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1597}
1598
Bram Moolenaard0190392019-08-23 21:17:35 +02001599/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001600 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1601 * For user defined commands, the completion context is set in 'xp' and the
1602 * completion flags in 'complp'.
1603 *
1604 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001605 */
1606 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001607set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001608{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001609 char_u *p = NULL;
1610 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001611 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001612
1613 // Isolate the command and search for it in the command table.
1614 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001615 // - the 'k' command can directly be followed by any character, but do
1616 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1617 // find matches anywhere in the command name, do this only for command
1618 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001619 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001620 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001622 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001623 p = cmd + 1;
1624 }
1625 else
1626 {
1627 p = cmd;
1628 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1629 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001630 // A user command may contain digits.
1631 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1632 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001633 while (ASCII_ISALNUM(*p) || *p == '*')
1634 ++p;
1635 // for python 3.x: ":py3*" commands completion
1636 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1637 {
1638 ++p;
1639 while (ASCII_ISALPHA(*p) || *p == '*')
1640 ++p;
1641 }
1642 // check for non-alpha command
1643 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1644 ++p;
1645 len = (int)(p - cmd);
1646
1647 if (len == 0)
1648 {
1649 xp->xp_context = EXPAND_UNSUCCESSFUL;
1650 return NULL;
1651 }
1652
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001653 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001654
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001655 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001656 // Also when doing fuzzy expansion for non-shell commands, support
1657 // alphanumeric characters.
1658 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1659 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001660 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1661 ++p;
1662 }
1663
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001664 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001665 // character, complete the command name.
1666 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1667 return NULL;
1668
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 {
1671 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1672 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001673 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001674 p = cmd + 1;
1675 }
1676 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1677 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 eap->cmd = cmd;
1679 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001680 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001682 }
1683 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001685 {
1686 // Not still touching the command and it was an illegal one
1687 xp->xp_context = EXPAND_UNSUCCESSFUL;
1688 return NULL;
1689 }
1690
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001691 return p;
1692}
Bram Moolenaard0190392019-08-23 21:17:35 +02001693
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694/*
1695 * Set the completion context for a command argument with wild card characters.
1696 */
1697 static void
1698set_context_for_wildcard_arg(
1699 exarg_T *eap,
1700 char_u *arg,
1701 int usefilter,
1702 expand_T *xp,
1703 int *complp)
1704{
1705 char_u *p;
1706 int c;
1707 int in_quote = FALSE;
1708 char_u *bow = NULL; // Beginning of word
1709 int len = 0;
1710
1711 // Allow spaces within back-quotes to count as part of the argument
1712 // being expanded.
1713 xp->xp_pattern = skipwhite(arg);
1714 p = xp->xp_pattern;
1715 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001716 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001717 if (has_mbyte)
1718 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001719 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001720 c = *p;
1721 if (c == '\\' && p[1] != NUL)
1722 ++p;
1723 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001724 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001725 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001726 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 xp->xp_pattern = p;
1728 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001729 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 in_quote = !in_quote;
1731 }
1732 // An argument can contain just about everything, except
1733 // characters that end the command and white space.
1734 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001735#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001736 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001737#endif
1738 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 {
1740 len = 0; // avoid getting stuck when space is in 'isfname'
1741 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 if (has_mbyte)
1744 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001745 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001746 c = *p;
1747 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 if (has_mbyte)
1750 len = (*mb_ptr2len)(p);
1751 else
1752 len = 1;
1753 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001754 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001755 if (in_quote)
1756 bow = p;
1757 else
1758 xp->xp_pattern = p;
1759 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001760 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001761 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001762 }
1763
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001764 // If we are still inside the quotes, and we passed a space, just
1765 // expand from there.
1766 if (bow != NULL && in_quote)
1767 xp->xp_pattern = bow;
1768 xp->xp_context = EXPAND_FILES;
1769
1770 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001771 if (usefilter
1772 || (eap != NULL
1773 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1774 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001775 {
1776#ifndef BACKSLASH_IN_FILENAME
1777 xp->xp_shell = TRUE;
1778#endif
1779 // When still after the command name expand executables.
1780 if (xp->xp_pattern == skipwhite(arg))
1781 xp->xp_context = EXPAND_SHELLCMD;
1782 }
1783
1784 // Check for environment variable.
1785 if (*xp->xp_pattern == '$')
1786 {
1787 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1788 if (!vim_isIDc(*p))
1789 break;
1790 if (*p == NUL)
1791 {
1792 xp->xp_context = EXPAND_ENV_VARS;
1793 ++xp->xp_pattern;
1794 // Avoid that the assignment uses EXPAND_FILES again.
1795 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1796 *complp = EXPAND_ENV_VARS;
1797 }
1798 }
1799 // Check for user names.
1800 if (*xp->xp_pattern == '~')
1801 {
1802 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1803 ;
1804 // Complete ~user only if it partially matches a user name.
1805 // A full match ~user<Tab> will be replaced by user's home
1806 // directory i.e. something like ~user<Tab> -> /home/user/
1807 if (*p == NUL && p > xp->xp_pattern + 1
1808 && match_user(xp->xp_pattern + 1) >= 1)
1809 {
1810 xp->xp_context = EXPAND_USER;
1811 ++xp->xp_pattern;
1812 }
1813 }
1814}
1815
1816/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001817 * Set the completion context for the "++opt=arg" argument. Always returns
1818 * NULL.
1819 */
1820 static char_u *
1821set_context_in_argopt(expand_T *xp, char_u *arg)
1822{
1823 char_u *p;
1824
1825 p = vim_strchr(arg, '=');
1826 if (p == NULL)
1827 xp->xp_pattern = arg;
1828 else
1829 xp->xp_pattern = p + 1;
1830
1831 xp->xp_context = EXPAND_ARGOPT;
1832 return NULL;
1833}
1834
1835#ifdef FEAT_TERMINAL
1836/*
1837 * Set the completion context for :terminal's [options]. Always returns NULL.
1838 */
1839 static char_u *
1840set_context_in_terminalopt(expand_T *xp, char_u *arg)
1841{
1842 char_u *p;
1843
1844 p = vim_strchr(arg, '=');
1845 if (p == NULL)
1846 xp->xp_pattern = arg;
1847 else
1848 xp->xp_pattern = p + 1;
1849
1850 xp->xp_context = EXPAND_TERMINALOPT;
1851 return NULL;
1852}
1853#endif
1854
1855/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001856 * Set the completion context for the :filter command. Returns a pointer to the
1857 * next command after the :filter command.
1858 */
1859 static char_u *
1860set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1861{
1862 if (*arg != NUL)
1863 arg = skip_vimgrep_pat(arg, NULL, NULL);
1864 if (arg == NULL || *arg == NUL)
1865 {
1866 xp->xp_context = EXPAND_NOTHING;
1867 return NULL;
1868 }
1869 return skipwhite(arg);
1870}
1871
1872#ifdef FEAT_SEARCH_EXTRA
1873/*
1874 * Set the completion context for the :match command. Returns a pointer to the
1875 * next command after the :match command.
1876 */
1877 static char_u *
1878set_context_in_match_cmd(expand_T *xp, char_u *arg)
1879{
1880 if (*arg == NUL || !ends_excmd(*arg))
1881 {
1882 // also complete "None"
1883 set_context_in_echohl_cmd(xp, arg);
1884 arg = skipwhite(skiptowhite(arg));
1885 if (*arg != NUL)
1886 {
1887 xp->xp_context = EXPAND_NOTHING;
1888 arg = skip_regexp(arg + 1, *arg, magic_isset());
1889 }
1890 }
1891 return find_nextcmd(arg);
1892}
1893#endif
1894
1895/*
1896 * Returns a pointer to the next command after a :global or a :v command.
1897 * Returns NULL if there is no next command.
1898 */
1899 static char_u *
1900find_cmd_after_global_cmd(char_u *arg)
1901{
1902 int delim;
1903
1904 delim = *arg; // get the delimiter
1905 if (delim)
1906 ++arg; // skip delimiter if there is one
1907
1908 while (arg[0] != NUL && arg[0] != delim)
1909 {
1910 if (arg[0] == '\\' && arg[1] != NUL)
1911 ++arg;
1912 ++arg;
1913 }
1914 if (arg[0] != NUL)
1915 return arg + 1;
1916
1917 return NULL;
1918}
1919
1920/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001921 * Returns a pointer to the next command after a :substitute or a :& command.
1922 * Returns NULL if there is no next command.
1923 */
1924 static char_u *
1925find_cmd_after_substitute_cmd(char_u *arg)
1926{
1927 int delim;
1928
1929 delim = *arg;
1930 if (delim)
1931 {
1932 // skip "from" part
1933 ++arg;
1934 arg = skip_regexp(arg, delim, magic_isset());
1935
1936 if (arg[0] != NUL && arg[0] == delim)
1937 {
1938 // skip "to" part
1939 ++arg;
1940 while (arg[0] != NUL && arg[0] != delim)
1941 {
1942 if (arg[0] == '\\' && arg[1] != NUL)
1943 ++arg;
1944 ++arg;
1945 }
1946 if (arg[0] != NUL) // skip delimiter
1947 ++arg;
1948 }
1949 }
1950 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1951 ++arg;
1952 if (arg[0] != NUL)
1953 return arg;
1954
1955 return NULL;
1956}
1957
1958/*
1959 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1960 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1961 * Returns NULL if there is no next command.
1962 */
1963 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001964find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001965{
1966 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001967 if (*arg != '/')
1968 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001969
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001970 // Match regexp, not just whole words
1971 for (++arg; *arg && *arg != '/'; arg++)
1972 if (*arg == '\\' && arg[1] != NUL)
1973 arg++;
1974 if (*arg)
1975 {
1976 arg = skipwhite(arg + 1);
1977
1978 // Check for trailing illegal characters
1979 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1980 xp->xp_context = EXPAND_NOTHING;
1981 else
1982 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001983 }
1984
1985 return NULL;
1986}
1987
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001988#ifdef FEAT_EVAL
1989/*
1990 * Set the completion context for the :unlet command. Always returns NULL.
1991 */
1992 static char_u *
1993set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1994{
1995 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1996 arg = xp->xp_pattern + 1;
1997
1998 xp->xp_context = EXPAND_USER_VARS;
1999 xp->xp_pattern = arg;
2000
2001 if (*xp->xp_pattern == '$')
2002 {
2003 xp->xp_context = EXPAND_ENV_VARS;
2004 ++xp->xp_pattern;
2005 }
2006
2007 return NULL;
2008}
2009#endif
2010
2011#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2012/*
2013 * Set the completion context for the :language command. Always returns NULL.
2014 */
2015 static char_u *
2016set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2017{
2018 char_u *p;
2019
2020 p = skiptowhite(arg);
2021 if (*p == NUL)
2022 {
2023 xp->xp_context = EXPAND_LANGUAGE;
2024 xp->xp_pattern = arg;
2025 }
2026 else
2027 {
2028 if ( STRNCMP(arg, "messages", p - arg) == 0
2029 || STRNCMP(arg, "ctype", p - arg) == 0
2030 || STRNCMP(arg, "time", p - arg) == 0
2031 || STRNCMP(arg, "collate", p - arg) == 0)
2032 {
2033 xp->xp_context = EXPAND_LOCALES;
2034 xp->xp_pattern = skipwhite(p);
2035 }
2036 else
2037 xp->xp_context = EXPAND_NOTHING;
2038 }
2039
2040 return NULL;
2041}
2042#endif
2043
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002044#ifdef FEAT_EVAL
2045static enum
2046{
2047 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002048 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2049 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002050} breakpt_expand_what;
2051
2052/*
2053 * Set the completion context for the :breakadd command. Always returns NULL.
2054 */
2055 static char_u *
2056set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2057{
2058 char_u *p;
2059 char_u *subcmd_start;
2060
2061 xp->xp_context = EXPAND_BREAKPOINT;
2062 xp->xp_pattern = arg;
2063
2064 if (cmdidx == CMD_breakadd)
2065 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002066 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002067 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002068 else
2069 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002070
2071 p = skipwhite(arg);
2072 if (*p == NUL)
2073 return NULL;
2074 subcmd_start = p;
2075
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002076 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002077 {
2078 // :breakadd file [lnum] <filename>
2079 // :breakadd func [lnum] <funcname>
2080 p += 4;
2081 p = skipwhite(p);
2082
2083 // skip line number (if specified)
2084 if (VIM_ISDIGIT(*p))
2085 {
2086 p = skipdigits(p);
2087 if (*p != ' ')
2088 {
2089 xp->xp_context = EXPAND_NOTHING;
2090 return NULL;
2091 }
2092 p = skipwhite(p);
2093 }
2094 if (STRNCMP("file", subcmd_start, 4) == 0)
2095 xp->xp_context = EXPAND_FILES;
2096 else
2097 xp->xp_context = EXPAND_USER_FUNC;
2098 xp->xp_pattern = p;
2099 }
2100 else if (STRNCMP("expr ", p, 5) == 0)
2101 {
2102 // :breakadd expr <expression>
2103 xp->xp_context = EXPAND_EXPRESSION;
2104 xp->xp_pattern = skipwhite(p + 5);
2105 }
2106
2107 return NULL;
2108}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002109
2110 static char_u *
2111set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2112{
2113 char_u *p;
2114
2115 xp->xp_context = EXPAND_NOTHING;
2116 xp->xp_pattern = NULL;
2117
2118 p = skipwhite(arg);
2119 if (VIM_ISDIGIT(*p))
2120 return NULL;
2121
2122 xp->xp_context = EXPAND_SCRIPTNAMES;
2123 xp->xp_pattern = p;
2124
2125 return NULL;
2126}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002127#endif
2128
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002129/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002130 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2131 * The argument to the command is 'arg' and the argument flags is 'argt'.
2132 * For user-defined commands and for environment variables, 'compl' has the
2133 * completion type.
2134 * Returns a pointer to the next command. Returns NULL if there is no next
2135 * command.
2136 */
2137 static char_u *
2138set_context_by_cmdname(
2139 char_u *cmd,
2140 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002141 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002142 char_u *arg,
2143 long argt,
2144 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002145 int forceit)
2146{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002147 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002148 {
2149 case CMD_find:
2150 case CMD_sfind:
2151 case CMD_tabfind:
2152 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002153 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002154 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002155 break;
2156 case CMD_cd:
2157 case CMD_chdir:
2158 case CMD_tcd:
2159 case CMD_tchdir:
2160 case CMD_lcd:
2161 case CMD_lchdir:
2162 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002163 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002164 break;
2165 case CMD_help:
2166 xp->xp_context = EXPAND_HELP;
2167 xp->xp_pattern = arg;
2168 break;
2169
2170 // Command modifiers: return the argument.
2171 // Also for commands with an argument that is a command.
2172 case CMD_aboveleft:
2173 case CMD_argdo:
2174 case CMD_belowright:
2175 case CMD_botright:
2176 case CMD_browse:
2177 case CMD_bufdo:
2178 case CMD_cdo:
2179 case CMD_cfdo:
2180 case CMD_confirm:
2181 case CMD_debug:
2182 case CMD_folddoclosed:
2183 case CMD_folddoopen:
2184 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002185 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002186 case CMD_keepalt:
2187 case CMD_keepjumps:
2188 case CMD_keepmarks:
2189 case CMD_keeppatterns:
2190 case CMD_ldo:
2191 case CMD_leftabove:
2192 case CMD_lfdo:
2193 case CMD_lockmarks:
2194 case CMD_noautocmd:
2195 case CMD_noswapfile:
2196 case CMD_rightbelow:
2197 case CMD_sandbox:
2198 case CMD_silent:
2199 case CMD_tab:
2200 case CMD_tabdo:
2201 case CMD_topleft:
2202 case CMD_verbose:
2203 case CMD_vertical:
2204 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002205 case CMD_vim9cmd:
2206 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002207 return arg;
2208
2209 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002210 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002211
2212#ifdef FEAT_SEARCH_EXTRA
2213 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002214 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002215#endif
2216
2217 // All completion for the +cmdline_compl feature goes here.
2218
2219 case CMD_command:
2220 return set_context_in_user_cmd(xp, arg);
2221
2222 case CMD_delcommand:
2223 xp->xp_context = EXPAND_USER_COMMANDS;
2224 xp->xp_pattern = arg;
2225 break;
2226
2227 case CMD_global:
2228 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002229 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002230 case CMD_and:
2231 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002232 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002233 case CMD_isearch:
2234 case CMD_dsearch:
2235 case CMD_ilist:
2236 case CMD_dlist:
2237 case CMD_ijump:
2238 case CMD_psearch:
2239 case CMD_djump:
2240 case CMD_isplit:
2241 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002242 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002243 case CMD_autocmd:
2244 return set_context_in_autocmd(xp, arg, FALSE);
2245 case CMD_doautocmd:
2246 case CMD_doautoall:
2247 return set_context_in_autocmd(xp, arg, TRUE);
2248 case CMD_set:
2249 set_context_in_set_cmd(xp, arg, 0);
2250 break;
2251 case CMD_setglobal:
2252 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2253 break;
2254 case CMD_setlocal:
2255 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2256 break;
2257 case CMD_tag:
2258 case CMD_stag:
2259 case CMD_ptag:
2260 case CMD_ltag:
2261 case CMD_tselect:
2262 case CMD_stselect:
2263 case CMD_ptselect:
2264 case CMD_tjump:
2265 case CMD_stjump:
2266 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002267 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002268 xp->xp_context = EXPAND_TAGS_LISTFILES;
2269 else
2270 xp->xp_context = EXPAND_TAGS;
2271 xp->xp_pattern = arg;
2272 break;
2273 case CMD_augroup:
2274 xp->xp_context = EXPAND_AUGROUP;
2275 xp->xp_pattern = arg;
2276 break;
2277#ifdef FEAT_SYN_HL
2278 case CMD_syntax:
2279 set_context_in_syntax_cmd(xp, arg);
2280 break;
2281#endif
2282#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002283 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002284 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002285 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002286 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002287 case CMD_if:
2288 case CMD_elseif:
2289 case CMD_while:
2290 case CMD_for:
2291 case CMD_echo:
2292 case CMD_echon:
2293 case CMD_execute:
2294 case CMD_echomsg:
2295 case CMD_echoerr:
2296 case CMD_call:
2297 case CMD_return:
2298 case CMD_cexpr:
2299 case CMD_caddexpr:
2300 case CMD_cgetexpr:
2301 case CMD_lexpr:
2302 case CMD_laddexpr:
2303 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002304 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002305 break;
2306
2307 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002308 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002309 case CMD_function:
2310 case CMD_delfunction:
2311 xp->xp_context = EXPAND_USER_FUNC;
2312 xp->xp_pattern = arg;
2313 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002314 case CMD_disassemble:
2315 set_context_in_disassemble_cmd(xp, arg);
2316 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002317
2318 case CMD_echohl:
2319 set_context_in_echohl_cmd(xp, arg);
2320 break;
2321#endif
2322 case CMD_highlight:
2323 set_context_in_highlight_cmd(xp, arg);
2324 break;
2325#ifdef FEAT_CSCOPE
2326 case CMD_cscope:
2327 case CMD_lcscope:
2328 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002329 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002330 break;
2331#endif
2332#ifdef FEAT_SIGNS
2333 case CMD_sign:
2334 set_context_in_sign_cmd(xp, arg);
2335 break;
2336#endif
2337 case CMD_bdelete:
2338 case CMD_bwipeout:
2339 case CMD_bunload:
2340 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2341 arg = xp->xp_pattern + 1;
2342 // FALLTHROUGH
2343 case CMD_buffer:
2344 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002345 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002346 case CMD_checktime:
2347 xp->xp_context = EXPAND_BUFFERS;
2348 xp->xp_pattern = arg;
2349 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002350#ifdef FEAT_DIFF
2351 case CMD_diffget:
2352 case CMD_diffput:
2353 // If current buffer is in diff mode, complete buffer names
2354 // which are in diff mode, and different than current buffer.
2355 xp->xp_context = EXPAND_DIFF_BUFFERS;
2356 xp->xp_pattern = arg;
2357 break;
2358#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002359 case CMD_USER:
2360 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002361 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2362 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002363
2364 case CMD_map: case CMD_noremap:
2365 case CMD_nmap: case CMD_nnoremap:
2366 case CMD_vmap: case CMD_vnoremap:
2367 case CMD_omap: case CMD_onoremap:
2368 case CMD_imap: case CMD_inoremap:
2369 case CMD_cmap: case CMD_cnoremap:
2370 case CMD_lmap: case CMD_lnoremap:
2371 case CMD_smap: case CMD_snoremap:
2372 case CMD_tmap: case CMD_tnoremap:
2373 case CMD_xmap: case CMD_xnoremap:
2374 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002375 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002376 case CMD_unmap:
2377 case CMD_nunmap:
2378 case CMD_vunmap:
2379 case CMD_ounmap:
2380 case CMD_iunmap:
2381 case CMD_cunmap:
2382 case CMD_lunmap:
2383 case CMD_sunmap:
2384 case CMD_tunmap:
2385 case CMD_xunmap:
2386 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002387 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002388 case CMD_mapclear:
2389 case CMD_nmapclear:
2390 case CMD_vmapclear:
2391 case CMD_omapclear:
2392 case CMD_imapclear:
2393 case CMD_cmapclear:
2394 case CMD_lmapclear:
2395 case CMD_smapclear:
2396 case CMD_tmapclear:
2397 case CMD_xmapclear:
2398 xp->xp_context = EXPAND_MAPCLEAR;
2399 xp->xp_pattern = arg;
2400 break;
2401
2402 case CMD_abbreviate: case CMD_noreabbrev:
2403 case CMD_cabbrev: case CMD_cnoreabbrev:
2404 case CMD_iabbrev: case CMD_inoreabbrev:
2405 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002406 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002407 case CMD_unabbreviate:
2408 case CMD_cunabbrev:
2409 case CMD_iunabbrev:
2410 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002411 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002412#ifdef FEAT_MENU
2413 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2414 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2415 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2416 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2417 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2418 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2419 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2420 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2421 case CMD_tmenu: case CMD_tunmenu:
2422 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2423 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2424#endif
2425
2426 case CMD_colorscheme:
2427 xp->xp_context = EXPAND_COLORS;
2428 xp->xp_pattern = arg;
2429 break;
2430
2431 case CMD_compiler:
2432 xp->xp_context = EXPAND_COMPILER;
2433 xp->xp_pattern = arg;
2434 break;
2435
2436 case CMD_ownsyntax:
2437 xp->xp_context = EXPAND_OWNSYNTAX;
2438 xp->xp_pattern = arg;
2439 break;
2440
2441 case CMD_setfiletype:
2442 xp->xp_context = EXPAND_FILETYPE;
2443 xp->xp_pattern = arg;
2444 break;
2445
2446 case CMD_packadd:
2447 xp->xp_context = EXPAND_PACKADD;
2448 xp->xp_pattern = arg;
2449 break;
2450
zeertzjqb0d45ec2023-01-25 15:04:22 +00002451 case CMD_runtime:
2452 set_context_in_runtime_cmd(xp, arg);
2453 break;
2454
Bram Moolenaard0190392019-08-23 21:17:35 +02002455#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2456 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002457 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002458#endif
2459#if defined(FEAT_PROFILE)
2460 case CMD_profile:
2461 set_context_in_profile_cmd(xp, arg);
2462 break;
2463#endif
2464 case CMD_behave:
2465 xp->xp_context = EXPAND_BEHAVE;
2466 xp->xp_pattern = arg;
2467 break;
2468
2469 case CMD_messages:
2470 xp->xp_context = EXPAND_MESSAGES;
2471 xp->xp_pattern = arg;
2472 break;
2473
2474 case CMD_history:
2475 xp->xp_context = EXPAND_HISTORY;
2476 xp->xp_pattern = arg;
2477 break;
2478#if defined(FEAT_PROFILE)
2479 case CMD_syntime:
2480 xp->xp_context = EXPAND_SYNTIME;
2481 xp->xp_pattern = arg;
2482 break;
2483#endif
2484
2485 case CMD_argdelete:
2486 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2487 arg = xp->xp_pattern + 1;
2488 xp->xp_context = EXPAND_ARGLIST;
2489 xp->xp_pattern = arg;
2490 break;
2491
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002492#ifdef FEAT_EVAL
2493 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002494 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002495 case CMD_breakdel:
2496 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002497
2498 case CMD_scriptnames:
2499 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002500#endif
2501
Bram Moolenaard0190392019-08-23 21:17:35 +02002502 default:
2503 break;
2504 }
2505 return NULL;
2506}
2507
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002508/*
2509 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2510 * we don't need/want deleted. Maybe this could be done better if we didn't
2511 * repeat all this stuff. The only problem is that they may not stay
2512 * perfectly compatible with each other, but then the command line syntax
2513 * probably won't change that much -- webb.
2514 */
2515 static char_u *
2516set_one_cmd_context(
2517 expand_T *xp,
2518 char_u *buff) // buffer for command string
2519{
2520 char_u *p;
2521 char_u *cmd, *arg;
2522 int len = 0;
2523 exarg_T ea;
2524 int compl = EXPAND_NOTHING;
2525 int forceit = FALSE;
2526 int usefilter = FALSE; // filter instead of file name
2527
2528 ExpandInit(xp);
2529 xp->xp_pattern = buff;
2530 xp->xp_line = buff;
2531 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2532 ea.argt = 0;
2533
2534 // 1. skip comment lines and leading space, colons or bars
2535 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2536 ;
2537 xp->xp_pattern = cmd;
2538
2539 if (*cmd == NUL)
2540 return NULL;
2541 if (*cmd == '"') // ignore comment lines
2542 {
2543 xp->xp_context = EXPAND_NOTHING;
2544 return NULL;
2545 }
2546
2547 // 3. Skip over the range to find the command.
2548 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2549 xp->xp_pattern = cmd;
2550 if (*cmd == NUL)
2551 return NULL;
2552 if (*cmd == '"')
2553 {
2554 xp->xp_context = EXPAND_NOTHING;
2555 return NULL;
2556 }
2557
2558 if (*cmd == '|' || *cmd == '\n')
2559 return cmd + 1; // There's another command
2560
2561 // Get the command index.
2562 p = set_cmd_index(cmd, &ea, xp, &compl);
2563 if (p == NULL)
2564 return NULL;
2565
2566 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2567
2568 if (*p == '!') // forced commands
2569 {
2570 forceit = TRUE;
2571 ++p;
2572 }
2573
2574 // 6. parse arguments
2575 if (!IS_USER_CMDIDX(ea.cmdidx))
2576 ea.argt = excmd_get_argt(ea.cmdidx);
2577
2578 arg = skipwhite(p);
2579
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002580 // Does command allow "++argopt" argument?
2581 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002582 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002583 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2584 {
2585 p = arg + 2;
2586 while (*p && !vim_isspace(*p))
2587 MB_PTR_ADV(p);
2588
2589 // Still touching the command after "++"?
2590 if (*p == NUL)
2591 {
2592 if (ea.argt & EX_ARGOPT)
2593 return set_context_in_argopt(xp, arg + 2);
2594#ifdef FEAT_TERMINAL
2595 if (ea.cmdidx == CMD_terminal)
2596 return set_context_in_terminalopt(xp, arg + 2);
2597#endif
2598 }
2599
2600 arg = skipwhite(p);
2601 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002602 }
2603
2604 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2605 {
2606 if (*arg == '>') // append
2607 {
2608 if (*++arg == '>')
2609 ++arg;
2610 arg = skipwhite(arg);
2611 }
2612 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2613 {
2614 ++arg;
2615 usefilter = TRUE;
2616 }
2617 }
2618
2619 if (ea.cmdidx == CMD_read)
2620 {
2621 usefilter = forceit; // :r! filter if forced
2622 if (*arg == '!') // :r !filter
2623 {
2624 ++arg;
2625 usefilter = TRUE;
2626 }
2627 }
2628
2629 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2630 {
2631 while (*arg == *cmd) // allow any number of '>' or '<'
2632 ++arg;
2633 arg = skipwhite(arg);
2634 }
2635
2636 // Does command allow "+command"?
2637 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2638 {
2639 // Check if we're in the +command
2640 p = arg + 1;
2641 arg = skip_cmd_arg(arg, FALSE);
2642
2643 // Still touching the command after '+'?
2644 if (*arg == NUL)
2645 return p;
2646
2647 // Skip space(s) after +command to get to the real argument
2648 arg = skipwhite(arg);
2649 }
2650
2651
2652 // Check for '|' to separate commands and '"' to start comments.
2653 // Don't do this for ":read !cmd" and ":write !cmd".
2654 if ((ea.argt & EX_TRLBAR) && !usefilter)
2655 {
2656 p = arg;
2657 // ":redir @" is not the start of a comment
2658 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2659 p += 2;
2660 while (*p)
2661 {
2662 if (*p == Ctrl_V)
2663 {
2664 if (p[1] != NUL)
2665 ++p;
2666 }
2667 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2668 || *p == '|' || *p == '\n')
2669 {
2670 if (*(p - 1) != '\\')
2671 {
2672 if (*p == '|' || *p == '\n')
2673 return p + 1;
2674 return NULL; // It's a comment
2675 }
2676 }
2677 MB_PTR_ADV(p);
2678 }
2679 }
2680
2681 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2682 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2683 // no arguments allowed but there is something
2684 return NULL;
2685
2686 // Find start of last argument (argument just before cursor):
2687 p = buff;
2688 xp->xp_pattern = p;
2689 len = (int)STRLEN(buff);
2690 while (*p && p < buff + len)
2691 {
2692 if (*p == ' ' || *p == TAB)
2693 {
2694 // argument starts after a space
2695 xp->xp_pattern = ++p;
2696 }
2697 else
2698 {
2699 if (*p == '\\' && *(p + 1) != NUL)
2700 ++p; // skip over escaped character
2701 MB_PTR_ADV(p);
2702 }
2703 }
2704
2705 if (ea.argt & EX_XFILE)
2706 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2707
2708 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002709 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002710 forceit);
2711}
2712
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002713/*
2714 * Set the completion context in 'xp' for command 'str'
2715 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002716 void
2717set_cmd_context(
2718 expand_T *xp,
2719 char_u *str, // start of command line
2720 int len, // length of command line (excl. NUL)
2721 int col, // position of cursor
2722 int use_ccline UNUSED) // use ccline for info
2723{
2724#ifdef FEAT_EVAL
2725 cmdline_info_T *ccline = get_cmdline_info();
2726#endif
2727 int old_char = NUL;
2728 char_u *nextcomm;
2729
2730 // Avoid a UMR warning from Purify, only save the character if it has been
2731 // written before.
2732 if (col < len)
2733 old_char = str[col];
2734 str[col] = NUL;
2735 nextcomm = str;
2736
2737#ifdef FEAT_EVAL
2738 if (use_ccline && ccline->cmdfirstc == '=')
2739 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002740 // pass CMD_SIZE because there is no real command
2741 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002742 }
2743 else if (use_ccline && ccline->input_fn)
2744 {
2745 xp->xp_context = ccline->xp_context;
2746 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002747 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002748 }
2749 else
2750#endif
2751 while (nextcomm != NULL)
2752 nextcomm = set_one_cmd_context(xp, nextcomm);
2753
2754 // Store the string here so that call_user_expand_func() can get to them
2755 // easily.
2756 xp->xp_line = str;
2757 xp->xp_col = col;
2758
2759 str[col] = old_char;
2760}
2761
2762/*
2763 * Expand the command line "str" from context "xp".
2764 * "xp" must have been set by set_cmd_context().
2765 * xp->xp_pattern points into "str", to where the text that is to be expanded
2766 * starts.
2767 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2768 * cursor.
2769 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2770 * key that triggered expansion literally.
2771 * Returns EXPAND_OK otherwise.
2772 */
2773 int
2774expand_cmdline(
2775 expand_T *xp,
2776 char_u *str, // start of command line
2777 int col, // position of cursor
2778 int *matchcount, // return: nr of matches
2779 char_u ***matches) // return: array of pointers to matches
2780{
2781 char_u *file_str = NULL;
2782 int options = WILD_ADD_SLASH|WILD_SILENT;
2783
2784 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2785 {
2786 beep_flush();
2787 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2788 }
2789 if (xp->xp_context == EXPAND_NOTHING)
2790 {
2791 // Caller can use the character as a normal char instead
2792 return EXPAND_NOTHING;
2793 }
2794
2795 // add star to file name, or convert to regexp if not exp. files.
2796 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002797 if (cmdline_fuzzy_completion_supported(xp))
2798 // If fuzzy matching, don't modify the search string
2799 file_str = vim_strsave(xp->xp_pattern);
2800 else
2801 {
2802 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2803 if (file_str == NULL)
2804 return EXPAND_UNSUCCESSFUL;
2805 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002806
2807 if (p_wic)
2808 options += WILD_ICASE;
2809
2810 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002811 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002812 {
2813 *matchcount = 0;
2814 *matches = NULL;
2815 }
2816 vim_free(file_str);
2817
2818 return EXPAND_OK;
2819}
2820
Bram Moolenaar66b51422019-08-18 21:44:12 +02002821/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002822 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002823 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002824 */
2825 static int
2826expand_files_and_dirs(
2827 expand_T *xp,
2828 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002829 char_u ***matches,
2830 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002831 int flags,
2832 int options)
2833{
2834 int free_pat = FALSE;
2835 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002836 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002837
2838 // for ":set path=" and ":set tags=" halve backslashes for escaped
2839 // space
2840 if (xp->xp_backslash != XP_BS_NONE)
2841 {
2842 free_pat = TRUE;
2843 pat = vim_strsave(pat);
2844 for (i = 0; pat[i]; ++i)
2845 if (pat[i] == '\\')
2846 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002847 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002848 && pat[i + 1] == '\\'
2849 && pat[i + 2] == '\\'
2850 && pat[i + 3] == ' ')
2851 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002852 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002853 && pat[i + 1] == ' ')
2854 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002855 else if ((xp->xp_backslash & XP_BS_COMMA)
2856 && pat[i + 1] == '\\'
2857 && pat[i + 2] == ',')
2858 STRMOVE(pat + i, pat + i + 2);
2859#ifdef BACKSLASH_IN_FILENAME
2860 else if ((xp->xp_backslash & XP_BS_COMMA)
2861 && pat[i + 1] == ',')
2862 STRMOVE(pat + i, pat + i + 1);
2863#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002864 }
2865 }
2866
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002867 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002868 {
2869#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002870 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002871#endif
2872 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002873 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002874 {
2875 if (xp->xp_context == EXPAND_FILES)
2876 flags |= EW_FILE;
2877 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2878 flags |= (EW_FILE | EW_PATH);
2879 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2880 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2881 else
2882 flags = (flags | EW_DIR) & ~EW_FILE;
2883 if (options & WILD_ICASE)
2884 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002885
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002886 // Expand wildcards, supporting %:h and the like.
2887 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2888 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002889 if (free_pat)
2890 vim_free(pat);
2891#ifdef BACKSLASH_IN_FILENAME
2892 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2893 {
2894 int j;
2895
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002896 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002897 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002898 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002899
2900 while (*ptr != NUL)
2901 {
2902 if (p_csl[0] == 's' && *ptr == '\\')
2903 *ptr = '/';
2904 else if (p_csl[0] == 'b' && *ptr == '/')
2905 *ptr = '\\';
2906 ptr += (*mb_ptr2len)(ptr);
2907 }
2908 }
2909 }
2910#endif
2911 return ret;
2912}
2913
2914/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002915 * Function given to ExpandGeneric() to obtain the possible arguments of the
2916 * ":behave {mswin,xterm}" command.
2917 */
2918 static char_u *
2919get_behave_arg(expand_T *xp UNUSED, int idx)
2920{
2921 if (idx == 0)
2922 return (char_u *)"mswin";
2923 if (idx == 1)
2924 return (char_u *)"xterm";
2925 return NULL;
2926}
2927
K.Takata161b6ac2022-11-14 15:31:07 +00002928#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002929/*
2930 * Function given to ExpandGeneric() to obtain the possible arguments of the
2931 * ":breakadd {expr, file, func, here}" command.
2932 * ":breakdel {func, file, here}" command.
2933 */
2934 static char_u *
2935get_breakadd_arg(expand_T *xp UNUSED, int idx)
2936{
2937 char *opts[] = {"expr", "file", "func", "here"};
2938
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002939 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002940 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002941 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002942 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2943 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002944 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2945 {
2946 // breakdel {func, file, here}
2947 if (idx <= 2)
2948 return (char_u *)opts[idx + 1];
2949 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002950 else
2951 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002952 // profdel {func, file}
2953 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002954 return (char_u *)opts[idx + 1];
2955 }
2956 }
2957 return NULL;
2958}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002959
2960/*
2961 * Function given to ExpandGeneric() to obtain the possible arguments for the
2962 * ":scriptnames" command.
2963 */
2964 static char_u *
2965get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2966{
2967 scriptitem_T *si;
2968
2969 if (!SCRIPT_ID_VALID(idx + 1))
2970 return NULL;
2971
2972 si = SCRIPT_ITEM(idx + 1);
2973 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2974 return NameBuff;
2975}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002976#endif
2977
Bram Moolenaard0190392019-08-23 21:17:35 +02002978/*
2979 * Function given to ExpandGeneric() to obtain the possible arguments of the
2980 * ":messages {clear}" command.
2981 */
2982 static char_u *
2983get_messages_arg(expand_T *xp UNUSED, int idx)
2984{
2985 if (idx == 0)
2986 return (char_u *)"clear";
2987 return NULL;
2988}
2989
2990 static char_u *
2991get_mapclear_arg(expand_T *xp UNUSED, int idx)
2992{
2993 if (idx == 0)
2994 return (char_u *)"<buffer>";
2995 return NULL;
2996}
2997
2998/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002999 * Do the expansion based on xp->xp_context and 'rmp'.
3000 */
3001 static int
3002ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003003 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003004 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003005 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003006 char_u ***matches,
3007 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003008{
3009 static struct expgen
3010 {
3011 int context;
3012 char_u *((*func)(expand_T *, int));
3013 int ic;
3014 int escaped;
3015 } tab[] =
3016 {
3017 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3018 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3019 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3020 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3021 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3022 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3023 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3024 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3025 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3026 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003027#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003028 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3029 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3030 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3031 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3032 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003033#endif
3034#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003035 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3036 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003037#endif
3038#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003039 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003040#endif
3041#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003042 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003043#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003044 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3045 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3046 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003047#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003048 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003049#endif
3050#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003051 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003052#endif
3053#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003054 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003055#endif
3056#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003057 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3058 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003059#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003060 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3061 {EXPAND_USER, get_users, TRUE, FALSE},
3062 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003063#ifdef FEAT_EVAL
3064 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003065 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003066#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003067 };
3068 int i;
3069 int ret = FAIL;
3070
3071 // Find a context in the table and call the ExpandGeneric() with the
3072 // right function to do the expansion.
3073 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3074 {
3075 if (xp->xp_context == tab[i].context)
3076 {
3077 if (tab[i].ic)
3078 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003079 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3080 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003081 break;
3082 }
3083 }
3084
3085 return ret;
3086}
3087
3088/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003089 * Map wild expand options to flags for expand_wildcards()
3090 */
3091 static int
3092map_wildopts_to_ewflags(int options)
3093{
3094 int flags;
3095
3096 flags = EW_DIR; // include directories
3097 if (options & WILD_LIST_NOTFOUND)
3098 flags |= EW_NOTFOUND;
3099 if (options & WILD_ADD_SLASH)
3100 flags |= EW_ADDSLASH;
3101 if (options & WILD_KEEP_ALL)
3102 flags |= EW_KEEPALL;
3103 if (options & WILD_SILENT)
3104 flags |= EW_SILENT;
3105 if (options & WILD_NOERROR)
3106 flags |= EW_NOERROR;
3107 if (options & WILD_ALLLINKS)
3108 flags |= EW_ALLLINKS;
3109
3110 return flags;
3111}
3112
3113/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 * Do the expansion based on xp->xp_context and "pat".
3115 */
3116 static int
3117ExpandFromContext(
3118 expand_T *xp,
3119 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003120 char_u ***matches,
3121 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 int options) // WILD_ flags
3123{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125 int ret;
3126 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003127 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003128 int fuzzy = cmdline_fuzzy_complete(pat)
3129 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003131 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132
3133 if (xp->xp_context == EXPAND_FILES
3134 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003135 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003136 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003137 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003138 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3139 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003141 *matches = (char_u **)"";
3142 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 if (xp->xp_context == EXPAND_HELP)
3144 {
3145 // With an empty argument we would get all the help tags, which is
3146 // very slow. Get matches for "help" instead.
3147 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 {
3150#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003151 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152#endif
3153 return OK;
3154 }
3155 return FAIL;
3156 }
3157
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003159 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003161 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003163 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003164#ifdef FEAT_DIFF
3165 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003166 return ExpandBufnames(pat, numMatches, matches,
3167 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003168#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003169 if (xp->xp_context == EXPAND_TAGS
3170 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003171 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3172 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 if (xp->xp_context == EXPAND_COLORS)
3174 {
3175 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003176 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003177 directories);
3178 }
3179 if (xp->xp_context == EXPAND_COMPILER)
3180 {
3181 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003182 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003183 }
3184 if (xp->xp_context == EXPAND_OWNSYNTAX)
3185 {
3186 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003187 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003188 }
3189 if (xp->xp_context == EXPAND_FILETYPE)
3190 {
3191 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003192 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 }
Doug Kearns81642d92024-01-04 22:37:44 +01003194#ifdef FEAT_KEYMAP
3195 if (xp->xp_context == EXPAND_KEYMAP)
3196 {
3197 char *directories[] = {"keymap", NULL};
3198 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3199 }
3200#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003201#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003203 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003204#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003205 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003206 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003207 if (xp->xp_context == EXPAND_RUNTIME)
3208 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003209
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003210 // When expanding a function name starting with s:, match the <SNR>nr_
3211 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003212 if ((xp->xp_context == EXPAND_USER_FUNC
3213 || xp->xp_context == EXPAND_DISASSEMBLE)
3214 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003215 {
3216 int len = (int)STRLEN(pat) + 20;
3217
3218 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003219 if (tofree == NULL)
3220 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003221 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003222 pat = tofree;
3223 }
3224
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003225 if (!fuzzy)
3226 {
3227 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3228 if (regmatch.regprog == NULL)
3229 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003230
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003231 // set ignore-case according to p_ic, p_scs and pat
3232 regmatch.rm_ic = ignorecase(pat);
3233 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234
3235 if (xp->xp_context == EXPAND_SETTINGS
3236 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003237 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003238 else if (xp->xp_context == EXPAND_STRING_SETTING)
3239 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3240 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3241 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003243 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003244 else if (xp->xp_context == EXPAND_ARGOPT)
3245 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003246 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3247 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003248#if defined(FEAT_TERMINAL)
3249 else if (xp->xp_context == EXPAND_TERMINALOPT)
3250 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3251#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003252#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003254 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003255#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003256 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003257 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003259 if (!fuzzy)
3260 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003261 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003262
3263 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264}
3265
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003266 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003268 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003269 expand_T *xp,
3270 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 char_u ***matches,
3272 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273 char_u *((*func)(expand_T *, int)),
3274 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003275 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276{
Yee Cheng China7b81202025-02-23 09:32:47 +01003277 return ExpandGenericExt(
3278 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3279}
3280
3281/*
3282 * Expand a list of names.
3283 *
3284 * Generic function for command line completion. It calls a function to
3285 * obtain strings, one by one. The strings are matched against a regexp
3286 * program. Matching strings are copied into an array, which is returned.
3287 *
3288 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3289 * is used.
3290 *
3291 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3292 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3293 * sorting.
3294 *
3295 * Returns OK when no problems encountered, FAIL for error (out of memory).
3296 */
3297 int
3298ExpandGenericExt(
3299 char_u *pat,
3300 expand_T *xp,
3301 regmatch_T *regmatch,
3302 char_u ***matches,
3303 int *numMatches,
3304 char_u *((*func)(expand_T *, int)),
3305 // returns a string from the list
3306 int escaped,
3307 int sortStartIdx)
3308{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003310 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003311 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003312 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003313 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003314 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003315 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003316 int sort_matches = FALSE;
3317 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003318 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003319
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003320 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003321 *matches = NULL;
3322 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003323
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003324 if (!fuzzy)
3325 ga_init2(&ga, sizeof(char *), 30);
3326 else
3327 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3328
3329 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003330 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003331 str = (*func)(xp, i);
3332 if (str == NULL) // end of list
3333 break;
3334 if (*str == NUL) // skip empty strings
3335 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003336
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003337 if (xp->xp_pattern[0] != NUL)
3338 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003339 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003340 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003341 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003342 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003343 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003344 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003345 }
3346 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003347 else
3348 match = TRUE;
3349
3350 if (!match)
3351 continue;
3352
3353 if (escaped)
3354 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3355 else
3356 str = vim_strsave(str);
3357 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003358 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003359 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003360 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003361 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003362 return FAIL;
3363 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003364 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003365 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003367
3368 if (ga_grow(&ga, 1) == FAIL)
3369 {
3370 vim_free(str);
3371 break;
3372 }
3373
3374 if (fuzzy)
3375 {
3376 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3377 fuzmatch->idx = ga.ga_len;
3378 fuzmatch->str = str;
3379 fuzmatch->score = score;
3380 }
3381 else
3382 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3383
K.Takata161b6ac2022-11-14 15:31:07 +00003384#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003385 if (func == get_menu_names)
3386 {
3387 // test for separator added by get_menu_names()
3388 str += STRLEN(str) - 1;
3389 if (*str == '\001')
3390 *str = '.';
3391 }
K.Takata161b6ac2022-11-14 15:31:07 +00003392#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003393
Yee Cheng China7b81202025-02-23 09:32:47 +01003394 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3395 {
3396 // Found first item to start sorting from. This is usually 0.
3397 sortStartMatchIdx = ga.ga_len;
3398 }
3399
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003400 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003401 }
3402
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003403 if (ga.ga_len == 0)
3404 return OK;
3405
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003406 // sort the matches when using regular expression matching and sorting
3407 // applies to the completion context. Menus and scriptnames should be kept
3408 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003409 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003410 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003411 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003412 && xp->xp_context != EXPAND_SCRIPTNAMES
3413 && xp->xp_context != EXPAND_ARGOPT
3414 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003415 sort_matches = TRUE;
3416
3417 // <SNR> functions should be sorted to the end.
3418 if (xp->xp_context == EXPAND_EXPRESSION
3419 || xp->xp_context == EXPAND_FUNCTIONS
3420 || xp->xp_context == EXPAND_USER_FUNC
3421 || xp->xp_context == EXPAND_DISASSEMBLE)
3422 funcsort = TRUE;
3423
3424 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003425 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003426 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003427 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003428 // <SNR> functions should be sorted to the end.
3429 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3430 sort_func_compare);
3431 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003432 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003433 }
3434
3435 if (!fuzzy)
3436 {
3437 *matches = ga.ga_data;
3438 *numMatches = ga.ga_len;
3439 }
3440 else
3441 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003442 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3443 funcsort) == FAIL)
3444 return FAIL;
3445 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003446 }
3447
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003448#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003449 // Reset the variables used for special highlight names expansion, so that
3450 // they don't show up when getting normal highlight names by ID.
3451 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003452#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003453
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454 return OK;
3455}
3456
3457/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003458 * Expand shell command matches in one directory of $PATH.
3459 */
3460 static void
3461expand_shellcmd_onedir(
3462 char_u *buf,
3463 char_u *s,
3464 size_t l,
3465 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003466 char_u ***matches,
3467 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003468 int flags,
3469 hashtab_T *ht,
3470 garray_T *gap)
3471{
3472 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003473 hash_T hash;
3474 hashitem_T *hi;
3475
3476 vim_strncpy(buf, s, l);
3477 add_pathsep(buf);
3478 l = STRLEN(buf);
3479 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3480
3481 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003482 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003483 if (ret != OK)
3484 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003485
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003486 if (ga_grow(gap, *numMatches) == FAIL)
3487 {
3488 FreeWild(*numMatches, *matches);
3489 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003490 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003491
3492 for (int i = 0; i < *numMatches; ++i)
3493 {
3494 char_u *name = (*matches)[i];
3495
3496 if (STRLEN(name) > l)
3497 {
3498 // Check if this name was already found.
3499 hash = hash_hash(name + l);
3500 hi = hash_lookup(ht, name + l, hash);
3501 if (HASHITEM_EMPTY(hi))
3502 {
3503 // Remove the path that was prepended.
3504 STRMOVE(name, name + l);
3505 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3506 hash_add_item(ht, hi, name, hash);
3507 name = NULL;
3508 }
3509 }
3510 vim_free(name);
3511 }
3512 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003513}
3514
3515/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003516 * Complete a shell command.
3517 * Returns FAIL or OK;
3518 */
3519 static int
3520expand_shellcmd(
3521 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003522 char_u ***matches, // return: array with matches
3523 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003524 int flagsarg) // EW_ flags
3525{
3526 char_u *pat;
3527 int i;
3528 char_u *path = NULL;
3529 int mustfree = FALSE;
3530 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003531 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 size_t l;
3533 char_u *s, *e;
3534 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003535 int did_curdir = FALSE;
3536 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003537
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003538 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003539 if (buf == NULL)
3540 return FAIL;
3541
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003542 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003544 if (pat == NULL)
3545 {
3546 vim_free(buf);
3547 return FAIL;
3548 }
3549
Bram Moolenaar66b51422019-08-18 21:44:12 +02003550 for (i = 0; pat[i]; ++i)
3551 if (pat[i] == '\\' && pat[i + 1] == ' ')
3552 STRMOVE(pat + i, pat + i + 1);
3553
3554 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3555
3556 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3557 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3558 path = (char_u *)".";
3559 else
3560 {
3561 // For an absolute name we don't use $PATH.
3562 if (!mch_isFullName(pat))
3563 path = vim_getenv((char_u *)"PATH", &mustfree);
3564 if (path == NULL)
3565 path = (char_u *)"";
3566 }
3567
3568 // Go over all directories in $PATH. Expand matches in that directory and
3569 // collect them in "ga". When "." is not in $PATH also expand for the
3570 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003571 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003572 hash_init(&found_ht);
3573 for (s = path; ; s = e)
3574 {
K.Takata161b6ac2022-11-14 15:31:07 +00003575#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003576 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003577#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003579#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003580 if (e == NULL)
3581 e = s + STRLEN(s);
3582
3583 if (*s == NUL)
3584 {
3585 if (did_curdir)
3586 break;
3587 // Find directories in the current directory, path is empty.
3588 did_curdir = TRUE;
3589 flags |= EW_DIR;
3590 }
3591 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3592 {
3593 did_curdir = TRUE;
3594 flags |= EW_DIR;
3595 }
3596 else
3597 // Do not match directories inside a $PATH item.
3598 flags &= ~EW_DIR;
3599
3600 l = e - s;
3601 if (l > MAXPATHL - 5)
3602 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003603
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003604 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003605 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003606
Bram Moolenaar66b51422019-08-18 21:44:12 +02003607 if (*e != NUL)
3608 ++e;
3609 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003610 *matches = ga.ga_data;
3611 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003612
3613 vim_free(buf);
3614 vim_free(pat);
3615 if (mustfree)
3616 vim_free(path);
3617 hash_clear(&found_ht);
3618 return OK;
3619}
3620
K.Takata161b6ac2022-11-14 15:31:07 +00003621#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003622/*
3623 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003624 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003625 */
3626 static void *
3627call_user_expand_func(
3628 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003629 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003630{
3631 cmdline_info_T *ccline = get_cmdline_info();
3632 int keep = 0;
3633 typval_T args[4];
3634 sctx_T save_current_sctx = current_sctx;
3635 char_u *pat = NULL;
3636 void *ret;
3637
3638 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3639 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640
3641 if (ccline->cmdbuff != NULL)
3642 {
3643 keep = ccline->cmdbuff[ccline->cmdlen];
3644 ccline->cmdbuff[ccline->cmdlen] = 0;
3645 }
3646
3647 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3648
3649 args[0].v_type = VAR_STRING;
3650 args[0].vval.v_string = pat;
3651 args[1].v_type = VAR_STRING;
3652 args[1].vval.v_string = xp->xp_line;
3653 args[2].v_type = VAR_NUMBER;
3654 args[2].vval.v_number = xp->xp_col;
3655 args[3].v_type = VAR_UNKNOWN;
3656
3657 current_sctx = xp->xp_script_ctx;
3658
3659 ret = user_expand_func(xp->xp_arg, 3, args);
3660
3661 current_sctx = save_current_sctx;
3662 if (ccline->cmdbuff != NULL)
3663 ccline->cmdbuff[ccline->cmdlen] = keep;
3664
3665 vim_free(pat);
3666 return ret;
3667}
3668
3669/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003670 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3671 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003672 */
3673 static int
3674ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003675 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 expand_T *xp,
3677 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003678 char_u ***matches,
3679 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680{
3681 char_u *retstr;
3682 char_u *s;
3683 char_u *e;
3684 int keep;
3685 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 int fuzzy;
3687 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003688 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003689
3690 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003691 *matches = NULL;
3692 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003693
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003694 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003695 if (retstr == NULL)
3696 return FAIL;
3697
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003698 if (!fuzzy)
3699 ga_init2(&ga, sizeof(char *), 3);
3700 else
3701 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3702
Bram Moolenaar66b51422019-08-18 21:44:12 +02003703 for (s = retstr; *s != NUL; s = e)
3704 {
3705 e = vim_strchr(s, '\n');
3706 if (e == NULL)
3707 e = s + STRLEN(s);
3708 keep = *e;
3709 *e = NUL;
3710
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003711 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003712 {
3713 if (!fuzzy)
3714 match = vim_regexec(regmatch, s, (colnr_T)0);
3715 else
3716 {
3717 score = fuzzy_match_str(s, pat);
3718 match = (score != 0);
3719 }
3720 }
3721 else
3722 match = TRUE; // match everything
3723
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724 *e = keep;
3725
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003726 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003727 {
3728 if (ga_grow(&ga, 1) == FAIL)
3729 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003730 if (!fuzzy)
3731 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3732 else
3733 {
3734 fuzmatch_str_T *fuzmatch =
3735 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003736 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003737 fuzmatch->str = vim_strnsave(s, e - s);
3738 fuzmatch->score = score;
3739 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003740 ++ga.ga_len;
3741 }
3742
3743 if (*e != NUL)
3744 ++e;
3745 }
3746 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003747
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003748 if (ga.ga_len == 0)
3749 return OK;
3750
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003751 if (!fuzzy)
3752 {
3753 *matches = ga.ga_data;
3754 *numMatches = ga.ga_len;
3755 }
3756 else
3757 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003758 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3759 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003760 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003761 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003762 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003763 return OK;
3764}
3765
3766/*
3767 * Expand names with a list returned by a function defined by the user.
3768 */
3769 static int
3770ExpandUserList(
3771 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003772 char_u ***matches,
3773 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003774{
3775 list_T *retlist;
3776 listitem_T *li;
3777 garray_T ga;
3778
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003779 *matches = NULL;
3780 *numMatches = 0;
3781 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782 if (retlist == NULL)
3783 return FAIL;
3784
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003785 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003786 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003787 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003788 {
3789 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3790 continue; // Skip non-string items and empty strings
3791
3792 if (ga_grow(&ga, 1) == FAIL)
3793 break;
3794
3795 ((char_u **)ga.ga_data)[ga.ga_len] =
3796 vim_strsave(li->li_tv.vval.v_string);
3797 ++ga.ga_len;
3798 }
3799 list_unref(retlist);
3800
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003801 *matches = ga.ga_data;
3802 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003803 return OK;
3804}
K.Takata161b6ac2022-11-14 15:31:07 +00003805#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003806
3807/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003808 * Expand "file" for all comma-separated directories in "path".
3809 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003810 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 */
3812 void
3813globpath(
3814 char_u *path,
3815 char_u *file,
3816 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003817 int expand_options,
3818 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003819{
3820 expand_T xpc;
3821 char_u *buf;
3822 int i;
3823 int num_p;
3824 char_u **p;
3825
3826 buf = alloc(MAXPATHL);
3827 if (buf == NULL)
3828 return;
3829
3830 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003831 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003832
3833 // Loop over all entries in {path}.
3834 while (*path != NUL)
3835 {
3836 // Copy one item of the path to buf[] and concatenate the file name.
3837 copy_option_part(&path, buf, MAXPATHL, ",");
3838 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3839 {
K.Takata161b6ac2022-11-14 15:31:07 +00003840#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003841 // Using the platform's path separator (\) makes vim incorrectly
3842 // treat it as an escape character, use '/' instead.
3843 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3844 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003845#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003846 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003847#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003848 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003849 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003850 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3851 {
3852 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3853
3854 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003855 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856 for (i = 0; i < num_p; ++i)
3857 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003858 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003859 ++ga->ga_len;
3860 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003861 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003862 }
3863 }
3864 }
3865
3866 vim_free(buf);
3867}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003868
Bram Moolenaareadee482020-09-04 15:37:31 +02003869/*
3870 * Translate some keys pressed when 'wildmenu' is used.
3871 */
3872 int
3873wildmenu_translate_key(
3874 cmdline_info_T *cclp,
3875 int key,
3876 expand_T *xp,
3877 int did_wild_list)
3878{
3879 int c = key;
3880
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003881 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003882 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003883 // When the popup menu is used for cmdline completion:
3884 // Up : go to the previous item in the menu
3885 // Down : go to the next item in the menu
3886 // Left : go to the parent directory
3887 // Right: list the files in the selected directory
3888 switch (c)
3889 {
3890 case K_UP: c = K_LEFT; break;
3891 case K_DOWN: c = K_RIGHT; break;
3892 case K_LEFT: c = K_UP; break;
3893 case K_RIGHT: c = K_DOWN; break;
3894 default: break;
3895 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003896 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003897
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003898 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003899 {
3900 if (c == K_LEFT)
3901 c = Ctrl_P;
3902 else if (c == K_RIGHT)
3903 c = Ctrl_N;
3904 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003905
Bram Moolenaareadee482020-09-04 15:37:31 +02003906 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003907 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003908 && cclp->cmdpos > 1
3909 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3910 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3911 && (c == '\n' || c == '\r' || c == K_KENTER))
3912 c = K_DOWN;
3913
3914 return c;
3915}
3916
3917/*
3918 * Delete characters on the command line, from "from" to the current
3919 * position.
3920 */
3921 static void
3922cmdline_del(cmdline_info_T *cclp, int from)
3923{
3924 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3925 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3926 cclp->cmdlen -= cclp->cmdpos - from;
3927 cclp->cmdpos = from;
3928}
3929
3930/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003931 * Handle a key pressed when the wild menu for the menu names
3932 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003933 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003934 static int
3935wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003936{
Bram Moolenaareadee482020-09-04 15:37:31 +02003937 int i;
3938 int j;
3939
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003940 // Hitting <Down> after "emenu Name.": complete submenu
3941 if (key == K_DOWN && cclp->cmdpos > 0
3942 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003943 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003944 key = p_wc;
3945 KeyTyped = TRUE; // in case the key was mapped
3946 }
3947 else if (key == K_UP)
3948 {
3949 // Hitting <Up>: Remove one submenu name in front of the
3950 // cursor
3951 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003952
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003953 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3954 i = 0;
3955 while (--j > 0)
3956 {
3957 // check for start of menu name
3958 if (cclp->cmdbuff[j] == ' '
3959 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003960 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003961 i = j + 1;
3962 break;
3963 }
3964 // check for start of submenu name
3965 if (cclp->cmdbuff[j] == '.'
3966 && cclp->cmdbuff[j - 1] != '\\')
3967 {
3968 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003969 {
3970 i = j + 1;
3971 break;
3972 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003973 else
3974 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003975 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003976 }
3977 if (i > 0)
3978 cmdline_del(cclp, i);
3979 key = p_wc;
3980 KeyTyped = TRUE; // in case the key was mapped
3981 xp->xp_context = EXPAND_NOTHING;
3982 }
3983
3984 return key;
3985}
3986
3987/*
3988 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3989 * directory names (EXPAND_DIRECTORIES) or shell command names
3990 * (EXPAND_SHELLCMD) is displayed.
3991 */
3992 static int
3993wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3994{
3995 int i;
3996 int j;
3997 char_u upseg[5];
3998
3999 upseg[0] = PATHSEP;
4000 upseg[1] = '.';
4001 upseg[2] = '.';
4002 upseg[3] = PATHSEP;
4003 upseg[4] = NUL;
4004
4005 if (key == K_DOWN
4006 && cclp->cmdpos > 0
4007 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4008 && (cclp->cmdpos < 3
4009 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4010 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4011 {
4012 // go down a directory
4013 key = p_wc;
4014 KeyTyped = TRUE; // in case the key was mapped
4015 }
4016 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4017 {
4018 // If in a direct ancestor, strip off one ../ to go down
4019 int found = FALSE;
4020
4021 j = cclp->cmdpos;
4022 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4023 while (--j > i)
4024 {
4025 if (has_mbyte)
4026 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4027 if (vim_ispathsep(cclp->cmdbuff[j]))
4028 {
4029 found = TRUE;
4030 break;
4031 }
4032 }
4033 if (found
4034 && cclp->cmdbuff[j - 1] == '.'
4035 && cclp->cmdbuff[j - 2] == '.'
4036 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4037 {
4038 cmdline_del(cclp, j - 2);
4039 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004040 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004041 }
4042 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004043 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004044 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004045 // go up a directory
4046 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004047
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004048 j = cclp->cmdpos - 1;
4049 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4050 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004051 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004052 if (has_mbyte)
4053 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4054 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004055#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004056 && vim_strchr((char_u *)" *?[{`$%#",
4057 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004058#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004059 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004060 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004061 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004062 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004063 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004064 break;
4065 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004066 else
4067 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004068 }
4069 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004070
4071 if (!found)
4072 j = i;
4073 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4074 j += 4;
4075 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4076 && j == i)
4077 j += 3;
4078 else
4079 j = 0;
4080 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004081 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004082 // TODO this is only for DOS/UNIX systems - need to put in
4083 // machine-specific stuff here and in upseg init
4084 cmdline_del(cclp, j);
4085 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004086 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004087 else if (cclp->cmdpos > i)
4088 cmdline_del(cclp, i);
4089
4090 // Now complete in the new directory. Set KeyTyped in case the
4091 // Up key came from a mapping.
4092 key = p_wc;
4093 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004094 }
4095
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004096 return key;
4097}
4098
4099/*
4100 * Handle a key pressed when the wild menu is displayed
4101 */
4102 int
4103wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4104{
4105 if (xp->xp_context == EXPAND_MENUNAMES)
4106 return wildmenu_process_key_menunames(cclp, key, xp);
4107 else if ((xp->xp_context == EXPAND_FILES
4108 || xp->xp_context == EXPAND_DIRECTORIES
4109 || xp->xp_context == EXPAND_SHELLCMD))
4110 return wildmenu_process_key_filenames(cclp, key, xp);
4111
4112 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004113}
4114
4115/*
4116 * Free expanded names when finished walking through the matches
4117 */
4118 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004119wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004120{
4121 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004122
4123 if (!p_wmnu || wild_menu_showing == 0)
4124 return;
4125
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004126#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004127 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004128 if (cclp->input_fn)
4129 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004130#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004131
4132 if (wild_menu_showing == WM_SCROLLED)
4133 {
4134 // Entered command line, move it up
4135 cmdline_row--;
4136 redrawcmd();
4137 }
4138 else if (save_p_ls != -1)
4139 {
4140 // restore 'laststatus' and 'winminheight'
4141 p_ls = save_p_ls;
4142 p_wmh = save_p_wmh;
4143 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004144 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004145 redrawcmd();
4146 save_p_ls = -1;
4147 }
4148 else
4149 {
4150 win_redraw_last_status(topframe);
4151 redraw_statuslines();
4152 }
4153 KeyTyped = skt;
4154 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004155#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004156 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004157 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004158#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004159}
Bram Moolenaareadee482020-09-04 15:37:31 +02004160
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004161#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004162/*
4163 * "getcompletion()" function
4164 */
4165 void
4166f_getcompletion(typval_T *argvars, typval_T *rettv)
4167{
4168 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004169 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004170 expand_T xpc;
4171 int filtered = FALSE;
4172 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004173 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004174
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004175 if (in_vim9script()
4176 && (check_for_string_arg(argvars, 0) == FAIL
4177 || check_for_string_arg(argvars, 1) == FAIL
4178 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4179 return;
4180
ii144785fe02021-11-21 12:13:56 +00004181 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004182 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004183 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004184 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004185
4186 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004187 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004188
4189 if (p_wic)
4190 options |= WILD_ICASE;
4191
4192 // For filtered results, 'wildignore' is used
4193 if (!filtered)
4194 options |= WILD_KEEP_ALL;
4195
4196 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004197 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004198 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004199 int cmdline_len = (int)STRLEN(pat);
4200 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004201 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004202 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004203 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004204 else
4205 {
ii144785fe02021-11-21 12:13:56 +00004206 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004207 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004208 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004209
4210 xpc.xp_context = cmdcomplete_str_to_type(type);
4211 if (xpc.xp_context == EXPAND_NOTHING)
4212 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004213 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004214 return;
4215 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004216
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004217 if (xpc.xp_context == EXPAND_USER_DEFINED)
4218 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004219 // Must be "custom,funcname" pattern
4220 if (STRNCMP(type, "custom,", 7) != 0)
4221 {
4222 semsg(_(e_invalid_argument_str), type);
4223 return;
4224 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004225
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004226 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004227 }
4228
4229 if (xpc.xp_context == EXPAND_USER_LIST)
4230 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004231 // Must be "customlist,funcname" pattern
4232 if (STRNCMP(type, "customlist,", 11) != 0)
4233 {
4234 semsg(_(e_invalid_argument_str), type);
4235 return;
4236 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004237
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004238 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004239 }
4240
Bram Moolenaar66b51422019-08-18 21:44:12 +02004241# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004242 if (xpc.xp_context == EXPAND_MENUS)
4243 {
4244 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4245 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4246 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004247# endif
4248# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004249 if (xpc.xp_context == EXPAND_CSCOPE)
4250 {
4251 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4252 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4253 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004254# endif
4255# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004256 if (xpc.xp_context == EXPAND_SIGN)
4257 {
4258 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4259 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4260 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004261# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004262 if (xpc.xp_context == EXPAND_RUNTIME)
4263 {
4264 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4265 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4266 }
zeertzjq85f36d62024-10-10 19:14:13 +02004267 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4268 {
4269 int context = EXPAND_SHELLCMDLINE;
4270 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4271 &context);
4272 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4273 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004274 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004275
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004276 if (cmdline_fuzzy_completion_supported(&xpc))
4277 // when fuzzy matching, don't modify the search string
4278 pat = vim_strsave(xpc.xp_pattern);
4279 else
4280 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4281
Bram Moolenaar93a10962022-06-16 11:42:09 +01004282 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004283 {
4284 int i;
4285
4286 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4287
4288 for (i = 0; i < xpc.xp_numfiles; i++)
4289 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4290 }
4291 vim_free(pat);
4292 ExpandCleanup(&xpc);
4293}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004294#endif // FEAT_EVAL