blob: bd9a410ac3612ba5cbcece5fb2417cb354b5e323 [file] [log] [blame]
Bram Moolenaar66b51422019-08-18 21:44:12 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdexpand.c: functions for command-line completion
12 */
13
14#include "vim.h"
15
16static int cmd_showtail; // Only show path tail in lists ?
17
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000018static int ExpandFromContext(expand_T *xp, char_u *, char_u ***, int *, int);
Bram Moolenaard6e91382022-11-12 17:44:13 +000019static char_u *showmatches_gettail(char_u *s);
Bram Moolenaar66b51422019-08-18 21:44:12 +020020static int expand_showtail(expand_T *xp);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000021static int expand_shellcmd(char_u *filepat, char_u ***matches, int *numMatches, int flagsarg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +020022#if defined(FEAT_EVAL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000023static int ExpandUserDefined(char_u *pat, expand_T *xp, regmatch_T *regmatch, char_u ***matches, int *numMatches);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000024static int ExpandUserList(expand_T *xp, char_u ***matches, int *numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +020025#endif
26
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000027// "compl_match_array" points the currently displayed list of entries in the
28// popup menu. It is NULL when there is no popup menu.
29static pumitem_T *compl_match_array = NULL;
30static int compl_match_arraysize;
31// First column in cmdline of the matched item for completion.
32static int compl_startcol;
33static int compl_selected;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +000034
zeertzjqc51a3762022-12-10 10:22:29 +000035#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m]) : matches[m])
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +000036
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000037/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000038 * Returns TRUE if fuzzy completion is supported for a given cmdline completion
39 * context.
40 */
41 static int
42cmdline_fuzzy_completion_supported(expand_T *xp)
43{
44 return (vim_strchr(p_wop, WOP_FUZZY) != NULL
45 && xp->xp_context != EXPAND_BOOL_SETTINGS
46 && xp->xp_context != EXPAND_COLORS
47 && xp->xp_context != EXPAND_COMPILER
48 && xp->xp_context != EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +020049 && xp->xp_context != EXPAND_DIRS_IN_CDPATH
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000050 && xp->xp_context != EXPAND_FILES
51 && xp->xp_context != EXPAND_FILES_IN_PATH
52 && xp->xp_context != EXPAND_FILETYPE
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +010053 && xp->xp_context != EXPAND_FINDFUNC
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000054 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010055 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000056 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020057 && xp->xp_context != EXPAND_STRING_SETTING
58 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000059 && xp->xp_context != EXPAND_OWNSYNTAX
60 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000061 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000062 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020063 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000064 && xp->xp_context != EXPAND_TAGS
65 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000066 && xp->xp_context != EXPAND_USER_LIST);
67}
68
69/*
70 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000071 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
72 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000073 */
74 int
75cmdline_fuzzy_complete(char_u *fuzzystr)
76{
77 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
78}
79
80/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000081 * sort function for the completion matches.
82 * <SNR> functions should be sorted to the end.
83 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020084 static int
85sort_func_compare(const void *s1, const void *s2)
86{
87 char_u *p1 = *(char_u **)s1;
88 char_u *p2 = *(char_u **)s2;
89
90 if (*p1 != '<' && *p2 == '<') return -1;
91 if (*p1 == '<' && *p2 != '<') return 1;
92 return STRCMP(p1, p2);
93}
Bram Moolenaar66b51422019-08-18 21:44:12 +020094
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000095/*
96 * Escape special characters in the cmdline completion matches.
97 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020098 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000099wildescape(
100 expand_T *xp,
101 char_u *str,
102 int numfiles,
103 char_u **files)
104{
105 char_u *p;
106 int vse_what = xp->xp_context == EXPAND_BUFFERS
107 ? VSE_BUFFER : VSE_NONE;
108
109 if (xp->xp_context == EXPAND_FILES
110 || xp->xp_context == EXPAND_FILES_IN_PATH
111 || xp->xp_context == EXPAND_SHELLCMD
112 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200113 || xp->xp_context == EXPAND_DIRECTORIES
114 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000115 {
116 // Insert a backslash into a file name before a space, \, %, #
117 // and wildmatch characters, except '~'.
118 for (int i = 0; i < numfiles; ++i)
119 {
120 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200121 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000122 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200123 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
124 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000125 if (p != NULL)
126 {
127 vim_free(files[i]);
128 files[i] = p;
129#if defined(BACKSLASH_IN_FILENAME)
130 p = vim_strsave_escaped(files[i], (char_u *)" ");
131 if (p != NULL)
132 {
133 vim_free(files[i]);
134 files[i] = p;
135 }
136#endif
137 }
138 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200139 else if (xp->xp_backslash & XP_BS_COMMA)
140 {
141 if (vim_strchr(files[i], ',') != NULL)
142 {
143 p = vim_strsave_escaped(files[i], (char_u *)",");
144 if (p != NULL)
145 {
146 vim_free(files[i]);
147 files[i] = p;
148 }
149 }
150 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000151#ifdef BACKSLASH_IN_FILENAME
152 p = vim_strsave_fnameescape(files[i], vse_what);
153#else
154 p = vim_strsave_fnameescape(files[i],
155 xp->xp_shell ? VSE_SHELL : vse_what);
156#endif
157 if (p != NULL)
158 {
159 vim_free(files[i]);
160 files[i] = p;
161 }
162
163 // If 'str' starts with "\~", replace "~" at start of
164 // files[i] with "\~".
165 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
166 escape_fname(&files[i]);
167 }
168 xp->xp_backslash = XP_BS_NONE;
169
170 // If the first file starts with a '+' escape it. Otherwise it
171 // could be seen as "+cmd".
172 if (*files[0] == '+')
173 escape_fname(&files[0]);
174 }
175 else if (xp->xp_context == EXPAND_TAGS)
176 {
177 // Insert a backslash before characters in a tag name that
178 // would terminate the ":tag" command.
179 for (int i = 0; i < numfiles; ++i)
180 {
181 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
182 if (p != NULL)
183 {
184 vim_free(files[i]);
185 files[i] = p;
186 }
187 }
188 }
189}
190
191/*
192 * Escape special characters in the cmdline completion matches.
193 */
194 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200195ExpandEscape(
196 expand_T *xp,
197 char_u *str,
198 int numfiles,
199 char_u **files,
200 int options)
201{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200202 // May change home directory back to "~"
203 if (options & WILD_HOME_REPLACE)
204 tilde_replace(str, numfiles, files);
205
206 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000207 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200208}
209
210/*
211 * Return FAIL if this is not an appropriate context in which to do
212 * completion of anything, return OK if it is (even if there are no matches).
213 * For the caller, this means that the character is just passed through like a
214 * normal character (instead of being expanded). This allows :s/^I^D etc.
215 */
216 int
217nextwild(
218 expand_T *xp,
219 int type,
220 int options, // extra options for ExpandOne()
221 int escape) // if TRUE, escape the returned matches
222{
223 cmdline_info_T *ccline = get_cmdline_info();
224 int i, j;
225 char_u *p1;
226 char_u *p2;
227 int difflen;
228 int v;
229
230 if (xp->xp_numfiles == -1)
231 {
232 set_expand_context(xp);
233 cmd_showtail = expand_showtail(xp);
234 }
235
236 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
237 {
238 beep_flush();
239 return OK; // Something illegal on command line
240 }
241 if (xp->xp_context == EXPAND_NOTHING)
242 {
243 // Caller can use the character as a normal char instead
244 return FAIL;
245 }
246
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000247 // If cmd_silent is set then don't show the dots, because redrawcmd() below
248 // won't remove them.
249 if (!cmd_silent)
250 {
251 msg_puts("..."); // show that we are busy
252 out_flush();
253 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200254
255 i = (int)(xp->xp_pattern - ccline->cmdbuff);
256 xp->xp_pattern_len = ccline->cmdpos - i;
257
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000258 if (type == WILD_NEXT || type == WILD_PREV
259 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200260 {
261 // Get next/previous match for a previous expanded pattern.
262 p2 = ExpandOne(xp, NULL, NULL, 0, type);
263 }
264 else
265 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000266 if (cmdline_fuzzy_completion_supported(xp))
267 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200268 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000269 else
270 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
271
Bram Moolenaar66b51422019-08-18 21:44:12 +0200272 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000273 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200274 p2 = NULL;
275 else
276 {
277 int use_options = options |
278 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
279 if (escape)
280 use_options |= WILD_ESCAPE;
281
282 if (p_wic)
283 use_options += WILD_ICASE;
284 p2 = ExpandOne(xp, p1,
285 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
286 use_options, type);
287 vim_free(p1);
288 // longest match: make sure it is not shorter, happens with :help
289 if (p2 != NULL && type == WILD_LONGEST)
290 {
291 for (j = 0; j < xp->xp_pattern_len; ++j)
292 if (ccline->cmdbuff[i + j] == '*'
293 || ccline->cmdbuff[i + j] == '?')
294 break;
295 if ((int)STRLEN(p2) < j)
296 VIM_CLEAR(p2);
297 }
298 }
299 }
300
301 if (p2 != NULL && !got_int)
302 {
303 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
304 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
305 {
306 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
307 xp->xp_pattern = ccline->cmdbuff + i;
308 }
309 else
310 v = OK;
311 if (v == OK)
312 {
313 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
314 &ccline->cmdbuff[ccline->cmdpos],
315 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
316 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
317 ccline->cmdlen += difflen;
318 ccline->cmdpos += difflen;
319 }
320 }
321 vim_free(p2);
322
323 redrawcmd();
324 cursorcmd();
325
326 // When expanding a ":map" command and no matches are found, assume that
327 // the key is supposed to be inserted literally
328 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
329 return FAIL;
330
331 if (xp->xp_numfiles <= 0 && p2 == NULL)
332 beep_flush();
333 else if (xp->xp_numfiles == 1)
334 // free expanded pattern
335 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
336
337 return OK;
338}
339
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000340/*
341 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000342 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000343 */
344 static int
345cmdline_pum_create(
346 cmdline_info_T *ccline,
347 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000348 char_u **matches,
349 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000350 int showtail)
351{
352 int i;
353 int columns;
354
355 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000356 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000357 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000358 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000359 {
zeertzjqc51a3762022-12-10 10:22:29 +0000360 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000361 compl_match_array[i].pum_info = NULL;
362 compl_match_array[i].pum_extra = NULL;
363 compl_match_array[i].pum_kind = NULL;
glepnir0fe17f82024-10-08 22:26:44 +0200364 compl_match_array[i].pum_user_abbr_hlattr = -1;
365 compl_match_array[i].pum_user_kind_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000366 }
367
368 // Compute the popup menu starting column
369 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
370 columns = vim_strsize(xp->xp_pattern);
371 if (showtail)
372 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000373 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000374 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000375 }
glepnir977561a2025-02-13 20:48:56 +0100376 compl_startcol = MAX(0, compl_startcol - columns);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000377
378 // no default selection
379 compl_selected = -1;
380
381 cmdline_pum_display();
382
383 return EXPAND_OK;
384}
385
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000386/*
387 * Display the cmdline completion matches in a popup menu
388 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000389 void
390cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000391{
392 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
393}
394
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000395/*
396 * Returns TRUE if the cmdline completion popup menu is being displayed.
397 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000398 int
399cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000400{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100401 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000402}
403
404/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000405 * Remove the cmdline completion popup menu (if present), free the list of
406 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000407 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000408 void
409cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000410{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000411 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100412 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000413
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000414 pum_undisplay();
415 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000416 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000417 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000418 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000419 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100420
421 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
422 // as a side effect.
423 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000424}
425
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000426 void
427cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000428{
429 cmdline_pum_remove();
430 wildmenu_cleanup(cclp);
431}
432
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000433/*
434 * Returns the starting column number to use for the cmdline completion popup
435 * menu.
436 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000437 int
438cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000439{
440 return compl_startcol;
441}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000442
Bram Moolenaar66b51422019-08-18 21:44:12 +0200443/*
zeertzjqd8c93402024-06-17 18:25:32 +0200444 * Returns the current cmdline completion pattern.
445 */
446 char_u *
447cmdline_compl_pattern(void)
448{
449 expand_T *xp = get_cmdline_info()->xpc;
450
451 return xp == NULL ? NULL : xp->xp_orig;
452}
453
454/*
455 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
456 */
457 int
458cmdline_compl_is_fuzzy(void)
459{
460 expand_T *xp = get_cmdline_info()->xpc;
461
462 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
463}
464
465/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000466 * Return the number of characters that should be skipped in a status match.
467 * These are backslashes used for escaping. Do show backslashes in help tags.
468 */
469 static int
470skip_status_match_char(expand_T *xp, char_u *s)
471{
472 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
473#ifdef FEAT_MENU
474 || ((xp->xp_context == EXPAND_MENUS
475 || xp->xp_context == EXPAND_MENUNAMES)
476 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
477#endif
478 )
479 {
480#ifndef BACKSLASH_IN_FILENAME
481 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
482 return 2;
483#endif
484 return 1;
485 }
486 return 0;
487}
488
489/*
490 * Get the length of an item as it will be shown in the status line.
491 */
492 static int
493status_match_len(expand_T *xp, char_u *s)
494{
495 int len = 0;
496
497#ifdef FEAT_MENU
498 int emenu = xp->xp_context == EXPAND_MENUS
499 || xp->xp_context == EXPAND_MENUNAMES;
500
501 // Check for menu separators - replace with '|'.
502 if (emenu && menu_is_separator(s))
503 return 1;
504#endif
505
506 while (*s != NUL)
507 {
508 s += skip_status_match_char(xp, s);
509 len += ptr2cells(s);
510 MB_PTR_ADV(s);
511 }
512
513 return len;
514}
515
516/*
517 * Show wildchar matches in the status line.
518 * Show at least the "match" item.
519 * We start at item 'first_match' in the list and show all matches that fit.
520 *
521 * If inversion is possible we use it. Else '=' characters are used.
522 */
523 static void
524win_redr_status_matches(
525 expand_T *xp,
526 int num_matches,
527 char_u **matches, // list of matches
528 int match,
529 int showtail)
530{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000531 int row;
532 char_u *buf;
533 int len;
534 int clen; // length in screen cells
535 int fillchar;
536 int attr;
537 int i;
538 int highlight = TRUE;
539 char_u *selstart = NULL;
540 int selstart_col = 0;
541 char_u *selend = NULL;
542 static int first_match = 0;
543 int add_left = FALSE;
544 char_u *s;
545#ifdef FEAT_MENU
546 int emenu;
547#endif
548 int l;
549
550 if (matches == NULL) // interrupted completion?
551 return;
552
553 if (has_mbyte)
554 buf = alloc(Columns * MB_MAXBYTES + 1);
555 else
556 buf = alloc(Columns + 1);
557 if (buf == NULL)
558 return;
559
560 if (match == -1) // don't show match but original text
561 {
562 match = 0;
563 highlight = FALSE;
564 }
565 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000566 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000567 if (match == 0)
568 first_match = 0;
569 else if (match < first_match)
570 {
571 // jumping left, as far as we can go
572 first_match = match;
573 add_left = TRUE;
574 }
575 else
576 {
577 // check if match fits on the screen
578 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000579 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000580 if (first_match > 0)
581 clen += 2;
582 // jumping right, put match at the left
583 if ((long)clen > Columns)
584 {
585 first_match = match;
586 // if showing the last match, we can add some on the left
587 clen = 2;
588 for (i = match; i < num_matches; ++i)
589 {
zeertzjqc51a3762022-12-10 10:22:29 +0000590 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000591 if ((long)clen >= Columns)
592 break;
593 }
594 if (i == num_matches)
595 add_left = TRUE;
596 }
597 }
598 if (add_left)
599 while (first_match > 0)
600 {
zeertzjqc51a3762022-12-10 10:22:29 +0000601 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000602 if ((long)clen >= Columns)
603 break;
604 --first_match;
605 }
606
607 fillchar = fillchar_status(&attr, curwin);
608
609 if (first_match == 0)
610 {
611 *buf = NUL;
612 len = 0;
613 }
614 else
615 {
616 STRCPY(buf, "< ");
617 len = 2;
618 }
619 clen = len;
620
621 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000622 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000623 {
624 if (i == match)
625 {
626 selstart = buf + len;
627 selstart_col = clen;
628 }
629
zeertzjqc51a3762022-12-10 10:22:29 +0000630 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000631 // Check for menu separators - replace with '|'
632#ifdef FEAT_MENU
633 emenu = (xp->xp_context == EXPAND_MENUS
634 || xp->xp_context == EXPAND_MENUNAMES);
635 if (emenu && menu_is_separator(s))
636 {
637 STRCPY(buf + len, transchar('|'));
638 l = (int)STRLEN(buf + len);
639 len += l;
640 clen += l;
641 }
642 else
643#endif
644 for ( ; *s != NUL; ++s)
645 {
646 s += skip_status_match_char(xp, s);
647 clen += ptr2cells(s);
648 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
649 {
650 STRNCPY(buf + len, s, l);
651 s += l - 1;
652 len += l;
653 }
654 else
655 {
656 STRCPY(buf + len, transchar_byte(*s));
657 len += (int)STRLEN(buf + len);
658 }
659 }
660 if (i == match)
661 selend = buf + len;
662
663 *(buf + len++) = ' ';
664 *(buf + len++) = ' ';
665 clen += 2;
666 if (++i == num_matches)
667 break;
668 }
669
670 if (i != num_matches)
671 {
672 *(buf + len++) = '>';
673 ++clen;
674 }
675
676 buf[len] = NUL;
677
678 row = cmdline_row - 1;
679 if (row >= 0)
680 {
681 if (wild_menu_showing == 0)
682 {
683 if (msg_scrolled > 0)
684 {
685 // Put the wildmenu just above the command line. If there is
686 // no room, scroll the screen one line up.
687 if (cmdline_row == Rows - 1)
688 {
689 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
690 ++msg_scrolled;
691 }
692 else
693 {
694 ++cmdline_row;
695 ++row;
696 }
697 wild_menu_showing = WM_SCROLLED;
698 }
699 else
700 {
701 // Create status line if needed by setting 'laststatus' to 2.
702 // Set 'winminheight' to zero to avoid that the window is
703 // resized.
704 if (lastwin->w_status_height == 0)
705 {
706 save_p_ls = p_ls;
707 save_p_wmh = p_wmh;
708 p_ls = 2;
709 p_wmh = 0;
710 last_status(FALSE);
711 }
712 wild_menu_showing = WM_SHOWN;
713 }
714 }
715
716 screen_puts(buf, row, 0, attr);
717 if (selstart != NULL && highlight)
718 {
719 *selend = NUL;
720 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
721 }
722
723 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
724 }
725
726 win_redraw_last_status(topframe);
727 vim_free(buf);
728}
729
730/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000731 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200732 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000733 */
734 static char_u *
glepnir977561a2025-02-13 20:48:56 +0100735get_next_or_prev_match(int mode, expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000736{
glepnir977561a2025-02-13 20:48:56 +0100737 int findex = xp->xp_selected;
738 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000739
glepnir977561a2025-02-13 20:48:56 +0100740 // When no files found, return NULL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000741 if (xp->xp_numfiles <= 0)
742 return NULL;
743
744 if (mode == WILD_PREV)
745 {
glepnir977561a2025-02-13 20:48:56 +0100746 // Select last file if at start
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000747 if (findex == -1)
748 findex = xp->xp_numfiles;
749 --findex;
750 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000751 else if (mode == WILD_NEXT)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000752 {
glepnir977561a2025-02-13 20:48:56 +0100753 // Select next file
754 findex = findex + 1;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000755 }
glepnir977561a2025-02-13 20:48:56 +0100756 else // WILD_PAGEDOWN or WILD_PAGEUP
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000757 {
glepnir977561a2025-02-13 20:48:56 +0100758 // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
759 ht = pum_get_height();
760 if (ht > 3)
761 ht -= 2;
762
763 if (mode == WILD_PAGEUP)
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000764 {
glepnir977561a2025-02-13 20:48:56 +0100765 if (findex == 0)
766 // at the first entry, don't select any entries
767 findex = -1;
768 else if (findex == -1)
769 // no entry is selected. select the last entry
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000770 findex = xp->xp_numfiles - 1;
glepnir977561a2025-02-13 20:48:56 +0100771 else
772 // go up by the pum height
773 findex = MAX(findex - ht, 0);
774 }
775 else // mode == WILD_PAGEDOWN
776 {
777 if (findex < 0)
778 // no entry is selected, select the first entry
779 findex = 0;
780 else if (findex >= xp->xp_numfiles - 1)
781 // at the last entry, don't select any entries
782 findex = -1;
783 else
784 // go down by the pum height
785 findex = MIN(findex + ht, xp->xp_numfiles - 1);
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000786 }
787 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000788
glepnir977561a2025-02-13 20:48:56 +0100789 // Handle wrapping around
790 if (findex < 0 || findex >= xp->xp_numfiles)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000791 {
glepnir977561a2025-02-13 20:48:56 +0100792 // If original string exists, return to it when wrapping around
793 if (xp->xp_orig != NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000794 findex = -1;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000795 else
glepnir977561a2025-02-13 20:48:56 +0100796 // Wrap around to opposite end
797 findex = (findex < 0) ? xp->xp_numfiles - 1 : 0;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000798 }
glepnir977561a2025-02-13 20:48:56 +0100799
800 // Display matches on screen
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000801 if (compl_match_array)
802 {
803 compl_selected = findex;
804 cmdline_pum_display();
805 }
806 else if (p_wmnu)
glepnir977561a2025-02-13 20:48:56 +0100807 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex,
808 cmd_showtail);
809
zeertzjqe9ef3472023-08-17 23:57:05 +0200810 xp->xp_selected = findex;
glepnir977561a2025-02-13 20:48:56 +0100811 // Return the original string or the selected match
812 return vim_strsave(findex == -1 ? xp->xp_orig : xp->xp_files[findex]);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000813}
814
815/*
816 * Start the command-line expansion and get the matches.
817 */
818 static char_u *
819ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
820{
821 int non_suf_match; // number without matching suffix
822 int i;
823 char_u *ss = NULL;
824
825 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000826 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000827 options) == FAIL)
828 {
829#ifdef FNAME_ILLEGAL
830 // Illegal file name has been silently skipped. But when there
831 // are wildcards, the real problem is that there was no match,
832 // causing the pattern to be added, which has illegal characters.
833 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
834 semsg(_(e_no_match_str_2), str);
835#endif
836 }
837 else if (xp->xp_numfiles == 0)
838 {
839 if (!(options & WILD_SILENT))
840 semsg(_(e_no_match_str_2), str);
841 }
842 else
843 {
844 // Escape the matches for use on the command line.
845 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
846
847 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000848 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000849 {
850 if (xp->xp_numfiles)
851 non_suf_match = xp->xp_numfiles;
852 else
853 non_suf_match = 1;
854 if ((xp->xp_context == EXPAND_FILES
855 || xp->xp_context == EXPAND_DIRECTORIES)
856 && xp->xp_numfiles > 1)
857 {
858 // More than one match; check suffix.
859 // The files will have been sorted on matching suffix in
860 // expand_wildcards, only need to check the first two.
861 non_suf_match = 0;
862 for (i = 0; i < 2; ++i)
863 if (match_suffix(xp->xp_files[i]))
864 ++non_suf_match;
865 }
866 if (non_suf_match != 1)
867 {
868 // Can we ever get here unless it's while expanding
869 // interactively? If not, we can get rid of this all
870 // together. Don't really want to wait for this message
871 // (and possibly have to hit return to continue!).
872 if (!(options & WILD_SILENT))
873 emsg(_(e_too_many_file_names));
874 else if (!(options & WILD_NO_BEEP))
875 beep_flush();
876 }
877 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
878 ss = vim_strsave(xp->xp_files[0]);
879 }
880 }
881
882 return ss;
883}
884
885/*
886 * Return the longest common part in the list of cmdline completion matches.
887 */
888 static char_u *
889find_longest_match(expand_T *xp, int options)
890{
891 long_u len;
892 int mb_len = 1;
893 int c0, ci;
894 int i;
895 char_u *ss;
896
897 for (len = 0; xp->xp_files[0][len]; len += mb_len)
898 {
899 if (has_mbyte)
900 {
901 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
902 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
903 }
904 else
905 c0 = xp->xp_files[0][len];
906 for (i = 1; i < xp->xp_numfiles; ++i)
907 {
908 if (has_mbyte)
909 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
910 else
911 ci = xp->xp_files[i][len];
912 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
913 || xp->xp_context == EXPAND_FILES
914 || xp->xp_context == EXPAND_SHELLCMD
915 || xp->xp_context == EXPAND_BUFFERS))
916 {
917 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
918 break;
919 }
920 else if (c0 != ci)
921 break;
922 }
923 if (i < xp->xp_numfiles)
924 {
925 if (!(options & WILD_NO_BEEP))
926 vim_beep(BO_WILD);
927 break;
928 }
929 }
930
931 ss = alloc(len + 1);
932 if (ss)
933 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
934
935 return ss;
936}
937
938/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000939 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200940 * Chars that should not be expanded must be preceded with a backslash.
941 * Return a pointer to allocated memory containing the new string.
942 * Return NULL for failure.
943 *
944 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200945 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
946 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200947 *
948 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
949 * is WILD_EXPAND_FREE or WILD_ALL.
950 *
951 * mode = WILD_FREE: just free previously expanded matches
952 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
953 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
954 * mode = WILD_NEXT: use next match in multiple match, wrap to first
955 * mode = WILD_PREV: use previous match in multiple match, wrap to first
956 * mode = WILD_ALL: return all matches concatenated
957 * mode = WILD_LONGEST: return longest matched part
958 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000959 * mode = WILD_APPLY: apply the item selected in the cmdline completion
960 * popup menu and close the menu.
961 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
962 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200963 *
964 * options = WILD_LIST_NOTFOUND: list entries without a match
965 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
966 * options = WILD_USE_NL: Use '\n' for WILD_ALL
967 * options = WILD_NO_BEEP: Don't beep for multiple matches
968 * options = WILD_ADD_SLASH: add a slash after directory names
969 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
970 * options = WILD_SILENT: don't print warning messages
971 * options = WILD_ESCAPE: put backslash before special chars
972 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200973 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 *
975 * The variables xp->xp_context and xp->xp_backslash must have been set!
976 */
977 char_u *
978ExpandOne(
979 expand_T *xp,
980 char_u *str,
981 char_u *orig, // allocated copy of original of expanded string
982 int options,
983 int mode)
984{
985 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200986 int orig_saved = FALSE;
987 int i;
988 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200989
990 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000991 if (mode == WILD_NEXT || mode == WILD_PREV
992 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +0200993 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200994
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000995 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +0200996 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000997 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +0200998 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +0200999 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001000 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001001
Bram Moolenaar66b51422019-08-18 21:44:12 +02001002 // free old names
1003 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1004 {
1005 FreeWild(xp->xp_numfiles, xp->xp_files);
1006 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001007 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001008
1009 // The entries from xp_files may be used in the PUM, remove it.
1010 if (compl_match_array != NULL)
1011 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001012 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001013 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001014
1015 if (mode == WILD_FREE) // only release file name
1016 return NULL;
1017
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001018 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001019 {
zeertzjq28a23602023-09-29 19:58:35 +02001020 vim_free(xp->xp_orig);
1021 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001022 orig_saved = TRUE;
1023
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001024 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025 }
1026
1027 // Find longest common part
1028 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1029 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001030 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001031 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001032 }
1033
Bram Moolenaar57e95172022-08-20 19:26:14 +01001034 // Concatenate all matching names. Unless interrupted, this can be slow
1035 // and the result probably won't be used.
1036 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001037 {
1038 len = 0;
1039 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001040 {
1041 if (i > 0)
1042 {
1043 if (xp->xp_prefix == XP_PREFIX_NO)
1044 len += 2; // prefix "no"
1045 else if (xp->xp_prefix == XP_PREFIX_INV)
1046 len += 3; // prefix "inv"
1047 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001049 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001050 ss = alloc(len);
1051 if (ss != NULL)
1052 {
1053 *ss = NUL;
1054 for (i = 0; i < xp->xp_numfiles; ++i)
1055 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001056 if (i > 0)
1057 {
1058 if (xp->xp_prefix == XP_PREFIX_NO)
1059 STRCAT(ss, "no");
1060 else if (xp->xp_prefix == XP_PREFIX_INV)
1061 STRCAT(ss, "inv");
1062 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001063 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001064
Bram Moolenaar66b51422019-08-18 21:44:12 +02001065 if (i != xp->xp_numfiles - 1)
1066 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1067 }
1068 }
1069 }
1070
1071 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1072 ExpandCleanup(xp);
1073
zeertzjq28a23602023-09-29 19:58:35 +02001074 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001075 if (!orig_saved)
1076 vim_free(orig);
1077
1078 return ss;
1079}
1080
1081/*
1082 * Prepare an expand structure for use.
1083 */
1084 void
1085ExpandInit(expand_T *xp)
1086{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001087 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001088 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001089 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001090 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001091}
1092
1093/*
1094 * Cleanup an expand structure after use.
1095 */
1096 void
1097ExpandCleanup(expand_T *xp)
1098{
1099 if (xp->xp_numfiles >= 0)
1100 {
1101 FreeWild(xp->xp_numfiles, xp->xp_files);
1102 xp->xp_numfiles = -1;
1103 }
zeertzjq28a23602023-09-29 19:58:35 +02001104 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001105}
1106
1107/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001108 * Display one line of completion matches. Multiple matches are displayed in
1109 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001110 * matches - list of completion match names
1111 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001112 * lines - number of output lines
1113 * linenr - line number of matches to display
1114 * maxlen - maximum number of characters in each line
1115 * showtail - display only the tail of the full path of a file name
1116 * dir_attr - highlight attribute to use for directory names
1117 */
1118 static void
1119showmatches_oneline(
1120 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001121 char_u **matches,
1122 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001123 int lines,
1124 int linenr,
1125 int maxlen,
1126 int showtail,
1127 int dir_attr)
1128{
1129 int i, j;
1130 int isdir;
1131 int lastlen;
1132 char_u *p;
1133
1134 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001135 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001136 {
1137 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1138 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001139 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1140 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001141 msg_advance(maxlen + 1);
1142 msg_puts((char *)p);
1143 msg_advance(maxlen + 3);
1144 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1145 break;
1146 }
1147 for (i = maxlen - lastlen; --i >= 0; )
1148 msg_putchar(' ');
1149 if (xp->xp_context == EXPAND_FILES
1150 || xp->xp_context == EXPAND_SHELLCMD
1151 || xp->xp_context == EXPAND_BUFFERS)
1152 {
1153 // highlight directories
1154 if (xp->xp_numfiles != -1)
1155 {
1156 char_u *halved_slash;
1157 char_u *exp_path;
1158 char_u *path;
1159
1160 // Expansion was done before and special characters
1161 // were escaped, need to halve backslashes. Also
1162 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001163 exp_path = expand_env_save_opt(matches[j], TRUE);
1164 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001165 halved_slash = backslash_halve_save(path);
1166 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001167 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001168 vim_free(exp_path);
1169 if (halved_slash != path)
1170 vim_free(halved_slash);
1171 }
1172 else
1173 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001174 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001175 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001176 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001177 else
1178 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001179 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001180 TRUE);
1181 p = NameBuff;
1182 }
1183 }
1184 else
1185 {
1186 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001187 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001188 }
1189 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1190 }
1191 if (msg_col > 0) // when not wrapped around
1192 {
1193 msg_clr_eos();
1194 msg_putchar('\n');
1195 }
1196 out_flush(); // show one line at a time
1197}
1198
1199/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001200 * Show all matches for completion on the command line.
1201 * Returns EXPAND_NOTHING when the character that triggered expansion should
1202 * be inserted like a normal character.
1203 */
1204 int
1205showmatches(expand_T *xp, int wildmenu UNUSED)
1206{
1207 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001208 int numMatches;
1209 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001210 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001211 int maxlen;
1212 int lines;
1213 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001214 int attr;
1215 int showtail;
1216
1217 if (xp->xp_numfiles == -1)
1218 {
1219 set_expand_context(xp);
1220 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001221 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001222 showtail = expand_showtail(xp);
1223 if (i != EXPAND_OK)
1224 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001225 }
1226 else
1227 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001228 numMatches = xp->xp_numfiles;
1229 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001230 showtail = cmd_showtail;
1231 }
1232
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001233 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001234 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001235 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001236
Bram Moolenaar66b51422019-08-18 21:44:12 +02001237 if (!wildmenu)
1238 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001239 msg_didany = FALSE; // lines_left will be set
1240 msg_start(); // prepare for paging
1241 msg_putchar('\n');
1242 out_flush();
1243 cmdline_row = msg_row;
1244 msg_didany = FALSE; // lines_left will be set again
1245 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001246 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001247
1248 if (got_int)
1249 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001250 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001251 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001252 else
1253 {
1254 // find the length of the longest file name
1255 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001256 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 {
1258 if (!showtail && (xp->xp_context == EXPAND_FILES
1259 || xp->xp_context == EXPAND_SHELLCMD
1260 || xp->xp_context == EXPAND_BUFFERS))
1261 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001262 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 j = vim_strsize(NameBuff);
1264 }
1265 else
zeertzjqc51a3762022-12-10 10:22:29 +00001266 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001267 if (j > maxlen)
1268 maxlen = j;
1269 }
1270
1271 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001272 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001273 else
1274 {
1275 // compute the number of columns and lines for the listing
1276 maxlen += 2; // two spaces between file names
1277 columns = ((int)Columns + 2) / maxlen;
1278 if (columns < 1)
1279 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001280 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001281 }
1282
1283 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1284
1285 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1286 {
1287 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1288 msg_clr_eos();
1289 msg_advance(maxlen - 3);
1290 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1291 }
1292
1293 // list the files line by line
1294 for (i = 0; i < lines; ++i)
1295 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001296 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001297 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001298 if (got_int)
1299 {
1300 got_int = FALSE;
1301 break;
1302 }
1303 }
1304
1305 // we redraw the command below the lines that we have just listed
1306 // This is a bit tricky, but it saves a lot of screen updating.
1307 cmdline_row = msg_row; // will put it back later
1308 }
1309
1310 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001311 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001312
1313 return EXPAND_OK;
1314}
1315
1316/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001317 * gettail() version for showmatches() and win_redr_status_matches():
1318 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001319 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001320 static char_u *
1321showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001322{
1323 char_u *p;
1324 char_u *t = s;
1325 int had_sep = FALSE;
1326
1327 for (p = s; *p != NUL; )
1328 {
1329 if (vim_ispathsep(*p)
1330#ifdef BACKSLASH_IN_FILENAME
1331 && !rem_backslash(p)
1332#endif
1333 )
1334 had_sep = TRUE;
1335 else if (had_sep)
1336 {
1337 t = p;
1338 had_sep = FALSE;
1339 }
1340 MB_PTR_ADV(p);
1341 }
1342 return t;
1343}
1344
1345/*
1346 * Return TRUE if we only need to show the tail of completion matches.
1347 * When not completing file names or there is a wildcard in the path FALSE is
1348 * returned.
1349 */
1350 static int
1351expand_showtail(expand_T *xp)
1352{
1353 char_u *s;
1354 char_u *end;
1355
1356 // When not completing file names a "/" may mean something different.
1357 if (xp->xp_context != EXPAND_FILES
1358 && xp->xp_context != EXPAND_SHELLCMD
1359 && xp->xp_context != EXPAND_DIRECTORIES)
1360 return FALSE;
1361
1362 end = gettail(xp->xp_pattern);
1363 if (end == xp->xp_pattern) // there is no path separator
1364 return FALSE;
1365
1366 for (s = xp->xp_pattern; s < end; s++)
1367 {
1368 // Skip escaped wildcards. Only when the backslash is not a path
1369 // separator, on DOS the '*' "path\*\file" must not be skipped.
1370 if (rem_backslash(s))
1371 ++s;
1372 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1373 return FALSE;
1374 }
1375 return TRUE;
1376}
1377
1378/*
1379 * Prepare a string for expansion.
1380 * When expanding file names: The string will be used with expand_wildcards().
1381 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1382 * When expanding other names: The string will be used with regcomp(). Copy
1383 * the name into allocated memory and prepend "^".
1384 */
1385 char_u *
1386addstar(
1387 char_u *fname,
1388 int len,
1389 int context) // EXPAND_FILES etc.
1390{
1391 char_u *retval;
1392 int i, j;
1393 int new_len;
1394 char_u *tail;
1395 int ends_in_star;
1396
1397 if (context != EXPAND_FILES
1398 && context != EXPAND_FILES_IN_PATH
1399 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001400 && context != EXPAND_DIRECTORIES
1401 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001402 {
1403 // Matching will be done internally (on something other than files).
1404 // So we convert the file-matching-type wildcards into our kind for
1405 // use with vim_regcomp(). First work out how long it will be:
1406
1407 // For help tags the translation is done in find_help_tags().
1408 // For a tag pattern starting with "/" no translation is needed.
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01001409 if (context == EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01001410 || context == EXPAND_HELP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001411 || context == EXPAND_COLORS
1412 || context == EXPAND_COMPILER
1413 || context == EXPAND_OWNSYNTAX
1414 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001415 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001416 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001417 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001418 || ((context == EXPAND_TAGS_LISTFILES
1419 || context == EXPAND_TAGS)
1420 && fname[0] == '/'))
1421 retval = vim_strnsave(fname, len);
1422 else
1423 {
1424 new_len = len + 2; // +2 for '^' at start, NUL at end
1425 for (i = 0; i < len; i++)
1426 {
1427 if (fname[i] == '*' || fname[i] == '~')
1428 new_len++; // '*' needs to be replaced by ".*"
1429 // '~' needs to be replaced by "\~"
1430
1431 // Buffer names are like file names. "." should be literal
1432 if (context == EXPAND_BUFFERS && fname[i] == '.')
1433 new_len++; // "." becomes "\."
1434
1435 // Custom expansion takes care of special things, match
1436 // backslashes literally (perhaps also for other types?)
1437 if ((context == EXPAND_USER_DEFINED
1438 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1439 new_len++; // '\' becomes "\\"
1440 }
1441 retval = alloc(new_len);
1442 if (retval != NULL)
1443 {
1444 retval[0] = '^';
1445 j = 1;
1446 for (i = 0; i < len; i++, j++)
1447 {
1448 // Skip backslash. But why? At least keep it for custom
1449 // expansion.
1450 if (context != EXPAND_USER_DEFINED
1451 && context != EXPAND_USER_LIST
1452 && fname[i] == '\\'
1453 && ++i == len)
1454 break;
1455
1456 switch (fname[i])
1457 {
1458 case '*': retval[j++] = '.';
1459 break;
1460 case '~': retval[j++] = '\\';
1461 break;
1462 case '?': retval[j] = '.';
1463 continue;
1464 case '.': if (context == EXPAND_BUFFERS)
1465 retval[j++] = '\\';
1466 break;
1467 case '\\': if (context == EXPAND_USER_DEFINED
1468 || context == EXPAND_USER_LIST)
1469 retval[j++] = '\\';
1470 break;
1471 }
1472 retval[j] = fname[i];
1473 }
1474 retval[j] = NUL;
1475 }
1476 }
1477 }
1478 else
1479 {
1480 retval = alloc(len + 4);
1481 if (retval != NULL)
1482 {
1483 vim_strncpy(retval, fname, len);
1484
1485 // Don't add a star to *, ~, ~user, $var or `cmd`.
1486 // * would become **, which walks the whole tree.
1487 // ~ would be at the start of the file name, but not the tail.
1488 // $ could be anywhere in the tail.
1489 // ` could be anywhere in the file name.
1490 // When the name ends in '$' don't add a star, remove the '$'.
1491 tail = gettail(retval);
1492 ends_in_star = (len > 0 && retval[len - 1] == '*');
1493#ifndef BACKSLASH_IN_FILENAME
1494 for (i = len - 2; i >= 0; --i)
1495 {
1496 if (retval[i] != '\\')
1497 break;
1498 ends_in_star = !ends_in_star;
1499 }
1500#endif
1501 if ((*retval != '~' || tail != retval)
1502 && !ends_in_star
1503 && vim_strchr(tail, '$') == NULL
1504 && vim_strchr(retval, '`') == NULL)
1505 retval[len++] = '*';
1506 else if (len > 0 && retval[len - 1] == '$')
1507 --len;
1508 retval[len] = NUL;
1509 }
1510 }
1511 return retval;
1512}
1513
1514/*
1515 * Must parse the command line so far to work out what context we are in.
1516 * Completion can then be done based on that context.
1517 * This routine sets the variables:
1518 * xp->xp_pattern The start of the pattern to be expanded within
1519 * the command line (ends at the cursor).
1520 * xp->xp_context The type of thing to expand. Will be one of:
1521 *
1522 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1523 * the command line, like an unknown command. Caller
1524 * should beep.
1525 * EXPAND_NOTHING Unrecognised context for completion, use char like
1526 * a normal char, rather than for completion. eg
1527 * :s/^I/
1528 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1529 * it.
1530 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1531 * EXPAND_FILES After command with EX_XFILE set, or after setting
1532 * with P_EXPAND set. eg :e ^I, :w>>^I
1533 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001534 * when we know only directories are of interest.
1535 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001536 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1537 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1538 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1539 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1540 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1541 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1542 * EXPAND_EVENTS Complete event names
1543 * EXPAND_SYNTAX Complete :syntax command arguments
1544 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1545 * EXPAND_AUGROUP Complete autocommand group names
1546 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1547 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1548 * eg :unmap a^I , :cunab x^I
1549 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1550 * eg :call sub^I
1551 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1552 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1553 * names in expressions, eg :while s^I
1554 * EXPAND_ENV_VARS Complete environment variable names
1555 * EXPAND_USER Complete user names
1556 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001557 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001558set_expand_context(expand_T *xp)
1559{
1560 cmdline_info_T *ccline = get_cmdline_info();
1561
1562 // only expansion for ':', '>' and '=' command-lines
1563 if (ccline->cmdfirstc != ':'
1564#ifdef FEAT_EVAL
1565 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1566 && !ccline->input_fn
1567#endif
1568 )
1569 {
1570 xp->xp_context = EXPAND_NOTHING;
1571 return;
1572 }
1573 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1574}
1575
Bram Moolenaard0190392019-08-23 21:17:35 +02001576/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001577 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1578 * For user defined commands, the completion context is set in 'xp' and the
1579 * completion flags in 'complp'.
1580 *
1581 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001582 */
1583 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001584set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001585{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001586 char_u *p = NULL;
1587 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001588 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001589
1590 // Isolate the command and search for it in the command table.
1591 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001592 // - the 'k' command can directly be followed by any character, but do
1593 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1594 // find matches anywhere in the command name, do this only for command
1595 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001596 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001597 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001598 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001599 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001600 p = cmd + 1;
1601 }
1602 else
1603 {
1604 p = cmd;
1605 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1606 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001607 // A user command may contain digits.
1608 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1609 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 while (ASCII_ISALNUM(*p) || *p == '*')
1611 ++p;
1612 // for python 3.x: ":py3*" commands completion
1613 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1614 {
1615 ++p;
1616 while (ASCII_ISALPHA(*p) || *p == '*')
1617 ++p;
1618 }
1619 // check for non-alpha command
1620 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1621 ++p;
1622 len = (int)(p - cmd);
1623
1624 if (len == 0)
1625 {
1626 xp->xp_context = EXPAND_UNSUCCESSFUL;
1627 return NULL;
1628 }
1629
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001630 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001631
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001632 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001633 // Also when doing fuzzy expansion for non-shell commands, support
1634 // alphanumeric characters.
1635 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1636 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001637 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1638 ++p;
1639 }
1640
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001641 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001642 // character, complete the command name.
1643 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1644 return NULL;
1645
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001646 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001647 {
1648 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1649 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001650 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001651 p = cmd + 1;
1652 }
1653 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1654 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001655 eap->cmd = cmd;
1656 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001657 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001658 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001659 }
1660 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001661 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001662 {
1663 // Not still touching the command and it was an illegal one
1664 xp->xp_context = EXPAND_UNSUCCESSFUL;
1665 return NULL;
1666 }
1667
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 return p;
1669}
Bram Moolenaard0190392019-08-23 21:17:35 +02001670
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671/*
1672 * Set the completion context for a command argument with wild card characters.
1673 */
1674 static void
1675set_context_for_wildcard_arg(
1676 exarg_T *eap,
1677 char_u *arg,
1678 int usefilter,
1679 expand_T *xp,
1680 int *complp)
1681{
1682 char_u *p;
1683 int c;
1684 int in_quote = FALSE;
1685 char_u *bow = NULL; // Beginning of word
1686 int len = 0;
1687
1688 // Allow spaces within back-quotes to count as part of the argument
1689 // being expanded.
1690 xp->xp_pattern = skipwhite(arg);
1691 p = xp->xp_pattern;
1692 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001693 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001694 if (has_mbyte)
1695 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001696 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001697 c = *p;
1698 if (c == '\\' && p[1] != NUL)
1699 ++p;
1700 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001701 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001702 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001703 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001704 xp->xp_pattern = p;
1705 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 in_quote = !in_quote;
1708 }
1709 // An argument can contain just about everything, except
1710 // characters that end the command and white space.
1711 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001712#ifdef SPACE_IN_FILENAME
zeertzjq85f36d62024-10-10 19:14:13 +02001713 && (!(eap != NULL && (eap->argt & EX_NOSPC)) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001714#endif
1715 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001716 {
1717 len = 0; // avoid getting stuck when space is in 'isfname'
1718 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001719 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001720 if (has_mbyte)
1721 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001722 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001723 c = *p;
1724 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001725 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001726 if (has_mbyte)
1727 len = (*mb_ptr2len)(p);
1728 else
1729 len = 1;
1730 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001731 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001732 if (in_quote)
1733 bow = p;
1734 else
1735 xp->xp_pattern = p;
1736 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001737 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001738 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001739 }
1740
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001741 // If we are still inside the quotes, and we passed a space, just
1742 // expand from there.
1743 if (bow != NULL && in_quote)
1744 xp->xp_pattern = bow;
1745 xp->xp_context = EXPAND_FILES;
1746
1747 // For a shell command more chars need to be escaped.
zeertzjq85f36d62024-10-10 19:14:13 +02001748 if (usefilter
1749 || (eap != NULL
1750 && (eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal))
1751 || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001752 {
1753#ifndef BACKSLASH_IN_FILENAME
1754 xp->xp_shell = TRUE;
1755#endif
1756 // When still after the command name expand executables.
1757 if (xp->xp_pattern == skipwhite(arg))
1758 xp->xp_context = EXPAND_SHELLCMD;
1759 }
1760
1761 // Check for environment variable.
1762 if (*xp->xp_pattern == '$')
1763 {
1764 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1765 if (!vim_isIDc(*p))
1766 break;
1767 if (*p == NUL)
1768 {
1769 xp->xp_context = EXPAND_ENV_VARS;
1770 ++xp->xp_pattern;
1771 // Avoid that the assignment uses EXPAND_FILES again.
1772 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1773 *complp = EXPAND_ENV_VARS;
1774 }
1775 }
1776 // Check for user names.
1777 if (*xp->xp_pattern == '~')
1778 {
1779 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1780 ;
1781 // Complete ~user only if it partially matches a user name.
1782 // A full match ~user<Tab> will be replaced by user's home
1783 // directory i.e. something like ~user<Tab> -> /home/user/
1784 if (*p == NUL && p > xp->xp_pattern + 1
1785 && match_user(xp->xp_pattern + 1) >= 1)
1786 {
1787 xp->xp_context = EXPAND_USER;
1788 ++xp->xp_pattern;
1789 }
1790 }
1791}
1792
1793/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001794 * Set the completion context for the "++opt=arg" argument. Always returns
1795 * NULL.
1796 */
1797 static char_u *
1798set_context_in_argopt(expand_T *xp, char_u *arg)
1799{
1800 char_u *p;
1801
1802 p = vim_strchr(arg, '=');
1803 if (p == NULL)
1804 xp->xp_pattern = arg;
1805 else
1806 xp->xp_pattern = p + 1;
1807
1808 xp->xp_context = EXPAND_ARGOPT;
1809 return NULL;
1810}
1811
1812#ifdef FEAT_TERMINAL
1813/*
1814 * Set the completion context for :terminal's [options]. Always returns NULL.
1815 */
1816 static char_u *
1817set_context_in_terminalopt(expand_T *xp, char_u *arg)
1818{
1819 char_u *p;
1820
1821 p = vim_strchr(arg, '=');
1822 if (p == NULL)
1823 xp->xp_pattern = arg;
1824 else
1825 xp->xp_pattern = p + 1;
1826
1827 xp->xp_context = EXPAND_TERMINALOPT;
1828 return NULL;
1829}
1830#endif
1831
1832/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001833 * Set the completion context for the :filter command. Returns a pointer to the
1834 * next command after the :filter command.
1835 */
1836 static char_u *
1837set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1838{
1839 if (*arg != NUL)
1840 arg = skip_vimgrep_pat(arg, NULL, NULL);
1841 if (arg == NULL || *arg == NUL)
1842 {
1843 xp->xp_context = EXPAND_NOTHING;
1844 return NULL;
1845 }
1846 return skipwhite(arg);
1847}
1848
1849#ifdef FEAT_SEARCH_EXTRA
1850/*
1851 * Set the completion context for the :match command. Returns a pointer to the
1852 * next command after the :match command.
1853 */
1854 static char_u *
1855set_context_in_match_cmd(expand_T *xp, char_u *arg)
1856{
1857 if (*arg == NUL || !ends_excmd(*arg))
1858 {
1859 // also complete "None"
1860 set_context_in_echohl_cmd(xp, arg);
1861 arg = skipwhite(skiptowhite(arg));
1862 if (*arg != NUL)
1863 {
1864 xp->xp_context = EXPAND_NOTHING;
1865 arg = skip_regexp(arg + 1, *arg, magic_isset());
1866 }
1867 }
1868 return find_nextcmd(arg);
1869}
1870#endif
1871
1872/*
1873 * Returns a pointer to the next command after a :global or a :v command.
1874 * Returns NULL if there is no next command.
1875 */
1876 static char_u *
1877find_cmd_after_global_cmd(char_u *arg)
1878{
1879 int delim;
1880
1881 delim = *arg; // get the delimiter
1882 if (delim)
1883 ++arg; // skip delimiter if there is one
1884
1885 while (arg[0] != NUL && arg[0] != delim)
1886 {
1887 if (arg[0] == '\\' && arg[1] != NUL)
1888 ++arg;
1889 ++arg;
1890 }
1891 if (arg[0] != NUL)
1892 return arg + 1;
1893
1894 return NULL;
1895}
1896
1897/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001898 * Returns a pointer to the next command after a :substitute or a :& command.
1899 * Returns NULL if there is no next command.
1900 */
1901 static char_u *
1902find_cmd_after_substitute_cmd(char_u *arg)
1903{
1904 int delim;
1905
1906 delim = *arg;
1907 if (delim)
1908 {
1909 // skip "from" part
1910 ++arg;
1911 arg = skip_regexp(arg, delim, magic_isset());
1912
1913 if (arg[0] != NUL && arg[0] == delim)
1914 {
1915 // skip "to" part
1916 ++arg;
1917 while (arg[0] != NUL && arg[0] != delim)
1918 {
1919 if (arg[0] == '\\' && arg[1] != NUL)
1920 ++arg;
1921 ++arg;
1922 }
1923 if (arg[0] != NUL) // skip delimiter
1924 ++arg;
1925 }
1926 }
1927 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1928 ++arg;
1929 if (arg[0] != NUL)
1930 return arg;
1931
1932 return NULL;
1933}
1934
1935/*
1936 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1937 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1938 * Returns NULL if there is no next command.
1939 */
1940 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001941find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001942{
1943 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001944 if (*arg != '/')
1945 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001946
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001947 // Match regexp, not just whole words
1948 for (++arg; *arg && *arg != '/'; arg++)
1949 if (*arg == '\\' && arg[1] != NUL)
1950 arg++;
1951 if (*arg)
1952 {
1953 arg = skipwhite(arg + 1);
1954
1955 // Check for trailing illegal characters
1956 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1957 xp->xp_context = EXPAND_NOTHING;
1958 else
1959 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001960 }
1961
1962 return NULL;
1963}
1964
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001965#ifdef FEAT_EVAL
1966/*
1967 * Set the completion context for the :unlet command. Always returns NULL.
1968 */
1969 static char_u *
1970set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1971{
1972 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1973 arg = xp->xp_pattern + 1;
1974
1975 xp->xp_context = EXPAND_USER_VARS;
1976 xp->xp_pattern = arg;
1977
1978 if (*xp->xp_pattern == '$')
1979 {
1980 xp->xp_context = EXPAND_ENV_VARS;
1981 ++xp->xp_pattern;
1982 }
1983
1984 return NULL;
1985}
1986#endif
1987
1988#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1989/*
1990 * Set the completion context for the :language command. Always returns NULL.
1991 */
1992 static char_u *
1993set_context_in_lang_cmd(expand_T *xp, char_u *arg)
1994{
1995 char_u *p;
1996
1997 p = skiptowhite(arg);
1998 if (*p == NUL)
1999 {
2000 xp->xp_context = EXPAND_LANGUAGE;
2001 xp->xp_pattern = arg;
2002 }
2003 else
2004 {
2005 if ( STRNCMP(arg, "messages", p - arg) == 0
2006 || STRNCMP(arg, "ctype", p - arg) == 0
2007 || STRNCMP(arg, "time", p - arg) == 0
2008 || STRNCMP(arg, "collate", p - arg) == 0)
2009 {
2010 xp->xp_context = EXPAND_LOCALES;
2011 xp->xp_pattern = skipwhite(p);
2012 }
2013 else
2014 xp->xp_context = EXPAND_NOTHING;
2015 }
2016
2017 return NULL;
2018}
2019#endif
2020
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002021#ifdef FEAT_EVAL
2022static enum
2023{
2024 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002025 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2026 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002027} breakpt_expand_what;
2028
2029/*
2030 * Set the completion context for the :breakadd command. Always returns NULL.
2031 */
2032 static char_u *
2033set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2034{
2035 char_u *p;
2036 char_u *subcmd_start;
2037
2038 xp->xp_context = EXPAND_BREAKPOINT;
2039 xp->xp_pattern = arg;
2040
2041 if (cmdidx == CMD_breakadd)
2042 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002043 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002044 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002045 else
2046 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002047
2048 p = skipwhite(arg);
2049 if (*p == NUL)
2050 return NULL;
2051 subcmd_start = p;
2052
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002053 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002054 {
2055 // :breakadd file [lnum] <filename>
2056 // :breakadd func [lnum] <funcname>
2057 p += 4;
2058 p = skipwhite(p);
2059
2060 // skip line number (if specified)
2061 if (VIM_ISDIGIT(*p))
2062 {
2063 p = skipdigits(p);
2064 if (*p != ' ')
2065 {
2066 xp->xp_context = EXPAND_NOTHING;
2067 return NULL;
2068 }
2069 p = skipwhite(p);
2070 }
2071 if (STRNCMP("file", subcmd_start, 4) == 0)
2072 xp->xp_context = EXPAND_FILES;
2073 else
2074 xp->xp_context = EXPAND_USER_FUNC;
2075 xp->xp_pattern = p;
2076 }
2077 else if (STRNCMP("expr ", p, 5) == 0)
2078 {
2079 // :breakadd expr <expression>
2080 xp->xp_context = EXPAND_EXPRESSION;
2081 xp->xp_pattern = skipwhite(p + 5);
2082 }
2083
2084 return NULL;
2085}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002086
2087 static char_u *
2088set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2089{
2090 char_u *p;
2091
2092 xp->xp_context = EXPAND_NOTHING;
2093 xp->xp_pattern = NULL;
2094
2095 p = skipwhite(arg);
2096 if (VIM_ISDIGIT(*p))
2097 return NULL;
2098
2099 xp->xp_context = EXPAND_SCRIPTNAMES;
2100 xp->xp_pattern = p;
2101
2102 return NULL;
2103}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002104#endif
2105
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002106/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002107 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2108 * The argument to the command is 'arg' and the argument flags is 'argt'.
2109 * For user-defined commands and for environment variables, 'compl' has the
2110 * completion type.
2111 * Returns a pointer to the next command. Returns NULL if there is no next
2112 * command.
2113 */
2114 static char_u *
2115set_context_by_cmdname(
2116 char_u *cmd,
2117 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002118 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002119 char_u *arg,
2120 long argt,
2121 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002122 int forceit)
2123{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002124 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002125 {
2126 case CMD_find:
2127 case CMD_sfind:
2128 case CMD_tabfind:
2129 if (xp->xp_context == EXPAND_FILES)
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002130 xp->xp_context = *get_findfunc() != NUL ? EXPAND_FINDFUNC
zeertzjq20e045f2024-10-28 22:05:26 +01002131 : EXPAND_FILES_IN_PATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002132 break;
2133 case CMD_cd:
2134 case CMD_chdir:
2135 case CMD_tcd:
2136 case CMD_tchdir:
2137 case CMD_lcd:
2138 case CMD_lchdir:
2139 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002140 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002141 break;
2142 case CMD_help:
2143 xp->xp_context = EXPAND_HELP;
2144 xp->xp_pattern = arg;
2145 break;
2146
2147 // Command modifiers: return the argument.
2148 // Also for commands with an argument that is a command.
2149 case CMD_aboveleft:
2150 case CMD_argdo:
2151 case CMD_belowright:
2152 case CMD_botright:
2153 case CMD_browse:
2154 case CMD_bufdo:
2155 case CMD_cdo:
2156 case CMD_cfdo:
2157 case CMD_confirm:
2158 case CMD_debug:
2159 case CMD_folddoclosed:
2160 case CMD_folddoopen:
2161 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002162 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002163 case CMD_keepalt:
2164 case CMD_keepjumps:
2165 case CMD_keepmarks:
2166 case CMD_keeppatterns:
2167 case CMD_ldo:
2168 case CMD_leftabove:
2169 case CMD_lfdo:
2170 case CMD_lockmarks:
2171 case CMD_noautocmd:
2172 case CMD_noswapfile:
2173 case CMD_rightbelow:
2174 case CMD_sandbox:
2175 case CMD_silent:
2176 case CMD_tab:
2177 case CMD_tabdo:
2178 case CMD_topleft:
2179 case CMD_verbose:
2180 case CMD_vertical:
2181 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002182 case CMD_vim9cmd:
2183 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002184 return arg;
2185
2186 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002187 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002188
2189#ifdef FEAT_SEARCH_EXTRA
2190 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002191 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002192#endif
2193
2194 // All completion for the +cmdline_compl feature goes here.
2195
2196 case CMD_command:
2197 return set_context_in_user_cmd(xp, arg);
2198
2199 case CMD_delcommand:
2200 xp->xp_context = EXPAND_USER_COMMANDS;
2201 xp->xp_pattern = arg;
2202 break;
2203
2204 case CMD_global:
2205 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002206 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002207 case CMD_and:
2208 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002209 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002210 case CMD_isearch:
2211 case CMD_dsearch:
2212 case CMD_ilist:
2213 case CMD_dlist:
2214 case CMD_ijump:
2215 case CMD_psearch:
2216 case CMD_djump:
2217 case CMD_isplit:
2218 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002219 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002220 case CMD_autocmd:
2221 return set_context_in_autocmd(xp, arg, FALSE);
2222 case CMD_doautocmd:
2223 case CMD_doautoall:
2224 return set_context_in_autocmd(xp, arg, TRUE);
2225 case CMD_set:
2226 set_context_in_set_cmd(xp, arg, 0);
2227 break;
2228 case CMD_setglobal:
2229 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2230 break;
2231 case CMD_setlocal:
2232 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2233 break;
2234 case CMD_tag:
2235 case CMD_stag:
2236 case CMD_ptag:
2237 case CMD_ltag:
2238 case CMD_tselect:
2239 case CMD_stselect:
2240 case CMD_ptselect:
2241 case CMD_tjump:
2242 case CMD_stjump:
2243 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002244 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002245 xp->xp_context = EXPAND_TAGS_LISTFILES;
2246 else
2247 xp->xp_context = EXPAND_TAGS;
2248 xp->xp_pattern = arg;
2249 break;
2250 case CMD_augroup:
2251 xp->xp_context = EXPAND_AUGROUP;
2252 xp->xp_pattern = arg;
2253 break;
2254#ifdef FEAT_SYN_HL
2255 case CMD_syntax:
2256 set_context_in_syntax_cmd(xp, arg);
2257 break;
2258#endif
2259#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002260 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002261 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002262 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002263 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002264 case CMD_if:
2265 case CMD_elseif:
2266 case CMD_while:
2267 case CMD_for:
2268 case CMD_echo:
2269 case CMD_echon:
2270 case CMD_execute:
2271 case CMD_echomsg:
2272 case CMD_echoerr:
2273 case CMD_call:
2274 case CMD_return:
2275 case CMD_cexpr:
2276 case CMD_caddexpr:
2277 case CMD_cgetexpr:
2278 case CMD_lexpr:
2279 case CMD_laddexpr:
2280 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002281 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002282 break;
2283
2284 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002285 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002286 case CMD_function:
2287 case CMD_delfunction:
2288 xp->xp_context = EXPAND_USER_FUNC;
2289 xp->xp_pattern = arg;
2290 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002291 case CMD_disassemble:
2292 set_context_in_disassemble_cmd(xp, arg);
2293 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002294
2295 case CMD_echohl:
2296 set_context_in_echohl_cmd(xp, arg);
2297 break;
2298#endif
2299 case CMD_highlight:
2300 set_context_in_highlight_cmd(xp, arg);
2301 break;
2302#ifdef FEAT_CSCOPE
2303 case CMD_cscope:
2304 case CMD_lcscope:
2305 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002306 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002307 break;
2308#endif
2309#ifdef FEAT_SIGNS
2310 case CMD_sign:
2311 set_context_in_sign_cmd(xp, arg);
2312 break;
2313#endif
2314 case CMD_bdelete:
2315 case CMD_bwipeout:
2316 case CMD_bunload:
2317 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2318 arg = xp->xp_pattern + 1;
2319 // FALLTHROUGH
2320 case CMD_buffer:
2321 case CMD_sbuffer:
zeertzjq3baf19a2024-12-19 20:05:28 +01002322 case CMD_pbuffer:
Bram Moolenaard0190392019-08-23 21:17:35 +02002323 case CMD_checktime:
2324 xp->xp_context = EXPAND_BUFFERS;
2325 xp->xp_pattern = arg;
2326 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002327#ifdef FEAT_DIFF
2328 case CMD_diffget:
2329 case CMD_diffput:
2330 // If current buffer is in diff mode, complete buffer names
2331 // which are in diff mode, and different than current buffer.
2332 xp->xp_context = EXPAND_DIFF_BUFFERS;
2333 xp->xp_pattern = arg;
2334 break;
2335#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002336 case CMD_USER:
2337 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002338 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2339 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002340
2341 case CMD_map: case CMD_noremap:
2342 case CMD_nmap: case CMD_nnoremap:
2343 case CMD_vmap: case CMD_vnoremap:
2344 case CMD_omap: case CMD_onoremap:
2345 case CMD_imap: case CMD_inoremap:
2346 case CMD_cmap: case CMD_cnoremap:
2347 case CMD_lmap: case CMD_lnoremap:
2348 case CMD_smap: case CMD_snoremap:
2349 case CMD_tmap: case CMD_tnoremap:
2350 case CMD_xmap: case CMD_xnoremap:
2351 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002352 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002353 case CMD_unmap:
2354 case CMD_nunmap:
2355 case CMD_vunmap:
2356 case CMD_ounmap:
2357 case CMD_iunmap:
2358 case CMD_cunmap:
2359 case CMD_lunmap:
2360 case CMD_sunmap:
2361 case CMD_tunmap:
2362 case CMD_xunmap:
2363 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002364 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002365 case CMD_mapclear:
2366 case CMD_nmapclear:
2367 case CMD_vmapclear:
2368 case CMD_omapclear:
2369 case CMD_imapclear:
2370 case CMD_cmapclear:
2371 case CMD_lmapclear:
2372 case CMD_smapclear:
2373 case CMD_tmapclear:
2374 case CMD_xmapclear:
2375 xp->xp_context = EXPAND_MAPCLEAR;
2376 xp->xp_pattern = arg;
2377 break;
2378
2379 case CMD_abbreviate: case CMD_noreabbrev:
2380 case CMD_cabbrev: case CMD_cnoreabbrev:
2381 case CMD_iabbrev: case CMD_inoreabbrev:
2382 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002383 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002384 case CMD_unabbreviate:
2385 case CMD_cunabbrev:
2386 case CMD_iunabbrev:
2387 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002388 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002389#ifdef FEAT_MENU
2390 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2391 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2392 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2393 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2394 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2395 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2396 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2397 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2398 case CMD_tmenu: case CMD_tunmenu:
2399 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2400 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2401#endif
2402
2403 case CMD_colorscheme:
2404 xp->xp_context = EXPAND_COLORS;
2405 xp->xp_pattern = arg;
2406 break;
2407
2408 case CMD_compiler:
2409 xp->xp_context = EXPAND_COMPILER;
2410 xp->xp_pattern = arg;
2411 break;
2412
2413 case CMD_ownsyntax:
2414 xp->xp_context = EXPAND_OWNSYNTAX;
2415 xp->xp_pattern = arg;
2416 break;
2417
2418 case CMD_setfiletype:
2419 xp->xp_context = EXPAND_FILETYPE;
2420 xp->xp_pattern = arg;
2421 break;
2422
2423 case CMD_packadd:
2424 xp->xp_context = EXPAND_PACKADD;
2425 xp->xp_pattern = arg;
2426 break;
2427
zeertzjqb0d45ec2023-01-25 15:04:22 +00002428 case CMD_runtime:
2429 set_context_in_runtime_cmd(xp, arg);
2430 break;
2431
Bram Moolenaard0190392019-08-23 21:17:35 +02002432#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2433 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002434 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002435#endif
2436#if defined(FEAT_PROFILE)
2437 case CMD_profile:
2438 set_context_in_profile_cmd(xp, arg);
2439 break;
2440#endif
2441 case CMD_behave:
2442 xp->xp_context = EXPAND_BEHAVE;
2443 xp->xp_pattern = arg;
2444 break;
2445
2446 case CMD_messages:
2447 xp->xp_context = EXPAND_MESSAGES;
2448 xp->xp_pattern = arg;
2449 break;
2450
2451 case CMD_history:
2452 xp->xp_context = EXPAND_HISTORY;
2453 xp->xp_pattern = arg;
2454 break;
2455#if defined(FEAT_PROFILE)
2456 case CMD_syntime:
2457 xp->xp_context = EXPAND_SYNTIME;
2458 xp->xp_pattern = arg;
2459 break;
2460#endif
2461
2462 case CMD_argdelete:
2463 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2464 arg = xp->xp_pattern + 1;
2465 xp->xp_context = EXPAND_ARGLIST;
2466 xp->xp_pattern = arg;
2467 break;
2468
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002469#ifdef FEAT_EVAL
2470 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002471 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002472 case CMD_breakdel:
2473 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002474
2475 case CMD_scriptnames:
2476 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002477#endif
2478
Bram Moolenaard0190392019-08-23 21:17:35 +02002479 default:
2480 break;
2481 }
2482 return NULL;
2483}
2484
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002485/*
2486 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2487 * we don't need/want deleted. Maybe this could be done better if we didn't
2488 * repeat all this stuff. The only problem is that they may not stay
2489 * perfectly compatible with each other, but then the command line syntax
2490 * probably won't change that much -- webb.
2491 */
2492 static char_u *
2493set_one_cmd_context(
2494 expand_T *xp,
2495 char_u *buff) // buffer for command string
2496{
2497 char_u *p;
2498 char_u *cmd, *arg;
2499 int len = 0;
2500 exarg_T ea;
2501 int compl = EXPAND_NOTHING;
2502 int forceit = FALSE;
2503 int usefilter = FALSE; // filter instead of file name
2504
2505 ExpandInit(xp);
2506 xp->xp_pattern = buff;
2507 xp->xp_line = buff;
2508 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2509 ea.argt = 0;
2510
2511 // 1. skip comment lines and leading space, colons or bars
2512 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2513 ;
2514 xp->xp_pattern = cmd;
2515
2516 if (*cmd == NUL)
2517 return NULL;
2518 if (*cmd == '"') // ignore comment lines
2519 {
2520 xp->xp_context = EXPAND_NOTHING;
2521 return NULL;
2522 }
2523
2524 // 3. Skip over the range to find the command.
2525 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2526 xp->xp_pattern = cmd;
2527 if (*cmd == NUL)
2528 return NULL;
2529 if (*cmd == '"')
2530 {
2531 xp->xp_context = EXPAND_NOTHING;
2532 return NULL;
2533 }
2534
2535 if (*cmd == '|' || *cmd == '\n')
2536 return cmd + 1; // There's another command
2537
2538 // Get the command index.
2539 p = set_cmd_index(cmd, &ea, xp, &compl);
2540 if (p == NULL)
2541 return NULL;
2542
2543 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2544
2545 if (*p == '!') // forced commands
2546 {
2547 forceit = TRUE;
2548 ++p;
2549 }
2550
2551 // 6. parse arguments
2552 if (!IS_USER_CMDIDX(ea.cmdidx))
2553 ea.argt = excmd_get_argt(ea.cmdidx);
2554
2555 arg = skipwhite(p);
2556
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002557 // Does command allow "++argopt" argument?
2558 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002559 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002560 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2561 {
2562 p = arg + 2;
2563 while (*p && !vim_isspace(*p))
2564 MB_PTR_ADV(p);
2565
2566 // Still touching the command after "++"?
2567 if (*p == NUL)
2568 {
2569 if (ea.argt & EX_ARGOPT)
2570 return set_context_in_argopt(xp, arg + 2);
2571#ifdef FEAT_TERMINAL
2572 if (ea.cmdidx == CMD_terminal)
2573 return set_context_in_terminalopt(xp, arg + 2);
2574#endif
2575 }
2576
2577 arg = skipwhite(p);
2578 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002579 }
2580
2581 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2582 {
2583 if (*arg == '>') // append
2584 {
2585 if (*++arg == '>')
2586 ++arg;
2587 arg = skipwhite(arg);
2588 }
2589 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2590 {
2591 ++arg;
2592 usefilter = TRUE;
2593 }
2594 }
2595
2596 if (ea.cmdidx == CMD_read)
2597 {
2598 usefilter = forceit; // :r! filter if forced
2599 if (*arg == '!') // :r !filter
2600 {
2601 ++arg;
2602 usefilter = TRUE;
2603 }
2604 }
2605
2606 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2607 {
2608 while (*arg == *cmd) // allow any number of '>' or '<'
2609 ++arg;
2610 arg = skipwhite(arg);
2611 }
2612
2613 // Does command allow "+command"?
2614 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2615 {
2616 // Check if we're in the +command
2617 p = arg + 1;
2618 arg = skip_cmd_arg(arg, FALSE);
2619
2620 // Still touching the command after '+'?
2621 if (*arg == NUL)
2622 return p;
2623
2624 // Skip space(s) after +command to get to the real argument
2625 arg = skipwhite(arg);
2626 }
2627
2628
2629 // Check for '|' to separate commands and '"' to start comments.
2630 // Don't do this for ":read !cmd" and ":write !cmd".
2631 if ((ea.argt & EX_TRLBAR) && !usefilter)
2632 {
2633 p = arg;
2634 // ":redir @" is not the start of a comment
2635 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2636 p += 2;
2637 while (*p)
2638 {
2639 if (*p == Ctrl_V)
2640 {
2641 if (p[1] != NUL)
2642 ++p;
2643 }
2644 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2645 || *p == '|' || *p == '\n')
2646 {
2647 if (*(p - 1) != '\\')
2648 {
2649 if (*p == '|' || *p == '\n')
2650 return p + 1;
2651 return NULL; // It's a comment
2652 }
2653 }
2654 MB_PTR_ADV(p);
2655 }
2656 }
2657
2658 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2659 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2660 // no arguments allowed but there is something
2661 return NULL;
2662
2663 // Find start of last argument (argument just before cursor):
2664 p = buff;
2665 xp->xp_pattern = p;
2666 len = (int)STRLEN(buff);
2667 while (*p && p < buff + len)
2668 {
2669 if (*p == ' ' || *p == TAB)
2670 {
2671 // argument starts after a space
2672 xp->xp_pattern = ++p;
2673 }
2674 else
2675 {
2676 if (*p == '\\' && *(p + 1) != NUL)
2677 ++p; // skip over escaped character
2678 MB_PTR_ADV(p);
2679 }
2680 }
2681
2682 if (ea.argt & EX_XFILE)
2683 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2684
2685 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002686 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002687 forceit);
2688}
2689
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002690/*
2691 * Set the completion context in 'xp' for command 'str'
2692 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002693 void
2694set_cmd_context(
2695 expand_T *xp,
2696 char_u *str, // start of command line
2697 int len, // length of command line (excl. NUL)
2698 int col, // position of cursor
2699 int use_ccline UNUSED) // use ccline for info
2700{
2701#ifdef FEAT_EVAL
2702 cmdline_info_T *ccline = get_cmdline_info();
2703#endif
2704 int old_char = NUL;
2705 char_u *nextcomm;
2706
2707 // Avoid a UMR warning from Purify, only save the character if it has been
2708 // written before.
2709 if (col < len)
2710 old_char = str[col];
2711 str[col] = NUL;
2712 nextcomm = str;
2713
2714#ifdef FEAT_EVAL
2715 if (use_ccline && ccline->cmdfirstc == '=')
2716 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002717 // pass CMD_SIZE because there is no real command
2718 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002719 }
2720 else if (use_ccline && ccline->input_fn)
2721 {
2722 xp->xp_context = ccline->xp_context;
2723 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002725 }
2726 else
2727#endif
2728 while (nextcomm != NULL)
2729 nextcomm = set_one_cmd_context(xp, nextcomm);
2730
2731 // Store the string here so that call_user_expand_func() can get to them
2732 // easily.
2733 xp->xp_line = str;
2734 xp->xp_col = col;
2735
2736 str[col] = old_char;
2737}
2738
2739/*
2740 * Expand the command line "str" from context "xp".
2741 * "xp" must have been set by set_cmd_context().
2742 * xp->xp_pattern points into "str", to where the text that is to be expanded
2743 * starts.
2744 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2745 * cursor.
2746 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2747 * key that triggered expansion literally.
2748 * Returns EXPAND_OK otherwise.
2749 */
2750 int
2751expand_cmdline(
2752 expand_T *xp,
2753 char_u *str, // start of command line
2754 int col, // position of cursor
2755 int *matchcount, // return: nr of matches
2756 char_u ***matches) // return: array of pointers to matches
2757{
2758 char_u *file_str = NULL;
2759 int options = WILD_ADD_SLASH|WILD_SILENT;
2760
2761 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2762 {
2763 beep_flush();
2764 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2765 }
2766 if (xp->xp_context == EXPAND_NOTHING)
2767 {
2768 // Caller can use the character as a normal char instead
2769 return EXPAND_NOTHING;
2770 }
2771
2772 // add star to file name, or convert to regexp if not exp. files.
2773 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002774 if (cmdline_fuzzy_completion_supported(xp))
2775 // If fuzzy matching, don't modify the search string
2776 file_str = vim_strsave(xp->xp_pattern);
2777 else
2778 {
2779 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2780 if (file_str == NULL)
2781 return EXPAND_UNSUCCESSFUL;
2782 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002783
2784 if (p_wic)
2785 options += WILD_ICASE;
2786
2787 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002788 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002789 {
2790 *matchcount = 0;
2791 *matches = NULL;
2792 }
2793 vim_free(file_str);
2794
2795 return EXPAND_OK;
2796}
2797
Bram Moolenaar66b51422019-08-18 21:44:12 +02002798/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002799 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002800 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002801 */
2802 static int
2803expand_files_and_dirs(
2804 expand_T *xp,
2805 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002806 char_u ***matches,
2807 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002808 int flags,
2809 int options)
2810{
2811 int free_pat = FALSE;
2812 int i;
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002813 int ret = FAIL;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002814
2815 // for ":set path=" and ":set tags=" halve backslashes for escaped
2816 // space
2817 if (xp->xp_backslash != XP_BS_NONE)
2818 {
2819 free_pat = TRUE;
2820 pat = vim_strsave(pat);
2821 for (i = 0; pat[i]; ++i)
2822 if (pat[i] == '\\')
2823 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002824 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002825 && pat[i + 1] == '\\'
2826 && pat[i + 2] == '\\'
2827 && pat[i + 3] == ' ')
2828 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002829 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002830 && pat[i + 1] == ' ')
2831 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002832 else if ((xp->xp_backslash & XP_BS_COMMA)
2833 && pat[i + 1] == '\\'
2834 && pat[i + 2] == ',')
2835 STRMOVE(pat + i, pat + i + 2);
2836#ifdef BACKSLASH_IN_FILENAME
2837 else if ((xp->xp_backslash & XP_BS_COMMA)
2838 && pat[i + 1] == ',')
2839 STRMOVE(pat + i, pat + i + 1);
2840#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002841 }
2842 }
2843
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002844 if (xp->xp_context == EXPAND_FINDFUNC)
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002845 {
2846#ifdef FEAT_EVAL
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002847 ret = expand_findfunc(pat, matches, numMatches);
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002848#endif
2849 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002850 else
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002851 {
2852 if (xp->xp_context == EXPAND_FILES)
2853 flags |= EW_FILE;
2854 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2855 flags |= (EW_FILE | EW_PATH);
2856 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2857 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
2858 else
2859 flags = (flags | EW_DIR) & ~EW_FILE;
2860 if (options & WILD_ICASE)
2861 flags |= EW_ICASE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002862
Yegappan Lakshmanan2f6efac2024-10-23 21:06:10 +02002863 // Expand wildcards, supporting %:h and the like.
2864 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
2865 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002866 if (free_pat)
2867 vim_free(pat);
2868#ifdef BACKSLASH_IN_FILENAME
2869 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2870 {
2871 int j;
2872
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002873 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002874 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002875 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002876
2877 while (*ptr != NUL)
2878 {
2879 if (p_csl[0] == 's' && *ptr == '\\')
2880 *ptr = '/';
2881 else if (p_csl[0] == 'b' && *ptr == '/')
2882 *ptr = '\\';
2883 ptr += (*mb_ptr2len)(ptr);
2884 }
2885 }
2886 }
2887#endif
2888 return ret;
2889}
2890
2891/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002892 * Function given to ExpandGeneric() to obtain the possible arguments of the
2893 * ":behave {mswin,xterm}" command.
2894 */
2895 static char_u *
2896get_behave_arg(expand_T *xp UNUSED, int idx)
2897{
2898 if (idx == 0)
2899 return (char_u *)"mswin";
2900 if (idx == 1)
2901 return (char_u *)"xterm";
2902 return NULL;
2903}
2904
K.Takata161b6ac2022-11-14 15:31:07 +00002905#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002906/*
2907 * Function given to ExpandGeneric() to obtain the possible arguments of the
2908 * ":breakadd {expr, file, func, here}" command.
2909 * ":breakdel {func, file, here}" command.
2910 */
2911 static char_u *
2912get_breakadd_arg(expand_T *xp UNUSED, int idx)
2913{
2914 char *opts[] = {"expr", "file", "func", "here"};
2915
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002916 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002917 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002918 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002919 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2920 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002921 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2922 {
2923 // breakdel {func, file, here}
2924 if (idx <= 2)
2925 return (char_u *)opts[idx + 1];
2926 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002927 else
2928 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002929 // profdel {func, file}
2930 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002931 return (char_u *)opts[idx + 1];
2932 }
2933 }
2934 return NULL;
2935}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002936
2937/*
2938 * Function given to ExpandGeneric() to obtain the possible arguments for the
2939 * ":scriptnames" command.
2940 */
2941 static char_u *
2942get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2943{
2944 scriptitem_T *si;
2945
2946 if (!SCRIPT_ID_VALID(idx + 1))
2947 return NULL;
2948
2949 si = SCRIPT_ITEM(idx + 1);
2950 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2951 return NameBuff;
2952}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002953#endif
2954
Bram Moolenaard0190392019-08-23 21:17:35 +02002955/*
2956 * Function given to ExpandGeneric() to obtain the possible arguments of the
2957 * ":messages {clear}" command.
2958 */
2959 static char_u *
2960get_messages_arg(expand_T *xp UNUSED, int idx)
2961{
2962 if (idx == 0)
2963 return (char_u *)"clear";
2964 return NULL;
2965}
2966
2967 static char_u *
2968get_mapclear_arg(expand_T *xp UNUSED, int idx)
2969{
2970 if (idx == 0)
2971 return (char_u *)"<buffer>";
2972 return NULL;
2973}
2974
2975/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002976 * Do the expansion based on xp->xp_context and 'rmp'.
2977 */
2978 static int
2979ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002980 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002981 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002982 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002983 char_u ***matches,
2984 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002985{
2986 static struct expgen
2987 {
2988 int context;
2989 char_u *((*func)(expand_T *, int));
2990 int ic;
2991 int escaped;
2992 } tab[] =
2993 {
2994 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2995 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2996 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2997 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2998 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2999 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
3000 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
3001 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
3002 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
3003 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003004#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003005 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3006 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3007 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3008 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3009 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003010#endif
3011#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003012 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3013 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003014#endif
3015#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003016 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003017#endif
3018#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003019 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003020#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003021 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3022 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3023 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003024#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003025 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003026#endif
3027#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003028 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003029#endif
3030#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003031 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003032#endif
3033#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003034 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3035 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003036#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003037 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3038 {EXPAND_USER, get_users, TRUE, FALSE},
3039 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003040#ifdef FEAT_EVAL
3041 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003042 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003043#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003044 };
3045 int i;
3046 int ret = FAIL;
3047
3048 // Find a context in the table and call the ExpandGeneric() with the
3049 // right function to do the expansion.
3050 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3051 {
3052 if (xp->xp_context == tab[i].context)
3053 {
3054 if (tab[i].ic)
3055 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003056 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3057 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003058 break;
3059 }
3060 }
3061
3062 return ret;
3063}
3064
3065/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003066 * Map wild expand options to flags for expand_wildcards()
3067 */
3068 static int
3069map_wildopts_to_ewflags(int options)
3070{
3071 int flags;
3072
3073 flags = EW_DIR; // include directories
3074 if (options & WILD_LIST_NOTFOUND)
3075 flags |= EW_NOTFOUND;
3076 if (options & WILD_ADD_SLASH)
3077 flags |= EW_ADDSLASH;
3078 if (options & WILD_KEEP_ALL)
3079 flags |= EW_KEEPALL;
3080 if (options & WILD_SILENT)
3081 flags |= EW_SILENT;
3082 if (options & WILD_NOERROR)
3083 flags |= EW_NOERROR;
3084 if (options & WILD_ALLLINKS)
3085 flags |= EW_ALLLINKS;
3086
3087 return flags;
3088}
3089
3090/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003091 * Do the expansion based on xp->xp_context and "pat".
3092 */
3093 static int
3094ExpandFromContext(
3095 expand_T *xp,
3096 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003097 char_u ***matches,
3098 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003099 int options) // WILD_ flags
3100{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003101 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003102 int ret;
3103 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003104 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003105 int fuzzy = cmdline_fuzzy_complete(pat)
3106 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003107
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003108 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003109
3110 if (xp->xp_context == EXPAND_FILES
3111 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003112 || xp->xp_context == EXPAND_FILES_IN_PATH
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01003113 || xp->xp_context == EXPAND_FINDFUNC
LemonBoya20bf692024-07-11 22:35:53 +02003114 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003115 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3116 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003117
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003118 *matches = (char_u **)"";
3119 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003120 if (xp->xp_context == EXPAND_HELP)
3121 {
3122 // With an empty argument we would get all the help tags, which is
3123 // very slow. Get matches for "help" instead.
3124 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003125 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003126 {
3127#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003128 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003129#endif
3130 return OK;
3131 }
3132 return FAIL;
3133 }
3134
Bram Moolenaar66b51422019-08-18 21:44:12 +02003135 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003136 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003137 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003138 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003139 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003140 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003141#ifdef FEAT_DIFF
3142 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003143 return ExpandBufnames(pat, numMatches, matches,
3144 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003145#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003146 if (xp->xp_context == EXPAND_TAGS
3147 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3149 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003150 if (xp->xp_context == EXPAND_COLORS)
3151 {
3152 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003153 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003154 directories);
3155 }
3156 if (xp->xp_context == EXPAND_COMPILER)
3157 {
3158 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003159 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003160 }
3161 if (xp->xp_context == EXPAND_OWNSYNTAX)
3162 {
3163 char *directories[] = {"syntax", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003164 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003165 }
3166 if (xp->xp_context == EXPAND_FILETYPE)
3167 {
3168 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003169 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003170 }
Doug Kearns81642d92024-01-04 22:37:44 +01003171#ifdef FEAT_KEYMAP
3172 if (xp->xp_context == EXPAND_KEYMAP)
3173 {
3174 char *directories[] = {"keymap", NULL};
3175 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3176 }
3177#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003178#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003179 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003180 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003181#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003182 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003183 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003184 if (xp->xp_context == EXPAND_RUNTIME)
3185 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003186
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003187 // When expanding a function name starting with s:, match the <SNR>nr_
3188 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003189 if ((xp->xp_context == EXPAND_USER_FUNC
3190 || xp->xp_context == EXPAND_DISASSEMBLE)
3191 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003192 {
3193 int len = (int)STRLEN(pat) + 20;
3194
3195 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003196 if (tofree == NULL)
3197 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003198 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003199 pat = tofree;
3200 }
3201
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003202 if (!fuzzy)
3203 {
3204 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3205 if (regmatch.regprog == NULL)
3206 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003207
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003208 // set ignore-case according to p_ic, p_scs and pat
3209 regmatch.rm_ic = ignorecase(pat);
3210 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003211
3212 if (xp->xp_context == EXPAND_SETTINGS
3213 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003214 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003215 else if (xp->xp_context == EXPAND_STRING_SETTING)
3216 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3217 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3218 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003219 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003220 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003221 else if (xp->xp_context == EXPAND_ARGOPT)
3222 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3223#if defined(FEAT_TERMINAL)
3224 else if (xp->xp_context == EXPAND_TERMINALOPT)
3225 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3226#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003227#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003228 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003229 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003230#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003231 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003232 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003233
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003234 if (!fuzzy)
3235 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003236 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003237
3238 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003239}
3240
Bram Moolenaar66b51422019-08-18 21:44:12 +02003241/*
3242 * Expand a list of names.
3243 *
3244 * Generic function for command line completion. It calls a function to
3245 * obtain strings, one by one. The strings are matched against a regexp
3246 * program. Matching strings are copied into an array, which is returned.
3247 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003248 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3249 * is used.
3250 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003251 * Returns OK when no problems encountered, FAIL for error (out of memory).
3252 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003253 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003254ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003255 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003256 expand_T *xp,
3257 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003258 char_u ***matches,
3259 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003260 char_u *((*func)(expand_T *, int)),
3261 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003262 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003263{
3264 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003265 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003266 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003267 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003268 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003269 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003270 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003271 int sort_matches = FALSE;
3272 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003273
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003274 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003275 *matches = NULL;
3276 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003277
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003278 if (!fuzzy)
3279 ga_init2(&ga, sizeof(char *), 30);
3280 else
3281 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3282
3283 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003284 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003285 str = (*func)(xp, i);
3286 if (str == NULL) // end of list
3287 break;
3288 if (*str == NUL) // skip empty strings
3289 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003290
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003291 if (xp->xp_pattern[0] != NUL)
3292 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003293 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003294 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003295 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003296 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003297 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003298 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003299 }
3300 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003301 else
3302 match = TRUE;
3303
3304 if (!match)
3305 continue;
3306
3307 if (escaped)
3308 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3309 else
3310 str = vim_strsave(str);
3311 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003312 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003313 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003314 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003315 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003316 return FAIL;
3317 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003318 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003319 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003320 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003321
3322 if (ga_grow(&ga, 1) == FAIL)
3323 {
3324 vim_free(str);
3325 break;
3326 }
3327
3328 if (fuzzy)
3329 {
3330 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3331 fuzmatch->idx = ga.ga_len;
3332 fuzmatch->str = str;
3333 fuzmatch->score = score;
3334 }
3335 else
3336 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3337
K.Takata161b6ac2022-11-14 15:31:07 +00003338#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003339 if (func == get_menu_names)
3340 {
3341 // test for separator added by get_menu_names()
3342 str += STRLEN(str) - 1;
3343 if (*str == '\001')
3344 *str = '.';
3345 }
K.Takata161b6ac2022-11-14 15:31:07 +00003346#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003347
3348 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003349 }
3350
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003351 if (ga.ga_len == 0)
3352 return OK;
3353
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003354 // sort the matches when using regular expression matching and sorting
3355 // applies to the completion context. Menus and scriptnames should be kept
3356 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003357 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003358 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003359 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003360 && xp->xp_context != EXPAND_SCRIPTNAMES
3361 && xp->xp_context != EXPAND_ARGOPT
3362 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003363 sort_matches = TRUE;
3364
3365 // <SNR> functions should be sorted to the end.
3366 if (xp->xp_context == EXPAND_EXPRESSION
3367 || xp->xp_context == EXPAND_FUNCTIONS
3368 || xp->xp_context == EXPAND_USER_FUNC
3369 || xp->xp_context == EXPAND_DISASSEMBLE)
3370 funcsort = TRUE;
3371
3372 // Sort the matches.
3373 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003374 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003375 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003376 // <SNR> functions should be sorted to the end.
3377 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3378 sort_func_compare);
3379 else
3380 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3381 }
3382
3383 if (!fuzzy)
3384 {
3385 *matches = ga.ga_data;
3386 *numMatches = ga.ga_len;
3387 }
3388 else
3389 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003390 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3391 funcsort) == FAIL)
3392 return FAIL;
3393 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003394 }
3395
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003396#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003397 // Reset the variables used for special highlight names expansion, so that
3398 // they don't show up when getting normal highlight names by ID.
3399 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003400#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003401
Bram Moolenaar66b51422019-08-18 21:44:12 +02003402 return OK;
3403}
3404
3405/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003406 * Expand shell command matches in one directory of $PATH.
3407 */
3408 static void
3409expand_shellcmd_onedir(
3410 char_u *buf,
3411 char_u *s,
3412 size_t l,
3413 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003414 char_u ***matches,
3415 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003416 int flags,
3417 hashtab_T *ht,
3418 garray_T *gap)
3419{
3420 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003421 hash_T hash;
3422 hashitem_T *hi;
3423
3424 vim_strncpy(buf, s, l);
3425 add_pathsep(buf);
3426 l = STRLEN(buf);
3427 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3428
3429 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003430 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003431 if (ret != OK)
3432 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003433
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003434 if (ga_grow(gap, *numMatches) == FAIL)
3435 {
3436 FreeWild(*numMatches, *matches);
3437 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003438 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003439
3440 for (int i = 0; i < *numMatches; ++i)
3441 {
3442 char_u *name = (*matches)[i];
3443
3444 if (STRLEN(name) > l)
3445 {
3446 // Check if this name was already found.
3447 hash = hash_hash(name + l);
3448 hi = hash_lookup(ht, name + l, hash);
3449 if (HASHITEM_EMPTY(hi))
3450 {
3451 // Remove the path that was prepended.
3452 STRMOVE(name, name + l);
3453 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3454 hash_add_item(ht, hi, name, hash);
3455 name = NULL;
3456 }
3457 }
3458 vim_free(name);
3459 }
3460 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003461}
3462
3463/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003464 * Complete a shell command.
3465 * Returns FAIL or OK;
3466 */
3467 static int
3468expand_shellcmd(
3469 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003470 char_u ***matches, // return: array with matches
3471 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003472 int flagsarg) // EW_ flags
3473{
3474 char_u *pat;
3475 int i;
3476 char_u *path = NULL;
3477 int mustfree = FALSE;
3478 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003479 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480 size_t l;
3481 char_u *s, *e;
3482 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003483 int did_curdir = FALSE;
3484 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003485
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003486 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003487 if (buf == NULL)
3488 return FAIL;
3489
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003490 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003491 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003492 if (pat == NULL)
3493 {
3494 vim_free(buf);
3495 return FAIL;
3496 }
3497
Bram Moolenaar66b51422019-08-18 21:44:12 +02003498 for (i = 0; pat[i]; ++i)
3499 if (pat[i] == '\\' && pat[i + 1] == ' ')
3500 STRMOVE(pat + i, pat + i + 1);
3501
3502 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3503
3504 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3505 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3506 path = (char_u *)".";
3507 else
3508 {
3509 // For an absolute name we don't use $PATH.
3510 if (!mch_isFullName(pat))
3511 path = vim_getenv((char_u *)"PATH", &mustfree);
3512 if (path == NULL)
3513 path = (char_u *)"";
3514 }
3515
3516 // Go over all directories in $PATH. Expand matches in that directory and
3517 // collect them in "ga". When "." is not in $PATH also expand for the
3518 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003519 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003520 hash_init(&found_ht);
3521 for (s = path; ; s = e)
3522 {
K.Takata161b6ac2022-11-14 15:31:07 +00003523#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003524 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003525#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003526 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003527#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003528 if (e == NULL)
3529 e = s + STRLEN(s);
3530
3531 if (*s == NUL)
3532 {
3533 if (did_curdir)
3534 break;
3535 // Find directories in the current directory, path is empty.
3536 did_curdir = TRUE;
3537 flags |= EW_DIR;
3538 }
3539 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3540 {
3541 did_curdir = TRUE;
3542 flags |= EW_DIR;
3543 }
3544 else
3545 // Do not match directories inside a $PATH item.
3546 flags &= ~EW_DIR;
3547
3548 l = e - s;
3549 if (l > MAXPATHL - 5)
3550 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003551
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003552 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003553 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003554
Bram Moolenaar66b51422019-08-18 21:44:12 +02003555 if (*e != NUL)
3556 ++e;
3557 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003558 *matches = ga.ga_data;
3559 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003560
3561 vim_free(buf);
3562 vim_free(pat);
3563 if (mustfree)
3564 vim_free(path);
3565 hash_clear(&found_ht);
3566 return OK;
3567}
3568
K.Takata161b6ac2022-11-14 15:31:07 +00003569#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003570/*
3571 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003572 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003573 */
3574 static void *
3575call_user_expand_func(
3576 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003577 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003578{
3579 cmdline_info_T *ccline = get_cmdline_info();
3580 int keep = 0;
3581 typval_T args[4];
3582 sctx_T save_current_sctx = current_sctx;
3583 char_u *pat = NULL;
3584 void *ret;
3585
3586 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3587 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003588
3589 if (ccline->cmdbuff != NULL)
3590 {
3591 keep = ccline->cmdbuff[ccline->cmdlen];
3592 ccline->cmdbuff[ccline->cmdlen] = 0;
3593 }
3594
3595 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3596
3597 args[0].v_type = VAR_STRING;
3598 args[0].vval.v_string = pat;
3599 args[1].v_type = VAR_STRING;
3600 args[1].vval.v_string = xp->xp_line;
3601 args[2].v_type = VAR_NUMBER;
3602 args[2].vval.v_number = xp->xp_col;
3603 args[3].v_type = VAR_UNKNOWN;
3604
3605 current_sctx = xp->xp_script_ctx;
3606
3607 ret = user_expand_func(xp->xp_arg, 3, args);
3608
3609 current_sctx = save_current_sctx;
3610 if (ccline->cmdbuff != NULL)
3611 ccline->cmdbuff[ccline->cmdlen] = keep;
3612
3613 vim_free(pat);
3614 return ret;
3615}
3616
3617/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003618 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3619 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003620 */
3621 static int
3622ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003623 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003624 expand_T *xp,
3625 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003626 char_u ***matches,
3627 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003628{
3629 char_u *retstr;
3630 char_u *s;
3631 char_u *e;
3632 int keep;
3633 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003634 int fuzzy;
3635 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003636 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003637
3638 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003639 *matches = NULL;
3640 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003641
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003642 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003643 if (retstr == NULL)
3644 return FAIL;
3645
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003646 if (!fuzzy)
3647 ga_init2(&ga, sizeof(char *), 3);
3648 else
3649 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3650
Bram Moolenaar66b51422019-08-18 21:44:12 +02003651 for (s = retstr; *s != NUL; s = e)
3652 {
3653 e = vim_strchr(s, '\n');
3654 if (e == NULL)
3655 e = s + STRLEN(s);
3656 keep = *e;
3657 *e = NUL;
3658
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003659 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003660 {
3661 if (!fuzzy)
3662 match = vim_regexec(regmatch, s, (colnr_T)0);
3663 else
3664 {
3665 score = fuzzy_match_str(s, pat);
3666 match = (score != 0);
3667 }
3668 }
3669 else
3670 match = TRUE; // match everything
3671
Bram Moolenaar66b51422019-08-18 21:44:12 +02003672 *e = keep;
3673
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003674 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003675 {
3676 if (ga_grow(&ga, 1) == FAIL)
3677 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003678 if (!fuzzy)
3679 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3680 else
3681 {
3682 fuzmatch_str_T *fuzmatch =
3683 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003684 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003685 fuzmatch->str = vim_strnsave(s, e - s);
3686 fuzmatch->score = score;
3687 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003688 ++ga.ga_len;
3689 }
3690
3691 if (*e != NUL)
3692 ++e;
3693 }
3694 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003695
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003696 if (ga.ga_len == 0)
3697 return OK;
3698
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003699 if (!fuzzy)
3700 {
3701 *matches = ga.ga_data;
3702 *numMatches = ga.ga_len;
3703 }
3704 else
3705 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003706 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3707 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003708 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003709 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003710 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003711 return OK;
3712}
3713
3714/*
3715 * Expand names with a list returned by a function defined by the user.
3716 */
3717 static int
3718ExpandUserList(
3719 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003720 char_u ***matches,
3721 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003722{
3723 list_T *retlist;
3724 listitem_T *li;
3725 garray_T ga;
3726
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003727 *matches = NULL;
3728 *numMatches = 0;
3729 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003730 if (retlist == NULL)
3731 return FAIL;
3732
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003733 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003734 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003735 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003736 {
3737 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3738 continue; // Skip non-string items and empty strings
3739
3740 if (ga_grow(&ga, 1) == FAIL)
3741 break;
3742
3743 ((char_u **)ga.ga_data)[ga.ga_len] =
3744 vim_strsave(li->li_tv.vval.v_string);
3745 ++ga.ga_len;
3746 }
3747 list_unref(retlist);
3748
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003749 *matches = ga.ga_data;
3750 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751 return OK;
3752}
K.Takata161b6ac2022-11-14 15:31:07 +00003753#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754
3755/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003756 * Expand "file" for all comma-separated directories in "path".
3757 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003758 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003759 */
3760 void
3761globpath(
3762 char_u *path,
3763 char_u *file,
3764 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003765 int expand_options,
3766 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003767{
3768 expand_T xpc;
3769 char_u *buf;
3770 int i;
3771 int num_p;
3772 char_u **p;
3773
3774 buf = alloc(MAXPATHL);
3775 if (buf == NULL)
3776 return;
3777
3778 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003779 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003780
3781 // Loop over all entries in {path}.
3782 while (*path != NUL)
3783 {
3784 // Copy one item of the path to buf[] and concatenate the file name.
3785 copy_option_part(&path, buf, MAXPATHL, ",");
3786 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3787 {
K.Takata161b6ac2022-11-14 15:31:07 +00003788#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003789 // Using the platform's path separator (\) makes vim incorrectly
3790 // treat it as an escape character, use '/' instead.
3791 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3792 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003793#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003794 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003795#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003796 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003797 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003798 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3799 {
3800 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3801
3802 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003803 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003804 for (i = 0; i < num_p; ++i)
3805 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003806 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003807 ++ga->ga_len;
3808 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003809 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003810 }
3811 }
3812 }
3813
3814 vim_free(buf);
3815}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003816
Bram Moolenaareadee482020-09-04 15:37:31 +02003817/*
3818 * Translate some keys pressed when 'wildmenu' is used.
3819 */
3820 int
3821wildmenu_translate_key(
3822 cmdline_info_T *cclp,
3823 int key,
3824 expand_T *xp,
3825 int did_wild_list)
3826{
3827 int c = key;
3828
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003829 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003830 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003831 // When the popup menu is used for cmdline completion:
3832 // Up : go to the previous item in the menu
3833 // Down : go to the next item in the menu
3834 // Left : go to the parent directory
3835 // Right: list the files in the selected directory
3836 switch (c)
3837 {
3838 case K_UP: c = K_LEFT; break;
3839 case K_DOWN: c = K_RIGHT; break;
3840 case K_LEFT: c = K_UP; break;
3841 case K_RIGHT: c = K_DOWN; break;
3842 default: break;
3843 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003844 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003845
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003846 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003847 {
3848 if (c == K_LEFT)
3849 c = Ctrl_P;
3850 else if (c == K_RIGHT)
3851 c = Ctrl_N;
3852 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003853
Bram Moolenaareadee482020-09-04 15:37:31 +02003854 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003855 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003856 && cclp->cmdpos > 1
3857 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3858 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3859 && (c == '\n' || c == '\r' || c == K_KENTER))
3860 c = K_DOWN;
3861
3862 return c;
3863}
3864
3865/*
3866 * Delete characters on the command line, from "from" to the current
3867 * position.
3868 */
3869 static void
3870cmdline_del(cmdline_info_T *cclp, int from)
3871{
3872 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3873 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3874 cclp->cmdlen -= cclp->cmdpos - from;
3875 cclp->cmdpos = from;
3876}
3877
3878/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003879 * Handle a key pressed when the wild menu for the menu names
3880 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003881 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003882 static int
3883wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003884{
Bram Moolenaareadee482020-09-04 15:37:31 +02003885 int i;
3886 int j;
3887
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003888 // Hitting <Down> after "emenu Name.": complete submenu
3889 if (key == K_DOWN && cclp->cmdpos > 0
3890 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003891 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003892 key = p_wc;
3893 KeyTyped = TRUE; // in case the key was mapped
3894 }
3895 else if (key == K_UP)
3896 {
3897 // Hitting <Up>: Remove one submenu name in front of the
3898 // cursor
3899 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003900
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003901 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3902 i = 0;
3903 while (--j > 0)
3904 {
3905 // check for start of menu name
3906 if (cclp->cmdbuff[j] == ' '
3907 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003908 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003909 i = j + 1;
3910 break;
3911 }
3912 // check for start of submenu name
3913 if (cclp->cmdbuff[j] == '.'
3914 && cclp->cmdbuff[j - 1] != '\\')
3915 {
3916 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003917 {
3918 i = j + 1;
3919 break;
3920 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003921 else
3922 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003923 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003924 }
3925 if (i > 0)
3926 cmdline_del(cclp, i);
3927 key = p_wc;
3928 KeyTyped = TRUE; // in case the key was mapped
3929 xp->xp_context = EXPAND_NOTHING;
3930 }
3931
3932 return key;
3933}
3934
3935/*
3936 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3937 * directory names (EXPAND_DIRECTORIES) or shell command names
3938 * (EXPAND_SHELLCMD) is displayed.
3939 */
3940 static int
3941wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3942{
3943 int i;
3944 int j;
3945 char_u upseg[5];
3946
3947 upseg[0] = PATHSEP;
3948 upseg[1] = '.';
3949 upseg[2] = '.';
3950 upseg[3] = PATHSEP;
3951 upseg[4] = NUL;
3952
3953 if (key == K_DOWN
3954 && cclp->cmdpos > 0
3955 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3956 && (cclp->cmdpos < 3
3957 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3958 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3959 {
3960 // go down a directory
3961 key = p_wc;
3962 KeyTyped = TRUE; // in case the key was mapped
3963 }
3964 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3965 {
3966 // If in a direct ancestor, strip off one ../ to go down
3967 int found = FALSE;
3968
3969 j = cclp->cmdpos;
3970 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3971 while (--j > i)
3972 {
3973 if (has_mbyte)
3974 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3975 if (vim_ispathsep(cclp->cmdbuff[j]))
3976 {
3977 found = TRUE;
3978 break;
3979 }
3980 }
3981 if (found
3982 && cclp->cmdbuff[j - 1] == '.'
3983 && cclp->cmdbuff[j - 2] == '.'
3984 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3985 {
3986 cmdline_del(cclp, j - 2);
3987 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003988 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003989 }
3990 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003991 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003992 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003993 // go up a directory
3994 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003995
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003996 j = cclp->cmdpos - 1;
3997 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3998 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003999 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004000 if (has_mbyte)
4001 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
4002 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00004003#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004004 && vim_strchr((char_u *)" *?[{`$%#",
4005 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004006#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004007 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004008 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004009 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004010 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004011 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004012 break;
4013 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004014 else
4015 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004016 }
4017 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004018
4019 if (!found)
4020 j = i;
4021 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4022 j += 4;
4023 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4024 && j == i)
4025 j += 3;
4026 else
4027 j = 0;
4028 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004029 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004030 // TODO this is only for DOS/UNIX systems - need to put in
4031 // machine-specific stuff here and in upseg init
4032 cmdline_del(cclp, j);
4033 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004034 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004035 else if (cclp->cmdpos > i)
4036 cmdline_del(cclp, i);
4037
4038 // Now complete in the new directory. Set KeyTyped in case the
4039 // Up key came from a mapping.
4040 key = p_wc;
4041 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004042 }
4043
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004044 return key;
4045}
4046
4047/*
4048 * Handle a key pressed when the wild menu is displayed
4049 */
4050 int
4051wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4052{
4053 if (xp->xp_context == EXPAND_MENUNAMES)
4054 return wildmenu_process_key_menunames(cclp, key, xp);
4055 else if ((xp->xp_context == EXPAND_FILES
4056 || xp->xp_context == EXPAND_DIRECTORIES
4057 || xp->xp_context == EXPAND_SHELLCMD))
4058 return wildmenu_process_key_filenames(cclp, key, xp);
4059
4060 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004061}
4062
4063/*
4064 * Free expanded names when finished walking through the matches
4065 */
4066 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004067wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004068{
4069 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004070
4071 if (!p_wmnu || wild_menu_showing == 0)
4072 return;
4073
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004074#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004075 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004076 if (cclp->input_fn)
4077 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004078#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004079
4080 if (wild_menu_showing == WM_SCROLLED)
4081 {
4082 // Entered command line, move it up
4083 cmdline_row--;
4084 redrawcmd();
4085 }
4086 else if (save_p_ls != -1)
4087 {
4088 // restore 'laststatus' and 'winminheight'
4089 p_ls = save_p_ls;
4090 p_wmh = save_p_wmh;
4091 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004092 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004093 redrawcmd();
4094 save_p_ls = -1;
4095 }
4096 else
4097 {
4098 win_redraw_last_status(topframe);
4099 redraw_statuslines();
4100 }
4101 KeyTyped = skt;
4102 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004103#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004104 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004105 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004106#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004107}
Bram Moolenaareadee482020-09-04 15:37:31 +02004108
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004109#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004110/*
4111 * "getcompletion()" function
4112 */
4113 void
4114f_getcompletion(typval_T *argvars, typval_T *rettv)
4115{
4116 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004117 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004118 expand_T xpc;
4119 int filtered = FALSE;
4120 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004121 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004122
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004123 if (in_vim9script()
4124 && (check_for_string_arg(argvars, 0) == FAIL
4125 || check_for_string_arg(argvars, 1) == FAIL
4126 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4127 return;
4128
ii144785fe02021-11-21 12:13:56 +00004129 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004130 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004131 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004132 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004133
4134 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004135 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004136
4137 if (p_wic)
4138 options |= WILD_ICASE;
4139
4140 // For filtered results, 'wildignore' is used
4141 if (!filtered)
4142 options |= WILD_KEEP_ALL;
4143
4144 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004145 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004146 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004147 int cmdline_len = (int)STRLEN(pat);
4148 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004149 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004150 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004151 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004152 else
4153 {
ii144785fe02021-11-21 12:13:56 +00004154 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004155 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004156 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004157
4158 xpc.xp_context = cmdcomplete_str_to_type(type);
4159 if (xpc.xp_context == EXPAND_NOTHING)
4160 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004161 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004162 return;
4163 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004164
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004165 if (xpc.xp_context == EXPAND_USER_DEFINED)
4166 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004167 // Must be "custom,funcname" pattern
4168 if (STRNCMP(type, "custom,", 7) != 0)
4169 {
4170 semsg(_(e_invalid_argument_str), type);
4171 return;
4172 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004173
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004174 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004175 }
4176
4177 if (xpc.xp_context == EXPAND_USER_LIST)
4178 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004179 // Must be "customlist,funcname" pattern
4180 if (STRNCMP(type, "customlist,", 11) != 0)
4181 {
4182 semsg(_(e_invalid_argument_str), type);
4183 return;
4184 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004185
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004186 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004187 }
4188
Bram Moolenaar66b51422019-08-18 21:44:12 +02004189# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004190 if (xpc.xp_context == EXPAND_MENUS)
4191 {
4192 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4193 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4194 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004195# endif
4196# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004197 if (xpc.xp_context == EXPAND_CSCOPE)
4198 {
4199 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4200 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4201 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004202# endif
4203# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004204 if (xpc.xp_context == EXPAND_SIGN)
4205 {
4206 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4207 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4208 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004209# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004210 if (xpc.xp_context == EXPAND_RUNTIME)
4211 {
4212 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4213 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4214 }
zeertzjq85f36d62024-10-10 19:14:13 +02004215 if (xpc.xp_context == EXPAND_SHELLCMDLINE)
4216 {
4217 int context = EXPAND_SHELLCMDLINE;
4218 set_context_for_wildcard_arg(NULL, xpc.xp_pattern, FALSE, &xpc,
4219 &context);
4220 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4221 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004222 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004223
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004224 if (cmdline_fuzzy_completion_supported(&xpc))
4225 // when fuzzy matching, don't modify the search string
4226 pat = vim_strsave(xpc.xp_pattern);
4227 else
4228 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4229
Bram Moolenaar93a10962022-06-16 11:42:09 +01004230 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004231 {
4232 int i;
4233
4234 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4235
4236 for (i = 0; i < xpc.xp_numfiles; i++)
4237 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4238 }
4239 vim_free(pat);
4240 ExpandCleanup(&xpc);
4241}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004242#endif // FEAT_EVAL