blob: e73f6f64fbfdf3be2082aab35fc6fbd7bd0ef170 [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
Shougo Matsushita79d599b2022-05-07 12:48:29 +0100446/*
447 * Get the name of completion type "expand" as a string.
448 */
449 char_u *
450cmdcomplete_type_to_str(int expand)
451{
452 int i;
453
454 for (i = 0; command_complete[i].expand != 0; i++)
455 if (command_complete[i].expand == expand)
456 return (char_u *)command_complete[i].name;
457
458 return NULL;
459}
460
461/*
462 * Get the index of completion type "complete_str".
463 * Returns EXPAND_NOTHING if no match found.
464 */
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200465 int
466cmdcomplete_str_to_type(char_u *complete_str)
467{
468 int i;
469
470 for (i = 0; command_complete[i].expand != 0; ++i)
471 if (STRCMP(complete_str, command_complete[i].name) == 0)
472 return command_complete[i].expand;
473
474 return EXPAND_NOTHING;
475}
Dominique Pelle748b3082022-01-08 12:41:16 +0000476#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200477
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200478/*
479 * List user commands starting with "name[name_len]".
480 */
481 static void
482uc_list(char_u *name, size_t name_len)
483{
484 int i, j;
485 int found = FALSE;
486 ucmd_T *cmd;
487 int len;
488 int over;
489 long a;
490 garray_T *gap;
491
Bram Moolenaare38eab22019-12-05 21:50:01 +0100492 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000493 gap = &prevwin_curwin()->w_buffer->b_ucmds;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200494 for (;;)
495 {
496 for (i = 0; i < gap->ga_len; ++i)
497 {
498 cmd = USER_CMD_GA(gap, i);
499 a = (long)cmd->uc_argt;
500
501 // Skip commands which don't match the requested prefix and
502 // commands filtered out.
503 if (STRNCMP(name, cmd->uc_name, name_len) != 0
504 || message_filtered(cmd->uc_name))
505 continue;
506
507 // Put out the title first time
508 if (!found)
509 msg_puts_title(_("\n Name Args Address Complete Definition"));
510 found = TRUE;
511 msg_putchar('\n');
512 if (got_int)
513 break;
514
515 // Special cases
516 len = 4;
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200517 if (a & EX_BANG)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200518 {
519 msg_putchar('!');
520 --len;
521 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200522 if (a & EX_REGSTR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200523 {
524 msg_putchar('"');
525 --len;
526 }
527 if (gap != &ucmds)
528 {
529 msg_putchar('b');
530 --len;
531 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200532 if (a & EX_TRLBAR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200533 {
534 msg_putchar('|');
535 --len;
536 }
537 while (len-- > 0)
538 msg_putchar(' ');
539
540 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
541 len = (int)STRLEN(cmd->uc_name) + 4;
542
543 do {
544 msg_putchar(' ');
545 ++len;
546 } while (len < 22);
547
548 // "over" is how much longer the name is than the column width for
549 // the name, we'll try to align what comes after.
550 over = len - 22;
551 len = 0;
552
553 // Arguments
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200554 switch ((int)(a & (EX_EXTRA|EX_NOSPC|EX_NEEDARG)))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200555 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200556 case 0: IObuff[len++] = '0'; break;
557 case (EX_EXTRA): IObuff[len++] = '*'; break;
558 case (EX_EXTRA|EX_NOSPC): IObuff[len++] = '?'; break;
559 case (EX_EXTRA|EX_NEEDARG): IObuff[len++] = '+'; break;
560 case (EX_EXTRA|EX_NOSPC|EX_NEEDARG): IObuff[len++] = '1'; break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200561 }
562
563 do {
564 IObuff[len++] = ' ';
565 } while (len < 5 - over);
566
567 // Address / Range
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200568 if (a & (EX_RANGE|EX_COUNT))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200569 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200570 if (a & EX_COUNT)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200571 {
572 // -count=N
573 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
574 len += (int)STRLEN(IObuff + len);
575 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200576 else if (a & EX_DFLALL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200577 IObuff[len++] = '%';
578 else if (cmd->uc_def >= 0)
579 {
580 // -range=N
581 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
582 len += (int)STRLEN(IObuff + len);
583 }
584 else
585 IObuff[len++] = '.';
586 }
587
588 do {
589 IObuff[len++] = ' ';
590 } while (len < 8 - over);
591
592 // Address Type
Bram Moolenaarb7316892019-05-01 18:08:42 +0200593 for (j = 0; addr_type_complete[j].expand != ADDR_NONE; ++j)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200594 if (addr_type_complete[j].expand != ADDR_LINES
595 && addr_type_complete[j].expand == cmd->uc_addr_type)
596 {
597 STRCPY(IObuff + len, addr_type_complete[j].shortname);
598 len += (int)STRLEN(IObuff + len);
599 break;
600 }
601
602 do {
603 IObuff[len++] = ' ';
604 } while (len < 13 - over);
605
606 // Completion
607 for (j = 0; command_complete[j].expand != 0; ++j)
608 if (command_complete[j].expand == cmd->uc_compl)
609 {
610 STRCPY(IObuff + len, command_complete[j].name);
611 len += (int)STRLEN(IObuff + len);
Bram Moolenaar78f60322022-01-17 22:16:33 +0000612#ifdef FEAT_EVAL
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000613 if (p_verbose > 0 && cmd->uc_compl_arg != NULL
614 && STRLEN(cmd->uc_compl_arg) < 200)
615 {
616 IObuff[len] = ',';
617 STRCPY(IObuff + len + 1, cmd->uc_compl_arg);
618 len += (int)STRLEN(IObuff + len);
619 }
Bram Moolenaar78f60322022-01-17 22:16:33 +0000620#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200621 break;
622 }
623
624 do {
625 IObuff[len++] = ' ';
626 } while (len < 25 - over);
627
628 IObuff[len] = '\0';
629 msg_outtrans(IObuff);
630
631 msg_outtrans_special(cmd->uc_rep, FALSE,
632 name_len == 0 ? Columns - 47 : 0);
633#ifdef FEAT_EVAL
634 if (p_verbose > 0)
635 last_set_msg(cmd->uc_script_ctx);
636#endif
637 out_flush();
638 ui_breakcheck();
639 if (got_int)
640 break;
641 }
642 if (gap == &ucmds || i < gap->ga_len)
643 break;
644 gap = &ucmds;
645 }
646
647 if (!found)
648 msg(_("No user-defined commands found"));
649}
650
651 char *
652uc_fun_cmd(void)
653{
654 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
655 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
656 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
657 0xb9, 0x7f, 0};
658 int i;
659
660 for (i = 0; fcmd[i]; ++i)
661 IObuff[i] = fcmd[i] - 0x40;
662 IObuff[i] = 0;
663 return (char *)IObuff;
664}
665
666/*
667 * Parse address type argument
668 */
669 static int
670parse_addr_type_arg(
671 char_u *value,
672 int vallen,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200673 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200674{
675 int i, a, b;
676
Bram Moolenaarb7316892019-05-01 18:08:42 +0200677 for (i = 0; addr_type_complete[i].expand != ADDR_NONE; ++i)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200678 {
679 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
680 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
681 if (a && b)
682 {
683 *addr_type_arg = addr_type_complete[i].expand;
684 break;
685 }
686 }
687
Bram Moolenaarb7316892019-05-01 18:08:42 +0200688 if (addr_type_complete[i].expand == ADDR_NONE)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200689 {
690 char_u *err = value;
691
692 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
693 ;
694 err[i] = NUL;
Bram Moolenaar11de43d2022-01-06 21:41:11 +0000695 semsg(_(e_invalid_address_type_value_str), err);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200696 return FAIL;
697 }
698
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200699 return OK;
700}
701
702/*
703 * Parse a completion argument "value[vallen]".
704 * The detected completion goes in "*complp", argument type in "*argt".
705 * When there is an argument, for function and user defined completion, it's
706 * copied to allocated memory and stored in "*compl_arg".
707 * Returns FAIL if something is wrong.
708 */
709 int
710parse_compl_arg(
711 char_u *value,
712 int vallen,
713 int *complp,
714 long *argt,
715 char_u **compl_arg UNUSED)
716{
717 char_u *arg = NULL;
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200718# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200719 size_t arglen = 0;
720# endif
721 int i;
722 int valend = vallen;
723
724 // Look for any argument part - which is the part after any ','
725 for (i = 0; i < vallen; ++i)
726 {
727 if (value[i] == ',')
728 {
729 arg = &value[i + 1];
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200730# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200731 arglen = vallen - i - 1;
732# endif
733 valend = i;
734 break;
735 }
736 }
737
738 for (i = 0; command_complete[i].expand != 0; ++i)
739 {
740 if ((int)STRLEN(command_complete[i].name) == valend
741 && STRNCMP(value, command_complete[i].name, valend) == 0)
742 {
743 *complp = command_complete[i].expand;
744 if (command_complete[i].expand == EXPAND_BUFFERS)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200745 *argt |= EX_BUFNAME;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200746 else if (command_complete[i].expand == EXPAND_DIRECTORIES
747 || command_complete[i].expand == EXPAND_FILES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200748 *argt |= EX_XFILE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200749 break;
750 }
751 }
752
753 if (command_complete[i].expand == 0)
754 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000755 semsg(_(e_invalid_complete_value_str), value);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200756 return FAIL;
757 }
758
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200759# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200760 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
761 && arg != NULL)
762# else
763 if (arg != NULL)
764# endif
765 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000766 emsg(_(e_completion_argument_only_allowed_for_custom_completion));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200767 return FAIL;
768 }
769
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200770# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200771 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
772 && arg == NULL)
773 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000774 emsg(_(e_custom_completion_requires_function_argument));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200775 return FAIL;
776 }
777
778 if (arg != NULL)
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200779 *compl_arg = vim_strnsave(arg, arglen);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200780# endif
781 return OK;
782}
783
784/*
785 * Scan attributes in the ":command" command.
786 * Return FAIL when something is wrong.
787 */
788 static int
789uc_scan_attr(
790 char_u *attr,
791 size_t len,
792 long *argt,
793 long *def,
794 int *flags,
Bram Moolenaar52111f82019-04-29 21:30:45 +0200795 int *complp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200796 char_u **compl_arg,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200797 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200798{
799 char_u *p;
800
801 if (len == 0)
802 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000803 emsg(_(e_no_attribute_specified));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200804 return FAIL;
805 }
806
807 // First, try the simple attributes (no arguments)
808 if (STRNICMP(attr, "bang", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200809 *argt |= EX_BANG;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200810 else if (STRNICMP(attr, "buffer", len) == 0)
811 *flags |= UC_BUFFER;
812 else if (STRNICMP(attr, "register", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200813 *argt |= EX_REGSTR;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000814 else if (STRNICMP(attr, "keepscript", len) == 0)
815 *argt |= EX_KEEPSCRIPT;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200816 else if (STRNICMP(attr, "bar", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200817 *argt |= EX_TRLBAR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200818 else
819 {
820 int i;
821 char_u *val = NULL;
822 size_t vallen = 0;
823 size_t attrlen = len;
824
825 // Look for the attribute name - which is the part before any '='
826 for (i = 0; i < (int)len; ++i)
827 {
828 if (attr[i] == '=')
829 {
830 val = &attr[i + 1];
831 vallen = len - i - 1;
832 attrlen = i;
833 break;
834 }
835 }
836
837 if (STRNICMP(attr, "nargs", attrlen) == 0)
838 {
839 if (vallen == 1)
840 {
841 if (*val == '0')
842 // Do nothing - this is the default
843 ;
844 else if (*val == '1')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200845 *argt |= (EX_EXTRA | EX_NOSPC | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200846 else if (*val == '*')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200847 *argt |= EX_EXTRA;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200848 else if (*val == '?')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200849 *argt |= (EX_EXTRA | EX_NOSPC);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200850 else if (*val == '+')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200851 *argt |= (EX_EXTRA | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200852 else
853 goto wrong_nargs;
854 }
855 else
856 {
857wrong_nargs:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000858 emsg(_(e_invalid_number_of_arguments));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200859 return FAIL;
860 }
861 }
862 else if (STRNICMP(attr, "range", attrlen) == 0)
863 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200864 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200865 if (vallen == 1 && *val == '%')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200866 *argt |= EX_DFLALL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200867 else if (val != NULL)
868 {
869 p = val;
870 if (*def >= 0)
871 {
872two_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000873 emsg(_(e_count_cannot_be_specified_twice));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200874 return FAIL;
875 }
876
877 *def = getdigits(&p);
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200878 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200879
880 if (p != val + vallen || vallen == 0)
881 {
882invalid_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000883 emsg(_(e_invalid_default_value_for_count));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200884 return FAIL;
885 }
886 }
Bram Moolenaarb7316892019-05-01 18:08:42 +0200887 // default for -range is using buffer lines
888 if (*addr_type_arg == ADDR_NONE)
889 *addr_type_arg = ADDR_LINES;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200890 }
891 else if (STRNICMP(attr, "count", attrlen) == 0)
892 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200893 *argt |= (EX_COUNT | EX_ZEROR | EX_RANGE);
Bram Moolenaarb7316892019-05-01 18:08:42 +0200894 // default for -count is using any number
895 if (*addr_type_arg == ADDR_NONE)
896 *addr_type_arg = ADDR_OTHER;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200897
898 if (val != NULL)
899 {
900 p = val;
901 if (*def >= 0)
902 goto two_count;
903
904 *def = getdigits(&p);
905
906 if (p != val + vallen)
907 goto invalid_count;
908 }
909
910 if (*def < 0)
911 *def = 0;
912 }
913 else if (STRNICMP(attr, "complete", attrlen) == 0)
914 {
915 if (val == NULL)
916 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000917 semsg(_(e_argument_required_for_str), "-complete");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200918 return FAIL;
919 }
920
Bram Moolenaar52111f82019-04-29 21:30:45 +0200921 if (parse_compl_arg(val, (int)vallen, complp, argt, compl_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200922 == FAIL)
923 return FAIL;
924 }
925 else if (STRNICMP(attr, "addr", attrlen) == 0)
926 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200927 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200928 if (val == NULL)
929 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000930 semsg(_(e_argument_required_for_str), "-addr");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200931 return FAIL;
932 }
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200933 if (parse_addr_type_arg(val, (int)vallen, addr_type_arg) == FAIL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200934 return FAIL;
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200935 if (*addr_type_arg != ADDR_LINES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200936 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200937 }
938 else
939 {
940 char_u ch = attr[len];
941 attr[len] = '\0';
Bram Moolenaar1a992222021-12-31 17:25:48 +0000942 semsg(_(e_invalid_attribute_str), attr);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200943 attr[len] = ch;
944 return FAIL;
945 }
946 }
947
948 return OK;
949}
950
951/*
952 * Add a user command to the list or replace an existing one.
953 */
954 static int
955uc_add_command(
956 char_u *name,
957 size_t name_len,
958 char_u *rep,
959 long argt,
960 long def,
961 int flags,
962 int compl,
963 char_u *compl_arg UNUSED,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200964 cmd_addr_T addr_type,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200965 int force)
966{
967 ucmd_T *cmd = NULL;
968 char_u *p;
969 int i;
970 int cmp = 1;
971 char_u *rep_buf = NULL;
972 garray_T *gap;
973
Bram Moolenaar459fd782019-10-13 16:43:39 +0200974 replace_termcodes(rep, &rep_buf, 0, NULL);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200975 if (rep_buf == NULL)
976 {
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200977 // can't replace termcodes - try using the string as is
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200978 rep_buf = vim_strsave(rep);
979
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200980 // give up if out of memory
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200981 if (rep_buf == NULL)
982 return FAIL;
983 }
984
985 // get address of growarray: global or in curbuf
986 if (flags & UC_BUFFER)
987 {
988 gap = &curbuf->b_ucmds;
989 if (gap->ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000990 ga_init2(gap, sizeof(ucmd_T), 4);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200991 }
992 else
993 gap = &ucmds;
994
995 // Search for the command in the already defined commands.
996 for (i = 0; i < gap->ga_len; ++i)
997 {
998 size_t len;
999
1000 cmd = USER_CMD_GA(gap, i);
1001 len = STRLEN(cmd->uc_name);
1002 cmp = STRNCMP(name, cmd->uc_name, name_len);
1003 if (cmp == 0)
1004 {
1005 if (name_len < len)
1006 cmp = -1;
1007 else if (name_len > len)
1008 cmp = 1;
1009 }
1010
1011 if (cmp == 0)
1012 {
1013 // Command can be replaced with "command!" and when sourcing the
1014 // same script again, but only once.
1015 if (!force
1016#ifdef FEAT_EVAL
1017 && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
1018 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq)
1019#endif
1020 )
1021 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001022 semsg(_(e_command_already_exists_add_bang_to_replace_it_str),
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001023 name);
1024 goto fail;
1025 }
1026
1027 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001028#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001029 VIM_CLEAR(cmd->uc_compl_arg);
1030#endif
1031 break;
1032 }
1033
1034 // Stop as soon as we pass the name to add
1035 if (cmp < 0)
1036 break;
1037 }
1038
1039 // Extend the array unless we're replacing an existing command
1040 if (cmp != 0)
1041 {
1042 if (ga_grow(gap, 1) != OK)
1043 goto fail;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001044 if ((p = vim_strnsave(name, name_len)) == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001045 goto fail;
1046
1047 cmd = USER_CMD_GA(gap, i);
1048 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
1049
1050 ++gap->ga_len;
1051
1052 cmd->uc_name = p;
1053 }
1054
1055 cmd->uc_rep = rep_buf;
1056 cmd->uc_argt = argt;
1057 cmd->uc_def = def;
1058 cmd->uc_compl = compl;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001059 cmd->uc_script_ctx = current_sctx;
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001060 if (flags & UC_VIM9)
1061 cmd->uc_script_ctx.sc_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001062 cmd->uc_flags = flags & UC_VIM9;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001063#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001064 cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001065 cmd->uc_compl_arg = compl_arg;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001066#endif
1067 cmd->uc_addr_type = addr_type;
1068
1069 return OK;
1070
1071fail:
1072 vim_free(rep_buf);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001073#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001074 vim_free(compl_arg);
1075#endif
1076 return FAIL;
1077}
1078
1079/*
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001080 * If "p" starts with "{" then read a block of commands until "}".
1081 * Used for ":command" and ":autocmd".
1082 */
1083 char_u *
1084may_get_cmd_block(exarg_T *eap, char_u *p, char_u **tofree, int *flags)
1085{
1086 char_u *retp = p;
1087
1088 if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
1089 && eap->getline != NULL)
1090 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001091 garray_T ga;
1092 char_u *line = NULL;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001093
1094 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001095 if (ga_copy_string(&ga, p) == FAIL)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001096 return retp;
1097
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001098 // If the argument ends in "}" it must have been concatenated already
1099 // for ISN_EXEC.
1100 if (p[STRLEN(p) - 1] != '}')
1101 // Read lines between '{' and '}'. Does not support nesting or
1102 // here-doc constructs.
1103 for (;;)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001104 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001105 vim_free(line);
1106 if ((line = eap->getline(':', eap->cookie,
1107 0, GETLINE_CONCAT_CONTBAR)) == NULL)
1108 {
1109 emsg(_(e_missing_rcurly));
1110 break;
1111 }
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001112 if (ga_copy_string(&ga, line) == FAIL)
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001113 break;
1114 if (*skipwhite(line) == '}')
1115 break;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001116 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001117 vim_free(line);
1118 retp = *tofree = ga_concat_strings(&ga, "\n");
1119 ga_clear_strings(&ga);
1120 *flags |= UC_VIM9;
1121 }
1122 return retp;
1123}
1124
1125/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001126 * ":command ..." implementation
1127 */
1128 void
1129ex_command(exarg_T *eap)
1130{
Bram Moolenaarb7316892019-05-01 18:08:42 +02001131 char_u *name;
1132 char_u *end;
1133 char_u *p;
1134 long argt = 0;
1135 long def = -1;
1136 int flags = 0;
1137 int compl = EXPAND_NOTHING;
1138 char_u *compl_arg = NULL;
1139 cmd_addr_T addr_type_arg = ADDR_NONE;
1140 int has_attr = (eap->arg[0] == '-');
1141 int name_len;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001142
1143 p = eap->arg;
1144
1145 // Check for attributes
1146 while (*p == '-')
1147 {
1148 ++p;
1149 end = skiptowhite(p);
1150 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
1151 &compl_arg, &addr_type_arg) == FAIL)
1152 return;
1153 p = skipwhite(end);
1154 }
1155
1156 // Get the name (if any) and skip to the following argument
1157 name = p;
1158 if (ASCII_ISALPHA(*p))
1159 while (ASCII_ISALNUM(*p))
1160 ++p;
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001161 if (!ends_excmd2(eap->arg, p) && !VIM_ISWHITE(*p))
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001162 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001163 emsg(_(e_invalid_command_name));
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001164 return;
1165 }
1166 end = p;
1167 name_len = (int)(end - name);
1168
1169 // If there is nothing after the name, and no attributes were specified,
1170 // we are listing commands
1171 p = skipwhite(end);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001172 if (!has_attr && ends_excmd2(eap->arg, p))
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001173 uc_list(name, end - name);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001174 else if (!ASCII_ISUPPER(*name))
Bram Moolenaar1a992222021-12-31 17:25:48 +00001175 emsg(_(e_user_defined_commands_must_start_with_an_uppercase_letter));
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001176 else if ((name_len == 1 && *name == 'X')
1177 || (name_len <= 4
1178 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001179 emsg(_(e_reserved_name_cannot_be_used_for_user_defined_command));
Martin Tournoijde69a732021-07-11 14:28:25 +02001180 else if (compl > 0 && (argt & EX_EXTRA) == 0)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001181 {
1182 // Some plugins rely on silently ignoring the mistake, only make this
1183 // an error in Vim9 script.
1184 if (in_vim9script())
Bram Moolenaar41a34852021-08-04 16:09:24 +02001185 emsg(_(e_complete_used_without_allowing_arguments));
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001186 else
1187 give_warning_with_source(
Bram Moolenaar41a34852021-08-04 16:09:24 +02001188 (char_u *)_(e_complete_used_without_allowing_arguments),
1189 TRUE, TRUE);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001190 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001191 else
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001192 {
1193 char_u *tofree = NULL;
1194
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001195 p = may_get_cmd_block(eap, p, &tofree, &flags);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001196
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001197 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
1198 addr_type_arg, eap->forceit);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001199 vim_free(tofree);
1200 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001201}
1202
1203/*
1204 * ":comclear" implementation
1205 * Clear all user commands, global and for current buffer.
1206 */
1207 void
1208ex_comclear(exarg_T *eap UNUSED)
1209{
1210 uc_clear(&ucmds);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001211 if (curbuf != NULL)
1212 uc_clear(&curbuf->b_ucmds);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001213}
1214
1215/*
1216 * Clear all user commands for "gap".
1217 */
1218 void
1219uc_clear(garray_T *gap)
1220{
1221 int i;
1222 ucmd_T *cmd;
1223
1224 for (i = 0; i < gap->ga_len; ++i)
1225 {
1226 cmd = USER_CMD_GA(gap, i);
1227 vim_free(cmd->uc_name);
1228 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001229# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001230 vim_free(cmd->uc_compl_arg);
1231# endif
1232 }
1233 ga_clear(gap);
1234}
1235
1236/*
1237 * ":delcommand" implementation
1238 */
1239 void
1240ex_delcommand(exarg_T *eap)
1241{
1242 int i = 0;
1243 ucmd_T *cmd = NULL;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001244 int res = -1;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001245 garray_T *gap;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001246 char_u *arg = eap->arg;
1247 int buffer_only = FALSE;
1248
1249 if (STRNCMP(arg, "-buffer", 7) == 0 && VIM_ISWHITE(arg[7]))
1250 {
1251 buffer_only = TRUE;
1252 arg = skipwhite(arg + 7);
1253 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001254
1255 gap = &curbuf->b_ucmds;
1256 for (;;)
1257 {
1258 for (i = 0; i < gap->ga_len; ++i)
1259 {
1260 cmd = USER_CMD_GA(gap, i);
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001261 res = STRCMP(arg, cmd->uc_name);
1262 if (res <= 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001263 break;
1264 }
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001265 if (gap == &ucmds || res == 0 || buffer_only)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001266 break;
1267 gap = &ucmds;
1268 }
1269
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001270 if (res != 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001271 {
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001272 semsg(_(buffer_only
1273 ? e_no_such_user_defined_command_in_current_buffer_str
1274 : e_no_such_user_defined_command_str), arg);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001275 return;
1276 }
1277
1278 vim_free(cmd->uc_name);
1279 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001280# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001281 vim_free(cmd->uc_compl_arg);
1282# endif
1283
1284 --gap->ga_len;
1285
1286 if (i < gap->ga_len)
1287 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
1288}
1289
1290/*
1291 * Split and quote args for <f-args>.
1292 */
1293 static char_u *
1294uc_split_args(char_u *arg, size_t *lenp)
1295{
1296 char_u *buf;
1297 char_u *p;
1298 char_u *q;
1299 int len;
1300
1301 // Precalculate length
1302 p = arg;
1303 len = 2; // Initial and final quotes
1304
1305 while (*p)
1306 {
1307 if (p[0] == '\\' && p[1] == '\\')
1308 {
1309 len += 2;
1310 p += 2;
1311 }
1312 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1313 {
1314 len += 1;
1315 p += 2;
1316 }
1317 else if (*p == '\\' || *p == '"')
1318 {
1319 len += 2;
1320 p += 1;
1321 }
1322 else if (VIM_ISWHITE(*p))
1323 {
1324 p = skipwhite(p);
1325 if (*p == NUL)
1326 break;
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001327 len += 4; // ", "
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001328 }
1329 else
1330 {
1331 int charlen = (*mb_ptr2len)(p);
1332
1333 len += charlen;
1334 p += charlen;
1335 }
1336 }
1337
1338 buf = alloc(len + 1);
1339 if (buf == NULL)
1340 {
1341 *lenp = 0;
1342 return buf;
1343 }
1344
1345 p = arg;
1346 q = buf;
1347 *q++ = '"';
1348 while (*p)
1349 {
1350 if (p[0] == '\\' && p[1] == '\\')
1351 {
1352 *q++ = '\\';
1353 *q++ = '\\';
1354 p += 2;
1355 }
1356 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1357 {
1358 *q++ = p[1];
1359 p += 2;
1360 }
1361 else if (*p == '\\' || *p == '"')
1362 {
1363 *q++ = '\\';
1364 *q++ = *p++;
1365 }
1366 else if (VIM_ISWHITE(*p))
1367 {
1368 p = skipwhite(p);
1369 if (*p == NUL)
1370 break;
1371 *q++ = '"';
1372 *q++ = ',';
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001373 *q++ = ' ';
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001374 *q++ = '"';
1375 }
1376 else
1377 {
1378 MB_COPY_CHAR(p, q);
1379 }
1380 }
1381 *q++ = '"';
1382 *q = 0;
1383
1384 *lenp = len;
1385 return buf;
1386}
1387
1388 static size_t
1389add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
1390{
1391 size_t result;
1392
1393 result = STRLEN(mod_str);
1394 if (*multi_mods)
1395 result += 1;
1396 if (buf != NULL)
1397 {
1398 if (*multi_mods)
1399 STRCAT(buf, " ");
1400 STRCAT(buf, mod_str);
1401 }
1402
1403 *multi_mods = 1;
1404
1405 return result;
1406}
1407
1408/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001409 * Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one
Bram Moolenaare1004402020-10-24 20:49:43 +02001410 * was added. Return the number of bytes added.
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001411 */
1412 size_t
Bram Moolenaar02194d22020-10-24 23:08:38 +02001413add_win_cmd_modifers(char_u *buf, cmdmod_T *cmod, int *multi_mods)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001414{
1415 size_t result = 0;
1416
1417 // :aboveleft and :leftabove
Bram Moolenaar02194d22020-10-24 23:08:38 +02001418 if (cmod->cmod_split & WSP_ABOVE)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001419 result += add_cmd_modifier(buf, "aboveleft", multi_mods);
1420 // :belowright and :rightbelow
Bram Moolenaar02194d22020-10-24 23:08:38 +02001421 if (cmod->cmod_split & WSP_BELOW)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001422 result += add_cmd_modifier(buf, "belowright", multi_mods);
1423 // :botright
Bram Moolenaar02194d22020-10-24 23:08:38 +02001424 if (cmod->cmod_split & WSP_BOT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001425 result += add_cmd_modifier(buf, "botright", multi_mods);
1426
1427 // :tab
Bram Moolenaar02194d22020-10-24 23:08:38 +02001428 if (cmod->cmod_tab > 0)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001429 result += add_cmd_modifier(buf, "tab", multi_mods);
1430 // :topleft
Bram Moolenaar02194d22020-10-24 23:08:38 +02001431 if (cmod->cmod_split & WSP_TOP)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001432 result += add_cmd_modifier(buf, "topleft", multi_mods);
1433 // :vertical
Bram Moolenaar02194d22020-10-24 23:08:38 +02001434 if (cmod->cmod_split & WSP_VERT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001435 result += add_cmd_modifier(buf, "vertical", multi_mods);
1436 return result;
1437}
1438
1439/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001440 * Generate text for the "cmod" command modifiers.
1441 * If "buf" is NULL just return the length.
1442 */
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001443 size_t
Bram Moolenaar02194d22020-10-24 23:08:38 +02001444produce_cmdmods(char_u *buf, cmdmod_T *cmod, int quote)
1445{
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001446 size_t result = 0;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001447 int multi_mods = 0;
1448 int i;
1449 typedef struct {
1450 int flag;
1451 char *name;
1452 } mod_entry_T;
1453 static mod_entry_T mod_entries[] = {
1454#ifdef FEAT_BROWSE_CMD
1455 {CMOD_BROWSE, "browse"},
1456#endif
1457#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1458 {CMOD_CONFIRM, "confirm"},
1459#endif
1460 {CMOD_HIDE, "hide"},
1461 {CMOD_KEEPALT, "keepalt"},
1462 {CMOD_KEEPJUMPS, "keepjumps"},
1463 {CMOD_KEEPMARKS, "keepmarks"},
1464 {CMOD_KEEPPATTERNS, "keeppatterns"},
1465 {CMOD_LOCKMARKS, "lockmarks"},
1466 {CMOD_NOSWAPFILE, "noswapfile"},
1467 {CMOD_UNSILENT, "unsilent"},
1468 {CMOD_NOAUTOCMD, "noautocmd"},
1469#ifdef HAVE_SANDBOX
1470 {CMOD_SANDBOX, "sandbox"},
1471#endif
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00001472 {CMOD_LEGACY, "legacy"},
Bram Moolenaar02194d22020-10-24 23:08:38 +02001473 {0, NULL}
1474 };
1475
1476 result = quote ? 2 : 0;
1477 if (buf != NULL)
1478 {
1479 if (quote)
1480 *buf++ = '"';
1481 *buf = '\0';
1482 }
1483
1484 // the modifiers that are simple flags
1485 for (i = 0; mod_entries[i].name != NULL; ++i)
1486 if (cmod->cmod_flags & mod_entries[i].flag)
1487 result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods);
1488
1489 // :silent
1490 if (cmod->cmod_flags & CMOD_SILENT)
1491 result += add_cmd_modifier(buf,
1492 (cmod->cmod_flags & CMOD_ERRSILENT) ? "silent!"
1493 : "silent", &multi_mods);
1494 // :verbose
1495 if (p_verbose > 0)
1496 result += add_cmd_modifier(buf, "verbose", &multi_mods);
1497 // flags from cmod->cmod_split
1498 result += add_win_cmd_modifers(buf, cmod, &multi_mods);
1499 if (quote && buf != NULL)
1500 {
1501 buf += result - 2;
1502 *buf = '"';
1503 }
1504 return result;
1505}
1506
1507/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001508 * Check for a <> code in a user command.
1509 * "code" points to the '<'. "len" the length of the <> (inclusive).
1510 * "buf" is where the result is to be added.
1511 * "split_buf" points to a buffer used for splitting, caller should free it.
1512 * "split_len" is the length of what "split_buf" contains.
1513 * Returns the length of the replacement, which has been added to "buf".
1514 * Returns -1 if there was no match, and only the "<" has been copied.
1515 */
1516 static size_t
1517uc_check_code(
1518 char_u *code,
1519 size_t len,
1520 char_u *buf,
1521 ucmd_T *cmd, // the user command we're expanding
1522 exarg_T *eap, // ex arguments
1523 char_u **split_buf,
1524 size_t *split_len)
1525{
1526 size_t result = 0;
1527 char_u *p = code + 1;
1528 size_t l = len - 2;
1529 int quote = 0;
1530 enum {
1531 ct_ARGS,
1532 ct_BANG,
1533 ct_COUNT,
1534 ct_LINE1,
1535 ct_LINE2,
1536 ct_RANGE,
1537 ct_MODS,
1538 ct_REGISTER,
1539 ct_LT,
1540 ct_NONE
1541 } type = ct_NONE;
1542
1543 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
1544 {
1545 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
1546 p += 2;
1547 l -= 2;
1548 }
1549
1550 ++l;
1551 if (l <= 1)
1552 type = ct_NONE;
1553 else if (STRNICMP(p, "args>", l) == 0)
1554 type = ct_ARGS;
1555 else if (STRNICMP(p, "bang>", l) == 0)
1556 type = ct_BANG;
1557 else if (STRNICMP(p, "count>", l) == 0)
1558 type = ct_COUNT;
1559 else if (STRNICMP(p, "line1>", l) == 0)
1560 type = ct_LINE1;
1561 else if (STRNICMP(p, "line2>", l) == 0)
1562 type = ct_LINE2;
1563 else if (STRNICMP(p, "range>", l) == 0)
1564 type = ct_RANGE;
1565 else if (STRNICMP(p, "lt>", l) == 0)
1566 type = ct_LT;
1567 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
1568 type = ct_REGISTER;
1569 else if (STRNICMP(p, "mods>", l) == 0)
1570 type = ct_MODS;
1571
1572 switch (type)
1573 {
1574 case ct_ARGS:
1575 // Simple case first
1576 if (*eap->arg == NUL)
1577 {
1578 if (quote == 1)
1579 {
1580 result = 2;
1581 if (buf != NULL)
1582 STRCPY(buf, "''");
1583 }
1584 else
1585 result = 0;
1586 break;
1587 }
1588
1589 // When specified there is a single argument don't split it.
1590 // Works for ":Cmd %" when % is "a b c".
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001591 if ((eap->argt & EX_NOSPC) && quote == 2)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001592 quote = 1;
1593
1594 switch (quote)
1595 {
1596 case 0: // No quoting, no splitting
1597 result = STRLEN(eap->arg);
1598 if (buf != NULL)
1599 STRCPY(buf, eap->arg);
1600 break;
1601 case 1: // Quote, but don't split
1602 result = STRLEN(eap->arg) + 2;
1603 for (p = eap->arg; *p; ++p)
1604 {
1605 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1606 // DBCS can contain \ in a trail byte, skip the
1607 // double-byte character.
1608 ++p;
1609 else
1610 if (*p == '\\' || *p == '"')
1611 ++result;
1612 }
1613
1614 if (buf != NULL)
1615 {
1616 *buf++ = '"';
1617 for (p = eap->arg; *p; ++p)
1618 {
1619 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1620 // DBCS can contain \ in a trail byte, copy the
1621 // double-byte character to avoid escaping.
1622 *buf++ = *p++;
1623 else
1624 if (*p == '\\' || *p == '"')
1625 *buf++ = '\\';
1626 *buf++ = *p;
1627 }
1628 *buf = '"';
1629 }
1630
1631 break;
1632 case 2: // Quote and split (<f-args>)
1633 // This is hard, so only do it once, and cache the result
1634 if (*split_buf == NULL)
1635 *split_buf = uc_split_args(eap->arg, split_len);
1636
1637 result = *split_len;
1638 if (buf != NULL && result != 0)
1639 STRCPY(buf, *split_buf);
1640
1641 break;
1642 }
1643 break;
1644
1645 case ct_BANG:
1646 result = eap->forceit ? 1 : 0;
1647 if (quote)
1648 result += 2;
1649 if (buf != NULL)
1650 {
1651 if (quote)
1652 *buf++ = '"';
1653 if (eap->forceit)
1654 *buf++ = '!';
1655 if (quote)
1656 *buf = '"';
1657 }
1658 break;
1659
1660 case ct_LINE1:
1661 case ct_LINE2:
1662 case ct_RANGE:
1663 case ct_COUNT:
1664 {
1665 char num_buf[20];
1666 long num = (type == ct_LINE1) ? eap->line1 :
1667 (type == ct_LINE2) ? eap->line2 :
1668 (type == ct_RANGE) ? eap->addr_count :
1669 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
1670 size_t num_len;
1671
1672 sprintf(num_buf, "%ld", num);
1673 num_len = STRLEN(num_buf);
1674 result = num_len;
1675
1676 if (quote)
1677 result += 2;
1678
1679 if (buf != NULL)
1680 {
1681 if (quote)
1682 *buf++ = '"';
1683 STRCPY(buf, num_buf);
1684 buf += num_len;
1685 if (quote)
1686 *buf = '"';
1687 }
1688
1689 break;
1690 }
1691
1692 case ct_MODS:
1693 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001694 result = produce_cmdmods(buf, &cmdmod, quote);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001695 break;
1696 }
1697
1698 case ct_REGISTER:
1699 result = eap->regname ? 1 : 0;
1700 if (quote)
1701 result += 2;
1702 if (buf != NULL)
1703 {
1704 if (quote)
1705 *buf++ = '\'';
1706 if (eap->regname)
1707 *buf++ = eap->regname;
1708 if (quote)
1709 *buf = '\'';
1710 }
1711 break;
1712
1713 case ct_LT:
1714 result = 1;
1715 if (buf != NULL)
1716 *buf = '<';
1717 break;
1718
1719 default:
1720 // Not recognized: just copy the '<' and return -1.
1721 result = (size_t)-1;
1722 if (buf != NULL)
1723 *buf = '<';
1724 break;
1725 }
1726
1727 return result;
1728}
1729
1730/*
1731 * Execute a user defined command.
1732 */
1733 void
1734do_ucmd(exarg_T *eap)
1735{
1736 char_u *buf;
1737 char_u *p;
1738 char_u *q;
1739
1740 char_u *start;
1741 char_u *end = NULL;
1742 char_u *ksp;
1743 size_t len, totlen;
1744
1745 size_t split_len = 0;
1746 char_u *split_buf = NULL;
1747 ucmd_T *cmd;
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001748 sctx_T save_current_sctx;
1749 int restore_current_sctx = FALSE;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001750#ifdef FEAT_EVAL
1751 int restore_script_version = 0;
1752#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001753
1754 if (eap->cmdidx == CMD_USER)
1755 cmd = USER_CMD(eap->useridx);
1756 else
1757 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
1758
1759 /*
1760 * Replace <> in the command by the arguments.
1761 * First round: "buf" is NULL, compute length, allocate "buf".
1762 * Second round: copy result into "buf".
1763 */
1764 buf = NULL;
1765 for (;;)
1766 {
1767 p = cmd->uc_rep; // source
1768 q = buf; // destination
1769 totlen = 0;
1770
1771 for (;;)
1772 {
1773 start = vim_strchr(p, '<');
1774 if (start != NULL)
1775 end = vim_strchr(start + 1, '>');
1776 if (buf != NULL)
1777 {
1778 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
1779 ;
1780 if (*ksp == K_SPECIAL
1781 && (start == NULL || ksp < start || end == NULL)
1782 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
1783# ifdef FEAT_GUI
1784 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
1785# endif
1786 ))
1787 {
1788 // K_SPECIAL has been put in the buffer as K_SPECIAL
1789 // KS_SPECIAL KE_FILLER, like for mappings, but
1790 // do_cmdline() doesn't handle that, so convert it back.
1791 // Also change K_SPECIAL KS_EXTRA KE_CSI into CSI.
1792 len = ksp - p;
1793 if (len > 0)
1794 {
1795 mch_memmove(q, p, len);
1796 q += len;
1797 }
1798 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
1799 p = ksp + 3;
1800 continue;
1801 }
1802 }
1803
1804 // break if no <item> is found
1805 if (start == NULL || end == NULL)
1806 break;
1807
1808 // Include the '>'
1809 ++end;
1810
1811 // Take everything up to the '<'
1812 len = start - p;
1813 if (buf == NULL)
1814 totlen += len;
1815 else
1816 {
1817 mch_memmove(q, p, len);
1818 q += len;
1819 }
1820
1821 len = uc_check_code(start, end - start, q, cmd, eap,
1822 &split_buf, &split_len);
1823 if (len == (size_t)-1)
1824 {
1825 // no match, continue after '<'
1826 p = start + 1;
1827 len = 1;
1828 }
1829 else
1830 p = end;
1831 if (buf == NULL)
1832 totlen += len;
1833 else
1834 q += len;
1835 }
1836 if (buf != NULL) // second time here, finished
1837 {
1838 STRCPY(q, p);
1839 break;
1840 }
1841
1842 totlen += STRLEN(p); // Add on the trailing characters
Bram Moolenaar964b3742019-05-24 18:54:09 +02001843 buf = alloc(totlen + 1);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001844 if (buf == NULL)
1845 {
1846 vim_free(split_buf);
1847 return;
1848 }
1849 }
1850
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001851 if ((cmd->uc_argt & EX_KEEPSCRIPT) == 0)
1852 {
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001853 restore_current_sctx = TRUE;
1854 save_current_sctx = current_sctx;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001855 current_sctx.sc_version = cmd->uc_script_ctx.sc_version;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001856#ifdef FEAT_EVAL
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001857 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001858 if (cmd->uc_flags & UC_VIM9)
1859 {
1860 // In a {} block variables use Vim9 script rules, even in a legacy
1861 // script.
1862 restore_script_version =
1863 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version;
1864 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = SCRIPT_VERSION_VIM9;
1865 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001866#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001867 }
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001868
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001869 (void)do_cmdline(buf, eap->getline, eap->cookie,
1870 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001871
1872 // Careful: Do not use "cmd" here, it may have become invalid if a user
1873 // command was added.
1874 if (restore_current_sctx)
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001875 {
1876#ifdef FEAT_EVAL
1877 if (restore_script_version != 0)
1878 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version =
1879 restore_script_version;
1880#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001881 current_sctx = save_current_sctx;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001882 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001883 vim_free(buf);
1884 vim_free(split_buf);
1885}