blob: 406135cffba7de6feafb4230c4a1769b1a3b7464 [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
53 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010054 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020056 && xp->xp_context != EXPAND_STRING_SETTING
57 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OWNSYNTAX
59 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000060 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020062 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_TAGS
64 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000065 && xp->xp_context != EXPAND_USER_LIST);
66}
67
68/*
69 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000070 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
71 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000072 */
73 int
74cmdline_fuzzy_complete(char_u *fuzzystr)
75{
76 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
77}
78
79/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000080 * sort function for the completion matches.
81 * <SNR> functions should be sorted to the end.
82 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020083 static int
84sort_func_compare(const void *s1, const void *s2)
85{
86 char_u *p1 = *(char_u **)s1;
87 char_u *p2 = *(char_u **)s2;
88
89 if (*p1 != '<' && *p2 == '<') return -1;
90 if (*p1 == '<' && *p2 != '<') return 1;
91 return STRCMP(p1, p2);
92}
Bram Moolenaar66b51422019-08-18 21:44:12 +020093
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000094/*
95 * Escape special characters in the cmdline completion matches.
96 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020097 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000098wildescape(
99 expand_T *xp,
100 char_u *str,
101 int numfiles,
102 char_u **files)
103{
104 char_u *p;
105 int vse_what = xp->xp_context == EXPAND_BUFFERS
106 ? VSE_BUFFER : VSE_NONE;
107
108 if (xp->xp_context == EXPAND_FILES
109 || xp->xp_context == EXPAND_FILES_IN_PATH
110 || xp->xp_context == EXPAND_SHELLCMD
111 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200112 || xp->xp_context == EXPAND_DIRECTORIES
113 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000114 {
115 // Insert a backslash into a file name before a space, \, %, #
116 // and wildmatch characters, except '~'.
117 for (int i = 0; i < numfiles; ++i)
118 {
119 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200120 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000121 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200122 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
123 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000124 if (p != NULL)
125 {
126 vim_free(files[i]);
127 files[i] = p;
128#if defined(BACKSLASH_IN_FILENAME)
129 p = vim_strsave_escaped(files[i], (char_u *)" ");
130 if (p != NULL)
131 {
132 vim_free(files[i]);
133 files[i] = p;
134 }
135#endif
136 }
137 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200138 else if (xp->xp_backslash & XP_BS_COMMA)
139 {
140 if (vim_strchr(files[i], ',') != NULL)
141 {
142 p = vim_strsave_escaped(files[i], (char_u *)",");
143 if (p != NULL)
144 {
145 vim_free(files[i]);
146 files[i] = p;
147 }
148 }
149 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000150#ifdef BACKSLASH_IN_FILENAME
151 p = vim_strsave_fnameescape(files[i], vse_what);
152#else
153 p = vim_strsave_fnameescape(files[i],
154 xp->xp_shell ? VSE_SHELL : vse_what);
155#endif
156 if (p != NULL)
157 {
158 vim_free(files[i]);
159 files[i] = p;
160 }
161
162 // If 'str' starts with "\~", replace "~" at start of
163 // files[i] with "\~".
164 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
165 escape_fname(&files[i]);
166 }
167 xp->xp_backslash = XP_BS_NONE;
168
169 // If the first file starts with a '+' escape it. Otherwise it
170 // could be seen as "+cmd".
171 if (*files[0] == '+')
172 escape_fname(&files[0]);
173 }
174 else if (xp->xp_context == EXPAND_TAGS)
175 {
176 // Insert a backslash before characters in a tag name that
177 // would terminate the ":tag" command.
178 for (int i = 0; i < numfiles; ++i)
179 {
180 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
181 if (p != NULL)
182 {
183 vim_free(files[i]);
184 files[i] = p;
185 }
186 }
187 }
188}
189
190/*
191 * Escape special characters in the cmdline completion matches.
192 */
193 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200194ExpandEscape(
195 expand_T *xp,
196 char_u *str,
197 int numfiles,
198 char_u **files,
199 int options)
200{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200201 // May change home directory back to "~"
202 if (options & WILD_HOME_REPLACE)
203 tilde_replace(str, numfiles, files);
204
205 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000206 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200207}
208
209/*
210 * Return FAIL if this is not an appropriate context in which to do
211 * completion of anything, return OK if it is (even if there are no matches).
212 * For the caller, this means that the character is just passed through like a
213 * normal character (instead of being expanded). This allows :s/^I^D etc.
214 */
215 int
216nextwild(
217 expand_T *xp,
218 int type,
219 int options, // extra options for ExpandOne()
220 int escape) // if TRUE, escape the returned matches
221{
222 cmdline_info_T *ccline = get_cmdline_info();
223 int i, j;
224 char_u *p1;
225 char_u *p2;
226 int difflen;
227 int v;
228
229 if (xp->xp_numfiles == -1)
230 {
231 set_expand_context(xp);
232 cmd_showtail = expand_showtail(xp);
233 }
234
235 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
236 {
237 beep_flush();
238 return OK; // Something illegal on command line
239 }
240 if (xp->xp_context == EXPAND_NOTHING)
241 {
242 // Caller can use the character as a normal char instead
243 return FAIL;
244 }
245
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000246 // If cmd_silent is set then don't show the dots, because redrawcmd() below
247 // won't remove them.
248 if (!cmd_silent)
249 {
250 msg_puts("..."); // show that we are busy
251 out_flush();
252 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200253
254 i = (int)(xp->xp_pattern - ccline->cmdbuff);
255 xp->xp_pattern_len = ccline->cmdpos - i;
256
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000257 if (type == WILD_NEXT || type == WILD_PREV
258 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200259 {
260 // Get next/previous match for a previous expanded pattern.
261 p2 = ExpandOne(xp, NULL, NULL, 0, type);
262 }
263 else
264 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000265 if (cmdline_fuzzy_completion_supported(xp))
266 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200267 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000268 else
269 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
270
Bram Moolenaar66b51422019-08-18 21:44:12 +0200271 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000272 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200273 p2 = NULL;
274 else
275 {
276 int use_options = options |
277 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
278 if (escape)
279 use_options |= WILD_ESCAPE;
280
281 if (p_wic)
282 use_options += WILD_ICASE;
283 p2 = ExpandOne(xp, p1,
284 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
285 use_options, type);
286 vim_free(p1);
287 // longest match: make sure it is not shorter, happens with :help
288 if (p2 != NULL && type == WILD_LONGEST)
289 {
290 for (j = 0; j < xp->xp_pattern_len; ++j)
291 if (ccline->cmdbuff[i + j] == '*'
292 || ccline->cmdbuff[i + j] == '?')
293 break;
294 if ((int)STRLEN(p2) < j)
295 VIM_CLEAR(p2);
296 }
297 }
298 }
299
300 if (p2 != NULL && !got_int)
301 {
302 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
303 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
304 {
305 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
306 xp->xp_pattern = ccline->cmdbuff + i;
307 }
308 else
309 v = OK;
310 if (v == OK)
311 {
312 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
313 &ccline->cmdbuff[ccline->cmdpos],
314 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
315 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
316 ccline->cmdlen += difflen;
317 ccline->cmdpos += difflen;
318 }
319 }
320 vim_free(p2);
321
322 redrawcmd();
323 cursorcmd();
324
325 // When expanding a ":map" command and no matches are found, assume that
326 // the key is supposed to be inserted literally
327 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
328 return FAIL;
329
330 if (xp->xp_numfiles <= 0 && p2 == NULL)
331 beep_flush();
332 else if (xp->xp_numfiles == 1)
333 // free expanded pattern
334 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
335
336 return OK;
337}
338
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339/*
340 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 */
343 static int
344cmdline_pum_create(
345 cmdline_info_T *ccline,
346 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000347 char_u **matches,
348 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000349 int showtail)
350{
351 int i;
352 int columns;
353
354 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000357 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000358 {
zeertzjqc51a3762022-12-10 10:22:29 +0000359 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000360 compl_match_array[i].pum_info = NULL;
361 compl_match_array[i].pum_extra = NULL;
362 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200363 compl_match_array[i].pum_user_abbr_hlattr = -1;
364 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000365 }
366
367 // Compute the popup menu starting column
368 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
369 columns = vim_strsize(xp->xp_pattern);
370 if (showtail)
371 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000372 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000373 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000374 }
375 if (columns >= compl_startcol)
376 compl_startcol = 0;
377 else
378 compl_startcol -= columns;
379
380 // no default selection
381 compl_selected = -1;
382
383 cmdline_pum_display();
384
385 return EXPAND_OK;
386}
387
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000388/*
389 * Display the cmdline completion matches in a popup menu
390 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000391 void
392cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000393{
394 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
395}
396
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000397/*
398 * Returns TRUE if the cmdline completion popup menu is being displayed.
399 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000400 int
401cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100403 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000404}
405
406/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000407 * Remove the cmdline completion popup menu (if present), free the list of
408 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000409 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000410 void
411cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000413 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100414 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000415
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416 pum_undisplay();
417 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000418 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000419 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000420 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000421 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100422
423 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
424 // as a side effect.
425 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000426}
427
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000428 void
429cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000430{
431 cmdline_pum_remove();
432 wildmenu_cleanup(cclp);
433}
434
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000435/*
436 * Returns the starting column number to use for the cmdline completion popup
437 * menu.
438 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000439 int
440cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000441{
442 return compl_startcol;
443}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000444
Bram Moolenaar66b51422019-08-18 21:44:12 +0200445/*
zeertzjqd8c93402024-06-17 18:25:32 +0200446 * Returns the current cmdline completion pattern.
447 */
448 char_u *
449cmdline_compl_pattern(void)
450{
451 expand_T *xp = get_cmdline_info()->xpc;
452
453 return xp == NULL ? NULL : xp->xp_orig;
454}
455
456/*
457 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
458 */
459 int
460cmdline_compl_is_fuzzy(void)
461{
462 expand_T *xp = get_cmdline_info()->xpc;
463
464 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
465}
466
467/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000468 * Return the number of characters that should be skipped in a status match.
469 * These are backslashes used for escaping. Do show backslashes in help tags.
470 */
471 static int
472skip_status_match_char(expand_T *xp, char_u *s)
473{
474 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
475#ifdef FEAT_MENU
476 || ((xp->xp_context == EXPAND_MENUS
477 || xp->xp_context == EXPAND_MENUNAMES)
478 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
479#endif
480 )
481 {
482#ifndef BACKSLASH_IN_FILENAME
483 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
484 return 2;
485#endif
486 return 1;
487 }
488 return 0;
489}
490
491/*
492 * Get the length of an item as it will be shown in the status line.
493 */
494 static int
495status_match_len(expand_T *xp, char_u *s)
496{
497 int len = 0;
498
499#ifdef FEAT_MENU
500 int emenu = xp->xp_context == EXPAND_MENUS
501 || xp->xp_context == EXPAND_MENUNAMES;
502
503 // Check for menu separators - replace with '|'.
504 if (emenu && menu_is_separator(s))
505 return 1;
506#endif
507
508 while (*s != NUL)
509 {
510 s += skip_status_match_char(xp, s);
511 len += ptr2cells(s);
512 MB_PTR_ADV(s);
513 }
514
515 return len;
516}
517
518/*
519 * Show wildchar matches in the status line.
520 * Show at least the "match" item.
521 * We start at item 'first_match' in the list and show all matches that fit.
522 *
523 * If inversion is possible we use it. Else '=' characters are used.
524 */
525 static void
526win_redr_status_matches(
527 expand_T *xp,
528 int num_matches,
529 char_u **matches, // list of matches
530 int match,
531 int showtail)
532{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000533 int row;
534 char_u *buf;
535 int len;
536 int clen; // length in screen cells
537 int fillchar;
538 int attr;
539 int i;
540 int highlight = TRUE;
541 char_u *selstart = NULL;
542 int selstart_col = 0;
543 char_u *selend = NULL;
544 static int first_match = 0;
545 int add_left = FALSE;
546 char_u *s;
547#ifdef FEAT_MENU
548 int emenu;
549#endif
550 int l;
551
552 if (matches == NULL) // interrupted completion?
553 return;
554
555 if (has_mbyte)
556 buf = alloc(Columns * MB_MAXBYTES + 1);
557 else
558 buf = alloc(Columns + 1);
559 if (buf == NULL)
560 return;
561
562 if (match == -1) // don't show match but original text
563 {
564 match = 0;
565 highlight = FALSE;
566 }
567 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000568 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000569 if (match == 0)
570 first_match = 0;
571 else if (match < first_match)
572 {
573 // jumping left, as far as we can go
574 first_match = match;
575 add_left = TRUE;
576 }
577 else
578 {
579 // check if match fits on the screen
580 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000581 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000582 if (first_match > 0)
583 clen += 2;
584 // jumping right, put match at the left
585 if ((long)clen > Columns)
586 {
587 first_match = match;
588 // if showing the last match, we can add some on the left
589 clen = 2;
590 for (i = match; i < num_matches; ++i)
591 {
zeertzjqc51a3762022-12-10 10:22:29 +0000592 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000593 if ((long)clen >= Columns)
594 break;
595 }
596 if (i == num_matches)
597 add_left = TRUE;
598 }
599 }
600 if (add_left)
601 while (first_match > 0)
602 {
zeertzjqc51a3762022-12-10 10:22:29 +0000603 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000604 if ((long)clen >= Columns)
605 break;
606 --first_match;
607 }
608
609 fillchar = fillchar_status(&attr, curwin);
610
611 if (first_match == 0)
612 {
613 *buf = NUL;
614 len = 0;
615 }
616 else
617 {
618 STRCPY(buf, "< ");
619 len = 2;
620 }
621 clen = len;
622
623 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000624 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000625 {
626 if (i == match)
627 {
628 selstart = buf + len;
629 selstart_col = clen;
630 }
631
zeertzjqc51a3762022-12-10 10:22:29 +0000632 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000633 // Check for menu separators - replace with '|'
634#ifdef FEAT_MENU
635 emenu = (xp->xp_context == EXPAND_MENUS
636 || xp->xp_context == EXPAND_MENUNAMES);
637 if (emenu && menu_is_separator(s))
638 {
639 STRCPY(buf + len, transchar('|'));
640 l = (int)STRLEN(buf + len);
641 len += l;
642 clen += l;
643 }
644 else
645#endif
646 for ( ; *s != NUL; ++s)
647 {
648 s += skip_status_match_char(xp, s);
649 clen += ptr2cells(s);
650 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
651 {
652 STRNCPY(buf + len, s, l);
653 s += l - 1;
654 len += l;
655 }
656 else
657 {
658 STRCPY(buf + len, transchar_byte(*s));
659 len += (int)STRLEN(buf + len);
660 }
661 }
662 if (i == match)
663 selend = buf + len;
664
665 *(buf + len++) = ' ';
666 *(buf + len++) = ' ';
667 clen += 2;
668 if (++i == num_matches)
669 break;
670 }
671
672 if (i != num_matches)
673 {
674 *(buf + len++) = '>';
675 ++clen;
676 }
677
678 buf[len] = NUL;
679
680 row = cmdline_row - 1;
681 if (row >= 0)
682 {
683 if (wild_menu_showing == 0)
684 {
685 if (msg_scrolled > 0)
686 {
687 // Put the wildmenu just above the command line. If there is
688 // no room, scroll the screen one line up.
689 if (cmdline_row == Rows - 1)
690 {
691 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
692 ++msg_scrolled;
693 }
694 else
695 {
696 ++cmdline_row;
697 ++row;
698 }
699 wild_menu_showing = WM_SCROLLED;
700 }
701 else
702 {
703 // Create status line if needed by setting 'laststatus' to 2.
704 // Set 'winminheight' to zero to avoid that the window is
705 // resized.
706 if (lastwin->w_status_height == 0)
707 {
708 save_p_ls = p_ls;
709 save_p_wmh = p_wmh;
710 p_ls = 2;
711 p_wmh = 0;
712 last_status(FALSE);
713 }
714 wild_menu_showing = WM_SHOWN;
715 }
716 }
717
718 screen_puts(buf, row, 0, attr);
719 if (selstart != NULL && highlight)
720 {
721 *selend = NUL;
722 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
723 }
724
725 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
726 }
727
728 win_redraw_last_status(topframe);
729 vim_free(buf);
730}
731
732/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000733 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200734 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000735 */
736 static char_u *
737get_next_or_prev_match(
738 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200739 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000740{
zeertzjqe9ef3472023-08-17 23:57:05 +0200741 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000742 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000743
744 if (xp->xp_numfiles <= 0)
745 return NULL;
746
747 if (mode == WILD_PREV)
748 {
749 if (findex == -1)
750 findex = xp->xp_numfiles;
751 --findex;
752 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000753 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000754 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000755 else if (mode == WILD_PAGEUP)
756 {
757 if (findex == 0)
758 // at the first entry, don't select any entries
759 findex = -1;
760 else if (findex == -1)
761 // no entry is selected. select the last entry
762 findex = xp->xp_numfiles - 1;
763 else
764 {
765 // go up by the pum height
766 ht = pum_get_height();
767 if (ht > 3)
768 ht -= 2;
769 findex -= ht;
770 if (findex < 0)
771 // few entries left, select the first entry
772 findex = 0;
773 }
774 }
775 else // mode == WILD_PAGEDOWN
776 {
777 if (findex == xp->xp_numfiles - 1)
778 // at the last entry, don't select any entries
779 findex = -1;
780 else if (findex == -1)
781 // no entry is selected. select the first entry
782 findex = 0;
783 else
784 {
785 // go down by the pum height
786 ht = pum_get_height();
787 if (ht > 3)
788 ht -= 2;
789 findex += ht;
790 if (findex >= xp->xp_numfiles)
791 // few entries left, select the last entry
792 findex = xp->xp_numfiles - 1;
793 }
794 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000796 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000797 if (findex < 0)
798 {
zeertzjq28a23602023-09-29 19:58:35 +0200799 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000800 findex = xp->xp_numfiles - 1;
801 else
802 findex = -1;
803 }
804 if (findex >= xp->xp_numfiles)
805 {
zeertzjq28a23602023-09-29 19:58:35 +0200806 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000807 findex = 0;
808 else
809 findex = -1;
810 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000811 if (compl_match_array)
812 {
813 compl_selected = findex;
814 cmdline_pum_display();
815 }
816 else if (p_wmnu)
817 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
818 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200819 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000820
821 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200822 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000823
824 return vim_strsave(xp->xp_files[findex]);
825}
826
827/*
828 * Start the command-line expansion and get the matches.
829 */
830 static char_u *
831ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
832{
833 int non_suf_match; // number without matching suffix
834 int i;
835 char_u *ss = NULL;
836
837 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000838 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000839 options) == FAIL)
840 {
841#ifdef FNAME_ILLEGAL
842 // Illegal file name has been silently skipped. But when there
843 // are wildcards, the real problem is that there was no match,
844 // causing the pattern to be added, which has illegal characters.
845 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
846 semsg(_(e_no_match_str_2), str);
847#endif
848 }
849 else if (xp->xp_numfiles == 0)
850 {
851 if (!(options & WILD_SILENT))
852 semsg(_(e_no_match_str_2), str);
853 }
854 else
855 {
856 // Escape the matches for use on the command line.
857 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
858
859 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000860 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000861 {
862 if (xp->xp_numfiles)
863 non_suf_match = xp->xp_numfiles;
864 else
865 non_suf_match = 1;
866 if ((xp->xp_context == EXPAND_FILES
867 || xp->xp_context == EXPAND_DIRECTORIES)
868 && xp->xp_numfiles > 1)
869 {
870 // More than one match; check suffix.
871 // The files will have been sorted on matching suffix in
872 // expand_wildcards, only need to check the first two.
873 non_suf_match = 0;
874 for (i = 0; i < 2; ++i)
875 if (match_suffix(xp->xp_files[i]))
876 ++non_suf_match;
877 }
878 if (non_suf_match != 1)
879 {
880 // Can we ever get here unless it's while expanding
881 // interactively? If not, we can get rid of this all
882 // together. Don't really want to wait for this message
883 // (and possibly have to hit return to continue!).
884 if (!(options & WILD_SILENT))
885 emsg(_(e_too_many_file_names));
886 else if (!(options & WILD_NO_BEEP))
887 beep_flush();
888 }
889 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
890 ss = vim_strsave(xp->xp_files[0]);
891 }
892 }
893
894 return ss;
895}
896
897/*
898 * Return the longest common part in the list of cmdline completion matches.
899 */
900 static char_u *
901find_longest_match(expand_T *xp, int options)
902{
903 long_u len;
904 int mb_len = 1;
905 int c0, ci;
906 int i;
907 char_u *ss;
908
909 for (len = 0; xp->xp_files[0][len]; len += mb_len)
910 {
911 if (has_mbyte)
912 {
913 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
914 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
915 }
916 else
917 c0 = xp->xp_files[0][len];
918 for (i = 1; i < xp->xp_numfiles; ++i)
919 {
920 if (has_mbyte)
921 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
922 else
923 ci = xp->xp_files[i][len];
924 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
925 || xp->xp_context == EXPAND_FILES
926 || xp->xp_context == EXPAND_SHELLCMD
927 || xp->xp_context == EXPAND_BUFFERS))
928 {
929 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
930 break;
931 }
932 else if (c0 != ci)
933 break;
934 }
935 if (i < xp->xp_numfiles)
936 {
937 if (!(options & WILD_NO_BEEP))
938 vim_beep(BO_WILD);
939 break;
940 }
941 }
942
943 ss = alloc(len + 1);
944 if (ss)
945 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
946
947 return ss;
948}
949
950/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000951 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200952 * Chars that should not be expanded must be preceded with a backslash.
953 * Return a pointer to allocated memory containing the new string.
954 * Return NULL for failure.
955 *
956 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200957 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
958 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200959 *
960 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
961 * is WILD_EXPAND_FREE or WILD_ALL.
962 *
963 * mode = WILD_FREE: just free previously expanded matches
964 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
965 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
966 * mode = WILD_NEXT: use next match in multiple match, wrap to first
967 * mode = WILD_PREV: use previous match in multiple match, wrap to first
968 * mode = WILD_ALL: return all matches concatenated
969 * mode = WILD_LONGEST: return longest matched part
970 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000971 * mode = WILD_APPLY: apply the item selected in the cmdline completion
972 * popup menu and close the menu.
973 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
974 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200975 *
976 * options = WILD_LIST_NOTFOUND: list entries without a match
977 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
978 * options = WILD_USE_NL: Use '\n' for WILD_ALL
979 * options = WILD_NO_BEEP: Don't beep for multiple matches
980 * options = WILD_ADD_SLASH: add a slash after directory names
981 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
982 * options = WILD_SILENT: don't print warning messages
983 * options = WILD_ESCAPE: put backslash before special chars
984 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200985 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 *
987 * The variables xp->xp_context and xp->xp_backslash must have been set!
988 */
989 char_u *
990ExpandOne(
991 expand_T *xp,
992 char_u *str,
993 char_u *orig, // allocated copy of original of expanded string
994 int options,
995 int mode)
996{
997 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200998 int orig_saved = FALSE;
999 int i;
1000 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001001
1002 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001003 if (mode == WILD_NEXT || mode == WILD_PREV
1004 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001005 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001006
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001007 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001008 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001009 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001010 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001011 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001012 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001013
Bram Moolenaar66b51422019-08-18 21:44:12 +02001014 // free old names
1015 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1016 {
1017 FreeWild(xp->xp_numfiles, xp->xp_files);
1018 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001019 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001020
1021 // The entries from xp_files may be used in the PUM, remove it.
1022 if (compl_match_array != NULL)
1023 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001024 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001025 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026
1027 if (mode == WILD_FREE) // only release file name
1028 return NULL;
1029
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001030 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001031 {
zeertzjq28a23602023-09-29 19:58:35 +02001032 vim_free(xp->xp_orig);
1033 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001034 orig_saved = TRUE;
1035
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001036 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037 }
1038
1039 // Find longest common part
1040 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1041 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001042 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001043 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001044 }
1045
Bram Moolenaar57e95172022-08-20 19:26:14 +01001046 // Concatenate all matching names. Unless interrupted, this can be slow
1047 // and the result probably won't be used.
1048 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 {
1050 len = 0;
1051 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001052 {
1053 if (i > 0)
1054 {
1055 if (xp->xp_prefix == XP_PREFIX_NO)
1056 len += 2; // prefix "no"
1057 else if (xp->xp_prefix == XP_PREFIX_INV)
1058 len += 3; // prefix "inv"
1059 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001060 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001061 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001062 ss = alloc(len);
1063 if (ss != NULL)
1064 {
1065 *ss = NUL;
1066 for (i = 0; i < xp->xp_numfiles; ++i)
1067 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001068 if (i > 0)
1069 {
1070 if (xp->xp_prefix == XP_PREFIX_NO)
1071 STRCAT(ss, "no");
1072 else if (xp->xp_prefix == XP_PREFIX_INV)
1073 STRCAT(ss, "inv");
1074 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001076
Bram Moolenaar66b51422019-08-18 21:44:12 +02001077 if (i != xp->xp_numfiles - 1)
1078 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1079 }
1080 }
1081 }
1082
1083 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1084 ExpandCleanup(xp);
1085
zeertzjq28a23602023-09-29 19:58:35 +02001086 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001087 if (!orig_saved)
1088 vim_free(orig);
1089
1090 return ss;
1091}
1092
1093/*
1094 * Prepare an expand structure for use.
1095 */
1096 void
1097ExpandInit(expand_T *xp)
1098{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001099 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001100 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001101 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001103}
1104
1105/*
1106 * Cleanup an expand structure after use.
1107 */
1108 void
1109ExpandCleanup(expand_T *xp)
1110{
1111 if (xp->xp_numfiles >= 0)
1112 {
1113 FreeWild(xp->xp_numfiles, xp->xp_files);
1114 xp->xp_numfiles = -1;
1115 }
zeertzjq28a23602023-09-29 19:58:35 +02001116 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001117}
1118
1119/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001120 * Display one line of completion matches. Multiple matches are displayed in
1121 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001122 * matches - list of completion match names
1123 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001124 * lines - number of output lines
1125 * linenr - line number of matches to display
1126 * maxlen - maximum number of characters in each line
1127 * showtail - display only the tail of the full path of a file name
1128 * dir_attr - highlight attribute to use for directory names
1129 */
1130 static void
1131showmatches_oneline(
1132 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001133 char_u **matches,
1134 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001135 int lines,
1136 int linenr,
1137 int maxlen,
1138 int showtail,
1139 int dir_attr)
1140{
1141 int i, j;
1142 int isdir;
1143 int lastlen;
1144 char_u *p;
1145
1146 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001147 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001148 {
1149 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1150 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001151 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1152 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001153 msg_advance(maxlen + 1);
1154 msg_puts((char *)p);
1155 msg_advance(maxlen + 3);
1156 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1157 break;
1158 }
1159 for (i = maxlen - lastlen; --i >= 0; )
1160 msg_putchar(' ');
1161 if (xp->xp_context == EXPAND_FILES
1162 || xp->xp_context == EXPAND_SHELLCMD
1163 || xp->xp_context == EXPAND_BUFFERS)
1164 {
1165 // highlight directories
1166 if (xp->xp_numfiles != -1)
1167 {
1168 char_u *halved_slash;
1169 char_u *exp_path;
1170 char_u *path;
1171
1172 // Expansion was done before and special characters
1173 // were escaped, need to halve backslashes. Also
1174 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001175 exp_path = expand_env_save_opt(matches[j], TRUE);
1176 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001177 halved_slash = backslash_halve_save(path);
1178 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001179 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001180 vim_free(exp_path);
1181 if (halved_slash != path)
1182 vim_free(halved_slash);
1183 }
1184 else
1185 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001186 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001187 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001188 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001189 else
1190 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001192 TRUE);
1193 p = NameBuff;
1194 }
1195 }
1196 else
1197 {
1198 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001199 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001200 }
1201 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1202 }
1203 if (msg_col > 0) // when not wrapped around
1204 {
1205 msg_clr_eos();
1206 msg_putchar('\n');
1207 }
1208 out_flush(); // show one line at a time
1209}
1210
1211/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001212 * Show all matches for completion on the command line.
1213 * Returns EXPAND_NOTHING when the character that triggered expansion should
1214 * be inserted like a normal character.
1215 */
1216 int
1217showmatches(expand_T *xp, int wildmenu UNUSED)
1218{
1219 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001220 int numMatches;
1221 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001222 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 int maxlen;
1224 int lines;
1225 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001226 int attr;
1227 int showtail;
1228
1229 if (xp->xp_numfiles == -1)
1230 {
1231 set_expand_context(xp);
1232 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001233 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001234 showtail = expand_showtail(xp);
1235 if (i != EXPAND_OK)
1236 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 }
1238 else
1239 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001240 numMatches = xp->xp_numfiles;
1241 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001242 showtail = cmd_showtail;
1243 }
1244
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001245 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001246 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001247 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001248
Bram Moolenaar66b51422019-08-18 21:44:12 +02001249 if (!wildmenu)
1250 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001251 msg_didany = FALSE; // lines_left will be set
1252 msg_start(); // prepare for paging
1253 msg_putchar('\n');
1254 out_flush();
1255 cmdline_row = msg_row;
1256 msg_didany = FALSE; // lines_left will be set again
1257 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259
1260 if (got_int)
1261 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001262 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001263 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001264 else
1265 {
1266 // find the length of the longest file name
1267 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001268 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001269 {
1270 if (!showtail && (xp->xp_context == EXPAND_FILES
1271 || xp->xp_context == EXPAND_SHELLCMD
1272 || xp->xp_context == EXPAND_BUFFERS))
1273 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001274 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001275 j = vim_strsize(NameBuff);
1276 }
1277 else
zeertzjqc51a3762022-12-10 10:22:29 +00001278 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001279 if (j > maxlen)
1280 maxlen = j;
1281 }
1282
1283 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001284 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001285 else
1286 {
1287 // compute the number of columns and lines for the listing
1288 maxlen += 2; // two spaces between file names
1289 columns = ((int)Columns + 2) / maxlen;
1290 if (columns < 1)
1291 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001292 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001293 }
1294
1295 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1296
1297 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1298 {
1299 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1300 msg_clr_eos();
1301 msg_advance(maxlen - 3);
1302 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1303 }
1304
1305 // list the files line by line
1306 for (i = 0; i < lines; ++i)
1307 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001308 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001309 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001310 if (got_int)
1311 {
1312 got_int = FALSE;
1313 break;
1314 }
1315 }
1316
1317 // we redraw the command below the lines that we have just listed
1318 // This is a bit tricky, but it saves a lot of screen updating.
1319 cmdline_row = msg_row; // will put it back later
1320 }
1321
1322 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001323 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001324
1325 return EXPAND_OK;
1326}
1327
1328/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001329 * gettail() version for showmatches() and win_redr_status_matches():
1330 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001331 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001332 static char_u *
1333showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001334{
1335 char_u *p;
1336 char_u *t = s;
1337 int had_sep = FALSE;
1338
1339 for (p = s; *p != NUL; )
1340 {
1341 if (vim_ispathsep(*p)
1342#ifdef BACKSLASH_IN_FILENAME
1343 && !rem_backslash(p)
1344#endif
1345 )
1346 had_sep = TRUE;
1347 else if (had_sep)
1348 {
1349 t = p;
1350 had_sep = FALSE;
1351 }
1352 MB_PTR_ADV(p);
1353 }
1354 return t;
1355}
1356
1357/*
1358 * Return TRUE if we only need to show the tail of completion matches.
1359 * When not completing file names or there is a wildcard in the path FALSE is
1360 * returned.
1361 */
1362 static int
1363expand_showtail(expand_T *xp)
1364{
1365 char_u *s;
1366 char_u *end;
1367
1368 // When not completing file names a "/" may mean something different.
1369 if (xp->xp_context != EXPAND_FILES
1370 && xp->xp_context != EXPAND_SHELLCMD
1371 && xp->xp_context != EXPAND_DIRECTORIES)
1372 return FALSE;
1373
1374 end = gettail(xp->xp_pattern);
1375 if (end == xp->xp_pattern) // there is no path separator
1376 return FALSE;
1377
1378 for (s = xp->xp_pattern; s < end; s++)
1379 {
1380 // Skip escaped wildcards. Only when the backslash is not a path
1381 // separator, on DOS the '*' "path\*\file" must not be skipped.
1382 if (rem_backslash(s))
1383 ++s;
1384 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1385 return FALSE;
1386 }
1387 return TRUE;
1388}
1389
1390/*
1391 * Prepare a string for expansion.
1392 * When expanding file names: The string will be used with expand_wildcards().
1393 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1394 * When expanding other names: The string will be used with regcomp(). Copy
1395 * the name into allocated memory and prepend "^".
1396 */
1397 char_u *
1398addstar(
1399 char_u *fname,
1400 int len,
1401 int context) // EXPAND_FILES etc.
1402{
1403 char_u *retval;
1404 int i, j;
1405 int new_len;
1406 char_u *tail;
1407 int ends_in_star;
1408
1409 if (context != EXPAND_FILES
1410 && context != EXPAND_FILES_IN_PATH
1411 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001412 && context != EXPAND_DIRECTORIES
1413 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001414 {
1415 // Matching will be done internally (on something other than files).
1416 // So we convert the file-matching-type wildcards into our kind for
1417 // use with vim_regcomp(). First work out how long it will be:
1418
1419 // For help tags the translation is done in find_help_tags().
1420 // For a tag pattern starting with "/" no translation is needed.
1421 if (context == EXPAND_HELP
1422 || context == EXPAND_COLORS
1423 || context == EXPAND_COMPILER
1424 || context == EXPAND_OWNSYNTAX
1425 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001426 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001427 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001428 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001429 || ((context == EXPAND_TAGS_LISTFILES
1430 || context == EXPAND_TAGS)
1431 && fname[0] == '/'))
1432 retval = vim_strnsave(fname, len);
1433 else
1434 {
1435 new_len = len + 2; // +2 for '^' at start, NUL at end
1436 for (i = 0; i < len; i++)
1437 {
1438 if (fname[i] == '*' || fname[i] == '~')
1439 new_len++; // '*' needs to be replaced by ".*"
1440 // '~' needs to be replaced by "\~"
1441
1442 // Buffer names are like file names. "." should be literal
1443 if (context == EXPAND_BUFFERS && fname[i] == '.')
1444 new_len++; // "." becomes "\."
1445
1446 // Custom expansion takes care of special things, match
1447 // backslashes literally (perhaps also for other types?)
1448 if ((context == EXPAND_USER_DEFINED
1449 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1450 new_len++; // '\' becomes "\\"
1451 }
1452 retval = alloc(new_len);
1453 if (retval != NULL)
1454 {
1455 retval[0] = '^';
1456 j = 1;
1457 for (i = 0; i < len; i++, j++)
1458 {
1459 // Skip backslash. But why? At least keep it for custom
1460 // expansion.
1461 if (context != EXPAND_USER_DEFINED
1462 && context != EXPAND_USER_LIST
1463 && fname[i] == '\\'
1464 && ++i == len)
1465 break;
1466
1467 switch (fname[i])
1468 {
1469 case '*': retval[j++] = '.';
1470 break;
1471 case '~': retval[j++] = '\\';
1472 break;
1473 case '?': retval[j] = '.';
1474 continue;
1475 case '.': if (context == EXPAND_BUFFERS)
1476 retval[j++] = '\\';
1477 break;
1478 case '\\': if (context == EXPAND_USER_DEFINED
1479 || context == EXPAND_USER_LIST)
1480 retval[j++] = '\\';
1481 break;
1482 }
1483 retval[j] = fname[i];
1484 }
1485 retval[j] = NUL;
1486 }
1487 }
1488 }
1489 else
1490 {
1491 retval = alloc(len + 4);
1492 if (retval != NULL)
1493 {
1494 vim_strncpy(retval, fname, len);
1495
1496 // Don't add a star to *, ~, ~user, $var or `cmd`.
1497 // * would become **, which walks the whole tree.
1498 // ~ would be at the start of the file name, but not the tail.
1499 // $ could be anywhere in the tail.
1500 // ` could be anywhere in the file name.
1501 // When the name ends in '$' don't add a star, remove the '$'.
1502 tail = gettail(retval);
1503 ends_in_star = (len > 0 && retval[len - 1] == '*');
1504#ifndef BACKSLASH_IN_FILENAME
1505 for (i = len - 2; i >= 0; --i)
1506 {
1507 if (retval[i] != '\\')
1508 break;
1509 ends_in_star = !ends_in_star;
1510 }
1511#endif
1512 if ((*retval != '~' || tail != retval)
1513 && !ends_in_star
1514 && vim_strchr(tail, '$') == NULL
1515 && vim_strchr(retval, '`') == NULL)
1516 retval[len++] = '*';
1517 else if (len > 0 && retval[len - 1] == '$')
1518 --len;
1519 retval[len] = NUL;
1520 }
1521 }
1522 return retval;
1523}
1524
1525/*
1526 * Must parse the command line so far to work out what context we are in.
1527 * Completion can then be done based on that context.
1528 * This routine sets the variables:
1529 * xp->xp_pattern The start of the pattern to be expanded within
1530 * the command line (ends at the cursor).
1531 * xp->xp_context The type of thing to expand. Will be one of:
1532 *
1533 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1534 * the command line, like an unknown command. Caller
1535 * should beep.
1536 * EXPAND_NOTHING Unrecognised context for completion, use char like
1537 * a normal char, rather than for completion. eg
1538 * :s/^I/
1539 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1540 * it.
1541 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1542 * EXPAND_FILES After command with EX_XFILE set, or after setting
1543 * with P_EXPAND set. eg :e ^I, :w>>^I
1544 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001545 * when we know only directories are of interest.
1546 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001547 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1548 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1549 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1550 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1551 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1552 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1553 * EXPAND_EVENTS Complete event names
1554 * EXPAND_SYNTAX Complete :syntax command arguments
1555 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1556 * EXPAND_AUGROUP Complete autocommand group names
1557 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1558 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1559 * eg :unmap a^I , :cunab x^I
1560 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1561 * eg :call sub^I
1562 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1563 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1564 * names in expressions, eg :while s^I
1565 * EXPAND_ENV_VARS Complete environment variable names
1566 * EXPAND_USER Complete user names
1567 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001568 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001569set_expand_context(expand_T *xp)
1570{
1571 cmdline_info_T *ccline = get_cmdline_info();
1572
1573 // only expansion for ':', '>' and '=' command-lines
1574 if (ccline->cmdfirstc != ':'
1575#ifdef FEAT_EVAL
1576 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1577 && !ccline->input_fn
1578#endif
1579 )
1580 {
1581 xp->xp_context = EXPAND_NOTHING;
1582 return;
1583 }
1584 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1585}
1586
Bram Moolenaard0190392019-08-23 21:17:35 +02001587/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001588 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1589 * For user defined commands, the completion context is set in 'xp' and the
1590 * completion flags in 'complp'.
1591 *
1592 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001593 */
1594 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001595set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001596{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001597 char_u *p = NULL;
1598 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001599 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001600
1601 // Isolate the command and search for it in the command table.
1602 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001603 // - the 'k' command can directly be followed by any character, but do
1604 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1605 // find matches anywhere in the command name, do this only for command
1606 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001607 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001608 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001609 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001610 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 p = cmd + 1;
1612 }
1613 else
1614 {
1615 p = cmd;
1616 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1617 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001618 // A user command may contain digits.
1619 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1620 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 while (ASCII_ISALNUM(*p) || *p == '*')
1622 ++p;
1623 // for python 3.x: ":py3*" commands completion
1624 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1625 {
1626 ++p;
1627 while (ASCII_ISALPHA(*p) || *p == '*')
1628 ++p;
1629 }
1630 // check for non-alpha command
1631 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1632 ++p;
1633 len = (int)(p - cmd);
1634
1635 if (len == 0)
1636 {
1637 xp->xp_context = EXPAND_UNSUCCESSFUL;
1638 return NULL;
1639 }
1640
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001641 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001642
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001643 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001644 // Also when doing fuzzy expansion for non-shell commands, support
1645 // alphanumeric characters.
1646 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1647 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001648 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1649 ++p;
1650 }
1651
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001652 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 // character, complete the command name.
1654 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1655 return NULL;
1656
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001657 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001658 {
1659 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1660 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001661 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 p = cmd + 1;
1663 }
1664 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1665 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 eap->cmd = cmd;
1667 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001668 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 }
1671 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001673 {
1674 // Not still touching the command and it was an illegal one
1675 xp->xp_context = EXPAND_UNSUCCESSFUL;
1676 return NULL;
1677 }
1678
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 return p;
1680}
Bram Moolenaard0190392019-08-23 21:17:35 +02001681
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682/*
1683 * Set the completion context for a command argument with wild card characters.
1684 */
1685 static void
1686set_context_for_wildcard_arg(
1687 exarg_T *eap,
1688 char_u *arg,
1689 int usefilter,
1690 expand_T *xp,
1691 int *complp)
1692{
1693 char_u *p;
1694 int c;
1695 int in_quote = FALSE;
1696 char_u *bow = NULL; // Beginning of word
1697 int len = 0;
1698
1699 // Allow spaces within back-quotes to count as part of the argument
1700 // being expanded.
1701 xp->xp_pattern = skipwhite(arg);
1702 p = xp->xp_pattern;
1703 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001705 if (has_mbyte)
1706 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 c = *p;
1709 if (c == '\\' && p[1] != NUL)
1710 ++p;
1711 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001712 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001713 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 xp->xp_pattern = p;
1716 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001718 in_quote = !in_quote;
1719 }
1720 // An argument can contain just about everything, except
1721 // characters that end the command and white space.
1722 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001723#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001724 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001725#endif
1726 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 {
1728 len = 0; // avoid getting stuck when space is in 'isfname'
1729 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001730 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 if (has_mbyte)
1732 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001734 c = *p;
1735 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001736 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 if (has_mbyte)
1738 len = (*mb_ptr2len)(p);
1739 else
1740 len = 1;
1741 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 if (in_quote)
1744 bow = p;
1745 else
1746 xp->xp_pattern = p;
1747 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001750 }
1751
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 // If we are still inside the quotes, and we passed a space, just
1753 // expand from there.
1754 if (bow != NULL && in_quote)
1755 xp->xp_pattern = bow;
1756 xp->xp_context = EXPAND_FILES;
1757
1758 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001759 if (usefilter
1760 || (eap != NULL
1761 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1762 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001763 {
1764#ifndef BACKSLASH_IN_FILENAME
1765 xp->xp_shell = TRUE;
1766#endif
1767 // When still after the command name expand executables.
1768 if (xp->xp_pattern == skipwhite(arg))
1769 xp->xp_context = EXPAND_SHELLCMD;
1770 }
1771
1772 // Check for environment variable.
1773 if (*xp->xp_pattern == '$')
1774 {
1775 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1776 if (!vim_isIDc(*p))
1777 break;
1778 if (*p == NUL)
1779 {
1780 xp->xp_context = EXPAND_ENV_VARS;
1781 ++xp->xp_pattern;
1782 // Avoid that the assignment uses EXPAND_FILES again.
1783 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1784 *complp = EXPAND_ENV_VARS;
1785 }
1786 }
1787 // Check for user names.
1788 if (*xp->xp_pattern == '~')
1789 {
1790 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1791 ;
1792 // Complete ~user only if it partially matches a user name.
1793 // A full match ~user<Tab> will be replaced by user's home
1794 // directory i.e. something like ~user<Tab> -> /home/user/
1795 if (*p == NUL && p > xp->xp_pattern + 1
1796 && match_user(xp->xp_pattern + 1) >= 1)
1797 {
1798 xp->xp_context = EXPAND_USER;
1799 ++xp->xp_pattern;
1800 }
1801 }
1802}
1803
1804/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001805 * Set the completion context for the "++opt=arg" argument. Always returns
1806 * NULL.
1807 */
1808 static char_u *
1809set_context_in_argopt(expand_T *xp, char_u *arg)
1810{
1811 char_u *p;
1812
1813 p = vim_strchr(arg, '=');
1814 if (p == NULL)
1815 xp->xp_pattern = arg;
1816 else
1817 xp->xp_pattern = p + 1;
1818
1819 xp->xp_context = EXPAND_ARGOPT;
1820 return NULL;
1821}
1822
1823#ifdef FEAT_TERMINAL
1824/*
1825 * Set the completion context for :terminal's [options]. Always returns NULL.
1826 */
1827 static char_u *
1828set_context_in_terminalopt(expand_T *xp, char_u *arg)
1829{
1830 char_u *p;
1831
1832 p = vim_strchr(arg, '=');
1833 if (p == NULL)
1834 xp->xp_pattern = arg;
1835 else
1836 xp->xp_pattern = p + 1;
1837
1838 xp->xp_context = EXPAND_TERMINALOPT;
1839 return NULL;
1840}
1841#endif
1842
1843/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001844 * Set the completion context for the :filter command. Returns a pointer to the
1845 * next command after the :filter command.
1846 */
1847 static char_u *
1848set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1849{
1850 if (*arg != NUL)
1851 arg = skip_vimgrep_pat(arg, NULL, NULL);
1852 if (arg == NULL || *arg == NUL)
1853 {
1854 xp->xp_context = EXPAND_NOTHING;
1855 return NULL;
1856 }
1857 return skipwhite(arg);
1858}
1859
1860#ifdef FEAT_SEARCH_EXTRA
1861/*
1862 * Set the completion context for the :match command. Returns a pointer to the
1863 * next command after the :match command.
1864 */
1865 static char_u *
1866set_context_in_match_cmd(expand_T *xp, char_u *arg)
1867{
1868 if (*arg == NUL || !ends_excmd(*arg))
1869 {
1870 // also complete "None"
1871 set_context_in_echohl_cmd(xp, arg);
1872 arg = skipwhite(skiptowhite(arg));
1873 if (*arg != NUL)
1874 {
1875 xp->xp_context = EXPAND_NOTHING;
1876 arg = skip_regexp(arg + 1, *arg, magic_isset());
1877 }
1878 }
1879 return find_nextcmd(arg);
1880}
1881#endif
1882
1883/*
1884 * Returns a pointer to the next command after a :global or a :v command.
1885 * Returns NULL if there is no next command.
1886 */
1887 static char_u *
1888find_cmd_after_global_cmd(char_u *arg)
1889{
1890 int delim;
1891
1892 delim = *arg; // get the delimiter
1893 if (delim)
1894 ++arg; // skip delimiter if there is one
1895
1896 while (arg[0] != NUL && arg[0] != delim)
1897 {
1898 if (arg[0] == '\\' && arg[1] != NUL)
1899 ++arg;
1900 ++arg;
1901 }
1902 if (arg[0] != NUL)
1903 return arg + 1;
1904
1905 return NULL;
1906}
1907
1908/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001909 * Returns a pointer to the next command after a :substitute or a :& command.
1910 * Returns NULL if there is no next command.
1911 */
1912 static char_u *
1913find_cmd_after_substitute_cmd(char_u *arg)
1914{
1915 int delim;
1916
1917 delim = *arg;
1918 if (delim)
1919 {
1920 // skip "from" part
1921 ++arg;
1922 arg = skip_regexp(arg, delim, magic_isset());
1923
1924 if (arg[0] != NUL && arg[0] == delim)
1925 {
1926 // skip "to" part
1927 ++arg;
1928 while (arg[0] != NUL && arg[0] != delim)
1929 {
1930 if (arg[0] == '\\' && arg[1] != NUL)
1931 ++arg;
1932 ++arg;
1933 }
1934 if (arg[0] != NUL) // skip delimiter
1935 ++arg;
1936 }
1937 }
1938 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1939 ++arg;
1940 if (arg[0] != NUL)
1941 return arg;
1942
1943 return NULL;
1944}
1945
1946/*
1947 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1948 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1949 * Returns NULL if there is no next command.
1950 */
1951 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001952find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001953{
1954 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001955 if (*arg != '/')
1956 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001957
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001958 // Match regexp, not just whole words
1959 for (++arg; *arg && *arg != '/'; arg++)
1960 if (*arg == '\\' && arg[1] != NUL)
1961 arg++;
1962 if (*arg)
1963 {
1964 arg = skipwhite(arg + 1);
1965
1966 // Check for trailing illegal characters
1967 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1968 xp->xp_context = EXPAND_NOTHING;
1969 else
1970 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001971 }
1972
1973 return NULL;
1974}
1975
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001976#ifdef FEAT_EVAL
1977/*
1978 * Set the completion context for the :unlet command. Always returns NULL.
1979 */
1980 static char_u *
1981set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1982{
1983 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1984 arg = xp->xp_pattern + 1;
1985
1986 xp->xp_context = EXPAND_USER_VARS;
1987 xp->xp_pattern = arg;
1988
1989 if (*xp->xp_pattern == '$')
1990 {
1991 xp->xp_context = EXPAND_ENV_VARS;
1992 ++xp->xp_pattern;
1993 }
1994
1995 return NULL;
1996}
1997#endif
1998
1999#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2000/*
2001 * Set the completion context for the :language command. Always returns NULL.
2002 */
2003 static char_u *
2004set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2005{
2006 char_u *p;
2007
2008 p = skiptowhite(arg);
2009 if (*p == NUL)
2010 {
2011 xp->xp_context = EXPAND_LANGUAGE;
2012 xp->xp_pattern = arg;
2013 }
2014 else
2015 {
2016 if ( STRNCMP(arg, "messages", p - arg) == 0
2017 || STRNCMP(arg, "ctype", p - arg) == 0
2018 || STRNCMP(arg, "time", p - arg) == 0
2019 || STRNCMP(arg, "collate", p - arg) == 0)
2020 {
2021 xp->xp_context = EXPAND_LOCALES;
2022 xp->xp_pattern = skipwhite(p);
2023 }
2024 else
2025 xp->xp_context = EXPAND_NOTHING;
2026 }
2027
2028 return NULL;
2029}
2030#endif
2031
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002032#ifdef FEAT_EVAL
2033static enum
2034{
2035 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002036 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2037 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002038} breakpt_expand_what;
2039
2040/*
2041 * Set the completion context for the :breakadd command. Always returns NULL.
2042 */
2043 static char_u *
2044set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2045{
2046 char_u *p;
2047 char_u *subcmd_start;
2048
2049 xp->xp_context = EXPAND_BREAKPOINT;
2050 xp->xp_pattern = arg;
2051
2052 if (cmdidx == CMD_breakadd)
2053 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002054 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002055 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002056 else
2057 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002058
2059 p = skipwhite(arg);
2060 if (*p == NUL)
2061 return NULL;
2062 subcmd_start = p;
2063
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002064 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002065 {
2066 // :breakadd file [lnum] <filename>
2067 // :breakadd func [lnum] <funcname>
2068 p += 4;
2069 p = skipwhite(p);
2070
2071 // skip line number (if specified)
2072 if (VIM_ISDIGIT(*p))
2073 {
2074 p = skipdigits(p);
2075 if (*p != ' ')
2076 {
2077 xp->xp_context = EXPAND_NOTHING;
2078 return NULL;
2079 }
2080 p = skipwhite(p);
2081 }
2082 if (STRNCMP("file", subcmd_start, 4) == 0)
2083 xp->xp_context = EXPAND_FILES;
2084 else
2085 xp->xp_context = EXPAND_USER_FUNC;
2086 xp->xp_pattern = p;
2087 }
2088 else if (STRNCMP("expr ", p, 5) == 0)
2089 {
2090 // :breakadd expr <expression>
2091 xp->xp_context = EXPAND_EXPRESSION;
2092 xp->xp_pattern = skipwhite(p + 5);
2093 }
2094
2095 return NULL;
2096}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002097
2098 static char_u *
2099set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2100{
2101 char_u *p;
2102
2103 xp->xp_context = EXPAND_NOTHING;
2104 xp->xp_pattern = NULL;
2105
2106 p = skipwhite(arg);
2107 if (VIM_ISDIGIT(*p))
2108 return NULL;
2109
2110 xp->xp_context = EXPAND_SCRIPTNAMES;
2111 xp->xp_pattern = p;
2112
2113 return NULL;
2114}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002115#endif
2116
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002117/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002118 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2119 * The argument to the command is 'arg' and the argument flags is 'argt'.
2120 * For user-defined commands and for environment variables, 'compl' has the
2121 * completion type.
2122 * Returns a pointer to the next command. Returns NULL if there is no next
2123 * command.
2124 */
2125 static char_u *
2126set_context_by_cmdname(
2127 char_u *cmd,
2128 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002129 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002130 char_u *arg,
2131 long argt,
2132 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002133 int forceit)
2134{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002135 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002136 {
2137 case CMD_find:
2138 case CMD_sfind:
2139 case CMD_tabfind:
2140 if (xp->xp_context == EXPAND_FILES)
2141 xp->xp_context = EXPAND_FILES_IN_PATH;
2142 break;
2143 case CMD_cd:
2144 case CMD_chdir:
2145 case CMD_tcd:
2146 case CMD_tchdir:
2147 case CMD_lcd:
2148 case CMD_lchdir:
2149 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002150 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002151 break;
2152 case CMD_help:
2153 xp->xp_context = EXPAND_HELP;
2154 xp->xp_pattern = arg;
2155 break;
2156
2157 // Command modifiers: return the argument.
2158 // Also for commands with an argument that is a command.
2159 case CMD_aboveleft:
2160 case CMD_argdo:
2161 case CMD_belowright:
2162 case CMD_botright:
2163 case CMD_browse:
2164 case CMD_bufdo:
2165 case CMD_cdo:
2166 case CMD_cfdo:
2167 case CMD_confirm:
2168 case CMD_debug:
2169 case CMD_folddoclosed:
2170 case CMD_folddoopen:
2171 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002172 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002173 case CMD_keepalt:
2174 case CMD_keepjumps:
2175 case CMD_keepmarks:
2176 case CMD_keeppatterns:
2177 case CMD_ldo:
2178 case CMD_leftabove:
2179 case CMD_lfdo:
2180 case CMD_lockmarks:
2181 case CMD_noautocmd:
2182 case CMD_noswapfile:
2183 case CMD_rightbelow:
2184 case CMD_sandbox:
2185 case CMD_silent:
2186 case CMD_tab:
2187 case CMD_tabdo:
2188 case CMD_topleft:
2189 case CMD_verbose:
2190 case CMD_vertical:
2191 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002192 case CMD_vim9cmd:
2193 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002194 return arg;
2195
2196 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002197 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002198
2199#ifdef FEAT_SEARCH_EXTRA
2200 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002201 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002202#endif
2203
2204 // All completion for the +cmdline_compl feature goes here.
2205
2206 case CMD_command:
2207 return set_context_in_user_cmd(xp, arg);
2208
2209 case CMD_delcommand:
2210 xp->xp_context = EXPAND_USER_COMMANDS;
2211 xp->xp_pattern = arg;
2212 break;
2213
2214 case CMD_global:
2215 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002216 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002217 case CMD_and:
2218 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002219 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002220 case CMD_isearch:
2221 case CMD_dsearch:
2222 case CMD_ilist:
2223 case CMD_dlist:
2224 case CMD_ijump:
2225 case CMD_psearch:
2226 case CMD_djump:
2227 case CMD_isplit:
2228 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002229 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002230 case CMD_autocmd:
2231 return set_context_in_autocmd(xp, arg, FALSE);
2232 case CMD_doautocmd:
2233 case CMD_doautoall:
2234 return set_context_in_autocmd(xp, arg, TRUE);
2235 case CMD_set:
2236 set_context_in_set_cmd(xp, arg, 0);
2237 break;
2238 case CMD_setglobal:
2239 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2240 break;
2241 case CMD_setlocal:
2242 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2243 break;
2244 case CMD_tag:
2245 case CMD_stag:
2246 case CMD_ptag:
2247 case CMD_ltag:
2248 case CMD_tselect:
2249 case CMD_stselect:
2250 case CMD_ptselect:
2251 case CMD_tjump:
2252 case CMD_stjump:
2253 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002254 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002255 xp->xp_context = EXPAND_TAGS_LISTFILES;
2256 else
2257 xp->xp_context = EXPAND_TAGS;
2258 xp->xp_pattern = arg;
2259 break;
2260 case CMD_augroup:
2261 xp->xp_context = EXPAND_AUGROUP;
2262 xp->xp_pattern = arg;
2263 break;
2264#ifdef FEAT_SYN_HL
2265 case CMD_syntax:
2266 set_context_in_syntax_cmd(xp, arg);
2267 break;
2268#endif
2269#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002270 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002271 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002272 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002273 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002274 case CMD_if:
2275 case CMD_elseif:
2276 case CMD_while:
2277 case CMD_for:
2278 case CMD_echo:
2279 case CMD_echon:
2280 case CMD_execute:
2281 case CMD_echomsg:
2282 case CMD_echoerr:
2283 case CMD_call:
2284 case CMD_return:
2285 case CMD_cexpr:
2286 case CMD_caddexpr:
2287 case CMD_cgetexpr:
2288 case CMD_lexpr:
2289 case CMD_laddexpr:
2290 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002291 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002292 break;
2293
2294 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002295 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002296 case CMD_function:
2297 case CMD_delfunction:
2298 xp->xp_context = EXPAND_USER_FUNC;
2299 xp->xp_pattern = arg;
2300 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002301 case CMD_disassemble:
2302 set_context_in_disassemble_cmd(xp, arg);
2303 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002304
2305 case CMD_echohl:
2306 set_context_in_echohl_cmd(xp, arg);
2307 break;
2308#endif
2309 case CMD_highlight:
2310 set_context_in_highlight_cmd(xp, arg);
2311 break;
2312#ifdef FEAT_CSCOPE
2313 case CMD_cscope:
2314 case CMD_lcscope:
2315 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002316 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002317 break;
2318#endif
2319#ifdef FEAT_SIGNS
2320 case CMD_sign:
2321 set_context_in_sign_cmd(xp, arg);
2322 break;
2323#endif
2324 case CMD_bdelete:
2325 case CMD_bwipeout:
2326 case CMD_bunload:
2327 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2328 arg = xp->xp_pattern + 1;
2329 // FALLTHROUGH
2330 case CMD_buffer:
2331 case CMD_sbuffer:
2332 case CMD_checktime:
2333 xp->xp_context = EXPAND_BUFFERS;
2334 xp->xp_pattern = arg;
2335 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002336#ifdef FEAT_DIFF
2337 case CMD_diffget:
2338 case CMD_diffput:
2339 // If current buffer is in diff mode, complete buffer names
2340 // which are in diff mode, and different than current buffer.
2341 xp->xp_context = EXPAND_DIFF_BUFFERS;
2342 xp->xp_pattern = arg;
2343 break;
2344#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002345 case CMD_USER:
2346 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002347 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2348 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002349
2350 case CMD_map: case CMD_noremap:
2351 case CMD_nmap: case CMD_nnoremap:
2352 case CMD_vmap: case CMD_vnoremap:
2353 case CMD_omap: case CMD_onoremap:
2354 case CMD_imap: case CMD_inoremap:
2355 case CMD_cmap: case CMD_cnoremap:
2356 case CMD_lmap: case CMD_lnoremap:
2357 case CMD_smap: case CMD_snoremap:
2358 case CMD_tmap: case CMD_tnoremap:
2359 case CMD_xmap: case CMD_xnoremap:
2360 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002361 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002362 case CMD_unmap:
2363 case CMD_nunmap:
2364 case CMD_vunmap:
2365 case CMD_ounmap:
2366 case CMD_iunmap:
2367 case CMD_cunmap:
2368 case CMD_lunmap:
2369 case CMD_sunmap:
2370 case CMD_tunmap:
2371 case CMD_xunmap:
2372 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002373 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002374 case CMD_mapclear:
2375 case CMD_nmapclear:
2376 case CMD_vmapclear:
2377 case CMD_omapclear:
2378 case CMD_imapclear:
2379 case CMD_cmapclear:
2380 case CMD_lmapclear:
2381 case CMD_smapclear:
2382 case CMD_tmapclear:
2383 case CMD_xmapclear:
2384 xp->xp_context = EXPAND_MAPCLEAR;
2385 xp->xp_pattern = arg;
2386 break;
2387
2388 case CMD_abbreviate: case CMD_noreabbrev:
2389 case CMD_cabbrev: case CMD_cnoreabbrev:
2390 case CMD_iabbrev: case CMD_inoreabbrev:
2391 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002392 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002393 case CMD_unabbreviate:
2394 case CMD_cunabbrev:
2395 case CMD_iunabbrev:
2396 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002397 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002398#ifdef FEAT_MENU
2399 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2400 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2401 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2402 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2403 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2404 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2405 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2406 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2407 case CMD_tmenu: case CMD_tunmenu:
2408 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2409 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2410#endif
2411
2412 case CMD_colorscheme:
2413 xp->xp_context = EXPAND_COLORS;
2414 xp->xp_pattern = arg;
2415 break;
2416
2417 case CMD_compiler:
2418 xp->xp_context = EXPAND_COMPILER;
2419 xp->xp_pattern = arg;
2420 break;
2421
2422 case CMD_ownsyntax:
2423 xp->xp_context = EXPAND_OWNSYNTAX;
2424 xp->xp_pattern = arg;
2425 break;
2426
2427 case CMD_setfiletype:
2428 xp->xp_context = EXPAND_FILETYPE;
2429 xp->xp_pattern = arg;
2430 break;
2431
2432 case CMD_packadd:
2433 xp->xp_context = EXPAND_PACKADD;
2434 xp->xp_pattern = arg;
2435 break;
2436
zeertzjqb0d45ec2023-01-25 15:04:22 +00002437 case CMD_runtime:
2438 set_context_in_runtime_cmd(xp, arg);
2439 break;
2440
Bram Moolenaard0190392019-08-23 21:17:35 +02002441#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2442 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002443 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002444#endif
2445#if defined(FEAT_PROFILE)
2446 case CMD_profile:
2447 set_context_in_profile_cmd(xp, arg);
2448 break;
2449#endif
2450 case CMD_behave:
2451 xp->xp_context = EXPAND_BEHAVE;
2452 xp->xp_pattern = arg;
2453 break;
2454
2455 case CMD_messages:
2456 xp->xp_context = EXPAND_MESSAGES;
2457 xp->xp_pattern = arg;
2458 break;
2459
2460 case CMD_history:
2461 xp->xp_context = EXPAND_HISTORY;
2462 xp->xp_pattern = arg;
2463 break;
2464#if defined(FEAT_PROFILE)
2465 case CMD_syntime:
2466 xp->xp_context = EXPAND_SYNTIME;
2467 xp->xp_pattern = arg;
2468 break;
2469#endif
2470
2471 case CMD_argdelete:
2472 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2473 arg = xp->xp_pattern + 1;
2474 xp->xp_context = EXPAND_ARGLIST;
2475 xp->xp_pattern = arg;
2476 break;
2477
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002478#ifdef FEAT_EVAL
2479 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002480 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002481 case CMD_breakdel:
2482 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002483
2484 case CMD_scriptnames:
2485 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002486#endif
2487
Bram Moolenaard0190392019-08-23 21:17:35 +02002488 default:
2489 break;
2490 }
2491 return NULL;
2492}
2493
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002494/*
2495 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2496 * we don't need/want deleted. Maybe this could be done better if we didn't
2497 * repeat all this stuff. The only problem is that they may not stay
2498 * perfectly compatible with each other, but then the command line syntax
2499 * probably won't change that much -- webb.
2500 */
2501 static char_u *
2502set_one_cmd_context(
2503 expand_T *xp,
2504 char_u *buff) // buffer for command string
2505{
2506 char_u *p;
2507 char_u *cmd, *arg;
2508 int len = 0;
2509 exarg_T ea;
2510 int compl = EXPAND_NOTHING;
2511 int forceit = FALSE;
2512 int usefilter = FALSE; // filter instead of file name
2513
2514 ExpandInit(xp);
2515 xp->xp_pattern = buff;
2516 xp->xp_line = buff;
2517 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2518 ea.argt = 0;
2519
2520 // 1. skip comment lines and leading space, colons or bars
2521 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2522 ;
2523 xp->xp_pattern = cmd;
2524
2525 if (*cmd == NUL)
2526 return NULL;
2527 if (*cmd == '"') // ignore comment lines
2528 {
2529 xp->xp_context = EXPAND_NOTHING;
2530 return NULL;
2531 }
2532
2533 // 3. Skip over the range to find the command.
2534 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2535 xp->xp_pattern = cmd;
2536 if (*cmd == NUL)
2537 return NULL;
2538 if (*cmd == '"')
2539 {
2540 xp->xp_context = EXPAND_NOTHING;
2541 return NULL;
2542 }
2543
2544 if (*cmd == '|' || *cmd == '\n')
2545 return cmd + 1; // There's another command
2546
2547 // Get the command index.
2548 p = set_cmd_index(cmd, &ea, xp, &compl);
2549 if (p == NULL)
2550 return NULL;
2551
2552 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2553
2554 if (*p == '!') // forced commands
2555 {
2556 forceit = TRUE;
2557 ++p;
2558 }
2559
2560 // 6. parse arguments
2561 if (!IS_USER_CMDIDX(ea.cmdidx))
2562 ea.argt = excmd_get_argt(ea.cmdidx);
2563
2564 arg = skipwhite(p);
2565
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002566 // Does command allow "++argopt" argument?
2567 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002568 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002569 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2570 {
2571 p = arg + 2;
2572 while (*p && !vim_isspace(*p))
2573 MB_PTR_ADV(p);
2574
2575 // Still touching the command after "++"?
2576 if (*p == NUL)
2577 {
2578 if (ea.argt & EX_ARGOPT)
2579 return set_context_in_argopt(xp, arg + 2);
2580#ifdef FEAT_TERMINAL
2581 if (ea.cmdidx == CMD_terminal)
2582 return set_context_in_terminalopt(xp, arg + 2);
2583#endif
2584 }
2585
2586 arg = skipwhite(p);
2587 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002588 }
2589
2590 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2591 {
2592 if (*arg == '>') // append
2593 {
2594 if (*++arg == '>')
2595 ++arg;
2596 arg = skipwhite(arg);
2597 }
2598 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2599 {
2600 ++arg;
2601 usefilter = TRUE;
2602 }
2603 }
2604
2605 if (ea.cmdidx == CMD_read)
2606 {
2607 usefilter = forceit; // :r! filter if forced
2608 if (*arg == '!') // :r !filter
2609 {
2610 ++arg;
2611 usefilter = TRUE;
2612 }
2613 }
2614
2615 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2616 {
2617 while (*arg == *cmd) // allow any number of '>' or '<'
2618 ++arg;
2619 arg = skipwhite(arg);
2620 }
2621
2622 // Does command allow "+command"?
2623 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2624 {
2625 // Check if we're in the +command
2626 p = arg + 1;
2627 arg = skip_cmd_arg(arg, FALSE);
2628
2629 // Still touching the command after '+'?
2630 if (*arg == NUL)
2631 return p;
2632
2633 // Skip space(s) after +command to get to the real argument
2634 arg = skipwhite(arg);
2635 }
2636
2637
2638 // Check for '|' to separate commands and '"' to start comments.
2639 // Don't do this for ":read !cmd" and ":write !cmd".
2640 if ((ea.argt & EX_TRLBAR) && !usefilter)
2641 {
2642 p = arg;
2643 // ":redir @" is not the start of a comment
2644 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2645 p += 2;
2646 while (*p)
2647 {
2648 if (*p == Ctrl_V)
2649 {
2650 if (p[1] != NUL)
2651 ++p;
2652 }
2653 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2654 || *p == '|' || *p == '\n')
2655 {
2656 if (*(p - 1) != '\\')
2657 {
2658 if (*p == '|' || *p == '\n')
2659 return p + 1;
2660 return NULL; // It's a comment
2661 }
2662 }
2663 MB_PTR_ADV(p);
2664 }
2665 }
2666
2667 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2668 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2669 // no arguments allowed but there is something
2670 return NULL;
2671
2672 // Find start of last argument (argument just before cursor):
2673 p = buff;
2674 xp->xp_pattern = p;
2675 len = (int)STRLEN(buff);
2676 while (*p && p < buff + len)
2677 {
2678 if (*p == ' ' || *p == TAB)
2679 {
2680 // argument starts after a space
2681 xp->xp_pattern = ++p;
2682 }
2683 else
2684 {
2685 if (*p == '\\' && *(p + 1) != NUL)
2686 ++p; // skip over escaped character
2687 MB_PTR_ADV(p);
2688 }
2689 }
2690
2691 if (ea.argt & EX_XFILE)
2692 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2693
2694 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002695 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002696 forceit);
2697}
2698
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002699/*
2700 * Set the completion context in 'xp' for command 'str'
2701 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002702 void
2703set_cmd_context(
2704 expand_T *xp,
2705 char_u *str, // start of command line
2706 int len, // length of command line (excl. NUL)
2707 int col, // position of cursor
2708 int use_ccline UNUSED) // use ccline for info
2709{
2710#ifdef FEAT_EVAL
2711 cmdline_info_T *ccline = get_cmdline_info();
2712#endif
2713 int old_char = NUL;
2714 char_u *nextcomm;
2715
2716 // Avoid a UMR warning from Purify, only save the character if it has been
2717 // written before.
2718 if (col < len)
2719 old_char = str[col];
2720 str[col] = NUL;
2721 nextcomm = str;
2722
2723#ifdef FEAT_EVAL
2724 if (use_ccline && ccline->cmdfirstc == '=')
2725 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002726 // pass CMD_SIZE because there is no real command
2727 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 }
2729 else if (use_ccline && ccline->input_fn)
2730 {
2731 xp->xp_context = ccline->xp_context;
2732 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002733 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002734 }
2735 else
2736#endif
2737 while (nextcomm != NULL)
2738 nextcomm = set_one_cmd_context(xp, nextcomm);
2739
2740 // Store the string here so that call_user_expand_func() can get to them
2741 // easily.
2742 xp->xp_line = str;
2743 xp->xp_col = col;
2744
2745 str[col] = old_char;
2746}
2747
2748/*
2749 * Expand the command line "str" from context "xp".
2750 * "xp" must have been set by set_cmd_context().
2751 * xp->xp_pattern points into "str", to where the text that is to be expanded
2752 * starts.
2753 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2754 * cursor.
2755 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2756 * key that triggered expansion literally.
2757 * Returns EXPAND_OK otherwise.
2758 */
2759 int
2760expand_cmdline(
2761 expand_T *xp,
2762 char_u *str, // start of command line
2763 int col, // position of cursor
2764 int *matchcount, // return: nr of matches
2765 char_u ***matches) // return: array of pointers to matches
2766{
2767 char_u *file_str = NULL;
2768 int options = WILD_ADD_SLASH|WILD_SILENT;
2769
2770 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2771 {
2772 beep_flush();
2773 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2774 }
2775 if (xp->xp_context == EXPAND_NOTHING)
2776 {
2777 // Caller can use the character as a normal char instead
2778 return EXPAND_NOTHING;
2779 }
2780
2781 // add star to file name, or convert to regexp if not exp. files.
2782 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002783 if (cmdline_fuzzy_completion_supported(xp))
2784 // If fuzzy matching, don't modify the search string
2785 file_str = vim_strsave(xp->xp_pattern);
2786 else
2787 {
2788 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2789 if (file_str == NULL)
2790 return EXPAND_UNSUCCESSFUL;
2791 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002792
2793 if (p_wic)
2794 options += WILD_ICASE;
2795
2796 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002797 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002798 {
2799 *matchcount = 0;
2800 *matches = NULL;
2801 }
2802 vim_free(file_str);
2803
2804 return EXPAND_OK;
2805}
2806
Bram Moolenaar66b51422019-08-18 21:44:12 +02002807/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002808 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002809 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002810 */
2811 static int
2812expand_files_and_dirs(
2813 expand_T *xp,
2814 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002815 char_u ***matches,
2816 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002817 int flags,
2818 int options)
2819{
2820 int free_pat = FALSE;
2821 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002822 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002823
2824 // for ":set path=" and ":set tags=" halve backslashes for escaped
2825 // space
2826 if (xp->xp_backslash != XP_BS_NONE)
2827 {
2828 free_pat = TRUE;
2829 pat = vim_strsave(pat);
2830 for (i = 0; pat[i]; ++i)
2831 if (pat[i] == '\\')
2832 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002833 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002834 && pat[i + 1] == '\\'
2835 && pat[i + 2] == '\\'
2836 && pat[i + 3] == ' ')
2837 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002838 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002839 && pat[i + 1] == ' ')
2840 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002841 else if ((xp->xp_backslash & XP_BS_COMMA)
2842 && pat[i + 1] == '\\'
2843 && pat[i + 2] == ',')
2844 STRMOVE(pat + i, pat + i + 2);
2845#ifdef BACKSLASH_IN_FILENAME
2846 else if ((xp->xp_backslash & XP_BS_COMMA)
2847 && pat[i + 1] == ',')
2848 STRMOVE(pat + i, pat + i + 1);
2849#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002850 }
2851 }
2852
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002853 if (xp->xp_context == EXPAND_FILES_IN_PATH && *get_findexpr() != NUL)
2854 {
2855#ifdef FEAT_EVAL
2856 ret = expand_findexpr(pat, matches, numMatches);
2857#endif
2858 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002859 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002860 {
2861 if (xp->xp_context == EXPAND_FILES)
2862 flags |= EW_FILE;
2863 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2864 flags |= (EW_FILE | EW_PATH);
2865 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2866 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2867 else
2868 flags = (flags | EW_DIR) & ~EW_FILE;
2869 if (options & WILD_ICASE)
2870 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002871
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002872 // Expand wildcards, supporting %:h and the like.
2873 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2874 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875 if (free_pat)
2876 vim_free(pat);
2877#ifdef BACKSLASH_IN_FILENAME
2878 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2879 {
2880 int j;
2881
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002882 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002883 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002884 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002885
2886 while (*ptr != NUL)
2887 {
2888 if (p_csl[0] == 's' && *ptr == '\\')
2889 *ptr = '/';
2890 else if (p_csl[0] == 'b' && *ptr == '/')
2891 *ptr = '\\';
2892 ptr += (*mb_ptr2len)(ptr);
2893 }
2894 }
2895 }
2896#endif
2897 return ret;
2898}
2899
2900/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002901 * Function given to ExpandGeneric() to obtain the possible arguments of the
2902 * ":behave {mswin,xterm}" command.
2903 */
2904 static char_u *
2905get_behave_arg(expand_T *xp UNUSED, int idx)
2906{
2907 if (idx == 0)
2908 return (char_u *)"mswin";
2909 if (idx == 1)
2910 return (char_u *)"xterm";
2911 return NULL;
2912}
2913
K.Takata161b6ac2022-11-14 15:31:07 +00002914#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915/*
2916 * Function given to ExpandGeneric() to obtain the possible arguments of the
2917 * ":breakadd {expr, file, func, here}" command.
2918 * ":breakdel {func, file, here}" command.
2919 */
2920 static char_u *
2921get_breakadd_arg(expand_T *xp UNUSED, int idx)
2922{
2923 char *opts[] = {"expr", "file", "func", "here"};
2924
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002925 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002926 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002927 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002928 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2929 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002930 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2931 {
2932 // breakdel {func, file, here}
2933 if (idx <= 2)
2934 return (char_u *)opts[idx + 1];
2935 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002936 else
2937 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002938 // profdel {func, file}
2939 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002940 return (char_u *)opts[idx + 1];
2941 }
2942 }
2943 return NULL;
2944}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002945
2946/*
2947 * Function given to ExpandGeneric() to obtain the possible arguments for the
2948 * ":scriptnames" command.
2949 */
2950 static char_u *
2951get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2952{
2953 scriptitem_T *si;
2954
2955 if (!SCRIPT_ID_VALID(idx + 1))
2956 return NULL;
2957
2958 si = SCRIPT_ITEM(idx + 1);
2959 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2960 return NameBuff;
2961}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002962#endif
2963
Bram Moolenaard0190392019-08-23 21:17:35 +02002964/*
2965 * Function given to ExpandGeneric() to obtain the possible arguments of the
2966 * ":messages {clear}" command.
2967 */
2968 static char_u *
2969get_messages_arg(expand_T *xp UNUSED, int idx)
2970{
2971 if (idx == 0)
2972 return (char_u *)"clear";
2973 return NULL;
2974}
2975
2976 static char_u *
2977get_mapclear_arg(expand_T *xp UNUSED, int idx)
2978{
2979 if (idx == 0)
2980 return (char_u *)"<buffer>";
2981 return NULL;
2982}
2983
2984/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985 * Do the expansion based on xp->xp_context and 'rmp'.
2986 */
2987 static int
2988ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002989 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002990 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002991 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002992 char_u ***matches,
2993 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002994{
2995 static struct expgen
2996 {
2997 int context;
2998 char_u *((*func)(expand_T *, int));
2999 int ic;
3000 int escaped;
3001 } tab[] =
3002 {
3003 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3004 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3005 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3006 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3007 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3008 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3009 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3010 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3011 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3012 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003013#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003014 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3015 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3016 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3017 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3018 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003019#endif
3020#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003021 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3022 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003023#endif
3024#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003026#endif
3027#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003028 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003029#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003030 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3031 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3032 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003033#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003034 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003035#endif
3036#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003037 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003038#endif
3039#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003040 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003041#endif
3042#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003043 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3044 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003045#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003046 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3047 {EXPAND_USER, get_users, TRUE, FALSE},
3048 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003049#ifdef FEAT_EVAL
3050 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003051 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003052#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003053 };
3054 int i;
3055 int ret = FAIL;
3056
3057 // Find a context in the table and call the ExpandGeneric() with the
3058 // right function to do the expansion.
3059 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3060 {
3061 if (xp->xp_context == tab[i].context)
3062 {
3063 if (tab[i].ic)
3064 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003065 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3066 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003067 break;
3068 }
3069 }
3070
3071 return ret;
3072}
3073
3074/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003075 * Map wild expand options to flags for expand_wildcards()
3076 */
3077 static int
3078map_wildopts_to_ewflags(int options)
3079{
3080 int flags;
3081
3082 flags = EW_DIR; // include directories
3083 if (options & WILD_LIST_NOTFOUND)
3084 flags |= EW_NOTFOUND;
3085 if (options & WILD_ADD_SLASH)
3086 flags |= EW_ADDSLASH;
3087 if (options & WILD_KEEP_ALL)
3088 flags |= EW_KEEPALL;
3089 if (options & WILD_SILENT)
3090 flags |= EW_SILENT;
3091 if (options & WILD_NOERROR)
3092 flags |= EW_NOERROR;
3093 if (options & WILD_ALLLINKS)
3094 flags |= EW_ALLLINKS;
3095
3096 return flags;
3097}
3098
3099/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 * Do the expansion based on xp->xp_context and "pat".
3101 */
3102 static int
3103ExpandFromContext(
3104 expand_T *xp,
3105 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003106 char_u ***matches,
3107 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003108 int options) // WILD_ flags
3109{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 int ret;
3112 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003113 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003114 int fuzzy = cmdline_fuzzy_complete(pat)
3115 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003116
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003117 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118
3119 if (xp->xp_context == EXPAND_FILES
3120 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003121 || xp->xp_context == EXPAND_FILES_IN_PATH
3122 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003123 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3124 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003125
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003126 *matches = (char_u **)"";
3127 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128 if (xp->xp_context == EXPAND_HELP)
3129 {
3130 // With an empty argument we would get all the help tags, which is
3131 // very slow. Get matches for "help" instead.
3132 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003133 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 {
3135#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137#endif
3138 return OK;
3139 }
3140 return FAIL;
3141 }
3142
Bram Moolenaar66b51422019-08-18 21:44:12 +02003143 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003144 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003145 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003146 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003149#ifdef FEAT_DIFF
3150 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003151 return ExpandBufnames(pat, numMatches, matches,
3152 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003153#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154 if (xp->xp_context == EXPAND_TAGS
3155 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003156 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3157 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 if (xp->xp_context == EXPAND_COLORS)
3159 {
3160 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003161 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162 directories);
3163 }
3164 if (xp->xp_context == EXPAND_COMPILER)
3165 {
3166 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 }
3169 if (xp->xp_context == EXPAND_OWNSYNTAX)
3170 {
3171 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003172 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 }
3174 if (xp->xp_context == EXPAND_FILETYPE)
3175 {
3176 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003177 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003178 }
Doug Kearns81642d92024-01-04 22:37:44 +01003179#ifdef FEAT_KEYMAP
3180 if (xp->xp_context == EXPAND_KEYMAP)
3181 {
3182 char *directories[] = {"keymap", NULL};
3183 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3184 }
3185#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003186#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003188 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003189#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003190 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003191 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003192 if (xp->xp_context == EXPAND_RUNTIME)
3193 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003195 // When expanding a function name starting with s:, match the <SNR>nr_
3196 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003197 if ((xp->xp_context == EXPAND_USER_FUNC
3198 || xp->xp_context == EXPAND_DISASSEMBLE)
3199 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003200 {
3201 int len = (int)STRLEN(pat) + 20;
3202
3203 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003204 if (tofree == NULL)
3205 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003206 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003207 pat = tofree;
3208 }
3209
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003210 if (!fuzzy)
3211 {
3212 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3213 if (regmatch.regprog == NULL)
3214 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003215
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003216 // set ignore-case according to p_ic, p_scs and pat
3217 regmatch.rm_ic = ignorecase(pat);
3218 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003219
3220 if (xp->xp_context == EXPAND_SETTINGS
3221 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003222 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003223 else if (xp->xp_context == EXPAND_STRING_SETTING)
3224 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3225 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3226 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003227 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003228 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003229 else if (xp->xp_context == EXPAND_ARGOPT)
3230 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3231#if defined(FEAT_TERMINAL)
3232 else if (xp->xp_context == EXPAND_TERMINALOPT)
3233 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3234#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003235#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003236 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003237 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003238#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003239 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003240 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003242 if (!fuzzy)
3243 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003244 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003245
3246 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003247}
3248
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249/*
3250 * Expand a list of names.
3251 *
3252 * Generic function for command line completion. It calls a function to
3253 * obtain strings, one by one. The strings are matched against a regexp
3254 * program. Matching strings are copied into an array, which is returned.
3255 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003256 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3257 * is used.
3258 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003259 * Returns OK when no problems encountered, FAIL for error (out of memory).
3260 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003261 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003262ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003263 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 expand_T *xp,
3265 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003266 char_u ***matches,
3267 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268 char_u *((*func)(expand_T *, int)),
3269 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003270 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003271{
3272 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003273 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003275 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003276 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003277 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003278 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003279 int sort_matches = FALSE;
3280 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003282 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003283 *matches = NULL;
3284 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003285
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003286 if (!fuzzy)
3287 ga_init2(&ga, sizeof(char *), 30);
3288 else
3289 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3290
3291 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003292 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003293 str = (*func)(xp, i);
3294 if (str == NULL) // end of list
3295 break;
3296 if (*str == NUL) // skip empty strings
3297 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003298
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003299 if (xp->xp_pattern[0] != NUL)
3300 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003301 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003302 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003303 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003304 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003305 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003306 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 }
3308 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003309 else
3310 match = TRUE;
3311
3312 if (!match)
3313 continue;
3314
3315 if (escaped)
3316 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3317 else
3318 str = vim_strsave(str);
3319 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003320 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003321 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003322 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003323 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324 return FAIL;
3325 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003326 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003327 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003328 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003329
3330 if (ga_grow(&ga, 1) == FAIL)
3331 {
3332 vim_free(str);
3333 break;
3334 }
3335
3336 if (fuzzy)
3337 {
3338 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3339 fuzmatch->idx = ga.ga_len;
3340 fuzmatch->str = str;
3341 fuzmatch->score = score;
3342 }
3343 else
3344 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3345
K.Takata161b6ac2022-11-14 15:31:07 +00003346#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003347 if (func == get_menu_names)
3348 {
3349 // test for separator added by get_menu_names()
3350 str += STRLEN(str) - 1;
3351 if (*str == '\001')
3352 *str = '.';
3353 }
K.Takata161b6ac2022-11-14 15:31:07 +00003354#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003355
3356 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003357 }
3358
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003359 if (ga.ga_len == 0)
3360 return OK;
3361
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003362 // sort the matches when using regular expression matching and sorting
3363 // applies to the completion context. Menus and scriptnames should be kept
3364 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003365 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003366 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003367 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003368 && xp->xp_context != EXPAND_SCRIPTNAMES
3369 && xp->xp_context != EXPAND_ARGOPT
3370 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003371 sort_matches = TRUE;
3372
3373 // <SNR> functions should be sorted to the end.
3374 if (xp->xp_context == EXPAND_EXPRESSION
3375 || xp->xp_context == EXPAND_FUNCTIONS
3376 || xp->xp_context == EXPAND_USER_FUNC
3377 || xp->xp_context == EXPAND_DISASSEMBLE)
3378 funcsort = TRUE;
3379
3380 // Sort the matches.
3381 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003382 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003383 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003384 // <SNR> functions should be sorted to the end.
3385 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3386 sort_func_compare);
3387 else
3388 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3389 }
3390
3391 if (!fuzzy)
3392 {
3393 *matches = ga.ga_data;
3394 *numMatches = ga.ga_len;
3395 }
3396 else
3397 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003398 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3399 funcsort) == FAIL)
3400 return FAIL;
3401 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003402 }
3403
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003404#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003405 // Reset the variables used for special highlight names expansion, so that
3406 // they don't show up when getting normal highlight names by ID.
3407 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003408#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003409
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410 return OK;
3411}
3412
3413/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003414 * Expand shell command matches in one directory of $PATH.
3415 */
3416 static void
3417expand_shellcmd_onedir(
3418 char_u *buf,
3419 char_u *s,
3420 size_t l,
3421 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003422 char_u ***matches,
3423 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003424 int flags,
3425 hashtab_T *ht,
3426 garray_T *gap)
3427{
3428 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003429 hash_T hash;
3430 hashitem_T *hi;
3431
3432 vim_strncpy(buf, s, l);
3433 add_pathsep(buf);
3434 l = STRLEN(buf);
3435 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3436
3437 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003438 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003439 if (ret != OK)
3440 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003441
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003442 if (ga_grow(gap, *numMatches) == FAIL)
3443 {
3444 FreeWild(*numMatches, *matches);
3445 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003446 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003447
3448 for (int i = 0; i < *numMatches; ++i)
3449 {
3450 char_u *name = (*matches)[i];
3451
3452 if (STRLEN(name) > l)
3453 {
3454 // Check if this name was already found.
3455 hash = hash_hash(name + l);
3456 hi = hash_lookup(ht, name + l, hash);
3457 if (HASHITEM_EMPTY(hi))
3458 {
3459 // Remove the path that was prepended.
3460 STRMOVE(name, name + l);
3461 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3462 hash_add_item(ht, hi, name, hash);
3463 name = NULL;
3464 }
3465 }
3466 vim_free(name);
3467 }
3468 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003469}
3470
3471/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 * Complete a shell command.
3473 * Returns FAIL or OK;
3474 */
3475 static int
3476expand_shellcmd(
3477 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003478 char_u ***matches, // return: array with matches
3479 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480 int flagsarg) // EW_ flags
3481{
3482 char_u *pat;
3483 int i;
3484 char_u *path = NULL;
3485 int mustfree = FALSE;
3486 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003487 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488 size_t l;
3489 char_u *s, *e;
3490 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491 int did_curdir = FALSE;
3492 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003494 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495 if (buf == NULL)
3496 return FAIL;
3497
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003498 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003500 if (pat == NULL)
3501 {
3502 vim_free(buf);
3503 return FAIL;
3504 }
3505
Bram Moolenaar66b51422019-08-18 21:44:12 +02003506 for (i = 0; pat[i]; ++i)
3507 if (pat[i] == '\\' && pat[i + 1] == ' ')
3508 STRMOVE(pat + i, pat + i + 1);
3509
3510 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3511
3512 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3513 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3514 path = (char_u *)".";
3515 else
3516 {
3517 // For an absolute name we don't use $PATH.
3518 if (!mch_isFullName(pat))
3519 path = vim_getenv((char_u *)"PATH", &mustfree);
3520 if (path == NULL)
3521 path = (char_u *)"";
3522 }
3523
3524 // Go over all directories in $PATH. Expand matches in that directory and
3525 // collect them in "ga". When "." is not in $PATH also expand for the
3526 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003527 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003528 hash_init(&found_ht);
3529 for (s = path; ; s = e)
3530 {
K.Takata161b6ac2022-11-14 15:31:07 +00003531#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003533#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003534 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003535#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003536 if (e == NULL)
3537 e = s + STRLEN(s);
3538
3539 if (*s == NUL)
3540 {
3541 if (did_curdir)
3542 break;
3543 // Find directories in the current directory, path is empty.
3544 did_curdir = TRUE;
3545 flags |= EW_DIR;
3546 }
3547 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3548 {
3549 did_curdir = TRUE;
3550 flags |= EW_DIR;
3551 }
3552 else
3553 // Do not match directories inside a $PATH item.
3554 flags &= ~EW_DIR;
3555
3556 l = e - s;
3557 if (l > MAXPATHL - 5)
3558 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003559
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003560 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003561 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003562
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563 if (*e != NUL)
3564 ++e;
3565 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003566 *matches = ga.ga_data;
3567 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568
3569 vim_free(buf);
3570 vim_free(pat);
3571 if (mustfree)
3572 vim_free(path);
3573 hash_clear(&found_ht);
3574 return OK;
3575}
3576
K.Takata161b6ac2022-11-14 15:31:07 +00003577#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578/*
3579 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003580 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003581 */
3582 static void *
3583call_user_expand_func(
3584 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003585 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586{
3587 cmdline_info_T *ccline = get_cmdline_info();
3588 int keep = 0;
3589 typval_T args[4];
3590 sctx_T save_current_sctx = current_sctx;
3591 char_u *pat = NULL;
3592 void *ret;
3593
3594 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3595 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003596
3597 if (ccline->cmdbuff != NULL)
3598 {
3599 keep = ccline->cmdbuff[ccline->cmdlen];
3600 ccline->cmdbuff[ccline->cmdlen] = 0;
3601 }
3602
3603 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3604
3605 args[0].v_type = VAR_STRING;
3606 args[0].vval.v_string = pat;
3607 args[1].v_type = VAR_STRING;
3608 args[1].vval.v_string = xp->xp_line;
3609 args[2].v_type = VAR_NUMBER;
3610 args[2].vval.v_number = xp->xp_col;
3611 args[3].v_type = VAR_UNKNOWN;
3612
3613 current_sctx = xp->xp_script_ctx;
3614
3615 ret = user_expand_func(xp->xp_arg, 3, args);
3616
3617 current_sctx = save_current_sctx;
3618 if (ccline->cmdbuff != NULL)
3619 ccline->cmdbuff[ccline->cmdlen] = keep;
3620
3621 vim_free(pat);
3622 return ret;
3623}
3624
3625/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003626 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3627 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628 */
3629 static int
3630ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003631 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 expand_T *xp,
3633 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003634 char_u ***matches,
3635 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636{
3637 char_u *retstr;
3638 char_u *s;
3639 char_u *e;
3640 int keep;
3641 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003642 int fuzzy;
3643 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003644 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003645
3646 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003647 *matches = NULL;
3648 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003649
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003650 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003651 if (retstr == NULL)
3652 return FAIL;
3653
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003654 if (!fuzzy)
3655 ga_init2(&ga, sizeof(char *), 3);
3656 else
3657 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3658
Bram Moolenaar66b51422019-08-18 21:44:12 +02003659 for (s = retstr; *s != NUL; s = e)
3660 {
3661 e = vim_strchr(s, '\n');
3662 if (e == NULL)
3663 e = s + STRLEN(s);
3664 keep = *e;
3665 *e = NUL;
3666
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003667 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003668 {
3669 if (!fuzzy)
3670 match = vim_regexec(regmatch, s, (colnr_T)0);
3671 else
3672 {
3673 score = fuzzy_match_str(s, pat);
3674 match = (score != 0);
3675 }
3676 }
3677 else
3678 match = TRUE; // match everything
3679
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680 *e = keep;
3681
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003682 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 {
3684 if (ga_grow(&ga, 1) == FAIL)
3685 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 if (!fuzzy)
3687 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3688 else
3689 {
3690 fuzmatch_str_T *fuzmatch =
3691 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003692 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003693 fuzmatch->str = vim_strnsave(s, e - s);
3694 fuzmatch->score = score;
3695 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003696 ++ga.ga_len;
3697 }
3698
3699 if (*e != NUL)
3700 ++e;
3701 }
3702 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003703
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003704 if (ga.ga_len == 0)
3705 return OK;
3706
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003707 if (!fuzzy)
3708 {
3709 *matches = ga.ga_data;
3710 *numMatches = ga.ga_len;
3711 }
3712 else
3713 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003714 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3715 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003716 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003717 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003718 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003719 return OK;
3720}
3721
3722/*
3723 * Expand names with a list returned by a function defined by the user.
3724 */
3725 static int
3726ExpandUserList(
3727 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003728 char_u ***matches,
3729 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730{
3731 list_T *retlist;
3732 listitem_T *li;
3733 garray_T ga;
3734
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003735 *matches = NULL;
3736 *numMatches = 0;
3737 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003738 if (retlist == NULL)
3739 return FAIL;
3740
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003741 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003742 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003743 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003744 {
3745 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3746 continue; // Skip non-string items and empty strings
3747
3748 if (ga_grow(&ga, 1) == FAIL)
3749 break;
3750
3751 ((char_u **)ga.ga_data)[ga.ga_len] =
3752 vim_strsave(li->li_tv.vval.v_string);
3753 ++ga.ga_len;
3754 }
3755 list_unref(retlist);
3756
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003757 *matches = ga.ga_data;
3758 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003759 return OK;
3760}
K.Takata161b6ac2022-11-14 15:31:07 +00003761#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003762
3763/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003764 * Expand "file" for all comma-separated directories in "path".
3765 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003766 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003767 */
3768 void
3769globpath(
3770 char_u *path,
3771 char_u *file,
3772 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003773 int expand_options,
3774 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003775{
3776 expand_T xpc;
3777 char_u *buf;
3778 int i;
3779 int num_p;
3780 char_u **p;
3781
3782 buf = alloc(MAXPATHL);
3783 if (buf == NULL)
3784 return;
3785
3786 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003787 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003788
3789 // Loop over all entries in {path}.
3790 while (*path != NUL)
3791 {
3792 // Copy one item of the path to buf[] and concatenate the file name.
3793 copy_option_part(&path, buf, MAXPATHL, ",");
3794 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3795 {
K.Takata161b6ac2022-11-14 15:31:07 +00003796#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003797 // Using the platform's path separator (\) makes vim incorrectly
3798 // treat it as an escape character, use '/' instead.
3799 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3800 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003801#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003803#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003804 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003805 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003806 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3807 {
3808 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3809
3810 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003811 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003812 for (i = 0; i < num_p; ++i)
3813 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003814 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003815 ++ga->ga_len;
3816 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003817 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003818 }
3819 }
3820 }
3821
3822 vim_free(buf);
3823}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003824
Bram Moolenaareadee482020-09-04 15:37:31 +02003825/*
3826 * Translate some keys pressed when 'wildmenu' is used.
3827 */
3828 int
3829wildmenu_translate_key(
3830 cmdline_info_T *cclp,
3831 int key,
3832 expand_T *xp,
3833 int did_wild_list)
3834{
3835 int c = key;
3836
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003837 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003838 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003839 // When the popup menu is used for cmdline completion:
3840 // Up : go to the previous item in the menu
3841 // Down : go to the next item in the menu
3842 // Left : go to the parent directory
3843 // Right: list the files in the selected directory
3844 switch (c)
3845 {
3846 case K_UP: c = K_LEFT; break;
3847 case K_DOWN: c = K_RIGHT; break;
3848 case K_LEFT: c = K_UP; break;
3849 case K_RIGHT: c = K_DOWN; break;
3850 default: break;
3851 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003852 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003853
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003854 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003855 {
3856 if (c == K_LEFT)
3857 c = Ctrl_P;
3858 else if (c == K_RIGHT)
3859 c = Ctrl_N;
3860 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003861
Bram Moolenaareadee482020-09-04 15:37:31 +02003862 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003863 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003864 && cclp->cmdpos > 1
3865 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3866 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3867 && (c == '\n' || c == '\r' || c == K_KENTER))
3868 c = K_DOWN;
3869
3870 return c;
3871}
3872
3873/*
3874 * Delete characters on the command line, from "from" to the current
3875 * position.
3876 */
3877 static void
3878cmdline_del(cmdline_info_T *cclp, int from)
3879{
3880 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3881 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3882 cclp->cmdlen -= cclp->cmdpos - from;
3883 cclp->cmdpos = from;
3884}
3885
3886/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887 * Handle a key pressed when the wild menu for the menu names
3888 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003889 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003890 static int
3891wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003892{
Bram Moolenaareadee482020-09-04 15:37:31 +02003893 int i;
3894 int j;
3895
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003896 // Hitting <Down> after "emenu Name.": complete submenu
3897 if (key == K_DOWN && cclp->cmdpos > 0
3898 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003899 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003900 key = p_wc;
3901 KeyTyped = TRUE; // in case the key was mapped
3902 }
3903 else if (key == K_UP)
3904 {
3905 // Hitting <Up>: Remove one submenu name in front of the
3906 // cursor
3907 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003908
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003909 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3910 i = 0;
3911 while (--j > 0)
3912 {
3913 // check for start of menu name
3914 if (cclp->cmdbuff[j] == ' '
3915 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003916 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003917 i = j + 1;
3918 break;
3919 }
3920 // check for start of submenu name
3921 if (cclp->cmdbuff[j] == '.'
3922 && cclp->cmdbuff[j - 1] != '\\')
3923 {
3924 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003925 {
3926 i = j + 1;
3927 break;
3928 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003929 else
3930 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003931 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003932 }
3933 if (i > 0)
3934 cmdline_del(cclp, i);
3935 key = p_wc;
3936 KeyTyped = TRUE; // in case the key was mapped
3937 xp->xp_context = EXPAND_NOTHING;
3938 }
3939
3940 return key;
3941}
3942
3943/*
3944 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3945 * directory names (EXPAND_DIRECTORIES) or shell command names
3946 * (EXPAND_SHELLCMD) is displayed.
3947 */
3948 static int
3949wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3950{
3951 int i;
3952 int j;
3953 char_u upseg[5];
3954
3955 upseg[0] = PATHSEP;
3956 upseg[1] = '.';
3957 upseg[2] = '.';
3958 upseg[3] = PATHSEP;
3959 upseg[4] = NUL;
3960
3961 if (key == K_DOWN
3962 && cclp->cmdpos > 0
3963 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3964 && (cclp->cmdpos < 3
3965 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3966 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3967 {
3968 // go down a directory
3969 key = p_wc;
3970 KeyTyped = TRUE; // in case the key was mapped
3971 }
3972 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3973 {
3974 // If in a direct ancestor, strip off one ../ to go down
3975 int found = FALSE;
3976
3977 j = cclp->cmdpos;
3978 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3979 while (--j > i)
3980 {
3981 if (has_mbyte)
3982 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3983 if (vim_ispathsep(cclp->cmdbuff[j]))
3984 {
3985 found = TRUE;
3986 break;
3987 }
3988 }
3989 if (found
3990 && cclp->cmdbuff[j - 1] == '.'
3991 && cclp->cmdbuff[j - 2] == '.'
3992 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3993 {
3994 cmdline_del(cclp, j - 2);
3995 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003996 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003997 }
3998 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003999 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004000 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004001 // go up a directory
4002 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004003
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004004 j = cclp->cmdpos - 1;
4005 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4006 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004007 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004008 if (has_mbyte)
4009 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4010 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004011#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004012 && vim_strchr((char_u *)" *?[{`$%#",
4013 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004014#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004015 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004016 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004017 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004018 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004019 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004020 break;
4021 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004022 else
4023 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004024 }
4025 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004026
4027 if (!found)
4028 j = i;
4029 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4030 j += 4;
4031 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4032 && j == i)
4033 j += 3;
4034 else
4035 j = 0;
4036 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004037 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004038 // TODO this is only for DOS/UNIX systems - need to put in
4039 // machine-specific stuff here and in upseg init
4040 cmdline_del(cclp, j);
4041 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004042 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004043 else if (cclp->cmdpos > i)
4044 cmdline_del(cclp, i);
4045
4046 // Now complete in the new directory. Set KeyTyped in case the
4047 // Up key came from a mapping.
4048 key = p_wc;
4049 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004050 }
4051
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004052 return key;
4053}
4054
4055/*
4056 * Handle a key pressed when the wild menu is displayed
4057 */
4058 int
4059wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4060{
4061 if (xp->xp_context == EXPAND_MENUNAMES)
4062 return wildmenu_process_key_menunames(cclp, key, xp);
4063 else if ((xp->xp_context == EXPAND_FILES
4064 || xp->xp_context == EXPAND_DIRECTORIES
4065 || xp->xp_context == EXPAND_SHELLCMD))
4066 return wildmenu_process_key_filenames(cclp, key, xp);
4067
4068 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004069}
4070
4071/*
4072 * Free expanded names when finished walking through the matches
4073 */
4074 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004075wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004076{
4077 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004078
4079 if (!p_wmnu || wild_menu_showing == 0)
4080 return;
4081
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004082#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004083 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004084 if (cclp->input_fn)
4085 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004086#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004087
4088 if (wild_menu_showing == WM_SCROLLED)
4089 {
4090 // Entered command line, move it up
4091 cmdline_row--;
4092 redrawcmd();
4093 }
4094 else if (save_p_ls != -1)
4095 {
4096 // restore 'laststatus' and 'winminheight'
4097 p_ls = save_p_ls;
4098 p_wmh = save_p_wmh;
4099 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004100 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004101 redrawcmd();
4102 save_p_ls = -1;
4103 }
4104 else
4105 {
4106 win_redraw_last_status(topframe);
4107 redraw_statuslines();
4108 }
4109 KeyTyped = skt;
4110 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004111#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004112 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004113 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004114#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004115}
Bram Moolenaareadee482020-09-04 15:37:31 +02004116
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004117#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004118/*
4119 * "getcompletion()" function
4120 */
4121 void
4122f_getcompletion(typval_T *argvars, typval_T *rettv)
4123{
4124 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004125 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004126 expand_T xpc;
4127 int filtered = FALSE;
4128 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004129 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004130
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004131 if (in_vim9script()
4132 && (check_for_string_arg(argvars, 0) == FAIL
4133 || check_for_string_arg(argvars, 1) == FAIL
4134 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4135 return;
4136
ii144785fe02021-11-21 12:13:56 +00004137 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004138 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004139 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004140 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004141
4142 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004143 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004144
4145 if (p_wic)
4146 options |= WILD_ICASE;
4147
4148 // For filtered results, 'wildignore' is used
4149 if (!filtered)
4150 options |= WILD_KEEP_ALL;
4151
4152 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004153 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004154 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004155 int cmdline_len = (int)STRLEN(pat);
4156 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004157 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004158 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004159 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004160 else
4161 {
ii144785fe02021-11-21 12:13:56 +00004162 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004163 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004164 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004165
4166 xpc.xp_context = cmdcomplete_str_to_type(type);
4167 if (xpc.xp_context == EXPAND_NOTHING)
4168 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004169 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004170 return;
4171 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004172
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004173 if (xpc.xp_context == EXPAND_USER_DEFINED)
4174 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004175 // Must be "custom,funcname" pattern
4176 if (STRNCMP(type, "custom,", 7) != 0)
4177 {
4178 semsg(_(e_invalid_argument_str), type);
4179 return;
4180 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004181
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004182 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004183 }
4184
4185 if (xpc.xp_context == EXPAND_USER_LIST)
4186 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004187 // Must be "customlist,funcname" pattern
4188 if (STRNCMP(type, "customlist,", 11) != 0)
4189 {
4190 semsg(_(e_invalid_argument_str), type);
4191 return;
4192 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004193
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004194 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004195 }
4196
Bram Moolenaar66b51422019-08-18 21:44:12 +02004197# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004198 if (xpc.xp_context == EXPAND_MENUS)
4199 {
4200 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4201 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004203# endif
4204# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004205 if (xpc.xp_context == EXPAND_CSCOPE)
4206 {
4207 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4208 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4209 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004210# endif
4211# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004212 if (xpc.xp_context == EXPAND_SIGN)
4213 {
4214 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4215 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4216 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004217# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004218 if (xpc.xp_context == EXPAND_RUNTIME)
4219 {
4220 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4221 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4222 }
zeertzjq85f36d62024-10-10 19:14:13 +02004223 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4224 {
4225 int context = EXPAND_SHELLCMDLINE;
4226 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4227 &context);
4228 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4229 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004230 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004231
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004232 if (cmdline_fuzzy_completion_supported(&xpc))
4233 // when fuzzy matching, don't modify the search string
4234 pat = vim_strsave(xpc.xp_pattern);
4235 else
4236 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4237
Bram Moolenaar93a10962022-06-16 11:42:09 +01004238 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004239 {
4240 int i;
4241
4242 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4243
4244 for (i = 0; i < xpc.xp_numfiles; i++)
4245 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4246 }
4247 vim_free(pat);
4248 ExpandCleanup(&xpc);
4249}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004250#endif // FEAT_EVAL