blob: f707b1d5866c4c41a82e76c64a16228eedd61a25 [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 }
glepnir977561a2025-02-13 20:48:56 +0100376 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000377
378 // no default selection
379 compl_selected = -1;
380
381 cmdline_pum_display();
382
383 return EXPAND_OK;
384}
385
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386/*
387 * Display the cmdline completion matches in a popup menu
388 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000389 void
390cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391{
392 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
393}
394
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000395/*
396 * Returns TRUE if the cmdline completion popup menu is being displayed.
397 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000398 int
399cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100401 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402}
403
404/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000405 * Remove the cmdline completion popup menu (if present), free the list of
406 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000408 void
409cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000411 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100412 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000413
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000414 pum_undisplay();
415 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000416 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000418 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000419 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100420
421 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
422 // as a side effect.
423 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424}
425
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000426 void
427cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000428{
429 cmdline_pum_remove();
430 wildmenu_cleanup(cclp);
431}
432
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000433/*
434 * Returns the starting column number to use for the cmdline completion popup
435 * menu.
436 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000437 int
438cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439{
440 return compl_startcol;
441}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000442
Bram Moolenaar66b51422019-08-18 21:44:12 +0200443/*
zeertzjqd8c93402024-06-17 18:25:32 +0200444 * Returns the current cmdline completion pattern.
445 */
446 char_u *
447cmdline_compl_pattern(void)
448{
449 expand_T *xp = get_cmdline_info()->xpc;
450
451 return xp == NULL ? NULL : xp->xp_orig;
452}
453
454/*
455 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
456 */
457 int
458cmdline_compl_is_fuzzy(void)
459{
460 expand_T *xp = get_cmdline_info()->xpc;
461
462 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
463}
464
465/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000466 * Return the number of characters that should be skipped in a status match.
467 * These are backslashes used for escaping. Do show backslashes in help tags.
468 */
469 static int
470skip_status_match_char(expand_T *xp, char_u *s)
471{
472 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
473#ifdef FEAT_MENU
474 || ((xp->xp_context == EXPAND_MENUS
475 || xp->xp_context == EXPAND_MENUNAMES)
476 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
477#endif
478 )
479 {
480#ifndef BACKSLASH_IN_FILENAME
481 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
482 return 2;
483#endif
484 return 1;
485 }
486 return 0;
487}
488
489/*
490 * Get the length of an item as it will be shown in the status line.
491 */
492 static int
493status_match_len(expand_T *xp, char_u *s)
494{
495 int len = 0;
496
497#ifdef FEAT_MENU
498 int emenu = xp->xp_context == EXPAND_MENUS
499 || xp->xp_context == EXPAND_MENUNAMES;
500
501 // Check for menu separators - replace with '|'.
502 if (emenu && menu_is_separator(s))
503 return 1;
504#endif
505
506 while (*s != NUL)
507 {
508 s += skip_status_match_char(xp, s);
509 len += ptr2cells(s);
510 MB_PTR_ADV(s);
511 }
512
513 return len;
514}
515
516/*
517 * Show wildchar matches in the status line.
518 * Show at least the "match" item.
519 * We start at item 'first_match' in the list and show all matches that fit.
520 *
521 * If inversion is possible we use it. Else '=' characters are used.
522 */
523 static void
524win_redr_status_matches(
525 expand_T *xp,
526 int num_matches,
527 char_u **matches, // list of matches
528 int match,
529 int showtail)
530{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000531 int row;
532 char_u *buf;
533 int len;
534 int clen; // length in screen cells
535 int fillchar;
536 int attr;
537 int i;
538 int highlight = TRUE;
539 char_u *selstart = NULL;
540 int selstart_col = 0;
541 char_u *selend = NULL;
542 static int first_match = 0;
543 int add_left = FALSE;
544 char_u *s;
545#ifdef FEAT_MENU
546 int emenu;
547#endif
548 int l;
549
550 if (matches == NULL) // interrupted completion?
551 return;
552
553 if (has_mbyte)
554 buf = alloc(Columns * MB_MAXBYTES + 1);
555 else
556 buf = alloc(Columns + 1);
557 if (buf == NULL)
558 return;
559
560 if (match == -1) // don't show match but original text
561 {
562 match = 0;
563 highlight = FALSE;
564 }
565 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000566 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000567 if (match == 0)
568 first_match = 0;
569 else if (match < first_match)
570 {
571 // jumping left, as far as we can go
572 first_match = match;
573 add_left = TRUE;
574 }
575 else
576 {
577 // check if match fits on the screen
578 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000579 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000580 if (first_match > 0)
581 clen += 2;
582 // jumping right, put match at the left
583 if ((long)clen > Columns)
584 {
585 first_match = match;
586 // if showing the last match, we can add some on the left
587 clen = 2;
588 for (i = match; i < num_matches; ++i)
589 {
zeertzjqc51a3762022-12-10 10:22:29 +0000590 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000591 if ((long)clen >= Columns)
592 break;
593 }
594 if (i == num_matches)
595 add_left = TRUE;
596 }
597 }
598 if (add_left)
599 while (first_match > 0)
600 {
zeertzjqc51a3762022-12-10 10:22:29 +0000601 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000602 if ((long)clen >= Columns)
603 break;
604 --first_match;
605 }
606
607 fillchar = fillchar_status(&attr, curwin);
608
609 if (first_match == 0)
610 {
611 *buf = NUL;
612 len = 0;
613 }
614 else
615 {
616 STRCPY(buf, "< ");
617 len = 2;
618 }
619 clen = len;
620
621 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000622 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000623 {
624 if (i == match)
625 {
626 selstart = buf + len;
627 selstart_col = clen;
628 }
629
zeertzjqc51a3762022-12-10 10:22:29 +0000630 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000631 // Check for menu separators - replace with '|'
632#ifdef FEAT_MENU
633 emenu = (xp->xp_context == EXPAND_MENUS
634 || xp->xp_context == EXPAND_MENUNAMES);
635 if (emenu && menu_is_separator(s))
636 {
637 STRCPY(buf + len, transchar('|'));
638 l = (int)STRLEN(buf + len);
639 len += l;
640 clen += l;
641 }
642 else
643#endif
644 for ( ; *s != NUL; ++s)
645 {
646 s += skip_status_match_char(xp, s);
647 clen += ptr2cells(s);
648 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
649 {
650 STRNCPY(buf + len, s, l);
651 s += l - 1;
652 len += l;
653 }
654 else
655 {
656 STRCPY(buf + len, transchar_byte(*s));
657 len += (int)STRLEN(buf + len);
658 }
659 }
660 if (i == match)
661 selend = buf + len;
662
663 *(buf + len++) = ' ';
664 *(buf + len++) = ' ';
665 clen += 2;
666 if (++i == num_matches)
667 break;
668 }
669
670 if (i != num_matches)
671 {
672 *(buf + len++) = '>';
673 ++clen;
674 }
675
676 buf[len] = NUL;
677
678 row = cmdline_row - 1;
679 if (row >= 0)
680 {
681 if (wild_menu_showing == 0)
682 {
683 if (msg_scrolled > 0)
684 {
685 // Put the wildmenu just above the command line. If there is
686 // no room, scroll the screen one line up.
687 if (cmdline_row == Rows - 1)
688 {
689 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
690 ++msg_scrolled;
691 }
692 else
693 {
694 ++cmdline_row;
695 ++row;
696 }
697 wild_menu_showing = WM_SCROLLED;
698 }
699 else
700 {
701 // Create status line if needed by setting 'laststatus' to 2.
702 // Set 'winminheight' to zero to avoid that the window is
703 // resized.
704 if (lastwin->w_status_height == 0)
705 {
706 save_p_ls = p_ls;
707 save_p_wmh = p_wmh;
708 p_ls = 2;
709 p_wmh = 0;
710 last_status(FALSE);
711 }
712 wild_menu_showing = WM_SHOWN;
713 }
714 }
715
716 screen_puts(buf, row, 0, attr);
717 if (selstart != NULL && highlight)
718 {
719 *selend = NUL;
720 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
721 }
722
723 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
724 }
725
726 win_redraw_last_status(topframe);
727 vim_free(buf);
728}
729
730/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000731 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200732 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000733 */
734 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100735get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000736{
glepnir977561a2025-02-13 20:48:56 +0100737 int findex = xp->xp_selected;
738 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000739
zeertzjqb6c900b2025-02-14 17:59:31 +0100740 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000741 if (xp->xp_numfiles <= 0)
742 return NULL;
743
744 if (mode == WILD_PREV)
745 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100746 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000747 if (findex == -1)
748 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100749 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000750 --findex;
751 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000752 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000753 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100754 // Select the next entry
755 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000756 }
glepnir977561a2025-02-13 20:48:56 +0100757 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000758 {
glepnir977561a2025-02-13 20:48:56 +0100759 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
760 ht = pum_get_height();
761 if (ht > 3)
762 ht -= 2;
763
764 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000765 {
glepnir977561a2025-02-13 20:48:56 +0100766 if (findex == 0)
767 // at the first entry, don't select any entries
768 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100769 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100770 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000771 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100772 else
773 // go up by the pum height
774 findex = MAX(findex - ht, 0);
775 }
776 else // mode == WILD_PAGEDOWN
777 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100778 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100779 // at the last entry, don't select any entries
780 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100781 else if (findex < 0)
782 // no entry is selected, select the first entry
783 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100784 else
785 // go down by the pum height
786 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000787 }
788 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000789
glepnir977561a2025-02-13 20:48:56 +0100790 // Handle wrapping around
791 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000792 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100793 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100794 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796 else
glepnir977561a2025-02-13 20:48:56 +0100797 // Wrap around to opposite end
798 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000799 }
glepnir977561a2025-02-13 20:48:56 +0100800
801 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000802 if (compl_match_array)
803 {
804 compl_selected = findex;
805 cmdline_pum_display();
806 }
807 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100808 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
809 cmd_showtail);
810
zeertzjqe9ef3472023-08-17 23:57:05 +0200811 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100812 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100813 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000814}
815
816/*
817 * Start the command-line expansion and get the matches.
818 */
819 static char_u *
820ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
821{
822 int non_suf_match; // number without matching suffix
823 int i;
824 char_u *ss = NULL;
825
826 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000827 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000828 options) == FAIL)
829 {
830#ifdef FNAME_ILLEGAL
831 // Illegal file name has been silently skipped. But when there
832 // are wildcards, the real problem is that there was no match,
833 // causing the pattern to be added, which has illegal characters.
834 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
835 semsg(_(e_no_match_str_2), str);
836#endif
837 }
838 else if (xp->xp_numfiles == 0)
839 {
840 if (!(options & WILD_SILENT))
841 semsg(_(e_no_match_str_2), str);
842 }
843 else
844 {
845 // Escape the matches for use on the command line.
846 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
847
848 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000849 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000850 {
851 if (xp->xp_numfiles)
852 non_suf_match = xp->xp_numfiles;
853 else
854 non_suf_match = 1;
855 if ((xp->xp_context == EXPAND_FILES
856 || xp->xp_context == EXPAND_DIRECTORIES)
857 && xp->xp_numfiles > 1)
858 {
859 // More than one match; check suffix.
860 // The files will have been sorted on matching suffix in
861 // expand_wildcards, only need to check the first two.
862 non_suf_match = 0;
863 for (i = 0; i < 2; ++i)
864 if (match_suffix(xp->xp_files[i]))
865 ++non_suf_match;
866 }
867 if (non_suf_match != 1)
868 {
869 // Can we ever get here unless it's while expanding
870 // interactively? If not, we can get rid of this all
871 // together. Don't really want to wait for this message
872 // (and possibly have to hit return to continue!).
873 if (!(options & WILD_SILENT))
874 emsg(_(e_too_many_file_names));
875 else if (!(options & WILD_NO_BEEP))
876 beep_flush();
877 }
878 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
879 ss = vim_strsave(xp->xp_files[0]);
880 }
881 }
882
883 return ss;
884}
885
886/*
887 * Return the longest common part in the list of cmdline completion matches.
888 */
889 static char_u *
890find_longest_match(expand_T *xp, int options)
891{
892 long_u len;
893 int mb_len = 1;
894 int c0, ci;
895 int i;
896 char_u *ss;
897
898 for (len = 0; xp->xp_files[0][len]; len += mb_len)
899 {
900 if (has_mbyte)
901 {
902 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
903 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
904 }
905 else
906 c0 = xp->xp_files[0][len];
907 for (i = 1; i < xp->xp_numfiles; ++i)
908 {
909 if (has_mbyte)
910 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
911 else
912 ci = xp->xp_files[i][len];
913 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
914 || xp->xp_context == EXPAND_FILES
915 || xp->xp_context == EXPAND_SHELLCMD
916 || xp->xp_context == EXPAND_BUFFERS))
917 {
918 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
919 break;
920 }
921 else if (c0 != ci)
922 break;
923 }
924 if (i < xp->xp_numfiles)
925 {
926 if (!(options & WILD_NO_BEEP))
927 vim_beep(BO_WILD);
928 break;
929 }
930 }
931
932 ss = alloc(len + 1);
933 if (ss)
934 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
935
936 return ss;
937}
938
939/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000940 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200941 * Chars that should not be expanded must be preceded with a backslash.
942 * Return a pointer to allocated memory containing the new string.
943 * Return NULL for failure.
944 *
945 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200946 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
947 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200948 *
949 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
950 * is WILD_EXPAND_FREE or WILD_ALL.
951 *
952 * mode = WILD_FREE: just free previously expanded matches
953 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
954 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
955 * mode = WILD_NEXT: use next match in multiple match, wrap to first
956 * mode = WILD_PREV: use previous match in multiple match, wrap to first
957 * mode = WILD_ALL: return all matches concatenated
958 * mode = WILD_LONGEST: return longest matched part
959 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000960 * mode = WILD_APPLY: apply the item selected in the cmdline completion
961 * popup menu and close the menu.
962 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
963 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200964 *
965 * options = WILD_LIST_NOTFOUND: list entries without a match
966 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
967 * options = WILD_USE_NL: Use '\n' for WILD_ALL
968 * options = WILD_NO_BEEP: Don't beep for multiple matches
969 * options = WILD_ADD_SLASH: add a slash after directory names
970 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
971 * options = WILD_SILENT: don't print warning messages
972 * options = WILD_ESCAPE: put backslash before special chars
973 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200974 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200975 *
976 * The variables xp->xp_context and xp->xp_backslash must have been set!
977 */
978 char_u *
979ExpandOne(
980 expand_T *xp,
981 char_u *str,
982 char_u *orig, // allocated copy of original of expanded string
983 int options,
984 int mode)
985{
986 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200987 int orig_saved = FALSE;
988 int i;
989 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200990
991 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000992 if (mode == WILD_NEXT || mode == WILD_PREV
993 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200994 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200995
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000996 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200997 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000998 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200999 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001000 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001001 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001002
Bram Moolenaar66b51422019-08-18 21:44:12 +02001003 // free old names
1004 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1005 {
1006 FreeWild(xp->xp_numfiles, xp->xp_files);
1007 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001008 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001009
1010 // The entries from xp_files may be used in the PUM, remove it.
1011 if (compl_match_array != NULL)
1012 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001014 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001015
1016 if (mode == WILD_FREE) // only release file name
1017 return NULL;
1018
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001019 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001020 {
zeertzjq28a23602023-09-29 19:58:35 +02001021 vim_free(xp->xp_orig);
1022 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023 orig_saved = TRUE;
1024
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001025 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001026 }
1027
1028 // Find longest common part
1029 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1030 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001031 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001032 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 }
1034
Bram Moolenaar57e95172022-08-20 19:26:14 +01001035 // Concatenate all matching names. Unless interrupted, this can be slow
1036 // and the result probably won't be used.
1037 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001038 {
1039 len = 0;
1040 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001041 {
1042 if (i > 0)
1043 {
1044 if (xp->xp_prefix == XP_PREFIX_NO)
1045 len += 2; // prefix "no"
1046 else if (xp->xp_prefix == XP_PREFIX_INV)
1047 len += 3; // prefix "inv"
1048 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001049 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001050 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001051 ss = alloc(len);
1052 if (ss != NULL)
1053 {
1054 *ss = NUL;
1055 for (i = 0; i < xp->xp_numfiles; ++i)
1056 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001057 if (i > 0)
1058 {
1059 if (xp->xp_prefix == XP_PREFIX_NO)
1060 STRCAT(ss, "no");
1061 else if (xp->xp_prefix == XP_PREFIX_INV)
1062 STRCAT(ss, "inv");
1063 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001064 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001065
Bram Moolenaar66b51422019-08-18 21:44:12 +02001066 if (i != xp->xp_numfiles - 1)
1067 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1068 }
1069 }
1070 }
1071
1072 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1073 ExpandCleanup(xp);
1074
zeertzjq28a23602023-09-29 19:58:35 +02001075 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 if (!orig_saved)
1077 vim_free(orig);
1078
1079 return ss;
1080}
1081
1082/*
1083 * Prepare an expand structure for use.
1084 */
1085 void
1086ExpandInit(expand_T *xp)
1087{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001088 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001089 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001090 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001091 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001092}
1093
1094/*
1095 * Cleanup an expand structure after use.
1096 */
1097 void
1098ExpandCleanup(expand_T *xp)
1099{
1100 if (xp->xp_numfiles >= 0)
1101 {
1102 FreeWild(xp->xp_numfiles, xp->xp_files);
1103 xp->xp_numfiles = -1;
1104 }
zeertzjq28a23602023-09-29 19:58:35 +02001105 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001106}
1107
1108/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001109 * Display one line of completion matches. Multiple matches are displayed in
1110 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001111 * matches - list of completion match names
1112 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001113 * lines - number of output lines
1114 * linenr - line number of matches to display
1115 * maxlen - maximum number of characters in each line
1116 * showtail - display only the tail of the full path of a file name
1117 * dir_attr - highlight attribute to use for directory names
1118 */
1119 static void
1120showmatches_oneline(
1121 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001122 char_u **matches,
1123 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001124 int lines,
1125 int linenr,
1126 int maxlen,
1127 int showtail,
1128 int dir_attr)
1129{
1130 int i, j;
1131 int isdir;
1132 int lastlen;
1133 char_u *p;
1134
1135 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001136 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001137 {
1138 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1139 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001140 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1141 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001142 msg_advance(maxlen + 1);
1143 msg_puts((char *)p);
1144 msg_advance(maxlen + 3);
1145 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1146 break;
1147 }
1148 for (i = maxlen - lastlen; --i >= 0; )
1149 msg_putchar(' ');
1150 if (xp->xp_context == EXPAND_FILES
1151 || xp->xp_context == EXPAND_SHELLCMD
1152 || xp->xp_context == EXPAND_BUFFERS)
1153 {
1154 // highlight directories
1155 if (xp->xp_numfiles != -1)
1156 {
1157 char_u *halved_slash;
1158 char_u *exp_path;
1159 char_u *path;
1160
1161 // Expansion was done before and special characters
1162 // were escaped, need to halve backslashes. Also
1163 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001164 exp_path = expand_env_save_opt(matches[j], TRUE);
1165 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001166 halved_slash = backslash_halve_save(path);
1167 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001168 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001169 vim_free(exp_path);
1170 if (halved_slash != path)
1171 vim_free(halved_slash);
1172 }
1173 else
1174 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001175 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001176 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001177 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001178 else
1179 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001180 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001181 TRUE);
1182 p = NameBuff;
1183 }
1184 }
1185 else
1186 {
1187 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001188 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001189 }
1190 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1191 }
1192 if (msg_col > 0) // when not wrapped around
1193 {
1194 msg_clr_eos();
1195 msg_putchar('\n');
1196 }
1197 out_flush(); // show one line at a time
1198}
1199
1200/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001201 * Show all matches for completion on the command line.
1202 * Returns EXPAND_NOTHING when the character that triggered expansion should
1203 * be inserted like a normal character.
1204 */
1205 int
1206showmatches(expand_T *xp, int wildmenu UNUSED)
1207{
1208 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001209 int numMatches;
1210 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001211 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001212 int maxlen;
1213 int lines;
1214 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001215 int attr;
1216 int showtail;
1217
1218 if (xp->xp_numfiles == -1)
1219 {
1220 set_expand_context(xp);
1221 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001222 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001223 showtail = expand_showtail(xp);
1224 if (i != EXPAND_OK)
1225 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001226 }
1227 else
1228 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001229 numMatches = xp->xp_numfiles;
1230 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001231 showtail = cmd_showtail;
1232 }
1233
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001234 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001235 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001236 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001237
Bram Moolenaar66b51422019-08-18 21:44:12 +02001238 if (!wildmenu)
1239 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001240 msg_didany = FALSE; // lines_left will be set
1241 msg_start(); // prepare for paging
1242 msg_putchar('\n');
1243 out_flush();
1244 cmdline_row = msg_row;
1245 msg_didany = FALSE; // lines_left will be set again
1246 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248
1249 if (got_int)
1250 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001251 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001252 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001253 else
1254 {
1255 // find the length of the longest file name
1256 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001257 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258 {
1259 if (!showtail && (xp->xp_context == EXPAND_FILES
1260 || xp->xp_context == EXPAND_SHELLCMD
1261 || xp->xp_context == EXPAND_BUFFERS))
1262 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001263 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001264 j = vim_strsize(NameBuff);
1265 }
1266 else
zeertzjqc51a3762022-12-10 10:22:29 +00001267 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268 if (j > maxlen)
1269 maxlen = j;
1270 }
1271
1272 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 else
1275 {
1276 // compute the number of columns and lines for the listing
1277 maxlen += 2; // two spaces between file names
1278 columns = ((int)Columns + 2) / maxlen;
1279 if (columns < 1)
1280 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001281 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001282 }
1283
1284 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1285
1286 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1287 {
1288 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1289 msg_clr_eos();
1290 msg_advance(maxlen - 3);
1291 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1292 }
1293
1294 // list the files line by line
1295 for (i = 0; i < lines; ++i)
1296 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001297 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001298 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001299 if (got_int)
1300 {
1301 got_int = FALSE;
1302 break;
1303 }
1304 }
1305
1306 // we redraw the command below the lines that we have just listed
1307 // This is a bit tricky, but it saves a lot of screen updating.
1308 cmdline_row = msg_row; // will put it back later
1309 }
1310
1311 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001312 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001313
1314 return EXPAND_OK;
1315}
1316
1317/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001318 * gettail() version for showmatches() and win_redr_status_matches():
1319 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001320 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001321 static char_u *
1322showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001323{
1324 char_u *p;
1325 char_u *t = s;
1326 int had_sep = FALSE;
1327
1328 for (p = s; *p != NUL; )
1329 {
1330 if (vim_ispathsep(*p)
1331#ifdef BACKSLASH_IN_FILENAME
1332 && !rem_backslash(p)
1333#endif
1334 )
1335 had_sep = TRUE;
1336 else if (had_sep)
1337 {
1338 t = p;
1339 had_sep = FALSE;
1340 }
1341 MB_PTR_ADV(p);
1342 }
1343 return t;
1344}
1345
1346/*
1347 * Return TRUE if we only need to show the tail of completion matches.
1348 * When not completing file names or there is a wildcard in the path FALSE is
1349 * returned.
1350 */
1351 static int
1352expand_showtail(expand_T *xp)
1353{
1354 char_u *s;
1355 char_u *end;
1356
1357 // When not completing file names a "/" may mean something different.
1358 if (xp->xp_context != EXPAND_FILES
1359 && xp->xp_context != EXPAND_SHELLCMD
1360 && xp->xp_context != EXPAND_DIRECTORIES)
1361 return FALSE;
1362
1363 end = gettail(xp->xp_pattern);
1364 if (end == xp->xp_pattern) // there is no path separator
1365 return FALSE;
1366
1367 for (s = xp->xp_pattern; s < end; s++)
1368 {
1369 // Skip escaped wildcards. Only when the backslash is not a path
1370 // separator, on DOS the '*' "path\*\file" must not be skipped.
1371 if (rem_backslash(s))
1372 ++s;
1373 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1374 return FALSE;
1375 }
1376 return TRUE;
1377}
1378
1379/*
1380 * Prepare a string for expansion.
1381 * When expanding file names: The string will be used with expand_wildcards().
1382 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1383 * When expanding other names: The string will be used with regcomp(). Copy
1384 * the name into allocated memory and prepend "^".
1385 */
1386 char_u *
1387addstar(
1388 char_u *fname,
1389 int len,
1390 int context) // EXPAND_FILES etc.
1391{
1392 char_u *retval;
1393 int i, j;
1394 int new_len;
1395 char_u *tail;
1396 int ends_in_star;
1397
1398 if (context != EXPAND_FILES
1399 && context != EXPAND_FILES_IN_PATH
1400 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001401 && context != EXPAND_DIRECTORIES
1402 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001403 {
1404 // Matching will be done internally (on something other than files).
1405 // So we convert the file-matching-type wildcards into our kind for
1406 // use with vim_regcomp(). First work out how long it will be:
1407
1408 // For help tags the translation is done in find_help_tags().
1409 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001410 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001411 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001412 || context == EXPAND_COLORS
1413 || context == EXPAND_COMPILER
1414 || context == EXPAND_OWNSYNTAX
1415 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001416 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001417 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001418 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001419 || ((context == EXPAND_TAGS_LISTFILES
1420 || context == EXPAND_TAGS)
1421 && fname[0] == '/'))
1422 retval = vim_strnsave(fname, len);
1423 else
1424 {
1425 new_len = len + 2; // +2 for '^' at start, NUL at end
1426 for (i = 0; i < len; i++)
1427 {
1428 if (fname[i] == '*' || fname[i] == '~')
1429 new_len++; // '*' needs to be replaced by ".*"
1430 // '~' needs to be replaced by "\~"
1431
1432 // Buffer names are like file names. "." should be literal
1433 if (context == EXPAND_BUFFERS && fname[i] == '.')
1434 new_len++; // "." becomes "\."
1435
1436 // Custom expansion takes care of special things, match
1437 // backslashes literally (perhaps also for other types?)
1438 if ((context == EXPAND_USER_DEFINED
1439 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1440 new_len++; // '\' becomes "\\"
1441 }
1442 retval = alloc(new_len);
1443 if (retval != NULL)
1444 {
1445 retval[0] = '^';
1446 j = 1;
1447 for (i = 0; i < len; i++, j++)
1448 {
1449 // Skip backslash. But why? At least keep it for custom
1450 // expansion.
1451 if (context != EXPAND_USER_DEFINED
1452 && context != EXPAND_USER_LIST
1453 && fname[i] == '\\'
1454 && ++i == len)
1455 break;
1456
1457 switch (fname[i])
1458 {
1459 case '*': retval[j++] = '.';
1460 break;
1461 case '~': retval[j++] = '\\';
1462 break;
1463 case '?': retval[j] = '.';
1464 continue;
1465 case '.': if (context == EXPAND_BUFFERS)
1466 retval[j++] = '\\';
1467 break;
1468 case '\\': if (context == EXPAND_USER_DEFINED
1469 || context == EXPAND_USER_LIST)
1470 retval[j++] = '\\';
1471 break;
1472 }
1473 retval[j] = fname[i];
1474 }
1475 retval[j] = NUL;
1476 }
1477 }
1478 }
1479 else
1480 {
1481 retval = alloc(len + 4);
1482 if (retval != NULL)
1483 {
1484 vim_strncpy(retval, fname, len);
1485
1486 // Don't add a star to *, ~, ~user, $var or `cmd`.
1487 // * would become **, which walks the whole tree.
1488 // ~ would be at the start of the file name, but not the tail.
1489 // $ could be anywhere in the tail.
1490 // ` could be anywhere in the file name.
1491 // When the name ends in '$' don't add a star, remove the '$'.
1492 tail = gettail(retval);
1493 ends_in_star = (len > 0 && retval[len - 1] == '*');
1494#ifndef BACKSLASH_IN_FILENAME
1495 for (i = len - 2; i >= 0; --i)
1496 {
1497 if (retval[i] != '\\')
1498 break;
1499 ends_in_star = !ends_in_star;
1500 }
1501#endif
1502 if ((*retval != '~' || tail != retval)
1503 && !ends_in_star
1504 && vim_strchr(tail, '$') == NULL
1505 && vim_strchr(retval, '`') == NULL)
1506 retval[len++] = '*';
1507 else if (len > 0 && retval[len - 1] == '$')
1508 --len;
1509 retval[len] = NUL;
1510 }
1511 }
1512 return retval;
1513}
1514
1515/*
1516 * Must parse the command line so far to work out what context we are in.
1517 * Completion can then be done based on that context.
1518 * This routine sets the variables:
1519 * xp->xp_pattern The start of the pattern to be expanded within
1520 * the command line (ends at the cursor).
1521 * xp->xp_context The type of thing to expand. Will be one of:
1522 *
1523 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1524 * the command line, like an unknown command. Caller
1525 * should beep.
1526 * EXPAND_NOTHING Unrecognised context for completion, use char like
1527 * a normal char, rather than for completion. eg
1528 * :s/^I/
1529 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1530 * it.
1531 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1532 * EXPAND_FILES After command with EX_XFILE set, or after setting
1533 * with P_EXPAND set. eg :e ^I, :w>>^I
1534 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001535 * when we know only directories are of interest.
1536 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001537 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1538 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1539 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1540 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1541 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1542 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1543 * EXPAND_EVENTS Complete event names
1544 * EXPAND_SYNTAX Complete :syntax command arguments
1545 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1546 * EXPAND_AUGROUP Complete autocommand group names
1547 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1548 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1549 * eg :unmap a^I , :cunab x^I
1550 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1551 * eg :call sub^I
1552 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1553 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1554 * names in expressions, eg :while s^I
1555 * EXPAND_ENV_VARS Complete environment variable names
1556 * EXPAND_USER Complete user names
1557 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001558 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001559set_expand_context(expand_T *xp)
1560{
1561 cmdline_info_T *ccline = get_cmdline_info();
1562
1563 // only expansion for ':', '>' and '=' command-lines
1564 if (ccline->cmdfirstc != ':'
1565#ifdef FEAT_EVAL
1566 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1567 && !ccline->input_fn
1568#endif
1569 )
1570 {
1571 xp->xp_context = EXPAND_NOTHING;
1572 return;
1573 }
1574 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1575}
1576
Bram Moolenaard0190392019-08-23 21:17:35 +02001577/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001578 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1579 * For user defined commands, the completion context is set in 'xp' and the
1580 * completion flags in 'complp'.
1581 *
1582 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001583 */
1584 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001585set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001586{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001587 char_u *p = NULL;
1588 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001589 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001590
1591 // Isolate the command and search for it in the command table.
1592 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001593 // - the 'k' command can directly be followed by any character, but do
1594 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1595 // find matches anywhere in the command name, do this only for command
1596 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001597 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001598 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001599 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001600 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001601 p = cmd + 1;
1602 }
1603 else
1604 {
1605 p = cmd;
1606 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1607 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001608 // A user command may contain digits.
1609 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1610 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 while (ASCII_ISALNUM(*p) || *p == '*')
1612 ++p;
1613 // for python 3.x: ":py3*" commands completion
1614 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1615 {
1616 ++p;
1617 while (ASCII_ISALPHA(*p) || *p == '*')
1618 ++p;
1619 }
1620 // check for non-alpha command
1621 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1622 ++p;
1623 len = (int)(p - cmd);
1624
1625 if (len == 0)
1626 {
1627 xp->xp_context = EXPAND_UNSUCCESSFUL;
1628 return NULL;
1629 }
1630
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001631 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001632
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001633 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001634 // Also when doing fuzzy expansion for non-shell commands, support
1635 // alphanumeric characters.
1636 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1637 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001638 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1639 ++p;
1640 }
1641
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001642 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001643 // character, complete the command name.
1644 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1645 return NULL;
1646
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001647 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001648 {
1649 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1650 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001651 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001652 p = cmd + 1;
1653 }
1654 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1655 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001656 eap->cmd = cmd;
1657 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001658 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001659 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001660 }
1661 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001662 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001663 {
1664 // Not still touching the command and it was an illegal one
1665 xp->xp_context = EXPAND_UNSUCCESSFUL;
1666 return NULL;
1667 }
1668
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 return p;
1670}
Bram Moolenaard0190392019-08-23 21:17:35 +02001671
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672/*
1673 * Set the completion context for a command argument with wild card characters.
1674 */
1675 static void
1676set_context_for_wildcard_arg(
1677 exarg_T *eap,
1678 char_u *arg,
1679 int usefilter,
1680 expand_T *xp,
1681 int *complp)
1682{
1683 char_u *p;
1684 int c;
1685 int in_quote = FALSE;
1686 char_u *bow = NULL; // Beginning of word
1687 int len = 0;
1688
1689 // Allow spaces within back-quotes to count as part of the argument
1690 // being expanded.
1691 xp->xp_pattern = skipwhite(arg);
1692 p = xp->xp_pattern;
1693 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001694 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001695 if (has_mbyte)
1696 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001697 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001698 c = *p;
1699 if (c == '\\' && p[1] != NUL)
1700 ++p;
1701 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001702 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001703 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001705 xp->xp_pattern = p;
1706 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 in_quote = !in_quote;
1709 }
1710 // An argument can contain just about everything, except
1711 // characters that end the command and white space.
1712 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001713#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001714 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001715#endif
1716 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001717 {
1718 len = 0; // avoid getting stuck when space is in 'isfname'
1719 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001720 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001721 if (has_mbyte)
1722 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001723 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001724 c = *p;
1725 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001726 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 if (has_mbyte)
1728 len = (*mb_ptr2len)(p);
1729 else
1730 len = 1;
1731 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001732 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001733 if (in_quote)
1734 bow = p;
1735 else
1736 xp->xp_pattern = p;
1737 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001738 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001739 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001740 }
1741
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 // If we are still inside the quotes, and we passed a space, just
1743 // expand from there.
1744 if (bow != NULL && in_quote)
1745 xp->xp_pattern = bow;
1746 xp->xp_context = EXPAND_FILES;
1747
1748 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001749 if (usefilter
1750 || (eap != NULL
1751 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1752 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001753 {
1754#ifndef BACKSLASH_IN_FILENAME
1755 xp->xp_shell = TRUE;
1756#endif
1757 // When still after the command name expand executables.
1758 if (xp->xp_pattern == skipwhite(arg))
1759 xp->xp_context = EXPAND_SHELLCMD;
1760 }
1761
1762 // Check for environment variable.
1763 if (*xp->xp_pattern == '$')
1764 {
1765 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1766 if (!vim_isIDc(*p))
1767 break;
1768 if (*p == NUL)
1769 {
1770 xp->xp_context = EXPAND_ENV_VARS;
1771 ++xp->xp_pattern;
1772 // Avoid that the assignment uses EXPAND_FILES again.
1773 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1774 *complp = EXPAND_ENV_VARS;
1775 }
1776 }
1777 // Check for user names.
1778 if (*xp->xp_pattern == '~')
1779 {
1780 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1781 ;
1782 // Complete ~user only if it partially matches a user name.
1783 // A full match ~user<Tab> will be replaced by user's home
1784 // directory i.e. something like ~user<Tab> -> /home/user/
1785 if (*p == NUL && p > xp->xp_pattern + 1
1786 && match_user(xp->xp_pattern + 1) >= 1)
1787 {
1788 xp->xp_context = EXPAND_USER;
1789 ++xp->xp_pattern;
1790 }
1791 }
1792}
1793
1794/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001795 * Set the completion context for the "++opt=arg" argument. Always returns
1796 * NULL.
1797 */
1798 static char_u *
1799set_context_in_argopt(expand_T *xp, char_u *arg)
1800{
1801 char_u *p;
1802
1803 p = vim_strchr(arg, '=');
1804 if (p == NULL)
1805 xp->xp_pattern = arg;
1806 else
1807 xp->xp_pattern = p + 1;
1808
1809 xp->xp_context = EXPAND_ARGOPT;
1810 return NULL;
1811}
1812
1813#ifdef FEAT_TERMINAL
1814/*
1815 * Set the completion context for :terminal's [options]. Always returns NULL.
1816 */
1817 static char_u *
1818set_context_in_terminalopt(expand_T *xp, char_u *arg)
1819{
1820 char_u *p;
1821
1822 p = vim_strchr(arg, '=');
1823 if (p == NULL)
1824 xp->xp_pattern = arg;
1825 else
1826 xp->xp_pattern = p + 1;
1827
1828 xp->xp_context = EXPAND_TERMINALOPT;
1829 return NULL;
1830}
1831#endif
1832
1833/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001834 * Set the completion context for the :filter command. Returns a pointer to the
1835 * next command after the :filter command.
1836 */
1837 static char_u *
1838set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1839{
1840 if (*arg != NUL)
1841 arg = skip_vimgrep_pat(arg, NULL, NULL);
1842 if (arg == NULL || *arg == NUL)
1843 {
1844 xp->xp_context = EXPAND_NOTHING;
1845 return NULL;
1846 }
1847 return skipwhite(arg);
1848}
1849
1850#ifdef FEAT_SEARCH_EXTRA
1851/*
1852 * Set the completion context for the :match command. Returns a pointer to the
1853 * next command after the :match command.
1854 */
1855 static char_u *
1856set_context_in_match_cmd(expand_T *xp, char_u *arg)
1857{
1858 if (*arg == NUL || !ends_excmd(*arg))
1859 {
1860 // also complete "None"
1861 set_context_in_echohl_cmd(xp, arg);
1862 arg = skipwhite(skiptowhite(arg));
1863 if (*arg != NUL)
1864 {
1865 xp->xp_context = EXPAND_NOTHING;
1866 arg = skip_regexp(arg + 1, *arg, magic_isset());
1867 }
1868 }
1869 return find_nextcmd(arg);
1870}
1871#endif
1872
1873/*
1874 * Returns a pointer to the next command after a :global or a :v command.
1875 * Returns NULL if there is no next command.
1876 */
1877 static char_u *
1878find_cmd_after_global_cmd(char_u *arg)
1879{
1880 int delim;
1881
1882 delim = *arg; // get the delimiter
1883 if (delim)
1884 ++arg; // skip delimiter if there is one
1885
1886 while (arg[0] != NUL && arg[0] != delim)
1887 {
1888 if (arg[0] == '\\' && arg[1] != NUL)
1889 ++arg;
1890 ++arg;
1891 }
1892 if (arg[0] != NUL)
1893 return arg + 1;
1894
1895 return NULL;
1896}
1897
1898/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001899 * Returns a pointer to the next command after a :substitute or a :& command.
1900 * Returns NULL if there is no next command.
1901 */
1902 static char_u *
1903find_cmd_after_substitute_cmd(char_u *arg)
1904{
1905 int delim;
1906
1907 delim = *arg;
1908 if (delim)
1909 {
1910 // skip "from" part
1911 ++arg;
1912 arg = skip_regexp(arg, delim, magic_isset());
1913
1914 if (arg[0] != NUL && arg[0] == delim)
1915 {
1916 // skip "to" part
1917 ++arg;
1918 while (arg[0] != NUL && arg[0] != delim)
1919 {
1920 if (arg[0] == '\\' && arg[1] != NUL)
1921 ++arg;
1922 ++arg;
1923 }
1924 if (arg[0] != NUL) // skip delimiter
1925 ++arg;
1926 }
1927 }
1928 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1929 ++arg;
1930 if (arg[0] != NUL)
1931 return arg;
1932
1933 return NULL;
1934}
1935
1936/*
1937 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1938 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1939 * Returns NULL if there is no next command.
1940 */
1941 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001942find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001943{
1944 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001945 if (*arg != '/')
1946 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001947
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001948 // Match regexp, not just whole words
1949 for (++arg; *arg && *arg != '/'; arg++)
1950 if (*arg == '\\' && arg[1] != NUL)
1951 arg++;
1952 if (*arg)
1953 {
1954 arg = skipwhite(arg + 1);
1955
1956 // Check for trailing illegal characters
1957 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1958 xp->xp_context = EXPAND_NOTHING;
1959 else
1960 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001961 }
1962
1963 return NULL;
1964}
1965
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001966#ifdef FEAT_EVAL
1967/*
1968 * Set the completion context for the :unlet command. Always returns NULL.
1969 */
1970 static char_u *
1971set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1972{
1973 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1974 arg = xp->xp_pattern + 1;
1975
1976 xp->xp_context = EXPAND_USER_VARS;
1977 xp->xp_pattern = arg;
1978
1979 if (*xp->xp_pattern == '$')
1980 {
1981 xp->xp_context = EXPAND_ENV_VARS;
1982 ++xp->xp_pattern;
1983 }
1984
1985 return NULL;
1986}
1987#endif
1988
1989#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1990/*
1991 * Set the completion context for the :language command. Always returns NULL.
1992 */
1993 static char_u *
1994set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1995{
1996 char_u *p;
1997
1998 p = skiptowhite(arg);
1999 if (*p == NUL)
2000 {
2001 xp->xp_context = EXPAND_LANGUAGE;
2002 xp->xp_pattern = arg;
2003 }
2004 else
2005 {
2006 if ( STRNCMP(arg, "messages", p - arg) == 0
2007 || STRNCMP(arg, "ctype", p - arg) == 0
2008 || STRNCMP(arg, "time", p - arg) == 0
2009 || STRNCMP(arg, "collate", p - arg) == 0)
2010 {
2011 xp->xp_context = EXPAND_LOCALES;
2012 xp->xp_pattern = skipwhite(p);
2013 }
2014 else
2015 xp->xp_context = EXPAND_NOTHING;
2016 }
2017
2018 return NULL;
2019}
2020#endif
2021
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002022#ifdef FEAT_EVAL
2023static enum
2024{
2025 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002026 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2027 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002028} breakpt_expand_what;
2029
2030/*
2031 * Set the completion context for the :breakadd command. Always returns NULL.
2032 */
2033 static char_u *
2034set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2035{
2036 char_u *p;
2037 char_u *subcmd_start;
2038
2039 xp->xp_context = EXPAND_BREAKPOINT;
2040 xp->xp_pattern = arg;
2041
2042 if (cmdidx == CMD_breakadd)
2043 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002044 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002045 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002046 else
2047 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002048
2049 p = skipwhite(arg);
2050 if (*p == NUL)
2051 return NULL;
2052 subcmd_start = p;
2053
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002054 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002055 {
2056 // :breakadd file [lnum] <filename>
2057 // :breakadd func [lnum] <funcname>
2058 p += 4;
2059 p = skipwhite(p);
2060
2061 // skip line number (if specified)
2062 if (VIM_ISDIGIT(*p))
2063 {
2064 p = skipdigits(p);
2065 if (*p != ' ')
2066 {
2067 xp->xp_context = EXPAND_NOTHING;
2068 return NULL;
2069 }
2070 p = skipwhite(p);
2071 }
2072 if (STRNCMP("file", subcmd_start, 4) == 0)
2073 xp->xp_context = EXPAND_FILES;
2074 else
2075 xp->xp_context = EXPAND_USER_FUNC;
2076 xp->xp_pattern = p;
2077 }
2078 else if (STRNCMP("expr ", p, 5) == 0)
2079 {
2080 // :breakadd expr <expression>
2081 xp->xp_context = EXPAND_EXPRESSION;
2082 xp->xp_pattern = skipwhite(p + 5);
2083 }
2084
2085 return NULL;
2086}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002087
2088 static char_u *
2089set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2090{
2091 char_u *p;
2092
2093 xp->xp_context = EXPAND_NOTHING;
2094 xp->xp_pattern = NULL;
2095
2096 p = skipwhite(arg);
2097 if (VIM_ISDIGIT(*p))
2098 return NULL;
2099
2100 xp->xp_context = EXPAND_SCRIPTNAMES;
2101 xp->xp_pattern = p;
2102
2103 return NULL;
2104}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002105#endif
2106
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002107/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002108 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2109 * The argument to the command is 'arg' and the argument flags is 'argt'.
2110 * For user-defined commands and for environment variables, 'compl' has the
2111 * completion type.
2112 * Returns a pointer to the next command. Returns NULL if there is no next
2113 * command.
2114 */
2115 static char_u *
2116set_context_by_cmdname(
2117 char_u *cmd,
2118 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002119 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002120 char_u *arg,
2121 long argt,
2122 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002123 int forceit)
2124{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002125 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002126 {
2127 case CMD_find:
2128 case CMD_sfind:
2129 case CMD_tabfind:
2130 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002131 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002132 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002133 break;
2134 case CMD_cd:
2135 case CMD_chdir:
2136 case CMD_tcd:
2137 case CMD_tchdir:
2138 case CMD_lcd:
2139 case CMD_lchdir:
2140 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002141 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002142 break;
2143 case CMD_help:
2144 xp->xp_context = EXPAND_HELP;
2145 xp->xp_pattern = arg;
2146 break;
2147
2148 // Command modifiers: return the argument.
2149 // Also for commands with an argument that is a command.
2150 case CMD_aboveleft:
2151 case CMD_argdo:
2152 case CMD_belowright:
2153 case CMD_botright:
2154 case CMD_browse:
2155 case CMD_bufdo:
2156 case CMD_cdo:
2157 case CMD_cfdo:
2158 case CMD_confirm:
2159 case CMD_debug:
2160 case CMD_folddoclosed:
2161 case CMD_folddoopen:
2162 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002163 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002164 case CMD_keepalt:
2165 case CMD_keepjumps:
2166 case CMD_keepmarks:
2167 case CMD_keeppatterns:
2168 case CMD_ldo:
2169 case CMD_leftabove:
2170 case CMD_lfdo:
2171 case CMD_lockmarks:
2172 case CMD_noautocmd:
2173 case CMD_noswapfile:
2174 case CMD_rightbelow:
2175 case CMD_sandbox:
2176 case CMD_silent:
2177 case CMD_tab:
2178 case CMD_tabdo:
2179 case CMD_topleft:
2180 case CMD_verbose:
2181 case CMD_vertical:
2182 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002183 case CMD_vim9cmd:
2184 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002185 return arg;
2186
2187 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002188 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002189
2190#ifdef FEAT_SEARCH_EXTRA
2191 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002192 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002193#endif
2194
2195 // All completion for the +cmdline_compl feature goes here.
2196
2197 case CMD_command:
2198 return set_context_in_user_cmd(xp, arg);
2199
2200 case CMD_delcommand:
2201 xp->xp_context = EXPAND_USER_COMMANDS;
2202 xp->xp_pattern = arg;
2203 break;
2204
2205 case CMD_global:
2206 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002207 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002208 case CMD_and:
2209 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002210 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002211 case CMD_isearch:
2212 case CMD_dsearch:
2213 case CMD_ilist:
2214 case CMD_dlist:
2215 case CMD_ijump:
2216 case CMD_psearch:
2217 case CMD_djump:
2218 case CMD_isplit:
2219 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002220 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002221 case CMD_autocmd:
2222 return set_context_in_autocmd(xp, arg, FALSE);
2223 case CMD_doautocmd:
2224 case CMD_doautoall:
2225 return set_context_in_autocmd(xp, arg, TRUE);
2226 case CMD_set:
2227 set_context_in_set_cmd(xp, arg, 0);
2228 break;
2229 case CMD_setglobal:
2230 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2231 break;
2232 case CMD_setlocal:
2233 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2234 break;
2235 case CMD_tag:
2236 case CMD_stag:
2237 case CMD_ptag:
2238 case CMD_ltag:
2239 case CMD_tselect:
2240 case CMD_stselect:
2241 case CMD_ptselect:
2242 case CMD_tjump:
2243 case CMD_stjump:
2244 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002245 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002246 xp->xp_context = EXPAND_TAGS_LISTFILES;
2247 else
2248 xp->xp_context = EXPAND_TAGS;
2249 xp->xp_pattern = arg;
2250 break;
2251 case CMD_augroup:
2252 xp->xp_context = EXPAND_AUGROUP;
2253 xp->xp_pattern = arg;
2254 break;
2255#ifdef FEAT_SYN_HL
2256 case CMD_syntax:
2257 set_context_in_syntax_cmd(xp, arg);
2258 break;
2259#endif
2260#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002261 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002262 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002263 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002264 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002265 case CMD_if:
2266 case CMD_elseif:
2267 case CMD_while:
2268 case CMD_for:
2269 case CMD_echo:
2270 case CMD_echon:
2271 case CMD_execute:
2272 case CMD_echomsg:
2273 case CMD_echoerr:
2274 case CMD_call:
2275 case CMD_return:
2276 case CMD_cexpr:
2277 case CMD_caddexpr:
2278 case CMD_cgetexpr:
2279 case CMD_lexpr:
2280 case CMD_laddexpr:
2281 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002282 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002283 break;
2284
2285 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002286 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002287 case CMD_function:
2288 case CMD_delfunction:
2289 xp->xp_context = EXPAND_USER_FUNC;
2290 xp->xp_pattern = arg;
2291 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002292 case CMD_disassemble:
2293 set_context_in_disassemble_cmd(xp, arg);
2294 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002295
2296 case CMD_echohl:
2297 set_context_in_echohl_cmd(xp, arg);
2298 break;
2299#endif
2300 case CMD_highlight:
2301 set_context_in_highlight_cmd(xp, arg);
2302 break;
2303#ifdef FEAT_CSCOPE
2304 case CMD_cscope:
2305 case CMD_lcscope:
2306 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002307 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002308 break;
2309#endif
2310#ifdef FEAT_SIGNS
2311 case CMD_sign:
2312 set_context_in_sign_cmd(xp, arg);
2313 break;
2314#endif
2315 case CMD_bdelete:
2316 case CMD_bwipeout:
2317 case CMD_bunload:
2318 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2319 arg = xp->xp_pattern + 1;
2320 // FALLTHROUGH
2321 case CMD_buffer:
2322 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002323 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002324 case CMD_checktime:
2325 xp->xp_context = EXPAND_BUFFERS;
2326 xp->xp_pattern = arg;
2327 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002328#ifdef FEAT_DIFF
2329 case CMD_diffget:
2330 case CMD_diffput:
2331 // If current buffer is in diff mode, complete buffer names
2332 // which are in diff mode, and different than current buffer.
2333 xp->xp_context = EXPAND_DIFF_BUFFERS;
2334 xp->xp_pattern = arg;
2335 break;
2336#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002337 case CMD_USER:
2338 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002339 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2340 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002341
2342 case CMD_map: case CMD_noremap:
2343 case CMD_nmap: case CMD_nnoremap:
2344 case CMD_vmap: case CMD_vnoremap:
2345 case CMD_omap: case CMD_onoremap:
2346 case CMD_imap: case CMD_inoremap:
2347 case CMD_cmap: case CMD_cnoremap:
2348 case CMD_lmap: case CMD_lnoremap:
2349 case CMD_smap: case CMD_snoremap:
2350 case CMD_tmap: case CMD_tnoremap:
2351 case CMD_xmap: case CMD_xnoremap:
2352 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002353 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002354 case CMD_unmap:
2355 case CMD_nunmap:
2356 case CMD_vunmap:
2357 case CMD_ounmap:
2358 case CMD_iunmap:
2359 case CMD_cunmap:
2360 case CMD_lunmap:
2361 case CMD_sunmap:
2362 case CMD_tunmap:
2363 case CMD_xunmap:
2364 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002365 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002366 case CMD_mapclear:
2367 case CMD_nmapclear:
2368 case CMD_vmapclear:
2369 case CMD_omapclear:
2370 case CMD_imapclear:
2371 case CMD_cmapclear:
2372 case CMD_lmapclear:
2373 case CMD_smapclear:
2374 case CMD_tmapclear:
2375 case CMD_xmapclear:
2376 xp->xp_context = EXPAND_MAPCLEAR;
2377 xp->xp_pattern = arg;
2378 break;
2379
2380 case CMD_abbreviate: case CMD_noreabbrev:
2381 case CMD_cabbrev: case CMD_cnoreabbrev:
2382 case CMD_iabbrev: case CMD_inoreabbrev:
2383 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002384 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002385 case CMD_unabbreviate:
2386 case CMD_cunabbrev:
2387 case CMD_iunabbrev:
2388 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002389 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002390#ifdef FEAT_MENU
2391 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2392 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2393 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2394 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2395 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2396 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2397 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2398 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2399 case CMD_tmenu: case CMD_tunmenu:
2400 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2401 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2402#endif
2403
2404 case CMD_colorscheme:
2405 xp->xp_context = EXPAND_COLORS;
2406 xp->xp_pattern = arg;
2407 break;
2408
2409 case CMD_compiler:
2410 xp->xp_context = EXPAND_COMPILER;
2411 xp->xp_pattern = arg;
2412 break;
2413
2414 case CMD_ownsyntax:
2415 xp->xp_context = EXPAND_OWNSYNTAX;
2416 xp->xp_pattern = arg;
2417 break;
2418
2419 case CMD_setfiletype:
2420 xp->xp_context = EXPAND_FILETYPE;
2421 xp->xp_pattern = arg;
2422 break;
2423
2424 case CMD_packadd:
2425 xp->xp_context = EXPAND_PACKADD;
2426 xp->xp_pattern = arg;
2427 break;
2428
zeertzjqb0d45ec2023-01-25 15:04:22 +00002429 case CMD_runtime:
2430 set_context_in_runtime_cmd(xp, arg);
2431 break;
2432
Bram Moolenaard0190392019-08-23 21:17:35 +02002433#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2434 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002435 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002436#endif
2437#if defined(FEAT_PROFILE)
2438 case CMD_profile:
2439 set_context_in_profile_cmd(xp, arg);
2440 break;
2441#endif
2442 case CMD_behave:
2443 xp->xp_context = EXPAND_BEHAVE;
2444 xp->xp_pattern = arg;
2445 break;
2446
2447 case CMD_messages:
2448 xp->xp_context = EXPAND_MESSAGES;
2449 xp->xp_pattern = arg;
2450 break;
2451
2452 case CMD_history:
2453 xp->xp_context = EXPAND_HISTORY;
2454 xp->xp_pattern = arg;
2455 break;
2456#if defined(FEAT_PROFILE)
2457 case CMD_syntime:
2458 xp->xp_context = EXPAND_SYNTIME;
2459 xp->xp_pattern = arg;
2460 break;
2461#endif
2462
2463 case CMD_argdelete:
2464 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2465 arg = xp->xp_pattern + 1;
2466 xp->xp_context = EXPAND_ARGLIST;
2467 xp->xp_pattern = arg;
2468 break;
2469
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002470#ifdef FEAT_EVAL
2471 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002472 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002473 case CMD_breakdel:
2474 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002475
2476 case CMD_scriptnames:
2477 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002478#endif
2479
Bram Moolenaard0190392019-08-23 21:17:35 +02002480 default:
2481 break;
2482 }
2483 return NULL;
2484}
2485
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002486/*
2487 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2488 * we don't need/want deleted. Maybe this could be done better if we didn't
2489 * repeat all this stuff. The only problem is that they may not stay
2490 * perfectly compatible with each other, but then the command line syntax
2491 * probably won't change that much -- webb.
2492 */
2493 static char_u *
2494set_one_cmd_context(
2495 expand_T *xp,
2496 char_u *buff) // buffer for command string
2497{
2498 char_u *p;
2499 char_u *cmd, *arg;
2500 int len = 0;
2501 exarg_T ea;
2502 int compl = EXPAND_NOTHING;
2503 int forceit = FALSE;
2504 int usefilter = FALSE; // filter instead of file name
2505
2506 ExpandInit(xp);
2507 xp->xp_pattern = buff;
2508 xp->xp_line = buff;
2509 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2510 ea.argt = 0;
2511
2512 // 1. skip comment lines and leading space, colons or bars
2513 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2514 ;
2515 xp->xp_pattern = cmd;
2516
2517 if (*cmd == NUL)
2518 return NULL;
2519 if (*cmd == '"') // ignore comment lines
2520 {
2521 xp->xp_context = EXPAND_NOTHING;
2522 return NULL;
2523 }
2524
2525 // 3. Skip over the range to find the command.
2526 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2527 xp->xp_pattern = cmd;
2528 if (*cmd == NUL)
2529 return NULL;
2530 if (*cmd == '"')
2531 {
2532 xp->xp_context = EXPAND_NOTHING;
2533 return NULL;
2534 }
2535
2536 if (*cmd == '|' || *cmd == '\n')
2537 return cmd + 1; // There's another command
2538
2539 // Get the command index.
2540 p = set_cmd_index(cmd, &ea, xp, &compl);
2541 if (p == NULL)
2542 return NULL;
2543
2544 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2545
2546 if (*p == '!') // forced commands
2547 {
2548 forceit = TRUE;
2549 ++p;
2550 }
2551
2552 // 6. parse arguments
2553 if (!IS_USER_CMDIDX(ea.cmdidx))
2554 ea.argt = excmd_get_argt(ea.cmdidx);
2555
2556 arg = skipwhite(p);
2557
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002558 // Does command allow "++argopt" argument?
2559 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002560 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002561 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2562 {
2563 p = arg + 2;
2564 while (*p && !vim_isspace(*p))
2565 MB_PTR_ADV(p);
2566
2567 // Still touching the command after "++"?
2568 if (*p == NUL)
2569 {
2570 if (ea.argt & EX_ARGOPT)
2571 return set_context_in_argopt(xp, arg + 2);
2572#ifdef FEAT_TERMINAL
2573 if (ea.cmdidx == CMD_terminal)
2574 return set_context_in_terminalopt(xp, arg + 2);
2575#endif
2576 }
2577
2578 arg = skipwhite(p);
2579 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002580 }
2581
2582 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2583 {
2584 if (*arg == '>') // append
2585 {
2586 if (*++arg == '>')
2587 ++arg;
2588 arg = skipwhite(arg);
2589 }
2590 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2591 {
2592 ++arg;
2593 usefilter = TRUE;
2594 }
2595 }
2596
2597 if (ea.cmdidx == CMD_read)
2598 {
2599 usefilter = forceit; // :r! filter if forced
2600 if (*arg == '!') // :r !filter
2601 {
2602 ++arg;
2603 usefilter = TRUE;
2604 }
2605 }
2606
2607 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2608 {
2609 while (*arg == *cmd) // allow any number of '>' or '<'
2610 ++arg;
2611 arg = skipwhite(arg);
2612 }
2613
2614 // Does command allow "+command"?
2615 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2616 {
2617 // Check if we're in the +command
2618 p = arg + 1;
2619 arg = skip_cmd_arg(arg, FALSE);
2620
2621 // Still touching the command after '+'?
2622 if (*arg == NUL)
2623 return p;
2624
2625 // Skip space(s) after +command to get to the real argument
2626 arg = skipwhite(arg);
2627 }
2628
2629
2630 // Check for '|' to separate commands and '"' to start comments.
2631 // Don't do this for ":read !cmd" and ":write !cmd".
2632 if ((ea.argt & EX_TRLBAR) && !usefilter)
2633 {
2634 p = arg;
2635 // ":redir @" is not the start of a comment
2636 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2637 p += 2;
2638 while (*p)
2639 {
2640 if (*p == Ctrl_V)
2641 {
2642 if (p[1] != NUL)
2643 ++p;
2644 }
2645 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2646 || *p == '|' || *p == '\n')
2647 {
2648 if (*(p - 1) != '\\')
2649 {
2650 if (*p == '|' || *p == '\n')
2651 return p + 1;
2652 return NULL; // It's a comment
2653 }
2654 }
2655 MB_PTR_ADV(p);
2656 }
2657 }
2658
2659 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2660 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2661 // no arguments allowed but there is something
2662 return NULL;
2663
2664 // Find start of last argument (argument just before cursor):
2665 p = buff;
2666 xp->xp_pattern = p;
2667 len = (int)STRLEN(buff);
2668 while (*p && p < buff + len)
2669 {
2670 if (*p == ' ' || *p == TAB)
2671 {
2672 // argument starts after a space
2673 xp->xp_pattern = ++p;
2674 }
2675 else
2676 {
2677 if (*p == '\\' && *(p + 1) != NUL)
2678 ++p; // skip over escaped character
2679 MB_PTR_ADV(p);
2680 }
2681 }
2682
2683 if (ea.argt & EX_XFILE)
2684 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2685
2686 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002687 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002688 forceit);
2689}
2690
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002691/*
2692 * Set the completion context in 'xp' for command 'str'
2693 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002694 void
2695set_cmd_context(
2696 expand_T *xp,
2697 char_u *str, // start of command line
2698 int len, // length of command line (excl. NUL)
2699 int col, // position of cursor
2700 int use_ccline UNUSED) // use ccline for info
2701{
2702#ifdef FEAT_EVAL
2703 cmdline_info_T *ccline = get_cmdline_info();
2704#endif
2705 int old_char = NUL;
2706 char_u *nextcomm;
2707
2708 // Avoid a UMR warning from Purify, only save the character if it has been
2709 // written before.
2710 if (col < len)
2711 old_char = str[col];
2712 str[col] = NUL;
2713 nextcomm = str;
2714
2715#ifdef FEAT_EVAL
2716 if (use_ccline && ccline->cmdfirstc == '=')
2717 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002718 // pass CMD_SIZE because there is no real command
2719 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002720 }
2721 else if (use_ccline && ccline->input_fn)
2722 {
2723 xp->xp_context = ccline->xp_context;
2724 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002726 }
2727 else
2728#endif
2729 while (nextcomm != NULL)
2730 nextcomm = set_one_cmd_context(xp, nextcomm);
2731
2732 // Store the string here so that call_user_expand_func() can get to them
2733 // easily.
2734 xp->xp_line = str;
2735 xp->xp_col = col;
2736
2737 str[col] = old_char;
2738}
2739
2740/*
2741 * Expand the command line "str" from context "xp".
2742 * "xp" must have been set by set_cmd_context().
2743 * xp->xp_pattern points into "str", to where the text that is to be expanded
2744 * starts.
2745 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2746 * cursor.
2747 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2748 * key that triggered expansion literally.
2749 * Returns EXPAND_OK otherwise.
2750 */
2751 int
2752expand_cmdline(
2753 expand_T *xp,
2754 char_u *str, // start of command line
2755 int col, // position of cursor
2756 int *matchcount, // return: nr of matches
2757 char_u ***matches) // return: array of pointers to matches
2758{
2759 char_u *file_str = NULL;
2760 int options = WILD_ADD_SLASH|WILD_SILENT;
2761
2762 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2763 {
2764 beep_flush();
2765 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2766 }
2767 if (xp->xp_context == EXPAND_NOTHING)
2768 {
2769 // Caller can use the character as a normal char instead
2770 return EXPAND_NOTHING;
2771 }
2772
2773 // add star to file name, or convert to regexp if not exp. files.
2774 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002775 if (cmdline_fuzzy_completion_supported(xp))
2776 // If fuzzy matching, don't modify the search string
2777 file_str = vim_strsave(xp->xp_pattern);
2778 else
2779 {
2780 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2781 if (file_str == NULL)
2782 return EXPAND_UNSUCCESSFUL;
2783 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002784
2785 if (p_wic)
2786 options += WILD_ICASE;
2787
2788 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002789 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002790 {
2791 *matchcount = 0;
2792 *matches = NULL;
2793 }
2794 vim_free(file_str);
2795
2796 return EXPAND_OK;
2797}
2798
Bram Moolenaar66b51422019-08-18 21:44:12 +02002799/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002800 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002801 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002802 */
2803 static int
2804expand_files_and_dirs(
2805 expand_T *xp,
2806 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002807 char_u ***matches,
2808 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002809 int flags,
2810 int options)
2811{
2812 int free_pat = FALSE;
2813 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002814 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002815
2816 // for ":set path=" and ":set tags=" halve backslashes for escaped
2817 // space
2818 if (xp->xp_backslash != XP_BS_NONE)
2819 {
2820 free_pat = TRUE;
2821 pat = vim_strsave(pat);
2822 for (i = 0; pat[i]; ++i)
2823 if (pat[i] == '\\')
2824 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002825 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002826 && pat[i + 1] == '\\'
2827 && pat[i + 2] == '\\'
2828 && pat[i + 3] == ' ')
2829 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002830 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002831 && pat[i + 1] == ' ')
2832 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002833 else if ((xp->xp_backslash & XP_BS_COMMA)
2834 && pat[i + 1] == '\\'
2835 && pat[i + 2] == ',')
2836 STRMOVE(pat + i, pat + i + 2);
2837#ifdef BACKSLASH_IN_FILENAME
2838 else if ((xp->xp_backslash & XP_BS_COMMA)
2839 && pat[i + 1] == ',')
2840 STRMOVE(pat + i, pat + i + 1);
2841#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002842 }
2843 }
2844
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002845 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002846 {
2847#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002848 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002849#endif
2850 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002851 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002852 {
2853 if (xp->xp_context == EXPAND_FILES)
2854 flags |= EW_FILE;
2855 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2856 flags |= (EW_FILE | EW_PATH);
2857 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2858 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2859 else
2860 flags = (flags | EW_DIR) & ~EW_FILE;
2861 if (options & WILD_ICASE)
2862 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002863
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002864 // Expand wildcards, supporting %:h and the like.
2865 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2866 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002867 if (free_pat)
2868 vim_free(pat);
2869#ifdef BACKSLASH_IN_FILENAME
2870 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2871 {
2872 int j;
2873
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002874 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002875 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002876 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002877
2878 while (*ptr != NUL)
2879 {
2880 if (p_csl[0] == 's' && *ptr == '\\')
2881 *ptr = '/';
2882 else if (p_csl[0] == 'b' && *ptr == '/')
2883 *ptr = '\\';
2884 ptr += (*mb_ptr2len)(ptr);
2885 }
2886 }
2887 }
2888#endif
2889 return ret;
2890}
2891
2892/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002893 * Function given to ExpandGeneric() to obtain the possible arguments of the
2894 * ":behave {mswin,xterm}" command.
2895 */
2896 static char_u *
2897get_behave_arg(expand_T *xp UNUSED, int idx)
2898{
2899 if (idx == 0)
2900 return (char_u *)"mswin";
2901 if (idx == 1)
2902 return (char_u *)"xterm";
2903 return NULL;
2904}
2905
K.Takata161b6ac2022-11-14 15:31:07 +00002906#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002907/*
2908 * Function given to ExpandGeneric() to obtain the possible arguments of the
2909 * ":breakadd {expr, file, func, here}" command.
2910 * ":breakdel {func, file, here}" command.
2911 */
2912 static char_u *
2913get_breakadd_arg(expand_T *xp UNUSED, int idx)
2914{
2915 char *opts[] = {"expr", "file", "func", "here"};
2916
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002917 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002918 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002919 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002920 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2921 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002922 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2923 {
2924 // breakdel {func, file, here}
2925 if (idx <= 2)
2926 return (char_u *)opts[idx + 1];
2927 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002928 else
2929 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002930 // profdel {func, file}
2931 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002932 return (char_u *)opts[idx + 1];
2933 }
2934 }
2935 return NULL;
2936}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002937
2938/*
2939 * Function given to ExpandGeneric() to obtain the possible arguments for the
2940 * ":scriptnames" command.
2941 */
2942 static char_u *
2943get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2944{
2945 scriptitem_T *si;
2946
2947 if (!SCRIPT_ID_VALID(idx + 1))
2948 return NULL;
2949
2950 si = SCRIPT_ITEM(idx + 1);
2951 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2952 return NameBuff;
2953}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002954#endif
2955
Bram Moolenaard0190392019-08-23 21:17:35 +02002956/*
2957 * Function given to ExpandGeneric() to obtain the possible arguments of the
2958 * ":messages {clear}" command.
2959 */
2960 static char_u *
2961get_messages_arg(expand_T *xp UNUSED, int idx)
2962{
2963 if (idx == 0)
2964 return (char_u *)"clear";
2965 return NULL;
2966}
2967
2968 static char_u *
2969get_mapclear_arg(expand_T *xp UNUSED, int idx)
2970{
2971 if (idx == 0)
2972 return (char_u *)"<buffer>";
2973 return NULL;
2974}
2975
2976/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002977 * Do the expansion based on xp->xp_context and 'rmp'.
2978 */
2979 static int
2980ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002981 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002982 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002983 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002984 char_u ***matches,
2985 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002986{
2987 static struct expgen
2988 {
2989 int context;
2990 char_u *((*func)(expand_T *, int));
2991 int ic;
2992 int escaped;
2993 } tab[] =
2994 {
2995 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2996 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2997 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2998 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2999 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3000 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3001 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3002 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3003 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3004 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003005#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003006 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3007 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3008 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3009 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3010 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003011#endif
3012#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003013 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3014 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003015#endif
3016#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003017 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003018#endif
3019#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003020 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003021#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003022 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3023 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3024 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003025#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003026 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003027#endif
3028#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003029 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003030#endif
3031#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003032 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003033#endif
3034#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003035 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3036 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003037#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003038 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3039 {EXPAND_USER, get_users, TRUE, FALSE},
3040 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003041#ifdef FEAT_EVAL
3042 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003043 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003044#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003045 };
3046 int i;
3047 int ret = FAIL;
3048
3049 // Find a context in the table and call the ExpandGeneric() with the
3050 // right function to do the expansion.
3051 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3052 {
3053 if (xp->xp_context == tab[i].context)
3054 {
3055 if (tab[i].ic)
3056 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003057 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3058 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003059 break;
3060 }
3061 }
3062
3063 return ret;
3064}
3065
3066/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003067 * Map wild expand options to flags for expand_wildcards()
3068 */
3069 static int
3070map_wildopts_to_ewflags(int options)
3071{
3072 int flags;
3073
3074 flags = EW_DIR; // include directories
3075 if (options & WILD_LIST_NOTFOUND)
3076 flags |= EW_NOTFOUND;
3077 if (options & WILD_ADD_SLASH)
3078 flags |= EW_ADDSLASH;
3079 if (options & WILD_KEEP_ALL)
3080 flags |= EW_KEEPALL;
3081 if (options & WILD_SILENT)
3082 flags |= EW_SILENT;
3083 if (options & WILD_NOERROR)
3084 flags |= EW_NOERROR;
3085 if (options & WILD_ALLLINKS)
3086 flags |= EW_ALLLINKS;
3087
3088 return flags;
3089}
3090
3091/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003092 * Do the expansion based on xp->xp_context and "pat".
3093 */
3094 static int
3095ExpandFromContext(
3096 expand_T *xp,
3097 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003098 char_u ***matches,
3099 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003100 int options) // WILD_ flags
3101{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103 int ret;
3104 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003105 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003106 int fuzzy = cmdline_fuzzy_complete(pat)
3107 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003108
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003109 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110
3111 if (xp->xp_context == EXPAND_FILES
3112 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003113 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003114 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003115 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003116 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3117 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003119 *matches = (char_u **)"";
3120 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 if (xp->xp_context == EXPAND_HELP)
3122 {
3123 // With an empty argument we would get all the help tags, which is
3124 // very slow. Get matches for "help" instead.
3125 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003126 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003127 {
3128#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130#endif
3131 return OK;
3132 }
3133 return FAIL;
3134 }
3135
Bram Moolenaar66b51422019-08-18 21:44:12 +02003136 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003137 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003138 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003139 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003141 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003142#ifdef FEAT_DIFF
3143 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003144 return ExpandBufnames(pat, numMatches, matches,
3145 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003146#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003147 if (xp->xp_context == EXPAND_TAGS
3148 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003149 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3150 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003151 if (xp->xp_context == EXPAND_COLORS)
3152 {
3153 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003155 directories);
3156 }
3157 if (xp->xp_context == EXPAND_COMPILER)
3158 {
3159 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003160 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003161 }
3162 if (xp->xp_context == EXPAND_OWNSYNTAX)
3163 {
3164 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003165 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003166 }
3167 if (xp->xp_context == EXPAND_FILETYPE)
3168 {
3169 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003170 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171 }
Doug Kearns81642d92024-01-04 22:37:44 +01003172#ifdef FEAT_KEYMAP
3173 if (xp->xp_context == EXPAND_KEYMAP)
3174 {
3175 char *directories[] = {"keymap", NULL};
3176 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3177 }
3178#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003179#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003180 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003181 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003182#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003183 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003184 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003185 if (xp->xp_context == EXPAND_RUNTIME)
3186 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003187
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003188 // When expanding a function name starting with s:, match the <SNR>nr_
3189 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003190 if ((xp->xp_context == EXPAND_USER_FUNC
3191 || xp->xp_context == EXPAND_DISASSEMBLE)
3192 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003193 {
3194 int len = (int)STRLEN(pat) + 20;
3195
3196 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003197 if (tofree == NULL)
3198 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003199 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003200 pat = tofree;
3201 }
3202
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003203 if (!fuzzy)
3204 {
3205 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3206 if (regmatch.regprog == NULL)
3207 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003208
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003209 // set ignore-case according to p_ic, p_scs and pat
3210 regmatch.rm_ic = ignorecase(pat);
3211 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003212
3213 if (xp->xp_context == EXPAND_SETTINGS
3214 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003215 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003216 else if (xp->xp_context == EXPAND_STRING_SETTING)
3217 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3218 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3219 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003220 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003221 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003222 else if (xp->xp_context == EXPAND_ARGOPT)
3223 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3224#if defined(FEAT_TERMINAL)
3225 else if (xp->xp_context == EXPAND_TERMINALOPT)
3226 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3227#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003228#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003229 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003230 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003231#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003233 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003235 if (!fuzzy)
3236 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003237 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003238
3239 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003240}
3241
Bram Moolenaar66b51422019-08-18 21:44:12 +02003242/*
3243 * Expand a list of names.
3244 *
3245 * Generic function for command line completion. It calls a function to
3246 * obtain strings, one by one. The strings are matched against a regexp
3247 * program. Matching strings are copied into an array, which is returned.
3248 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003249 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3250 * is used.
3251 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252 * Returns OK when no problems encountered, FAIL for error (out of memory).
3253 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003254 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003256 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003257 expand_T *xp,
3258 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003259 char_u ***matches,
3260 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 char_u *((*func)(expand_T *, int)),
3262 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003263 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003264{
3265 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003266 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003267 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003268 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003269 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003270 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003271 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003272 int sort_matches = FALSE;
3273 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003274
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003275 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003276 *matches = NULL;
3277 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003278
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003279 if (!fuzzy)
3280 ga_init2(&ga, sizeof(char *), 30);
3281 else
3282 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3283
3284 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003286 str = (*func)(xp, i);
3287 if (str == NULL) // end of list
3288 break;
3289 if (*str == NUL) // skip empty strings
3290 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003291
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003292 if (xp->xp_pattern[0] != NUL)
3293 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003294 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003295 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003296 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003297 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003298 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003299 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003300 }
3301 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003302 else
3303 match = TRUE;
3304
3305 if (!match)
3306 continue;
3307
3308 if (escaped)
3309 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3310 else
3311 str = vim_strsave(str);
3312 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003313 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003314 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003316 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003317 return FAIL;
3318 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003319 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003320 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003321 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003322
3323 if (ga_grow(&ga, 1) == FAIL)
3324 {
3325 vim_free(str);
3326 break;
3327 }
3328
3329 if (fuzzy)
3330 {
3331 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3332 fuzmatch->idx = ga.ga_len;
3333 fuzmatch->str = str;
3334 fuzmatch->score = score;
3335 }
3336 else
3337 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3338
K.Takata161b6ac2022-11-14 15:31:07 +00003339#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003340 if (func == get_menu_names)
3341 {
3342 // test for separator added by get_menu_names()
3343 str += STRLEN(str) - 1;
3344 if (*str == '\001')
3345 *str = '.';
3346 }
K.Takata161b6ac2022-11-14 15:31:07 +00003347#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003348
3349 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 }
3351
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003352 if (ga.ga_len == 0)
3353 return OK;
3354
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003355 // sort the matches when using regular expression matching and sorting
3356 // applies to the completion context. Menus and scriptnames should be kept
3357 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003358 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003359 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003360 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003361 && xp->xp_context != EXPAND_SCRIPTNAMES
3362 && xp->xp_context != EXPAND_ARGOPT
3363 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003364 sort_matches = TRUE;
3365
3366 // <SNR> functions should be sorted to the end.
3367 if (xp->xp_context == EXPAND_EXPRESSION
3368 || xp->xp_context == EXPAND_FUNCTIONS
3369 || xp->xp_context == EXPAND_USER_FUNC
3370 || xp->xp_context == EXPAND_DISASSEMBLE)
3371 funcsort = TRUE;
3372
3373 // Sort the matches.
3374 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003375 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003376 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003377 // <SNR> functions should be sorted to the end.
3378 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3379 sort_func_compare);
3380 else
3381 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3382 }
3383
3384 if (!fuzzy)
3385 {
3386 *matches = ga.ga_data;
3387 *numMatches = ga.ga_len;
3388 }
3389 else
3390 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003391 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3392 funcsort) == FAIL)
3393 return FAIL;
3394 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003395 }
3396
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003397#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003398 // Reset the variables used for special highlight names expansion, so that
3399 // they don't show up when getting normal highlight names by ID.
3400 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003401#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003402
Bram Moolenaar66b51422019-08-18 21:44:12 +02003403 return OK;
3404}
3405
3406/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003407 * Expand shell command matches in one directory of $PATH.
3408 */
3409 static void
3410expand_shellcmd_onedir(
3411 char_u *buf,
3412 char_u *s,
3413 size_t l,
3414 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003415 char_u ***matches,
3416 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003417 int flags,
3418 hashtab_T *ht,
3419 garray_T *gap)
3420{
3421 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003422 hash_T hash;
3423 hashitem_T *hi;
3424
3425 vim_strncpy(buf, s, l);
3426 add_pathsep(buf);
3427 l = STRLEN(buf);
3428 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3429
3430 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003431 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003432 if (ret != OK)
3433 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003434
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003435 if (ga_grow(gap, *numMatches) == FAIL)
3436 {
3437 FreeWild(*numMatches, *matches);
3438 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003439 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003440
3441 for (int i = 0; i < *numMatches; ++i)
3442 {
3443 char_u *name = (*matches)[i];
3444
3445 if (STRLEN(name) > l)
3446 {
3447 // Check if this name was already found.
3448 hash = hash_hash(name + l);
3449 hi = hash_lookup(ht, name + l, hash);
3450 if (HASHITEM_EMPTY(hi))
3451 {
3452 // Remove the path that was prepended.
3453 STRMOVE(name, name + l);
3454 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3455 hash_add_item(ht, hi, name, hash);
3456 name = NULL;
3457 }
3458 }
3459 vim_free(name);
3460 }
3461 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003462}
3463
3464/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003465 * Complete a shell command.
3466 * Returns FAIL or OK;
3467 */
3468 static int
3469expand_shellcmd(
3470 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003471 char_u ***matches, // return: array with matches
3472 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003473 int flagsarg) // EW_ flags
3474{
3475 char_u *pat;
3476 int i;
3477 char_u *path = NULL;
3478 int mustfree = FALSE;
3479 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003480 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003481 size_t l;
3482 char_u *s, *e;
3483 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003484 int did_curdir = FALSE;
3485 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003487 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003488 if (buf == NULL)
3489 return FAIL;
3490
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003491 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003492 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003493 if (pat == NULL)
3494 {
3495 vim_free(buf);
3496 return FAIL;
3497 }
3498
Bram Moolenaar66b51422019-08-18 21:44:12 +02003499 for (i = 0; pat[i]; ++i)
3500 if (pat[i] == '\\' && pat[i + 1] == ' ')
3501 STRMOVE(pat + i, pat + i + 1);
3502
3503 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3504
3505 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3506 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3507 path = (char_u *)".";
3508 else
3509 {
3510 // For an absolute name we don't use $PATH.
3511 if (!mch_isFullName(pat))
3512 path = vim_getenv((char_u *)"PATH", &mustfree);
3513 if (path == NULL)
3514 path = (char_u *)"";
3515 }
3516
3517 // Go over all directories in $PATH. Expand matches in that directory and
3518 // collect them in "ga". When "." is not in $PATH also expand for the
3519 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003520 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003521 hash_init(&found_ht);
3522 for (s = path; ; s = e)
3523 {
K.Takata161b6ac2022-11-14 15:31:07 +00003524#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003525 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003526#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003527 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003528#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003529 if (e == NULL)
3530 e = s + STRLEN(s);
3531
3532 if (*s == NUL)
3533 {
3534 if (did_curdir)
3535 break;
3536 // Find directories in the current directory, path is empty.
3537 did_curdir = TRUE;
3538 flags |= EW_DIR;
3539 }
3540 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3541 {
3542 did_curdir = TRUE;
3543 flags |= EW_DIR;
3544 }
3545 else
3546 // Do not match directories inside a $PATH item.
3547 flags &= ~EW_DIR;
3548
3549 l = e - s;
3550 if (l > MAXPATHL - 5)
3551 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003552
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003553 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003554 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003555
Bram Moolenaar66b51422019-08-18 21:44:12 +02003556 if (*e != NUL)
3557 ++e;
3558 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003559 *matches = ga.ga_data;
3560 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003561
3562 vim_free(buf);
3563 vim_free(pat);
3564 if (mustfree)
3565 vim_free(path);
3566 hash_clear(&found_ht);
3567 return OK;
3568}
3569
K.Takata161b6ac2022-11-14 15:31:07 +00003570#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003571/*
3572 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003573 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003574 */
3575 static void *
3576call_user_expand_func(
3577 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003578 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003579{
3580 cmdline_info_T *ccline = get_cmdline_info();
3581 int keep = 0;
3582 typval_T args[4];
3583 sctx_T save_current_sctx = current_sctx;
3584 char_u *pat = NULL;
3585 void *ret;
3586
3587 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3588 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003589
3590 if (ccline->cmdbuff != NULL)
3591 {
3592 keep = ccline->cmdbuff[ccline->cmdlen];
3593 ccline->cmdbuff[ccline->cmdlen] = 0;
3594 }
3595
3596 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3597
3598 args[0].v_type = VAR_STRING;
3599 args[0].vval.v_string = pat;
3600 args[1].v_type = VAR_STRING;
3601 args[1].vval.v_string = xp->xp_line;
3602 args[2].v_type = VAR_NUMBER;
3603 args[2].vval.v_number = xp->xp_col;
3604 args[3].v_type = VAR_UNKNOWN;
3605
3606 current_sctx = xp->xp_script_ctx;
3607
3608 ret = user_expand_func(xp->xp_arg, 3, args);
3609
3610 current_sctx = save_current_sctx;
3611 if (ccline->cmdbuff != NULL)
3612 ccline->cmdbuff[ccline->cmdlen] = keep;
3613
3614 vim_free(pat);
3615 return ret;
3616}
3617
3618/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003619 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3620 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003621 */
3622 static int
3623ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003624 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003625 expand_T *xp,
3626 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003627 char_u ***matches,
3628 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003629{
3630 char_u *retstr;
3631 char_u *s;
3632 char_u *e;
3633 int keep;
3634 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003635 int fuzzy;
3636 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003637 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003638
3639 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003640 *matches = NULL;
3641 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003642
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003643 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003644 if (retstr == NULL)
3645 return FAIL;
3646
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003647 if (!fuzzy)
3648 ga_init2(&ga, sizeof(char *), 3);
3649 else
3650 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3651
Bram Moolenaar66b51422019-08-18 21:44:12 +02003652 for (s = retstr; *s != NUL; s = e)
3653 {
3654 e = vim_strchr(s, '\n');
3655 if (e == NULL)
3656 e = s + STRLEN(s);
3657 keep = *e;
3658 *e = NUL;
3659
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003660 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003661 {
3662 if (!fuzzy)
3663 match = vim_regexec(regmatch, s, (colnr_T)0);
3664 else
3665 {
3666 score = fuzzy_match_str(s, pat);
3667 match = (score != 0);
3668 }
3669 }
3670 else
3671 match = TRUE; // match everything
3672
Bram Moolenaar66b51422019-08-18 21:44:12 +02003673 *e = keep;
3674
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003675 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003676 {
3677 if (ga_grow(&ga, 1) == FAIL)
3678 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003679 if (!fuzzy)
3680 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3681 else
3682 {
3683 fuzmatch_str_T *fuzmatch =
3684 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003685 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 fuzmatch->str = vim_strnsave(s, e - s);
3687 fuzmatch->score = score;
3688 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003689 ++ga.ga_len;
3690 }
3691
3692 if (*e != NUL)
3693 ++e;
3694 }
3695 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003696
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003697 if (ga.ga_len == 0)
3698 return OK;
3699
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003700 if (!fuzzy)
3701 {
3702 *matches = ga.ga_data;
3703 *numMatches = ga.ga_len;
3704 }
3705 else
3706 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003707 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3708 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003709 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003710 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003711 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003712 return OK;
3713}
3714
3715/*
3716 * Expand names with a list returned by a function defined by the user.
3717 */
3718 static int
3719ExpandUserList(
3720 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003721 char_u ***matches,
3722 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003723{
3724 list_T *retlist;
3725 listitem_T *li;
3726 garray_T ga;
3727
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003728 *matches = NULL;
3729 *numMatches = 0;
3730 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003731 if (retlist == NULL)
3732 return FAIL;
3733
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003734 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003735 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003736 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003737 {
3738 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3739 continue; // Skip non-string items and empty strings
3740
3741 if (ga_grow(&ga, 1) == FAIL)
3742 break;
3743
3744 ((char_u **)ga.ga_data)[ga.ga_len] =
3745 vim_strsave(li->li_tv.vval.v_string);
3746 ++ga.ga_len;
3747 }
3748 list_unref(retlist);
3749
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003750 *matches = ga.ga_data;
3751 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003752 return OK;
3753}
K.Takata161b6ac2022-11-14 15:31:07 +00003754#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003755
3756/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003757 * Expand "file" for all comma-separated directories in "path".
3758 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003759 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003760 */
3761 void
3762globpath(
3763 char_u *path,
3764 char_u *file,
3765 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003766 int expand_options,
3767 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003768{
3769 expand_T xpc;
3770 char_u *buf;
3771 int i;
3772 int num_p;
3773 char_u **p;
3774
3775 buf = alloc(MAXPATHL);
3776 if (buf == NULL)
3777 return;
3778
3779 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003780 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003781
3782 // Loop over all entries in {path}.
3783 while (*path != NUL)
3784 {
3785 // Copy one item of the path to buf[] and concatenate the file name.
3786 copy_option_part(&path, buf, MAXPATHL, ",");
3787 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3788 {
K.Takata161b6ac2022-11-14 15:31:07 +00003789#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003790 // Using the platform's path separator (\) makes vim incorrectly
3791 // treat it as an escape character, use '/' instead.
3792 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3793 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003794#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003795 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003796#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003797 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003798 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003799 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3800 {
3801 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3802
3803 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003804 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 for (i = 0; i < num_p; ++i)
3806 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003807 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003808 ++ga->ga_len;
3809 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003810 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811 }
3812 }
3813 }
3814
3815 vim_free(buf);
3816}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003817
Bram Moolenaareadee482020-09-04 15:37:31 +02003818/*
3819 * Translate some keys pressed when 'wildmenu' is used.
3820 */
3821 int
3822wildmenu_translate_key(
3823 cmdline_info_T *cclp,
3824 int key,
3825 expand_T *xp,
3826 int did_wild_list)
3827{
3828 int c = key;
3829
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003830 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003831 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003832 // When the popup menu is used for cmdline completion:
3833 // Up : go to the previous item in the menu
3834 // Down : go to the next item in the menu
3835 // Left : go to the parent directory
3836 // Right: list the files in the selected directory
3837 switch (c)
3838 {
3839 case K_UP: c = K_LEFT; break;
3840 case K_DOWN: c = K_RIGHT; break;
3841 case K_LEFT: c = K_UP; break;
3842 case K_RIGHT: c = K_DOWN; break;
3843 default: break;
3844 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003845 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003846
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003847 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003848 {
3849 if (c == K_LEFT)
3850 c = Ctrl_P;
3851 else if (c == K_RIGHT)
3852 c = Ctrl_N;
3853 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003854
Bram Moolenaareadee482020-09-04 15:37:31 +02003855 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003856 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003857 && cclp->cmdpos > 1
3858 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3859 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3860 && (c == '\n' || c == '\r' || c == K_KENTER))
3861 c = K_DOWN;
3862
3863 return c;
3864}
3865
3866/*
3867 * Delete characters on the command line, from "from" to the current
3868 * position.
3869 */
3870 static void
3871cmdline_del(cmdline_info_T *cclp, int from)
3872{
3873 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3874 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3875 cclp->cmdlen -= cclp->cmdpos - from;
3876 cclp->cmdpos = from;
3877}
3878
3879/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003880 * Handle a key pressed when the wild menu for the menu names
3881 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003882 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003883 static int
3884wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003885{
Bram Moolenaareadee482020-09-04 15:37:31 +02003886 int i;
3887 int j;
3888
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003889 // Hitting <Down> after "emenu Name.": complete submenu
3890 if (key == K_DOWN && cclp->cmdpos > 0
3891 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003892 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003893 key = p_wc;
3894 KeyTyped = TRUE; // in case the key was mapped
3895 }
3896 else if (key == K_UP)
3897 {
3898 // Hitting <Up>: Remove one submenu name in front of the
3899 // cursor
3900 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003901
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003902 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3903 i = 0;
3904 while (--j > 0)
3905 {
3906 // check for start of menu name
3907 if (cclp->cmdbuff[j] == ' '
3908 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003909 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003910 i = j + 1;
3911 break;
3912 }
3913 // check for start of submenu name
3914 if (cclp->cmdbuff[j] == '.'
3915 && cclp->cmdbuff[j - 1] != '\\')
3916 {
3917 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003918 {
3919 i = j + 1;
3920 break;
3921 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003922 else
3923 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003924 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003925 }
3926 if (i > 0)
3927 cmdline_del(cclp, i);
3928 key = p_wc;
3929 KeyTyped = TRUE; // in case the key was mapped
3930 xp->xp_context = EXPAND_NOTHING;
3931 }
3932
3933 return key;
3934}
3935
3936/*
3937 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3938 * directory names (EXPAND_DIRECTORIES) or shell command names
3939 * (EXPAND_SHELLCMD) is displayed.
3940 */
3941 static int
3942wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3943{
3944 int i;
3945 int j;
3946 char_u upseg[5];
3947
3948 upseg[0] = PATHSEP;
3949 upseg[1] = '.';
3950 upseg[2] = '.';
3951 upseg[3] = PATHSEP;
3952 upseg[4] = NUL;
3953
3954 if (key == K_DOWN
3955 && cclp->cmdpos > 0
3956 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3957 && (cclp->cmdpos < 3
3958 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3959 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3960 {
3961 // go down a directory
3962 key = p_wc;
3963 KeyTyped = TRUE; // in case the key was mapped
3964 }
3965 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3966 {
3967 // If in a direct ancestor, strip off one ../ to go down
3968 int found = FALSE;
3969
3970 j = cclp->cmdpos;
3971 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3972 while (--j > i)
3973 {
3974 if (has_mbyte)
3975 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3976 if (vim_ispathsep(cclp->cmdbuff[j]))
3977 {
3978 found = TRUE;
3979 break;
3980 }
3981 }
3982 if (found
3983 && cclp->cmdbuff[j - 1] == '.'
3984 && cclp->cmdbuff[j - 2] == '.'
3985 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3986 {
3987 cmdline_del(cclp, j - 2);
3988 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003989 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003990 }
3991 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003992 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003993 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003994 // go up a directory
3995 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003996
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003997 j = cclp->cmdpos - 1;
3998 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3999 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004000 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004001 if (has_mbyte)
4002 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4003 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004004#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004005 && vim_strchr((char_u *)" *?[{`$%#",
4006 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004007#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004008 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004009 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004010 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004011 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004012 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004013 break;
4014 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004015 else
4016 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004017 }
4018 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004019
4020 if (!found)
4021 j = i;
4022 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4023 j += 4;
4024 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4025 && j == i)
4026 j += 3;
4027 else
4028 j = 0;
4029 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004030 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004031 // TODO this is only for DOS/UNIX systems - need to put in
4032 // machine-specific stuff here and in upseg init
4033 cmdline_del(cclp, j);
4034 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004035 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004036 else if (cclp->cmdpos > i)
4037 cmdline_del(cclp, i);
4038
4039 // Now complete in the new directory. Set KeyTyped in case the
4040 // Up key came from a mapping.
4041 key = p_wc;
4042 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004043 }
4044
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004045 return key;
4046}
4047
4048/*
4049 * Handle a key pressed when the wild menu is displayed
4050 */
4051 int
4052wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4053{
4054 if (xp->xp_context == EXPAND_MENUNAMES)
4055 return wildmenu_process_key_menunames(cclp, key, xp);
4056 else if ((xp->xp_context == EXPAND_FILES
4057 || xp->xp_context == EXPAND_DIRECTORIES
4058 || xp->xp_context == EXPAND_SHELLCMD))
4059 return wildmenu_process_key_filenames(cclp, key, xp);
4060
4061 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004062}
4063
4064/*
4065 * Free expanded names when finished walking through the matches
4066 */
4067 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004068wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004069{
4070 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004071
4072 if (!p_wmnu || wild_menu_showing == 0)
4073 return;
4074
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004075#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004076 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004077 if (cclp->input_fn)
4078 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004079#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004080
4081 if (wild_menu_showing == WM_SCROLLED)
4082 {
4083 // Entered command line, move it up
4084 cmdline_row--;
4085 redrawcmd();
4086 }
4087 else if (save_p_ls != -1)
4088 {
4089 // restore 'laststatus' and 'winminheight'
4090 p_ls = save_p_ls;
4091 p_wmh = save_p_wmh;
4092 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004093 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004094 redrawcmd();
4095 save_p_ls = -1;
4096 }
4097 else
4098 {
4099 win_redraw_last_status(topframe);
4100 redraw_statuslines();
4101 }
4102 KeyTyped = skt;
4103 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004104#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004105 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004106 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004107#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004108}
Bram Moolenaareadee482020-09-04 15:37:31 +02004109
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004110#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004111/*
4112 * "getcompletion()" function
4113 */
4114 void
4115f_getcompletion(typval_T *argvars, typval_T *rettv)
4116{
4117 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004118 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004119 expand_T xpc;
4120 int filtered = FALSE;
4121 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004122 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004123
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004124 if (in_vim9script()
4125 && (check_for_string_arg(argvars, 0) == FAIL
4126 || check_for_string_arg(argvars, 1) == FAIL
4127 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4128 return;
4129
ii144785fe02021-11-21 12:13:56 +00004130 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004131 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004132 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004133 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004134
4135 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004136 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004137
4138 if (p_wic)
4139 options |= WILD_ICASE;
4140
4141 // For filtered results, 'wildignore' is used
4142 if (!filtered)
4143 options |= WILD_KEEP_ALL;
4144
4145 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004146 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004147 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004148 int cmdline_len = (int)STRLEN(pat);
4149 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004150 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004151 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004152 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004153 else
4154 {
ii144785fe02021-11-21 12:13:56 +00004155 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004156 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004157 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004158
4159 xpc.xp_context = cmdcomplete_str_to_type(type);
4160 if (xpc.xp_context == EXPAND_NOTHING)
4161 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004162 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004163 return;
4164 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004165
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004166 if (xpc.xp_context == EXPAND_USER_DEFINED)
4167 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004168 // Must be "custom,funcname" pattern
4169 if (STRNCMP(type, "custom,", 7) != 0)
4170 {
4171 semsg(_(e_invalid_argument_str), type);
4172 return;
4173 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004174
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004175 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004176 }
4177
4178 if (xpc.xp_context == EXPAND_USER_LIST)
4179 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004180 // Must be "customlist,funcname" pattern
4181 if (STRNCMP(type, "customlist,", 11) != 0)
4182 {
4183 semsg(_(e_invalid_argument_str), type);
4184 return;
4185 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004186
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004187 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004188 }
4189
Bram Moolenaar66b51422019-08-18 21:44:12 +02004190# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004191 if (xpc.xp_context == EXPAND_MENUS)
4192 {
4193 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4194 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4195 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004196# endif
4197# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004198 if (xpc.xp_context == EXPAND_CSCOPE)
4199 {
4200 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4201 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4202 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004203# endif
4204# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004205 if (xpc.xp_context == EXPAND_SIGN)
4206 {
4207 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4208 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4209 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004210# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004211 if (xpc.xp_context == EXPAND_RUNTIME)
4212 {
4213 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4214 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4215 }
zeertzjq85f36d62024-10-10 19:14:13 +02004216 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4217 {
4218 int context = EXPAND_SHELLCMDLINE;
4219 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4220 &context);
4221 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4222 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004223 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004224
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004225 if (cmdline_fuzzy_completion_supported(&xpc))
4226 // when fuzzy matching, don't modify the search string
4227 pat = vim_strsave(xpc.xp_pattern);
4228 else
4229 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4230
Bram Moolenaar93a10962022-06-16 11:42:09 +01004231 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004232 {
4233 int i;
4234
4235 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4236
4237 for (i = 0; i < xpc.xp_numfiles; i++)
4238 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4239 }
4240 vim_free(pat);
4241 ExpandCleanup(&xpc);
4242}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004243#endif // FEAT_EVAL