blob: 7d844620bdae78033f0cdc4e1f8276d38e6d149b [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
Bram Moolenaarcf2594f2022-11-13 23:30:06 +000034// When non-zero it is not allowed to add or remove user commands
35static int ucmd_locked = 0;
36
Bram Moolenaarac9fb182019-04-27 13:04:13 +020037#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
38#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
39
40/*
41 * List of names for completion for ":command" with the EXPAND_ flag.
42 * Must be alphabetical for completion.
43 */
44static struct
45{
46 int expand;
47 char *name;
48} command_complete[] =
49{
50 {EXPAND_ARGLIST, "arglist"},
51 {EXPAND_AUGROUP, "augroup"},
52 {EXPAND_BEHAVE, "behave"},
53 {EXPAND_BUFFERS, "buffer"},
54 {EXPAND_COLORS, "color"},
55 {EXPAND_COMMANDS, "command"},
56 {EXPAND_COMPILER, "compiler"},
57#if defined(FEAT_CSCOPE)
58 {EXPAND_CSCOPE, "cscope"},
59#endif
Bram Moolenaar0a52df52019-08-18 22:26:31 +020060#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +020061 {EXPAND_USER_DEFINED, "custom"},
62 {EXPAND_USER_LIST, "customlist"},
63#endif
Bram Moolenaarae7dba82019-12-29 13:56:33 +010064 {EXPAND_DIFF_BUFFERS, "diff_buffer"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020065 {EXPAND_DIRECTORIES, "dir"},
66 {EXPAND_ENV_VARS, "environment"},
67 {EXPAND_EVENTS, "event"},
68 {EXPAND_EXPRESSION, "expression"},
69 {EXPAND_FILES, "file"},
70 {EXPAND_FILES_IN_PATH, "file_in_path"},
71 {EXPAND_FILETYPE, "filetype"},
72 {EXPAND_FUNCTIONS, "function"},
73 {EXPAND_HELP, "help"},
74 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020075 {EXPAND_HISTORY, "history"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020076#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
77 {EXPAND_LOCALES, "locale"},
78#endif
79 {EXPAND_MAPCLEAR, "mapclear"},
80 {EXPAND_MAPPINGS, "mapping"},
81 {EXPAND_MENUS, "menu"},
82 {EXPAND_MESSAGES, "messages"},
83 {EXPAND_OWNSYNTAX, "syntax"},
84#if defined(FEAT_PROFILE)
85 {EXPAND_SYNTIME, "syntime"},
86#endif
87 {EXPAND_SETTINGS, "option"},
88 {EXPAND_PACKADD, "packadd"},
roota6759382023-01-21 21:56:06 +000089 {EXPAND_RUNTIME, "runtime"},
Bram Moolenaarac9fb182019-04-27 13:04:13 +020090 {EXPAND_SHELLCMD, "shellcmd"},
91#if defined(FEAT_SIGNS)
92 {EXPAND_SIGN, "sign"},
93#endif
94 {EXPAND_TAGS, "tag"},
95 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
96 {EXPAND_USER, "user"},
97 {EXPAND_USER_VARS, "var"},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +000098#if defined(FEAT_EVAL)
99 {EXPAND_BREAKPOINT, "breakpoint"},
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +0000100 {EXPAND_SCRIPTNAMES, "scriptnames"},
Bram Moolenaar6e2e2cc2022-03-14 19:24:46 +0000101#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200102 {0, NULL}
103};
104
105/*
106 * List of names of address types. Must be alphabetical for completion.
107 */
108static struct
109{
Bram Moolenaarb7316892019-05-01 18:08:42 +0200110 cmd_addr_T expand;
111 char *name;
112 char *shortname;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200113} addr_type_complete[] =
114{
115 {ADDR_ARGUMENTS, "arguments", "arg"},
116 {ADDR_LINES, "lines", "line"},
117 {ADDR_LOADED_BUFFERS, "loaded_buffers", "load"},
118 {ADDR_TABS, "tabs", "tab"},
119 {ADDR_BUFFERS, "buffers", "buf"},
120 {ADDR_WINDOWS, "windows", "win"},
121 {ADDR_QUICKFIX, "quickfix", "qf"},
122 {ADDR_OTHER, "other", "?"},
Bram Moolenaarb7316892019-05-01 18:08:42 +0200123 {ADDR_NONE, NULL, NULL}
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200124};
125
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200126/*
127 * Search for a user command that matches "eap->cmd".
128 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
129 * Return a pointer to just after the command.
130 * Return NULL if there is no matching command.
131 */
132 char_u *
133find_ucmd(
134 exarg_T *eap,
Bram Moolenaar0023f822022-01-16 21:54:19 +0000135 char_u *p, // end of the command (possibly including count)
136 int *full, // set to TRUE for a full match
137 expand_T *xp, // used for completion, NULL otherwise
138 int *complp) // completion flags or NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200139{
140 int len = (int)(p - eap->cmd);
141 int j, k, matchlen = 0;
142 ucmd_T *uc;
143 int found = FALSE;
144 int possible = FALSE;
145 char_u *cp, *np; // Point into typed cmd and test name
146 garray_T *gap;
147 int amb_local = FALSE; // Found ambiguous buffer-local command,
148 // only full match global is accepted.
149
150 /*
151 * Look for buffer-local user commands first, then global ones.
152 */
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000153 gap = &prevwin_curwin()->w_buffer->b_ucmds;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200154 for (;;)
155 {
156 for (j = 0; j < gap->ga_len; ++j)
157 {
158 uc = USER_CMD_GA(gap, j);
159 cp = eap->cmd;
160 np = uc->uc_name;
161 k = 0;
162 while (k < len && *np != NUL && *cp++ == *np++)
163 k++;
164 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
165 {
166 // If finding a second match, the command is ambiguous. But
167 // not if a buffer-local command wasn't a full match and a
168 // global command is a full match.
169 if (k == len && found && *np != NUL)
170 {
171 if (gap == &ucmds)
172 return NULL;
173 amb_local = TRUE;
174 }
175
176 if (!found || (k == len && *np == NUL))
177 {
178 // If we matched up to a digit, then there could
179 // be another command including the digit that we
180 // should use instead.
181 if (k == len)
182 found = TRUE;
183 else
184 possible = TRUE;
185
186 if (gap == &ucmds)
187 eap->cmdidx = CMD_USER;
188 else
189 eap->cmdidx = CMD_USER_BUF;
190 eap->argt = (long)uc->uc_argt;
191 eap->useridx = j;
192 eap->addr_type = uc->uc_addr_type;
193
Bram Moolenaar52111f82019-04-29 21:30:45 +0200194 if (complp != NULL)
195 *complp = uc->uc_compl;
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200196# ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200197 if (xp != NULL)
198 {
199 xp->xp_arg = uc->uc_compl_arg;
200 xp->xp_script_ctx = uc->uc_script_ctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100201 xp->xp_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200202 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200203# endif
204 // Do not search for further abbreviations
205 // if this is an exact match.
206 matchlen = k;
207 if (k == len && *np == NUL)
208 {
209 if (full != NULL)
210 *full = TRUE;
211 amb_local = FALSE;
212 break;
213 }
214 }
215 }
216 }
217
218 // Stop if we found a full match or searched all.
219 if (j < gap->ga_len || gap == &ucmds)
220 break;
221 gap = &ucmds;
222 }
223
224 // Only found ambiguous matches.
225 if (amb_local)
226 {
227 if (xp != NULL)
228 xp->xp_context = EXPAND_UNSUCCESSFUL;
229 return NULL;
230 }
231
232 // The match we found may be followed immediately by a number. Move "p"
233 // back to point to it.
234 if (found || possible)
235 return p + (matchlen - len);
236 return p;
237}
238
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000239/*
240 * Set completion context for :command
241 */
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200242 char_u *
243set_context_in_user_cmd(expand_T *xp, char_u *arg_in)
244{
245 char_u *arg = arg_in;
246 char_u *p;
247
248 // Check for attributes
249 while (*arg == '-')
250 {
251 arg++; // Skip "-"
252 p = skiptowhite(arg);
253 if (*p == NUL)
254 {
255 // Cursor is still in the attribute
256 p = vim_strchr(arg, '=');
257 if (p == NULL)
258 {
259 // No "=", so complete attribute names
260 xp->xp_context = EXPAND_USER_CMD_FLAGS;
261 xp->xp_pattern = arg;
262 return NULL;
263 }
264
265 // For the -complete, -nargs and -addr attributes, we complete
266 // their arguments as well.
267 if (STRNICMP(arg, "complete", p - arg) == 0)
268 {
269 xp->xp_context = EXPAND_USER_COMPLETE;
270 xp->xp_pattern = p + 1;
271 return NULL;
272 }
273 else if (STRNICMP(arg, "nargs", p - arg) == 0)
274 {
275 xp->xp_context = EXPAND_USER_NARGS;
276 xp->xp_pattern = p + 1;
277 return NULL;
278 }
279 else if (STRNICMP(arg, "addr", p - arg) == 0)
280 {
281 xp->xp_context = EXPAND_USER_ADDR_TYPE;
282 xp->xp_pattern = p + 1;
283 return NULL;
284 }
285 return NULL;
286 }
287 arg = skipwhite(p);
288 }
289
290 // After the attributes comes the new command name
291 p = skiptowhite(arg);
292 if (*p == NUL)
293 {
294 xp->xp_context = EXPAND_USER_COMMANDS;
295 xp->xp_pattern = arg;
296 return NULL;
297 }
298
299 // And finally comes a normal command
300 return skipwhite(p);
301}
302
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000303/*
304 * Set the completion context for the argument of a user defined command.
305 */
306 char_u *
307set_context_in_user_cmdarg(
308 char_u *cmd UNUSED,
309 char_u *arg,
310 long argt,
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000311 int context,
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000312 expand_T *xp,
313 int forceit)
314{
315 char_u *p;
316
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000317 if (context == EXPAND_NOTHING)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000318 return NULL;
319
320 if (argt & EX_XFILE)
321 {
322 // EX_XFILE: file names are handled before this call
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000323 xp->xp_context = context;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000324 return NULL;
325 }
326
327#ifdef FEAT_MENU
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000328 if (context == EXPAND_MENUS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000329 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
330#endif
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000331 if (context == EXPAND_COMMANDS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000332 return arg;
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000333 if (context == EXPAND_MAPPINGS)
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000334 return set_context_in_map_cmd(xp, (char_u *)"map", arg, forceit, FALSE,
335 FALSE, CMD_map);
336 // Find start of last argument.
337 p = arg;
338 while (*p)
339 {
340 if (*p == ' ')
341 // argument starts after a space
342 arg = p + 1;
343 else if (*p == '\\' && *(p + 1) != NUL)
344 ++p; // skip over escaped character
345 MB_PTR_ADV(p);
346 }
347 xp->xp_pattern = arg;
Bram Moolenaarb8fb5bb2022-02-18 13:56:38 +0000348 xp->xp_context = context;
Yegappan Lakshmananb31aec32022-02-16 12:44:29 +0000349
350 return NULL;
351}
352
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200353 char_u *
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200354expand_user_command_name(int idx)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200355{
356 return get_user_commands(NULL, idx - (int)CMD_SIZE);
357}
358
359/*
360 * Function given to ExpandGeneric() to obtain the list of user command names.
361 */
362 char_u *
363get_user_commands(expand_T *xp UNUSED, int idx)
364{
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200365 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000366 buf_T *buf = prevwin_curwin()->w_buffer;
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200367
368 if (idx < buf->b_ucmds.ga_len)
369 return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name;
Bram Moolenaarc2842ad2022-07-26 17:23:47 +0100370
Bram Moolenaarf03e3282019-07-22 21:55:18 +0200371 idx -= buf->b_ucmds.ga_len;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200372 if (idx < ucmds.ga_len)
Bram Moolenaarc2842ad2022-07-26 17:23:47 +0100373 {
374 int i;
375 char_u *name = USER_CMD(idx)->uc_name;
376
377 for (i = 0; i < buf->b_ucmds.ga_len; ++i)
378 if (STRCMP(name, USER_CMD_GA(&buf->b_ucmds, i)->uc_name) == 0)
379 // global command is overruled by buffer-local one
380 return (char_u *)"";
381 return name;
382 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200383 return NULL;
384}
385
Dominique Pelle748b3082022-01-08 12:41:16 +0000386#ifdef FEAT_EVAL
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200387/*
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200388 * Get the name of user command "idx". "cmdidx" can be CMD_USER or
389 * CMD_USER_BUF.
390 * Returns NULL if the command is not found.
391 */
392 char_u *
393get_user_command_name(int idx, int cmdidx)
394{
395 if (cmdidx == CMD_USER && idx < ucmds.ga_len)
396 return USER_CMD(idx)->uc_name;
397 if (cmdidx == CMD_USER_BUF)
398 {
399 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000400 buf_T *buf = prevwin_curwin()->w_buffer;
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200401
402 if (idx < buf->b_ucmds.ga_len)
403 return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name;
404 }
405 return NULL;
406}
Dominique Pelle748b3082022-01-08 12:41:16 +0000407#endif
Bram Moolenaar80c88ea2021-09-08 14:29:46 +0200408
409/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200410 * Function given to ExpandGeneric() to obtain the list of user address type
411 * names.
412 */
413 char_u *
414get_user_cmd_addr_type(expand_T *xp UNUSED, int idx)
415{
416 return (char_u *)addr_type_complete[idx].name;
417}
418
419/*
420 * Function given to ExpandGeneric() to obtain the list of user command
421 * attributes.
422 */
423 char_u *
424get_user_cmd_flags(expand_T *xp UNUSED, int idx)
425{
426 static char *user_cmd_flags[] = {
427 "addr", "bang", "bar", "buffer", "complete",
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000428 "count", "nargs", "range", "register", "keepscript"
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200429 };
430
K.Takataeeec2542021-06-02 13:28:16 +0200431 if (idx >= (int)ARRAY_LENGTH(user_cmd_flags))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200432 return NULL;
433 return (char_u *)user_cmd_flags[idx];
434}
435
436/*
437 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
438 */
439 char_u *
440get_user_cmd_nargs(expand_T *xp UNUSED, int idx)
441{
442 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
443
K.Takataeeec2542021-06-02 13:28:16 +0200444 if (idx >= (int)ARRAY_LENGTH(user_cmd_nargs))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200445 return NULL;
446 return (char_u *)user_cmd_nargs[idx];
447}
448
449/*
450 * Function given to ExpandGeneric() to obtain the list of values for
451 * -complete.
452 */
453 char_u *
454get_user_cmd_complete(expand_T *xp UNUSED, int idx)
455{
456 return (char_u *)command_complete[idx].name;
457}
458
Dominique Pelle748b3082022-01-08 12:41:16 +0000459#ifdef FEAT_EVAL
Shougo Matsushita79d599b2022-05-07 12:48:29 +0100460/*
461 * Get the name of completion type "expand" as a string.
462 */
463 char_u *
464cmdcomplete_type_to_str(int expand)
465{
466 int i;
467
468 for (i = 0; command_complete[i].expand != 0; i++)
469 if (command_complete[i].expand == expand)
470 return (char_u *)command_complete[i].name;
471
472 return NULL;
473}
474
475/*
476 * Get the index of completion type "complete_str".
477 * Returns EXPAND_NOTHING if no match found.
478 */
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200479 int
480cmdcomplete_str_to_type(char_u *complete_str)
481{
482 int i;
483
484 for (i = 0; command_complete[i].expand != 0; ++i)
485 if (STRCMP(complete_str, command_complete[i].name) == 0)
486 return command_complete[i].expand;
487
488 return EXPAND_NOTHING;
489}
Dominique Pelle748b3082022-01-08 12:41:16 +0000490#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200491
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200492/*
493 * List user commands starting with "name[name_len]".
494 */
495 static void
496uc_list(char_u *name, size_t name_len)
497{
498 int i, j;
499 int found = FALSE;
500 ucmd_T *cmd;
501 int len;
502 int over;
503 long a;
504 garray_T *gap;
505
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000506 // don't allow for adding or removing user commands here
507 ++ucmd_locked;
508
Bram Moolenaare38eab22019-12-05 21:50:01 +0100509 // In cmdwin, the alternative buffer should be used.
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +0000510 gap = &prevwin_curwin()->w_buffer->b_ucmds;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200511 for (;;)
512 {
513 for (i = 0; i < gap->ga_len; ++i)
514 {
515 cmd = USER_CMD_GA(gap, i);
516 a = (long)cmd->uc_argt;
517
518 // Skip commands which don't match the requested prefix and
519 // commands filtered out.
520 if (STRNCMP(name, cmd->uc_name, name_len) != 0
521 || message_filtered(cmd->uc_name))
522 continue;
523
524 // Put out the title first time
525 if (!found)
526 msg_puts_title(_("\n Name Args Address Complete Definition"));
527 found = TRUE;
528 msg_putchar('\n');
529 if (got_int)
530 break;
531
532 // Special cases
533 len = 4;
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200534 if (a & EX_BANG)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200535 {
536 msg_putchar('!');
537 --len;
538 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200539 if (a & EX_REGSTR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200540 {
541 msg_putchar('"');
542 --len;
543 }
544 if (gap != &ucmds)
545 {
546 msg_putchar('b');
547 --len;
548 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200549 if (a & EX_TRLBAR)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200550 {
551 msg_putchar('|');
552 --len;
553 }
554 while (len-- > 0)
555 msg_putchar(' ');
556
557 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D));
558 len = (int)STRLEN(cmd->uc_name) + 4;
559
560 do {
561 msg_putchar(' ');
562 ++len;
563 } while (len < 22);
564
565 // "over" is how much longer the name is than the column width for
566 // the name, we'll try to align what comes after.
567 over = len - 22;
568 len = 0;
569
570 // Arguments
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200571 switch ((int)(a & (EX_EXTRA|EX_NOSPC|EX_NEEDARG)))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200572 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200573 case 0: IObuff[len++] = '0'; break;
574 case (EX_EXTRA): IObuff[len++] = '*'; break;
575 case (EX_EXTRA|EX_NOSPC): IObuff[len++] = '?'; break;
576 case (EX_EXTRA|EX_NEEDARG): IObuff[len++] = '+'; break;
577 case (EX_EXTRA|EX_NOSPC|EX_NEEDARG): IObuff[len++] = '1'; break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200578 }
579
580 do {
581 IObuff[len++] = ' ';
582 } while (len < 5 - over);
583
584 // Address / Range
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200585 if (a & (EX_RANGE|EX_COUNT))
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200586 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200587 if (a & EX_COUNT)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200588 {
589 // -count=N
590 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
591 len += (int)STRLEN(IObuff + len);
592 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200593 else if (a & EX_DFLALL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200594 IObuff[len++] = '%';
595 else if (cmd->uc_def >= 0)
596 {
597 // -range=N
598 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
599 len += (int)STRLEN(IObuff + len);
600 }
601 else
602 IObuff[len++] = '.';
603 }
604
605 do {
606 IObuff[len++] = ' ';
607 } while (len < 8 - over);
608
609 // Address Type
Bram Moolenaarb7316892019-05-01 18:08:42 +0200610 for (j = 0; addr_type_complete[j].expand != ADDR_NONE; ++j)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200611 if (addr_type_complete[j].expand != ADDR_LINES
612 && addr_type_complete[j].expand == cmd->uc_addr_type)
613 {
614 STRCPY(IObuff + len, addr_type_complete[j].shortname);
615 len += (int)STRLEN(IObuff + len);
616 break;
617 }
618
619 do {
620 IObuff[len++] = ' ';
621 } while (len < 13 - over);
622
623 // Completion
624 for (j = 0; command_complete[j].expand != 0; ++j)
625 if (command_complete[j].expand == cmd->uc_compl)
626 {
627 STRCPY(IObuff + len, command_complete[j].name);
628 len += (int)STRLEN(IObuff + len);
Bram Moolenaar78f60322022-01-17 22:16:33 +0000629#ifdef FEAT_EVAL
Bram Moolenaar3f3597b2022-01-17 19:06:56 +0000630 if (p_verbose > 0 && cmd->uc_compl_arg != NULL
631 && STRLEN(cmd->uc_compl_arg) < 200)
632 {
633 IObuff[len] = ',';
634 STRCPY(IObuff + len + 1, cmd->uc_compl_arg);
635 len += (int)STRLEN(IObuff + len);
636 }
Bram Moolenaar78f60322022-01-17 22:16:33 +0000637#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200638 break;
639 }
640
641 do {
642 IObuff[len++] = ' ';
643 } while (len < 25 - over);
644
645 IObuff[len] = '\0';
646 msg_outtrans(IObuff);
647
648 msg_outtrans_special(cmd->uc_rep, FALSE,
649 name_len == 0 ? Columns - 47 : 0);
650#ifdef FEAT_EVAL
651 if (p_verbose > 0)
652 last_set_msg(cmd->uc_script_ctx);
653#endif
654 out_flush();
655 ui_breakcheck();
656 if (got_int)
657 break;
658 }
659 if (gap == &ucmds || i < gap->ga_len)
660 break;
661 gap = &ucmds;
662 }
663
664 if (!found)
665 msg(_("No user-defined commands found"));
Bram Moolenaarcf2594f2022-11-13 23:30:06 +0000666
667 --ucmd_locked;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200668}
669
670 char *
671uc_fun_cmd(void)
672{
673 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
674 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
675 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
676 0xb9, 0x7f, 0};
677 int i;
678
679 for (i = 0; fcmd[i]; ++i)
680 IObuff[i] = fcmd[i] - 0x40;
681 IObuff[i] = 0;
682 return (char *)IObuff;
683}
684
685/*
686 * Parse address type argument
687 */
688 static int
689parse_addr_type_arg(
690 char_u *value,
691 int vallen,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200692 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200693{
694 int i, a, b;
695
Bram Moolenaarb7316892019-05-01 18:08:42 +0200696 for (i = 0; addr_type_complete[i].expand != ADDR_NONE; ++i)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200697 {
698 a = (int)STRLEN(addr_type_complete[i].name) == vallen;
699 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0;
700 if (a && b)
701 {
702 *addr_type_arg = addr_type_complete[i].expand;
703 break;
704 }
705 }
706
Bram Moolenaarb7316892019-05-01 18:08:42 +0200707 if (addr_type_complete[i].expand == ADDR_NONE)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200708 {
709 char_u *err = value;
710
711 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++)
712 ;
713 err[i] = NUL;
Bram Moolenaar11de43d2022-01-06 21:41:11 +0000714 semsg(_(e_invalid_address_type_value_str), err);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200715 return FAIL;
716 }
717
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200718 return OK;
719}
720
721/*
722 * Parse a completion argument "value[vallen]".
723 * The detected completion goes in "*complp", argument type in "*argt".
724 * When there is an argument, for function and user defined completion, it's
725 * copied to allocated memory and stored in "*compl_arg".
726 * Returns FAIL if something is wrong.
727 */
728 int
729parse_compl_arg(
730 char_u *value,
731 int vallen,
732 int *complp,
733 long *argt,
734 char_u **compl_arg UNUSED)
735{
736 char_u *arg = NULL;
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200737# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200738 size_t arglen = 0;
739# endif
740 int i;
741 int valend = vallen;
742
743 // Look for any argument part - which is the part after any ','
744 for (i = 0; i < vallen; ++i)
745 {
746 if (value[i] == ',')
747 {
748 arg = &value[i + 1];
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200749# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200750 arglen = vallen - i - 1;
751# endif
752 valend = i;
753 break;
754 }
755 }
756
757 for (i = 0; command_complete[i].expand != 0; ++i)
758 {
759 if ((int)STRLEN(command_complete[i].name) == valend
760 && STRNCMP(value, command_complete[i].name, valend) == 0)
761 {
762 *complp = command_complete[i].expand;
763 if (command_complete[i].expand == EXPAND_BUFFERS)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200764 *argt |= EX_BUFNAME;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200765 else if (command_complete[i].expand == EXPAND_DIRECTORIES
766 || command_complete[i].expand == EXPAND_FILES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200767 *argt |= EX_XFILE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200768 break;
769 }
770 }
771
772 if (command_complete[i].expand == 0)
773 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000774 semsg(_(e_invalid_complete_value_str), value);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200775 return FAIL;
776 }
777
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200778# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200779 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
780 && arg != NULL)
781# else
782 if (arg != NULL)
783# endif
784 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000785 emsg(_(e_completion_argument_only_allowed_for_custom_completion));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200786 return FAIL;
787 }
788
Bram Moolenaar0a52df52019-08-18 22:26:31 +0200789# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200790 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
791 && arg == NULL)
792 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000793 emsg(_(e_custom_completion_requires_function_argument));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200794 return FAIL;
795 }
796
797 if (arg != NULL)
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200798 *compl_arg = vim_strnsave(arg, arglen);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200799# endif
800 return OK;
801}
802
803/*
804 * Scan attributes in the ":command" command.
805 * Return FAIL when something is wrong.
806 */
807 static int
808uc_scan_attr(
809 char_u *attr,
810 size_t len,
811 long *argt,
812 long *def,
813 int *flags,
Bram Moolenaar52111f82019-04-29 21:30:45 +0200814 int *complp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200815 char_u **compl_arg,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200816 cmd_addr_T *addr_type_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200817{
818 char_u *p;
819
820 if (len == 0)
821 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000822 emsg(_(e_no_attribute_specified));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200823 return FAIL;
824 }
825
826 // First, try the simple attributes (no arguments)
827 if (STRNICMP(attr, "bang", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200828 *argt |= EX_BANG;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200829 else if (STRNICMP(attr, "buffer", len) == 0)
830 *flags |= UC_BUFFER;
831 else if (STRNICMP(attr, "register", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200832 *argt |= EX_REGSTR;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +0000833 else if (STRNICMP(attr, "keepscript", len) == 0)
834 *argt |= EX_KEEPSCRIPT;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200835 else if (STRNICMP(attr, "bar", len) == 0)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200836 *argt |= EX_TRLBAR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200837 else
838 {
839 int i;
840 char_u *val = NULL;
841 size_t vallen = 0;
842 size_t attrlen = len;
843
844 // Look for the attribute name - which is the part before any '='
845 for (i = 0; i < (int)len; ++i)
846 {
847 if (attr[i] == '=')
848 {
849 val = &attr[i + 1];
850 vallen = len - i - 1;
851 attrlen = i;
852 break;
853 }
854 }
855
856 if (STRNICMP(attr, "nargs", attrlen) == 0)
857 {
858 if (vallen == 1)
859 {
860 if (*val == '0')
861 // Do nothing - this is the default
862 ;
863 else if (*val == '1')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200864 *argt |= (EX_EXTRA | EX_NOSPC | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200865 else if (*val == '*')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200866 *argt |= EX_EXTRA;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200867 else if (*val == '?')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200868 *argt |= (EX_EXTRA | EX_NOSPC);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200869 else if (*val == '+')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200870 *argt |= (EX_EXTRA | EX_NEEDARG);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200871 else
872 goto wrong_nargs;
873 }
874 else
875 {
876wrong_nargs:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000877 emsg(_(e_invalid_number_of_arguments));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200878 return FAIL;
879 }
880 }
881 else if (STRNICMP(attr, "range", attrlen) == 0)
882 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200883 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200884 if (vallen == 1 && *val == '%')
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200885 *argt |= EX_DFLALL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200886 else if (val != NULL)
887 {
888 p = val;
889 if (*def >= 0)
890 {
891two_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000892 emsg(_(e_count_cannot_be_specified_twice));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200893 return FAIL;
894 }
895
896 *def = getdigits(&p);
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200897 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200898
899 if (p != val + vallen || vallen == 0)
900 {
901invalid_count:
Bram Moolenaar1a992222021-12-31 17:25:48 +0000902 emsg(_(e_invalid_default_value_for_count));
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200903 return FAIL;
904 }
905 }
Bram Moolenaarb7316892019-05-01 18:08:42 +0200906 // default for -range is using buffer lines
907 if (*addr_type_arg == ADDR_NONE)
908 *addr_type_arg = ADDR_LINES;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200909 }
910 else if (STRNICMP(attr, "count", attrlen) == 0)
911 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200912 *argt |= (EX_COUNT | EX_ZEROR | EX_RANGE);
Bram Moolenaarb7316892019-05-01 18:08:42 +0200913 // default for -count is using any number
914 if (*addr_type_arg == ADDR_NONE)
915 *addr_type_arg = ADDR_OTHER;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200916
917 if (val != NULL)
918 {
919 p = val;
920 if (*def >= 0)
921 goto two_count;
922
923 *def = getdigits(&p);
924
925 if (p != val + vallen)
926 goto invalid_count;
927 }
928
929 if (*def < 0)
930 *def = 0;
931 }
932 else if (STRNICMP(attr, "complete", attrlen) == 0)
933 {
934 if (val == NULL)
935 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000936 semsg(_(e_argument_required_for_str), "-complete");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200937 return FAIL;
938 }
939
Bram Moolenaar52111f82019-04-29 21:30:45 +0200940 if (parse_compl_arg(val, (int)vallen, complp, argt, compl_arg)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200941 == FAIL)
942 return FAIL;
943 }
944 else if (STRNICMP(attr, "addr", attrlen) == 0)
945 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200946 *argt |= EX_RANGE;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200947 if (val == NULL)
948 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000949 semsg(_(e_argument_required_for_str), "-addr");
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200950 return FAIL;
951 }
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200952 if (parse_addr_type_arg(val, (int)vallen, addr_type_arg) == FAIL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200953 return FAIL;
Bram Moolenaare4f5f3a2019-05-04 14:05:08 +0200954 if (*addr_type_arg != ADDR_LINES)
Bram Moolenaar8071cb22019-07-12 17:58:01 +0200955 *argt |= EX_ZEROR;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200956 }
957 else
958 {
959 char_u ch = attr[len];
960 attr[len] = '\0';
Bram Moolenaar1a992222021-12-31 17:25:48 +0000961 semsg(_(e_invalid_attribute_str), attr);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200962 attr[len] = ch;
963 return FAIL;
964 }
965 }
966
967 return OK;
968}
969
970/*
971 * Add a user command to the list or replace an existing one.
972 */
973 static int
974uc_add_command(
975 char_u *name,
976 size_t name_len,
977 char_u *rep,
978 long argt,
979 long def,
980 int flags,
981 int compl,
982 char_u *compl_arg UNUSED,
Bram Moolenaarb7316892019-05-01 18:08:42 +0200983 cmd_addr_T addr_type,
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200984 int force)
985{
986 ucmd_T *cmd = NULL;
987 char_u *p;
988 int i;
989 int cmp = 1;
990 char_u *rep_buf = NULL;
991 garray_T *gap;
992
Bram Moolenaar459fd782019-10-13 16:43:39 +0200993 replace_termcodes(rep, &rep_buf, 0, NULL);
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200994 if (rep_buf == NULL)
995 {
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200996 // can't replace termcodes - try using the string as is
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200997 rep_buf = vim_strsave(rep);
998
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +0200999 // give up if out of memory
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001000 if (rep_buf == NULL)
1001 return FAIL;
1002 }
1003
1004 // get address of growarray: global or in curbuf
1005 if (flags & UC_BUFFER)
1006 {
1007 gap = &curbuf->b_ucmds;
1008 if (gap->ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001009 ga_init2(gap, sizeof(ucmd_T), 4);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001010 }
1011 else
1012 gap = &ucmds;
1013
1014 // Search for the command in the already defined commands.
1015 for (i = 0; i < gap->ga_len; ++i)
1016 {
1017 size_t len;
1018
1019 cmd = USER_CMD_GA(gap, i);
1020 len = STRLEN(cmd->uc_name);
1021 cmp = STRNCMP(name, cmd->uc_name, name_len);
1022 if (cmp == 0)
1023 {
1024 if (name_len < len)
1025 cmp = -1;
1026 else if (name_len > len)
1027 cmp = 1;
1028 }
1029
1030 if (cmp == 0)
1031 {
1032 // Command can be replaced with "command!" and when sourcing the
1033 // same script again, but only once.
1034 if (!force
1035#ifdef FEAT_EVAL
1036 && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid
1037 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq)
1038#endif
1039 )
1040 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001041 semsg(_(e_command_already_exists_add_bang_to_replace_it_str),
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001042 name);
1043 goto fail;
1044 }
1045
1046 VIM_CLEAR(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001047#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001048 VIM_CLEAR(cmd->uc_compl_arg);
1049#endif
1050 break;
1051 }
1052
1053 // Stop as soon as we pass the name to add
1054 if (cmp < 0)
1055 break;
1056 }
1057
1058 // Extend the array unless we're replacing an existing command
1059 if (cmp != 0)
1060 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00001061 if (ga_grow(gap, 1) == FAIL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001062 goto fail;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001063 if ((p = vim_strnsave(name, name_len)) == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001064 goto fail;
1065
1066 cmd = USER_CMD_GA(gap, i);
1067 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
1068
1069 ++gap->ga_len;
1070
1071 cmd->uc_name = p;
1072 }
1073
1074 cmd->uc_rep = rep_buf;
1075 cmd->uc_argt = argt;
1076 cmd->uc_def = def;
1077 cmd->uc_compl = compl;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001078 cmd->uc_script_ctx = current_sctx;
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001079 if (flags & UC_VIM9)
1080 cmd->uc_script_ctx.sc_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001081 cmd->uc_flags = flags & UC_VIM9;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001082#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001083 cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001084 cmd->uc_compl_arg = compl_arg;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001085#endif
1086 cmd->uc_addr_type = addr_type;
1087
1088 return OK;
1089
1090fail:
1091 vim_free(rep_buf);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001092#if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001093 vim_free(compl_arg);
1094#endif
1095 return FAIL;
1096}
1097
1098/*
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001099 * If "p" starts with "{" then read a block of commands until "}".
1100 * Used for ":command" and ":autocmd".
1101 */
1102 char_u *
1103may_get_cmd_block(exarg_T *eap, char_u *p, char_u **tofree, int *flags)
1104{
1105 char_u *retp = p;
1106
1107 if (*p == '{' && ends_excmd2(eap->arg, skipwhite(p + 1))
1108 && eap->getline != NULL)
1109 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001110 garray_T ga;
1111 char_u *line = NULL;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001112
1113 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001114 if (ga_copy_string(&ga, p) == FAIL)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001115 return retp;
1116
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001117 // If the argument ends in "}" it must have been concatenated already
1118 // for ISN_EXEC.
1119 if (p[STRLEN(p) - 1] != '}')
1120 // Read lines between '{' and '}'. Does not support nesting or
1121 // here-doc constructs.
1122 for (;;)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001123 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001124 vim_free(line);
1125 if ((line = eap->getline(':', eap->cookie,
1126 0, GETLINE_CONCAT_CONTBAR)) == NULL)
1127 {
1128 emsg(_(e_missing_rcurly));
1129 break;
1130 }
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001131 if (ga_copy_string(&ga, line) == FAIL)
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001132 break;
1133 if (*skipwhite(line) == '}')
1134 break;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001135 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001136 vim_free(line);
1137 retp = *tofree = ga_concat_strings(&ga, "\n");
1138 ga_clear_strings(&ga);
1139 *flags |= UC_VIM9;
1140 }
1141 return retp;
1142}
1143
1144/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001145 * ":command ..." implementation
1146 */
1147 void
1148ex_command(exarg_T *eap)
1149{
Bram Moolenaarb7316892019-05-01 18:08:42 +02001150 char_u *name;
1151 char_u *end;
1152 char_u *p;
1153 long argt = 0;
1154 long def = -1;
1155 int flags = 0;
1156 int compl = EXPAND_NOTHING;
1157 char_u *compl_arg = NULL;
1158 cmd_addr_T addr_type_arg = ADDR_NONE;
1159 int has_attr = (eap->arg[0] == '-');
1160 int name_len;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001161
1162 p = eap->arg;
1163
1164 // Check for attributes
1165 while (*p == '-')
1166 {
1167 ++p;
1168 end = skiptowhite(p);
1169 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl,
1170 &compl_arg, &addr_type_arg) == FAIL)
zeertzjq33e54302022-12-19 16:49:27 +00001171 goto theend;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001172 p = skipwhite(end);
1173 }
1174
1175 // Get the name (if any) and skip to the following argument
1176 name = p;
1177 if (ASCII_ISALPHA(*p))
1178 while (ASCII_ISALNUM(*p))
1179 ++p;
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001180 if (!ends_excmd2(eap->arg, p) && !VIM_ISWHITE(*p))
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001181 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001182 emsg(_(e_invalid_command_name));
zeertzjq33e54302022-12-19 16:49:27 +00001183 goto theend;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001184 }
1185 end = p;
1186 name_len = (int)(end - name);
1187
1188 // If there is nothing after the name, and no attributes were specified,
1189 // we are listing commands
1190 p = skipwhite(end);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02001191 if (!has_attr && ends_excmd2(eap->arg, p))
zeertzjq33e54302022-12-19 16:49:27 +00001192 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001193 uc_list(name, end - name);
zeertzjq33e54302022-12-19 16:49:27 +00001194 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001195 else if (!ASCII_ISUPPER(*name))
zeertzjq33e54302022-12-19 16:49:27 +00001196 {
Bram Moolenaar1a992222021-12-31 17:25:48 +00001197 emsg(_(e_user_defined_commands_must_start_with_an_uppercase_letter));
zeertzjq33e54302022-12-19 16:49:27 +00001198 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001199 else if ((name_len == 1 && *name == 'X')
1200 || (name_len <= 4
1201 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
zeertzjq33e54302022-12-19 16:49:27 +00001202 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00001203 emsg(_(e_reserved_name_cannot_be_used_for_user_defined_command));
zeertzjq33e54302022-12-19 16:49:27 +00001204 }
Martin Tournoijde69a732021-07-11 14:28:25 +02001205 else if (compl > 0 && (argt & EX_EXTRA) == 0)
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001206 {
1207 // Some plugins rely on silently ignoring the mistake, only make this
1208 // an error in Vim9 script.
1209 if (in_vim9script())
Bram Moolenaar41a34852021-08-04 16:09:24 +02001210 emsg(_(e_complete_used_without_allowing_arguments));
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001211 else
1212 give_warning_with_source(
Bram Moolenaar41a34852021-08-04 16:09:24 +02001213 (char_u *)_(e_complete_used_without_allowing_arguments),
1214 TRUE, TRUE);
Bram Moolenaarcc7eb2a2021-07-11 19:12:04 +02001215 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001216 else
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001217 {
1218 char_u *tofree = NULL;
1219
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001220 p = may_get_cmd_block(eap, p, &tofree, &flags);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001221
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001222 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
1223 addr_type_arg, eap->forceit);
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001224 vim_free(tofree);
zeertzjq33e54302022-12-19 16:49:27 +00001225
1226 return; // success
Bram Moolenaar5d7c2df2021-07-27 21:17:32 +02001227 }
zeertzjq33e54302022-12-19 16:49:27 +00001228
1229theend:
1230 vim_free(compl_arg);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001231}
1232
1233/*
1234 * ":comclear" implementation
1235 * Clear all user commands, global and for current buffer.
1236 */
1237 void
1238ex_comclear(exarg_T *eap UNUSED)
1239{
1240 uc_clear(&ucmds);
Bram Moolenaare5c83282019-05-03 23:15:37 +02001241 if (curbuf != NULL)
1242 uc_clear(&curbuf->b_ucmds);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001243}
1244
1245/*
Bram Moolenaarcf2594f2022-11-13 23:30:06 +00001246 * If ucmd_locked is set give an error and return TRUE.
1247 * Otherwise return FALSE.
1248 */
1249 static int
1250is_ucmd_locked(void)
1251{
1252 if (ucmd_locked > 0)
1253 {
1254 emsg(_(e_cannot_change_user_commands_while_listing));
1255 return TRUE;
1256 }
1257 return FALSE;
1258}
1259
1260/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001261 * Clear all user commands for "gap".
1262 */
1263 void
1264uc_clear(garray_T *gap)
1265{
1266 int i;
1267 ucmd_T *cmd;
1268
Bram Moolenaarcf2594f2022-11-13 23:30:06 +00001269 if (is_ucmd_locked())
1270 return;
1271
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001272 for (i = 0; i < gap->ga_len; ++i)
1273 {
1274 cmd = USER_CMD_GA(gap, i);
1275 vim_free(cmd->uc_name);
1276 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001277# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001278 vim_free(cmd->uc_compl_arg);
1279# endif
1280 }
1281 ga_clear(gap);
1282}
1283
1284/*
1285 * ":delcommand" implementation
1286 */
1287 void
1288ex_delcommand(exarg_T *eap)
1289{
1290 int i = 0;
1291 ucmd_T *cmd = NULL;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001292 int res = -1;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001293 garray_T *gap;
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001294 char_u *arg = eap->arg;
1295 int buffer_only = FALSE;
1296
1297 if (STRNCMP(arg, "-buffer", 7) == 0 && VIM_ISWHITE(arg[7]))
1298 {
1299 buffer_only = TRUE;
1300 arg = skipwhite(arg + 7);
1301 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001302
1303 gap = &curbuf->b_ucmds;
1304 for (;;)
1305 {
1306 for (i = 0; i < gap->ga_len; ++i)
1307 {
1308 cmd = USER_CMD_GA(gap, i);
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001309 res = STRCMP(arg, cmd->uc_name);
1310 if (res <= 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001311 break;
1312 }
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001313 if (gap == &ucmds || res == 0 || buffer_only)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001314 break;
1315 gap = &ucmds;
1316 }
1317
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001318 if (res != 0)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001319 {
Bram Moolenaarbdcba242021-09-12 20:58:02 +02001320 semsg(_(buffer_only
1321 ? e_no_such_user_defined_command_in_current_buffer_str
1322 : e_no_such_user_defined_command_str), arg);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001323 return;
1324 }
1325
Bram Moolenaarcf2594f2022-11-13 23:30:06 +00001326 if (is_ucmd_locked())
1327 return;
1328
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001329 vim_free(cmd->uc_name);
1330 vim_free(cmd->uc_rep);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02001331# if defined(FEAT_EVAL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001332 vim_free(cmd->uc_compl_arg);
1333# endif
1334
1335 --gap->ga_len;
1336
1337 if (i < gap->ga_len)
1338 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
1339}
1340
1341/*
1342 * Split and quote args for <f-args>.
1343 */
1344 static char_u *
1345uc_split_args(char_u *arg, size_t *lenp)
1346{
1347 char_u *buf;
1348 char_u *p;
1349 char_u *q;
1350 int len;
1351
1352 // Precalculate length
1353 p = arg;
1354 len = 2; // Initial and final quotes
1355
1356 while (*p)
1357 {
1358 if (p[0] == '\\' && p[1] == '\\')
1359 {
1360 len += 2;
1361 p += 2;
1362 }
1363 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1364 {
1365 len += 1;
1366 p += 2;
1367 }
1368 else if (*p == '\\' || *p == '"')
1369 {
1370 len += 2;
1371 p += 1;
1372 }
1373 else if (VIM_ISWHITE(*p))
1374 {
1375 p = skipwhite(p);
1376 if (*p == NUL)
1377 break;
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001378 len += 4; // ", "
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001379 }
1380 else
1381 {
1382 int charlen = (*mb_ptr2len)(p);
1383
1384 len += charlen;
1385 p += charlen;
1386 }
1387 }
1388
1389 buf = alloc(len + 1);
1390 if (buf == NULL)
1391 {
1392 *lenp = 0;
1393 return buf;
1394 }
1395
1396 p = arg;
1397 q = buf;
1398 *q++ = '"';
1399 while (*p)
1400 {
1401 if (p[0] == '\\' && p[1] == '\\')
1402 {
1403 *q++ = '\\';
1404 *q++ = '\\';
1405 p += 2;
1406 }
1407 else if (p[0] == '\\' && VIM_ISWHITE(p[1]))
1408 {
1409 *q++ = p[1];
1410 p += 2;
1411 }
1412 else if (*p == '\\' || *p == '"')
1413 {
1414 *q++ = '\\';
1415 *q++ = *p++;
1416 }
1417 else if (VIM_ISWHITE(*p))
1418 {
1419 p = skipwhite(p);
1420 if (*p == NUL)
1421 break;
1422 *q++ = '"';
1423 *q++ = ',';
Bram Moolenaar20d89e02020-10-20 23:11:33 +02001424 *q++ = ' ';
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001425 *q++ = '"';
1426 }
1427 else
1428 {
1429 MB_COPY_CHAR(p, q);
1430 }
1431 }
1432 *q++ = '"';
1433 *q = 0;
1434
1435 *lenp = len;
1436 return buf;
1437}
1438
1439 static size_t
1440add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods)
1441{
1442 size_t result;
1443
1444 result = STRLEN(mod_str);
1445 if (*multi_mods)
1446 result += 1;
1447 if (buf != NULL)
1448 {
1449 if (*multi_mods)
1450 STRCAT(buf, " ");
1451 STRCAT(buf, mod_str);
1452 }
1453
1454 *multi_mods = 1;
1455
1456 return result;
1457}
1458
1459/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001460 * Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one
Bram Moolenaare1004402020-10-24 20:49:43 +02001461 * was added. Return the number of bytes added.
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001462 */
1463 size_t
dundargocc57b5bc2022-11-02 13:30:51 +00001464add_win_cmd_modifiers(char_u *buf, cmdmod_T *cmod, int *multi_mods)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001465{
1466 size_t result = 0;
1467
1468 // :aboveleft and :leftabove
Bram Moolenaar02194d22020-10-24 23:08:38 +02001469 if (cmod->cmod_split & WSP_ABOVE)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001470 result += add_cmd_modifier(buf, "aboveleft", multi_mods);
1471 // :belowright and :rightbelow
Bram Moolenaar02194d22020-10-24 23:08:38 +02001472 if (cmod->cmod_split & WSP_BELOW)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001473 result += add_cmd_modifier(buf, "belowright", multi_mods);
1474 // :botright
Bram Moolenaar02194d22020-10-24 23:08:38 +02001475 if (cmod->cmod_split & WSP_BOT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001476 result += add_cmd_modifier(buf, "botright", multi_mods);
1477
1478 // :tab
Bram Moolenaar02194d22020-10-24 23:08:38 +02001479 if (cmod->cmod_tab > 0)
zeertzjq208567e2022-10-18 13:11:21 +01001480 {
1481 int tabnr = cmod->cmod_tab - 1;
1482
1483 if (tabnr == tabpage_index(curtab))
1484 {
1485 // For compatibility, don't add a tabpage number if it is the same
1486 // as the default number for :tab.
1487 result += add_cmd_modifier(buf, "tab", multi_mods);
1488 }
1489 else
1490 {
1491 char tab_buf[NUMBUFLEN + 3];
1492
1493 sprintf(tab_buf, "%dtab", tabnr);
1494 result += add_cmd_modifier(buf, tab_buf, multi_mods);
1495 }
1496 }
1497
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001498 // :topleft
Bram Moolenaar02194d22020-10-24 23:08:38 +02001499 if (cmod->cmod_split & WSP_TOP)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001500 result += add_cmd_modifier(buf, "topleft", multi_mods);
1501 // :vertical
Bram Moolenaar02194d22020-10-24 23:08:38 +02001502 if (cmod->cmod_split & WSP_VERT)
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001503 result += add_cmd_modifier(buf, "vertical", multi_mods);
zeertzjqd3de1782022-09-01 12:58:52 +01001504 // :horizontal
1505 if (cmod->cmod_split & WSP_HOR)
1506 result += add_cmd_modifier(buf, "horizontal", multi_mods);
Bram Moolenaar7a1637f2020-04-13 21:16:21 +02001507 return result;
1508}
1509
1510/*
Bram Moolenaar02194d22020-10-24 23:08:38 +02001511 * Generate text for the "cmod" command modifiers.
1512 * If "buf" is NULL just return the length.
1513 */
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001514 size_t
Bram Moolenaar02194d22020-10-24 23:08:38 +02001515produce_cmdmods(char_u *buf, cmdmod_T *cmod, int quote)
1516{
Bram Moolenaara360dbe2020-10-26 18:46:53 +01001517 size_t result = 0;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001518 int multi_mods = 0;
1519 int i;
1520 typedef struct {
1521 int flag;
1522 char *name;
1523 } mod_entry_T;
1524 static mod_entry_T mod_entries[] = {
1525#ifdef FEAT_BROWSE_CMD
1526 {CMOD_BROWSE, "browse"},
1527#endif
1528#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1529 {CMOD_CONFIRM, "confirm"},
1530#endif
1531 {CMOD_HIDE, "hide"},
1532 {CMOD_KEEPALT, "keepalt"},
1533 {CMOD_KEEPJUMPS, "keepjumps"},
1534 {CMOD_KEEPMARKS, "keepmarks"},
1535 {CMOD_KEEPPATTERNS, "keeppatterns"},
1536 {CMOD_LOCKMARKS, "lockmarks"},
1537 {CMOD_NOSWAPFILE, "noswapfile"},
1538 {CMOD_UNSILENT, "unsilent"},
1539 {CMOD_NOAUTOCMD, "noautocmd"},
1540#ifdef HAVE_SANDBOX
1541 {CMOD_SANDBOX, "sandbox"},
1542#endif
Bram Moolenaarb579f6e2021-12-04 11:57:00 +00001543 {CMOD_LEGACY, "legacy"},
Bram Moolenaar02194d22020-10-24 23:08:38 +02001544 {0, NULL}
1545 };
1546
1547 result = quote ? 2 : 0;
1548 if (buf != NULL)
1549 {
1550 if (quote)
1551 *buf++ = '"';
1552 *buf = '\0';
1553 }
1554
1555 // the modifiers that are simple flags
1556 for (i = 0; mod_entries[i].name != NULL; ++i)
1557 if (cmod->cmod_flags & mod_entries[i].flag)
1558 result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods);
1559
1560 // :silent
1561 if (cmod->cmod_flags & CMOD_SILENT)
1562 result += add_cmd_modifier(buf,
1563 (cmod->cmod_flags & CMOD_ERRSILENT) ? "silent!"
1564 : "silent", &multi_mods);
1565 // :verbose
zeertzjq9359e8a2022-07-03 13:16:09 +01001566 if (cmod->cmod_verbose > 0)
1567 {
1568 int verbose_value = cmod->cmod_verbose - 1;
1569
1570 if (verbose_value == 1)
1571 result += add_cmd_modifier(buf, "verbose", &multi_mods);
1572 else
1573 {
1574 char verbose_buf[NUMBUFLEN];
1575
1576 sprintf(verbose_buf, "%dverbose", verbose_value);
1577 result += add_cmd_modifier(buf, verbose_buf, &multi_mods);
1578 }
1579 }
Bram Moolenaar02194d22020-10-24 23:08:38 +02001580 // flags from cmod->cmod_split
dundargocc57b5bc2022-11-02 13:30:51 +00001581 result += add_win_cmd_modifiers(buf, cmod, &multi_mods);
zeertzjq9359e8a2022-07-03 13:16:09 +01001582
Bram Moolenaar02194d22020-10-24 23:08:38 +02001583 if (quote && buf != NULL)
1584 {
1585 buf += result - 2;
1586 *buf = '"';
1587 }
1588 return result;
1589}
1590
1591/*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001592 * Check for a <> code in a user command.
1593 * "code" points to the '<'. "len" the length of the <> (inclusive).
1594 * "buf" is where the result is to be added.
1595 * "split_buf" points to a buffer used for splitting, caller should free it.
1596 * "split_len" is the length of what "split_buf" contains.
1597 * Returns the length of the replacement, which has been added to "buf".
1598 * Returns -1 if there was no match, and only the "<" has been copied.
1599 */
1600 static size_t
1601uc_check_code(
1602 char_u *code,
1603 size_t len,
1604 char_u *buf,
1605 ucmd_T *cmd, // the user command we're expanding
1606 exarg_T *eap, // ex arguments
1607 char_u **split_buf,
1608 size_t *split_len)
1609{
1610 size_t result = 0;
1611 char_u *p = code + 1;
1612 size_t l = len - 2;
1613 int quote = 0;
1614 enum {
1615 ct_ARGS,
1616 ct_BANG,
1617 ct_COUNT,
1618 ct_LINE1,
1619 ct_LINE2,
1620 ct_RANGE,
1621 ct_MODS,
1622 ct_REGISTER,
1623 ct_LT,
1624 ct_NONE
1625 } type = ct_NONE;
1626
1627 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
1628 {
1629 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
1630 p += 2;
1631 l -= 2;
1632 }
1633
1634 ++l;
1635 if (l <= 1)
1636 type = ct_NONE;
1637 else if (STRNICMP(p, "args>", l) == 0)
1638 type = ct_ARGS;
1639 else if (STRNICMP(p, "bang>", l) == 0)
1640 type = ct_BANG;
1641 else if (STRNICMP(p, "count>", l) == 0)
1642 type = ct_COUNT;
1643 else if (STRNICMP(p, "line1>", l) == 0)
1644 type = ct_LINE1;
1645 else if (STRNICMP(p, "line2>", l) == 0)
1646 type = ct_LINE2;
1647 else if (STRNICMP(p, "range>", l) == 0)
1648 type = ct_RANGE;
1649 else if (STRNICMP(p, "lt>", l) == 0)
1650 type = ct_LT;
1651 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
1652 type = ct_REGISTER;
1653 else if (STRNICMP(p, "mods>", l) == 0)
1654 type = ct_MODS;
1655
1656 switch (type)
1657 {
1658 case ct_ARGS:
1659 // Simple case first
1660 if (*eap->arg == NUL)
1661 {
1662 if (quote == 1)
1663 {
1664 result = 2;
1665 if (buf != NULL)
1666 STRCPY(buf, "''");
1667 }
1668 else
1669 result = 0;
1670 break;
1671 }
1672
1673 // When specified there is a single argument don't split it.
1674 // Works for ":Cmd %" when % is "a b c".
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001675 if ((eap->argt & EX_NOSPC) && quote == 2)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001676 quote = 1;
1677
1678 switch (quote)
1679 {
1680 case 0: // No quoting, no splitting
1681 result = STRLEN(eap->arg);
1682 if (buf != NULL)
1683 STRCPY(buf, eap->arg);
1684 break;
1685 case 1: // Quote, but don't split
1686 result = STRLEN(eap->arg) + 2;
1687 for (p = eap->arg; *p; ++p)
1688 {
1689 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1690 // DBCS can contain \ in a trail byte, skip the
1691 // double-byte character.
1692 ++p;
1693 else
1694 if (*p == '\\' || *p == '"')
1695 ++result;
1696 }
1697
1698 if (buf != NULL)
1699 {
1700 *buf++ = '"';
1701 for (p = eap->arg; *p; ++p)
1702 {
1703 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
1704 // DBCS can contain \ in a trail byte, copy the
1705 // double-byte character to avoid escaping.
1706 *buf++ = *p++;
1707 else
1708 if (*p == '\\' || *p == '"')
1709 *buf++ = '\\';
1710 *buf++ = *p;
1711 }
1712 *buf = '"';
1713 }
1714
1715 break;
1716 case 2: // Quote and split (<f-args>)
1717 // This is hard, so only do it once, and cache the result
1718 if (*split_buf == NULL)
1719 *split_buf = uc_split_args(eap->arg, split_len);
1720
1721 result = *split_len;
1722 if (buf != NULL && result != 0)
1723 STRCPY(buf, *split_buf);
1724
1725 break;
1726 }
1727 break;
1728
1729 case ct_BANG:
1730 result = eap->forceit ? 1 : 0;
1731 if (quote)
1732 result += 2;
1733 if (buf != NULL)
1734 {
1735 if (quote)
1736 *buf++ = '"';
1737 if (eap->forceit)
1738 *buf++ = '!';
1739 if (quote)
1740 *buf = '"';
1741 }
1742 break;
1743
1744 case ct_LINE1:
1745 case ct_LINE2:
1746 case ct_RANGE:
1747 case ct_COUNT:
1748 {
1749 char num_buf[20];
1750 long num = (type == ct_LINE1) ? eap->line1 :
1751 (type == ct_LINE2) ? eap->line2 :
1752 (type == ct_RANGE) ? eap->addr_count :
1753 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
1754 size_t num_len;
1755
1756 sprintf(num_buf, "%ld", num);
1757 num_len = STRLEN(num_buf);
1758 result = num_len;
1759
1760 if (quote)
1761 result += 2;
1762
1763 if (buf != NULL)
1764 {
1765 if (quote)
1766 *buf++ = '"';
1767 STRCPY(buf, num_buf);
1768 buf += num_len;
1769 if (quote)
1770 *buf = '"';
1771 }
1772
1773 break;
1774 }
1775
1776 case ct_MODS:
1777 {
Bram Moolenaar02194d22020-10-24 23:08:38 +02001778 result = produce_cmdmods(buf, &cmdmod, quote);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001779 break;
1780 }
1781
1782 case ct_REGISTER:
1783 result = eap->regname ? 1 : 0;
1784 if (quote)
1785 result += 2;
1786 if (buf != NULL)
1787 {
1788 if (quote)
1789 *buf++ = '\'';
1790 if (eap->regname)
1791 *buf++ = eap->regname;
1792 if (quote)
1793 *buf = '\'';
1794 }
1795 break;
1796
1797 case ct_LT:
1798 result = 1;
1799 if (buf != NULL)
1800 *buf = '<';
1801 break;
1802
1803 default:
1804 // Not recognized: just copy the '<' and return -1.
1805 result = (size_t)-1;
1806 if (buf != NULL)
1807 *buf = '<';
1808 break;
1809 }
1810
1811 return result;
1812}
1813
1814/*
1815 * Execute a user defined command.
1816 */
1817 void
1818do_ucmd(exarg_T *eap)
1819{
1820 char_u *buf;
1821 char_u *p;
1822 char_u *q;
1823
1824 char_u *start;
1825 char_u *end = NULL;
1826 char_u *ksp;
1827 size_t len, totlen;
1828
1829 size_t split_len = 0;
1830 char_u *split_buf = NULL;
1831 ucmd_T *cmd;
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001832 sctx_T save_current_sctx;
1833 int restore_current_sctx = FALSE;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001834#ifdef FEAT_EVAL
1835 int restore_script_version = 0;
1836#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001837
1838 if (eap->cmdidx == CMD_USER)
1839 cmd = USER_CMD(eap->useridx);
1840 else
zeertzjqb444ee72023-02-20 15:25:13 +00001841 cmd = USER_CMD_GA(&prevwin_curwin()->w_buffer->b_ucmds, eap->useridx);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001842
1843 /*
1844 * Replace <> in the command by the arguments.
1845 * First round: "buf" is NULL, compute length, allocate "buf".
1846 * Second round: copy result into "buf".
1847 */
1848 buf = NULL;
1849 for (;;)
1850 {
1851 p = cmd->uc_rep; // source
1852 q = buf; // destination
1853 totlen = 0;
1854
1855 for (;;)
1856 {
1857 start = vim_strchr(p, '<');
1858 if (start != NULL)
1859 end = vim_strchr(start + 1, '>');
1860 if (buf != NULL)
1861 {
1862 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
1863 ;
1864 if (*ksp == K_SPECIAL
1865 && (start == NULL || ksp < start || end == NULL)
1866 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
1867# ifdef FEAT_GUI
1868 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
1869# endif
1870 ))
1871 {
1872 // K_SPECIAL has been put in the buffer as K_SPECIAL
1873 // KS_SPECIAL KE_FILLER, like for mappings, but
1874 // do_cmdline() doesn't handle that, so convert it back.
1875 // Also change K_SPECIAL KS_EXTRA KE_CSI into CSI.
1876 len = ksp - p;
1877 if (len > 0)
1878 {
1879 mch_memmove(q, p, len);
1880 q += len;
1881 }
1882 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
1883 p = ksp + 3;
1884 continue;
1885 }
1886 }
1887
1888 // break if no <item> is found
1889 if (start == NULL || end == NULL)
1890 break;
1891
1892 // Include the '>'
1893 ++end;
1894
1895 // Take everything up to the '<'
1896 len = start - p;
1897 if (buf == NULL)
1898 totlen += len;
1899 else
1900 {
1901 mch_memmove(q, p, len);
1902 q += len;
1903 }
1904
1905 len = uc_check_code(start, end - start, q, cmd, eap,
1906 &split_buf, &split_len);
1907 if (len == (size_t)-1)
1908 {
1909 // no match, continue after '<'
1910 p = start + 1;
1911 len = 1;
1912 }
1913 else
1914 p = end;
1915 if (buf == NULL)
1916 totlen += len;
1917 else
1918 q += len;
1919 }
1920 if (buf != NULL) // second time here, finished
1921 {
1922 STRCPY(q, p);
1923 break;
1924 }
1925
1926 totlen += STRLEN(p); // Add on the trailing characters
Bram Moolenaar964b3742019-05-24 18:54:09 +02001927 buf = alloc(totlen + 1);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001928 if (buf == NULL)
1929 {
1930 vim_free(split_buf);
1931 return;
1932 }
1933 }
1934
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001935 if ((cmd->uc_argt & EX_KEEPSCRIPT) == 0)
1936 {
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001937 restore_current_sctx = TRUE;
1938 save_current_sctx = current_sctx;
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001939 current_sctx.sc_version = cmd->uc_script_ctx.sc_version;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001940#ifdef FEAT_EVAL
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001941 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001942 if (cmd->uc_flags & UC_VIM9)
1943 {
1944 // In a {} block variables use Vim9 script rules, even in a legacy
1945 // script.
1946 restore_script_version =
1947 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version;
1948 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = SCRIPT_VERSION_VIM9;
1949 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001950#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001951 }
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001952
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001953 (void)do_cmdline(buf, eap->getline, eap->cookie,
1954 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
Bram Moolenaar205f29c2021-12-10 21:46:09 +00001955
1956 // Careful: Do not use "cmd" here, it may have become invalid if a user
1957 // command was added.
1958 if (restore_current_sctx)
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001959 {
1960#ifdef FEAT_EVAL
1961 if (restore_script_version != 0)
1962 SCRIPT_ITEM(current_sctx.sc_sid)->sn_version =
1963 restore_script_version;
1964#endif
Bram Moolenaar58ef8a32021-11-12 11:25:11 +00001965 current_sctx = save_current_sctx;
Bram Moolenaar98b7fe72022-03-23 21:36:27 +00001966 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001967 vim_free(buf);
1968 vim_free(split_buf);
1969}