blob: 3ceb6c73e7c40439d1ea0226297136d1fb7a3977 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010053 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000054 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010055 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020057 && xp->xp_context != EXPAND_STRING_SETTING
58 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000061 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020063 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_TAGS
65 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_USER_LIST);
67}
68
69/*
70 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000071 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
72 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000073 */
74 int
75cmdline_fuzzy_complete(char_u *fuzzystr)
76{
77 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
78}
79
80/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000081 * sort function for the completion matches.
82 * <SNR> functions should be sorted to the end.
83 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020084 static int
85sort_func_compare(const void *s1, const void *s2)
86{
87 char_u *p1 = *(char_u **)s1;
88 char_u *p2 = *(char_u **)s2;
89
90 if (*p1 != '<' && *p2 == '<') return -1;
91 if (*p1 == '<' && *p2 != '<') return 1;
92 return STRCMP(p1, p2);
93}
Bram Moolenaar66b51422019-08-18 21:44:12 +020094
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000095/*
96 * Escape special characters in the cmdline completion matches.
97 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020098 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000099wildescape(
100 expand_T *xp,
101 char_u *str,
102 int numfiles,
103 char_u **files)
104{
105 char_u *p;
106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
108
109 if (xp->xp_context == EXPAND_FILES
110 || xp->xp_context == EXPAND_FILES_IN_PATH
111 || xp->xp_context == EXPAND_SHELLCMD
112 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200113 || xp->xp_context == EXPAND_DIRECTORIES
114 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000115 {
116 // Insert a backslash into a file name before a space, \, %, #
117 // and wildmatch characters, except '~'.
118 for (int i = 0; i < numfiles; ++i)
119 {
120 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000122 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200123 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
124 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129#if defined(BACKSLASH_IN_FILENAME)
130 p = vim_strsave_escaped(files[i], (char_u *)" ");
131 if (p != NULL)
132 {
133 vim_free(files[i]);
134 files[i] = p;
135 }
136#endif
137 }
138 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200139 else if (xp->xp_backslash & XP_BS_COMMA)
140 {
141 if (vim_strchr(files[i], ',') != NULL)
142 {
143 p = vim_strsave_escaped(files[i], (char_u *)",");
144 if (p != NULL)
145 {
146 vim_free(files[i]);
147 files[i] = p;
148 }
149 }
150 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000151#ifdef BACKSLASH_IN_FILENAME
152 p = vim_strsave_fnameescape(files[i], vse_what);
153#else
154 p = vim_strsave_fnameescape(files[i],
155 xp->xp_shell ? VSE_SHELL : vse_what);
156#endif
157 if (p != NULL)
158 {
159 vim_free(files[i]);
160 files[i] = p;
161 }
162
163 // If 'str' starts with "\~", replace "~" at start of
164 // files[i] with "\~".
165 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
166 escape_fname(&files[i]);
167 }
168 xp->xp_backslash = XP_BS_NONE;
169
170 // If the first file starts with a '+' escape it. Otherwise it
171 // could be seen as "+cmd".
172 if (*files[0] == '+')
173 escape_fname(&files[0]);
174 }
175 else if (xp->xp_context == EXPAND_TAGS)
176 {
177 // Insert a backslash before characters in a tag name that
178 // would terminate the ":tag" command.
179 for (int i = 0; i < numfiles; ++i)
180 {
181 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
182 if (p != NULL)
183 {
184 vim_free(files[i]);
185 files[i] = p;
186 }
187 }
188 }
189}
190
191/*
192 * Escape special characters in the cmdline completion matches.
193 */
194 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200195ExpandEscape(
196 expand_T *xp,
197 char_u *str,
198 int numfiles,
199 char_u **files,
200 int options)
201{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200202 // May change home directory back to "~"
203 if (options & WILD_HOME_REPLACE)
204 tilde_replace(str, numfiles, files);
205
206 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000207 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200208}
209
210/*
211 * Return FAIL if this is not an appropriate context in which to do
212 * completion of anything, return OK if it is (even if there are no matches).
213 * For the caller, this means that the character is just passed through like a
214 * normal character (instead of being expanded). This allows :s/^I^D etc.
215 */
216 int
217nextwild(
218 expand_T *xp,
219 int type,
220 int options, // extra options for ExpandOne()
221 int escape) // if TRUE, escape the returned matches
222{
223 cmdline_info_T *ccline = get_cmdline_info();
224 int i, j;
225 char_u *p1;
226 char_u *p2;
227 int difflen;
228 int v;
229
230 if (xp->xp_numfiles == -1)
231 {
232 set_expand_context(xp);
233 cmd_showtail = expand_showtail(xp);
234 }
235
236 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
237 {
238 beep_flush();
239 return OK; // Something illegal on command line
240 }
241 if (xp->xp_context == EXPAND_NOTHING)
242 {
243 // Caller can use the character as a normal char instead
244 return FAIL;
245 }
246
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000247 // If cmd_silent is set then don't show the dots, because redrawcmd() below
248 // won't remove them.
249 if (!cmd_silent)
250 {
251 msg_puts("..."); // show that we are busy
252 out_flush();
253 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200254
255 i = (int)(xp->xp_pattern - ccline->cmdbuff);
256 xp->xp_pattern_len = ccline->cmdpos - i;
257
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000258 if (type == WILD_NEXT || type == WILD_PREV
259 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200260 {
261 // Get next/previous match for a previous expanded pattern.
262 p2 = ExpandOne(xp, NULL, NULL, 0, type);
263 }
264 else
265 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000266 if (cmdline_fuzzy_completion_supported(xp))
267 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200268 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000269 else
270 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
271
Bram Moolenaar66b51422019-08-18 21:44:12 +0200272 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000273 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200274 p2 = NULL;
275 else
276 {
277 int use_options = options |
278 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
279 if (escape)
280 use_options |= WILD_ESCAPE;
281
282 if (p_wic)
283 use_options += WILD_ICASE;
284 p2 = ExpandOne(xp, p1,
285 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
286 use_options, type);
287 vim_free(p1);
288 // longest match: make sure it is not shorter, happens with :help
289 if (p2 != NULL && type == WILD_LONGEST)
290 {
291 for (j = 0; j < xp->xp_pattern_len; ++j)
292 if (ccline->cmdbuff[i + j] == '*'
293 || ccline->cmdbuff[i + j] == '?')
294 break;
295 if ((int)STRLEN(p2) < j)
296 VIM_CLEAR(p2);
297 }
298 }
299 }
300
301 if (p2 != NULL && !got_int)
302 {
303 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
304 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
305 {
306 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
307 xp->xp_pattern = ccline->cmdbuff + i;
308 }
309 else
310 v = OK;
311 if (v == OK)
312 {
313 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
314 &ccline->cmdbuff[ccline->cmdpos],
315 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
316 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
317 ccline->cmdlen += difflen;
318 ccline->cmdpos += difflen;
319 }
320 }
321 vim_free(p2);
322
323 redrawcmd();
324 cursorcmd();
325
326 // When expanding a ":map" command and no matches are found, assume that
327 // the key is supposed to be inserted literally
328 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
329 return FAIL;
330
331 if (xp->xp_numfiles <= 0 && p2 == NULL)
332 beep_flush();
333 else if (xp->xp_numfiles == 1)
334 // free expanded pattern
335 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
336
337 return OK;
338}
339
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340/*
341 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000342 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000343 */
344 static int
345cmdline_pum_create(
346 cmdline_info_T *ccline,
347 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000348 char_u **matches,
349 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000350 int showtail)
351{
352 int i;
353 int columns;
354
355 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000356 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000358 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 {
zeertzjqc51a3762022-12-10 10:22:29 +0000360 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000361 compl_match_array[i].pum_info = NULL;
362 compl_match_array[i].pum_extra = NULL;
363 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200364 compl_match_array[i].pum_user_abbr_hlattr = -1;
365 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366 }
367
368 // Compute the popup menu starting column
369 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
370 columns = vim_strsize(xp->xp_pattern);
371 if (showtail)
372 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000373 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000374 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 }
376 if (columns >= compl_startcol)
377 compl_startcol = 0;
378 else
379 compl_startcol -= columns;
380
381 // no default selection
382 compl_selected = -1;
383
384 cmdline_pum_display();
385
386 return EXPAND_OK;
387}
388
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000389/*
390 * Display the cmdline completion matches in a popup menu
391 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000392 void
393cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000394{
395 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
396}
397
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000398/*
399 * Returns TRUE if the cmdline completion popup menu is being displayed.
400 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000401 int
402cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000403{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100404 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000405}
406
407/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000408 * Remove the cmdline completion popup menu (if present), free the list of
409 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000411 void
412cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000413{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000414 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100415 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000416
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417 pum_undisplay();
418 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000419 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000421 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000422 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100423
424 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
425 // as a side effect.
426 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000427}
428
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000429 void
430cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000431{
432 cmdline_pum_remove();
433 wildmenu_cleanup(cclp);
434}
435
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000436/*
437 * Returns the starting column number to use for the cmdline completion popup
438 * menu.
439 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000440 int
441cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000442{
443 return compl_startcol;
444}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000445
Bram Moolenaar66b51422019-08-18 21:44:12 +0200446/*
zeertzjqd8c93402024-06-17 18:25:32 +0200447 * Returns the current cmdline completion pattern.
448 */
449 char_u *
450cmdline_compl_pattern(void)
451{
452 expand_T *xp = get_cmdline_info()->xpc;
453
454 return xp == NULL ? NULL : xp->xp_orig;
455}
456
457/*
458 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
459 */
460 int
461cmdline_compl_is_fuzzy(void)
462{
463 expand_T *xp = get_cmdline_info()->xpc;
464
465 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
466}
467
468/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000469 * Return the number of characters that should be skipped in a status match.
470 * These are backslashes used for escaping. Do show backslashes in help tags.
471 */
472 static int
473skip_status_match_char(expand_T *xp, char_u *s)
474{
475 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
476#ifdef FEAT_MENU
477 || ((xp->xp_context == EXPAND_MENUS
478 || xp->xp_context == EXPAND_MENUNAMES)
479 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
480#endif
481 )
482 {
483#ifndef BACKSLASH_IN_FILENAME
484 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
485 return 2;
486#endif
487 return 1;
488 }
489 return 0;
490}
491
492/*
493 * Get the length of an item as it will be shown in the status line.
494 */
495 static int
496status_match_len(expand_T *xp, char_u *s)
497{
498 int len = 0;
499
500#ifdef FEAT_MENU
501 int emenu = xp->xp_context == EXPAND_MENUS
502 || xp->xp_context == EXPAND_MENUNAMES;
503
504 // Check for menu separators - replace with '|'.
505 if (emenu && menu_is_separator(s))
506 return 1;
507#endif
508
509 while (*s != NUL)
510 {
511 s += skip_status_match_char(xp, s);
512 len += ptr2cells(s);
513 MB_PTR_ADV(s);
514 }
515
516 return len;
517}
518
519/*
520 * Show wildchar matches in the status line.
521 * Show at least the "match" item.
522 * We start at item 'first_match' in the list and show all matches that fit.
523 *
524 * If inversion is possible we use it. Else '=' characters are used.
525 */
526 static void
527win_redr_status_matches(
528 expand_T *xp,
529 int num_matches,
530 char_u **matches, // list of matches
531 int match,
532 int showtail)
533{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000534 int row;
535 char_u *buf;
536 int len;
537 int clen; // length in screen cells
538 int fillchar;
539 int attr;
540 int i;
541 int highlight = TRUE;
542 char_u *selstart = NULL;
543 int selstart_col = 0;
544 char_u *selend = NULL;
545 static int first_match = 0;
546 int add_left = FALSE;
547 char_u *s;
548#ifdef FEAT_MENU
549 int emenu;
550#endif
551 int l;
552
553 if (matches == NULL) // interrupted completion?
554 return;
555
556 if (has_mbyte)
557 buf = alloc(Columns * MB_MAXBYTES + 1);
558 else
559 buf = alloc(Columns + 1);
560 if (buf == NULL)
561 return;
562
563 if (match == -1) // don't show match but original text
564 {
565 match = 0;
566 highlight = FALSE;
567 }
568 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000569 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000570 if (match == 0)
571 first_match = 0;
572 else if (match < first_match)
573 {
574 // jumping left, as far as we can go
575 first_match = match;
576 add_left = TRUE;
577 }
578 else
579 {
580 // check if match fits on the screen
581 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000582 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000583 if (first_match > 0)
584 clen += 2;
585 // jumping right, put match at the left
586 if ((long)clen > Columns)
587 {
588 first_match = match;
589 // if showing the last match, we can add some on the left
590 clen = 2;
591 for (i = match; i < num_matches; ++i)
592 {
zeertzjqc51a3762022-12-10 10:22:29 +0000593 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000594 if ((long)clen >= Columns)
595 break;
596 }
597 if (i == num_matches)
598 add_left = TRUE;
599 }
600 }
601 if (add_left)
602 while (first_match > 0)
603 {
zeertzjqc51a3762022-12-10 10:22:29 +0000604 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000605 if ((long)clen >= Columns)
606 break;
607 --first_match;
608 }
609
610 fillchar = fillchar_status(&attr, curwin);
611
612 if (first_match == 0)
613 {
614 *buf = NUL;
615 len = 0;
616 }
617 else
618 {
619 STRCPY(buf, "< ");
620 len = 2;
621 }
622 clen = len;
623
624 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000625 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000626 {
627 if (i == match)
628 {
629 selstart = buf + len;
630 selstart_col = clen;
631 }
632
zeertzjqc51a3762022-12-10 10:22:29 +0000633 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000634 // Check for menu separators - replace with '|'
635#ifdef FEAT_MENU
636 emenu = (xp->xp_context == EXPAND_MENUS
637 || xp->xp_context == EXPAND_MENUNAMES);
638 if (emenu && menu_is_separator(s))
639 {
640 STRCPY(buf + len, transchar('|'));
641 l = (int)STRLEN(buf + len);
642 len += l;
643 clen += l;
644 }
645 else
646#endif
647 for ( ; *s != NUL; ++s)
648 {
649 s += skip_status_match_char(xp, s);
650 clen += ptr2cells(s);
651 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
652 {
653 STRNCPY(buf + len, s, l);
654 s += l - 1;
655 len += l;
656 }
657 else
658 {
659 STRCPY(buf + len, transchar_byte(*s));
660 len += (int)STRLEN(buf + len);
661 }
662 }
663 if (i == match)
664 selend = buf + len;
665
666 *(buf + len++) = ' ';
667 *(buf + len++) = ' ';
668 clen += 2;
669 if (++i == num_matches)
670 break;
671 }
672
673 if (i != num_matches)
674 {
675 *(buf + len++) = '>';
676 ++clen;
677 }
678
679 buf[len] = NUL;
680
681 row = cmdline_row - 1;
682 if (row >= 0)
683 {
684 if (wild_menu_showing == 0)
685 {
686 if (msg_scrolled > 0)
687 {
688 // Put the wildmenu just above the command line. If there is
689 // no room, scroll the screen one line up.
690 if (cmdline_row == Rows - 1)
691 {
692 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
693 ++msg_scrolled;
694 }
695 else
696 {
697 ++cmdline_row;
698 ++row;
699 }
700 wild_menu_showing = WM_SCROLLED;
701 }
702 else
703 {
704 // Create status line if needed by setting 'laststatus' to 2.
705 // Set 'winminheight' to zero to avoid that the window is
706 // resized.
707 if (lastwin->w_status_height == 0)
708 {
709 save_p_ls = p_ls;
710 save_p_wmh = p_wmh;
711 p_ls = 2;
712 p_wmh = 0;
713 last_status(FALSE);
714 }
715 wild_menu_showing = WM_SHOWN;
716 }
717 }
718
719 screen_puts(buf, row, 0, attr);
720 if (selstart != NULL && highlight)
721 {
722 *selend = NUL;
723 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
724 }
725
726 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
727 }
728
729 win_redraw_last_status(topframe);
730 vim_free(buf);
731}
732
733/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000734 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200735 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000736 */
737 static char_u *
738get_next_or_prev_match(
739 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200740 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000741{
zeertzjqe9ef3472023-08-17 23:57:05 +0200742 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000743 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000744
745 if (xp->xp_numfiles <= 0)
746 return NULL;
747
748 if (mode == WILD_PREV)
749 {
750 if (findex == -1)
751 findex = xp->xp_numfiles;
752 --findex;
753 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000754 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000755 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000756 else if (mode == WILD_PAGEUP)
757 {
758 if (findex == 0)
759 // at the first entry, don't select any entries
760 findex = -1;
761 else if (findex == -1)
762 // no entry is selected. select the last entry
763 findex = xp->xp_numfiles - 1;
764 else
765 {
766 // go up by the pum height
767 ht = pum_get_height();
768 if (ht > 3)
769 ht -= 2;
770 findex -= ht;
771 if (findex < 0)
772 // few entries left, select the first entry
773 findex = 0;
774 }
775 }
776 else // mode == WILD_PAGEDOWN
777 {
778 if (findex == xp->xp_numfiles - 1)
779 // at the last entry, don't select any entries
780 findex = -1;
781 else if (findex == -1)
782 // no entry is selected. select the first entry
783 findex = 0;
784 else
785 {
786 // go down by the pum height
787 ht = pum_get_height();
788 if (ht > 3)
789 ht -= 2;
790 findex += ht;
791 if (findex >= xp->xp_numfiles)
792 // few entries left, select the last entry
793 findex = xp->xp_numfiles - 1;
794 }
795 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000797 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000798 if (findex < 0)
799 {
zeertzjq28a23602023-09-29 19:58:35 +0200800 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000801 findex = xp->xp_numfiles - 1;
802 else
803 findex = -1;
804 }
805 if (findex >= xp->xp_numfiles)
806 {
zeertzjq28a23602023-09-29 19:58:35 +0200807 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000808 findex = 0;
809 else
810 findex = -1;
811 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000812 if (compl_match_array)
813 {
814 compl_selected = findex;
815 cmdline_pum_display();
816 }
817 else if (p_wmnu)
818 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
819 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200820 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000821
822 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200823 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000824
825 return vim_strsave(xp->xp_files[findex]);
826}
827
828/*
829 * Start the command-line expansion and get the matches.
830 */
831 static char_u *
832ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
833{
834 int non_suf_match; // number without matching suffix
835 int i;
836 char_u *ss = NULL;
837
838 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000839 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000840 options) == FAIL)
841 {
842#ifdef FNAME_ILLEGAL
843 // Illegal file name has been silently skipped. But when there
844 // are wildcards, the real problem is that there was no match,
845 // causing the pattern to be added, which has illegal characters.
846 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
847 semsg(_(e_no_match_str_2), str);
848#endif
849 }
850 else if (xp->xp_numfiles == 0)
851 {
852 if (!(options & WILD_SILENT))
853 semsg(_(e_no_match_str_2), str);
854 }
855 else
856 {
857 // Escape the matches for use on the command line.
858 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
859
860 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000861 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000862 {
863 if (xp->xp_numfiles)
864 non_suf_match = xp->xp_numfiles;
865 else
866 non_suf_match = 1;
867 if ((xp->xp_context == EXPAND_FILES
868 || xp->xp_context == EXPAND_DIRECTORIES)
869 && xp->xp_numfiles > 1)
870 {
871 // More than one match; check suffix.
872 // The files will have been sorted on matching suffix in
873 // expand_wildcards, only need to check the first two.
874 non_suf_match = 0;
875 for (i = 0; i < 2; ++i)
876 if (match_suffix(xp->xp_files[i]))
877 ++non_suf_match;
878 }
879 if (non_suf_match != 1)
880 {
881 // Can we ever get here unless it's while expanding
882 // interactively? If not, we can get rid of this all
883 // together. Don't really want to wait for this message
884 // (and possibly have to hit return to continue!).
885 if (!(options & WILD_SILENT))
886 emsg(_(e_too_many_file_names));
887 else if (!(options & WILD_NO_BEEP))
888 beep_flush();
889 }
890 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
891 ss = vim_strsave(xp->xp_files[0]);
892 }
893 }
894
895 return ss;
896}
897
898/*
899 * Return the longest common part in the list of cmdline completion matches.
900 */
901 static char_u *
902find_longest_match(expand_T *xp, int options)
903{
904 long_u len;
905 int mb_len = 1;
906 int c0, ci;
907 int i;
908 char_u *ss;
909
910 for (len = 0; xp->xp_files[0][len]; len += mb_len)
911 {
912 if (has_mbyte)
913 {
914 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
915 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
916 }
917 else
918 c0 = xp->xp_files[0][len];
919 for (i = 1; i < xp->xp_numfiles; ++i)
920 {
921 if (has_mbyte)
922 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
923 else
924 ci = xp->xp_files[i][len];
925 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
926 || xp->xp_context == EXPAND_FILES
927 || xp->xp_context == EXPAND_SHELLCMD
928 || xp->xp_context == EXPAND_BUFFERS))
929 {
930 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
931 break;
932 }
933 else if (c0 != ci)
934 break;
935 }
936 if (i < xp->xp_numfiles)
937 {
938 if (!(options & WILD_NO_BEEP))
939 vim_beep(BO_WILD);
940 break;
941 }
942 }
943
944 ss = alloc(len + 1);
945 if (ss)
946 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
947
948 return ss;
949}
950
951/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000952 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200953 * Chars that should not be expanded must be preceded with a backslash.
954 * Return a pointer to allocated memory containing the new string.
955 * Return NULL for failure.
956 *
957 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200958 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
959 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200960 *
961 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
962 * is WILD_EXPAND_FREE or WILD_ALL.
963 *
964 * mode = WILD_FREE: just free previously expanded matches
965 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
966 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
967 * mode = WILD_NEXT: use next match in multiple match, wrap to first
968 * mode = WILD_PREV: use previous match in multiple match, wrap to first
969 * mode = WILD_ALL: return all matches concatenated
970 * mode = WILD_LONGEST: return longest matched part
971 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000972 * mode = WILD_APPLY: apply the item selected in the cmdline completion
973 * popup menu and close the menu.
974 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
975 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200976 *
977 * options = WILD_LIST_NOTFOUND: list entries without a match
978 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
979 * options = WILD_USE_NL: Use '\n' for WILD_ALL
980 * options = WILD_NO_BEEP: Don't beep for multiple matches
981 * options = WILD_ADD_SLASH: add a slash after directory names
982 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
983 * options = WILD_SILENT: don't print warning messages
984 * options = WILD_ESCAPE: put backslash before special chars
985 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200986 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 *
988 * The variables xp->xp_context and xp->xp_backslash must have been set!
989 */
990 char_u *
991ExpandOne(
992 expand_T *xp,
993 char_u *str,
994 char_u *orig, // allocated copy of original of expanded string
995 int options,
996 int mode)
997{
998 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200999 int orig_saved = FALSE;
1000 int i;
1001 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001002
1003 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001004 if (mode == WILD_NEXT || mode == WILD_PREV
1005 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001006 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001007
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001008 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001009 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001010 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001011 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001012 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001013 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001014
Bram Moolenaar66b51422019-08-18 21:44:12 +02001015 // free old names
1016 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1017 {
1018 FreeWild(xp->xp_numfiles, xp->xp_files);
1019 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001020 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001021
1022 // The entries from xp_files may be used in the PUM, remove it.
1023 if (compl_match_array != NULL)
1024 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001026 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001027
1028 if (mode == WILD_FREE) // only release file name
1029 return NULL;
1030
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001031 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 {
zeertzjq28a23602023-09-29 19:58:35 +02001033 vim_free(xp->xp_orig);
1034 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001035 orig_saved = TRUE;
1036
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001037 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038 }
1039
1040 // Find longest common part
1041 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1042 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001043 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001044 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001045 }
1046
Bram Moolenaar57e95172022-08-20 19:26:14 +01001047 // Concatenate all matching names. Unless interrupted, this can be slow
1048 // and the result probably won't be used.
1049 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001050 {
1051 len = 0;
1052 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001053 {
1054 if (i > 0)
1055 {
1056 if (xp->xp_prefix == XP_PREFIX_NO)
1057 len += 2; // prefix "no"
1058 else if (xp->xp_prefix == XP_PREFIX_INV)
1059 len += 3; // prefix "inv"
1060 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001062 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001063 ss = alloc(len);
1064 if (ss != NULL)
1065 {
1066 *ss = NUL;
1067 for (i = 0; i < xp->xp_numfiles; ++i)
1068 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001069 if (i > 0)
1070 {
1071 if (xp->xp_prefix == XP_PREFIX_NO)
1072 STRCAT(ss, "no");
1073 else if (xp->xp_prefix == XP_PREFIX_INV)
1074 STRCAT(ss, "inv");
1075 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001077
Bram Moolenaar66b51422019-08-18 21:44:12 +02001078 if (i != xp->xp_numfiles - 1)
1079 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1080 }
1081 }
1082 }
1083
1084 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1085 ExpandCleanup(xp);
1086
zeertzjq28a23602023-09-29 19:58:35 +02001087 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001088 if (!orig_saved)
1089 vim_free(orig);
1090
1091 return ss;
1092}
1093
1094/*
1095 * Prepare an expand structure for use.
1096 */
1097 void
1098ExpandInit(expand_T *xp)
1099{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001100 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001101 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001102 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001103 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001104}
1105
1106/*
1107 * Cleanup an expand structure after use.
1108 */
1109 void
1110ExpandCleanup(expand_T *xp)
1111{
1112 if (xp->xp_numfiles >= 0)
1113 {
1114 FreeWild(xp->xp_numfiles, xp->xp_files);
1115 xp->xp_numfiles = -1;
1116 }
zeertzjq28a23602023-09-29 19:58:35 +02001117 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001118}
1119
1120/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001121 * Display one line of completion matches. Multiple matches are displayed in
1122 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001123 * matches - list of completion match names
1124 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001125 * lines - number of output lines
1126 * linenr - line number of matches to display
1127 * maxlen - maximum number of characters in each line
1128 * showtail - display only the tail of the full path of a file name
1129 * dir_attr - highlight attribute to use for directory names
1130 */
1131 static void
1132showmatches_oneline(
1133 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001134 char_u **matches,
1135 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 int lines,
1137 int linenr,
1138 int maxlen,
1139 int showtail,
1140 int dir_attr)
1141{
1142 int i, j;
1143 int isdir;
1144 int lastlen;
1145 char_u *p;
1146
1147 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001148 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001149 {
1150 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1151 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001152 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1153 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001154 msg_advance(maxlen + 1);
1155 msg_puts((char *)p);
1156 msg_advance(maxlen + 3);
1157 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1158 break;
1159 }
1160 for (i = maxlen - lastlen; --i >= 0; )
1161 msg_putchar(' ');
1162 if (xp->xp_context == EXPAND_FILES
1163 || xp->xp_context == EXPAND_SHELLCMD
1164 || xp->xp_context == EXPAND_BUFFERS)
1165 {
1166 // highlight directories
1167 if (xp->xp_numfiles != -1)
1168 {
1169 char_u *halved_slash;
1170 char_u *exp_path;
1171 char_u *path;
1172
1173 // Expansion was done before and special characters
1174 // were escaped, need to halve backslashes. Also
1175 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001176 exp_path = expand_env_save_opt(matches[j], TRUE);
1177 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001178 halved_slash = backslash_halve_save(path);
1179 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001180 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001181 vim_free(exp_path);
1182 if (halved_slash != path)
1183 vim_free(halved_slash);
1184 }
1185 else
1186 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001187 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001188 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001189 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001190 else
1191 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001192 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001193 TRUE);
1194 p = NameBuff;
1195 }
1196 }
1197 else
1198 {
1199 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001200 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001201 }
1202 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1203 }
1204 if (msg_col > 0) // when not wrapped around
1205 {
1206 msg_clr_eos();
1207 msg_putchar('\n');
1208 }
1209 out_flush(); // show one line at a time
1210}
1211
1212/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001213 * Show all matches for completion on the command line.
1214 * Returns EXPAND_NOTHING when the character that triggered expansion should
1215 * be inserted like a normal character.
1216 */
1217 int
1218showmatches(expand_T *xp, int wildmenu UNUSED)
1219{
1220 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001221 int numMatches;
1222 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001223 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001224 int maxlen;
1225 int lines;
1226 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001227 int attr;
1228 int showtail;
1229
1230 if (xp->xp_numfiles == -1)
1231 {
1232 set_expand_context(xp);
1233 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001234 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001235 showtail = expand_showtail(xp);
1236 if (i != EXPAND_OK)
1237 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001238 }
1239 else
1240 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001241 numMatches = xp->xp_numfiles;
1242 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001243 showtail = cmd_showtail;
1244 }
1245
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001246 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001247 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001248 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001249
Bram Moolenaar66b51422019-08-18 21:44:12 +02001250 if (!wildmenu)
1251 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 msg_didany = FALSE; // lines_left will be set
1253 msg_start(); // prepare for paging
1254 msg_putchar('\n');
1255 out_flush();
1256 cmdline_row = msg_row;
1257 msg_didany = FALSE; // lines_left will be set again
1258 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001259 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001260
1261 if (got_int)
1262 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001264 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001265 else
1266 {
1267 // find the length of the longest file name
1268 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001269 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001270 {
1271 if (!showtail && (xp->xp_context == EXPAND_FILES
1272 || xp->xp_context == EXPAND_SHELLCMD
1273 || xp->xp_context == EXPAND_BUFFERS))
1274 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001275 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001276 j = vim_strsize(NameBuff);
1277 }
1278 else
zeertzjqc51a3762022-12-10 10:22:29 +00001279 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001280 if (j > maxlen)
1281 maxlen = j;
1282 }
1283
1284 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001285 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001286 else
1287 {
1288 // compute the number of columns and lines for the listing
1289 maxlen += 2; // two spaces between file names
1290 columns = ((int)Columns + 2) / maxlen;
1291 if (columns < 1)
1292 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001293 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001294 }
1295
1296 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1297
1298 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1299 {
1300 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1301 msg_clr_eos();
1302 msg_advance(maxlen - 3);
1303 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1304 }
1305
1306 // list the files line by line
1307 for (i = 0; i < lines; ++i)
1308 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001309 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001310 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001311 if (got_int)
1312 {
1313 got_int = FALSE;
1314 break;
1315 }
1316 }
1317
1318 // we redraw the command below the lines that we have just listed
1319 // This is a bit tricky, but it saves a lot of screen updating.
1320 cmdline_row = msg_row; // will put it back later
1321 }
1322
1323 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001324 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001325
1326 return EXPAND_OK;
1327}
1328
1329/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001330 * gettail() version for showmatches() and win_redr_status_matches():
1331 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001332 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001333 static char_u *
1334showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001335{
1336 char_u *p;
1337 char_u *t = s;
1338 int had_sep = FALSE;
1339
1340 for (p = s; *p != NUL; )
1341 {
1342 if (vim_ispathsep(*p)
1343#ifdef BACKSLASH_IN_FILENAME
1344 && !rem_backslash(p)
1345#endif
1346 )
1347 had_sep = TRUE;
1348 else if (had_sep)
1349 {
1350 t = p;
1351 had_sep = FALSE;
1352 }
1353 MB_PTR_ADV(p);
1354 }
1355 return t;
1356}
1357
1358/*
1359 * Return TRUE if we only need to show the tail of completion matches.
1360 * When not completing file names or there is a wildcard in the path FALSE is
1361 * returned.
1362 */
1363 static int
1364expand_showtail(expand_T *xp)
1365{
1366 char_u *s;
1367 char_u *end;
1368
1369 // When not completing file names a "/" may mean something different.
1370 if (xp->xp_context != EXPAND_FILES
1371 && xp->xp_context != EXPAND_SHELLCMD
1372 && xp->xp_context != EXPAND_DIRECTORIES)
1373 return FALSE;
1374
1375 end = gettail(xp->xp_pattern);
1376 if (end == xp->xp_pattern) // there is no path separator
1377 return FALSE;
1378
1379 for (s = xp->xp_pattern; s < end; s++)
1380 {
1381 // Skip escaped wildcards. Only when the backslash is not a path
1382 // separator, on DOS the '*' "path\*\file" must not be skipped.
1383 if (rem_backslash(s))
1384 ++s;
1385 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1386 return FALSE;
1387 }
1388 return TRUE;
1389}
1390
1391/*
1392 * Prepare a string for expansion.
1393 * When expanding file names: The string will be used with expand_wildcards().
1394 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1395 * When expanding other names: The string will be used with regcomp(). Copy
1396 * the name into allocated memory and prepend "^".
1397 */
1398 char_u *
1399addstar(
1400 char_u *fname,
1401 int len,
1402 int context) // EXPAND_FILES etc.
1403{
1404 char_u *retval;
1405 int i, j;
1406 int new_len;
1407 char_u *tail;
1408 int ends_in_star;
1409
1410 if (context != EXPAND_FILES
1411 && context != EXPAND_FILES_IN_PATH
1412 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001413 && context != EXPAND_DIRECTORIES
1414 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001415 {
1416 // Matching will be done internally (on something other than files).
1417 // So we convert the file-matching-type wildcards into our kind for
1418 // use with vim_regcomp(). First work out how long it will be:
1419
1420 // For help tags the translation is done in find_help_tags().
1421 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001422 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001423 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001424 || context == EXPAND_COLORS
1425 || context == EXPAND_COMPILER
1426 || context == EXPAND_OWNSYNTAX
1427 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001428 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001429 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001430 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001431 || ((context == EXPAND_TAGS_LISTFILES
1432 || context == EXPAND_TAGS)
1433 && fname[0] == '/'))
1434 retval = vim_strnsave(fname, len);
1435 else
1436 {
1437 new_len = len + 2; // +2 for '^' at start, NUL at end
1438 for (i = 0; i < len; i++)
1439 {
1440 if (fname[i] == '*' || fname[i] == '~')
1441 new_len++; // '*' needs to be replaced by ".*"
1442 // '~' needs to be replaced by "\~"
1443
1444 // Buffer names are like file names. "." should be literal
1445 if (context == EXPAND_BUFFERS && fname[i] == '.')
1446 new_len++; // "." becomes "\."
1447
1448 // Custom expansion takes care of special things, match
1449 // backslashes literally (perhaps also for other types?)
1450 if ((context == EXPAND_USER_DEFINED
1451 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1452 new_len++; // '\' becomes "\\"
1453 }
1454 retval = alloc(new_len);
1455 if (retval != NULL)
1456 {
1457 retval[0] = '^';
1458 j = 1;
1459 for (i = 0; i < len; i++, j++)
1460 {
1461 // Skip backslash. But why? At least keep it for custom
1462 // expansion.
1463 if (context != EXPAND_USER_DEFINED
1464 && context != EXPAND_USER_LIST
1465 && fname[i] == '\\'
1466 && ++i == len)
1467 break;
1468
1469 switch (fname[i])
1470 {
1471 case '*': retval[j++] = '.';
1472 break;
1473 case '~': retval[j++] = '\\';
1474 break;
1475 case '?': retval[j] = '.';
1476 continue;
1477 case '.': if (context == EXPAND_BUFFERS)
1478 retval[j++] = '\\';
1479 break;
1480 case '\\': if (context == EXPAND_USER_DEFINED
1481 || context == EXPAND_USER_LIST)
1482 retval[j++] = '\\';
1483 break;
1484 }
1485 retval[j] = fname[i];
1486 }
1487 retval[j] = NUL;
1488 }
1489 }
1490 }
1491 else
1492 {
1493 retval = alloc(len + 4);
1494 if (retval != NULL)
1495 {
1496 vim_strncpy(retval, fname, len);
1497
1498 // Don't add a star to *, ~, ~user, $var or `cmd`.
1499 // * would become **, which walks the whole tree.
1500 // ~ would be at the start of the file name, but not the tail.
1501 // $ could be anywhere in the tail.
1502 // ` could be anywhere in the file name.
1503 // When the name ends in '$' don't add a star, remove the '$'.
1504 tail = gettail(retval);
1505 ends_in_star = (len > 0 && retval[len - 1] == '*');
1506#ifndef BACKSLASH_IN_FILENAME
1507 for (i = len - 2; i >= 0; --i)
1508 {
1509 if (retval[i] != '\\')
1510 break;
1511 ends_in_star = !ends_in_star;
1512 }
1513#endif
1514 if ((*retval != '~' || tail != retval)
1515 && !ends_in_star
1516 && vim_strchr(tail, '$') == NULL
1517 && vim_strchr(retval, '`') == NULL)
1518 retval[len++] = '*';
1519 else if (len > 0 && retval[len - 1] == '$')
1520 --len;
1521 retval[len] = NUL;
1522 }
1523 }
1524 return retval;
1525}
1526
1527/*
1528 * Must parse the command line so far to work out what context we are in.
1529 * Completion can then be done based on that context.
1530 * This routine sets the variables:
1531 * xp->xp_pattern The start of the pattern to be expanded within
1532 * the command line (ends at the cursor).
1533 * xp->xp_context The type of thing to expand. Will be one of:
1534 *
1535 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1536 * the command line, like an unknown command. Caller
1537 * should beep.
1538 * EXPAND_NOTHING Unrecognised context for completion, use char like
1539 * a normal char, rather than for completion. eg
1540 * :s/^I/
1541 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1542 * it.
1543 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1544 * EXPAND_FILES After command with EX_XFILE set, or after setting
1545 * with P_EXPAND set. eg :e ^I, :w>>^I
1546 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001547 * when we know only directories are of interest.
1548 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001549 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1550 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1551 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1552 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1553 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1554 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1555 * EXPAND_EVENTS Complete event names
1556 * EXPAND_SYNTAX Complete :syntax command arguments
1557 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1558 * EXPAND_AUGROUP Complete autocommand group names
1559 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1560 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1561 * eg :unmap a^I , :cunab x^I
1562 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1563 * eg :call sub^I
1564 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1565 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1566 * names in expressions, eg :while s^I
1567 * EXPAND_ENV_VARS Complete environment variable names
1568 * EXPAND_USER Complete user names
1569 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001570 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001571set_expand_context(expand_T *xp)
1572{
1573 cmdline_info_T *ccline = get_cmdline_info();
1574
1575 // only expansion for ':', '>' and '=' command-lines
1576 if (ccline->cmdfirstc != ':'
1577#ifdef FEAT_EVAL
1578 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1579 && !ccline->input_fn
1580#endif
1581 )
1582 {
1583 xp->xp_context = EXPAND_NOTHING;
1584 return;
1585 }
1586 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1587}
1588
Bram Moolenaard0190392019-08-23 21:17:35 +02001589/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001590 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1591 * For user defined commands, the completion context is set in 'xp' and the
1592 * completion flags in 'complp'.
1593 *
1594 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001595 */
1596 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001597set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001598{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 char_u *p = NULL;
1600 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001601 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001602
1603 // Isolate the command and search for it in the command table.
1604 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001605 // - the 'k' command can directly be followed by any character, but do
1606 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1607 // find matches anywhere in the command name, do this only for command
1608 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001609 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001610 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001612 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001613 p = cmd + 1;
1614 }
1615 else
1616 {
1617 p = cmd;
1618 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1619 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001620 // A user command may contain digits.
1621 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1622 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001623 while (ASCII_ISALNUM(*p) || *p == '*')
1624 ++p;
1625 // for python 3.x: ":py3*" commands completion
1626 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1627 {
1628 ++p;
1629 while (ASCII_ISALPHA(*p) || *p == '*')
1630 ++p;
1631 }
1632 // check for non-alpha command
1633 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1634 ++p;
1635 len = (int)(p - cmd);
1636
1637 if (len == 0)
1638 {
1639 xp->xp_context = EXPAND_UNSUCCESSFUL;
1640 return NULL;
1641 }
1642
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001643 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001644
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001645 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001646 // Also when doing fuzzy expansion for non-shell commands, support
1647 // alphanumeric characters.
1648 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1649 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001650 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1651 ++p;
1652 }
1653
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001654 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001655 // character, complete the command name.
1656 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1657 return NULL;
1658
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001660 {
1661 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1662 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001663 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001664 p = cmd + 1;
1665 }
1666 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1667 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 eap->cmd = cmd;
1669 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 }
1673 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001674 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001675 {
1676 // Not still touching the command and it was an illegal one
1677 xp->xp_context = EXPAND_UNSUCCESSFUL;
1678 return NULL;
1679 }
1680
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681 return p;
1682}
Bram Moolenaard0190392019-08-23 21:17:35 +02001683
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001684/*
1685 * Set the completion context for a command argument with wild card characters.
1686 */
1687 static void
1688set_context_for_wildcard_arg(
1689 exarg_T *eap,
1690 char_u *arg,
1691 int usefilter,
1692 expand_T *xp,
1693 int *complp)
1694{
1695 char_u *p;
1696 int c;
1697 int in_quote = FALSE;
1698 char_u *bow = NULL; // Beginning of word
1699 int len = 0;
1700
1701 // Allow spaces within back-quotes to count as part of the argument
1702 // being expanded.
1703 xp->xp_pattern = skipwhite(arg);
1704 p = xp->xp_pattern;
1705 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 if (has_mbyte)
1708 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001709 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001710 c = *p;
1711 if (c == '\\' && p[1] != NUL)
1712 ++p;
1713 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001716 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001717 xp->xp_pattern = p;
1718 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001719 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001720 in_quote = !in_quote;
1721 }
1722 // An argument can contain just about everything, except
1723 // characters that end the command and white space.
1724 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001725#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001726 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001727#endif
1728 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001729 {
1730 len = 0; // avoid getting stuck when space is in 'isfname'
1731 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001732 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001733 if (has_mbyte)
1734 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001735 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001736 c = *p;
1737 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 if (has_mbyte)
1740 len = (*mb_ptr2len)(p);
1741 else
1742 len = 1;
1743 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001744 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001745 if (in_quote)
1746 bow = p;
1747 else
1748 xp->xp_pattern = p;
1749 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001750 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001752 }
1753
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001754 // If we are still inside the quotes, and we passed a space, just
1755 // expand from there.
1756 if (bow != NULL && in_quote)
1757 xp->xp_pattern = bow;
1758 xp->xp_context = EXPAND_FILES;
1759
1760 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001761 if (usefilter
1762 || (eap != NULL
1763 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1764 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001765 {
1766#ifndef BACKSLASH_IN_FILENAME
1767 xp->xp_shell = TRUE;
1768#endif
1769 // When still after the command name expand executables.
1770 if (xp->xp_pattern == skipwhite(arg))
1771 xp->xp_context = EXPAND_SHELLCMD;
1772 }
1773
1774 // Check for environment variable.
1775 if (*xp->xp_pattern == '$')
1776 {
1777 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1778 if (!vim_isIDc(*p))
1779 break;
1780 if (*p == NUL)
1781 {
1782 xp->xp_context = EXPAND_ENV_VARS;
1783 ++xp->xp_pattern;
1784 // Avoid that the assignment uses EXPAND_FILES again.
1785 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1786 *complp = EXPAND_ENV_VARS;
1787 }
1788 }
1789 // Check for user names.
1790 if (*xp->xp_pattern == '~')
1791 {
1792 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1793 ;
1794 // Complete ~user only if it partially matches a user name.
1795 // A full match ~user<Tab> will be replaced by user's home
1796 // directory i.e. something like ~user<Tab> -> /home/user/
1797 if (*p == NUL && p > xp->xp_pattern + 1
1798 && match_user(xp->xp_pattern + 1) >= 1)
1799 {
1800 xp->xp_context = EXPAND_USER;
1801 ++xp->xp_pattern;
1802 }
1803 }
1804}
1805
1806/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001807 * Set the completion context for the "++opt=arg" argument. Always returns
1808 * NULL.
1809 */
1810 static char_u *
1811set_context_in_argopt(expand_T *xp, char_u *arg)
1812{
1813 char_u *p;
1814
1815 p = vim_strchr(arg, '=');
1816 if (p == NULL)
1817 xp->xp_pattern = arg;
1818 else
1819 xp->xp_pattern = p + 1;
1820
1821 xp->xp_context = EXPAND_ARGOPT;
1822 return NULL;
1823}
1824
1825#ifdef FEAT_TERMINAL
1826/*
1827 * Set the completion context for :terminal's [options]. Always returns NULL.
1828 */
1829 static char_u *
1830set_context_in_terminalopt(expand_T *xp, char_u *arg)
1831{
1832 char_u *p;
1833
1834 p = vim_strchr(arg, '=');
1835 if (p == NULL)
1836 xp->xp_pattern = arg;
1837 else
1838 xp->xp_pattern = p + 1;
1839
1840 xp->xp_context = EXPAND_TERMINALOPT;
1841 return NULL;
1842}
1843#endif
1844
1845/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001846 * Set the completion context for the :filter command. Returns a pointer to the
1847 * next command after the :filter command.
1848 */
1849 static char_u *
1850set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1851{
1852 if (*arg != NUL)
1853 arg = skip_vimgrep_pat(arg, NULL, NULL);
1854 if (arg == NULL || *arg == NUL)
1855 {
1856 xp->xp_context = EXPAND_NOTHING;
1857 return NULL;
1858 }
1859 return skipwhite(arg);
1860}
1861
1862#ifdef FEAT_SEARCH_EXTRA
1863/*
1864 * Set the completion context for the :match command. Returns a pointer to the
1865 * next command after the :match command.
1866 */
1867 static char_u *
1868set_context_in_match_cmd(expand_T *xp, char_u *arg)
1869{
1870 if (*arg == NUL || !ends_excmd(*arg))
1871 {
1872 // also complete "None"
1873 set_context_in_echohl_cmd(xp, arg);
1874 arg = skipwhite(skiptowhite(arg));
1875 if (*arg != NUL)
1876 {
1877 xp->xp_context = EXPAND_NOTHING;
1878 arg = skip_regexp(arg + 1, *arg, magic_isset());
1879 }
1880 }
1881 return find_nextcmd(arg);
1882}
1883#endif
1884
1885/*
1886 * Returns a pointer to the next command after a :global or a :v command.
1887 * Returns NULL if there is no next command.
1888 */
1889 static char_u *
1890find_cmd_after_global_cmd(char_u *arg)
1891{
1892 int delim;
1893
1894 delim = *arg; // get the delimiter
1895 if (delim)
1896 ++arg; // skip delimiter if there is one
1897
1898 while (arg[0] != NUL && arg[0] != delim)
1899 {
1900 if (arg[0] == '\\' && arg[1] != NUL)
1901 ++arg;
1902 ++arg;
1903 }
1904 if (arg[0] != NUL)
1905 return arg + 1;
1906
1907 return NULL;
1908}
1909
1910/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001911 * Returns a pointer to the next command after a :substitute or a :& command.
1912 * Returns NULL if there is no next command.
1913 */
1914 static char_u *
1915find_cmd_after_substitute_cmd(char_u *arg)
1916{
1917 int delim;
1918
1919 delim = *arg;
1920 if (delim)
1921 {
1922 // skip "from" part
1923 ++arg;
1924 arg = skip_regexp(arg, delim, magic_isset());
1925
1926 if (arg[0] != NUL && arg[0] == delim)
1927 {
1928 // skip "to" part
1929 ++arg;
1930 while (arg[0] != NUL && arg[0] != delim)
1931 {
1932 if (arg[0] == '\\' && arg[1] != NUL)
1933 ++arg;
1934 ++arg;
1935 }
1936 if (arg[0] != NUL) // skip delimiter
1937 ++arg;
1938 }
1939 }
1940 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1941 ++arg;
1942 if (arg[0] != NUL)
1943 return arg;
1944
1945 return NULL;
1946}
1947
1948/*
1949 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1950 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1951 * Returns NULL if there is no next command.
1952 */
1953 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001954find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001955{
1956 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001957 if (*arg != '/')
1958 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001959
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001960 // Match regexp, not just whole words
1961 for (++arg; *arg && *arg != '/'; arg++)
1962 if (*arg == '\\' && arg[1] != NUL)
1963 arg++;
1964 if (*arg)
1965 {
1966 arg = skipwhite(arg + 1);
1967
1968 // Check for trailing illegal characters
1969 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1970 xp->xp_context = EXPAND_NOTHING;
1971 else
1972 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001973 }
1974
1975 return NULL;
1976}
1977
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001978#ifdef FEAT_EVAL
1979/*
1980 * Set the completion context for the :unlet command. Always returns NULL.
1981 */
1982 static char_u *
1983set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1984{
1985 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1986 arg = xp->xp_pattern + 1;
1987
1988 xp->xp_context = EXPAND_USER_VARS;
1989 xp->xp_pattern = arg;
1990
1991 if (*xp->xp_pattern == '$')
1992 {
1993 xp->xp_context = EXPAND_ENV_VARS;
1994 ++xp->xp_pattern;
1995 }
1996
1997 return NULL;
1998}
1999#endif
2000
2001#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2002/*
2003 * Set the completion context for the :language command. Always returns NULL.
2004 */
2005 static char_u *
2006set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2007{
2008 char_u *p;
2009
2010 p = skiptowhite(arg);
2011 if (*p == NUL)
2012 {
2013 xp->xp_context = EXPAND_LANGUAGE;
2014 xp->xp_pattern = arg;
2015 }
2016 else
2017 {
2018 if ( STRNCMP(arg, "messages", p - arg) == 0
2019 || STRNCMP(arg, "ctype", p - arg) == 0
2020 || STRNCMP(arg, "time", p - arg) == 0
2021 || STRNCMP(arg, "collate", p - arg) == 0)
2022 {
2023 xp->xp_context = EXPAND_LOCALES;
2024 xp->xp_pattern = skipwhite(p);
2025 }
2026 else
2027 xp->xp_context = EXPAND_NOTHING;
2028 }
2029
2030 return NULL;
2031}
2032#endif
2033
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002034#ifdef FEAT_EVAL
2035static enum
2036{
2037 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002038 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2039 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002040} breakpt_expand_what;
2041
2042/*
2043 * Set the completion context for the :breakadd command. Always returns NULL.
2044 */
2045 static char_u *
2046set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2047{
2048 char_u *p;
2049 char_u *subcmd_start;
2050
2051 xp->xp_context = EXPAND_BREAKPOINT;
2052 xp->xp_pattern = arg;
2053
2054 if (cmdidx == CMD_breakadd)
2055 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002056 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002057 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002058 else
2059 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002060
2061 p = skipwhite(arg);
2062 if (*p == NUL)
2063 return NULL;
2064 subcmd_start = p;
2065
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002066 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002067 {
2068 // :breakadd file [lnum] <filename>
2069 // :breakadd func [lnum] <funcname>
2070 p += 4;
2071 p = skipwhite(p);
2072
2073 // skip line number (if specified)
2074 if (VIM_ISDIGIT(*p))
2075 {
2076 p = skipdigits(p);
2077 if (*p != ' ')
2078 {
2079 xp->xp_context = EXPAND_NOTHING;
2080 return NULL;
2081 }
2082 p = skipwhite(p);
2083 }
2084 if (STRNCMP("file", subcmd_start, 4) == 0)
2085 xp->xp_context = EXPAND_FILES;
2086 else
2087 xp->xp_context = EXPAND_USER_FUNC;
2088 xp->xp_pattern = p;
2089 }
2090 else if (STRNCMP("expr ", p, 5) == 0)
2091 {
2092 // :breakadd expr <expression>
2093 xp->xp_context = EXPAND_EXPRESSION;
2094 xp->xp_pattern = skipwhite(p + 5);
2095 }
2096
2097 return NULL;
2098}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002099
2100 static char_u *
2101set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2102{
2103 char_u *p;
2104
2105 xp->xp_context = EXPAND_NOTHING;
2106 xp->xp_pattern = NULL;
2107
2108 p = skipwhite(arg);
2109 if (VIM_ISDIGIT(*p))
2110 return NULL;
2111
2112 xp->xp_context = EXPAND_SCRIPTNAMES;
2113 xp->xp_pattern = p;
2114
2115 return NULL;
2116}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002117#endif
2118
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002119/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002120 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2121 * The argument to the command is 'arg' and the argument flags is 'argt'.
2122 * For user-defined commands and for environment variables, 'compl' has the
2123 * completion type.
2124 * Returns a pointer to the next command. Returns NULL if there is no next
2125 * command.
2126 */
2127 static char_u *
2128set_context_by_cmdname(
2129 char_u *cmd,
2130 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002131 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002132 char_u *arg,
2133 long argt,
2134 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002135 int forceit)
2136{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002137 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002138 {
2139 case CMD_find:
2140 case CMD_sfind:
2141 case CMD_tabfind:
2142 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002143 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002144 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002145 break;
2146 case CMD_cd:
2147 case CMD_chdir:
2148 case CMD_tcd:
2149 case CMD_tchdir:
2150 case CMD_lcd:
2151 case CMD_lchdir:
2152 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002153 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002154 break;
2155 case CMD_help:
2156 xp->xp_context = EXPAND_HELP;
2157 xp->xp_pattern = arg;
2158 break;
2159
2160 // Command modifiers: return the argument.
2161 // Also for commands with an argument that is a command.
2162 case CMD_aboveleft:
2163 case CMD_argdo:
2164 case CMD_belowright:
2165 case CMD_botright:
2166 case CMD_browse:
2167 case CMD_bufdo:
2168 case CMD_cdo:
2169 case CMD_cfdo:
2170 case CMD_confirm:
2171 case CMD_debug:
2172 case CMD_folddoclosed:
2173 case CMD_folddoopen:
2174 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002175 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002176 case CMD_keepalt:
2177 case CMD_keepjumps:
2178 case CMD_keepmarks:
2179 case CMD_keeppatterns:
2180 case CMD_ldo:
2181 case CMD_leftabove:
2182 case CMD_lfdo:
2183 case CMD_lockmarks:
2184 case CMD_noautocmd:
2185 case CMD_noswapfile:
2186 case CMD_rightbelow:
2187 case CMD_sandbox:
2188 case CMD_silent:
2189 case CMD_tab:
2190 case CMD_tabdo:
2191 case CMD_topleft:
2192 case CMD_verbose:
2193 case CMD_vertical:
2194 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002195 case CMD_vim9cmd:
2196 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002197 return arg;
2198
2199 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002200 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002201
2202#ifdef FEAT_SEARCH_EXTRA
2203 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002204 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002205#endif
2206
2207 // All completion for the +cmdline_compl feature goes here.
2208
2209 case CMD_command:
2210 return set_context_in_user_cmd(xp, arg);
2211
2212 case CMD_delcommand:
2213 xp->xp_context = EXPAND_USER_COMMANDS;
2214 xp->xp_pattern = arg;
2215 break;
2216
2217 case CMD_global:
2218 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002219 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002220 case CMD_and:
2221 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002222 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002223 case CMD_isearch:
2224 case CMD_dsearch:
2225 case CMD_ilist:
2226 case CMD_dlist:
2227 case CMD_ijump:
2228 case CMD_psearch:
2229 case CMD_djump:
2230 case CMD_isplit:
2231 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002232 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002233 case CMD_autocmd:
2234 return set_context_in_autocmd(xp, arg, FALSE);
2235 case CMD_doautocmd:
2236 case CMD_doautoall:
2237 return set_context_in_autocmd(xp, arg, TRUE);
2238 case CMD_set:
2239 set_context_in_set_cmd(xp, arg, 0);
2240 break;
2241 case CMD_setglobal:
2242 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2243 break;
2244 case CMD_setlocal:
2245 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2246 break;
2247 case CMD_tag:
2248 case CMD_stag:
2249 case CMD_ptag:
2250 case CMD_ltag:
2251 case CMD_tselect:
2252 case CMD_stselect:
2253 case CMD_ptselect:
2254 case CMD_tjump:
2255 case CMD_stjump:
2256 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002257 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002258 xp->xp_context = EXPAND_TAGS_LISTFILES;
2259 else
2260 xp->xp_context = EXPAND_TAGS;
2261 xp->xp_pattern = arg;
2262 break;
2263 case CMD_augroup:
2264 xp->xp_context = EXPAND_AUGROUP;
2265 xp->xp_pattern = arg;
2266 break;
2267#ifdef FEAT_SYN_HL
2268 case CMD_syntax:
2269 set_context_in_syntax_cmd(xp, arg);
2270 break;
2271#endif
2272#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002273 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002274 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002275 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002276 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002277 case CMD_if:
2278 case CMD_elseif:
2279 case CMD_while:
2280 case CMD_for:
2281 case CMD_echo:
2282 case CMD_echon:
2283 case CMD_execute:
2284 case CMD_echomsg:
2285 case CMD_echoerr:
2286 case CMD_call:
2287 case CMD_return:
2288 case CMD_cexpr:
2289 case CMD_caddexpr:
2290 case CMD_cgetexpr:
2291 case CMD_lexpr:
2292 case CMD_laddexpr:
2293 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002294 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002295 break;
2296
2297 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002298 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002299 case CMD_function:
2300 case CMD_delfunction:
2301 xp->xp_context = EXPAND_USER_FUNC;
2302 xp->xp_pattern = arg;
2303 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002304 case CMD_disassemble:
2305 set_context_in_disassemble_cmd(xp, arg);
2306 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002307
2308 case CMD_echohl:
2309 set_context_in_echohl_cmd(xp, arg);
2310 break;
2311#endif
2312 case CMD_highlight:
2313 set_context_in_highlight_cmd(xp, arg);
2314 break;
2315#ifdef FEAT_CSCOPE
2316 case CMD_cscope:
2317 case CMD_lcscope:
2318 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002319 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002320 break;
2321#endif
2322#ifdef FEAT_SIGNS
2323 case CMD_sign:
2324 set_context_in_sign_cmd(xp, arg);
2325 break;
2326#endif
2327 case CMD_bdelete:
2328 case CMD_bwipeout:
2329 case CMD_bunload:
2330 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2331 arg = xp->xp_pattern + 1;
2332 // FALLTHROUGH
2333 case CMD_buffer:
2334 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002335 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002336 case CMD_checktime:
2337 xp->xp_context = EXPAND_BUFFERS;
2338 xp->xp_pattern = arg;
2339 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002340#ifdef FEAT_DIFF
2341 case CMD_diffget:
2342 case CMD_diffput:
2343 // If current buffer is in diff mode, complete buffer names
2344 // which are in diff mode, and different than current buffer.
2345 xp->xp_context = EXPAND_DIFF_BUFFERS;
2346 xp->xp_pattern = arg;
2347 break;
2348#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002349 case CMD_USER:
2350 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002351 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2352 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002353
2354 case CMD_map: case CMD_noremap:
2355 case CMD_nmap: case CMD_nnoremap:
2356 case CMD_vmap: case CMD_vnoremap:
2357 case CMD_omap: case CMD_onoremap:
2358 case CMD_imap: case CMD_inoremap:
2359 case CMD_cmap: case CMD_cnoremap:
2360 case CMD_lmap: case CMD_lnoremap:
2361 case CMD_smap: case CMD_snoremap:
2362 case CMD_tmap: case CMD_tnoremap:
2363 case CMD_xmap: case CMD_xnoremap:
2364 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002365 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002366 case CMD_unmap:
2367 case CMD_nunmap:
2368 case CMD_vunmap:
2369 case CMD_ounmap:
2370 case CMD_iunmap:
2371 case CMD_cunmap:
2372 case CMD_lunmap:
2373 case CMD_sunmap:
2374 case CMD_tunmap:
2375 case CMD_xunmap:
2376 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002377 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002378 case CMD_mapclear:
2379 case CMD_nmapclear:
2380 case CMD_vmapclear:
2381 case CMD_omapclear:
2382 case CMD_imapclear:
2383 case CMD_cmapclear:
2384 case CMD_lmapclear:
2385 case CMD_smapclear:
2386 case CMD_tmapclear:
2387 case CMD_xmapclear:
2388 xp->xp_context = EXPAND_MAPCLEAR;
2389 xp->xp_pattern = arg;
2390 break;
2391
2392 case CMD_abbreviate: case CMD_noreabbrev:
2393 case CMD_cabbrev: case CMD_cnoreabbrev:
2394 case CMD_iabbrev: case CMD_inoreabbrev:
2395 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002396 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002397 case CMD_unabbreviate:
2398 case CMD_cunabbrev:
2399 case CMD_iunabbrev:
2400 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002401 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002402#ifdef FEAT_MENU
2403 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2404 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2405 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2406 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2407 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2408 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2409 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2410 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2411 case CMD_tmenu: case CMD_tunmenu:
2412 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2413 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2414#endif
2415
2416 case CMD_colorscheme:
2417 xp->xp_context = EXPAND_COLORS;
2418 xp->xp_pattern = arg;
2419 break;
2420
2421 case CMD_compiler:
2422 xp->xp_context = EXPAND_COMPILER;
2423 xp->xp_pattern = arg;
2424 break;
2425
2426 case CMD_ownsyntax:
2427 xp->xp_context = EXPAND_OWNSYNTAX;
2428 xp->xp_pattern = arg;
2429 break;
2430
2431 case CMD_setfiletype:
2432 xp->xp_context = EXPAND_FILETYPE;
2433 xp->xp_pattern = arg;
2434 break;
2435
2436 case CMD_packadd:
2437 xp->xp_context = EXPAND_PACKADD;
2438 xp->xp_pattern = arg;
2439 break;
2440
zeertzjqb0d45ec2023-01-25 15:04:22 +00002441 case CMD_runtime:
2442 set_context_in_runtime_cmd(xp, arg);
2443 break;
2444
Bram Moolenaard0190392019-08-23 21:17:35 +02002445#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2446 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002447 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002448#endif
2449#if defined(FEAT_PROFILE)
2450 case CMD_profile:
2451 set_context_in_profile_cmd(xp, arg);
2452 break;
2453#endif
2454 case CMD_behave:
2455 xp->xp_context = EXPAND_BEHAVE;
2456 xp->xp_pattern = arg;
2457 break;
2458
2459 case CMD_messages:
2460 xp->xp_context = EXPAND_MESSAGES;
2461 xp->xp_pattern = arg;
2462 break;
2463
2464 case CMD_history:
2465 xp->xp_context = EXPAND_HISTORY;
2466 xp->xp_pattern = arg;
2467 break;
2468#if defined(FEAT_PROFILE)
2469 case CMD_syntime:
2470 xp->xp_context = EXPAND_SYNTIME;
2471 xp->xp_pattern = arg;
2472 break;
2473#endif
2474
2475 case CMD_argdelete:
2476 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2477 arg = xp->xp_pattern + 1;
2478 xp->xp_context = EXPAND_ARGLIST;
2479 xp->xp_pattern = arg;
2480 break;
2481
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002482#ifdef FEAT_EVAL
2483 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002484 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002485 case CMD_breakdel:
2486 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002487
2488 case CMD_scriptnames:
2489 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002490#endif
2491
Bram Moolenaard0190392019-08-23 21:17:35 +02002492 default:
2493 break;
2494 }
2495 return NULL;
2496}
2497
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002498/*
2499 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2500 * we don't need/want deleted. Maybe this could be done better if we didn't
2501 * repeat all this stuff. The only problem is that they may not stay
2502 * perfectly compatible with each other, but then the command line syntax
2503 * probably won't change that much -- webb.
2504 */
2505 static char_u *
2506set_one_cmd_context(
2507 expand_T *xp,
2508 char_u *buff) // buffer for command string
2509{
2510 char_u *p;
2511 char_u *cmd, *arg;
2512 int len = 0;
2513 exarg_T ea;
2514 int compl = EXPAND_NOTHING;
2515 int forceit = FALSE;
2516 int usefilter = FALSE; // filter instead of file name
2517
2518 ExpandInit(xp);
2519 xp->xp_pattern = buff;
2520 xp->xp_line = buff;
2521 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2522 ea.argt = 0;
2523
2524 // 1. skip comment lines and leading space, colons or bars
2525 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2526 ;
2527 xp->xp_pattern = cmd;
2528
2529 if (*cmd == NUL)
2530 return NULL;
2531 if (*cmd == '"') // ignore comment lines
2532 {
2533 xp->xp_context = EXPAND_NOTHING;
2534 return NULL;
2535 }
2536
2537 // 3. Skip over the range to find the command.
2538 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2539 xp->xp_pattern = cmd;
2540 if (*cmd == NUL)
2541 return NULL;
2542 if (*cmd == '"')
2543 {
2544 xp->xp_context = EXPAND_NOTHING;
2545 return NULL;
2546 }
2547
2548 if (*cmd == '|' || *cmd == '\n')
2549 return cmd + 1; // There's another command
2550
2551 // Get the command index.
2552 p = set_cmd_index(cmd, &ea, xp, &compl);
2553 if (p == NULL)
2554 return NULL;
2555
2556 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2557
2558 if (*p == '!') // forced commands
2559 {
2560 forceit = TRUE;
2561 ++p;
2562 }
2563
2564 // 6. parse arguments
2565 if (!IS_USER_CMDIDX(ea.cmdidx))
2566 ea.argt = excmd_get_argt(ea.cmdidx);
2567
2568 arg = skipwhite(p);
2569
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002570 // Does command allow "++argopt" argument?
2571 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002572 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002573 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2574 {
2575 p = arg + 2;
2576 while (*p && !vim_isspace(*p))
2577 MB_PTR_ADV(p);
2578
2579 // Still touching the command after "++"?
2580 if (*p == NUL)
2581 {
2582 if (ea.argt & EX_ARGOPT)
2583 return set_context_in_argopt(xp, arg + 2);
2584#ifdef FEAT_TERMINAL
2585 if (ea.cmdidx == CMD_terminal)
2586 return set_context_in_terminalopt(xp, arg + 2);
2587#endif
2588 }
2589
2590 arg = skipwhite(p);
2591 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002592 }
2593
2594 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2595 {
2596 if (*arg == '>') // append
2597 {
2598 if (*++arg == '>')
2599 ++arg;
2600 arg = skipwhite(arg);
2601 }
2602 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2603 {
2604 ++arg;
2605 usefilter = TRUE;
2606 }
2607 }
2608
2609 if (ea.cmdidx == CMD_read)
2610 {
2611 usefilter = forceit; // :r! filter if forced
2612 if (*arg == '!') // :r !filter
2613 {
2614 ++arg;
2615 usefilter = TRUE;
2616 }
2617 }
2618
2619 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2620 {
2621 while (*arg == *cmd) // allow any number of '>' or '<'
2622 ++arg;
2623 arg = skipwhite(arg);
2624 }
2625
2626 // Does command allow "+command"?
2627 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2628 {
2629 // Check if we're in the +command
2630 p = arg + 1;
2631 arg = skip_cmd_arg(arg, FALSE);
2632
2633 // Still touching the command after '+'?
2634 if (*arg == NUL)
2635 return p;
2636
2637 // Skip space(s) after +command to get to the real argument
2638 arg = skipwhite(arg);
2639 }
2640
2641
2642 // Check for '|' to separate commands and '"' to start comments.
2643 // Don't do this for ":read !cmd" and ":write !cmd".
2644 if ((ea.argt & EX_TRLBAR) && !usefilter)
2645 {
2646 p = arg;
2647 // ":redir @" is not the start of a comment
2648 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2649 p += 2;
2650 while (*p)
2651 {
2652 if (*p == Ctrl_V)
2653 {
2654 if (p[1] != NUL)
2655 ++p;
2656 }
2657 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2658 || *p == '|' || *p == '\n')
2659 {
2660 if (*(p - 1) != '\\')
2661 {
2662 if (*p == '|' || *p == '\n')
2663 return p + 1;
2664 return NULL; // It's a comment
2665 }
2666 }
2667 MB_PTR_ADV(p);
2668 }
2669 }
2670
2671 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2672 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2673 // no arguments allowed but there is something
2674 return NULL;
2675
2676 // Find start of last argument (argument just before cursor):
2677 p = buff;
2678 xp->xp_pattern = p;
2679 len = (int)STRLEN(buff);
2680 while (*p && p < buff + len)
2681 {
2682 if (*p == ' ' || *p == TAB)
2683 {
2684 // argument starts after a space
2685 xp->xp_pattern = ++p;
2686 }
2687 else
2688 {
2689 if (*p == '\\' && *(p + 1) != NUL)
2690 ++p; // skip over escaped character
2691 MB_PTR_ADV(p);
2692 }
2693 }
2694
2695 if (ea.argt & EX_XFILE)
2696 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2697
2698 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002699 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002700 forceit);
2701}
2702
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002703/*
2704 * Set the completion context in 'xp' for command 'str'
2705 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002706 void
2707set_cmd_context(
2708 expand_T *xp,
2709 char_u *str, // start of command line
2710 int len, // length of command line (excl. NUL)
2711 int col, // position of cursor
2712 int use_ccline UNUSED) // use ccline for info
2713{
2714#ifdef FEAT_EVAL
2715 cmdline_info_T *ccline = get_cmdline_info();
2716#endif
2717 int old_char = NUL;
2718 char_u *nextcomm;
2719
2720 // Avoid a UMR warning from Purify, only save the character if it has been
2721 // written before.
2722 if (col < len)
2723 old_char = str[col];
2724 str[col] = NUL;
2725 nextcomm = str;
2726
2727#ifdef FEAT_EVAL
2728 if (use_ccline && ccline->cmdfirstc == '=')
2729 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002730 // pass CMD_SIZE because there is no real command
2731 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002732 }
2733 else if (use_ccline && ccline->input_fn)
2734 {
2735 xp->xp_context = ccline->xp_context;
2736 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002738 }
2739 else
2740#endif
2741 while (nextcomm != NULL)
2742 nextcomm = set_one_cmd_context(xp, nextcomm);
2743
2744 // Store the string here so that call_user_expand_func() can get to them
2745 // easily.
2746 xp->xp_line = str;
2747 xp->xp_col = col;
2748
2749 str[col] = old_char;
2750}
2751
2752/*
2753 * Expand the command line "str" from context "xp".
2754 * "xp" must have been set by set_cmd_context().
2755 * xp->xp_pattern points into "str", to where the text that is to be expanded
2756 * starts.
2757 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2758 * cursor.
2759 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2760 * key that triggered expansion literally.
2761 * Returns EXPAND_OK otherwise.
2762 */
2763 int
2764expand_cmdline(
2765 expand_T *xp,
2766 char_u *str, // start of command line
2767 int col, // position of cursor
2768 int *matchcount, // return: nr of matches
2769 char_u ***matches) // return: array of pointers to matches
2770{
2771 char_u *file_str = NULL;
2772 int options = WILD_ADD_SLASH|WILD_SILENT;
2773
2774 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2775 {
2776 beep_flush();
2777 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2778 }
2779 if (xp->xp_context == EXPAND_NOTHING)
2780 {
2781 // Caller can use the character as a normal char instead
2782 return EXPAND_NOTHING;
2783 }
2784
2785 // add star to file name, or convert to regexp if not exp. files.
2786 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002787 if (cmdline_fuzzy_completion_supported(xp))
2788 // If fuzzy matching, don't modify the search string
2789 file_str = vim_strsave(xp->xp_pattern);
2790 else
2791 {
2792 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2793 if (file_str == NULL)
2794 return EXPAND_UNSUCCESSFUL;
2795 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002796
2797 if (p_wic)
2798 options += WILD_ICASE;
2799
2800 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002801 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002802 {
2803 *matchcount = 0;
2804 *matches = NULL;
2805 }
2806 vim_free(file_str);
2807
2808 return EXPAND_OK;
2809}
2810
Bram Moolenaar66b51422019-08-18 21:44:12 +02002811/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002812 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002813 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002814 */
2815 static int
2816expand_files_and_dirs(
2817 expand_T *xp,
2818 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002819 char_u ***matches,
2820 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002821 int flags,
2822 int options)
2823{
2824 int free_pat = FALSE;
2825 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002826 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002827
2828 // for ":set path=" and ":set tags=" halve backslashes for escaped
2829 // space
2830 if (xp->xp_backslash != XP_BS_NONE)
2831 {
2832 free_pat = TRUE;
2833 pat = vim_strsave(pat);
2834 for (i = 0; pat[i]; ++i)
2835 if (pat[i] == '\\')
2836 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002837 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002838 && pat[i + 1] == '\\'
2839 && pat[i + 2] == '\\'
2840 && pat[i + 3] == ' ')
2841 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002842 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002843 && pat[i + 1] == ' ')
2844 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002845 else if ((xp->xp_backslash & XP_BS_COMMA)
2846 && pat[i + 1] == '\\'
2847 && pat[i + 2] == ',')
2848 STRMOVE(pat + i, pat + i + 2);
2849#ifdef BACKSLASH_IN_FILENAME
2850 else if ((xp->xp_backslash & XP_BS_COMMA)
2851 && pat[i + 1] == ',')
2852 STRMOVE(pat + i, pat + i + 1);
2853#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002854 }
2855 }
2856
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002857 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002858 {
2859#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002860 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002861#endif
2862 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002863 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002864 {
2865 if (xp->xp_context == EXPAND_FILES)
2866 flags |= EW_FILE;
2867 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2868 flags |= (EW_FILE | EW_PATH);
2869 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2870 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2871 else
2872 flags = (flags | EW_DIR) & ~EW_FILE;
2873 if (options & WILD_ICASE)
2874 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002876 // Expand wildcards, supporting %:h and the like.
2877 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2878 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002879 if (free_pat)
2880 vim_free(pat);
2881#ifdef BACKSLASH_IN_FILENAME
2882 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2883 {
2884 int j;
2885
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002886 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002888 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002889
2890 while (*ptr != NUL)
2891 {
2892 if (p_csl[0] == 's' && *ptr == '\\')
2893 *ptr = '/';
2894 else if (p_csl[0] == 'b' && *ptr == '/')
2895 *ptr = '\\';
2896 ptr += (*mb_ptr2len)(ptr);
2897 }
2898 }
2899 }
2900#endif
2901 return ret;
2902}
2903
2904/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002905 * Function given to ExpandGeneric() to obtain the possible arguments of the
2906 * ":behave {mswin,xterm}" command.
2907 */
2908 static char_u *
2909get_behave_arg(expand_T *xp UNUSED, int idx)
2910{
2911 if (idx == 0)
2912 return (char_u *)"mswin";
2913 if (idx == 1)
2914 return (char_u *)"xterm";
2915 return NULL;
2916}
2917
K.Takata161b6ac2022-11-14 15:31:07 +00002918#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002919/*
2920 * Function given to ExpandGeneric() to obtain the possible arguments of the
2921 * ":breakadd {expr, file, func, here}" command.
2922 * ":breakdel {func, file, here}" command.
2923 */
2924 static char_u *
2925get_breakadd_arg(expand_T *xp UNUSED, int idx)
2926{
2927 char *opts[] = {"expr", "file", "func", "here"};
2928
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002929 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002930 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002931 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002932 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2933 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002934 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2935 {
2936 // breakdel {func, file, here}
2937 if (idx <= 2)
2938 return (char_u *)opts[idx + 1];
2939 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002940 else
2941 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002942 // profdel {func, file}
2943 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002944 return (char_u *)opts[idx + 1];
2945 }
2946 }
2947 return NULL;
2948}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002949
2950/*
2951 * Function given to ExpandGeneric() to obtain the possible arguments for the
2952 * ":scriptnames" command.
2953 */
2954 static char_u *
2955get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2956{
2957 scriptitem_T *si;
2958
2959 if (!SCRIPT_ID_VALID(idx + 1))
2960 return NULL;
2961
2962 si = SCRIPT_ITEM(idx + 1);
2963 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2964 return NameBuff;
2965}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002966#endif
2967
Bram Moolenaard0190392019-08-23 21:17:35 +02002968/*
2969 * Function given to ExpandGeneric() to obtain the possible arguments of the
2970 * ":messages {clear}" command.
2971 */
2972 static char_u *
2973get_messages_arg(expand_T *xp UNUSED, int idx)
2974{
2975 if (idx == 0)
2976 return (char_u *)"clear";
2977 return NULL;
2978}
2979
2980 static char_u *
2981get_mapclear_arg(expand_T *xp UNUSED, int idx)
2982{
2983 if (idx == 0)
2984 return (char_u *)"<buffer>";
2985 return NULL;
2986}
2987
2988/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002989 * Do the expansion based on xp->xp_context and 'rmp'.
2990 */
2991 static int
2992ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002993 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002994 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002995 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002996 char_u ***matches,
2997 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002998{
2999 static struct expgen
3000 {
3001 int context;
3002 char_u *((*func)(expand_T *, int));
3003 int ic;
3004 int escaped;
3005 } tab[] =
3006 {
3007 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3008 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3009 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3010 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3011 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3012 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3013 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3014 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3015 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3016 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003017#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003018 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3019 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3020 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3021 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3022 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003023#endif
3024#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3026 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003027#endif
3028#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003029 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003030#endif
3031#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003032 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003033#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003034 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3035 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3036 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003037#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003038 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003039#endif
3040#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003041 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003042#endif
3043#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003044 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003045#endif
3046#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003047 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3048 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003049#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003050 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3051 {EXPAND_USER, get_users, TRUE, FALSE},
3052 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003053#ifdef FEAT_EVAL
3054 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003055 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003056#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003057 };
3058 int i;
3059 int ret = FAIL;
3060
3061 // Find a context in the table and call the ExpandGeneric() with the
3062 // right function to do the expansion.
3063 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3064 {
3065 if (xp->xp_context == tab[i].context)
3066 {
3067 if (tab[i].ic)
3068 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003069 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3070 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003071 break;
3072 }
3073 }
3074
3075 return ret;
3076}
3077
3078/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003079 * Map wild expand options to flags for expand_wildcards()
3080 */
3081 static int
3082map_wildopts_to_ewflags(int options)
3083{
3084 int flags;
3085
3086 flags = EW_DIR; // include directories
3087 if (options & WILD_LIST_NOTFOUND)
3088 flags |= EW_NOTFOUND;
3089 if (options & WILD_ADD_SLASH)
3090 flags |= EW_ADDSLASH;
3091 if (options & WILD_KEEP_ALL)
3092 flags |= EW_KEEPALL;
3093 if (options & WILD_SILENT)
3094 flags |= EW_SILENT;
3095 if (options & WILD_NOERROR)
3096 flags |= EW_NOERROR;
3097 if (options & WILD_ALLLINKS)
3098 flags |= EW_ALLLINKS;
3099
3100 return flags;
3101}
3102
3103/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003104 * Do the expansion based on xp->xp_context and "pat".
3105 */
3106 static int
3107ExpandFromContext(
3108 expand_T *xp,
3109 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003110 char_u ***matches,
3111 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112 int options) // WILD_ flags
3113{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115 int ret;
3116 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003117 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003118 int fuzzy = cmdline_fuzzy_complete(pat)
3119 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003121 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003122
3123 if (xp->xp_context == EXPAND_FILES
3124 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003125 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003126 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003127 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003128 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3129 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003131 *matches = (char_u **)"";
3132 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003133 if (xp->xp_context == EXPAND_HELP)
3134 {
3135 // With an empty argument we would get all the help tags, which is
3136 // very slow. Get matches for "help" instead.
3137 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003138 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 {
3140#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003141 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003142#endif
3143 return OK;
3144 }
3145 return FAIL;
3146 }
3147
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003149 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003151 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003152 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003153 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003154#ifdef FEAT_DIFF
3155 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003156 return ExpandBufnames(pat, numMatches, matches,
3157 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003158#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003159 if (xp->xp_context == EXPAND_TAGS
3160 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003161 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3162 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003163 if (xp->xp_context == EXPAND_COLORS)
3164 {
3165 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003166 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003167 directories);
3168 }
3169 if (xp->xp_context == EXPAND_COMPILER)
3170 {
3171 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003172 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003173 }
3174 if (xp->xp_context == EXPAND_OWNSYNTAX)
3175 {
3176 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003177 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003178 }
3179 if (xp->xp_context == EXPAND_FILETYPE)
3180 {
3181 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003182 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003183 }
Doug Kearns81642d92024-01-04 22:37:44 +01003184#ifdef FEAT_KEYMAP
3185 if (xp->xp_context == EXPAND_KEYMAP)
3186 {
3187 char *directories[] = {"keymap", NULL};
3188 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3189 }
3190#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003191#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003192 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003193 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003194#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003195 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003196 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003197 if (xp->xp_context == EXPAND_RUNTIME)
3198 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003199
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003200 // When expanding a function name starting with s:, match the <SNR>nr_
3201 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003202 if ((xp->xp_context == EXPAND_USER_FUNC
3203 || xp->xp_context == EXPAND_DISASSEMBLE)
3204 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003205 {
3206 int len = (int)STRLEN(pat) + 20;
3207
3208 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003209 if (tofree == NULL)
3210 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003211 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003212 pat = tofree;
3213 }
3214
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003215 if (!fuzzy)
3216 {
3217 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3218 if (regmatch.regprog == NULL)
3219 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003220
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003221 // set ignore-case according to p_ic, p_scs and pat
3222 regmatch.rm_ic = ignorecase(pat);
3223 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003224
3225 if (xp->xp_context == EXPAND_SETTINGS
3226 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003227 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003228 else if (xp->xp_context == EXPAND_STRING_SETTING)
3229 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3230 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3231 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003233 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003234 else if (xp->xp_context == EXPAND_ARGOPT)
3235 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3236#if defined(FEAT_TERMINAL)
3237 else if (xp->xp_context == EXPAND_TERMINALOPT)
3238 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3239#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003240#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003242 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003243#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003245 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003247 if (!fuzzy)
3248 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003249 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250
3251 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252}
3253
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254/*
3255 * Expand a list of names.
3256 *
3257 * Generic function for command line completion. It calls a function to
3258 * obtain strings, one by one. The strings are matched against a regexp
3259 * program. Matching strings are copied into an array, which is returned.
3260 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003261 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3262 * is used.
3263 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264 * Returns OK when no problems encountered, FAIL for error (out of memory).
3265 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003266 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003268 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003269 expand_T *xp,
3270 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003271 char_u ***matches,
3272 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273 char_u *((*func)(expand_T *, int)),
3274 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003275 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003276{
3277 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003278 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003279 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003280 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003281 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003282 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003283 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003284 int sort_matches = FALSE;
3285 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003286
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003287 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003288 *matches = NULL;
3289 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003290
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003291 if (!fuzzy)
3292 ga_init2(&ga, sizeof(char *), 30);
3293 else
3294 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3295
3296 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003297 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003298 str = (*func)(xp, i);
3299 if (str == NULL) // end of list
3300 break;
3301 if (*str == NUL) // skip empty strings
3302 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003303
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003304 if (xp->xp_pattern[0] != NUL)
3305 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003306 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003307 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003308 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003310 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003311 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003312 }
3313 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003314 else
3315 match = TRUE;
3316
3317 if (!match)
3318 continue;
3319
3320 if (escaped)
3321 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3322 else
3323 str = vim_strsave(str);
3324 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003325 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003326 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003327 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003328 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003329 return FAIL;
3330 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003331 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003332 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003334
3335 if (ga_grow(&ga, 1) == FAIL)
3336 {
3337 vim_free(str);
3338 break;
3339 }
3340
3341 if (fuzzy)
3342 {
3343 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3344 fuzmatch->idx = ga.ga_len;
3345 fuzmatch->str = str;
3346 fuzmatch->score = score;
3347 }
3348 else
3349 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3350
K.Takata161b6ac2022-11-14 15:31:07 +00003351#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003352 if (func == get_menu_names)
3353 {
3354 // test for separator added by get_menu_names()
3355 str += STRLEN(str) - 1;
3356 if (*str == '\001')
3357 *str = '.';
3358 }
K.Takata161b6ac2022-11-14 15:31:07 +00003359#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003360
3361 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003362 }
3363
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003364 if (ga.ga_len == 0)
3365 return OK;
3366
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003367 // sort the matches when using regular expression matching and sorting
3368 // applies to the completion context. Menus and scriptnames should be kept
3369 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003370 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003371 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003372 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003373 && xp->xp_context != EXPAND_SCRIPTNAMES
3374 && xp->xp_context != EXPAND_ARGOPT
3375 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003376 sort_matches = TRUE;
3377
3378 // <SNR> functions should be sorted to the end.
3379 if (xp->xp_context == EXPAND_EXPRESSION
3380 || xp->xp_context == EXPAND_FUNCTIONS
3381 || xp->xp_context == EXPAND_USER_FUNC
3382 || xp->xp_context == EXPAND_DISASSEMBLE)
3383 funcsort = TRUE;
3384
3385 // Sort the matches.
3386 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003387 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003388 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003389 // <SNR> functions should be sorted to the end.
3390 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3391 sort_func_compare);
3392 else
3393 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3394 }
3395
3396 if (!fuzzy)
3397 {
3398 *matches = ga.ga_data;
3399 *numMatches = ga.ga_len;
3400 }
3401 else
3402 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003403 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3404 funcsort) == FAIL)
3405 return FAIL;
3406 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003407 }
3408
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003409#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003410 // Reset the variables used for special highlight names expansion, so that
3411 // they don't show up when getting normal highlight names by ID.
3412 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003413#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003414
Bram Moolenaar66b51422019-08-18 21:44:12 +02003415 return OK;
3416}
3417
3418/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003419 * Expand shell command matches in one directory of $PATH.
3420 */
3421 static void
3422expand_shellcmd_onedir(
3423 char_u *buf,
3424 char_u *s,
3425 size_t l,
3426 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003427 char_u ***matches,
3428 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003429 int flags,
3430 hashtab_T *ht,
3431 garray_T *gap)
3432{
3433 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003434 hash_T hash;
3435 hashitem_T *hi;
3436
3437 vim_strncpy(buf, s, l);
3438 add_pathsep(buf);
3439 l = STRLEN(buf);
3440 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3441
3442 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003443 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003444 if (ret != OK)
3445 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003446
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003447 if (ga_grow(gap, *numMatches) == FAIL)
3448 {
3449 FreeWild(*numMatches, *matches);
3450 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003451 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003452
3453 for (int i = 0; i < *numMatches; ++i)
3454 {
3455 char_u *name = (*matches)[i];
3456
3457 if (STRLEN(name) > l)
3458 {
3459 // Check if this name was already found.
3460 hash = hash_hash(name + l);
3461 hi = hash_lookup(ht, name + l, hash);
3462 if (HASHITEM_EMPTY(hi))
3463 {
3464 // Remove the path that was prepended.
3465 STRMOVE(name, name + l);
3466 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3467 hash_add_item(ht, hi, name, hash);
3468 name = NULL;
3469 }
3470 }
3471 vim_free(name);
3472 }
3473 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003474}
3475
3476/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003477 * Complete a shell command.
3478 * Returns FAIL or OK;
3479 */
3480 static int
3481expand_shellcmd(
3482 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003483 char_u ***matches, // return: array with matches
3484 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485 int flagsarg) // EW_ flags
3486{
3487 char_u *pat;
3488 int i;
3489 char_u *path = NULL;
3490 int mustfree = FALSE;
3491 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003492 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493 size_t l;
3494 char_u *s, *e;
3495 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003496 int did_curdir = FALSE;
3497 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003498
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003499 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003500 if (buf == NULL)
3501 return FAIL;
3502
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003503 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003504 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003505 if (pat == NULL)
3506 {
3507 vim_free(buf);
3508 return FAIL;
3509 }
3510
Bram Moolenaar66b51422019-08-18 21:44:12 +02003511 for (i = 0; pat[i]; ++i)
3512 if (pat[i] == '\\' && pat[i + 1] == ' ')
3513 STRMOVE(pat + i, pat + i + 1);
3514
3515 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3516
3517 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3518 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3519 path = (char_u *)".";
3520 else
3521 {
3522 // For an absolute name we don't use $PATH.
3523 if (!mch_isFullName(pat))
3524 path = vim_getenv((char_u *)"PATH", &mustfree);
3525 if (path == NULL)
3526 path = (char_u *)"";
3527 }
3528
3529 // Go over all directories in $PATH. Expand matches in that directory and
3530 // collect them in "ga". When "." is not in $PATH also expand for the
3531 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003532 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003533 hash_init(&found_ht);
3534 for (s = path; ; s = e)
3535 {
K.Takata161b6ac2022-11-14 15:31:07 +00003536#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003537 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003538#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003539 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003540#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003541 if (e == NULL)
3542 e = s + STRLEN(s);
3543
3544 if (*s == NUL)
3545 {
3546 if (did_curdir)
3547 break;
3548 // Find directories in the current directory, path is empty.
3549 did_curdir = TRUE;
3550 flags |= EW_DIR;
3551 }
3552 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3553 {
3554 did_curdir = TRUE;
3555 flags |= EW_DIR;
3556 }
3557 else
3558 // Do not match directories inside a $PATH item.
3559 flags &= ~EW_DIR;
3560
3561 l = e - s;
3562 if (l > MAXPATHL - 5)
3563 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003564
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003565 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003566 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568 if (*e != NUL)
3569 ++e;
3570 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003571 *matches = ga.ga_data;
3572 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003573
3574 vim_free(buf);
3575 vim_free(pat);
3576 if (mustfree)
3577 vim_free(path);
3578 hash_clear(&found_ht);
3579 return OK;
3580}
3581
K.Takata161b6ac2022-11-14 15:31:07 +00003582#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003583/*
3584 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003585 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003586 */
3587 static void *
3588call_user_expand_func(
3589 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003590 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591{
3592 cmdline_info_T *ccline = get_cmdline_info();
3593 int keep = 0;
3594 typval_T args[4];
3595 sctx_T save_current_sctx = current_sctx;
3596 char_u *pat = NULL;
3597 void *ret;
3598
3599 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3600 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003601
3602 if (ccline->cmdbuff != NULL)
3603 {
3604 keep = ccline->cmdbuff[ccline->cmdlen];
3605 ccline->cmdbuff[ccline->cmdlen] = 0;
3606 }
3607
3608 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3609
3610 args[0].v_type = VAR_STRING;
3611 args[0].vval.v_string = pat;
3612 args[1].v_type = VAR_STRING;
3613 args[1].vval.v_string = xp->xp_line;
3614 args[2].v_type = VAR_NUMBER;
3615 args[2].vval.v_number = xp->xp_col;
3616 args[3].v_type = VAR_UNKNOWN;
3617
3618 current_sctx = xp->xp_script_ctx;
3619
3620 ret = user_expand_func(xp->xp_arg, 3, args);
3621
3622 current_sctx = save_current_sctx;
3623 if (ccline->cmdbuff != NULL)
3624 ccline->cmdbuff[ccline->cmdlen] = keep;
3625
3626 vim_free(pat);
3627 return ret;
3628}
3629
3630/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003631 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3632 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003633 */
3634 static int
3635ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003636 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003637 expand_T *xp,
3638 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003639 char_u ***matches,
3640 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003641{
3642 char_u *retstr;
3643 char_u *s;
3644 char_u *e;
3645 int keep;
3646 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003647 int fuzzy;
3648 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003649 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003650
3651 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003652 *matches = NULL;
3653 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003654
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003655 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003656 if (retstr == NULL)
3657 return FAIL;
3658
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003659 if (!fuzzy)
3660 ga_init2(&ga, sizeof(char *), 3);
3661 else
3662 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3663
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664 for (s = retstr; *s != NUL; s = e)
3665 {
3666 e = vim_strchr(s, '\n');
3667 if (e == NULL)
3668 e = s + STRLEN(s);
3669 keep = *e;
3670 *e = NUL;
3671
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003672 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003673 {
3674 if (!fuzzy)
3675 match = vim_regexec(regmatch, s, (colnr_T)0);
3676 else
3677 {
3678 score = fuzzy_match_str(s, pat);
3679 match = (score != 0);
3680 }
3681 }
3682 else
3683 match = TRUE; // match everything
3684
Bram Moolenaar66b51422019-08-18 21:44:12 +02003685 *e = keep;
3686
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003687 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 {
3689 if (ga_grow(&ga, 1) == FAIL)
3690 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003691 if (!fuzzy)
3692 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3693 else
3694 {
3695 fuzmatch_str_T *fuzmatch =
3696 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003697 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003698 fuzmatch->str = vim_strnsave(s, e - s);
3699 fuzmatch->score = score;
3700 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003701 ++ga.ga_len;
3702 }
3703
3704 if (*e != NUL)
3705 ++e;
3706 }
3707 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003708
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003709 if (ga.ga_len == 0)
3710 return OK;
3711
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003712 if (!fuzzy)
3713 {
3714 *matches = ga.ga_data;
3715 *numMatches = ga.ga_len;
3716 }
3717 else
3718 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003719 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3720 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003721 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003722 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003723 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003724 return OK;
3725}
3726
3727/*
3728 * Expand names with a list returned by a function defined by the user.
3729 */
3730 static int
3731ExpandUserList(
3732 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003733 char_u ***matches,
3734 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735{
3736 list_T *retlist;
3737 listitem_T *li;
3738 garray_T ga;
3739
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003740 *matches = NULL;
3741 *numMatches = 0;
3742 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003743 if (retlist == NULL)
3744 return FAIL;
3745
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003746 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003747 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003748 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749 {
3750 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3751 continue; // Skip non-string items and empty strings
3752
3753 if (ga_grow(&ga, 1) == FAIL)
3754 break;
3755
3756 ((char_u **)ga.ga_data)[ga.ga_len] =
3757 vim_strsave(li->li_tv.vval.v_string);
3758 ++ga.ga_len;
3759 }
3760 list_unref(retlist);
3761
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003762 *matches = ga.ga_data;
3763 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003764 return OK;
3765}
K.Takata161b6ac2022-11-14 15:31:07 +00003766#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003767
3768/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003769 * Expand "file" for all comma-separated directories in "path".
3770 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003771 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003772 */
3773 void
3774globpath(
3775 char_u *path,
3776 char_u *file,
3777 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003778 int expand_options,
3779 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003780{
3781 expand_T xpc;
3782 char_u *buf;
3783 int i;
3784 int num_p;
3785 char_u **p;
3786
3787 buf = alloc(MAXPATHL);
3788 if (buf == NULL)
3789 return;
3790
3791 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003792 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003793
3794 // Loop over all entries in {path}.
3795 while (*path != NUL)
3796 {
3797 // Copy one item of the path to buf[] and concatenate the file name.
3798 copy_option_part(&path, buf, MAXPATHL, ",");
3799 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3800 {
K.Takata161b6ac2022-11-14 15:31:07 +00003801#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802 // Using the platform's path separator (\) makes vim incorrectly
3803 // treat it as an escape character, use '/' instead.
3804 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3805 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003806#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003808#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003809 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003810 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3812 {
3813 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3814
3815 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003816 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003817 for (i = 0; i < num_p; ++i)
3818 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003819 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003820 ++ga->ga_len;
3821 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003822 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003823 }
3824 }
3825 }
3826
3827 vim_free(buf);
3828}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003829
Bram Moolenaareadee482020-09-04 15:37:31 +02003830/*
3831 * Translate some keys pressed when 'wildmenu' is used.
3832 */
3833 int
3834wildmenu_translate_key(
3835 cmdline_info_T *cclp,
3836 int key,
3837 expand_T *xp,
3838 int did_wild_list)
3839{
3840 int c = key;
3841
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003842 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003843 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003844 // When the popup menu is used for cmdline completion:
3845 // Up : go to the previous item in the menu
3846 // Down : go to the next item in the menu
3847 // Left : go to the parent directory
3848 // Right: list the files in the selected directory
3849 switch (c)
3850 {
3851 case K_UP: c = K_LEFT; break;
3852 case K_DOWN: c = K_RIGHT; break;
3853 case K_LEFT: c = K_UP; break;
3854 case K_RIGHT: c = K_DOWN; break;
3855 default: break;
3856 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003857 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003858
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003859 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003860 {
3861 if (c == K_LEFT)
3862 c = Ctrl_P;
3863 else if (c == K_RIGHT)
3864 c = Ctrl_N;
3865 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003866
Bram Moolenaareadee482020-09-04 15:37:31 +02003867 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003868 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003869 && cclp->cmdpos > 1
3870 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3871 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3872 && (c == '\n' || c == '\r' || c == K_KENTER))
3873 c = K_DOWN;
3874
3875 return c;
3876}
3877
3878/*
3879 * Delete characters on the command line, from "from" to the current
3880 * position.
3881 */
3882 static void
3883cmdline_del(cmdline_info_T *cclp, int from)
3884{
3885 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3886 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3887 cclp->cmdlen -= cclp->cmdpos - from;
3888 cclp->cmdpos = from;
3889}
3890
3891/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003892 * Handle a key pressed when the wild menu for the menu names
3893 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003894 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003895 static int
3896wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003897{
Bram Moolenaareadee482020-09-04 15:37:31 +02003898 int i;
3899 int j;
3900
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003901 // Hitting <Down> after "emenu Name.": complete submenu
3902 if (key == K_DOWN && cclp->cmdpos > 0
3903 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003904 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003905 key = p_wc;
3906 KeyTyped = TRUE; // in case the key was mapped
3907 }
3908 else if (key == K_UP)
3909 {
3910 // Hitting <Up>: Remove one submenu name in front of the
3911 // cursor
3912 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003913
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003914 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3915 i = 0;
3916 while (--j > 0)
3917 {
3918 // check for start of menu name
3919 if (cclp->cmdbuff[j] == ' '
3920 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003921 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003922 i = j + 1;
3923 break;
3924 }
3925 // check for start of submenu name
3926 if (cclp->cmdbuff[j] == '.'
3927 && cclp->cmdbuff[j - 1] != '\\')
3928 {
3929 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003930 {
3931 i = j + 1;
3932 break;
3933 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003934 else
3935 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003936 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003937 }
3938 if (i > 0)
3939 cmdline_del(cclp, i);
3940 key = p_wc;
3941 KeyTyped = TRUE; // in case the key was mapped
3942 xp->xp_context = EXPAND_NOTHING;
3943 }
3944
3945 return key;
3946}
3947
3948/*
3949 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3950 * directory names (EXPAND_DIRECTORIES) or shell command names
3951 * (EXPAND_SHELLCMD) is displayed.
3952 */
3953 static int
3954wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3955{
3956 int i;
3957 int j;
3958 char_u upseg[5];
3959
3960 upseg[0] = PATHSEP;
3961 upseg[1] = '.';
3962 upseg[2] = '.';
3963 upseg[3] = PATHSEP;
3964 upseg[4] = NUL;
3965
3966 if (key == K_DOWN
3967 && cclp->cmdpos > 0
3968 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3969 && (cclp->cmdpos < 3
3970 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3971 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3972 {
3973 // go down a directory
3974 key = p_wc;
3975 KeyTyped = TRUE; // in case the key was mapped
3976 }
3977 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3978 {
3979 // If in a direct ancestor, strip off one ../ to go down
3980 int found = FALSE;
3981
3982 j = cclp->cmdpos;
3983 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3984 while (--j > i)
3985 {
3986 if (has_mbyte)
3987 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3988 if (vim_ispathsep(cclp->cmdbuff[j]))
3989 {
3990 found = TRUE;
3991 break;
3992 }
3993 }
3994 if (found
3995 && cclp->cmdbuff[j - 1] == '.'
3996 && cclp->cmdbuff[j - 2] == '.'
3997 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3998 {
3999 cmdline_del(cclp, j - 2);
4000 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004001 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004002 }
4003 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004004 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004005 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004006 // go up a directory
4007 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004008
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004009 j = cclp->cmdpos - 1;
4010 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4011 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004012 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004013 if (has_mbyte)
4014 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4015 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004016#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004017 && vim_strchr((char_u *)" *?[{`$%#",
4018 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004019#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004020 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004021 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004022 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004023 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004024 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004025 break;
4026 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004027 else
4028 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004029 }
4030 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004031
4032 if (!found)
4033 j = i;
4034 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4035 j += 4;
4036 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4037 && j == i)
4038 j += 3;
4039 else
4040 j = 0;
4041 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004042 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004043 // TODO this is only for DOS/UNIX systems - need to put in
4044 // machine-specific stuff here and in upseg init
4045 cmdline_del(cclp, j);
4046 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004047 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004048 else if (cclp->cmdpos > i)
4049 cmdline_del(cclp, i);
4050
4051 // Now complete in the new directory. Set KeyTyped in case the
4052 // Up key came from a mapping.
4053 key = p_wc;
4054 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004055 }
4056
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004057 return key;
4058}
4059
4060/*
4061 * Handle a key pressed when the wild menu is displayed
4062 */
4063 int
4064wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4065{
4066 if (xp->xp_context == EXPAND_MENUNAMES)
4067 return wildmenu_process_key_menunames(cclp, key, xp);
4068 else if ((xp->xp_context == EXPAND_FILES
4069 || xp->xp_context == EXPAND_DIRECTORIES
4070 || xp->xp_context == EXPAND_SHELLCMD))
4071 return wildmenu_process_key_filenames(cclp, key, xp);
4072
4073 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004074}
4075
4076/*
4077 * Free expanded names when finished walking through the matches
4078 */
4079 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004080wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004081{
4082 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004083
4084 if (!p_wmnu || wild_menu_showing == 0)
4085 return;
4086
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004087#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004088 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004089 if (cclp->input_fn)
4090 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004091#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004092
4093 if (wild_menu_showing == WM_SCROLLED)
4094 {
4095 // Entered command line, move it up
4096 cmdline_row--;
4097 redrawcmd();
4098 }
4099 else if (save_p_ls != -1)
4100 {
4101 // restore 'laststatus' and 'winminheight'
4102 p_ls = save_p_ls;
4103 p_wmh = save_p_wmh;
4104 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004105 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004106 redrawcmd();
4107 save_p_ls = -1;
4108 }
4109 else
4110 {
4111 win_redraw_last_status(topframe);
4112 redraw_statuslines();
4113 }
4114 KeyTyped = skt;
4115 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004116#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004117 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004118 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004119#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004120}
Bram Moolenaareadee482020-09-04 15:37:31 +02004121
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004122#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004123/*
4124 * "getcompletion()" function
4125 */
4126 void
4127f_getcompletion(typval_T *argvars, typval_T *rettv)
4128{
4129 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004130 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004131 expand_T xpc;
4132 int filtered = FALSE;
4133 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004134 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004135
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004136 if (in_vim9script()
4137 && (check_for_string_arg(argvars, 0) == FAIL
4138 || check_for_string_arg(argvars, 1) == FAIL
4139 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4140 return;
4141
ii144785fe02021-11-21 12:13:56 +00004142 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004143 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004144 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004145 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004146
4147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004148 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004149
4150 if (p_wic)
4151 options |= WILD_ICASE;
4152
4153 // For filtered results, 'wildignore' is used
4154 if (!filtered)
4155 options |= WILD_KEEP_ALL;
4156
4157 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004158 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004159 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004160 int cmdline_len = (int)STRLEN(pat);
4161 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004162 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004163 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004164 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004165 else
4166 {
ii144785fe02021-11-21 12:13:56 +00004167 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004168 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004169 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004170
4171 xpc.xp_context = cmdcomplete_str_to_type(type);
4172 if (xpc.xp_context == EXPAND_NOTHING)
4173 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004174 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004175 return;
4176 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004177
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004178 if (xpc.xp_context == EXPAND_USER_DEFINED)
4179 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004180 // Must be "custom,funcname" pattern
4181 if (STRNCMP(type, "custom,", 7) != 0)
4182 {
4183 semsg(_(e_invalid_argument_str), type);
4184 return;
4185 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004186
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004187 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004188 }
4189
4190 if (xpc.xp_context == EXPAND_USER_LIST)
4191 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004192 // Must be "customlist,funcname" pattern
4193 if (STRNCMP(type, "customlist,", 11) != 0)
4194 {
4195 semsg(_(e_invalid_argument_str), type);
4196 return;
4197 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004198
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004199 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004200 }
4201
Bram Moolenaar66b51422019-08-18 21:44:12 +02004202# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004203 if (xpc.xp_context == EXPAND_MENUS)
4204 {
4205 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4206 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4207 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004208# endif
4209# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004210 if (xpc.xp_context == EXPAND_CSCOPE)
4211 {
4212 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4213 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4214 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004215# endif
4216# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004217 if (xpc.xp_context == EXPAND_SIGN)
4218 {
4219 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4220 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4221 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004222# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004223 if (xpc.xp_context == EXPAND_RUNTIME)
4224 {
4225 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4226 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4227 }
zeertzjq85f36d62024-10-10 19:14:13 +02004228 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4229 {
4230 int context = EXPAND_SHELLCMDLINE;
4231 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4232 &context);
4233 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4234 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004235 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004236
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004237 if (cmdline_fuzzy_completion_supported(&xpc))
4238 // when fuzzy matching, don't modify the search string
4239 pat = vim_strsave(xpc.xp_pattern);
4240 else
4241 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4242
Bram Moolenaar93a10962022-06-16 11:42:09 +01004243 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004244 {
4245 int i;
4246
4247 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4248
4249 for (i = 0; i < xpc.xp_numfiles; i++)
4250 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4251 }
4252 vim_free(pat);
4253 ExpandCleanup(&xpc);
4254}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004255#endif // FEAT_EVAL