robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e02755cd9f
master
1// usercmd.c: User defined command support 2 3#include <assert.h> 4#include <inttypes.h> 5#include <lauxlib.h> 6#include <stdbool.h> 7#include <stdio.h> 8#include <string.h> 9 10#include "auto/config.h" 11#include "nvim/api/private/helpers.h" 12#include "nvim/ascii_defs.h" 13#include "nvim/buffer_defs.h" 14#include "nvim/charset.h" 15#include "nvim/cmdexpand_defs.h" 16#include "nvim/eval.h" 17#include "nvim/ex_cmds_defs.h" 18#include "nvim/ex_docmd.h" 19#include "nvim/garray.h" 20#include "nvim/gettext_defs.h" 21#include "nvim/globals.h" 22#include "nvim/highlight_defs.h" 23#include "nvim/keycodes.h" 24#include "nvim/lua/executor.h" 25#include "nvim/macros_defs.h" 26#include "nvim/mapping.h" 27#include "nvim/mbyte.h" 28#include "nvim/memory.h" 29#include "nvim/memory_defs.h" 30#include "nvim/menu.h" 31#include "nvim/message.h" 32#include "nvim/option_vars.h" 33#include "nvim/os/input.h" 34#include "nvim/runtime.h" 35#include "nvim/strings.h" 36#include "nvim/usercmd.h" 37#include "nvim/vim_defs.h" 38#include "nvim/window.h" 39 40#include "usercmd.c.generated.h" 41 42garray_T ucmds = {0 ,0 ,sizeof (ucmd_T ),4 ,NULL }; 43 44static const char e_argument_required_for_str [] 45= N_ ("E179: Argument required for %s" ); 46static const char e_no_such_user_defined_command_str [] 47= N_ ("E184: No such user-defined command: %s" ); 48static const char e_complete_used_without_allowing_arguments [] 49= N_ ("E1208: -complete used without allowing arguments" ); 50static const char e_no_such_user_defined_command_in_current_buffer_str [] 51= N_ ("E1237: No such user-defined command in current buffer: %s" ); 52 53/// List of names for completion for ":command" with the EXPAND_ flag. 54/// Must be alphabetical for completion. 55static const char * command_complete []= { 56 [EXPAND_ARGLIST ]= "arglist" , 57 [EXPAND_AUGROUP ]= "augroup" , 58 [EXPAND_BUFFERS ]= "buffer" , 59 [EXPAND_CHECKHEALTH ]= "checkhealth" , 60 [EXPAND_COLORS ]= "color" , 61 [EXPAND_COMMANDS ]= "command" , 62 [EXPAND_COMPILER ]= "compiler" , 63 [EXPAND_USER_DEFINED ]= "custom" , 64 [EXPAND_USER_LIST ]= "customlist" , 65 [EXPAND_USER_LUA ]= "<Lua function>" , 66 [EXPAND_DIFF_BUFFERS ]= "diff_buffer" , 67 [EXPAND_DIRECTORIES ]= "dir" , 68 [EXPAND_ENV_VARS ]= "environment" , 69 [EXPAND_EVENTS ]= "event" , 70 [EXPAND_EXPRESSION ]= "expression" , 71 [EXPAND_FILES ]= "file" , 72 [EXPAND_FILES_IN_PATH ]= "file_in_path" , 73 [EXPAND_FILETYPE ]= "filetype" , 74 [EXPAND_FILETYPECMD ]= "filetypecmd" , 75 [EXPAND_FUNCTIONS ]= "function" , 76 [EXPAND_HELP ]= "help" , 77 [EXPAND_HIGHLIGHT ]= "highlight" , 78 [EXPAND_HISTORY ]= "history" , 79 [EXPAND_KEYMAP ]= "keymap" , 80 [EXPAND_LOCALES ]= "locale" , 81 [EXPAND_LUA ]= "lua" , 82 [EXPAND_MAPCLEAR ]= "mapclear" , 83 [EXPAND_MAPPINGS ]= "mapping" , 84 [EXPAND_MENUS ]= "menu" , 85 [EXPAND_MESSAGES ]= "messages" , 86 [EXPAND_OWNSYNTAX ]= "syntax" , 87 [EXPAND_SYNTIME ]= "syntime" , 88 [EXPAND_SETTINGS ]= "option" , 89 [EXPAND_PACKADD ]= "packadd" , 90 [EXPAND_RETAB ]= "retab" , 91 [EXPAND_RUNTIME ]= "runtime" , 92 [EXPAND_SHELLCMD ]= "shellcmd" , 93 [EXPAND_SHELLCMDLINE ]= "shellcmdline" , 94 [EXPAND_SIGN ]= "sign" , 95 [EXPAND_TAGS ]= "tag" , 96 [EXPAND_TAGS_LISTFILES ]= "tag_listfiles" , 97 [EXPAND_USER ]= "user" , 98 [EXPAND_USER_VARS ]= "var" , 99 [EXPAND_BREAKPOINT ]= "breakpoint" , 100 [EXPAND_SCRIPTNAMES ]= "scriptnames" , 101 [EXPAND_DIRS_IN_CDPATH ]= "dir_in_path" , 102}; 103 104/// List of names of address types. Must be alphabetical for completion. 105static struct { 106cmd_addr_T expand ; 107char * name ; 108char * shortname ; 109}addr_type_complete []= { 110 {ADDR_ARGUMENTS ,"arguments" ,"arg" }, 111 {ADDR_LINES ,"lines" ,"line" }, 112 {ADDR_LOADED_BUFFERS ,"loaded_buffers" ,"load" }, 113 {ADDR_TABS ,"tabs" ,"tab" }, 114 {ADDR_BUFFERS ,"buffers" ,"buf" }, 115 {ADDR_WINDOWS ,"windows" ,"win" }, 116 {ADDR_QUICKFIX ,"quickfix" ,"qf" }, 117 {ADDR_OTHER ,"other" ,"?" }, 118 {ADDR_NONE ,NULL ,NULL } 119}; 120 121/// Search for a user command that matches "eap->cmd". 122/// Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". 123/// Return a pointer to just after the command. 124/// Return NULL if there is no matching command. 125/// 126/// @param *p end of the command (possibly including count) 127/// @param full set to true for a full match 128/// @param xp used for completion, NULL otherwise 129/// @param complp completion flags or NULL 130char * find_ucmd (exarg_T * eap ,char * p ,int * full ,expand_T * xp ,int * complp ) 131{ 132int len = (int )(p - eap -> cmd ); 133int matchlen = 0 ; 134bool found = false; 135bool possible = false; 136bool amb_local = false;// Found ambiguous buffer-local command, 137// only full match global is accepted. 138 139// Look for buffer-local user commands first, then global ones. 140garray_T * gap = & prevwin_curwin ()-> w_buffer -> b_ucmds ; 141while (true) { 142int j ; 143for (j = 0 ;j < gap -> ga_len ;j ++ ) { 144ucmd_T * uc = USER_CMD_GA (gap ,j ); 145char * cp = eap -> cmd ; 146char * np = uc -> uc_name ; 147int k = 0 ; 148while (k < len && * np != NUL && * cp ++ == * np ++ ) { 149k ++ ; 150 } 151if (k == len || (* np == NUL && ascii_isdigit (eap -> cmd [k ]))) { 152// If finding a second match, the command is ambiguous. But 153// not if a buffer-local command wasn't a full match and a 154// global command is a full match. 155if (k == len && found && * np != NUL ) { 156if (gap == & ucmds ) { 157return NULL ; 158 } 159amb_local = true; 160 } 161 162if (!found || (k == len && * np == NUL )) { 163// If we matched up to a digit, then there could 164// be another command including the digit that we 165// should use instead. 166if (k == len ) { 167found = true; 168 }else { 169possible = true; 170 } 171 172if (gap == & ucmds ) { 173eap -> cmdidx = CMD_USER ; 174 }else { 175eap -> cmdidx = CMD_USER_BUF ; 176 } 177eap -> argt = uc -> uc_argt ; 178eap -> useridx = j ; 179eap -> addr_type = uc -> uc_addr_type ; 180 181if (complp != NULL ) { 182* complp = uc -> uc_compl ; 183 } 184if (xp != NULL ) { 185xp -> xp_luaref = uc -> uc_compl_luaref ; 186xp -> xp_arg = uc -> uc_compl_arg ; 187xp -> xp_script_ctx = uc -> uc_script_ctx ; 188xp -> xp_script_ctx .sc_lnum += SOURCING_LNUM ; 189 } 190// Do not search for further abbreviations 191// if this is an exact match. 192matchlen = k ; 193if (k == len && * np == NUL ) { 194if (full != NULL ) { 195* full = true; 196 } 197amb_local = false; 198break ; 199 } 200 } 201 } 202 } 203 204// Stop if we found a full match or searched all. 205if (j < gap -> ga_len || gap == & ucmds ) { 206break ; 207 } 208gap = & ucmds ; 209 } 210 211// Only found ambiguous matches. 212if (amb_local ) { 213if (xp != NULL ) { 214xp -> xp_context = EXPAND_UNSUCCESSFUL ; 215 } 216return NULL ; 217 } 218 219// The match we found may be followed immediately by a number. Move "p" 220// back to point to it. 221if (found || possible ) { 222return p + (matchlen - len ); 223 } 224return p ; 225} 226 227/// Set completion context for :command 228const char * set_context_in_user_cmd (expand_T * xp ,const char * arg_in ) 229{ 230const char * arg = arg_in ; 231const char * p ; 232 233// Check for attributes 234while (* arg == '-' ) { 235arg ++ ;// Skip "-". 236p = skiptowhite (arg ); 237if (* p == NUL ) { 238// Cursor is still in the attribute. 239p = strchr (arg ,'=' ); 240if (p == NULL ) { 241// No "=", so complete attribute names. 242xp -> xp_context = EXPAND_USER_CMD_FLAGS ; 243xp -> xp_pattern = (char * )arg ; 244return NULL ; 245 } 246 247// For the -complete, -nargs and -addr attributes, we complete 248// their arguments as well. 249if (STRNICMP (arg ,"complete" , (size_t )(p - arg ))== 0 ) { 250xp -> xp_context = EXPAND_USER_COMPLETE ; 251xp -> xp_pattern = (char * )p + 1 ; 252return NULL ; 253 }else if (STRNICMP (arg ,"nargs" , (size_t )(p - arg ))== 0 ) { 254xp -> xp_context = EXPAND_USER_NARGS ; 255xp -> xp_pattern = (char * )p + 1 ; 256return NULL ; 257 }else if (STRNICMP (arg ,"addr" , (size_t )(p - arg ))== 0 ) { 258xp -> xp_context = EXPAND_USER_ADDR_TYPE ; 259xp -> xp_pattern = (char * )p + 1 ; 260return NULL ; 261 } 262return NULL ; 263 } 264arg = skipwhite (p ); 265 } 266 267// After the attributes comes the new command name. 268p = skiptowhite (arg ); 269if (* p == NUL ) { 270xp -> xp_context = EXPAND_USER_COMMANDS ; 271xp -> xp_pattern = (char * )arg ; 272return NULL ; 273 } 274 275// And finally comes a normal command. 276return skipwhite (p ); 277} 278 279/// Set the completion context for the argument of a user defined command. 280const char * set_context_in_user_cmdarg (const char * cmd FUNC_ATTR_UNUSED ,const char * arg , 281uint32_t argt ,int context ,expand_T * xp ,bool forceit ) 282{ 283if (context == EXPAND_NOTHING ) { 284return NULL ; 285 } 286 287if (argt & EX_XFILE ) { 288// EX_XFILE: file names are handled before this call. 289return NULL ; 290 } 291 292if (context == EXPAND_MENUS ) { 293return set_context_in_menu_cmd (xp ,cmd , (char * )arg ,forceit ); 294 } 295if (context == EXPAND_COMMANDS ) { 296return arg ; 297 } 298if (context == EXPAND_MAPPINGS ) { 299return set_context_in_map_cmd (xp ,"map" , (char * )arg ,forceit , false, false, 300CMD_map ); 301 } 302// Find start of last argument. 303if (!(argt & EX_ARGSPACE )) { 304const char * p = arg ; 305while (* p ) { 306if (* p == ' ' ) { 307// argument starts after a space 308arg = p + 1 ; 309 }else if (* p == '\\' && * (p + 1 )!= NUL ) { 310p ++ ;// skip over escaped character 311 } 312MB_PTR_ADV (p ); 313 } 314 } 315xp -> xp_pattern = (char * )arg ; 316xp -> xp_context = context ; 317 318return NULL ; 319} 320 321char * expand_user_command_name (int idx ) 322{ 323return get_user_commands (NULL ,idx - CMD_SIZE ); 324} 325 326/// Function given to ExpandGeneric() to obtain the list of user command names. 327char * get_user_commands (expand_T * xp FUNC_ATTR_UNUSED ,int idx ) 328FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 329{ 330// In cmdwin, the alternative buffer should be used. 331const buf_T * const buf = prevwin_curwin ()-> w_buffer ; 332 333if (idx < buf -> b_ucmds .ga_len ) { 334return USER_CMD_GA (& buf -> b_ucmds ,idx )-> uc_name ; 335 } 336 337idx -= buf -> b_ucmds .ga_len ; 338if (idx < ucmds .ga_len ) { 339char * name = USER_CMD (idx )-> uc_name ; 340 341for (int i = 0 ;i < buf -> b_ucmds .ga_len ;i ++ ) { 342if (strcmp (name ,USER_CMD_GA (& buf -> b_ucmds ,i )-> uc_name )== 0 ) { 343// global command is overruled by buffer-local one 344return "" ; 345 } 346 } 347return name ; 348 } 349return NULL ; 350} 351 352/// Get the name of user command "idx". "cmdidx" can be CMD_USER or 353/// CMD_USER_BUF. 354/// 355/// @return NULL if the command is not found. 356char * get_user_command_name (int idx ,int cmdidx ) 357{ 358if (cmdidx == CMD_USER && idx < ucmds .ga_len ) { 359return USER_CMD (idx )-> uc_name ; 360 } 361if (cmdidx == CMD_USER_BUF ) { 362// In cmdwin, the alternative buffer should be used. 363const buf_T * const buf = prevwin_curwin ()-> w_buffer ; 364 365if (idx < buf -> b_ucmds .ga_len ) { 366return USER_CMD_GA (& buf -> b_ucmds ,idx )-> uc_name ; 367 } 368 } 369return NULL ; 370} 371 372/// Function given to ExpandGeneric() to obtain the list of user address type names. 373char * get_user_cmd_addr_type (expand_T * xp ,int idx ) 374{ 375return addr_type_complete [idx ].name ; 376} 377 378/// Function given to ExpandGeneric() to obtain the list of user command 379/// attributes. 380char * get_user_cmd_flags (expand_T * xp ,int idx ) 381{ 382static char * user_cmd_flags []= {"addr" ,"bang" ,"bar" , 383"buffer" ,"complete" ,"count" , 384"nargs" ,"range" ,"register" , 385"keepscript" }; 386 387if (idx >= (int )ARRAY_SIZE (user_cmd_flags )) { 388return NULL ; 389 } 390return user_cmd_flags [idx ]; 391} 392 393/// Function given to ExpandGeneric() to obtain the list of values for -nargs. 394char * get_user_cmd_nargs (expand_T * xp ,int idx ) 395{ 396static char * user_cmd_nargs []= {"0" ,"1" ,"_" ,"*" ,"?" ,"+" }; 397 398if (idx >= (int )ARRAY_SIZE (user_cmd_nargs )) { 399return NULL ; 400 } 401return user_cmd_nargs [idx ]; 402} 403 404static char * get_command_complete (int arg ) 405{ 406if (arg < 0 || arg >= (int )(ARRAY_SIZE (command_complete ))) { 407return NULL ; 408 } 409return (char * )command_complete [arg ]; 410} 411 412/// Function given to ExpandGeneric() to obtain the list of values for -complete. 413char * get_user_cmd_complete (expand_T * xp ,int idx ) 414{ 415if (idx >= (int )ARRAY_SIZE (command_complete )) { 416return NULL ; 417 } 418char * cmd_compl = get_command_complete (idx ); 419if (cmd_compl == NULL || idx == EXPAND_USER_LUA ) { 420return "" ; 421 } 422return cmd_compl ; 423} 424 425/// Get the name of completion type "expand" as an allocated string. 426/// "compl_arg" is the function name for "custom" and "customlist" types. 427/// Returns NULL if no completion is available. 428char * cmdcomplete_type_to_str (int expand ,const char * compl_arg ) 429{ 430char * cmd_compl = get_command_complete (expand ); 431if (cmd_compl == NULL || expand == EXPAND_USER_LUA ) { 432return NULL ; 433 } 434 435if (expand == EXPAND_USER_LIST || expand == EXPAND_USER_DEFINED ) { 436size_t buflen = strlen (cmd_compl )+ strlen (compl_arg )+ 2 ; 437char * buffer = xmalloc (buflen ); 438snprintf (buffer ,buflen ,"%s,%s" ,cmd_compl ,compl_arg ); 439return buffer ; 440 } 441 442return xstrdup (cmd_compl ); 443} 444 445int cmdcomplete_str_to_type (const char * complete_str ) 446{ 447if (strncmp (complete_str ,"custom," ,7 )== 0 ) { 448return EXPAND_USER_DEFINED ; 449 } 450if (strncmp (complete_str ,"customlist," ,11 )== 0 ) { 451return EXPAND_USER_LIST ; 452 } 453 454for (int i = 0 ;i < (int )(ARRAY_SIZE (command_complete ));i ++ ) { 455char * cmd_compl = get_command_complete (i ); 456if (cmd_compl == NULL ) { 457continue ; 458 } 459if (strcmp (complete_str ,command_complete [i ])== 0 ) { 460return i ; 461 } 462 } 463 464return EXPAND_NOTHING ; 465} 466 467static void uc_list (char * name ,size_t name_len ) 468{ 469bool found = false; 470 471msg_ext_set_kind ("list_cmd" ); 472// In cmdwin, the alternative buffer should be used. 473const garray_T * gap = & prevwin_curwin ()-> w_buffer -> b_ucmds ; 474while (true) { 475int i ; 476for (i = 0 ;i < gap -> ga_len ;i ++ ) { 477ucmd_T * cmd = USER_CMD_GA (gap ,i ); 478uint32_t a = cmd -> uc_argt ; 479 480// Skip commands which don't match the requested prefix and 481// commands filtered out. 482if (strncmp (name ,cmd -> uc_name ,name_len )!= 0 483|| message_filtered (cmd -> uc_name )) { 484continue ; 485 } 486 487// Put out the title first time 488if (!found ) { 489msg_puts_title (_ ("\n Name Args Address Complete Definition" )); 490 } 491found = true; 492msg_putchar ('\n' ); 493if (got_int ) { 494break ; 495 } 496 497// Special cases 498size_t len = 4 ; 499if (a & EX_BANG ) { 500msg_putchar ('!' ); 501len -- ; 502 } 503if (a & EX_REGSTR ) { 504msg_putchar ('"' ); 505len -- ; 506 } 507if (gap != & ucmds ) { 508msg_putchar ('b' ); 509len -- ; 510 } 511if (a & EX_TRLBAR ) { 512msg_putchar ('|' ); 513len -- ; 514 } 515if (len != 0 ) { 516msg_puts (& " " [4 - len ]); 517 } 518 519msg_outtrans (cmd -> uc_name ,HLF_D , false); 520len = strlen (cmd -> uc_name )+ 4 ; 521 522if (len < 21 ) { 523// Field padding spaces 12345678901234567 524static char spaces [18 ]= " " ; 525msg_puts (& spaces [len - 4 ]); 526len = 21 ; 527 } 528msg_putchar (' ' ); 529len ++ ; 530 531// "over" is how much longer the name is than the column width for 532// the name, we'll try to align what comes after. 533const int64_t over = (int64_t )len - 22 ; 534len = 0 ; 535 536// Arguments 537switch (a & (EX_EXTRA |EX_NOSPC |EX_NEEDARG |EX_ARGSPACE )) { 538case 0 : 539IObuff [len ++ ]= '0' ; 540break ; 541case (EX_EXTRA ): 542IObuff [len ++ ]= '*' ; 543break ; 544case (EX_EXTRA |EX_NOSPC ): 545IObuff [len ++ ]= '?' ; 546break ; 547case (EX_EXTRA |EX_NEEDARG ): 548IObuff [len ++ ]= '+' ; 549break ; 550case (EX_EXTRA |EX_NOSPC |EX_NEEDARG ): 551IObuff [len ++ ]= '1' ; 552break ; 553case (EX_EXTRA |EX_NOSPC |EX_NEEDARG |EX_ARGSPACE ): 554IObuff [len ++ ]= '_' ; 555break ; 556 } 557 558do { 559IObuff [len ++ ]= ' ' ; 560 }while ((int64_t )len < 5 - over ); 561 562// Address / Range 563if (a & (EX_RANGE |EX_COUNT )) { 564if (a & EX_COUNT ) { 565// -count=N 566int rc = snprintf (IObuff + len ,IOSIZE - len ,"%" PRId64 "c" ,cmd -> uc_def ); 567assert (rc > 0 ); 568len += (size_t )rc ; 569 }else if (a & EX_DFLALL ) { 570IObuff [len ++ ]= '%' ; 571 }else if (cmd -> uc_def >=0 ) { 572// -range=N 573int rc = snprintf (IObuff + len ,IOSIZE - len ,"%" PRId64 "" ,cmd -> uc_def ); 574assert (rc > 0 ); 575len += (size_t )rc ; 576 }else { 577IObuff [len ++ ]= '.' ; 578 } 579 } 580 581do { 582IObuff [len ++ ]= ' ' ; 583 }while ((int64_t )len < 8 - over ); 584 585// Address Type 586for (int j = 0 ;addr_type_complete [j ].expand != ADDR_NONE ;j ++ ) { 587if (addr_type_complete [j ].expand != ADDR_LINES 588&& addr_type_complete [j ].expand == cmd -> uc_addr_type ) { 589int rc = snprintf (IObuff + len ,IOSIZE - len ,"%s" ,addr_type_complete [j ].shortname ); 590assert (rc > 0 ); 591len += (size_t )rc ; 592break ; 593 } 594 } 595 596do { 597IObuff [len ++ ]= ' ' ; 598 }while ((int64_t )len < 13 - over ); 599 600// Completion 601char * cmd_compl = get_command_complete (cmd -> uc_compl ); 602if (cmd_compl != NULL ) { 603int rc = snprintf (IObuff + len ,IOSIZE - len ,"%s" ,get_command_complete (cmd -> uc_compl )); 604assert (rc > 0 ); 605len += (size_t )rc ; 606 } 607 608do { 609IObuff [len ++ ]= ' ' ; 610 }while ((int64_t )len < 25 - over ); 611 612IObuff [len ]= NUL ; 613msg_outtrans (IObuff ,0 , false); 614 615if (cmd -> uc_luaref != LUA_NOREF ) { 616char * fn = nlua_funcref_str (cmd -> uc_luaref ,NULL , true); 617msg_puts_hl (fn ,HLF_8 , false); 618xfree (fn ); 619// put the description on a new line 620if (* cmd -> uc_rep != NUL ) { 621msg_puts ("\n " ); 622 } 623 } 624 625msg_outtrans_special (cmd -> uc_rep , false, 626name_len == 0 ?Columns - 47 :0 ); 627if (p_verbose > 0 ) { 628last_set_msg (cmd -> uc_script_ctx ); 629 } 630line_breakcheck (); 631if (got_int ) { 632break ; 633 } 634 } 635if (gap == & ucmds || i < gap -> ga_len ) { 636break ; 637 } 638gap = & ucmds ; 639 } 640 641if (!found ) { 642msg (_ ("No user-defined commands found" ),0 ); 643 } 644} 645 646/// Parse address type argument 647int parse_addr_type_arg (char * value ,int vallen ,cmd_addr_T * addr_type_arg ) 648FUNC_ATTR_NONNULL_ALL 649{ 650int i ; 651 652for (i = 0 ;addr_type_complete [i ].expand != ADDR_NONE ;i ++ ) { 653int a = (int )strlen (addr_type_complete [i ].name )== vallen ; 654int b = strncmp (value ,addr_type_complete [i ].name , (size_t )vallen )== 0 ; 655if (a && b ) { 656* addr_type_arg = addr_type_complete [i ].expand ; 657break ; 658 } 659 } 660 661if (addr_type_complete [i ].expand == ADDR_NONE ) { 662char * err = value ; 663 664for (i = 0 ;err [i ]!= NUL && !ascii_iswhite (err [i ]);i ++ ) {} 665err [i ]= NUL ; 666semsg (_ ("E180: Invalid address type value: %s" ),err ); 667return FAIL ; 668 } 669 670return OK ; 671} 672 673/// Parse a completion argument "value[vallen]". 674/// The detected completion goes in "*complp", argument type in "*argt". 675/// When there is an argument, for function and user defined completion, it's 676/// copied to allocated memory and stored in "*compl_arg". 677/// 678/// @return FAIL if something is wrong. 679int parse_compl_arg (const char * value ,int vallen ,int * complp ,uint32_t * argt ,char ** compl_arg ) 680FUNC_ATTR_NONNULL_ALL 681{ 682const char * arg = NULL ; 683size_t arglen = 0 ; 684int valend = vallen ; 685 686// Look for any argument part - which is the part after any ',' 687for (int i = 0 ;i < vallen ;i ++ ) { 688if (value [i ]== ',' ) { 689arg = (char * )& value [i + 1 ]; 690arglen = (size_t )(vallen - i - 1 ); 691valend = i ; 692break ; 693 } 694 } 695 696int i ; 697for (i = 0 ;i < (int )ARRAY_SIZE (command_complete );i ++ ) { 698if (get_command_complete (i )== NULL ) { 699continue ; 700 } 701if ((int )strlen (command_complete [i ])== valend 702&& strncmp (value ,command_complete [i ], (size_t )valend )== 0 ) { 703* complp = i ; 704if (i == EXPAND_BUFFERS ) { 705* argt |=EX_BUFNAME ; 706 }else if (i == EXPAND_DIRECTORIES || i == EXPAND_FILES 707|| i == EXPAND_SHELLCMDLINE ) { 708* argt |=EX_XFILE ; 709 } 710break ; 711 } 712 } 713 714if (i == (int )ARRAY_SIZE (command_complete )) { 715semsg (_ ("E180: Invalid complete value: %s" ),value ); 716return FAIL ; 717 } 718 719if (* complp != EXPAND_USER_DEFINED && * complp != EXPAND_USER_LIST 720&& arg != NULL ) { 721emsg (_ ("E468: Completion argument only allowed for custom completion" )); 722return FAIL ; 723 } 724 725if ((* complp == EXPAND_USER_DEFINED || * complp == EXPAND_USER_LIST ) 726&& arg == NULL ) { 727emsg (_ ("E467: Custom completion requires a function argument" )); 728return FAIL ; 729 } 730 731if (arg != NULL ) { 732* compl_arg = xstrnsave (arg ,arglen ); 733 } 734return OK ; 735} 736 737static int uc_scan_attr (char * attr ,size_t len ,uint32_t * argt ,int * def ,int * flags ,int * complp , 738char ** compl_arg ,cmd_addr_T * addr_type_arg ) 739FUNC_ATTR_NONNULL_ALL 740{ 741if (len == 0 ) { 742emsg (_ ("E175: No attribute specified" )); 743return FAIL ; 744 } 745 746// First, try the simple attributes (no arguments) 747if (STRNICMP (attr ,"bang" ,len )== 0 ) { 748* argt |=EX_BANG ; 749 }else if (STRNICMP (attr ,"buffer" ,len )== 0 ) { 750* flags |=UC_BUFFER ; 751 }else if (STRNICMP (attr ,"register" ,len )== 0 ) { 752* argt |=EX_REGSTR ; 753 }else if (STRNICMP (attr ,"keepscript" ,len )== 0 ) { 754* argt |=EX_KEEPSCRIPT ; 755 }else if (STRNICMP (attr ,"bar" ,len )== 0 ) { 756* argt |=EX_TRLBAR ; 757 }else { 758char * val = NULL ; 759size_t vallen = 0 ; 760size_t attrlen = len ; 761 762// Look for the attribute name - which is the part before any '=' 763for (int i = 0 ;i < (int )len ;i ++ ) { 764if (attr [i ]== '=' ) { 765val = & attr [i + 1 ]; 766vallen = len - (size_t )i - 1 ; 767attrlen = (size_t )i ; 768break ; 769 } 770 } 771 772if (STRNICMP (attr ,"nargs" ,attrlen )== 0 ) { 773if (vallen == 1 ) { 774if (* val == '0' ) { 775// Do nothing - this is the default; 776 }else if (* val == '1' ) { 777* argt |= (EX_EXTRA |EX_NOSPC |EX_NEEDARG ); 778 }else if (* val == '*' ) { 779* argt |=EX_EXTRA ; 780 }else if (* val == '?' ) { 781* argt |= (EX_EXTRA |EX_NOSPC ); 782 }else if (* val == '+' ) { 783* argt |= (EX_EXTRA |EX_NEEDARG ); 784 }else if (* val == '_' ) { 785* argt |= (EX_EXTRA |EX_NOSPC |EX_NEEDARG |EX_ARGSPACE ); 786 }else { 787 gotowrong_nargs ; 788 } 789 }else { 790wrong_nargs : 791emsg (_ ("E176: Invalid number of arguments" )); 792return FAIL ; 793 } 794 }else if (STRNICMP (attr ,"range" ,attrlen )== 0 ) { 795* argt |=EX_RANGE ; 796if (vallen == 1 && * val == '%' ) { 797* argt |=EX_DFLALL ; 798 }else if (val != NULL ) { 799char * p = val ; 800if (* def >=0 ) { 801two_count : 802emsg (_ ("E177: Count cannot be specified twice" )); 803return FAIL ; 804 } 805 806* def = getdigits_int (& p , true,0 ); 807* argt |=EX_ZEROR ; 808 809if (p != val + vallen || vallen == 0 ) { 810invalid_count : 811emsg (_ ("E178: Invalid default value for count" )); 812return FAIL ; 813 } 814 } 815// default for -range is using buffer lines 816if (* addr_type_arg == ADDR_NONE ) { 817* addr_type_arg = ADDR_LINES ; 818 } 819 }else if (STRNICMP (attr ,"count" ,attrlen )== 0 ) { 820* argt |= (EX_COUNT |EX_ZEROR |EX_RANGE ); 821// default for -count is using any number 822if (* addr_type_arg == ADDR_NONE ) { 823* addr_type_arg = ADDR_OTHER ; 824 } 825 826if (val != NULL ) { 827char * p = val ; 828if (* def >=0 ) { 829 gototwo_count ; 830 } 831 832* def = getdigits_int (& p , true,0 ); 833 834if (p != val + vallen ) { 835 gotoinvalid_count ; 836 } 837 } 838 839* def = MAX (* def ,0 ); 840 }else if (STRNICMP (attr ,"complete" ,attrlen )== 0 ) { 841if (val == NULL ) { 842semsg (_ (e_argument_required_for_str ),"-complete" ); 843return FAIL ; 844 } 845 846if (parse_compl_arg (val , (int )vallen ,complp ,argt ,compl_arg ) 847== FAIL ) { 848return FAIL ; 849 } 850 }else if (STRNICMP (attr ,"addr" ,attrlen )== 0 ) { 851* argt |=EX_RANGE ; 852if (val == NULL ) { 853semsg (_ (e_argument_required_for_str ),"-addr" ); 854return FAIL ; 855 } 856if (parse_addr_type_arg (val , (int )vallen ,addr_type_arg )== FAIL ) { 857return FAIL ; 858 } 859if (* addr_type_arg != ADDR_LINES ) { 860* argt |=EX_ZEROR ; 861 } 862 }else { 863char ch = attr [len ]; 864attr [len ]= NUL ; 865semsg (_ ("E181: Invalid attribute: %s" ),attr ); 866attr [len ]= ch ; 867return FAIL ; 868 } 869 } 870 871return OK ; 872} 873 874/// Check for a valid user command name 875/// 876/// If the given {name} is valid, then a pointer to the end of the valid name is returned. 877/// Otherwise, returns NULL. 878char * uc_validate_name (char * name ) 879{ 880if (ASCII_ISALPHA (* name )) { 881while (ASCII_ISALNUM (* name )) { 882name ++ ; 883 } 884 } 885if (!ends_excmd (* name )&& !ascii_iswhite (* name )) { 886return NULL ; 887 } 888 889return name ; 890} 891 892/// Create a new user command {name}, if one doesn't already exist. 893/// 894/// This function takes ownership of compl_arg, compl_luaref, and luaref. 895/// 896/// @return OK if the command is created, FAIL otherwise. 897int uc_add_command (char * name ,size_t name_len ,const char * rep ,uint32_t argt ,int64_t def , 898int flags ,int context ,char * compl_arg ,LuaRef compl_luaref , 899LuaRef preview_luaref ,cmd_addr_T addr_type ,LuaRef luaref ,const char * desc , 900bool force ) 901FUNC_ATTR_NONNULL_ARG (1 ,3 ) 902{ 903ucmd_T * cmd = NULL ; 904int cmp = 1 ; 905char * rep_buf = NULL ; 906garray_T * gap ; 907 908replace_termcodes (rep ,strlen (rep ),& rep_buf ,0 ,0 ,NULL ,p_cpo ); 909if (rep_buf == NULL ) { 910// Can't replace termcodes - try using the string as is 911rep_buf = xstrdup (rep ); 912 } 913 914// get address of growarray: global or in curbuf 915if (flags & UC_BUFFER ) { 916gap = & curbuf -> b_ucmds ; 917if (gap -> ga_itemsize == 0 ) { 918ga_init (gap , (int )sizeof (ucmd_T ),4 ); 919 } 920 }else { 921gap = & ucmds ; 922 } 923 924int i ; 925 926// Search for the command in the already defined commands. 927for (i = 0 ;i < gap -> ga_len ;i ++ ) { 928cmd = USER_CMD_GA (gap ,i ); 929size_t len = strlen (cmd -> uc_name ); 930cmp = strncmp (name ,cmd -> uc_name ,name_len ); 931if (cmp == 0 ) { 932if (name_len < len ) { 933cmp = -1 ; 934 }else if (name_len > len ) { 935cmp = 1 ; 936 } 937 } 938 939if (cmp == 0 ) { 940// Command can be replaced with "command!" and when sourcing the 941// same script again, but only once. 942if (!force 943&& (cmd -> uc_script_ctx .sc_sid != current_sctx .sc_sid 944|| cmd -> uc_script_ctx .sc_seq == current_sctx .sc_seq )) { 945semsg (_ ("E174: Command already exists: add ! to replace it: %s" ), 946name ); 947 gotofail ; 948 } 949 950XFREE_CLEAR (cmd -> uc_rep ); 951XFREE_CLEAR (cmd -> uc_compl_arg ); 952XFREE_CLEAR (cmd -> uc_desc ); 953NLUA_CLEAR_REF (cmd -> uc_luaref ); 954NLUA_CLEAR_REF (cmd -> uc_compl_luaref ); 955NLUA_CLEAR_REF (cmd -> uc_preview_luaref ); 956break ; 957 } 958 959// Stop as soon as we pass the name to add 960if (cmp < 0 ) { 961break ; 962 } 963 } 964 965// Extend the array unless we're replacing an existing command 966if (cmp != 0 ) { 967ga_grow (gap ,1 ); 968 969char * const p = xstrnsave (name ,name_len ); 970 971cmd = USER_CMD_GA (gap ,i ); 972memmove (cmd + 1 ,cmd , (size_t )(gap -> ga_len - i )* sizeof (ucmd_T )); 973 974gap -> ga_len ++ ; 975 976cmd -> uc_name = p ; 977 } 978 979cmd -> uc_rep = rep_buf ; 980cmd -> uc_desc = (desc != NULL && * desc != NUL ) ?xstrdup (desc ) :NULL ; 981cmd -> uc_argt = argt ; 982cmd -> uc_def = def ; 983cmd -> uc_compl = context ; 984cmd -> uc_script_ctx = current_sctx ; 985cmd -> uc_script_ctx .sc_lnum += SOURCING_LNUM ; 986nlua_set_sctx (& cmd -> uc_script_ctx ); 987cmd -> uc_compl_arg = compl_arg ; 988cmd -> uc_compl_luaref = compl_luaref ; 989cmd -> uc_preview_luaref = preview_luaref ; 990cmd -> uc_addr_type = addr_type ; 991cmd -> uc_luaref = luaref ; 992 993return OK ; 994 995fail : 996xfree (rep_buf ); 997xfree (compl_arg ); 998NLUA_CLEAR_REF (luaref ); 999NLUA_CLEAR_REF (compl_luaref ); 1000NLUA_CLEAR_REF (preview_luaref ); 1001return FAIL ; 1002} 1003 1004/// ":command ..." 1005void ex_command (exarg_T * eap ) 1006{ 1007char * end ; 1008uint32_t argt = 0 ; 1009int def = -1 ; 1010int flags = 0 ; 1011int context = EXPAND_NOTHING ; 1012char * compl_arg = NULL ; 1013cmd_addr_T addr_type_arg = ADDR_NONE ; 1014int has_attr = (eap -> arg [0 ]== '-' ); 1015 1016char * p = eap -> arg ; 1017 1018// Check for attributes 1019while (* p == '-' ) { 1020p ++ ; 1021end = skiptowhite (p ); 1022if (uc_scan_attr (p , (size_t )(end - p ),& argt ,& def ,& flags ,& context ,& compl_arg , 1023& addr_type_arg )== FAIL ) { 1024 gototheend ; 1025 } 1026p = skipwhite (end ); 1027 } 1028 1029// Get the name (if any) and skip to the following argument. 1030char * name = p ; 1031end = uc_validate_name (name ); 1032if (!end ) { 1033emsg (_ ("E182: Invalid command name" )); 1034 gototheend ; 1035 } 1036size_t name_len = (size_t )(end - name ); 1037 1038// If there is nothing after the name, and no attributes were specified, 1039// we are listing commands 1040p = skipwhite (end ); 1041if (!has_attr && ends_excmd (* p )) { 1042uc_list (name ,name_len ); 1043 }else if (!ASCII_ISUPPER (* name )) { 1044emsg (_ ("E183: User defined commands must start with an uppercase letter" )); 1045 }else if (name_len <=4 && strncmp (name ,"Next" ,name_len )== 0 ) { 1046emsg (_ ("E841: Reserved name, cannot be used for user defined command" )); 1047 }else if (context > 0 && (argt & EX_EXTRA )== 0 ) { 1048emsg (_ (e_complete_used_without_allowing_arguments )); 1049 }else { 1050uc_add_command (name ,name_len ,p ,argt ,def ,flags ,context ,compl_arg ,LUA_NOREF ,LUA_NOREF , 1051addr_type_arg ,LUA_NOREF ,NULL ,eap -> forceit ); 1052 1053return ;// success 1054 } 1055 1056theend : 1057xfree (compl_arg ); 1058} 1059 1060/// ":comclear" 1061/// Clear all user commands, global and for current buffer. 1062void ex_comclear (exarg_T * eap ) 1063{ 1064uc_clear (& ucmds ); 1065if (curbuf != NULL ) { 1066uc_clear (& curbuf -> b_ucmds ); 1067 } 1068} 1069 1070void free_ucmd (ucmd_T * cmd ) 1071{ 1072xfree (cmd -> uc_name ); 1073xfree (cmd -> uc_rep ); 1074xfree (cmd -> uc_compl_arg ); 1075xfree (cmd -> uc_desc ); 1076NLUA_CLEAR_REF (cmd -> uc_compl_luaref ); 1077NLUA_CLEAR_REF (cmd -> uc_luaref ); 1078NLUA_CLEAR_REF (cmd -> uc_preview_luaref ); 1079} 1080 1081/// Clear all user commands for "gap". 1082void uc_clear (garray_T * gap ) 1083{ 1084GA_DEEP_CLEAR (gap ,ucmd_T ,free_ucmd ); 1085} 1086 1087void ex_delcommand (exarg_T * eap ) 1088{ 1089int i = 0 ; 1090ucmd_T * cmd = NULL ; 1091int res = -1 ; 1092const char * arg = eap -> arg ; 1093bool buffer_only = false; 1094 1095if (strncmp (arg ,"-buffer" ,7 )== 0 && ascii_iswhite (arg [7 ])) { 1096buffer_only = true; 1097arg = skipwhite (arg + 7 ); 1098 } 1099 1100garray_T * gap = & curbuf -> b_ucmds ; 1101while (true) { 1102for (i = 0 ;i < gap -> ga_len ;i ++ ) { 1103cmd = USER_CMD_GA (gap ,i ); 1104res = strcmp (arg ,cmd -> uc_name ); 1105if (res <=0 ) { 1106break ; 1107 } 1108 } 1109if (gap == & ucmds || res == 0 || buffer_only ) { 1110break ; 1111 } 1112gap = & ucmds ; 1113 } 1114 1115if (res != 0 ) { 1116semsg (_ (buffer_only 1117 ?e_no_such_user_defined_command_in_current_buffer_str 1118 :e_no_such_user_defined_command_str ), 1119arg ); 1120return ; 1121 } 1122 1123free_ucmd (cmd ); 1124 1125gap -> ga_len -- ; 1126 1127if (i < gap -> ga_len ) { 1128memmove (cmd ,cmd + 1 , (size_t )(gap -> ga_len - i )* sizeof (ucmd_T )); 1129 } 1130} 1131 1132/// Split a string by unescaped whitespace (space & tab), used for f-args on Lua commands callback. 1133/// Similar to uc_split_args(), but does not allocate, add quotes, add commas and is an iterator. 1134/// 1135/// @param[in] arg String to split 1136/// @param[in] arglen Length of {arg} 1137/// @param[inout] end Index of last character of previous iteration 1138/// @param[out] buf Buffer to copy string into 1139/// @param[out] len Length of string in {buf} 1140/// 1141/// @return true if iteration is complete, else false 1142bool uc_split_args_iter (const char * arg ,size_t arglen ,size_t * end ,char * buf ,size_t * len ) 1143{ 1144if (!arglen ) { 1145return true; 1146 } 1147 1148size_t pos = * end ; 1149while (pos < arglen && ascii_iswhite (arg [pos ])) { 1150pos ++ ; 1151 } 1152 1153size_t l = 0 ; 1154for (;pos < arglen - 1 ;pos ++ ) { 1155if (arg [pos ]== '\\' && (arg [pos + 1 ]== '\\' || ascii_iswhite (arg [pos + 1 ]))) { 1156buf [l ++ ]= arg [++ pos ]; 1157 }else { 1158buf [l ++ ]= arg [pos ]; 1159 } 1160if (ascii_iswhite (arg [pos + 1 ])) { 1161* end = pos + 1 ; 1162* len = l ; 1163return false; 1164 } 1165 } 1166 1167if (pos < arglen && !ascii_iswhite (arg [pos ])) { 1168buf [l ++ ]= arg [pos ]; 1169 } 1170 1171* len = l ; 1172return true; 1173} 1174 1175size_t uc_nargs_upper_bound (const char * arg ,size_t arglen ) 1176{ 1177bool was_white = true;// space before first arg 1178size_t nargs = 0 ; 1179for (size_t i = 0 ;i < arglen ;i ++ ) { 1180bool is_white = ascii_iswhite (arg [i ]); 1181if (was_white && !is_white ) { 1182nargs ++ ; 1183 } 1184was_white = is_white ; 1185 } 1186return nargs ; 1187} 1188 1189/// split and quote args for <f-args> 1190static char * uc_split_args (const char * arg ,char ** args ,const size_t * arglens ,size_t argc , 1191size_t * lenp ) 1192{ 1193// Precalculate length 1194int len = 2 ;// Initial and final quotes 1195if (args == NULL ) { 1196const char * p = arg ; 1197 1198while (* p ) { 1199if (p [0 ]== '\\' && p [1 ]== '\\' ) { 1200len += 2 ; 1201p += 2 ; 1202 }else if (p [0 ]== '\\' && ascii_iswhite (p [1 ])) { 1203len += 1 ; 1204p += 2 ; 1205 }else if (* p == '\\' || * p == '"' ) { 1206len += 2 ; 1207p += 1 ; 1208 }else if (ascii_iswhite (* p )) { 1209p = skipwhite (p ); 1210if (* p == NUL ) { 1211break ; 1212 } 1213len += 4 ;// ", " 1214 }else { 1215const int charlen = utfc_ptr2len (p ); 1216 1217len += charlen ; 1218p += charlen ; 1219 } 1220 } 1221 }else { 1222for (size_t i = 0 ;i < argc ;i ++ ) { 1223const char * p = args [i ]; 1224const char * arg_end = args [i ]+ arglens [i ]; 1225 1226while (p < arg_end ) { 1227if (* p == '\\' || * p == '"' ) { 1228len += 2 ; 1229p += 1 ; 1230 }else { 1231const int charlen = utfc_ptr2len (p ); 1232 1233len += charlen ; 1234p += charlen ; 1235 } 1236 } 1237 1238if (i != argc - 1 ) { 1239len += 4 ;// ", " 1240 } 1241 } 1242 } 1243 1244char * buf = xmalloc ((size_t )len + 1 ); 1245 1246char * q = buf ; 1247* q ++ = '"' ; 1248 1249if (args == NULL ) { 1250const char * p = arg ; 1251while (* p ) { 1252if (p [0 ]== '\\' && p [1 ]== '\\' ) { 1253* q ++ = '\\' ; 1254* q ++ = '\\' ; 1255p += 2 ; 1256 }else if (p [0 ]== '\\' && ascii_iswhite (p [1 ])) { 1257* q ++ = p [1 ]; 1258p += 2 ; 1259 }else if (* p == '\\' || * p == '"' ) { 1260* q ++ = '\\' ; 1261* q ++ = * p ++ ; 1262 }else if (ascii_iswhite (* p )) { 1263p = skipwhite (p ); 1264if (* p == NUL ) { 1265break ; 1266 } 1267* q ++ = '"' ; 1268* q ++ = ',' ; 1269* q ++ = ' ' ; 1270* q ++ = '"' ; 1271 }else { 1272mb_copy_char (& p ,& q ); 1273 } 1274 } 1275 }else { 1276for (size_t i = 0 ;i < argc ;i ++ ) { 1277const char * p = args [i ]; 1278const char * arg_end = args [i ]+ arglens [i ]; 1279 1280while (p < arg_end ) { 1281if (* p == '\\' || * p == '"' ) { 1282* q ++ = '\\' ; 1283* q ++ = * p ++ ; 1284 }else { 1285mb_copy_char (& p ,& q ); 1286 } 1287 } 1288if (i != argc - 1 ) { 1289* q ++ = '"' ; 1290* q ++ = ',' ; 1291* q ++ = ' ' ; 1292* q ++ = '"' ; 1293 } 1294 } 1295 } 1296 1297* q ++ = '"' ; 1298* q = 0 ; 1299 1300* lenp = (size_t )len ; 1301return buf ; 1302} 1303 1304static size_t add_cmd_modifier (char * buf ,char * mod_str ,bool * multi_mods ) 1305{ 1306size_t result = strlen (mod_str ); 1307if (* multi_mods ) { 1308result ++ ; 1309 } 1310 1311if (buf != NULL ) { 1312if (* multi_mods ) { 1313strcat (buf ," " ); 1314 } 1315strcat (buf ,mod_str ); 1316 } 1317 1318* multi_mods = true; 1319return result ; 1320} 1321 1322/// Add modifiers from "cmod->cmod_split" to "buf". Set "multi_mods" when one 1323/// was added. 1324/// 1325/// @return the number of bytes added 1326size_t add_win_cmd_modifiers (char * buf ,const cmdmod_T * cmod ,bool * multi_mods ) 1327{ 1328size_t result = 0 ; 1329 1330// :aboveleft and :leftabove 1331if (cmod -> cmod_split & WSP_ABOVE ) { 1332result += add_cmd_modifier (buf ,"aboveleft" ,multi_mods ); 1333 } 1334// :belowright and :rightbelow 1335if (cmod -> cmod_split & WSP_BELOW ) { 1336result += add_cmd_modifier (buf ,"belowright" ,multi_mods ); 1337 } 1338// :botright 1339if (cmod -> cmod_split & WSP_BOT ) { 1340result += add_cmd_modifier (buf ,"botright" ,multi_mods ); 1341 } 1342 1343// :tab 1344if (cmod -> cmod_tab > 0 ) { 1345int tabnr = cmod -> cmod_tab - 1 ; 1346if (tabnr == tabpage_index (curtab )) { 1347// For compatibility, don't add a tabpage number if it is the same 1348// as the default number for :tab. 1349result += add_cmd_modifier (buf ,"tab" ,multi_mods ); 1350 }else { 1351char tab_buf [NUMBUFLEN + 3 ]; 1352snprintf (tab_buf ,sizeof (tab_buf ),"%dtab" ,tabnr ); 1353result += add_cmd_modifier (buf ,tab_buf ,multi_mods ); 1354 } 1355 } 1356 1357// :topleft 1358if (cmod -> cmod_split & WSP_TOP ) { 1359result += add_cmd_modifier (buf ,"topleft" ,multi_mods ); 1360 } 1361// :vertical 1362if (cmod -> cmod_split & WSP_VERT ) { 1363result += add_cmd_modifier (buf ,"vertical" ,multi_mods ); 1364 } 1365// :horizontal 1366if (cmod -> cmod_split & WSP_HOR ) { 1367result += add_cmd_modifier (buf ,"horizontal" ,multi_mods ); 1368 } 1369return result ; 1370} 1371 1372/// Generate text for the "cmod" command modifiers. 1373/// If "buf" is NULL just return the length. 1374size_t uc_mods (char * buf ,const cmdmod_T * cmod ,bool quote ) 1375{ 1376size_t result = 0 ; 1377bool multi_mods = false; 1378 1379typedef struct { 1380int flag ; 1381char * name ; 1382 }mod_entry_T ; 1383static mod_entry_T mod_entries []= { 1384 {CMOD_BROWSE ,"browse" }, 1385 {CMOD_CONFIRM ,"confirm" }, 1386 {CMOD_HIDE ,"hide" }, 1387 {CMOD_KEEPALT ,"keepalt" }, 1388 {CMOD_KEEPJUMPS ,"keepjumps" }, 1389 {CMOD_KEEPMARKS ,"keepmarks" }, 1390 {CMOD_KEEPPATTERNS ,"keeppatterns" }, 1391 {CMOD_LOCKMARKS ,"lockmarks" }, 1392 {CMOD_NOSWAPFILE ,"noswapfile" }, 1393 {CMOD_UNSILENT ,"unsilent" }, 1394 {CMOD_NOAUTOCMD ,"noautocmd" }, 1395 {CMOD_SANDBOX ,"sandbox" }, 1396 }; 1397 1398result = quote ?2 :0 ; 1399if (buf != NULL ) { 1400if (quote ) { 1401* buf ++ = '"' ; 1402 } 1403* buf = NUL ; 1404 } 1405 1406// the modifiers that are simple flags 1407for (size_t i = 0 ;i < ARRAY_SIZE (mod_entries );i ++ ) { 1408if (cmod -> cmod_flags & mod_entries [i ].flag ) { 1409result += add_cmd_modifier (buf ,mod_entries [i ].name ,& multi_mods ); 1410 } 1411 } 1412 1413// :silent 1414if (cmod -> cmod_flags & CMOD_SILENT ) { 1415result += add_cmd_modifier (buf , 1416 (cmod -> cmod_flags & CMOD_ERRSILENT ) ?"silent!" :"silent" , 1417& multi_mods ); 1418 } 1419// :verbose 1420if (cmod -> cmod_verbose > 0 ) { 1421int verbose_value = cmod -> cmod_verbose - 1 ; 1422if (verbose_value == 1 ) { 1423result += add_cmd_modifier (buf ,"verbose" ,& multi_mods ); 1424 }else { 1425char verbose_buf [NUMBUFLEN ]; 1426snprintf (verbose_buf ,sizeof (verbose_buf ),"%dverbose" ,verbose_value ); 1427result += add_cmd_modifier (buf ,verbose_buf ,& multi_mods ); 1428 } 1429 } 1430// flags from cmod->cmod_split 1431result += add_win_cmd_modifiers (buf ,cmod ,& multi_mods ); 1432 1433if (quote && buf != NULL ) { 1434buf += result - 2 ; 1435* buf = '"' ; 1436 } 1437return result ; 1438} 1439 1440/// Check for a <> code in a user command. 1441/// 1442/// @param code points to the '<'. "len" the length of the <> (inclusive). 1443/// @param buf is where the result is to be added. 1444/// @param cmd the user command we're expanding 1445/// @param eap ex arguments 1446/// @param split_buf points to a buffer used for splitting, caller should free it. 1447/// @param split_len is the length of what "split_buf" contains. 1448/// 1449/// @return the length of the replacement, which has been added to "buf". 1450/// Return -1 if there was no match, and only the "<" has been copied. 1451static size_t uc_check_code (char * code ,size_t len ,char * buf ,ucmd_T * cmd ,exarg_T * eap , 1452char ** split_buf ,size_t * split_len ) 1453{ 1454size_t result = 0 ; 1455char * p = code + 1 ; 1456size_t l = len - 2 ; 1457int quote = 0 ; 1458enum { 1459ct_ARGS , 1460ct_BANG , 1461ct_COUNT , 1462ct_LINE1 , 1463ct_LINE2 , 1464ct_RANGE , 1465ct_MODS , 1466ct_REGISTER , 1467ct_LT , 1468ct_NONE , 1469 }type = ct_NONE ; 1470 1471if ((vim_strchr ("qQfF" , (uint8_t )(* p ))!= NULL )&& p [1 ]== '-' ) { 1472quote = (* p == 'q' || * p == 'Q' ) ?1 :2 ; 1473p += 2 ; 1474l -= 2 ; 1475 } 1476 1477l ++ ; 1478if (l <=1 ) { 1479// type = ct_NONE; 1480 }else if (STRNICMP (p ,"args>" ,l )== 0 ) { 1481type = ct_ARGS ; 1482 }else if (STRNICMP (p ,"bang>" ,l )== 0 ) { 1483type = ct_BANG ; 1484 }else if (STRNICMP (p ,"count>" ,l )== 0 ) { 1485type = ct_COUNT ; 1486 }else if (STRNICMP (p ,"line1>" ,l )== 0 ) { 1487type = ct_LINE1 ; 1488 }else if (STRNICMP (p ,"line2>" ,l )== 0 ) { 1489type = ct_LINE2 ; 1490 }else if (STRNICMP (p ,"range>" ,l )== 0 ) { 1491type = ct_RANGE ; 1492 }else if (STRNICMP (p ,"lt>" ,l )== 0 ) { 1493type = ct_LT ; 1494 }else if (STRNICMP (p ,"reg>" ,l )== 0 || STRNICMP (p ,"register>" ,l )== 0 ) { 1495type = ct_REGISTER ; 1496 }else if (STRNICMP (p ,"mods>" ,l )== 0 ) { 1497type = ct_MODS ; 1498 } 1499 1500switch (type ) { 1501case ct_ARGS : 1502// Simple case first 1503if (* eap -> arg == NUL ) { 1504if (quote == 1 ) { 1505result = 2 ; 1506if (buf != NULL ) { 1507STRCPY (buf ,"''" ); 1508 } 1509 }else { 1510result = 0 ; 1511 } 1512break ; 1513 } 1514 1515// When specified there is a single argument don't split it. 1516// Works for ":Cmd %" when % is "a b c". 1517if ((eap -> argt & EX_NOSPC )&& quote == 2 ) { 1518quote = 1 ; 1519 } 1520 1521switch (quote ) { 1522case 0 :// No quoting, no splitting 1523result = strlen (eap -> arg ); 1524if (buf != NULL ) { 1525STRCPY (buf ,eap -> arg ); 1526 } 1527break ; 1528case 1 :// Quote, but don't split 1529result = strlen (eap -> arg )+ 2 ; 1530for (p = eap -> arg ;* p ;p ++ ) { 1531if (* p == '\\' || * p == '"' ) { 1532result ++ ; 1533 } 1534 } 1535 1536if (buf != NULL ) { 1537* buf ++ = '"' ; 1538for (p = eap -> arg ;* p ;p ++ ) { 1539if (* p == '\\' || * p == '"' ) { 1540* buf ++ = '\\' ; 1541 } 1542* buf ++ = * p ; 1543 } 1544* buf = '"' ; 1545 } 1546 1547break ; 1548case 2 :// Quote and split (<f-args>) 1549// This is hard, so only do it once, and cache the result 1550if (* split_buf == NULL ) { 1551* split_buf = uc_split_args (eap -> arg ,eap -> args ,eap -> arglens ,eap -> argc ,split_len ); 1552 } 1553 1554result = * split_len ; 1555if (buf != NULL && result != 0 ) { 1556STRCPY (buf ,* split_buf ); 1557 } 1558 1559break ; 1560 } 1561break ; 1562 1563case ct_BANG : 1564result = eap -> forceit ?1 :0 ; 1565if (quote ) { 1566result += 2 ; 1567 } 1568if (buf != NULL ) { 1569if (quote ) { 1570* buf ++ = '"' ; 1571 } 1572if (eap -> forceit ) { 1573* buf ++ = '!' ; 1574 } 1575if (quote ) { 1576* buf = '"' ; 1577 } 1578 } 1579break ; 1580 1581case ct_LINE1 : 1582case ct_LINE2 : 1583case ct_RANGE : 1584case ct_COUNT : { 1585char num_buf [20 ]; 1586int64_t num = type == ct_LINE1 1587 ?eap -> line1 1588 : (type == ct_LINE2 1589 ?eap -> line2 1590 : (type == ct_RANGE 1591 ?eap -> addr_count 1592 : (eap -> addr_count > 0 ?eap -> line2 :cmd -> uc_def ))); 1593size_t num_len ; 1594 1595snprintf (num_buf ,sizeof (num_buf ),"%" PRId64 ,num ); 1596num_len = strlen (num_buf ); 1597result = num_len ; 1598 1599if (quote ) { 1600result += 2 ; 1601 } 1602 1603if (buf != NULL ) { 1604if (quote ) { 1605* buf ++ = '"' ; 1606 } 1607STRCPY (buf ,num_buf ); 1608buf += num_len ; 1609if (quote ) { 1610* buf = '"' ; 1611 } 1612 } 1613 1614break ; 1615 } 1616 1617case ct_MODS : 1618result = uc_mods (buf ,& cmdmod ,quote ); 1619break ; 1620 1621case ct_REGISTER : 1622result = eap -> regname ?1 :0 ; 1623if (quote ) { 1624result += 2 ; 1625 } 1626if (buf != NULL ) { 1627if (quote ) { 1628* buf ++ = '\'' ; 1629 } 1630if (eap -> regname ) { 1631* buf ++ = (char )eap -> regname ; 1632 } 1633if (quote ) { 1634* buf = '\'' ; 1635 } 1636 } 1637break ; 1638 1639case ct_LT : 1640result = 1 ; 1641if (buf != NULL ) { 1642* buf = '<' ; 1643 } 1644break ; 1645 1646default : 1647// Not recognized: just copy the '<' and return -1. 1648result = (size_t )-1 ; 1649if (buf != NULL ) { 1650* buf = '<' ; 1651 } 1652break ; 1653 } 1654 1655return result ; 1656} 1657 1658int do_ucmd (exarg_T * eap ,bool preview ) 1659{ 1660char * end = NULL ; 1661 1662size_t split_len = 0 ; 1663char * split_buf = NULL ; 1664ucmd_T * cmd ; 1665 1666if (eap -> cmdidx == CMD_USER ) { 1667cmd = USER_CMD (eap -> useridx ); 1668 }else { 1669cmd = USER_CMD_GA (& prevwin_curwin ()-> w_buffer -> b_ucmds ,eap -> useridx ); 1670 } 1671 1672if (preview ) { 1673assert (cmd -> uc_preview_luaref > 0 ); 1674return nlua_do_ucmd (cmd ,eap , true); 1675 } 1676 1677if (cmd -> uc_luaref > 0 ) { 1678nlua_do_ucmd (cmd ,eap , false); 1679return 0 ; 1680 } 1681 1682// Replace <> in the command by the arguments. 1683// First round: "buf" is NULL, compute length, allocate "buf". 1684// Second round: copy result into "buf". 1685char * buf = NULL ; 1686while (true) { 1687char * p = cmd -> uc_rep ;// source 1688char * q = buf ;// destination 1689size_t totlen = 0 ; 1690 1691while (true) { 1692char * start = vim_strchr (p ,'<' ); 1693if (start != NULL ) { 1694end = vim_strchr (start + 1 ,'>' ); 1695 } 1696if (buf != NULL ) { 1697char * ksp ; 1698for (ksp = p ;* ksp != NUL && (uint8_t )(* ksp )!= K_SPECIAL ;ksp ++ ) {} 1699if ((uint8_t )(* ksp )== K_SPECIAL 1700&& (start == NULL || ksp < start || end == NULL ) 1701&& ((uint8_t )ksp [1 ]== KS_SPECIAL && ksp [2 ]== KE_FILLER )) { 1702// K_SPECIAL has been put in the buffer as K_SPECIAL 1703// KS_SPECIAL KE_FILLER, like for mappings, but 1704// do_cmdline() doesn't handle that, so convert it back. 1705size_t len = (size_t )(ksp - p ); 1706if (len > 0 ) { 1707memmove (q ,p ,len ); 1708q += len ; 1709 } 1710* q ++ = (char )K_SPECIAL ; 1711p = ksp + 3 ; 1712continue ; 1713 } 1714 } 1715 1716// break if no <item> is found 1717if (start == NULL || end == NULL ) { 1718break ; 1719 } 1720 1721// Include the '>' 1722end ++ ; 1723 1724// Take everything up to the '<' 1725size_t len = (size_t )(start - p ); 1726if (buf == NULL ) { 1727totlen += len ; 1728 }else { 1729memmove (q ,p ,len ); 1730q += len ; 1731 } 1732 1733len = uc_check_code (start , (size_t )(end - start ),q ,cmd ,eap ,& split_buf ,& split_len ); 1734if (len == (size_t )-1 ) { 1735// no match, continue after '<' 1736p = start + 1 ; 1737len = 1 ; 1738 }else { 1739p = end ; 1740 } 1741if (buf == NULL ) { 1742totlen += len ; 1743 }else { 1744q += len ; 1745 } 1746 } 1747if (buf != NULL ) {// second time here, finished 1748STRCPY (q ,p ); 1749break ; 1750 } 1751 1752totlen += strlen (p );// Add on the trailing characters 1753buf = xmalloc (totlen + 1 ); 1754 } 1755 1756sctx_T save_current_sctx ; 1757bool restore_current_sctx = false; 1758if ((cmd -> uc_argt & EX_KEEPSCRIPT )== 0 ) { 1759restore_current_sctx = true; 1760save_current_sctx = current_sctx ; 1761current_sctx .sc_sid = cmd -> uc_script_ctx .sc_sid ; 1762 } 1763do_cmdline (buf ,eap -> ea_getline ,eap -> cookie , 1764DOCMD_VERBOSE |DOCMD_NOWAIT |DOCMD_KEYTYPED ); 1765 1766// Careful: Do not use "cmd" here, it may have become invalid if a user 1767// command was added. 1768if (restore_current_sctx ) { 1769current_sctx = save_current_sctx ; 1770 } 1771xfree (buf ); 1772xfree (split_buf ); 1773 1774return 0 ; 1775} 1776 1777/// Gets a map of maps describing user-commands defined for buffer `buf` or 1778/// defined globally if `buf` is NULL. 1779/// 1780/// @param buf Buffer to inspect, or NULL to get global commands. 1781/// 1782/// @return Map of maps describing commands 1783Dict commands_array (buf_T * buf ,Arena * arena ) 1784{ 1785garray_T * gap = (buf == NULL ) ?& ucmds :& buf -> b_ucmds ; 1786 1787Dict rv = arena_dict (arena , (size_t )gap -> ga_len ); 1788for (int i = 0 ;i < gap -> ga_len ;i ++ ) { 1789char arg [2 ]= {0 ,0 }; 1790Dict d = arena_dict (arena ,16 ); 1791ucmd_T * cmd = USER_CMD_GA (gap ,i ); 1792 1793PUT_C (d ,"name" ,CSTR_AS_OBJ (cmd -> uc_name )); 1794PUT_C (d ,"definition" ,CSTR_AS_OBJ (cmd -> uc_rep )); 1795PUT_C (d ,"desc" ,CSTR_AS_OBJ (cmd -> uc_desc )); 1796PUT_C (d ,"script_id" ,INTEGER_OBJ (cmd -> uc_script_ctx .sc_sid )); 1797PUT_C (d ,"bang" ,BOOLEAN_OBJ (!!(cmd -> uc_argt & EX_BANG ))); 1798PUT_C (d ,"bar" ,BOOLEAN_OBJ (!!(cmd -> uc_argt & EX_TRLBAR ))); 1799PUT_C (d ,"register" ,BOOLEAN_OBJ (!!(cmd -> uc_argt & EX_REGSTR ))); 1800PUT_C (d ,"keepscript" ,BOOLEAN_OBJ (!!(cmd -> uc_argt & EX_KEEPSCRIPT ))); 1801 1802if (cmd -> uc_preview_luaref != LUA_NOREF ) { 1803PUT_C (d ,"preview" ,LUAREF_OBJ (api_new_luaref (cmd -> uc_preview_luaref ))); 1804 } 1805 1806if (cmd -> uc_luaref != LUA_NOREF ) { 1807PUT_C (d ,"callback" ,LUAREF_OBJ (api_new_luaref (cmd -> uc_luaref ))); 1808 } 1809 1810switch (cmd -> uc_argt & (EX_EXTRA |EX_NOSPC |EX_NEEDARG |EX_ARGSPACE )) { 1811case 0 : 1812arg [0 ]= '0' ;break ; 1813case (EX_EXTRA ): 1814arg [0 ]= '*' ;break ; 1815case (EX_EXTRA |EX_NOSPC ): 1816arg [0 ]= '?' ;break ; 1817case (EX_EXTRA |EX_NEEDARG ): 1818arg [0 ]= '+' ;break ; 1819case (EX_EXTRA |EX_NOSPC |EX_NEEDARG ): 1820arg [0 ]= '1' ;break ; 1821case (EX_EXTRA |EX_NOSPC |EX_NEEDARG |EX_ARGSPACE ): 1822arg [0 ]= '_' ;break ; 1823 } 1824PUT_C (d ,"nargs" ,CSTR_TO_ARENA_OBJ (arena ,arg )); 1825 1826if (cmd -> uc_compl_luaref != LUA_NOREF ) { 1827PUT_C (d ,"complete" ,LUAREF_OBJ (api_new_luaref (cmd -> uc_compl_luaref ))); 1828 }else { 1829char * cmd_compl = get_command_complete (cmd -> uc_compl ); 1830PUT_C (d ,"complete" , (cmd_compl == NULL ?NIL :CSTR_AS_OBJ (cmd_compl ))); 1831 } 1832PUT_C (d ,"complete_arg" ,cmd -> uc_compl_arg == NULL 1833 ?NIL :CSTR_AS_OBJ (cmd -> uc_compl_arg )); 1834 1835Object obj = NIL ; 1836if (cmd -> uc_argt & EX_COUNT ) { 1837if (cmd -> uc_def >=0 ) { 1838obj = STRING_OBJ (arena_printf (arena ,"%" PRId64 ,cmd -> uc_def ));// -count=N 1839 }else { 1840obj = CSTR_AS_OBJ ("0" );// -count 1841 } 1842 } 1843PUT_C (d ,"count" ,obj ); 1844 1845obj = NIL ; 1846if (cmd -> uc_argt & EX_RANGE ) { 1847if (cmd -> uc_argt & EX_DFLALL ) { 1848obj = STATIC_CSTR_AS_OBJ ("%" );// -range=% 1849 }else if (cmd -> uc_def >=0 ) { 1850obj = STRING_OBJ (arena_printf (arena ,"%" PRId64 ,cmd -> uc_def ));// -range=N 1851 }else { 1852obj = STATIC_CSTR_AS_OBJ ("." );// -range 1853 } 1854 } 1855PUT_C (d ,"range" ,obj ); 1856 1857obj = NIL ; 1858for (int j = 0 ;addr_type_complete [j ].expand != ADDR_NONE ;j ++ ) { 1859if (addr_type_complete [j ].expand != ADDR_LINES 1860&& addr_type_complete [j ].expand == cmd -> uc_addr_type ) { 1861obj = CSTR_AS_OBJ (addr_type_complete [j ].name ); 1862break ; 1863 } 1864 } 1865PUT_C (d ,"addr" ,obj ); 1866 1867PUT_C (rv ,cmd -> uc_name ,DICT_OBJ (d )); 1868 } 1869return rv ; 1870}