blob: 85422b802846f4fda6e937a3594adb23330c9f1a [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
zeertzjq7a5115c2025-03-19 20:29:58 +010018static void set_context_for_wildcard_arg(exarg_T *eap, char_u *arg, int usefilter, expand_T *xp, int *complp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000019static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000020static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020021static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000022static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020023#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000024static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000025static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020026#endif
27
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000028// "compl_match_array" points the currently displayed list of entries in the
29// popup menu. It is NULL when there is no popup menu.
30static pumitem_T *compl_match_array = NULL;
31static int compl_match_arraysize;
32// First column in cmdline of the matched item for completion.
33static int compl_startcol;
34static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000035
zeertzjqc51a3762022-12-10 10:22:29 +000036#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000037
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000038/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000039 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
40 * context.
41 */
42 static int
43cmdline_fuzzy_completion_supported(expand_T *xp)
44{
45 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
46 && xp->xp_context != EXPAND_BOOL_SETTINGS
47 && xp->xp_context != EXPAND_COLORS
48 && xp->xp_context != EXPAND_COMPILER
49 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020050 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000051 && xp->xp_context != EXPAND_FILES
52 && xp->xp_context != EXPAND_FILES_IN_PATH
53 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010054 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010056 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000057 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020058 && xp->xp_context != EXPAND_STRING_SETTING
59 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000060 && xp->xp_context != EXPAND_OWNSYNTAX
61 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000062 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020064 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000065 && xp->xp_context != EXPAND_TAGS
66 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000067 && xp->xp_context != EXPAND_USER_LIST);
68}
69
70/*
71 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000072 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
73 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000074 */
75 int
76cmdline_fuzzy_complete(char_u *fuzzystr)
77{
78 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
79}
80
81/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000082 * sort function for the completion matches.
83 * <SNR> functions should be sorted to the end.
84 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020085 static int
86sort_func_compare(const void *s1, const void *s2)
87{
88 char_u *p1 = *(char_u **)s1;
89 char_u *p2 = *(char_u **)s2;
90
91 if (*p1 != '<' && *p2 == '<') return -1;
92 if (*p1 == '<' && *p2 != '<') return 1;
93 return STRCMP(p1, p2);
94}
Bram Moolenaar66b51422019-08-18 21:44:12 +020095
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000096/*
97 * Escape special characters in the cmdline completion matches.
98 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020099 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000100wildescape(
101 expand_T *xp,
102 char_u *str,
103 int numfiles,
104 char_u **files)
105{
106 char_u *p;
107 int vse_what = xp->xp_context == EXPAND_BUFFERS
108 ? VSE_BUFFER : VSE_NONE;
109
110 if (xp->xp_context == EXPAND_FILES
111 || xp->xp_context == EXPAND_FILES_IN_PATH
112 || xp->xp_context == EXPAND_SHELLCMD
113 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200114 || xp->xp_context == EXPAND_DIRECTORIES
115 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000116 {
117 // Insert a backslash into a file name before a space, \, %, #
118 // and wildmatch characters, except '~'.
119 for (int i = 0; i < numfiles; ++i)
120 {
121 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200122 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000123 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200124 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
125 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000126 if (p != NULL)
127 {
128 vim_free(files[i]);
129 files[i] = p;
130#if defined(BACKSLASH_IN_FILENAME)
131 p = vim_strsave_escaped(files[i], (char_u *)" ");
132 if (p != NULL)
133 {
134 vim_free(files[i]);
135 files[i] = p;
136 }
137#endif
138 }
139 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200140 else if (xp->xp_backslash & XP_BS_COMMA)
141 {
142 if (vim_strchr(files[i], ',') != NULL)
143 {
144 p = vim_strsave_escaped(files[i], (char_u *)",");
145 if (p != NULL)
146 {
147 vim_free(files[i]);
148 files[i] = p;
149 }
150 }
151 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000152#ifdef BACKSLASH_IN_FILENAME
153 p = vim_strsave_fnameescape(files[i], vse_what);
154#else
155 p = vim_strsave_fnameescape(files[i],
156 xp->xp_shell ? VSE_SHELL : vse_what);
157#endif
158 if (p != NULL)
159 {
160 vim_free(files[i]);
161 files[i] = p;
162 }
163
164 // If 'str' starts with "\~", replace "~" at start of
165 // files[i] with "\~".
166 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
167 escape_fname(&files[i]);
168 }
169 xp->xp_backslash = XP_BS_NONE;
170
171 // If the first file starts with a '+' escape it. Otherwise it
172 // could be seen as "+cmd".
173 if (*files[0] == '+')
174 escape_fname(&files[0]);
175 }
176 else if (xp->xp_context == EXPAND_TAGS)
177 {
178 // Insert a backslash before characters in a tag name that
179 // would terminate the ":tag" command.
180 for (int i = 0; i < numfiles; ++i)
181 {
182 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
183 if (p != NULL)
184 {
185 vim_free(files[i]);
186 files[i] = p;
187 }
188 }
189 }
190}
191
192/*
193 * Escape special characters in the cmdline completion matches.
194 */
195 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200196ExpandEscape(
197 expand_T *xp,
198 char_u *str,
199 int numfiles,
200 char_u **files,
201 int options)
202{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200203 // May change home directory back to "~"
204 if (options & WILD_HOME_REPLACE)
205 tilde_replace(str, numfiles, files);
206
207 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000208 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200209}
210
211/*
212 * Return FAIL if this is not an appropriate context in which to do
213 * completion of anything, return OK if it is (even if there are no matches).
214 * For the caller, this means that the character is just passed through like a
215 * normal character (instead of being expanded). This allows :s/^I^D etc.
216 */
217 int
218nextwild(
219 expand_T *xp,
220 int type,
221 int options, // extra options for ExpandOne()
222 int escape) // if TRUE, escape the returned matches
223{
224 cmdline_info_T *ccline = get_cmdline_info();
225 int i, j;
226 char_u *p1;
227 char_u *p2;
228 int difflen;
229 int v;
230
231 if (xp->xp_numfiles == -1)
232 {
Jim Zhou3255af82025-02-27 19:29:50 +0100233#ifdef FEAT_EVAL
zeertzjq7a5115c2025-03-19 20:29:58 +0100234 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
Jim Zhou3255af82025-02-27 19:29:50 +0100235 {
236 // Expand commands typed in input() function
237 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
zeertzjq7a5115c2025-03-19 20:29:58 +0100238 }
239 else
Jim Zhou3255af82025-02-27 19:29:50 +0100240#endif
zeertzjq7a5115c2025-03-19 20:29:58 +0100241 {
Jim Zhou3255af82025-02-27 19:29:50 +0100242 set_expand_context(xp);
zeertzjq7a5115c2025-03-19 20:29:58 +0100243 }
244 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200245 }
246
247 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
248 {
249 beep_flush();
250 return OK; // Something illegal on command line
251 }
252 if (xp->xp_context == EXPAND_NOTHING)
253 {
254 // Caller can use the character as a normal char instead
255 return FAIL;
256 }
257
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000258 // If cmd_silent is set then don't show the dots, because redrawcmd() below
259 // won't remove them.
260 if (!cmd_silent)
261 {
262 msg_puts("..."); // show that we are busy
263 out_flush();
264 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200265
266 i = (int)(xp->xp_pattern - ccline->cmdbuff);
267 xp->xp_pattern_len = ccline->cmdpos - i;
268
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000269 if (type == WILD_NEXT || type == WILD_PREV
270 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200271 {
272 // Get next/previous match for a previous expanded pattern.
273 p2 = ExpandOne(xp, NULL, NULL, 0, type);
274 }
275 else
276 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000277 if (cmdline_fuzzy_completion_supported(xp))
278 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200279 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000280 else
281 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
282
Bram Moolenaar66b51422019-08-18 21:44:12 +0200283 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000284 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200285 p2 = NULL;
286 else
287 {
288 int use_options = options |
289 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
Girish Palya2bacc3e2025-03-02 22:55:57 +0100290 if (use_options & WILD_KEEP_SOLE_ITEM)
291 use_options &= ~WILD_KEEP_SOLE_ITEM;
292
Bram Moolenaar66b51422019-08-18 21:44:12 +0200293 if (escape)
294 use_options |= WILD_ESCAPE;
295
296 if (p_wic)
297 use_options += WILD_ICASE;
298 p2 = ExpandOne(xp, p1,
299 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
300 use_options, type);
301 vim_free(p1);
302 // longest match: make sure it is not shorter, happens with :help
303 if (p2 != NULL && type == WILD_LONGEST)
304 {
305 for (j = 0; j < xp->xp_pattern_len; ++j)
306 if (ccline->cmdbuff[i + j] == '*'
307 || ccline->cmdbuff[i + j] == '?')
308 break;
309 if ((int)STRLEN(p2) < j)
310 VIM_CLEAR(p2);
311 }
312 }
313 }
314
315 if (p2 != NULL && !got_int)
316 {
317 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
318 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
319 {
320 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
321 xp->xp_pattern = ccline->cmdbuff + i;
322 }
323 else
324 v = OK;
325 if (v == OK)
326 {
327 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
328 &ccline->cmdbuff[ccline->cmdpos],
329 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
330 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
331 ccline->cmdlen += difflen;
332 ccline->cmdpos += difflen;
333 }
334 }
335 vim_free(p2);
336
337 redrawcmd();
338 cursorcmd();
339
340 // When expanding a ":map" command and no matches are found, assume that
341 // the key is supposed to be inserted literally
342 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
343 return FAIL;
344
345 if (xp->xp_numfiles <= 0 && p2 == NULL)
346 beep_flush();
Girish Palya2bacc3e2025-03-02 22:55:57 +0100347 else if (xp->xp_numfiles == 1 && !(options & WILD_KEEP_SOLE_ITEM))
Bram Moolenaar66b51422019-08-18 21:44:12 +0200348 // free expanded pattern
349 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
350
351 return OK;
352}
353
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000354/*
355 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000356 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 */
358 static int
359cmdline_pum_create(
360 cmdline_info_T *ccline,
361 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000362 char_u **matches,
363 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000364 int showtail)
365{
366 int i;
367 int columns;
368
369 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000370 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000371 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 {
zeertzjqc51a3762022-12-10 10:22:29 +0000374 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 compl_match_array[i].pum_info = NULL;
376 compl_match_array[i].pum_extra = NULL;
377 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200378 compl_match_array[i].pum_user_abbr_hlattr = -1;
379 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000380 }
381
382 // Compute the popup menu starting column
383 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
384 columns = vim_strsize(xp->xp_pattern);
385 if (showtail)
386 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000387 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000388 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000389 }
glepnir977561a2025-02-13 20:48:56 +0100390 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000391
392 // no default selection
393 compl_selected = -1;
394
395 cmdline_pum_display();
396
397 return EXPAND_OK;
398}
399
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400/*
401 * Display the cmdline completion matches in a popup menu
402 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000403 void
404cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000405{
406 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
407}
408
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000409/*
410 * Returns TRUE if the cmdline completion popup menu is being displayed.
411 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000412 int
413cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000414{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100415 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000416}
417
418/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000419 * Remove the cmdline completion popup menu (if present), free the list of
420 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000421 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000422 void
zeertzjq1830e782025-03-13 20:29:13 +0100423cmdline_pum_remove(cmdline_info_T *cclp UNUSED)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000425 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100426 int save_KeyTyped = KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100427#ifdef FEAT_EVAL
428 int save_RedrawingDisabled = RedrawingDisabled;
429 if (cclp->input_fn)
430 RedrawingDisabled = 0;
431#endif
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000432
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000433 pum_undisplay();
434 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000435 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000436 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000437 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000438 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100439
440 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
441 // as a side effect.
442 KeyTyped = save_KeyTyped;
zeertzjq1830e782025-03-13 20:29:13 +0100443#ifdef FEAT_EVAL
444 if (cclp->input_fn)
445 RedrawingDisabled = save_RedrawingDisabled;
446#endif
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000447}
448
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000449 void
450cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000451{
zeertzjq1830e782025-03-13 20:29:13 +0100452 cmdline_pum_remove(cclp);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000453 wildmenu_cleanup(cclp);
454}
455
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000456/*
457 * Returns the starting column number to use for the cmdline completion popup
458 * menu.
459 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000460 int
461cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000462{
463 return compl_startcol;
464}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000465
Bram Moolenaar66b51422019-08-18 21:44:12 +0200466/*
zeertzjqd8c93402024-06-17 18:25:32 +0200467 * Returns the current cmdline completion pattern.
468 */
469 char_u *
470cmdline_compl_pattern(void)
471{
472 expand_T *xp = get_cmdline_info()->xpc;
473
474 return xp == NULL ? NULL : xp->xp_orig;
475}
476
477/*
478 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
479 */
480 int
481cmdline_compl_is_fuzzy(void)
482{
483 expand_T *xp = get_cmdline_info()->xpc;
484
485 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
486}
487
488/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000489 * Return the number of characters that should be skipped in a status match.
490 * These are backslashes used for escaping. Do show backslashes in help tags.
491 */
492 static int
493skip_status_match_char(expand_T *xp, char_u *s)
494{
495 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
496#ifdef FEAT_MENU
497 || ((xp->xp_context == EXPAND_MENUS
498 || xp->xp_context == EXPAND_MENUNAMES)
499 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
500#endif
501 )
502 {
503#ifndef BACKSLASH_IN_FILENAME
504 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
505 return 2;
506#endif
507 return 1;
508 }
509 return 0;
510}
511
512/*
513 * Get the length of an item as it will be shown in the status line.
514 */
515 static int
516status_match_len(expand_T *xp, char_u *s)
517{
518 int len = 0;
519
520#ifdef FEAT_MENU
521 int emenu = xp->xp_context == EXPAND_MENUS
522 || xp->xp_context == EXPAND_MENUNAMES;
523
524 // Check for menu separators - replace with '|'.
525 if (emenu && menu_is_separator(s))
526 return 1;
527#endif
528
529 while (*s != NUL)
530 {
531 s += skip_status_match_char(xp, s);
532 len += ptr2cells(s);
533 MB_PTR_ADV(s);
534 }
535
536 return len;
537}
538
539/*
540 * Show wildchar matches in the status line.
541 * Show at least the "match" item.
542 * We start at item 'first_match' in the list and show all matches that fit.
543 *
544 * If inversion is possible we use it. Else '=' characters are used.
545 */
546 static void
547win_redr_status_matches(
548 expand_T *xp,
549 int num_matches,
550 char_u **matches, // list of matches
551 int match,
552 int showtail)
553{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000554 int row;
555 char_u *buf;
556 int len;
557 int clen; // length in screen cells
558 int fillchar;
559 int attr;
560 int i;
561 int highlight = TRUE;
562 char_u *selstart = NULL;
563 int selstart_col = 0;
564 char_u *selend = NULL;
565 static int first_match = 0;
566 int add_left = FALSE;
567 char_u *s;
568#ifdef FEAT_MENU
569 int emenu;
570#endif
571 int l;
572
573 if (matches == NULL) // interrupted completion?
574 return;
575
576 if (has_mbyte)
577 buf = alloc(Columns * MB_MAXBYTES + 1);
578 else
579 buf = alloc(Columns + 1);
580 if (buf == NULL)
581 return;
582
583 if (match == -1) // don't show match but original text
584 {
585 match = 0;
586 highlight = FALSE;
587 }
588 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000589 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000590 if (match == 0)
591 first_match = 0;
592 else if (match < first_match)
593 {
594 // jumping left, as far as we can go
595 first_match = match;
596 add_left = TRUE;
597 }
598 else
599 {
600 // check if match fits on the screen
601 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000602 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000603 if (first_match > 0)
604 clen += 2;
605 // jumping right, put match at the left
606 if ((long)clen > Columns)
607 {
608 first_match = match;
609 // if showing the last match, we can add some on the left
610 clen = 2;
611 for (i = match; i < num_matches; ++i)
612 {
zeertzjqc51a3762022-12-10 10:22:29 +0000613 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000614 if ((long)clen >= Columns)
615 break;
616 }
617 if (i == num_matches)
618 add_left = TRUE;
619 }
620 }
621 if (add_left)
622 while (first_match > 0)
623 {
zeertzjqc51a3762022-12-10 10:22:29 +0000624 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000625 if ((long)clen >= Columns)
626 break;
627 --first_match;
628 }
629
630 fillchar = fillchar_status(&attr, curwin);
631
632 if (first_match == 0)
633 {
634 *buf = NUL;
635 len = 0;
636 }
637 else
638 {
639 STRCPY(buf, "< ");
640 len = 2;
641 }
642 clen = len;
643
644 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000645 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000646 {
647 if (i == match)
648 {
649 selstart = buf + len;
650 selstart_col = clen;
651 }
652
zeertzjqc51a3762022-12-10 10:22:29 +0000653 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000654 // Check for menu separators - replace with '|'
655#ifdef FEAT_MENU
656 emenu = (xp->xp_context == EXPAND_MENUS
657 || xp->xp_context == EXPAND_MENUNAMES);
658 if (emenu && menu_is_separator(s))
659 {
660 STRCPY(buf + len, transchar('|'));
661 l = (int)STRLEN(buf + len);
662 len += l;
663 clen += l;
664 }
665 else
666#endif
667 for ( ; *s != NUL; ++s)
668 {
669 s += skip_status_match_char(xp, s);
670 clen += ptr2cells(s);
671 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
672 {
673 STRNCPY(buf + len, s, l);
674 s += l - 1;
675 len += l;
676 }
677 else
678 {
679 STRCPY(buf + len, transchar_byte(*s));
680 len += (int)STRLEN(buf + len);
681 }
682 }
683 if (i == match)
684 selend = buf + len;
685
686 *(buf + len++) = ' ';
687 *(buf + len++) = ' ';
688 clen += 2;
689 if (++i == num_matches)
690 break;
691 }
692
693 if (i != num_matches)
694 {
695 *(buf + len++) = '>';
696 ++clen;
697 }
698
699 buf[len] = NUL;
700
701 row = cmdline_row - 1;
702 if (row >= 0)
703 {
704 if (wild_menu_showing == 0)
705 {
706 if (msg_scrolled > 0)
707 {
708 // Put the wildmenu just above the command line. If there is
709 // no room, scroll the screen one line up.
710 if (cmdline_row == Rows - 1)
711 {
712 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
713 ++msg_scrolled;
714 }
715 else
716 {
717 ++cmdline_row;
718 ++row;
719 }
720 wild_menu_showing = WM_SCROLLED;
721 }
722 else
723 {
724 // Create status line if needed by setting 'laststatus' to 2.
725 // Set 'winminheight' to zero to avoid that the window is
726 // resized.
727 if (lastwin->w_status_height == 0)
728 {
729 save_p_ls = p_ls;
730 save_p_wmh = p_wmh;
731 p_ls = 2;
732 p_wmh = 0;
733 last_status(FALSE);
734 }
735 wild_menu_showing = WM_SHOWN;
736 }
737 }
738
739 screen_puts(buf, row, 0, attr);
740 if (selstart != NULL && highlight)
741 {
742 *selend = NUL;
743 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
744 }
745
746 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
747 }
748
749 win_redraw_last_status(topframe);
750 vim_free(buf);
751}
752
753/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000754 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200755 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000756 */
757 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100758get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000759{
glepnir977561a2025-02-13 20:48:56 +0100760 int findex = xp->xp_selected;
761 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000762
zeertzjqb6c900b2025-02-14 17:59:31 +0100763 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000764 if (xp->xp_numfiles <= 0)
765 return NULL;
766
767 if (mode == WILD_PREV)
768 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100769 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000770 if (findex == -1)
771 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100772 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000773 --findex;
774 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000775 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000776 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100777 // Select the next entry
778 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000779 }
glepnir977561a2025-02-13 20:48:56 +0100780 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000781 {
glepnir977561a2025-02-13 20:48:56 +0100782 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
783 ht = pum_get_height();
784 if (ht > 3)
785 ht -= 2;
786
787 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000788 {
glepnir977561a2025-02-13 20:48:56 +0100789 if (findex == 0)
790 // at the first entry, don't select any entries
791 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100792 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100793 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000794 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100795 else
796 // go up by the pum height
797 findex = MAX(findex - ht, 0);
798 }
799 else // mode == WILD_PAGEDOWN
800 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100801 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100802 // at the last entry, don't select any entries
803 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100804 else if (findex < 0)
805 // no entry is selected, select the first entry
806 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100807 else
808 // go down by the pum height
809 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000810 }
811 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000812
glepnir977561a2025-02-13 20:48:56 +0100813 // Handle wrapping around
814 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000815 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100816 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100817 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000818 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000819 else
glepnir977561a2025-02-13 20:48:56 +0100820 // Wrap around to opposite end
821 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822 }
glepnir977561a2025-02-13 20:48:56 +0100823
824 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000825 if (compl_match_array)
826 {
827 compl_selected = findex;
828 cmdline_pum_display();
829 }
830 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100831 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
832 cmd_showtail);
833
zeertzjqe9ef3472023-08-17 23:57:05 +0200834 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100835 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100836 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000837}
838
839/*
840 * Start the command-line expansion and get the matches.
841 */
842 static char_u *
843ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
844{
845 int non_suf_match; // number without matching suffix
846 int i;
847 char_u *ss = NULL;
848
849 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000850 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000851 options) == FAIL)
852 {
853#ifdef FNAME_ILLEGAL
854 // Illegal file name has been silently skipped. But when there
855 // are wildcards, the real problem is that there was no match,
856 // causing the pattern to be added, which has illegal characters.
857 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
858 semsg(_(e_no_match_str_2), str);
859#endif
860 }
861 else if (xp->xp_numfiles == 0)
862 {
863 if (!(options & WILD_SILENT))
864 semsg(_(e_no_match_str_2), str);
865 }
866 else
867 {
868 // Escape the matches for use on the command line.
869 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
870
871 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000872 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000873 {
874 if (xp->xp_numfiles)
875 non_suf_match = xp->xp_numfiles;
876 else
877 non_suf_match = 1;
878 if ((xp->xp_context == EXPAND_FILES
879 || xp->xp_context == EXPAND_DIRECTORIES)
880 && xp->xp_numfiles > 1)
881 {
882 // More than one match; check suffix.
883 // The files will have been sorted on matching suffix in
884 // expand_wildcards, only need to check the first two.
885 non_suf_match = 0;
886 for (i = 0; i < 2; ++i)
887 if (match_suffix(xp->xp_files[i]))
888 ++non_suf_match;
889 }
890 if (non_suf_match != 1)
891 {
892 // Can we ever get here unless it's while expanding
893 // interactively? If not, we can get rid of this all
894 // together. Don't really want to wait for this message
895 // (and possibly have to hit return to continue!).
896 if (!(options & WILD_SILENT))
897 emsg(_(e_too_many_file_names));
898 else if (!(options & WILD_NO_BEEP))
899 beep_flush();
900 }
901 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
902 ss = vim_strsave(xp->xp_files[0]);
903 }
904 }
905
906 return ss;
907}
908
909/*
910 * Return the longest common part in the list of cmdline completion matches.
911 */
912 static char_u *
913find_longest_match(expand_T *xp, int options)
914{
915 long_u len;
916 int mb_len = 1;
917 int c0, ci;
918 int i;
919 char_u *ss;
920
921 for (len = 0; xp->xp_files[0][len]; len += mb_len)
922 {
923 if (has_mbyte)
924 {
925 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
926 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
927 }
928 else
929 c0 = xp->xp_files[0][len];
930 for (i = 1; i < xp->xp_numfiles; ++i)
931 {
932 if (has_mbyte)
933 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
934 else
935 ci = xp->xp_files[i][len];
936 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
937 || xp->xp_context == EXPAND_FILES
938 || xp->xp_context == EXPAND_SHELLCMD
939 || xp->xp_context == EXPAND_BUFFERS))
940 {
941 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
942 break;
943 }
944 else if (c0 != ci)
945 break;
946 }
947 if (i < xp->xp_numfiles)
948 {
949 if (!(options & WILD_NO_BEEP))
950 vim_beep(BO_WILD);
951 break;
952 }
953 }
954
955 ss = alloc(len + 1);
956 if (ss)
957 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
958
959 return ss;
960}
961
962/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000963 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200964 * Chars that should not be expanded must be preceded with a backslash.
965 * Return a pointer to allocated memory containing the new string.
966 * Return NULL for failure.
967 *
968 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200969 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
970 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200971 *
972 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
973 * is WILD_EXPAND_FREE or WILD_ALL.
974 *
975 * mode = WILD_FREE: just free previously expanded matches
976 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
977 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
978 * mode = WILD_NEXT: use next match in multiple match, wrap to first
979 * mode = WILD_PREV: use previous match in multiple match, wrap to first
980 * mode = WILD_ALL: return all matches concatenated
981 * mode = WILD_LONGEST: return longest matched part
982 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000983 * mode = WILD_APPLY: apply the item selected in the cmdline completion
984 * popup menu and close the menu.
985 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
986 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 *
988 * options = WILD_LIST_NOTFOUND: list entries without a match
989 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
990 * options = WILD_USE_NL: Use '\n' for WILD_ALL
991 * options = WILD_NO_BEEP: Don't beep for multiple matches
992 * options = WILD_ADD_SLASH: add a slash after directory names
993 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
994 * options = WILD_SILENT: don't print warning messages
995 * options = WILD_ESCAPE: put backslash before special chars
996 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200997 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200998 *
999 * The variables xp->xp_context and xp->xp_backslash must have been set!
1000 */
1001 char_u *
1002ExpandOne(
1003 expand_T *xp,
1004 char_u *str,
1005 char_u *orig, // allocated copy of original of expanded string
1006 int options,
1007 int mode)
1008{
1009 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001010 int orig_saved = FALSE;
1011 int i;
1012 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013
1014 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001015 if (mode == WILD_NEXT || mode == WILD_PREV
1016 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001017 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001018
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001019 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001020 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001021 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001022 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001023 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001024 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001025
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026 // free old names
1027 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1028 {
1029 FreeWild(xp->xp_numfiles, xp->xp_files);
1030 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001031 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001032
1033 // The entries from xp_files may be used in the PUM, remove it.
1034 if (compl_match_array != NULL)
zeertzjq1830e782025-03-13 20:29:13 +01001035 cmdline_pum_remove(get_cmdline_info());
Bram Moolenaar66b51422019-08-18 21:44:12 +02001036 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001037 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038
1039 if (mode == WILD_FREE) // only release file name
1040 return NULL;
1041
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001042 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001043 {
zeertzjq28a23602023-09-29 19:58:35 +02001044 vim_free(xp->xp_orig);
1045 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001046 orig_saved = TRUE;
1047
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001048 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 }
1050
1051 // Find longest common part
1052 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1053 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001054 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001055 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001056 }
1057
Bram Moolenaar57e95172022-08-20 19:26:14 +01001058 // Concatenate all matching names. Unless interrupted, this can be slow
1059 // and the result probably won't be used.
1060 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 {
1062 len = 0;
1063 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001064 {
1065 if (i > 0)
1066 {
1067 if (xp->xp_prefix == XP_PREFIX_NO)
1068 len += 2; // prefix "no"
1069 else if (xp->xp_prefix == XP_PREFIX_INV)
1070 len += 3; // prefix "inv"
1071 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001072 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001073 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001074 ss = alloc(len);
1075 if (ss != NULL)
1076 {
1077 *ss = NUL;
1078 for (i = 0; i < xp->xp_numfiles; ++i)
1079 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001080 if (i > 0)
1081 {
1082 if (xp->xp_prefix == XP_PREFIX_NO)
1083 STRCAT(ss, "no");
1084 else if (xp->xp_prefix == XP_PREFIX_INV)
1085 STRCAT(ss, "inv");
1086 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001087 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001088
Bram Moolenaar66b51422019-08-18 21:44:12 +02001089 if (i != xp->xp_numfiles - 1)
1090 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1091 }
1092 }
1093 }
1094
1095 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1096 ExpandCleanup(xp);
1097
zeertzjq28a23602023-09-29 19:58:35 +02001098 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001099 if (!orig_saved)
1100 vim_free(orig);
1101
1102 return ss;
1103}
1104
1105/*
1106 * Prepare an expand structure for use.
1107 */
1108 void
1109ExpandInit(expand_T *xp)
1110{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001111 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001112 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001113 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001114 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001115}
1116
1117/*
1118 * Cleanup an expand structure after use.
1119 */
1120 void
1121ExpandCleanup(expand_T *xp)
1122{
1123 if (xp->xp_numfiles >= 0)
1124 {
1125 FreeWild(xp->xp_numfiles, xp->xp_files);
1126 xp->xp_numfiles = -1;
1127 }
zeertzjq28a23602023-09-29 19:58:35 +02001128 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001129}
1130
1131/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001132 * Display one line of completion matches. Multiple matches are displayed in
1133 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001134 * matches - list of completion match names
1135 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 * lines - number of output lines
1137 * linenr - line number of matches to display
1138 * maxlen - maximum number of characters in each line
1139 * showtail - display only the tail of the full path of a file name
1140 * dir_attr - highlight attribute to use for directory names
1141 */
1142 static void
1143showmatches_oneline(
1144 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001145 char_u **matches,
1146 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001147 int lines,
1148 int linenr,
1149 int maxlen,
1150 int showtail,
1151 int dir_attr)
1152{
1153 int i, j;
1154 int isdir;
1155 int lastlen;
1156 char_u *p;
1157
1158 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001159 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001160 {
1161 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1162 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001163 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1164 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001165 msg_advance(maxlen + 1);
1166 msg_puts((char *)p);
1167 msg_advance(maxlen + 3);
1168 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1169 break;
1170 }
1171 for (i = maxlen - lastlen; --i >= 0; )
1172 msg_putchar(' ');
1173 if (xp->xp_context == EXPAND_FILES
1174 || xp->xp_context == EXPAND_SHELLCMD
1175 || xp->xp_context == EXPAND_BUFFERS)
1176 {
1177 // highlight directories
1178 if (xp->xp_numfiles != -1)
1179 {
1180 char_u *halved_slash;
1181 char_u *exp_path;
1182 char_u *path;
1183
1184 // Expansion was done before and special characters
1185 // were escaped, need to halve backslashes. Also
1186 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001187 exp_path = expand_env_save_opt(matches[j], TRUE);
1188 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001189 halved_slash = backslash_halve_save(path);
1190 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001191 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001192 vim_free(exp_path);
1193 if (halved_slash != path)
1194 vim_free(halved_slash);
1195 }
1196 else
1197 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001198 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001199 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001200 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001201 else
1202 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001203 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001204 TRUE);
1205 p = NameBuff;
1206 }
1207 }
1208 else
1209 {
1210 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001211 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001212 }
1213 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1214 }
1215 if (msg_col > 0) // when not wrapped around
1216 {
1217 msg_clr_eos();
1218 msg_putchar('\n');
1219 }
1220 out_flush(); // show one line at a time
1221}
1222
1223/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 * Show all matches for completion on the command line.
1225 * Returns EXPAND_NOTHING when the character that triggered expansion should
1226 * be inserted like a normal character.
1227 */
1228 int
1229showmatches(expand_T *xp, int wildmenu UNUSED)
1230{
1231 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001232 int numMatches;
1233 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001234 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001235 int maxlen;
1236 int lines;
1237 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001238 int attr;
1239 int showtail;
1240
1241 if (xp->xp_numfiles == -1)
1242 {
1243 set_expand_context(xp);
1244 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001245 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001246 showtail = expand_showtail(xp);
1247 if (i != EXPAND_OK)
1248 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001249 }
1250 else
1251 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001252 numMatches = xp->xp_numfiles;
1253 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001254 showtail = cmd_showtail;
1255 }
1256
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001257 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001258 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001259 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001260
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261 if (!wildmenu)
1262 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 msg_didany = FALSE; // lines_left will be set
1264 msg_start(); // prepare for paging
1265 msg_putchar('\n');
1266 out_flush();
1267 cmdline_row = msg_row;
1268 msg_didany = FALSE; // lines_left will be set again
1269 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001271
1272 if (got_int)
1273 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001275 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001276 else
1277 {
1278 // find the length of the longest file name
1279 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001280 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001281 {
1282 if (!showtail && (xp->xp_context == EXPAND_FILES
1283 || xp->xp_context == EXPAND_SHELLCMD
1284 || xp->xp_context == EXPAND_BUFFERS))
1285 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001286 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001287 j = vim_strsize(NameBuff);
1288 }
1289 else
zeertzjqc51a3762022-12-10 10:22:29 +00001290 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001291 if (j > maxlen)
1292 maxlen = j;
1293 }
1294
1295 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001296 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001297 else
1298 {
1299 // compute the number of columns and lines for the listing
1300 maxlen += 2; // two spaces between file names
1301 columns = ((int)Columns + 2) / maxlen;
1302 if (columns < 1)
1303 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001304 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001305 }
1306
1307 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1308
1309 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1310 {
1311 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1312 msg_clr_eos();
1313 msg_advance(maxlen - 3);
1314 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1315 }
1316
1317 // list the files line by line
1318 for (i = 0; i < lines; ++i)
1319 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001320 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001321 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001322 if (got_int)
1323 {
1324 got_int = FALSE;
1325 break;
1326 }
1327 }
1328
1329 // we redraw the command below the lines that we have just listed
1330 // This is a bit tricky, but it saves a lot of screen updating.
1331 cmdline_row = msg_row; // will put it back later
1332 }
1333
1334 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001335 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001336
1337 return EXPAND_OK;
1338}
1339
1340/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001341 * gettail() version for showmatches() and win_redr_status_matches():
1342 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001343 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001344 static char_u *
1345showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001346{
1347 char_u *p;
1348 char_u *t = s;
1349 int had_sep = FALSE;
1350
1351 for (p = s; *p != NUL; )
1352 {
1353 if (vim_ispathsep(*p)
1354#ifdef BACKSLASH_IN_FILENAME
1355 && !rem_backslash(p)
1356#endif
1357 )
1358 had_sep = TRUE;
1359 else if (had_sep)
1360 {
1361 t = p;
1362 had_sep = FALSE;
1363 }
1364 MB_PTR_ADV(p);
1365 }
1366 return t;
1367}
1368
1369/*
1370 * Return TRUE if we only need to show the tail of completion matches.
1371 * When not completing file names or there is a wildcard in the path FALSE is
1372 * returned.
1373 */
1374 static int
1375expand_showtail(expand_T *xp)
1376{
1377 char_u *s;
1378 char_u *end;
1379
1380 // When not completing file names a "/" may mean something different.
1381 if (xp->xp_context != EXPAND_FILES
1382 && xp->xp_context != EXPAND_SHELLCMD
1383 && xp->xp_context != EXPAND_DIRECTORIES)
1384 return FALSE;
1385
1386 end = gettail(xp->xp_pattern);
1387 if (end == xp->xp_pattern) // there is no path separator
1388 return FALSE;
1389
1390 for (s = xp->xp_pattern; s < end; s++)
1391 {
1392 // Skip escaped wildcards. Only when the backslash is not a path
1393 // separator, on DOS the '*' "path\*\file" must not be skipped.
1394 if (rem_backslash(s))
1395 ++s;
1396 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1397 return FALSE;
1398 }
1399 return TRUE;
1400}
1401
1402/*
1403 * Prepare a string for expansion.
1404 * When expanding file names: The string will be used with expand_wildcards().
1405 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1406 * When expanding other names: The string will be used with regcomp(). Copy
1407 * the name into allocated memory and prepend "^".
1408 */
1409 char_u *
1410addstar(
1411 char_u *fname,
1412 int len,
1413 int context) // EXPAND_FILES etc.
1414{
1415 char_u *retval;
1416 int i, j;
1417 int new_len;
1418 char_u *tail;
1419 int ends_in_star;
1420
1421 if (context != EXPAND_FILES
1422 && context != EXPAND_FILES_IN_PATH
1423 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001424 && context != EXPAND_DIRECTORIES
1425 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001426 {
1427 // Matching will be done internally (on something other than files).
1428 // So we convert the file-matching-type wildcards into our kind for
1429 // use with vim_regcomp(). First work out how long it will be:
1430
1431 // For help tags the translation is done in find_help_tags().
1432 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001433 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001434 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001435 || context == EXPAND_COLORS
1436 || context == EXPAND_COMPILER
1437 || context == EXPAND_OWNSYNTAX
1438 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001439 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001440 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001441 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001442 || ((context == EXPAND_TAGS_LISTFILES
1443 || context == EXPAND_TAGS)
1444 && fname[0] == '/'))
1445 retval = vim_strnsave(fname, len);
1446 else
1447 {
1448 new_len = len + 2; // +2 for '^' at start, NUL at end
1449 for (i = 0; i < len; i++)
1450 {
1451 if (fname[i] == '*' || fname[i] == '~')
1452 new_len++; // '*' needs to be replaced by ".*"
1453 // '~' needs to be replaced by "\~"
1454
1455 // Buffer names are like file names. "." should be literal
1456 if (context == EXPAND_BUFFERS && fname[i] == '.')
1457 new_len++; // "." becomes "\."
1458
1459 // Custom expansion takes care of special things, match
1460 // backslashes literally (perhaps also for other types?)
1461 if ((context == EXPAND_USER_DEFINED
1462 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1463 new_len++; // '\' becomes "\\"
1464 }
1465 retval = alloc(new_len);
1466 if (retval != NULL)
1467 {
1468 retval[0] = '^';
1469 j = 1;
1470 for (i = 0; i < len; i++, j++)
1471 {
1472 // Skip backslash. But why? At least keep it for custom
1473 // expansion.
1474 if (context != EXPAND_USER_DEFINED
1475 && context != EXPAND_USER_LIST
1476 && fname[i] == '\\'
1477 && ++i == len)
1478 break;
1479
1480 switch (fname[i])
1481 {
1482 case '*': retval[j++] = '.';
1483 break;
1484 case '~': retval[j++] = '\\';
1485 break;
1486 case '?': retval[j] = '.';
1487 continue;
1488 case '.': if (context == EXPAND_BUFFERS)
1489 retval[j++] = '\\';
1490 break;
1491 case '\\': if (context == EXPAND_USER_DEFINED
1492 || context == EXPAND_USER_LIST)
1493 retval[j++] = '\\';
1494 break;
1495 }
1496 retval[j] = fname[i];
1497 }
1498 retval[j] = NUL;
1499 }
1500 }
1501 }
1502 else
1503 {
1504 retval = alloc(len + 4);
1505 if (retval != NULL)
1506 {
1507 vim_strncpy(retval, fname, len);
1508
1509 // Don't add a star to *, ~, ~user, $var or `cmd`.
1510 // * would become **, which walks the whole tree.
1511 // ~ would be at the start of the file name, but not the tail.
1512 // $ could be anywhere in the tail.
1513 // ` could be anywhere in the file name.
1514 // When the name ends in '$' don't add a star, remove the '$'.
1515 tail = gettail(retval);
1516 ends_in_star = (len > 0 && retval[len - 1] == '*');
1517#ifndef BACKSLASH_IN_FILENAME
1518 for (i = len - 2; i >= 0; --i)
1519 {
1520 if (retval[i] != '\\')
1521 break;
1522 ends_in_star = !ends_in_star;
1523 }
1524#endif
1525 if ((*retval != '~' || tail != retval)
1526 && !ends_in_star
1527 && vim_strchr(tail, '$') == NULL
1528 && vim_strchr(retval, '`') == NULL)
1529 retval[len++] = '*';
1530 else if (len > 0 && retval[len - 1] == '$')
1531 --len;
1532 retval[len] = NUL;
1533 }
1534 }
1535 return retval;
1536}
1537
1538/*
1539 * Must parse the command line so far to work out what context we are in.
1540 * Completion can then be done based on that context.
1541 * This routine sets the variables:
1542 * xp->xp_pattern The start of the pattern to be expanded within
1543 * the command line (ends at the cursor).
1544 * xp->xp_context The type of thing to expand. Will be one of:
1545 *
1546 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1547 * the command line, like an unknown command. Caller
1548 * should beep.
1549 * EXPAND_NOTHING Unrecognised context for completion, use char like
1550 * a normal char, rather than for completion. eg
1551 * :s/^I/
1552 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1553 * it.
1554 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1555 * EXPAND_FILES After command with EX_XFILE set, or after setting
1556 * with P_EXPAND set. eg :e ^I, :w>>^I
1557 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001558 * when we know only directories are of interest.
1559 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001560 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1561 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1562 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1563 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1564 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1565 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1566 * EXPAND_EVENTS Complete event names
1567 * EXPAND_SYNTAX Complete :syntax command arguments
1568 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1569 * EXPAND_AUGROUP Complete autocommand group names
1570 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1571 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1572 * eg :unmap a^I , :cunab x^I
1573 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1574 * eg :call sub^I
1575 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1576 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1577 * names in expressions, eg :while s^I
1578 * EXPAND_ENV_VARS Complete environment variable names
1579 * EXPAND_USER Complete user names
1580 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001581 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001582set_expand_context(expand_T *xp)
1583{
1584 cmdline_info_T *ccline = get_cmdline_info();
1585
1586 // only expansion for ':', '>' and '=' command-lines
1587 if (ccline->cmdfirstc != ':'
1588#ifdef FEAT_EVAL
1589 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1590 && !ccline->input_fn
1591#endif
1592 )
1593 {
1594 xp->xp_context = EXPAND_NOTHING;
1595 return;
1596 }
1597 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1598}
1599
Bram Moolenaard0190392019-08-23 21:17:35 +02001600/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001601 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1602 * For user defined commands, the completion context is set in 'xp' and the
1603 * completion flags in 'complp'.
1604 *
1605 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001606 */
1607 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001608set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001609{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001610 char_u *p = NULL;
1611 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001612 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001613
1614 // Isolate the command and search for it in the command table.
1615 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001616 // - the 'k' command can directly be followed by any character, but do
1617 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1618 // find matches anywhere in the command name, do this only for command
1619 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001621 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001622 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001623 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001624 p = cmd + 1;
1625 }
1626 else
1627 {
1628 p = cmd;
1629 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1630 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001631 // A user command may contain digits.
1632 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1633 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001634 while (ASCII_ISALNUM(*p) || *p == '*')
1635 ++p;
1636 // for python 3.x: ":py3*" commands completion
1637 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1638 {
1639 ++p;
1640 while (ASCII_ISALPHA(*p) || *p == '*')
1641 ++p;
1642 }
1643 // check for non-alpha command
1644 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1645 ++p;
1646 len = (int)(p - cmd);
1647
1648 if (len == 0)
1649 {
1650 xp->xp_context = EXPAND_UNSUCCESSFUL;
1651 return NULL;
1652 }
1653
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001654 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001655
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001656 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001657 // Also when doing fuzzy expansion for non-shell commands, support
1658 // alphanumeric characters.
1659 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1660 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001661 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1662 ++p;
1663 }
1664
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001665 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001666 // character, complete the command name.
1667 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1668 return NULL;
1669
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001670 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001671 {
1672 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1673 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001674 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 p = cmd + 1;
1676 }
1677 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1678 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 eap->cmd = cmd;
1680 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001681 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001683 }
1684 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001685 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001686 {
1687 // Not still touching the command and it was an illegal one
1688 xp->xp_context = EXPAND_UNSUCCESSFUL;
1689 return NULL;
1690 }
1691
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001692 return p;
1693}
Bram Moolenaard0190392019-08-23 21:17:35 +02001694
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001695/*
1696 * Set the completion context for a command argument with wild card characters.
1697 */
1698 static void
1699set_context_for_wildcard_arg(
1700 exarg_T *eap,
1701 char_u *arg,
1702 int usefilter,
1703 expand_T *xp,
1704 int *complp)
1705{
1706 char_u *p;
1707 int c;
1708 int in_quote = FALSE;
1709 char_u *bow = NULL; // Beginning of word
1710 int len = 0;
1711
1712 // Allow spaces within back-quotes to count as part of the argument
1713 // being expanded.
1714 xp->xp_pattern = skipwhite(arg);
1715 p = xp->xp_pattern;
1716 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001718 if (has_mbyte)
1719 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 c = *p;
1722 if (c == '\\' && p[1] != NUL)
1723 ++p;
1724 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001725 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001726 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001727 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001728 xp->xp_pattern = p;
1729 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001730 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 in_quote = !in_quote;
1732 }
1733 // An argument can contain just about everything, except
1734 // characters that end the command and white space.
1735 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001736#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001737 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001738#endif
1739 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001740 {
1741 len = 0; // avoid getting stuck when space is in 'isfname'
1742 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001743 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001744 if (has_mbyte)
1745 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001746 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001747 c = *p;
1748 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001749 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001750 if (has_mbyte)
1751 len = (*mb_ptr2len)(p);
1752 else
1753 len = 1;
1754 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001755 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001756 if (in_quote)
1757 bow = p;
1758 else
1759 xp->xp_pattern = p;
1760 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001761 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001762 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001763 }
1764
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001765 // If we are still inside the quotes, and we passed a space, just
1766 // expand from there.
1767 if (bow != NULL && in_quote)
1768 xp->xp_pattern = bow;
1769 xp->xp_context = EXPAND_FILES;
1770
1771 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001772 if (usefilter
1773 || (eap != NULL
1774 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1775 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001776 {
1777#ifndef BACKSLASH_IN_FILENAME
1778 xp->xp_shell = TRUE;
1779#endif
1780 // When still after the command name expand executables.
1781 if (xp->xp_pattern == skipwhite(arg))
1782 xp->xp_context = EXPAND_SHELLCMD;
1783 }
1784
1785 // Check for environment variable.
1786 if (*xp->xp_pattern == '$')
1787 {
1788 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1789 if (!vim_isIDc(*p))
1790 break;
1791 if (*p == NUL)
1792 {
1793 xp->xp_context = EXPAND_ENV_VARS;
1794 ++xp->xp_pattern;
1795 // Avoid that the assignment uses EXPAND_FILES again.
1796 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1797 *complp = EXPAND_ENV_VARS;
1798 }
1799 }
1800 // Check for user names.
1801 if (*xp->xp_pattern == '~')
1802 {
1803 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1804 ;
1805 // Complete ~user only if it partially matches a user name.
1806 // A full match ~user<Tab> will be replaced by user's home
1807 // directory i.e. something like ~user<Tab> -> /home/user/
1808 if (*p == NUL && p > xp->xp_pattern + 1
1809 && match_user(xp->xp_pattern + 1) >= 1)
1810 {
1811 xp->xp_context = EXPAND_USER;
1812 ++xp->xp_pattern;
1813 }
1814 }
1815}
1816
1817/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001818 * Set the completion context for the "++opt=arg" argument. Always returns
1819 * NULL.
1820 */
1821 static char_u *
1822set_context_in_argopt(expand_T *xp, char_u *arg)
1823{
1824 char_u *p;
1825
1826 p = vim_strchr(arg, '=');
1827 if (p == NULL)
1828 xp->xp_pattern = arg;
1829 else
1830 xp->xp_pattern = p + 1;
1831
1832 xp->xp_context = EXPAND_ARGOPT;
1833 return NULL;
1834}
1835
1836#ifdef FEAT_TERMINAL
1837/*
1838 * Set the completion context for :terminal's [options]. Always returns NULL.
1839 */
1840 static char_u *
1841set_context_in_terminalopt(expand_T *xp, char_u *arg)
1842{
1843 char_u *p;
1844
1845 p = vim_strchr(arg, '=');
1846 if (p == NULL)
1847 xp->xp_pattern = arg;
1848 else
1849 xp->xp_pattern = p + 1;
1850
1851 xp->xp_context = EXPAND_TERMINALOPT;
1852 return NULL;
1853}
1854#endif
1855
1856/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001857 * Set the completion context for the :filter command. Returns a pointer to the
1858 * next command after the :filter command.
1859 */
1860 static char_u *
1861set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1862{
1863 if (*arg != NUL)
1864 arg = skip_vimgrep_pat(arg, NULL, NULL);
1865 if (arg == NULL || *arg == NUL)
1866 {
1867 xp->xp_context = EXPAND_NOTHING;
1868 return NULL;
1869 }
1870 return skipwhite(arg);
1871}
1872
1873#ifdef FEAT_SEARCH_EXTRA
1874/*
1875 * Set the completion context for the :match command. Returns a pointer to the
1876 * next command after the :match command.
1877 */
1878 static char_u *
1879set_context_in_match_cmd(expand_T *xp, char_u *arg)
1880{
1881 if (*arg == NUL || !ends_excmd(*arg))
1882 {
1883 // also complete "None"
1884 set_context_in_echohl_cmd(xp, arg);
1885 arg = skipwhite(skiptowhite(arg));
1886 if (*arg != NUL)
1887 {
1888 xp->xp_context = EXPAND_NOTHING;
1889 arg = skip_regexp(arg + 1, *arg, magic_isset());
1890 }
1891 }
1892 return find_nextcmd(arg);
1893}
1894#endif
1895
1896/*
1897 * Returns a pointer to the next command after a :global or a :v command.
1898 * Returns NULL if there is no next command.
1899 */
1900 static char_u *
1901find_cmd_after_global_cmd(char_u *arg)
1902{
1903 int delim;
1904
1905 delim = *arg; // get the delimiter
1906 if (delim)
1907 ++arg; // skip delimiter if there is one
1908
1909 while (arg[0] != NUL && arg[0] != delim)
1910 {
1911 if (arg[0] == '\\' && arg[1] != NUL)
1912 ++arg;
1913 ++arg;
1914 }
1915 if (arg[0] != NUL)
1916 return arg + 1;
1917
1918 return NULL;
1919}
1920
1921/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001922 * Returns a pointer to the next command after a :substitute or a :& command.
1923 * Returns NULL if there is no next command.
1924 */
1925 static char_u *
1926find_cmd_after_substitute_cmd(char_u *arg)
1927{
1928 int delim;
1929
1930 delim = *arg;
1931 if (delim)
1932 {
1933 // skip "from" part
1934 ++arg;
1935 arg = skip_regexp(arg, delim, magic_isset());
1936
1937 if (arg[0] != NUL && arg[0] == delim)
1938 {
1939 // skip "to" part
1940 ++arg;
1941 while (arg[0] != NUL && arg[0] != delim)
1942 {
1943 if (arg[0] == '\\' && arg[1] != NUL)
1944 ++arg;
1945 ++arg;
1946 }
1947 if (arg[0] != NUL) // skip delimiter
1948 ++arg;
1949 }
1950 }
1951 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1952 ++arg;
1953 if (arg[0] != NUL)
1954 return arg;
1955
1956 return NULL;
1957}
1958
1959/*
1960 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1961 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1962 * Returns NULL if there is no next command.
1963 */
1964 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001965find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001966{
1967 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001968 if (*arg != '/')
1969 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001970
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001971 // Match regexp, not just whole words
1972 for (++arg; *arg && *arg != '/'; arg++)
1973 if (*arg == '\\' && arg[1] != NUL)
1974 arg++;
1975 if (*arg)
1976 {
1977 arg = skipwhite(arg + 1);
1978
1979 // Check for trailing illegal characters
1980 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1981 xp->xp_context = EXPAND_NOTHING;
1982 else
1983 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001984 }
1985
1986 return NULL;
1987}
1988
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001989#ifdef FEAT_EVAL
1990/*
1991 * Set the completion context for the :unlet command. Always returns NULL.
1992 */
1993 static char_u *
1994set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1995{
1996 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1997 arg = xp->xp_pattern + 1;
1998
1999 xp->xp_context = EXPAND_USER_VARS;
2000 xp->xp_pattern = arg;
2001
2002 if (*xp->xp_pattern == '$')
2003 {
2004 xp->xp_context = EXPAND_ENV_VARS;
2005 ++xp->xp_pattern;
2006 }
2007
2008 return NULL;
2009}
2010#endif
2011
2012#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2013/*
2014 * Set the completion context for the :language command. Always returns NULL.
2015 */
2016 static char_u *
2017set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2018{
2019 char_u *p;
2020
2021 p = skiptowhite(arg);
2022 if (*p == NUL)
2023 {
2024 xp->xp_context = EXPAND_LANGUAGE;
2025 xp->xp_pattern = arg;
2026 }
2027 else
2028 {
2029 if ( STRNCMP(arg, "messages", p - arg) == 0
2030 || STRNCMP(arg, "ctype", p - arg) == 0
2031 || STRNCMP(arg, "time", p - arg) == 0
2032 || STRNCMP(arg, "collate", p - arg) == 0)
2033 {
2034 xp->xp_context = EXPAND_LOCALES;
2035 xp->xp_pattern = skipwhite(p);
2036 }
2037 else
2038 xp->xp_context = EXPAND_NOTHING;
2039 }
2040
2041 return NULL;
2042}
2043#endif
2044
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002045#ifdef FEAT_EVAL
2046static enum
2047{
2048 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002049 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2050 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002051} breakpt_expand_what;
2052
2053/*
2054 * Set the completion context for the :breakadd command. Always returns NULL.
2055 */
2056 static char_u *
2057set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2058{
2059 char_u *p;
2060 char_u *subcmd_start;
2061
2062 xp->xp_context = EXPAND_BREAKPOINT;
2063 xp->xp_pattern = arg;
2064
2065 if (cmdidx == CMD_breakadd)
2066 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002067 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002068 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002069 else
2070 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002071
2072 p = skipwhite(arg);
2073 if (*p == NUL)
2074 return NULL;
2075 subcmd_start = p;
2076
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002077 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002078 {
2079 // :breakadd file [lnum] <filename>
2080 // :breakadd func [lnum] <funcname>
2081 p += 4;
2082 p = skipwhite(p);
2083
2084 // skip line number (if specified)
2085 if (VIM_ISDIGIT(*p))
2086 {
2087 p = skipdigits(p);
2088 if (*p != ' ')
2089 {
2090 xp->xp_context = EXPAND_NOTHING;
2091 return NULL;
2092 }
2093 p = skipwhite(p);
2094 }
2095 if (STRNCMP("file", subcmd_start, 4) == 0)
2096 xp->xp_context = EXPAND_FILES;
2097 else
2098 xp->xp_context = EXPAND_USER_FUNC;
2099 xp->xp_pattern = p;
2100 }
2101 else if (STRNCMP("expr ", p, 5) == 0)
2102 {
2103 // :breakadd expr <expression>
2104 xp->xp_context = EXPAND_EXPRESSION;
2105 xp->xp_pattern = skipwhite(p + 5);
2106 }
2107
2108 return NULL;
2109}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002110
2111 static char_u *
2112set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2113{
2114 char_u *p;
2115
2116 xp->xp_context = EXPAND_NOTHING;
2117 xp->xp_pattern = NULL;
2118
2119 p = skipwhite(arg);
2120 if (VIM_ISDIGIT(*p))
2121 return NULL;
2122
2123 xp->xp_context = EXPAND_SCRIPTNAMES;
2124 xp->xp_pattern = p;
2125
2126 return NULL;
2127}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002128#endif
2129
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002130/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002131 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2132 * The argument to the command is 'arg' and the argument flags is 'argt'.
2133 * For user-defined commands and for environment variables, 'compl' has the
2134 * completion type.
2135 * Returns a pointer to the next command. Returns NULL if there is no next
2136 * command.
2137 */
2138 static char_u *
2139set_context_by_cmdname(
2140 char_u *cmd,
2141 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002142 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002143 char_u *arg,
2144 long argt,
2145 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002146 int forceit)
2147{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002148 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002149 {
2150 case CMD_find:
2151 case CMD_sfind:
2152 case CMD_tabfind:
2153 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002154 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002155 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002156 break;
2157 case CMD_cd:
2158 case CMD_chdir:
2159 case CMD_tcd:
2160 case CMD_tchdir:
2161 case CMD_lcd:
2162 case CMD_lchdir:
2163 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002164 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002165 break;
2166 case CMD_help:
2167 xp->xp_context = EXPAND_HELP;
2168 xp->xp_pattern = arg;
2169 break;
2170
2171 // Command modifiers: return the argument.
2172 // Also for commands with an argument that is a command.
2173 case CMD_aboveleft:
2174 case CMD_argdo:
2175 case CMD_belowright:
2176 case CMD_botright:
2177 case CMD_browse:
2178 case CMD_bufdo:
2179 case CMD_cdo:
2180 case CMD_cfdo:
2181 case CMD_confirm:
2182 case CMD_debug:
2183 case CMD_folddoclosed:
2184 case CMD_folddoopen:
2185 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002186 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002187 case CMD_keepalt:
2188 case CMD_keepjumps:
2189 case CMD_keepmarks:
2190 case CMD_keeppatterns:
2191 case CMD_ldo:
2192 case CMD_leftabove:
2193 case CMD_lfdo:
2194 case CMD_lockmarks:
2195 case CMD_noautocmd:
2196 case CMD_noswapfile:
2197 case CMD_rightbelow:
2198 case CMD_sandbox:
2199 case CMD_silent:
2200 case CMD_tab:
2201 case CMD_tabdo:
2202 case CMD_topleft:
2203 case CMD_verbose:
2204 case CMD_vertical:
2205 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002206 case CMD_vim9cmd:
2207 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002208 return arg;
2209
2210 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002211 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002212
2213#ifdef FEAT_SEARCH_EXTRA
2214 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002215 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002216#endif
2217
2218 // All completion for the +cmdline_compl feature goes here.
2219
2220 case CMD_command:
2221 return set_context_in_user_cmd(xp, arg);
2222
2223 case CMD_delcommand:
2224 xp->xp_context = EXPAND_USER_COMMANDS;
2225 xp->xp_pattern = arg;
2226 break;
2227
2228 case CMD_global:
2229 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002230 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002231 case CMD_and:
2232 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002233 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002234 case CMD_isearch:
2235 case CMD_dsearch:
2236 case CMD_ilist:
2237 case CMD_dlist:
2238 case CMD_ijump:
2239 case CMD_psearch:
2240 case CMD_djump:
2241 case CMD_isplit:
2242 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002243 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002244 case CMD_autocmd:
2245 return set_context_in_autocmd(xp, arg, FALSE);
2246 case CMD_doautocmd:
2247 case CMD_doautoall:
2248 return set_context_in_autocmd(xp, arg, TRUE);
2249 case CMD_set:
2250 set_context_in_set_cmd(xp, arg, 0);
2251 break;
2252 case CMD_setglobal:
2253 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2254 break;
2255 case CMD_setlocal:
2256 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2257 break;
2258 case CMD_tag:
2259 case CMD_stag:
2260 case CMD_ptag:
2261 case CMD_ltag:
2262 case CMD_tselect:
2263 case CMD_stselect:
2264 case CMD_ptselect:
2265 case CMD_tjump:
2266 case CMD_stjump:
2267 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002268 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002269 xp->xp_context = EXPAND_TAGS_LISTFILES;
2270 else
2271 xp->xp_context = EXPAND_TAGS;
2272 xp->xp_pattern = arg;
2273 break;
2274 case CMD_augroup:
2275 xp->xp_context = EXPAND_AUGROUP;
2276 xp->xp_pattern = arg;
2277 break;
2278#ifdef FEAT_SYN_HL
2279 case CMD_syntax:
2280 set_context_in_syntax_cmd(xp, arg);
2281 break;
2282#endif
2283#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002284 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002285 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002286 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002287 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002288 case CMD_if:
2289 case CMD_elseif:
2290 case CMD_while:
2291 case CMD_for:
2292 case CMD_echo:
2293 case CMD_echon:
2294 case CMD_execute:
2295 case CMD_echomsg:
2296 case CMD_echoerr:
2297 case CMD_call:
2298 case CMD_return:
2299 case CMD_cexpr:
2300 case CMD_caddexpr:
2301 case CMD_cgetexpr:
2302 case CMD_lexpr:
2303 case CMD_laddexpr:
2304 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002305 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002306 break;
2307
2308 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002309 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002310 case CMD_function:
2311 case CMD_delfunction:
2312 xp->xp_context = EXPAND_USER_FUNC;
2313 xp->xp_pattern = arg;
2314 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002315 case CMD_disassemble:
2316 set_context_in_disassemble_cmd(xp, arg);
2317 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002318
2319 case CMD_echohl:
2320 set_context_in_echohl_cmd(xp, arg);
2321 break;
2322#endif
2323 case CMD_highlight:
2324 set_context_in_highlight_cmd(xp, arg);
2325 break;
2326#ifdef FEAT_CSCOPE
2327 case CMD_cscope:
2328 case CMD_lcscope:
2329 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002330 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002331 break;
2332#endif
2333#ifdef FEAT_SIGNS
2334 case CMD_sign:
2335 set_context_in_sign_cmd(xp, arg);
2336 break;
2337#endif
2338 case CMD_bdelete:
2339 case CMD_bwipeout:
2340 case CMD_bunload:
2341 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2342 arg = xp->xp_pattern + 1;
2343 // FALLTHROUGH
2344 case CMD_buffer:
2345 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002346 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002347 case CMD_checktime:
2348 xp->xp_context = EXPAND_BUFFERS;
2349 xp->xp_pattern = arg;
2350 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002351#ifdef FEAT_DIFF
2352 case CMD_diffget:
2353 case CMD_diffput:
2354 // If current buffer is in diff mode, complete buffer names
2355 // which are in diff mode, and different than current buffer.
2356 xp->xp_context = EXPAND_DIFF_BUFFERS;
2357 xp->xp_pattern = arg;
2358 break;
2359#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002360 case CMD_USER:
2361 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002362 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2363 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002364
2365 case CMD_map: case CMD_noremap:
2366 case CMD_nmap: case CMD_nnoremap:
2367 case CMD_vmap: case CMD_vnoremap:
2368 case CMD_omap: case CMD_onoremap:
2369 case CMD_imap: case CMD_inoremap:
2370 case CMD_cmap: case CMD_cnoremap:
2371 case CMD_lmap: case CMD_lnoremap:
2372 case CMD_smap: case CMD_snoremap:
2373 case CMD_tmap: case CMD_tnoremap:
2374 case CMD_xmap: case CMD_xnoremap:
2375 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002376 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002377 case CMD_unmap:
2378 case CMD_nunmap:
2379 case CMD_vunmap:
2380 case CMD_ounmap:
2381 case CMD_iunmap:
2382 case CMD_cunmap:
2383 case CMD_lunmap:
2384 case CMD_sunmap:
2385 case CMD_tunmap:
2386 case CMD_xunmap:
2387 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002388 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002389 case CMD_mapclear:
2390 case CMD_nmapclear:
2391 case CMD_vmapclear:
2392 case CMD_omapclear:
2393 case CMD_imapclear:
2394 case CMD_cmapclear:
2395 case CMD_lmapclear:
2396 case CMD_smapclear:
2397 case CMD_tmapclear:
2398 case CMD_xmapclear:
2399 xp->xp_context = EXPAND_MAPCLEAR;
2400 xp->xp_pattern = arg;
2401 break;
2402
2403 case CMD_abbreviate: case CMD_noreabbrev:
2404 case CMD_cabbrev: case CMD_cnoreabbrev:
2405 case CMD_iabbrev: case CMD_inoreabbrev:
2406 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002407 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002408 case CMD_unabbreviate:
2409 case CMD_cunabbrev:
2410 case CMD_iunabbrev:
2411 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002412 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002413#ifdef FEAT_MENU
2414 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2415 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2416 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2417 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2418 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2419 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2420 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2421 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2422 case CMD_tmenu: case CMD_tunmenu:
2423 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2424 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2425#endif
2426
2427 case CMD_colorscheme:
2428 xp->xp_context = EXPAND_COLORS;
2429 xp->xp_pattern = arg;
2430 break;
2431
2432 case CMD_compiler:
2433 xp->xp_context = EXPAND_COMPILER;
2434 xp->xp_pattern = arg;
2435 break;
2436
2437 case CMD_ownsyntax:
2438 xp->xp_context = EXPAND_OWNSYNTAX;
2439 xp->xp_pattern = arg;
2440 break;
2441
2442 case CMD_setfiletype:
2443 xp->xp_context = EXPAND_FILETYPE;
2444 xp->xp_pattern = arg;
2445 break;
2446
2447 case CMD_packadd:
2448 xp->xp_context = EXPAND_PACKADD;
2449 xp->xp_pattern = arg;
2450 break;
2451
zeertzjqb0d45ec2023-01-25 15:04:22 +00002452 case CMD_runtime:
2453 set_context_in_runtime_cmd(xp, arg);
2454 break;
2455
Bram Moolenaard0190392019-08-23 21:17:35 +02002456#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2457 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002458 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002459#endif
2460#if defined(FEAT_PROFILE)
2461 case CMD_profile:
2462 set_context_in_profile_cmd(xp, arg);
2463 break;
2464#endif
2465 case CMD_behave:
2466 xp->xp_context = EXPAND_BEHAVE;
2467 xp->xp_pattern = arg;
2468 break;
2469
2470 case CMD_messages:
2471 xp->xp_context = EXPAND_MESSAGES;
2472 xp->xp_pattern = arg;
2473 break;
2474
2475 case CMD_history:
2476 xp->xp_context = EXPAND_HISTORY;
2477 xp->xp_pattern = arg;
2478 break;
2479#if defined(FEAT_PROFILE)
2480 case CMD_syntime:
2481 xp->xp_context = EXPAND_SYNTIME;
2482 xp->xp_pattern = arg;
2483 break;
2484#endif
2485
2486 case CMD_argdelete:
2487 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2488 arg = xp->xp_pattern + 1;
2489 xp->xp_context = EXPAND_ARGLIST;
2490 xp->xp_pattern = arg;
2491 break;
2492
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002493#ifdef FEAT_EVAL
2494 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002495 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002496 case CMD_breakdel:
2497 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002498
2499 case CMD_scriptnames:
2500 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002501#endif
2502
Bram Moolenaard0190392019-08-23 21:17:35 +02002503 default:
2504 break;
2505 }
2506 return NULL;
2507}
2508
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002509/*
2510 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2511 * we don't need/want deleted. Maybe this could be done better if we didn't
2512 * repeat all this stuff. The only problem is that they may not stay
2513 * perfectly compatible with each other, but then the command line syntax
2514 * probably won't change that much -- webb.
2515 */
2516 static char_u *
2517set_one_cmd_context(
2518 expand_T *xp,
2519 char_u *buff) // buffer for command string
2520{
2521 char_u *p;
2522 char_u *cmd, *arg;
2523 int len = 0;
2524 exarg_T ea;
2525 int compl = EXPAND_NOTHING;
2526 int forceit = FALSE;
2527 int usefilter = FALSE; // filter instead of file name
2528
2529 ExpandInit(xp);
2530 xp->xp_pattern = buff;
2531 xp->xp_line = buff;
2532 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2533 ea.argt = 0;
2534
2535 // 1. skip comment lines and leading space, colons or bars
2536 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2537 ;
2538 xp->xp_pattern = cmd;
2539
2540 if (*cmd == NUL)
2541 return NULL;
2542 if (*cmd == '"') // ignore comment lines
2543 {
2544 xp->xp_context = EXPAND_NOTHING;
2545 return NULL;
2546 }
2547
2548 // 3. Skip over the range to find the command.
2549 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2550 xp->xp_pattern = cmd;
2551 if (*cmd == NUL)
2552 return NULL;
2553 if (*cmd == '"')
2554 {
2555 xp->xp_context = EXPAND_NOTHING;
2556 return NULL;
2557 }
2558
2559 if (*cmd == '|' || *cmd == '\n')
2560 return cmd + 1; // There's another command
2561
2562 // Get the command index.
2563 p = set_cmd_index(cmd, &ea, xp, &compl);
2564 if (p == NULL)
2565 return NULL;
2566
2567 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2568
2569 if (*p == '!') // forced commands
2570 {
2571 forceit = TRUE;
2572 ++p;
2573 }
2574
2575 // 6. parse arguments
2576 if (!IS_USER_CMDIDX(ea.cmdidx))
2577 ea.argt = excmd_get_argt(ea.cmdidx);
2578
2579 arg = skipwhite(p);
2580
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002581 // Does command allow "++argopt" argument?
2582 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002583 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002584 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2585 {
2586 p = arg + 2;
2587 while (*p && !vim_isspace(*p))
2588 MB_PTR_ADV(p);
2589
2590 // Still touching the command after "++"?
2591 if (*p == NUL)
2592 {
2593 if (ea.argt & EX_ARGOPT)
2594 return set_context_in_argopt(xp, arg + 2);
2595#ifdef FEAT_TERMINAL
2596 if (ea.cmdidx == CMD_terminal)
2597 return set_context_in_terminalopt(xp, arg + 2);
2598#endif
2599 }
2600
2601 arg = skipwhite(p);
2602 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002603 }
2604
2605 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2606 {
2607 if (*arg == '>') // append
2608 {
2609 if (*++arg == '>')
2610 ++arg;
2611 arg = skipwhite(arg);
2612 }
2613 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2614 {
2615 ++arg;
2616 usefilter = TRUE;
2617 }
2618 }
2619
2620 if (ea.cmdidx == CMD_read)
2621 {
2622 usefilter = forceit; // :r! filter if forced
2623 if (*arg == '!') // :r !filter
2624 {
2625 ++arg;
2626 usefilter = TRUE;
2627 }
2628 }
2629
2630 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2631 {
2632 while (*arg == *cmd) // allow any number of '>' or '<'
2633 ++arg;
2634 arg = skipwhite(arg);
2635 }
2636
2637 // Does command allow "+command"?
2638 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2639 {
2640 // Check if we're in the +command
2641 p = arg + 1;
2642 arg = skip_cmd_arg(arg, FALSE);
2643
2644 // Still touching the command after '+'?
2645 if (*arg == NUL)
2646 return p;
2647
2648 // Skip space(s) after +command to get to the real argument
2649 arg = skipwhite(arg);
2650 }
2651
2652
2653 // Check for '|' to separate commands and '"' to start comments.
2654 // Don't do this for ":read !cmd" and ":write !cmd".
2655 if ((ea.argt & EX_TRLBAR) && !usefilter)
2656 {
2657 p = arg;
2658 // ":redir @" is not the start of a comment
2659 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2660 p += 2;
2661 while (*p)
2662 {
2663 if (*p == Ctrl_V)
2664 {
2665 if (p[1] != NUL)
2666 ++p;
2667 }
2668 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2669 || *p == '|' || *p == '\n')
2670 {
2671 if (*(p - 1) != '\\')
2672 {
2673 if (*p == '|' || *p == '\n')
2674 return p + 1;
2675 return NULL; // It's a comment
2676 }
2677 }
2678 MB_PTR_ADV(p);
2679 }
2680 }
2681
2682 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2683 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2684 // no arguments allowed but there is something
2685 return NULL;
2686
2687 // Find start of last argument (argument just before cursor):
2688 p = buff;
2689 xp->xp_pattern = p;
2690 len = (int)STRLEN(buff);
2691 while (*p && p < buff + len)
2692 {
2693 if (*p == ' ' || *p == TAB)
2694 {
2695 // argument starts after a space
2696 xp->xp_pattern = ++p;
2697 }
2698 else
2699 {
2700 if (*p == '\\' && *(p + 1) != NUL)
2701 ++p; // skip over escaped character
2702 MB_PTR_ADV(p);
2703 }
2704 }
2705
2706 if (ea.argt & EX_XFILE)
2707 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2708
2709 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002710 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002711 forceit);
2712}
2713
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002714/*
2715 * Set the completion context in 'xp' for command 'str'
2716 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717 void
2718set_cmd_context(
2719 expand_T *xp,
2720 char_u *str, // start of command line
2721 int len, // length of command line (excl. NUL)
2722 int col, // position of cursor
2723 int use_ccline UNUSED) // use ccline for info
2724{
2725#ifdef FEAT_EVAL
2726 cmdline_info_T *ccline = get_cmdline_info();
zeertzjq7a5115c2025-03-19 20:29:58 +01002727 int context;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728#endif
2729 int old_char = NUL;
2730 char_u *nextcomm;
2731
2732 // Avoid a UMR warning from Purify, only save the character if it has been
2733 // written before.
2734 if (col < len)
2735 old_char = str[col];
2736 str[col] = NUL;
2737 nextcomm = str;
2738
2739#ifdef FEAT_EVAL
2740 if (use_ccline && ccline->cmdfirstc == '=')
2741 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002742 // pass CMD_SIZE because there is no real command
2743 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002744 }
2745 else if (use_ccline && ccline->input_fn)
2746 {
2747 xp->xp_context = ccline->xp_context;
2748 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002749 xp->xp_arg = ccline->xp_arg;
zeertzjq7a5115c2025-03-19 20:29:58 +01002750 if (xp->xp_context == EXPAND_SHELLCMDLINE)
2751 {
2752 context = xp->xp_context;
2753 set_context_for_wildcard_arg(NULL, xp->xp_pattern, FALSE, xp,
2754 &context);
2755 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002756 }
2757 else
2758#endif
2759 while (nextcomm != NULL)
2760 nextcomm = set_one_cmd_context(xp, nextcomm);
2761
2762 // Store the string here so that call_user_expand_func() can get to them
2763 // easily.
2764 xp->xp_line = str;
2765 xp->xp_col = col;
2766
2767 str[col] = old_char;
2768}
2769
2770/*
2771 * Expand the command line "str" from context "xp".
2772 * "xp" must have been set by set_cmd_context().
2773 * xp->xp_pattern points into "str", to where the text that is to be expanded
2774 * starts.
2775 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2776 * cursor.
2777 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2778 * key that triggered expansion literally.
2779 * Returns EXPAND_OK otherwise.
2780 */
2781 int
2782expand_cmdline(
2783 expand_T *xp,
2784 char_u *str, // start of command line
2785 int col, // position of cursor
2786 int *matchcount, // return: nr of matches
2787 char_u ***matches) // return: array of pointers to matches
2788{
2789 char_u *file_str = NULL;
2790 int options = WILD_ADD_SLASH|WILD_SILENT;
2791
2792 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2793 {
2794 beep_flush();
2795 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2796 }
2797 if (xp->xp_context == EXPAND_NOTHING)
2798 {
2799 // Caller can use the character as a normal char instead
2800 return EXPAND_NOTHING;
2801 }
2802
2803 // add star to file name, or convert to regexp if not exp. files.
2804 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002805 if (cmdline_fuzzy_completion_supported(xp))
2806 // If fuzzy matching, don't modify the search string
2807 file_str = vim_strsave(xp->xp_pattern);
2808 else
2809 {
2810 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2811 if (file_str == NULL)
2812 return EXPAND_UNSUCCESSFUL;
2813 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002814
2815 if (p_wic)
2816 options += WILD_ICASE;
2817
2818 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002819 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002820 {
2821 *matchcount = 0;
2822 *matches = NULL;
2823 }
2824 vim_free(file_str);
2825
2826 return EXPAND_OK;
2827}
2828
Bram Moolenaar66b51422019-08-18 21:44:12 +02002829/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002830 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002831 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002832 */
2833 static int
2834expand_files_and_dirs(
2835 expand_T *xp,
2836 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002837 char_u ***matches,
2838 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002839 int flags,
2840 int options)
2841{
2842 int free_pat = FALSE;
2843 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002844 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002845
2846 // for ":set path=" and ":set tags=" halve backslashes for escaped
2847 // space
2848 if (xp->xp_backslash != XP_BS_NONE)
2849 {
2850 free_pat = TRUE;
2851 pat = vim_strsave(pat);
2852 for (i = 0; pat[i]; ++i)
2853 if (pat[i] == '\\')
2854 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002855 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002856 && pat[i + 1] == '\\'
2857 && pat[i + 2] == '\\'
2858 && pat[i + 3] == ' ')
2859 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002860 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002861 && pat[i + 1] == ' ')
2862 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002863 else if ((xp->xp_backslash & XP_BS_COMMA)
2864 && pat[i + 1] == '\\'
2865 && pat[i + 2] == ',')
2866 STRMOVE(pat + i, pat + i + 2);
2867#ifdef BACKSLASH_IN_FILENAME
2868 else if ((xp->xp_backslash & XP_BS_COMMA)
2869 && pat[i + 1] == ',')
2870 STRMOVE(pat + i, pat + i + 1);
2871#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872 }
2873 }
2874
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002875 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002876 {
2877#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002878 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002879#endif
2880 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002881 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002882 {
2883 if (xp->xp_context == EXPAND_FILES)
2884 flags |= EW_FILE;
2885 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2886 flags |= (EW_FILE | EW_PATH);
2887 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2888 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2889 else
2890 flags = (flags | EW_DIR) & ~EW_FILE;
2891 if (options & WILD_ICASE)
2892 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002893
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002894 // Expand wildcards, supporting %:h and the like.
2895 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2896 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002897 if (free_pat)
2898 vim_free(pat);
2899#ifdef BACKSLASH_IN_FILENAME
2900 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2901 {
2902 int j;
2903
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002904 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002905 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002906 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002907
2908 while (*ptr != NUL)
2909 {
2910 if (p_csl[0] == 's' && *ptr == '\\')
2911 *ptr = '/';
2912 else if (p_csl[0] == 'b' && *ptr == '/')
2913 *ptr = '\\';
2914 ptr += (*mb_ptr2len)(ptr);
2915 }
2916 }
2917 }
2918#endif
2919 return ret;
2920}
2921
2922/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002923 * Function given to ExpandGeneric() to obtain the possible arguments of the
2924 * ":behave {mswin,xterm}" command.
2925 */
2926 static char_u *
2927get_behave_arg(expand_T *xp UNUSED, int idx)
2928{
2929 if (idx == 0)
2930 return (char_u *)"mswin";
2931 if (idx == 1)
2932 return (char_u *)"xterm";
2933 return NULL;
2934}
2935
K.Takata161b6ac2022-11-14 15:31:07 +00002936#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002937/*
2938 * Function given to ExpandGeneric() to obtain the possible arguments of the
2939 * ":breakadd {expr, file, func, here}" command.
2940 * ":breakdel {func, file, here}" command.
2941 */
2942 static char_u *
2943get_breakadd_arg(expand_T *xp UNUSED, int idx)
2944{
2945 char *opts[] = {"expr", "file", "func", "here"};
2946
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002947 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002948 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002949 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002950 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2951 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002952 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2953 {
2954 // breakdel {func, file, here}
2955 if (idx <= 2)
2956 return (char_u *)opts[idx + 1];
2957 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002958 else
2959 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002960 // profdel {func, file}
2961 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002962 return (char_u *)opts[idx + 1];
2963 }
2964 }
2965 return NULL;
2966}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002967
2968/*
2969 * Function given to ExpandGeneric() to obtain the possible arguments for the
2970 * ":scriptnames" command.
2971 */
2972 static char_u *
2973get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2974{
2975 scriptitem_T *si;
2976
2977 if (!SCRIPT_ID_VALID(idx + 1))
2978 return NULL;
2979
2980 si = SCRIPT_ITEM(idx + 1);
2981 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2982 return NameBuff;
2983}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002984#endif
2985
Bram Moolenaard0190392019-08-23 21:17:35 +02002986/*
2987 * Function given to ExpandGeneric() to obtain the possible arguments of the
2988 * ":messages {clear}" command.
2989 */
2990 static char_u *
2991get_messages_arg(expand_T *xp UNUSED, int idx)
2992{
2993 if (idx == 0)
2994 return (char_u *)"clear";
2995 return NULL;
2996}
2997
2998 static char_u *
2999get_mapclear_arg(expand_T *xp UNUSED, int idx)
3000{
3001 if (idx == 0)
3002 return (char_u *)"<buffer>";
3003 return NULL;
3004}
3005
3006/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003007 * Do the expansion based on xp->xp_context and 'rmp'.
3008 */
3009 static int
3010ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003011 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00003012 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003013 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003014 char_u ***matches,
3015 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003016{
3017 static struct expgen
3018 {
3019 int context;
3020 char_u *((*func)(expand_T *, int));
3021 int ic;
3022 int escaped;
3023 } tab[] =
3024 {
3025 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3026 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3027 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3028 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3029 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3030 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3031 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3032 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3033 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3034 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003035#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003036 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3037 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3038 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3039 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3040 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003041#endif
3042#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003043 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3044 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003045#endif
3046#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003047 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003048#endif
3049#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003050 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003051#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003052 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3053 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3054 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003055#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003056 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003057#endif
3058#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003059 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003060#endif
3061#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003062 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003063#endif
3064#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003065 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3066 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003067#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003068 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3069 {EXPAND_USER, get_users, TRUE, FALSE},
3070 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003071#ifdef FEAT_EVAL
3072 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003073 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003074#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003075 };
3076 int i;
3077 int ret = FAIL;
3078
3079 // Find a context in the table and call the ExpandGeneric() with the
3080 // right function to do the expansion.
3081 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3082 {
3083 if (xp->xp_context == tab[i].context)
3084 {
3085 if (tab[i].ic)
3086 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003087 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3088 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003089 break;
3090 }
3091 }
3092
3093 return ret;
3094}
3095
3096/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003097 * Map wild expand options to flags for expand_wildcards()
3098 */
3099 static int
3100map_wildopts_to_ewflags(int options)
3101{
3102 int flags;
3103
3104 flags = EW_DIR; // include directories
3105 if (options & WILD_LIST_NOTFOUND)
3106 flags |= EW_NOTFOUND;
3107 if (options & WILD_ADD_SLASH)
3108 flags |= EW_ADDSLASH;
3109 if (options & WILD_KEEP_ALL)
3110 flags |= EW_KEEPALL;
3111 if (options & WILD_SILENT)
3112 flags |= EW_SILENT;
3113 if (options & WILD_NOERROR)
3114 flags |= EW_NOERROR;
3115 if (options & WILD_ALLLINKS)
3116 flags |= EW_ALLLINKS;
3117
3118 return flags;
3119}
3120
3121/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122 * Do the expansion based on xp->xp_context and "pat".
3123 */
3124 static int
3125ExpandFromContext(
3126 expand_T *xp,
3127 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003128 char_u ***matches,
3129 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 int options) // WILD_ flags
3131{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 int ret;
3134 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003135 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003136 int fuzzy = cmdline_fuzzy_complete(pat)
3137 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003139 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140
3141 if (xp->xp_context == EXPAND_FILES
3142 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003143 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003144 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003145 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003146 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3147 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003149 *matches = (char_u **)"";
3150 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 if (xp->xp_context == EXPAND_HELP)
3152 {
3153 // With an empty argument we would get all the help tags, which is
3154 // very slow. Get matches for "help" instead.
3155 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003156 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 {
3158#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003159 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160#endif
3161 return OK;
3162 }
3163 return FAIL;
3164 }
3165
Bram Moolenaar66b51422019-08-18 21:44:12 +02003166 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003167 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003168 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003169 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003171 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003172#ifdef FEAT_DIFF
3173 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003174 return ExpandBufnames(pat, numMatches, matches,
3175 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003176#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003177 if (xp->xp_context == EXPAND_TAGS
3178 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003179 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3180 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003181 if (xp->xp_context == EXPAND_COLORS)
3182 {
3183 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003184 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003185 directories);
3186 }
3187 if (xp->xp_context == EXPAND_COMPILER)
3188 {
3189 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003190 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191 }
3192 if (xp->xp_context == EXPAND_OWNSYNTAX)
3193 {
3194 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003195 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003196 }
3197 if (xp->xp_context == EXPAND_FILETYPE)
3198 {
3199 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003200 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003201 }
Doug Kearns81642d92024-01-04 22:37:44 +01003202#ifdef FEAT_KEYMAP
3203 if (xp->xp_context == EXPAND_KEYMAP)
3204 {
3205 char *directories[] = {"keymap", NULL};
3206 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3207 }
3208#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003209#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003210 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003211 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003212#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003213 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003214 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003215 if (xp->xp_context == EXPAND_RUNTIME)
3216 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003217
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003218 // When expanding a function name starting with s:, match the <SNR>nr_
3219 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003220 if ((xp->xp_context == EXPAND_USER_FUNC
3221 || xp->xp_context == EXPAND_DISASSEMBLE)
3222 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003223 {
3224 int len = (int)STRLEN(pat) + 20;
3225
3226 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003227 if (tofree == NULL)
3228 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003229 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003230 pat = tofree;
3231 }
3232
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003233 if (!fuzzy)
3234 {
3235 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3236 if (regmatch.regprog == NULL)
3237 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003239 // set ignore-case according to p_ic, p_scs and pat
3240 regmatch.rm_ic = ignorecase(pat);
3241 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242
3243 if (xp->xp_context == EXPAND_SETTINGS
3244 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003245 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003246 else if (xp->xp_context == EXPAND_STRING_SETTING)
3247 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3248 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3249 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003251 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003252 else if (xp->xp_context == EXPAND_ARGOPT)
3253 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003254 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3255 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003256#if defined(FEAT_TERMINAL)
3257 else if (xp->xp_context == EXPAND_TERMINALOPT)
3258 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3259#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003260#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003262 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003263#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003265 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003267 if (!fuzzy)
3268 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003269 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003270
3271 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272}
3273
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003274 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003275ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003276 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003277 expand_T *xp,
3278 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003279 char_u ***matches,
3280 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003281 char_u *((*func)(expand_T *, int)),
3282 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003283 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003284{
Yee Cheng China7b81202025-02-23 09:32:47 +01003285 return ExpandGenericExt(
3286 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3287}
3288
3289/*
3290 * Expand a list of names.
3291 *
3292 * Generic function for command line completion. It calls a function to
3293 * obtain strings, one by one. The strings are matched against a regexp
3294 * program. Matching strings are copied into an array, which is returned.
3295 *
3296 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3297 * is used.
3298 *
3299 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3300 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3301 * sorting.
3302 *
3303 * Returns OK when no problems encountered, FAIL for error (out of memory).
3304 */
3305 int
3306ExpandGenericExt(
3307 char_u *pat,
3308 expand_T *xp,
3309 regmatch_T *regmatch,
3310 char_u ***matches,
3311 int *numMatches,
3312 char_u *((*func)(expand_T *, int)),
3313 // returns a string from the list
3314 int escaped,
3315 int sortStartIdx)
3316{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003318 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003319 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003320 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003321 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003322 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003323 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003324 int sort_matches = FALSE;
3325 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003326 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003328 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003329 *matches = NULL;
3330 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003331
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003332 if (!fuzzy)
3333 ga_init2(&ga, sizeof(char *), 30);
3334 else
3335 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3336
3337 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003338 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003339 str = (*func)(xp, i);
3340 if (str == NULL) // end of list
3341 break;
3342 if (*str == NUL) // skip empty strings
3343 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003345 if (xp->xp_pattern[0] != NUL)
3346 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003347 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003348 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003349 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003351 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003352 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003353 }
3354 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003355 else
3356 match = TRUE;
3357
3358 if (!match)
3359 continue;
3360
3361 if (escaped)
3362 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3363 else
3364 str = vim_strsave(str);
3365 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003366 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003367 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003368 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003369 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003370 return FAIL;
3371 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003372 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003373 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003375
3376 if (ga_grow(&ga, 1) == FAIL)
3377 {
3378 vim_free(str);
3379 break;
3380 }
3381
3382 if (fuzzy)
3383 {
3384 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3385 fuzmatch->idx = ga.ga_len;
3386 fuzmatch->str = str;
3387 fuzmatch->score = score;
3388 }
3389 else
3390 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3391
K.Takata161b6ac2022-11-14 15:31:07 +00003392#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003393 if (func == get_menu_names)
3394 {
3395 // test for separator added by get_menu_names()
3396 str += STRLEN(str) - 1;
3397 if (*str == '\001')
3398 *str = '.';
3399 }
K.Takata161b6ac2022-11-14 15:31:07 +00003400#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003401
Yee Cheng China7b81202025-02-23 09:32:47 +01003402 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3403 {
3404 // Found first item to start sorting from. This is usually 0.
3405 sortStartMatchIdx = ga.ga_len;
3406 }
3407
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003408 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003409 }
3410
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003411 if (ga.ga_len == 0)
3412 return OK;
3413
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003414 // sort the matches when using regular expression matching and sorting
3415 // applies to the completion context. Menus and scriptnames should be kept
3416 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003417 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003418 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003419 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003420 && xp->xp_context != EXPAND_SCRIPTNAMES
3421 && xp->xp_context != EXPAND_ARGOPT
3422 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003423 sort_matches = TRUE;
3424
3425 // <SNR> functions should be sorted to the end.
3426 if (xp->xp_context == EXPAND_EXPRESSION
3427 || xp->xp_context == EXPAND_FUNCTIONS
3428 || xp->xp_context == EXPAND_USER_FUNC
3429 || xp->xp_context == EXPAND_DISASSEMBLE)
3430 funcsort = TRUE;
3431
3432 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003433 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003434 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003435 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003436 // <SNR> functions should be sorted to the end.
3437 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3438 sort_func_compare);
3439 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003440 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003441 }
3442
3443 if (!fuzzy)
3444 {
3445 *matches = ga.ga_data;
3446 *numMatches = ga.ga_len;
3447 }
3448 else
3449 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003450 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3451 funcsort) == FAIL)
3452 return FAIL;
3453 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003454 }
3455
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003456#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003457 // Reset the variables used for special highlight names expansion, so that
3458 // they don't show up when getting normal highlight names by ID.
3459 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003460#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003461
Bram Moolenaar66b51422019-08-18 21:44:12 +02003462 return OK;
3463}
3464
3465/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003466 * Expand shell command matches in one directory of $PATH.
3467 */
3468 static void
3469expand_shellcmd_onedir(
3470 char_u *buf,
3471 char_u *s,
3472 size_t l,
3473 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003474 char_u ***matches,
3475 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003476 int flags,
3477 hashtab_T *ht,
3478 garray_T *gap)
3479{
3480 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003481 hash_T hash;
3482 hashitem_T *hi;
3483
3484 vim_strncpy(buf, s, l);
3485 add_pathsep(buf);
3486 l = STRLEN(buf);
3487 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3488
3489 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003490 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003491 if (ret != OK)
3492 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003493
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003494 if (ga_grow(gap, *numMatches) == FAIL)
3495 {
3496 FreeWild(*numMatches, *matches);
3497 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003498 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003499
3500 for (int i = 0; i < *numMatches; ++i)
3501 {
3502 char_u *name = (*matches)[i];
3503
3504 if (STRLEN(name) > l)
3505 {
3506 // Check if this name was already found.
3507 hash = hash_hash(name + l);
3508 hi = hash_lookup(ht, name + l, hash);
3509 if (HASHITEM_EMPTY(hi))
3510 {
3511 // Remove the path that was prepended.
3512 STRMOVE(name, name + l);
3513 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3514 hash_add_item(ht, hi, name, hash);
3515 name = NULL;
3516 }
3517 }
3518 vim_free(name);
3519 }
3520 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003521}
3522
3523/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003524 * Complete a shell command.
3525 * Returns FAIL or OK;
3526 */
3527 static int
3528expand_shellcmd(
3529 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003530 char_u ***matches, // return: array with matches
3531 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 int flagsarg) // EW_ flags
3533{
3534 char_u *pat;
3535 int i;
3536 char_u *path = NULL;
3537 int mustfree = FALSE;
3538 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003539 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 size_t l;
3541 char_u *s, *e;
3542 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003543 int did_curdir = FALSE;
3544 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003545
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003546 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003547 if (buf == NULL)
3548 return FAIL;
3549
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003550 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003551 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003552 if (pat == NULL)
3553 {
3554 vim_free(buf);
3555 return FAIL;
3556 }
3557
Bram Moolenaar66b51422019-08-18 21:44:12 +02003558 for (i = 0; pat[i]; ++i)
3559 if (pat[i] == '\\' && pat[i + 1] == ' ')
3560 STRMOVE(pat + i, pat + i + 1);
3561
3562 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3563
3564 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3565 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3566 path = (char_u *)".";
3567 else
3568 {
3569 // For an absolute name we don't use $PATH.
3570 if (!mch_isFullName(pat))
3571 path = vim_getenv((char_u *)"PATH", &mustfree);
3572 if (path == NULL)
3573 path = (char_u *)"";
3574 }
3575
3576 // Go over all directories in $PATH. Expand matches in that directory and
3577 // collect them in "ga". When "." is not in $PATH also expand for the
3578 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003579 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003580 hash_init(&found_ht);
3581 for (s = path; ; s = e)
3582 {
K.Takata161b6ac2022-11-14 15:31:07 +00003583#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003584 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003585#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003587#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003588 if (e == NULL)
3589 e = s + STRLEN(s);
3590
3591 if (*s == NUL)
3592 {
3593 if (did_curdir)
3594 break;
3595 // Find directories in the current directory, path is empty.
3596 did_curdir = TRUE;
3597 flags |= EW_DIR;
3598 }
3599 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3600 {
3601 did_curdir = TRUE;
3602 flags |= EW_DIR;
3603 }
3604 else
3605 // Do not match directories inside a $PATH item.
3606 flags &= ~EW_DIR;
3607
3608 l = e - s;
3609 if (l > MAXPATHL - 5)
3610 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003611
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003612 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003613 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003614
Bram Moolenaar66b51422019-08-18 21:44:12 +02003615 if (*e != NUL)
3616 ++e;
3617 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003618 *matches = ga.ga_data;
3619 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003620
3621 vim_free(buf);
3622 vim_free(pat);
3623 if (mustfree)
3624 vim_free(path);
3625 hash_clear(&found_ht);
3626 return OK;
3627}
3628
K.Takata161b6ac2022-11-14 15:31:07 +00003629#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003630/*
3631 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003632 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003633 */
3634 static void *
3635call_user_expand_func(
3636 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003637 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638{
3639 cmdline_info_T *ccline = get_cmdline_info();
3640 int keep = 0;
3641 typval_T args[4];
3642 sctx_T save_current_sctx = current_sctx;
3643 char_u *pat = NULL;
3644 void *ret;
3645
3646 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3647 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003648
3649 if (ccline->cmdbuff != NULL)
3650 {
3651 keep = ccline->cmdbuff[ccline->cmdlen];
3652 ccline->cmdbuff[ccline->cmdlen] = 0;
3653 }
3654
3655 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3656
3657 args[0].v_type = VAR_STRING;
3658 args[0].vval.v_string = pat;
3659 args[1].v_type = VAR_STRING;
3660 args[1].vval.v_string = xp->xp_line;
3661 args[2].v_type = VAR_NUMBER;
3662 args[2].vval.v_number = xp->xp_col;
3663 args[3].v_type = VAR_UNKNOWN;
3664
3665 current_sctx = xp->xp_script_ctx;
3666
3667 ret = user_expand_func(xp->xp_arg, 3, args);
3668
3669 current_sctx = save_current_sctx;
3670 if (ccline->cmdbuff != NULL)
3671 ccline->cmdbuff[ccline->cmdlen] = keep;
3672
3673 vim_free(pat);
3674 return ret;
3675}
3676
3677/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003678 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3679 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003680 */
3681 static int
3682ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003683 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003684 expand_T *xp,
3685 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003686 char_u ***matches,
3687 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688{
3689 char_u *retstr;
3690 char_u *s;
3691 char_u *e;
3692 int keep;
3693 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003694 int fuzzy;
3695 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003696 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003697
3698 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003699 *matches = NULL;
3700 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003701
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003702 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003703 if (retstr == NULL)
3704 return FAIL;
3705
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003706 if (!fuzzy)
3707 ga_init2(&ga, sizeof(char *), 3);
3708 else
3709 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3710
Bram Moolenaar66b51422019-08-18 21:44:12 +02003711 for (s = retstr; *s != NUL; s = e)
3712 {
3713 e = vim_strchr(s, '\n');
3714 if (e == NULL)
3715 e = s + STRLEN(s);
3716 keep = *e;
3717 *e = NUL;
3718
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003719 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003720 {
3721 if (!fuzzy)
3722 match = vim_regexec(regmatch, s, (colnr_T)0);
3723 else
3724 {
3725 score = fuzzy_match_str(s, pat);
3726 match = (score != 0);
3727 }
3728 }
3729 else
3730 match = TRUE; // match everything
3731
Bram Moolenaar66b51422019-08-18 21:44:12 +02003732 *e = keep;
3733
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003734 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735 {
3736 if (ga_grow(&ga, 1) == FAIL)
3737 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003738 if (!fuzzy)
3739 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3740 else
3741 {
3742 fuzmatch_str_T *fuzmatch =
3743 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003744 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003745 fuzmatch->str = vim_strnsave(s, e - s);
3746 fuzmatch->score = score;
3747 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003748 ++ga.ga_len;
3749 }
3750
3751 if (*e != NUL)
3752 ++e;
3753 }
3754 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003755
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003756 if (ga.ga_len == 0)
3757 return OK;
3758
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003759 if (!fuzzy)
3760 {
3761 *matches = ga.ga_data;
3762 *numMatches = ga.ga_len;
3763 }
3764 else
3765 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003766 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3767 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003768 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003769 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003770 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003771 return OK;
3772}
3773
3774/*
3775 * Expand names with a list returned by a function defined by the user.
3776 */
3777 static int
3778ExpandUserList(
3779 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003780 char_u ***matches,
3781 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003782{
3783 list_T *retlist;
3784 listitem_T *li;
3785 garray_T ga;
3786
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003787 *matches = NULL;
3788 *numMatches = 0;
3789 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003790 if (retlist == NULL)
3791 return FAIL;
3792
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003793 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003794 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003795 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003796 {
3797 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3798 continue; // Skip non-string items and empty strings
3799
3800 if (ga_grow(&ga, 1) == FAIL)
3801 break;
3802
3803 ((char_u **)ga.ga_data)[ga.ga_len] =
3804 vim_strsave(li->li_tv.vval.v_string);
3805 ++ga.ga_len;
3806 }
3807 list_unref(retlist);
3808
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003809 *matches = ga.ga_data;
3810 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 return OK;
3812}
K.Takata161b6ac2022-11-14 15:31:07 +00003813#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003814
3815/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003816 * Expand "file" for all comma-separated directories in "path".
3817 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003818 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003819 */
3820 void
3821globpath(
3822 char_u *path,
3823 char_u *file,
3824 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003825 int expand_options,
3826 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003827{
3828 expand_T xpc;
3829 char_u *buf;
3830 int i;
3831 int num_p;
3832 char_u **p;
3833
3834 buf = alloc(MAXPATHL);
3835 if (buf == NULL)
3836 return;
3837
3838 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003839 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003840
3841 // Loop over all entries in {path}.
3842 while (*path != NUL)
3843 {
3844 // Copy one item of the path to buf[] and concatenate the file name.
3845 copy_option_part(&path, buf, MAXPATHL, ",");
3846 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3847 {
K.Takata161b6ac2022-11-14 15:31:07 +00003848#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003849 // Using the platform's path separator (\) makes vim incorrectly
3850 // treat it as an escape character, use '/' instead.
3851 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3852 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003853#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003854 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003855#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003857 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003858 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3859 {
3860 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3861
3862 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003863 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003864 for (i = 0; i < num_p; ++i)
3865 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003866 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003867 ++ga->ga_len;
3868 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003869 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003870 }
3871 }
3872 }
3873
3874 vim_free(buf);
3875}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003876
Bram Moolenaareadee482020-09-04 15:37:31 +02003877/*
3878 * Translate some keys pressed when 'wildmenu' is used.
3879 */
3880 int
3881wildmenu_translate_key(
3882 cmdline_info_T *cclp,
3883 int key,
3884 expand_T *xp,
3885 int did_wild_list)
3886{
3887 int c = key;
3888
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003889 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003890 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003891 // When the popup menu is used for cmdline completion:
3892 // Up : go to the previous item in the menu
3893 // Down : go to the next item in the menu
3894 // Left : go to the parent directory
3895 // Right: list the files in the selected directory
3896 switch (c)
3897 {
3898 case K_UP: c = K_LEFT; break;
3899 case K_DOWN: c = K_RIGHT; break;
3900 case K_LEFT: c = K_UP; break;
3901 case K_RIGHT: c = K_DOWN; break;
3902 default: break;
3903 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003904 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003905
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003906 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003907 {
3908 if (c == K_LEFT)
3909 c = Ctrl_P;
3910 else if (c == K_RIGHT)
3911 c = Ctrl_N;
3912 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003913
Bram Moolenaareadee482020-09-04 15:37:31 +02003914 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003915 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003916 && cclp->cmdpos > 1
3917 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3918 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3919 && (c == '\n' || c == '\r' || c == K_KENTER))
3920 c = K_DOWN;
3921
3922 return c;
3923}
3924
3925/*
3926 * Delete characters on the command line, from "from" to the current
3927 * position.
3928 */
3929 static void
3930cmdline_del(cmdline_info_T *cclp, int from)
3931{
3932 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3933 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3934 cclp->cmdlen -= cclp->cmdpos - from;
3935 cclp->cmdpos = from;
3936}
3937
3938/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003939 * Handle a key pressed when the wild menu for the menu names
3940 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003941 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003942 static int
3943wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003944{
Bram Moolenaareadee482020-09-04 15:37:31 +02003945 int i;
3946 int j;
3947
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003948 // Hitting <Down> after "emenu Name.": complete submenu
3949 if (key == K_DOWN && cclp->cmdpos > 0
3950 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003951 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003952 key = p_wc;
3953 KeyTyped = TRUE; // in case the key was mapped
3954 }
3955 else if (key == K_UP)
3956 {
3957 // Hitting <Up>: Remove one submenu name in front of the
3958 // cursor
3959 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003960
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003961 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3962 i = 0;
3963 while (--j > 0)
3964 {
3965 // check for start of menu name
3966 if (cclp->cmdbuff[j] == ' '
3967 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003968 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003969 i = j + 1;
3970 break;
3971 }
3972 // check for start of submenu name
3973 if (cclp->cmdbuff[j] == '.'
3974 && cclp->cmdbuff[j - 1] != '\\')
3975 {
3976 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003977 {
3978 i = j + 1;
3979 break;
3980 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003981 else
3982 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003983 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003984 }
3985 if (i > 0)
3986 cmdline_del(cclp, i);
3987 key = p_wc;
3988 KeyTyped = TRUE; // in case the key was mapped
3989 xp->xp_context = EXPAND_NOTHING;
3990 }
3991
3992 return key;
3993}
3994
3995/*
3996 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3997 * directory names (EXPAND_DIRECTORIES) or shell command names
3998 * (EXPAND_SHELLCMD) is displayed.
3999 */
4000 static int
4001wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
4002{
4003 int i;
4004 int j;
4005 char_u upseg[5];
4006
4007 upseg[0] = PATHSEP;
4008 upseg[1] = '.';
4009 upseg[2] = '.';
4010 upseg[3] = PATHSEP;
4011 upseg[4] = NUL;
4012
4013 if (key == K_DOWN
4014 && cclp->cmdpos > 0
4015 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
4016 && (cclp->cmdpos < 3
4017 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
4018 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
4019 {
4020 // go down a directory
4021 key = p_wc;
4022 KeyTyped = TRUE; // in case the key was mapped
4023 }
4024 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4025 {
4026 // If in a direct ancestor, strip off one ../ to go down
4027 int found = FALSE;
4028
4029 j = cclp->cmdpos;
4030 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4031 while (--j > i)
4032 {
4033 if (has_mbyte)
4034 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4035 if (vim_ispathsep(cclp->cmdbuff[j]))
4036 {
4037 found = TRUE;
4038 break;
4039 }
4040 }
4041 if (found
4042 && cclp->cmdbuff[j - 1] == '.'
4043 && cclp->cmdbuff[j - 2] == '.'
4044 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4045 {
4046 cmdline_del(cclp, j - 2);
4047 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004048 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004049 }
4050 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004051 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004052 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004053 // go up a directory
4054 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004055
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004056 j = cclp->cmdpos - 1;
4057 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4058 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004059 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004060 if (has_mbyte)
4061 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4062 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004063#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004064 && vim_strchr((char_u *)" *?[{`$%#",
4065 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004066#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004067 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004068 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004069 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004070 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004071 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004072 break;
4073 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004074 else
4075 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004076 }
4077 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004078
4079 if (!found)
4080 j = i;
4081 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4082 j += 4;
4083 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4084 && j == i)
4085 j += 3;
4086 else
4087 j = 0;
4088 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004089 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004090 // TODO this is only for DOS/UNIX systems - need to put in
4091 // machine-specific stuff here and in upseg init
4092 cmdline_del(cclp, j);
4093 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004094 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004095 else if (cclp->cmdpos > i)
4096 cmdline_del(cclp, i);
4097
4098 // Now complete in the new directory. Set KeyTyped in case the
4099 // Up key came from a mapping.
4100 key = p_wc;
4101 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004102 }
4103
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004104 return key;
4105}
4106
4107/*
4108 * Handle a key pressed when the wild menu is displayed
4109 */
4110 int
4111wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4112{
4113 if (xp->xp_context == EXPAND_MENUNAMES)
4114 return wildmenu_process_key_menunames(cclp, key, xp);
4115 else if ((xp->xp_context == EXPAND_FILES
4116 || xp->xp_context == EXPAND_DIRECTORIES
4117 || xp->xp_context == EXPAND_SHELLCMD))
4118 return wildmenu_process_key_filenames(cclp, key, xp);
4119
4120 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004121}
4122
4123/*
4124 * Free expanded names when finished walking through the matches
4125 */
4126 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004127wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004128{
4129 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004130
4131 if (!p_wmnu || wild_menu_showing == 0)
4132 return;
4133
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004134#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004135 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004136 if (cclp->input_fn)
4137 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004138#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004139
4140 if (wild_menu_showing == WM_SCROLLED)
4141 {
4142 // Entered command line, move it up
4143 cmdline_row--;
4144 redrawcmd();
4145 }
4146 else if (save_p_ls != -1)
4147 {
4148 // restore 'laststatus' and 'winminheight'
4149 p_ls = save_p_ls;
4150 p_wmh = save_p_wmh;
4151 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004152 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004153 redrawcmd();
4154 save_p_ls = -1;
4155 }
4156 else
4157 {
4158 win_redraw_last_status(topframe);
4159 redraw_statuslines();
4160 }
4161 KeyTyped = skt;
4162 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004163#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004164 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004165 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004166#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004167}
Bram Moolenaareadee482020-09-04 15:37:31 +02004168
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004169#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004170/*
4171 * "getcompletion()" function
4172 */
4173 void
4174f_getcompletion(typval_T *argvars, typval_T *rettv)
4175{
4176 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004177 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004178 expand_T xpc;
4179 int filtered = FALSE;
4180 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004181 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004182
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004183 if (in_vim9script()
4184 && (check_for_string_arg(argvars, 0) == FAIL
4185 || check_for_string_arg(argvars, 1) == FAIL
4186 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4187 return;
4188
ii144785fe02021-11-21 12:13:56 +00004189 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004190 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004191 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004192 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004193
4194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004195 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004196
4197 if (p_wic)
4198 options |= WILD_ICASE;
4199
4200 // For filtered results, 'wildignore' is used
4201 if (!filtered)
4202 options |= WILD_KEEP_ALL;
4203
4204 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004205 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004206 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004207 int cmdline_len = (int)STRLEN(pat);
4208 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004209 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004210 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004211 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004212 else
4213 {
ii144785fe02021-11-21 12:13:56 +00004214 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004215 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004216 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004217
4218 xpc.xp_context = cmdcomplete_str_to_type(type);
4219 if (xpc.xp_context == EXPAND_NOTHING)
4220 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004221 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004222 return;
4223 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004224
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004225 if (xpc.xp_context == EXPAND_USER_DEFINED)
4226 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004227 // Must be "custom,funcname" pattern
4228 if (STRNCMP(type, "custom,", 7) != 0)
4229 {
4230 semsg(_(e_invalid_argument_str), type);
4231 return;
4232 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004233
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004234 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004235 }
4236
4237 if (xpc.xp_context == EXPAND_USER_LIST)
4238 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004239 // Must be "customlist,funcname" pattern
4240 if (STRNCMP(type, "customlist,", 11) != 0)
4241 {
4242 semsg(_(e_invalid_argument_str), type);
4243 return;
4244 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004245
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004246 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004247 }
4248
Bram Moolenaar66b51422019-08-18 21:44:12 +02004249# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004250 if (xpc.xp_context == EXPAND_MENUS)
4251 {
4252 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4253 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4254 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004255# endif
4256# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004257 if (xpc.xp_context == EXPAND_CSCOPE)
4258 {
4259 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4260 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4261 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004262# endif
4263# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004264 if (xpc.xp_context == EXPAND_SIGN)
4265 {
4266 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4267 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4268 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004269# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004270 if (xpc.xp_context == EXPAND_RUNTIME)
4271 {
4272 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4273 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4274 }
zeertzjq85f36d62024-10-10 19:14:13 +02004275 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4276 {
4277 int context = EXPAND_SHELLCMDLINE;
4278 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4279 &context);
4280 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4281 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004282 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004283
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004284 if (cmdline_fuzzy_completion_supported(&xpc))
4285 // when fuzzy matching, don't modify the search string
4286 pat = vim_strsave(xpc.xp_pattern);
4287 else
4288 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4289
Bram Moolenaar93a10962022-06-16 11:42:09 +01004290 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004291 {
4292 int i;
4293
4294 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4295
4296 for (i = 0; i < xpc.xp_numfiles; i++)
4297 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4298 }
4299 vim_free(pat);
4300 ExpandCleanup(&xpc);
4301}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004302#endif // FEAT_EVAL