blob: c7c42abffa82b6d955419010691af7f799695147 [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
53 && xp->xp_context != EXPAND_HELP
Doug Kearns81642d92024-01-04 22:37:44 +010054 && xp->xp_context != EXPAND_KEYMAP
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000055 && xp->xp_context != EXPAND_OLD_SETTING
Yee Cheng Chin900894b2023-09-29 20:42:32 +020056 && xp->xp_context != EXPAND_STRING_SETTING
57 && xp->xp_context != EXPAND_SETTING_SUBTRACT
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000058 && xp->xp_context != EXPAND_OWNSYNTAX
59 && xp->xp_context != EXPAND_PACKADD
roota6759382023-01-21 21:56:06 +000060 && xp->xp_context != EXPAND_RUNTIME
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000061 && xp->xp_context != EXPAND_SHELLCMD
Ruslan Russkikh0407d622024-10-08 22:21:05 +020062 && xp->xp_context != EXPAND_SHELLCMDLINE
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000063 && xp->xp_context != EXPAND_TAGS
64 && xp->xp_context != EXPAND_TAGS_LISTFILES
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000065 && xp->xp_context != EXPAND_USER_LIST);
66}
67
68/*
69 * Returns TRUE if fuzzy completion for cmdline completion is enabled and
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +000070 * 'fuzzystr' is not empty. If search pattern is empty, then don't use fuzzy
71 * matching.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +000072 */
73 int
74cmdline_fuzzy_complete(char_u *fuzzystr)
75{
76 return vim_strchr(p_wop, WOP_FUZZY) != NULL && *fuzzystr != NUL;
77}
78
79/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +000080 * sort function for the completion matches.
81 * <SNR> functions should be sorted to the end.
82 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020083 static int
84sort_func_compare(const void *s1, const void *s2)
85{
86 char_u *p1 = *(char_u **)s1;
87 char_u *p2 = *(char_u **)s2;
88
89 if (*p1 != '<' && *p2 == '<') return -1;
90 if (*p1 == '<' && *p2 != '<') return 1;
91 return STRCMP(p1, p2);
92}
Bram Moolenaar66b51422019-08-18 21:44:12 +020093
Yegappan Lakshmanan4d03d872022-02-13 11:45:09 +000094/*
95 * Escape special characters in the cmdline completion matches.
96 */
Bram Moolenaar66b51422019-08-18 21:44:12 +020097 static void
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +000098wildescape(
99 expand_T *xp,
100 char_u *str,
101 int numfiles,
102 char_u **files)
103{
104 char_u *p;
105 int vse_what = xp->xp_context == EXPAND_BUFFERS
106 ? VSE_BUFFER : VSE_NONE;
107
108 if (xp->xp_context == EXPAND_FILES
109 || xp->xp_context == EXPAND_FILES_IN_PATH
110 || xp->xp_context == EXPAND_SHELLCMD
111 || xp->xp_context == EXPAND_BUFFERS
LemonBoya20bf692024-07-11 22:35:53 +0200112 || xp->xp_context == EXPAND_DIRECTORIES
113 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000114 {
115 // Insert a backslash into a file name before a space, \, %, #
116 // and wildmatch characters, except '~'.
117 for (int i = 0; i < numfiles; ++i)
118 {
119 // for ":set path=" we need to escape spaces twice
Yee Cheng Chin54844852023-10-09 18:12:31 +0200120 if (xp->xp_backslash & XP_BS_THREE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000121 {
Yee Cheng Chin54844852023-10-09 18:12:31 +0200122 char *pat = (xp->xp_backslash & XP_BS_COMMA) ? " ," : " ";
123 p = vim_strsave_escaped(files[i], (char_u *)pat);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000124 if (p != NULL)
125 {
126 vim_free(files[i]);
127 files[i] = p;
128#if defined(BACKSLASH_IN_FILENAME)
129 p = vim_strsave_escaped(files[i], (char_u *)" ");
130 if (p != NULL)
131 {
132 vim_free(files[i]);
133 files[i] = p;
134 }
135#endif
136 }
137 }
Yee Cheng Chin54844852023-10-09 18:12:31 +0200138 else if (xp->xp_backslash & XP_BS_COMMA)
139 {
140 if (vim_strchr(files[i], ',') != NULL)
141 {
142 p = vim_strsave_escaped(files[i], (char_u *)",");
143 if (p != NULL)
144 {
145 vim_free(files[i]);
146 files[i] = p;
147 }
148 }
149 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000150#ifdef BACKSLASH_IN_FILENAME
151 p = vim_strsave_fnameescape(files[i], vse_what);
152#else
153 p = vim_strsave_fnameescape(files[i],
154 xp->xp_shell ? VSE_SHELL : vse_what);
155#endif
156 if (p != NULL)
157 {
158 vim_free(files[i]);
159 files[i] = p;
160 }
161
162 // If 'str' starts with "\~", replace "~" at start of
163 // files[i] with "\~".
164 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
165 escape_fname(&files[i]);
166 }
167 xp->xp_backslash = XP_BS_NONE;
168
169 // If the first file starts with a '+' escape it. Otherwise it
170 // could be seen as "+cmd".
171 if (*files[0] == '+')
172 escape_fname(&files[0]);
173 }
174 else if (xp->xp_context == EXPAND_TAGS)
175 {
176 // Insert a backslash before characters in a tag name that
177 // would terminate the ":tag" command.
178 for (int i = 0; i < numfiles; ++i)
179 {
180 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
181 if (p != NULL)
182 {
183 vim_free(files[i]);
184 files[i] = p;
185 }
186 }
187 }
188}
189
190/*
191 * Escape special characters in the cmdline completion matches.
192 */
193 static void
Bram Moolenaar66b51422019-08-18 21:44:12 +0200194ExpandEscape(
195 expand_T *xp,
196 char_u *str,
197 int numfiles,
198 char_u **files,
199 int options)
200{
Bram Moolenaar66b51422019-08-18 21:44:12 +0200201 // May change home directory back to "~"
202 if (options & WILD_HOME_REPLACE)
203 tilde_replace(str, numfiles, files);
204
205 if (options & WILD_ESCAPE)
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +0000206 wildescape(xp, str, numfiles, files);
Bram Moolenaar66b51422019-08-18 21:44:12 +0200207}
208
209/*
210 * Return FAIL if this is not an appropriate context in which to do
211 * completion of anything, return OK if it is (even if there are no matches).
212 * For the caller, this means that the character is just passed through like a
213 * normal character (instead of being expanded). This allows :s/^I^D etc.
214 */
215 int
216nextwild(
217 expand_T *xp,
218 int type,
219 int options, // extra options for ExpandOne()
220 int escape) // if TRUE, escape the returned matches
221{
222 cmdline_info_T *ccline = get_cmdline_info();
223 int i, j;
224 char_u *p1;
225 char_u *p2;
226 int difflen;
227 int v;
228
229 if (xp->xp_numfiles == -1)
230 {
231 set_expand_context(xp);
232 cmd_showtail = expand_showtail(xp);
233 }
234
235 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
236 {
237 beep_flush();
238 return OK; // Something illegal on command line
239 }
240 if (xp->xp_context == EXPAND_NOTHING)
241 {
242 // Caller can use the character as a normal char instead
243 return FAIL;
244 }
245
Bram Moolenaar698a00f2022-11-14 22:07:45 +0000246 // If cmd_silent is set then don't show the dots, because redrawcmd() below
247 // won't remove them.
248 if (!cmd_silent)
249 {
250 msg_puts("..."); // show that we are busy
251 out_flush();
252 }
Bram Moolenaar66b51422019-08-18 21:44:12 +0200253
254 i = (int)(xp->xp_pattern - ccline->cmdbuff);
255 xp->xp_pattern_len = ccline->cmdpos - i;
256
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000257 if (type == WILD_NEXT || type == WILD_PREV
258 || type == WILD_PAGEUP || type == WILD_PAGEDOWN)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200259 {
260 // Get next/previous match for a previous expanded pattern.
261 p2 = ExpandOne(xp, NULL, NULL, 0, type);
262 }
263 else
264 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000265 if (cmdline_fuzzy_completion_supported(xp))
266 // If fuzzy matching, don't modify the search string
Yee Cheng Chin209ec902023-10-17 10:56:25 +0200267 p1 = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000268 else
269 p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
270
Bram Moolenaar66b51422019-08-18 21:44:12 +0200271 // Translate string into pattern and expand it.
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +0000272 if (p1 == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +0200273 p2 = NULL;
274 else
275 {
276 int use_options = options |
277 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
278 if (escape)
279 use_options |= WILD_ESCAPE;
280
281 if (p_wic)
282 use_options += WILD_ICASE;
283 p2 = ExpandOne(xp, p1,
284 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
285 use_options, type);
286 vim_free(p1);
287 // longest match: make sure it is not shorter, happens with :help
288 if (p2 != NULL && type == WILD_LONGEST)
289 {
290 for (j = 0; j < xp->xp_pattern_len; ++j)
291 if (ccline->cmdbuff[i + j] == '*'
292 || ccline->cmdbuff[i + j] == '?')
293 break;
294 if ((int)STRLEN(p2) < j)
295 VIM_CLEAR(p2);
296 }
297 }
298 }
299
300 if (p2 != NULL && !got_int)
301 {
302 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
303 if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
304 {
305 v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
306 xp->xp_pattern = ccline->cmdbuff + i;
307 }
308 else
309 v = OK;
310 if (v == OK)
311 {
312 mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
313 &ccline->cmdbuff[ccline->cmdpos],
314 (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
315 mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
316 ccline->cmdlen += difflen;
317 ccline->cmdpos += difflen;
318 }
319 }
320 vim_free(p2);
321
322 redrawcmd();
323 cursorcmd();
324
325 // When expanding a ":map" command and no matches are found, assume that
326 // the key is supposed to be inserted literally
327 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
328 return FAIL;
329
330 if (xp->xp_numfiles <= 0 && p2 == NULL)
331 beep_flush();
332 else if (xp->xp_numfiles == 1)
333 // free expanded pattern
334 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
335
336 return OK;
337}
338
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000339/*
340 * Create and display a cmdline completion popup menu with items from
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000341 * 'matches'.
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000342 */
343 static int
344cmdline_pum_create(
345 cmdline_info_T *ccline,
346 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000347 char_u **matches,
348 int numMatches,
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000349 int showtail)
350{
351 int i;
352 int columns;
353
354 // Add all the completion matches
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000355 compl_match_arraysize = numMatches;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000356 compl_match_array = ALLOC_MULT(pumitem_T, compl_match_arraysize);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000357 for (i = 0; i < numMatches; i++)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000358 {
zeertzjqc51a3762022-12-10 10:22:29 +0000359 compl_match_array[i].pum_text = SHOW_MATCH(i);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000360 compl_match_array[i].pum_info = NULL;
361 compl_match_array[i].pum_extra = NULL;
362 compl_match_array[i].pum_kind = NULL;
zeertzjq41008522024-07-27 13:21:49 +0200363 compl_match_array[i].pum_user_hlattr = -1;
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000364 }
365
366 // Compute the popup menu starting column
367 compl_startcol = vim_strsize(ccline->cmdbuff) + 1;
368 columns = vim_strsize(xp->xp_pattern);
369 if (showtail)
370 {
Bram Moolenaard6e91382022-11-12 17:44:13 +0000371 columns += vim_strsize(showmatches_gettail(matches[0]));
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000372 columns -= vim_strsize(matches[0]);
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000373 }
374 if (columns >= compl_startcol)
375 compl_startcol = 0;
376 else
377 compl_startcol -= columns;
378
379 // no default selection
380 compl_selected = -1;
381
382 cmdline_pum_display();
383
384 return EXPAND_OK;
385}
386
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000387/*
388 * Display the cmdline completion matches in a popup menu
389 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000390 void
391cmdline_pum_display(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000392{
393 pum_display(compl_match_array, compl_match_arraysize, compl_selected);
394}
395
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000396/*
397 * Returns TRUE if the cmdline completion popup menu is being displayed.
398 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000399 int
400cmdline_pum_active(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000401{
zeertzjqb82a2ab2022-08-21 14:33:57 +0100402 return pum_visible() && compl_match_array != NULL;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000403}
404
405/*
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000406 * Remove the cmdline completion popup menu (if present), free the list of
407 * items and refresh the screen.
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000408 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000409 void
410cmdline_pum_remove(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000411{
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000412 int save_p_lz = p_lz;
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100413 int save_KeyTyped = KeyTyped;
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000414
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000415 pum_undisplay();
416 VIM_CLEAR(compl_match_array);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000417 p_lz = FALSE; // avoid the popup menu hanging around
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000418 update_screen(0);
Bram Moolenaar5c52be42022-02-27 14:28:31 +0000419 p_lz = save_p_lz;
Bram Moolenaar414acd32022-02-10 21:09:45 +0000420 redrawcmd();
Bram Moolenaar11a57df2022-04-11 19:38:56 +0100421
422 // When a function is called (e.g. for 'foldtext') KeyTyped might be reset
423 // as a side effect.
424 KeyTyped = save_KeyTyped;
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000425}
426
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000427 void
428cmdline_pum_cleanup(cmdline_info_T *cclp)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000429{
430 cmdline_pum_remove();
431 wildmenu_cleanup(cclp);
432}
433
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +0000434/*
435 * Returns the starting column number to use for the cmdline completion popup
436 * menu.
437 */
Bram Moolenaar038e6d22022-12-08 12:00:50 +0000438 int
439cmdline_compl_startcol(void)
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000440{
441 return compl_startcol;
442}
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +0000443
Bram Moolenaar66b51422019-08-18 21:44:12 +0200444/*
zeertzjqd8c93402024-06-17 18:25:32 +0200445 * Returns the current cmdline completion pattern.
446 */
447 char_u *
448cmdline_compl_pattern(void)
449{
450 expand_T *xp = get_cmdline_info()->xpc;
451
452 return xp == NULL ? NULL : xp->xp_orig;
453}
454
455/*
456 * Returns TRUE if fuzzy cmdline completion is active, FALSE otherwise.
457 */
458 int
459cmdline_compl_is_fuzzy(void)
460{
461 expand_T *xp = get_cmdline_info()->xpc;
462
463 return xp != NULL && cmdline_fuzzy_completion_supported(xp);
464}
465
466/*
Bram Moolenaard6e91382022-11-12 17:44:13 +0000467 * Return the number of characters that should be skipped in a status match.
468 * These are backslashes used for escaping. Do show backslashes in help tags.
469 */
470 static int
471skip_status_match_char(expand_T *xp, char_u *s)
472{
473 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
474#ifdef FEAT_MENU
475 || ((xp->xp_context == EXPAND_MENUS
476 || xp->xp_context == EXPAND_MENUNAMES)
477 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
478#endif
479 )
480 {
481#ifndef BACKSLASH_IN_FILENAME
482 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
483 return 2;
484#endif
485 return 1;
486 }
487 return 0;
488}
489
490/*
491 * Get the length of an item as it will be shown in the status line.
492 */
493 static int
494status_match_len(expand_T *xp, char_u *s)
495{
496 int len = 0;
497
498#ifdef FEAT_MENU
499 int emenu = xp->xp_context == EXPAND_MENUS
500 || xp->xp_context == EXPAND_MENUNAMES;
501
502 // Check for menu separators - replace with '|'.
503 if (emenu && menu_is_separator(s))
504 return 1;
505#endif
506
507 while (*s != NUL)
508 {
509 s += skip_status_match_char(xp, s);
510 len += ptr2cells(s);
511 MB_PTR_ADV(s);
512 }
513
514 return len;
515}
516
517/*
518 * Show wildchar matches in the status line.
519 * Show at least the "match" item.
520 * We start at item 'first_match' in the list and show all matches that fit.
521 *
522 * If inversion is possible we use it. Else '=' characters are used.
523 */
524 static void
525win_redr_status_matches(
526 expand_T *xp,
527 int num_matches,
528 char_u **matches, // list of matches
529 int match,
530 int showtail)
531{
Bram Moolenaard6e91382022-11-12 17:44:13 +0000532 int row;
533 char_u *buf;
534 int len;
535 int clen; // length in screen cells
536 int fillchar;
537 int attr;
538 int i;
539 int highlight = TRUE;
540 char_u *selstart = NULL;
541 int selstart_col = 0;
542 char_u *selend = NULL;
543 static int first_match = 0;
544 int add_left = FALSE;
545 char_u *s;
546#ifdef FEAT_MENU
547 int emenu;
548#endif
549 int l;
550
551 if (matches == NULL) // interrupted completion?
552 return;
553
554 if (has_mbyte)
555 buf = alloc(Columns * MB_MAXBYTES + 1);
556 else
557 buf = alloc(Columns + 1);
558 if (buf == NULL)
559 return;
560
561 if (match == -1) // don't show match but original text
562 {
563 match = 0;
564 highlight = FALSE;
565 }
566 // count 1 for the ending ">"
zeertzjqc51a3762022-12-10 10:22:29 +0000567 clen = status_match_len(xp, SHOW_MATCH(match)) + 3;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000568 if (match == 0)
569 first_match = 0;
570 else if (match < first_match)
571 {
572 // jumping left, as far as we can go
573 first_match = match;
574 add_left = TRUE;
575 }
576 else
577 {
578 // check if match fits on the screen
579 for (i = first_match; i < match; ++i)
zeertzjqc51a3762022-12-10 10:22:29 +0000580 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000581 if (first_match > 0)
582 clen += 2;
583 // jumping right, put match at the left
584 if ((long)clen > Columns)
585 {
586 first_match = match;
587 // if showing the last match, we can add some on the left
588 clen = 2;
589 for (i = match; i < num_matches; ++i)
590 {
zeertzjqc51a3762022-12-10 10:22:29 +0000591 clen += status_match_len(xp, SHOW_MATCH(i)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000592 if ((long)clen >= Columns)
593 break;
594 }
595 if (i == num_matches)
596 add_left = TRUE;
597 }
598 }
599 if (add_left)
600 while (first_match > 0)
601 {
zeertzjqc51a3762022-12-10 10:22:29 +0000602 clen += status_match_len(xp, SHOW_MATCH(first_match - 1)) + 2;
Bram Moolenaard6e91382022-11-12 17:44:13 +0000603 if ((long)clen >= Columns)
604 break;
605 --first_match;
606 }
607
608 fillchar = fillchar_status(&attr, curwin);
609
610 if (first_match == 0)
611 {
612 *buf = NUL;
613 len = 0;
614 }
615 else
616 {
617 STRCPY(buf, "< ");
618 len = 2;
619 }
620 clen = len;
621
622 i = first_match;
zeertzjqc51a3762022-12-10 10:22:29 +0000623 while ((long)(clen + status_match_len(xp, SHOW_MATCH(i)) + 2) < Columns)
Bram Moolenaard6e91382022-11-12 17:44:13 +0000624 {
625 if (i == match)
626 {
627 selstart = buf + len;
628 selstart_col = clen;
629 }
630
zeertzjqc51a3762022-12-10 10:22:29 +0000631 s = SHOW_MATCH(i);
Bram Moolenaard6e91382022-11-12 17:44:13 +0000632 // Check for menu separators - replace with '|'
633#ifdef FEAT_MENU
634 emenu = (xp->xp_context == EXPAND_MENUS
635 || xp->xp_context == EXPAND_MENUNAMES);
636 if (emenu && menu_is_separator(s))
637 {
638 STRCPY(buf + len, transchar('|'));
639 l = (int)STRLEN(buf + len);
640 len += l;
641 clen += l;
642 }
643 else
644#endif
645 for ( ; *s != NUL; ++s)
646 {
647 s += skip_status_match_char(xp, s);
648 clen += ptr2cells(s);
649 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
650 {
651 STRNCPY(buf + len, s, l);
652 s += l - 1;
653 len += l;
654 }
655 else
656 {
657 STRCPY(buf + len, transchar_byte(*s));
658 len += (int)STRLEN(buf + len);
659 }
660 }
661 if (i == match)
662 selend = buf + len;
663
664 *(buf + len++) = ' ';
665 *(buf + len++) = ' ';
666 clen += 2;
667 if (++i == num_matches)
668 break;
669 }
670
671 if (i != num_matches)
672 {
673 *(buf + len++) = '>';
674 ++clen;
675 }
676
677 buf[len] = NUL;
678
679 row = cmdline_row - 1;
680 if (row >= 0)
681 {
682 if (wild_menu_showing == 0)
683 {
684 if (msg_scrolled > 0)
685 {
686 // Put the wildmenu just above the command line. If there is
687 // no room, scroll the screen one line up.
688 if (cmdline_row == Rows - 1)
689 {
690 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
691 ++msg_scrolled;
692 }
693 else
694 {
695 ++cmdline_row;
696 ++row;
697 }
698 wild_menu_showing = WM_SCROLLED;
699 }
700 else
701 {
702 // Create status line if needed by setting 'laststatus' to 2.
703 // Set 'winminheight' to zero to avoid that the window is
704 // resized.
705 if (lastwin->w_status_height == 0)
706 {
707 save_p_ls = p_ls;
708 save_p_wmh = p_wmh;
709 p_ls = 2;
710 p_wmh = 0;
711 last_status(FALSE);
712 }
713 wild_menu_showing = WM_SHOWN;
714 }
715 }
716
717 screen_puts(buf, row, 0, attr);
718 if (selstart != NULL && highlight)
719 {
720 *selend = NUL;
721 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
722 }
723
724 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
725 }
726
727 win_redraw_last_status(topframe);
728 vim_free(buf);
729}
730
731/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000732 * Get the next or prev cmdline completion match. The index of the match is set
zeertzjqe9ef3472023-08-17 23:57:05 +0200733 * in "xp->xp_selected"
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000734 */
735 static char_u *
736get_next_or_prev_match(
737 int mode,
zeertzjq28a23602023-09-29 19:58:35 +0200738 expand_T *xp)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000739{
zeertzjqe9ef3472023-08-17 23:57:05 +0200740 int findex = xp->xp_selected;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000741 int ht;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000742
743 if (xp->xp_numfiles <= 0)
744 return NULL;
745
746 if (mode == WILD_PREV)
747 {
748 if (findex == -1)
749 findex = xp->xp_numfiles;
750 --findex;
751 }
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000752 else if (mode == WILD_NEXT)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000753 ++findex;
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000754 else if (mode == WILD_PAGEUP)
755 {
756 if (findex == 0)
757 // at the first entry, don't select any entries
758 findex = -1;
759 else if (findex == -1)
760 // no entry is selected. select the last entry
761 findex = xp->xp_numfiles - 1;
762 else
763 {
764 // go up by the pum height
765 ht = pum_get_height();
766 if (ht > 3)
767 ht -= 2;
768 findex -= ht;
769 if (findex < 0)
770 // few entries left, select the first entry
771 findex = 0;
772 }
773 }
774 else // mode == WILD_PAGEDOWN
775 {
776 if (findex == xp->xp_numfiles - 1)
777 // at the last entry, don't select any entries
778 findex = -1;
779 else if (findex == -1)
780 // no entry is selected. select the first entry
781 findex = 0;
782 else
783 {
784 // go down by the pum height
785 ht = pum_get_height();
786 if (ht > 3)
787 ht -= 2;
788 findex += ht;
789 if (findex >= xp->xp_numfiles)
790 // few entries left, select the last entry
791 findex = xp->xp_numfiles - 1;
792 }
793 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000794
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000795 // When wrapping around, return the original string, set findex to -1.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000796 if (findex < 0)
797 {
zeertzjq28a23602023-09-29 19:58:35 +0200798 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000799 findex = xp->xp_numfiles - 1;
800 else
801 findex = -1;
802 }
803 if (findex >= xp->xp_numfiles)
804 {
zeertzjq28a23602023-09-29 19:58:35 +0200805 if (xp->xp_orig == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000806 findex = 0;
807 else
808 findex = -1;
809 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000810 if (compl_match_array)
811 {
812 compl_selected = findex;
813 cmdline_pum_display();
814 }
815 else if (p_wmnu)
816 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
817 findex, cmd_showtail);
zeertzjqe9ef3472023-08-17 23:57:05 +0200818 xp->xp_selected = findex;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000819
820 if (findex == -1)
zeertzjq28a23602023-09-29 19:58:35 +0200821 return vim_strsave(xp->xp_orig);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000822
823 return vim_strsave(xp->xp_files[findex]);
824}
825
826/*
827 * Start the command-line expansion and get the matches.
828 */
829 static char_u *
830ExpandOne_start(int mode, expand_T *xp, char_u *str, int options)
831{
832 int non_suf_match; // number without matching suffix
833 int i;
834 char_u *ss = NULL;
835
836 // Do the expansion.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000837 if (ExpandFromContext(xp, str, &xp->xp_files, &xp->xp_numfiles,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000838 options) == FAIL)
839 {
840#ifdef FNAME_ILLEGAL
841 // Illegal file name has been silently skipped. But when there
842 // are wildcards, the real problem is that there was no match,
843 // causing the pattern to be added, which has illegal characters.
844 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
845 semsg(_(e_no_match_str_2), str);
846#endif
847 }
848 else if (xp->xp_numfiles == 0)
849 {
850 if (!(options & WILD_SILENT))
851 semsg(_(e_no_match_str_2), str);
852 }
853 else
854 {
855 // Escape the matches for use on the command line.
856 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
857
858 // Check for matching suffixes in file names.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +0000859 if (mode != WILD_ALL && mode != WILD_ALL_KEEP && mode != WILD_LONGEST)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000860 {
861 if (xp->xp_numfiles)
862 non_suf_match = xp->xp_numfiles;
863 else
864 non_suf_match = 1;
865 if ((xp->xp_context == EXPAND_FILES
866 || xp->xp_context == EXPAND_DIRECTORIES)
867 && xp->xp_numfiles > 1)
868 {
869 // More than one match; check suffix.
870 // The files will have been sorted on matching suffix in
871 // expand_wildcards, only need to check the first two.
872 non_suf_match = 0;
873 for (i = 0; i < 2; ++i)
874 if (match_suffix(xp->xp_files[i]))
875 ++non_suf_match;
876 }
877 if (non_suf_match != 1)
878 {
879 // Can we ever get here unless it's while expanding
880 // interactively? If not, we can get rid of this all
881 // together. Don't really want to wait for this message
882 // (and possibly have to hit return to continue!).
883 if (!(options & WILD_SILENT))
884 emsg(_(e_too_many_file_names));
885 else if (!(options & WILD_NO_BEEP))
886 beep_flush();
887 }
888 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
889 ss = vim_strsave(xp->xp_files[0]);
890 }
891 }
892
893 return ss;
894}
895
896/*
897 * Return the longest common part in the list of cmdline completion matches.
898 */
899 static char_u *
900find_longest_match(expand_T *xp, int options)
901{
902 long_u len;
903 int mb_len = 1;
904 int c0, ci;
905 int i;
906 char_u *ss;
907
908 for (len = 0; xp->xp_files[0][len]; len += mb_len)
909 {
910 if (has_mbyte)
911 {
912 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
913 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
914 }
915 else
916 c0 = xp->xp_files[0][len];
917 for (i = 1; i < xp->xp_numfiles; ++i)
918 {
919 if (has_mbyte)
920 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
921 else
922 ci = xp->xp_files[i][len];
923 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
924 || xp->xp_context == EXPAND_FILES
925 || xp->xp_context == EXPAND_SHELLCMD
926 || xp->xp_context == EXPAND_BUFFERS))
927 {
928 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
929 break;
930 }
931 else if (c0 != ci)
932 break;
933 }
934 if (i < xp->xp_numfiles)
935 {
936 if (!(options & WILD_NO_BEEP))
937 vim_beep(BO_WILD);
938 break;
939 }
940 }
941
942 ss = alloc(len + 1);
943 if (ss)
944 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
945
946 return ss;
947}
948
949/*
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +0000950 * Do wildcard expansion on the string "str".
Bram Moolenaar66b51422019-08-18 21:44:12 +0200951 * Chars that should not be expanded must be preceded with a backslash.
952 * Return a pointer to allocated memory containing the new string.
953 * Return NULL for failure.
954 *
955 * "orig" is the originally expanded string, copied to allocated memory. It
zeertzjq28a23602023-09-29 19:58:35 +0200956 * should either be kept in "xp->xp_orig" or freed. When "mode" is WILD_NEXT
957 * or WILD_PREV "orig" should be NULL.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200958 *
959 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
960 * is WILD_EXPAND_FREE or WILD_ALL.
961 *
962 * mode = WILD_FREE: just free previously expanded matches
963 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
964 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
965 * mode = WILD_NEXT: use next match in multiple match, wrap to first
966 * mode = WILD_PREV: use previous match in multiple match, wrap to first
967 * mode = WILD_ALL: return all matches concatenated
968 * mode = WILD_LONGEST: return longest matched part
969 * mode = WILD_ALL_KEEP: get all matches, keep matches
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +0000970 * mode = WILD_APPLY: apply the item selected in the cmdline completion
971 * popup menu and close the menu.
972 * mode = WILD_CANCEL: cancel and close the cmdline completion popup and
973 * use the original text.
Bram Moolenaar66b51422019-08-18 21:44:12 +0200974 *
975 * options = WILD_LIST_NOTFOUND: list entries without a match
976 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
977 * options = WILD_USE_NL: Use '\n' for WILD_ALL
978 * options = WILD_NO_BEEP: Don't beep for multiple matches
979 * options = WILD_ADD_SLASH: add a slash after directory names
980 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
981 * options = WILD_SILENT: don't print warning messages
982 * options = WILD_ESCAPE: put backslash before special chars
983 * options = WILD_ICASE: ignore case for files
Bram Moolenaarec680282020-06-12 19:35:32 +0200984 * options = WILD_ALLLINKS; keep broken links
Bram Moolenaar66b51422019-08-18 21:44:12 +0200985 *
986 * The variables xp->xp_context and xp->xp_backslash must have been set!
987 */
988 char_u *
989ExpandOne(
990 expand_T *xp,
991 char_u *str,
992 char_u *orig, // allocated copy of original of expanded string
993 int options,
994 int mode)
995{
996 char_u *ss = NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +0200997 int orig_saved = FALSE;
998 int i;
999 long_u len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001000
1001 // first handle the case of using an old match
Yegappan Lakshmanan5cffa8d2022-03-16 13:33:53 +00001002 if (mode == WILD_NEXT || mode == WILD_PREV
1003 || mode == WILD_PAGEUP || mode == WILD_PAGEDOWN)
zeertzjq28a23602023-09-29 19:58:35 +02001004 return get_next_or_prev_match(mode, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001005
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001006 if (mode == WILD_CANCEL)
zeertzjq28a23602023-09-29 19:58:35 +02001007 ss = vim_strsave(xp->xp_orig ? xp->xp_orig : (char_u *)"");
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001008 else if (mode == WILD_APPLY)
zeertzjqe9ef3472023-08-17 23:57:05 +02001009 ss = vim_strsave(xp->xp_selected == -1
zeertzjq28a23602023-09-29 19:58:35 +02001010 ? (xp->xp_orig ? xp->xp_orig : (char_u *)"")
zeertzjqe9ef3472023-08-17 23:57:05 +02001011 : xp->xp_files[xp->xp_selected]);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001012
Bram Moolenaar66b51422019-08-18 21:44:12 +02001013 // free old names
1014 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
1015 {
1016 FreeWild(xp->xp_numfiles, xp->xp_files);
1017 xp->xp_numfiles = -1;
zeertzjq28a23602023-09-29 19:58:35 +02001018 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar038e6d22022-12-08 12:00:50 +00001019
1020 // The entries from xp_files may be used in the PUM, remove it.
1021 if (compl_match_array != NULL)
1022 cmdline_pum_remove();
Bram Moolenaar66b51422019-08-18 21:44:12 +02001023 }
zeertzjqe9ef3472023-08-17 23:57:05 +02001024 xp->xp_selected = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001025
1026 if (mode == WILD_FREE) // only release file name
1027 return NULL;
1028
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001029 if (xp->xp_numfiles == -1 && mode != WILD_APPLY && mode != WILD_CANCEL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001030 {
zeertzjq28a23602023-09-29 19:58:35 +02001031 vim_free(xp->xp_orig);
1032 xp->xp_orig = orig;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001033 orig_saved = TRUE;
1034
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001035 ss = ExpandOne_start(mode, xp, str, options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001036 }
1037
1038 // Find longest common part
1039 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
1040 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001041 ss = find_longest_match(xp, options);
zeertzjqe9ef3472023-08-17 23:57:05 +02001042 xp->xp_selected = -1; // next p_wc gets first one
Bram Moolenaar66b51422019-08-18 21:44:12 +02001043 }
1044
Bram Moolenaar57e95172022-08-20 19:26:14 +01001045 // Concatenate all matching names. Unless interrupted, this can be slow
1046 // and the result probably won't be used.
1047 if (mode == WILD_ALL && xp->xp_numfiles > 0 && !got_int)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001048 {
1049 len = 0;
1050 for (i = 0; i < xp->xp_numfiles; ++i)
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001051 {
1052 if (i > 0)
1053 {
1054 if (xp->xp_prefix == XP_PREFIX_NO)
1055 len += 2; // prefix "no"
1056 else if (xp->xp_prefix == XP_PREFIX_INV)
1057 len += 3; // prefix "inv"
1058 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001059 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001060 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001061 ss = alloc(len);
1062 if (ss != NULL)
1063 {
1064 *ss = NUL;
1065 for (i = 0; i < xp->xp_numfiles; ++i)
1066 {
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001067 if (i > 0)
1068 {
1069 if (xp->xp_prefix == XP_PREFIX_NO)
1070 STRCAT(ss, "no");
1071 else if (xp->xp_prefix == XP_PREFIX_INV)
1072 STRCAT(ss, "inv");
1073 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001074 STRCAT(ss, xp->xp_files[i]);
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001075
Bram Moolenaar66b51422019-08-18 21:44:12 +02001076 if (i != xp->xp_numfiles - 1)
1077 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
1078 }
1079 }
1080 }
1081
1082 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
1083 ExpandCleanup(xp);
1084
zeertzjq28a23602023-09-29 19:58:35 +02001085 // Free "orig" if it wasn't stored in "xp->xp_orig".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001086 if (!orig_saved)
1087 vim_free(orig);
1088
1089 return ss;
1090}
1091
1092/*
1093 * Prepare an expand structure for use.
1094 */
1095 void
1096ExpandInit(expand_T *xp)
1097{
Bram Moolenaarc841aff2020-07-25 14:11:55 +02001098 CLEAR_POINTER(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001099 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar048d9d22023-05-06 22:21:11 +01001100 xp->xp_prefix = XP_PREFIX_NONE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001101 xp->xp_numfiles = -1;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001102}
1103
1104/*
1105 * Cleanup an expand structure after use.
1106 */
1107 void
1108ExpandCleanup(expand_T *xp)
1109{
1110 if (xp->xp_numfiles >= 0)
1111 {
1112 FreeWild(xp->xp_numfiles, xp->xp_files);
1113 xp->xp_numfiles = -1;
1114 }
zeertzjq28a23602023-09-29 19:58:35 +02001115 VIM_CLEAR(xp->xp_orig);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001116}
1117
1118/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001119 * Display one line of completion matches. Multiple matches are displayed in
1120 * each line (used by wildmode=list and CTRL-D)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001121 * matches - list of completion match names
1122 * numMatches - number of completion matches in "matches"
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001123 * lines - number of output lines
1124 * linenr - line number of matches to display
1125 * maxlen - maximum number of characters in each line
1126 * showtail - display only the tail of the full path of a file name
1127 * dir_attr - highlight attribute to use for directory names
1128 */
1129 static void
1130showmatches_oneline(
1131 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001132 char_u **matches,
1133 int numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001134 int lines,
1135 int linenr,
1136 int maxlen,
1137 int showtail,
1138 int dir_attr)
1139{
1140 int i, j;
1141 int isdir;
1142 int lastlen;
1143 char_u *p;
1144
1145 lastlen = 999;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001146 for (j = linenr; j < numMatches; j += lines)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001147 {
1148 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1149 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001150 msg_outtrans_attr(matches[j], HL_ATTR(HLF_D));
1151 p = matches[j] + STRLEN(matches[j]) + 1;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001152 msg_advance(maxlen + 1);
1153 msg_puts((char *)p);
1154 msg_advance(maxlen + 3);
1155 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
1156 break;
1157 }
1158 for (i = maxlen - lastlen; --i >= 0; )
1159 msg_putchar(' ');
1160 if (xp->xp_context == EXPAND_FILES
1161 || xp->xp_context == EXPAND_SHELLCMD
1162 || xp->xp_context == EXPAND_BUFFERS)
1163 {
1164 // highlight directories
1165 if (xp->xp_numfiles != -1)
1166 {
1167 char_u *halved_slash;
1168 char_u *exp_path;
1169 char_u *path;
1170
1171 // Expansion was done before and special characters
1172 // were escaped, need to halve backslashes. Also
1173 // $HOME has been replaced with ~/.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001174 exp_path = expand_env_save_opt(matches[j], TRUE);
1175 path = exp_path != NULL ? exp_path : matches[j];
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001176 halved_slash = backslash_halve_save(path);
1177 isdir = mch_isdir(halved_slash != NULL ? halved_slash
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001178 : matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001179 vim_free(exp_path);
1180 if (halved_slash != path)
1181 vim_free(halved_slash);
1182 }
1183 else
1184 // Expansion was done here, file names are literal.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001185 isdir = mch_isdir(matches[j]);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001186 if (showtail)
zeertzjqc51a3762022-12-10 10:22:29 +00001187 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001188 else
1189 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001190 home_replace(NULL, matches[j], NameBuff, MAXPATHL,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001191 TRUE);
1192 p = NameBuff;
1193 }
1194 }
1195 else
1196 {
1197 isdir = FALSE;
zeertzjqc51a3762022-12-10 10:22:29 +00001198 p = SHOW_MATCH(j);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001199 }
1200 lastlen = msg_outtrans_attr(p, isdir ? dir_attr : 0);
1201 }
1202 if (msg_col > 0) // when not wrapped around
1203 {
1204 msg_clr_eos();
1205 msg_putchar('\n');
1206 }
1207 out_flush(); // show one line at a time
1208}
1209
1210/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02001211 * Show all matches for completion on the command line.
1212 * Returns EXPAND_NOTHING when the character that triggered expansion should
1213 * be inserted like a normal character.
1214 */
1215 int
1216showmatches(expand_T *xp, int wildmenu UNUSED)
1217{
1218 cmdline_info_T *ccline = get_cmdline_info();
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001219 int numMatches;
1220 char_u **matches;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001221 int i, j;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001222 int maxlen;
1223 int lines;
1224 int columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001225 int attr;
1226 int showtail;
1227
1228 if (xp->xp_numfiles == -1)
1229 {
1230 set_expand_context(xp);
1231 i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001232 &numMatches, &matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001233 showtail = expand_showtail(xp);
1234 if (i != EXPAND_OK)
1235 return i;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001236 }
1237 else
1238 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001239 numMatches = xp->xp_numfiles;
1240 matches = xp->xp_files;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001241 showtail = cmd_showtail;
1242 }
1243
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001244 if (wildmenu && vim_strchr(p_wop, WOP_PUM) != NULL)
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00001245 // cmdline completion popup menu (with wildoptions=pum)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001246 return cmdline_pum_create(ccline, xp, matches, numMatches, showtail);
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00001247
Bram Moolenaar66b51422019-08-18 21:44:12 +02001248 if (!wildmenu)
1249 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02001250 msg_didany = FALSE; // lines_left will be set
1251 msg_start(); // prepare for paging
1252 msg_putchar('\n');
1253 out_flush();
1254 cmdline_row = msg_row;
1255 msg_didany = FALSE; // lines_left will be set again
1256 msg_start(); // prepare for paging
Bram Moolenaar66b51422019-08-18 21:44:12 +02001257 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02001258
1259 if (got_int)
1260 got_int = FALSE; // only int. the completion, not the cmd line
Bram Moolenaar66b51422019-08-18 21:44:12 +02001261 else if (wildmenu)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001262 win_redr_status_matches(xp, numMatches, matches, -1, showtail);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001263 else
1264 {
1265 // find the length of the longest file name
1266 maxlen = 0;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001267 for (i = 0; i < numMatches; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001268 {
1269 if (!showtail && (xp->xp_context == EXPAND_FILES
1270 || xp->xp_context == EXPAND_SHELLCMD
1271 || xp->xp_context == EXPAND_BUFFERS))
1272 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001273 home_replace(NULL, matches[i], NameBuff, MAXPATHL, TRUE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001274 j = vim_strsize(NameBuff);
1275 }
1276 else
zeertzjqc51a3762022-12-10 10:22:29 +00001277 j = vim_strsize(SHOW_MATCH(i));
Bram Moolenaar66b51422019-08-18 21:44:12 +02001278 if (j > maxlen)
1279 maxlen = j;
1280 }
1281
1282 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001283 lines = numMatches;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001284 else
1285 {
1286 // compute the number of columns and lines for the listing
1287 maxlen += 2; // two spaces between file names
1288 columns = ((int)Columns + 2) / maxlen;
1289 if (columns < 1)
1290 columns = 1;
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001291 lines = (numMatches + columns - 1) / columns;
Bram Moolenaar66b51422019-08-18 21:44:12 +02001292 }
1293
1294 attr = HL_ATTR(HLF_D); // find out highlighting for directories
1295
1296 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
1297 {
1298 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
1299 msg_clr_eos();
1300 msg_advance(maxlen - 3);
1301 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
1302 }
1303
1304 // list the files line by line
1305 for (i = 0; i < lines; ++i)
1306 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001307 showmatches_oneline(xp, matches, numMatches, lines, i,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001308 maxlen, showtail, attr);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001309 if (got_int)
1310 {
1311 got_int = FALSE;
1312 break;
1313 }
1314 }
1315
1316 // we redraw the command below the lines that we have just listed
1317 // This is a bit tricky, but it saves a lot of screen updating.
1318 cmdline_row = msg_row; // will put it back later
1319 }
1320
1321 if (xp->xp_numfiles == -1)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001322 FreeWild(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02001323
1324 return EXPAND_OK;
1325}
1326
1327/*
Bram Moolenaard6e91382022-11-12 17:44:13 +00001328 * gettail() version for showmatches() and win_redr_status_matches():
1329 * Return the tail of file name path "s", ignoring a trailing "/".
Bram Moolenaar66b51422019-08-18 21:44:12 +02001330 */
Bram Moolenaard6e91382022-11-12 17:44:13 +00001331 static char_u *
1332showmatches_gettail(char_u *s)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001333{
1334 char_u *p;
1335 char_u *t = s;
1336 int had_sep = FALSE;
1337
1338 for (p = s; *p != NUL; )
1339 {
1340 if (vim_ispathsep(*p)
1341#ifdef BACKSLASH_IN_FILENAME
1342 && !rem_backslash(p)
1343#endif
1344 )
1345 had_sep = TRUE;
1346 else if (had_sep)
1347 {
1348 t = p;
1349 had_sep = FALSE;
1350 }
1351 MB_PTR_ADV(p);
1352 }
1353 return t;
1354}
1355
1356/*
1357 * Return TRUE if we only need to show the tail of completion matches.
1358 * When not completing file names or there is a wildcard in the path FALSE is
1359 * returned.
1360 */
1361 static int
1362expand_showtail(expand_T *xp)
1363{
1364 char_u *s;
1365 char_u *end;
1366
1367 // When not completing file names a "/" may mean something different.
1368 if (xp->xp_context != EXPAND_FILES
1369 && xp->xp_context != EXPAND_SHELLCMD
1370 && xp->xp_context != EXPAND_DIRECTORIES)
1371 return FALSE;
1372
1373 end = gettail(xp->xp_pattern);
1374 if (end == xp->xp_pattern) // there is no path separator
1375 return FALSE;
1376
1377 for (s = xp->xp_pattern; s < end; s++)
1378 {
1379 // Skip escaped wildcards. Only when the backslash is not a path
1380 // separator, on DOS the '*' "path\*\file" must not be skipped.
1381 if (rem_backslash(s))
1382 ++s;
1383 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
1384 return FALSE;
1385 }
1386 return TRUE;
1387}
1388
1389/*
1390 * Prepare a string for expansion.
1391 * When expanding file names: The string will be used with expand_wildcards().
1392 * Copy "fname[len]" into allocated memory and add a '*' at the end.
1393 * When expanding other names: The string will be used with regcomp(). Copy
1394 * the name into allocated memory and prepend "^".
1395 */
1396 char_u *
1397addstar(
1398 char_u *fname,
1399 int len,
1400 int context) // EXPAND_FILES etc.
1401{
1402 char_u *retval;
1403 int i, j;
1404 int new_len;
1405 char_u *tail;
1406 int ends_in_star;
1407
1408 if (context != EXPAND_FILES
1409 && context != EXPAND_FILES_IN_PATH
1410 && context != EXPAND_SHELLCMD
LemonBoya20bf692024-07-11 22:35:53 +02001411 && context != EXPAND_DIRECTORIES
1412 && context != EXPAND_DIRS_IN_CDPATH)
Bram Moolenaar66b51422019-08-18 21:44:12 +02001413 {
1414 // Matching will be done internally (on something other than files).
1415 // So we convert the file-matching-type wildcards into our kind for
1416 // use with vim_regcomp(). First work out how long it will be:
1417
1418 // For help tags the translation is done in find_help_tags().
1419 // For a tag pattern starting with "/" no translation is needed.
1420 if (context == EXPAND_HELP
1421 || context == EXPAND_COLORS
1422 || context == EXPAND_COMPILER
1423 || context == EXPAND_OWNSYNTAX
1424 || context == EXPAND_FILETYPE
Doug Kearns81642d92024-01-04 22:37:44 +01001425 || context == EXPAND_KEYMAP
Bram Moolenaar66b51422019-08-18 21:44:12 +02001426 || context == EXPAND_PACKADD
zeertzjqb0d45ec2023-01-25 15:04:22 +00001427 || context == EXPAND_RUNTIME
Bram Moolenaar66b51422019-08-18 21:44:12 +02001428 || ((context == EXPAND_TAGS_LISTFILES
1429 || context == EXPAND_TAGS)
1430 && fname[0] == '/'))
1431 retval = vim_strnsave(fname, len);
1432 else
1433 {
1434 new_len = len + 2; // +2 for '^' at start, NUL at end
1435 for (i = 0; i < len; i++)
1436 {
1437 if (fname[i] == '*' || fname[i] == '~')
1438 new_len++; // '*' needs to be replaced by ".*"
1439 // '~' needs to be replaced by "\~"
1440
1441 // Buffer names are like file names. "." should be literal
1442 if (context == EXPAND_BUFFERS && fname[i] == '.')
1443 new_len++; // "." becomes "\."
1444
1445 // Custom expansion takes care of special things, match
1446 // backslashes literally (perhaps also for other types?)
1447 if ((context == EXPAND_USER_DEFINED
1448 || context == EXPAND_USER_LIST) && fname[i] == '\\')
1449 new_len++; // '\' becomes "\\"
1450 }
1451 retval = alloc(new_len);
1452 if (retval != NULL)
1453 {
1454 retval[0] = '^';
1455 j = 1;
1456 for (i = 0; i < len; i++, j++)
1457 {
1458 // Skip backslash. But why? At least keep it for custom
1459 // expansion.
1460 if (context != EXPAND_USER_DEFINED
1461 && context != EXPAND_USER_LIST
1462 && fname[i] == '\\'
1463 && ++i == len)
1464 break;
1465
1466 switch (fname[i])
1467 {
1468 case '*': retval[j++] = '.';
1469 break;
1470 case '~': retval[j++] = '\\';
1471 break;
1472 case '?': retval[j] = '.';
1473 continue;
1474 case '.': if (context == EXPAND_BUFFERS)
1475 retval[j++] = '\\';
1476 break;
1477 case '\\': if (context == EXPAND_USER_DEFINED
1478 || context == EXPAND_USER_LIST)
1479 retval[j++] = '\\';
1480 break;
1481 }
1482 retval[j] = fname[i];
1483 }
1484 retval[j] = NUL;
1485 }
1486 }
1487 }
1488 else
1489 {
1490 retval = alloc(len + 4);
1491 if (retval != NULL)
1492 {
1493 vim_strncpy(retval, fname, len);
1494
1495 // Don't add a star to *, ~, ~user, $var or `cmd`.
1496 // * would become **, which walks the whole tree.
1497 // ~ would be at the start of the file name, but not the tail.
1498 // $ could be anywhere in the tail.
1499 // ` could be anywhere in the file name.
1500 // When the name ends in '$' don't add a star, remove the '$'.
1501 tail = gettail(retval);
1502 ends_in_star = (len > 0 && retval[len - 1] == '*');
1503#ifndef BACKSLASH_IN_FILENAME
1504 for (i = len - 2; i >= 0; --i)
1505 {
1506 if (retval[i] != '\\')
1507 break;
1508 ends_in_star = !ends_in_star;
1509 }
1510#endif
1511 if ((*retval != '~' || tail != retval)
1512 && !ends_in_star
1513 && vim_strchr(tail, '$') == NULL
1514 && vim_strchr(retval, '`') == NULL)
1515 retval[len++] = '*';
1516 else if (len > 0 && retval[len - 1] == '$')
1517 --len;
1518 retval[len] = NUL;
1519 }
1520 }
1521 return retval;
1522}
1523
1524/*
1525 * Must parse the command line so far to work out what context we are in.
1526 * Completion can then be done based on that context.
1527 * This routine sets the variables:
1528 * xp->xp_pattern The start of the pattern to be expanded within
1529 * the command line (ends at the cursor).
1530 * xp->xp_context The type of thing to expand. Will be one of:
1531 *
1532 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
1533 * the command line, like an unknown command. Caller
1534 * should beep.
1535 * EXPAND_NOTHING Unrecognised context for completion, use char like
1536 * a normal char, rather than for completion. eg
1537 * :s/^I/
1538 * EXPAND_COMMANDS Cursor is still touching the command, so complete
1539 * it.
1540 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
1541 * EXPAND_FILES After command with EX_XFILE set, or after setting
1542 * with P_EXPAND set. eg :e ^I, :w>>^I
1543 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
Bram Moolenaarf39d9e92023-04-22 22:54:40 +01001544 * when we know only directories are of interest.
1545 * E.g. :set dir=^I and :cd ^I
Bram Moolenaar66b51422019-08-18 21:44:12 +02001546 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
1547 * EXPAND_SETTINGS Complete variable names. eg :set d^I
1548 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
1549 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
1550 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
1551 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
1552 * EXPAND_EVENTS Complete event names
1553 * EXPAND_SYNTAX Complete :syntax command arguments
1554 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
1555 * EXPAND_AUGROUP Complete autocommand group names
1556 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
1557 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
1558 * eg :unmap a^I , :cunab x^I
1559 * EXPAND_FUNCTIONS Complete internal or user defined function names,
1560 * eg :call sub^I
1561 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
1562 * EXPAND_EXPRESSION Complete internal or user defined function/variable
1563 * names in expressions, eg :while s^I
1564 * EXPAND_ENV_VARS Complete environment variable names
1565 * EXPAND_USER Complete user names
1566 */
Shougo Matsushita79d599b2022-05-07 12:48:29 +01001567 void
Bram Moolenaar66b51422019-08-18 21:44:12 +02001568set_expand_context(expand_T *xp)
1569{
1570 cmdline_info_T *ccline = get_cmdline_info();
1571
1572 // only expansion for ':', '>' and '=' command-lines
1573 if (ccline->cmdfirstc != ':'
1574#ifdef FEAT_EVAL
1575 && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
1576 && !ccline->input_fn
1577#endif
1578 )
1579 {
1580 xp->xp_context = EXPAND_NOTHING;
1581 return;
1582 }
1583 set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
1584}
1585
Bram Moolenaard0190392019-08-23 21:17:35 +02001586/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001587 * Sets the index of a built-in or user defined command 'cmd' in eap->cmdidx.
1588 * For user defined commands, the completion context is set in 'xp' and the
1589 * completion flags in 'complp'.
1590 *
1591 * Returns a pointer to the text after the command or NULL for failure.
Bram Moolenaard0190392019-08-23 21:17:35 +02001592 */
1593 static char_u *
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001594set_cmd_index(char_u *cmd, exarg_T *eap, expand_T *xp, int *complp)
Bram Moolenaard0190392019-08-23 21:17:35 +02001595{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001596 char_u *p = NULL;
1597 int len = 0;
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001598 int fuzzy = cmdline_fuzzy_complete(cmd);
Bram Moolenaard0190392019-08-23 21:17:35 +02001599
1600 // Isolate the command and search for it in the command table.
1601 // Exceptions:
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001602 // - the 'k' command can directly be followed by any character, but do
1603 // accept "keepmarks", "keepalt" and "keepjumps". As fuzzy matching can
1604 // find matches anywhere in the command name, do this only for command
1605 // expansion based on regular expression and not for fuzzy matching.
Bram Moolenaard0190392019-08-23 21:17:35 +02001606 // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00001607 if (!fuzzy && (*cmd == 'k' && cmd[1] != 'e'))
Bram Moolenaard0190392019-08-23 21:17:35 +02001608 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001609 eap->cmdidx = CMD_k;
Bram Moolenaard0190392019-08-23 21:17:35 +02001610 p = cmd + 1;
1611 }
1612 else
1613 {
1614 p = cmd;
1615 while (ASCII_ISALPHA(*p) || *p == '*') // Allow * wild card
1616 ++p;
Bram Moolenaardf749a22021-03-28 15:29:43 +02001617 // A user command may contain digits.
1618 // Include "9" for "vim9*" commands; "vim9cmd" and "vim9script".
1619 if (ASCII_ISUPPER(cmd[0]) || STRNCMP("vim9", cmd, 4) == 0)
Bram Moolenaard0190392019-08-23 21:17:35 +02001620 while (ASCII_ISALNUM(*p) || *p == '*')
1621 ++p;
1622 // for python 3.x: ":py3*" commands completion
1623 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1624 {
1625 ++p;
1626 while (ASCII_ISALPHA(*p) || *p == '*')
1627 ++p;
1628 }
1629 // check for non-alpha command
1630 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1631 ++p;
1632 len = (int)(p - cmd);
1633
1634 if (len == 0)
1635 {
1636 xp->xp_context = EXPAND_UNSUCCESSFUL;
1637 return NULL;
1638 }
1639
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001640 eap->cmdidx = excmd_get_cmdidx(cmd, len);
Bram Moolenaard0190392019-08-23 21:17:35 +02001641
Yegappan Lakshmanan4df5b332022-02-26 11:04:42 +00001642 // User defined commands support alphanumeric characters.
Yegappan Lakshmanan7db3a8e2022-07-26 22:01:36 +01001643 // Also when doing fuzzy expansion for non-shell commands, support
1644 // alphanumeric characters.
1645 if ((cmd[0] >= 'A' && cmd[0] <= 'Z')
1646 || (fuzzy && eap->cmdidx != CMD_bang && *p != NUL))
Bram Moolenaard0190392019-08-23 21:17:35 +02001647 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
1648 ++p;
1649 }
1650
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001651 // If the cursor is touching the command, and it ends in an alphanumeric
Bram Moolenaard0190392019-08-23 21:17:35 +02001652 // character, complete the command name.
1653 if (*p == NUL && ASCII_ISALNUM(p[-1]))
1654 return NULL;
1655
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001656 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001657 {
1658 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1659 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001660 eap->cmdidx = CMD_substitute;
Bram Moolenaard0190392019-08-23 21:17:35 +02001661 p = cmd + 1;
1662 }
1663 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1664 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001665 eap->cmd = cmd;
1666 p = find_ucmd(eap, p, NULL, xp, complp);
Bram Moolenaard0190392019-08-23 21:17:35 +02001667 if (p == NULL)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001668 eap->cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaard0190392019-08-23 21:17:35 +02001669 }
1670 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001671 if (eap->cmdidx == CMD_SIZE)
Bram Moolenaard0190392019-08-23 21:17:35 +02001672 {
1673 // Not still touching the command and it was an illegal one
1674 xp->xp_context = EXPAND_UNSUCCESSFUL;
1675 return NULL;
1676 }
1677
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001678 return p;
1679}
Bram Moolenaard0190392019-08-23 21:17:35 +02001680
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001681/*
1682 * Set the completion context for a command argument with wild card characters.
1683 */
1684 static void
1685set_context_for_wildcard_arg(
1686 exarg_T *eap,
1687 char_u *arg,
1688 int usefilter,
1689 expand_T *xp,
1690 int *complp)
1691{
1692 char_u *p;
1693 int c;
1694 int in_quote = FALSE;
1695 char_u *bow = NULL; // Beginning of word
1696 int len = 0;
1697
1698 // Allow spaces within back-quotes to count as part of the argument
1699 // being expanded.
1700 xp->xp_pattern = skipwhite(arg);
1701 p = xp->xp_pattern;
1702 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001703 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001704 if (has_mbyte)
1705 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001706 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001707 c = *p;
1708 if (c == '\\' && p[1] != NUL)
1709 ++p;
1710 else if (c == '`')
Bram Moolenaard0190392019-08-23 21:17:35 +02001711 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001712 if (!in_quote)
Bram Moolenaard0190392019-08-23 21:17:35 +02001713 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001714 xp->xp_pattern = p;
1715 bow = p + 1;
Bram Moolenaard0190392019-08-23 21:17:35 +02001716 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001717 in_quote = !in_quote;
1718 }
1719 // An argument can contain just about everything, except
1720 // characters that end the command and white space.
1721 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaard0190392019-08-23 21:17:35 +02001722#ifdef SPACE_IN_FILENAME
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001723 && (!(eap->argt & EX_NOSPC) || usefilter)
Bram Moolenaard0190392019-08-23 21:17:35 +02001724#endif
1725 ))
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001726 {
1727 len = 0; // avoid getting stuck when space is in 'isfname'
1728 while (*p != NUL)
Bram Moolenaard0190392019-08-23 21:17:35 +02001729 {
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001730 if (has_mbyte)
1731 c = mb_ptr2char(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001732 else
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001733 c = *p;
1734 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaard0190392019-08-23 21:17:35 +02001735 break;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001736 if (has_mbyte)
1737 len = (*mb_ptr2len)(p);
1738 else
1739 len = 1;
1740 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001741 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001742 if (in_quote)
1743 bow = p;
1744 else
1745 xp->xp_pattern = p;
1746 p -= len;
Bram Moolenaard0190392019-08-23 21:17:35 +02001747 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001748 MB_PTR_ADV(p);
Bram Moolenaard0190392019-08-23 21:17:35 +02001749 }
1750
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001751 // If we are still inside the quotes, and we passed a space, just
1752 // expand from there.
1753 if (bow != NULL && in_quote)
1754 xp->xp_pattern = bow;
1755 xp->xp_context = EXPAND_FILES;
1756
1757 // For a shell command more chars need to be escaped.
Ruslan Russkikh0407d622024-10-08 22:21:05 +02001758 if (usefilter || eap->cmdidx == CMD_bang || eap->cmdidx == CMD_terminal || *complp == EXPAND_SHELLCMDLINE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00001759 {
1760#ifndef BACKSLASH_IN_FILENAME
1761 xp->xp_shell = TRUE;
1762#endif
1763 // When still after the command name expand executables.
1764 if (xp->xp_pattern == skipwhite(arg))
1765 xp->xp_context = EXPAND_SHELLCMD;
1766 }
1767
1768 // Check for environment variable.
1769 if (*xp->xp_pattern == '$')
1770 {
1771 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1772 if (!vim_isIDc(*p))
1773 break;
1774 if (*p == NUL)
1775 {
1776 xp->xp_context = EXPAND_ENV_VARS;
1777 ++xp->xp_pattern;
1778 // Avoid that the assignment uses EXPAND_FILES again.
1779 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST)
1780 *complp = EXPAND_ENV_VARS;
1781 }
1782 }
1783 // Check for user names.
1784 if (*xp->xp_pattern == '~')
1785 {
1786 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1787 ;
1788 // Complete ~user only if it partially matches a user name.
1789 // A full match ~user<Tab> will be replaced by user's home
1790 // directory i.e. something like ~user<Tab> -> /home/user/
1791 if (*p == NUL && p > xp->xp_pattern + 1
1792 && match_user(xp->xp_pattern + 1) >= 1)
1793 {
1794 xp->xp_context = EXPAND_USER;
1795 ++xp->xp_pattern;
1796 }
1797 }
1798}
1799
1800/*
Yee Cheng Chin989426b2023-10-14 11:46:51 +02001801 * Set the completion context for the "++opt=arg" argument. Always returns
1802 * NULL.
1803 */
1804 static char_u *
1805set_context_in_argopt(expand_T *xp, char_u *arg)
1806{
1807 char_u *p;
1808
1809 p = vim_strchr(arg, '=');
1810 if (p == NULL)
1811 xp->xp_pattern = arg;
1812 else
1813 xp->xp_pattern = p + 1;
1814
1815 xp->xp_context = EXPAND_ARGOPT;
1816 return NULL;
1817}
1818
1819#ifdef FEAT_TERMINAL
1820/*
1821 * Set the completion context for :terminal's [options]. Always returns NULL.
1822 */
1823 static char_u *
1824set_context_in_terminalopt(expand_T *xp, char_u *arg)
1825{
1826 char_u *p;
1827
1828 p = vim_strchr(arg, '=');
1829 if (p == NULL)
1830 xp->xp_pattern = arg;
1831 else
1832 xp->xp_pattern = p + 1;
1833
1834 xp->xp_context = EXPAND_TERMINALOPT;
1835 return NULL;
1836}
1837#endif
1838
1839/*
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001840 * Set the completion context for the :filter command. Returns a pointer to the
1841 * next command after the :filter command.
1842 */
1843 static char_u *
1844set_context_in_filter_cmd(expand_T *xp, char_u *arg)
1845{
1846 if (*arg != NUL)
1847 arg = skip_vimgrep_pat(arg, NULL, NULL);
1848 if (arg == NULL || *arg == NUL)
1849 {
1850 xp->xp_context = EXPAND_NOTHING;
1851 return NULL;
1852 }
1853 return skipwhite(arg);
1854}
1855
1856#ifdef FEAT_SEARCH_EXTRA
1857/*
1858 * Set the completion context for the :match command. Returns a pointer to the
1859 * next command after the :match command.
1860 */
1861 static char_u *
1862set_context_in_match_cmd(expand_T *xp, char_u *arg)
1863{
1864 if (*arg == NUL || !ends_excmd(*arg))
1865 {
1866 // also complete "None"
1867 set_context_in_echohl_cmd(xp, arg);
1868 arg = skipwhite(skiptowhite(arg));
1869 if (*arg != NUL)
1870 {
1871 xp->xp_context = EXPAND_NOTHING;
1872 arg = skip_regexp(arg + 1, *arg, magic_isset());
1873 }
1874 }
1875 return find_nextcmd(arg);
1876}
1877#endif
1878
1879/*
1880 * Returns a pointer to the next command after a :global or a :v command.
1881 * Returns NULL if there is no next command.
1882 */
1883 static char_u *
1884find_cmd_after_global_cmd(char_u *arg)
1885{
1886 int delim;
1887
1888 delim = *arg; // get the delimiter
1889 if (delim)
1890 ++arg; // skip delimiter if there is one
1891
1892 while (arg[0] != NUL && arg[0] != delim)
1893 {
1894 if (arg[0] == '\\' && arg[1] != NUL)
1895 ++arg;
1896 ++arg;
1897 }
1898 if (arg[0] != NUL)
1899 return arg + 1;
1900
1901 return NULL;
1902}
1903
1904/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001905 * Returns a pointer to the next command after a :substitute or a :& command.
1906 * Returns NULL if there is no next command.
1907 */
1908 static char_u *
1909find_cmd_after_substitute_cmd(char_u *arg)
1910{
1911 int delim;
1912
1913 delim = *arg;
1914 if (delim)
1915 {
1916 // skip "from" part
1917 ++arg;
1918 arg = skip_regexp(arg, delim, magic_isset());
1919
1920 if (arg[0] != NUL && arg[0] == delim)
1921 {
1922 // skip "to" part
1923 ++arg;
1924 while (arg[0] != NUL && arg[0] != delim)
1925 {
1926 if (arg[0] == '\\' && arg[1] != NUL)
1927 ++arg;
1928 ++arg;
1929 }
1930 if (arg[0] != NUL) // skip delimiter
1931 ++arg;
1932 }
1933 }
1934 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1935 ++arg;
1936 if (arg[0] != NUL)
1937 return arg;
1938
1939 return NULL;
1940}
1941
1942/*
1943 * Returns a pointer to the next command after a :isearch/:dsearch/:ilist
1944 * :dlist/:ijump/:psearch/:djump/:isplit/:dsplit command.
1945 * Returns NULL if there is no next command.
1946 */
1947 static char_u *
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001948find_cmd_after_isearch_cmd(expand_T *xp, char_u *arg)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001949{
1950 arg = skipwhite(skipdigits(arg)); // skip count
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001951 if (*arg != '/')
1952 return NULL;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001953
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001954 // Match regexp, not just whole words
1955 for (++arg; *arg && *arg != '/'; arg++)
1956 if (*arg == '\\' && arg[1] != NUL)
1957 arg++;
1958 if (*arg)
1959 {
1960 arg = skipwhite(arg + 1);
1961
1962 // Check for trailing illegal characters
1963 if (*arg == NUL || vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1964 xp->xp_context = EXPAND_NOTHING;
1965 else
1966 return arg;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00001967 }
1968
1969 return NULL;
1970}
1971
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00001972#ifdef FEAT_EVAL
1973/*
1974 * Set the completion context for the :unlet command. Always returns NULL.
1975 */
1976 static char_u *
1977set_context_in_unlet_cmd(expand_T *xp, char_u *arg)
1978{
1979 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1980 arg = xp->xp_pattern + 1;
1981
1982 xp->xp_context = EXPAND_USER_VARS;
1983 xp->xp_pattern = arg;
1984
1985 if (*xp->xp_pattern == '$')
1986 {
1987 xp->xp_context = EXPAND_ENV_VARS;
1988 ++xp->xp_pattern;
1989 }
1990
1991 return NULL;
1992}
1993#endif
1994
1995#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1996/*
1997 * Set the completion context for the :language command. Always returns NULL.
1998 */
1999 static char_u *
2000set_context_in_lang_cmd(expand_T *xp, char_u *arg)
2001{
2002 char_u *p;
2003
2004 p = skiptowhite(arg);
2005 if (*p == NUL)
2006 {
2007 xp->xp_context = EXPAND_LANGUAGE;
2008 xp->xp_pattern = arg;
2009 }
2010 else
2011 {
2012 if ( STRNCMP(arg, "messages", p - arg) == 0
2013 || STRNCMP(arg, "ctype", p - arg) == 0
2014 || STRNCMP(arg, "time", p - arg) == 0
2015 || STRNCMP(arg, "collate", p - arg) == 0)
2016 {
2017 xp->xp_context = EXPAND_LOCALES;
2018 xp->xp_pattern = skipwhite(p);
2019 }
2020 else
2021 xp->xp_context = EXPAND_NOTHING;
2022 }
2023
2024 return NULL;
2025}
2026#endif
2027
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002028#ifdef FEAT_EVAL
2029static enum
2030{
2031 EXP_BREAKPT_ADD, // expand ":breakadd" sub-commands
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002032 EXP_BREAKPT_DEL, // expand ":breakdel" sub-commands
2033 EXP_PROFDEL // expand ":profdel" sub-commands
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002034} breakpt_expand_what;
2035
2036/*
2037 * Set the completion context for the :breakadd command. Always returns NULL.
2038 */
2039 static char_u *
2040set_context_in_breakadd_cmd(expand_T *xp, char_u *arg, cmdidx_T cmdidx)
2041{
2042 char_u *p;
2043 char_u *subcmd_start;
2044
2045 xp->xp_context = EXPAND_BREAKPOINT;
2046 xp->xp_pattern = arg;
2047
2048 if (cmdidx == CMD_breakadd)
2049 breakpt_expand_what = EXP_BREAKPT_ADD;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002050 else if (cmdidx == CMD_breakdel)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002051 breakpt_expand_what = EXP_BREAKPT_DEL;
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002052 else
2053 breakpt_expand_what = EXP_PROFDEL;
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002054
2055 p = skipwhite(arg);
2056 if (*p == NUL)
2057 return NULL;
2058 subcmd_start = p;
2059
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002060 if (STRNCMP("file ", p, 5) == 0 || STRNCMP("func ", p, 5) == 0)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002061 {
2062 // :breakadd file [lnum] <filename>
2063 // :breakadd func [lnum] <funcname>
2064 p += 4;
2065 p = skipwhite(p);
2066
2067 // skip line number (if specified)
2068 if (VIM_ISDIGIT(*p))
2069 {
2070 p = skipdigits(p);
2071 if (*p != ' ')
2072 {
2073 xp->xp_context = EXPAND_NOTHING;
2074 return NULL;
2075 }
2076 p = skipwhite(p);
2077 }
2078 if (STRNCMP("file", subcmd_start, 4) == 0)
2079 xp->xp_context = EXPAND_FILES;
2080 else
2081 xp->xp_context = EXPAND_USER_FUNC;
2082 xp->xp_pattern = p;
2083 }
2084 else if (STRNCMP("expr ", p, 5) == 0)
2085 {
2086 // :breakadd expr <expression>
2087 xp->xp_context = EXPAND_EXPRESSION;
2088 xp->xp_pattern = skipwhite(p + 5);
2089 }
2090
2091 return NULL;
2092}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002093
2094 static char_u *
2095set_context_in_scriptnames_cmd(expand_T *xp, char_u *arg)
2096{
2097 char_u *p;
2098
2099 xp->xp_context = EXPAND_NOTHING;
2100 xp->xp_pattern = NULL;
2101
2102 p = skipwhite(arg);
2103 if (VIM_ISDIGIT(*p))
2104 return NULL;
2105
2106 xp->xp_context = EXPAND_SCRIPTNAMES;
2107 xp->xp_pattern = p;
2108
2109 return NULL;
2110}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002111#endif
2112
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002113/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002114 * Set the completion context in 'xp' for command 'cmd' with index 'cmdidx'.
2115 * The argument to the command is 'arg' and the argument flags is 'argt'.
2116 * For user-defined commands and for environment variables, 'compl' has the
2117 * completion type.
2118 * Returns a pointer to the next command. Returns NULL if there is no next
2119 * command.
2120 */
2121 static char_u *
2122set_context_by_cmdname(
2123 char_u *cmd,
2124 cmdidx_T cmdidx,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002125 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002126 char_u *arg,
2127 long argt,
2128 int compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002129 int forceit)
2130{
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002131 switch (cmdidx)
Bram Moolenaard0190392019-08-23 21:17:35 +02002132 {
2133 case CMD_find:
2134 case CMD_sfind:
2135 case CMD_tabfind:
2136 if (xp->xp_context == EXPAND_FILES)
2137 xp->xp_context = EXPAND_FILES_IN_PATH;
2138 break;
2139 case CMD_cd:
2140 case CMD_chdir:
2141 case CMD_tcd:
2142 case CMD_tchdir:
2143 case CMD_lcd:
2144 case CMD_lchdir:
2145 if (xp->xp_context == EXPAND_FILES)
LemonBoya20bf692024-07-11 22:35:53 +02002146 xp->xp_context = EXPAND_DIRS_IN_CDPATH;
Bram Moolenaard0190392019-08-23 21:17:35 +02002147 break;
2148 case CMD_help:
2149 xp->xp_context = EXPAND_HELP;
2150 xp->xp_pattern = arg;
2151 break;
2152
2153 // Command modifiers: return the argument.
2154 // Also for commands with an argument that is a command.
2155 case CMD_aboveleft:
2156 case CMD_argdo:
2157 case CMD_belowright:
2158 case CMD_botright:
2159 case CMD_browse:
2160 case CMD_bufdo:
2161 case CMD_cdo:
2162 case CMD_cfdo:
2163 case CMD_confirm:
2164 case CMD_debug:
2165 case CMD_folddoclosed:
2166 case CMD_folddoopen:
2167 case CMD_hide:
zeertzjqd3de1782022-09-01 12:58:52 +01002168 case CMD_horizontal:
Bram Moolenaard0190392019-08-23 21:17:35 +02002169 case CMD_keepalt:
2170 case CMD_keepjumps:
2171 case CMD_keepmarks:
2172 case CMD_keeppatterns:
2173 case CMD_ldo:
2174 case CMD_leftabove:
2175 case CMD_lfdo:
2176 case CMD_lockmarks:
2177 case CMD_noautocmd:
2178 case CMD_noswapfile:
2179 case CMD_rightbelow:
2180 case CMD_sandbox:
2181 case CMD_silent:
2182 case CMD_tab:
2183 case CMD_tabdo:
2184 case CMD_topleft:
2185 case CMD_verbose:
2186 case CMD_vertical:
2187 case CMD_windo:
Bram Moolenaare70e12b2021-06-13 17:20:08 +02002188 case CMD_vim9cmd:
2189 case CMD_legacy:
Bram Moolenaard0190392019-08-23 21:17:35 +02002190 return arg;
2191
2192 case CMD_filter:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002193 return set_context_in_filter_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002194
2195#ifdef FEAT_SEARCH_EXTRA
2196 case CMD_match:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002197 return set_context_in_match_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002198#endif
2199
2200 // All completion for the +cmdline_compl feature goes here.
2201
2202 case CMD_command:
2203 return set_context_in_user_cmd(xp, arg);
2204
2205 case CMD_delcommand:
2206 xp->xp_context = EXPAND_USER_COMMANDS;
2207 xp->xp_pattern = arg;
2208 break;
2209
2210 case CMD_global:
2211 case CMD_vglobal:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002212 return find_cmd_after_global_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002213 case CMD_and:
2214 case CMD_substitute:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002215 return find_cmd_after_substitute_cmd(arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002216 case CMD_isearch:
2217 case CMD_dsearch:
2218 case CMD_ilist:
2219 case CMD_dlist:
2220 case CMD_ijump:
2221 case CMD_psearch:
2222 case CMD_djump:
2223 case CMD_isplit:
2224 case CMD_dsplit:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002225 return find_cmd_after_isearch_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002226 case CMD_autocmd:
2227 return set_context_in_autocmd(xp, arg, FALSE);
2228 case CMD_doautocmd:
2229 case CMD_doautoall:
2230 return set_context_in_autocmd(xp, arg, TRUE);
2231 case CMD_set:
2232 set_context_in_set_cmd(xp, arg, 0);
2233 break;
2234 case CMD_setglobal:
2235 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
2236 break;
2237 case CMD_setlocal:
2238 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
2239 break;
2240 case CMD_tag:
2241 case CMD_stag:
2242 case CMD_ptag:
2243 case CMD_ltag:
2244 case CMD_tselect:
2245 case CMD_stselect:
2246 case CMD_ptselect:
2247 case CMD_tjump:
2248 case CMD_stjump:
2249 case CMD_ptjump:
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00002250 if (vim_strchr(p_wop, WOP_TAGFILE) != NULL)
Bram Moolenaard0190392019-08-23 21:17:35 +02002251 xp->xp_context = EXPAND_TAGS_LISTFILES;
2252 else
2253 xp->xp_context = EXPAND_TAGS;
2254 xp->xp_pattern = arg;
2255 break;
2256 case CMD_augroup:
2257 xp->xp_context = EXPAND_AUGROUP;
2258 xp->xp_pattern = arg;
2259 break;
2260#ifdef FEAT_SYN_HL
2261 case CMD_syntax:
2262 set_context_in_syntax_cmd(xp, arg);
2263 break;
2264#endif
2265#ifdef FEAT_EVAL
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002266 case CMD_final:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002267 case CMD_const:
Bram Moolenaard0190392019-08-23 21:17:35 +02002268 case CMD_let:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002269 case CMD_var:
Bram Moolenaard0190392019-08-23 21:17:35 +02002270 case CMD_if:
2271 case CMD_elseif:
2272 case CMD_while:
2273 case CMD_for:
2274 case CMD_echo:
2275 case CMD_echon:
2276 case CMD_execute:
2277 case CMD_echomsg:
2278 case CMD_echoerr:
2279 case CMD_call:
2280 case CMD_return:
2281 case CMD_cexpr:
2282 case CMD_caddexpr:
2283 case CMD_cgetexpr:
2284 case CMD_lexpr:
2285 case CMD_laddexpr:
2286 case CMD_lgetexpr:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002287 set_context_for_expression(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002288 break;
2289
2290 case CMD_unlet:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002291 return set_context_in_unlet_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002292 case CMD_function:
2293 case CMD_delfunction:
2294 xp->xp_context = EXPAND_USER_FUNC;
2295 xp->xp_pattern = arg;
2296 break;
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02002297 case CMD_disassemble:
2298 set_context_in_disassemble_cmd(xp, arg);
2299 break;
Bram Moolenaard0190392019-08-23 21:17:35 +02002300
2301 case CMD_echohl:
2302 set_context_in_echohl_cmd(xp, arg);
2303 break;
2304#endif
2305 case CMD_highlight:
2306 set_context_in_highlight_cmd(xp, arg);
2307 break;
2308#ifdef FEAT_CSCOPE
2309 case CMD_cscope:
2310 case CMD_lcscope:
2311 case CMD_scscope:
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002312 set_context_in_cscope_cmd(xp, arg, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002313 break;
2314#endif
2315#ifdef FEAT_SIGNS
2316 case CMD_sign:
2317 set_context_in_sign_cmd(xp, arg);
2318 break;
2319#endif
2320 case CMD_bdelete:
2321 case CMD_bwipeout:
2322 case CMD_bunload:
2323 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2324 arg = xp->xp_pattern + 1;
2325 // FALLTHROUGH
2326 case CMD_buffer:
2327 case CMD_sbuffer:
2328 case CMD_checktime:
2329 xp->xp_context = EXPAND_BUFFERS;
2330 xp->xp_pattern = arg;
2331 break;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002332#ifdef FEAT_DIFF
2333 case CMD_diffget:
2334 case CMD_diffput:
2335 // If current buffer is in diff mode, complete buffer names
2336 // which are in diff mode, and different than current buffer.
2337 xp->xp_context = EXPAND_DIFF_BUFFERS;
2338 xp->xp_pattern = arg;
2339 break;
2340#endif
Bram Moolenaard0190392019-08-23 21:17:35 +02002341 case CMD_USER:
2342 case CMD_USER_BUF:
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00002343 return set_context_in_user_cmdarg(cmd, arg, argt, compl, xp,
2344 forceit);
Bram Moolenaard0190392019-08-23 21:17:35 +02002345
2346 case CMD_map: case CMD_noremap:
2347 case CMD_nmap: case CMD_nnoremap:
2348 case CMD_vmap: case CMD_vnoremap:
2349 case CMD_omap: case CMD_onoremap:
2350 case CMD_imap: case CMD_inoremap:
2351 case CMD_cmap: case CMD_cnoremap:
2352 case CMD_lmap: case CMD_lnoremap:
2353 case CMD_smap: case CMD_snoremap:
2354 case CMD_tmap: case CMD_tnoremap:
2355 case CMD_xmap: case CMD_xnoremap:
2356 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002357 FALSE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002358 case CMD_unmap:
2359 case CMD_nunmap:
2360 case CMD_vunmap:
2361 case CMD_ounmap:
2362 case CMD_iunmap:
2363 case CMD_cunmap:
2364 case CMD_lunmap:
2365 case CMD_sunmap:
2366 case CMD_tunmap:
2367 case CMD_xunmap:
2368 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002369 FALSE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002370 case CMD_mapclear:
2371 case CMD_nmapclear:
2372 case CMD_vmapclear:
2373 case CMD_omapclear:
2374 case CMD_imapclear:
2375 case CMD_cmapclear:
2376 case CMD_lmapclear:
2377 case CMD_smapclear:
2378 case CMD_tmapclear:
2379 case CMD_xmapclear:
2380 xp->xp_context = EXPAND_MAPCLEAR;
2381 xp->xp_pattern = arg;
2382 break;
2383
2384 case CMD_abbreviate: case CMD_noreabbrev:
2385 case CMD_cabbrev: case CMD_cnoreabbrev:
2386 case CMD_iabbrev: case CMD_inoreabbrev:
2387 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002388 TRUE, FALSE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002389 case CMD_unabbreviate:
2390 case CMD_cunabbrev:
2391 case CMD_iunabbrev:
2392 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002393 TRUE, TRUE, cmdidx);
Bram Moolenaard0190392019-08-23 21:17:35 +02002394#ifdef FEAT_MENU
2395 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
2396 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
2397 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
2398 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
2399 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
2400 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
2401 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
2402 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
2403 case CMD_tmenu: case CMD_tunmenu:
2404 case CMD_popup: case CMD_tearoff: case CMD_emenu:
2405 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
2406#endif
2407
2408 case CMD_colorscheme:
2409 xp->xp_context = EXPAND_COLORS;
2410 xp->xp_pattern = arg;
2411 break;
2412
2413 case CMD_compiler:
2414 xp->xp_context = EXPAND_COMPILER;
2415 xp->xp_pattern = arg;
2416 break;
2417
2418 case CMD_ownsyntax:
2419 xp->xp_context = EXPAND_OWNSYNTAX;
2420 xp->xp_pattern = arg;
2421 break;
2422
2423 case CMD_setfiletype:
2424 xp->xp_context = EXPAND_FILETYPE;
2425 xp->xp_pattern = arg;
2426 break;
2427
2428 case CMD_packadd:
2429 xp->xp_context = EXPAND_PACKADD;
2430 xp->xp_pattern = arg;
2431 break;
2432
zeertzjqb0d45ec2023-01-25 15:04:22 +00002433 case CMD_runtime:
2434 set_context_in_runtime_cmd(xp, arg);
2435 break;
2436
Bram Moolenaard0190392019-08-23 21:17:35 +02002437#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2438 case CMD_language:
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002439 return set_context_in_lang_cmd(xp, arg);
Bram Moolenaard0190392019-08-23 21:17:35 +02002440#endif
2441#if defined(FEAT_PROFILE)
2442 case CMD_profile:
2443 set_context_in_profile_cmd(xp, arg);
2444 break;
2445#endif
2446 case CMD_behave:
2447 xp->xp_context = EXPAND_BEHAVE;
2448 xp->xp_pattern = arg;
2449 break;
2450
2451 case CMD_messages:
2452 xp->xp_context = EXPAND_MESSAGES;
2453 xp->xp_pattern = arg;
2454 break;
2455
2456 case CMD_history:
2457 xp->xp_context = EXPAND_HISTORY;
2458 xp->xp_pattern = arg;
2459 break;
2460#if defined(FEAT_PROFILE)
2461 case CMD_syntime:
2462 xp->xp_context = EXPAND_SYNTIME;
2463 xp->xp_pattern = arg;
2464 break;
2465#endif
2466
2467 case CMD_argdelete:
2468 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
2469 arg = xp->xp_pattern + 1;
2470 xp->xp_context = EXPAND_ARGLIST;
2471 xp->xp_pattern = arg;
2472 break;
2473
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002474#ifdef FEAT_EVAL
2475 case CMD_breakadd:
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002476 case CMD_profdel:
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002477 case CMD_breakdel:
2478 return set_context_in_breakadd_cmd(xp, arg, cmdidx);
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002479
2480 case CMD_scriptnames:
2481 return set_context_in_scriptnames_cmd(xp, arg);
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002482#endif
2483
Bram Moolenaard0190392019-08-23 21:17:35 +02002484 default:
2485 break;
2486 }
2487 return NULL;
2488}
2489
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002490/*
2491 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
2492 * we don't need/want deleted. Maybe this could be done better if we didn't
2493 * repeat all this stuff. The only problem is that they may not stay
2494 * perfectly compatible with each other, but then the command line syntax
2495 * probably won't change that much -- webb.
2496 */
2497 static char_u *
2498set_one_cmd_context(
2499 expand_T *xp,
2500 char_u *buff) // buffer for command string
2501{
2502 char_u *p;
2503 char_u *cmd, *arg;
2504 int len = 0;
2505 exarg_T ea;
2506 int compl = EXPAND_NOTHING;
2507 int forceit = FALSE;
2508 int usefilter = FALSE; // filter instead of file name
2509
2510 ExpandInit(xp);
2511 xp->xp_pattern = buff;
2512 xp->xp_line = buff;
2513 xp->xp_context = EXPAND_COMMANDS; // Default until we get past command
2514 ea.argt = 0;
2515
2516 // 1. skip comment lines and leading space, colons or bars
2517 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
2518 ;
2519 xp->xp_pattern = cmd;
2520
2521 if (*cmd == NUL)
2522 return NULL;
2523 if (*cmd == '"') // ignore comment lines
2524 {
2525 xp->xp_context = EXPAND_NOTHING;
2526 return NULL;
2527 }
2528
2529 // 3. Skip over the range to find the command.
2530 cmd = skip_range(cmd, TRUE, &xp->xp_context);
2531 xp->xp_pattern = cmd;
2532 if (*cmd == NUL)
2533 return NULL;
2534 if (*cmd == '"')
2535 {
2536 xp->xp_context = EXPAND_NOTHING;
2537 return NULL;
2538 }
2539
2540 if (*cmd == '|' || *cmd == '\n')
2541 return cmd + 1; // There's another command
2542
2543 // Get the command index.
2544 p = set_cmd_index(cmd, &ea, xp, &compl);
2545 if (p == NULL)
2546 return NULL;
2547
2548 xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
2549
2550 if (*p == '!') // forced commands
2551 {
2552 forceit = TRUE;
2553 ++p;
2554 }
2555
2556 // 6. parse arguments
2557 if (!IS_USER_CMDIDX(ea.cmdidx))
2558 ea.argt = excmd_get_argt(ea.cmdidx);
2559
2560 arg = skipwhite(p);
2561
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002562 // Does command allow "++argopt" argument?
2563 if ((ea.argt & EX_ARGOPT) || ea.cmdidx == CMD_terminal)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002564 {
Yee Cheng Chin989426b2023-10-14 11:46:51 +02002565 while (*arg != NUL && STRNCMP(arg, "++", 2) == 0)
2566 {
2567 p = arg + 2;
2568 while (*p && !vim_isspace(*p))
2569 MB_PTR_ADV(p);
2570
2571 // Still touching the command after "++"?
2572 if (*p == NUL)
2573 {
2574 if (ea.argt & EX_ARGOPT)
2575 return set_context_in_argopt(xp, arg + 2);
2576#ifdef FEAT_TERMINAL
2577 if (ea.cmdidx == CMD_terminal)
2578 return set_context_in_terminalopt(xp, arg + 2);
2579#endif
2580 }
2581
2582 arg = skipwhite(p);
2583 }
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002584 }
2585
2586 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2587 {
2588 if (*arg == '>') // append
2589 {
2590 if (*++arg == '>')
2591 ++arg;
2592 arg = skipwhite(arg);
2593 }
2594 else if (*arg == '!' && ea.cmdidx == CMD_write) // :w !filter
2595 {
2596 ++arg;
2597 usefilter = TRUE;
2598 }
2599 }
2600
2601 if (ea.cmdidx == CMD_read)
2602 {
2603 usefilter = forceit; // :r! filter if forced
2604 if (*arg == '!') // :r !filter
2605 {
2606 ++arg;
2607 usefilter = TRUE;
2608 }
2609 }
2610
2611 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2612 {
2613 while (*arg == *cmd) // allow any number of '>' or '<'
2614 ++arg;
2615 arg = skipwhite(arg);
2616 }
2617
2618 // Does command allow "+command"?
2619 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
2620 {
2621 // Check if we're in the +command
2622 p = arg + 1;
2623 arg = skip_cmd_arg(arg, FALSE);
2624
2625 // Still touching the command after '+'?
2626 if (*arg == NUL)
2627 return p;
2628
2629 // Skip space(s) after +command to get to the real argument
2630 arg = skipwhite(arg);
2631 }
2632
2633
2634 // Check for '|' to separate commands and '"' to start comments.
2635 // Don't do this for ":read !cmd" and ":write !cmd".
2636 if ((ea.argt & EX_TRLBAR) && !usefilter)
2637 {
2638 p = arg;
2639 // ":redir @" is not the start of a comment
2640 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
2641 p += 2;
2642 while (*p)
2643 {
2644 if (*p == Ctrl_V)
2645 {
2646 if (p[1] != NUL)
2647 ++p;
2648 }
2649 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
2650 || *p == '|' || *p == '\n')
2651 {
2652 if (*(p - 1) != '\\')
2653 {
2654 if (*p == '|' || *p == '\n')
2655 return p + 1;
2656 return NULL; // It's a comment
2657 }
2658 }
2659 MB_PTR_ADV(p);
2660 }
2661 }
2662
2663 if (!(ea.argt & EX_EXTRA) && *arg != NUL
2664 && vim_strchr((char_u *)"|\"", *arg) == NULL)
2665 // no arguments allowed but there is something
2666 return NULL;
2667
2668 // Find start of last argument (argument just before cursor):
2669 p = buff;
2670 xp->xp_pattern = p;
2671 len = (int)STRLEN(buff);
2672 while (*p && p < buff + len)
2673 {
2674 if (*p == ' ' || *p == TAB)
2675 {
2676 // argument starts after a space
2677 xp->xp_pattern = ++p;
2678 }
2679 else
2680 {
2681 if (*p == '\\' && *(p + 1) != NUL)
2682 ++p; // skip over escaped character
2683 MB_PTR_ADV(p);
2684 }
2685 }
2686
2687 if (ea.argt & EX_XFILE)
2688 set_context_for_wildcard_arg(&ea, arg, usefilter, xp, &compl);
2689
2690 // 6. Switch on command name.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002691 return set_context_by_cmdname(cmd, ea.cmdidx, xp, arg, ea.argt, compl,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002692 forceit);
2693}
2694
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002695/*
2696 * Set the completion context in 'xp' for command 'str'
2697 */
Bram Moolenaar66b51422019-08-18 21:44:12 +02002698 void
2699set_cmd_context(
2700 expand_T *xp,
2701 char_u *str, // start of command line
2702 int len, // length of command line (excl. NUL)
2703 int col, // position of cursor
2704 int use_ccline UNUSED) // use ccline for info
2705{
2706#ifdef FEAT_EVAL
2707 cmdline_info_T *ccline = get_cmdline_info();
2708#endif
2709 int old_char = NUL;
2710 char_u *nextcomm;
2711
2712 // Avoid a UMR warning from Purify, only save the character if it has been
2713 // written before.
2714 if (col < len)
2715 old_char = str[col];
2716 str[col] = NUL;
2717 nextcomm = str;
2718
2719#ifdef FEAT_EVAL
2720 if (use_ccline && ccline->cmdfirstc == '=')
2721 {
Bram Moolenaar66b51422019-08-18 21:44:12 +02002722 // pass CMD_SIZE because there is no real command
2723 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar66b51422019-08-18 21:44:12 +02002724 }
2725 else if (use_ccline && ccline->input_fn)
2726 {
2727 xp->xp_context = ccline->xp_context;
2728 xp->xp_pattern = ccline->cmdbuff;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002729 xp->xp_arg = ccline->xp_arg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02002730 }
2731 else
2732#endif
2733 while (nextcomm != NULL)
2734 nextcomm = set_one_cmd_context(xp, nextcomm);
2735
2736 // Store the string here so that call_user_expand_func() can get to them
2737 // easily.
2738 xp->xp_line = str;
2739 xp->xp_col = col;
2740
2741 str[col] = old_char;
2742}
2743
2744/*
2745 * Expand the command line "str" from context "xp".
2746 * "xp" must have been set by set_cmd_context().
2747 * xp->xp_pattern points into "str", to where the text that is to be expanded
2748 * starts.
2749 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
2750 * cursor.
2751 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
2752 * key that triggered expansion literally.
2753 * Returns EXPAND_OK otherwise.
2754 */
2755 int
2756expand_cmdline(
2757 expand_T *xp,
2758 char_u *str, // start of command line
2759 int col, // position of cursor
2760 int *matchcount, // return: nr of matches
2761 char_u ***matches) // return: array of pointers to matches
2762{
2763 char_u *file_str = NULL;
2764 int options = WILD_ADD_SLASH|WILD_SILENT;
2765
2766 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2767 {
2768 beep_flush();
2769 return EXPAND_UNSUCCESSFUL; // Something illegal on command line
2770 }
2771 if (xp->xp_context == EXPAND_NOTHING)
2772 {
2773 // Caller can use the character as a normal char instead
2774 return EXPAND_NOTHING;
2775 }
2776
2777 // add star to file name, or convert to regexp if not exp. files.
2778 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002779 if (cmdline_fuzzy_completion_supported(xp))
2780 // If fuzzy matching, don't modify the search string
2781 file_str = vim_strsave(xp->xp_pattern);
2782 else
2783 {
2784 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
2785 if (file_str == NULL)
2786 return EXPAND_UNSUCCESSFUL;
2787 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02002788
2789 if (p_wic)
2790 options += WILD_ICASE;
2791
2792 // find all files that match the description
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002793 if (ExpandFromContext(xp, file_str, matches, matchcount, options) == FAIL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02002794 {
2795 *matchcount = 0;
2796 *matches = NULL;
2797 }
2798 vim_free(file_str);
2799
2800 return EXPAND_OK;
2801}
2802
Bram Moolenaar66b51422019-08-18 21:44:12 +02002803/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002804 * Expand file or directory names.
Bram Moolenaar747f1102022-09-18 13:06:41 +01002805 * Returns OK or FAIL.
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002806 */
2807 static int
2808expand_files_and_dirs(
2809 expand_T *xp,
2810 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002811 char_u ***matches,
2812 int *numMatches,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002813 int flags,
2814 int options)
2815{
2816 int free_pat = FALSE;
2817 int i;
2818 int ret;
2819
2820 // for ":set path=" and ":set tags=" halve backslashes for escaped
2821 // space
2822 if (xp->xp_backslash != XP_BS_NONE)
2823 {
2824 free_pat = TRUE;
2825 pat = vim_strsave(pat);
2826 for (i = 0; pat[i]; ++i)
2827 if (pat[i] == '\\')
2828 {
Yee Cheng Chin54844852023-10-09 18:12:31 +02002829 if (xp->xp_backslash & XP_BS_THREE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002830 && pat[i + 1] == '\\'
2831 && pat[i + 2] == '\\'
2832 && pat[i + 3] == ' ')
2833 STRMOVE(pat + i, pat + i + 3);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002834 else if (xp->xp_backslash & XP_BS_ONE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002835 && pat[i + 1] == ' ')
2836 STRMOVE(pat + i, pat + i + 1);
Yee Cheng Chin54844852023-10-09 18:12:31 +02002837 else if ((xp->xp_backslash & XP_BS_COMMA)
2838 && pat[i + 1] == '\\'
2839 && pat[i + 2] == ',')
2840 STRMOVE(pat + i, pat + i + 2);
2841#ifdef BACKSLASH_IN_FILENAME
2842 else if ((xp->xp_backslash & XP_BS_COMMA)
2843 && pat[i + 1] == ',')
2844 STRMOVE(pat + i, pat + i + 1);
2845#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002846 }
2847 }
2848
2849 if (xp->xp_context == EXPAND_FILES)
2850 flags |= EW_FILE;
2851 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2852 flags |= (EW_FILE | EW_PATH);
LemonBoya20bf692024-07-11 22:35:53 +02002853 else if (xp->xp_context == EXPAND_DIRS_IN_CDPATH)
2854 flags = (flags | EW_DIR | EW_CDPATH) & ~EW_FILE;
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002855 else
2856 flags = (flags | EW_DIR) & ~EW_FILE;
2857 if (options & WILD_ICASE)
2858 flags |= EW_ICASE;
2859
2860 // Expand wildcards, supporting %:h and the like.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002861 ret = expand_wildcards_eval(&pat, numMatches, matches, flags);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002862 if (free_pat)
2863 vim_free(pat);
2864#ifdef BACKSLASH_IN_FILENAME
2865 if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2866 {
2867 int j;
2868
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002869 for (j = 0; j < *numMatches; ++j)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002870 {
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002871 char_u *ptr = (*matches)[j];
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002872
2873 while (*ptr != NUL)
2874 {
2875 if (p_csl[0] == 's' && *ptr == '\\')
2876 *ptr = '/';
2877 else if (p_csl[0] == 'b' && *ptr == '/')
2878 *ptr = '\\';
2879 ptr += (*mb_ptr2len)(ptr);
2880 }
2881 }
2882 }
2883#endif
2884 return ret;
2885}
2886
2887/*
Bram Moolenaard0190392019-08-23 21:17:35 +02002888 * Function given to ExpandGeneric() to obtain the possible arguments of the
2889 * ":behave {mswin,xterm}" command.
2890 */
2891 static char_u *
2892get_behave_arg(expand_T *xp UNUSED, int idx)
2893{
2894 if (idx == 0)
2895 return (char_u *)"mswin";
2896 if (idx == 1)
2897 return (char_u *)"xterm";
2898 return NULL;
2899}
2900
K.Takata161b6ac2022-11-14 15:31:07 +00002901#ifdef FEAT_EVAL
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002902/*
2903 * Function given to ExpandGeneric() to obtain the possible arguments of the
2904 * ":breakadd {expr, file, func, here}" command.
2905 * ":breakdel {func, file, here}" command.
2906 */
2907 static char_u *
2908get_breakadd_arg(expand_T *xp UNUSED, int idx)
2909{
2910 char *opts[] = {"expr", "file", "func", "here"};
2911
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00002912 if (idx >= 0 && idx <= 3)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002913 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002914 // breakadd {expr, file, func, here}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002915 if (breakpt_expand_what == EXP_BREAKPT_ADD)
2916 return (char_u *)opts[idx];
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002917 else if (breakpt_expand_what == EXP_BREAKPT_DEL)
2918 {
2919 // breakdel {func, file, here}
2920 if (idx <= 2)
2921 return (char_u *)opts[idx + 1];
2922 }
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002923 else
2924 {
Yegappan Lakshmanan1fdf84e2022-03-15 10:53:09 +00002925 // profdel {func, file}
2926 if (idx <= 1)
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002927 return (char_u *)opts[idx + 1];
2928 }
2929 }
2930 return NULL;
2931}
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00002932
2933/*
2934 * Function given to ExpandGeneric() to obtain the possible arguments for the
2935 * ":scriptnames" command.
2936 */
2937 static char_u *
2938get_scriptnames_arg(expand_T *xp UNUSED, int idx)
2939{
2940 scriptitem_T *si;
2941
2942 if (!SCRIPT_ID_VALID(idx + 1))
2943 return NULL;
2944
2945 si = SCRIPT_ITEM(idx + 1);
2946 home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE);
2947 return NameBuff;
2948}
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00002949#endif
2950
Bram Moolenaard0190392019-08-23 21:17:35 +02002951/*
2952 * Function given to ExpandGeneric() to obtain the possible arguments of the
2953 * ":messages {clear}" command.
2954 */
2955 static char_u *
2956get_messages_arg(expand_T *xp UNUSED, int idx)
2957{
2958 if (idx == 0)
2959 return (char_u *)"clear";
2960 return NULL;
2961}
2962
2963 static char_u *
2964get_mapclear_arg(expand_T *xp UNUSED, int idx)
2965{
2966 if (idx == 0)
2967 return (char_u *)"<buffer>";
2968 return NULL;
2969}
2970
2971/*
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002972 * Do the expansion based on xp->xp_context and 'rmp'.
2973 */
2974 static int
2975ExpandOther(
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002976 char_u *pat,
Bram Moolenaar29ab6ce2022-02-26 15:52:08 +00002977 expand_T *xp,
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002978 regmatch_T *rmp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00002979 char_u ***matches,
2980 int *numMatches)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00002981{
2982 static struct expgen
2983 {
2984 int context;
2985 char_u *((*func)(expand_T *, int));
2986 int ic;
2987 int escaped;
2988 } tab[] =
2989 {
2990 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2991 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2992 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2993 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2994 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2995 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2996 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2997 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2998 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2999 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003000#ifdef FEAT_EVAL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003001 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
3002 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
3003 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
3004 {EXPAND_DISASSEMBLE, get_disassemble_argument, FALSE, TRUE},
3005 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003006#endif
3007#ifdef FEAT_MENU
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003008 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
3009 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003010#endif
3011#ifdef FEAT_SYN_HL
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003012 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003013#endif
3014#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003015 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003016#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003017 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
3018 {EXPAND_EVENTS, get_event_name, TRUE, FALSE},
3019 {EXPAND_AUGROUP, get_augroup_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003020#ifdef FEAT_CSCOPE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003021 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003022#endif
3023#ifdef FEAT_SIGNS
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003024 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003025#endif
3026#ifdef FEAT_PROFILE
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003027 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003028#endif
3029#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003030 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
3031 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003032#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003033 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
3034 {EXPAND_USER, get_users, TRUE, FALSE},
3035 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003036#ifdef FEAT_EVAL
3037 {EXPAND_BREAKPOINT, get_breakadd_arg, TRUE, TRUE},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003038 {EXPAND_SCRIPTNAMES, get_scriptnames_arg, TRUE, FALSE},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +00003039#endif
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003040 };
3041 int i;
3042 int ret = FAIL;
3043
3044 // Find a context in the table and call the ExpandGeneric() with the
3045 // right function to do the expansion.
3046 for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
3047 {
3048 if (xp->xp_context == tab[i].context)
3049 {
3050 if (tab[i].ic)
3051 rmp->rm_ic = TRUE;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003052 ret = ExpandGeneric(pat, xp, rmp, matches, numMatches,
3053 tab[i].func, tab[i].escaped);
Yegappan Lakshmanan620d8ed2022-02-12 12:03:07 +00003054 break;
3055 }
3056 }
3057
3058 return ret;
3059}
3060
3061/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003062 * Map wild expand options to flags for expand_wildcards()
3063 */
3064 static int
3065map_wildopts_to_ewflags(int options)
3066{
3067 int flags;
3068
3069 flags = EW_DIR; // include directories
3070 if (options & WILD_LIST_NOTFOUND)
3071 flags |= EW_NOTFOUND;
3072 if (options & WILD_ADD_SLASH)
3073 flags |= EW_ADDSLASH;
3074 if (options & WILD_KEEP_ALL)
3075 flags |= EW_KEEPALL;
3076 if (options & WILD_SILENT)
3077 flags |= EW_SILENT;
3078 if (options & WILD_NOERROR)
3079 flags |= EW_NOERROR;
3080 if (options & WILD_ALLLINKS)
3081 flags |= EW_ALLLINKS;
3082
3083 return flags;
3084}
3085
3086/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003087 * Do the expansion based on xp->xp_context and "pat".
3088 */
3089 static int
3090ExpandFromContext(
3091 expand_T *xp,
3092 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003093 char_u ***matches,
3094 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003095 int options) // WILD_ flags
3096{
Bram Moolenaar66b51422019-08-18 21:44:12 +02003097 regmatch_T regmatch;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003098 int ret;
3099 int flags;
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003100 char_u *tofree = NULL;
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003101 int fuzzy = cmdline_fuzzy_complete(pat)
3102 && cmdline_fuzzy_completion_supported(xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003103
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003104 flags = map_wildopts_to_ewflags(options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003105
3106 if (xp->xp_context == EXPAND_FILES
3107 || xp->xp_context == EXPAND_DIRECTORIES
LemonBoya20bf692024-07-11 22:35:53 +02003108 || xp->xp_context == EXPAND_FILES_IN_PATH
3109 || xp->xp_context == EXPAND_DIRS_IN_CDPATH)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003110 return expand_files_and_dirs(xp, pat, matches, numMatches, flags,
3111 options);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003112
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003113 *matches = (char_u **)"";
3114 *numMatches = 0;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003115 if (xp->xp_context == EXPAND_HELP)
3116 {
3117 // With an empty argument we would get all the help tags, which is
3118 // very slow. Get matches for "help" instead.
3119 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003120 numMatches, matches, FALSE) == OK)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003121 {
3122#ifdef FEAT_MULTI_LANG
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003123 cleanup_help_tags(*numMatches, *matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003124#endif
3125 return OK;
3126 }
3127 return FAIL;
3128 }
3129
Bram Moolenaar66b51422019-08-18 21:44:12 +02003130 if (xp->xp_context == EXPAND_SHELLCMD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003131 return expand_shellcmd(pat, matches, numMatches, flags);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003132 if (xp->xp_context == EXPAND_OLD_SETTING)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003133 return ExpandOldSetting(numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003134 if (xp->xp_context == EXPAND_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003135 return ExpandBufnames(pat, numMatches, matches, options);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003136#ifdef FEAT_DIFF
3137 if (xp->xp_context == EXPAND_DIFF_BUFFERS)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003138 return ExpandBufnames(pat, numMatches, matches,
3139 options | BUF_DIFF_FILTER);
Bram Moolenaarae7dba82019-12-29 13:56:33 +01003140#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003141 if (xp->xp_context == EXPAND_TAGS
3142 || xp->xp_context == EXPAND_TAGS_LISTFILES)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003143 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, numMatches,
3144 matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003145 if (xp->xp_context == EXPAND_COLORS)
3146 {
3147 char *directories[] = {"colors", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003148 return ExpandRTDir(pat, DIP_START + DIP_OPT, numMatches, matches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003149 directories);
3150 }
3151 if (xp->xp_context == EXPAND_COMPILER)
3152 {
3153 char *directories[] = {"compiler", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003154 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003155 }
3156 if (xp->xp_context == EXPAND_OWNSYNTAX)
3157 {
3158 char *directories[] = {"syntax", 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_FILETYPE)
3162 {
3163 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003164 return ExpandRTDir(pat, 0, numMatches, matches, directories);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003165 }
Doug Kearns81642d92024-01-04 22:37:44 +01003166#ifdef FEAT_KEYMAP
3167 if (xp->xp_context == EXPAND_KEYMAP)
3168 {
3169 char *directories[] = {"keymap", NULL};
3170 return ExpandRTDir(pat, 0, numMatches, matches, directories);
3171 }
3172#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003173#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003174 if (xp->xp_context == EXPAND_USER_LIST)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003175 return ExpandUserList(xp, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003176#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003177 if (xp->xp_context == EXPAND_PACKADD)
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003178 return ExpandPackAddDir(pat, numMatches, matches);
zeertzjq5c8771b2023-01-24 12:34:03 +00003179 if (xp->xp_context == EXPAND_RUNTIME)
3180 return expand_runtime_cmd(pat, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003181
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003182 // When expanding a function name starting with s:, match the <SNR>nr_
3183 // prefix.
Bram Moolenaar47016f52021-08-26 16:39:58 +02003184 if ((xp->xp_context == EXPAND_USER_FUNC
3185 || xp->xp_context == EXPAND_DISASSEMBLE)
3186 && STRNCMP(pat, "^s:", 3) == 0)
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003187 {
3188 int len = (int)STRLEN(pat) + 20;
3189
3190 tofree = alloc(len);
Yegappan Lakshmanane3846cf2022-02-15 11:35:54 +00003191 if (tofree == NULL)
3192 return FAIL;
Bram Moolenaarb54b8e02020-03-01 01:05:53 +01003193 vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003194 pat = tofree;
3195 }
3196
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003197 if (!fuzzy)
3198 {
3199 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
3200 if (regmatch.regprog == NULL)
3201 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003202
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003203 // set ignore-case according to p_ic, p_scs and pat
3204 regmatch.rm_ic = ignorecase(pat);
3205 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003206
3207 if (xp->xp_context == EXPAND_SETTINGS
3208 || xp->xp_context == EXPAND_BOOL_SETTINGS)
Christian Brabandtcb747892022-05-08 21:10:56 +01003209 ret = ExpandSettings(xp, &regmatch, pat, numMatches, matches, fuzzy);
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003210 else if (xp->xp_context == EXPAND_STRING_SETTING)
3211 ret = ExpandStringSetting(xp, &regmatch, numMatches, matches);
3212 else if (xp->xp_context == EXPAND_SETTING_SUBTRACT)
3213 ret = ExpandSettingSubtract(xp, &regmatch, numMatches, matches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003214 else if (xp->xp_context == EXPAND_MAPPINGS)
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00003215 ret = ExpandMappings(pat, &regmatch, numMatches, matches);
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003216 else if (xp->xp_context == EXPAND_ARGOPT)
3217 ret = expand_argopt(pat, xp, &regmatch, matches, numMatches);
3218#if defined(FEAT_TERMINAL)
3219 else if (xp->xp_context == EXPAND_TERMINALOPT)
3220 ret = expand_terminal_opt(pat, xp, &regmatch, matches, numMatches);
3221#endif
K.Takata161b6ac2022-11-14 15:31:07 +00003222#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003223 else if (xp->xp_context == EXPAND_USER_DEFINED)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003224 ret = ExpandUserDefined(pat, xp, &regmatch, matches, numMatches);
K.Takata161b6ac2022-11-14 15:31:07 +00003225#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003226 else
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003227 ret = ExpandOther(pat, xp, &regmatch, matches, numMatches);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003228
Yegappan Lakshmanan00333cb2022-02-26 16:05:08 +00003229 if (!fuzzy)
3230 vim_regfree(regmatch.regprog);
Bram Moolenaarcc390ff2020-02-29 22:06:30 +01003231 vim_free(tofree);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003232
3233 return ret;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003234}
3235
Bram Moolenaar66b51422019-08-18 21:44:12 +02003236/*
3237 * Expand a list of names.
3238 *
3239 * Generic function for command line completion. It calls a function to
3240 * obtain strings, one by one. The strings are matched against a regexp
3241 * program. Matching strings are copied into an array, which is returned.
3242 *
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003243 * If 'fuzzy' is TRUE, then fuzzy matching is used. Otherwise, regex matching
3244 * is used.
3245 *
Bram Moolenaar66b51422019-08-18 21:44:12 +02003246 * Returns OK when no problems encountered, FAIL for error (out of memory).
3247 */
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003248 int
Bram Moolenaar66b51422019-08-18 21:44:12 +02003249ExpandGeneric(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003250 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003251 expand_T *xp,
3252 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003253 char_u ***matches,
3254 int *numMatches,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003255 char_u *((*func)(expand_T *, int)),
3256 // returns a string from the list
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003257 int escaped)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003258{
3259 int i;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003260 garray_T ga;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003261 char_u *str;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003262 fuzmatch_str_T *fuzmatch = NULL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003263 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003264 int fuzzy;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003265 int match;
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003266 int sort_matches = FALSE;
3267 int funcsort = FALSE;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003268
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003269 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003270 *matches = NULL;
3271 *numMatches = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003272
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003273 if (!fuzzy)
3274 ga_init2(&ga, sizeof(char *), 30);
3275 else
3276 ga_init2(&ga, sizeof(fuzmatch_str_T), 30);
3277
3278 for (i = 0; ; ++i)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003279 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003280 str = (*func)(xp, i);
3281 if (str == NULL) // end of list
3282 break;
3283 if (*str == NUL) // skip empty strings
3284 continue;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003285
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003286 if (xp->xp_pattern[0] != NUL)
3287 {
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003288 if (!fuzzy)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003289 match = vim_regexec(regmatch, str, (colnr_T)0);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003290 else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003291 {
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003292 score = fuzzy_match_str(str, pat);
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00003293 match = (score != 0);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003294 }
3295 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003296 else
3297 match = TRUE;
3298
3299 if (!match)
3300 continue;
3301
3302 if (escaped)
3303 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
3304 else
3305 str = vim_strsave(str);
3306 if (str == NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003307 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003308 if (!fuzzy)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003309 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003310 ga_clear_strings(&ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003311 return FAIL;
3312 }
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01003313 fuzmatch_str_free(ga.ga_data, ga.ga_len);
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003314 return FAIL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003315 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003316
3317 if (ga_grow(&ga, 1) == FAIL)
3318 {
3319 vim_free(str);
3320 break;
3321 }
3322
3323 if (fuzzy)
3324 {
3325 fuzmatch = &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
3326 fuzmatch->idx = ga.ga_len;
3327 fuzmatch->str = str;
3328 fuzmatch->score = score;
3329 }
3330 else
3331 ((char_u **)ga.ga_data)[ga.ga_len] = str;
3332
K.Takata161b6ac2022-11-14 15:31:07 +00003333#ifdef FEAT_MENU
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003334 if (func == get_menu_names)
3335 {
3336 // test for separator added by get_menu_names()
3337 str += STRLEN(str) - 1;
3338 if (*str == '\001')
3339 *str = '.';
3340 }
K.Takata161b6ac2022-11-14 15:31:07 +00003341#endif
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003342
3343 ++ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003344 }
3345
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003346 if (ga.ga_len == 0)
3347 return OK;
3348
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003349 // sort the matches when using regular expression matching and sorting
3350 // applies to the completion context. Menus and scriptnames should be kept
3351 // in the specified order.
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003352 if (!fuzzy && xp->xp_context != EXPAND_MENUNAMES
Yee Cheng Chin900894b2023-09-29 20:42:32 +02003353 && xp->xp_context != EXPAND_STRING_SETTING
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003354 && xp->xp_context != EXPAND_MENUS
Yee Cheng Chin989426b2023-10-14 11:46:51 +02003355 && xp->xp_context != EXPAND_SCRIPTNAMES
3356 && xp->xp_context != EXPAND_ARGOPT
3357 && xp->xp_context != EXPAND_TERMINALOPT)
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003358 sort_matches = TRUE;
3359
3360 // <SNR> functions should be sorted to the end.
3361 if (xp->xp_context == EXPAND_EXPRESSION
3362 || xp->xp_context == EXPAND_FUNCTIONS
3363 || xp->xp_context == EXPAND_USER_FUNC
3364 || xp->xp_context == EXPAND_DISASSEMBLE)
3365 funcsort = TRUE;
3366
3367 // Sort the matches.
3368 if (sort_matches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003369 {
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00003370 if (funcsort)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003371 // <SNR> functions should be sorted to the end.
3372 qsort((void *)ga.ga_data, (size_t)ga.ga_len, sizeof(char_u *),
3373 sort_func_compare);
3374 else
3375 sort_strings((char_u **)ga.ga_data, ga.ga_len);
3376 }
3377
3378 if (!fuzzy)
3379 {
3380 *matches = ga.ga_data;
3381 *numMatches = ga.ga_len;
3382 }
3383 else
3384 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003385 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3386 funcsort) == FAIL)
3387 return FAIL;
3388 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003389 }
3390
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003391#if defined(FEAT_SYN_HL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003392 // Reset the variables used for special highlight names expansion, so that
3393 // they don't show up when getting normal highlight names by ID.
3394 reset_expand_highlight();
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003395#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003396
Bram Moolenaar66b51422019-08-18 21:44:12 +02003397 return OK;
3398}
3399
3400/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003401 * Expand shell command matches in one directory of $PATH.
3402 */
3403 static void
3404expand_shellcmd_onedir(
3405 char_u *buf,
3406 char_u *s,
3407 size_t l,
3408 char_u *pat,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003409 char_u ***matches,
3410 int *numMatches,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003411 int flags,
3412 hashtab_T *ht,
3413 garray_T *gap)
3414{
3415 int ret;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003416 hash_T hash;
3417 hashitem_T *hi;
3418
3419 vim_strncpy(buf, s, l);
3420 add_pathsep(buf);
3421 l = STRLEN(buf);
3422 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
3423
3424 // Expand matches in one directory of $PATH.
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003425 ret = expand_wildcards(1, &buf, numMatches, matches, flags);
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003426 if (ret != OK)
3427 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003428
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003429 if (ga_grow(gap, *numMatches) == FAIL)
3430 {
3431 FreeWild(*numMatches, *matches);
3432 return;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003433 }
Yegappan Lakshmanan68353e52022-11-13 22:38:10 +00003434
3435 for (int i = 0; i < *numMatches; ++i)
3436 {
3437 char_u *name = (*matches)[i];
3438
3439 if (STRLEN(name) > l)
3440 {
3441 // Check if this name was already found.
3442 hash = hash_hash(name + l);
3443 hi = hash_lookup(ht, name + l, hash);
3444 if (HASHITEM_EMPTY(hi))
3445 {
3446 // Remove the path that was prepended.
3447 STRMOVE(name, name + l);
3448 ((char_u **)gap->ga_data)[gap->ga_len++] = name;
3449 hash_add_item(ht, hi, name, hash);
3450 name = NULL;
3451 }
3452 }
3453 vim_free(name);
3454 }
3455 vim_free(*matches);
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003456}
3457
3458/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003459 * Complete a shell command.
3460 * Returns FAIL or OK;
3461 */
3462 static int
3463expand_shellcmd(
3464 char_u *filepat, // pattern to match with command names
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003465 char_u ***matches, // return: array with matches
3466 int *numMatches, // return: number of matches
Bram Moolenaar66b51422019-08-18 21:44:12 +02003467 int flagsarg) // EW_ flags
3468{
3469 char_u *pat;
3470 int i;
3471 char_u *path = NULL;
3472 int mustfree = FALSE;
3473 garray_T ga;
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003474 char_u *buf;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003475 size_t l;
3476 char_u *s, *e;
3477 int flags = flagsarg;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003478 int did_curdir = FALSE;
3479 hashtab_T found_ht;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003480
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003481 buf = alloc(MAXPATHL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003482 if (buf == NULL)
3483 return FAIL;
3484
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003485 // for ":set path=" and ":set tags=" halve backslashes for escaped space
Bram Moolenaar66b51422019-08-18 21:44:12 +02003486 pat = vim_strsave(filepat);
Bram Moolenaar8b7aa2f2020-01-07 21:05:49 +01003487 if (pat == NULL)
3488 {
3489 vim_free(buf);
3490 return FAIL;
3491 }
3492
Bram Moolenaar66b51422019-08-18 21:44:12 +02003493 for (i = 0; pat[i]; ++i)
3494 if (pat[i] == '\\' && pat[i + 1] == ' ')
3495 STRMOVE(pat + i, pat + i + 1);
3496
3497 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
3498
3499 if (pat[0] == '.' && (vim_ispathsep(pat[1])
3500 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
3501 path = (char_u *)".";
3502 else
3503 {
3504 // For an absolute name we don't use $PATH.
3505 if (!mch_isFullName(pat))
3506 path = vim_getenv((char_u *)"PATH", &mustfree);
3507 if (path == NULL)
3508 path = (char_u *)"";
3509 }
3510
3511 // Go over all directories in $PATH. Expand matches in that directory and
3512 // collect them in "ga". When "." is not in $PATH also expand for the
3513 // current directory, to find "subdir/cmd".
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003514 ga_init2(&ga, sizeof(char *), 10);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003515 hash_init(&found_ht);
3516 for (s = path; ; s = e)
3517 {
K.Takata161b6ac2022-11-14 15:31:07 +00003518#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003519 e = vim_strchr(s, ';');
K.Takata161b6ac2022-11-14 15:31:07 +00003520#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003521 e = vim_strchr(s, ':');
K.Takata161b6ac2022-11-14 15:31:07 +00003522#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003523 if (e == NULL)
3524 e = s + STRLEN(s);
3525
3526 if (*s == NUL)
3527 {
3528 if (did_curdir)
3529 break;
3530 // Find directories in the current directory, path is empty.
3531 did_curdir = TRUE;
3532 flags |= EW_DIR;
3533 }
3534 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
3535 {
3536 did_curdir = TRUE;
3537 flags |= EW_DIR;
3538 }
3539 else
3540 // Do not match directories inside a $PATH item.
3541 flags &= ~EW_DIR;
3542
3543 l = e - s;
3544 if (l > MAXPATHL - 5)
3545 break;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003546
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003547 expand_shellcmd_onedir(buf, s, l, pat, matches, numMatches, flags,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003548 &found_ht, &ga);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003549
Bram Moolenaar66b51422019-08-18 21:44:12 +02003550 if (*e != NUL)
3551 ++e;
3552 }
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003553 *matches = ga.ga_data;
3554 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003555
3556 vim_free(buf);
3557 vim_free(pat);
3558 if (mustfree)
3559 vim_free(path);
3560 hash_clear(&found_ht);
3561 return OK;
3562}
3563
K.Takata161b6ac2022-11-14 15:31:07 +00003564#if defined(FEAT_EVAL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003565/*
3566 * Call "user_expand_func()" to invoke a user defined Vim script function and
Bram Moolenaarc841aff2020-07-25 14:11:55 +02003567 * return the result (either a string, a List or NULL).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003568 */
3569 static void *
3570call_user_expand_func(
3571 void *(*user_expand_func)(char_u *, int, typval_T *),
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003572 expand_T *xp)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003573{
3574 cmdline_info_T *ccline = get_cmdline_info();
3575 int keep = 0;
3576 typval_T args[4];
3577 sctx_T save_current_sctx = current_sctx;
3578 char_u *pat = NULL;
3579 void *ret;
3580
3581 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
3582 return NULL;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003583
3584 if (ccline->cmdbuff != NULL)
3585 {
3586 keep = ccline->cmdbuff[ccline->cmdlen];
3587 ccline->cmdbuff[ccline->cmdlen] = 0;
3588 }
3589
3590 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
3591
3592 args[0].v_type = VAR_STRING;
3593 args[0].vval.v_string = pat;
3594 args[1].v_type = VAR_STRING;
3595 args[1].vval.v_string = xp->xp_line;
3596 args[2].v_type = VAR_NUMBER;
3597 args[2].vval.v_number = xp->xp_col;
3598 args[3].v_type = VAR_UNKNOWN;
3599
3600 current_sctx = xp->xp_script_ctx;
3601
3602 ret = user_expand_func(xp->xp_arg, 3, args);
3603
3604 current_sctx = save_current_sctx;
3605 if (ccline->cmdbuff != NULL)
3606 ccline->cmdbuff[ccline->cmdlen] = keep;
3607
3608 vim_free(pat);
3609 return ret;
3610}
3611
3612/*
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003613 * Expand names with a function defined by the user (EXPAND_USER_DEFINED and
3614 * EXPAND_USER_LIST).
Bram Moolenaar66b51422019-08-18 21:44:12 +02003615 */
3616 static int
3617ExpandUserDefined(
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003618 char_u *pat,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003619 expand_T *xp,
3620 regmatch_T *regmatch,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003621 char_u ***matches,
3622 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003623{
3624 char_u *retstr;
3625 char_u *s;
3626 char_u *e;
3627 int keep;
3628 garray_T ga;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003629 int fuzzy;
3630 int match;
Bram Moolenaar3e7637b2022-02-28 21:02:19 +00003631 int score = 0;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003632
3633 fuzzy = cmdline_fuzzy_complete(pat);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003634 *matches = NULL;
3635 *numMatches = 0;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003636
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003637 retstr = call_user_expand_func(call_func_retstr, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003638 if (retstr == NULL)
3639 return FAIL;
3640
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003641 if (!fuzzy)
3642 ga_init2(&ga, sizeof(char *), 3);
3643 else
3644 ga_init2(&ga, sizeof(fuzmatch_str_T), 3);
3645
Bram Moolenaar66b51422019-08-18 21:44:12 +02003646 for (s = retstr; *s != NUL; s = e)
3647 {
3648 e = vim_strchr(s, '\n');
3649 if (e == NULL)
3650 e = s + STRLEN(s);
3651 keep = *e;
3652 *e = NUL;
3653
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003654 if (xp->xp_pattern[0] != NUL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003655 {
3656 if (!fuzzy)
3657 match = vim_regexec(regmatch, s, (colnr_T)0);
3658 else
3659 {
3660 score = fuzzy_match_str(s, pat);
3661 match = (score != 0);
3662 }
3663 }
3664 else
3665 match = TRUE; // match everything
3666
Bram Moolenaar66b51422019-08-18 21:44:12 +02003667 *e = keep;
3668
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003669 if (match)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003670 {
3671 if (ga_grow(&ga, 1) == FAIL)
3672 break;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003673 if (!fuzzy)
3674 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, e - s);
3675 else
3676 {
3677 fuzmatch_str_T *fuzmatch =
3678 &((fuzmatch_str_T *)ga.ga_data)[ga.ga_len];
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003679 fuzmatch->idx = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003680 fuzmatch->str = vim_strnsave(s, e - s);
3681 fuzmatch->score = score;
3682 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003683 ++ga.ga_len;
3684 }
3685
3686 if (*e != NUL)
3687 ++e;
3688 }
3689 vim_free(retstr);
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003690
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003691 if (ga.ga_len == 0)
3692 return OK;
3693
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003694 if (!fuzzy)
3695 {
3696 *matches = ga.ga_data;
3697 *numMatches = ga.ga_len;
3698 }
3699 else
3700 {
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003701 if (fuzzymatches_to_strmatches(ga.ga_data, matches, ga.ga_len,
3702 FALSE) == FAIL)
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003703 return FAIL;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00003704 *numMatches = ga.ga_len;
Yegappan Lakshmananafd4ae32022-02-27 21:03:21 +00003705 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02003706 return OK;
3707}
3708
3709/*
3710 * Expand names with a list returned by a function defined by the user.
3711 */
3712 static int
3713ExpandUserList(
3714 expand_T *xp,
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003715 char_u ***matches,
3716 int *numMatches)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003717{
3718 list_T *retlist;
3719 listitem_T *li;
3720 garray_T ga;
3721
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003722 *matches = NULL;
3723 *numMatches = 0;
3724 retlist = call_user_expand_func(call_func_retlist, xp);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003725 if (retlist == NULL)
3726 return FAIL;
3727
Bram Moolenaar04935fb2022-01-08 16:19:22 +00003728 ga_init2(&ga, sizeof(char *), 3);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003729 // Loop over the items in the list.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003730 FOR_ALL_LIST_ITEMS(retlist, li)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003731 {
3732 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
3733 continue; // Skip non-string items and empty strings
3734
3735 if (ga_grow(&ga, 1) == FAIL)
3736 break;
3737
3738 ((char_u **)ga.ga_data)[ga.ga_len] =
3739 vim_strsave(li->li_tv.vval.v_string);
3740 ++ga.ga_len;
3741 }
3742 list_unref(retlist);
3743
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003744 *matches = ga.ga_data;
3745 *numMatches = ga.ga_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003746 return OK;
3747}
K.Takata161b6ac2022-11-14 15:31:07 +00003748#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003749
3750/*
Bram Moolenaar66b51422019-08-18 21:44:12 +02003751 * Expand "file" for all comma-separated directories in "path".
3752 * Adds the matches to "ga". Caller must init "ga".
zeertzjq3770f4c2023-01-22 18:38:51 +00003753 * If "dirs" is TRUE only expand directory names.
Bram Moolenaar66b51422019-08-18 21:44:12 +02003754 */
3755 void
3756globpath(
3757 char_u *path,
3758 char_u *file,
3759 garray_T *ga,
zeertzjq3770f4c2023-01-22 18:38:51 +00003760 int expand_options,
3761 int dirs)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003762{
3763 expand_T xpc;
3764 char_u *buf;
3765 int i;
3766 int num_p;
3767 char_u **p;
3768
3769 buf = alloc(MAXPATHL);
3770 if (buf == NULL)
3771 return;
3772
3773 ExpandInit(&xpc);
zeertzjq3770f4c2023-01-22 18:38:51 +00003774 xpc.xp_context = dirs ? EXPAND_DIRECTORIES : EXPAND_FILES;
Bram Moolenaar66b51422019-08-18 21:44:12 +02003775
3776 // Loop over all entries in {path}.
3777 while (*path != NUL)
3778 {
3779 // Copy one item of the path to buf[] and concatenate the file name.
3780 copy_option_part(&path, buf, MAXPATHL, ",");
3781 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
3782 {
K.Takata161b6ac2022-11-14 15:31:07 +00003783#if defined(MSWIN)
Bram Moolenaar66b51422019-08-18 21:44:12 +02003784 // Using the platform's path separator (\) makes vim incorrectly
3785 // treat it as an escape character, use '/' instead.
3786 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
3787 STRCAT(buf, "/");
K.Takata161b6ac2022-11-14 15:31:07 +00003788#else
Bram Moolenaar66b51422019-08-18 21:44:12 +02003789 add_pathsep(buf);
K.Takata161b6ac2022-11-14 15:31:07 +00003790#endif
Bram Moolenaar66b51422019-08-18 21:44:12 +02003791 STRCAT(buf, file);
Yegappan Lakshmanan24384302022-02-17 11:26:42 +00003792 if (ExpandFromContext(&xpc, buf, &p, &num_p,
Bram Moolenaar66b51422019-08-18 21:44:12 +02003793 WILD_SILENT|expand_options) != FAIL && num_p > 0)
3794 {
3795 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
3796
3797 if (ga_grow(ga, num_p) == OK)
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003798 // take over the pointers and put them in "ga"
Bram Moolenaar66b51422019-08-18 21:44:12 +02003799 for (i = 0; i < num_p; ++i)
3800 {
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003801 ((char_u **)ga->ga_data)[ga->ga_len] = p[i];
Bram Moolenaar66b51422019-08-18 21:44:12 +02003802 ++ga->ga_len;
3803 }
Bram Moolenaarf0f80552020-01-05 22:05:49 +01003804 vim_free(p);
Bram Moolenaar66b51422019-08-18 21:44:12 +02003805 }
3806 }
3807 }
3808
3809 vim_free(buf);
3810}
Bram Moolenaar66b51422019-08-18 21:44:12 +02003811
Bram Moolenaareadee482020-09-04 15:37:31 +02003812/*
3813 * Translate some keys pressed when 'wildmenu' is used.
3814 */
3815 int
3816wildmenu_translate_key(
3817 cmdline_info_T *cclp,
3818 int key,
3819 expand_T *xp,
3820 int did_wild_list)
3821{
3822 int c = key;
3823
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003824 if (cmdline_pum_active())
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003825 {
Yegappan Lakshmanan560dff42022-02-10 19:52:10 +00003826 // When the popup menu is used for cmdline completion:
3827 // Up : go to the previous item in the menu
3828 // Down : go to the next item in the menu
3829 // Left : go to the parent directory
3830 // Right: list the files in the selected directory
3831 switch (c)
3832 {
3833 case K_UP: c = K_LEFT; break;
3834 case K_DOWN: c = K_RIGHT; break;
3835 case K_LEFT: c = K_UP; break;
3836 case K_RIGHT: c = K_DOWN; break;
3837 default: break;
3838 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003839 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003840
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003841 if (did_wild_list)
Bram Moolenaareadee482020-09-04 15:37:31 +02003842 {
3843 if (c == K_LEFT)
3844 c = Ctrl_P;
3845 else if (c == K_RIGHT)
3846 c = Ctrl_N;
3847 }
Yegappan Lakshmanan3908ef52022-02-08 12:08:07 +00003848
Bram Moolenaareadee482020-09-04 15:37:31 +02003849 // Hitting CR after "emenu Name.": complete submenu
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003850 if (xp->xp_context == EXPAND_MENUNAMES
Bram Moolenaareadee482020-09-04 15:37:31 +02003851 && cclp->cmdpos > 1
3852 && cclp->cmdbuff[cclp->cmdpos - 1] == '.'
3853 && cclp->cmdbuff[cclp->cmdpos - 2] != '\\'
3854 && (c == '\n' || c == '\r' || c == K_KENTER))
3855 c = K_DOWN;
3856
3857 return c;
3858}
3859
3860/*
3861 * Delete characters on the command line, from "from" to the current
3862 * position.
3863 */
3864 static void
3865cmdline_del(cmdline_info_T *cclp, int from)
3866{
3867 mch_memmove(cclp->cmdbuff + from, cclp->cmdbuff + cclp->cmdpos,
3868 (size_t)(cclp->cmdlen - cclp->cmdpos + 1));
3869 cclp->cmdlen -= cclp->cmdpos - from;
3870 cclp->cmdpos = from;
3871}
3872
3873/*
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003874 * Handle a key pressed when the wild menu for the menu names
3875 * (EXPAND_MENUNAMES) is displayed.
Bram Moolenaareadee482020-09-04 15:37:31 +02003876 */
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003877 static int
3878wildmenu_process_key_menunames(cmdline_info_T *cclp, int key, expand_T *xp)
Bram Moolenaareadee482020-09-04 15:37:31 +02003879{
Bram Moolenaareadee482020-09-04 15:37:31 +02003880 int i;
3881 int j;
3882
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003883 // Hitting <Down> after "emenu Name.": complete submenu
3884 if (key == K_DOWN && cclp->cmdpos > 0
3885 && cclp->cmdbuff[cclp->cmdpos - 1] == '.')
Bram Moolenaareadee482020-09-04 15:37:31 +02003886 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003887 key = p_wc;
3888 KeyTyped = TRUE; // in case the key was mapped
3889 }
3890 else if (key == K_UP)
3891 {
3892 // Hitting <Up>: Remove one submenu name in front of the
3893 // cursor
3894 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003895
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003896 j = (int)(xp->xp_pattern - cclp->cmdbuff);
3897 i = 0;
3898 while (--j > 0)
3899 {
3900 // check for start of menu name
3901 if (cclp->cmdbuff[j] == ' '
3902 && cclp->cmdbuff[j - 1] != '\\')
Bram Moolenaareadee482020-09-04 15:37:31 +02003903 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003904 i = j + 1;
3905 break;
3906 }
3907 // check for start of submenu name
3908 if (cclp->cmdbuff[j] == '.'
3909 && cclp->cmdbuff[j - 1] != '\\')
3910 {
3911 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02003912 {
3913 i = j + 1;
3914 break;
3915 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003916 else
3917 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003918 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003919 }
3920 if (i > 0)
3921 cmdline_del(cclp, i);
3922 key = p_wc;
3923 KeyTyped = TRUE; // in case the key was mapped
3924 xp->xp_context = EXPAND_NOTHING;
3925 }
3926
3927 return key;
3928}
3929
3930/*
3931 * Handle a key pressed when the wild menu for file names (EXPAND_FILES) or
3932 * directory names (EXPAND_DIRECTORIES) or shell command names
3933 * (EXPAND_SHELLCMD) is displayed.
3934 */
3935 static int
3936wildmenu_process_key_filenames(cmdline_info_T *cclp, int key, expand_T *xp)
3937{
3938 int i;
3939 int j;
3940 char_u upseg[5];
3941
3942 upseg[0] = PATHSEP;
3943 upseg[1] = '.';
3944 upseg[2] = '.';
3945 upseg[3] = PATHSEP;
3946 upseg[4] = NUL;
3947
3948 if (key == K_DOWN
3949 && cclp->cmdpos > 0
3950 && cclp->cmdbuff[cclp->cmdpos - 1] == PATHSEP
3951 && (cclp->cmdpos < 3
3952 || cclp->cmdbuff[cclp->cmdpos - 2] != '.'
3953 || cclp->cmdbuff[cclp->cmdpos - 3] != '.'))
3954 {
3955 // go down a directory
3956 key = p_wc;
3957 KeyTyped = TRUE; // in case the key was mapped
3958 }
3959 else if (STRNCMP(xp->xp_pattern, upseg + 1, 3) == 0 && key == K_DOWN)
3960 {
3961 // If in a direct ancestor, strip off one ../ to go down
3962 int found = FALSE;
3963
3964 j = cclp->cmdpos;
3965 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3966 while (--j > i)
3967 {
3968 if (has_mbyte)
3969 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3970 if (vim_ispathsep(cclp->cmdbuff[j]))
3971 {
3972 found = TRUE;
3973 break;
3974 }
3975 }
3976 if (found
3977 && cclp->cmdbuff[j - 1] == '.'
3978 && cclp->cmdbuff[j - 2] == '.'
3979 && (vim_ispathsep(cclp->cmdbuff[j - 3]) || j == i + 2))
3980 {
3981 cmdline_del(cclp, j - 2);
3982 key = p_wc;
Bram Moolenaarb0ac4ea2020-12-26 12:06:54 +01003983 KeyTyped = TRUE; // in case the key was mapped
Bram Moolenaareadee482020-09-04 15:37:31 +02003984 }
3985 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003986 else if (key == K_UP)
Bram Moolenaareadee482020-09-04 15:37:31 +02003987 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003988 // go up a directory
3989 int found = FALSE;
Bram Moolenaareadee482020-09-04 15:37:31 +02003990
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003991 j = cclp->cmdpos - 1;
3992 i = (int)(xp->xp_pattern - cclp->cmdbuff);
3993 while (--j > i)
Bram Moolenaareadee482020-09-04 15:37:31 +02003994 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003995 if (has_mbyte)
3996 j -= (*mb_head_off)(cclp->cmdbuff, cclp->cmdbuff + j);
3997 if (vim_ispathsep(cclp->cmdbuff[j])
K.Takata161b6ac2022-11-14 15:31:07 +00003998#ifdef BACKSLASH_IN_FILENAME
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00003999 && vim_strchr((char_u *)" *?[{`$%#",
4000 cclp->cmdbuff[j + 1]) == NULL
K.Takata161b6ac2022-11-14 15:31:07 +00004001#endif
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004002 )
Bram Moolenaareadee482020-09-04 15:37:31 +02004003 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004004 if (found)
Bram Moolenaareadee482020-09-04 15:37:31 +02004005 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004006 i = j + 1;
Bram Moolenaareadee482020-09-04 15:37:31 +02004007 break;
4008 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004009 else
4010 found = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004011 }
4012 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004013
4014 if (!found)
4015 j = i;
4016 else if (STRNCMP(cclp->cmdbuff + j, upseg, 4) == 0)
4017 j += 4;
4018 else if (STRNCMP(cclp->cmdbuff + j, upseg + 1, 3) == 0
4019 && j == i)
4020 j += 3;
4021 else
4022 j = 0;
4023 if (j > 0)
Bram Moolenaareadee482020-09-04 15:37:31 +02004024 {
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004025 // TODO this is only for DOS/UNIX systems - need to put in
4026 // machine-specific stuff here and in upseg init
4027 cmdline_del(cclp, j);
4028 put_on_cmdline(upseg + 1, 3, FALSE);
Bram Moolenaareadee482020-09-04 15:37:31 +02004029 }
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004030 else if (cclp->cmdpos > i)
4031 cmdline_del(cclp, i);
4032
4033 // Now complete in the new directory. Set KeyTyped in case the
4034 // Up key came from a mapping.
4035 key = p_wc;
4036 KeyTyped = TRUE;
Bram Moolenaareadee482020-09-04 15:37:31 +02004037 }
4038
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +00004039 return key;
4040}
4041
4042/*
4043 * Handle a key pressed when the wild menu is displayed
4044 */
4045 int
4046wildmenu_process_key(cmdline_info_T *cclp, int key, expand_T *xp)
4047{
4048 if (xp->xp_context == EXPAND_MENUNAMES)
4049 return wildmenu_process_key_menunames(cclp, key, xp);
4050 else if ((xp->xp_context == EXPAND_FILES
4051 || xp->xp_context == EXPAND_DIRECTORIES
4052 || xp->xp_context == EXPAND_SHELLCMD))
4053 return wildmenu_process_key_filenames(cclp, key, xp);
4054
4055 return key;
Bram Moolenaareadee482020-09-04 15:37:31 +02004056}
4057
4058/*
4059 * Free expanded names when finished walking through the matches
4060 */
4061 void
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004062wildmenu_cleanup(cmdline_info_T *cclp UNUSED)
Bram Moolenaareadee482020-09-04 15:37:31 +02004063{
4064 int skt = KeyTyped;
Bram Moolenaareadee482020-09-04 15:37:31 +02004065
4066 if (!p_wmnu || wild_menu_showing == 0)
4067 return;
4068
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004069#ifdef FEAT_EVAL
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004070 int save_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaareadee482020-09-04 15:37:31 +02004071 if (cclp->input_fn)
4072 RedrawingDisabled = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004073#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004074
4075 if (wild_menu_showing == WM_SCROLLED)
4076 {
4077 // Entered command line, move it up
4078 cmdline_row--;
4079 redrawcmd();
4080 }
4081 else if (save_p_ls != -1)
4082 {
4083 // restore 'laststatus' and 'winminheight'
4084 p_ls = save_p_ls;
4085 p_wmh = save_p_wmh;
4086 last_status(FALSE);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01004087 update_screen(UPD_VALID); // redraw the screen NOW
Bram Moolenaareadee482020-09-04 15:37:31 +02004088 redrawcmd();
4089 save_p_ls = -1;
4090 }
4091 else
4092 {
4093 win_redraw_last_status(topframe);
4094 redraw_statuslines();
4095 }
4096 KeyTyped = skt;
4097 wild_menu_showing = 0;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004098#ifdef FEAT_EVAL
Bram Moolenaareadee482020-09-04 15:37:31 +02004099 if (cclp->input_fn)
Bram Moolenaar79cdf022023-05-20 14:07:00 +01004100 RedrawingDisabled = save_RedrawingDisabled;
Bram Moolenaar58dcbf12022-08-26 19:58:49 +01004101#endif
Bram Moolenaareadee482020-09-04 15:37:31 +02004102}
Bram Moolenaareadee482020-09-04 15:37:31 +02004103
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004104#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004105/*
4106 * "getcompletion()" function
4107 */
4108 void
4109f_getcompletion(typval_T *argvars, typval_T *rettv)
4110{
4111 char_u *pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004112 char_u *type;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004113 expand_T xpc;
4114 int filtered = FALSE;
4115 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
Shougo Matsushitaae38a9d2021-10-21 11:39:53 +01004116 | WILD_NO_BEEP | WILD_HOME_REPLACE;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004117
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004118 if (in_vim9script()
4119 && (check_for_string_arg(argvars, 0) == FAIL
4120 || check_for_string_arg(argvars, 1) == FAIL
4121 || check_for_opt_bool_arg(argvars, 2) == FAIL))
4122 return;
4123
ii144785fe02021-11-21 12:13:56 +00004124 pat = tv_get_string(&argvars[0]);
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01004125 if (check_for_string_arg(argvars, 1) == FAIL)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004126 return;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004127 type = tv_get_string(&argvars[1]);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004128
4129 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard217a872020-09-05 18:31:33 +02004130 filtered = tv_get_bool_chk(&argvars[2], NULL);
Bram Moolenaar66b51422019-08-18 21:44:12 +02004131
4132 if (p_wic)
4133 options |= WILD_ICASE;
4134
4135 // For filtered results, 'wildignore' is used
4136 if (!filtered)
4137 options |= WILD_KEEP_ALL;
4138
4139 ExpandInit(&xpc);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004140 if (STRCMP(type, "cmdline") == 0)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004141 {
zeertzjqe4c79d32023-08-15 22:41:53 +02004142 int cmdline_len = (int)STRLEN(pat);
4143 set_cmd_context(&xpc, pat, cmdline_len, cmdline_len, FALSE);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004144 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
zeertzjqe4c79d32023-08-15 22:41:53 +02004145 xpc.xp_col = cmdline_len;
Bram Moolenaar66b51422019-08-18 21:44:12 +02004146 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004147 else
4148 {
ii144785fe02021-11-21 12:13:56 +00004149 xpc.xp_pattern = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004150 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004151 xpc.xp_line = pat;
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004152
4153 xpc.xp_context = cmdcomplete_str_to_type(type);
4154 if (xpc.xp_context == EXPAND_NOTHING)
4155 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004156 semsg(_(e_invalid_argument_str), type);
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004157 return;
4158 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004159
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004160 if (xpc.xp_context == EXPAND_USER_DEFINED)
4161 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004162 // Must be "custom,funcname" pattern
4163 if (STRNCMP(type, "custom,", 7) != 0)
4164 {
4165 semsg(_(e_invalid_argument_str), type);
4166 return;
4167 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004168
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004169 xpc.xp_arg = type + 7;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004170 }
4171
4172 if (xpc.xp_context == EXPAND_USER_LIST)
4173 {
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004174 // Must be "customlist,funcname" pattern
4175 if (STRNCMP(type, "customlist,", 11) != 0)
4176 {
4177 semsg(_(e_invalid_argument_str), type);
4178 return;
4179 }
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004180
Yegappan Lakshmanand4e4ecb2023-08-27 18:35:45 +02004181 xpc.xp_arg = type + 11;
Shougo Matsushita92997dd2023-08-20 20:55:55 +02004182 }
4183
Bram Moolenaar66b51422019-08-18 21:44:12 +02004184# if defined(FEAT_MENU)
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004185 if (xpc.xp_context == EXPAND_MENUS)
4186 {
4187 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4188 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4189 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004190# endif
4191# ifdef FEAT_CSCOPE
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004192 if (xpc.xp_context == EXPAND_CSCOPE)
4193 {
4194 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4195 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4196 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004197# endif
4198# ifdef FEAT_SIGNS
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004199 if (xpc.xp_context == EXPAND_SIGN)
4200 {
4201 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4202 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4203 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004204# endif
zeertzjq3770f4c2023-01-22 18:38:51 +00004205 if (xpc.xp_context == EXPAND_RUNTIME)
4206 {
4207 set_context_in_runtime_cmd(&xpc, xpc.xp_pattern);
4208 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4209 }
Bram Moolenaar1f1fd442020-06-07 18:45:14 +02004210 }
Bram Moolenaar66b51422019-08-18 21:44:12 +02004211
Yegappan Lakshmanane7dd0fa2022-03-22 16:06:31 +00004212 if (cmdline_fuzzy_completion_supported(&xpc))
4213 // when fuzzy matching, don't modify the search string
4214 pat = vim_strsave(xpc.xp_pattern);
4215 else
4216 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4217
Bram Moolenaar93a10962022-06-16 11:42:09 +01004218 if (rettv_list_alloc(rettv) == OK && pat != NULL)
Bram Moolenaar66b51422019-08-18 21:44:12 +02004219 {
4220 int i;
4221
4222 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4223
4224 for (i = 0; i < xpc.xp_numfiles; i++)
4225 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4226 }
4227 vim_free(pat);
4228 ExpandCleanup(&xpc);
4229}
Bram Moolenaar0a52df52019-08-18 22:26:31 +02004230#endif // FEAT_EVAL