blob: 7e5311481b8684f9776f66492dda5557be509765 [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:
2335 case CMD_checktime:
2336 xp->xp_context = EXPAND_BUFFERS;
2337 xp->xp_pattern = arg;
2338 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002339#ifdef FEAT_DIFF
2340 case CMD_diffget:
2341 case CMD_diffput:
2342 // If current buffer is in diff mode, complete buffer names
2343 // which are in diff mode, and different than current buffer.
2344 xp->xp_context = EXPAND_DIFF_BUFFERS;
2345 xp->xp_pattern = arg;
2346 break;
2347#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002348 case CMD_USER:
2349 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002350 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2351 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002352
2353 case CMD_map: case CMD_noremap:
2354 case CMD_nmap: case CMD_nnoremap:
2355 case CMD_vmap: case CMD_vnoremap:
2356 case CMD_omap: case CMD_onoremap:
2357 case CMD_imap: case CMD_inoremap:
2358 case CMD_cmap: case CMD_cnoremap:
2359 case CMD_lmap: case CMD_lnoremap:
2360 case CMD_smap: case CMD_snoremap:
2361 case CMD_tmap: case CMD_tnoremap:
2362 case CMD_xmap: case CMD_xnoremap:
2363 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002364 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002365 case CMD_unmap:
2366 case CMD_nunmap:
2367 case CMD_vunmap:
2368 case CMD_ounmap:
2369 case CMD_iunmap:
2370 case CMD_cunmap:
2371 case CMD_lunmap:
2372 case CMD_sunmap:
2373 case CMD_tunmap:
2374 case CMD_xunmap:
2375 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002376 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002377 case CMD_mapclear:
2378 case CMD_nmapclear:
2379 case CMD_vmapclear:
2380 case CMD_omapclear:
2381 case CMD_imapclear:
2382 case CMD_cmapclear:
2383 case CMD_lmapclear:
2384 case CMD_smapclear:
2385 case CMD_tmapclear:
2386 case CMD_xmapclear:
2387 xp->xp_context = EXPAND_MAPCLEAR;
2388 xp->xp_pattern = arg;
2389 break;
2390
2391 case CMD_abbreviate: case CMD_noreabbrev:
2392 case CMD_cabbrev: case CMD_cnoreabbrev:
2393 case CMD_iabbrev: case CMD_inoreabbrev:
2394 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002395 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002396 case CMD_unabbreviate:
2397 case CMD_cunabbrev:
2398 case CMD_iunabbrev:
2399 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002400 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002401#ifdef FEAT_MENU
2402 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2403 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2404 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2405 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2406 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2407 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2408 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2409 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2410 case CMD_tmenu: case CMD_tunmenu:
2411 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2412 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2413#endif
2414
2415 case CMD_colorscheme:
2416 xp->xp_context = EXPAND_COLORS;
2417 xp->xp_pattern = arg;
2418 break;
2419
2420 case CMD_compiler:
2421 xp->xp_context = EXPAND_COMPILER;
2422 xp->xp_pattern = arg;
2423 break;
2424
2425 case CMD_ownsyntax:
2426 xp->xp_context = EXPAND_OWNSYNTAX;
2427 xp->xp_pattern = arg;
2428 break;
2429
2430 case CMD_setfiletype:
2431 xp->xp_context = EXPAND_FILETYPE;
2432 xp->xp_pattern = arg;
2433 break;
2434
2435 case CMD_packadd:
2436 xp->xp_context = EXPAND_PACKADD;
2437 xp->xp_pattern = arg;
2438 break;
2439
zeertzjqb0d45ec2023-01-25 15:04:22 +00002440 case CMD_runtime:
2441 set_context_in_runtime_cmd(xp, arg);
2442 break;
2443
Bram Moolenaard0190392019-08-23 21:17:35 +02002444#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2445 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002446 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002447#endif
2448#if defined(FEAT_PROFILE)
2449 case CMD_profile:
2450 set_context_in_profile_cmd(xp, arg);
2451 break;
2452#endif
2453 case CMD_behave:
2454 xp->xp_context = EXPAND_BEHAVE;
2455 xp->xp_pattern = arg;
2456 break;
2457
2458 case CMD_messages:
2459 xp->xp_context = EXPAND_MESSAGES;
2460 xp->xp_pattern = arg;
2461 break;
2462
2463 case CMD_history:
2464 xp->xp_context = EXPAND_HISTORY;
2465 xp->xp_pattern = arg;
2466 break;
2467#if defined(FEAT_PROFILE)
2468 case CMD_syntime:
2469 xp->xp_context = EXPAND_SYNTIME;
2470 xp->xp_pattern = arg;
2471 break;
2472#endif
2473
2474 case CMD_argdelete:
2475 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2476 arg = xp->xp_pattern + 1;
2477 xp->xp_context = EXPAND_ARGLIST;
2478 xp->xp_pattern = arg;
2479 break;
2480
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002481#ifdef FEAT_EVAL
2482 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002483 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002484 case CMD_breakdel:
2485 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002486
2487 case CMD_scriptnames:
2488 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002489#endif
2490
Bram Moolenaard0190392019-08-23 21:17:35 +02002491 default:
2492 break;
2493 }
2494 return NULL;
2495}
2496
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002497/*
2498 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2499 * we don't need/want deleted. Maybe this could be done better if we didn't
2500 * repeat all this stuff. The only problem is that they may not stay
2501 * perfectly compatible with each other, but then the command line syntax
2502 * probably won't change that much -- webb.
2503 */
2504 static char_u *
2505set_one_cmd_context(
2506 expand_T *xp,
2507 char_u *buff) // buffer for command string
2508{
2509 char_u *p;
2510 char_u *cmd, *arg;
2511 int len = 0;
2512 exarg_T ea;
2513 int compl = EXPAND_NOTHING;
2514 int forceit = FALSE;
2515 int usefilter = FALSE; // filter instead of file name
2516
2517 ExpandInit(xp);
2518 xp->xp_pattern = buff;
2519 xp->xp_line = buff;
2520 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2521 ea.argt = 0;
2522
2523 // 1. skip comment lines and leading space, colons or bars
2524 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2525 ;
2526 xp->xp_pattern = cmd;
2527
2528 if (*cmd == NUL)
2529 return NULL;
2530 if (*cmd == '"') // ignore comment lines
2531 {
2532 xp->xp_context = EXPAND_NOTHING;
2533 return NULL;
2534 }
2535
2536 // 3. Skip over the range to find the command.
2537 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2538 xp->xp_pattern = cmd;
2539 if (*cmd == NUL)
2540 return NULL;
2541 if (*cmd == '"')
2542 {
2543 xp->xp_context = EXPAND_NOTHING;
2544 return NULL;
2545 }
2546
2547 if (*cmd == '|' || *cmd == '\n')
2548 return cmd + 1; // There's another command
2549
2550 // Get the command index.
2551 p = set_cmd_index(cmd, &ea, xp, &compl);
2552 if (p == NULL)
2553 return NULL;
2554
2555 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2556
2557 if (*p == '!') // forced commands
2558 {
2559 forceit = TRUE;
2560 ++p;
2561 }
2562
2563 // 6. parse arguments
2564 if (!IS_USER_CMDIDX(ea.cmdidx))
2565 ea.argt = excmd_get_argt(ea.cmdidx);
2566
2567 arg = skipwhite(p);
2568
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002569 // Does command allow "++argopt" argument?
2570 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002571 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002572 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2573 {
2574 p = arg + 2;
2575 while (*p && !vim_isspace(*p))
2576 MB_PTR_ADV(p);
2577
2578 // Still touching the command after "++"?
2579 if (*p == NUL)
2580 {
2581 if (ea.argt & EX_ARGOPT)
2582 return set_context_in_argopt(xp, arg + 2);
2583#ifdef FEAT_TERMINAL
2584 if (ea.cmdidx == CMD_terminal)
2585 return set_context_in_terminalopt(xp, arg + 2);
2586#endif
2587 }
2588
2589 arg = skipwhite(p);
2590 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002591 }
2592
2593 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2594 {
2595 if (*arg == '>') // append
2596 {
2597 if (*++arg == '>')
2598 ++arg;
2599 arg = skipwhite(arg);
2600 }
2601 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2602 {
2603 ++arg;
2604 usefilter = TRUE;
2605 }
2606 }
2607
2608 if (ea.cmdidx == CMD_read)
2609 {
2610 usefilter = forceit; // :r! filter if forced
2611 if (*arg == '!') // :r !filter
2612 {
2613 ++arg;
2614 usefilter = TRUE;
2615 }
2616 }
2617
2618 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2619 {
2620 while (*arg == *cmd) // allow any number of '>' or '<'
2621 ++arg;
2622 arg = skipwhite(arg);
2623 }
2624
2625 // Does command allow "+command"?
2626 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2627 {
2628 // Check if we're in the +command
2629 p = arg + 1;
2630 arg = skip_cmd_arg(arg, FALSE);
2631
2632 // Still touching the command after '+'?
2633 if (*arg == NUL)
2634 return p;
2635
2636 // Skip space(s) after +command to get to the real argument
2637 arg = skipwhite(arg);
2638 }
2639
2640
2641 // Check for '|' to separate commands and '"' to start comments.
2642 // Don't do this for ":read !cmd" and ":write !cmd".
2643 if ((ea.argt & EX_TRLBAR) && !usefilter)
2644 {
2645 p = arg;
2646 // ":redir @" is not the start of a comment
2647 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2648 p += 2;
2649 while (*p)
2650 {
2651 if (*p == Ctrl_V)
2652 {
2653 if (p[1] != NUL)
2654 ++p;
2655 }
2656 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2657 || *p == '|' || *p == '\n')
2658 {
2659 if (*(p - 1) != '\\')
2660 {
2661 if (*p == '|' || *p == '\n')
2662 return p + 1;
2663 return NULL; // It's a comment
2664 }
2665 }
2666 MB_PTR_ADV(p);
2667 }
2668 }
2669
2670 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2671 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2672 // no arguments allowed but there is something
2673 return NULL;
2674
2675 // Find start of last argument (argument just before cursor):
2676 p = buff;
2677 xp->xp_pattern = p;
2678 len = (int)STRLEN(buff);
2679 while (*p && p < buff + len)
2680 {
2681 if (*p == ' ' || *p == TAB)
2682 {
2683 // argument starts after a space
2684 xp->xp_pattern = ++p;
2685 }
2686 else
2687 {
2688 if (*p == '\\' && *(p + 1) != NUL)
2689 ++p; // skip over escaped character
2690 MB_PTR_ADV(p);
2691 }
2692 }
2693
2694 if (ea.argt & EX_XFILE)
2695 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2696
2697 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002698 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002699 forceit);
2700}
2701
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002702/*
2703 * Set the completion context in 'xp' for command 'str'
2704 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002705 void
2706set_cmd_context(
2707 expand_T *xp,
2708 char_u *str, // start of command line
2709 int len, // length of command line (excl. NUL)
2710 int col, // position of cursor
2711 int use_ccline UNUSED) // use ccline for info
2712{
2713#ifdef FEAT_EVAL
2714 cmdline_info_T *ccline = get_cmdline_info();
2715#endif
2716 int old_char = NUL;
2717 char_u *nextcomm;
2718
2719 // Avoid a UMR warning from Purify, only save the character if it has been
2720 // written before.
2721 if (col < len)
2722 old_char = str[col];
2723 str[col] = NUL;
2724 nextcomm = str;
2725
2726#ifdef FEAT_EVAL
2727 if (use_ccline && ccline->cmdfirstc == '=')
2728 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002729 // pass CMD_SIZE because there is no real command
2730 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002731 }
2732 else if (use_ccline && ccline->input_fn)
2733 {
2734 xp->xp_context = ccline->xp_context;
2735 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002737 }
2738 else
2739#endif
2740 while (nextcomm != NULL)
2741 nextcomm = set_one_cmd_context(xp, nextcomm);
2742
2743 // Store the string here so that call_user_expand_func() can get to them
2744 // easily.
2745 xp->xp_line = str;
2746 xp->xp_col = col;
2747
2748 str[col] = old_char;
2749}
2750
2751/*
2752 * Expand the command line "str" from context "xp".
2753 * "xp" must have been set by set_cmd_context().
2754 * xp->xp_pattern points into "str", to where the text that is to be expanded
2755 * starts.
2756 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2757 * cursor.
2758 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2759 * key that triggered expansion literally.
2760 * Returns EXPAND_OK otherwise.
2761 */
2762 int
2763expand_cmdline(
2764 expand_T *xp,
2765 char_u *str, // start of command line
2766 int col, // position of cursor
2767 int *matchcount, // return: nr of matches
2768 char_u ***matches) // return: array of pointers to matches
2769{
2770 char_u *file_str = NULL;
2771 int options = WILD_ADD_SLASH|WILD_SILENT;
2772
2773 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2774 {
2775 beep_flush();
2776 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2777 }
2778 if (xp->xp_context == EXPAND_NOTHING)
2779 {
2780 // Caller can use the character as a normal char instead
2781 return EXPAND_NOTHING;
2782 }
2783
2784 // add star to file name, or convert to regexp if not exp. files.
2785 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002786 if (cmdline_fuzzy_completion_supported(xp))
2787 // If fuzzy matching, don't modify the search string
2788 file_str = vim_strsave(xp->xp_pattern);
2789 else
2790 {
2791 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2792 if (file_str == NULL)
2793 return EXPAND_UNSUCCESSFUL;
2794 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002795
2796 if (p_wic)
2797 options += WILD_ICASE;
2798
2799 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002800 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002801 {
2802 *matchcount = 0;
2803 *matches = NULL;
2804 }
2805 vim_free(file_str);
2806
2807 return EXPAND_OK;
2808}
2809
Bram Moolenaar66b51422019-08-18 21:44:12 +02002810/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002811 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002812 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002813 */
2814 static int
2815expand_files_and_dirs(
2816 expand_T *xp,
2817 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002818 char_u ***matches,
2819 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002820 int flags,
2821 int options)
2822{
2823 int free_pat = FALSE;
2824 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002825 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002826
2827 // for ":set path=" and ":set tags=" halve backslashes for escaped
2828 // space
2829 if (xp->xp_backslash != XP_BS_NONE)
2830 {
2831 free_pat = TRUE;
2832 pat = vim_strsave(pat);
2833 for (i = 0; pat[i]; ++i)
2834 if (pat[i] == '\\')
2835 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002836 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002837 && pat[i + 1] == '\\'
2838 && pat[i + 2] == '\\'
2839 && pat[i + 3] == ' ')
2840 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002841 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002842 && pat[i + 1] == ' ')
2843 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002844 else if ((xp->xp_backslash & XP_BS_COMMA)
2845 && pat[i + 1] == '\\'
2846 && pat[i + 2] == ',')
2847 STRMOVE(pat + i, pat + i + 2);
2848#ifdef BACKSLASH_IN_FILENAME
2849 else if ((xp->xp_backslash & XP_BS_COMMA)
2850 && pat[i + 1] == ',')
2851 STRMOVE(pat + i, pat + i + 1);
2852#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002853 }
2854 }
2855
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002856 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002857 {
2858#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002859 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002860#endif
2861 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002862 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002863 {
2864 if (xp->xp_context == EXPAND_FILES)
2865 flags |= EW_FILE;
2866 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2867 flags |= (EW_FILE | EW_PATH);
2868 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2869 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2870 else
2871 flags = (flags | EW_DIR) & ~EW_FILE;
2872 if (options & WILD_ICASE)
2873 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002874
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002875 // Expand wildcards, supporting %:h and the like.
2876 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2877 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002878 if (free_pat)
2879 vim_free(pat);
2880#ifdef BACKSLASH_IN_FILENAME
2881 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2882 {
2883 int j;
2884
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002885 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002886 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002887 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002888
2889 while (*ptr != NUL)
2890 {
2891 if (p_csl[0] == 's' && *ptr == '\\')
2892 *ptr = '/';
2893 else if (p_csl[0] == 'b' && *ptr == '/')
2894 *ptr = '\\';
2895 ptr += (*mb_ptr2len)(ptr);
2896 }
2897 }
2898 }
2899#endif
2900 return ret;
2901}
2902
2903/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002904 * Function given to ExpandGeneric() to obtain the possible arguments of the
2905 * ":behave {mswin,xterm}" command.
2906 */
2907 static char_u *
2908get_behave_arg(expand_T *xp UNUSED, int idx)
2909{
2910 if (idx == 0)
2911 return (char_u *)"mswin";
2912 if (idx == 1)
2913 return (char_u *)"xterm";
2914 return NULL;
2915}
2916
K.Takata161b6ac2022-11-14 15:31:07 +00002917#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918/*
2919 * Function given to ExpandGeneric() to obtain the possible arguments of the
2920 * ":breakadd {expr, file, func, here}" command.
2921 * ":breakdel {func, file, here}" command.
2922 */
2923 static char_u *
2924get_breakadd_arg(expand_T *xp UNUSED, int idx)
2925{
2926 char *opts[] = {"expr", "file", "func", "here"};
2927
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002928 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002929 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002930 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2932 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002933 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2934 {
2935 // breakdel {func, file, here}
2936 if (idx <= 2)
2937 return (char_u *)opts[idx + 1];
2938 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002939 else
2940 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002941 // profdel {func, file}
2942 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002943 return (char_u *)opts[idx + 1];
2944 }
2945 }
2946 return NULL;
2947}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002948
2949/*
2950 * Function given to ExpandGeneric() to obtain the possible arguments for the
2951 * ":scriptnames" command.
2952 */
2953 static char_u *
2954get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2955{
2956 scriptitem_T *si;
2957
2958 if (!SCRIPT_ID_VALID(idx + 1))
2959 return NULL;
2960
2961 si = SCRIPT_ITEM(idx + 1);
2962 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2963 return NameBuff;
2964}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002965#endif
2966
Bram Moolenaard0190392019-08-23 21:17:35 +02002967/*
2968 * Function given to ExpandGeneric() to obtain the possible arguments of the
2969 * ":messages {clear}" command.
2970 */
2971 static char_u *
2972get_messages_arg(expand_T *xp UNUSED, int idx)
2973{
2974 if (idx == 0)
2975 return (char_u *)"clear";
2976 return NULL;
2977}
2978
2979 static char_u *
2980get_mapclear_arg(expand_T *xp UNUSED, int idx)
2981{
2982 if (idx == 0)
2983 return (char_u *)"<buffer>";
2984 return NULL;
2985}
2986
2987/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002988 * Do the expansion based on xp->xp_context and 'rmp'.
2989 */
2990 static int
2991ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002992 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002993 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002994 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002995 char_u ***matches,
2996 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002997{
2998 static struct expgen
2999 {
3000 int context;
3001 char_u *((*func)(expand_T *, int));
3002 int ic;
3003 int escaped;
3004 } tab[] =
3005 {
3006 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3007 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3008 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3009 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3010 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3011 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3012 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3013 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3014 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3015 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003016#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003017 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3018 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3019 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3020 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3021 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003022#endif
3023#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003024 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3025 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003026#endif
3027#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003028 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003029#endif
3030#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003031 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003032#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003033 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3034 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3035 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003036#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003037 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003038#endif
3039#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003040 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003041#endif
3042#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003043 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003044#endif
3045#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003046 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3047 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003048#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003049 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3050 {EXPAND_USER, get_users, TRUE, FALSE},
3051 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003052#ifdef FEAT_EVAL
3053 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003054 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003055#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003056 };
3057 int i;
3058 int ret = FAIL;
3059
3060 // Find a context in the table and call the ExpandGeneric() with the
3061 // right function to do the expansion.
3062 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3063 {
3064 if (xp->xp_context == tab[i].context)
3065 {
3066 if (tab[i].ic)
3067 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003068 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3069 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003070 break;
3071 }
3072 }
3073
3074 return ret;
3075}
3076
3077/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003078 * Map wild expand options to flags for expand_wildcards()
3079 */
3080 static int
3081map_wildopts_to_ewflags(int options)
3082{
3083 int flags;
3084
3085 flags = EW_DIR; // include directories
3086 if (options & WILD_LIST_NOTFOUND)
3087 flags |= EW_NOTFOUND;
3088 if (options & WILD_ADD_SLASH)
3089 flags |= EW_ADDSLASH;
3090 if (options & WILD_KEEP_ALL)
3091 flags |= EW_KEEPALL;
3092 if (options & WILD_SILENT)
3093 flags |= EW_SILENT;
3094 if (options & WILD_NOERROR)
3095 flags |= EW_NOERROR;
3096 if (options & WILD_ALLLINKS)
3097 flags |= EW_ALLLINKS;
3098
3099 return flags;
3100}
3101
3102/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103 * Do the expansion based on xp->xp_context and "pat".
3104 */
3105 static int
3106ExpandFromContext(
3107 expand_T *xp,
3108 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003109 char_u ***matches,
3110 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003111 int options) // WILD_ flags
3112{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003114 int ret;
3115 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003116 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003117 int fuzzy = cmdline_fuzzy_complete(pat)
3118 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003119
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003120 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121
3122 if (xp->xp_context == EXPAND_FILES
3123 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003124 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003125 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003126 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003127 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3128 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003130 *matches = (char_u **)"";
3131 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 if (xp->xp_context == EXPAND_HELP)
3133 {
3134 // With an empty argument we would get all the help tags, which is
3135 // very slow. Get matches for "help" instead.
3136 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003137 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 {
3139#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003140 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141#endif
3142 return OK;
3143 }
3144 return FAIL;
3145 }
3146
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003150 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003152 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003153#ifdef FEAT_DIFF
3154 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003155 return ExpandBufnames(pat, numMatches, matches,
3156 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003157#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003158 if (xp->xp_context == EXPAND_TAGS
3159 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003160 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3161 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003162 if (xp->xp_context == EXPAND_COLORS)
3163 {
3164 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003165 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003166 directories);
3167 }
3168 if (xp->xp_context == EXPAND_COMPILER)
3169 {
3170 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003171 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003172 }
3173 if (xp->xp_context == EXPAND_OWNSYNTAX)
3174 {
3175 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003176 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003177 }
3178 if (xp->xp_context == EXPAND_FILETYPE)
3179 {
3180 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003181 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003182 }
Doug Kearns81642d92024-01-04 22:37:44 +01003183#ifdef FEAT_KEYMAP
3184 if (xp->xp_context == EXPAND_KEYMAP)
3185 {
3186 char *directories[] = {"keymap", NULL};
3187 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3188 }
3189#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003190#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003191 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003192 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003193#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003194 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003195 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003196 if (xp->xp_context == EXPAND_RUNTIME)
3197 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003198
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003199 // When expanding a function name starting with s:, match the <SNR>nr_
3200 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003201 if ((xp->xp_context == EXPAND_USER_FUNC
3202 || xp->xp_context == EXPAND_DISASSEMBLE)
3203 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003204 {
3205 int len = (int)STRLEN(pat) + 20;
3206
3207 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003208 if (tofree == NULL)
3209 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003210 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003211 pat = tofree;
3212 }
3213
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003214 if (!fuzzy)
3215 {
3216 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3217 if (regmatch.regprog == NULL)
3218 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003219
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003220 // set ignore-case according to p_ic, p_scs and pat
3221 regmatch.rm_ic = ignorecase(pat);
3222 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003223
3224 if (xp->xp_context == EXPAND_SETTINGS
3225 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003226 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003227 else if (xp->xp_context == EXPAND_STRING_SETTING)
3228 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3229 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3230 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003232 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003233 else if (xp->xp_context == EXPAND_ARGOPT)
3234 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3235#if defined(FEAT_TERMINAL)
3236 else if (xp->xp_context == EXPAND_TERMINALOPT)
3237 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3238#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003239#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003241 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003242#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003243 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003244 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003245
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003246 if (!fuzzy)
3247 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003248 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249
3250 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003251}
3252
Bram Moolenaar66b51422019-08-18 21:44:12 +02003253/*
3254 * Expand a list of names.
3255 *
3256 * Generic function for command line completion. It calls a function to
3257 * obtain strings, one by one. The strings are matched against a regexp
3258 * program. Matching strings are copied into an array, which is returned.
3259 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003260 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3261 * is used.
3262 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003263 * Returns OK when no problems encountered, FAIL for error (out of memory).
3264 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003265 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003267 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268 expand_T *xp,
3269 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003270 char_u ***matches,
3271 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003272 char_u *((*func)(expand_T *, int)),
3273 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003274 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003275{
3276 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003277 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003278 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003279 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003280 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003281 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003282 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003283 int sort_matches = FALSE;
3284 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003286 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003287 *matches = NULL;
3288 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003289
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003290 if (!fuzzy)
3291 ga_init2(&ga, sizeof(char *), 30);
3292 else
3293 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3294
3295 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003296 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003297 str = (*func)(xp, i);
3298 if (str == NULL) // end of list
3299 break;
3300 if (*str == NUL) // skip empty strings
3301 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003302
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003303 if (xp->xp_pattern[0] != NUL)
3304 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003305 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003306 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003307 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003308 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003309 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003310 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003311 }
3312 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003313 else
3314 match = TRUE;
3315
3316 if (!match)
3317 continue;
3318
3319 if (escaped)
3320 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3321 else
3322 str = vim_strsave(str);
3323 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003325 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003326 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003327 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003328 return FAIL;
3329 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003330 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003331 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003332 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003333
3334 if (ga_grow(&ga, 1) == FAIL)
3335 {
3336 vim_free(str);
3337 break;
3338 }
3339
3340 if (fuzzy)
3341 {
3342 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3343 fuzmatch->idx = ga.ga_len;
3344 fuzmatch->str = str;
3345 fuzmatch->score = score;
3346 }
3347 else
3348 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3349
K.Takata161b6ac2022-11-14 15:31:07 +00003350#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003351 if (func == get_menu_names)
3352 {
3353 // test for separator added by get_menu_names()
3354 str += STRLEN(str) - 1;
3355 if (*str == '\001')
3356 *str = '.';
3357 }
K.Takata161b6ac2022-11-14 15:31:07 +00003358#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003359
3360 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003361 }
3362
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003363 if (ga.ga_len == 0)
3364 return OK;
3365
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003366 // sort the matches when using regular expression matching and sorting
3367 // applies to the completion context. Menus and scriptnames should be kept
3368 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003369 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003370 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003371 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003372 && xp->xp_context != EXPAND_SCRIPTNAMES
3373 && xp->xp_context != EXPAND_ARGOPT
3374 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003375 sort_matches = TRUE;
3376
3377 // <SNR> functions should be sorted to the end.
3378 if (xp->xp_context == EXPAND_EXPRESSION
3379 || xp->xp_context == EXPAND_FUNCTIONS
3380 || xp->xp_context == EXPAND_USER_FUNC
3381 || xp->xp_context == EXPAND_DISASSEMBLE)
3382 funcsort = TRUE;
3383
3384 // Sort the matches.
3385 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003386 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003387 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003388 // <SNR> functions should be sorted to the end.
3389 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3390 sort_func_compare);
3391 else
3392 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3393 }
3394
3395 if (!fuzzy)
3396 {
3397 *matches = ga.ga_data;
3398 *numMatches = ga.ga_len;
3399 }
3400 else
3401 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003402 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3403 funcsort) == FAIL)
3404 return FAIL;
3405 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003406 }
3407
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003408#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003409 // Reset the variables used for special highlight names expansion, so that
3410 // they don't show up when getting normal highlight names by ID.
3411 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003412#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003413
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414 return OK;
3415}
3416
3417/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003418 * Expand shell command matches in one directory of $PATH.
3419 */
3420 static void
3421expand_shellcmd_onedir(
3422 char_u *buf,
3423 char_u *s,
3424 size_t l,
3425 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003426 char_u ***matches,
3427 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003428 int flags,
3429 hashtab_T *ht,
3430 garray_T *gap)
3431{
3432 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003433 hash_T hash;
3434 hashitem_T *hi;
3435
3436 vim_strncpy(buf, s, l);
3437 add_pathsep(buf);
3438 l = STRLEN(buf);
3439 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3440
3441 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003442 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003443 if (ret != OK)
3444 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003445
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003446 if (ga_grow(gap, *numMatches) == FAIL)
3447 {
3448 FreeWild(*numMatches, *matches);
3449 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003450 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003451
3452 for (int i = 0; i < *numMatches; ++i)
3453 {
3454 char_u *name = (*matches)[i];
3455
3456 if (STRLEN(name) > l)
3457 {
3458 // Check if this name was already found.
3459 hash = hash_hash(name + l);
3460 hi = hash_lookup(ht, name + l, hash);
3461 if (HASHITEM_EMPTY(hi))
3462 {
3463 // Remove the path that was prepended.
3464 STRMOVE(name, name + l);
3465 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3466 hash_add_item(ht, hi, name, hash);
3467 name = NULL;
3468 }
3469 }
3470 vim_free(name);
3471 }
3472 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003473}
3474
3475/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003476 * Complete a shell command.
3477 * Returns FAIL or OK;
3478 */
3479 static int
3480expand_shellcmd(
3481 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003482 char_u ***matches, // return: array with matches
3483 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 int flagsarg) // EW_ flags
3485{
3486 char_u *pat;
3487 int i;
3488 char_u *path = NULL;
3489 int mustfree = FALSE;
3490 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003491 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 size_t l;
3493 char_u *s, *e;
3494 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003495 int did_curdir = FALSE;
3496 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003497
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003498 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499 if (buf == NULL)
3500 return FAIL;
3501
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003502 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003503 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003504 if (pat == NULL)
3505 {
3506 vim_free(buf);
3507 return FAIL;
3508 }
3509
Bram Moolenaar66b51422019-08-18 21:44:12 +02003510 for (i = 0; pat[i]; ++i)
3511 if (pat[i] == '\\' && pat[i + 1] == ' ')
3512 STRMOVE(pat + i, pat + i + 1);
3513
3514 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3515
3516 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3517 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3518 path = (char_u *)".";
3519 else
3520 {
3521 // For an absolute name we don't use $PATH.
3522 if (!mch_isFullName(pat))
3523 path = vim_getenv((char_u *)"PATH", &mustfree);
3524 if (path == NULL)
3525 path = (char_u *)"";
3526 }
3527
3528 // Go over all directories in $PATH. Expand matches in that directory and
3529 // collect them in "ga". When "." is not in $PATH also expand for the
3530 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003531 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003532 hash_init(&found_ht);
3533 for (s = path; ; s = e)
3534 {
K.Takata161b6ac2022-11-14 15:31:07 +00003535#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003536 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003537#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003539#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003540 if (e == NULL)
3541 e = s + STRLEN(s);
3542
3543 if (*s == NUL)
3544 {
3545 if (did_curdir)
3546 break;
3547 // Find directories in the current directory, path is empty.
3548 did_curdir = TRUE;
3549 flags |= EW_DIR;
3550 }
3551 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3552 {
3553 did_curdir = TRUE;
3554 flags |= EW_DIR;
3555 }
3556 else
3557 // Do not match directories inside a $PATH item.
3558 flags &= ~EW_DIR;
3559
3560 l = e - s;
3561 if (l > MAXPATHL - 5)
3562 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003563
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003564 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003565 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566
Bram Moolenaar66b51422019-08-18 21:44:12 +02003567 if (*e != NUL)
3568 ++e;
3569 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003570 *matches = ga.ga_data;
3571 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003572
3573 vim_free(buf);
3574 vim_free(pat);
3575 if (mustfree)
3576 vim_free(path);
3577 hash_clear(&found_ht);
3578 return OK;
3579}
3580
K.Takata161b6ac2022-11-14 15:31:07 +00003581#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003582/*
3583 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003584 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003585 */
3586 static void *
3587call_user_expand_func(
3588 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003589 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003590{
3591 cmdline_info_T *ccline = get_cmdline_info();
3592 int keep = 0;
3593 typval_T args[4];
3594 sctx_T save_current_sctx = current_sctx;
3595 char_u *pat = NULL;
3596 void *ret;
3597
3598 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3599 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003600
3601 if (ccline->cmdbuff != NULL)
3602 {
3603 keep = ccline->cmdbuff[ccline->cmdlen];
3604 ccline->cmdbuff[ccline->cmdlen] = 0;
3605 }
3606
3607 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3608
3609 args[0].v_type = VAR_STRING;
3610 args[0].vval.v_string = pat;
3611 args[1].v_type = VAR_STRING;
3612 args[1].vval.v_string = xp->xp_line;
3613 args[2].v_type = VAR_NUMBER;
3614 args[2].vval.v_number = xp->xp_col;
3615 args[3].v_type = VAR_UNKNOWN;
3616
3617 current_sctx = xp->xp_script_ctx;
3618
3619 ret = user_expand_func(xp->xp_arg, 3, args);
3620
3621 current_sctx = save_current_sctx;
3622 if (ccline->cmdbuff != NULL)
3623 ccline->cmdbuff[ccline->cmdlen] = keep;
3624
3625 vim_free(pat);
3626 return ret;
3627}
3628
3629/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003630 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3631 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003632 */
3633 static int
3634ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003635 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003636 expand_T *xp,
3637 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003638 char_u ***matches,
3639 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003640{
3641 char_u *retstr;
3642 char_u *s;
3643 char_u *e;
3644 int keep;
3645 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003646 int fuzzy;
3647 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003648 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003649
3650 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003651 *matches = NULL;
3652 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003653
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003654 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003655 if (retstr == NULL)
3656 return FAIL;
3657
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003658 if (!fuzzy)
3659 ga_init2(&ga, sizeof(char *), 3);
3660 else
3661 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3662
Bram Moolenaar66b51422019-08-18 21:44:12 +02003663 for (s = retstr; *s != NUL; s = e)
3664 {
3665 e = vim_strchr(s, '\n');
3666 if (e == NULL)
3667 e = s + STRLEN(s);
3668 keep = *e;
3669 *e = NUL;
3670
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003671 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003672 {
3673 if (!fuzzy)
3674 match = vim_regexec(regmatch, s, (colnr_T)0);
3675 else
3676 {
3677 score = fuzzy_match_str(s, pat);
3678 match = (score != 0);
3679 }
3680 }
3681 else
3682 match = TRUE; // match everything
3683
Bram Moolenaar66b51422019-08-18 21:44:12 +02003684 *e = keep;
3685
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003687 {
3688 if (ga_grow(&ga, 1) == FAIL)
3689 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003690 if (!fuzzy)
3691 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3692 else
3693 {
3694 fuzmatch_str_T *fuzmatch =
3695 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003696 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003697 fuzmatch->str = vim_strnsave(s, e - s);
3698 fuzmatch->score = score;
3699 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003700 ++ga.ga_len;
3701 }
3702
3703 if (*e != NUL)
3704 ++e;
3705 }
3706 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003707
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003708 if (ga.ga_len == 0)
3709 return OK;
3710
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003711 if (!fuzzy)
3712 {
3713 *matches = ga.ga_data;
3714 *numMatches = ga.ga_len;
3715 }
3716 else
3717 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003718 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3719 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003720 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003721 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003722 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723 return OK;
3724}
3725
3726/*
3727 * Expand names with a list returned by a function defined by the user.
3728 */
3729 static int
3730ExpandUserList(
3731 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003732 char_u ***matches,
3733 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003734{
3735 list_T *retlist;
3736 listitem_T *li;
3737 garray_T ga;
3738
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003739 *matches = NULL;
3740 *numMatches = 0;
3741 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003742 if (retlist == NULL)
3743 return FAIL;
3744
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003745 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003746 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003747 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003748 {
3749 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3750 continue; // Skip non-string items and empty strings
3751
3752 if (ga_grow(&ga, 1) == FAIL)
3753 break;
3754
3755 ((char_u **)ga.ga_data)[ga.ga_len] =
3756 vim_strsave(li->li_tv.vval.v_string);
3757 ++ga.ga_len;
3758 }
3759 list_unref(retlist);
3760
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003761 *matches = ga.ga_data;
3762 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003763 return OK;
3764}
K.Takata161b6ac2022-11-14 15:31:07 +00003765#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003766
3767/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003768 * Expand "file" for all comma-separated directories in "path".
3769 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003770 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003771 */
3772 void
3773globpath(
3774 char_u *path,
3775 char_u *file,
3776 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003777 int expand_options,
3778 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003779{
3780 expand_T xpc;
3781 char_u *buf;
3782 int i;
3783 int num_p;
3784 char_u **p;
3785
3786 buf = alloc(MAXPATHL);
3787 if (buf == NULL)
3788 return;
3789
3790 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003791 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003792
3793 // Loop over all entries in {path}.
3794 while (*path != NUL)
3795 {
3796 // Copy one item of the path to buf[] and concatenate the file name.
3797 copy_option_part(&path, buf, MAXPATHL, ",");
3798 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3799 {
K.Takata161b6ac2022-11-14 15:31:07 +00003800#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003801 // Using the platform's path separator (\) makes vim incorrectly
3802 // treat it as an escape character, use '/' instead.
3803 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3804 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003805#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003806 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003807#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003808 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003809 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3811 {
3812 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3813
3814 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003815 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003816 for (i = 0; i < num_p; ++i)
3817 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003818 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003819 ++ga->ga_len;
3820 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003821 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003822 }
3823 }
3824 }
3825
3826 vim_free(buf);
3827}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003828
Bram Moolenaareadee482020-09-04 15:37:31 +02003829/*
3830 * Translate some keys pressed when 'wildmenu' is used.
3831 */
3832 int
3833wildmenu_translate_key(
3834 cmdline_info_T *cclp,
3835 int key,
3836 expand_T *xp,
3837 int did_wild_list)
3838{
3839 int c = key;
3840
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003841 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003842 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003843 // When the popup menu is used for cmdline completion:
3844 // Up : go to the previous item in the menu
3845 // Down : go to the next item in the menu
3846 // Left : go to the parent directory
3847 // Right: list the files in the selected directory
3848 switch (c)
3849 {
3850 case K_UP: c = K_LEFT; break;
3851 case K_DOWN: c = K_RIGHT; break;
3852 case K_LEFT: c = K_UP; break;
3853 case K_RIGHT: c = K_DOWN; break;
3854 default: break;
3855 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003856 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003857
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003858 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003859 {
3860 if (c == K_LEFT)
3861 c = Ctrl_P;
3862 else if (c == K_RIGHT)
3863 c = Ctrl_N;
3864 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003865
Bram Moolenaareadee482020-09-04 15:37:31 +02003866 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003867 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003868 && cclp->cmdpos > 1
3869 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3870 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3871 && (c == '\n' || c == '\r' || c == K_KENTER))
3872 c = K_DOWN;
3873
3874 return c;
3875}
3876
3877/*
3878 * Delete characters on the command line, from "from" to the current
3879 * position.
3880 */
3881 static void
3882cmdline_del(cmdline_info_T *cclp, int from)
3883{
3884 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3885 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3886 cclp->cmdlen -= cclp->cmdpos - from;
3887 cclp->cmdpos = from;
3888}
3889
3890/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003891 * Handle a key pressed when the wild menu for the menu names
3892 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003893 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003894 static int
3895wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003896{
Bram Moolenaareadee482020-09-04 15:37:31 +02003897 int i;
3898 int j;
3899
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003900 // Hitting <Down> after "emenu Name.": complete submenu
3901 if (key == K_DOWN && cclp->cmdpos > 0
3902 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003903 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003904 key = p_wc;
3905 KeyTyped = TRUE; // in case the key was mapped
3906 }
3907 else if (key == K_UP)
3908 {
3909 // Hitting <Up>: Remove one submenu name in front of the
3910 // cursor
3911 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003912
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003913 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3914 i = 0;
3915 while (--j > 0)
3916 {
3917 // check for start of menu name
3918 if (cclp->cmdbuff[j] == ' '
3919 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003920 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003921 i = j + 1;
3922 break;
3923 }
3924 // check for start of submenu name
3925 if (cclp->cmdbuff[j] == '.'
3926 && cclp->cmdbuff[j - 1] != '\\')
3927 {
3928 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003929 {
3930 i = j + 1;
3931 break;
3932 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003933 else
3934 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003935 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003936 }
3937 if (i > 0)
3938 cmdline_del(cclp, i);
3939 key = p_wc;
3940 KeyTyped = TRUE; // in case the key was mapped
3941 xp->xp_context = EXPAND_NOTHING;
3942 }
3943
3944 return key;
3945}
3946
3947/*
3948 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3949 * directory names (EXPAND_DIRECTORIES) or shell command names
3950 * (EXPAND_SHELLCMD) is displayed.
3951 */
3952 static int
3953wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3954{
3955 int i;
3956 int j;
3957 char_u upseg[5];
3958
3959 upseg[0] = PATHSEP;
3960 upseg[1] = '.';
3961 upseg[2] = '.';
3962 upseg[3] = PATHSEP;
3963 upseg[4] = NUL;
3964
3965 if (key == K_DOWN
3966 && cclp->cmdpos > 0
3967 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3968 && (cclp->cmdpos < 3
3969 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3970 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3971 {
3972 // go down a directory
3973 key = p_wc;
3974 KeyTyped = TRUE; // in case the key was mapped
3975 }
3976 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3977 {
3978 // If in a direct ancestor, strip off one ../ to go down
3979 int found = FALSE;
3980
3981 j = cclp->cmdpos;
3982 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3983 while (--j > i)
3984 {
3985 if (has_mbyte)
3986 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3987 if (vim_ispathsep(cclp->cmdbuff[j]))
3988 {
3989 found = TRUE;
3990 break;
3991 }
3992 }
3993 if (found
3994 && cclp->cmdbuff[j - 1] == '.'
3995 && cclp->cmdbuff[j - 2] == '.'
3996 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3997 {
3998 cmdline_del(cclp, j - 2);
3999 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004000 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004001 }
4002 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004003 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004004 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004005 // go up a directory
4006 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004007
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004008 j = cclp->cmdpos - 1;
4009 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4010 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004011 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004012 if (has_mbyte)
4013 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4014 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004015#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004016 && vim_strchr((char_u *)" *?[{`$%#",
4017 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004018#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004019 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004020 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004021 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004022 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004023 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004024 break;
4025 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004026 else
4027 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004028 }
4029 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004030
4031 if (!found)
4032 j = i;
4033 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4034 j += 4;
4035 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4036 && j == i)
4037 j += 3;
4038 else
4039 j = 0;
4040 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004041 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004042 // TODO this is only for DOS/UNIX systems - need to put in
4043 // machine-specific stuff here and in upseg init
4044 cmdline_del(cclp, j);
4045 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004046 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004047 else if (cclp->cmdpos > i)
4048 cmdline_del(cclp, i);
4049
4050 // Now complete in the new directory. Set KeyTyped in case the
4051 // Up key came from a mapping.
4052 key = p_wc;
4053 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004054 }
4055
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004056 return key;
4057}
4058
4059/*
4060 * Handle a key pressed when the wild menu is displayed
4061 */
4062 int
4063wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4064{
4065 if (xp->xp_context == EXPAND_MENUNAMES)
4066 return wildmenu_process_key_menunames(cclp, key, xp);
4067 else if ((xp->xp_context == EXPAND_FILES
4068 || xp->xp_context == EXPAND_DIRECTORIES
4069 || xp->xp_context == EXPAND_SHELLCMD))
4070 return wildmenu_process_key_filenames(cclp, key, xp);
4071
4072 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004073}
4074
4075/*
4076 * Free expanded names when finished walking through the matches
4077 */
4078 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004079wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004080{
4081 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004082
4083 if (!p_wmnu || wild_menu_showing == 0)
4084 return;
4085
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004086#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004087 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004088 if (cclp->input_fn)
4089 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004090#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004091
4092 if (wild_menu_showing == WM_SCROLLED)
4093 {
4094 // Entered command line, move it up
4095 cmdline_row--;
4096 redrawcmd();
4097 }
4098 else if (save_p_ls != -1)
4099 {
4100 // restore 'laststatus' and 'winminheight'
4101 p_ls = save_p_ls;
4102 p_wmh = save_p_wmh;
4103 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004104 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004105 redrawcmd();
4106 save_p_ls = -1;
4107 }
4108 else
4109 {
4110 win_redraw_last_status(topframe);
4111 redraw_statuslines();
4112 }
4113 KeyTyped = skt;
4114 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004115#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004116 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004117 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004118#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004119}
Bram Moolenaareadee482020-09-04 15:37:31 +02004120
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004121#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004122/*
4123 * "getcompletion()" function
4124 */
4125 void
4126f_getcompletion(typval_T *argvars, typval_T *rettv)
4127{
4128 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004129 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004130 expand_T xpc;
4131 int filtered = FALSE;
4132 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004133 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004134
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004135 if (in_vim9script()
4136 && (check_for_string_arg(argvars, 0) == FAIL
4137 || check_for_string_arg(argvars, 1) == FAIL
4138 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4139 return;
4140
ii144785fe02021-11-21 12:13:56 +00004141 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004142 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004143 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004144 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004145
4146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004147 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004148
4149 if (p_wic)
4150 options |= WILD_ICASE;
4151
4152 // For filtered results, 'wildignore' is used
4153 if (!filtered)
4154 options |= WILD_KEEP_ALL;
4155
4156 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004157 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004158 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004159 int cmdline_len = (int)STRLEN(pat);
4160 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004161 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004162 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004163 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004164 else
4165 {
ii144785fe02021-11-21 12:13:56 +00004166 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004167 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004168 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004169
4170 xpc.xp_context = cmdcomplete_str_to_type(type);
4171 if (xpc.xp_context == EXPAND_NOTHING)
4172 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004173 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004174 return;
4175 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004176
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004177 if (xpc.xp_context == EXPAND_USER_DEFINED)
4178 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004179 // Must be "custom,funcname" pattern
4180 if (STRNCMP(type, "custom,", 7) != 0)
4181 {
4182 semsg(_(e_invalid_argument_str), type);
4183 return;
4184 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004185
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004186 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004187 }
4188
4189 if (xpc.xp_context == EXPAND_USER_LIST)
4190 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004191 // Must be "customlist,funcname" pattern
4192 if (STRNCMP(type, "customlist,", 11) != 0)
4193 {
4194 semsg(_(e_invalid_argument_str), type);
4195 return;
4196 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004197
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004198 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004199 }
4200
Bram Moolenaar66b51422019-08-18 21:44:12 +02004201# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004202 if (xpc.xp_context == EXPAND_MENUS)
4203 {
4204 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4205 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4206 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004207# endif
4208# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004209 if (xpc.xp_context == EXPAND_CSCOPE)
4210 {
4211 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4212 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4213 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004214# endif
4215# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004216 if (xpc.xp_context == EXPAND_SIGN)
4217 {
4218 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4219 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4220 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004221# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004222 if (xpc.xp_context == EXPAND_RUNTIME)
4223 {
4224 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4225 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4226 }
zeertzjq85f36d62024-10-10 19:14:13 +02004227 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4228 {
4229 int context = EXPAND_SHELLCMDLINE;
4230 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4231 &context);
4232 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4233 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004234 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004235
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004236 if (cmdline_fuzzy_completion_supported(&xpc))
4237 // when fuzzy matching, don't modify the search string
4238 pat = vim_strsave(xpc.xp_pattern);
4239 else
4240 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4241
Bram Moolenaar93a10962022-06-16 11:42:09 +01004242 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004243 {
4244 int i;
4245
4246 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4247
4248 for (i = 0; i < xpc.xp_numfiles; i++)
4249 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4250 }
4251 vim_free(pat);
4252 ExpandCleanup(&xpc);
4253}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004254#endif // FEAT_EVAL