Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1 | /* 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 | |
| 16 | typedef 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 Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 23 | cmd_addr_T uc_addr_type; // The command's address type |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 24 | sctx_T uc_script_ctx; // SCTX where the command was defined |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 25 | # ifdef FEAT_EVAL |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 26 | char_u *uc_compl_arg; // completion argument if any |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 27 | # endif |
| 28 | } ucmd_T; |
| 29 | |
| 30 | // List of all user commands. |
| 31 | static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; |
| 32 | |
| 33 | #define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i]) |
| 34 | #define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i]) |
| 35 | |
| 36 | /* |
| 37 | * List of names for completion for ":command" with the EXPAND_ flag. |
| 38 | * Must be alphabetical for completion. |
| 39 | */ |
| 40 | static struct |
| 41 | { |
| 42 | int expand; |
| 43 | char *name; |
| 44 | } command_complete[] = |
| 45 | { |
| 46 | {EXPAND_ARGLIST, "arglist"}, |
| 47 | {EXPAND_AUGROUP, "augroup"}, |
| 48 | {EXPAND_BEHAVE, "behave"}, |
| 49 | {EXPAND_BUFFERS, "buffer"}, |
| 50 | {EXPAND_COLORS, "color"}, |
| 51 | {EXPAND_COMMANDS, "command"}, |
| 52 | {EXPAND_COMPILER, "compiler"}, |
| 53 | #if defined(FEAT_CSCOPE) |
| 54 | {EXPAND_CSCOPE, "cscope"}, |
| 55 | #endif |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 56 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 57 | {EXPAND_USER_DEFINED, "custom"}, |
| 58 | {EXPAND_USER_LIST, "customlist"}, |
| 59 | #endif |
Bram Moolenaar | ae7dba8 | 2019-12-29 13:56:33 +0100 | [diff] [blame] | 60 | {EXPAND_DIFF_BUFFERS, "diff_buffer"}, |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 61 | {EXPAND_DIRECTORIES, "dir"}, |
| 62 | {EXPAND_ENV_VARS, "environment"}, |
| 63 | {EXPAND_EVENTS, "event"}, |
| 64 | {EXPAND_EXPRESSION, "expression"}, |
| 65 | {EXPAND_FILES, "file"}, |
| 66 | {EXPAND_FILES_IN_PATH, "file_in_path"}, |
| 67 | {EXPAND_FILETYPE, "filetype"}, |
| 68 | {EXPAND_FUNCTIONS, "function"}, |
| 69 | {EXPAND_HELP, "help"}, |
| 70 | {EXPAND_HIGHLIGHT, "highlight"}, |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 71 | {EXPAND_HISTORY, "history"}, |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 72 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 73 | {EXPAND_LOCALES, "locale"}, |
| 74 | #endif |
| 75 | {EXPAND_MAPCLEAR, "mapclear"}, |
| 76 | {EXPAND_MAPPINGS, "mapping"}, |
| 77 | {EXPAND_MENUS, "menu"}, |
| 78 | {EXPAND_MESSAGES, "messages"}, |
| 79 | {EXPAND_OWNSYNTAX, "syntax"}, |
| 80 | #if defined(FEAT_PROFILE) |
| 81 | {EXPAND_SYNTIME, "syntime"}, |
| 82 | #endif |
| 83 | {EXPAND_SETTINGS, "option"}, |
| 84 | {EXPAND_PACKADD, "packadd"}, |
| 85 | {EXPAND_SHELLCMD, "shellcmd"}, |
| 86 | #if defined(FEAT_SIGNS) |
| 87 | {EXPAND_SIGN, "sign"}, |
| 88 | #endif |
| 89 | {EXPAND_TAGS, "tag"}, |
| 90 | {EXPAND_TAGS_LISTFILES, "tag_listfiles"}, |
| 91 | {EXPAND_USER, "user"}, |
| 92 | {EXPAND_USER_VARS, "var"}, |
| 93 | {0, NULL} |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * List of names of address types. Must be alphabetical for completion. |
| 98 | */ |
| 99 | static struct |
| 100 | { |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 101 | cmd_addr_T expand; |
| 102 | char *name; |
| 103 | char *shortname; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 104 | } addr_type_complete[] = |
| 105 | { |
| 106 | {ADDR_ARGUMENTS, "arguments", "arg"}, |
| 107 | {ADDR_LINES, "lines", "line"}, |
| 108 | {ADDR_LOADED_BUFFERS, "loaded_buffers", "load"}, |
| 109 | {ADDR_TABS, "tabs", "tab"}, |
| 110 | {ADDR_BUFFERS, "buffers", "buf"}, |
| 111 | {ADDR_WINDOWS, "windows", "win"}, |
| 112 | {ADDR_QUICKFIX, "quickfix", "qf"}, |
| 113 | {ADDR_OTHER, "other", "?"}, |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 114 | {ADDR_NONE, NULL, NULL} |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #define UC_BUFFER 1 // -buffer: local to current buffer |
| 118 | |
| 119 | /* |
| 120 | * Search for a user command that matches "eap->cmd". |
| 121 | * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". |
| 122 | * Return a pointer to just after the command. |
| 123 | * Return NULL if there is no matching command. |
| 124 | */ |
| 125 | char_u * |
| 126 | find_ucmd( |
| 127 | exarg_T *eap, |
| 128 | char_u *p, // end of the command (possibly including count) |
| 129 | int *full, // set to TRUE for a full match |
| 130 | expand_T *xp, // used for completion, NULL otherwise |
Bram Moolenaar | 52111f8 | 2019-04-29 21:30:45 +0200 | [diff] [blame] | 131 | int *complp UNUSED) // completion flags or NULL |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 132 | { |
| 133 | int len = (int)(p - eap->cmd); |
| 134 | int j, k, matchlen = 0; |
| 135 | ucmd_T *uc; |
| 136 | int found = FALSE; |
| 137 | int possible = FALSE; |
| 138 | char_u *cp, *np; // Point into typed cmd and test name |
| 139 | garray_T *gap; |
| 140 | int amb_local = FALSE; // Found ambiguous buffer-local command, |
| 141 | // only full match global is accepted. |
| 142 | |
| 143 | /* |
| 144 | * Look for buffer-local user commands first, then global ones. |
| 145 | */ |
| 146 | gap = &curbuf->b_ucmds; |
| 147 | for (;;) |
| 148 | { |
| 149 | for (j = 0; j < gap->ga_len; ++j) |
| 150 | { |
| 151 | uc = USER_CMD_GA(gap, j); |
| 152 | cp = eap->cmd; |
| 153 | np = uc->uc_name; |
| 154 | k = 0; |
| 155 | while (k < len && *np != NUL && *cp++ == *np++) |
| 156 | k++; |
| 157 | if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k]))) |
| 158 | { |
| 159 | // If finding a second match, the command is ambiguous. But |
| 160 | // not if a buffer-local command wasn't a full match and a |
| 161 | // global command is a full match. |
| 162 | if (k == len && found && *np != NUL) |
| 163 | { |
| 164 | if (gap == &ucmds) |
| 165 | return NULL; |
| 166 | amb_local = TRUE; |
| 167 | } |
| 168 | |
| 169 | if (!found || (k == len && *np == NUL)) |
| 170 | { |
| 171 | // If we matched up to a digit, then there could |
| 172 | // be another command including the digit that we |
| 173 | // should use instead. |
| 174 | if (k == len) |
| 175 | found = TRUE; |
| 176 | else |
| 177 | possible = TRUE; |
| 178 | |
| 179 | if (gap == &ucmds) |
| 180 | eap->cmdidx = CMD_USER; |
| 181 | else |
| 182 | eap->cmdidx = CMD_USER_BUF; |
| 183 | eap->argt = (long)uc->uc_argt; |
| 184 | eap->useridx = j; |
| 185 | eap->addr_type = uc->uc_addr_type; |
| 186 | |
Bram Moolenaar | 52111f8 | 2019-04-29 21:30:45 +0200 | [diff] [blame] | 187 | if (complp != NULL) |
| 188 | *complp = uc->uc_compl; |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 189 | # ifdef FEAT_EVAL |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 190 | if (xp != NULL) |
| 191 | { |
| 192 | xp->xp_arg = uc->uc_compl_arg; |
| 193 | xp->xp_script_ctx = uc->uc_script_ctx; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 194 | xp->xp_script_ctx.sc_lnum += SOURCING_LNUM; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 195 | } |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 196 | # endif |
| 197 | // Do not search for further abbreviations |
| 198 | // if this is an exact match. |
| 199 | matchlen = k; |
| 200 | if (k == len && *np == NUL) |
| 201 | { |
| 202 | if (full != NULL) |
| 203 | *full = TRUE; |
| 204 | amb_local = FALSE; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Stop if we found a full match or searched all. |
| 212 | if (j < gap->ga_len || gap == &ucmds) |
| 213 | break; |
| 214 | gap = &ucmds; |
| 215 | } |
| 216 | |
| 217 | // Only found ambiguous matches. |
| 218 | if (amb_local) |
| 219 | { |
| 220 | if (xp != NULL) |
| 221 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | // The match we found may be followed immediately by a number. Move "p" |
| 226 | // back to point to it. |
| 227 | if (found || possible) |
| 228 | return p + (matchlen - len); |
| 229 | return p; |
| 230 | } |
| 231 | |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 232 | char_u * |
| 233 | set_context_in_user_cmd(expand_T *xp, char_u *arg_in) |
| 234 | { |
| 235 | char_u *arg = arg_in; |
| 236 | char_u *p; |
| 237 | |
| 238 | // Check for attributes |
| 239 | while (*arg == '-') |
| 240 | { |
| 241 | arg++; // Skip "-" |
| 242 | p = skiptowhite(arg); |
| 243 | if (*p == NUL) |
| 244 | { |
| 245 | // Cursor is still in the attribute |
| 246 | p = vim_strchr(arg, '='); |
| 247 | if (p == NULL) |
| 248 | { |
| 249 | // No "=", so complete attribute names |
| 250 | xp->xp_context = EXPAND_USER_CMD_FLAGS; |
| 251 | xp->xp_pattern = arg; |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | // For the -complete, -nargs and -addr attributes, we complete |
| 256 | // their arguments as well. |
| 257 | if (STRNICMP(arg, "complete", p - arg) == 0) |
| 258 | { |
| 259 | xp->xp_context = EXPAND_USER_COMPLETE; |
| 260 | xp->xp_pattern = p + 1; |
| 261 | return NULL; |
| 262 | } |
| 263 | else if (STRNICMP(arg, "nargs", p - arg) == 0) |
| 264 | { |
| 265 | xp->xp_context = EXPAND_USER_NARGS; |
| 266 | xp->xp_pattern = p + 1; |
| 267 | return NULL; |
| 268 | } |
| 269 | else if (STRNICMP(arg, "addr", p - arg) == 0) |
| 270 | { |
| 271 | xp->xp_context = EXPAND_USER_ADDR_TYPE; |
| 272 | xp->xp_pattern = p + 1; |
| 273 | return NULL; |
| 274 | } |
| 275 | return NULL; |
| 276 | } |
| 277 | arg = skipwhite(p); |
| 278 | } |
| 279 | |
| 280 | // After the attributes comes the new command name |
| 281 | p = skiptowhite(arg); |
| 282 | if (*p == NUL) |
| 283 | { |
| 284 | xp->xp_context = EXPAND_USER_COMMANDS; |
| 285 | xp->xp_pattern = arg; |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | // And finally comes a normal command |
| 290 | return skipwhite(p); |
| 291 | } |
| 292 | |
| 293 | char_u * |
| 294 | get_user_command_name(int idx) |
| 295 | { |
| 296 | return get_user_commands(NULL, idx - (int)CMD_SIZE); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Function given to ExpandGeneric() to obtain the list of user command names. |
| 301 | */ |
| 302 | char_u * |
| 303 | get_user_commands(expand_T *xp UNUSED, int idx) |
| 304 | { |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 305 | // In cmdwin, the alternative buffer should be used. |
| 306 | buf_T *buf = |
| 307 | #ifdef FEAT_CMDWIN |
| 308 | (cmdwin_type != 0 && get_cmdline_type() == NUL) ? prevwin->w_buffer : |
| 309 | #endif |
| 310 | curbuf; |
| 311 | |
| 312 | if (idx < buf->b_ucmds.ga_len) |
| 313 | return USER_CMD_GA(&buf->b_ucmds, idx)->uc_name; |
| 314 | idx -= buf->b_ucmds.ga_len; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 315 | if (idx < ucmds.ga_len) |
| 316 | return USER_CMD(idx)->uc_name; |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Function given to ExpandGeneric() to obtain the list of user address type |
| 322 | * names. |
| 323 | */ |
| 324 | char_u * |
| 325 | get_user_cmd_addr_type(expand_T *xp UNUSED, int idx) |
| 326 | { |
| 327 | return (char_u *)addr_type_complete[idx].name; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Function given to ExpandGeneric() to obtain the list of user command |
| 332 | * attributes. |
| 333 | */ |
| 334 | char_u * |
| 335 | get_user_cmd_flags(expand_T *xp UNUSED, int idx) |
| 336 | { |
| 337 | static char *user_cmd_flags[] = { |
| 338 | "addr", "bang", "bar", "buffer", "complete", |
| 339 | "count", "nargs", "range", "register" |
| 340 | }; |
| 341 | |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 342 | if (idx >= (int)ARRAY_LENGTH(user_cmd_flags)) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 343 | return NULL; |
| 344 | return (char_u *)user_cmd_flags[idx]; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Function given to ExpandGeneric() to obtain the list of values for -nargs. |
| 349 | */ |
| 350 | char_u * |
| 351 | get_user_cmd_nargs(expand_T *xp UNUSED, int idx) |
| 352 | { |
| 353 | static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"}; |
| 354 | |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 355 | if (idx >= (int)ARRAY_LENGTH(user_cmd_nargs)) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 356 | return NULL; |
| 357 | return (char_u *)user_cmd_nargs[idx]; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Function given to ExpandGeneric() to obtain the list of values for |
| 362 | * -complete. |
| 363 | */ |
| 364 | char_u * |
| 365 | get_user_cmd_complete(expand_T *xp UNUSED, int idx) |
| 366 | { |
| 367 | return (char_u *)command_complete[idx].name; |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | cmdcomplete_str_to_type(char_u *complete_str) |
| 372 | { |
| 373 | int i; |
| 374 | |
| 375 | for (i = 0; command_complete[i].expand != 0; ++i) |
| 376 | if (STRCMP(complete_str, command_complete[i].name) == 0) |
| 377 | return command_complete[i].expand; |
| 378 | |
| 379 | return EXPAND_NOTHING; |
| 380 | } |
| 381 | |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 382 | /* |
| 383 | * List user commands starting with "name[name_len]". |
| 384 | */ |
| 385 | static void |
| 386 | uc_list(char_u *name, size_t name_len) |
| 387 | { |
| 388 | int i, j; |
| 389 | int found = FALSE; |
| 390 | ucmd_T *cmd; |
| 391 | int len; |
| 392 | int over; |
| 393 | long a; |
| 394 | garray_T *gap; |
| 395 | |
Bram Moolenaar | e38eab2 | 2019-12-05 21:50:01 +0100 | [diff] [blame] | 396 | // In cmdwin, the alternative buffer should be used. |
Bram Moolenaar | f03e328 | 2019-07-22 21:55:18 +0200 | [diff] [blame] | 397 | gap = |
| 398 | #ifdef FEAT_CMDWIN |
| 399 | (cmdwin_type != 0 && get_cmdline_type() == NUL) ? |
| 400 | &prevwin->w_buffer->b_ucmds : |
| 401 | #endif |
| 402 | &curbuf->b_ucmds; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 403 | for (;;) |
| 404 | { |
| 405 | for (i = 0; i < gap->ga_len; ++i) |
| 406 | { |
| 407 | cmd = USER_CMD_GA(gap, i); |
| 408 | a = (long)cmd->uc_argt; |
| 409 | |
| 410 | // Skip commands which don't match the requested prefix and |
| 411 | // commands filtered out. |
| 412 | if (STRNCMP(name, cmd->uc_name, name_len) != 0 |
| 413 | || message_filtered(cmd->uc_name)) |
| 414 | continue; |
| 415 | |
| 416 | // Put out the title first time |
| 417 | if (!found) |
| 418 | msg_puts_title(_("\n Name Args Address Complete Definition")); |
| 419 | found = TRUE; |
| 420 | msg_putchar('\n'); |
| 421 | if (got_int) |
| 422 | break; |
| 423 | |
| 424 | // Special cases |
| 425 | len = 4; |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 426 | if (a & EX_BANG) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 427 | { |
| 428 | msg_putchar('!'); |
| 429 | --len; |
| 430 | } |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 431 | if (a & EX_REGSTR) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 432 | { |
| 433 | msg_putchar('"'); |
| 434 | --len; |
| 435 | } |
| 436 | if (gap != &ucmds) |
| 437 | { |
| 438 | msg_putchar('b'); |
| 439 | --len; |
| 440 | } |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 441 | if (a & EX_TRLBAR) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 442 | { |
| 443 | msg_putchar('|'); |
| 444 | --len; |
| 445 | } |
| 446 | while (len-- > 0) |
| 447 | msg_putchar(' '); |
| 448 | |
| 449 | msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D)); |
| 450 | len = (int)STRLEN(cmd->uc_name) + 4; |
| 451 | |
| 452 | do { |
| 453 | msg_putchar(' '); |
| 454 | ++len; |
| 455 | } while (len < 22); |
| 456 | |
| 457 | // "over" is how much longer the name is than the column width for |
| 458 | // the name, we'll try to align what comes after. |
| 459 | over = len - 22; |
| 460 | len = 0; |
| 461 | |
| 462 | // Arguments |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 463 | switch ((int)(a & (EX_EXTRA|EX_NOSPC|EX_NEEDARG))) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 464 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 465 | case 0: IObuff[len++] = '0'; break; |
| 466 | case (EX_EXTRA): IObuff[len++] = '*'; break; |
| 467 | case (EX_EXTRA|EX_NOSPC): IObuff[len++] = '?'; break; |
| 468 | case (EX_EXTRA|EX_NEEDARG): IObuff[len++] = '+'; break; |
| 469 | case (EX_EXTRA|EX_NOSPC|EX_NEEDARG): IObuff[len++] = '1'; break; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | do { |
| 473 | IObuff[len++] = ' '; |
| 474 | } while (len < 5 - over); |
| 475 | |
| 476 | // Address / Range |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 477 | if (a & (EX_RANGE|EX_COUNT)) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 478 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 479 | if (a & EX_COUNT) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 480 | { |
| 481 | // -count=N |
| 482 | sprintf((char *)IObuff + len, "%ldc", cmd->uc_def); |
| 483 | len += (int)STRLEN(IObuff + len); |
| 484 | } |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 485 | else if (a & EX_DFLALL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 486 | IObuff[len++] = '%'; |
| 487 | else if (cmd->uc_def >= 0) |
| 488 | { |
| 489 | // -range=N |
| 490 | sprintf((char *)IObuff + len, "%ld", cmd->uc_def); |
| 491 | len += (int)STRLEN(IObuff + len); |
| 492 | } |
| 493 | else |
| 494 | IObuff[len++] = '.'; |
| 495 | } |
| 496 | |
| 497 | do { |
| 498 | IObuff[len++] = ' '; |
| 499 | } while (len < 8 - over); |
| 500 | |
| 501 | // Address Type |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 502 | for (j = 0; addr_type_complete[j].expand != ADDR_NONE; ++j) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 503 | if (addr_type_complete[j].expand != ADDR_LINES |
| 504 | && addr_type_complete[j].expand == cmd->uc_addr_type) |
| 505 | { |
| 506 | STRCPY(IObuff + len, addr_type_complete[j].shortname); |
| 507 | len += (int)STRLEN(IObuff + len); |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | do { |
| 512 | IObuff[len++] = ' '; |
| 513 | } while (len < 13 - over); |
| 514 | |
| 515 | // Completion |
| 516 | for (j = 0; command_complete[j].expand != 0; ++j) |
| 517 | if (command_complete[j].expand == cmd->uc_compl) |
| 518 | { |
| 519 | STRCPY(IObuff + len, command_complete[j].name); |
| 520 | len += (int)STRLEN(IObuff + len); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | do { |
| 525 | IObuff[len++] = ' '; |
| 526 | } while (len < 25 - over); |
| 527 | |
| 528 | IObuff[len] = '\0'; |
| 529 | msg_outtrans(IObuff); |
| 530 | |
| 531 | msg_outtrans_special(cmd->uc_rep, FALSE, |
| 532 | name_len == 0 ? Columns - 47 : 0); |
| 533 | #ifdef FEAT_EVAL |
| 534 | if (p_verbose > 0) |
| 535 | last_set_msg(cmd->uc_script_ctx); |
| 536 | #endif |
| 537 | out_flush(); |
| 538 | ui_breakcheck(); |
| 539 | if (got_int) |
| 540 | break; |
| 541 | } |
| 542 | if (gap == &ucmds || i < gap->ga_len) |
| 543 | break; |
| 544 | gap = &ucmds; |
| 545 | } |
| 546 | |
| 547 | if (!found) |
| 548 | msg(_("No user-defined commands found")); |
| 549 | } |
| 550 | |
| 551 | char * |
| 552 | uc_fun_cmd(void) |
| 553 | { |
| 554 | static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4, |
| 555 | 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60, |
| 556 | 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2, |
| 557 | 0xb9, 0x7f, 0}; |
| 558 | int i; |
| 559 | |
| 560 | for (i = 0; fcmd[i]; ++i) |
| 561 | IObuff[i] = fcmd[i] - 0x40; |
| 562 | IObuff[i] = 0; |
| 563 | return (char *)IObuff; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Parse address type argument |
| 568 | */ |
| 569 | static int |
| 570 | parse_addr_type_arg( |
| 571 | char_u *value, |
| 572 | int vallen, |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 573 | cmd_addr_T *addr_type_arg) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 574 | { |
| 575 | int i, a, b; |
| 576 | |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 577 | for (i = 0; addr_type_complete[i].expand != ADDR_NONE; ++i) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 578 | { |
| 579 | a = (int)STRLEN(addr_type_complete[i].name) == vallen; |
| 580 | b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0; |
| 581 | if (a && b) |
| 582 | { |
| 583 | *addr_type_arg = addr_type_complete[i].expand; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 588 | if (addr_type_complete[i].expand == ADDR_NONE) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 589 | { |
| 590 | char_u *err = value; |
| 591 | |
| 592 | for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++) |
| 593 | ; |
| 594 | err[i] = NUL; |
| 595 | semsg(_("E180: Invalid address type value: %s"), err); |
| 596 | return FAIL; |
| 597 | } |
| 598 | |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 599 | return OK; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Parse a completion argument "value[vallen]". |
| 604 | * The detected completion goes in "*complp", argument type in "*argt". |
| 605 | * When there is an argument, for function and user defined completion, it's |
| 606 | * copied to allocated memory and stored in "*compl_arg". |
| 607 | * Returns FAIL if something is wrong. |
| 608 | */ |
| 609 | int |
| 610 | parse_compl_arg( |
| 611 | char_u *value, |
| 612 | int vallen, |
| 613 | int *complp, |
| 614 | long *argt, |
| 615 | char_u **compl_arg UNUSED) |
| 616 | { |
| 617 | char_u *arg = NULL; |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 618 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 619 | size_t arglen = 0; |
| 620 | # endif |
| 621 | int i; |
| 622 | int valend = vallen; |
| 623 | |
| 624 | // Look for any argument part - which is the part after any ',' |
| 625 | for (i = 0; i < vallen; ++i) |
| 626 | { |
| 627 | if (value[i] == ',') |
| 628 | { |
| 629 | arg = &value[i + 1]; |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 630 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 631 | arglen = vallen - i - 1; |
| 632 | # endif |
| 633 | valend = i; |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | for (i = 0; command_complete[i].expand != 0; ++i) |
| 639 | { |
| 640 | if ((int)STRLEN(command_complete[i].name) == valend |
| 641 | && STRNCMP(value, command_complete[i].name, valend) == 0) |
| 642 | { |
| 643 | *complp = command_complete[i].expand; |
| 644 | if (command_complete[i].expand == EXPAND_BUFFERS) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 645 | *argt |= EX_BUFNAME; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 646 | else if (command_complete[i].expand == EXPAND_DIRECTORIES |
| 647 | || command_complete[i].expand == EXPAND_FILES) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 648 | *argt |= EX_XFILE; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | if (command_complete[i].expand == 0) |
| 654 | { |
| 655 | semsg(_("E180: Invalid complete value: %s"), value); |
| 656 | return FAIL; |
| 657 | } |
| 658 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 659 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 660 | if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST |
| 661 | && arg != NULL) |
| 662 | # else |
| 663 | if (arg != NULL) |
| 664 | # endif |
| 665 | { |
| 666 | emsg(_("E468: Completion argument only allowed for custom completion")); |
| 667 | return FAIL; |
| 668 | } |
| 669 | |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 670 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 671 | if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST) |
| 672 | && arg == NULL) |
| 673 | { |
| 674 | emsg(_("E467: Custom completion requires a function argument")); |
| 675 | return FAIL; |
| 676 | } |
| 677 | |
| 678 | if (arg != NULL) |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 679 | *compl_arg = vim_strnsave(arg, arglen); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 680 | # endif |
| 681 | return OK; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Scan attributes in the ":command" command. |
| 686 | * Return FAIL when something is wrong. |
| 687 | */ |
| 688 | static int |
| 689 | uc_scan_attr( |
| 690 | char_u *attr, |
| 691 | size_t len, |
| 692 | long *argt, |
| 693 | long *def, |
| 694 | int *flags, |
Bram Moolenaar | 52111f8 | 2019-04-29 21:30:45 +0200 | [diff] [blame] | 695 | int *complp, |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 696 | char_u **compl_arg, |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 697 | cmd_addr_T *addr_type_arg) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 698 | { |
| 699 | char_u *p; |
| 700 | |
| 701 | if (len == 0) |
| 702 | { |
| 703 | emsg(_("E175: No attribute specified")); |
| 704 | return FAIL; |
| 705 | } |
| 706 | |
| 707 | // First, try the simple attributes (no arguments) |
| 708 | if (STRNICMP(attr, "bang", len) == 0) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 709 | *argt |= EX_BANG; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 710 | else if (STRNICMP(attr, "buffer", len) == 0) |
| 711 | *flags |= UC_BUFFER; |
| 712 | else if (STRNICMP(attr, "register", len) == 0) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 713 | *argt |= EX_REGSTR; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 714 | else if (STRNICMP(attr, "bar", len) == 0) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 715 | *argt |= EX_TRLBAR; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 716 | else |
| 717 | { |
| 718 | int i; |
| 719 | char_u *val = NULL; |
| 720 | size_t vallen = 0; |
| 721 | size_t attrlen = len; |
| 722 | |
| 723 | // Look for the attribute name - which is the part before any '=' |
| 724 | for (i = 0; i < (int)len; ++i) |
| 725 | { |
| 726 | if (attr[i] == '=') |
| 727 | { |
| 728 | val = &attr[i + 1]; |
| 729 | vallen = len - i - 1; |
| 730 | attrlen = i; |
| 731 | break; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (STRNICMP(attr, "nargs", attrlen) == 0) |
| 736 | { |
| 737 | if (vallen == 1) |
| 738 | { |
| 739 | if (*val == '0') |
| 740 | // Do nothing - this is the default |
| 741 | ; |
| 742 | else if (*val == '1') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 743 | *argt |= (EX_EXTRA | EX_NOSPC | EX_NEEDARG); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 744 | else if (*val == '*') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 745 | *argt |= EX_EXTRA; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 746 | else if (*val == '?') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 747 | *argt |= (EX_EXTRA | EX_NOSPC); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 748 | else if (*val == '+') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 749 | *argt |= (EX_EXTRA | EX_NEEDARG); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 750 | else |
| 751 | goto wrong_nargs; |
| 752 | } |
| 753 | else |
| 754 | { |
| 755 | wrong_nargs: |
| 756 | emsg(_("E176: Invalid number of arguments")); |
| 757 | return FAIL; |
| 758 | } |
| 759 | } |
| 760 | else if (STRNICMP(attr, "range", attrlen) == 0) |
| 761 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 762 | *argt |= EX_RANGE; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 763 | if (vallen == 1 && *val == '%') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 764 | *argt |= EX_DFLALL; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 765 | else if (val != NULL) |
| 766 | { |
| 767 | p = val; |
| 768 | if (*def >= 0) |
| 769 | { |
| 770 | two_count: |
| 771 | emsg(_("E177: Count cannot be specified twice")); |
| 772 | return FAIL; |
| 773 | } |
| 774 | |
| 775 | *def = getdigits(&p); |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 776 | *argt |= EX_ZEROR; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 777 | |
| 778 | if (p != val + vallen || vallen == 0) |
| 779 | { |
| 780 | invalid_count: |
| 781 | emsg(_("E178: Invalid default value for count")); |
| 782 | return FAIL; |
| 783 | } |
| 784 | } |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 785 | // default for -range is using buffer lines |
| 786 | if (*addr_type_arg == ADDR_NONE) |
| 787 | *addr_type_arg = ADDR_LINES; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 788 | } |
| 789 | else if (STRNICMP(attr, "count", attrlen) == 0) |
| 790 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 791 | *argt |= (EX_COUNT | EX_ZEROR | EX_RANGE); |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 792 | // default for -count is using any number |
| 793 | if (*addr_type_arg == ADDR_NONE) |
| 794 | *addr_type_arg = ADDR_OTHER; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 795 | |
| 796 | if (val != NULL) |
| 797 | { |
| 798 | p = val; |
| 799 | if (*def >= 0) |
| 800 | goto two_count; |
| 801 | |
| 802 | *def = getdigits(&p); |
| 803 | |
| 804 | if (p != val + vallen) |
| 805 | goto invalid_count; |
| 806 | } |
| 807 | |
| 808 | if (*def < 0) |
| 809 | *def = 0; |
| 810 | } |
| 811 | else if (STRNICMP(attr, "complete", attrlen) == 0) |
| 812 | { |
| 813 | if (val == NULL) |
| 814 | { |
| 815 | emsg(_("E179: argument required for -complete")); |
| 816 | return FAIL; |
| 817 | } |
| 818 | |
Bram Moolenaar | 52111f8 | 2019-04-29 21:30:45 +0200 | [diff] [blame] | 819 | if (parse_compl_arg(val, (int)vallen, complp, argt, compl_arg) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 820 | == FAIL) |
| 821 | return FAIL; |
| 822 | } |
| 823 | else if (STRNICMP(attr, "addr", attrlen) == 0) |
| 824 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 825 | *argt |= EX_RANGE; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 826 | if (val == NULL) |
| 827 | { |
| 828 | emsg(_("E179: argument required for -addr")); |
| 829 | return FAIL; |
| 830 | } |
Bram Moolenaar | e4f5f3a | 2019-05-04 14:05:08 +0200 | [diff] [blame] | 831 | if (parse_addr_type_arg(val, (int)vallen, addr_type_arg) == FAIL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 832 | return FAIL; |
Bram Moolenaar | e4f5f3a | 2019-05-04 14:05:08 +0200 | [diff] [blame] | 833 | if (*addr_type_arg != ADDR_LINES) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 834 | *argt |= EX_ZEROR; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 835 | } |
| 836 | else |
| 837 | { |
| 838 | char_u ch = attr[len]; |
| 839 | attr[len] = '\0'; |
| 840 | semsg(_("E181: Invalid attribute: %s"), attr); |
| 841 | attr[len] = ch; |
| 842 | return FAIL; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return OK; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Add a user command to the list or replace an existing one. |
| 851 | */ |
| 852 | static int |
| 853 | uc_add_command( |
| 854 | char_u *name, |
| 855 | size_t name_len, |
| 856 | char_u *rep, |
| 857 | long argt, |
| 858 | long def, |
| 859 | int flags, |
| 860 | int compl, |
| 861 | char_u *compl_arg UNUSED, |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 862 | cmd_addr_T addr_type, |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 863 | int force) |
| 864 | { |
| 865 | ucmd_T *cmd = NULL; |
| 866 | char_u *p; |
| 867 | int i; |
| 868 | int cmp = 1; |
| 869 | char_u *rep_buf = NULL; |
| 870 | garray_T *gap; |
| 871 | |
Bram Moolenaar | 459fd78 | 2019-10-13 16:43:39 +0200 | [diff] [blame] | 872 | replace_termcodes(rep, &rep_buf, 0, NULL); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 873 | if (rep_buf == NULL) |
| 874 | { |
| 875 | // Can't replace termcodes - try using the string as is |
| 876 | rep_buf = vim_strsave(rep); |
| 877 | |
| 878 | // Give up if out of memory |
| 879 | if (rep_buf == NULL) |
| 880 | return FAIL; |
| 881 | } |
| 882 | |
| 883 | // get address of growarray: global or in curbuf |
| 884 | if (flags & UC_BUFFER) |
| 885 | { |
| 886 | gap = &curbuf->b_ucmds; |
| 887 | if (gap->ga_itemsize == 0) |
| 888 | ga_init2(gap, (int)sizeof(ucmd_T), 4); |
| 889 | } |
| 890 | else |
| 891 | gap = &ucmds; |
| 892 | |
| 893 | // Search for the command in the already defined commands. |
| 894 | for (i = 0; i < gap->ga_len; ++i) |
| 895 | { |
| 896 | size_t len; |
| 897 | |
| 898 | cmd = USER_CMD_GA(gap, i); |
| 899 | len = STRLEN(cmd->uc_name); |
| 900 | cmp = STRNCMP(name, cmd->uc_name, name_len); |
| 901 | if (cmp == 0) |
| 902 | { |
| 903 | if (name_len < len) |
| 904 | cmp = -1; |
| 905 | else if (name_len > len) |
| 906 | cmp = 1; |
| 907 | } |
| 908 | |
| 909 | if (cmp == 0) |
| 910 | { |
| 911 | // Command can be replaced with "command!" and when sourcing the |
| 912 | // same script again, but only once. |
| 913 | if (!force |
| 914 | #ifdef FEAT_EVAL |
| 915 | && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid |
| 916 | || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq) |
| 917 | #endif |
| 918 | ) |
| 919 | { |
| 920 | semsg(_("E174: Command already exists: add ! to replace it: %s"), |
| 921 | name); |
| 922 | goto fail; |
| 923 | } |
| 924 | |
| 925 | VIM_CLEAR(cmd->uc_rep); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 926 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 927 | VIM_CLEAR(cmd->uc_compl_arg); |
| 928 | #endif |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | // Stop as soon as we pass the name to add |
| 933 | if (cmp < 0) |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | // Extend the array unless we're replacing an existing command |
| 938 | if (cmp != 0) |
| 939 | { |
| 940 | if (ga_grow(gap, 1) != OK) |
| 941 | goto fail; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 942 | if ((p = vim_strnsave(name, name_len)) == NULL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 943 | goto fail; |
| 944 | |
| 945 | cmd = USER_CMD_GA(gap, i); |
| 946 | mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T)); |
| 947 | |
| 948 | ++gap->ga_len; |
| 949 | |
| 950 | cmd->uc_name = p; |
| 951 | } |
| 952 | |
| 953 | cmd->uc_rep = rep_buf; |
| 954 | cmd->uc_argt = argt; |
| 955 | cmd->uc_def = def; |
| 956 | cmd->uc_compl = compl; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 957 | cmd->uc_script_ctx = current_sctx; |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 958 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 959 | cmd->uc_script_ctx.sc_lnum += SOURCING_LNUM; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 960 | cmd->uc_compl_arg = compl_arg; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 961 | #endif |
| 962 | cmd->uc_addr_type = addr_type; |
| 963 | |
| 964 | return OK; |
| 965 | |
| 966 | fail: |
| 967 | vim_free(rep_buf); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 968 | #if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 969 | vim_free(compl_arg); |
| 970 | #endif |
| 971 | return FAIL; |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * ":command ..." implementation |
| 976 | */ |
| 977 | void |
| 978 | ex_command(exarg_T *eap) |
| 979 | { |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 980 | char_u *name; |
| 981 | char_u *end; |
| 982 | char_u *p; |
| 983 | long argt = 0; |
| 984 | long def = -1; |
| 985 | int flags = 0; |
| 986 | int compl = EXPAND_NOTHING; |
| 987 | char_u *compl_arg = NULL; |
| 988 | cmd_addr_T addr_type_arg = ADDR_NONE; |
| 989 | int has_attr = (eap->arg[0] == '-'); |
| 990 | int name_len; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 991 | |
| 992 | p = eap->arg; |
| 993 | |
| 994 | // Check for attributes |
| 995 | while (*p == '-') |
| 996 | { |
| 997 | ++p; |
| 998 | end = skiptowhite(p); |
| 999 | if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, |
| 1000 | &compl_arg, &addr_type_arg) == FAIL) |
| 1001 | return; |
| 1002 | p = skipwhite(end); |
| 1003 | } |
| 1004 | |
| 1005 | // Get the name (if any) and skip to the following argument |
| 1006 | name = p; |
| 1007 | if (ASCII_ISALPHA(*p)) |
| 1008 | while (ASCII_ISALNUM(*p)) |
| 1009 | ++p; |
Bram Moolenaar | a72cfb8 | 2020-04-23 17:07:30 +0200 | [diff] [blame] | 1010 | if (!ends_excmd2(eap->arg, p) && !VIM_ISWHITE(*p)) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1011 | { |
| 1012 | emsg(_("E182: Invalid command name")); |
| 1013 | return; |
| 1014 | } |
| 1015 | end = p; |
| 1016 | name_len = (int)(end - name); |
| 1017 | |
| 1018 | // If there is nothing after the name, and no attributes were specified, |
| 1019 | // we are listing commands |
| 1020 | p = skipwhite(end); |
Bram Moolenaar | a72cfb8 | 2020-04-23 17:07:30 +0200 | [diff] [blame] | 1021 | if (!has_attr && ends_excmd2(eap->arg, p)) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1022 | { |
| 1023 | uc_list(name, end - name); |
| 1024 | } |
| 1025 | else if (!ASCII_ISUPPER(*name)) |
| 1026 | { |
| 1027 | emsg(_("E183: User defined commands must start with an uppercase letter")); |
| 1028 | return; |
| 1029 | } |
| 1030 | else if ((name_len == 1 && *name == 'X') |
| 1031 | || (name_len <= 4 |
| 1032 | && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0)) |
| 1033 | { |
| 1034 | emsg(_("E841: Reserved name, cannot be used for user defined command")); |
| 1035 | return; |
| 1036 | } |
| 1037 | else |
| 1038 | uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg, |
| 1039 | addr_type_arg, eap->forceit); |
| 1040 | } |
| 1041 | |
| 1042 | /* |
| 1043 | * ":comclear" implementation |
| 1044 | * Clear all user commands, global and for current buffer. |
| 1045 | */ |
| 1046 | void |
| 1047 | ex_comclear(exarg_T *eap UNUSED) |
| 1048 | { |
| 1049 | uc_clear(&ucmds); |
Bram Moolenaar | e5c8328 | 2019-05-03 23:15:37 +0200 | [diff] [blame] | 1050 | if (curbuf != NULL) |
| 1051 | uc_clear(&curbuf->b_ucmds); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* |
| 1055 | * Clear all user commands for "gap". |
| 1056 | */ |
| 1057 | void |
| 1058 | uc_clear(garray_T *gap) |
| 1059 | { |
| 1060 | int i; |
| 1061 | ucmd_T *cmd; |
| 1062 | |
| 1063 | for (i = 0; i < gap->ga_len; ++i) |
| 1064 | { |
| 1065 | cmd = USER_CMD_GA(gap, i); |
| 1066 | vim_free(cmd->uc_name); |
| 1067 | vim_free(cmd->uc_rep); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 1068 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1069 | vim_free(cmd->uc_compl_arg); |
| 1070 | # endif |
| 1071 | } |
| 1072 | ga_clear(gap); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * ":delcommand" implementation |
| 1077 | */ |
| 1078 | void |
| 1079 | ex_delcommand(exarg_T *eap) |
| 1080 | { |
| 1081 | int i = 0; |
| 1082 | ucmd_T *cmd = NULL; |
| 1083 | int cmp = -1; |
| 1084 | garray_T *gap; |
| 1085 | |
| 1086 | gap = &curbuf->b_ucmds; |
| 1087 | for (;;) |
| 1088 | { |
| 1089 | for (i = 0; i < gap->ga_len; ++i) |
| 1090 | { |
| 1091 | cmd = USER_CMD_GA(gap, i); |
| 1092 | cmp = STRCMP(eap->arg, cmd->uc_name); |
| 1093 | if (cmp <= 0) |
| 1094 | break; |
| 1095 | } |
| 1096 | if (gap == &ucmds || cmp == 0) |
| 1097 | break; |
| 1098 | gap = &ucmds; |
| 1099 | } |
| 1100 | |
| 1101 | if (cmp != 0) |
| 1102 | { |
| 1103 | semsg(_("E184: No such user-defined command: %s"), eap->arg); |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | vim_free(cmd->uc_name); |
| 1108 | vim_free(cmd->uc_rep); |
Bram Moolenaar | 0a52df5 | 2019-08-18 22:26:31 +0200 | [diff] [blame] | 1109 | # if defined(FEAT_EVAL) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1110 | vim_free(cmd->uc_compl_arg); |
| 1111 | # endif |
| 1112 | |
| 1113 | --gap->ga_len; |
| 1114 | |
| 1115 | if (i < gap->ga_len) |
| 1116 | mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T)); |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Split and quote args for <f-args>. |
| 1121 | */ |
| 1122 | static char_u * |
| 1123 | uc_split_args(char_u *arg, size_t *lenp) |
| 1124 | { |
| 1125 | char_u *buf; |
| 1126 | char_u *p; |
| 1127 | char_u *q; |
| 1128 | int len; |
| 1129 | |
| 1130 | // Precalculate length |
| 1131 | p = arg; |
| 1132 | len = 2; // Initial and final quotes |
| 1133 | |
| 1134 | while (*p) |
| 1135 | { |
| 1136 | if (p[0] == '\\' && p[1] == '\\') |
| 1137 | { |
| 1138 | len += 2; |
| 1139 | p += 2; |
| 1140 | } |
| 1141 | else if (p[0] == '\\' && VIM_ISWHITE(p[1])) |
| 1142 | { |
| 1143 | len += 1; |
| 1144 | p += 2; |
| 1145 | } |
| 1146 | else if (*p == '\\' || *p == '"') |
| 1147 | { |
| 1148 | len += 2; |
| 1149 | p += 1; |
| 1150 | } |
| 1151 | else if (VIM_ISWHITE(*p)) |
| 1152 | { |
| 1153 | p = skipwhite(p); |
| 1154 | if (*p == NUL) |
| 1155 | break; |
Bram Moolenaar | 20d89e0 | 2020-10-20 23:11:33 +0200 | [diff] [blame] | 1156 | len += 4; // ", " |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1157 | } |
| 1158 | else |
| 1159 | { |
| 1160 | int charlen = (*mb_ptr2len)(p); |
| 1161 | |
| 1162 | len += charlen; |
| 1163 | p += charlen; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | buf = alloc(len + 1); |
| 1168 | if (buf == NULL) |
| 1169 | { |
| 1170 | *lenp = 0; |
| 1171 | return buf; |
| 1172 | } |
| 1173 | |
| 1174 | p = arg; |
| 1175 | q = buf; |
| 1176 | *q++ = '"'; |
| 1177 | while (*p) |
| 1178 | { |
| 1179 | if (p[0] == '\\' && p[1] == '\\') |
| 1180 | { |
| 1181 | *q++ = '\\'; |
| 1182 | *q++ = '\\'; |
| 1183 | p += 2; |
| 1184 | } |
| 1185 | else if (p[0] == '\\' && VIM_ISWHITE(p[1])) |
| 1186 | { |
| 1187 | *q++ = p[1]; |
| 1188 | p += 2; |
| 1189 | } |
| 1190 | else if (*p == '\\' || *p == '"') |
| 1191 | { |
| 1192 | *q++ = '\\'; |
| 1193 | *q++ = *p++; |
| 1194 | } |
| 1195 | else if (VIM_ISWHITE(*p)) |
| 1196 | { |
| 1197 | p = skipwhite(p); |
| 1198 | if (*p == NUL) |
| 1199 | break; |
| 1200 | *q++ = '"'; |
| 1201 | *q++ = ','; |
Bram Moolenaar | 20d89e0 | 2020-10-20 23:11:33 +0200 | [diff] [blame] | 1202 | *q++ = ' '; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1203 | *q++ = '"'; |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | MB_COPY_CHAR(p, q); |
| 1208 | } |
| 1209 | } |
| 1210 | *q++ = '"'; |
| 1211 | *q = 0; |
| 1212 | |
| 1213 | *lenp = len; |
| 1214 | return buf; |
| 1215 | } |
| 1216 | |
| 1217 | static size_t |
| 1218 | add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods) |
| 1219 | { |
| 1220 | size_t result; |
| 1221 | |
| 1222 | result = STRLEN(mod_str); |
| 1223 | if (*multi_mods) |
| 1224 | result += 1; |
| 1225 | if (buf != NULL) |
| 1226 | { |
| 1227 | if (*multi_mods) |
| 1228 | STRCAT(buf, " "); |
| 1229 | STRCAT(buf, mod_str); |
| 1230 | } |
| 1231 | |
| 1232 | *multi_mods = 1; |
| 1233 | |
| 1234 | return result; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1238 | * Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1239 | * was added. Return the number of bytes added. |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1240 | */ |
| 1241 | size_t |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1242 | add_win_cmd_modifers(char_u *buf, cmdmod_T *cmod, int *multi_mods) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1243 | { |
| 1244 | size_t result = 0; |
| 1245 | |
| 1246 | // :aboveleft and :leftabove |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1247 | if (cmod->cmod_split & WSP_ABOVE) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1248 | result += add_cmd_modifier(buf, "aboveleft", multi_mods); |
| 1249 | // :belowright and :rightbelow |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1250 | if (cmod->cmod_split & WSP_BELOW) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1251 | result += add_cmd_modifier(buf, "belowright", multi_mods); |
| 1252 | // :botright |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1253 | if (cmod->cmod_split & WSP_BOT) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1254 | result += add_cmd_modifier(buf, "botright", multi_mods); |
| 1255 | |
| 1256 | // :tab |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1257 | if (cmod->cmod_tab > 0) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1258 | result += add_cmd_modifier(buf, "tab", multi_mods); |
| 1259 | // :topleft |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1260 | if (cmod->cmod_split & WSP_TOP) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1261 | result += add_cmd_modifier(buf, "topleft", multi_mods); |
| 1262 | // :vertical |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1263 | if (cmod->cmod_split & WSP_VERT) |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1264 | result += add_cmd_modifier(buf, "vertical", multi_mods); |
| 1265 | return result; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1269 | * Generate text for the "cmod" command modifiers. |
| 1270 | * If "buf" is NULL just return the length. |
| 1271 | */ |
Bram Moolenaar | a360dbe | 2020-10-26 18:46:53 +0100 | [diff] [blame] | 1272 | size_t |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1273 | produce_cmdmods(char_u *buf, cmdmod_T *cmod, int quote) |
| 1274 | { |
Bram Moolenaar | a360dbe | 2020-10-26 18:46:53 +0100 | [diff] [blame] | 1275 | size_t result = 0; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1276 | int multi_mods = 0; |
| 1277 | int i; |
| 1278 | typedef struct { |
| 1279 | int flag; |
| 1280 | char *name; |
| 1281 | } mod_entry_T; |
| 1282 | static mod_entry_T mod_entries[] = { |
| 1283 | #ifdef FEAT_BROWSE_CMD |
| 1284 | {CMOD_BROWSE, "browse"}, |
| 1285 | #endif |
| 1286 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 1287 | {CMOD_CONFIRM, "confirm"}, |
| 1288 | #endif |
| 1289 | {CMOD_HIDE, "hide"}, |
| 1290 | {CMOD_KEEPALT, "keepalt"}, |
| 1291 | {CMOD_KEEPJUMPS, "keepjumps"}, |
| 1292 | {CMOD_KEEPMARKS, "keepmarks"}, |
| 1293 | {CMOD_KEEPPATTERNS, "keeppatterns"}, |
| 1294 | {CMOD_LOCKMARKS, "lockmarks"}, |
| 1295 | {CMOD_NOSWAPFILE, "noswapfile"}, |
| 1296 | {CMOD_UNSILENT, "unsilent"}, |
| 1297 | {CMOD_NOAUTOCMD, "noautocmd"}, |
| 1298 | #ifdef HAVE_SANDBOX |
| 1299 | {CMOD_SANDBOX, "sandbox"}, |
| 1300 | #endif |
| 1301 | {0, NULL} |
| 1302 | }; |
| 1303 | |
| 1304 | result = quote ? 2 : 0; |
| 1305 | if (buf != NULL) |
| 1306 | { |
| 1307 | if (quote) |
| 1308 | *buf++ = '"'; |
| 1309 | *buf = '\0'; |
| 1310 | } |
| 1311 | |
| 1312 | // the modifiers that are simple flags |
| 1313 | for (i = 0; mod_entries[i].name != NULL; ++i) |
| 1314 | if (cmod->cmod_flags & mod_entries[i].flag) |
| 1315 | result += add_cmd_modifier(buf, mod_entries[i].name, &multi_mods); |
| 1316 | |
| 1317 | // :silent |
| 1318 | if (cmod->cmod_flags & CMOD_SILENT) |
| 1319 | result += add_cmd_modifier(buf, |
| 1320 | (cmod->cmod_flags & CMOD_ERRSILENT) ? "silent!" |
| 1321 | : "silent", &multi_mods); |
| 1322 | // :verbose |
| 1323 | if (p_verbose > 0) |
| 1324 | result += add_cmd_modifier(buf, "verbose", &multi_mods); |
| 1325 | // flags from cmod->cmod_split |
| 1326 | result += add_win_cmd_modifers(buf, cmod, &multi_mods); |
| 1327 | if (quote && buf != NULL) |
| 1328 | { |
| 1329 | buf += result - 2; |
| 1330 | *buf = '"'; |
| 1331 | } |
| 1332 | return result; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1336 | * Check for a <> code in a user command. |
| 1337 | * "code" points to the '<'. "len" the length of the <> (inclusive). |
| 1338 | * "buf" is where the result is to be added. |
| 1339 | * "split_buf" points to a buffer used for splitting, caller should free it. |
| 1340 | * "split_len" is the length of what "split_buf" contains. |
| 1341 | * Returns the length of the replacement, which has been added to "buf". |
| 1342 | * Returns -1 if there was no match, and only the "<" has been copied. |
| 1343 | */ |
| 1344 | static size_t |
| 1345 | uc_check_code( |
| 1346 | char_u *code, |
| 1347 | size_t len, |
| 1348 | char_u *buf, |
| 1349 | ucmd_T *cmd, // the user command we're expanding |
| 1350 | exarg_T *eap, // ex arguments |
| 1351 | char_u **split_buf, |
| 1352 | size_t *split_len) |
| 1353 | { |
| 1354 | size_t result = 0; |
| 1355 | char_u *p = code + 1; |
| 1356 | size_t l = len - 2; |
| 1357 | int quote = 0; |
| 1358 | enum { |
| 1359 | ct_ARGS, |
| 1360 | ct_BANG, |
| 1361 | ct_COUNT, |
| 1362 | ct_LINE1, |
| 1363 | ct_LINE2, |
| 1364 | ct_RANGE, |
| 1365 | ct_MODS, |
| 1366 | ct_REGISTER, |
| 1367 | ct_LT, |
| 1368 | ct_NONE |
| 1369 | } type = ct_NONE; |
| 1370 | |
| 1371 | if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-') |
| 1372 | { |
| 1373 | quote = (*p == 'q' || *p == 'Q') ? 1 : 2; |
| 1374 | p += 2; |
| 1375 | l -= 2; |
| 1376 | } |
| 1377 | |
| 1378 | ++l; |
| 1379 | if (l <= 1) |
| 1380 | type = ct_NONE; |
| 1381 | else if (STRNICMP(p, "args>", l) == 0) |
| 1382 | type = ct_ARGS; |
| 1383 | else if (STRNICMP(p, "bang>", l) == 0) |
| 1384 | type = ct_BANG; |
| 1385 | else if (STRNICMP(p, "count>", l) == 0) |
| 1386 | type = ct_COUNT; |
| 1387 | else if (STRNICMP(p, "line1>", l) == 0) |
| 1388 | type = ct_LINE1; |
| 1389 | else if (STRNICMP(p, "line2>", l) == 0) |
| 1390 | type = ct_LINE2; |
| 1391 | else if (STRNICMP(p, "range>", l) == 0) |
| 1392 | type = ct_RANGE; |
| 1393 | else if (STRNICMP(p, "lt>", l) == 0) |
| 1394 | type = ct_LT; |
| 1395 | else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0) |
| 1396 | type = ct_REGISTER; |
| 1397 | else if (STRNICMP(p, "mods>", l) == 0) |
| 1398 | type = ct_MODS; |
| 1399 | |
| 1400 | switch (type) |
| 1401 | { |
| 1402 | case ct_ARGS: |
| 1403 | // Simple case first |
| 1404 | if (*eap->arg == NUL) |
| 1405 | { |
| 1406 | if (quote == 1) |
| 1407 | { |
| 1408 | result = 2; |
| 1409 | if (buf != NULL) |
| 1410 | STRCPY(buf, "''"); |
| 1411 | } |
| 1412 | else |
| 1413 | result = 0; |
| 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | // When specified there is a single argument don't split it. |
| 1418 | // Works for ":Cmd %" when % is "a b c". |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 1419 | if ((eap->argt & EX_NOSPC) && quote == 2) |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1420 | quote = 1; |
| 1421 | |
| 1422 | switch (quote) |
| 1423 | { |
| 1424 | case 0: // No quoting, no splitting |
| 1425 | result = STRLEN(eap->arg); |
| 1426 | if (buf != NULL) |
| 1427 | STRCPY(buf, eap->arg); |
| 1428 | break; |
| 1429 | case 1: // Quote, but don't split |
| 1430 | result = STRLEN(eap->arg) + 2; |
| 1431 | for (p = eap->arg; *p; ++p) |
| 1432 | { |
| 1433 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2) |
| 1434 | // DBCS can contain \ in a trail byte, skip the |
| 1435 | // double-byte character. |
| 1436 | ++p; |
| 1437 | else |
| 1438 | if (*p == '\\' || *p == '"') |
| 1439 | ++result; |
| 1440 | } |
| 1441 | |
| 1442 | if (buf != NULL) |
| 1443 | { |
| 1444 | *buf++ = '"'; |
| 1445 | for (p = eap->arg; *p; ++p) |
| 1446 | { |
| 1447 | if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2) |
| 1448 | // DBCS can contain \ in a trail byte, copy the |
| 1449 | // double-byte character to avoid escaping. |
| 1450 | *buf++ = *p++; |
| 1451 | else |
| 1452 | if (*p == '\\' || *p == '"') |
| 1453 | *buf++ = '\\'; |
| 1454 | *buf++ = *p; |
| 1455 | } |
| 1456 | *buf = '"'; |
| 1457 | } |
| 1458 | |
| 1459 | break; |
| 1460 | case 2: // Quote and split (<f-args>) |
| 1461 | // This is hard, so only do it once, and cache the result |
| 1462 | if (*split_buf == NULL) |
| 1463 | *split_buf = uc_split_args(eap->arg, split_len); |
| 1464 | |
| 1465 | result = *split_len; |
| 1466 | if (buf != NULL && result != 0) |
| 1467 | STRCPY(buf, *split_buf); |
| 1468 | |
| 1469 | break; |
| 1470 | } |
| 1471 | break; |
| 1472 | |
| 1473 | case ct_BANG: |
| 1474 | result = eap->forceit ? 1 : 0; |
| 1475 | if (quote) |
| 1476 | result += 2; |
| 1477 | if (buf != NULL) |
| 1478 | { |
| 1479 | if (quote) |
| 1480 | *buf++ = '"'; |
| 1481 | if (eap->forceit) |
| 1482 | *buf++ = '!'; |
| 1483 | if (quote) |
| 1484 | *buf = '"'; |
| 1485 | } |
| 1486 | break; |
| 1487 | |
| 1488 | case ct_LINE1: |
| 1489 | case ct_LINE2: |
| 1490 | case ct_RANGE: |
| 1491 | case ct_COUNT: |
| 1492 | { |
| 1493 | char num_buf[20]; |
| 1494 | long num = (type == ct_LINE1) ? eap->line1 : |
| 1495 | (type == ct_LINE2) ? eap->line2 : |
| 1496 | (type == ct_RANGE) ? eap->addr_count : |
| 1497 | (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; |
| 1498 | size_t num_len; |
| 1499 | |
| 1500 | sprintf(num_buf, "%ld", num); |
| 1501 | num_len = STRLEN(num_buf); |
| 1502 | result = num_len; |
| 1503 | |
| 1504 | if (quote) |
| 1505 | result += 2; |
| 1506 | |
| 1507 | if (buf != NULL) |
| 1508 | { |
| 1509 | if (quote) |
| 1510 | *buf++ = '"'; |
| 1511 | STRCPY(buf, num_buf); |
| 1512 | buf += num_len; |
| 1513 | if (quote) |
| 1514 | *buf = '"'; |
| 1515 | } |
| 1516 | |
| 1517 | break; |
| 1518 | } |
| 1519 | |
| 1520 | case ct_MODS: |
| 1521 | { |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1522 | result = produce_cmdmods(buf, &cmdmod, quote); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1523 | break; |
| 1524 | } |
| 1525 | |
| 1526 | case ct_REGISTER: |
| 1527 | result = eap->regname ? 1 : 0; |
| 1528 | if (quote) |
| 1529 | result += 2; |
| 1530 | if (buf != NULL) |
| 1531 | { |
| 1532 | if (quote) |
| 1533 | *buf++ = '\''; |
| 1534 | if (eap->regname) |
| 1535 | *buf++ = eap->regname; |
| 1536 | if (quote) |
| 1537 | *buf = '\''; |
| 1538 | } |
| 1539 | break; |
| 1540 | |
| 1541 | case ct_LT: |
| 1542 | result = 1; |
| 1543 | if (buf != NULL) |
| 1544 | *buf = '<'; |
| 1545 | break; |
| 1546 | |
| 1547 | default: |
| 1548 | // Not recognized: just copy the '<' and return -1. |
| 1549 | result = (size_t)-1; |
| 1550 | if (buf != NULL) |
| 1551 | *buf = '<'; |
| 1552 | break; |
| 1553 | } |
| 1554 | |
| 1555 | return result; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Execute a user defined command. |
| 1560 | */ |
| 1561 | void |
| 1562 | do_ucmd(exarg_T *eap) |
| 1563 | { |
| 1564 | char_u *buf; |
| 1565 | char_u *p; |
| 1566 | char_u *q; |
| 1567 | |
| 1568 | char_u *start; |
| 1569 | char_u *end = NULL; |
| 1570 | char_u *ksp; |
| 1571 | size_t len, totlen; |
| 1572 | |
| 1573 | size_t split_len = 0; |
| 1574 | char_u *split_buf = NULL; |
| 1575 | ucmd_T *cmd; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1576 | sctx_T save_current_sctx = current_sctx; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1577 | |
| 1578 | if (eap->cmdidx == CMD_USER) |
| 1579 | cmd = USER_CMD(eap->useridx); |
| 1580 | else |
| 1581 | cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx); |
| 1582 | |
| 1583 | /* |
| 1584 | * Replace <> in the command by the arguments. |
| 1585 | * First round: "buf" is NULL, compute length, allocate "buf". |
| 1586 | * Second round: copy result into "buf". |
| 1587 | */ |
| 1588 | buf = NULL; |
| 1589 | for (;;) |
| 1590 | { |
| 1591 | p = cmd->uc_rep; // source |
| 1592 | q = buf; // destination |
| 1593 | totlen = 0; |
| 1594 | |
| 1595 | for (;;) |
| 1596 | { |
| 1597 | start = vim_strchr(p, '<'); |
| 1598 | if (start != NULL) |
| 1599 | end = vim_strchr(start + 1, '>'); |
| 1600 | if (buf != NULL) |
| 1601 | { |
| 1602 | for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp) |
| 1603 | ; |
| 1604 | if (*ksp == K_SPECIAL |
| 1605 | && (start == NULL || ksp < start || end == NULL) |
| 1606 | && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER) |
| 1607 | # ifdef FEAT_GUI |
| 1608 | || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI) |
| 1609 | # endif |
| 1610 | )) |
| 1611 | { |
| 1612 | // K_SPECIAL has been put in the buffer as K_SPECIAL |
| 1613 | // KS_SPECIAL KE_FILLER, like for mappings, but |
| 1614 | // do_cmdline() doesn't handle that, so convert it back. |
| 1615 | // Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. |
| 1616 | len = ksp - p; |
| 1617 | if (len > 0) |
| 1618 | { |
| 1619 | mch_memmove(q, p, len); |
| 1620 | q += len; |
| 1621 | } |
| 1622 | *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI; |
| 1623 | p = ksp + 3; |
| 1624 | continue; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // break if no <item> is found |
| 1629 | if (start == NULL || end == NULL) |
| 1630 | break; |
| 1631 | |
| 1632 | // Include the '>' |
| 1633 | ++end; |
| 1634 | |
| 1635 | // Take everything up to the '<' |
| 1636 | len = start - p; |
| 1637 | if (buf == NULL) |
| 1638 | totlen += len; |
| 1639 | else |
| 1640 | { |
| 1641 | mch_memmove(q, p, len); |
| 1642 | q += len; |
| 1643 | } |
| 1644 | |
| 1645 | len = uc_check_code(start, end - start, q, cmd, eap, |
| 1646 | &split_buf, &split_len); |
| 1647 | if (len == (size_t)-1) |
| 1648 | { |
| 1649 | // no match, continue after '<' |
| 1650 | p = start + 1; |
| 1651 | len = 1; |
| 1652 | } |
| 1653 | else |
| 1654 | p = end; |
| 1655 | if (buf == NULL) |
| 1656 | totlen += len; |
| 1657 | else |
| 1658 | q += len; |
| 1659 | } |
| 1660 | if (buf != NULL) // second time here, finished |
| 1661 | { |
| 1662 | STRCPY(q, p); |
| 1663 | break; |
| 1664 | } |
| 1665 | |
| 1666 | totlen += STRLEN(p); // Add on the trailing characters |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1667 | buf = alloc(totlen + 1); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1668 | if (buf == NULL) |
| 1669 | { |
| 1670 | vim_free(split_buf); |
| 1671 | return; |
| 1672 | } |
| 1673 | } |
| 1674 | |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 1675 | current_sctx.sc_version = cmd->uc_script_ctx.sc_version; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1676 | #ifdef FEAT_EVAL |
| 1677 | current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1678 | #endif |
| 1679 | (void)do_cmdline(buf, eap->getline, eap->cookie, |
| 1680 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1681 | current_sctx = save_current_sctx; |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1682 | vim_free(buf); |
| 1683 | vim_free(split_buf); |
| 1684 | } |