blob: 9f24429f46b13049a96dd7a6a51f5eccd933a9bc [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 {
Jim Zhou3255af82025-02-27 19:29:50 +0100232#ifdef FEAT_EVAL
233 if (ccline->input_fn && ccline->xp_context == EXPAND_COMMANDS)
234 {
235 // Expand commands typed in input() function
236 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, FALSE);
237 }
238 else
239#endif
240 {
241 set_expand_context(xp);
242 }
243 cmd_showtail = expand_showtail(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200244 }
245
246 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
247 {
248 beep_flush();
249 return OK; // Something illegal on command line
250 }
251 if (xp->xp_context == EXPAND_NOTHING)
252 {
253 // Caller can use the character as a normal char instead
254 return FAIL;
255 }
256
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000257 // If cmd_silent is set then don't show the dots, because redrawcmd() below
258 // won't remove them.
259 if (!cmd_silent)
260 {
261 msg_puts("..."); // show that we are busy
262 out_flush();
263 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200264
265 i = (int)(xp->xp_pattern - ccline->cmdbuff);
266 xp->xp_pattern_len = ccline->cmdpos - i;
267
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000268 if (type == WILD_NEXT || type == WILD_PREV
269 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200270 {
271 // Get next/previous match for a previous expanded pattern.
272 p2 = ExpandOne(xp, NULL, NULL, 0, type);
273 }
274 else
275 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000276 if (cmdline_fuzzy_completion_supported(xp))
277 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200278 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000279 else
280 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
281
Bram Moolenaar66b51422019-08-18 21:44:12 +0200282 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000283 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200284 p2 = NULL;
285 else
286 {
287 int use_options = options |
288 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
289 if (escape)
290 use_options |= WILD_ESCAPE;
291
292 if (p_wic)
293 use_options += WILD_ICASE;
294 p2 = ExpandOne(xp, p1,
295 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
296 use_options, type);
297 vim_free(p1);
298 // longest match: make sure it is not shorter, happens with :help
299 if (p2 != NULL && type == WILD_LONGEST)
300 {
301 for (j = 0; j < xp->xp_pattern_len; ++j)
302 if (ccline->cmdbuff[i + j] == '*'
303 || ccline->cmdbuff[i + j] == '?')
304 break;
305 if ((int)STRLEN(p2) < j)
306 VIM_CLEAR(p2);
307 }
308 }
309 }
310
311 if (p2 != NULL && !got_int)
312 {
313 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
314 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
315 {
316 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
317 xp->xp_pattern = ccline->cmdbuff + i;
318 }
319 else
320 v = OK;
321 if (v == OK)
322 {
323 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
324 &ccline->cmdbuff[ccline->cmdpos],
325 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
326 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
327 ccline->cmdlen += difflen;
328 ccline->cmdpos += difflen;
329 }
330 }
331 vim_free(p2);
332
333 redrawcmd();
334 cursorcmd();
335
336 // When expanding a ":map" command and no matches are found, assume that
337 // the key is supposed to be inserted literally
338 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
339 return FAIL;
340
341 if (xp->xp_numfiles <= 0 && p2 == NULL)
342 beep_flush();
343 else if (xp->xp_numfiles == 1)
344 // free expanded pattern
345 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
346
347 return OK;
348}
349
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000350/*
351 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000352 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000353 */
354 static int
355cmdline_pum_create(
356 cmdline_info_T *ccline,
357 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000358 char_u **matches,
359 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000360 int showtail)
361{
362 int i;
363 int columns;
364
365 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000366 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000367 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000368 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000369 {
zeertzjqc51a3762022-12-10 10:22:29 +0000370 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000371 compl_match_array[i].pum_info = NULL;
372 compl_match_array[i].pum_extra = NULL;
373 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200374 compl_match_array[i].pum_user_abbr_hlattr = -1;
375 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000376 }
377
378 // Compute the popup menu starting column
379 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
380 columns = vim_strsize(xp->xp_pattern);
381 if (showtail)
382 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000383 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000384 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000385 }
glepnir977561a2025-02-13 20:48:56 +0100386 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000387
388 // no default selection
389 compl_selected = -1;
390
391 cmdline_pum_display();
392
393 return EXPAND_OK;
394}
395
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000396/*
397 * Display the cmdline completion matches in a popup menu
398 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000399 void
400cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401{
402 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
403}
404
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000405/*
406 * Returns TRUE if the cmdline completion popup menu is being displayed.
407 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000408 int
409cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100411 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000412}
413
414/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000415 * Remove the cmdline completion popup menu (if present), free the list of
416 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000418 void
419cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000420{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000421 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100422 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000423
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424 pum_undisplay();
425 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000426 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000427 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000428 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000429 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100430
431 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
432 // as a side effect.
433 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000434}
435
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000436 void
437cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000438{
439 cmdline_pum_remove();
440 wildmenu_cleanup(cclp);
441}
442
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000443/*
444 * Returns the starting column number to use for the cmdline completion popup
445 * menu.
446 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000447 int
448cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000449{
450 return compl_startcol;
451}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000452
Bram Moolenaar66b51422019-08-18 21:44:12 +0200453/*
zeertzjqd8c93402024-06-17 18:25:32 +0200454 * Returns the current cmdline completion pattern.
455 */
456 char_u *
457cmdline_compl_pattern(void)
458{
459 expand_T *xp = get_cmdline_info()->xpc;
460
461 return xp == NULL ? NULL : xp->xp_orig;
462}
463
464/*
465 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
466 */
467 int
468cmdline_compl_is_fuzzy(void)
469{
470 expand_T *xp = get_cmdline_info()->xpc;
471
472 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
473}
474
475/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000476 * Return the number of characters that should be skipped in a status match.
477 * These are backslashes used for escaping. Do show backslashes in help tags.
478 */
479 static int
480skip_status_match_char(expand_T *xp, char_u *s)
481{
482 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
483#ifdef FEAT_MENU
484 || ((xp->xp_context == EXPAND_MENUS
485 || xp->xp_context == EXPAND_MENUNAMES)
486 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
487#endif
488 )
489 {
490#ifndef BACKSLASH_IN_FILENAME
491 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
492 return 2;
493#endif
494 return 1;
495 }
496 return 0;
497}
498
499/*
500 * Get the length of an item as it will be shown in the status line.
501 */
502 static int
503status_match_len(expand_T *xp, char_u *s)
504{
505 int len = 0;
506
507#ifdef FEAT_MENU
508 int emenu = xp->xp_context == EXPAND_MENUS
509 || xp->xp_context == EXPAND_MENUNAMES;
510
511 // Check for menu separators - replace with '|'.
512 if (emenu && menu_is_separator(s))
513 return 1;
514#endif
515
516 while (*s != NUL)
517 {
518 s += skip_status_match_char(xp, s);
519 len += ptr2cells(s);
520 MB_PTR_ADV(s);
521 }
522
523 return len;
524}
525
526/*
527 * Show wildchar matches in the status line.
528 * Show at least the "match" item.
529 * We start at item 'first_match' in the list and show all matches that fit.
530 *
531 * If inversion is possible we use it. Else '=' characters are used.
532 */
533 static void
534win_redr_status_matches(
535 expand_T *xp,
536 int num_matches,
537 char_u **matches, // list of matches
538 int match,
539 int showtail)
540{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000541 int row;
542 char_u *buf;
543 int len;
544 int clen; // length in screen cells
545 int fillchar;
546 int attr;
547 int i;
548 int highlight = TRUE;
549 char_u *selstart = NULL;
550 int selstart_col = 0;
551 char_u *selend = NULL;
552 static int first_match = 0;
553 int add_left = FALSE;
554 char_u *s;
555#ifdef FEAT_MENU
556 int emenu;
557#endif
558 int l;
559
560 if (matches == NULL) // interrupted completion?
561 return;
562
563 if (has_mbyte)
564 buf = alloc(Columns * MB_MAXBYTES + 1);
565 else
566 buf = alloc(Columns + 1);
567 if (buf == NULL)
568 return;
569
570 if (match == -1) // don't show match but original text
571 {
572 match = 0;
573 highlight = FALSE;
574 }
575 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000576 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000577 if (match == 0)
578 first_match = 0;
579 else if (match < first_match)
580 {
581 // jumping left, as far as we can go
582 first_match = match;
583 add_left = TRUE;
584 }
585 else
586 {
587 // check if match fits on the screen
588 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000589 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000590 if (first_match > 0)
591 clen += 2;
592 // jumping right, put match at the left
593 if ((long)clen > Columns)
594 {
595 first_match = match;
596 // if showing the last match, we can add some on the left
597 clen = 2;
598 for (i = match; i < num_matches; ++i)
599 {
zeertzjqc51a3762022-12-10 10:22:29 +0000600 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000601 if ((long)clen >= Columns)
602 break;
603 }
604 if (i == num_matches)
605 add_left = TRUE;
606 }
607 }
608 if (add_left)
609 while (first_match > 0)
610 {
zeertzjqc51a3762022-12-10 10:22:29 +0000611 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000612 if ((long)clen >= Columns)
613 break;
614 --first_match;
615 }
616
617 fillchar = fillchar_status(&attr, curwin);
618
619 if (first_match == 0)
620 {
621 *buf = NUL;
622 len = 0;
623 }
624 else
625 {
626 STRCPY(buf, "< ");
627 len = 2;
628 }
629 clen = len;
630
631 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000632 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000633 {
634 if (i == match)
635 {
636 selstart = buf + len;
637 selstart_col = clen;
638 }
639
zeertzjqc51a3762022-12-10 10:22:29 +0000640 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000641 // Check for menu separators - replace with '|'
642#ifdef FEAT_MENU
643 emenu = (xp->xp_context == EXPAND_MENUS
644 || xp->xp_context == EXPAND_MENUNAMES);
645 if (emenu && menu_is_separator(s))
646 {
647 STRCPY(buf + len, transchar('|'));
648 l = (int)STRLEN(buf + len);
649 len += l;
650 clen += l;
651 }
652 else
653#endif
654 for ( ; *s != NUL; ++s)
655 {
656 s += skip_status_match_char(xp, s);
657 clen += ptr2cells(s);
658 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
659 {
660 STRNCPY(buf + len, s, l);
661 s += l - 1;
662 len += l;
663 }
664 else
665 {
666 STRCPY(buf + len, transchar_byte(*s));
667 len += (int)STRLEN(buf + len);
668 }
669 }
670 if (i == match)
671 selend = buf + len;
672
673 *(buf + len++) = ' ';
674 *(buf + len++) = ' ';
675 clen += 2;
676 if (++i == num_matches)
677 break;
678 }
679
680 if (i != num_matches)
681 {
682 *(buf + len++) = '>';
683 ++clen;
684 }
685
686 buf[len] = NUL;
687
688 row = cmdline_row - 1;
689 if (row >= 0)
690 {
691 if (wild_menu_showing == 0)
692 {
693 if (msg_scrolled > 0)
694 {
695 // Put the wildmenu just above the command line. If there is
696 // no room, scroll the screen one line up.
697 if (cmdline_row == Rows - 1)
698 {
699 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
700 ++msg_scrolled;
701 }
702 else
703 {
704 ++cmdline_row;
705 ++row;
706 }
707 wild_menu_showing = WM_SCROLLED;
708 }
709 else
710 {
711 // Create status line if needed by setting 'laststatus' to 2.
712 // Set 'winminheight' to zero to avoid that the window is
713 // resized.
714 if (lastwin->w_status_height == 0)
715 {
716 save_p_ls = p_ls;
717 save_p_wmh = p_wmh;
718 p_ls = 2;
719 p_wmh = 0;
720 last_status(FALSE);
721 }
722 wild_menu_showing = WM_SHOWN;
723 }
724 }
725
726 screen_puts(buf, row, 0, attr);
727 if (selstart != NULL && highlight)
728 {
729 *selend = NUL;
730 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
731 }
732
733 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
734 }
735
736 win_redraw_last_status(topframe);
737 vim_free(buf);
738}
739
740/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000741 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200742 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000743 */
744 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100745get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000746{
glepnir977561a2025-02-13 20:48:56 +0100747 int findex = xp->xp_selected;
748 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000749
zeertzjqb6c900b2025-02-14 17:59:31 +0100750 // When no matches found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000751 if (xp->xp_numfiles <= 0)
752 return NULL;
753
754 if (mode == WILD_PREV)
755 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100756 // Select the last entry if at original text
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000757 if (findex == -1)
758 findex = xp->xp_numfiles;
zeertzjqb6c900b2025-02-14 17:59:31 +0100759 // Otherwise select the previous entry
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000760 --findex;
761 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000762 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000763 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100764 // Select the next entry
765 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000766 }
glepnir977561a2025-02-13 20:48:56 +0100767 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000768 {
glepnir977561a2025-02-13 20:48:56 +0100769 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
770 ht = pum_get_height();
771 if (ht > 3)
772 ht -= 2;
773
774 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000775 {
glepnir977561a2025-02-13 20:48:56 +0100776 if (findex == 0)
777 // at the first entry, don't select any entries
778 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100779 else if (findex < 0)
glepnir977561a2025-02-13 20:48:56 +0100780 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000781 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100782 else
783 // go up by the pum height
784 findex = MAX(findex - ht, 0);
785 }
786 else // mode == WILD_PAGEDOWN
787 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100788 if (findex >= xp->xp_numfiles - 1)
glepnir977561a2025-02-13 20:48:56 +0100789 // at the last entry, don't select any entries
790 findex = -1;
zeertzjqb6c900b2025-02-14 17:59:31 +0100791 else if (findex < 0)
792 // no entry is selected, select the first entry
793 findex = 0;
glepnir977561a2025-02-13 20:48:56 +0100794 else
795 // go down by the pum height
796 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000797 }
798 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000799
glepnir977561a2025-02-13 20:48:56 +0100800 // Handle wrapping around
801 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000802 {
zeertzjqb6c900b2025-02-14 17:59:31 +0100803 // If original text exists, return to it when wrapping around
glepnir977561a2025-02-13 20:48:56 +0100804 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000805 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000806 else
glepnir977561a2025-02-13 20:48:56 +0100807 // Wrap around to opposite end
808 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000809 }
glepnir977561a2025-02-13 20:48:56 +0100810
811 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000812 if (compl_match_array)
813 {
814 compl_selected = findex;
815 cmdline_pum_display();
816 }
817 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100818 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
819 cmd_showtail);
820
zeertzjqe9ef3472023-08-17 23:57:05 +0200821 xp->xp_selected = findex;
zeertzjqb6c900b2025-02-14 17:59:31 +0100822 // Return the original text or the selected match
glepnir977561a2025-02-13 20:48:56 +0100823 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000824}
825
826/*
827 * Start the command-line expansion and get the matches.
828 */
829 static char_u *
830ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
831{
832 int non_suf_match; // number without matching suffix
833 int i;
834 char_u *ss = NULL;
835
836 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000837 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000838 options) == FAIL)
839 {
840#ifdef FNAME_ILLEGAL
841 // Illegal file name has been silently skipped. But when there
842 // are wildcards, the real problem is that there was no match,
843 // causing the pattern to be added, which has illegal characters.
844 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
845 semsg(_(e_no_match_str_2), str);
846#endif
847 }
848 else if (xp->xp_numfiles == 0)
849 {
850 if (!(options & WILD_SILENT))
851 semsg(_(e_no_match_str_2), str);
852 }
853 else
854 {
855 // Escape the matches for use on the command line.
856 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
857
858 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000859 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000860 {
861 if (xp->xp_numfiles)
862 non_suf_match = xp->xp_numfiles;
863 else
864 non_suf_match = 1;
865 if ((xp->xp_context == EXPAND_FILES
866 || xp->xp_context == EXPAND_DIRECTORIES)
867 && xp->xp_numfiles > 1)
868 {
869 // More than one match; check suffix.
870 // The files will have been sorted on matching suffix in
871 // expand_wildcards, only need to check the first two.
872 non_suf_match = 0;
873 for (i = 0; i < 2; ++i)
874 if (match_suffix(xp->xp_files[i]))
875 ++non_suf_match;
876 }
877 if (non_suf_match != 1)
878 {
879 // Can we ever get here unless it's while expanding
880 // interactively? If not, we can get rid of this all
881 // together. Don't really want to wait for this message
882 // (and possibly have to hit return to continue!).
883 if (!(options & WILD_SILENT))
884 emsg(_(e_too_many_file_names));
885 else if (!(options & WILD_NO_BEEP))
886 beep_flush();
887 }
888 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
889 ss = vim_strsave(xp->xp_files[0]);
890 }
891 }
892
893 return ss;
894}
895
896/*
897 * Return the longest common part in the list of cmdline completion matches.
898 */
899 static char_u *
900find_longest_match(expand_T *xp, int options)
901{
902 long_u len;
903 int mb_len = 1;
904 int c0, ci;
905 int i;
906 char_u *ss;
907
908 for (len = 0; xp->xp_files[0][len]; len += mb_len)
909 {
910 if (has_mbyte)
911 {
912 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
913 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
914 }
915 else
916 c0 = xp->xp_files[0][len];
917 for (i = 1; i < xp->xp_numfiles; ++i)
918 {
919 if (has_mbyte)
920 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
921 else
922 ci = xp->xp_files[i][len];
923 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
924 || xp->xp_context == EXPAND_FILES
925 || xp->xp_context == EXPAND_SHELLCMD
926 || xp->xp_context == EXPAND_BUFFERS))
927 {
928 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
929 break;
930 }
931 else if (c0 != ci)
932 break;
933 }
934 if (i < xp->xp_numfiles)
935 {
936 if (!(options & WILD_NO_BEEP))
937 vim_beep(BO_WILD);
938 break;
939 }
940 }
941
942 ss = alloc(len + 1);
943 if (ss)
944 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
945
946 return ss;
947}
948
949/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000950 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200951 * Chars that should not be expanded must be preceded with a backslash.
952 * Return a pointer to allocated memory containing the new string.
953 * Return NULL for failure.
954 *
955 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200956 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
957 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200958 *
959 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
960 * is WILD_EXPAND_FREE or WILD_ALL.
961 *
962 * mode = WILD_FREE: just free previously expanded matches
963 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
964 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
965 * mode = WILD_NEXT: use next match in multiple match, wrap to first
966 * mode = WILD_PREV: use previous match in multiple match, wrap to first
967 * mode = WILD_ALL: return all matches concatenated
968 * mode = WILD_LONGEST: return longest matched part
969 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000970 * mode = WILD_APPLY: apply the item selected in the cmdline completion
971 * popup menu and close the menu.
972 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
973 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 *
975 * options = WILD_LIST_NOTFOUND: list entries without a match
976 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
977 * options = WILD_USE_NL: Use '\n' for WILD_ALL
978 * options = WILD_NO_BEEP: Don't beep for multiple matches
979 * options = WILD_ADD_SLASH: add a slash after directory names
980 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
981 * options = WILD_SILENT: don't print warning messages
982 * options = WILD_ESCAPE: put backslash before special chars
983 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200984 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200985 *
986 * The variables xp->xp_context and xp->xp_backslash must have been set!
987 */
988 char_u *
989ExpandOne(
990 expand_T *xp,
991 char_u *str,
992 char_u *orig, // allocated copy of original of expanded string
993 int options,
994 int mode)
995{
996 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 int orig_saved = FALSE;
998 int i;
999 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001000
1001 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001002 if (mode == WILD_NEXT || mode == WILD_PREV
1003 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001004 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001005
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001006 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001007 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001008 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001009 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001010 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001011 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001012
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 // free old names
1014 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1015 {
1016 FreeWild(xp->xp_numfiles, xp->xp_files);
1017 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001018 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001019
1020 // The entries from xp_files may be used in the PUM, remove it.
1021 if (compl_match_array != NULL)
1022 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001024 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025
1026 if (mode == WILD_FREE) // only release file name
1027 return NULL;
1028
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001029 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001030 {
zeertzjq28a23602023-09-29 19:58:35 +02001031 vim_free(xp->xp_orig);
1032 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 orig_saved = TRUE;
1034
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001035 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001036 }
1037
1038 // Find longest common part
1039 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1040 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001041 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001042 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001043 }
1044
Bram Moolenaar57e95172022-08-20 19:26:14 +01001045 // Concatenate all matching names. Unless interrupted, this can be slow
1046 // and the result probably won't be used.
1047 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 {
1049 len = 0;
1050 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001051 {
1052 if (i > 0)
1053 {
1054 if (xp->xp_prefix == XP_PREFIX_NO)
1055 len += 2; // prefix "no"
1056 else if (xp->xp_prefix == XP_PREFIX_INV)
1057 len += 3; // prefix "inv"
1058 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001060 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 ss = alloc(len);
1062 if (ss != NULL)
1063 {
1064 *ss = NUL;
1065 for (i = 0; i < xp->xp_numfiles; ++i)
1066 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001067 if (i > 0)
1068 {
1069 if (xp->xp_prefix == XP_PREFIX_NO)
1070 STRCAT(ss, "no");
1071 else if (xp->xp_prefix == XP_PREFIX_INV)
1072 STRCAT(ss, "inv");
1073 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001074 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001075
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 if (i != xp->xp_numfiles - 1)
1077 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1078 }
1079 }
1080 }
1081
1082 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1083 ExpandCleanup(xp);
1084
zeertzjq28a23602023-09-29 19:58:35 +02001085 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001086 if (!orig_saved)
1087 vim_free(orig);
1088
1089 return ss;
1090}
1091
1092/*
1093 * Prepare an expand structure for use.
1094 */
1095 void
1096ExpandInit(expand_T *xp)
1097{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001098 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001099 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001100 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001101 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102}
1103
1104/*
1105 * Cleanup an expand structure after use.
1106 */
1107 void
1108ExpandCleanup(expand_T *xp)
1109{
1110 if (xp->xp_numfiles >= 0)
1111 {
1112 FreeWild(xp->xp_numfiles, xp->xp_files);
1113 xp->xp_numfiles = -1;
1114 }
zeertzjq28a23602023-09-29 19:58:35 +02001115 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001116}
1117
1118/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001119 * Display one line of completion matches. Multiple matches are displayed in
1120 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001121 * matches - list of completion match names
1122 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001123 * lines - number of output lines
1124 * linenr - line number of matches to display
1125 * maxlen - maximum number of characters in each line
1126 * showtail - display only the tail of the full path of a file name
1127 * dir_attr - highlight attribute to use for directory names
1128 */
1129 static void
1130showmatches_oneline(
1131 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001132 char_u **matches,
1133 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001134 int lines,
1135 int linenr,
1136 int maxlen,
1137 int showtail,
1138 int dir_attr)
1139{
1140 int i, j;
1141 int isdir;
1142 int lastlen;
1143 char_u *p;
1144
1145 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001146 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001147 {
1148 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1149 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001150 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1151 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 msg_advance(maxlen + 1);
1153 msg_puts((char *)p);
1154 msg_advance(maxlen + 3);
1155 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1156 break;
1157 }
1158 for (i = maxlen - lastlen; --i >= 0; )
1159 msg_putchar(' ');
1160 if (xp->xp_context == EXPAND_FILES
1161 || xp->xp_context == EXPAND_SHELLCMD
1162 || xp->xp_context == EXPAND_BUFFERS)
1163 {
1164 // highlight directories
1165 if (xp->xp_numfiles != -1)
1166 {
1167 char_u *halved_slash;
1168 char_u *exp_path;
1169 char_u *path;
1170
1171 // Expansion was done before and special characters
1172 // were escaped, need to halve backslashes. Also
1173 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001174 exp_path = expand_env_save_opt(matches[j], TRUE);
1175 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001176 halved_slash = backslash_halve_save(path);
1177 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001178 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001179 vim_free(exp_path);
1180 if (halved_slash != path)
1181 vim_free(halved_slash);
1182 }
1183 else
1184 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001185 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001186 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001187 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001188 else
1189 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001190 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001191 TRUE);
1192 p = NameBuff;
1193 }
1194 }
1195 else
1196 {
1197 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001198 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001199 }
1200 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1201 }
1202 if (msg_col > 0) // when not wrapped around
1203 {
1204 msg_clr_eos();
1205 msg_putchar('\n');
1206 }
1207 out_flush(); // show one line at a time
1208}
1209
1210/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001211 * Show all matches for completion on the command line.
1212 * Returns EXPAND_NOTHING when the character that triggered expansion should
1213 * be inserted like a normal character.
1214 */
1215 int
1216showmatches(expand_T *xp, int wildmenu UNUSED)
1217{
1218 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 int numMatches;
1220 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001221 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001222 int maxlen;
1223 int lines;
1224 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001225 int attr;
1226 int showtail;
1227
1228 if (xp->xp_numfiles == -1)
1229 {
1230 set_expand_context(xp);
1231 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001232 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001233 showtail = expand_showtail(xp);
1234 if (i != EXPAND_OK)
1235 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001236 }
1237 else
1238 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001239 numMatches = xp->xp_numfiles;
1240 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001241 showtail = cmd_showtail;
1242 }
1243
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001244 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001245 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001246 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001247
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 if (!wildmenu)
1249 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001250 msg_didany = FALSE; // lines_left will be set
1251 msg_start(); // prepare for paging
1252 msg_putchar('\n');
1253 out_flush();
1254 cmdline_row = msg_row;
1255 msg_didany = FALSE; // lines_left will be set again
1256 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258
1259 if (got_int)
1260 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001262 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 else
1264 {
1265 // find the length of the longest file name
1266 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268 {
1269 if (!showtail && (xp->xp_context == EXPAND_FILES
1270 || xp->xp_context == EXPAND_SHELLCMD
1271 || xp->xp_context == EXPAND_BUFFERS))
1272 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 j = vim_strsize(NameBuff);
1275 }
1276 else
zeertzjqc51a3762022-12-10 10:22:29 +00001277 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278 if (j > maxlen)
1279 maxlen = j;
1280 }
1281
1282 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001283 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001284 else
1285 {
1286 // compute the number of columns and lines for the listing
1287 maxlen += 2; // two spaces between file names
1288 columns = ((int)Columns + 2) / maxlen;
1289 if (columns < 1)
1290 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001291 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292 }
1293
1294 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1295
1296 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1297 {
1298 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1299 msg_clr_eos();
1300 msg_advance(maxlen - 3);
1301 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1302 }
1303
1304 // list the files line by line
1305 for (i = 0; i < lines; ++i)
1306 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001307 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001308 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001309 if (got_int)
1310 {
1311 got_int = FALSE;
1312 break;
1313 }
1314 }
1315
1316 // we redraw the command below the lines that we have just listed
1317 // This is a bit tricky, but it saves a lot of screen updating.
1318 cmdline_row = msg_row; // will put it back later
1319 }
1320
1321 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001322 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001323
1324 return EXPAND_OK;
1325}
1326
1327/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001328 * gettail() version for showmatches() and win_redr_status_matches():
1329 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001330 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001331 static char_u *
1332showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001333{
1334 char_u *p;
1335 char_u *t = s;
1336 int had_sep = FALSE;
1337
1338 for (p = s; *p != NUL; )
1339 {
1340 if (vim_ispathsep(*p)
1341#ifdef BACKSLASH_IN_FILENAME
1342 && !rem_backslash(p)
1343#endif
1344 )
1345 had_sep = TRUE;
1346 else if (had_sep)
1347 {
1348 t = p;
1349 had_sep = FALSE;
1350 }
1351 MB_PTR_ADV(p);
1352 }
1353 return t;
1354}
1355
1356/*
1357 * Return TRUE if we only need to show the tail of completion matches.
1358 * When not completing file names or there is a wildcard in the path FALSE is
1359 * returned.
1360 */
1361 static int
1362expand_showtail(expand_T *xp)
1363{
1364 char_u *s;
1365 char_u *end;
1366
1367 // When not completing file names a "/" may mean something different.
1368 if (xp->xp_context != EXPAND_FILES
1369 && xp->xp_context != EXPAND_SHELLCMD
1370 && xp->xp_context != EXPAND_DIRECTORIES)
1371 return FALSE;
1372
1373 end = gettail(xp->xp_pattern);
1374 if (end == xp->xp_pattern) // there is no path separator
1375 return FALSE;
1376
1377 for (s = xp->xp_pattern; s < end; s++)
1378 {
1379 // Skip escaped wildcards. Only when the backslash is not a path
1380 // separator, on DOS the '*' "path\*\file" must not be skipped.
1381 if (rem_backslash(s))
1382 ++s;
1383 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1384 return FALSE;
1385 }
1386 return TRUE;
1387}
1388
1389/*
1390 * Prepare a string for expansion.
1391 * When expanding file names: The string will be used with expand_wildcards().
1392 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1393 * When expanding other names: The string will be used with regcomp(). Copy
1394 * the name into allocated memory and prepend "^".
1395 */
1396 char_u *
1397addstar(
1398 char_u *fname,
1399 int len,
1400 int context) // EXPAND_FILES etc.
1401{
1402 char_u *retval;
1403 int i, j;
1404 int new_len;
1405 char_u *tail;
1406 int ends_in_star;
1407
1408 if (context != EXPAND_FILES
1409 && context != EXPAND_FILES_IN_PATH
1410 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001411 && context != EXPAND_DIRECTORIES
1412 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001413 {
1414 // Matching will be done internally (on something other than files).
1415 // So we convert the file-matching-type wildcards into our kind for
1416 // use with vim_regcomp(). First work out how long it will be:
1417
1418 // For help tags the translation is done in find_help_tags().
1419 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001420 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001421 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001422 || context == EXPAND_COLORS
1423 || context == EXPAND_COMPILER
1424 || context == EXPAND_OWNSYNTAX
1425 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001426 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001427 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001428 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001429 || ((context == EXPAND_TAGS_LISTFILES
1430 || context == EXPAND_TAGS)
1431 && fname[0] == '/'))
1432 retval = vim_strnsave(fname, len);
1433 else
1434 {
1435 new_len = len + 2; // +2 for '^' at start, NUL at end
1436 for (i = 0; i < len; i++)
1437 {
1438 if (fname[i] == '*' || fname[i] == '~')
1439 new_len++; // '*' needs to be replaced by ".*"
1440 // '~' needs to be replaced by "\~"
1441
1442 // Buffer names are like file names. "." should be literal
1443 if (context == EXPAND_BUFFERS && fname[i] == '.')
1444 new_len++; // "." becomes "\."
1445
1446 // Custom expansion takes care of special things, match
1447 // backslashes literally (perhaps also for other types?)
1448 if ((context == EXPAND_USER_DEFINED
1449 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1450 new_len++; // '\' becomes "\\"
1451 }
1452 retval = alloc(new_len);
1453 if (retval != NULL)
1454 {
1455 retval[0] = '^';
1456 j = 1;
1457 for (i = 0; i < len; i++, j++)
1458 {
1459 // Skip backslash. But why? At least keep it for custom
1460 // expansion.
1461 if (context != EXPAND_USER_DEFINED
1462 && context != EXPAND_USER_LIST
1463 && fname[i] == '\\'
1464 && ++i == len)
1465 break;
1466
1467 switch (fname[i])
1468 {
1469 case '*': retval[j++] = '.';
1470 break;
1471 case '~': retval[j++] = '\\';
1472 break;
1473 case '?': retval[j] = '.';
1474 continue;
1475 case '.': if (context == EXPAND_BUFFERS)
1476 retval[j++] = '\\';
1477 break;
1478 case '\\': if (context == EXPAND_USER_DEFINED
1479 || context == EXPAND_USER_LIST)
1480 retval[j++] = '\\';
1481 break;
1482 }
1483 retval[j] = fname[i];
1484 }
1485 retval[j] = NUL;
1486 }
1487 }
1488 }
1489 else
1490 {
1491 retval = alloc(len + 4);
1492 if (retval != NULL)
1493 {
1494 vim_strncpy(retval, fname, len);
1495
1496 // Don't add a star to *, ~, ~user, $var or `cmd`.
1497 // * would become **, which walks the whole tree.
1498 // ~ would be at the start of the file name, but not the tail.
1499 // $ could be anywhere in the tail.
1500 // ` could be anywhere in the file name.
1501 // When the name ends in '$' don't add a star, remove the '$'.
1502 tail = gettail(retval);
1503 ends_in_star = (len > 0 && retval[len - 1] == '*');
1504#ifndef BACKSLASH_IN_FILENAME
1505 for (i = len - 2; i >= 0; --i)
1506 {
1507 if (retval[i] != '\\')
1508 break;
1509 ends_in_star = !ends_in_star;
1510 }
1511#endif
1512 if ((*retval != '~' || tail != retval)
1513 && !ends_in_star
1514 && vim_strchr(tail, '$') == NULL
1515 && vim_strchr(retval, '`') == NULL)
1516 retval[len++] = '*';
1517 else if (len > 0 && retval[len - 1] == '$')
1518 --len;
1519 retval[len] = NUL;
1520 }
1521 }
1522 return retval;
1523}
1524
1525/*
1526 * Must parse the command line so far to work out what context we are in.
1527 * Completion can then be done based on that context.
1528 * This routine sets the variables:
1529 * xp->xp_pattern The start of the pattern to be expanded within
1530 * the command line (ends at the cursor).
1531 * xp->xp_context The type of thing to expand. Will be one of:
1532 *
1533 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1534 * the command line, like an unknown command. Caller
1535 * should beep.
1536 * EXPAND_NOTHING Unrecognised context for completion, use char like
1537 * a normal char, rather than for completion. eg
1538 * :s/^I/
1539 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1540 * it.
1541 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1542 * EXPAND_FILES After command with EX_XFILE set, or after setting
1543 * with P_EXPAND set. eg :e ^I, :w>>^I
1544 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001545 * when we know only directories are of interest.
1546 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001547 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1548 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1549 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1550 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1551 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1552 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1553 * EXPAND_EVENTS Complete event names
1554 * EXPAND_SYNTAX Complete :syntax command arguments
1555 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1556 * EXPAND_AUGROUP Complete autocommand group names
1557 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1558 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1559 * eg :unmap a^I , :cunab x^I
1560 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1561 * eg :call sub^I
1562 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1563 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1564 * names in expressions, eg :while s^I
1565 * EXPAND_ENV_VARS Complete environment variable names
1566 * EXPAND_USER Complete user names
1567 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001568 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001569set_expand_context(expand_T *xp)
1570{
1571 cmdline_info_T *ccline = get_cmdline_info();
1572
1573 // only expansion for ':', '>' and '=' command-lines
1574 if (ccline->cmdfirstc != ':'
1575#ifdef FEAT_EVAL
1576 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1577 && !ccline->input_fn
1578#endif
1579 )
1580 {
1581 xp->xp_context = EXPAND_NOTHING;
1582 return;
1583 }
1584 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1585}
1586
Bram Moolenaard0190392019-08-23 21:17:35 +02001587/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001588 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1589 * For user defined commands, the completion context is set in 'xp' and the
1590 * completion flags in 'complp'.
1591 *
1592 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001593 */
1594 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001595set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001596{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001597 char_u *p = NULL;
1598 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001599 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001600
1601 // Isolate the command and search for it in the command table.
1602 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001603 // - the 'k' command can directly be followed by any character, but do
1604 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1605 // find matches anywhere in the command name, do this only for command
1606 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001607 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001608 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001609 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001610 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001611 p = cmd + 1;
1612 }
1613 else
1614 {
1615 p = cmd;
1616 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1617 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001618 // A user command may contain digits.
1619 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1620 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001621 while (ASCII_ISALNUM(*p) || *p == '*')
1622 ++p;
1623 // for python 3.x: ":py3*" commands completion
1624 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1625 {
1626 ++p;
1627 while (ASCII_ISALPHA(*p) || *p == '*')
1628 ++p;
1629 }
1630 // check for non-alpha command
1631 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1632 ++p;
1633 len = (int)(p - cmd);
1634
1635 if (len == 0)
1636 {
1637 xp->xp_context = EXPAND_UNSUCCESSFUL;
1638 return NULL;
1639 }
1640
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001641 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001642
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001643 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001644 // Also when doing fuzzy expansion for non-shell commands, support
1645 // alphanumeric characters.
1646 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1647 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001648 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1649 ++p;
1650 }
1651
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001652 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001653 // character, complete the command name.
1654 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1655 return NULL;
1656
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001657 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001658 {
1659 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1660 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001661 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 p = cmd + 1;
1663 }
1664 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1665 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001666 eap->cmd = cmd;
1667 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001668 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001669 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001670 }
1671 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001672 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001673 {
1674 // Not still touching the command and it was an illegal one
1675 xp->xp_context = EXPAND_UNSUCCESSFUL;
1676 return NULL;
1677 }
1678
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001679 return p;
1680}
Bram Moolenaard0190392019-08-23 21:17:35 +02001681
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001682/*
1683 * Set the completion context for a command argument with wild card characters.
1684 */
1685 static void
1686set_context_for_wildcard_arg(
1687 exarg_T *eap,
1688 char_u *arg,
1689 int usefilter,
1690 expand_T *xp,
1691 int *complp)
1692{
1693 char_u *p;
1694 int c;
1695 int in_quote = FALSE;
1696 char_u *bow = NULL; // Beginning of word
1697 int len = 0;
1698
1699 // Allow spaces within back-quotes to count as part of the argument
1700 // being expanded.
1701 xp->xp_pattern = skipwhite(arg);
1702 p = xp->xp_pattern;
1703 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001704 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001705 if (has_mbyte)
1706 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001707 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001708 c = *p;
1709 if (c == '\\' && p[1] != NUL)
1710 ++p;
1711 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001712 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001713 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001714 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001715 xp->xp_pattern = p;
1716 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001717 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001718 in_quote = !in_quote;
1719 }
1720 // An argument can contain just about everything, except
1721 // characters that end the command and white space.
1722 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001723#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001724 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001725#endif
1726 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001727 {
1728 len = 0; // avoid getting stuck when space is in 'isfname'
1729 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001730 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001731 if (has_mbyte)
1732 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001733 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001734 c = *p;
1735 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001736 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001737 if (has_mbyte)
1738 len = (*mb_ptr2len)(p);
1739 else
1740 len = 1;
1741 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001742 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001743 if (in_quote)
1744 bow = p;
1745 else
1746 xp->xp_pattern = p;
1747 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001748 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001749 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001750 }
1751
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 // If we are still inside the quotes, and we passed a space, just
1753 // expand from there.
1754 if (bow != NULL && in_quote)
1755 xp->xp_pattern = bow;
1756 xp->xp_context = EXPAND_FILES;
1757
1758 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001759 if (usefilter
1760 || (eap != NULL
1761 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1762 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001763 {
1764#ifndef BACKSLASH_IN_FILENAME
1765 xp->xp_shell = TRUE;
1766#endif
1767 // When still after the command name expand executables.
1768 if (xp->xp_pattern == skipwhite(arg))
1769 xp->xp_context = EXPAND_SHELLCMD;
1770 }
1771
1772 // Check for environment variable.
1773 if (*xp->xp_pattern == '$')
1774 {
1775 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1776 if (!vim_isIDc(*p))
1777 break;
1778 if (*p == NUL)
1779 {
1780 xp->xp_context = EXPAND_ENV_VARS;
1781 ++xp->xp_pattern;
1782 // Avoid that the assignment uses EXPAND_FILES again.
1783 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1784 *complp = EXPAND_ENV_VARS;
1785 }
1786 }
1787 // Check for user names.
1788 if (*xp->xp_pattern == '~')
1789 {
1790 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1791 ;
1792 // Complete ~user only if it partially matches a user name.
1793 // A full match ~user<Tab> will be replaced by user's home
1794 // directory i.e. something like ~user<Tab> -> /home/user/
1795 if (*p == NUL && p > xp->xp_pattern + 1
1796 && match_user(xp->xp_pattern + 1) >= 1)
1797 {
1798 xp->xp_context = EXPAND_USER;
1799 ++xp->xp_pattern;
1800 }
1801 }
1802}
1803
1804/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001805 * Set the completion context for the "++opt=arg" argument. Always returns
1806 * NULL.
1807 */
1808 static char_u *
1809set_context_in_argopt(expand_T *xp, char_u *arg)
1810{
1811 char_u *p;
1812
1813 p = vim_strchr(arg, '=');
1814 if (p == NULL)
1815 xp->xp_pattern = arg;
1816 else
1817 xp->xp_pattern = p + 1;
1818
1819 xp->xp_context = EXPAND_ARGOPT;
1820 return NULL;
1821}
1822
1823#ifdef FEAT_TERMINAL
1824/*
1825 * Set the completion context for :terminal's [options]. Always returns NULL.
1826 */
1827 static char_u *
1828set_context_in_terminalopt(expand_T *xp, char_u *arg)
1829{
1830 char_u *p;
1831
1832 p = vim_strchr(arg, '=');
1833 if (p == NULL)
1834 xp->xp_pattern = arg;
1835 else
1836 xp->xp_pattern = p + 1;
1837
1838 xp->xp_context = EXPAND_TERMINALOPT;
1839 return NULL;
1840}
1841#endif
1842
1843/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001844 * Set the completion context for the :filter command. Returns a pointer to the
1845 * next command after the :filter command.
1846 */
1847 static char_u *
1848set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1849{
1850 if (*arg != NUL)
1851 arg = skip_vimgrep_pat(arg, NULL, NULL);
1852 if (arg == NULL || *arg == NUL)
1853 {
1854 xp->xp_context = EXPAND_NOTHING;
1855 return NULL;
1856 }
1857 return skipwhite(arg);
1858}
1859
1860#ifdef FEAT_SEARCH_EXTRA
1861/*
1862 * Set the completion context for the :match command. Returns a pointer to the
1863 * next command after the :match command.
1864 */
1865 static char_u *
1866set_context_in_match_cmd(expand_T *xp, char_u *arg)
1867{
1868 if (*arg == NUL || !ends_excmd(*arg))
1869 {
1870 // also complete "None"
1871 set_context_in_echohl_cmd(xp, arg);
1872 arg = skipwhite(skiptowhite(arg));
1873 if (*arg != NUL)
1874 {
1875 xp->xp_context = EXPAND_NOTHING;
1876 arg = skip_regexp(arg + 1, *arg, magic_isset());
1877 }
1878 }
1879 return find_nextcmd(arg);
1880}
1881#endif
1882
1883/*
1884 * Returns a pointer to the next command after a :global or a :v command.
1885 * Returns NULL if there is no next command.
1886 */
1887 static char_u *
1888find_cmd_after_global_cmd(char_u *arg)
1889{
1890 int delim;
1891
1892 delim = *arg; // get the delimiter
1893 if (delim)
1894 ++arg; // skip delimiter if there is one
1895
1896 while (arg[0] != NUL && arg[0] != delim)
1897 {
1898 if (arg[0] == '\\' && arg[1] != NUL)
1899 ++arg;
1900 ++arg;
1901 }
1902 if (arg[0] != NUL)
1903 return arg + 1;
1904
1905 return NULL;
1906}
1907
1908/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001909 * Returns a pointer to the next command after a :substitute or a :& command.
1910 * Returns NULL if there is no next command.
1911 */
1912 static char_u *
1913find_cmd_after_substitute_cmd(char_u *arg)
1914{
1915 int delim;
1916
1917 delim = *arg;
1918 if (delim)
1919 {
1920 // skip "from" part
1921 ++arg;
1922 arg = skip_regexp(arg, delim, magic_isset());
1923
1924 if (arg[0] != NUL && arg[0] == delim)
1925 {
1926 // skip "to" part
1927 ++arg;
1928 while (arg[0] != NUL && arg[0] != delim)
1929 {
1930 if (arg[0] == '\\' && arg[1] != NUL)
1931 ++arg;
1932 ++arg;
1933 }
1934 if (arg[0] != NUL) // skip delimiter
1935 ++arg;
1936 }
1937 }
1938 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1939 ++arg;
1940 if (arg[0] != NUL)
1941 return arg;
1942
1943 return NULL;
1944}
1945
1946/*
1947 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1948 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1949 * Returns NULL if there is no next command.
1950 */
1951 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001952find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001953{
1954 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001955 if (*arg != '/')
1956 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001957
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001958 // Match regexp, not just whole words
1959 for (++arg; *arg && *arg != '/'; arg++)
1960 if (*arg == '\\' && arg[1] != NUL)
1961 arg++;
1962 if (*arg)
1963 {
1964 arg = skipwhite(arg + 1);
1965
1966 // Check for trailing illegal characters
1967 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1968 xp->xp_context = EXPAND_NOTHING;
1969 else
1970 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001971 }
1972
1973 return NULL;
1974}
1975
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001976#ifdef FEAT_EVAL
1977/*
1978 * Set the completion context for the :unlet command. Always returns NULL.
1979 */
1980 static char_u *
1981set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1982{
1983 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1984 arg = xp->xp_pattern + 1;
1985
1986 xp->xp_context = EXPAND_USER_VARS;
1987 xp->xp_pattern = arg;
1988
1989 if (*xp->xp_pattern == '$')
1990 {
1991 xp->xp_context = EXPAND_ENV_VARS;
1992 ++xp->xp_pattern;
1993 }
1994
1995 return NULL;
1996}
1997#endif
1998
1999#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2000/*
2001 * Set the completion context for the :language command. Always returns NULL.
2002 */
2003 static char_u *
2004set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2005{
2006 char_u *p;
2007
2008 p = skiptowhite(arg);
2009 if (*p == NUL)
2010 {
2011 xp->xp_context = EXPAND_LANGUAGE;
2012 xp->xp_pattern = arg;
2013 }
2014 else
2015 {
2016 if ( STRNCMP(arg, "messages", p - arg) == 0
2017 || STRNCMP(arg, "ctype", p - arg) == 0
2018 || STRNCMP(arg, "time", p - arg) == 0
2019 || STRNCMP(arg, "collate", p - arg) == 0)
2020 {
2021 xp->xp_context = EXPAND_LOCALES;
2022 xp->xp_pattern = skipwhite(p);
2023 }
2024 else
2025 xp->xp_context = EXPAND_NOTHING;
2026 }
2027
2028 return NULL;
2029}
2030#endif
2031
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002032#ifdef FEAT_EVAL
2033static enum
2034{
2035 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002036 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2037 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002038} breakpt_expand_what;
2039
2040/*
2041 * Set the completion context for the :breakadd command. Always returns NULL.
2042 */
2043 static char_u *
2044set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2045{
2046 char_u *p;
2047 char_u *subcmd_start;
2048
2049 xp->xp_context = EXPAND_BREAKPOINT;
2050 xp->xp_pattern = arg;
2051
2052 if (cmdidx == CMD_breakadd)
2053 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002054 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002055 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002056 else
2057 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002058
2059 p = skipwhite(arg);
2060 if (*p == NUL)
2061 return NULL;
2062 subcmd_start = p;
2063
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002064 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002065 {
2066 // :breakadd file [lnum] <filename>
2067 // :breakadd func [lnum] <funcname>
2068 p += 4;
2069 p = skipwhite(p);
2070
2071 // skip line number (if specified)
2072 if (VIM_ISDIGIT(*p))
2073 {
2074 p = skipdigits(p);
2075 if (*p != ' ')
2076 {
2077 xp->xp_context = EXPAND_NOTHING;
2078 return NULL;
2079 }
2080 p = skipwhite(p);
2081 }
2082 if (STRNCMP("file", subcmd_start, 4) == 0)
2083 xp->xp_context = EXPAND_FILES;
2084 else
2085 xp->xp_context = EXPAND_USER_FUNC;
2086 xp->xp_pattern = p;
2087 }
2088 else if (STRNCMP("expr ", p, 5) == 0)
2089 {
2090 // :breakadd expr <expression>
2091 xp->xp_context = EXPAND_EXPRESSION;
2092 xp->xp_pattern = skipwhite(p + 5);
2093 }
2094
2095 return NULL;
2096}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002097
2098 static char_u *
2099set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2100{
2101 char_u *p;
2102
2103 xp->xp_context = EXPAND_NOTHING;
2104 xp->xp_pattern = NULL;
2105
2106 p = skipwhite(arg);
2107 if (VIM_ISDIGIT(*p))
2108 return NULL;
2109
2110 xp->xp_context = EXPAND_SCRIPTNAMES;
2111 xp->xp_pattern = p;
2112
2113 return NULL;
2114}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002115#endif
2116
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002117/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002118 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2119 * The argument to the command is 'arg' and the argument flags is 'argt'.
2120 * For user-defined commands and for environment variables, 'compl' has the
2121 * completion type.
2122 * Returns a pointer to the next command. Returns NULL if there is no next
2123 * command.
2124 */
2125 static char_u *
2126set_context_by_cmdname(
2127 char_u *cmd,
2128 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002129 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002130 char_u *arg,
2131 long argt,
2132 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002133 int forceit)
2134{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002135 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002136 {
2137 case CMD_find:
2138 case CMD_sfind:
2139 case CMD_tabfind:
2140 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002141 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002142 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002143 break;
2144 case CMD_cd:
2145 case CMD_chdir:
2146 case CMD_tcd:
2147 case CMD_tchdir:
2148 case CMD_lcd:
2149 case CMD_lchdir:
2150 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002151 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002152 break;
2153 case CMD_help:
2154 xp->xp_context = EXPAND_HELP;
2155 xp->xp_pattern = arg;
2156 break;
2157
2158 // Command modifiers: return the argument.
2159 // Also for commands with an argument that is a command.
2160 case CMD_aboveleft:
2161 case CMD_argdo:
2162 case CMD_belowright:
2163 case CMD_botright:
2164 case CMD_browse:
2165 case CMD_bufdo:
2166 case CMD_cdo:
2167 case CMD_cfdo:
2168 case CMD_confirm:
2169 case CMD_debug:
2170 case CMD_folddoclosed:
2171 case CMD_folddoopen:
2172 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002173 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002174 case CMD_keepalt:
2175 case CMD_keepjumps:
2176 case CMD_keepmarks:
2177 case CMD_keeppatterns:
2178 case CMD_ldo:
2179 case CMD_leftabove:
2180 case CMD_lfdo:
2181 case CMD_lockmarks:
2182 case CMD_noautocmd:
2183 case CMD_noswapfile:
2184 case CMD_rightbelow:
2185 case CMD_sandbox:
2186 case CMD_silent:
2187 case CMD_tab:
2188 case CMD_tabdo:
2189 case CMD_topleft:
2190 case CMD_verbose:
2191 case CMD_vertical:
2192 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002193 case CMD_vim9cmd:
2194 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002195 return arg;
2196
2197 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002198 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002199
2200#ifdef FEAT_SEARCH_EXTRA
2201 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002202 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002203#endif
2204
2205 // All completion for the +cmdline_compl feature goes here.
2206
2207 case CMD_command:
2208 return set_context_in_user_cmd(xp, arg);
2209
2210 case CMD_delcommand:
2211 xp->xp_context = EXPAND_USER_COMMANDS;
2212 xp->xp_pattern = arg;
2213 break;
2214
2215 case CMD_global:
2216 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002217 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002218 case CMD_and:
2219 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002220 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002221 case CMD_isearch:
2222 case CMD_dsearch:
2223 case CMD_ilist:
2224 case CMD_dlist:
2225 case CMD_ijump:
2226 case CMD_psearch:
2227 case CMD_djump:
2228 case CMD_isplit:
2229 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002230 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002231 case CMD_autocmd:
2232 return set_context_in_autocmd(xp, arg, FALSE);
2233 case CMD_doautocmd:
2234 case CMD_doautoall:
2235 return set_context_in_autocmd(xp, arg, TRUE);
2236 case CMD_set:
2237 set_context_in_set_cmd(xp, arg, 0);
2238 break;
2239 case CMD_setglobal:
2240 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2241 break;
2242 case CMD_setlocal:
2243 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2244 break;
2245 case CMD_tag:
2246 case CMD_stag:
2247 case CMD_ptag:
2248 case CMD_ltag:
2249 case CMD_tselect:
2250 case CMD_stselect:
2251 case CMD_ptselect:
2252 case CMD_tjump:
2253 case CMD_stjump:
2254 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002255 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002256 xp->xp_context = EXPAND_TAGS_LISTFILES;
2257 else
2258 xp->xp_context = EXPAND_TAGS;
2259 xp->xp_pattern = arg;
2260 break;
2261 case CMD_augroup:
2262 xp->xp_context = EXPAND_AUGROUP;
2263 xp->xp_pattern = arg;
2264 break;
2265#ifdef FEAT_SYN_HL
2266 case CMD_syntax:
2267 set_context_in_syntax_cmd(xp, arg);
2268 break;
2269#endif
2270#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002271 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002272 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002273 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002274 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002275 case CMD_if:
2276 case CMD_elseif:
2277 case CMD_while:
2278 case CMD_for:
2279 case CMD_echo:
2280 case CMD_echon:
2281 case CMD_execute:
2282 case CMD_echomsg:
2283 case CMD_echoerr:
2284 case CMD_call:
2285 case CMD_return:
2286 case CMD_cexpr:
2287 case CMD_caddexpr:
2288 case CMD_cgetexpr:
2289 case CMD_lexpr:
2290 case CMD_laddexpr:
2291 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002292 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002293 break;
2294
2295 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002296 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002297 case CMD_function:
2298 case CMD_delfunction:
2299 xp->xp_context = EXPAND_USER_FUNC;
2300 xp->xp_pattern = arg;
2301 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002302 case CMD_disassemble:
2303 set_context_in_disassemble_cmd(xp, arg);
2304 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002305
2306 case CMD_echohl:
2307 set_context_in_echohl_cmd(xp, arg);
2308 break;
2309#endif
2310 case CMD_highlight:
2311 set_context_in_highlight_cmd(xp, arg);
2312 break;
2313#ifdef FEAT_CSCOPE
2314 case CMD_cscope:
2315 case CMD_lcscope:
2316 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002317 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002318 break;
2319#endif
2320#ifdef FEAT_SIGNS
2321 case CMD_sign:
2322 set_context_in_sign_cmd(xp, arg);
2323 break;
2324#endif
2325 case CMD_bdelete:
2326 case CMD_bwipeout:
2327 case CMD_bunload:
2328 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2329 arg = xp->xp_pattern + 1;
2330 // FALLTHROUGH
2331 case CMD_buffer:
2332 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002333 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002334 case CMD_checktime:
2335 xp->xp_context = EXPAND_BUFFERS;
2336 xp->xp_pattern = arg;
2337 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002338#ifdef FEAT_DIFF
2339 case CMD_diffget:
2340 case CMD_diffput:
2341 // If current buffer is in diff mode, complete buffer names
2342 // which are in diff mode, and different than current buffer.
2343 xp->xp_context = EXPAND_DIFF_BUFFERS;
2344 xp->xp_pattern = arg;
2345 break;
2346#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002347 case CMD_USER:
2348 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002349 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2350 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002351
2352 case CMD_map: case CMD_noremap:
2353 case CMD_nmap: case CMD_nnoremap:
2354 case CMD_vmap: case CMD_vnoremap:
2355 case CMD_omap: case CMD_onoremap:
2356 case CMD_imap: case CMD_inoremap:
2357 case CMD_cmap: case CMD_cnoremap:
2358 case CMD_lmap: case CMD_lnoremap:
2359 case CMD_smap: case CMD_snoremap:
2360 case CMD_tmap: case CMD_tnoremap:
2361 case CMD_xmap: case CMD_xnoremap:
2362 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002363 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002364 case CMD_unmap:
2365 case CMD_nunmap:
2366 case CMD_vunmap:
2367 case CMD_ounmap:
2368 case CMD_iunmap:
2369 case CMD_cunmap:
2370 case CMD_lunmap:
2371 case CMD_sunmap:
2372 case CMD_tunmap:
2373 case CMD_xunmap:
2374 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002375 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002376 case CMD_mapclear:
2377 case CMD_nmapclear:
2378 case CMD_vmapclear:
2379 case CMD_omapclear:
2380 case CMD_imapclear:
2381 case CMD_cmapclear:
2382 case CMD_lmapclear:
2383 case CMD_smapclear:
2384 case CMD_tmapclear:
2385 case CMD_xmapclear:
2386 xp->xp_context = EXPAND_MAPCLEAR;
2387 xp->xp_pattern = arg;
2388 break;
2389
2390 case CMD_abbreviate: case CMD_noreabbrev:
2391 case CMD_cabbrev: case CMD_cnoreabbrev:
2392 case CMD_iabbrev: case CMD_inoreabbrev:
2393 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002394 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002395 case CMD_unabbreviate:
2396 case CMD_cunabbrev:
2397 case CMD_iunabbrev:
2398 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002399 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002400#ifdef FEAT_MENU
2401 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2402 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2403 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2404 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2405 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2406 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2407 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2408 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2409 case CMD_tmenu: case CMD_tunmenu:
2410 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2411 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2412#endif
2413
2414 case CMD_colorscheme:
2415 xp->xp_context = EXPAND_COLORS;
2416 xp->xp_pattern = arg;
2417 break;
2418
2419 case CMD_compiler:
2420 xp->xp_context = EXPAND_COMPILER;
2421 xp->xp_pattern = arg;
2422 break;
2423
2424 case CMD_ownsyntax:
2425 xp->xp_context = EXPAND_OWNSYNTAX;
2426 xp->xp_pattern = arg;
2427 break;
2428
2429 case CMD_setfiletype:
2430 xp->xp_context = EXPAND_FILETYPE;
2431 xp->xp_pattern = arg;
2432 break;
2433
2434 case CMD_packadd:
2435 xp->xp_context = EXPAND_PACKADD;
2436 xp->xp_pattern = arg;
2437 break;
2438
zeertzjqb0d45ec2023-01-25 15:04:22 +00002439 case CMD_runtime:
2440 set_context_in_runtime_cmd(xp, arg);
2441 break;
2442
Bram Moolenaard0190392019-08-23 21:17:35 +02002443#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2444 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002445 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002446#endif
2447#if defined(FEAT_PROFILE)
2448 case CMD_profile:
2449 set_context_in_profile_cmd(xp, arg);
2450 break;
2451#endif
2452 case CMD_behave:
2453 xp->xp_context = EXPAND_BEHAVE;
2454 xp->xp_pattern = arg;
2455 break;
2456
2457 case CMD_messages:
2458 xp->xp_context = EXPAND_MESSAGES;
2459 xp->xp_pattern = arg;
2460 break;
2461
2462 case CMD_history:
2463 xp->xp_context = EXPAND_HISTORY;
2464 xp->xp_pattern = arg;
2465 break;
2466#if defined(FEAT_PROFILE)
2467 case CMD_syntime:
2468 xp->xp_context = EXPAND_SYNTIME;
2469 xp->xp_pattern = arg;
2470 break;
2471#endif
2472
2473 case CMD_argdelete:
2474 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2475 arg = xp->xp_pattern + 1;
2476 xp->xp_context = EXPAND_ARGLIST;
2477 xp->xp_pattern = arg;
2478 break;
2479
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002480#ifdef FEAT_EVAL
2481 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002482 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002483 case CMD_breakdel:
2484 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002485
2486 case CMD_scriptnames:
2487 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002488#endif
2489
Bram Moolenaard0190392019-08-23 21:17:35 +02002490 default:
2491 break;
2492 }
2493 return NULL;
2494}
2495
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002496/*
2497 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2498 * we don't need/want deleted. Maybe this could be done better if we didn't
2499 * repeat all this stuff. The only problem is that they may not stay
2500 * perfectly compatible with each other, but then the command line syntax
2501 * probably won't change that much -- webb.
2502 */
2503 static char_u *
2504set_one_cmd_context(
2505 expand_T *xp,
2506 char_u *buff) // buffer for command string
2507{
2508 char_u *p;
2509 char_u *cmd, *arg;
2510 int len = 0;
2511 exarg_T ea;
2512 int compl = EXPAND_NOTHING;
2513 int forceit = FALSE;
2514 int usefilter = FALSE; // filter instead of file name
2515
2516 ExpandInit(xp);
2517 xp->xp_pattern = buff;
2518 xp->xp_line = buff;
2519 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2520 ea.argt = 0;
2521
2522 // 1. skip comment lines and leading space, colons or bars
2523 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2524 ;
2525 xp->xp_pattern = cmd;
2526
2527 if (*cmd == NUL)
2528 return NULL;
2529 if (*cmd == '"') // ignore comment lines
2530 {
2531 xp->xp_context = EXPAND_NOTHING;
2532 return NULL;
2533 }
2534
2535 // 3. Skip over the range to find the command.
2536 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2537 xp->xp_pattern = cmd;
2538 if (*cmd == NUL)
2539 return NULL;
2540 if (*cmd == '"')
2541 {
2542 xp->xp_context = EXPAND_NOTHING;
2543 return NULL;
2544 }
2545
2546 if (*cmd == '|' || *cmd == '\n')
2547 return cmd + 1; // There's another command
2548
2549 // Get the command index.
2550 p = set_cmd_index(cmd, &ea, xp, &compl);
2551 if (p == NULL)
2552 return NULL;
2553
2554 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2555
2556 if (*p == '!') // forced commands
2557 {
2558 forceit = TRUE;
2559 ++p;
2560 }
2561
2562 // 6. parse arguments
2563 if (!IS_USER_CMDIDX(ea.cmdidx))
2564 ea.argt = excmd_get_argt(ea.cmdidx);
2565
2566 arg = skipwhite(p);
2567
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002568 // Does command allow "++argopt" argument?
2569 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002570 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002571 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2572 {
2573 p = arg + 2;
2574 while (*p && !vim_isspace(*p))
2575 MB_PTR_ADV(p);
2576
2577 // Still touching the command after "++"?
2578 if (*p == NUL)
2579 {
2580 if (ea.argt & EX_ARGOPT)
2581 return set_context_in_argopt(xp, arg + 2);
2582#ifdef FEAT_TERMINAL
2583 if (ea.cmdidx == CMD_terminal)
2584 return set_context_in_terminalopt(xp, arg + 2);
2585#endif
2586 }
2587
2588 arg = skipwhite(p);
2589 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002590 }
2591
2592 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2593 {
2594 if (*arg == '>') // append
2595 {
2596 if (*++arg == '>')
2597 ++arg;
2598 arg = skipwhite(arg);
2599 }
2600 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2601 {
2602 ++arg;
2603 usefilter = TRUE;
2604 }
2605 }
2606
2607 if (ea.cmdidx == CMD_read)
2608 {
2609 usefilter = forceit; // :r! filter if forced
2610 if (*arg == '!') // :r !filter
2611 {
2612 ++arg;
2613 usefilter = TRUE;
2614 }
2615 }
2616
2617 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2618 {
2619 while (*arg == *cmd) // allow any number of '>' or '<'
2620 ++arg;
2621 arg = skipwhite(arg);
2622 }
2623
2624 // Does command allow "+command"?
2625 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2626 {
2627 // Check if we're in the +command
2628 p = arg + 1;
2629 arg = skip_cmd_arg(arg, FALSE);
2630
2631 // Still touching the command after '+'?
2632 if (*arg == NUL)
2633 return p;
2634
2635 // Skip space(s) after +command to get to the real argument
2636 arg = skipwhite(arg);
2637 }
2638
2639
2640 // Check for '|' to separate commands and '"' to start comments.
2641 // Don't do this for ":read !cmd" and ":write !cmd".
2642 if ((ea.argt & EX_TRLBAR) && !usefilter)
2643 {
2644 p = arg;
2645 // ":redir @" is not the start of a comment
2646 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2647 p += 2;
2648 while (*p)
2649 {
2650 if (*p == Ctrl_V)
2651 {
2652 if (p[1] != NUL)
2653 ++p;
2654 }
2655 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2656 || *p == '|' || *p == '\n')
2657 {
2658 if (*(p - 1) != '\\')
2659 {
2660 if (*p == '|' || *p == '\n')
2661 return p + 1;
2662 return NULL; // It's a comment
2663 }
2664 }
2665 MB_PTR_ADV(p);
2666 }
2667 }
2668
2669 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2670 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2671 // no arguments allowed but there is something
2672 return NULL;
2673
2674 // Find start of last argument (argument just before cursor):
2675 p = buff;
2676 xp->xp_pattern = p;
2677 len = (int)STRLEN(buff);
2678 while (*p && p < buff + len)
2679 {
2680 if (*p == ' ' || *p == TAB)
2681 {
2682 // argument starts after a space
2683 xp->xp_pattern = ++p;
2684 }
2685 else
2686 {
2687 if (*p == '\\' && *(p + 1) != NUL)
2688 ++p; // skip over escaped character
2689 MB_PTR_ADV(p);
2690 }
2691 }
2692
2693 if (ea.argt & EX_XFILE)
2694 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2695
2696 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002697 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002698 forceit);
2699}
2700
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002701/*
2702 * Set the completion context in 'xp' for command 'str'
2703 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002704 void
2705set_cmd_context(
2706 expand_T *xp,
2707 char_u *str, // start of command line
2708 int len, // length of command line (excl. NUL)
2709 int col, // position of cursor
2710 int use_ccline UNUSED) // use ccline for info
2711{
2712#ifdef FEAT_EVAL
2713 cmdline_info_T *ccline = get_cmdline_info();
2714#endif
2715 int old_char = NUL;
2716 char_u *nextcomm;
2717
2718 // Avoid a UMR warning from Purify, only save the character if it has been
2719 // written before.
2720 if (col < len)
2721 old_char = str[col];
2722 str[col] = NUL;
2723 nextcomm = str;
2724
2725#ifdef FEAT_EVAL
2726 if (use_ccline && ccline->cmdfirstc == '=')
2727 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002728 // pass CMD_SIZE because there is no real command
2729 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002730 }
2731 else if (use_ccline && ccline->input_fn)
2732 {
2733 xp->xp_context = ccline->xp_context;
2734 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002735 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002736 }
2737 else
2738#endif
2739 while (nextcomm != NULL)
2740 nextcomm = set_one_cmd_context(xp, nextcomm);
2741
2742 // Store the string here so that call_user_expand_func() can get to them
2743 // easily.
2744 xp->xp_line = str;
2745 xp->xp_col = col;
2746
2747 str[col] = old_char;
2748}
2749
2750/*
2751 * Expand the command line "str" from context "xp".
2752 * "xp" must have been set by set_cmd_context().
2753 * xp->xp_pattern points into "str", to where the text that is to be expanded
2754 * starts.
2755 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2756 * cursor.
2757 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2758 * key that triggered expansion literally.
2759 * Returns EXPAND_OK otherwise.
2760 */
2761 int
2762expand_cmdline(
2763 expand_T *xp,
2764 char_u *str, // start of command line
2765 int col, // position of cursor
2766 int *matchcount, // return: nr of matches
2767 char_u ***matches) // return: array of pointers to matches
2768{
2769 char_u *file_str = NULL;
2770 int options = WILD_ADD_SLASH|WILD_SILENT;
2771
2772 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2773 {
2774 beep_flush();
2775 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2776 }
2777 if (xp->xp_context == EXPAND_NOTHING)
2778 {
2779 // Caller can use the character as a normal char instead
2780 return EXPAND_NOTHING;
2781 }
2782
2783 // add star to file name, or convert to regexp if not exp. files.
2784 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002785 if (cmdline_fuzzy_completion_supported(xp))
2786 // If fuzzy matching, don't modify the search string
2787 file_str = vim_strsave(xp->xp_pattern);
2788 else
2789 {
2790 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2791 if (file_str == NULL)
2792 return EXPAND_UNSUCCESSFUL;
2793 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794
2795 if (p_wic)
2796 options += WILD_ICASE;
2797
2798 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002799 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002800 {
2801 *matchcount = 0;
2802 *matches = NULL;
2803 }
2804 vim_free(file_str);
2805
2806 return EXPAND_OK;
2807}
2808
Bram Moolenaar66b51422019-08-18 21:44:12 +02002809/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002810 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002811 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002812 */
2813 static int
2814expand_files_and_dirs(
2815 expand_T *xp,
2816 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002817 char_u ***matches,
2818 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002819 int flags,
2820 int options)
2821{
2822 int free_pat = FALSE;
2823 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002824 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002825
2826 // for ":set path=" and ":set tags=" halve backslashes for escaped
2827 // space
2828 if (xp->xp_backslash != XP_BS_NONE)
2829 {
2830 free_pat = TRUE;
2831 pat = vim_strsave(pat);
2832 for (i = 0; pat[i]; ++i)
2833 if (pat[i] == '\\')
2834 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002835 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002836 && pat[i + 1] == '\\'
2837 && pat[i + 2] == '\\'
2838 && pat[i + 3] == ' ')
2839 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002840 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 && pat[i + 1] == ' ')
2842 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002843 else if ((xp->xp_backslash & XP_BS_COMMA)
2844 && pat[i + 1] == '\\'
2845 && pat[i + 2] == ',')
2846 STRMOVE(pat + i, pat + i + 2);
2847#ifdef BACKSLASH_IN_FILENAME
2848 else if ((xp->xp_backslash & XP_BS_COMMA)
2849 && pat[i + 1] == ',')
2850 STRMOVE(pat + i, pat + i + 1);
2851#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002852 }
2853 }
2854
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002855 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002856 {
2857#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002858 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002859#endif
2860 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002861 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002862 {
2863 if (xp->xp_context == EXPAND_FILES)
2864 flags |= EW_FILE;
2865 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2866 flags |= (EW_FILE | EW_PATH);
2867 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2868 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2869 else
2870 flags = (flags | EW_DIR) & ~EW_FILE;
2871 if (options & WILD_ICASE)
2872 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002873
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002874 // Expand wildcards, supporting %:h and the like.
2875 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2876 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002877 if (free_pat)
2878 vim_free(pat);
2879#ifdef BACKSLASH_IN_FILENAME
2880 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2881 {
2882 int j;
2883
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002884 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002885 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002886 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002887
2888 while (*ptr != NUL)
2889 {
2890 if (p_csl[0] == 's' && *ptr == '\\')
2891 *ptr = '/';
2892 else if (p_csl[0] == 'b' && *ptr == '/')
2893 *ptr = '\\';
2894 ptr += (*mb_ptr2len)(ptr);
2895 }
2896 }
2897 }
2898#endif
2899 return ret;
2900}
2901
2902/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002903 * Function given to ExpandGeneric() to obtain the possible arguments of the
2904 * ":behave {mswin,xterm}" command.
2905 */
2906 static char_u *
2907get_behave_arg(expand_T *xp UNUSED, int idx)
2908{
2909 if (idx == 0)
2910 return (char_u *)"mswin";
2911 if (idx == 1)
2912 return (char_u *)"xterm";
2913 return NULL;
2914}
2915
K.Takata161b6ac2022-11-14 15:31:07 +00002916#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002917/*
2918 * Function given to ExpandGeneric() to obtain the possible arguments of the
2919 * ":breakadd {expr, file, func, here}" command.
2920 * ":breakdel {func, file, here}" command.
2921 */
2922 static char_u *
2923get_breakadd_arg(expand_T *xp UNUSED, int idx)
2924{
2925 char *opts[] = {"expr", "file", "func", "here"};
2926
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002927 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002928 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002929 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002930 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2931 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002932 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2933 {
2934 // breakdel {func, file, here}
2935 if (idx <= 2)
2936 return (char_u *)opts[idx + 1];
2937 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002938 else
2939 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002940 // profdel {func, file}
2941 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002942 return (char_u *)opts[idx + 1];
2943 }
2944 }
2945 return NULL;
2946}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002947
2948/*
2949 * Function given to ExpandGeneric() to obtain the possible arguments for the
2950 * ":scriptnames" command.
2951 */
2952 static char_u *
2953get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2954{
2955 scriptitem_T *si;
2956
2957 if (!SCRIPT_ID_VALID(idx + 1))
2958 return NULL;
2959
2960 si = SCRIPT_ITEM(idx + 1);
2961 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2962 return NameBuff;
2963}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002964#endif
2965
Bram Moolenaard0190392019-08-23 21:17:35 +02002966/*
2967 * Function given to ExpandGeneric() to obtain the possible arguments of the
2968 * ":messages {clear}" command.
2969 */
2970 static char_u *
2971get_messages_arg(expand_T *xp UNUSED, int idx)
2972{
2973 if (idx == 0)
2974 return (char_u *)"clear";
2975 return NULL;
2976}
2977
2978 static char_u *
2979get_mapclear_arg(expand_T *xp UNUSED, int idx)
2980{
2981 if (idx == 0)
2982 return (char_u *)"<buffer>";
2983 return NULL;
2984}
2985
2986/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002987 * Do the expansion based on xp->xp_context and 'rmp'.
2988 */
2989 static int
2990ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002991 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002992 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002993 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002994 char_u ***matches,
2995 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002996{
2997 static struct expgen
2998 {
2999 int context;
3000 char_u *((*func)(expand_T *, int));
3001 int ic;
3002 int escaped;
3003 } tab[] =
3004 {
3005 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
3006 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
3007 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
3008 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
3009 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
3010 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3011 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3012 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3013 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3014 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003015#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003016 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3017 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3018 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3019 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3020 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003021#endif
3022#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003023 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3024 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003025#endif
3026#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003027 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003028#endif
3029#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003030 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003031#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003032 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3033 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3034 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003035#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003036 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003037#endif
3038#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003039 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003040#endif
3041#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003042 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003043#endif
3044#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003045 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3046 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003047#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003048 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3049 {EXPAND_USER, get_users, TRUE, FALSE},
3050 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003051#ifdef FEAT_EVAL
3052 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003053 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003054#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003055 };
3056 int i;
3057 int ret = FAIL;
3058
3059 // Find a context in the table and call the ExpandGeneric() with the
3060 // right function to do the expansion.
3061 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3062 {
3063 if (xp->xp_context == tab[i].context)
3064 {
3065 if (tab[i].ic)
3066 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003067 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3068 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003069 break;
3070 }
3071 }
3072
3073 return ret;
3074}
3075
3076/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003077 * Map wild expand options to flags for expand_wildcards()
3078 */
3079 static int
3080map_wildopts_to_ewflags(int options)
3081{
3082 int flags;
3083
3084 flags = EW_DIR; // include directories
3085 if (options & WILD_LIST_NOTFOUND)
3086 flags |= EW_NOTFOUND;
3087 if (options & WILD_ADD_SLASH)
3088 flags |= EW_ADDSLASH;
3089 if (options & WILD_KEEP_ALL)
3090 flags |= EW_KEEPALL;
3091 if (options & WILD_SILENT)
3092 flags |= EW_SILENT;
3093 if (options & WILD_NOERROR)
3094 flags |= EW_NOERROR;
3095 if (options & WILD_ALLLINKS)
3096 flags |= EW_ALLLINKS;
3097
3098 return flags;
3099}
3100
3101/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 * Do the expansion based on xp->xp_context and "pat".
3103 */
3104 static int
3105ExpandFromContext(
3106 expand_T *xp,
3107 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003108 char_u ***matches,
3109 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003110 int options) // WILD_ flags
3111{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003113 int ret;
3114 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003115 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003116 int fuzzy = cmdline_fuzzy_complete(pat)
3117 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003118
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003119 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120
3121 if (xp->xp_context == EXPAND_FILES
3122 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003123 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003124 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003125 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003126 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3127 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003128
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003129 *matches = (char_u **)"";
3130 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003131 if (xp->xp_context == EXPAND_HELP)
3132 {
3133 // With an empty argument we would get all the help tags, which is
3134 // very slow. Get matches for "help" instead.
3135 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 {
3138#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003139 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003140#endif
3141 return OK;
3142 }
3143 return FAIL;
3144 }
3145
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003147 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003148 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003149 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003151 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003152#ifdef FEAT_DIFF
3153 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 return ExpandBufnames(pat, numMatches, matches,
3155 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003156#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003157 if (xp->xp_context == EXPAND_TAGS
3158 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003159 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3160 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003161 if (xp->xp_context == EXPAND_COLORS)
3162 {
3163 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003164 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003165 directories);
3166 }
3167 if (xp->xp_context == EXPAND_COMPILER)
3168 {
3169 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003170 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003171 }
3172 if (xp->xp_context == EXPAND_OWNSYNTAX)
3173 {
3174 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003175 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003176 }
3177 if (xp->xp_context == EXPAND_FILETYPE)
3178 {
3179 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003180 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003181 }
Doug Kearns81642d92024-01-04 22:37:44 +01003182#ifdef FEAT_KEYMAP
3183 if (xp->xp_context == EXPAND_KEYMAP)
3184 {
3185 char *directories[] = {"keymap", NULL};
3186 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3187 }
3188#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003189#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003190 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003191 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003192#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003193 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003194 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003195 if (xp->xp_context == EXPAND_RUNTIME)
3196 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003197
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003198 // When expanding a function name starting with s:, match the <SNR>nr_
3199 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003200 if ((xp->xp_context == EXPAND_USER_FUNC
3201 || xp->xp_context == EXPAND_DISASSEMBLE)
3202 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003203 {
3204 int len = (int)STRLEN(pat) + 20;
3205
3206 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003207 if (tofree == NULL)
3208 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003209 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003210 pat = tofree;
3211 }
3212
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003213 if (!fuzzy)
3214 {
3215 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3216 if (regmatch.regprog == NULL)
3217 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003218
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003219 // set ignore-case according to p_ic, p_scs and pat
3220 regmatch.rm_ic = ignorecase(pat);
3221 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003222
3223 if (xp->xp_context == EXPAND_SETTINGS
3224 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003225 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003226 else if (xp->xp_context == EXPAND_STRING_SETTING)
3227 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3228 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3229 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003230 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003231 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003232 else if (xp->xp_context == EXPAND_ARGOPT)
3233 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
Yee Cheng China7b81202025-02-23 09:32:47 +01003234 else if (xp->xp_context == EXPAND_HIGHLIGHT_GROUP)
3235 ret = expand_highlight_group(pat, xp, &regmatch, matches, numMatches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003236#if defined(FEAT_TERMINAL)
3237 else if (xp->xp_context == EXPAND_TERMINALOPT)
3238 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3239#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003240#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003242 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003243#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003244 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003245 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003247 if (!fuzzy)
3248 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003249 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003250
3251 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003252}
3253
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{
Yee Cheng China7b81202025-02-23 09:32:47 +01003265 return ExpandGenericExt(
3266 pat, xp, regmatch, matches, numMatches, func, escaped, 0);
3267}
3268
3269/*
3270 * Expand a list of names.
3271 *
3272 * Generic function for command line completion. It calls a function to
3273 * obtain strings, one by one. The strings are matched against a regexp
3274 * program. Matching strings are copied into an array, which is returned.
3275 *
3276 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3277 * is used.
3278 *
3279 * 'sortStartIdx' allows the caller to control sorting behavior. Items before
3280 * the index will not be sorted. Pass 0 to sort all, and -1 to prevent any
3281 * sorting.
3282 *
3283 * Returns OK when no problems encountered, FAIL for error (out of memory).
3284 */
3285 int
3286ExpandGenericExt(
3287 char_u *pat,
3288 expand_T *xp,
3289 regmatch_T *regmatch,
3290 char_u ***matches,
3291 int *numMatches,
3292 char_u *((*func)(expand_T *, int)),
3293 // returns a string from the list
3294 int escaped,
3295 int sortStartIdx)
3296{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003297 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003298 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003300 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003301 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003302 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003303 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003304 int sort_matches = FALSE;
3305 int funcsort = FALSE;
Yee Cheng China7b81202025-02-23 09:32:47 +01003306 int sortStartMatchIdx = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003308 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003309 *matches = NULL;
3310 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003311
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003312 if (!fuzzy)
3313 ga_init2(&ga, sizeof(char *), 30);
3314 else
3315 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3316
3317 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003318 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003319 str = (*func)(xp, i);
3320 if (str == NULL) // end of list
3321 break;
3322 if (*str == NUL) // skip empty strings
3323 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003324
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003325 if (xp->xp_pattern[0] != NUL)
3326 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003327 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003328 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003329 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003330 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003331 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003332 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003333 }
3334 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003335 else
3336 match = TRUE;
3337
3338 if (!match)
3339 continue;
3340
3341 if (escaped)
3342 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3343 else
3344 str = vim_strsave(str);
3345 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003346 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003347 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003348 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003349 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003350 return FAIL;
3351 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003352 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003353 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003354 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003355
3356 if (ga_grow(&ga, 1) == FAIL)
3357 {
3358 vim_free(str);
3359 break;
3360 }
3361
3362 if (fuzzy)
3363 {
3364 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3365 fuzmatch->idx = ga.ga_len;
3366 fuzmatch->str = str;
3367 fuzmatch->score = score;
3368 }
3369 else
3370 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3371
K.Takata161b6ac2022-11-14 15:31:07 +00003372#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003373 if (func == get_menu_names)
3374 {
3375 // test for separator added by get_menu_names()
3376 str += STRLEN(str) - 1;
3377 if (*str == '\001')
3378 *str = '.';
3379 }
K.Takata161b6ac2022-11-14 15:31:07 +00003380#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003381
Yee Cheng China7b81202025-02-23 09:32:47 +01003382 if (sortStartIdx >= 0 && i >= sortStartIdx && sortStartMatchIdx == -1)
3383 {
3384 // Found first item to start sorting from. This is usually 0.
3385 sortStartMatchIdx = ga.ga_len;
3386 }
3387
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003388 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389 }
3390
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003391 if (ga.ga_len == 0)
3392 return OK;
3393
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003394 // sort the matches when using regular expression matching and sorting
3395 // applies to the completion context. Menus and scriptnames should be kept
3396 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003397 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003398 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003399 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003400 && xp->xp_context != EXPAND_SCRIPTNAMES
3401 && xp->xp_context != EXPAND_ARGOPT
3402 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003403 sort_matches = TRUE;
3404
3405 // <SNR> functions should be sorted to the end.
3406 if (xp->xp_context == EXPAND_EXPRESSION
3407 || xp->xp_context == EXPAND_FUNCTIONS
3408 || xp->xp_context == EXPAND_USER_FUNC
3409 || xp->xp_context == EXPAND_DISASSEMBLE)
3410 funcsort = TRUE;
3411
3412 // Sort the matches.
Yee Cheng China7b81202025-02-23 09:32:47 +01003413 if (sort_matches && sortStartMatchIdx != -1)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003414 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003415 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003416 // <SNR> functions should be sorted to the end.
3417 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3418 sort_func_compare);
3419 else
Yee Cheng China7b81202025-02-23 09:32:47 +01003420 sort_strings((char_u **)ga.ga_data + sortStartMatchIdx, ga.ga_len - sortStartMatchIdx);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003421 }
3422
3423 if (!fuzzy)
3424 {
3425 *matches = ga.ga_data;
3426 *numMatches = ga.ga_len;
3427 }
3428 else
3429 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003430 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3431 funcsort) == FAIL)
3432 return FAIL;
3433 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003434 }
3435
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003436#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003437 // Reset the variables used for special highlight names expansion, so that
3438 // they don't show up when getting normal highlight names by ID.
3439 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003440#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003441
Bram Moolenaar66b51422019-08-18 21:44:12 +02003442 return OK;
3443}
3444
3445/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003446 * Expand shell command matches in one directory of $PATH.
3447 */
3448 static void
3449expand_shellcmd_onedir(
3450 char_u *buf,
3451 char_u *s,
3452 size_t l,
3453 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003454 char_u ***matches,
3455 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003456 int flags,
3457 hashtab_T *ht,
3458 garray_T *gap)
3459{
3460 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003461 hash_T hash;
3462 hashitem_T *hi;
3463
3464 vim_strncpy(buf, s, l);
3465 add_pathsep(buf);
3466 l = STRLEN(buf);
3467 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3468
3469 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003470 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003471 if (ret != OK)
3472 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003473
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003474 if (ga_grow(gap, *numMatches) == FAIL)
3475 {
3476 FreeWild(*numMatches, *matches);
3477 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003478 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003479
3480 for (int i = 0; i < *numMatches; ++i)
3481 {
3482 char_u *name = (*matches)[i];
3483
3484 if (STRLEN(name) > l)
3485 {
3486 // Check if this name was already found.
3487 hash = hash_hash(name + l);
3488 hi = hash_lookup(ht, name + l, hash);
3489 if (HASHITEM_EMPTY(hi))
3490 {
3491 // Remove the path that was prepended.
3492 STRMOVE(name, name + l);
3493 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3494 hash_add_item(ht, hi, name, hash);
3495 name = NULL;
3496 }
3497 }
3498 vim_free(name);
3499 }
3500 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003501}
3502
3503/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003504 * Complete a shell command.
3505 * Returns FAIL or OK;
3506 */
3507 static int
3508expand_shellcmd(
3509 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003510 char_u ***matches, // return: array with matches
3511 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003512 int flagsarg) // EW_ flags
3513{
3514 char_u *pat;
3515 int i;
3516 char_u *path = NULL;
3517 int mustfree = FALSE;
3518 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003519 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520 size_t l;
3521 char_u *s, *e;
3522 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003523 int did_curdir = FALSE;
3524 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003525
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003526 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003527 if (buf == NULL)
3528 return FAIL;
3529
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003530 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003531 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003532 if (pat == NULL)
3533 {
3534 vim_free(buf);
3535 return FAIL;
3536 }
3537
Bram Moolenaar66b51422019-08-18 21:44:12 +02003538 for (i = 0; pat[i]; ++i)
3539 if (pat[i] == '\\' && pat[i + 1] == ' ')
3540 STRMOVE(pat + i, pat + i + 1);
3541
3542 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3543
3544 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3545 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3546 path = (char_u *)".";
3547 else
3548 {
3549 // For an absolute name we don't use $PATH.
3550 if (!mch_isFullName(pat))
3551 path = vim_getenv((char_u *)"PATH", &mustfree);
3552 if (path == NULL)
3553 path = (char_u *)"";
3554 }
3555
3556 // Go over all directories in $PATH. Expand matches in that directory and
3557 // collect them in "ga". When "." is not in $PATH also expand for the
3558 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003559 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003560 hash_init(&found_ht);
3561 for (s = path; ; s = e)
3562 {
K.Takata161b6ac2022-11-14 15:31:07 +00003563#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003564 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003565#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003566 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003567#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568 if (e == NULL)
3569 e = s + STRLEN(s);
3570
3571 if (*s == NUL)
3572 {
3573 if (did_curdir)
3574 break;
3575 // Find directories in the current directory, path is empty.
3576 did_curdir = TRUE;
3577 flags |= EW_DIR;
3578 }
3579 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3580 {
3581 did_curdir = TRUE;
3582 flags |= EW_DIR;
3583 }
3584 else
3585 // Do not match directories inside a $PATH item.
3586 flags &= ~EW_DIR;
3587
3588 l = e - s;
3589 if (l > MAXPATHL - 5)
3590 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003591
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003592 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003593 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003594
Bram Moolenaar66b51422019-08-18 21:44:12 +02003595 if (*e != NUL)
3596 ++e;
3597 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003598 *matches = ga.ga_data;
3599 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003600
3601 vim_free(buf);
3602 vim_free(pat);
3603 if (mustfree)
3604 vim_free(path);
3605 hash_clear(&found_ht);
3606 return OK;
3607}
3608
K.Takata161b6ac2022-11-14 15:31:07 +00003609#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003610/*
3611 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003612 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003613 */
3614 static void *
3615call_user_expand_func(
3616 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003617 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003618{
3619 cmdline_info_T *ccline = get_cmdline_info();
3620 int keep = 0;
3621 typval_T args[4];
3622 sctx_T save_current_sctx = current_sctx;
3623 char_u *pat = NULL;
3624 void *ret;
3625
3626 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3627 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628
3629 if (ccline->cmdbuff != NULL)
3630 {
3631 keep = ccline->cmdbuff[ccline->cmdlen];
3632 ccline->cmdbuff[ccline->cmdlen] = 0;
3633 }
3634
3635 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3636
3637 args[0].v_type = VAR_STRING;
3638 args[0].vval.v_string = pat;
3639 args[1].v_type = VAR_STRING;
3640 args[1].vval.v_string = xp->xp_line;
3641 args[2].v_type = VAR_NUMBER;
3642 args[2].vval.v_number = xp->xp_col;
3643 args[3].v_type = VAR_UNKNOWN;
3644
3645 current_sctx = xp->xp_script_ctx;
3646
3647 ret = user_expand_func(xp->xp_arg, 3, args);
3648
3649 current_sctx = save_current_sctx;
3650 if (ccline->cmdbuff != NULL)
3651 ccline->cmdbuff[ccline->cmdlen] = keep;
3652
3653 vim_free(pat);
3654 return ret;
3655}
3656
3657/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003658 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3659 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003660 */
3661 static int
3662ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003663 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003664 expand_T *xp,
3665 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003666 char_u ***matches,
3667 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003668{
3669 char_u *retstr;
3670 char_u *s;
3671 char_u *e;
3672 int keep;
3673 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003674 int fuzzy;
3675 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003676 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003677
3678 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003679 *matches = NULL;
3680 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003681
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003682 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 if (retstr == NULL)
3684 return FAIL;
3685
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003686 if (!fuzzy)
3687 ga_init2(&ga, sizeof(char *), 3);
3688 else
3689 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3690
Bram Moolenaar66b51422019-08-18 21:44:12 +02003691 for (s = retstr; *s != NUL; s = e)
3692 {
3693 e = vim_strchr(s, '\n');
3694 if (e == NULL)
3695 e = s + STRLEN(s);
3696 keep = *e;
3697 *e = NUL;
3698
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003699 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003700 {
3701 if (!fuzzy)
3702 match = vim_regexec(regmatch, s, (colnr_T)0);
3703 else
3704 {
3705 score = fuzzy_match_str(s, pat);
3706 match = (score != 0);
3707 }
3708 }
3709 else
3710 match = TRUE; // match everything
3711
Bram Moolenaar66b51422019-08-18 21:44:12 +02003712 *e = keep;
3713
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003714 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003715 {
3716 if (ga_grow(&ga, 1) == FAIL)
3717 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003718 if (!fuzzy)
3719 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3720 else
3721 {
3722 fuzmatch_str_T *fuzmatch =
3723 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003724 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003725 fuzmatch->str = vim_strnsave(s, e - s);
3726 fuzmatch->score = score;
3727 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003728 ++ga.ga_len;
3729 }
3730
3731 if (*e != NUL)
3732 ++e;
3733 }
3734 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003735
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003736 if (ga.ga_len == 0)
3737 return OK;
3738
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003739 if (!fuzzy)
3740 {
3741 *matches = ga.ga_data;
3742 *numMatches = ga.ga_len;
3743 }
3744 else
3745 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003746 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3747 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003748 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003749 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003750 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751 return OK;
3752}
3753
3754/*
3755 * Expand names with a list returned by a function defined by the user.
3756 */
3757 static int
3758ExpandUserList(
3759 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003760 char_u ***matches,
3761 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003762{
3763 list_T *retlist;
3764 listitem_T *li;
3765 garray_T ga;
3766
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003767 *matches = NULL;
3768 *numMatches = 0;
3769 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003770 if (retlist == NULL)
3771 return FAIL;
3772
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003773 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003774 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003775 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003776 {
3777 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3778 continue; // Skip non-string items and empty strings
3779
3780 if (ga_grow(&ga, 1) == FAIL)
3781 break;
3782
3783 ((char_u **)ga.ga_data)[ga.ga_len] =
3784 vim_strsave(li->li_tv.vval.v_string);
3785 ++ga.ga_len;
3786 }
3787 list_unref(retlist);
3788
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003789 *matches = ga.ga_data;
3790 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003791 return OK;
3792}
K.Takata161b6ac2022-11-14 15:31:07 +00003793#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003794
3795/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003796 * Expand "file" for all comma-separated directories in "path".
3797 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003798 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003799 */
3800 void
3801globpath(
3802 char_u *path,
3803 char_u *file,
3804 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003805 int expand_options,
3806 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807{
3808 expand_T xpc;
3809 char_u *buf;
3810 int i;
3811 int num_p;
3812 char_u **p;
3813
3814 buf = alloc(MAXPATHL);
3815 if (buf == NULL)
3816 return;
3817
3818 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003819 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003820
3821 // Loop over all entries in {path}.
3822 while (*path != NUL)
3823 {
3824 // Copy one item of the path to buf[] and concatenate the file name.
3825 copy_option_part(&path, buf, MAXPATHL, ",");
3826 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3827 {
K.Takata161b6ac2022-11-14 15:31:07 +00003828#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003829 // Using the platform's path separator (\) makes vim incorrectly
3830 // treat it as an escape character, use '/' instead.
3831 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3832 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003833#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003834 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003835#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003836 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003837 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003838 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3839 {
3840 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3841
3842 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003843 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003844 for (i = 0; i < num_p; ++i)
3845 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003846 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003847 ++ga->ga_len;
3848 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003849 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003850 }
3851 }
3852 }
3853
3854 vim_free(buf);
3855}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003856
Bram Moolenaareadee482020-09-04 15:37:31 +02003857/*
3858 * Translate some keys pressed when 'wildmenu' is used.
3859 */
3860 int
3861wildmenu_translate_key(
3862 cmdline_info_T *cclp,
3863 int key,
3864 expand_T *xp,
3865 int did_wild_list)
3866{
3867 int c = key;
3868
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003869 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003870 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003871 // When the popup menu is used for cmdline completion:
3872 // Up : go to the previous item in the menu
3873 // Down : go to the next item in the menu
3874 // Left : go to the parent directory
3875 // Right: list the files in the selected directory
3876 switch (c)
3877 {
3878 case K_UP: c = K_LEFT; break;
3879 case K_DOWN: c = K_RIGHT; break;
3880 case K_LEFT: c = K_UP; break;
3881 case K_RIGHT: c = K_DOWN; break;
3882 default: break;
3883 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003884 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003885
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003886 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003887 {
3888 if (c == K_LEFT)
3889 c = Ctrl_P;
3890 else if (c == K_RIGHT)
3891 c = Ctrl_N;
3892 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003893
Bram Moolenaareadee482020-09-04 15:37:31 +02003894 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003895 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003896 && cclp->cmdpos > 1
3897 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3898 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3899 && (c == '\n' || c == '\r' || c == K_KENTER))
3900 c = K_DOWN;
3901
3902 return c;
3903}
3904
3905/*
3906 * Delete characters on the command line, from "from" to the current
3907 * position.
3908 */
3909 static void
3910cmdline_del(cmdline_info_T *cclp, int from)
3911{
3912 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3913 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3914 cclp->cmdlen -= cclp->cmdpos - from;
3915 cclp->cmdpos = from;
3916}
3917
3918/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003919 * Handle a key pressed when the wild menu for the menu names
3920 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003921 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003922 static int
3923wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003924{
Bram Moolenaareadee482020-09-04 15:37:31 +02003925 int i;
3926 int j;
3927
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003928 // Hitting <Down> after "emenu Name.": complete submenu
3929 if (key == K_DOWN && cclp->cmdpos > 0
3930 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003931 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003932 key = p_wc;
3933 KeyTyped = TRUE; // in case the key was mapped
3934 }
3935 else if (key == K_UP)
3936 {
3937 // Hitting <Up>: Remove one submenu name in front of the
3938 // cursor
3939 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003940
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003941 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3942 i = 0;
3943 while (--j > 0)
3944 {
3945 // check for start of menu name
3946 if (cclp->cmdbuff[j] == ' '
3947 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003948 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003949 i = j + 1;
3950 break;
3951 }
3952 // check for start of submenu name
3953 if (cclp->cmdbuff[j] == '.'
3954 && cclp->cmdbuff[j - 1] != '\\')
3955 {
3956 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003957 {
3958 i = j + 1;
3959 break;
3960 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003961 else
3962 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003963 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003964 }
3965 if (i > 0)
3966 cmdline_del(cclp, i);
3967 key = p_wc;
3968 KeyTyped = TRUE; // in case the key was mapped
3969 xp->xp_context = EXPAND_NOTHING;
3970 }
3971
3972 return key;
3973}
3974
3975/*
3976 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3977 * directory names (EXPAND_DIRECTORIES) or shell command names
3978 * (EXPAND_SHELLCMD) is displayed.
3979 */
3980 static int
3981wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3982{
3983 int i;
3984 int j;
3985 char_u upseg[5];
3986
3987 upseg[0] = PATHSEP;
3988 upseg[1] = '.';
3989 upseg[2] = '.';
3990 upseg[3] = PATHSEP;
3991 upseg[4] = NUL;
3992
3993 if (key == K_DOWN
3994 && cclp->cmdpos > 0
3995 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3996 && (cclp->cmdpos < 3
3997 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3998 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3999 {
4000 // go down a directory
4001 key = p_wc;
4002 KeyTyped = TRUE; // in case the key was mapped
4003 }
4004 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
4005 {
4006 // If in a direct ancestor, strip off one ../ to go down
4007 int found = FALSE;
4008
4009 j = cclp->cmdpos;
4010 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4011 while (--j > i)
4012 {
4013 if (has_mbyte)
4014 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4015 if (vim_ispathsep(cclp->cmdbuff[j]))
4016 {
4017 found = TRUE;
4018 break;
4019 }
4020 }
4021 if (found
4022 && cclp->cmdbuff[j - 1] == '.'
4023 && cclp->cmdbuff[j - 2] == '.'
4024 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
4025 {
4026 cmdline_del(cclp, j - 2);
4027 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01004028 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02004029 }
4030 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004031 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02004032 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004033 // go up a directory
4034 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004035
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004036 j = cclp->cmdpos - 1;
4037 i = (int)(xp->xp_pattern - cclp->cmdbuff);
4038 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02004039 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004040 if (has_mbyte)
4041 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4042 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004043#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004044 && vim_strchr((char_u *)" *?[{`$%#",
4045 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004046#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004047 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004048 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004049 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004050 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004051 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004052 break;
4053 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004054 else
4055 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004056 }
4057 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004058
4059 if (!found)
4060 j = i;
4061 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4062 j += 4;
4063 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4064 && j == i)
4065 j += 3;
4066 else
4067 j = 0;
4068 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004069 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004070 // TODO this is only for DOS/UNIX systems - need to put in
4071 // machine-specific stuff here and in upseg init
4072 cmdline_del(cclp, j);
4073 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004074 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004075 else if (cclp->cmdpos > i)
4076 cmdline_del(cclp, i);
4077
4078 // Now complete in the new directory. Set KeyTyped in case the
4079 // Up key came from a mapping.
4080 key = p_wc;
4081 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004082 }
4083
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004084 return key;
4085}
4086
4087/*
4088 * Handle a key pressed when the wild menu is displayed
4089 */
4090 int
4091wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4092{
4093 if (xp->xp_context == EXPAND_MENUNAMES)
4094 return wildmenu_process_key_menunames(cclp, key, xp);
4095 else if ((xp->xp_context == EXPAND_FILES
4096 || xp->xp_context == EXPAND_DIRECTORIES
4097 || xp->xp_context == EXPAND_SHELLCMD))
4098 return wildmenu_process_key_filenames(cclp, key, xp);
4099
4100 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004101}
4102
4103/*
4104 * Free expanded names when finished walking through the matches
4105 */
4106 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004107wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004108{
4109 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004110
4111 if (!p_wmnu || wild_menu_showing == 0)
4112 return;
4113
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004114#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004115 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004116 if (cclp->input_fn)
4117 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004118#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004119
4120 if (wild_menu_showing == WM_SCROLLED)
4121 {
4122 // Entered command line, move it up
4123 cmdline_row--;
4124 redrawcmd();
4125 }
4126 else if (save_p_ls != -1)
4127 {
4128 // restore 'laststatus' and 'winminheight'
4129 p_ls = save_p_ls;
4130 p_wmh = save_p_wmh;
4131 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004132 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004133 redrawcmd();
4134 save_p_ls = -1;
4135 }
4136 else
4137 {
4138 win_redraw_last_status(topframe);
4139 redraw_statuslines();
4140 }
4141 KeyTyped = skt;
4142 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004143#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004144 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004145 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004146#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004147}
Bram Moolenaareadee482020-09-04 15:37:31 +02004148
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004149#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004150/*
4151 * "getcompletion()" function
4152 */
4153 void
4154f_getcompletion(typval_T *argvars, typval_T *rettv)
4155{
4156 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004157 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004158 expand_T xpc;
4159 int filtered = FALSE;
4160 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004161 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004162
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004163 if (in_vim9script()
4164 && (check_for_string_arg(argvars, 0) == FAIL
4165 || check_for_string_arg(argvars, 1) == FAIL
4166 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4167 return;
4168
ii144785fe02021-11-21 12:13:56 +00004169 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004170 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004171 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004172 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004173
4174 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004175 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004176
4177 if (p_wic)
4178 options |= WILD_ICASE;
4179
4180 // For filtered results, 'wildignore' is used
4181 if (!filtered)
4182 options |= WILD_KEEP_ALL;
4183
4184 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004185 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004186 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004187 int cmdline_len = (int)STRLEN(pat);
4188 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004189 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004190 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004191 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004192 else
4193 {
ii144785fe02021-11-21 12:13:56 +00004194 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004195 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004196 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004197
4198 xpc.xp_context = cmdcomplete_str_to_type(type);
4199 if (xpc.xp_context == EXPAND_NOTHING)
4200 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004201 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004202 return;
4203 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004204
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004205 if (xpc.xp_context == EXPAND_USER_DEFINED)
4206 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004207 // Must be "custom,funcname" pattern
4208 if (STRNCMP(type, "custom,", 7) != 0)
4209 {
4210 semsg(_(e_invalid_argument_str), type);
4211 return;
4212 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004213
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004214 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004215 }
4216
4217 if (xpc.xp_context == EXPAND_USER_LIST)
4218 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004219 // Must be "customlist,funcname" pattern
4220 if (STRNCMP(type, "customlist,", 11) != 0)
4221 {
4222 semsg(_(e_invalid_argument_str), type);
4223 return;
4224 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004225
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004226 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004227 }
4228
Bram Moolenaar66b51422019-08-18 21:44:12 +02004229# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004230 if (xpc.xp_context == EXPAND_MENUS)
4231 {
4232 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4233 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4234 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004235# endif
4236# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004237 if (xpc.xp_context == EXPAND_CSCOPE)
4238 {
4239 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4240 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4241 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004242# endif
4243# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004244 if (xpc.xp_context == EXPAND_SIGN)
4245 {
4246 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4247 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4248 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004249# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004250 if (xpc.xp_context == EXPAND_RUNTIME)
4251 {
4252 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4253 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4254 }
zeertzjq85f36d62024-10-10 19:14:13 +02004255 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4256 {
4257 int context = EXPAND_SHELLCMDLINE;
4258 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4259 &context);
4260 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4261 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004262 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004263
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004264 if (cmdline_fuzzy_completion_supported(&xpc))
4265 // when fuzzy matching, don't modify the search string
4266 pat = vim_strsave(xpc.xp_pattern);
4267 else
4268 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4269
Bram Moolenaar93a10962022-06-16 11:42:09 +01004270 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004271 {
4272 int i;
4273
4274 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4275
4276 for (i = 0; i < xpc.xp_numfiles; i++)
4277 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4278 }
4279 vim_free(pat);
4280 ExpandCleanup(&xpc);
4281}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004282#endif // FEAT_EVAL