blob: 0e9d712e31b41e0b78a34bfc283982e43adeef1d [file] [log] [blame]
Bram Moolenaarac9fb182019-04-27 13:04:13 +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 * usercmd.c: User defined command support
12 */
13
14#include "vim.h"
15
16typedef struct ucmd
17{
18 char_u *uc_name; // The command name
19 long_u uc_argt; // The argument type
20 char_u *uc_rep; // The command's replacement string
21 long uc_def; // The default value for a range/count
22 int uc_compl; // completion type
Bram Moolenaarb7316892019-05-01 18:08:42 +020023 cmd_addr_T uc_addr_type; // The command's address type
Bram Moolenaarac9fb182019-04-27 13:04:13 +020024 sctx_T uc_script_ctx; // SCTX where the command was defined
Bram Moolenaar98b7fe72022-03-23 21:36:27 +000025 int uc_flags; // some UC_ flags
Bram Moolenaar9b8d6222020-12-28 18:26:00 +010026# ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +020027 char_u *uc_compl_arg; // completion argument if any
Bram Moolenaarac9fb182019-04-27 13:04:13 +020028# endif
29} ucmd_T;
30
31// List of all user commands.
32static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
33
34#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
35#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
36
37/*
38 * List of names for completion for ":command" with the EXPAND_ flag.
39 * Must be alphabetical for completion.
40 */
41static struct
42{
43 int expand;
44 char *name;
45} command_complete[] =
46{
47 {EXPAND_ARGLIST, "arglist"},
48 {EXPAND_AUGROUP, "augroup"},
49 {EXPAND_BEHAVE, "behave"},
50 {EXPAND_BUFFERS, "buffer"},
51 {EXPAND_COLORS, "color"},
52 {EXPAND_COMMANDS, "command"},
53 {EXPAND_COMPILER, "compiler"},
54#if defined(FEAT_CSCOPE)
55 {EXPAND_CSCOPE, "cscope"},
56#endif
Bram Moolenaar0a52df52019-08-18 22:26:31 +020057#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +020058 {EXPAND_USER_DEFINED, "custom"},
59 {EXPAND_USER_LIST, "customlist"},
60#endif
Bram Moolenaarae7dba82019-12-29 13:56:33 +010061 {EXPAND_DIFF_BUFFERS, "diff_buffer"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020062 {EXPAND_DIRECTORIES, "dir"},
63 {EXPAND_ENV_VARS, "environment"},
64 {EXPAND_EVENTS, "event"},
65 {EXPAND_EXPRESSION, "expression"},
66 {EXPAND_FILES, "file"},
67 {EXPAND_FILES_IN_PATH, "file_in_path"},
68 {EXPAND_FILETYPE, "filetype"},
69 {EXPAND_FUNCTIONS, "function"},
70 {EXPAND_HELP, "help"},
71 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020072 {EXPAND_HISTORY, "history"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020073#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
74 {EXPAND_LOCALES, "locale"},
75#endif
76 {EXPAND_MAPCLEAR, "mapclear"},
77 {EXPAND_MAPPINGS, "mapping"},
78 {EXPAND_MENUS, "menu"},
79 {EXPAND_MESSAGES, "messages"},
80 {EXPAND_OWNSYNTAX, "syntax"},
81#if defined(FEAT_PROFILE)
82 {EXPAND_SYNTIME, "syntime"},
83#endif
84 {EXPAND_SETTINGS, "option"},
85 {EXPAND_PACKADD, "packadd"},
86 {EXPAND_SHELLCMD, "shellcmd"},
87#if defined(FEAT_SIGNS)
88 {EXPAND_SIGN, "sign"},
89#endif
90 {EXPAND_TAGS, "tag"},
91 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
92 {EXPAND_USER, "user"},
93 {EXPAND_USER_VARS, "var"},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +000094#if defined(FEAT_EVAL)
95 {EXPAND_BREAKPOINT, "breakpoint"},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +000096 {EXPAND_SCRIPTNAMES, "scriptnames"},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +000097#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +020098 {0, NULL}
99};
100
101/*
102 * List of names of address types. Must be alphabetical for completion.
103 */
104static struct
105{
Bram Moolenaarb7316892019-05-01 18:08:42 +0200106 cmd_addr_T expand;
107 char *name;
108 char *shortname;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200109} addr_type_complete[] =
110{
111 {ADDR_ARGUMENTS, "arguments", "arg"},
112 {ADDR_LINES, "lines", "line"},
113 {ADDR_LOADED_BUFFERS, "loaded_buffers", "load"},
114 {ADDR_TABS, "tabs", "tab"},
115 {ADDR_BUFFERS, "buffers", "buf"},
116 {ADDR_WINDOWS, "windows", "win"},
117 {ADDR_QUICKFIX, "quickfix", "qf"},
118 {ADDR_OTHER, "other", "?"},
Bram Moolenaarb7316892019-05-01 18:08:42 +0200119 {ADDR_NONE, NULL, NULL}
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200120};
121
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200122/*
123 * Search for a user command that matches "eap->cmd".
124 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
125 * Return a pointer to just after the command.
126 * Return NULL if there is no matching command.
127 */
128 char_u *
129find_ucmd(
130 exarg_T *eap,
Bram Moolenaar0023f822022-01-16 21:54:19 +0000131 char_u *p, // end of the command (possibly including count)
132 int *full, // set to TRUE for a full match
133 expand_T *xp, // used for completion, NULL otherwise
134 int *complp) // completion flags or NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200135{
136 int len = (int)(p - eap->cmd);
137 int j, k, matchlen = 0;
138 ucmd_T *uc;
139 int found = FALSE;
140 int possible = FALSE;
141 char_u *cp, *np; // Point into typed cmd and test name
142 garray_T *gap;
143 int amb_local = FALSE; // Found ambiguous buffer-local command,
144 // only full match global is accepted.
145
146 /*
147 * Look for buffer-local user commands first, then global ones.
148 */
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000149 gap = &prevwin_curwin()->w_buffer->b_ucmds;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200150 for (;;)
151 {
152 for (j = 0; j < gap->ga_len; ++j)
153 {
154 uc = USER_CMD_GA(gap, j);
155 cp = eap->cmd;
156 np = uc->uc_name;
157 k = 0;
158 while (k < len && *np != NUL && *cp++ == *np++)
159 k++;
160 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
161 {
162 // If finding a second match, the command is ambiguous. But
163 // not if a buffer-local command wasn't a full match and a
164 // global command is a full match.
165 if (k == len && found && *np != NUL)
166 {
167 if (gap == &ucmds)
168 return NULL;
169 amb_local = TRUE;
170 }
171
172 if (!found || (k == len && *np == NUL))
173 {
174 // If we matched up to a digit, then there could
175 // be another command including the digit that we
176 // should use instead.
177 if (k == len)
178 found = TRUE;
179 else
180 possible = TRUE;
181
182 if (gap == &ucmds)
183 eap->cmdidx = CMD_USER;
184 else
185 eap->cmdidx = CMD_USER_BUF;
186 eap->argt = (long)uc->uc_argt;
187 eap->useridx = j;
188 eap->addr_type = uc->uc_addr_type;
189
Bram Moolenaar52111f82019-04-29 21:30:45 +0200190 if (complp != NULL)
191 *complp = uc->uc_compl;
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200192# ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200193 if (xp != NULL)
194 {
195 xp->xp_arg = uc->uc_compl_arg;
196 xp->xp_script_ctx = uc->uc_script_ctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100197 xp->xp_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200198 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200199# endif
200 // Do not search for further abbreviations
201 // if this is an exact match.
202 matchlen = k;
203 if (k == len && *np == NUL)
204 {
205 if (full != NULL)
206 *full = TRUE;
207 amb_local = FALSE;
208 break;
209 }
210 }
211 }
212 }
213
214 // Stop if we found a full match or searched all.
215 if (j < gap->ga_len || gap == &ucmds)
216 break;
217 gap = &ucmds;
218 }
219
220 // Only found ambiguous matches.
221 if (amb_local)
222 {
223 if (xp != NULL)
224 xp->xp_context = EXPAND_UNSUCCESSFUL;
225 return NULL;
226 }
227
228 // The match we found may be followed immediately by a number. Move "p"
229 // back to point to it.
230 if (found || possible)
231 return p + (matchlen - len);
232 return p;
233}
234
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000235/*
236 * Set completion context for :command
237 */
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200238 char_u *
239set_context_in_user_cmd(expand_T *xp, char_u *arg_in)
240{
241 char_u *arg = arg_in;
242 char_u *p;
243
244 // Check for attributes
245 while (*arg == '-')
246 {
247 arg++; // Skip "-"
248 p = skiptowhite(arg);
249 if (*p == NUL)
250 {
251 // Cursor is still in the attribute
252 p = vim_strchr(arg, '=');
253 if (p == NULL)
254 {
255 // No "=", so complete attribute names
256 xp->xp_context = EXPAND_USER_CMD_FLAGS;
257 xp->xp_pattern = arg;
258 return NULL;
259 }
260
261 // For the -complete, -nargs and -addr attributes, we complete
262 // their arguments as well.
263 if (STRNICMP(arg, "complete", p - arg) == 0)
264 {
265 xp->xp_context = EXPAND_USER_COMPLETE;
266 xp->xp_pattern = p + 1;
267 return NULL;
268 }
269 else if (STRNICMP(arg, "nargs", p - arg) == 0)
270 {
271 xp->xp_context = EXPAND_USER_NARGS;
272 xp->xp_pattern = p + 1;
273 return NULL;
274 }
275 else if (STRNICMP(arg, "addr", p - arg) == 0)
276 {
277 xp->xp_context = EXPAND_USER_ADDR_TYPE;
278 xp->xp_pattern = p + 1;
279 return NULL;
280 }
281 return NULL;
282 }
283 arg = skipwhite(p);
284 }
285
286 // After the attributes comes the new command name
287 p = skiptowhite(arg);
288 if (*p == NUL)
289 {
290 xp->xp_context = EXPAND_USER_COMMANDS;
291 xp->xp_pattern = arg;
292 return NULL;
293 }
294
295 // And finally comes a normal command
296 return skipwhite(p);
297}
298
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000299/*
300 * Set the completion context for the argument of a user defined command.
301 */
302 char_u *
303set_context_in_user_cmdarg(
304 char_u *cmd UNUSED,
305 char_u *arg,
306 long argt,
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000307 int context,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000308 expand_T *xp,
309 int forceit)
310{
311 char_u *p;
312
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000313 if (context == EXPAND_NOTHING)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000314 return NULL;
315
316 if (argt & EX_XFILE)
317 {
318 // EX_XFILE: file names are handled before this call
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000319 xp->xp_context = context;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000320 return NULL;
321 }
322
323#ifdef FEAT_MENU
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000324 if (context == EXPAND_MENUS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000325 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
326#endif
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000327 if (context == EXPAND_COMMANDS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000328 return arg;
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000329 if (context == EXPAND_MAPPINGS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000330 return set_context_in_map_cmd(xp, (char_u *)"map", arg, forceit, FALSE,
331 FALSE, CMD_map);
332 // Find start of last argument.
333 p = arg;
334 while (*p)
335 {
336 if (*p == ' ')
337 // argument starts after a space
338 arg = p + 1;
339 else if (*p == '\\' && *(p + 1) != NUL)
340 ++p; // skip over escaped character
341 MB_PTR_ADV(p);
342 }
343 xp->xp_pattern = arg;
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000344 xp->xp_context = context;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000345
346 return NULL;
347}
348
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200349 char_u *
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200350expand_user_command_name(int idx)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200351{
352 return get_user_commands(NULL, idx - (int)CMD_SIZE);
353}
354
355/*
356 * Function given to ExpandGeneric() to obtain the list of user command names.
357 */
358 char_u *
359get_user_commands(expand_T *xp UNUSED, int idx)
360{
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200361 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000362 buf_T *buf = prevwin_curwin()->w_buffer;
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200363
364 if (idx < buf->b_ucmds.ga_len)
365 return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name;
366 idx -= buf->b_ucmds.ga_len;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200367 if (idx < ucmds.ga_len)
368 return USER_CMD(idx)->uc_name;
369 return NULL;
370}
371
Dominique Pelle748b3082022-01-08 12:41:16 +0000372#ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200373/*
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200374 * Get the name of user command "idx". "cmdidx" can be CMD_USER or
375 * CMD_USER_BUF.
376 * Returns NULL if the command is not found.
377 */
378 char_u *
379get_user_command_name(int idx, int cmdidx)
380{
381 if (cmdidx == CMD_USER && idx < ucmds.ga_len)
382 return USER_CMD(idx)->uc_name;
383 if (cmdidx == CMD_USER_BUF)
384 {
385 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000386 buf_T *buf = prevwin_curwin()->w_buffer;
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200387
388 if (idx < buf->b_ucmds.ga_len)
389 return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name;
390 }
391 return NULL;
392}
Dominique Pelle748b3082022-01-08 12:41:16 +0000393#endif
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200394
395/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200396 * Function given to ExpandGeneric() to obtain the list of user address type
397 * names.
398 */
399 char_u *
400get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
401{
402 return (char_u *)addr_type_complete[idx].name;
403}
404
405/*
406 * Function given to ExpandGeneric() to obtain the list of user command
407 * attributes.
408 */
409 char_u *
410get_user_cmd_flags(expand_T *xp UNUSED, int idx)
411{
412 static char *user_cmd_flags[] = {
413 "addr", "bang", "bar", "buffer", "complete",
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000414 "count", "nargs", "range", "register", "keepscript"
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200415 };
416
K.Takataeeec2542021-06-02 13:28:16 +0200417 if (idx >= (int)ARRAY_LENGTH(user_cmd_flags))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200418 return NULL;
419 return (char_u *)user_cmd_flags[idx];
420}
421
422/*
423 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
424 */
425 char_u *
426get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
427{
428 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
429
K.Takataeeec2542021-06-02 13:28:16 +0200430 if (idx >= (int)ARRAY_LENGTH(user_cmd_nargs))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200431 return NULL;
432 return (char_u *)user_cmd_nargs[idx];
433}
434
435/*
436 * Function given to ExpandGeneric() to obtain the list of values for
437 * -complete.
438 */
439 char_u *
440get_user_cmd_complete(expand_T *xp UNUSED, int idx)
441{
442 return (char_u *)command_complete[idx].name;
443}
444
Dominique Pelle748b3082022-01-08 12:41:16 +0000445#ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200446 int
447cmdcomplete_str_to_type(char_u *complete_str)
448{
449 int i;
450
451 for (i = 0; command_complete[i].expand != 0; ++i)
452 if (STRCMP(complete_str, command_complete[i].name) == 0)
453 return command_complete[i].expand;
454
455 return EXPAND_NOTHING;
456}
Dominique Pelle748b3082022-01-08 12:41:16 +0000457#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200458
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200459/*
460 * List user commands starting with "name[name_len]".
461 */
462 static void
463uc_list(char_u *name, size_t name_len)
464{
465 int i, j;
466 int found = FALSE;
467 ucmd_T *cmd;
468 int len;
469 int over;
470 long a;
471 garray_T *gap;
472
Bram Moolenaare38eab22019-12-05 21:50:01 +0100473 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000474 gap = &prevwin_curwin()->w_buffer->b_ucmds;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200475 for (;;)
476 {
477 for (i = 0; i < gap->ga_len; ++i)
478 {
479 cmd = USER_CMD_GA(gap, i);
480 a = (long)cmd->uc_argt;
481
482 // Skip commands which don't match the requested prefix and
483 // commands filtered out.
484 if (STRNCMP(name, cmd->uc_name, name_len) != 0
485 || message_filtered(cmd->uc_name))
486 continue;
487
488 // Put out the title first time
489 if (!found)
490 msg_puts_title(_("\n Name Args Address Complete Definition"));
491 found = TRUE;
492 msg_putchar('\n');
493 if (got_int)
494 break;
495
496 // Special cases
497 len = 4;
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200498 if (a & EX_BANG)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200499 {
500 msg_putchar('!');
501 --len;
502 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200503 if (a & EX_REGSTR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200504 {
505 msg_putchar('"');
506 --len;
507 }
508 if (gap != &ucmds)
509 {
510 msg_putchar('b');
511 --len;
512 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200513 if (a & EX_TRLBAR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200514 {
515 msg_putchar('|');
516 --len;
517 }
518 while (len-- > 0)
519 msg_putchar(' ');
520
521 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
522 len = (int)STRLEN(cmd->uc_name) + 4;
523
524 do {
525 msg_putchar(' ');
526 ++len;
527 } while (len < 22);
528
529 // "over" is how much longer the name is than the column width for
530 // the name, we'll try to align what comes after.
531 over = len - 22;
532 len = 0;
533
534 // Arguments
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200535 switch ((int)(a & (EX_EXTRA|EX_NOSPC|EX_NEEDARG)))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200536 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200537 case 0: IObuff[len++] = '0'; break;
538 case (EX_EXTRA): IObuff[len++] = '*'; break;
539 case (EX_EXTRA|EX_NOSPC): IObuff[len++] = '?'; break;
540 case (EX_EXTRA|EX_NEEDARG): IObuff[len++] = '+'; break;
541 case (EX_EXTRA|EX_NOSPC|EX_NEEDARG): IObuff[len++] = '1'; break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200542 }
543
544 do {
545 IObuff[len++] = ' ';
546 } while (len < 5 - over);
547
548 // Address / Range
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200549 if (a & (EX_RANGE|EX_COUNT))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200550 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200551 if (a & EX_COUNT)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200552 {
553 // -count=N
554 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
555 len += (int)STRLEN(IObuff + len);
556 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200557 else if (a & EX_DFLALL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200558 IObuff[len++] = '%';
559 else if (cmd->uc_def >= 0)
560 {
561 // -range=N
562 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
563 len += (int)STRLEN(IObuff + len);
564 }
565 else
566 IObuff[len++] = '.';
567 }
568
569 do {
570 IObuff[len++] = ' ';
571 } while (len < 8 - over);
572
573 // Address Type
Bram Moolenaarb7316892019-05-01 18:08:42 +0200574 for (j = 0; addr_type_complete[j].expand != ADDR_NONE; ++j)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200575 if (addr_type_complete[j].expand != ADDR_LINES
576 && addr_type_complete[j].expand == cmd->uc_addr_type)
577 {
578 STRCPY(IObuff + len, addr_type_complete[j].shortname);
579 len += (int)STRLEN(IObuff + len);
580 break;
581 }
582
583 do {
584 IObuff[len++] = ' ';
585 } while (len < 13 - over);
586
587 // Completion
588 for (j = 0; command_complete[j].expand != 0; ++j)
589 if (command_complete[j].expand == cmd->uc_compl)
590 {
591 STRCPY(IObuff + len, command_complete[j].name);
592 len += (int)STRLEN(IObuff + len);
Bram Moolenaar78f60322022-01-17 22:16:33 +0000593#ifdef FEAT_EVAL
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000594 if (p_verbose > 0 && cmd->uc_compl_arg != NULL
595 && STRLEN(cmd->uc_compl_arg) < 200)
596 {
597 IObuff[len] = ',';
598 STRCPY(IObuff + len + 1, cmd->uc_compl_arg);
599 len += (int)STRLEN(IObuff + len);
600 }
Bram Moolenaar78f60322022-01-17 22:16:33 +0000601#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200602 break;
603 }
604
605 do {
606 IObuff[len++] = ' ';
607 } while (len < 25 - over);
608
609 IObuff[len] = '\0';
610 msg_outtrans(IObuff);
611
612 msg_outtrans_special(cmd->uc_rep, FALSE,
613 name_len == 0 ? Columns - 47 : 0);
614#ifdef FEAT_EVAL
615 if (p_verbose > 0)
616 last_set_msg(cmd->uc_script_ctx);
617#endif
618 out_flush();
619 ui_breakcheck();
620 if (got_int)
621 break;
622 }
623 if (gap == &ucmds || i < gap->ga_len)
624 break;
625 gap = &ucmds;
626 }
627
628 if (!found)
629 msg(_("No user-defined commands found"));
630}
631
632 char *
633uc_fun_cmd(void)
634{
635 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
636 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
637 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
638 0xb9, 0x7f, 0};
639 int i;
640
641 for (i = 0; fcmd[i]; ++i)
642 IObuff[i] = fcmd[i] - 0x40;
643 IObuff[i] = 0;
644 return (char *)IObuff;
645}
646
647/*
648 * Parse address type argument
649 */
650 static int
651parse_addr_type_arg(
652 char_u *value,
653 int vallen,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200654 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200655{
656 int i, a, b;
657
Bram Moolenaarb7316892019-05-01 18:08:42 +0200658 for (i = 0; addr_type_complete[i].expand != ADDR_NONE; ++i)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200659 {
660 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
661 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
662 if (a && b)
663 {
664 *addr_type_arg = addr_type_complete[i].expand;
665 break;
666 }
667 }
668
Bram Moolenaarb7316892019-05-01 18:08:42 +0200669 if (addr_type_complete[i].expand == ADDR_NONE)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200670 {
671 char_u *err = value;
672
673 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
674 ;
675 err[i] = NUL;
Bram Moolenaar11de43d2022-01-06 21:41:11 +0000676 semsg(_(e_invalid_address_type_value_str), err);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200677 return FAIL;
678 }
679
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200680 return OK;
681}
682
683/*
684 * Parse a completion argument "value[vallen]".
685 * The detected completion goes in "*complp", argument type in "*argt".
686 * When there is an argument, for function and user defined completion, it's
687 * copied to allocated memory and stored in "*compl_arg".
688 * Returns FAIL if something is wrong.
689 */
690 int
691parse_compl_arg(
692 char_u *value,
693 int vallen,
694 int *complp,
695 long *argt,
696 char_u **compl_arg UNUSED)
697{
698 char_u *arg = NULL;
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200699# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200700 size_t arglen = 0;
701# endif
702 int i;
703 int valend = vallen;
704
705 // Look for any argument part - which is the part after any ','
706 for (i = 0; i < vallen; ++i)
707 {
708 if (value[i] == ',')
709 {
710 arg = &value[i + 1];
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200711# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200712 arglen = vallen - i - 1;
713# endif
714 valend = i;
715 break;
716 }
717 }
718
719 for (i = 0; command_complete[i].expand != 0; ++i)
720 {
721 if ((int)STRLEN(command_complete[i].name) == valend
722 && STRNCMP(value, command_complete[i].name, valend) == 0)
723 {
724 *complp = command_complete[i].expand;
725 if (command_complete[i].expand == EXPAND_BUFFERS)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200726 *argt |= EX_BUFNAME;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200727 else if (command_complete[i].expand == EXPAND_DIRECTORIES
728 || command_complete[i].expand == EXPAND_FILES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200729 *argt |= EX_XFILE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200730 break;
731 }
732 }
733
734 if (command_complete[i].expand == 0)
735 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000736 semsg(_(e_invalid_complete_value_str), value);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200737 return FAIL;
738 }
739
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200740# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200741 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
742 && arg != NULL)
743# else
744 if (arg != NULL)
745# endif
746 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000747 emsg(_(e_completion_argument_only_allowed_for_custom_completion));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200748 return FAIL;
749 }
750
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200751# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200752 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
753 && arg == NULL)
754 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000755 emsg(_(e_custom_completion_requires_function_argument));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200756 return FAIL;
757 }
758
759 if (arg != NULL)
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200760 *compl_arg = vim_strnsave(arg, arglen);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200761# endif
762 return OK;
763}
764
765/*
766 * Scan attributes in the ":command" command.
767 * Return FAIL when something is wrong.
768 */
769 static int
770uc_scan_attr(
771 char_u *attr,
772 size_t len,
773 long *argt,
774 long *def,
775 int *flags,
Bram Moolenaar52111f82019-04-29 21:30:45 +0200776 int *complp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200777 char_u **compl_arg,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200778 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200779{
780 char_u *p;
781
782 if (len == 0)
783 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000784 emsg(_(e_no_attribute_specified));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200785 return FAIL;
786 }
787
788 // First, try the simple attributes (no arguments)
789 if (STRNICMP(attr, "bang", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200790 *argt |= EX_BANG;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200791 else if (STRNICMP(attr, "buffer", len) == 0)
792 *flags |= UC_BUFFER;
793 else if (STRNICMP(attr, "register", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200794 *argt |= EX_REGSTR;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000795 else if (STRNICMP(attr, "keepscript", len) == 0)
796 *argt |= EX_KEEPSCRIPT;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200797 else if (STRNICMP(attr, "bar", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200798 *argt |= EX_TRLBAR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200799 else
800 {
801 int i;
802 char_u *val = NULL;
803 size_t vallen = 0;
804 size_t attrlen = len;
805
806 // Look for the attribute name - which is the part before any '='
807 for (i = 0; i < (int)len; ++i)
808 {
809 if (attr[i] == '=')
810 {
811 val = &attr[i + 1];
812 vallen = len - i - 1;
813 attrlen = i;
814 break;
815 }
816 }
817
818 if (STRNICMP(attr, "nargs", attrlen) == 0)
819 {
820 if (vallen == 1)
821 {
822 if (*val == '0')
823 // Do nothing - this is the default
824 ;
825 else if (*val == '1')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200826 *argt |= (EX_EXTRA | EX_NOSPC | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200827 else if (*val == '*')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200828 *argt |= EX_EXTRA;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200829 else if (*val == '?')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200830 *argt |= (EX_EXTRA | EX_NOSPC);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200831 else if (*val == '+')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200832 *argt |= (EX_EXTRA | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200833 else
834 goto wrong_nargs;
835 }
836 else
837 {
838wrong_nargs:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000839 emsg(_(e_invalid_number_of_arguments));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200840 return FAIL;
841 }
842 }
843 else if (STRNICMP(attr, "range", attrlen) == 0)
844 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200845 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200846 if (vallen == 1 && *val == '%')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200847 *argt |= EX_DFLALL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200848 else if (val != NULL)
849 {
850 p = val;
851 if (*def >= 0)
852 {
853two_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000854 emsg(_(e_count_cannot_be_specified_twice));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200855 return FAIL;
856 }
857
858 *def = getdigits(&p);
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200859 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200860
861 if (p != val + vallen || vallen == 0)
862 {
863invalid_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000864 emsg(_(e_invalid_default_value_for_count));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200865 return FAIL;
866 }
867 }
Bram Moolenaarb7316892019-05-01 18:08:42 +0200868 // default for -range is using buffer lines
869 if (*addr_type_arg == ADDR_NONE)
870 *addr_type_arg = ADDR_LINES;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200871 }
872 else if (STRNICMP(attr, "count", attrlen) == 0)
873 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200874 *argt |= (EX_COUNT | EX_ZEROR | EX_RANGE);
Bram Moolenaarb7316892019-05-01 18:08:42 +0200875 // default for -count is using any number
876 if (*addr_type_arg == ADDR_NONE)
877 *addr_type_arg = ADDR_OTHER;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200878
879 if (val != NULL)
880 {
881 p = val;
882 if (*def >= 0)
883 goto two_count;
884
885 *def = getdigits(&p);
886
887 if (p != val + vallen)
888 goto invalid_count;
889 }
890
891 if (*def < 0)
892 *def = 0;
893 }
894 else if (STRNICMP(attr, "complete", attrlen) == 0)
895 {
896 if (val == NULL)
897 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000898 semsg(_(e_argument_required_for_str), "-complete");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200899 return FAIL;
900 }
901
Bram Moolenaar52111f82019-04-29 21:30:45 +0200902 if (parse_compl_arg(val, (int)vallen, complp, argt, compl_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200903 == FAIL)
904 return FAIL;
905 }
906 else if (STRNICMP(attr, "addr", attrlen) == 0)
907 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200908 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200909 if (val == NULL)
910 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000911 semsg(_(e_argument_required_for_str), "-addr");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200912 return FAIL;
913 }
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200914 if (parse_addr_type_arg(val, (int)vallen, addr_type_arg) == FAIL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200915 return FAIL;
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200916 if (*addr_type_arg != ADDR_LINES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200917 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200918 }
919 else
920 {
921 char_u ch = attr[len];
922 attr[len] = '\0';
Bram Moolenaar1a992222021-12-31 17:25:48 +0000923 semsg(_(e_invalid_attribute_str), attr);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200924 attr[len] = ch;
925 return FAIL;
926 }
927 }
928
929 return OK;
930}
931
932/*
933 * Add a user command to the list or replace an existing one.
934 */
935 static int
936uc_add_command(
937 char_u *name,
938 size_t name_len,
939 char_u *rep,
940 long argt,
941 long def,
942 int flags,
943 int compl,
944 char_u *compl_arg UNUSED,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200945 cmd_addr_T addr_type,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200946 int force)
947{
948 ucmd_T *cmd = NULL;
949 char_u *p;
950 int i;
951 int cmp = 1;
952 char_u *rep_buf = NULL;
953 garray_T *gap;
954
Bram Moolenaar459fd782019-10-13 16:43:39 +0200955 replace_termcodes(rep, &rep_buf, 0, NULL);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200956 if (rep_buf == NULL)
957 {
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200958 // can't replace termcodes - try using the string as is
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200959 rep_buf = vim_strsave(rep);
960
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200961 // give up if out of memory
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200962 if (rep_buf == NULL)
963 return FAIL;
964 }
965
966 // get address of growarray: global or in curbuf
967 if (flags & UC_BUFFER)
968 {
969 gap = &curbuf->b_ucmds;
970 if (gap->ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000971 ga_init2(gap, sizeof(ucmd_T), 4);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200972 }
973 else
974 gap = &ucmds;
975
976 // Search for the command in the already defined commands.
977 for (i = 0; i < gap->ga_len; ++i)
978 {
979 size_t len;
980
981 cmd = USER_CMD_GA(gap, i);
982 len = STRLEN(cmd->uc_name);
983 cmp = STRNCMP(name, cmd->uc_name, name_len);
984 if (cmp == 0)
985 {
986 if (name_len < len)
987 cmp = -1;
988 else if (name_len > len)
989 cmp = 1;
990 }
991
992 if (cmp == 0)
993 {
994 // Command can be replaced with "command!" and when sourcing the
995 // same script again, but only once.
996 if (!force
997#ifdef FEAT_EVAL
998 && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
999 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq)
1000#endif
1001 )
1002 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001003 semsg(_(e_command_already_exists_add_bang_to_replace_it_str),
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001004 name);
1005 goto fail;
1006 }
1007
1008 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001009#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001010 VIM_CLEAR(cmd->uc_compl_arg);
1011#endif
1012 break;
1013 }
1014
1015 // Stop as soon as we pass the name to add
1016 if (cmp < 0)
1017 break;
1018 }
1019
1020 // Extend the array unless we're replacing an existing command
1021 if (cmp != 0)
1022 {
1023 if (ga_grow(gap, 1) != OK)
1024 goto fail;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001025 if ((p = vim_strnsave(name, name_len)) == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001026 goto fail;
1027
1028 cmd = USER_CMD_GA(gap, i);
1029 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
1030
1031 ++gap->ga_len;
1032
1033 cmd->uc_name = p;
1034 }
1035
1036 cmd->uc_rep = rep_buf;
1037 cmd->uc_argt = argt;
1038 cmd->uc_def = def;
1039 cmd->uc_compl = compl;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001040 cmd->uc_script_ctx = current_sctx;
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001041 if (flags & UC_VIM9)
1042 cmd->uc_script_ctx.sc_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001043 cmd->uc_flags = flags & UC_VIM9;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001044#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001045 cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001046 cmd->uc_compl_arg = compl_arg;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001047#endif
1048 cmd->uc_addr_type = addr_type;
1049
1050 return OK;
1051
1052fail:
1053 vim_free(rep_buf);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001054#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001055 vim_free(compl_arg);
1056#endif
1057 return FAIL;
1058}
1059
1060/*
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001061 * If "p" starts with "{" then read a block of commands until "}".
1062 * Used for ":command" and ":autocmd".
1063 */
1064 char_u *
1065may_get_cmd_block(exarg_T *eap, char_u *p, char_u **tofree, int *flags)
1066{
1067 char_u *retp = p;
1068
1069 if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
1070 && eap->getline != NULL)
1071 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001072 garray_T ga;
1073 char_u *line = NULL;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001074
1075 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001076 if (ga_copy_string(&ga, p) == FAIL)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001077 return retp;
1078
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001079 // If the argument ends in "}" it must have been concatenated already
1080 // for ISN_EXEC.
1081 if (p[STRLEN(p) - 1] != '}')
1082 // Read lines between '{' and '}'. Does not support nesting or
1083 // here-doc constructs.
1084 for (;;)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001085 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001086 vim_free(line);
1087 if ((line = eap->getline(':', eap->cookie,
1088 0, GETLINE_CONCAT_CONTBAR)) == NULL)
1089 {
1090 emsg(_(e_missing_rcurly));
1091 break;
1092 }
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001093 if (ga_copy_string(&ga, line) == FAIL)
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001094 break;
1095 if (*skipwhite(line) == '}')
1096 break;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001097 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001098 vim_free(line);
1099 retp = *tofree = ga_concat_strings(&ga, "\n");
1100 ga_clear_strings(&ga);
1101 *flags |= UC_VIM9;
1102 }
1103 return retp;
1104}
1105
1106/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001107 * ":command ..." implementation
1108 */
1109 void
1110ex_command(exarg_T *eap)
1111{
Bram Moolenaarb7316892019-05-01 18:08:42 +02001112 char_u *name;
1113 char_u *end;
1114 char_u *p;
1115 long argt = 0;
1116 long def = -1;
1117 int flags = 0;
1118 int compl = EXPAND_NOTHING;
1119 char_u *compl_arg = NULL;
1120 cmd_addr_T addr_type_arg = ADDR_NONE;
1121 int has_attr = (eap->arg[0] == '-');
1122 int name_len;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001123
1124 p = eap->arg;
1125
1126 // Check for attributes
1127 while (*p == '-')
1128 {
1129 ++p;
1130 end = skiptowhite(p);
1131 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
1132 &compl_arg, &addr_type_arg) == FAIL)
1133 return;
1134 p = skipwhite(end);
1135 }
1136
1137 // Get the name (if any) and skip to the following argument
1138 name = p;
1139 if (ASCII_ISALPHA(*p))
1140 while (ASCII_ISALNUM(*p))
1141 ++p;
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001142 if (!ends_excmd2(eap->arg, p) && !VIM_ISWHITE(*p))
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001143 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001144 emsg(_(e_invalid_command_name));
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001145 return;
1146 }
1147 end = p;
1148 name_len = (int)(end - name);
1149
1150 // If there is nothing after the name, and no attributes were specified,
1151 // we are listing commands
1152 p = skipwhite(end);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001153 if (!has_attr && ends_excmd2(eap->arg, p))
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001154 uc_list(name, end - name);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001155 else if (!ASCII_ISUPPER(*name))
Bram Moolenaar1a992222021-12-31 17:25:48 +00001156 emsg(_(e_user_defined_commands_must_start_with_an_uppercase_letter));
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001157 else if ((name_len == 1 && *name == 'X')
1158 || (name_len <= 4
1159 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001160 emsg(_(e_reserved_name_cannot_be_used_for_user_defined_command));
Martin Tournoijde69a732021-07-11 14:28:25 +02001161 else if (compl > 0 && (argt & EX_EXTRA) == 0)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001162 {
1163 // Some plugins rely on silently ignoring the mistake, only make this
1164 // an error in Vim9 script.
1165 if (in_vim9script())
Bram Moolenaar41a34852021-08-04 16:09:24 +02001166 emsg(_(e_complete_used_without_allowing_arguments));
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001167 else
1168 give_warning_with_source(
Bram Moolenaar41a34852021-08-04 16:09:24 +02001169 (char_u *)_(e_complete_used_without_allowing_arguments),
1170 TRUE, TRUE);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001171 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001172 else
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001173 {
1174 char_u *tofree = NULL;
1175
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001176 p = may_get_cmd_block(eap, p, &tofree, &flags);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001177
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001178 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
1179 addr_type_arg, eap->forceit);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001180 vim_free(tofree);
1181 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001182}
1183
1184/*
1185 * ":comclear" implementation
1186 * Clear all user commands, global and for current buffer.
1187 */
1188 void
1189ex_comclear(exarg_T *eap UNUSED)
1190{
1191 uc_clear(&ucmds);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001192 if (curbuf != NULL)
1193 uc_clear(&curbuf->b_ucmds);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001194}
1195
1196/*
1197 * Clear all user commands for "gap".
1198 */
1199 void
1200uc_clear(garray_T *gap)
1201{
1202 int i;
1203 ucmd_T *cmd;
1204
1205 for (i = 0; i < gap->ga_len; ++i)
1206 {
1207 cmd = USER_CMD_GA(gap, i);
1208 vim_free(cmd->uc_name);
1209 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001210# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001211 vim_free(cmd->uc_compl_arg);
1212# endif
1213 }
1214 ga_clear(gap);
1215}
1216
1217/*
1218 * ":delcommand" implementation
1219 */
1220 void
1221ex_delcommand(exarg_T *eap)
1222{
1223 int i = 0;
1224 ucmd_T *cmd = NULL;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001225 int res = -1;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001226 garray_T *gap;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001227 char_u *arg = eap->arg;
1228 int buffer_only = FALSE;
1229
1230 if (STRNCMP(arg, "-buffer", 7) == 0 && VIM_ISWHITE(arg[7]))
1231 {
1232 buffer_only = TRUE;
1233 arg = skipwhite(arg + 7);
1234 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001235
1236 gap = &curbuf->b_ucmds;
1237 for (;;)
1238 {
1239 for (i = 0; i < gap->ga_len; ++i)
1240 {
1241 cmd = USER_CMD_GA(gap, i);
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001242 res = STRCMP(arg, cmd->uc_name);
1243 if (res <= 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001244 break;
1245 }
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001246 if (gap == &ucmds || res == 0 || buffer_only)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001247 break;
1248 gap = &ucmds;
1249 }
1250
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001251 if (res != 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001252 {
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001253 semsg(_(buffer_only
1254 ? e_no_such_user_defined_command_in_current_buffer_str
1255 : e_no_such_user_defined_command_str), arg);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001256 return;
1257 }
1258
1259 vim_free(cmd->uc_name);
1260 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001261# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001262 vim_free(cmd->uc_compl_arg);
1263# endif
1264
1265 --gap->ga_len;
1266
1267 if (i < gap->ga_len)
1268 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
1269}
1270
1271/*
1272 * Split and quote args for <f-args>.
1273 */
1274 static char_u *
1275uc_split_args(char_u *arg, size_t *lenp)
1276{
1277 char_u *buf;
1278 char_u *p;
1279 char_u *q;
1280 int len;
1281
1282 // Precalculate length
1283 p = arg;
1284 len = 2; // Initial and final quotes
1285
1286 while (*p)
1287 {
1288 if (p[0] == '\\' && p[1] == '\\')
1289 {
1290 len += 2;
1291 p += 2;
1292 }
1293 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1294 {
1295 len += 1;
1296 p += 2;
1297 }
1298 else if (*p == '\\' || *p == '"')
1299 {
1300 len += 2;
1301 p += 1;
1302 }
1303 else if (VIM_ISWHITE(*p))
1304 {
1305 p = skipwhite(p);
1306 if (*p == NUL)
1307 break;
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001308 len += 4; // ", "
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001309 }
1310 else
1311 {
1312 int charlen = (*mb_ptr2len)(p);
1313
1314 len += charlen;
1315 p += charlen;
1316 }
1317 }
1318
1319 buf = alloc(len + 1);
1320 if (buf == NULL)
1321 {
1322 *lenp = 0;
1323 return buf;
1324 }
1325
1326 p = arg;
1327 q = buf;
1328 *q++ = '"';
1329 while (*p)
1330 {
1331 if (p[0] == '\\' && p[1] == '\\')
1332 {
1333 *q++ = '\\';
1334 *q++ = '\\';
1335 p += 2;
1336 }
1337 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1338 {
1339 *q++ = p[1];
1340 p += 2;
1341 }
1342 else if (*p == '\\' || *p == '"')
1343 {
1344 *q++ = '\\';
1345 *q++ = *p++;
1346 }
1347 else if (VIM_ISWHITE(*p))
1348 {
1349 p = skipwhite(p);
1350 if (*p == NUL)
1351 break;
1352 *q++ = '"';
1353 *q++ = ',';
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001354 *q++ = ' ';
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001355 *q++ = '"';
1356 }
1357 else
1358 {
1359 MB_COPY_CHAR(p, q);
1360 }
1361 }
1362 *q++ = '"';
1363 *q = 0;
1364
1365 *lenp = len;
1366 return buf;
1367}
1368
1369 static size_t
1370add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
1371{
1372 size_t result;
1373
1374 result = STRLEN(mod_str);
1375 if (*multi_mods)
1376 result += 1;
1377 if (buf != NULL)
1378 {
1379 if (*multi_mods)
1380 STRCAT(buf, " ");
1381 STRCAT(buf, mod_str);
1382 }
1383
1384 *multi_mods = 1;
1385
1386 return result;
1387}
1388
1389/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001390 * Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one
Bram Moolenaare1004402020-10-24 20:49:43 +02001391 * was added. Return the number of bytes added.
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001392 */
1393 size_t
Bram Moolenaar02194d22020-10-24 23:08:38 +02001394add_win_cmd_modifers(char_u *buf, cmdmod_T *cmod, int *multi_mods)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001395{
1396 size_t result = 0;
1397
1398 // :aboveleft and :leftabove
Bram Moolenaar02194d22020-10-24 23:08:38 +02001399 if (cmod->cmod_split & WSP_ABOVE)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001400 result += add_cmd_modifier(buf, "aboveleft", multi_mods);
1401 // :belowright and :rightbelow
Bram Moolenaar02194d22020-10-24 23:08:38 +02001402 if (cmod->cmod_split & WSP_BELOW)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001403 result += add_cmd_modifier(buf, "belowright", multi_mods);
1404 // :botright
Bram Moolenaar02194d22020-10-24 23:08:38 +02001405 if (cmod->cmod_split & WSP_BOT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001406 result += add_cmd_modifier(buf, "botright", multi_mods);
1407
1408 // :tab
Bram Moolenaar02194d22020-10-24 23:08:38 +02001409 if (cmod->cmod_tab > 0)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001410 result += add_cmd_modifier(buf, "tab", multi_mods);
1411 // :topleft
Bram Moolenaar02194d22020-10-24 23:08:38 +02001412 if (cmod->cmod_split & WSP_TOP)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001413 result += add_cmd_modifier(buf, "topleft", multi_mods);
1414 // :vertical
Bram Moolenaar02194d22020-10-24 23:08:38 +02001415 if (cmod->cmod_split & WSP_VERT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001416 result += add_cmd_modifier(buf, "vertical", multi_mods);
1417 return result;
1418}
1419
1420/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001421 * Generate text for the "cmod" command modifiers.
1422 * If "buf" is NULL just return the length.
1423 */
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001424 size_t
Bram Moolenaar02194d22020-10-24 23:08:38 +02001425produce_cmdmods(char_u *buf, cmdmod_T *cmod, int quote)
1426{
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001427 size_t result = 0;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001428 int multi_mods = 0;
1429 int i;
1430 typedef struct {
1431 int flag;
1432 char *name;
1433 } mod_entry_T;
1434 static mod_entry_T mod_entries[] = {
1435#ifdef FEAT_BROWSE_CMD
1436 {CMOD_BROWSE, "browse"},
1437#endif
1438#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1439 {CMOD_CONFIRM, "confirm"},
1440#endif
1441 {CMOD_HIDE, "hide"},
1442 {CMOD_KEEPALT, "keepalt"},
1443 {CMOD_KEEPJUMPS, "keepjumps"},
1444 {CMOD_KEEPMARKS, "keepmarks"},
1445 {CMOD_KEEPPATTERNS, "keeppatterns"},
1446 {CMOD_LOCKMARKS, "lockmarks"},
1447 {CMOD_NOSWAPFILE, "noswapfile"},
1448 {CMOD_UNSILENT, "unsilent"},
1449 {CMOD_NOAUTOCMD, "noautocmd"},
1450#ifdef HAVE_SANDBOX
1451 {CMOD_SANDBOX, "sandbox"},
1452#endif
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00001453 {CMOD_LEGACY, "legacy"},
Bram Moolenaar02194d22020-10-24 23:08:38 +02001454 {0, NULL}
1455 };
1456
1457 result = quote ? 2 : 0;
1458 if (buf != NULL)
1459 {
1460 if (quote)
1461 *buf++ = '"';
1462 *buf = '\0';
1463 }
1464
1465 // the modifiers that are simple flags
1466 for (i = 0; mod_entries[i].name != NULL; ++i)
1467 if (cmod->cmod_flags & mod_entries[i].flag)
1468 result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods);
1469
1470 // :silent
1471 if (cmod->cmod_flags & CMOD_SILENT)
1472 result += add_cmd_modifier(buf,
1473 (cmod->cmod_flags & CMOD_ERRSILENT) ? "silent!"
1474 : "silent", &multi_mods);
1475 // :verbose
1476 if (p_verbose > 0)
1477 result += add_cmd_modifier(buf, "verbose", &multi_mods);
1478 // flags from cmod->cmod_split
1479 result += add_win_cmd_modifers(buf, cmod, &multi_mods);
1480 if (quote && buf != NULL)
1481 {
1482 buf += result - 2;
1483 *buf = '"';
1484 }
1485 return result;
1486}
1487
1488/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001489 * Check for a <> code in a user command.
1490 * "code" points to the '<'. "len" the length of the <> (inclusive).
1491 * "buf" is where the result is to be added.
1492 * "split_buf" points to a buffer used for splitting, caller should free it.
1493 * "split_len" is the length of what "split_buf" contains.
1494 * Returns the length of the replacement, which has been added to "buf".
1495 * Returns -1 if there was no match, and only the "<" has been copied.
1496 */
1497 static size_t
1498uc_check_code(
1499 char_u *code,
1500 size_t len,
1501 char_u *buf,
1502 ucmd_T *cmd, // the user command we're expanding
1503 exarg_T *eap, // ex arguments
1504 char_u **split_buf,
1505 size_t *split_len)
1506{
1507 size_t result = 0;
1508 char_u *p = code + 1;
1509 size_t l = len - 2;
1510 int quote = 0;
1511 enum {
1512 ct_ARGS,
1513 ct_BANG,
1514 ct_COUNT,
1515 ct_LINE1,
1516 ct_LINE2,
1517 ct_RANGE,
1518 ct_MODS,
1519 ct_REGISTER,
1520 ct_LT,
1521 ct_NONE
1522 } type = ct_NONE;
1523
1524 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
1525 {
1526 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
1527 p += 2;
1528 l -= 2;
1529 }
1530
1531 ++l;
1532 if (l <= 1)
1533 type = ct_NONE;
1534 else if (STRNICMP(p, "args>", l) == 0)
1535 type = ct_ARGS;
1536 else if (STRNICMP(p, "bang>", l) == 0)
1537 type = ct_BANG;
1538 else if (STRNICMP(p, "count>", l) == 0)
1539 type = ct_COUNT;
1540 else if (STRNICMP(p, "line1>", l) == 0)
1541 type = ct_LINE1;
1542 else if (STRNICMP(p, "line2>", l) == 0)
1543 type = ct_LINE2;
1544 else if (STRNICMP(p, "range>", l) == 0)
1545 type = ct_RANGE;
1546 else if (STRNICMP(p, "lt>", l) == 0)
1547 type = ct_LT;
1548 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
1549 type = ct_REGISTER;
1550 else if (STRNICMP(p, "mods>", l) == 0)
1551 type = ct_MODS;
1552
1553 switch (type)
1554 {
1555 case ct_ARGS:
1556 // Simple case first
1557 if (*eap->arg == NUL)
1558 {
1559 if (quote == 1)
1560 {
1561 result = 2;
1562 if (buf != NULL)
1563 STRCPY(buf, "''");
1564 }
1565 else
1566 result = 0;
1567 break;
1568 }
1569
1570 // When specified there is a single argument don't split it.
1571 // Works for ":Cmd %" when % is "a b c".
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001572 if ((eap->argt & EX_NOSPC) && quote == 2)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001573 quote = 1;
1574
1575 switch (quote)
1576 {
1577 case 0: // No quoting, no splitting
1578 result = STRLEN(eap->arg);
1579 if (buf != NULL)
1580 STRCPY(buf, eap->arg);
1581 break;
1582 case 1: // Quote, but don't split
1583 result = STRLEN(eap->arg) + 2;
1584 for (p = eap->arg; *p; ++p)
1585 {
1586 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1587 // DBCS can contain \ in a trail byte, skip the
1588 // double-byte character.
1589 ++p;
1590 else
1591 if (*p == '\\' || *p == '"')
1592 ++result;
1593 }
1594
1595 if (buf != NULL)
1596 {
1597 *buf++ = '"';
1598 for (p = eap->arg; *p; ++p)
1599 {
1600 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1601 // DBCS can contain \ in a trail byte, copy the
1602 // double-byte character to avoid escaping.
1603 *buf++ = *p++;
1604 else
1605 if (*p == '\\' || *p == '"')
1606 *buf++ = '\\';
1607 *buf++ = *p;
1608 }
1609 *buf = '"';
1610 }
1611
1612 break;
1613 case 2: // Quote and split (<f-args>)
1614 // This is hard, so only do it once, and cache the result
1615 if (*split_buf == NULL)
1616 *split_buf = uc_split_args(eap->arg, split_len);
1617
1618 result = *split_len;
1619 if (buf != NULL && result != 0)
1620 STRCPY(buf, *split_buf);
1621
1622 break;
1623 }
1624 break;
1625
1626 case ct_BANG:
1627 result = eap->forceit ? 1 : 0;
1628 if (quote)
1629 result += 2;
1630 if (buf != NULL)
1631 {
1632 if (quote)
1633 *buf++ = '"';
1634 if (eap->forceit)
1635 *buf++ = '!';
1636 if (quote)
1637 *buf = '"';
1638 }
1639 break;
1640
1641 case ct_LINE1:
1642 case ct_LINE2:
1643 case ct_RANGE:
1644 case ct_COUNT:
1645 {
1646 char num_buf[20];
1647 long num = (type == ct_LINE1) ? eap->line1 :
1648 (type == ct_LINE2) ? eap->line2 :
1649 (type == ct_RANGE) ? eap->addr_count :
1650 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
1651 size_t num_len;
1652
1653 sprintf(num_buf, "%ld", num);
1654 num_len = STRLEN(num_buf);
1655 result = num_len;
1656
1657 if (quote)
1658 result += 2;
1659
1660 if (buf != NULL)
1661 {
1662 if (quote)
1663 *buf++ = '"';
1664 STRCPY(buf, num_buf);
1665 buf += num_len;
1666 if (quote)
1667 *buf = '"';
1668 }
1669
1670 break;
1671 }
1672
1673 case ct_MODS:
1674 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001675 result = produce_cmdmods(buf, &cmdmod, quote);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001676 break;
1677 }
1678
1679 case ct_REGISTER:
1680 result = eap->regname ? 1 : 0;
1681 if (quote)
1682 result += 2;
1683 if (buf != NULL)
1684 {
1685 if (quote)
1686 *buf++ = '\'';
1687 if (eap->regname)
1688 *buf++ = eap->regname;
1689 if (quote)
1690 *buf = '\'';
1691 }
1692 break;
1693
1694 case ct_LT:
1695 result = 1;
1696 if (buf != NULL)
1697 *buf = '<';
1698 break;
1699
1700 default:
1701 // Not recognized: just copy the '<' and return -1.
1702 result = (size_t)-1;
1703 if (buf != NULL)
1704 *buf = '<';
1705 break;
1706 }
1707
1708 return result;
1709}
1710
1711/*
1712 * Execute a user defined command.
1713 */
1714 void
1715do_ucmd(exarg_T *eap)
1716{
1717 char_u *buf;
1718 char_u *p;
1719 char_u *q;
1720
1721 char_u *start;
1722 char_u *end = NULL;
1723 char_u *ksp;
1724 size_t len, totlen;
1725
1726 size_t split_len = 0;
1727 char_u *split_buf = NULL;
1728 ucmd_T *cmd;
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001729 sctx_T save_current_sctx;
1730 int restore_current_sctx = FALSE;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001731#ifdef FEAT_EVAL
1732 int restore_script_version = 0;
1733#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001734
1735 if (eap->cmdidx == CMD_USER)
1736 cmd = USER_CMD(eap->useridx);
1737 else
1738 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
1739
1740 /*
1741 * Replace <> in the command by the arguments.
1742 * First round: "buf" is NULL, compute length, allocate "buf".
1743 * Second round: copy result into "buf".
1744 */
1745 buf = NULL;
1746 for (;;)
1747 {
1748 p = cmd->uc_rep; // source
1749 q = buf; // destination
1750 totlen = 0;
1751
1752 for (;;)
1753 {
1754 start = vim_strchr(p, '<');
1755 if (start != NULL)
1756 end = vim_strchr(start + 1, '>');
1757 if (buf != NULL)
1758 {
1759 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
1760 ;
1761 if (*ksp == K_SPECIAL
1762 && (start == NULL || ksp < start || end == NULL)
1763 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
1764# ifdef FEAT_GUI
1765 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
1766# endif
1767 ))
1768 {
1769 // K_SPECIAL has been put in the buffer as K_SPECIAL
1770 // KS_SPECIAL KE_FILLER, like for mappings, but
1771 // do_cmdline() doesn't handle that, so convert it back.
1772 // Also change K_SPECIAL KS_EXTRA KE_CSI into CSI.
1773 len = ksp - p;
1774 if (len > 0)
1775 {
1776 mch_memmove(q, p, len);
1777 q += len;
1778 }
1779 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
1780 p = ksp + 3;
1781 continue;
1782 }
1783 }
1784
1785 // break if no <item> is found
1786 if (start == NULL || end == NULL)
1787 break;
1788
1789 // Include the '>'
1790 ++end;
1791
1792 // Take everything up to the '<'
1793 len = start - p;
1794 if (buf == NULL)
1795 totlen += len;
1796 else
1797 {
1798 mch_memmove(q, p, len);
1799 q += len;
1800 }
1801
1802 len = uc_check_code(start, end - start, q, cmd, eap,
1803 &split_buf, &split_len);
1804 if (len == (size_t)-1)
1805 {
1806 // no match, continue after '<'
1807 p = start + 1;
1808 len = 1;
1809 }
1810 else
1811 p = end;
1812 if (buf == NULL)
1813 totlen += len;
1814 else
1815 q += len;
1816 }
1817 if (buf != NULL) // second time here, finished
1818 {
1819 STRCPY(q, p);
1820 break;
1821 }
1822
1823 totlen += STRLEN(p); // Add on the trailing characters
Bram Moolenaar964b3742019-05-24 18:54:09 +02001824 buf = alloc(totlen + 1);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001825 if (buf == NULL)
1826 {
1827 vim_free(split_buf);
1828 return;
1829 }
1830 }
1831
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001832 if ((cmd->uc_argt & EX_KEEPSCRIPT) == 0)
1833 {
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001834 restore_current_sctx = TRUE;
1835 save_current_sctx = current_sctx;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001836 current_sctx.sc_version = cmd->uc_script_ctx.sc_version;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001837#ifdef FEAT_EVAL
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001838 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001839 if (cmd->uc_flags & UC_VIM9)
1840 {
1841 // In a {} block variables use Vim9 script rules, even in a legacy
1842 // script.
1843 restore_script_version =
1844 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version;
1845 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = SCRIPT_VERSION_VIM9;
1846 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001847#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001848 }
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001849
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001850 (void)do_cmdline(buf, eap->getline, eap->cookie,
1851 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001852
1853 // Careful: Do not use "cmd" here, it may have become invalid if a user
1854 // command was added.
1855 if (restore_current_sctx)
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001856 {
1857#ifdef FEAT_EVAL
1858 if (restore_script_version != 0)
1859 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version =
1860 restore_script_version;
1861#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001862 current_sctx = save_current_sctx;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001863 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001864 vim_free(buf);
1865 vim_free(split_buf);
1866}