robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// ex_docmd.c: functions for executing an Ex command line. 2 3#include <assert.h> 4#include <ctype.h> 5#include <inttypes.h> 6#include <limits.h> 7#include <signal.h> 8#include <stdbool.h> 9#include <stddef.h> 10#include <stdio.h> 11#include <stdlib.h> 12#include <string.h> 13#include <uv.h> 14 15#include "auto/config.h" 16#include "nvim/api/private/defs.h" 17#include "nvim/api/private/dispatch.h" 18#include "nvim/api/private/helpers.h" 19#include "nvim/api/ui.h" 20#include "nvim/api/vim.h" 21#include "nvim/api/vimscript.h" 22#include "nvim/arglist.h" 23#include "nvim/ascii_defs.h" 24#include "nvim/autocmd.h" 25#include "nvim/autocmd_defs.h" 26#include "nvim/buffer.h" 27#include "nvim/buffer_defs.h" 28#include "nvim/change.h" 29#include "nvim/channel.h" 30#include "nvim/charset.h" 31#include "nvim/clipboard.h" 32#include "nvim/cmdexpand.h" 33#include "nvim/cmdexpand_defs.h" 34#include "nvim/context.h" 35#include "nvim/cursor.h" 36#include "nvim/debugger.h" 37#include "nvim/dialog.h" 38#include "nvim/digraph.h" 39#include "nvim/drawscreen.h" 40#include "nvim/errors.h" 41#include "nvim/eval/fs.h" 42#include "nvim/eval/funcs.h" 43#include "nvim/eval/typval.h" 44#include "nvim/eval/typval_defs.h" 45#include "nvim/eval/userfunc.h" 46#include "nvim/eval/vars.h" 47#include "nvim/eval/window.h" 48#include "nvim/eval_defs.h" 49#include "nvim/event/loop.h" 50#include "nvim/event/multiqueue.h" 51#include "nvim/event/proc.h" 52#include "nvim/event/socket.h" 53#include "nvim/ex_cmds.h" 54#include "nvim/ex_cmds2.h" 55#include "nvim/ex_cmds_defs.h" 56#include "nvim/ex_docmd.h" 57#include "nvim/ex_eval.h" 58#include "nvim/ex_eval_defs.h" 59#include "nvim/ex_getln.h" 60#include "nvim/file_search.h" 61#include "nvim/fileio.h" 62#include "nvim/fold.h" 63#include "nvim/garray.h" 64#include "nvim/garray_defs.h" 65#include "nvim/gettext_defs.h" 66#include "nvim/globals.h" 67#include "nvim/highlight_defs.h" 68#include "nvim/highlight_group.h" 69#include "nvim/input.h" 70#include "nvim/insert.h" 71#include "nvim/keycodes.h" 72#include "nvim/lua/executor.h" 73#include "nvim/macros_defs.h" 74#include "nvim/main.h" 75#include "nvim/mark.h" 76#include "nvim/mark_defs.h" 77#include "nvim/mbyte.h" 78#include "nvim/memline.h" 79#include "nvim/memline_defs.h" 80#include "nvim/memory.h" 81#include "nvim/message.h" 82#include "nvim/mouse.h" 83#include "nvim/move.h" 84#include "nvim/msgpack_rpc/channel.h" 85#include "nvim/msgpack_rpc/server.h" 86#include "nvim/normal.h" 87#include "nvim/normal_defs.h" 88#include "nvim/option.h" 89#include "nvim/option_defs.h" 90#include "nvim/option_vars.h" 91#include "nvim/optionstr.h" 92#include "nvim/os/fs.h" 93#include "nvim/os/input.h" 94#include "nvim/os/os.h" 95#include "nvim/os/os_defs.h" 96#ifdef MSWIN 97# include "nvim/os/os_win_console.h" 98#endif 99#include "nvim/os/shell.h" 100#include "nvim/path.h" 101#include "nvim/plines.h" 102#include "nvim/popupmenu.h" 103#include "nvim/pos_defs.h" 104#include "nvim/profile.h" 105#include "nvim/quickfix.h" 106#include "nvim/regexp.h" 107#include "nvim/regexp_defs.h" 108#include "nvim/runtime.h" 109#include "nvim/runtime_defs.h" 110#include "nvim/search.h" 111#include "nvim/shada.h" 112#include "nvim/state.h" 113#include "nvim/state_defs.h" 114#include "nvim/statusline.h" 115#include "nvim/strings.h" 116#include "nvim/tag.h" 117#include "nvim/types_defs.h" 118#include "nvim/ui.h" 119#include "nvim/ui_client.h" 120#include "nvim/undo.h" 121#include "nvim/undo_defs.h" 122#include "nvim/usercmd.h" 123#include "nvim/vim_defs.h" 124#include "nvim/window.h" 125#include "nvim/winfloat.h" 126 127static const char e_ambiguous_use_of_user_defined_command [] 128= N_ ("E464: Ambiguous use of user-defined command" ); 129static const char e_no_call_stack_to_substitute_for_stack [] 130= N_ ("E489: No call stack to substitute for \"<stack>\"" ); 131static const char e_not_an_editor_command [] 132= N_ ("E492: Not an editor command" ); 133static const char e_no_autocommand_file_name_to_substitute_for_afile [] 134= N_ ("E495: No autocommand file name to substitute for \"<afile>\"" ); 135static const char e_no_autocommand_buffer_number_to_substitute_for_abuf [] 136= N_ ("E496: No autocommand buffer number to substitute for \"<abuf>\"" ); 137static const char e_no_autocommand_match_name_to_substitute_for_amatch [] 138= N_ ("E497: No autocommand match name to substitute for \"<amatch>\"" ); 139static const char e_no_source_file_name_to_substitute_for_sfile [] 140= N_ ("E498: No :source file name to substitute for \"<sfile>\"" ); 141static const char e_no_line_number_to_use_for_slnum [] 142= N_ ("E842: No line number to use for \"<slnum>\"" ); 143static const char e_no_line_number_to_use_for_sflnum [] 144= N_ ("E961: No line number to use for \"<sflnum>\"" ); 145static const char e_no_script_file_name_to_substitute_for_script [] 146= N_ ("E1274: No script file name to substitute for \"<script>\"" ); 147 148static int quitmore = 0 ; 149 150// Struct for storing a line inside a while/for loop 151typedef struct { 152char * line ;// command line 153linenr_T lnum ;// sourcing_lnum of the line 154}wcmd_T ; 155 156#define FREE_WCMD (wcmd ) xfree((wcmd)->line) 157 158/// Structure used to store info for line position in a while or for loop. 159/// This is required, because do_one_cmd() may invoke ex_function(), which 160/// reads more lines that may come from the while/for loop. 161struct loop_cookie { 162garray_T * lines_gap ;// growarray with line info 163int current_line ;// last read line from growarray 164int repeating ;// true when looping a second time 165// When "repeating" is false use "getline" and "cookie" to get lines 166LineGetter lc_getline ; 167void * cookie ; 168}; 169 170// Struct to save a few things while debugging. Used in do_cmdline() only. 171struct dbg_stuff { 172int trylevel ; 173int force_abort ; 174except_T * caught_stack ; 175char * vv_exception ; 176char * vv_throwpoint ; 177int did_emsg ; 178int got_int ; 179bool did_throw ; 180int need_rethrow ; 181int check_cstack ; 182except_T * current_exception ; 183}; 184 185#include "ex_docmd.c.generated.h" 186 187// Declare cmdnames[]. 188#include "ex_cmds_defs.generated.h" 189 190static char dollar_command [2 ]= {'$' ,0 }; 191 192static void save_dbg_stuff (struct dbg_stuff * dsp ) 193{ 194dsp -> trylevel = trylevel ; 195trylevel = 0 ; 196dsp -> force_abort = force_abort ; 197force_abort = false; 198dsp -> caught_stack = caught_stack ; 199caught_stack = NULL ; 200dsp -> vv_exception = v_exception (NULL ); 201dsp -> vv_throwpoint = v_throwpoint (NULL ); 202 203// Necessary for debugging an inactive ":catch", ":finally", ":endtry". 204dsp -> did_emsg = did_emsg ; 205did_emsg = false; 206dsp -> got_int = got_int ; 207got_int = false; 208dsp -> did_throw = did_throw ; 209did_throw = false; 210dsp -> need_rethrow = need_rethrow ; 211need_rethrow = false; 212dsp -> check_cstack = check_cstack ; 213check_cstack = false; 214dsp -> current_exception = current_exception ; 215current_exception = NULL ; 216} 217 218static void restore_dbg_stuff (struct dbg_stuff * dsp ) 219{ 220suppress_errthrow = false; 221trylevel = dsp -> trylevel ; 222force_abort = dsp -> force_abort ; 223caught_stack = dsp -> caught_stack ; 224v_exception (dsp -> vv_exception ); 225v_throwpoint (dsp -> vv_throwpoint ); 226did_emsg = dsp -> did_emsg ; 227got_int = dsp -> got_int ; 228did_throw = dsp -> did_throw ; 229need_rethrow = dsp -> need_rethrow ; 230check_cstack = dsp -> check_cstack ; 231current_exception = dsp -> current_exception ; 232} 233 234/// Check if ffname differs from fnum. 235/// fnum is a buffer number. 0 == current buffer, 1-or-more must be a valid buffer ID. 236/// ffname is a full path to where a buffer lives on-disk or would live on-disk. 237static bool is_other_file (int fnum ,char * ffname ) 238{ 239if (fnum != 0 ) { 240if (fnum == curbuf -> b_fnum ) { 241return false; 242 } 243 244return true; 245 } 246 247if (ffname == NULL ) { 248return true; 249 } 250 251if (* ffname == NUL ) { 252return false; 253 } 254 255if (!curbuf -> file_id_valid 256&& curbuf -> b_sfname != NULL 257&& * curbuf -> b_sfname != NUL ) { 258// This occurs with unsaved buffers. In which case `ffname` 259// actually corresponds to curbuf->b_sfname 260return !path_equal (ffname ,curbuf -> b_sfname ,kPathCmpLiteral ); 261 } 262 263return otherfile (ffname ); 264} 265 266/// Print the executed command for when 'verbose' is set. 267/// 268/// @param lnum if 0, only print the command. 269static void msg_verbose_cmd (linenr_T lnum ,char * cmd ) 270FUNC_ATTR_NONNULL_ALL 271{ 272no_wait_return ++ ; 273verbose_enter_scroll (); 274 275if (lnum == 0 ) { 276smsg (0 ,_ ("Executing: %s" ),cmd ); 277 }else { 278smsg (0 ,_ ("line %" PRIdLINENR ": %s" ),lnum ,cmd ); 279 } 280if (msg_silent == 0 ) { 281msg_puts ("\n" );// don't overwrite this 282 } 283 284verbose_leave_scroll (); 285no_wait_return -- ; 286} 287 288static int cmdline_call_depth = 0 ;///< recursiveness 289 290/// Start executing an Ex command line. 291/// 292/// @return FAIL if too recursive, OK otherwise. 293static int do_cmdline_start (void ) 294{ 295assert (cmdline_call_depth >=0 ); 296// It's possible to create an endless loop with ":execute", catch that 297// here. The value of 200 allows nested function calls, ":source", etc. 298// Allow 200 or 'maxfuncdepth', whatever is larger. 299if (cmdline_call_depth >=200 && cmdline_call_depth >=p_mfd ) { 300return FAIL ; 301 } 302cmdline_call_depth ++ ; 303start_batch_changes (); 304return OK ; 305} 306 307/// End executing an Ex command line. 308static void do_cmdline_end (void ) 309{ 310cmdline_call_depth -- ; 311assert (cmdline_call_depth >=0 ); 312end_batch_changes (); 313} 314 315/// Execute a simple command line. Used for translated commands like "*". 316int do_cmdline_cmd (const char * cmd ) 317{ 318return do_cmdline ((char * )cmd ,NULL ,NULL ,DOCMD_VERBOSE |DOCMD_NOWAIT |DOCMD_KEYTYPED ); 319} 320 321/// do_cmdline(): execute one Ex command line 322/// 323/// 1. Execute "cmdline" when it is not NULL. 324/// If "cmdline" is NULL, or more lines are needed, fgetline() is used. 325/// 2. Split up in parts separated with '|'. 326/// 327/// This function can be called recursively! 328/// 329/// flags: 330/// DOCMD_VERBOSE - The command will be included in the error message. 331/// DOCMD_NOWAIT - Don't call wait_return() and friends. 332/// DOCMD_REPEAT - Repeat execution until fgetline() returns NULL. 333/// DOCMD_KEYTYPED - Don't reset KeyTyped. 334/// DOCMD_EXCRESET - Reset the exception environment (used for debugging). 335/// DOCMD_KEEPLINE - Store first typed line (for repeating with "."). 336/// 337/// @param cookie argument for fgetline() 338/// 339/// @return FAIL if cmdline could not be executed, OK otherwise 340int do_cmdline (char * cmdline ,LineGetter fgetline ,void * cookie ,int flags ) 341{ 342char * next_cmdline ;// next cmd to execute 343char * cmdline_copy = NULL ;// copy of cmd line 344bool used_getline = false;// used "fgetline" to obtain command 345static int recursive = 0 ;// recursive depth 346bool msg_didout_before_start = false; 347int count = 0 ;// line number count 348bool did_inc = false;// incremented RedrawingDisabled 349bool did_block = false;// emitted cmdline_block event 350int retval = OK ; 351cstack_T cstack = {// conditional stack 352 .cs_idx = -1 , 353 }; 354garray_T lines_ga ;// keep lines for ":while"/":for" 355int current_line = 0 ;// active line in lines_ga 356char * fname = NULL ;// function or script name 357linenr_T * breakpoint = NULL ;// ptr to breakpoint field in cookie 358int * dbg_tick = NULL ;// ptr to dbg_tick field in cookie 359struct dbg_stuff debug_saved ;// saved things for debug mode 360msglist_T * private_msg_list ; 361 362// "fgetline" and "cookie" passed to do_one_cmd() 363char * (* cmd_getline )(int ,void * ,int ,bool ); 364void * cmd_cookie ; 365struct loop_cookie cmd_loop_cookie ; 366 367// For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory 368// location for storing error messages to be converted to an exception. 369// This ensures that the do_errthrow() call in do_one_cmd() does not 370// combine the messages stored by an earlier invocation of do_one_cmd() 371// with the command name of the later one. This would happen when 372// BufWritePost autocommands are executed after a write error. 373msglist_T ** saved_msg_list = msg_list ; 374msg_list = & private_msg_list ; 375private_msg_list = NULL ; 376 377if (do_cmdline_start ()== FAIL ) { 378emsg (_ (e_command_too_recursive )); 379// When converting to an exception, we do not include the command name 380// since this is not an error of the specific command. 381do_errthrow ((cstack_T * )NULL ,NULL ); 382msg_list = saved_msg_list ; 383return FAIL ; 384 } 385 386ga_init (& lines_ga , (int )sizeof (wcmd_T ),10 ); 387 388void * real_cookie = getline_cookie (fgetline ,cookie ); 389 390// Inside a function use a higher nesting level. 391bool getline_is_func = getline_equal (fgetline ,cookie ,get_func_line ); 392if (getline_is_func && ex_nesting_level == func_level (real_cookie )) { 393ex_nesting_level ++ ; 394 } 395 396// Get the function or script name and the address where the next breakpoint 397// line and the debug tick for a function or script are stored. 398if (getline_is_func ) { 399fname = func_name (real_cookie ); 400breakpoint = func_breakpoint (real_cookie ); 401dbg_tick = func_dbg_tick (real_cookie ); 402 }else if (getline_equal (fgetline ,cookie ,getsourceline )) { 403fname = SOURCING_NAME ; 404breakpoint = source_breakpoint (real_cookie ); 405dbg_tick = source_dbg_tick (real_cookie ); 406 } 407 408// Initialize "force_abort" and "suppress_errthrow" at the top level. 409if (!recursive ) { 410force_abort = false; 411suppress_errthrow = false; 412 } 413 414// If requested, store and reset the global values controlling the 415// exception handling (used when debugging). Otherwise clear it to avoid 416// a bogus compiler warning when the optimizer uses inline functions... 417if (flags & DOCMD_EXCRESET ) { 418save_dbg_stuff (& debug_saved ); 419 }else { 420CLEAR_FIELD (debug_saved ); 421 } 422 423int initial_trylevel = trylevel ; 424 425// "did_throw" will be set to true when an exception is being thrown. 426did_throw = false; 427// "did_emsg" will be set to true when emsg() is used, in which case we 428// cancel the whole command line, and any if/endif or loop. 429// If force_abort is set, we cancel everything. 430did_emsg = false; 431 432// KeyTyped is only set when calling vgetc(). Reset it here when not 433// calling vgetc() (sourced command lines). 434if (!(flags & DOCMD_KEYTYPED ) 435&& !getline_equal (fgetline ,cookie ,getexline )) { 436KeyTyped = false; 437 } 438 439// Continue executing command lines: 440// - when inside an ":if", ":while" or ":for" 441// - for multiple commands on one line, separated with '|' 442// - when repeating until there are no more lines (for ":source") 443next_cmdline = cmdline ; 444do { 445getline_is_func = getline_equal (fgetline ,cookie ,get_func_line ); 446 447// stop skipping cmds for an error msg after all endif/while/for 448if (next_cmdline == NULL 449&& !force_abort 450&& cstack .cs_idx < 0 451&& !(getline_is_func 452&& func_has_abort (real_cookie ))) { 453did_emsg = false; 454 } 455 456// 1. If repeating a line in a loop, get a line from lines_ga. 457// 2. If no line given: Get an allocated line with fgetline(). 458// 3. If a line is given: Make a copy, so we can mess with it. 459 460// 1. If repeating, get a previous line from lines_ga. 461if (cstack .cs_looplevel > 0 && current_line < lines_ga .ga_len ) { 462// Each '|' separated command is stored separately in lines_ga, to 463// be able to jump to it. Don't use next_cmdline now. 464XFREE_CLEAR (cmdline_copy ); 465 466// Check if a function has returned or, unless it has an unclosed 467// try conditional, aborted. 468if (getline_is_func ) { 469if (do_profiling == PROF_YES ) { 470func_line_end (real_cookie ); 471 } 472if (func_has_ended (real_cookie )) { 473retval = FAIL ; 474break ; 475 } 476 }else if (do_profiling == PROF_YES 477&& getline_equal (fgetline ,cookie ,getsourceline )) { 478script_line_end (); 479 } 480 481// Check if a sourced file hit a ":finish" command. 482if (source_finished (fgetline ,cookie )) { 483retval = FAIL ; 484break ; 485 } 486 487// If breakpoints have been added/deleted need to check for it. 488if (breakpoint != NULL && dbg_tick != NULL 489&& * dbg_tick != debug_tick ) { 490* breakpoint = dbg_find_breakpoint (getline_equal (fgetline ,cookie ,getsourceline ), 491fname ,SOURCING_LNUM ); 492* dbg_tick = debug_tick ; 493 } 494 495next_cmdline = ((wcmd_T * )(lines_ga .ga_data ))[current_line ].line ; 496SOURCING_LNUM = ((wcmd_T * )(lines_ga .ga_data ))[current_line ].lnum ; 497 498// Did we encounter a breakpoint? 499if (breakpoint != NULL && * breakpoint != 0 && * breakpoint <=SOURCING_LNUM ) { 500dbg_breakpoint (fname ,SOURCING_LNUM ); 501// Find next breakpoint. 502* breakpoint = dbg_find_breakpoint (getline_equal (fgetline ,cookie ,getsourceline ), 503fname ,SOURCING_LNUM ); 504* dbg_tick = debug_tick ; 505 } 506if (do_profiling == PROF_YES ) { 507if (getline_is_func ) { 508func_line_start (real_cookie ); 509 }else if (getline_equal (fgetline ,cookie ,getsourceline )) { 510script_line_start (); 511 } 512 } 513 } 514 515// 2. If no line given, get an allocated line with fgetline(). 516if (next_cmdline == NULL ) { 517int indent = cstack .cs_idx < 0 ?0 : (cstack .cs_idx + 1 )* 2 ; 518 519if (count == 1 && getline_equal (fgetline ,cookie ,getexline )) { 520if (ui_has (kUICmdline )) { 521// Emit cmdline_block event for loop/conditional block. 522ui_ext_cmdline_block_append (0 ,last_cmdline ); 523did_block = true; 524 } 525// Need to set msg_didout for the first line after an ":if", 526// otherwise the ":if" will be overwritten. 527msg_didout = true; 528 } 529 530if (fgetline == NULL || (next_cmdline = fgetline (':' ,cookie ,indent , true))== NULL ) { 531// Don't call wait_return() for aborted command line. The NULL 532// returned for the end of a sourced file or executed function 533// doesn't do this. 534if (KeyTyped && !(flags & DOCMD_REPEAT )) { 535need_wait_return = false; 536 } 537retval = FAIL ; 538break ; 539 } 540used_getline = true; 541 542// Emit all but the first cmdline_block event immediately; waiting until after 543// command execution would mess up event ordering with nested command lines. 544if (ui_has (kUICmdline )&& count > 0 && getline_equal (fgetline ,cookie ,getexline )) { 545ui_ext_cmdline_block_append ((size_t )indent ,next_cmdline ); 546 } 547 548// Keep the first typed line. Clear it when more lines are typed. 549if (flags & DOCMD_KEEPLINE ) { 550xfree (repeat_cmdline ); 551if (count == 0 ) { 552repeat_cmdline = xstrdup (next_cmdline ); 553 }else { 554repeat_cmdline = NULL ; 555 } 556 } 557 }else if (cmdline_copy == NULL ) { 558// 3. Make a copy of the command so we can mess with it. 559next_cmdline = xstrdup (next_cmdline ); 560 } 561cmdline_copy = next_cmdline ; 562 563int current_line_before = 0 ; 564// Inside a while/for loop, and when the command looks like a ":while" 565// or ":for", the line is stored, because we may need it later when 566// looping. 567// 568// When there is a '|' and another command, it is stored separately, 569// because we need to be able to jump back to it from an 570// :endwhile/:endfor. 571// 572// Pass a different "fgetline" function to do_one_cmd() below, 573// that it stores lines in or reads them from "lines_ga". Makes it 574// possible to define a function inside a while/for loop. 575if ((cstack .cs_looplevel > 0 || has_loop_cmd (next_cmdline ))) { 576cmd_getline = get_loop_line ; 577cmd_cookie = (void * )& cmd_loop_cookie ; 578cmd_loop_cookie .lines_gap = & lines_ga ; 579cmd_loop_cookie .current_line = current_line ; 580cmd_loop_cookie .lc_getline = fgetline ; 581cmd_loop_cookie .cookie = cookie ; 582cmd_loop_cookie .repeating = (current_line < lines_ga .ga_len ); 583 584// Save the current line when encountering it the first time. 585if (current_line == lines_ga .ga_len ) { 586store_loop_line (& lines_ga ,next_cmdline ); 587 } 588current_line_before = current_line ; 589 }else { 590cmd_getline = fgetline ; 591cmd_cookie = cookie ; 592 } 593 594did_endif = false; 595 596if (count ++ == 0 ) { 597// All output from the commands is put below each other, without 598// waiting for a return. Don't do this when executing commands 599// from a script or when being called recursive (e.g. for ":e 600// +command file"). 601if (!(flags & DOCMD_NOWAIT )&& !recursive ) { 602msg_didout_before_start = msg_didout ; 603msg_didany = false;// no output yet 604msg_start (); 605msg_scroll = true;// put messages below each other 606no_wait_return ++ ;// don't wait for return until finished 607RedrawingDisabled ++ ; 608did_inc = true; 609 } 610 } 611 612if ((p_verbose >=15 && SOURCING_NAME != NULL )|| p_verbose >=16 ) { 613msg_verbose_cmd (SOURCING_LNUM ,cmdline_copy ); 614 } 615 616// 2. Execute one '|' separated command. 617// do_one_cmd() will return NULL if there is no trailing '|'. 618// "cmdline_copy" can change, e.g. for '%' and '#' expansion. 619recursive ++ ; 620next_cmdline = do_one_cmd (& cmdline_copy ,flags ,& cstack ,cmd_getline ,cmd_cookie ); 621recursive -- ; 622 623if (cmd_cookie == (void * )& cmd_loop_cookie ) { 624// Use "current_line" from "cmd_loop_cookie", it may have been 625// incremented when defining a function. 626current_line = cmd_loop_cookie .current_line ; 627 } 628 629if (next_cmdline == NULL ) { 630XFREE_CLEAR (cmdline_copy ); 631 632// If the command was typed, remember it for the ':' register. 633// Do this AFTER executing the command to make :@: work. 634if (getline_equal (fgetline ,cookie ,getexline )&& new_last_cmdline != NULL ) { 635xfree (last_cmdline ); 636last_cmdline = new_last_cmdline ; 637new_last_cmdline = NULL ; 638 } 639 }else { 640// need to copy the command after the '|' to cmdline_copy, for the 641// next do_one_cmd() 642STRMOVE (cmdline_copy ,next_cmdline ); 643next_cmdline = cmdline_copy ; 644 } 645 646// reset did_emsg for a function that is not aborted by an error 647if (did_emsg && !force_abort 648&& getline_equal (fgetline ,cookie ,get_func_line ) 649&& !func_has_abort (real_cookie )) { 650did_emsg = false; 651 } 652 653if (cstack .cs_looplevel > 0 ) { 654current_line ++ ; 655 656// An ":endwhile", ":endfor" and ":continue" is handled here. 657// If we were executing commands, jump back to the ":while" or 658// ":for". 659// If we were not executing commands, decrement cs_looplevel. 660if (cstack .cs_lflags & (CSL_HAD_CONT |CSL_HAD_ENDLOOP )) { 661cstack .cs_lflags &= ~(CSL_HAD_CONT |CSL_HAD_ENDLOOP ); 662 663// Jump back to the matching ":while" or ":for". Be careful 664// not to use a cs_line[] from an entry that isn't a ":while" 665// or ":for": It would make "current_line" invalid and can 666// cause a crash. 667if (!did_emsg && !got_int && !did_throw 668&& cstack .cs_idx >=0 669&& (cstack .cs_flags [cstack .cs_idx ] 670& (CSF_WHILE |CSF_FOR )) 671&& cstack .cs_line [cstack .cs_idx ] >=0 672&& (cstack .cs_flags [cstack .cs_idx ]& CSF_ACTIVE )) { 673current_line = cstack .cs_line [cstack .cs_idx ]; 674// remember we jumped there 675cstack .cs_lflags |=CSL_HAD_LOOP ; 676line_breakcheck ();// check if CTRL-C typed 677 678// Check for the next breakpoint at or after the ":while" 679// or ":for". 680if (breakpoint != NULL && lines_ga .ga_len > current_line ) { 681* breakpoint = dbg_find_breakpoint (getline_equal (fgetline ,cookie ,getsourceline ),fname , 682 ((wcmd_T * )lines_ga .ga_data )[current_line ].lnum - 1 ); 683* dbg_tick = debug_tick ; 684 } 685 }else { 686// can only get here with ":endwhile" or ":endfor" 687if (cstack .cs_idx >=0 ) { 688rewind_conditionals (& cstack ,cstack .cs_idx - 1 , 689CSF_WHILE |CSF_FOR ,& cstack .cs_looplevel ); 690 } 691 } 692 }else if (cstack .cs_lflags & CSL_HAD_LOOP ) { 693// For a ":while" or ":for" we need to remember the line number. 694cstack .cs_lflags &= ~CSL_HAD_LOOP ; 695cstack .cs_line [cstack .cs_idx ]= current_line_before ; 696 } 697 } 698 699// When not inside any ":while" loop, clear remembered lines. 700if (cstack .cs_looplevel == 0 ) { 701if (!GA_EMPTY (& lines_ga )) { 702SOURCING_LNUM = ((wcmd_T * )lines_ga .ga_data )[lines_ga .ga_len - 1 ].lnum ; 703GA_DEEP_CLEAR (& lines_ga ,wcmd_T ,FREE_WCMD ); 704 } 705current_line = 0 ; 706 } 707 708// A ":finally" makes did_emsg, got_int and did_throw pending for 709// being restored at the ":endtry". Reset them here and set the 710// ACTIVE and FINALLY flags, so that the finally clause gets executed. 711// This includes the case where a missing ":endif", ":endwhile" or 712// ":endfor" was detected by the ":finally" itself. 713if (cstack .cs_lflags & CSL_HAD_FINA ) { 714cstack .cs_lflags &= ~CSL_HAD_FINA ; 715report_make_pending ((cstack .cs_pending [cstack .cs_idx ] 716& (CSTP_ERROR |CSTP_INTERRUPT |CSTP_THROW )), 717did_throw ?current_exception :NULL ); 718did_emsg = got_int = did_throw = false; 719cstack .cs_flags [cstack .cs_idx ] |=CSF_ACTIVE |CSF_FINALLY ; 720 } 721 722// Update global "trylevel" for recursive calls to do_cmdline() from 723// within this loop. 724trylevel = initial_trylevel + cstack .cs_trylevel ; 725 726// If the outermost try conditional (across function calls and sourced 727// files) is aborted because of an error, an interrupt, or an uncaught 728// exception, cancel everything. If it is left normally, reset 729// force_abort to get the non-EH compatible abortion behavior for 730// the rest of the script. 731if (trylevel == 0 && !did_emsg && !got_int && !did_throw ) { 732force_abort = false; 733 } 734 735// Convert an interrupt to an exception if appropriate. 736do_intthrow (& cstack ); 737 738// Continue executing command lines when: 739// - no CTRL-C typed, no aborting error, no exception thrown or try 740// conditionals need to be checked for executing finally clauses or 741// catching an interrupt exception 742// - didn't get an error message or lines are not typed 743// - there is a command after '|', inside a :if, :while, :for or :try, or 744// looping for ":source" command or function call. 745 }while (!((got_int || (did_emsg && force_abort )|| did_throw ) 746&& cstack .cs_trylevel == 0 ) 747&& !(did_emsg 748// Keep going when inside try/catch, so that the error can be 749// deal with, except when it is a syntax error, it may cause 750// the :endtry to be missed. 751&& (cstack .cs_trylevel == 0 || did_emsg_syntax ) 752&& used_getline 753&& getline_equal (fgetline ,cookie ,getexline )) 754&& (next_cmdline != NULL 755|| cstack .cs_idx >=0 756|| (flags & DOCMD_REPEAT ))); 757 758xfree (cmdline_copy ); 759did_emsg_syntax = false; 760GA_DEEP_CLEAR (& lines_ga ,wcmd_T ,FREE_WCMD ); 761 762if (cstack .cs_idx >=0 ) { 763// If a sourced file or executed function ran to its end, report the 764// unclosed conditional. 765if (!got_int && !did_throw && !aborting () 766&& ((getline_equal (fgetline ,cookie ,getsourceline ) 767&& !source_finished (fgetline ,cookie )) 768|| (getline_equal (fgetline ,cookie ,get_func_line ) 769&& !func_has_ended (real_cookie )))) { 770if (cstack .cs_flags [cstack .cs_idx ]& CSF_TRY ) { 771emsg (_ (e_endtry )); 772 }else if (cstack .cs_flags [cstack .cs_idx ]& CSF_WHILE ) { 773emsg (_ (e_endwhile )); 774 }else if (cstack .cs_flags [cstack .cs_idx ]& CSF_FOR ) { 775emsg (_ (e_endfor )); 776 }else { 777emsg (_ (e_endif )); 778 } 779 } 780 781// Reset "trylevel" in case of a ":finish" or ":return" or a missing 782// ":endtry" in a sourced file or executed function. If the try 783// conditional is in its finally clause, ignore anything pending. 784// If it is in a catch clause, finish the caught exception. 785// Also cleanup any "cs_forinfo" structures. 786do { 787int idx = cleanup_conditionals (& cstack ,0 , true); 788 789if (idx >=0 ) { 790idx -- ;// remove try block not in its finally clause 791 } 792rewind_conditionals (& cstack ,idx ,CSF_WHILE |CSF_FOR , 793& cstack .cs_looplevel ); 794 }while (cstack .cs_idx >=0 ); 795trylevel = initial_trylevel ; 796 } 797 798// If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory 799// lack was reported above and the error message is to be converted to an 800// exception, do this now after rewinding the cstack. 801do_errthrow (& cstack ,getline_equal (fgetline ,cookie ,get_func_line ) ?"endfunction" :NULL ); 802 803if (trylevel == 0 ) { 804// When an exception is being thrown out of the outermost try 805// conditional, discard the uncaught exception, disable the conversion 806// of interrupts or errors to exceptions, and ensure that no more 807// commands are executed. 808if (did_throw ) { 809handle_did_throw (); 810 }else if (got_int || (did_emsg && force_abort )) { 811// On an interrupt or an aborting error not converted to an exception, 812// disable the conversion of errors to exceptions. (Interrupts are not 813// converted any more, here.) This enables also the interrupt message 814// when force_abort is set and did_emsg unset in case of an interrupt 815// from a finally clause after an error. 816suppress_errthrow = true; 817 } 818 } 819 820// The current cstack will be freed when do_cmdline() returns. An uncaught 821// exception will have to be rethrown in the previous cstack. If a function 822// has just returned or a script file was just finished and the previous 823// cstack belongs to the same function or, respectively, script file, it 824// will have to be checked for finally clauses to be executed due to the 825// ":return" or ":finish". This is done in do_one_cmd(). 826if (did_throw ) { 827need_rethrow = true; 828 } 829if ((getline_equal (fgetline ,cookie ,getsourceline ) 830&& ex_nesting_level > source_level (real_cookie )) 831|| (getline_equal (fgetline ,cookie ,get_func_line ) 832&& ex_nesting_level > func_level (real_cookie )+ 1 )) { 833if (!did_throw ) { 834check_cstack = true; 835 } 836 }else { 837// When leaving a function, reduce nesting level. 838if (getline_equal (fgetline ,cookie ,get_func_line )) { 839ex_nesting_level -- ; 840 } 841// Go to debug mode when returning from a function in which we are 842// single-stepping. 843if ((getline_equal (fgetline ,cookie ,getsourceline ) 844|| getline_equal (fgetline ,cookie ,get_func_line )) 845&& ex_nesting_level + 1 <=debug_break_level ) { 846do_debug (getline_equal (fgetline ,cookie ,getsourceline ) 847 ?_ ("End of sourced file" ) 848 :_ ("End of function" )); 849 } 850 } 851 852// Restore the exception environment (done after returning from the 853// debugger). 854if (flags & DOCMD_EXCRESET ) { 855restore_dbg_stuff (& debug_saved ); 856 } 857 858msg_list = saved_msg_list ; 859 860// Cleanup if "cs_emsg_silent_list" remains. 861if (cstack .cs_emsg_silent_list != NULL ) { 862eslist_T * temp ; 863for (eslist_T * elem = cstack .cs_emsg_silent_list ;elem != NULL ;elem = temp ) { 864temp = elem -> next ; 865xfree (elem ); 866 } 867 } 868 869// If there was too much output to fit on the command line, ask the user to 870// hit return before redrawing the screen. With the ":global" command we do 871// this only once after the command is finished. 872if (did_inc ) { 873RedrawingDisabled -- ; 874no_wait_return -- ; 875msg_scroll = false; 876 877// When just finished an ":if"-":else" which was typed, no need to 878// wait for hit-return. Also for an error situation. 879if (retval == FAIL 880|| (did_endif && KeyTyped && !did_emsg )) { 881need_wait_return = false; 882msg_didany = false;// don't wait when restarting edit 883 }else if (need_wait_return ) { 884// The msg_start() above clears msg_didout. The wait_return() we do 885// here should not overwrite the command that may be shown before 886// doing that. 887msg_didout |=msg_didout_before_start ; 888wait_return (false); 889 } 890 } 891 892if (did_block ) { 893ui_ext_cmdline_block_leave (); 894 } 895 896did_endif = false;// in case do_cmdline used recursively 897 898do_cmdline_end (); 899return retval ; 900} 901 902/// Handle when "did_throw" is set after executing commands. 903void handle_did_throw (void ) 904{ 905assert (current_exception != NULL ); 906char * p = NULL ; 907msglist_T * messages = NULL ; 908ESTACK_CHECK_DECLARATION ; 909 910// If the uncaught exception is a user exception, report it as an 911// error. If it is an error exception, display the saved error 912// message now. For an interrupt exception, do nothing; the 913// interrupt message is given elsewhere. 914switch (current_exception -> type ) { 915case ET_USER : 916vim_snprintf (IObuff ,IOSIZE , 917_ ("E605: Exception not caught: %s" ), 918current_exception -> value ); 919p = xstrdup (IObuff ); 920break ; 921case ET_ERROR : 922messages = current_exception -> messages ; 923current_exception -> messages = NULL ; 924break ; 925case ET_INTERRUPT : 926break ; 927 } 928 929estack_push (ETYPE_EXCEPT ,current_exception -> throw_name ,current_exception -> throw_lnum ); 930ESTACK_CHECK_SETUP ; 931current_exception -> throw_name = NULL ; 932 933discard_current_exception ();// uses IObuff if 'verbose' 934 935// If "silent!" is active the uncaught exception is not fatal. 936if (emsg_silent == 0 ) { 937suppress_errthrow = true; 938force_abort = true; 939 } 940 941if (messages != NULL ) { 942do { 943msglist_T * next = messages -> next ; 944emsg_multiline (messages -> msg ,"emsg" ,HLF_E ,messages -> multiline ); 945xfree (messages -> msg ); 946xfree (messages -> sfile ); 947xfree (messages ); 948messages = next ; 949 }while (messages != NULL ); 950 }else if (p != NULL ) { 951emsg (p ); 952xfree (p ); 953 } 954xfree (SOURCING_NAME ); 955ESTACK_CHECK_NOW ; 956estack_pop (); 957} 958 959/// Obtain a line when inside a ":while" or ":for" loop. 960static char * get_loop_line (int c ,void * cookie ,int indent ,bool do_concat ) 961{ 962struct loop_cookie * cp = (struct loop_cookie * )cookie ; 963 964if (cp -> current_line + 1 >=cp -> lines_gap -> ga_len ) { 965if (cp -> repeating ) { 966return NULL ;// trying to read past ":endwhile"/":endfor" 967 } 968char * line ; 969// First time inside the ":while"/":for": get line normally. 970if (cp -> lc_getline == NULL ) { 971line = getcmdline (c ,0 ,indent ,do_concat ); 972 }else { 973line = cp -> lc_getline (c ,cp -> cookie ,indent ,do_concat ); 974 } 975if (line != NULL ) { 976store_loop_line (cp -> lines_gap ,line ); 977cp -> current_line ++ ; 978 } 979 980return line ; 981 } 982 983KeyTyped = false; 984cp -> current_line ++ ; 985wcmd_T * wp = (wcmd_T * )(cp -> lines_gap -> ga_data )+ cp -> current_line ; 986SOURCING_LNUM = wp -> lnum ; 987return xstrdup (wp -> line ); 988} 989 990/// Store a line in "gap" so that a ":while" loop can execute it again. 991static void store_loop_line (garray_T * gap ,char * line ) 992{ 993wcmd_T * p = GA_APPEND_VIA_PTR (wcmd_T ,gap ); 994p -> line = xstrdup (line ); 995p -> lnum = SOURCING_LNUM ; 996} 997 998/// If "fgetline" is get_loop_line(), return true if the getline it uses equals 999/// "func". * Otherwise return true when "fgetline" equals "func". 1000/// 1001/// @param cookie argument for fgetline() 1002bool getline_equal (LineGetter fgetline ,void * cookie ,LineGetter func ) 1003{ 1004// When "fgetline" is "get_loop_line()" use the "cookie" to find the 1005// function that's originally used to obtain the lines. This may be 1006// nested several levels. 1007LineGetter gp = fgetline ; 1008struct loop_cookie * cp = (struct loop_cookie * )cookie ; 1009while (gp == get_loop_line ) { 1010gp = cp -> lc_getline ; 1011cp = cp -> cookie ; 1012 } 1013return gp == func ; 1014} 1015 1016/// If "fgetline" is get_loop_line(), return the cookie used by the original 1017/// getline function. Otherwise return "cookie". 1018/// 1019/// @param cookie argument for fgetline() 1020void * getline_cookie (LineGetter fgetline ,void * cookie ) 1021{ 1022// When "fgetline" is "get_loop_line()" use the "cookie" to find the 1023// cookie that's originally used to obtain the lines. This may be nested 1024// several levels. 1025LineGetter gp = fgetline ; 1026struct loop_cookie * cp = (struct loop_cookie * )cookie ; 1027while (gp == get_loop_line ) { 1028gp = cp -> lc_getline ; 1029cp = cp -> cookie ; 1030 } 1031return cp ; 1032} 1033 1034/// Helper function to apply an offset for buffer commands, i.e. ":bdelete", 1035/// ":bwipeout", etc. 1036/// 1037/// @return the buffer number. 1038static int compute_buffer_local_count (cmd_addr_T addr_type ,linenr_T lnum ,int offset ) 1039{ 1040int count = offset ; 1041 1042buf_T * buf = firstbuf ; 1043while (buf -> b_next != NULL && buf -> b_fnum < lnum ) { 1044buf = buf -> b_next ; 1045 } 1046while (count != 0 ) { 1047count += (count < 0 ) ?1 :-1 ; 1048buf_T * nextbuf = (offset < 0 ) ?buf -> b_prev :buf -> b_next ; 1049if (nextbuf == NULL ) { 1050break ; 1051 } 1052buf = nextbuf ; 1053if (addr_type == ADDR_LOADED_BUFFERS ) { 1054// skip over unloaded buffers 1055while (buf -> b_ml .ml_mfp == NULL ) { 1056nextbuf = (offset < 0 ) ?buf -> b_prev :buf -> b_next ; 1057if (nextbuf == NULL ) { 1058break ; 1059 } 1060buf = nextbuf ; 1061 } 1062 } 1063 } 1064// we might have gone too far, last buffer is not loaded 1065if (addr_type == ADDR_LOADED_BUFFERS ) { 1066while (buf -> b_ml .ml_mfp == NULL ) { 1067buf_T * nextbuf = (offset >=0 ) ?buf -> b_prev :buf -> b_next ; 1068if (nextbuf == NULL ) { 1069break ; 1070 } 1071buf = nextbuf ; 1072 } 1073 } 1074return buf -> b_fnum ; 1075} 1076 1077/// @return the window number of "win" or, 1078/// the number of windows if "win" is NULL 1079static int current_win_nr (const win_T * win ,exarg_T * cmdarg ) 1080FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 1081{ 1082// Whether to also match hidden / non-focusable windows (in current tab). 1083bool nonnr = cmdarg != NULL && cmdarg -> cmdidx != CMD_quit ; 1084int nr = 0 ; 1085 1086FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 1087// only include non-hidden and focusable windows 1088// (so ui2 windows are excluded) 1089bool include_this = nonnr || win_has_winnr (wp ,curtab ); 1090nr += include_this ; 1091 1092if (wp == win ) { 1093// same as in win_id2win() 1094if (!include_this ) { 1095return 0 ; 1096 } 1097break ; 1098 } 1099 } 1100return nr ; 1101} 1102 1103static int current_tab_nr (tabpage_T * tab ) 1104{ 1105int nr = 0 ; 1106 1107FOR_ALL_TABS (tp ) { 1108nr ++ ; 1109if (tp == tab ) { 1110break ; 1111 } 1112 } 1113return nr ; 1114} 1115 1116#define CURRENT_WIN_NR current_win_nr(curwin, NULL) 1117#define LAST_WIN_NR (cmdarg ) current_win_nr(NULL, (cmdarg)) 1118#define CURRENT_TAB_NR current_tab_nr(curtab) 1119#define LAST_TAB_NR current_tab_nr(NULL) 1120 1121/// Figure out the address type for ":wincmd". 1122static void get_wincmd_addr_type (const char * arg ,exarg_T * eap ) 1123{ 1124switch (* arg ) { 1125case 'S' : 1126case Ctrl_S : 1127case 's' : 1128case Ctrl_N : 1129case 'n' : 1130case 'j' : 1131case Ctrl_J : 1132case 'k' : 1133case Ctrl_K : 1134case 'T' : 1135case Ctrl_R : 1136case 'r' : 1137case 'R' : 1138case 'K' : 1139case 'J' : 1140case '+' : 1141case '-' : 1142case Ctrl__ : 1143case '_' : 1144case '|' : 1145case ']' : 1146case Ctrl_RSB : 1147case 'g' : 1148case Ctrl_G : 1149case Ctrl_V : 1150case 'v' : 1151case 'h' : 1152case Ctrl_H : 1153case 'l' : 1154case Ctrl_L : 1155case 'H' : 1156case 'L' : 1157case '>' : 1158case '<' : 1159case '}' : 1160case 'f' : 1161case 'F' : 1162case Ctrl_F : 1163case 'i' : 1164case Ctrl_I : 1165case 'd' : 1166case Ctrl_D : 1167// window size or any count 1168eap -> addr_type = ADDR_OTHER ; 1169break ; 1170 1171case Ctrl_HAT : 1172case '^' : 1173// buffer number 1174eap -> addr_type = ADDR_BUFFERS ; 1175break ; 1176 1177case Ctrl_Q : 1178case 'q' : 1179case Ctrl_C : 1180case 'c' : 1181case Ctrl_O : 1182case 'o' : 1183case Ctrl_W : 1184case 'w' : 1185case 'W' : 1186case 'x' : 1187case Ctrl_X : 1188// window number 1189eap -> addr_type = ADDR_WINDOWS ; 1190break ; 1191 1192case Ctrl_Z : 1193case 'z' : 1194case 'P' : 1195case 't' : 1196case Ctrl_T : 1197case 'b' : 1198case Ctrl_B : 1199case 'p' : 1200case Ctrl_P : 1201case '=' : 1202case CAR : 1203// no count 1204eap -> addr_type = ADDR_NONE ; 1205break ; 1206 } 1207} 1208 1209/// Skip colons and trailing whitespace, returning a pointer to the first 1210/// non-colon, non-whitespace character. 1211// 1212/// @param skipleadingwhite Skip leading whitespace too 1213static char * skip_colon_white (const char * p ,bool skipleadingwhite ) 1214{ 1215if (skipleadingwhite ) { 1216p = skipwhite (p ); 1217 } 1218 1219while (* p == ':' ) { 1220p = skipwhite (p + 1 ); 1221 } 1222 1223return (char * )p ; 1224} 1225 1226/// Set the addr type for command 1227/// 1228/// @param p pointer to character after command name in cmdline 1229void set_cmd_addr_type (exarg_T * eap ,char * p ) 1230{ 1231// ea.addr_type for user commands is set by find_ucmd 1232if (IS_USER_CMDIDX (eap -> cmdidx )) { 1233return ; 1234 } 1235if (eap -> cmdidx != CMD_SIZE ) { 1236eap -> addr_type = cmdnames [(int )eap -> cmdidx ].cmd_addr_type ; 1237 }else { 1238eap -> addr_type = ADDR_LINES ; 1239 } 1240// :wincmd range depends on the argument 1241if (eap -> cmdidx == CMD_wincmd && p != NULL ) { 1242get_wincmd_addr_type (skipwhite (p ),eap ); 1243 } 1244// :.cc in quickfix window uses line number 1245if ((eap -> cmdidx == CMD_cc || eap -> cmdidx == CMD_ll )&& bt_quickfix (curbuf )) { 1246eap -> addr_type = ADDR_OTHER ; 1247 } 1248} 1249 1250/// Get default range number for command based on its address type 1251linenr_T get_cmd_default_range (exarg_T * eap ) 1252{ 1253switch (eap -> addr_type ) { 1254case ADDR_LINES : 1255case ADDR_OTHER : 1256// Default is the cursor line number. Avoid using an invalid 1257// line number though. 1258return MIN (curwin -> w_cursor .lnum ,curbuf -> b_ml .ml_line_count ); 1259break ; 1260case ADDR_WINDOWS : 1261return CURRENT_WIN_NR ; 1262break ; 1263case ADDR_ARGUMENTS : 1264return MIN (curwin -> w_arg_idx + 1 ,ARGCOUNT ); 1265break ; 1266case ADDR_LOADED_BUFFERS : 1267case ADDR_BUFFERS : 1268return curbuf -> b_fnum ; 1269break ; 1270case ADDR_TABS : 1271return CURRENT_TAB_NR ; 1272break ; 1273case ADDR_TABS_RELATIVE : 1274case ADDR_UNSIGNED : 1275return 1 ; 1276break ; 1277case ADDR_QUICKFIX : 1278return (linenr_T )qf_get_cur_idx (eap ); 1279break ; 1280case ADDR_QUICKFIX_VALID : 1281return qf_get_cur_valid_idx (eap ); 1282break ; 1283default : 1284return 0 ; 1285// Will give an error later if a range is found. 1286break ; 1287 } 1288} 1289 1290/// Set default command range for -range=% based on the addr type of the command 1291void set_cmd_dflall_range (exarg_T * eap ) 1292{ 1293buf_T * buf ; 1294 1295eap -> line1 = 1 ; 1296switch (eap -> addr_type ) { 1297case ADDR_LINES : 1298case ADDR_OTHER : 1299eap -> line2 = curbuf -> b_ml .ml_line_count ; 1300break ; 1301case ADDR_LOADED_BUFFERS : 1302buf = firstbuf ; 1303while (buf -> b_next != NULL && buf -> b_ml .ml_mfp == NULL ) { 1304buf = buf -> b_next ; 1305 } 1306eap -> line1 = buf -> b_fnum ; 1307buf = lastbuf ; 1308while (buf -> b_prev != NULL && buf -> b_ml .ml_mfp == NULL ) { 1309buf = buf -> b_prev ; 1310 } 1311eap -> line2 = buf -> b_fnum ; 1312break ; 1313case ADDR_BUFFERS : 1314eap -> line1 = firstbuf -> b_fnum ; 1315eap -> line2 = lastbuf -> b_fnum ; 1316break ; 1317case ADDR_WINDOWS : 1318eap -> line2 = LAST_WIN_NR (eap ); 1319break ; 1320case ADDR_TABS : 1321eap -> line2 = LAST_TAB_NR ; 1322break ; 1323case ADDR_TABS_RELATIVE : 1324eap -> line2 = 1 ; 1325break ; 1326case ADDR_ARGUMENTS : 1327if (ARGCOUNT == 0 ) { 1328eap -> line1 = eap -> line2 = 0 ; 1329 }else { 1330eap -> line2 = ARGCOUNT ; 1331 } 1332break ; 1333case ADDR_QUICKFIX_VALID : 1334eap -> line2 = (linenr_T )qf_get_valid_size (eap ); 1335if (eap -> line2 == 0 ) { 1336eap -> line2 = 1 ; 1337 } 1338break ; 1339case ADDR_NONE : 1340case ADDR_UNSIGNED : 1341case ADDR_QUICKFIX : 1342iemsg (_ ("INTERNAL: Cannot use EX_DFLALL " 1343"with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX" )); 1344break ; 1345 } 1346} 1347 1348static void parse_register (exarg_T * eap ) 1349{ 1350// Accept numbered register only when no count allowed (:put) 1351if ((eap -> argt & EX_REGSTR ) 1352&& * eap -> arg != NUL 1353// Do not allow register = for user commands 1354&& (!IS_USER_CMDIDX (eap -> cmdidx )|| * eap -> arg != '=' ) 1355&& !((eap -> argt & EX_COUNT )&& ascii_isdigit (* eap -> arg ))) { 1356if (valid_yank_reg (* eap -> arg , 1357 (!IS_USER_CMDIDX (eap -> cmdidx ) 1358&& eap -> cmdidx != CMD_put && eap -> cmdidx != CMD_iput ))) { 1359eap -> regname = (uint8_t )(* eap -> arg ++ ); 1360// for '=' register: accept the rest of the line as an expression 1361if (eap -> arg [-1 ]== '=' && eap -> arg [0 ]!= NUL ) { 1362if (!eap -> skip ) { 1363set_expr_line (xstrdup (eap -> arg )); 1364 } 1365eap -> arg += strlen (eap -> arg ); 1366 } 1367eap -> arg = skipwhite (eap -> arg ); 1368 } 1369 } 1370} 1371 1372// Change line1 and line2 of Ex command to use count 1373void set_cmd_count (exarg_T * eap ,linenr_T count ,bool validate ) 1374{ 1375if (eap -> addr_type != ADDR_LINES ) {// e.g. :buffer 2, :sleep 3 1376eap -> line2 = count ; 1377if (eap -> addr_count == 0 ) { 1378eap -> addr_count = 1 ; 1379 } 1380 }else { 1381eap -> line1 = eap -> line2 ; 1382if (eap -> line2 >=INT32_MAX - (count - 1 )) { 1383eap -> line2 = INT32_MAX ; 1384 }else { 1385eap -> line2 += count - 1 ; 1386 } 1387eap -> addr_count ++ ; 1388// Be vi compatible: no error message for out of range. 1389if (validate && eap -> line2 > curbuf -> b_ml .ml_line_count ) { 1390eap -> line2 = curbuf -> b_ml .ml_line_count ; 1391 } 1392 } 1393} 1394 1395static int parse_count (exarg_T * eap ,const char ** errormsg ,bool validate ) 1396{ 1397// Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a 1398// count, it's a buffer name. 1399char * p ; 1400 1401if ((eap -> argt & EX_COUNT )&& ascii_isdigit (* eap -> arg ) 1402&& (!(eap -> argt & EX_BUFNAME )|| * (p = skipdigits (eap -> arg + 1 ))== NUL 1403|| ascii_iswhite (* p ))) { 1404linenr_T n = getdigits_int32 (& eap -> arg , false,INT32_MAX ); 1405eap -> arg = skipwhite (eap -> arg ); 1406 1407if (eap -> args != NULL ) { 1408assert (eap -> argc > 0 && eap -> arg >=eap -> args [0 ]); 1409// If eap->arg is still pointing to the first argument, just make eap->args[0] point to the 1410// same location. This is needed for usecases like vim.cmd.sleep('10m'). If eap->arg is 1411// pointing outside the first argument, shift arguments by 1. 1412if (eap -> arg < eap -> args [0 ]+ eap -> arglens [0 ]) { 1413eap -> arglens [0 ]-= (size_t )(eap -> arg - eap -> args [0 ]); 1414eap -> args [0 ]= eap -> arg ; 1415 }else { 1416shift_cmd_args (eap ); 1417 } 1418 } 1419 1420if (n <=0 && (eap -> argt & EX_ZEROR )== 0 ) { 1421if (errormsg != NULL ) { 1422* errormsg = _ (e_zerocount ); 1423 } 1424return FAIL ; 1425 } 1426set_cmd_count (eap ,n ,validate ); 1427 } 1428 1429return OK ; 1430} 1431 1432/// Check if command is not implemented 1433bool is_cmd_ni (cmdidx_T cmdidx ) 1434{ 1435return !IS_USER_CMDIDX (cmdidx )&& (cmdnames [cmdidx ].cmd_func == ex_ni 1436|| cmdnames [cmdidx ].cmd_func == ex_script_ni ); 1437} 1438 1439// Find the command name after skipping range specifiers. 1440static char * find_excmd_after_range (exarg_T * eap ) 1441{ 1442// Save location after command modifiers. 1443char * cmd = eap -> cmd ; 1444eap -> cmd = skip_range (eap -> cmd ,NULL ); 1445char * p = find_ex_command (eap ,NULL ); 1446eap -> cmd = cmd ;// Restore original position for address parsing 1447return p ; 1448} 1449 1450// Set the forceit flag based on the presence of '!' after the command. 1451static bool parse_bang (const exarg_T * eap ,char ** p ) 1452{ 1453if (** p == '!' 1454&& eap -> cmdidx != CMD_substitute 1455&& eap -> cmdidx != CMD_smagic 1456&& eap -> cmdidx != CMD_snomagic ) { 1457 (* p )++ ; 1458return true; 1459 } 1460return false; 1461} 1462 1463/// Check if command expects expression arguments that need special parsing 1464bool cmd_has_expr_args (cmdidx_T cmdidx ) 1465{ 1466return cmdidx == CMD_execute 1467|| cmdidx == CMD_echo 1468|| cmdidx == CMD_echon 1469|| cmdidx == CMD_echomsg 1470|| cmdidx == CMD_echoerr ; 1471} 1472 1473/// Parse command line and return information about the first command. 1474/// If parsing is done successfully, need to free cmod_filter_pat and cmod_filter_regmatch.regprog 1475/// after calling, usually done using undo_cmdmod() or execute_cmd(). 1476/// 1477/// @param cmdline Command line string 1478/// @param[out] eap Ex command arguments 1479/// @param[out] cmod Command modifiers 1480/// @param[out] errormsg Error message, if any 1481/// @param skipmods Skip modifiers, nextcmd, register and count. Keep leading 1482/// whitespace of the arguments. 1483/// 1484/// @return Success or failure 1485bool parse_cmdline (char ** cmdline ,exarg_T * eap ,cmdmod_T * cmod ,const char ** errormsg , 1486bool skipmods ) 1487{ 1488char * after_modifier = NULL ; 1489bool retval = false; 1490// parsing the command range may require moving the cursor 1491const pos_T save_cursor = curwin -> w_cursor ; 1492// parsing the command range may set the last search pattern 1493save_last_search_pattern (); 1494 1495// Initialize cmod 1496CLEAR_POINTER (cmod ); 1497 1498// Initialize eap 1499* eap = (exarg_T ){ 1500 .line1 = 1 , 1501 .line2 = 1 , 1502 .cmd = * cmdline , 1503 .cmdlinep = cmdline , 1504 .ea_getline = NULL , 1505 .cookie = NULL , 1506 }; 1507 1508char * orig_cmd = eap -> cmd ; 1509// If parse command modifiers failed but modifiers were passed, continue 1510int result = skipmods ?OK :parse_command_modifiers (eap ,errormsg ,cmod , false); 1511after_modifier = eap -> cmd ; 1512if (result == FAIL && after_modifier == orig_cmd ) { 1513 gotoend ; 1514 } 1515 1516// We need the command name to know what kind of range it uses. 1517char * p = find_excmd_after_range (eap ); 1518if (p == NULL ) { 1519* errormsg = _ (e_ambiguous_use_of_user_defined_command ); 1520 gotoend ; 1521 } 1522 1523// Set command address type and parse command range 1524set_cmd_addr_type (eap ,p ); 1525if (parse_cmd_address (eap ,errormsg , true)== FAIL ) { 1526 gotoend ; 1527 } 1528 1529// Skip colon and whitespace 1530eap -> cmd = skip_colon_white (eap -> cmd , true); 1531// Fail if command is a comment 1532if (* eap -> cmd == '"' ) { 1533 gotoend ; 1534 } 1535// Fail only if: empty command AND no range AND no modifier 1536if (* eap -> cmd == NUL && eap -> addr_count == 0 && after_modifier == * cmdline ) { 1537 gotoend ; 1538 } 1539 1540// Allow range-only (:1) or modifier-only (:aboveleft) commands. 1541if (* eap -> cmd == NUL && eap -> cmdidx == CMD_SIZE ) { 1542eap -> arg = eap -> cmd ; 1543if (eap -> addr_count > 0 ) { 1544eap -> argt = EX_RANGE ; 1545 }else { 1546eap -> argt = 0 ; 1547eap -> addr_type = ADDR_NONE ; 1548 } 1549retval = true; 1550 gotoend ; 1551 } 1552 1553// Fail if command is invalid 1554if (eap -> cmdidx == CMD_SIZE ) { 1555xstrlcpy (IObuff ,_ (e_not_an_editor_command ),IOSIZE ); 1556// If the modifier was parsed OK the error must be in the following command 1557char * cmdname = after_modifier ?after_modifier :* cmdline ; 1558append_command (cmdname ); 1559* errormsg = IObuff ; 1560 gotoend ; 1561 } 1562 1563// Correctly set 'forceit' for commands 1564eap -> forceit = parse_bang (eap ,& p ); 1565 1566// Parse arguments. 1567if (!IS_USER_CMDIDX (eap -> cmdidx )) { 1568eap -> argt = cmdnames [(int )eap -> cmdidx ].cmd_argt ; 1569 } 1570if (skipmods ) { 1571// Only skip the space before the arguments, the rest belongs to them. 1572eap -> arg = p + (* p == ' ' ); 1573 }else { 1574// Skip to start of argument. 1575// Don't do this for the ":!" command, because ":!! -l" needs the space. 1576eap -> arg = eap -> cmdidx == CMD_bang ?p :skipwhite (p ); 1577 } 1578 1579// Don't treat ":r! filter" like a bang 1580if (eap -> cmdidx == CMD_read && eap -> forceit ) { 1581eap -> forceit = false;// :r! filter 1582 } 1583 1584// Check for '|' to separate commands and '"' to start comments. 1585// Don't do this for ":read !cmd" and ":write !cmd". 1586if (!skipmods && (eap -> argt & EX_TRLBAR )) { 1587separate_nextcmd (eap ); 1588 }else if (!skipmods && cmd_has_expr_args (eap -> cmdidx )) { 1589// For commands without EX_TRLBAR, check for '|' separator 1590// by skipping over expressions (including string literals) 1591char * arg = eap -> arg ; 1592while (* arg != NUL && * arg != '|' && * arg != '\n' ) { 1593char * start = arg ; 1594emsg_skip ++ ; 1595skip_expr (& arg ,NULL ); 1596emsg_skip -- ; 1597// If skip_expr didn't advance, move forward to avoid infinite loop 1598if (arg == start ) { 1599arg ++ ; 1600 } 1601 } 1602if (* arg == '|' || * arg == '\n' ) { 1603eap -> nextcmd = check_nextcmd (arg ); 1604* arg = NUL ; 1605 } 1606 } 1607// Fail if command doesn't support bang but is used with a bang 1608if (!(eap -> argt & EX_BANG )&& eap -> forceit ) { 1609* errormsg = _ (e_nobang ); 1610 gotoend ; 1611 } 1612// Fail if command doesn't support a range but it is given a range 1613if (!(eap -> argt & EX_RANGE )&& eap -> addr_count > 0 ) { 1614* errormsg = _ (e_norange ); 1615 gotoend ; 1616 } 1617// Set default range for command if required 1618if ((eap -> argt & EX_DFLALL )&& eap -> addr_count == 0 ) { 1619set_cmd_dflall_range (eap ); 1620 } 1621 1622// Parse register and count 1623if (!skipmods ) { 1624parse_register (eap ); 1625if (parse_count (eap ,errormsg , false)== FAIL ) { 1626 gotoend ; 1627 } 1628 } 1629 1630// Remove leading whitespace and colon from next command 1631if (eap -> nextcmd ) { 1632eap -> nextcmd = skip_colon_white (eap -> nextcmd , true); 1633 } 1634 1635// Set the "magic" values (characters that get treated specially) 1636eap -> magic .file = eap -> argt & EX_XFILE ; 1637eap -> magic .bar = eap -> argt & EX_TRLBAR ; 1638 1639retval = true; 1640end : 1641if (!retval ) { 1642undo_cmdmod (cmod ); 1643 } 1644curwin -> w_cursor = save_cursor ; 1645restore_last_search_pattern (); 1646return retval ; 1647} 1648 1649// Shift Ex-command arguments to the right. 1650static void shift_cmd_args (exarg_T * eap ) 1651{ 1652assert (eap -> args != NULL && eap -> argc > 0 ); 1653 1654char ** oldargs = eap -> args ; 1655size_t * oldarglens = eap -> arglens ; 1656 1657eap -> argc -- ; 1658eap -> args = eap -> argc > 0 ?xcalloc (eap -> argc ,sizeof (char * )) :NULL ; 1659eap -> arglens = eap -> argc > 0 ?xcalloc (eap -> argc ,sizeof (size_t )) :NULL ; 1660 1661for (size_t i = 0 ;i < eap -> argc ;i ++ ) { 1662eap -> args [i ]= oldargs [i + 1 ]; 1663eap -> arglens [i ]= oldarglens [i + 1 ]; 1664 } 1665 1666// If there are no arguments, make eap->arg point to the end of string. 1667eap -> arg = (eap -> argc > 0 ?eap -> args [0 ] : (oldargs [0 ]+ oldarglens [0 ])); 1668 1669xfree (oldargs ); 1670xfree (oldarglens ); 1671} 1672 1673static int execute_cmd0 (int * retv ,exarg_T * eap ,const char ** errormsg ,bool preview ) 1674{ 1675// If filename expansion is enabled, expand filenames 1676if (eap -> argt & EX_XFILE ) { 1677if (expand_filename (eap ,eap -> cmdlinep ,errormsg )== FAIL ) { 1678return FAIL ; 1679 } 1680 } 1681 1682// Accept buffer name. Cannot be used at the same time with a buffer 1683// number. Don't do this for a user command. 1684if ((eap -> argt & EX_BUFNAME )&& * eap -> arg != NUL && eap -> addr_count == 0 1685&& !IS_USER_CMDIDX (eap -> cmdidx )) { 1686if (eap -> args == NULL ) { 1687// If argument positions are not specified, search the argument for the buffer name. 1688// :bdelete, :bwipeout and :bunload take several arguments, separated by spaces: 1689// find next space (skipping over escaped characters). 1690// The others take one argument: ignore trailing spaces. 1691char * p ; 1692 1693if (eap -> cmdidx == CMD_bdelete || eap -> cmdidx == CMD_bwipeout 1694|| eap -> cmdidx == CMD_bunload ) { 1695p = skiptowhite_esc (eap -> arg ); 1696 }else { 1697p = eap -> arg + strlen (eap -> arg ); 1698while (p > eap -> arg && ascii_iswhite (p [-1 ])) { 1699p -- ; 1700 } 1701 } 1702eap -> line2 = buflist_findpat (eap -> arg ,p , (eap -> argt & EX_BUFUNL )!= 0 , 1703 false, false); 1704eap -> addr_count = 1 ; 1705eap -> arg = skipwhite (p ); 1706 }else { 1707// If argument positions are specified, just use the first argument 1708eap -> line2 = buflist_findpat (eap -> args [0 ], 1709eap -> args [0 ]+ eap -> arglens [0 ], 1710 (eap -> argt & EX_BUFUNL )!= 0 , false, false); 1711eap -> addr_count = 1 ; 1712shift_cmd_args (eap ); 1713 } 1714if (eap -> line2 < 0 ) {// failed 1715return FAIL ; 1716 } 1717 } 1718 1719// The :try command saves the emsg_silent flag, reset it here when 1720// ":silent! try" was used, it should only apply to :try itself. 1721if (eap -> cmdidx == CMD_try && cmdmod .cmod_did_esilent > 0 ) { 1722emsg_silent -= cmdmod .cmod_did_esilent ; 1723emsg_silent = MAX (emsg_silent ,0 ); 1724cmdmod .cmod_did_esilent = 0 ; 1725 } 1726 1727// Execute the command 1728if (IS_USER_CMDIDX (eap -> cmdidx )) { 1729// Execute a user-defined command. 1730* retv = do_ucmd (eap ,preview ); 1731 }else { 1732// Call the function to execute the builtin command or the preview callback. 1733eap -> errmsg = NULL ; 1734if (preview ) { 1735* retv = (cmdnames [eap -> cmdidx ].cmd_preview_func )(eap ,cmdpreview_get_ns (), 1736cmdpreview_get_bufnr ()); 1737 }else { 1738 (cmdnames [eap -> cmdidx ].cmd_func )(eap ); 1739 } 1740if (eap -> errmsg != NULL ) { 1741* errormsg = eap -> errmsg ; 1742 } 1743 } 1744 1745return OK ; 1746} 1747 1748/// Execute an Ex command using parsed command line information. 1749/// Does not do any validation of the Ex command arguments. 1750/// 1751/// @param eap Ex-command arguments 1752/// @param cmod Command modifiers 1753/// @param preview Execute command preview callback instead of actual command 1754int execute_cmd (exarg_T * eap ,cmdmod_T * cmod ,bool preview ) 1755{ 1756int retv = 0 ; 1757if (do_cmdline_start ()== FAIL ) { 1758emsg (_ (e_command_too_recursive )); 1759return retv ; 1760 } 1761 1762const char * errormsg = NULL ; 1763 1764cmdmod_T save_cmdmod = cmdmod ; 1765cmdmod = * cmod ; 1766 1767// Apply command modifiers 1768apply_cmdmod (& cmdmod ); 1769 1770if (!MODIFIABLE (curbuf )&& (eap -> argt & EX_MODIFY ) 1771// allow :put in terminals 1772&& !(curbuf -> terminal && (eap -> cmdidx == CMD_put || eap -> cmdidx == CMD_iput ))) { 1773errormsg = _ (e_modifiable ); 1774 gotoend ; 1775 } 1776if (!IS_USER_CMDIDX (eap -> cmdidx )) { 1777if (text_locked ()&& !(eap -> argt & EX_LOCK_OK )) { 1778// Command not allowed when text is locked 1779errormsg = _ (get_text_locked_msg ()); 1780 gotoend ; 1781 } 1782 } 1783// Disallow editing another buffer when "curbuf->b_ro_locked" is set. 1784// Do allow ":checktime" (it is postponed). 1785// Do allow ":edit" (check for an argument later). 1786// Do allow ":file" with no arguments 1787if (!(eap -> argt & EX_BUFLOCK_OK ) 1788&& eap -> cmdidx != CMD_checktime 1789&& eap -> cmdidx != CMD_edit 1790&& !(eap -> cmdidx == CMD_file && * eap -> arg == NUL ) 1791&& !IS_USER_CMDIDX (eap -> cmdidx ) 1792&& curbuf_locked ()) { 1793 gotoend ; 1794 } 1795 1796correct_range (eap ); 1797if (eap -> cmdidx == CMD_SIZE && eap -> addr_count > 0 ) { 1798errormsg = ex_range_without_command (eap ); 1799 gotoend ; 1800 } 1801 1802if (((eap -> argt & EX_WHOLEFOLD )|| eap -> addr_count >=2 )&& !global_busy 1803&& eap -> addr_type == ADDR_LINES ) { 1804// Put the first line at the start of a closed fold, put the last line 1805// at the end of a closed fold. 1806hasFolding (curwin ,eap -> line1 ,& eap -> line1 ,NULL ); 1807hasFolding (curwin ,eap -> line2 ,NULL ,& eap -> line2 ); 1808 } 1809 1810// Use first argument as count when possible 1811if (parse_count (eap ,& errormsg , true)== FAIL ) { 1812 gotoend ; 1813 } 1814 1815cstack_T cstack = { .cs_idx = -1 }; 1816eap -> cstack = & cstack ; 1817 1818// Execute the command 1819execute_cmd0 (& retv ,eap ,& errormsg ,preview ); 1820 1821end : 1822if (errormsg != NULL && * errormsg != NUL ) { 1823emsg (errormsg ); 1824 } 1825 1826// Undo command modifiers 1827undo_cmdmod (& cmdmod ); 1828cmdmod = save_cmdmod ; 1829 1830do_cmdline_end (); 1831return retv ; 1832} 1833 1834static void profile_cmd (const exarg_T * eap ,cstack_T * cstack ,LineGetter fgetline ,void * cookie ) 1835{ 1836// Count this line for profiling if skip is true. 1837if (do_profiling == PROF_YES 1838&& (!eap -> skip || cstack -> cs_idx == 0 1839|| (cstack -> cs_idx > 0 1840&& (cstack -> cs_flags [cstack -> cs_idx - 1 ]& CSF_ACTIVE )))) { 1841bool skip = did_emsg || got_int || did_throw ; 1842 1843if (eap -> cmdidx == CMD_catch ) { 1844skip = !skip && !(cstack -> cs_idx >=0 1845&& (cstack -> cs_flags [cstack -> cs_idx ]& CSF_THROWN ) 1846&& !(cstack -> cs_flags [cstack -> cs_idx ]& CSF_CAUGHT )); 1847 }else if (eap -> cmdidx == CMD_else || eap -> cmdidx == CMD_elseif ) { 1848skip = skip || !(cstack -> cs_idx >=0 1849&& !(cstack -> cs_flags [cstack -> cs_idx ] 1850& (CSF_ACTIVE |CSF_TRUE ))); 1851 }else if (eap -> cmdidx == CMD_finally ) { 1852skip = false; 1853 }else if (eap -> cmdidx != CMD_endif 1854&& eap -> cmdidx != CMD_endfor 1855&& eap -> cmdidx != CMD_endtry 1856&& eap -> cmdidx != CMD_endwhile ) { 1857skip = eap -> skip ; 1858 } 1859 1860if (!skip ) { 1861if (getline_equal (fgetline ,cookie ,get_func_line )) { 1862func_line_exec (getline_cookie (fgetline ,cookie )); 1863 }else if (getline_equal (fgetline ,cookie ,getsourceline )) { 1864script_line_exec (); 1865 } 1866 } 1867 } 1868} 1869 1870static bool skip_cmd (const exarg_T * eap ) 1871{ 1872// Skip the command when it's not going to be executed. 1873// The commands like :if, :endif, etc. always need to be executed. 1874// Also make an exception for commands that handle a trailing command 1875// themselves. 1876if (eap -> skip ) { 1877switch (eap -> cmdidx ) { 1878// commands that need evaluation 1879case CMD_while : 1880case CMD_endwhile : 1881case CMD_for : 1882case CMD_endfor : 1883case CMD_if : 1884case CMD_elseif : 1885case CMD_else : 1886case CMD_endif : 1887case CMD_try : 1888case CMD_catch : 1889case CMD_finally : 1890case CMD_endtry : 1891case CMD_function : 1892break ; 1893 1894// commands that read a block of lines 1895case CMD_append : 1896case CMD_change : 1897case CMD_insert : 1898case CMD_loadkeymap : 1899break ; 1900 1901// Commands that handle '|' themselves. Check: A command should 1902// either have the EX_TRLBAR flag, appear in this list or appear in 1903// the list at ":help :bar". 1904case CMD_aboveleft : 1905case CMD_and : 1906case CMD_belowright : 1907case CMD_botright : 1908case CMD_browse : 1909case CMD_call : 1910case CMD_confirm : 1911case CMD_const : 1912case CMD_delfunction : 1913case CMD_djump : 1914case CMD_dlist : 1915case CMD_dsearch : 1916case CMD_dsplit : 1917case CMD_echo : 1918case CMD_echoerr : 1919case CMD_echomsg : 1920case CMD_echon : 1921case CMD_eval : 1922case CMD_execute : 1923case CMD_filter : 1924case CMD_help : 1925case CMD_hide : 1926case CMD_horizontal : 1927case CMD_ijump : 1928case CMD_ilist : 1929case CMD_isearch : 1930case CMD_isplit : 1931case CMD_keepalt : 1932case CMD_keepjumps : 1933case CMD_keepmarks : 1934case CMD_keeppatterns : 1935case CMD_leftabove : 1936case CMD_let : 1937case CMD_lockmarks : 1938case CMD_lockvar : 1939case CMD_lua : 1940case CMD_match : 1941case CMD_mzscheme : 1942case CMD_noautocmd : 1943case CMD_noswapfile : 1944case CMD_perl : 1945case CMD_psearch : 1946case CMD_python : 1947case CMD_py3 : 1948case CMD_python3 : 1949case CMD_pythonx : 1950case CMD_pyx : 1951case CMD_return : 1952case CMD_rightbelow : 1953case CMD_ruby : 1954case CMD_silent : 1955case CMD_smagic : 1956case CMD_snomagic : 1957case CMD_substitute : 1958case CMD_syntax : 1959case CMD_tab : 1960case CMD_tcl : 1961case CMD_throw : 1962case CMD_tilde : 1963case CMD_topleft : 1964case CMD_unlet : 1965case CMD_unlockvar : 1966case CMD_verbose : 1967case CMD_vertical : 1968case CMD_wincmd : 1969break ; 1970 1971default : 1972return true; 1973 } 1974 } 1975return false; 1976} 1977 1978/// Execute one Ex command. 1979/// 1980/// If "flags" has DOCMD_VERBOSE, the command will be included in the error 1981/// message. 1982/// 1983/// 1. skip comment lines and leading space 1984/// 2. handle command modifiers 1985/// 3. skip over the range to find the command 1986/// 4. parse the range 1987/// 5. parse the command 1988/// 6. parse arguments 1989/// 7. switch on command name 1990/// 1991/// Note: "fgetline" can be NULL. 1992/// 1993/// This function may be called recursively! 1994/// 1995/// @param cookie argument for fgetline() 1996static char * do_one_cmd (char ** cmdlinep ,int flags ,cstack_T * cstack ,LineGetter fgetline , 1997void * cookie ) 1998{ 1999const char * errormsg = NULL ;// error message 2000const int save_reg_executing = reg_executing ; 2001const bool save_pending_end_reg_executing = pending_end_reg_executing ; 2002 2003exarg_T ea = { 2004 .line1 = 1 , 2005 .line2 = 1 , 2006 }; 2007ex_nesting_level ++ ; 2008 2009// When the last file has not been edited :q has to be typed twice. 2010if (quitmore 2011// avoid that a function call in 'statusline' does this 2012&& !getline_equal (fgetline ,cookie ,get_func_line ) 2013// avoid that an autocommand, e.g. QuitPre, does this 2014&& !getline_equal (fgetline ,cookie ,getnextac )) { 2015quitmore -- ; 2016 } 2017 2018// Reset browse, confirm, etc.. They are restored when returning, for 2019// recursive calls. 2020cmdmod_T save_cmdmod = cmdmod ; 2021 2022// "#!anything" is handled like a comment. 2023if ((* cmdlinep )[0 ]== '#' && (* cmdlinep )[1 ]== '!' ) { 2024 gotodoend ; 2025 } 2026 2027// 1. Skip comment lines and leading white space and colons. 2028// 2. Handle command modifiers. 2029 2030// The "ea" structure holds the arguments that can be used. 2031ea .cmd = * cmdlinep ; 2032ea .cmdlinep = cmdlinep ; 2033ea .ea_getline = fgetline ; 2034ea .cookie = cookie ; 2035ea .cstack = cstack ; 2036 2037if (parse_command_modifiers (& ea ,& errormsg ,& cmdmod , false)== FAIL ) { 2038 gotodoend ; 2039 } 2040apply_cmdmod (& cmdmod ); 2041 2042char * after_modifier = ea .cmd ; 2043 2044ea .skip = (did_emsg 2045|| got_int 2046|| did_throw 2047|| (cstack -> cs_idx >=0 2048&& !(cstack -> cs_flags [cstack -> cs_idx ]& CSF_ACTIVE ))); 2049 2050// 3. Skip over the range to find the command. Let "p" point to after it. 2051// 2052// We need the command to know what kind of range it uses. 2053char * p = find_excmd_after_range (& ea ); 2054profile_cmd (& ea ,cstack ,fgetline ,cookie ); 2055 2056if (!exiting ) { 2057// May go to debug mode. If this happens and the ">quit" debug command is 2058// used, throw an interrupt exception and skip the next command. 2059dbg_check_breakpoint (& ea ); 2060 } 2061if (!ea .skip && got_int ) { 2062ea .skip = true; 2063do_intthrow (cstack ); 2064 } 2065 2066// 4. Parse a range specifier of the form: addr [,addr] [;addr] .. 2067// 2068// where 'addr' is: 2069// 2070// % (entire file) 2071// $ [+-NUM] 2072// 'x [+-NUM] (where x denotes a currently defined mark) 2073// . [+-NUM] 2074// [+-NUM].. 2075// NUM 2076// 2077// The ea.cmd pointer is updated to point to the first character following the 2078// range spec. If an initial address is found, but no second, the upper bound 2079// is equal to the lower. 2080set_cmd_addr_type (& ea ,p ); 2081 2082if (parse_cmd_address (& ea ,& errormsg , false)== FAIL ) { 2083 gotodoend ; 2084 } 2085 2086// 5. Parse the command. 2087 2088// Skip ':' and any white space 2089ea .cmd = skip_colon_white (ea .cmd , true); 2090 2091// If we got a line, but no command, then go to the line. 2092// If we find a '|' or '\n' we set ea.nextcmd. 2093if (* ea .cmd == NUL || * ea .cmd == '"' 2094|| (ea .nextcmd = check_nextcmd (ea .cmd ))!= NULL ) { 2095// strange vi behaviour: 2096// ":3" jumps to line 3 2097// ":3|..." prints line 3 2098// ":|" prints current line 2099if (ea .skip ) {// skip this if inside :if 2100 gotodoend ; 2101 } 2102assert (errormsg == NULL ); 2103errormsg = ex_range_without_command (& ea ); 2104 gotodoend ; 2105 } 2106 2107// If this looks like an undefined user command and there are CmdUndefined 2108// autocommands defined, trigger the matching autocommands. 2109if (p != NULL && ea .cmdidx == CMD_SIZE && !ea .skip 2110&& ASCII_ISUPPER (* ea .cmd ) 2111&& has_event (EVENT_CMDUNDEFINED )) { 2112char * cmdname = ea .cmd ; 2113while (ASCII_ISALNUM (* cmdname )) { 2114cmdname ++ ; 2115 } 2116cmdname = xmemdupz (ea .cmd , (size_t )(cmdname - ea .cmd )); 2117int ret = apply_autocmds (EVENT_CMDUNDEFINED ,cmdname ,cmdname , true,NULL ); 2118xfree (cmdname ); 2119// If the autocommands did something and didn't cause an error, try 2120// finding the command again. 2121p = (ret && !aborting ()) ?find_ex_command (& ea ,NULL ) :ea .cmd ; 2122 } 2123 2124if (p == NULL ) { 2125if (!ea .skip ) { 2126errormsg = _ (e_ambiguous_use_of_user_defined_command ); 2127 } 2128 gotodoend ; 2129 } 2130 2131// Check for wrong commands. 2132if (ea .cmdidx == CMD_SIZE ) { 2133if (!ea .skip ) { 2134xstrlcpy (IObuff ,_ (e_not_an_editor_command ),IOSIZE ); 2135// If the modifier was parsed OK the error must be in the following 2136// command 2137char * cmdname = after_modifier ?after_modifier :* cmdlinep ; 2138if (!(flags & DOCMD_VERBOSE )) { 2139append_command (cmdname ); 2140 } 2141errormsg = IObuff ; 2142did_emsg_syntax = true; 2143verify_command (cmdname ); 2144 } 2145 gotodoend ; 2146 } 2147 2148// set when Not Implemented 2149const int ni = is_cmd_ni (ea .cmdidx ); 2150 2151// Determine if command has forceit flag ('!') 2152ea .forceit = parse_bang (& ea ,& p ); 2153 2154// 6. Parse arguments. Then check for errors. 2155if (!IS_USER_CMDIDX (ea .cmdidx )) { 2156ea .argt = cmdnames [(int )ea .cmdidx ].cmd_argt ; 2157 } 2158 2159if (!ea .skip ) { 2160if (sandbox != 0 && !(ea .argt & EX_SBOXOK )) { 2161// Command not allowed in sandbox. 2162errormsg = _ (e_sandbox ); 2163 gotodoend ; 2164 } 2165if (!MODIFIABLE (curbuf )&& (ea .argt & EX_MODIFY ) 2166// allow :put in terminals 2167&& !(curbuf -> terminal && (ea .cmdidx == CMD_put || ea .cmdidx == CMD_iput ))) { 2168// Command not allowed in non-'modifiable' buffer 2169errormsg = _ (e_modifiable ); 2170 gotodoend ; 2171 } 2172 2173if (!IS_USER_CMDIDX (ea .cmdidx )) { 2174if (text_locked ()&& !(ea .argt & EX_LOCK_OK )) { 2175// Command not allowed when text is locked 2176errormsg = _ (get_text_locked_msg ()); 2177 gotodoend ; 2178 } 2179 } 2180 2181// Disallow editing another buffer when "curbuf->b_ro_locked" is set. 2182// Do allow ":checktime" (it is postponed). 2183// Do allow ":edit" (check for an argument later). 2184// Do allow ":file" with no arguments (check for an argument later). 2185if (!(ea .argt & EX_BUFLOCK_OK ) 2186&& ea .cmdidx != CMD_checktime 2187&& ea .cmdidx != CMD_edit 2188&& ea .cmdidx != CMD_file 2189&& !IS_USER_CMDIDX (ea .cmdidx ) 2190&& curbuf_locked ()) { 2191 gotodoend ; 2192 } 2193 2194if (!ni && !(ea .argt & EX_RANGE )&& ea .addr_count > 0 ) { 2195// no range allowed 2196errormsg = _ (e_norange ); 2197 gotodoend ; 2198 } 2199 } 2200 2201if (!ni && !(ea .argt & EX_BANG )&& ea .forceit ) {// no <!> allowed 2202errormsg = _ (e_nobang ); 2203 gotodoend ; 2204 } 2205 2206// Don't complain about the range if it is not used 2207// (could happen if line_count is accidentally set to 0). 2208if (!ea .skip && !ni && (ea .argt & EX_RANGE )) { 2209// If the range is backwards, ask for confirmation and, if given, swap 2210// ea.line1 & ea.line2 so it's forwards again. 2211// When global command is busy, don't ask, will fail below. 2212if (!global_busy && ea .line1 > ea .line2 ) { 2213if (msg_silent == 0 ) { 2214if ((flags & DOCMD_VERBOSE )|| silent_mode ) { 2215errormsg = _ ("E493: Backwards range given" ); 2216 gotodoend ; 2217 } 2218if (ask_yesno (_ ("Backwards range given, OK to swap" ))!= 'y' ) { 2219 gotodoend ; 2220 } 2221 } 2222linenr_T lnum = ea .line1 ; 2223ea .line1 = ea .line2 ; 2224ea .line2 = lnum ; 2225 } 2226if ((errormsg = invalid_range (& ea ))!= NULL ) { 2227 gotodoend ; 2228 } 2229 } 2230 2231if ((ea .addr_type == ADDR_OTHER )&& ea .addr_count == 0 ) { 2232// default is 1, not cursor 2233ea .line2 = 1 ; 2234 } 2235 2236correct_range (& ea ); 2237 2238if (((ea .argt & EX_WHOLEFOLD )|| ea .addr_count >=2 )&& !global_busy 2239&& ea .addr_type == ADDR_LINES ) { 2240// Put the first line at the start of a closed fold, put the last line 2241// at the end of a closed fold. 2242hasFolding (curwin ,ea .line1 ,& ea .line1 ,NULL ); 2243hasFolding (curwin ,ea .line2 ,NULL ,& ea .line2 ); 2244 } 2245 2246// For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg' 2247// option here, so things like % get expanded. 2248p = replace_makeprg (& ea ,p ,cmdlinep ); 2249if (p == NULL ) { 2250 gotodoend ; 2251 } 2252 2253// Skip to start of argument. 2254// Don't do this for the ":!" command, because ":!! -l" needs the space. 2255ea .arg = ea .cmdidx == CMD_bang ?p :skipwhite (p ); 2256 2257// ":file" cannot be run with an argument when "curbuf->b_ro_locked" is set 2258if (ea .cmdidx == CMD_file && * ea .arg != NUL && curbuf_locked ()) { 2259 gotodoend ; 2260 } 2261 2262// Check for "++opt=val" argument. 2263// Must be first, allow ":w ++enc=utf8 !cmd" 2264if (ea .argt & EX_ARGOPT ) { 2265while (ea .arg [0 ]== '+' && ea .arg [1 ]== '+' ) { 2266if (getargopt (& ea )== FAIL && !ni ) { 2267errormsg = _ (e_invarg ); 2268 gotodoend ; 2269 } 2270 } 2271 } 2272 2273if (ea .cmdidx == CMD_write || ea .cmdidx == CMD_update ) { 2274if (* ea .arg == '>' ) {// append 2275if (*++ ea .arg != '>' ) {// typed wrong 2276errormsg = _ ("E494: Use w or w>>" ); 2277 gotodoend ; 2278 } 2279ea .arg = skipwhite (ea .arg + 1 ); 2280ea .append = true; 2281 }else if (* ea .arg == '!' && ea .cmdidx == CMD_write ) {// :w !filter 2282ea .arg ++ ; 2283ea .usefilter = true; 2284 } 2285 }else if (ea .cmdidx == CMD_read ) { 2286if (ea .forceit ) { 2287ea .usefilter = true;// :r! filter if ea.forceit 2288ea .forceit = false; 2289 }else if (* ea .arg == '!' ) {// :r !filter 2290ea .arg ++ ; 2291ea .usefilter = true; 2292 } 2293 }else if (ea .cmdidx == CMD_lshift || ea .cmdidx == CMD_rshift ) { 2294ea .amount = 1 ; 2295while (* ea .arg == * ea .cmd ) {// count number of '>' or '<' 2296ea .arg ++ ; 2297ea .amount ++ ; 2298 } 2299ea .arg = skipwhite (ea .arg ); 2300 } 2301 2302// Check for "+command" argument, before checking for next command. 2303// Don't do this for ":read !cmd" and ":write !cmd". 2304if ((ea .argt & EX_CMDARG )&& !ea .usefilter ) { 2305ea .do_ecmd_cmd = getargcmd (& ea .arg ); 2306 } 2307 2308// Check for '|' to separate commands and '"' to start comments. 2309// Don't do this for ":read !cmd" and ":write !cmd". 2310if ((ea .argt & EX_TRLBAR )&& !ea .usefilter ) { 2311separate_nextcmd (& ea ); 2312 }else if (ea .cmdidx == CMD_bang 2313|| ea .cmdidx == CMD_terminal 2314|| ea .cmdidx == CMD_global 2315|| ea .cmdidx == CMD_vglobal 2316|| ea .usefilter ) { 2317// Check for <newline> to end a shell command. 2318// Also do this for ":read !cmd", ":write !cmd" and ":global". 2319// Any others? 2320for (char * s = ea .arg ;* s ;s ++ ) { 2321// Remove one backslash before a newline. 2322if (* s == '\\' && s [1 ]== '\n' ) { 2323STRMOVE (s ,s + 1 ); 2324 }else if (* s == '\n' ) { 2325ea .nextcmd = s + 1 ; 2326* s = NUL ; 2327break ; 2328 } 2329 } 2330 } 2331 2332if ((ea .argt & EX_DFLALL )&& ea .addr_count == 0 ) { 2333set_cmd_dflall_range (& ea ); 2334 } 2335 2336// Parse register and count 2337parse_register (& ea ); 2338if (parse_count (& ea ,& errormsg , true)== FAIL ) { 2339 gotodoend ; 2340 } 2341 2342// Check for flags: 'l', 'p' and '#'. 2343if (ea .argt & EX_FLAGS ) { 2344get_flags (& ea ); 2345 } 2346if (!ni && !(ea .argt & EX_EXTRA )&& * ea .arg != NUL 2347&& * ea .arg != '"' && (* ea .arg != '|' || (ea .argt & EX_TRLBAR )== 0 )) { 2348// no arguments allowed but there is something 2349errormsg = ex_errmsg (e_trailing_arg ,ea .arg ); 2350 gotodoend ; 2351 } 2352 2353if (!ni && (ea .argt & EX_NEEDARG )&& * ea .arg == NUL ) { 2354errormsg = _ (e_argreq ); 2355 gotodoend ; 2356 } 2357 2358if (skip_cmd (& ea )) { 2359 gotodoend ; 2360 } 2361 2362// 7. Execute the command. 2363int retv = 0 ; 2364if (execute_cmd0 (& retv ,& ea ,& errormsg , false)== FAIL ) { 2365 gotodoend ; 2366 } 2367 2368// If the command just executed called do_cmdline(), any throw or ":return" 2369// or ":finish" encountered there must also check the cstack of the still 2370// active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught 2371// exception, or reanimate a returned function or finished script file and 2372// return or finish it again. 2373if (need_rethrow ) { 2374do_throw (cstack ); 2375 }else if (check_cstack ) { 2376if (source_finished (fgetline ,cookie )) { 2377do_finish (& ea , true); 2378 }else if (getline_equal (fgetline ,cookie ,get_func_line ) 2379&& current_func_returned ()) { 2380do_return (& ea , true, false,NULL ); 2381 } 2382 } 2383need_rethrow = check_cstack = false; 2384 2385doend : 2386// can happen with zero line number 2387if (curwin -> w_cursor .lnum == 0 ) { 2388curwin -> w_cursor .lnum = 1 ; 2389curwin -> w_cursor .col = 0 ; 2390 } 2391 2392if (errormsg != NULL && * errormsg != NUL && !did_emsg ) { 2393if (flags & DOCMD_VERBOSE ) { 2394if (errormsg != IObuff ) { 2395xstrlcpy (IObuff ,errormsg ,IOSIZE ); 2396errormsg = IObuff ; 2397 } 2398append_command (* ea .cmdlinep ); 2399 } 2400emsg (errormsg ); 2401 } 2402do_errthrow (cstack , 2403 (ea .cmdidx != CMD_SIZE 2404&& !IS_USER_CMDIDX (ea .cmdidx )) ?cmdnames [(int )ea .cmdidx ].cmd_name :NULL ); 2405 2406undo_cmdmod (& cmdmod ); 2407cmdmod = save_cmdmod ; 2408reg_executing = save_reg_executing ; 2409pending_end_reg_executing = save_pending_end_reg_executing ; 2410 2411if (ea .nextcmd && * ea .nextcmd == NUL ) {// not really a next command 2412ea .nextcmd = NULL ; 2413 } 2414 2415ex_nesting_level -- ; 2416xfree (ea .cmdline_tofree ); 2417 2418return ea .nextcmd ; 2419} 2420 2421static char ex_error_buf [MSG_BUF_LEN ]; 2422 2423/// @return an error message with argument included. 2424/// Uses a static buffer, only the last error will be kept. 2425/// "msg" will be translated, caller should use N_(). 2426char * ex_errmsg (const char * const msg , ...) 2427FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PRINTF (1 ,2 ) 2428{ 2429va_list ap ; 2430va_start (ap ,msg ); 2431vim_vsnprintf (ex_error_buf ,MSG_BUF_LEN ,_ (msg ),ap ); 2432va_end (ap ); 2433return ex_error_buf ; 2434} 2435 2436/// Handle a range without a command. 2437/// Returns an error message on failure. 2438static char * ex_range_without_command (exarg_T * eap ) 2439{ 2440char * errormsg = NULL ; 2441 2442if (* eap -> cmd == '|' ) { 2443eap -> cmdidx = CMD_print ; 2444eap -> argt = EX_RANGE |EX_COUNT |EX_TRLBAR ; 2445if ((errormsg = invalid_range (eap ))== NULL ) { 2446correct_range (eap ); 2447ex_print (eap ); 2448 } 2449 }else if (eap -> addr_count != 0 ) { 2450eap -> line2 = MIN (eap -> line2 ,curbuf -> b_ml .ml_line_count ); 2451 2452if (eap -> line2 < 0 ) { 2453errormsg = _ (e_invrange ); 2454 }else { 2455if (eap -> line2 == 0 ) { 2456curwin -> w_cursor .lnum = 1 ; 2457 }else { 2458curwin -> w_cursor .lnum = eap -> line2 ; 2459 } 2460beginline (BL_SOL |BL_FIX ); 2461 } 2462 } 2463return errormsg ; 2464} 2465 2466/// Parse and skip over command modifiers: 2467/// - update eap->cmd 2468/// - store flags in "cmod". 2469/// 2470/// @param skip_only if false, undo_cmdmod() must be called later to free 2471/// any cmod_filter_pat and cmod_filter_regmatch.regprog. 2472/// @param[out] errormsg potential error message. 2473/// 2474/// Call apply_cmdmod() to get the side effects of the modifiers: 2475/// - Increment "sandbox" for ":sandbox" 2476/// - set p_verbose for ":verbose" 2477/// - set msg_silent for ":silent" 2478/// - set 'eventignore' to "all" for ":noautocmd" 2479/// 2480/// @return FAIL when the command is not to be executed. 2481int parse_command_modifiers (exarg_T * eap ,const char ** errormsg ,cmdmod_T * cmod ,bool skip_only ) 2482{ 2483char * orig_cmd = eap -> cmd ; 2484char * cmd_start = NULL ; 2485bool has_visual_range = false; 2486CLEAR_POINTER (cmod ); 2487 2488if (strncmp (eap -> cmd ,"'<,'>" ,5 )== 0 ) { 2489// The automatically inserted Visual area range is skipped, so that 2490// typing ":cmdmod cmd" in Visual mode works without having to move the 2491// range to after the modifiers. The command will be "'<,'>cmdmod cmd", 2492// parse "cmdmod cmd" and then put back "'<,'>" before "cmd" below. 2493// Only skip '<,'>' if there's a command after it 2494const char * p = skipwhite (eap -> cmd + 5 ); 2495if (* p != NUL && * p != '|' ) { 2496eap -> cmd += 5 ; 2497cmd_start = eap -> cmd ; 2498has_visual_range = true; 2499 } 2500 } 2501 2502// Repeat until no more command modifiers are found. 2503while (true) { 2504while (* eap -> cmd == ' ' 2505|| * eap -> cmd == '\t' 2506|| * eap -> cmd == ':' ) { 2507eap -> cmd ++ ; 2508 } 2509 2510// ignore comment and empty lines 2511if (* eap -> cmd == '"' ) { 2512// a comment ends at a NL 2513eap -> nextcmd = vim_strchr (eap -> cmd ,'\n' ); 2514if (eap -> nextcmd != NULL ) { 2515eap -> nextcmd ++ ; 2516 } 2517return FAIL ; 2518 } 2519if (* eap -> cmd == '\n' ) { 2520eap -> nextcmd = eap -> cmd + 1 ; 2521return FAIL ; 2522 } 2523if (* eap -> cmd == NUL ) { 2524return FAIL ; 2525 } 2526 2527char * p = skip_range (eap -> cmd ,NULL ); 2528switch (* p ) { 2529// When adding an entry, also modify cmdmods[] 2530case 'a' : 2531if (!checkforcmd (& eap -> cmd ,"aboveleft" ,3 )) { 2532break ; 2533 } 2534cmod -> cmod_split |=WSP_ABOVE ; 2535continue ; 2536 2537case 'b' : 2538if (checkforcmd (& eap -> cmd ,"belowright" ,3 )) { 2539cmod -> cmod_split |=WSP_BELOW ; 2540continue ; 2541 } 2542if (checkforcmd (& eap -> cmd ,"browse" ,3 )) { 2543cmod -> cmod_flags |=CMOD_BROWSE ; 2544continue ; 2545 } 2546if (!checkforcmd (& eap -> cmd ,"botright" ,2 )) { 2547break ; 2548 } 2549cmod -> cmod_split |=WSP_BOT ; 2550continue ; 2551 2552case 'c' : 2553if (!checkforcmd (& eap -> cmd ,"confirm" ,4 )) { 2554break ; 2555 } 2556cmod -> cmod_flags |=CMOD_CONFIRM ; 2557continue ; 2558 2559case 'k' : 2560if (checkforcmd (& eap -> cmd ,"keepmarks" ,3 )) { 2561cmod -> cmod_flags |=CMOD_KEEPMARKS ; 2562continue ; 2563 } 2564if (checkforcmd (& eap -> cmd ,"keepalt" ,5 )) { 2565cmod -> cmod_flags |=CMOD_KEEPALT ; 2566continue ; 2567 } 2568if (checkforcmd (& eap -> cmd ,"keeppatterns" ,5 )) { 2569cmod -> cmod_flags |=CMOD_KEEPPATTERNS ; 2570continue ; 2571 } 2572if (!checkforcmd (& eap -> cmd ,"keepjumps" ,5 )) { 2573break ; 2574 } 2575cmod -> cmod_flags |=CMOD_KEEPJUMPS ; 2576continue ; 2577 2578case 'f' : {// only accept ":filter {pat} cmd" 2579char * reg_pat ; 2580 2581if (!checkforcmd (& p ,"filter" ,4 )|| * p == NUL || ends_excmd (* p )) { 2582break ; 2583 } 2584if (* p == '!' ) { 2585cmod -> cmod_filter_force = true; 2586p = skipwhite (p + 1 ); 2587if (* p == NUL || ends_excmd (* p )) { 2588break ; 2589 } 2590 } 2591if (skip_only ) { 2592p = skip_vimgrep_pat (p ,NULL ,NULL ); 2593 }else { 2594// NOTE: This puts a NUL after the pattern. 2595p = skip_vimgrep_pat (p ,& reg_pat ,NULL ); 2596 } 2597if (p == NULL || * p == NUL ) { 2598break ; 2599 } 2600if (!skip_only ) { 2601cmod -> cmod_filter_pat = xstrdup (reg_pat ); 2602cmod -> cmod_filter_regmatch .regprog = vim_regcomp (reg_pat ,RE_MAGIC ); 2603if (cmod -> cmod_filter_regmatch .regprog == NULL ) { 2604break ; 2605 } 2606 } 2607eap -> cmd = p ; 2608continue ; 2609 } 2610 2611case 'h' : 2612if (checkforcmd (& eap -> cmd ,"horizontal" ,3 )) { 2613cmod -> cmod_split |=WSP_HOR ; 2614continue ; 2615 } 2616// ":hide" and ":hide | cmd" are not modifiers 2617if (p != eap -> cmd || !checkforcmd (& p ,"hide" ,3 ) 2618|| * p == NUL || ends_excmd (* p )) { 2619break ; 2620 } 2621eap -> cmd = p ; 2622cmod -> cmod_flags |=CMOD_HIDE ; 2623continue ; 2624 2625case 'l' : 2626if (checkforcmd (& eap -> cmd ,"lockmarks" ,3 )) { 2627cmod -> cmod_flags |=CMOD_LOCKMARKS ; 2628continue ; 2629 } 2630 2631if (!checkforcmd (& eap -> cmd ,"leftabove" ,5 )) { 2632break ; 2633 } 2634cmod -> cmod_split |=WSP_ABOVE ; 2635continue ; 2636 2637case 'n' : 2638if (checkforcmd (& eap -> cmd ,"noautocmd" ,3 )) { 2639cmod -> cmod_flags |=CMOD_NOAUTOCMD ; 2640continue ; 2641 } 2642if (!checkforcmd (& eap -> cmd ,"noswapfile" ,3 )) { 2643break ; 2644 } 2645cmod -> cmod_flags |=CMOD_NOSWAPFILE ; 2646continue ; 2647 2648case 'r' : 2649if (!checkforcmd (& eap -> cmd ,"rightbelow" ,6 )) { 2650break ; 2651 } 2652cmod -> cmod_split |=WSP_BELOW ; 2653continue ; 2654 2655case 's' : 2656if (checkforcmd (& eap -> cmd ,"sandbox" ,3 )) { 2657cmod -> cmod_flags |=CMOD_SANDBOX ; 2658continue ; 2659 } 2660if (!checkforcmd (& eap -> cmd ,"silent" ,3 )) { 2661break ; 2662 } 2663cmod -> cmod_flags |=CMOD_SILENT ; 2664if (* eap -> cmd == '!' && !ascii_iswhite (eap -> cmd [-1 ])) { 2665// ":silent!", but not "silent !cmd" 2666eap -> cmd = skipwhite (eap -> cmd + 1 ); 2667cmod -> cmod_flags |=CMOD_ERRSILENT ; 2668 } 2669continue ; 2670 2671case 't' : 2672if (checkforcmd (& p ,"tab" ,3 )) { 2673if (!skip_only ) { 2674int tabnr = (int )get_address (eap ,& eap -> cmd ,ADDR_TABS ,eap -> skip ,skip_only , 2675 false,1 ,errormsg ); 2676if (eap -> cmd == NULL ) { 2677return false; 2678 } 2679 2680if (tabnr == MAXLNUM ) { 2681cmod -> cmod_tab = tabpage_index (curtab )+ 1 ; 2682 }else { 2683if (tabnr < 0 || tabnr > LAST_TAB_NR ) { 2684* errormsg = _ (e_invrange ); 2685return false; 2686 } 2687cmod -> cmod_tab = tabnr + 1 ; 2688 } 2689 } 2690eap -> cmd = p ; 2691continue ; 2692 } 2693if (!checkforcmd (& eap -> cmd ,"topleft" ,2 )) { 2694break ; 2695 } 2696cmod -> cmod_split |=WSP_TOP ; 2697continue ; 2698 2699case 'u' : 2700if (!checkforcmd (& eap -> cmd ,"unsilent" ,3 )) { 2701break ; 2702 } 2703cmod -> cmod_flags |=CMOD_UNSILENT ; 2704continue ; 2705 2706case 'v' : 2707if (checkforcmd (& eap -> cmd ,"vertical" ,4 )) { 2708cmod -> cmod_split |=WSP_VERT ; 2709continue ; 2710 } 2711if (!checkforcmd (& p ,"verbose" ,4 )) { 2712break ; 2713 } 2714if (ascii_isdigit (* eap -> cmd )) { 2715// zero means not set, one is verbose == 0, etc. 2716cmod -> cmod_verbose = atoi (eap -> cmd )+ 1 ; 2717 }else { 2718cmod -> cmod_verbose = 2 ;// default: verbose == 1 2719 } 2720eap -> cmd = p ; 2721continue ; 2722 } 2723break ; 2724 } 2725 2726if (has_visual_range ) { 2727if (eap -> cmd > cmd_start ) { 2728// Move the '<,'> range to after the modifiers and insert a colon. 2729// Since the modifiers have been parsed put the colon on top of the 2730// space: "'<,'>mod cmd" -> "mod:'<,'>cmd 2731// Put eap->cmd after the colon. 2732memmove (cmd_start - 5 ,cmd_start , (size_t )(eap -> cmd - cmd_start )); 2733eap -> cmd -= 5 ; 2734memmove (eap -> cmd - 1 ,":'<,'>" ,6 ); 2735 }else { 2736// No modifiers, move the pointer back. 2737eap -> cmd = orig_cmd ; 2738 } 2739 } 2740 2741return OK ; 2742} 2743 2744/// Apply the command modifiers. Saves current state in "cmdmod", call 2745/// undo_cmdmod() later. 2746void apply_cmdmod (cmdmod_T * cmod ) 2747{ 2748if ((cmod -> cmod_flags & CMOD_SANDBOX )&& !cmod -> cmod_did_sandbox ) { 2749sandbox ++ ; 2750cmod -> cmod_did_sandbox = true; 2751 } 2752if (cmod -> cmod_verbose > 0 ) { 2753if (cmod -> cmod_verbose_save == 0 ) { 2754cmod -> cmod_verbose_save = p_verbose + 1 ; 2755 } 2756p_verbose = cmod -> cmod_verbose - 1 ; 2757 } 2758 2759if ((cmod -> cmod_flags & (CMOD_SILENT |CMOD_UNSILENT )) 2760&& cmod -> cmod_save_msg_silent == 0 ) { 2761cmod -> cmod_save_msg_silent = msg_silent + 1 ; 2762cmod -> cmod_save_msg_scroll = msg_scroll ; 2763 } 2764if (cmod -> cmod_flags & CMOD_SILENT ) { 2765msg_silent ++ ; 2766 } 2767if (cmod -> cmod_flags & CMOD_UNSILENT ) { 2768msg_silent = 0 ; 2769 } 2770 2771if (cmod -> cmod_flags & CMOD_ERRSILENT ) { 2772emsg_silent ++ ; 2773cmod -> cmod_did_esilent ++ ; 2774 } 2775 2776if ((cmod -> cmod_flags & CMOD_NOAUTOCMD )&& cmod -> cmod_save_ei == NULL ) { 2777// Set 'eventignore' to "all". 2778// First save the existing option value for restoring it later. 2779cmod -> cmod_save_ei = xstrdup (p_ei ); 2780set_option_direct (kOptEventignore ,STATIC_CSTR_AS_OBJ ("all" ),0 ,SID_NONE ); 2781 } 2782} 2783 2784/// Undo and free contents of "cmod". 2785void undo_cmdmod (cmdmod_T * cmod ) 2786FUNC_ATTR_NONNULL_ALL 2787{ 2788if (cmod -> cmod_verbose_save > 0 ) { 2789p_verbose = cmod -> cmod_verbose_save - 1 ; 2790cmod -> cmod_verbose_save = 0 ; 2791 } 2792 2793if (cmod -> cmod_did_sandbox ) { 2794sandbox -- ; 2795cmod -> cmod_did_sandbox = false; 2796 } 2797 2798if (cmod -> cmod_save_ei != NULL ) { 2799// Restore 'eventignore' to the value before ":noautocmd". 2800set_option_direct (kOptEventignore ,CSTR_AS_OBJ (cmod -> cmod_save_ei ),0 ,SID_NONE ); 2801free_string_option (cmod -> cmod_save_ei ); 2802cmod -> cmod_save_ei = NULL ; 2803 } 2804 2805xfree (cmod -> cmod_filter_pat ); 2806vim_regfree (cmod -> cmod_filter_regmatch .regprog ); 2807 2808if (cmod -> cmod_save_msg_silent > 0 ) { 2809// messages could be enabled for a serious error, need to check if the 2810// counters don't become negative 2811if (!did_emsg || msg_silent > cmod -> cmod_save_msg_silent - 1 ) { 2812msg_silent = cmod -> cmod_save_msg_silent - 1 ; 2813 } 2814emsg_silent -= cmod -> cmod_did_esilent ; 2815emsg_silent = MAX (emsg_silent ,0 ); 2816// Restore msg_scroll, it's set by file I/O commands, even when no 2817// message is actually displayed. 2818msg_scroll = cmod -> cmod_save_msg_scroll ; 2819 2820// "silent reg" or "silent echo x" inside "redir" leaves msg_col 2821// somewhere in the line. Put it back in the first column. 2822if (redirecting ()) { 2823msg_col = 0 ; 2824 } 2825 2826cmod -> cmod_save_msg_silent = 0 ; 2827cmod -> cmod_did_esilent = 0 ; 2828 } 2829} 2830 2831/// Parse the address range, if any, in "eap". 2832/// May set the last search pattern, unless "silent" is true. 2833/// 2834/// @return FAIL and set "errormsg" or return OK. 2835int parse_cmd_address (exarg_T * eap ,const char ** errormsg ,bool silent ) 2836FUNC_ATTR_NONNULL_ALL 2837{ 2838int address_count = 1 ; 2839linenr_T lnum ; 2840bool need_check_cursor = false; 2841int ret = FAIL ; 2842 2843// Repeat for all ',' or ';' separated addresses. 2844while (true) { 2845eap -> line1 = eap -> line2 ; 2846eap -> line2 = get_cmd_default_range (eap ); 2847eap -> cmd = skipwhite (eap -> cmd ); 2848lnum = get_address (eap ,& eap -> cmd ,eap -> addr_type ,eap -> skip ,silent , 2849eap -> addr_count == 0 ,address_count ++ ,errormsg ); 2850if (eap -> cmd == NULL ) {// error detected 2851 gototheend ; 2852 } 2853if (lnum == MAXLNUM ) { 2854if (* eap -> cmd == '%' ) {// '%' - all lines 2855eap -> cmd ++ ; 2856switch (eap -> addr_type ) { 2857case ADDR_LINES : 2858case ADDR_OTHER : 2859eap -> line1 = 1 ; 2860eap -> line2 = curbuf -> b_ml .ml_line_count ; 2861break ; 2862case ADDR_LOADED_BUFFERS : { 2863buf_T * buf = firstbuf ; 2864 2865while (buf -> b_next != NULL && buf -> b_ml .ml_mfp == NULL ) { 2866buf = buf -> b_next ; 2867 } 2868eap -> line1 = buf -> b_fnum ; 2869buf = lastbuf ; 2870while (buf -> b_prev != NULL && buf -> b_ml .ml_mfp == NULL ) { 2871buf = buf -> b_prev ; 2872 } 2873eap -> line2 = buf -> b_fnum ; 2874break ; 2875 } 2876case ADDR_BUFFERS : 2877eap -> line1 = firstbuf -> b_fnum ; 2878eap -> line2 = lastbuf -> b_fnum ; 2879break ; 2880case ADDR_WINDOWS : 2881case ADDR_TABS : 2882if (IS_USER_CMDIDX (eap -> cmdidx )) { 2883eap -> line1 = 1 ; 2884eap -> line2 = eap -> addr_type == ADDR_WINDOWS 2885 ?LAST_WIN_NR (false) :LAST_TAB_NR ; 2886 }else { 2887// there is no Vim command which uses '%' and 2888// ADDR_WINDOWS or ADDR_TABS 2889* errormsg = _ (e_invrange ); 2890 gototheend ; 2891 } 2892break ; 2893case ADDR_TABS_RELATIVE : 2894case ADDR_UNSIGNED : 2895case ADDR_QUICKFIX : 2896* errormsg = _ (e_invrange ); 2897 gototheend ; 2898case ADDR_ARGUMENTS : 2899if (ARGCOUNT == 0 ) { 2900eap -> line1 = eap -> line2 = 0 ; 2901 }else { 2902eap -> line1 = 1 ; 2903eap -> line2 = ARGCOUNT ; 2904 } 2905break ; 2906case ADDR_QUICKFIX_VALID : 2907eap -> line1 = 1 ; 2908eap -> line2 = (linenr_T )qf_get_valid_size (eap ); 2909if (eap -> line2 == 0 ) { 2910eap -> line2 = 1 ; 2911 } 2912break ; 2913case ADDR_NONE : 2914// Will give an error later if a range is found. 2915break ; 2916 } 2917eap -> addr_count ++ ; 2918 }else if (* eap -> cmd == '*' ) { 2919// '*' - visual area 2920if (eap -> addr_type != ADDR_LINES ) { 2921* errormsg = _ (e_invrange ); 2922 gototheend ; 2923 } 2924 2925eap -> cmd ++ ; 2926if (!eap -> skip ) { 2927fmark_T * fm = mark_get_visual (curbuf ,'<' ); 2928if (!mark_check (fm ,errormsg )) { 2929 gototheend ; 2930 } 2931assert (fm != NULL ); 2932eap -> line1 = fm -> mark .lnum ; 2933fm = mark_get_visual (curbuf ,'>' ); 2934if (!mark_check (fm ,errormsg )) { 2935 gototheend ; 2936 } 2937assert (fm != NULL ); 2938eap -> line2 = fm -> mark .lnum ; 2939eap -> addr_count ++ ; 2940 } 2941 } 2942 }else { 2943eap -> line2 = lnum ; 2944 } 2945eap -> addr_count ++ ; 2946 2947if (* eap -> cmd == ';' ) { 2948if (!eap -> skip ) { 2949curwin -> w_cursor .lnum = eap -> line2 ; 2950 2951// Don't leave the cursor on an illegal line or column, but do 2952// accept zero as address, so 0;/PATTERN/ works correctly 2953// (where zero usually means to use the first line). 2954// Check the cursor position before returning. 2955if (eap -> line2 > 0 ) { 2956check_cursor (curwin ); 2957 }else { 2958check_cursor_col (curwin ); 2959 } 2960need_check_cursor = true; 2961 } 2962 }else if (* eap -> cmd != ',' ) { 2963break ; 2964 } 2965eap -> cmd ++ ; 2966 } 2967 2968// One address given: set start and end lines. 2969if (eap -> addr_count == 1 ) { 2970eap -> line1 = eap -> line2 ; 2971// ... but only implicit: really no address given 2972if (lnum == MAXLNUM ) { 2973eap -> addr_count = 0 ; 2974 } 2975 } 2976ret = OK ; 2977 2978theend : 2979if (need_check_cursor ) { 2980check_cursor (curwin ); 2981 } 2982return ret ; 2983} 2984 2985/// Check for an Ex command with optional tail. 2986/// If there is a match advance "pp" to the argument and return true. 2987/// 2988/// @param pp start of command 2989/// @param cmd name of command 2990/// @param len required length 2991bool checkforcmd (char ** pp ,const char * cmd ,int len ) 2992{ 2993int i ; 2994 2995for (i = 0 ;cmd [i ]!= NUL ;i ++ ) { 2996if (cmd [i ]!= (* pp )[i ]) { 2997break ; 2998 } 2999 } 3000if (i >=len && !ASCII_ISALPHA ((* pp )[i ])) { 3001* pp = skipwhite (* pp + i ); 3002return true; 3003 } 3004return false; 3005} 3006 3007/// Append "cmd" to the error message in IObuff. 3008/// Takes care of limiting the length and handling 0xa0, which would be 3009/// invisible otherwise. 3010static void append_command (const char * cmd ) 3011{ 3012size_t len = strlen (IObuff ); 3013const char * s = cmd ; 3014char * d ; 3015 3016if (len > IOSIZE - 100 ) { 3017// Not enough space, truncate and put in "...". 3018d = IObuff + IOSIZE - 100 ; 3019d -= utf_head_off (IObuff ,d ); 3020STRCPY (d ,"..." ); 3021 } 3022xstrlcat (IObuff ,": " ,IOSIZE ); 3023d = IObuff + strlen (IObuff ); 3024while (* s != NUL && d - IObuff + 5 < IOSIZE ) { 3025if ((uint8_t )s [0 ]== 0xc2 && (uint8_t )s [1 ]== 0xa0 ) { 3026s += 2 ; 3027STRCPY (d ,"<a0>" ); 3028d += 4 ; 3029 }else if (d - IObuff + utfc_ptr2len (s )+ 1 >=IOSIZE ) { 3030break ; 3031 }else { 3032mb_copy_char (& s ,& d ); 3033 } 3034 } 3035* d = NUL ; 3036} 3037 3038/// Return true and set "*idx" if "p" points to a one letter command. 3039/// - The 'k' command can directly be followed by any character 3040/// but :keepa[lt] is another command, as are :keepj[umps], 3041/// :kee[pmarks] and :keepp[atterns]. 3042/// - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' 3043/// but :sre[wind] is another command, as are :scr[iptnames], 3044/// :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. 3045static int one_letter_cmd (const char * p ,cmdidx_T * idx ) 3046{ 3047if (p [0 ]== 'k' 3048&& (p [1 ]!= 'e' || (p [1 ]== 'e' && p [2 ]!= 'e' ))) { 3049* idx = CMD_k ; 3050return true; 3051 } 3052if (p [0 ]== 's' 3053&& ((p [1 ]== 'c' 3054&& (p [2 ]== NUL 3055|| (p [2 ]!= 's' && p [2 ]!= 'r' 3056&& (p [3 ]== NUL 3057|| (p [3 ]!= 'i' && p [4 ]!= 'p' ))))) 3058|| p [1 ]== 'g' 3059|| (p [1 ]== 'i' && p [2 ]!= 'm' && p [2 ]!= 'l' && p [2 ]!= 'g' ) 3060|| p [1 ]== 'I' 3061|| (p [1 ]== 'r' && p [2 ]!= 'e' ))) { 3062* idx = CMD_substitute ; 3063return true; 3064 } 3065return false; 3066} 3067 3068/// Find an Ex command by its name, either built-in or user. 3069/// Start of the name can be found at eap->cmd. 3070/// Sets eap->cmdidx and returns a pointer to char after the command name. 3071/// "full" is set to true if the whole command name matched. 3072/// 3073/// @return NULL for an ambiguous user command. 3074char * find_ex_command (exarg_T * eap ,int * full ) 3075FUNC_ATTR_NONNULL_ARG (1 ) 3076{ 3077// Isolate the command and search for it in the command table. 3078char * p = eap -> cmd ; 3079if (one_letter_cmd (p ,& eap -> cmdidx )) { 3080p ++ ; 3081if (full != NULL ) { 3082* full = true; 3083 } 3084 }else { 3085while (ASCII_ISALPHA (* p )) { 3086p ++ ; 3087 } 3088// for python 3.x support ":py3", ":python3", ":py3file", etc. 3089if (eap -> cmd [0 ]== 'p' && eap -> cmd [1 ]== 'y' ) { 3090while (ASCII_ISALNUM (* p )) { 3091p ++ ; 3092 } 3093 } 3094 3095// check for non-alpha command 3096if (p == eap -> cmd && vim_strchr ("@!=><&~#" , (uint8_t )(* p ))!= NULL ) { 3097p ++ ; 3098 } 3099int len = (int )(p - eap -> cmd ); 3100// The "d" command can directly be followed by 'l' or 'p' flag. 3101if (* eap -> cmd == 'd' && (p [-1 ]== 'l' || p [-1 ]== 'p' )) { 3102// Check for ":dl", ":dell", etc. to ":deletel": that's 3103// :delete with the 'l' flag. Same for 'p'. 3104int i ; 3105for (i = 0 ;i < len ;i ++ ) { 3106if (eap -> cmd [i ]!= "delete" [i ]) { 3107break ; 3108 } 3109 } 3110if (i == len - 1 ) { 3111len -- ; 3112if (p [-1 ]== 'l' ) { 3113eap -> flags |=EXFLAG_LIST ; 3114 }else { 3115eap -> flags |=EXFLAG_PRINT ; 3116 } 3117 } 3118 } 3119 3120if (ASCII_ISLOWER (eap -> cmd [0 ])) { 3121const int c1 = (uint8_t )eap -> cmd [0 ]; 3122const int c2 = len == 1 ?NUL :eap -> cmd [1 ]; 3123 3124if (command_count != CMD_SIZE ) { 3125iemsg (_ ("E943: Command table needs to be updated, run 'make'" )); 3126getout (1 ); 3127 } 3128 3129// Use a precomputed index for fast look-up in cmdnames[] 3130// taking into account the first 2 letters of eap->cmd. 3131eap -> cmdidx = cmdidxs1 [CHAR_ORD_LOW (c1 )]; 3132if (ASCII_ISLOWER (c2 )) { 3133eap -> cmdidx += cmdidxs2 [CHAR_ORD_LOW (c1 )][CHAR_ORD_LOW (c2 )]; 3134 } 3135 }else if (ASCII_ISUPPER (eap -> cmd [0 ])) { 3136eap -> cmdidx = CMD_Next ; 3137 }else { 3138eap -> cmdidx = CMD_bang ; 3139 } 3140assert (eap -> cmdidx >=0 ); 3141 3142if (len == 3 && strncmp ("def" ,eap -> cmd ,3 )== 0 ) { 3143// Make :def an unknown command to avoid confusing behavior. #23149 3144eap -> cmdidx = CMD_SIZE ; 3145 } 3146 3147for (; (int )eap -> cmdidx < CMD_SIZE ; 3148eap -> cmdidx = (cmdidx_T )((int )eap -> cmdidx + 1 )) { 3149if (strncmp (cmdnames [(int )eap -> cmdidx ].cmd_name ,eap -> cmd , 3150 (size_t )len )== 0 ) { 3151if (full != NULL 3152&& cmdnames [(int )eap -> cmdidx ].cmd_name [len ]== NUL ) { 3153* full = true; 3154 } 3155break ; 3156 } 3157 } 3158 3159// Look for a user defined command as a last resort. 3160if ((eap -> cmdidx == CMD_SIZE ) 3161&& * eap -> cmd >='A' && * eap -> cmd <='Z' ) { 3162// User defined commands may contain digits. 3163while (ASCII_ISALNUM (* p )) { 3164p ++ ; 3165 } 3166p = find_ucmd (eap ,p ,full ,NULL ,NULL ); 3167 } 3168if (p == eap -> cmd ) { 3169eap -> cmdidx = CMD_SIZE ; 3170 } 3171 } 3172 3173// Force ":ho" to be unresolved. Without this, find_ex_command() 3174// matches it to CMD_horizontal (the only "ho*" entry), which makes 3175// fullcommand("ho") return "horizontal" even though ":ho" cannot be 3176// used as the modifier (cmdmods[] requires 3 chars, "hor"). 3177if (eap -> cmdidx == CMD_horizontal && p - eap -> cmd == 2 ) { 3178eap -> cmdidx = CMD_SIZE ; 3179 } 3180 3181return p ; 3182} 3183 3184static struct cmdmod { 3185char * name ; 3186int minlen ; 3187int has_count ;// :123verbose :3tab 3188}cmdmods []= { 3189 {"aboveleft" ,3 , false }, 3190 {"belowright" ,3 , false }, 3191 {"botright" ,2 , false }, 3192 {"browse" ,3 , false }, 3193 {"confirm" ,4 , false }, 3194 {"filter" ,4 , false }, 3195 {"hide" ,3 , false }, 3196 {"horizontal" ,3 , false }, 3197 {"keepalt" ,5 , false }, 3198 {"keepjumps" ,5 , false }, 3199 {"keepmarks" ,3 , false }, 3200 {"keeppatterns" ,5 , false }, 3201 {"leftabove" ,5 , false }, 3202 {"lockmarks" ,3 , false }, 3203 {"noautocmd" ,3 , false }, 3204 {"noswapfile" ,3 , false }, 3205 {"rightbelow" ,6 , false }, 3206 {"sandbox" ,3 , false }, 3207 {"silent" ,3 , false }, 3208 {"tab" ,3 , true }, 3209 {"topleft" ,2 , false }, 3210 {"unsilent" ,3 , false }, 3211 {"verbose" ,4 , true }, 3212 {"vertical" ,4 , false }, 3213}; 3214 3215/// @return length of a command modifier (including optional count) or, 3216/// zero when it's not a modifier. 3217int modifier_len (char * cmd ) 3218{ 3219char * p = cmd ; 3220 3221if (ascii_isdigit (* cmd )) { 3222p = skipwhite (skipdigits (cmd + 1 )); 3223 } 3224for (int i = 0 ;i < (int )ARRAY_SIZE (cmdmods );i ++ ) { 3225// cmdmod_info_tab[] is sorted by name: once the first letter is past 3226// the command's first letter no later entry can match. 3227if (cmdmods [i ].name [0 ]> * p ) { 3228break ; 3229 } 3230int j ; 3231for (j = 0 ;p [j ]!= NUL ;j ++ ) { 3232if (p [j ]!= cmdmods [i ].name [j ]) { 3233break ; 3234 } 3235 } 3236if (j >=cmdmods [i ].minlen 3237&& !ASCII_ISALPHA (p [j ]) 3238&& (p == cmd || cmdmods [i ].has_count )) { 3239return j + (int )(p - cmd ); 3240 } 3241 } 3242return 0 ; 3243} 3244 3245/// @return > 0 if an Ex command "name" exists or, 3246/// 2 if there is an exact match or, 3247/// 3 if there is an ambiguous match. 3248int cmd_exists (const char * const name ) 3249{ 3250// Check command modifiers. 3251for (int i = 0 ;i < (int )ARRAY_SIZE (cmdmods );i ++ ) { 3252int j ; 3253for (j = 0 ;name [j ]!= NUL ;j ++ ) { 3254if (name [j ]!= cmdmods [i ].name [j ]) { 3255break ; 3256 } 3257 } 3258if (name [j ]== NUL && j >=cmdmods [i ].minlen ) { 3259return cmdmods [i ].name [j ]== NUL ?2 :1 ; 3260 } 3261 } 3262 3263// Check built-in commands and user defined commands. 3264// For ":2match" and ":3match" we need to skip the number. 3265exarg_T ea ; 3266ea .cmd = (char * )((* name == '2' || * name == '3' ) ?name + 1 :name ); 3267ea .cmdidx = 0 ; 3268ea .flags = 0 ; 3269int full = false; 3270char * p = find_ex_command (& ea ,& full ); 3271if (p == NULL ) { 3272return 3 ; 3273 } 3274if (ascii_isdigit (* name )&& ea .cmdidx != CMD_match ) { 3275return 0 ; 3276 } 3277if (* skipwhite (p )!= NUL ) { 3278return 0 ;// trailing garbage 3279 } 3280return ea .cmdidx == CMD_SIZE ?0 : (full ?2 :1 ); 3281} 3282 3283/// "fullcommand" function 3284void f_fullcommand (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 3285{ 3286char * name = (char * )tv_get_string (& argvars [0 ]); 3287 3288rettv -> v_type = VAR_STRING ; 3289rettv -> vval .v_string = NULL ; 3290 3291while (* name == ':' ) { 3292name ++ ; 3293 } 3294name = skip_range (name ,NULL ); 3295 3296exarg_T ea ; 3297ea .cmd = (* name == '2' || * name == '3' ) ?name + 1 :name ; 3298ea .cmdidx = 0 ; 3299ea .flags = 0 ; 3300char * p = find_ex_command (& ea ,NULL ); 3301if (p == NULL || ea .cmdidx == CMD_SIZE ) { 3302return ; 3303 } 3304 3305rettv -> vval .v_string = xstrdup (IS_USER_CMDIDX (ea .cmdidx ) 3306 ?get_user_command_name (ea .useridx ,ea .cmdidx ) 3307 :cmdnames [ea .cmdidx ].cmd_name ); 3308} 3309 3310cmdidx_T excmd_get_cmdidx (const char * cmd ,size_t len ) 3311{ 3312if (len == 3 && strncmp ("def" ,cmd ,3 )== 0 ) { 3313// Make :def an unknown command to avoid confusing behavior. #23149 3314return CMD_SIZE ; 3315 } 3316 3317cmdidx_T idx ; 3318 3319if (!one_letter_cmd (cmd ,& idx )) { 3320for (idx = 0 ; (int )idx < CMD_SIZE ;idx = (cmdidx_T )((int )idx + 1 )) { 3321if (strncmp (cmdnames [(int )idx ].cmd_name ,cmd ,len )== 0 ) { 3322break ; 3323 } 3324 } 3325 } 3326 3327return idx ; 3328} 3329 3330uint32_t excmd_get_argt (cmdidx_T idx ) 3331{ 3332return cmdnames [(int )idx ].cmd_argt ; 3333} 3334 3335/// Skip a range specifier of the form: addr [,addr] [;addr] .. 3336/// 3337/// Backslashed delimiters after / or ? will be skipped, and commands will 3338/// not be expanded between /'s and ?'s or after "'". 3339/// 3340/// Also skip white space and ":" characters. 3341/// 3342/// @param ctx pointer to xp_context or NULL 3343/// 3344/// @return the "cmd" pointer advanced to beyond the range. 3345char * skip_range (const char * cmd ,int * ctx ) 3346{ 3347while (vim_strchr (" \t0123456789.$%'/?-+,;\\" , (uint8_t )(* cmd ))!= NULL ) { 3348if (* cmd == '\\' ) { 3349if (cmd [1 ]== '?' || cmd [1 ]== '/' || cmd [1 ]== '&' ) { 3350cmd ++ ; 3351 }else { 3352break ; 3353 } 3354 }else if (* cmd == '\'' ) { 3355if (*++ cmd == NUL && ctx != NULL ) { 3356* ctx = EXPAND_NOTHING ; 3357 } 3358 }else if (* cmd == '/' || * cmd == '?' ) { 3359unsigned delim = (unsigned )(* cmd ++ ); 3360while (* cmd != NUL && * cmd != (char )delim ) { 3361if (* cmd ++ == '\\' && * cmd != NUL ) { 3362cmd ++ ; 3363 } 3364 } 3365if (* cmd == NUL && ctx != NULL ) { 3366* ctx = EXPAND_NOTHING ; 3367 } 3368 } 3369if (* cmd != NUL ) { 3370cmd ++ ; 3371 } 3372 } 3373 3374// Skip ":" and white space. 3375cmd = skip_colon_white (cmd , false); 3376 3377// Skip "*" used for Visual range. 3378if (* cmd == '*' ) { 3379cmd = skipwhite (cmd + 1 ); 3380 } 3381 3382return (char * )cmd ; 3383} 3384 3385static const char * addr_error (cmd_addr_T addr_type ) 3386{ 3387if (addr_type == ADDR_NONE ) { 3388return _ (e_norange ); 3389 }else { 3390return _ (e_invrange ); 3391 } 3392} 3393 3394/// Gets a single EX address. 3395/// 3396/// Sets ptr to the next character after the part that was interpreted. 3397/// Sets ptr to NULL when an error is encountered (stored in `errormsg`). 3398/// May set the last used search pattern. 3399/// 3400/// @param skip only skip the address, don't use it 3401/// @param silent no errors or side effects 3402/// @param to_other_file flag: may jump to other file 3403/// @param address_count 1 for first, >1 after comma 3404/// @param errormsg Error message, if any 3405/// 3406/// @return MAXLNUM when no Ex address was found. 3407linenr_T get_address (exarg_T * eap ,char ** ptr ,cmd_addr_T addr_type ,bool skip ,bool silent , 3408int to_other_file ,int address_count ,const char ** errormsg ) 3409FUNC_ATTR_NONNULL_ARG (2 ,8 ) 3410{ 3411int c ; 3412int i ; 3413linenr_T n ; 3414pos_T pos ; 3415buf_T * buf ; 3416 3417char * cmd = skipwhite (* ptr ); 3418linenr_T lnum = MAXLNUM ; 3419do { 3420switch (* cmd ) { 3421case '.' :// '.' - Cursor position 3422cmd ++ ; 3423switch (addr_type ) { 3424case ADDR_LINES : 3425case ADDR_OTHER : 3426lnum = curwin -> w_cursor .lnum ; 3427break ; 3428case ADDR_WINDOWS : 3429lnum = CURRENT_WIN_NR ; 3430break ; 3431case ADDR_ARGUMENTS : 3432lnum = curwin -> w_arg_idx + 1 ; 3433break ; 3434case ADDR_LOADED_BUFFERS : 3435case ADDR_BUFFERS : 3436lnum = curbuf -> b_fnum ; 3437break ; 3438case ADDR_TABS : 3439lnum = CURRENT_TAB_NR ; 3440break ; 3441case ADDR_NONE : 3442case ADDR_TABS_RELATIVE : 3443case ADDR_UNSIGNED : 3444* errormsg = addr_error (addr_type ); 3445cmd = NULL ; 3446 gotoerror ; 3447break ; 3448case ADDR_QUICKFIX : 3449lnum = (linenr_T )qf_get_cur_idx (eap ); 3450break ; 3451case ADDR_QUICKFIX_VALID : 3452lnum = qf_get_cur_valid_idx (eap ); 3453break ; 3454 } 3455break ; 3456 3457case '$' :// '$' - last line 3458cmd ++ ; 3459switch (addr_type ) { 3460case ADDR_LINES : 3461case ADDR_OTHER : 3462lnum = curbuf -> b_ml .ml_line_count ; 3463break ; 3464case ADDR_WINDOWS : 3465lnum = LAST_WIN_NR (eap ); 3466break ; 3467case ADDR_ARGUMENTS : 3468lnum = ARGCOUNT ; 3469break ; 3470case ADDR_LOADED_BUFFERS : 3471buf = lastbuf ; 3472while (buf -> b_ml .ml_mfp == NULL ) { 3473if (buf -> b_prev == NULL ) { 3474break ; 3475 } 3476buf = buf -> b_prev ; 3477 } 3478lnum = buf -> b_fnum ; 3479break ; 3480case ADDR_BUFFERS : 3481lnum = lastbuf -> b_fnum ; 3482break ; 3483case ADDR_TABS : 3484lnum = LAST_TAB_NR ; 3485break ; 3486case ADDR_NONE : 3487case ADDR_TABS_RELATIVE : 3488case ADDR_UNSIGNED : 3489* errormsg = addr_error (addr_type ); 3490cmd = NULL ; 3491 gotoerror ; 3492break ; 3493case ADDR_QUICKFIX : 3494lnum = (linenr_T )qf_get_size (eap ); 3495if (lnum == 0 ) { 3496lnum = 1 ; 3497 } 3498break ; 3499case ADDR_QUICKFIX_VALID : 3500lnum = (linenr_T )qf_get_valid_size (eap ); 3501if (lnum == 0 ) { 3502lnum = 1 ; 3503 } 3504break ; 3505 } 3506break ; 3507 3508case '\'' :// ''' - mark 3509if (*++ cmd == NUL ) { 3510cmd = NULL ; 3511 gotoerror ; 3512 } 3513if (addr_type != ADDR_LINES ) { 3514* errormsg = addr_error (addr_type ); 3515cmd = NULL ; 3516 gotoerror ; 3517 } 3518if (skip ) { 3519cmd ++ ; 3520 }else { 3521// Only accept a mark in another file when it is 3522// used by itself: ":'M". 3523MarkGet flag = to_other_file && cmd [1 ]== NUL ?kMarkAll :kMarkBufLocal ; 3524fmark_T * fm = mark_get (curbuf ,curwin ,NULL ,flag ,* cmd ); 3525cmd ++ ; 3526if (fm != NULL && fm -> fnum != curbuf -> handle ) { 3527mark_move_to (fm ,0 ); 3528// Jumped to another file. 3529lnum = curwin -> w_cursor .lnum ; 3530 }else { 3531if (!mark_check (fm ,errormsg )) { 3532cmd = NULL ; 3533 gotoerror ; 3534 } 3535assert (fm != NULL ); 3536lnum = fm -> mark .lnum ; 3537 } 3538 } 3539break ; 3540 3541case '/' : 3542case '?' :// '/' or '?' - search 3543c = (uint8_t )(* cmd ++ ); 3544if (addr_type != ADDR_LINES ) { 3545* errormsg = addr_error (addr_type ); 3546cmd = NULL ; 3547 gotoerror ; 3548 } 3549if (skip ) {// skip "/pat/" 3550cmd = skip_regexp (cmd ,c ,p_magic ); 3551if (* cmd == c ) { 3552cmd ++ ; 3553 } 3554 }else { 3555int flags ; 3556 3557pos = curwin -> w_cursor ;// save curwin->w_cursor 3558 3559// When '/' or '?' follows another address, start from 3560// there. 3561if (lnum > 0 && lnum != MAXLNUM ) { 3562curwin -> w_cursor .lnum 3563= lnum > curbuf -> b_ml .ml_line_count ?curbuf -> b_ml .ml_line_count :lnum ; 3564 } 3565 3566// Start a forward search at the end of the line (unless 3567// before the first line). 3568// Start a backward search at the start of the line. 3569// This makes sure we never match in the current 3570// line, and can match anywhere in the 3571// next/previous line. 3572curwin -> w_cursor .col = (c == '/' && curwin -> w_cursor .lnum > 0 ) ?MAXCOL :0 ; 3573Search .cmdlen = 0 ; 3574flags = silent ?SEARCH_KEEP :SEARCH_HIS |SEARCH_MSG ; 3575if (!do_search (NULL ,c ,c ,cmd ,strlen (cmd ),1 ,flags ,p_magic ,NULL )) { 3576curwin -> w_cursor = pos ; 3577cmd = NULL ; 3578 gotoerror ; 3579 } 3580lnum = curwin -> w_cursor .lnum ; 3581curwin -> w_cursor = pos ; 3582// adjust command string pointer 3583cmd += Search .cmdlen ; 3584 } 3585break ; 3586 3587case '\\' :// "\?", "\/" or "\&", repeat search 3588cmd ++ ; 3589if (addr_type != ADDR_LINES ) { 3590* errormsg = addr_error (addr_type ); 3591cmd = NULL ; 3592 gotoerror ; 3593 } 3594if (* cmd == '&' ) { 3595i = RE_SUBST ; 3596 }else if (* cmd == '?' || * cmd == '/' ) { 3597i = RE_SEARCH ; 3598 }else { 3599* errormsg = _ (e_backslash ); 3600cmd = NULL ; 3601 gotoerror ; 3602 } 3603 3604if (!skip ) { 3605// When search follows another address, start from there. 3606pos .lnum = (lnum != MAXLNUM ) ?lnum :curwin -> w_cursor .lnum ; 3607// Start the search just like for the above do_search(). 3608pos .col = (* cmd != '?' ) ?MAXCOL :0 ; 3609pos .coladd = 0 ; 3610if (searchit (curwin ,curbuf ,& pos ,NULL , 3611* cmd == '?' ?BACKWARD :FORWARD , 3612"" ,0 ,1 ,SEARCH_MSG ,i ,p_magic ,NULL )!= FAIL ) { 3613lnum = pos .lnum ; 3614 }else { 3615cmd = NULL ; 3616 gotoerror ; 3617 } 3618 } 3619cmd ++ ; 3620break ; 3621 3622default : 3623if (ascii_isdigit (* cmd )) {// absolute line number 3624lnum = (linenr_T )getdigits (& cmd , false,0 ); 3625 } 3626 } 3627 3628while (true) { 3629cmd = skipwhite (cmd ); 3630if (* cmd != '-' && * cmd != '+' && !ascii_isdigit (* cmd )) { 3631break ; 3632 } 3633 3634if (lnum == MAXLNUM ) { 3635switch (addr_type ) { 3636case ADDR_LINES : 3637case ADDR_OTHER : 3638// "+1" is same as ".+1" 3639lnum = curwin -> w_cursor .lnum ; 3640break ; 3641case ADDR_WINDOWS : 3642lnum = CURRENT_WIN_NR ; 3643break ; 3644case ADDR_ARGUMENTS : 3645lnum = curwin -> w_arg_idx + 1 ; 3646break ; 3647case ADDR_LOADED_BUFFERS : 3648case ADDR_BUFFERS : 3649lnum = curbuf -> b_fnum ; 3650break ; 3651case ADDR_TABS : 3652lnum = CURRENT_TAB_NR ; 3653break ; 3654case ADDR_TABS_RELATIVE : 3655lnum = 1 ; 3656break ; 3657case ADDR_QUICKFIX : 3658lnum = (linenr_T )qf_get_cur_idx (eap ); 3659break ; 3660case ADDR_QUICKFIX_VALID : 3661lnum = qf_get_cur_valid_idx (eap ); 3662break ; 3663case ADDR_NONE : 3664case ADDR_UNSIGNED : 3665lnum = 0 ; 3666break ; 3667 } 3668 } 3669 3670if (ascii_isdigit (* cmd )) { 3671i = '+' ;// "number" is same as "+number" 3672 }else { 3673i = (uint8_t )(* cmd ++ ); 3674 } 3675if (!ascii_isdigit (* cmd )) {// '+' is '+1' 3676n = 1 ; 3677 }else { 3678// "number", "+number" or "-number" 3679n = getdigits_int32 (& cmd , false,MAXLNUM ); 3680if (n == MAXLNUM ) { 3681* errormsg = _ (e_line_number_out_of_range ); 3682cmd = NULL ; 3683 gotoerror ; 3684 } 3685 } 3686 3687if (addr_type == ADDR_TABS_RELATIVE ) { 3688* errormsg = _ (e_invrange ); 3689cmd = NULL ; 3690 gotoerror ; 3691 }else if (addr_type == ADDR_LOADED_BUFFERS || addr_type == ADDR_BUFFERS ) { 3692lnum = compute_buffer_local_count (addr_type ,lnum , (i == '-' ) ?-1 * n :n ); 3693 }else { 3694// Relative line addressing: need to adjust for lines in a 3695// closed fold after the first address. 3696if (addr_type == ADDR_LINES && (i == '-' || i == '+' ) 3697&& address_count >=2 ) { 3698hasFolding (curwin ,lnum ,NULL ,& lnum ); 3699 } 3700if (i == '-' ) { 3701lnum -= n ; 3702 }else { 3703if (lnum >=0 && n >=INT32_MAX - lnum ) { 3704* errormsg = _ (e_line_number_out_of_range ); 3705cmd = NULL ; 3706 gotoerror ; 3707 } 3708lnum += n ; 3709 } 3710 } 3711 } 3712 }while (* cmd == '/' || * cmd == '?' ); 3713 3714error : 3715* ptr = cmd ; 3716return lnum ; 3717} 3718 3719/// Get flags from an Ex command argument. 3720static void get_flags (exarg_T * eap ) 3721{ 3722while (vim_strchr ("lp#" , (uint8_t )(* eap -> arg ))!= NULL ) { 3723if (* eap -> arg == 'l' ) { 3724eap -> flags |=EXFLAG_LIST ; 3725 }else if (* eap -> arg == 'p' ) { 3726eap -> flags |=EXFLAG_PRINT ; 3727 }else { 3728eap -> flags |=EXFLAG_NR ; 3729 } 3730eap -> arg = skipwhite (eap -> arg + 1 ); 3731 } 3732} 3733 3734/// Stub function for command which is Not Implemented. NI! 3735void ex_ni (exarg_T * eap ) 3736{ 3737if (!eap -> skip ) { 3738eap -> errmsg = _ ("E319: The command is not available in this version" ); 3739 } 3740} 3741 3742/// Stub function for script command which is Not Implemented. NI! 3743/// Skips over ":perl <<EOF" constructs. 3744static void ex_script_ni (exarg_T * eap ) 3745{ 3746if (!eap -> skip ) { 3747ex_ni (eap ); 3748 }else { 3749size_t len ; 3750xfree (script_get (eap ,& len )); 3751 } 3752} 3753 3754/// Check range in Ex command for validity. 3755/// 3756/// @return NULL when valid, error message when invalid. 3757char * invalid_range (exarg_T * eap ) 3758{ 3759buf_T * buf ; 3760if (eap -> line1 < 0 || eap -> line2 < 0 || eap -> line1 > eap -> line2 ) { 3761return _ (e_invrange ); 3762 } 3763 3764if (eap -> argt & EX_RANGE ) { 3765switch (eap -> addr_type ) { 3766case ADDR_LINES : 3767if (eap -> line2 > 3768 (curbuf -> b_ml .ml_line_count 3769+ (eap -> cmdidx == CMD_diffget || eap -> cmdidx == CMD_diffput ))) { 3770return _ (e_invrange ); 3771 } 3772break ; 3773case ADDR_ARGUMENTS : 3774// add 1 if ARGCOUNT is 0 3775if (eap -> line2 > ARGCOUNT + (!ARGCOUNT )) { 3776return _ (e_invrange ); 3777 } 3778break ; 3779case ADDR_BUFFERS : 3780// Only a boundary check, not whether the buffers actually 3781// exist. 3782if (eap -> line1 < 1 || eap -> line2 > get_highest_fnum ()) { 3783return _ (e_invrange ); 3784 } 3785break ; 3786case ADDR_LOADED_BUFFERS : 3787buf = firstbuf ; 3788while (buf -> b_ml .ml_mfp == NULL ) { 3789if (buf -> b_next == NULL ) { 3790return _ (e_invrange ); 3791 } 3792buf = buf -> b_next ; 3793 } 3794if (eap -> line1 < buf -> b_fnum ) { 3795return _ (e_invrange ); 3796 } 3797buf = lastbuf ; 3798while (buf -> b_ml .ml_mfp == NULL ) { 3799if (buf -> b_prev == NULL ) { 3800return _ (e_invrange ); 3801 } 3802buf = buf -> b_prev ; 3803 } 3804if (eap -> line2 > buf -> b_fnum ) { 3805return _ (e_invrange ); 3806 } 3807break ; 3808case ADDR_WINDOWS : 3809if (eap -> line2 > LAST_WIN_NR (eap )) { 3810return _ (e_invrange ); 3811 } 3812break ; 3813case ADDR_TABS : 3814if (eap -> line2 > LAST_TAB_NR ) { 3815return _ (e_invrange ); 3816 } 3817break ; 3818case ADDR_TABS_RELATIVE : 3819case ADDR_OTHER : 3820// Any range is OK. 3821break ; 3822case ADDR_QUICKFIX : 3823assert (eap -> line2 >=0 ); 3824// No error for value that is too big, will use the last entry. 3825if (eap -> line2 <=0 ) { 3826if (eap -> addr_count == 0 ) { 3827return _ (e_no_errors ); 3828 } 3829return _ (e_invrange ); 3830 } 3831break ; 3832case ADDR_QUICKFIX_VALID : 3833if ((eap -> line2 != 1 && (size_t )eap -> line2 > qf_get_valid_size (eap )) 3834|| eap -> line2 < 0 ) { 3835return _ (e_invrange ); 3836 } 3837break ; 3838case ADDR_UNSIGNED : 3839case ADDR_NONE : 3840// Will give an error elsewhere. 3841break ; 3842 } 3843 } 3844return NULL ; 3845} 3846 3847/// Correct the range for zero line number, if required. 3848static void correct_range (exarg_T * eap ) 3849{ 3850if (!(eap -> argt & EX_ZEROR )) {// zero in range not allowed 3851if (eap -> line1 == 0 ) { 3852eap -> line1 = 1 ; 3853 } 3854if (eap -> line2 == 0 ) { 3855eap -> line2 = 1 ; 3856 } 3857 } 3858} 3859 3860/// For a ":vimgrep" or ":vimgrepadd" command return a pointer past the 3861/// pattern. Otherwise return eap->arg. 3862static char * skip_grep_pat (exarg_T * eap ) 3863{ 3864char * p = eap -> arg ; 3865 3866if (* p != NUL && (eap -> cmdidx == CMD_vimgrep || eap -> cmdidx == CMD_lvimgrep 3867|| eap -> cmdidx == CMD_vimgrepadd 3868|| eap -> cmdidx == CMD_lvimgrepadd 3869|| grep_internal (eap -> cmdidx ))) { 3870p = skip_vimgrep_pat (p ,NULL ,NULL ); 3871if (p == NULL ) { 3872p = eap -> arg ; 3873 } 3874 } 3875return p ; 3876} 3877 3878/// For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option 3879/// in the command line, so that things like % get expanded. 3880char * replace_makeprg (exarg_T * eap ,char * arg ,char ** cmdlinep ) 3881{ 3882bool isgrep = eap -> cmdidx == CMD_grep 3883|| eap -> cmdidx == CMD_lgrep 3884|| eap -> cmdidx == CMD_grepadd 3885|| eap -> cmdidx == CMD_lgrepadd ; 3886 3887// Don't do it when ":vimgrep" is used for ":grep". 3888if ((eap -> cmdidx == CMD_make || eap -> cmdidx == CMD_lmake || isgrep ) 3889&& !grep_internal (eap -> cmdidx )) { 3890const char * program = isgrep ? (* curbuf -> b_p_gp == NUL ?p_gp :curbuf -> b_p_gp ) 3891 : (* curbuf -> b_p_mp == NUL ?p_mp :curbuf -> b_p_mp ); 3892 3893arg = skipwhite (arg ); 3894 3895char * new_cmdline ; 3896// Replace $* by given arguments 3897if ((new_cmdline = strrep (program ,"$*" ,arg ))== NULL ) { 3898// No $* in arg, build "<makeprg> <arg>" instead 3899new_cmdline = xmalloc (strlen (program )+ strlen (arg )+ 2 ); 3900STRCPY (new_cmdline ,program ); 3901strcat (new_cmdline ," " ); 3902strcat (new_cmdline ,arg ); 3903 } 3904 3905msg_make (arg ); 3906 3907// 'eap->cmd' is not set here, because it is not used at CMD_make 3908xfree (* cmdlinep ); 3909* cmdlinep = new_cmdline ; 3910arg = new_cmdline ; 3911 } 3912return arg ; 3913} 3914 3915/// Expand file name in Ex command argument. 3916/// When an error is detected, "errormsgp" is set to a non-NULL pointer. 3917/// 3918/// @return FAIL for failure, OK otherwise. 3919int expand_filename (exarg_T * eap ,char ** cmdlinep ,const char ** errormsgp ) 3920{ 3921// Skip a regexp pattern for ":vimgrep[add] pat file..." 3922char * p = skip_grep_pat (eap ); 3923 3924// Decide to expand wildcards *before* replacing '%', '#', etc. If 3925// the file name contains a wildcard it should not cause expanding. 3926// (it will be expanded anyway if there is a wildcard before replacing). 3927bool has_wildcards = path_has_wildcard (p , true); 3928while (* p != NUL ) { 3929// Skip over `=expr`, wildcards in it are not expanded. 3930if (p [0 ]== '`' && p [1 ]== '=' ) { 3931p += 2 ; 3932skip_expr (& p ,NULL ); 3933if (* p == '`' ) { 3934p ++ ; 3935 } 3936continue ; 3937 } 3938// Quick check if this cannot be the start of a special string. 3939// Also removes backslash before '%', '#' and '<'. 3940if (vim_strchr ("%#<" , (uint8_t )(* p ))== NULL ) { 3941p ++ ; 3942continue ; 3943 } 3944 3945// Try to find a match at this position. 3946size_t srclen ; 3947int escaped ; 3948char * repl = eval_vars (p ,eap -> arg ,& srclen ,& (eap -> do_ecmd_lnum ), 3949errormsgp ,& escaped , true); 3950if (* errormsgp != NULL ) {// error detected 3951return FAIL ; 3952 } 3953if (repl == NULL ) {// no match found 3954p += srclen ; 3955continue ; 3956 } 3957 3958// Wildcards won't be expanded below, the replacement is taken 3959// literally. But do expand "~/file", "~user/file" and "$HOME/file". 3960if (vim_strchr (repl ,'$' )!= NULL || vim_strchr (repl ,'~' )!= NULL ) { 3961char * l = repl ; 3962 3963repl = expand_env_save (repl ); 3964xfree (l ); 3965 } 3966 3967// Need to escape white space et al. with a backslash. 3968// Don't do this for: 3969// - replacement that already has been escaped: "##" 3970// - shell commands (may have to use quotes instead). 3971if (!eap -> usefilter 3972&& !escaped 3973&& eap -> cmdidx != CMD_bang 3974&& eap -> cmdidx != CMD_grep 3975&& eap -> cmdidx != CMD_grepadd 3976&& eap -> cmdidx != CMD_lgrep 3977&& eap -> cmdidx != CMD_lgrepadd 3978&& eap -> cmdidx != CMD_lmake 3979&& eap -> cmdidx != CMD_make 3980&& eap -> cmdidx != CMD_terminal 3981&& !(eap -> argt & EX_NOSPC )) { 3982char * l ; 3983#ifdef BACKSLASH_IN_FILENAME 3984// Don't escape a backslash here, because rem_backslash() doesn't 3985// remove it later. 3986static char * nobslash = " \t\"|" ; 3987# define ESCAPE_CHARS nobslash 3988#else 3989# define ESCAPE_CHARS escape_chars 3990#endif 3991 3992for (l = repl ;* l ;l ++ ) { 3993if (vim_strchr (ESCAPE_CHARS , (uint8_t )(* l ))!= NULL ) { 3994l = vim_strsave_escaped (repl ,ESCAPE_CHARS ); 3995xfree (repl ); 3996repl = l ; 3997break ; 3998 } 3999 } 4000 } 4001 4002// For a shell command a '!' must be escaped. 4003if ((eap -> usefilter 4004|| eap -> cmdidx == CMD_bang 4005|| eap -> cmdidx == CMD_terminal ) 4006&& strpbrk (repl ,"!" )!= NULL ) { 4007char * l = vim_strsave_escaped (repl ,"!" ); 4008xfree (repl ); 4009repl = l ; 4010 } 4011 4012p = repl_cmdline (eap ,p ,srclen ,repl ,cmdlinep ); 4013xfree (repl ); 4014 } 4015 4016// One file argument: Expand wildcards. 4017// Don't do this with ":r !command" or ":w !command". 4018if ((eap -> argt & EX_NOSPC )&& !eap -> usefilter ) { 4019// Replace environment variables. 4020if (has_wildcards ) { 4021// May expand environment variables. This 4022// can be done much faster with expand_env() than with 4023// something else (e.g., calling a shell). 4024// After expanding environment variables, check again 4025// if there are still wildcards present. 4026if (vim_strchr (eap -> arg ,'$' )!= NULL 4027|| vim_strchr (eap -> arg ,'~' )!= NULL ) { 4028expand_env_esc (eap -> arg ,NameBuff ,MAXPATHL , (char * )(" \t" PATH_ESC_WILDCARDS ), true, 4029NULL ); 4030has_wildcards = path_has_wildcard (NameBuff , true); 4031p = NameBuff ; 4032 }else { 4033p = NULL ; 4034 } 4035if (p != NULL ) { 4036repl_cmdline (eap ,eap -> arg ,strlen (eap -> arg ),p ,cmdlinep ); 4037 } 4038 } 4039 4040// Halve the number of backslashes (this is Vi compatible). 4041// For Unix, when wildcards are expanded, this is 4042// done by ExpandOne() below. 4043#ifdef UNIX 4044if (!has_wildcards ) { 4045backslash_halve (eap -> arg ); 4046 } 4047#else 4048backslash_halve (eap -> arg ); 4049#endif 4050 4051if (has_wildcards ) { 4052expand_T xpc ; 4053int options = WILD_LIST_NOTFOUND |WILD_NOERROR |WILD_ADD_SLASH ; 4054 4055ExpandInit (& xpc ); 4056xpc .xp_context = EXPAND_FILES ; 4057if (p_wic ) { 4058options += WILD_ICASE ; 4059 } 4060p = ExpandOne (& xpc ,eap -> arg ,NULL ,options ,WILD_EXPAND_FREE ); 4061if (p == NULL ) { 4062return FAIL ; 4063 } 4064repl_cmdline (eap ,eap -> arg ,strlen (eap -> arg ),p ,cmdlinep ); 4065xfree (p ); 4066 }else { 4067TO_SLASH (eap -> arg ); 4068 } 4069 } 4070return OK ; 4071} 4072 4073/// Replace part of the command line, keeping eap->cmd, eap->arg, eap->args and 4074/// eap->nextcmd correct. 4075/// "src" points to the part that is to be replaced, of length "srclen". 4076/// "repl" is the replacement string. 4077/// 4078/// @return a pointer to the character after the replaced string. 4079static char * repl_cmdline (exarg_T * eap ,char * src ,size_t srclen ,char * repl ,char ** cmdlinep ) 4080{ 4081// The new command line is build in new_cmdline[]. 4082// First allocate it. 4083// Careful: a "+cmd" argument may have been NUL terminated. 4084size_t len = strlen (repl ); 4085size_t i = (size_t )(src - * cmdlinep )+ strlen (src + srclen )+ len + 3 ; 4086if (eap -> nextcmd != NULL ) { 4087i += strlen (eap -> nextcmd );// add space for next command 4088 } 4089char * new_cmdline = xmalloc (i ); 4090size_t offset = (size_t )(src - * cmdlinep ); 4091 4092// Copy the stuff before the expanded part. 4093// Copy the expanded stuff. 4094// Copy what came after the expanded part. 4095// Copy the next commands, if there are any. 4096i = offset ;// length of part before match 4097memmove (new_cmdline ,* cmdlinep ,i ); 4098 4099memmove (new_cmdline + i ,repl ,len ); 4100i += len ;// remember the end of the string 4101STRCPY (new_cmdline + i ,src + srclen ); 4102src = new_cmdline + i ;// remember where to continue 4103 4104if (eap -> nextcmd != NULL ) {// append next command 4105i = strlen (new_cmdline )+ 1 ; 4106STRCPY (new_cmdline + i ,eap -> nextcmd ); 4107eap -> nextcmd = new_cmdline + i ; 4108 } 4109eap -> cmd = new_cmdline + (eap -> cmd - * cmdlinep ); 4110eap -> arg = new_cmdline + (eap -> arg - * cmdlinep ); 4111 4112for (size_t j = 0 ;j < eap -> argc ;j ++ ) { 4113if (offset >= (size_t )(eap -> args [j ]- * cmdlinep )) { 4114// If replaced text is after or in the same position as the argument, 4115// the argument's position relative to the beginning of the cmdline stays the same. 4116eap -> args [j ]= new_cmdline + (eap -> args [j ]- * cmdlinep ); 4117 }else { 4118// Otherwise, argument gets shifted alongside the replaced text. 4119// The amount of the shift is equal to the difference of the old and new string length. 4120eap -> args [j ]= new_cmdline + ((eap -> args [j ]- * cmdlinep )+ (ptrdiff_t )(len - srclen )); 4121 } 4122 } 4123 4124if (eap -> do_ecmd_cmd != NULL && eap -> do_ecmd_cmd != dollar_command ) { 4125eap -> do_ecmd_cmd = new_cmdline + (eap -> do_ecmd_cmd - * cmdlinep ); 4126 } 4127xfree (* cmdlinep ); 4128* cmdlinep = new_cmdline ; 4129 4130return src ; 4131} 4132 4133/// Check for '|' to separate commands and '"' to start comments. 4134void separate_nextcmd (exarg_T * eap ) 4135{ 4136char * p = skip_grep_pat (eap ); 4137 4138for (;* p ;MB_PTR_ADV (p )) { 4139if (* p == Ctrl_V ) { 4140if (eap -> argt & (EX_CTRLV |EX_XFILE )) { 4141p ++ ;// skip CTRL-V and next char 4142 }else { 4143// remove CTRL-V and skip next char 4144STRMOVE (p ,p + 1 ); 4145 } 4146if (* p == NUL ) {// stop at NUL after CTRL-V 4147break ; 4148 } 4149 }else if (p [0 ]== '`' && p [1 ]== '=' && (eap -> argt & EX_XFILE )) { 4150// Skip over `=expr` when wildcards are expanded. 4151p += 2 ; 4152skip_expr (& p ,NULL ); 4153if (* p == NUL ) {// stop at NUL after CTRL-V 4154break ; 4155 } 4156 }else if ( 4157// Check for '"': start of comment or '|': next command 4158// :@" does not start a comment! 4159// :redir @" doesn't either. 4160 (* p == '"' 4161&& !(eap -> argt & EX_NOTRLCOM ) 4162&& (eap -> cmdidx != CMD_at || p != eap -> arg ) 4163&& (eap -> cmdidx != CMD_redir 4164|| p != eap -> arg + 1 || p [-1 ]!= '@' )) 4165|| (* p == '|' 4166&& eap -> cmdidx != CMD_append 4167&& eap -> cmdidx != CMD_change 4168&& eap -> cmdidx != CMD_insert ) 4169|| * p == '\n' ) { 4170// We remove the '\' before the '|', unless EX_CTRLV is used 4171// AND 'b' is present in 'cpoptions'. 4172if ((vim_strchr (p_cpo ,kCpoBar )== NULL 4173|| !(eap -> argt & EX_CTRLV ))&& * (p - 1 )== '\\' ) { 4174STRMOVE (p - 1 ,p );// remove the '\' 4175p -- ; 4176 }else { 4177eap -> nextcmd = check_nextcmd (p ); 4178* p = NUL ; 4179break ; 4180 } 4181 } 4182 } 4183 4184if (!(eap -> argt & EX_NOTRLCOM )) {// remove trailing spaces 4185del_trailing_spaces (eap -> arg ); 4186 } 4187} 4188 4189/// get + command from ex argument 4190char * getargcmd (char ** argp ) 4191{ 4192char * arg = * argp ; 4193char * command = NULL ; 4194 4195if (* arg == '+' ) {// +[command] 4196arg ++ ; 4197if (ascii_isspace (* arg )|| * arg == NUL ) { 4198command = dollar_command ; 4199 }else { 4200command = arg ; 4201arg = skip_cmd_arg (command , true); 4202if (* arg != NUL ) { 4203* arg ++ = NUL ;// terminate command with NUL 4204 } 4205 } 4206 4207arg = skipwhite (arg );// skip over spaces 4208* argp = arg ; 4209 } 4210return command ; 4211} 4212 4213/// Find end of "+command" argument. Skip over "\ " and "\\". 4214/// 4215/// @param rembs true to halve the number of backslashes 4216char * skip_cmd_arg (char * p ,bool rembs ) 4217{ 4218while (* p && !ascii_isspace (* p )) { 4219if (* p == '\\' && p [1 ]!= NUL ) { 4220if (rembs ) { 4221STRMOVE (p ,p + 1 ); 4222 }else { 4223p ++ ; 4224 } 4225 } 4226MB_PTR_ADV (p ); 4227 } 4228return p ; 4229} 4230 4231int get_bad_opt (const char * p ,exarg_T * eap ) 4232FUNC_ATTR_NONNULL_ALL 4233{ 4234if (STRICMP (p ,"keep" )== 0 ) { 4235eap -> bad_char = BAD_KEEP ; 4236 }else if (STRICMP (p ,"drop" )== 0 ) { 4237eap -> bad_char = BAD_DROP ; 4238 }else if (MB_BYTE2LEN ((uint8_t )(* p ))== 1 && p [1 ]== NUL ) { 4239eap -> bad_char = (uint8_t )(* p ); 4240 }else { 4241return FAIL ; 4242 } 4243return OK ; 4244} 4245 4246/// Function given to ExpandGeneric() to obtain the list of bad= names. 4247static char * get_bad_name (expand_T * xp FUNC_ATTR_UNUSED ,int idx ) 4248{ 4249// Note: Keep this in sync with get_bad_opt(). 4250static char * (p_bad_values [])= { 4251"?" , 4252"keep" , 4253"drop" , 4254 }; 4255 4256if (idx < (int )ARRAY_SIZE (p_bad_values )) { 4257return p_bad_values [idx ]; 4258 } 4259return NULL ; 4260} 4261 4262/// Get "++opt=arg" argument. 4263/// 4264/// @return FAIL or OK. 4265int getargopt (exarg_T * eap ) 4266{ 4267char * arg = eap -> arg + 2 ; 4268int * pp = NULL ; 4269int bad_char_idx ; 4270 4271// Note: Keep this in sync with get_argopt_name. 4272 4273// ":edit ++[no]bin[ary] file" 4274if (strncmp (arg ,"bin" ,3 )== 0 || strncmp (arg ,"nobin" ,5 )== 0 ) { 4275if (* arg == 'n' ) { 4276arg += 2 ; 4277eap -> force_bin = FORCE_NOBIN ; 4278 }else { 4279eap -> force_bin = FORCE_BIN ; 4280 } 4281if (!checkforcmd (& arg ,"binary" ,3 )) { 4282return FAIL ; 4283 } 4284eap -> arg = skipwhite (arg ); 4285return OK ; 4286 } 4287 4288// ":read ++edit file" 4289if (strncmp (arg ,"edit" ,4 )== 0 && !ASCII_ISALPHA (arg [4 ])) { 4290eap -> read_edit = true; 4291eap -> arg = skipwhite (arg + 4 ); 4292return OK ; 4293 } 4294 4295// ":write ++p foo/bar/file 4296if (arg [0 ]== 'p' && !ASCII_ISALPHA (arg [1 ])) { 4297eap -> mkdir_p = true; 4298eap -> arg = skipwhite (arg + 1 ); 4299return OK ; 4300 } 4301 4302if (strncmp (arg ,"ff" ,2 )== 0 ) { 4303arg += 2 ; 4304pp = & eap -> force_ff ; 4305 }else if (strncmp (arg ,"fileformat" ,10 )== 0 ) { 4306arg += 10 ; 4307pp = & eap -> force_ff ; 4308 }else if (strncmp (arg ,"enc" ,3 )== 0 ) { 4309if (strncmp (arg ,"encoding" ,8 )== 0 ) { 4310arg += 8 ; 4311 }else { 4312arg += 3 ; 4313 } 4314pp = & eap -> force_enc ; 4315 }else if (strncmp (arg ,"bad" ,3 )== 0 ) { 4316arg += 3 ; 4317pp = & bad_char_idx ; 4318 } 4319 4320if (pp == NULL || * arg != '=' ) { 4321return FAIL ; 4322 } 4323 4324arg ++ ; 4325* pp = (int )(arg - eap -> cmd ); 4326arg = skip_cmd_arg (arg , false); 4327eap -> arg = skipwhite (arg ); 4328* arg = NUL ; 4329 4330if (pp == & eap -> force_ff ) { 4331if (check_ff_value (eap -> cmd + eap -> force_ff )== FAIL ) { 4332return FAIL ; 4333 } 4334eap -> force_ff = (uint8_t )eap -> cmd [eap -> force_ff ]; 4335 }else if (pp == & eap -> force_enc ) { 4336// Make 'fileencoding' lower case. 4337for (char * p = eap -> cmd + eap -> force_enc ;* p != NUL ;p ++ ) { 4338* p = (char )TOLOWER_ASC (* p ); 4339 } 4340 }else { 4341// Check ++bad= argument. Must be a single-byte character, "keep" or 4342// "drop". 4343if (get_bad_opt (eap -> cmd + bad_char_idx ,eap )== FAIL ) { 4344return FAIL ; 4345 } 4346 } 4347 4348return OK ; 4349} 4350 4351/// Function given to ExpandGeneric() to obtain the list of ++opt names. 4352static char * get_argopt_name (expand_T * xp FUNC_ATTR_UNUSED ,int idx ) 4353{ 4354// Note: Keep this in sync with getargopt(). 4355static char * (p_opt_values [])= { 4356"fileformat=" , 4357"encoding=" , 4358"binary" , 4359"nobinary" , 4360"bad=" , 4361"edit" , 4362"p" , 4363 }; 4364 4365if (idx < (int )ARRAY_SIZE (p_opt_values )) { 4366return p_opt_values [idx ]; 4367 } 4368return NULL ; 4369} 4370 4371/// Command-line expansion for ++opt=name. 4372int expand_argopt (char * pat ,expand_T * xp ,regmatch_T * rmp ,char *** matches ,int * numMatches ) 4373{ 4374if (xp -> xp_pattern > xp -> xp_line && * (xp -> xp_pattern - 1 )== '=' ) { 4375CompleteListItemGetter cb = NULL ; 4376 4377char * name_end = xp -> xp_pattern - 1 ; 4378if (name_end - xp -> xp_line >=2 4379&& strncmp (name_end - 2 ,"ff" ,2 )== 0 ) { 4380cb = get_fileformat_name ; 4381 }else if (name_end - xp -> xp_line >=10 4382&& strncmp (name_end - 10 ,"fileformat" ,10 )== 0 ) { 4383cb = get_fileformat_name ; 4384 }else if (name_end - xp -> xp_line >=3 4385&& strncmp (name_end - 3 ,"enc" ,3 )== 0 ) { 4386cb = get_encoding_name ; 4387 }else if (name_end - xp -> xp_line >=8 4388&& strncmp (name_end - 8 ,"encoding" ,8 )== 0 ) { 4389cb = get_encoding_name ; 4390 }else if (name_end - xp -> xp_line >=3 4391&& strncmp (name_end - 3 ,"bad" ,3 )== 0 ) { 4392cb = get_bad_name ; 4393 } 4394 4395if (cb != NULL ) { 4396ExpandGeneric (pat ,xp ,rmp ,matches ,numMatches ,cb , false); 4397return OK ; 4398 } 4399return FAIL ; 4400 } 4401 4402// Special handling of "ff" which acts as a short form of 4403// "fileformat", as "ff" is not a substring of it. 4404if (xp -> xp_pattern_len == 2 4405&& strncmp (xp -> xp_pattern ,"ff" ,xp -> xp_pattern_len )== 0 ) { 4406* matches = xmalloc (sizeof (char * )); 4407* numMatches = 1 ; 4408 (* matches )[0 ]= xstrdup ("fileformat=" ); 4409return OK ; 4410 } 4411 4412ExpandGeneric (pat ,xp ,rmp ,matches ,numMatches ,get_argopt_name , false); 4413return OK ; 4414} 4415 4416/// Handle the argument for a tabpage related ex command. 4417/// When an error is encountered then eap->errmsg is set. 4418/// 4419/// @return a tabpage number. 4420static int get_tabpage_arg (exarg_T * eap ) 4421{ 4422int tab_number = 0 ; 4423int unaccept_arg0 = (eap -> cmdidx == CMD_tabmove ) ?0 :1 ; 4424 4425if (eap -> arg && * eap -> arg != NUL ) { 4426char * p = eap -> arg ; 4427int relative = 0 ;// argument +N/-N means: go to N places to the 4428// right/left relative to the current position. 4429 4430if (* p == '-' ) { 4431relative = -1 ; 4432p ++ ; 4433 }else if (* p == '+' ) { 4434relative = 1 ; 4435p ++ ; 4436 } 4437 4438char * p_save = p ; 4439tab_number = (int )getdigits (& p , false,tab_number ); 4440 4441if (relative == 0 ) { 4442if (strcmp (p ,"$" )== 0 ) { 4443tab_number = LAST_TAB_NR ; 4444 }else if (strcmp (p ,"#" )== 0 ) { 4445if (valid_tabpage (lastused_tabpage )) { 4446tab_number = tabpage_index (lastused_tabpage ); 4447 }else { 4448eap -> errmsg = ex_errmsg (e_invargval ,eap -> arg ); 4449tab_number = 0 ; 4450 gototheend ; 4451 } 4452 }else if (p == p_save || * p_save == '-' || * p != NUL 4453|| tab_number > LAST_TAB_NR ) { 4454// No numbers as argument. 4455eap -> errmsg = ex_errmsg (e_invarg2 ,eap -> arg ); 4456 gototheend ; 4457 } 4458 }else { 4459if (* p_save == NUL ) { 4460tab_number = 1 ; 4461 }else if (p == p_save || * p_save == '-' || * p != NUL || tab_number == 0 ) { 4462// No numbers as argument. 4463eap -> errmsg = ex_errmsg (e_invarg2 ,eap -> arg ); 4464 gototheend ; 4465 } 4466tab_number = tab_number * relative + tabpage_index (curtab ); 4467if (!unaccept_arg0 && relative == -1 ) { 4468tab_number -- ; 4469 } 4470 } 4471if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR ) { 4472eap -> errmsg = ex_errmsg (e_invarg2 ,eap -> arg ); 4473 } 4474 }else if (eap -> addr_count > 0 ) { 4475if (unaccept_arg0 && eap -> line2 == 0 ) { 4476eap -> errmsg = _ (e_invrange ); 4477tab_number = 0 ; 4478 }else { 4479tab_number = (int )eap -> line2 ; 4480if (!unaccept_arg0 ) { 4481char * cmdp = eap -> cmd ; 4482while (-- cmdp > * eap -> cmdlinep 4483&& (ascii_iswhite (* cmdp )|| ascii_isdigit (* cmdp ))) {} 4484if (* cmdp == '-' ) { 4485tab_number -- ; 4486if (tab_number < unaccept_arg0 ) { 4487eap -> errmsg = _ (e_invrange ); 4488 } 4489 } 4490 } 4491 } 4492 }else { 4493switch (eap -> cmdidx ) { 4494case CMD_tabnext : 4495tab_number = tabpage_index (curtab )+ 1 ; 4496if (tab_number > LAST_TAB_NR ) { 4497tab_number = 1 ; 4498 } 4499break ; 4500case CMD_tabmove : 4501tab_number = LAST_TAB_NR ; 4502break ; 4503default : 4504tab_number = tabpage_index (curtab ); 4505 } 4506 } 4507 4508theend : 4509return tab_number ; 4510} 4511 4512static void ex_autocmd (exarg_T * eap ) 4513{ 4514// Disallow autocommands in secure mode. 4515if (secure ) { 4516secure = 2 ; 4517eap -> errmsg = _ (e_curdir ); 4518 }else if (eap -> cmdidx == CMD_autocmd ) { 4519do_autocmd (eap ,eap -> arg ,eap -> forceit ); 4520 }else { 4521do_augroup (eap -> arg ,eap -> forceit ); 4522 } 4523} 4524 4525/// ":doautocmd": Apply the automatic commands to the current buffer. 4526static void ex_doautocmd (exarg_T * eap ) 4527{ 4528char * arg = eap -> arg ; 4529int call_do_modelines = check_nomodeline (& arg ); 4530bool did_aucmd ; 4531 4532do_doautocmd (arg , false,& did_aucmd ); 4533// Only when there is no <nomodeline>. 4534if (call_do_modelines && did_aucmd ) { 4535do_modelines (0 ); 4536 } 4537} 4538 4539/// :[N]bunload[!] [N] [bufname] unload buffer 4540/// :[N]bdelete[!] [N] [bufname] delete buffer from buffer list 4541/// :[N]bwipeout[!] [N] [bufname] delete buffer really 4542static void ex_bunload (exarg_T * eap ) 4543{ 4544eap -> errmsg = do_bufdel (eap -> cmdidx == CMD_bdelete 4545 ?DOBUF_DEL 4546 :eap -> cmdidx == CMD_bwipeout 4547 ?DOBUF_WIPE 4548 :DOBUF_UNLOAD , 4549eap -> arg ,eap -> addr_count , (int )eap -> line1 , (int )eap -> line2 , 4550eap -> forceit ); 4551} 4552 4553/// :[N]buffer [N] to buffer N 4554/// :[N]sbuffer [N] to buffer N 4555static void ex_buffer (exarg_T * eap ) 4556{ 4557do_exbuffer (eap ); 4558} 4559 4560/// ":buffer" command and alike. 4561static void do_exbuffer (exarg_T * eap ) 4562{ 4563if (* eap -> arg ) { 4564eap -> errmsg = ex_errmsg (e_trailing_arg ,eap -> arg ); 4565 }else { 4566if (eap -> addr_count == 0 ) {// default is current buffer 4567goto_buffer (eap ,DOBUF_CURRENT ,FORWARD ,0 ); 4568 }else { 4569goto_buffer (eap ,DOBUF_FIRST ,FORWARD , (int )eap -> line2 ); 4570 } 4571if (eap -> do_ecmd_cmd != NULL ) { 4572do_cmdline_cmd (eap -> do_ecmd_cmd ); 4573 } 4574 } 4575} 4576 4577/// :[N]bmodified [N] to next mod. buffer 4578/// :[N]sbmodified [N] to next mod. buffer 4579static void ex_bmodified (exarg_T * eap ) 4580{ 4581goto_buffer (eap ,DOBUF_MOD ,FORWARD , (int )eap -> line2 ); 4582if (eap -> do_ecmd_cmd != NULL ) { 4583do_cmdline_cmd (eap -> do_ecmd_cmd ); 4584 } 4585} 4586 4587/// :[N]bnext [N] to next buffer 4588/// :[N]sbnext [N] split and to next buffer 4589static void ex_bnext (exarg_T * eap ) 4590{ 4591goto_buffer (eap ,DOBUF_CURRENT ,FORWARD , (int )eap -> line2 ); 4592if (eap -> do_ecmd_cmd != NULL ) { 4593do_cmdline_cmd (eap -> do_ecmd_cmd ); 4594 } 4595} 4596 4597/// :[N]bNext [N] to previous buffer 4598/// :[N]bprevious [N] to previous buffer 4599/// :[N]sbNext [N] split and to previous buffer 4600/// :[N]sbprevious [N] split and to previous buffer 4601static void ex_bprevious (exarg_T * eap ) 4602{ 4603goto_buffer (eap ,DOBUF_CURRENT ,BACKWARD , (int )eap -> line2 ); 4604if (eap -> do_ecmd_cmd != NULL ) { 4605do_cmdline_cmd (eap -> do_ecmd_cmd ); 4606 } 4607} 4608 4609/// :brewind to first buffer 4610/// :bfirst to first buffer 4611/// :sbrewind split and to first buffer 4612/// :sbfirst split and to first buffer 4613static void ex_brewind (exarg_T * eap ) 4614{ 4615goto_buffer (eap ,DOBUF_FIRST ,FORWARD ,0 ); 4616if (eap -> do_ecmd_cmd != NULL ) { 4617do_cmdline_cmd (eap -> do_ecmd_cmd ); 4618 } 4619} 4620 4621/// :blast to last buffer 4622/// :sblast split and to last buffer 4623static void ex_blast (exarg_T * eap ) 4624{ 4625goto_buffer (eap ,DOBUF_LAST ,BACKWARD ,0 ); 4626if (eap -> do_ecmd_cmd != NULL ) { 4627do_cmdline_cmd (eap -> do_ecmd_cmd ); 4628 } 4629} 4630 4631int ends_excmd (int c )FUNC_ATTR_CONST 4632{ 4633return c == NUL || c == '|' || c == '"' || c == '\n' ; 4634} 4635 4636/// @return the next command, after the first '|' or '\n' or, 4637/// NULL if not found. 4638char * find_nextcmd (const char * p ) 4639{ 4640while (* p != '|' && * p != '\n' ) { 4641if (* p == NUL ) { 4642return NULL ; 4643 } 4644p ++ ; 4645 } 4646return (char * )p + 1 ; 4647} 4648 4649/// Check if *p is a separator between Ex commands, skipping over white space. 4650/// 4651/// @return NULL if it isn't, the following character if it is. 4652char * check_nextcmd (char * p ) 4653{ 4654char * s = skipwhite (p ); 4655 4656if (* s == '|' || * s == '\n' ) { 4657return s + 1 ; 4658 } 4659return NULL ; 4660} 4661 4662/// - if there are more files to edit 4663/// - and this is the last window 4664/// - and forceit not used 4665/// - and not repeated twice on a row 4666/// 4667/// @param message when false check only, no messages 4668/// 4669/// @return FAIL and give error message if 'message' true, return OK otherwise 4670static int check_more (bool message ,bool forceit ) 4671{ 4672int n = ARGCOUNT - curwin -> w_arg_idx - 1 ; 4673 4674if (!forceit && only_one_window () 4675&& ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0 ) { 4676if (message ) { 4677if ((p_confirm || (cmdmod .cmod_flags & CMOD_CONFIRM ))&& curbuf -> b_fname != NULL ) { 4678char buff [DIALOG_MSG_SIZE ]; 4679 4680vim_snprintf (buff ,DIALOG_MSG_SIZE , 4681NGETTEXT ("%d more file to edit. Quit anyway?" , 4682"%d more files to edit. Quit anyway?" ,n ),n ); 4683if (vim_dialog_yesno (VIM_QUESTION ,NULL ,buff ,1 )== VIM_YES ) { 4684return OK ; 4685 } 4686return FAIL ; 4687 } 4688semsg (NGETTEXT ("E173: %d more file to edit" , 4689"E173: %d more files to edit" ,n ),n ); 4690quitmore = 2 ;// next try to quit is allowed 4691 } 4692return FAIL ; 4693 } 4694return OK ; 4695} 4696 4697/// Function given to ExpandGeneric() to obtain the list of command names. 4698char * get_command_name (expand_T * xp ,int idx ) 4699{ 4700if (idx >=CMD_SIZE ) { 4701return expand_user_command_name (idx ); 4702 } 4703return cmdnames [idx ].cmd_name ; 4704} 4705 4706static void ex_colorscheme (exarg_T * eap ) 4707{ 4708if (* eap -> arg == NUL ) { 4709char * expr = xstrdup ("g:colors_name" ); 4710 4711emsg_off ++ ; 4712char * p = eval_to_string (expr , false, false); 4713emsg_off -- ; 4714xfree (expr ); 4715 4716msg_ext_set_kind ("list_cmd" ); 4717if (p != NULL ) { 4718msg (p ,0 ); 4719xfree (p ); 4720 }else { 4721msg ("default" ,0 ); 4722 } 4723 }else if (load_colors (eap -> arg )== FAIL ) { 4724semsg (_ ("E185: Cannot find color scheme '%s'" ),eap -> arg ); 4725 } 4726} 4727 4728static void ex_highlight (exarg_T * eap ) 4729{ 4730if (* eap -> arg == NUL && eap -> cmd [2 ]== '!' ) { 4731msg (_ ("Greetings, Vim user!" ),0 ); 4732 } 4733do_highlight (eap -> arg ,eap -> forceit , false); 4734} 4735 4736/// Call this function if we thought we were going to exit, but we won't 4737/// (because of an error). May need to restore the terminal mode. 4738void not_exiting (bool save_exiting ) 4739{ 4740exiting = save_exiting ; 4741set_vim_var_string (VV_EXITREASON ,NULL ,-1 ); 4742} 4743 4744bool before_quit_autocmds (win_T * wp ,bool quit_all ,bool forceit ) 4745{ 4746// Set v:exitreason if not already set (e.g. by :restart). 4747if (* get_vim_var_str (VV_EXITREASON )== NUL ) { 4748set_vim_var_string (VV_EXITREASON ,S_LEN ("quit" )); 4749 } 4750apply_autocmds_win (EVENT_QUITPRE ,NULL ,NULL , false,wp -> w_buffer ,wp ); 4751 4752// Bail out when autocommands closed the window. 4753// Refuse to quit when the buffer in the last window is being closed (can 4754// only happen in autocommands). 4755if (!win_valid (wp ) 4756|| curbuf_locked () 4757|| (wp -> w_buffer -> b_nwindows == 1 && wp -> w_buffer -> b_locked > 0 )) { 4758set_vim_var_string (VV_EXITREASON ,NULL ,-1 ); 4759return true; 4760 } 4761 4762if (quit_all 4763|| (check_more (false,forceit )== OK && only_one_window ())) { 4764apply_autocmds (EVENT_EXITPRE ,NULL ,NULL , false,curbuf ); 4765// Refuse to quit when locked or when the window was closed or the 4766// buffer in the last window is being closed (can only happen in 4767// autocommands). 4768if (!win_valid (wp ) 4769|| curbuf_locked () 4770|| (curbuf -> b_nwindows == 1 && curbuf -> b_locked > 0 )) { 4771set_vim_var_string (VV_EXITREASON ,NULL ,-1 ); 4772return true; 4773 } 4774 } 4775 4776return false; 4777} 4778 4779/// ":quit": quit current window, quit Vim if the last window is closed. 4780/// ":{nr}quit": quit window {nr} 4781static void ex_quit (exarg_T * eap ) 4782{ 4783// Don't quit while editing the command line. 4784if (text_locked ()) { 4785text_locked_msg (); 4786return ; 4787 } 4788 4789win_T * wp ; 4790 4791if (eap -> addr_count > 0 ) { 4792linenr_T wnr = eap -> line2 ; 4793 4794for (wp = firstwin ;wp -> w_next != NULL ;wp = wp -> w_next ) { 4795if (-- wnr <=0 ) { 4796break ; 4797 } 4798 } 4799 }else { 4800wp = curwin ; 4801 } 4802 4803// Refuse to quit when locked. 4804if (curbuf_locked ()) { 4805return ; 4806 } 4807 4808// Trigger QuitPre and maybe ExitPre 4809if (before_quit_autocmds (wp , false,eap -> forceit )) { 4810return ; 4811 } 4812 4813bool save_exiting = exiting ; 4814// If there is only one relevant window we will exit. 4815if (check_more (false,eap -> forceit )== OK && only_one_window ()) { 4816exiting = true; 4817 } 4818if ((!buf_hide (wp -> w_buffer ) 4819&& check_changed (wp -> w_buffer , (p_awa ?CCGD_AW :0 ) 4820 | (eap -> forceit ?CCGD_FORCEIT :0 ) 4821 |CCGD_EXCMD )) 4822|| check_more (true,eap -> forceit )== FAIL 4823|| (only_one_window ()&& check_changed_any (eap -> forceit , true))) { 4824not_exiting (save_exiting ); 4825 }else { 4826// quit last window 4827// Note: only_one_window() returns true, even so a help window is 4828// still open. In that case only quit, if no address has been 4829// specified. Example: 4830// :h|wincmd w|1q - don't quit 4831// :h|wincmd w|q - quit 4832if (only_one_window ()&& (ONE_WINDOW || eap -> addr_count == 0 )) { 4833getout (0 ); 4834 } 4835not_exiting (save_exiting ); 4836// close window; may free buffer 4837win_close (wp , !buf_hide (wp -> w_buffer )|| eap -> forceit ,eap -> forceit ); 4838 } 4839} 4840 4841/// ":cquit". 4842static void ex_cquit (exarg_T * eap ) 4843FUNC_ATTR_NORETURN 4844{ 4845// this does not always pass on the exit code to the Manx compiler. why? 4846int status = eap -> addr_count > 0 ? (int )eap -> line2 :EXIT_FAILURE ; 4847ui_call_error_exit (status ); 4848getout (status ); 4849} 4850 4851/// Do preparations for "qall" and "wqall". 4852/// Returns FAIL when quitting should be aborted. 4853int before_quit_all (exarg_T * eap ) 4854{ 4855// Don't quit while editing the command line. 4856if (text_locked ()) { 4857text_locked_msg (); 4858return FAIL ; 4859 } 4860 4861if (before_quit_autocmds (curwin , true,eap -> forceit )) { 4862return FAIL ; 4863 } 4864 4865return OK ; 4866} 4867 4868/// ":qall": try to quit all windows 4869static void ex_quitall (exarg_T * eap ) 4870{ 4871if (before_quit_all (eap )== FAIL ) { 4872return ; 4873 } 4874bool save_exiting = exiting ; 4875exiting = true; 4876if (eap -> forceit || !check_changed_any (false, false)) { 4877getout (0 ); 4878 } 4879not_exiting (save_exiting ); 4880} 4881 4882/// ":exmode": Enter interactive Ex mode. 4883static void ex_exmode (exarg_T * eap ) 4884{ 4885if (silent_mode ) { 4886return ; 4887 } 4888if (ex_normal_busy > 0 || global_busy ) { 4889// Ex mode cannot run inside ":normal" or ":global"; discard the rest of the command. 4890flush_buffers (FLUSH_TYPEAHEAD ); 4891return ; 4892 } 4893typval_T args []= { { .v_type = VAR_UNKNOWN } }; 4894nlua_call_typval ("vim._core.exmode" ,"open" ,args ,NULL ); 4895} 4896 4897/// ":restart": restart the Nvim server (using ":qall!"). 4898/// ":restart +cmd": restart the Nvim server using ":cmd". 4899/// ":restart +cmd <command>": restart the Nvim server using ":cmd" and runs <command> in the new server. 4900static void ex_restart (exarg_T * eap ) 4901{ 4902if (!eap -> forceit ) { 4903dict_T * extra_d = tv_dict_alloc ();// Pass +cmd in the `extra` param of nlua_call_excmd. 4904tv_dict_add_str (extra_d ,S_LEN ("quit_cmd" ),eap -> do_ecmd_cmd ?eap -> do_ecmd_cmd :"" ); 4905typval_T extra_tv = { .v_type = VAR_DICT , .vval .v_dict = extra_d }; 4906nlua_call_excmd ("vim._core.server" ,"ex_session_restart" ,eap ,& cmdmod ,& extra_tv ); 4907tv_clear (& extra_tv ); 4908return ; 4909 } 4910 4911const char * startreason = "restart!" ; 4912char * quit_cmd = (eap -> do_ecmd_cmd ) ?eap -> do_ecmd_cmd :"qall" ; 4913char * after_cmd = eap -> arg ; 4914 4915// XXX: "+:::" is how ex_session_restart() signals that it (recursively) called :restart. 4916if (strequal (quit_cmd ,":::" )) { 4917startreason = "restart" ; 4918if (eap -> argc > 1 ) { 4919eap -> args [1 ][eap -> arglens [1 ]]= NUL ; 4920quit_cmd = eap -> args [1 ]; 4921after_cmd = eap -> argc > 2 ?eap -> args [2 ] :"" ; 4922 }else { 4923semsg (e_restart_failed_cmd_no_quit ,quit_cmd ); 4924return ; 4925 } 4926 } 4927 4928Error err = ERROR_INIT ; 4929const bool no_ui = !ui_active (); 4930const char * exepath = get_vim_var_str (VV_PROGPATH ); 4931const list_T * l = get_vim_var_list (VV_ARGV ); 4932int argc = tv_list_len (l ); 4933 4934char ** argv = xcalloc ((size_t )argc + 3 ,sizeof (char * )); 4935size_t i = 0 ; 4936const char * listen_arg = NULL ;// --listen arg given by user, if any. 4937 4938// Build args to start the new Nvim, based on the current v:argv. 4939for (const listitem_T * li = l -> lv_first ;li != NULL ;li = li -> li_next ) { 4940const char * arg = tv_get_string (TV_LIST_ITEM_TV (li )); 4941// Drop "-- [files…]". Usually isn't wanted. User can :mksession instead. 4942if (i > 0 && strequal (arg ,"--" )) { 4943break ; 4944 } 4945// Drop "-S [file]". It conflicts with :restart and usually isn't wanted for :restart! 4946if (i > 0 && strequal (arg ,"-S" )) { 4947if (li -> li_next != NULL ) { 4948const char * next_arg = tv_get_string (TV_LIST_ITEM_TV (li -> li_next )); 4949if (next_arg [0 ]!= '-' ) { 4950li = li -> li_next ; 4951 } 4952 } 4953continue ; 4954 } 4955// Drop "-s <scriptfile>": skip the scriptfile arg too. 4956if (i > 0 && strequal (arg ,"-s" )) { 4957li = li -> li_next ; 4958continue ; 4959 } 4960// The address after --listen may be in use by the current server. 4961if (i > 0 && strequal (arg ,"--listen" )) { 4962const listitem_T * next_li = li -> li_next ; 4963if (next_li != NULL ) { 4964const char * addr = tv_get_string (TV_LIST_ITEM_TV (next_li )); 4965if (strstr (addr ,":" )|| strstr (addr ,"/" )|| strstr (addr ,"\\" )) { 4966XFREE_CLEAR (listen_arg ); 4967listen_arg = TO_SLASH_SAVE (addr ); 4968#ifdef MSWIN 4969// On Windows, don't pass --listen to new server (named pipe can't be reused immediately). 4970// Instead pass the address via RPC; new server rebinds after startup. 4971li = next_li ; 4972continue ; 4973#endif 4974 } 4975 } 4976 } 4977// Replace `--embed` OR `--headless` with `--embed` or `--embed --headless` once. 4978// Drop stdin ("-") argument. 4979if (i == 0 4980|| (!strequal (arg ,"--embed" )&& !strequal (arg ,"--headless" )&& !strequal (arg ,"-" ))) { 4981argv [i ++ ]= xstrdup (arg ); 4982if (i == 1 ) { 4983argv [i ++ ]= xstrdup ("--embed" ); 4984// Without --headless, embed waits for UI to attach. 4985// Only add --headless when there is no UI. 4986if (no_ui ) { 4987argv [i ++ ]= xstrdup ("--headless" ); 4988 } 4989 } 4990 } 4991 } 4992 4993#ifdef MSWIN 4994// On Windows, --listen is omitted from child argv because the named pipe can't be reused immediately. 4995// Recover the canonical address from the Lua module state (set by the previous rebind_after_restart() call), 4996// and keep the current listener alive (new server reclaims it). 4997if (listen_arg == NULL ) { 4998Error lua_err = ERROR_INIT ; 4999Object rv = NLUA_EXEC_STATIC ("return require('vim._core.server').restart_canonical_addr" , 5000 (Array )ARRAY_DICT_INIT ,kRetObject ,NULL ,& lua_err ); 5001if (!ERROR_SET (& lua_err )&& rv .type == kObjectTypeString && rv .data .string .size > 0 ) { 5002listen_arg = xstrdup (rv .data .string .data ); 5003 } 5004api_free_object (rv ); 5005api_clear_error (& lua_err ); 5006 } 5007bool server_stopped = false; 5008#else 5009// Stop listening on the --listen address so that the new server can listen. 5010bool server_stopped = listen_arg ?server_stop (listen_arg , true) : false; 5011#endif 5012 5013dict_T * env = create_environment (NULL , false, false, false,NULL ); 5014tv_dict_add_str (env ,S_LEN (ENV_STARTREASON ),startreason ); 5015 5016CallbackReader on_err = CALLBACK_READER_INIT ; 5017#ifdef MSWIN 5018// On Windows, don't forward stderr as it won't work after the current server exits. 5019on_err .fwd_err = false; 5020#else 5021// On Unix, stderr fd is inherited, so it works even after the current server exits. 5022on_err .fwd_err = true; 5023#endif 5024bool detach = true; 5025varnumber_T exit_status ; 5026 5027Channel * channel = channel_job_start (argv ,exepath , 5028CALLBACK_READER_INIT ,on_err ,CALLBACK_NONE , 5029 false, true, true,detach ,kChannelStdinPipe , 5030globaldir ,0 ,0 ,env ,& exit_status ); 5031if (!channel ) { 5032emsg ("cannot create a channel job" ); 5033 gotofail_1 ; 5034 } 5035 5036// Prevent new server from self-exiting when the channel closes. 5037ArenaMem result_mem = NULL ; 5038MAXSIZE_TEMP_ARRAY (detach_args ,1 ); 5039ADD_C (detach_args ,BOOLEAN_OBJ (true)); 5040rpc_send_call (channel -> id ,"nvim__chan_set_detach" ,detach_args ,& result_mem ,& err ); 5041if (ERROR_SET (& err )) { 5042 gotofail_2 ; 5043 } 5044arena_mem_free (result_mem ); 5045result_mem = NULL ; 5046 5047if (* after_cmd != NUL ) { 5048// Execute [command] on new server on UIEnter. 5049MAXSIZE_TEMP_DICT (autocmd_opts ,3 ); 5050PUT_C (autocmd_opts ,"once" ,BOOLEAN_OBJ (true)); 5051PUT_C (autocmd_opts ,"nested" ,BOOLEAN_OBJ (true)); 5052PUT_C (autocmd_opts ,"command" ,CSTR_AS_OBJ (after_cmd )); 5053MAXSIZE_TEMP_ARRAY (autocmd_args ,2 ); 5054ADD_C (autocmd_args ,CSTR_AS_OBJ ("UIEnter" )); 5055ADD_C (autocmd_args ,DICT_OBJ (autocmd_opts )); 5056rpc_send_call (channel -> id ,"nvim_create_autocmd" ,autocmd_args ,& result_mem ,& err ); 5057if (ERROR_SET (& err )) { 5058 gotofail_2 ; 5059 } 5060arena_mem_free (result_mem ); 5061result_mem = NULL ; 5062 } 5063 5064// Get the new server's initial address. On Windows this is the temporary self-generated address. 5065MAXSIZE_TEMP_ARRAY (servername_args ,1 ); 5066ADD_C (servername_args ,CSTR_AS_OBJ ("servername" )); 5067Object result = rpc_send_call (channel -> id ,"nvim_get_vvar" ,servername_args ,& result_mem ,& err ); 5068if (ERROR_SET (& err )) { 5069 gotofail_2 ; 5070 } 5071if (result .type != kObjectTypeString || result .data .string .size == 0 ) { 5072emsg ("restart failed: could not get listen address from new server" ); 5073 gotofail_2 ; 5074 } 5075// New server's self-generated address. 5076char * listen_addr = xmemdupz (result .data .string .data ,result .data .string .size ); 5077arena_mem_free (result_mem ); 5078result_mem = NULL ; 5079 5080#ifdef MSWIN 5081if (listen_arg != NULL ) { 5082// Tell the new server to reclaim the canonical --listen address once the old listener exits, 5083// then retire the bootstrap address after all UIs have reattached (or timeout). 5084MAXSIZE_TEMP_ARRAY (lua_args ,2 ); 5085ADD_C (lua_args , 5086CSTR_AS_OBJ ("return require('vim._core.server').rebind_after_restart(...)" )); 5087MAXSIZE_TEMP_ARRAY (handoff_params ,3 ); 5088ADD_C (handoff_params ,CSTR_AS_OBJ (listen_arg )); 5089ADD_C (handoff_params ,INTEGER_OBJ ((Integer )ui_active ())); 5090ADD_C (lua_args ,ARRAY_OBJ (handoff_params )); 5091rpc_send_call (channel -> id ,"nvim_exec_lua" ,lua_args ,& result_mem ,& err ); 5092if (ERROR_SET (& err )) { 5093 gotofail_2 ; 5094 } 5095arena_mem_free (result_mem ); 5096result_mem = NULL ; 5097 } 5098#endif 5099 5100// Send restart event with new listen address to all UIs. 5101ui_call_restart (cstr_as_string (listen_addr )); 5102ui_flush (); 5103xfree (listen_addr ); 5104 5105set_vim_var_string (VV_EXITREASON ,startreason ,-1 ); 5106 5107char * quit_cmd_copy = NULL ; 5108 5109// Prepend "confirm " to cmd if :confirm is used 5110if (cmdmod .cmod_flags & CMOD_CONFIRM ) { 5111quit_cmd_copy = concat_str ("confirm " ,quit_cmd ); 5112quit_cmd = quit_cmd_copy ; 5113 } 5114// Try to quit. 5115nvim_command (cstr_as_string (quit_cmd ),& err ); 5116 5117if (ERROR_SET (& err )) { 5118emsg (err .msg );// Could not exit 5119api_clear_error (& err ); 5120 }else if (!exiting ) { 5121semsg (e_restart_failed_cmd_no_quit ,quit_cmd ); 5122 } 5123xfree (quit_cmd_copy ); 5124 5125fail_2 : 5126set_vim_var_string (VV_EXITREASON ,NULL ,-1 ); 5127if (ERROR_SET (& err )) { 5128emsg (err .msg ); 5129api_clear_error (& err ); 5130 } 5131arena_mem_free (result_mem ); 5132result_mem = NULL ; 5133 5134#ifndef MSWIN 5135// Before killing the new server, close its stderr to avoid polluting the current UI. 5136MAXSIZE_TEMP_ARRAY (chanclose_expr_args ,1 ); 5137ADD_C (chanclose_expr_args ,CSTR_AS_OBJ ("chanclose(v:stderr)" )); 5138rpc_send_call (channel -> id ,"nvim_eval" ,chanclose_expr_args ,& result_mem ,& err ); 5139api_clear_error (& err ); 5140arena_mem_free (result_mem ); 5141#endif 5142 5143// Kill the new Nvim server. 5144proc_stop (& channel -> stream .proc ); 5145if (proc_wait (& channel -> stream .proc ,-1 ,NULL )< 0 ) { 5146emsg ("killing new nvim server failed" ); 5147 } 5148 5149fail_1 : 5150// Restart listening on the --listen address. 5151if (server_stopped && server_start (listen_arg )!= 0 ) { 5152semsg ("couldn't resume listening on %s" ,listen_arg ); 5153 } 5154XFREE_CLEAR (listen_arg ); 5155} 5156 5157/// ":close": close current window, unless it is the last one 5158static void ex_close (exarg_T * eap ) 5159{ 5160if (!text_locked ()&& !curbuf_locked ()) { 5161win_T * win ; 5162if (eap -> addr_count == 0 ) { 5163win = curwin ; 5164 }else { 5165win = win_find_nr ((int )eap -> line2 ,curtab ); 5166if (win == NULL ) { 5167win = lastwin ; 5168 } 5169 } 5170ex_win_close (eap -> forceit ,win ,NULL ); 5171 } 5172} 5173 5174/// ":pclose": Close any preview window. 5175static void ex_pclose (exarg_T * eap ) 5176{ 5177FOR_ALL_WINDOWS_IN_TAB (win ,curtab ) { 5178if (win -> w_p_pvw ) { 5179ex_win_close (eap -> forceit ,win ,NULL ); 5180break ; 5181 } 5182 } 5183 5184win_float_close (kWinPreview ); 5185} 5186 5187/// Close window "win" and take care of handling closing the last window for a 5188/// modified buffer. 5189/// 5190/// @param tp NULL or the tab page "win" is in, if different from "curtab" 5191void ex_win_close (int forceit ,win_T * win ,tabpage_T * tp ) 5192{ 5193assert (tp == NULL || tp != curtab ); 5194 5195// Never close the autocommand window. 5196if (is_ctx_win (win )) { 5197emsg (_ (e_autocmd_close )); 5198return ; 5199 } 5200if (!win -> w_floating && window_layout_locked (CMD_close )) { 5201return ; 5202 } 5203 5204buf_T * buf = win -> w_buffer ; 5205 5206bool need_hide = (bufIsChanged (buf )&& buf -> b_nwindows <=1 ); 5207if (need_hide && !buf_hide (buf )&& !forceit ) { 5208if ((p_confirm || (cmdmod .cmod_flags & CMOD_CONFIRM ))&& p_write ) { 5209bufref_T bufref ; 5210set_bufref (& bufref ,buf ); 5211dialog_changed (buf , false); 5212if (bufref_valid (& bufref )&& bufIsChanged (buf )) { 5213return ; 5214 } 5215need_hide = false; 5216 }else { 5217no_write_message (); 5218return ; 5219 } 5220 } 5221 5222// free buffer when not hiding it or when it's a scratch buffer 5223if (tp == NULL ) { 5224win_close (win , !need_hide && !buf_hide (buf ),forceit ); 5225 }else { 5226win_close_othertab (win , !need_hide && !buf_hide (buf ),tp ,forceit ); 5227 } 5228} 5229 5230/// ":tabclose": close current tab page, unless it is the last one. 5231/// ":tabclose N": close tab page N. 5232static void ex_tabclose (exarg_T * eap ) 5233{ 5234if (first_tabpage -> tp_next == NULL ) { 5235emsg (_ ("E784: Cannot close last tab page" )); 5236return ; 5237 } 5238 5239if (window_layout_locked (CMD_tabclose )) { 5240return ; 5241 } 5242 5243int tab_number = get_tabpage_arg (eap ); 5244if (eap -> errmsg != NULL ) { 5245return ; 5246 } 5247 5248tabpage_T * tp = find_tabpage (tab_number ); 5249if (tp == NULL ) { 5250beep_flush (); 5251return ; 5252 } 5253if (tp != curtab ) { 5254tabpage_close_other (tp ,eap -> forceit ); 5255return ; 5256 }else if (!text_locked ()&& !curbuf_locked ()) { 5257tabpage_close (eap -> forceit ); 5258 } 5259} 5260 5261/// ":tabonly": close all tab pages except the current one 5262static void ex_tabonly (exarg_T * eap ) 5263{ 5264if (first_tabpage -> tp_next == NULL ) { 5265msg (_ ("Already only one tab page" ),0 ); 5266return ; 5267 } 5268 5269if (window_layout_locked (CMD_tabonly )) { 5270return ; 5271 } 5272 5273int tab_number = get_tabpage_arg (eap ); 5274if (eap -> errmsg != NULL ) { 5275return ; 5276 } 5277 5278goto_tabpage (tab_number ); 5279// Repeat this up to a 1000 times, because autocommands may 5280// mess up the lists. 5281for (int done = 0 ;done < 1000 ;done ++ ) { 5282FOR_ALL_TABS (tp ) { 5283if (tp -> tp_topframe != topframe ) { 5284tabpage_close_other (tp ,eap -> forceit ); 5285// if we failed to close it quit 5286if (valid_tabpage (tp )) { 5287done = 1000 ; 5288 } 5289// start over, "tp" is now invalid 5290break ; 5291 } 5292 } 5293assert (first_tabpage ); 5294if (first_tabpage -> tp_next == NULL ) { 5295break ; 5296 } 5297 } 5298} 5299 5300/// Close the current tab page. 5301void tabpage_close (int forceit ) 5302{ 5303int done = 0 ; 5304 5305if (window_layout_locked (CMD_tabclose )) { 5306return ; 5307 } 5308 5309trigger_tabclosedpre (curtab ); 5310curtab -> tp_did_tabclosedpre = true; 5311tabpage_T * const save_curtab = curtab ; 5312 5313// First close all the windows but the current one. If that worked then 5314// close the last window in this tab, that will close it. 5315while (curwin -> w_floating && ++ done < 1000 ) { 5316ex_win_close (forceit ,curwin ,NULL ); 5317 } 5318if (done == 1000 ) { 5319return ; 5320 } 5321 5322if (!ONE_WINDOW ) { 5323close_others (true,forceit , true); 5324 } 5325if (ONE_WINDOW ) { 5326ex_win_close (forceit ,curwin ,NULL ); 5327 } 5328if (curtab == save_curtab ) { 5329// When closing the tab page failed, reset tp_did_tabclosedpre so that 5330// TabClosedPre behaves consistently on next :close vs :tabclose. 5331curtab -> tp_did_tabclosedpre = false; 5332 } 5333} 5334 5335/// Close tab page "tp", which is not the current tab page. 5336/// Note that autocommands may make "tp" invalid. 5337/// Also takes care of the tab pages line disappearing when closing the 5338/// last-but-one tab page. 5339void tabpage_close_other (tabpage_T * tp ,int forceit ) 5340{ 5341int done = 0 ; 5342char prev_idx [NUMBUFLEN ]; 5343 5344assert (tp != curtab ); 5345 5346if (window_layout_locked (CMD_SIZE )) { 5347return ; 5348 } 5349 5350trigger_tabclosedpre (tp ); 5351tp -> tp_did_tabclosedpre = true; 5352 5353// Limit to 1000 windows, autocommands may add a window while we close 5354// one. OK, so I'm paranoid... 5355while (++ done < 1000 ) { 5356snprintf (prev_idx ,sizeof (prev_idx ),"%i" ,tabpage_index (tp )); 5357win_T * wp = tp -> tp_lastwin ; 5358 5359// Autocommands (including TabClosedPre above) may change the current tab page, 5360// abort a `:tabonly` (etc) if we're now on the tab we're trying to close 5361if (tp == curtab ) { 5362done = 1000 ; 5363break ; 5364 } 5365ex_win_close (forceit ,wp ,tp ); 5366 5367// Autocommands may delete the tab page under our fingers. 5368if (!valid_tabpage (tp )) { 5369break ; 5370 } 5371 5372// We may fail to close a window with a modified buffer. 5373if (tp -> tp_lastwin == wp ) { 5374done = 1000 ; 5375break ; 5376 } 5377 } 5378if (done >=1000 ) { 5379// When closing the tab page failed, reset tp_did_tabclosedpre so that 5380// TabClosedPre behaves consistently on next :close vs :tabclose. 5381tp -> tp_did_tabclosedpre = false; 5382return ; 5383 } 5384} 5385 5386/// ":only". 5387static void ex_only (exarg_T * eap ) 5388{ 5389if (window_layout_locked (CMD_only )) { 5390return ; 5391 } 5392 5393if (eap -> addr_count > 0 ) { 5394win_T * wp ; 5395linenr_T wnr = eap -> line2 ; 5396for (wp = firstwin ;-- wnr > 0 ;) { 5397if (wp -> w_next == NULL ) { 5398break ; 5399 } 5400wp = wp -> w_next ; 5401 } 5402if (wp != curwin ) { 5403win_goto (wp ); 5404 } 5405 } 5406close_others (true,eap -> forceit , false); 5407} 5408 5409static void ex_hide (exarg_T * eap ) 5410{ 5411// ":hide" or ":hide | cmd": hide current window 5412if (eap -> skip ) { 5413return ; 5414 } 5415 5416win_T * win ; 5417if (eap -> addr_count == 0 ) { 5418win = curwin ; 5419 }else { 5420win = win_find_nr ((int )eap -> line2 ,curtab ); 5421if (win == NULL ) { 5422win = lastwin ; 5423 } 5424 } 5425 5426if (!win -> w_floating && window_layout_locked (CMD_hide )) { 5427return ; 5428 } 5429 5430win_close (win , false,eap -> forceit );// don't free buffer 5431} 5432 5433/// ":stop" and ":suspend": Suspend Vim. 5434static void ex_stop (exarg_T * eap ) 5435{ 5436if (!eap -> forceit ) { 5437autowrite_all (); 5438 } 5439may_trigger_vim_suspend_resume (true); 5440ui_call_suspend (); 5441ui_flush (); 5442} 5443 5444/// ":exit", ":xit" and ":wq": Write file and quit the current window. 5445static void ex_exit (exarg_T * eap ) 5446{ 5447// Don't quit while editing the command line. 5448if (text_locked ()) { 5449text_locked_msg (); 5450return ; 5451 } 5452 5453bool save_exiting = exiting ; 5454// we plan to exit if there is only one relevant window 5455if (check_more (false,eap -> forceit )== OK && only_one_window ()) { 5456exiting = true; 5457 } 5458// Write the buffer for ":wq" or when it was changed. 5459// Trigger QuitPre and ExitPre. 5460// Check if we can exit now, after autocommands have changed things. 5461if (((eap -> cmdidx == CMD_wq || curbufIsChanged ())&& do_write (eap )== FAIL ) 5462|| before_quit_autocmds (curwin , false,eap -> forceit ) 5463|| check_more (true,eap -> forceit )== FAIL 5464|| (only_one_window ()&& check_changed_any (eap -> forceit , false))) { 5465not_exiting (save_exiting ); 5466 }else { 5467if (only_one_window ()) { 5468// quit last window, exit Vim 5469getout (0 ); 5470 } 5471not_exiting (save_exiting ); 5472// Quit current window, may free the buffer. 5473win_close (curwin , !buf_hide (curwin -> w_buffer ),eap -> forceit ); 5474 } 5475} 5476 5477/// ":print", ":list", ":number". 5478static void ex_print (exarg_T * eap ) 5479{ 5480if (curbuf -> b_ml .ml_flags & ML_EMPTY ) { 5481emsg (_ (e_empty_buffer )); 5482 }else { 5483for (linenr_T line = eap -> line1 ;line <=eap -> line2 && !got_int ;os_breakcheck ()) { 5484print_line (line , 5485 (eap -> cmdidx == CMD_number || eap -> cmdidx == CMD_pound 5486|| (eap -> flags & EXFLAG_NR )), 5487eap -> cmdidx == CMD_list || (eap -> flags & EXFLAG_LIST ), 5488line == eap -> line1 ); 5489line ++ ; 5490 } 5491setpcmark (); 5492// put cursor at last line 5493curwin -> w_cursor .lnum = eap -> line2 ; 5494beginline (BL_SOL |BL_FIX ); 5495 } 5496} 5497 5498static void ex_goto (exarg_T * eap ) 5499{ 5500goto_byte (eap -> line2 ); 5501} 5502 5503/// ":preserve". 5504static void ex_preserve (exarg_T * eap ) 5505{ 5506ml_preserve (curbuf , true, true); 5507} 5508 5509/// ":recover". 5510static void ex_recover (exarg_T * eap ) 5511{ 5512// Set recoverymode right away to avoid the ATTENTION prompt. 5513recoverymode = true; 5514if (!check_changed (curbuf , (p_awa ?CCGD_AW :0 ) 5515 |CCGD_MULTWIN 5516 | (eap -> forceit ?CCGD_FORCEIT :0 ) 5517 |CCGD_EXCMD ) 5518 5519&& (* eap -> arg == NUL 5520|| setfname (curbuf ,eap -> arg ,NULL , true)== OK )) { 5521ml_recover (true); 5522 } 5523recoverymode = false; 5524} 5525 5526/// Command modifier used in a wrong way. 5527static void ex_wrongmodifier (exarg_T * eap ) 5528{ 5529eap -> errmsg = _ (e_invcmd ); 5530} 5531 5532/// Call 'findfunc' to obtain a list of file names. 5533static list_T * call_findfunc (char * pat ,BoolVarValue cmdcomplete ) 5534{ 5535const sctx_T saved_sctx = current_sctx ; 5536 5537typval_T args [3 ]; 5538args [0 ].v_type = VAR_STRING ; 5539args [0 ].vval .v_string = pat ; 5540args [1 ].v_type = VAR_BOOL ; 5541args [1 ].vval .v_bool = cmdcomplete ; 5542args [2 ].v_type = VAR_UNKNOWN ; 5543 5544// Lock the text to prevent weird things from happening. Also disallow 5545// switching to another window, it should not be needed and may end up in 5546// Insert mode in another buffer. 5547textlock ++ ; 5548 5549sctx_T * ctx = get_option_sctx (kOptFindfunc ); 5550if (ctx != NULL ) { 5551current_sctx = * ctx ; 5552 } 5553 5554Callback * cb = get_findfunc (); 5555typval_T rettv ; 5556int retval = callback_call (cb ,2 ,args ,& rettv ); 5557 5558current_sctx = saved_sctx ; 5559 5560textlock -- ; 5561 5562list_T * retlist = NULL ; 5563 5564if (retval == OK ) { 5565if (rettv .v_type == VAR_LIST ) { 5566retlist = tv_list_copy (NULL ,rettv .vval .v_list , false,get_copyID ()); 5567 }else { 5568emsg (_ (e_invalid_return_type_from_findfunc )); 5569 } 5570 5571tv_clear (& rettv ); 5572 } 5573 5574return retlist ; 5575} 5576 5577/// Find file names matching "pat" using 'findfunc' and return it in "files". 5578/// Used for expanding the :find, :sfind and :tabfind command argument. 5579/// Returns OK on success and FAIL otherwise. 5580int expand_findfunc (expand_T * xp ,char * pat ,char *** files ,int * numMatches ) 5581{ 5582* numMatches = 0 ; 5583* files = NULL ; 5584 5585list_T * l = call_findfunc (pat ,kBoolVarTrue ); 5586if (l == NULL ) { 5587return FAIL ; 5588 } 5589 5590int len = tv_list_len (l ); 5591if (len == 0 ) {// empty List 5592tv_list_free (l ); 5593return FAIL ; 5594 } 5595 5596expand_process_user_list (l ,files ,numMatches ,xp ); 5597tv_list_free (l ); 5598 5599return OK ; 5600} 5601 5602/// Use 'findfunc' to find file 'findarg'. The 'count' argument is used to find 5603/// the n'th matching file. 5604static char * findfunc_find_file (char * findarg ,size_t findarg_len ,int count ) 5605{ 5606char * ret_fname = NULL ; 5607 5608const char cc = findarg [findarg_len ]; 5609findarg [findarg_len ]= NUL ; 5610 5611list_T * fname_list = call_findfunc (findarg ,kBoolVarFalse ); 5612int fname_count = tv_list_len (fname_list ); 5613 5614if (fname_count == 0 ) { 5615semsg (_ (e_cant_find_file_str_in_path ),findarg ); 5616 }else { 5617if (count > fname_count ) { 5618semsg (_ (e_no_more_file_str_found_in_path ),findarg ); 5619 }else { 5620const listitem_T * li = tv_list_find (fname_list ,count - 1 ); 5621if (li != NULL ) { 5622const typval_T * tv = TV_LIST_ITEM_TV (li ); 5623if (tv -> v_type == VAR_STRING && tv -> vval .v_string != NULL ) { 5624ret_fname = xstrdup (tv -> vval .v_string ); 5625 }else if (tv -> v_type == VAR_DICT && tv -> vval .v_dict != NULL ) { 5626ret_fname = tv_dict_get_string (tv -> vval .v_dict ,"word" , true); 5627 } 5628 } 5629 } 5630 } 5631 5632if (fname_list != NULL ) { 5633tv_list_free (fname_list ); 5634 } 5635 5636findarg [findarg_len ]= cc ; 5637 5638TO_SLASH (ret_fname ); 5639return ret_fname ; 5640} 5641 5642void free_findfunc_option (void ) 5643{ 5644callback_free (& p_ffu ); 5645} 5646 5647/// Mark the global 'findfunc' callback with "copyID" so that it is not 5648/// garbage collected. 5649bool set_ref_in_findfunc (int copyID ) 5650{ 5651return set_ref_in_callback (& p_ffu ,copyID ,NULL ,NULL ); 5652} 5653 5654static void set_browse_edit_arg (exarg_T * eap ) 5655{ 5656if ((cmdmod .cmod_flags & CMOD_BROWSE )&& * eap -> arg == NUL 5657&& (eap -> cmdidx == CMD_edit 5658|| eap -> cmdidx == CMD_split 5659|| eap -> cmdidx == CMD_vsplit 5660|| eap -> cmdidx == CMD_tabedit 5661|| eap -> cmdidx == CMD_tabnew )) { 5662eap -> arg = "." ; 5663 } 5664} 5665 5666/// :sview [+command] file split window with new file, read-only 5667/// :split [[+command] file] split window with current or new file 5668/// :vsplit [[+command] file] split window vertically with current or new file 5669/// :new [[+command] file] split window with no or new file 5670/// :vnew [[+command] file] split vertically window with no or new file 5671/// :sfind [+command] file split window with file in 'path' 5672/// 5673/// :tabedit open new Tab page with empty window 5674/// :tabedit [+command] file open new Tab page and edit "file" 5675/// :tabnew [[+command] file] just like :tabedit 5676/// :tabfind [+command] file open new Tab page and find "file" 5677void ex_splitview (exarg_T * eap ) 5678{ 5679set_browse_edit_arg (eap ); 5680win_T * old_curwin = curwin ; 5681char * fname = NULL ; 5682const bool use_tab = eap -> cmdidx == CMD_tabedit 5683|| eap -> cmdidx == CMD_tabfind 5684|| eap -> cmdidx == CMD_tabnew ; 5685 5686// A ":split" in the quickfix window works like ":new". Don't want two 5687// quickfix windows. But it's OK when doing ":tab split". 5688if (bt_quickfix (curbuf )&& cmdmod .cmod_tab == 0 ) { 5689if (eap -> cmdidx == CMD_split ) { 5690eap -> cmdidx = CMD_new ; 5691 } 5692if (eap -> cmdidx == CMD_vsplit ) { 5693eap -> cmdidx = CMD_vnew ; 5694 } 5695 } 5696 5697if (eap -> cmdidx == CMD_sfind || eap -> cmdidx == CMD_tabfind ) { 5698if (get_findfunc ()-> type != kCallbackNone ) { 5699fname = findfunc_find_file (eap -> arg ,strlen (eap -> arg ), 5700eap -> addr_count > 0 ?eap -> line2 :1 ); 5701 }else { 5702char * file_to_find = NULL ; 5703char * search_ctx = NULL ; 5704fname = find_file_in_path (eap -> arg ,strlen (eap -> arg ),FNAME_MESS , true, 5705curbuf -> b_ffname ,& file_to_find ,& search_ctx ); 5706xfree (file_to_find ); 5707vim_findfile_cleanup (search_ctx ); 5708 } 5709if (fname == NULL ) { 5710 gototheend ; 5711 } 5712eap -> arg = fname ; 5713 } 5714 5715// Either open new tab page or split the window. 5716if (use_tab ) { 5717if (win_new_tabpage (cmdmod .cmod_tab != 0 ?cmdmod .cmod_tab :eap -> addr_count == 0 5718 ?0 : (int )eap -> line2 + 1 ,eap -> arg , true,NULL )) { 5719do_exedit (eap ,old_curwin ); 5720apply_autocmds (EVENT_TABNEWENTERED ,NULL ,NULL , false,curbuf ); 5721 5722// set the alternate buffer for the window we came from 5723if (curwin != old_curwin 5724&& win_valid (old_curwin ) 5725&& old_curwin -> w_buffer != curbuf 5726&& (cmdmod .cmod_flags & CMOD_KEEPALT )== 0 ) { 5727old_curwin -> w_alt_fnum = curbuf -> b_fnum ; 5728 } 5729 } 5730 }else if (win_split (eap -> addr_count > 0 ? (int )eap -> line2 :0 , 5731* eap -> cmd == 'v' ?WSP_VERT :0 )!= FAIL ) { 5732// Reset 'scrollbind' when editing another file, but keep it when 5733// doing ":split" without arguments. 5734if (* eap -> arg != NUL ) { 5735RESET_BINDING (curwin ); 5736 }else { 5737do_check_scrollbind (false); 5738 } 5739do_exedit (eap ,old_curwin ); 5740 } 5741 5742theend : 5743xfree (fname ); 5744} 5745 5746/// Open a new tab page. 5747void tabpage_new (void ) 5748{ 5749exarg_T ea = { 5750 .cmdidx = CMD_tabnew , 5751 .cmd = "tabn" , 5752 .arg = "" , 5753 }; 5754ex_splitview (& ea ); 5755} 5756 5757/// :tabnext command 5758static void ex_tabnext (exarg_T * eap ) 5759{ 5760int tab_number ; 5761 5762switch (eap -> cmdidx ) { 5763case CMD_tabfirst : 5764case CMD_tabrewind : 5765goto_tabpage (1 ); 5766break ; 5767case CMD_tablast : 5768goto_tabpage (9999 ); 5769break ; 5770case CMD_tabprevious : 5771case CMD_tabNext : 5772if (eap -> arg && * eap -> arg != NUL ) { 5773char * p = eap -> arg ; 5774char * p_save = p ; 5775tab_number = (int )getdigits (& p , false,0 ); 5776if (p == p_save || * p_save == '-' || * p_save == '+' || * p != NUL 5777|| tab_number == 0 ) { 5778// No numbers as argument. 5779eap -> errmsg = ex_errmsg (e_invarg2 ,eap -> arg ); 5780return ; 5781 } 5782 }else { 5783if (eap -> addr_count == 0 ) { 5784tab_number = 1 ; 5785 }else { 5786tab_number = (int )eap -> line2 ; 5787if (tab_number < 1 ) { 5788eap -> errmsg = _ (e_invrange ); 5789return ; 5790 } 5791 } 5792 } 5793goto_tabpage (- tab_number ); 5794break ; 5795default :// CMD_tabnext 5796tab_number = get_tabpage_arg (eap ); 5797if (eap -> errmsg == NULL ) { 5798goto_tabpage (tab_number ); 5799 } 5800break ; 5801 } 5802} 5803 5804/// :tabmove command 5805static void ex_tabmove (exarg_T * eap ) 5806{ 5807int tab_number = get_tabpage_arg (eap ); 5808if (eap -> errmsg == NULL ) { 5809tabpage_move (tab_number ); 5810 } 5811} 5812 5813/// :tabs command: List tabs and their contents. 5814static void ex_tabs (exarg_T * eap ) 5815{ 5816int tabcount = 1 ; 5817 5818msg_ext_set_kind ("list_cmd" ); 5819msg_start (); 5820msg_scroll = true; 5821 5822win_T * lastused_win = valid_tabpage (lastused_tabpage ) 5823 ?lastused_tabpage -> tp_curwin 5824 :NULL ; 5825 5826FOR_ALL_TABS (tp ) { 5827if (got_int ) { 5828break ; 5829 } 5830 5831if (msg_col > 0 ) { 5832msg_putchar ('\n' ); 5833 } 5834vim_snprintf (IObuff ,IOSIZE ,_ ("Tab page %d" ),tabcount ++ ); 5835msg_outtrans (IObuff ,HLF_T , false); 5836os_breakcheck (); 5837 5838FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 5839if (got_int ) { 5840break ; 5841 }else if (!wp -> w_config .focusable || wp -> w_config .hide ) { 5842continue ; 5843 } 5844 5845msg_putchar ('\n' ); 5846msg_putchar (wp == curwin ?'>' :wp == lastused_win ?'#' :' ' ); 5847msg_putchar (' ' ); 5848msg_putchar (bufIsChanged (wp -> w_buffer ) ?'+' :' ' ); 5849msg_putchar (' ' ); 5850if (buf_spname (wp -> w_buffer )!= NULL ) { 5851xstrlcpy (IObuff ,buf_spname (wp -> w_buffer ),IOSIZE ); 5852 }else { 5853home_replace (wp -> w_buffer ,wp -> w_buffer -> b_fname ,IObuff ,IOSIZE , true); 5854 } 5855msg_outtrans (IObuff ,0 , false); 5856os_breakcheck (); 5857 } 5858 } 5859} 5860 5861/// ":detach" 5862/// 5863/// Detaches the current UI. 5864/// 5865/// ":%detach" detaches all UIs _except_ the current UI. 5866static void ex_detach (exarg_T * eap ) 5867{ 5868Channel * chan = find_channel (current_ui ); 5869if (!chan ) { 5870emsg (_ (e_noui )); 5871return ; 5872 } 5873 5874if (eap && eap -> forceit ) { 5875chan -> detach = true; 5876msg (_ ("Nvim will continue running if the UI disconnects" ),0 ); 5877return ; 5878 } 5879 5880if (eap && eap -> addr_count > 0 ) { 5881// ":%detach" detaches every UI except the current one. 5882if (eap -> line1 != 1 || eap -> line2 != curbuf -> b_ml .ml_line_count ) { 5883emsg (_ (e_invrange )); 5884return ; 5885 } 5886 5887size_t n = ui_detach_others (current_ui ); 5888if (n == 0 ) { 5889msg (_ ("No other UIs are attached" ),0 ); 5890 }else { 5891smsg (0 ,_ ("Detached %d non-current UIs" ), (int )n ); 5892 } 5893ILOG ("%%detach current_ui=%" PRIu64 " detached=%zu" ,current_ui ,n ); 5894 }else { 5895// come on pooky let's burn this mf down 5896// 5897// 1. Send "error_exit" UI-event (notification only). 5898// 2. Perform server-side UI detach. 5899// 3. Close server-side channel without self-exit. 5900Error err = ERROR_INIT ; 5901ui_detach_channel (current_ui ,& err ); 5902if (ERROR_SET (& err )) { 5903emsg (err .msg );// UI disappeared already? 5904api_clear_error (& err ); 5905return ; 5906 } 5907ILOG ("detach current_ui=%" PRIu64 ,current_ui ); 5908 } 5909} 5910 5911/// ":connect" 5912/// 5913/// Connects the current UI to a different server 5914/// 5915/// ":connect <address>" detaches the current UI and connects to the given server. 5916/// ":connect! <address>" stops the current server if no other UIs are attached, then connects to the given server. 5917static void ex_connect (exarg_T * eap ) 5918{ 5919bool stop_server = eap -> forceit ? (ui_active ()== 1 ) : false; 5920 5921Error err = ERROR_INIT ; 5922remote_ui_connect (current_ui ,eap -> arg ,& err ); 5923 5924if (ERROR_SET (& err )) { 5925emsg (err .msg ); 5926api_clear_error (& err ); 5927return ; 5928 } 5929 5930ex_detach (NULL ); 5931if (stop_server ) { 5932exiting = true; 5933getout (0 ); 5934 } 5935} 5936 5937/// ":mode": 5938/// If no argument given, get the screen size and redraw. 5939static void ex_mode (exarg_T * eap ) 5940{ 5941if (* eap -> arg == NUL ) { 5942must_redraw = UPD_CLEAR ; 5943ex_redraw (eap ); 5944 }else { 5945emsg (_ (e_screenmode )); 5946 } 5947} 5948 5949/// ":resize". 5950/// set, increment or decrement current window height 5951static void ex_resize (exarg_T * eap ) 5952{ 5953win_T * wp = curwin ; 5954 5955if (eap -> addr_count > 0 ) { 5956int n = (int )eap -> line2 ; 5957for (wp = firstwin ;wp -> w_next != NULL && -- n > 0 ;wp = wp -> w_next ) {} 5958 } 5959 5960int n = (int )atol (eap -> arg ); 5961if (cmdmod .cmod_split & WSP_VERT ) { 5962if (* eap -> arg == '-' || * eap -> arg == '+' ) { 5963n += wp -> w_width ; 5964 }else if (n == 0 && eap -> arg [0 ]== NUL ) {// default is very wide 5965n = Columns ; 5966 } 5967win_setwidth_win (n ,wp , true); 5968 }else { 5969if (* eap -> arg == '-' || * eap -> arg == '+' ) { 5970n += wp -> w_height ; 5971 }else if (n == 0 && eap -> arg [0 ]== NUL ) {// default is very high 5972n = Rows - 1 ; 5973 } 5974win_setheight_win (n ,wp , true); 5975 } 5976} 5977 5978/// ":find [+command] <file>" command. 5979static void ex_find (exarg_T * eap ) 5980{ 5981if (!check_can_set_curbuf_forceit (eap -> forceit )) { 5982return ; 5983 } 5984 5985char * fname = NULL ; 5986if (get_findfunc ()-> type != kCallbackNone ) { 5987fname = findfunc_find_file (eap -> arg ,strlen (eap -> arg ), 5988eap -> addr_count > 0 ?eap -> line2 :1 ); 5989 }else { 5990char * file_to_find = NULL ; 5991char * search_ctx = NULL ; 5992fname = find_file_in_path (eap -> arg ,strlen (eap -> arg ),FNAME_MESS , true, 5993curbuf -> b_ffname ,& file_to_find ,& search_ctx ); 5994if (eap -> addr_count > 0 ) { 5995// Repeat finding the file "count" times. This matters when it appears 5996// several times in the path. 5997linenr_T count = eap -> line2 ; 5998while (fname != NULL && -- count > 0 ) { 5999xfree (fname ); 6000fname = find_file_in_path (NULL ,0 ,FNAME_MESS , false, 6001curbuf -> b_ffname ,& file_to_find ,& search_ctx ); 6002 } 6003 } 6004xfree (file_to_find ); 6005vim_findfile_cleanup (search_ctx ); 6006 } 6007 6008if (fname == NULL ) { 6009return ; 6010 } 6011 6012eap -> arg = fname ; 6013do_exedit (eap ,NULL ); 6014xfree (fname ); 6015} 6016 6017/// ":edit", ":badd", ":balt", ":visual". 6018static void ex_edit (exarg_T * eap ) 6019{ 6020set_browse_edit_arg (eap ); 6021char * ffname = eap -> cmdidx == CMD_enew ?NULL :eap -> arg ; 6022 6023// Exclude commands which keep the window's current buffer 6024if (eap -> cmdidx != CMD_badd 6025&& eap -> cmdidx != CMD_balt 6026// All other commands must obey 'winfixbuf' / ! rules 6027&& (is_other_file (0 ,ffname )&& !check_can_set_curbuf_forceit (eap -> forceit ))) { 6028return ; 6029 } 6030 6031// prevent use of :edit on prompt-buffers 6032if (bt_prompt (curbuf )&& eap -> cmdidx == CMD_edit && * eap -> arg == NUL ) { 6033emsg ("cannot :edit a prompt buffer" ); 6034return ; 6035 } 6036 6037do_exedit (eap ,NULL ); 6038} 6039 6040/// ":edit <file>" command and alike. 6041/// 6042/// @param old_curwin curwin before doing a split or NULL 6043void do_exedit (exarg_T * eap ,win_T * old_curwin ) 6044{ 6045if ((eap -> cmdidx == CMD_new 6046|| eap -> cmdidx == CMD_tabnew 6047|| eap -> cmdidx == CMD_tabedit 6048|| eap -> cmdidx == CMD_vnew )&& * eap -> arg == NUL ) { 6049// ":new" or ":tabnew" without argument: edit a new empty buffer 6050setpcmark (); 6051do_ecmd (0 ,NULL ,NULL ,eap ,ECMD_ONE , 6052ECMD_HIDE + (eap -> forceit ?ECMD_FORCEIT :0 ), 6053old_curwin == NULL ?curwin :NULL ); 6054 }else if ((eap -> cmdidx != CMD_split && eap -> cmdidx != CMD_vsplit ) 6055|| * eap -> arg != NUL ) { 6056// Can't edit another file when "textlock" or "curbuf->b_ro_locked" is set. 6057// Only ":edit" or ":script" can bring us here, others are stopped earlier. 6058if (* eap -> arg != NUL && text_or_buf_locked ()) { 6059return ; 6060 } 6061int n = readonlymode ; 6062if (eap -> cmdidx == CMD_view || eap -> cmdidx == CMD_sview ) { 6063readonlymode = true; 6064 }else if (eap -> cmdidx == CMD_enew ) { 6065readonlymode = false;// 'readonly' doesn't make sense 6066// in an empty buffer 6067 } 6068if (eap -> cmdidx != CMD_balt && eap -> cmdidx != CMD_badd ) { 6069setpcmark (); 6070 } 6071if (do_ecmd (0 ,eap -> cmdidx == CMD_enew ?NULL :eap -> arg , 6072NULL ,eap ,eap -> do_ecmd_lnum , 6073 (buf_hide (curbuf ) ?ECMD_HIDE :0 ) 6074+ (eap -> forceit ?ECMD_FORCEIT :0 ) 6075// After a split we can use an existing buffer. 6076+ (old_curwin != NULL ?ECMD_OLDBUF :0 ) 6077+ (eap -> cmdidx == CMD_badd ?ECMD_ADDBUF :0 ) 6078+ (eap -> cmdidx == CMD_balt ?ECMD_ALTBUF :0 ), 6079old_curwin == NULL ?curwin :NULL )== FAIL ) { 6080// Editing the file failed. If the window was split, close it. 6081if (old_curwin != NULL ) { 6082bool need_hide = (curbufIsChanged ()&& curbuf -> b_nwindows <=1 ); 6083if (!need_hide || buf_hide (curbuf )) { 6084cleanup_T cs ; 6085 6086// Reset the error/interrupt/exception state here so that 6087// aborting() returns false when closing a window. 6088enter_cleanup (& cs ); 6089win_close (curwin , !need_hide && !buf_hide (curbuf ), false); 6090 6091// Restore the error/interrupt/exception state if not 6092// discarded by a new aborting error, interrupt, or 6093// uncaught exception. 6094leave_cleanup (& cs ); 6095 } 6096 } 6097 }else if (readonlymode && curbuf -> b_nwindows == 1 ) { 6098// When editing an already visited buffer, 'readonly' won't be set 6099// but the previous value is kept. With ":view" and ":sview" we 6100// want the file to be readonly, except when another window is 6101// editing the same buffer. 6102curbuf -> b_p_ro = true; 6103 } 6104readonlymode = n ; 6105 }else { 6106if (eap -> do_ecmd_cmd != NULL ) { 6107do_cmdline_cmd (eap -> do_ecmd_cmd ); 6108 } 6109int n = curwin -> w_arg_idx_invalid ; 6110check_arg_idx (curwin ); 6111if (n != curwin -> w_arg_idx_invalid ) { 6112maketitle (); 6113 } 6114 } 6115 6116// if ":split file" worked, set alternate file name in old window to new 6117// file 6118if (old_curwin != NULL 6119&& * eap -> arg != NUL 6120&& curwin != old_curwin 6121&& win_valid (old_curwin ) 6122&& old_curwin -> w_buffer != curbuf 6123&& (cmdmod .cmod_flags & CMOD_KEEPALT )== 0 ) { 6124old_curwin -> w_alt_fnum = curbuf -> b_fnum ; 6125 } 6126} 6127 6128/// ":gui" and ":gvim" when there is no GUI. 6129static void ex_nogui (exarg_T * eap ) 6130{ 6131eap -> errmsg = _ ("E25: Nvim does not have a built-in GUI" ); 6132} 6133 6134static void ex_popup (exarg_T * eap ) 6135{ 6136pum_make_popup (eap -> arg ,eap -> forceit ); 6137} 6138 6139static void ex_swapname (exarg_T * eap ) 6140{ 6141if (curbuf -> b_ml .ml_mfp == NULL || curbuf -> b_ml .ml_mfp -> mf_fname == NULL ) { 6142msg (_ ("No swap file" ),0 ); 6143 }else { 6144msg (curbuf -> b_ml .ml_mfp -> mf_fname ,0 ); 6145 } 6146} 6147 6148/// ":syncbind" forces all 'scrollbind' windows to have the same relative 6149/// offset. 6150/// (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>) 6151static void ex_syncbind (exarg_T * eap ) 6152{ 6153linenr_T vtopline ;// Target topline (including fill) 6154 6155linenr_T old_linenr = curwin -> w_cursor .lnum ; 6156 6157setpcmark (); 6158 6159// determine max (virtual) topline 6160if (curwin -> w_p_scb ) { 6161vtopline = get_vtopline (curwin ); 6162FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 6163if (wp -> w_p_scb && wp -> w_buffer ) { 6164linenr_T y = plines_m_win_fill (wp ,1 ,wp -> w_buffer -> b_ml .ml_line_count ) 6165- (int )get_scrolloff_value (curwin ); 6166vtopline = MIN (vtopline ,y ); 6167 } 6168 } 6169vtopline = MAX (vtopline ,1 ); 6170 }else { 6171vtopline = 1 ; 6172 } 6173 6174// Set all scrollbind windows to the same topline. 6175FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 6176if (wp -> w_p_scb ) { 6177int y = vtopline - get_vtopline (wp ); 6178if (y > 0 ) { 6179scrollup (wp ,y , true); 6180 }else { 6181scrolldown (wp ,- y , true); 6182 } 6183wp -> w_scbind_pos = vtopline ; 6184redraw_later (wp ,UPD_VALID ); 6185cursor_correct (wp ); 6186wp -> w_redr_status = true; 6187 } 6188 } 6189 6190if (curwin -> w_p_scb ) { 6191did_syncbind = true; 6192checkpcmark (); 6193if (old_linenr != curwin -> w_cursor .lnum ) { 6194char ctrl_o [2 ]; 6195 6196ctrl_o [0 ]= Ctrl_O ; 6197ctrl_o [1 ]= 0 ; 6198ins_typebuf (ctrl_o ,REMAP_NONE ,0 , true, false); 6199 } 6200 } 6201} 6202 6203static void ex_read (exarg_T * eap ) 6204{ 6205if (eap -> usefilter ) {// :r!cmd 6206do_bang (1 ,eap , false, false, true); 6207return ; 6208 } 6209 6210if (u_save (eap -> line2 , (linenr_T )(eap -> line2 + 1 ))== FAIL ) { 6211return ; 6212 } 6213 6214int i ; 6215if (* eap -> arg == NUL ) { 6216if (check_fname ()== FAIL ) {// check for no file name 6217return ; 6218 } 6219i = readfile (curbuf -> b_ffname ,curbuf -> b_fname , 6220eap -> line2 ,0 , (linenr_T )MAXLNUM ,eap ,0 , false); 6221 }else { 6222if (vim_strchr (p_cpo ,kCpoAltread )!= NULL ) { 6223setaltfname (eap -> arg ,eap -> arg ,1 ); 6224 } 6225i = readfile (eap -> arg ,NULL , 6226eap -> line2 ,0 , (linenr_T )MAXLNUM ,eap ,0 , false); 6227 } 6228if (i != OK ) { 6229if (!aborting ()) { 6230semsg (_ (e_notopen ),eap -> arg ); 6231 } 6232 }else { 6233redraw_curbuf_later (UPD_VALID ); 6234 } 6235} 6236 6237static char * prev_dir = NULL ; 6238 6239#ifdef EXITFREE 6240void free_cd_dir (void ) 6241{ 6242XFREE_CLEAR (prev_dir ); 6243XFREE_CLEAR (globaldir ); 6244} 6245 6246#endif 6247 6248/// Gets the previous-directory slot for the given chdir scope. 6249static char ** get_prevdir (CdScope scope ) 6250{ 6251switch (scope ) { 6252case kCdScopeTabpage : 6253return & curtab -> tp_prevdir ; 6254case kCdScopeBuffer : 6255return & curbuf -> b_prevdir ; 6256case kCdScopeWindow : 6257return & curwin -> w_prevdir ; 6258default : 6259return & prev_dir ; 6260 } 6261} 6262 6263/// Deal with the side effects of changing the current directory. 6264/// 6265/// @param scope Scope of the function call (global, tab, window or buffer). 6266static void post_chdir (CdScope scope ,bool trigger_dirchanged ) 6267{ 6268// Only :bcd overwrites the buffer-local CWD. 6269if (scope == kCdScopeBuffer ) { 6270XFREE_CLEAR (curbuf -> b_localdir ); 6271 } 6272 6273// Overwrite the window-local CWD for :cd, :tcd, :lcd. 6274if (scope >=kCdScopeWindow ) { 6275XFREE_CLEAR (curwin -> w_localdir ); 6276 } 6277 6278// Overwrite the tab-local CWD for :cd, :tcd. 6279if (scope >=kCdScopeTabpage ) { 6280XFREE_CLEAR (curtab -> tp_localdir ); 6281 } 6282 6283if (scope < kCdScopeGlobal ) { 6284char * pdir = * get_prevdir (scope ); 6285// If still in global directory, set CWD as the global directory. 6286if (globaldir == NULL && pdir != NULL ) { 6287globaldir = xstrdup (pdir ); 6288 } 6289 } 6290 6291char cwd [MAXPATHL ]; 6292if (os_dirname (cwd ,MAXPATHL )!= OK ) { 6293return ; 6294 } 6295switch (scope ) { 6296case kCdScopeGlobal : 6297// We are now in the global directory, no need to remember its name. 6298// Unless a buffer-local CWD is active, then it is in effect and must be remembered. 6299XFREE_CLEAR (globaldir ); 6300if (curbuf -> b_localdir != NULL ) { 6301globaldir = xstrdup (cwd ); 6302 } 6303break ; 6304case kCdScopeTabpage : 6305curtab -> tp_localdir = xstrdup (cwd ); 6306break ; 6307case kCdScopeBuffer : 6308curbuf -> b_localdir = xstrdup (cwd ); 6309break ; 6310case kCdScopeWindow : 6311curwin -> w_localdir = xstrdup (cwd ); 6312break ; 6313case kCdScopeInvalid : 6314abort (); 6315 } 6316 6317last_chdir_reason = NULL ; 6318shorten_fnames (vim_strchr (p_cpo ,kCpoNosymlinks )== NULL ); 6319 6320if (trigger_dirchanged ) { 6321do_autocmd_dirchanged (cwd ,scope ,kCdCauseManual , false); 6322 } 6323} 6324 6325/// Performs explicit (user) chdir (:cd/:tcd/:lcd/:bcd/chdir()/nvim_set_current_dir()). Unlike 6326/// do_autochdir() and update_cwd(), this sets the `scope` dir and reports kCdCauseManual. 6327/// 6328/// @param new_dir Directory to change to. 6329/// @param scope Set the directory on this scope. 6330/// @return true if the directory was successfully changed. 6331bool do_chdir (char * new_dir ,CdScope scope ) 6332{ 6333if (new_dir == NULL || allbuf_locked ()) { 6334return false; 6335 } 6336 6337char * pdir = NULL ; 6338// ":cd -": Change to previous directory 6339if (strcmp (new_dir ,"-" )== 0 ) { 6340pdir = * get_prevdir (scope ); 6341if (pdir == NULL ) { 6342emsg (_ ("E186: No previous directory" )); 6343return false; 6344 } 6345new_dir = pdir ; 6346 } 6347 6348if (os_dirname (NameBuff ,MAXPATHL )== OK ) { 6349pdir = xstrdup (NameBuff ); 6350 }else { 6351pdir = NULL ; 6352 } 6353 6354// For UNIX ":cd" means: go to home directory. 6355// On other systems too if 'cdhome' is set. 6356if (* new_dir == NUL && p_cdh ) { 6357// Use NameBuff for home directory name. 6358expand_env ("$HOME" ,NameBuff ,MAXPATHL ); 6359new_dir = NameBuff ; 6360 } 6361 6362new_dir = TO_SLASH_SAVE (new_dir ); 6363 6364// Buffer-local CWD is never "cleared" by :lcd/:tcd/:cd, so it stays in effect. 6365const bool bcd_active = scope != kCdScopeBuffer && curbuf -> b_localdir != NULL ; 6366 6367bool dir_differs = pdir == NULL || !path_equal (pdir ,new_dir ,kPathCmpLiteral ); 6368if (dir_differs ) { 6369if (!bcd_active ) { 6370do_autocmd_dirchanged (new_dir ,scope ,kCdCauseManual , true); 6371 } 6372// Enter `new_dir` even if it will not apply (because of buf-local dir): 6373// post_chdir() reads back its resolved name. 6374if (vim_chdir (new_dir )!= 0 ) { 6375emsg (_ (e_failed )); 6376xfree (new_dir ); 6377xfree (pdir ); 6378return false; 6379 } 6380 } 6381xfree (new_dir ); 6382 6383char ** pp = get_prevdir (scope ); 6384xfree (* pp ); 6385* pp = pdir ; 6386 6387post_chdir (scope ,dir_differs && !bcd_active ); 6388if (bcd_active ) { 6389os_chdir (curbuf -> b_localdir ); 6390shorten_fnames (true); 6391 } 6392ctx_did_chdir (); 6393 6394return true; 6395} 6396 6397/// ":lcd!", ":tcd!", ":bcd!": Clears the scope-local directory. No-op if scope has no local dir. 6398static void chdir_clear (CdScope scope ) 6399{ 6400char ** localdir = & curtab -> tp_localdir ; 6401if (scope == kCdScopeBuffer ) { 6402localdir = & curbuf -> b_localdir ; 6403 }else if (scope == kCdScopeWindow ) { 6404localdir = & curwin -> w_localdir ; 6405 } 6406if (* localdir == NULL ) { 6407return ; 6408 } 6409XFREE_CLEAR (* localdir ); 6410update_cwd (kCdCauseManual ); 6411ctx_did_chdir (); 6412} 6413 6414/// ":cd", ":tcd", ":lcd", ":bcd", ":chdir", ":tchdir", ":lchdir", and ":bchdir". 6415void ex_cd (exarg_T * eap ) 6416{ 6417char * new_dir = eap -> arg ; 6418 6419CdScope scope = kCdScopeGlobal ; 6420switch (eap -> cmdidx ) { 6421case CMD_tcd : 6422case CMD_tchdir : 6423scope = kCdScopeTabpage ; 6424break ; 6425case CMD_bcd : 6426case CMD_bchdir : 6427scope = kCdScopeBuffer ; 6428break ; 6429case CMD_lcd : 6430case CMD_lchdir : 6431scope = kCdScopeWindow ; 6432break ; 6433default : 6434break ; 6435 } 6436 6437// ":lcd!", ":tcd!", ":bcd!": unset that scope. 6438if (eap -> forceit && * new_dir == NUL && scope != kCdScopeGlobal ) { 6439chdir_clear (scope ); 6440if (KeyTyped || p_verbose >=5 ) { 6441ex_pwd (eap ); 6442 } 6443return ; 6444 } 6445 6446// for non-UNIX ":cd" means: print current directory unless 'cdhome' is set 6447if (* new_dir == NUL && !p_cdh ) { 6448ex_pwd (NULL ); 6449return ; 6450 } 6451 6452if (do_chdir (new_dir ,scope )) { 6453// Echo the new current directory if the command was typed. 6454if (KeyTyped || p_verbose >=5 ) { 6455ex_pwd (eap ); 6456 } 6457 } 6458} 6459 6460/// ":pwd". 6461static void ex_pwd (exarg_T * eap ) 6462{ 6463if (os_dirname (NameBuff ,MAXPATHL )== OK ) { 6464#ifdef BACKSLASH_IN_FILENAME 6465slash_adjust (NameBuff ); 6466#endif 6467if (p_verbose > 0 ) { 6468char * context = "global" ; 6469if (last_chdir_reason != NULL ) { 6470context = last_chdir_reason ; 6471 }else if (curbuf -> b_localdir != NULL ) { 6472context = "buffer" ; 6473 }else if (curwin -> w_localdir != NULL ) { 6474context = "window" ; 6475 }else if (curtab -> tp_localdir != NULL ) { 6476context = "tabpage" ; 6477 } 6478smsg (0 ,"[%s] %s" ,context ,NameBuff ); 6479 }else { 6480msg (NameBuff ,0 ); 6481 } 6482 }else { 6483emsg (_ ("E187: Unknown" )); 6484 } 6485} 6486 6487/// ":=". 6488static void ex_equal (exarg_T * eap ) 6489{ 6490if (* eap -> arg != NUL && * eap -> arg != '|' ) { 6491// equivalent to :lua= expr 6492ex_lua (eap ); 6493 }else { 6494eap -> nextcmd = find_nextcmd (eap -> arg ); 6495smsg (0 ,"%" PRId64 , (int64_t )eap -> line2 ); 6496 } 6497} 6498 6499static void ex_sleep (exarg_T * eap ) 6500{ 6501if (cursor_valid (curwin )) { 6502setcursor_mayforce (curwin , true); 6503 } 6504 6505int64_t len = eap -> line2 ; 6506switch (* eap -> arg ) { 6507case 'm' : 6508break ; 6509case NUL : 6510len *=1000 ;break ; 6511default : 6512semsg (_ (e_invarg2 ),eap -> arg );return ; 6513 } 6514 6515// Hide the cursor if invoked with ! 6516do_sleep (len ,eap -> forceit ); 6517} 6518 6519/// Sleep for "msec" milliseconds, but return early on CTRL-C. 6520/// 6521/// @param hide_cursor hide the cursor if true 6522void do_sleep (int64_t msec ,bool hide_cursor ) 6523{ 6524if (hide_cursor ) { 6525ui_busy_start (); 6526 } 6527 6528ui_flush ();// flush before waiting 6529LOOP_PROCESS_EVENTS_UNTIL (& main_loop ,main_loop .events ,msec ,got_int ); 6530 6531// If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the 6532// input buffer, otherwise a following call to input() fails. 6533if (got_int ) { 6534vpeekc (); 6535 } 6536 6537if (hide_cursor ) { 6538ui_busy_stop (); 6539 } 6540} 6541 6542/// ":winsize" command (obsolete). 6543static void ex_winsize (exarg_T * eap ) 6544{ 6545char * arg = eap -> arg ; 6546 6547if (!ascii_isdigit (* arg )) { 6548semsg (_ (e_invarg2 ),arg ); 6549return ; 6550 } 6551int w = getdigits_int (& arg , false,10 ); 6552arg = skipwhite (arg ); 6553char * p = arg ; 6554int h = getdigits_int (& arg , false,10 ); 6555if (* p != NUL && * arg == NUL ) { 6556screen_resize (w ,h ); 6557 }else { 6558emsg (_ ("E465: :winsize requires two number arguments" )); 6559 } 6560} 6561 6562static void ex_wincmd (exarg_T * eap ) 6563{ 6564int xchar = NUL ; 6565char * p ; 6566 6567if (* eap -> arg == 'g' || * eap -> arg == Ctrl_G ) { 6568// CTRL-W g and CTRL-W CTRL-G have an extra command character 6569if (eap -> arg [1 ]== NUL ) { 6570emsg (_ (e_invarg )); 6571return ; 6572 } 6573xchar = (uint8_t )eap -> arg [1 ]; 6574p = eap -> arg + 2 ; 6575 }else { 6576p = eap -> arg + 1 ; 6577 } 6578 6579eap -> nextcmd = check_nextcmd (p ); 6580p = skipwhite (p ); 6581if (* p != NUL && * p != '"' && eap -> nextcmd == NULL ) { 6582emsg (_ (e_invarg )); 6583 }else if (!eap -> skip ) { 6584// Pass flags on for ":vertical wincmd ]". 6585postponed_split_flags = cmdmod .cmod_split ; 6586postponed_split_tab = cmdmod .cmod_tab ; 6587do_window (* eap -> arg ,eap -> addr_count > 0 ?eap -> line2 :0 ,xchar ); 6588postponed_split_flags = 0 ; 6589postponed_split_tab = 0 ; 6590 } 6591} 6592 6593/// Handle command that work like operators: ":delete", ":yank", ":>" and ":<". 6594static void ex_operators (exarg_T * eap ) 6595{ 6596oparg_T oa ; 6597 6598clear_oparg (& oa ); 6599oa .regname = eap -> regname ; 6600oa .start .lnum = eap -> line1 ; 6601oa .end .lnum = eap -> line2 ; 6602oa .line_count = eap -> line2 - eap -> line1 + 1 ; 6603oa .motion_type = kMTLineWise ; 6604virtual_op = kFalse ; 6605if (eap -> cmdidx != CMD_yank ) {// position cursor for undo 6606setpcmark (); 6607curwin -> w_cursor .lnum = eap -> line1 ; 6608beginline (BL_SOL |BL_FIX ); 6609 } 6610 6611if (Visual .active ) { 6612end_visual_mode (); 6613 } 6614 6615switch (eap -> cmdidx ) { 6616case CMD_delete : 6617oa .op_type = OP_DELETE ; 6618op_delete (& oa ); 6619break ; 6620 6621case CMD_yank : 6622oa .op_type = OP_YANK ; 6623op_yank (& oa , true); 6624break ; 6625 6626default :// CMD_rshift or CMD_lshift 6627if ( 6628 (eap -> cmdidx == CMD_rshift ) ^curwin -> w_p_rl ) { 6629oa .op_type = OP_RSHIFT ; 6630 }else { 6631oa .op_type = OP_LSHIFT ; 6632 } 6633op_shift (& oa , false,eap -> amount ); 6634break ; 6635 } 6636virtual_op = kNone ; 6637ex_may_print (eap ); 6638} 6639 6640/// ":put". 6641static void ex_put (exarg_T * eap ) 6642{ 6643// ":0put" works like ":1put!". 6644if (eap -> line2 == 0 ) { 6645eap -> line2 = 1 ; 6646eap -> forceit = true; 6647 } 6648curwin -> w_cursor .lnum = eap -> line2 ; 6649check_cursor_col (curwin ); 6650do_put (eap -> regname ,NULL ,eap -> forceit ?BACKWARD :FORWARD ,1 , 6651PUT_LINE |PUT_CURSLINE ); 6652} 6653 6654/// ":iput". 6655static void ex_iput (exarg_T * eap ) 6656{ 6657// ":0iput" works like ":1iput!". 6658if (eap -> line2 == 0 ) { 6659eap -> line2 = 1 ; 6660eap -> forceit = true; 6661 } 6662curwin -> w_cursor .lnum = eap -> line2 ; 6663check_cursor_col (curwin ); 6664do_put (eap -> regname ,NULL ,eap -> forceit ?BACKWARD :FORWARD ,1L , 6665PUT_LINE |PUT_CURSLINE |PUT_FIXINDENT ); 6666} 6667 6668/// Handle ":copy" and ":move". 6669static void ex_copymove (exarg_T * eap ) 6670{ 6671const char * errormsg = NULL ; 6672linenr_T n = get_address (eap ,& eap -> arg ,eap -> addr_type , false, false, false,1 ,& errormsg ); 6673if (eap -> arg == NULL ) {// error detected 6674if (errormsg != NULL ) { 6675emsg (errormsg ); 6676 } 6677eap -> nextcmd = NULL ; 6678return ; 6679 } 6680get_flags (eap ); 6681 6682// move or copy lines from 'eap->line1'-'eap->line2' to below line 'n' 6683if (n == MAXLNUM || n < 0 || n > curbuf -> b_ml .ml_line_count ) { 6684emsg (_ (e_invrange )); 6685return ; 6686 } 6687 6688if (eap -> cmdidx == CMD_move ) { 6689if (do_move (eap -> line1 ,eap -> line2 ,n )== FAIL ) { 6690return ; 6691 } 6692 }else { 6693ex_copy (eap -> line1 ,eap -> line2 ,n ); 6694 } 6695u_clearline (curbuf ); 6696beginline (BL_SOL |BL_FIX ); 6697ex_may_print (eap ); 6698} 6699 6700/// Print the current line if flags were given to the Ex command. 6701void ex_may_print (exarg_T * eap ) 6702{ 6703if (eap -> flags != 0 ) { 6704print_line (curwin -> w_cursor .lnum , (eap -> flags & EXFLAG_NR ), 6705 (eap -> flags & EXFLAG_LIST ), true); 6706 } 6707} 6708 6709/// ":join". 6710static void ex_join (exarg_T * eap ) 6711{ 6712curwin -> w_cursor .lnum = eap -> line1 ; 6713if (eap -> line1 == eap -> line2 ) { 6714if (eap -> addr_count >=2 ) {// :2,2join does nothing 6715return ; 6716 } 6717if (eap -> line2 == curbuf -> b_ml .ml_line_count ) { 6718beep_flush (); 6719return ; 6720 } 6721eap -> line2 ++ ; 6722 } 6723do_join ((size_t )((ssize_t )eap -> line2 - eap -> line1 + 1 ), !eap -> forceit , true, true, true); 6724beginline (BL_WHITE |BL_FIX ); 6725ex_may_print (eap ); 6726} 6727 6728/// ":[addr]@r": execute register 6729static void ex_at (exarg_T * eap ) 6730{ 6731int prev_len = typebuf .tb_len ; 6732 6733curwin -> w_cursor .lnum = eap -> line2 ; 6734check_cursor_col (curwin ); 6735 6736// Get the register name. No name means use the previous one. 6737int c = (uint8_t )(* eap -> arg ); 6738if (c == NUL ) { 6739c = '@' ; 6740 } 6741 6742// Put the register in the typeahead buffer with the "silent" flag. 6743if (do_execreg (c , true,vim_strchr (p_cpo ,kCpoExecbuf )!= NULL , true)== FAIL ) { 6744beep_flush (); 6745return ; 6746 } 6747 6748const bool save_efr = exec_from_reg ; 6749 6750exec_from_reg = true; 6751 6752// Execute from the typeahead buffer. 6753// Continue until the stuff buffer is empty and all added characters 6754// have been consumed. 6755while (!stuff_empty ()|| typebuf .tb_len > prev_len ) { 6756do_cmdline (NULL ,getexline ,NULL ,DOCMD_NOWAIT |DOCMD_VERBOSE ); 6757 } 6758 6759exec_from_reg = save_efr ; 6760} 6761 6762/// ":!". 6763static void ex_bang (exarg_T * eap ) 6764{ 6765do_bang (eap -> addr_count ,eap ,eap -> forceit , true, true); 6766} 6767 6768/// ":undo". 6769static void ex_undo (exarg_T * eap ) 6770{ 6771if (eap -> addr_count != 1 ) { 6772if (eap -> forceit ) { 6773u_undo_and_forget (1 , true);// :undo! 6774 }else { 6775u_undo (1 );// :undo 6776 } 6777return ; 6778 } 6779 6780linenr_T step = eap -> line2 ; 6781 6782if (eap -> forceit ) {// undo! 123 6783// change number for "undo!" must be lesser than current change number 6784if (step >=curbuf -> b_u_seq_cur ) { 6785emsg (_ (e_undobang_cannot_redo_or_move_branch )); 6786return ; 6787 } 6788// ensure that target change number is in same branch 6789// while also counting the amount of undoes it'd take to reach target 6790u_header_T * uhp ; 6791int count = 0 ; 6792 6793for (uhp = curbuf -> b_u_curhead ?curbuf -> b_u_curhead :curbuf -> b_u_newhead ; 6794uhp != NULL && uhp -> uh_seq > step ; 6795uhp = uhp -> uh_next .ptr ,++ count ) {} 6796if (step != 0 && (uhp == NULL || uhp -> uh_seq < step )) { 6797emsg (_ (e_undobang_cannot_redo_or_move_branch )); 6798return ; 6799 } 6800u_undo_and_forget (count , true); 6801 }else {// :undo 123 6802undo_time (step , false, false, true); 6803 } 6804} 6805 6806static void ex_wundo (exarg_T * eap ) 6807{ 6808uint8_t hash [UNDO_HASH_SIZE ]; 6809 6810u_compute_hash (curbuf ,hash ); 6811u_write_undo (eap -> arg ,eap -> forceit ,curbuf ,hash ); 6812} 6813 6814static void ex_rundo (exarg_T * eap ) 6815{ 6816uint8_t hash [UNDO_HASH_SIZE ]; 6817 6818u_compute_hash (curbuf ,hash ); 6819u_read_undo (eap -> arg ,hash ,NULL ); 6820} 6821 6822/// ":redo". 6823static void ex_redo (exarg_T * eap ) 6824{ 6825u_redo (1 ); 6826} 6827 6828/// ":earlier" and ":later". 6829static void ex_later (exarg_T * eap ) 6830{ 6831int count = 0 ; 6832bool sec = false; 6833bool file = false; 6834char * p = eap -> arg ; 6835 6836if (* p == NUL ) { 6837count = 1 ; 6838 }else if (isdigit ((uint8_t )(* p ))) { 6839count = getdigits_int (& p , false,0 ); 6840switch (* p ) { 6841case 's' : 6842p ++ ;sec = true;break ; 6843case 'm' : 6844p ++ ;sec = true;count *=60 ;break ; 6845case 'h' : 6846p ++ ;sec = true;count *=60 * 60 ;break ; 6847case 'd' : 6848p ++ ;sec = true;count *=24 * 60 * 60 ;break ; 6849case 'f' : 6850p ++ ;file = true;break ; 6851 } 6852 } 6853 6854if (* p != NUL ) { 6855semsg (_ (e_invarg2 ),eap -> arg ); 6856 }else { 6857undo_time (eap -> cmdidx == CMD_earlier ?- count :count , 6858sec ,file , false); 6859 } 6860} 6861 6862/// ":redir": start/stop redirection. 6863static void ex_redir (exarg_T * eap ) 6864{ 6865char * arg = eap -> arg ; 6866 6867if (STRICMP (eap -> arg ,"END" )== 0 ) { 6868close_redir (); 6869 }else { 6870if (* arg == '>' ) { 6871arg ++ ; 6872char * mode ; 6873if (* arg == '>' ) { 6874arg ++ ; 6875mode = "a" ; 6876 }else { 6877mode = "w" ; 6878 } 6879arg = skipwhite (arg ); 6880 6881close_redir (); 6882 6883// Expand environment variables and "~/". 6884char * fname = expand_env_save (arg ); 6885if (fname == NULL ) { 6886return ; 6887 } 6888 6889redir_fd = open_exfile (fname ,eap -> forceit ,mode ); 6890xfree (fname ); 6891 }else if (* arg == '@' ) { 6892// redirect to a register a-z (resp. A-Z for appending) 6893close_redir (); 6894arg ++ ; 6895if (valid_yank_reg (* arg , true)&& * arg != '_' ) { 6896redir_reg = (uint8_t )(* arg ++ ); 6897if (* arg == '>' && arg [1 ]== '>' ) {// append 6898arg += 2 ; 6899 }else { 6900// Can use both "@a" and "@a>". 6901if (* arg == '>' ) { 6902arg ++ ; 6903 } 6904// Make register empty when not using @A-@Z and the 6905// command is valid. 6906if (* arg == NUL && !isupper (redir_reg )) { 6907write_reg_contents (redir_reg ,"" ,0 , false); 6908 } 6909 } 6910 } 6911if (* arg != NUL ) { 6912redir_reg = 0 ; 6913semsg (_ (e_invarg2 ),eap -> arg ); 6914 } 6915 }else if (* arg == '=' && arg [1 ]== '>' ) { 6916bool append ; 6917 6918// redirect to a variable 6919close_redir (); 6920arg += 2 ; 6921 6922if (* arg == '>' ) { 6923arg ++ ; 6924append = true; 6925 }else { 6926append = false; 6927 } 6928 6929if (var_redir_start (skipwhite (arg ),append )== OK ) { 6930redir_vname = true; 6931 } 6932 }else {// TODO(vim): redirect to a buffer 6933semsg (_ (e_invarg2 ),eap -> arg ); 6934 } 6935 } 6936 6937// Make sure redirection is not off. Can happen for cmdline completion 6938// that indirectly invokes a command to catch its output. 6939if (redir_fd != NULL 6940|| redir_reg || redir_vname ) { 6941redir_off = false; 6942 } 6943} 6944 6945/// ":redraw": force redraw 6946static void ex_redraw (exarg_T * eap ) 6947{ 6948if (cmdpreview ) { 6949return ;// Ignore :redraw during 'inccommand' preview. #9777 6950 } 6951int r = RedrawingDisabled ; 6952int p = p_lz ; 6953 6954RedrawingDisabled = 0 ; 6955p_lz = false; 6956validate_cursor (curwin ); 6957update_topline (curwin ); 6958if (eap -> forceit ) { 6959redraw_all_later (UPD_NOT_VALID ); 6960redraw_cmdline = true; 6961 }else if (Visual .active ) { 6962redraw_curbuf_later (UPD_INVERTED ); 6963 } 6964update_screen (); 6965if (need_maketitle ) { 6966maketitle (); 6967 } 6968RedrawingDisabled = r ; 6969p_lz = p ; 6970 6971// Reset msg_didout, so that a message that's there is overwritten. 6972msg_didout = false; 6973msg_col = 0 ; 6974 6975// No need to wait after an intentional redraw. 6976need_wait_return = false; 6977 6978ui_flush (); 6979} 6980 6981/// ":redrawstatus": force redraw of status line(s) and window bar(s) 6982static void ex_redrawstatus (exarg_T * eap ) 6983{ 6984if (cmdpreview ) { 6985return ;// Ignore :redrawstatus during 'inccommand' preview. #9777 6986 } 6987int r = RedrawingDisabled ; 6988int p = p_lz ; 6989 6990if (eap -> forceit ) { 6991status_redraw_all (); 6992 }else { 6993status_redraw_curbuf (); 6994 } 6995 6996RedrawingDisabled = 0 ; 6997p_lz = false; 6998if (State & MODE_CMDLINE ) { 6999redraw_statuslines (); 7000 }else { 7001if (Visual .active ) { 7002redraw_curbuf_later (UPD_INVERTED ); 7003 } 7004update_screen (); 7005 } 7006RedrawingDisabled = r ; 7007p_lz = p ; 7008ui_flush (); 7009} 7010 7011/// ":redrawtabline": force redraw of the tabline 7012static void ex_redrawtabline (exarg_T * eap FUNC_ATTR_UNUSED ) 7013{ 7014const int r = RedrawingDisabled ; 7015const int p = p_lz ; 7016 7017RedrawingDisabled = 0 ; 7018p_lz = false; 7019 7020draw_tabline (); 7021 7022RedrawingDisabled = r ; 7023p_lz = p ; 7024ui_flush (); 7025} 7026 7027static void close_redir (void ) 7028{ 7029if (redir_fd != NULL ) { 7030fclose (redir_fd ); 7031redir_fd = NULL ; 7032 } 7033redir_reg = 0 ; 7034if (redir_vname ) { 7035var_redir_stop (); 7036redir_vname = false; 7037 } 7038} 7039 7040/// Try creating a directory, give error message on failure 7041/// 7042/// @param[in] name Directory to create. 7043/// @param[in] prot Directory permissions. 7044/// 7045/// @return OK in case of success, FAIL otherwise. 7046int vim_mkdir_emsg (const char * const name ,const int prot ) 7047FUNC_ATTR_NONNULL_ALL 7048{ 7049int ret ; 7050if ((ret = os_mkdir (name ,prot ))!= 0 ) { 7051semsg (_ (e_mkdir ),name ,os_strerror (ret )); 7052return FAIL ; 7053 } 7054return OK ; 7055} 7056 7057/// Open a file for writing for an Ex command, with some checks. 7058/// 7059/// @param mode "w" for create new file or "a" for append 7060/// 7061/// @return file descriptor, or NULL on failure. 7062FILE * open_exfile (char * fname ,int forceit ,char * mode ) 7063{ 7064#ifdef UNIX 7065// with Unix it is possible to open a directory 7066if (os_isdir (fname )) { 7067semsg (_ (e_isadir2 ),fname ); 7068return NULL ; 7069 } 7070#endif 7071if (!forceit && * mode != 'a' && os_path_exists (fname )) { 7072semsg (_ ("E189: \"%s\" exists (add ! to override)" ),fname ); 7073return NULL ; 7074 } 7075 7076FILE * fd ; 7077if ((fd = os_fopen (fname ,mode ))== NULL ) { 7078semsg (_ ("E190: Cannot open \"%s\" for writing" ),fname ); 7079 } 7080 7081return fd ; 7082} 7083 7084/// ":mark" and ":k". 7085static void ex_mark (exarg_T * eap ) 7086{ 7087if (* eap -> arg == NUL ) {// No argument? 7088emsg (_ (e_argreq )); 7089return ; 7090 } 7091 7092if (eap -> arg [1 ]!= NUL ) {// more than one character? 7093semsg (_ (e_trailing_arg ),eap -> arg ); 7094return ; 7095 } 7096 7097pos_T pos = curwin -> w_cursor ;// save curwin->w_cursor 7098curwin -> w_cursor .lnum = eap -> line2 ; 7099beginline (BL_WHITE |BL_FIX ); 7100if (setmark (* eap -> arg )== FAIL ) {// set mark 7101emsg (_ ("E191: Argument must be a letter or forward/backward quote" )); 7102 } 7103curwin -> w_cursor = pos ;// restore curwin->w_cursor 7104} 7105 7106/// Update w_topline, w_leftcol and the cursor position. 7107void update_topline_cursor (void ) 7108{ 7109check_cursor (curwin );// put cursor on valid line 7110update_topline (curwin ); 7111if (!curwin -> w_p_wrap ) { 7112validate_cursor (curwin ); 7113 } 7114update_curswant (); 7115} 7116 7117/// Save the current State (editor-mode) and go to Normal mode. 7118void save_current_state (save_state_T * sst ) 7119FUNC_ATTR_NONNULL_ALL 7120{ 7121sst -> save_msg_scroll = msg_scroll ; 7122sst -> save_restart_edit = restart_edit ; 7123sst -> save_msg_didout = msg_didout ; 7124sst -> save_State = State ; 7125sst -> save_finish_op = finish_op ; 7126sst -> save_opcount = opcount ; 7127sst -> save_reg_executing = reg_executing ; 7128sst -> save_pending_end_reg_executing = pending_end_reg_executing ; 7129 7130msg_scroll = false;// no msg scrolling in Normal mode 7131restart_edit = 0 ;// don't go to Insert mode 7132 7133// Save the current typeahead. This is required to allow using ":normal" 7134// from an event handler and makes sure we don't hang when the argument 7135// ends with half a command. 7136save_typeahead (& sst -> tabuf ); 7137} 7138 7139void restore_current_state (save_state_T * sst ) 7140FUNC_ATTR_NONNULL_ALL 7141{ 7142// Restore the previous typeahead. 7143restore_typeahead (& sst -> tabuf ); 7144 7145msg_scroll = sst -> save_msg_scroll ; 7146if (force_restart_edit ) { 7147force_restart_edit = false; 7148 }else { 7149// Some function (terminal_enter()) was aware of ex_normal and decided to 7150// override the value of restart_edit anyway. 7151restart_edit = sst -> save_restart_edit ; 7152 } 7153finish_op = sst -> save_finish_op ; 7154opcount = sst -> save_opcount ; 7155reg_executing = sst -> save_reg_executing ; 7156pending_end_reg_executing = sst -> save_pending_end_reg_executing ; 7157 7158// don't reset msg_didout now 7159msg_didout |=sst -> save_msg_didout ; 7160 7161// Restore the state (needed when called from a function executed for 7162// 'indentexpr'). Update the mouse and cursor, they may have changed. 7163State = sst -> save_State ; 7164ui_cursor_shape ();// may show different cursor shape 7165} 7166 7167/// True while evaluating an `<expr>` mapping. 7168/// Buffer changes are not allowed, except for internal-only dummy buffers. 7169bool expr_map_locked (void ) 7170{ 7171return expr_map_lock > 0 && !(curbuf -> b_flags & BF_DUMMY ); 7172} 7173 7174/// ":normal[!] {commands}": Execute normal mode commands. 7175static void ex_normal (exarg_T * eap ) 7176{ 7177if (curbuf -> terminal && State & MODE_TERMINAL ) { 7178emsg ("Can't re-enter normal mode from terminal mode" ); 7179return ; 7180 } 7181char * arg = NULL ; 7182 7183if (expr_map_locked ()) { 7184emsg (_ (e_secure )); 7185return ; 7186 } 7187 7188if (ex_normal_busy >=p_mmd ) { 7189emsg (_ ("E192: Recursive use of :normal too deep" )); 7190return ; 7191 } 7192 7193// vgetc() expects K_SPECIAL to have been escaped. Don't do 7194// this for the K_SPECIAL leading byte, otherwise special keys will not 7195// work. 7196 { 7197int len = 0 ; 7198 7199// Count the number of characters to be escaped. 7200int l ; 7201for (char * p = eap -> arg ;* p != NUL ;p ++ ) { 7202for (l = utfc_ptr2len (p )- 1 ;l > 0 ;l -- ) { 7203if (*++ p == (char )K_SPECIAL ) {// trailbyte K_SPECIAL 7204len += 2 ; 7205 } 7206 } 7207 } 7208if (len > 0 ) { 7209arg = xmalloc (strlen (eap -> arg )+ (size_t )len + 1 ); 7210len = 0 ; 7211for (char * p = eap -> arg ;* p != NUL ;p ++ ) { 7212arg [len ++ ]= * p ; 7213for (l = utfc_ptr2len (p )- 1 ;l > 0 ;l -- ) { 7214arg [len ++ ]= *++ p ; 7215if (* p == (char )K_SPECIAL ) { 7216arg [len ++ ]= (char )KS_SPECIAL ; 7217arg [len ++ ]= KE_FILLER ; 7218 } 7219 } 7220arg [len ]= NUL ; 7221 } 7222 } 7223 } 7224 7225ex_normal_busy ++ ; 7226save_state_T save_state ; 7227save_current_state (& save_state ); 7228// Repeat the :normal command for each line in the range. When no 7229// range given, execute it just once, without positioning the cursor 7230// first. 7231do { 7232if (eap -> addr_count != 0 ) { 7233curwin -> w_cursor .lnum = eap -> line1 ++ ; 7234curwin -> w_cursor .col = 0 ; 7235check_cursor_moved (curwin ); 7236 } 7237 7238exec_normal_cmd ((arg != NULL ?arg :eap -> arg ), 7239eap -> forceit ?REMAP_NONE :REMAP_YES , false); 7240 }while (eap -> addr_count > 0 && eap -> line1 <=eap -> line2 && !got_int ); 7241 7242// Might not return to the main loop when in an event handler. 7243update_topline_cursor (); 7244 7245restore_current_state (& save_state ); 7246 7247ex_normal_busy -- ; 7248 7249setmouse (); 7250ui_cursor_shape ();// may show different cursor shape 7251xfree (arg ); 7252} 7253 7254/// ":startinsert", ":startreplace" and ":startgreplace" 7255static void ex_startinsert (exarg_T * eap ) 7256{ 7257if (!curbuf -> b_p_ma && !curbuf -> terminal ) { 7258emsg (_ (e_modifiable )); 7259return ; 7260 } 7261if (eap -> forceit ) { 7262// cursor line can be zero on startup 7263if (!curwin -> w_cursor .lnum ) { 7264curwin -> w_cursor .lnum = 1 ; 7265 } 7266set_cursor_for_append_to_line (); 7267 } 7268 7269// Ignore the command when already in Insert mode. Inserting an 7270// expression register that invokes a function can do this. 7271if (State & MODE_INSERT ) { 7272return ; 7273 } 7274 7275if (eap -> cmdidx == CMD_startinsert ) { 7276restart_edit = 'a' ; 7277 }else if (eap -> cmdidx == CMD_startreplace ) { 7278restart_edit = 'R' ; 7279 }else { 7280restart_edit = 'V' ; 7281 } 7282 7283if (!eap -> forceit ) { 7284if (eap -> cmdidx == CMD_startinsert ) { 7285restart_edit = 'i' ; 7286 } 7287curwin -> w_curswant = 0 ;// avoid MAXCOL 7288 } 7289 7290if (Visual .active ) { 7291showmode (); 7292 } 7293} 7294 7295/// ":stopinsert" 7296static void ex_stopinsert (exarg_T * eap ) 7297{ 7298restart_edit = 0 ; 7299Ins .stop_insert_mode = true; 7300clearmode (); 7301} 7302 7303/// Execute normal mode command "cmd". 7304/// "remap" can be REMAP_NONE or REMAP_YES. 7305void exec_normal_cmd (char * cmd ,int remap ,bool silent ) 7306{ 7307// Stuff the argument into the typeahead buffer. 7308ins_typebuf (cmd ,remap ,0 , true,silent ); 7309exec_normal (false, false); 7310} 7311 7312/// Execute normal_cmd() until there is no typeahead left. 7313/// 7314/// @param was_typed whether or not something was typed 7315/// @param use_vpeekc true to use vpeekc() to check for available chars 7316void exec_normal (bool was_typed ,bool use_vpeekc ) 7317{ 7318oparg_T oa ; 7319int c ; 7320 7321// When calling vpeekc() from feedkeys() it will return Ctrl_C when there 7322// is nothing to get, so also check for Ctrl_C. 7323clear_oparg (& oa ); 7324finish_op = false; 7325while ((!stuff_empty () 7326|| ((was_typed || !typebuf_typed ()) 7327&& typebuf .tb_len > 0 ) 7328|| (use_vpeekc && (c = vpeekc ())!= NUL && c != Ctrl_C )) 7329&& !got_int ) { 7330update_topline_cursor (); 7331normal_cmd (& oa , true);// execute a Normal mode cmd 7332 } 7333} 7334 7335static void ex_checkpath (exarg_T * eap ) 7336{ 7337find_pattern_in_path (NULL ,0 ,0 , false, false,CHECK_PATH ,1 , 7338eap -> forceit ?ACTION_SHOW_ALL :ACTION_SHOW , 73391 , (linenr_T )MAXLNUM ,eap -> forceit , false); 7340} 7341 7342/// ":psearch" 7343static void ex_psearch (exarg_T * eap ) 7344{ 7345g_do_tagpreview = (int )p_pvh ; 7346ex_findpat (eap ); 7347g_do_tagpreview = 0 ; 7348} 7349 7350static void ex_findpat (exarg_T * eap ) 7351{ 7352bool whole = true; 7353int action ; 7354 7355switch (cmdnames [eap -> cmdidx ].cmd_name [2 ]) { 7356case 'e' :// ":psearch", ":isearch" and ":dsearch" 7357if (cmdnames [eap -> cmdidx ].cmd_name [0 ]== 'p' ) { 7358action = ACTION_GOTO ; 7359 }else { 7360action = ACTION_SHOW ; 7361 } 7362break ; 7363case 'i' :// ":ilist" and ":dlist" 7364action = ACTION_SHOW_ALL ; 7365break ; 7366case 'u' :// ":ijump" and ":djump" 7367action = ACTION_GOTO ; 7368break ; 7369default :// ":isplit" and ":dsplit" 7370action = ACTION_SPLIT ; 7371break ; 7372 } 7373 7374int n = 1 ; 7375if (ascii_isdigit (* eap -> arg )) {// get count 7376n = getdigits_int (& eap -> arg , false,0 ); 7377eap -> arg = skipwhite (eap -> arg ); 7378 } 7379if (* eap -> arg == '/' ) {// Match regexp, not just whole words 7380whole = false; 7381eap -> arg ++ ; 7382char * p = skip_regexp (eap -> arg ,'/' ,p_magic ); 7383if (* p ) { 7384* p ++ = NUL ; 7385p = skipwhite (p ); 7386 7387// Check for trailing illegal characters. 7388if (!ends_excmd (* p )) { 7389eap -> errmsg = ex_errmsg (e_trailing_arg ,p ); 7390 }else { 7391eap -> nextcmd = check_nextcmd (p ); 7392 } 7393 } 7394 } 7395if (!eap -> skip ) { 7396find_pattern_in_path (eap -> arg ,0 ,strlen (eap -> arg ),whole , !eap -> forceit , 7397* eap -> cmd == 'd' ?FIND_DEFINE :FIND_ANY ,n ,action , 7398eap -> line1 ,eap -> line2 ,eap -> forceit , false); 7399 } 7400} 7401 7402/// ":ptag", ":ptselect", ":ptjump", ":ptnext", etc. 7403static void ex_ptag (exarg_T * eap ) 7404{ 7405g_do_tagpreview = (int )p_pvh ;// will be reset to 0 in ex_tag_cmd() 7406ex_tag_cmd (eap ,cmdnames [eap -> cmdidx ].cmd_name + 1 ); 7407} 7408 7409/// ":pedit" 7410static void ex_pedit (exarg_T * eap ) 7411{ 7412win_T * curwin_save = curwin ; 7413prepare_preview_window (); 7414 7415// Edit the file. 7416do_exedit (eap ,NULL ); 7417 7418back_to_current_window (curwin_save ); 7419} 7420 7421/// ":pbuffer" 7422static void ex_pbuffer (exarg_T * eap ) 7423{ 7424win_T * curwin_save = curwin ; 7425prepare_preview_window (); 7426 7427// Go to the buffer. 7428do_exbuffer (eap ); 7429 7430back_to_current_window (curwin_save ); 7431} 7432 7433static void prepare_preview_window (void ) 7434{ 7435// Open the preview window or popup and make it the current window. 7436g_do_tagpreview = (int )p_pvh ; 7437prepare_tagpreview (true,* p_pvp != NUL ); 7438} 7439 7440static void back_to_current_window (win_T * curwin_save ) 7441{ 7442if (curwin != curwin_save && win_valid (curwin_save )) { 7443// Return cursor to where we were 7444validate_cursor (curwin ); 7445redraw_later (curwin ,UPD_VALID ); 7446win_enter (curwin_save , true); 7447 } 7448g_do_tagpreview = 0 ; 7449} 7450 7451/// ":stag", ":stselect" and ":stjump". 7452static void ex_stag (exarg_T * eap ) 7453{ 7454postponed_split = -1 ; 7455postponed_split_flags = cmdmod .cmod_split ; 7456postponed_split_tab = cmdmod .cmod_tab ; 7457ex_tag_cmd (eap ,cmdnames [eap -> cmdidx ].cmd_name + 1 ); 7458postponed_split_flags = 0 ; 7459postponed_split_tab = 0 ; 7460} 7461 7462/// ":tag", ":tselect", ":tjump", ":tnext", etc. 7463static void ex_tag (exarg_T * eap ) 7464{ 7465ex_tag_cmd (eap ,cmdnames [eap -> cmdidx ].cmd_name ); 7466} 7467 7468static void ex_tag_cmd (exarg_T * eap ,const char * name ) 7469{ 7470int cmd ; 7471 7472switch (name [1 ]) { 7473case 'j' : 7474cmd = DT_JUMP ;// ":tjump" 7475break ; 7476case 's' : 7477cmd = DT_SELECT ;// ":tselect" 7478break ; 7479case 'p' :// ":tprevious" 7480case 'N' : 7481cmd = DT_PREV ;// ":tNext" 7482break ; 7483case 'n' : 7484cmd = DT_NEXT ;// ":tnext" 7485break ; 7486case 'o' : 7487cmd = DT_POP ;// ":pop" 7488break ; 7489case 'f' :// ":tfirst" 7490case 'r' : 7491cmd = DT_FIRST ;// ":trewind" 7492break ; 7493case 'l' : 7494cmd = DT_LAST ;// ":tlast" 7495break ; 7496default :// ":tag" 7497cmd = DT_TAG ; 7498break ; 7499 } 7500 7501if (name [0 ]== 'l' ) { 7502cmd = DT_LTAG ; 7503 } 7504 7505do_tag (eap ,eap -> arg ,cmd ,eap -> addr_count > 0 ? (int )eap -> line2 :1 ,eap -> forceit , true); 7506} 7507 7508enum { 7509SPEC_PERC = 0 , 7510SPEC_HASH , 7511SPEC_CWORD , 7512SPEC_CCWORD , 7513SPEC_CEXPR , 7514SPEC_CFILE , 7515SPEC_SFILE , 7516SPEC_SLNUM , 7517SPEC_STACK , 7518SPEC_SCRIPT , 7519SPEC_AFILE , 7520SPEC_ABUF , 7521SPEC_AMATCH , 7522SPEC_SFLNUM , 7523SPEC_SID , 7524// SPEC_CLIENT, 7525}; 7526 7527/// Check "str" for starting with a special cmdline variable. 7528/// If found return one of the SPEC_ values and set "*usedlen" to the length of 7529/// the variable. Otherwise return -1 and "*usedlen" is unchanged. 7530ssize_t find_cmdline_var (const char * src ,size_t * usedlen ) 7531FUNC_ATTR_NONNULL_ALL 7532{ 7533static char * (spec_str [])= { 7534 [SPEC_PERC ]= "%" , 7535 [SPEC_HASH ]= "#" , 7536 [SPEC_CWORD ]= "<cword>" ,// cursor word 7537 [SPEC_CCWORD ]= "<cWORD>" ,// cursor WORD 7538 [SPEC_CEXPR ]= "<cexpr>" ,// expr under cursor 7539 [SPEC_CFILE ]= "<cfile>" ,// cursor path name 7540 [SPEC_SFILE ]= "<sfile>" ,// ":so" file name 7541 [SPEC_SLNUM ]= "<slnum>" ,// ":so" file line number 7542 [SPEC_STACK ]= "<stack>" ,// call stack 7543 [SPEC_SCRIPT ]= "<script>" ,// script file name 7544 [SPEC_AFILE ]= "<afile>" ,// autocommand file name 7545 [SPEC_ABUF ]= "<abuf>" ,// autocommand buffer number 7546 [SPEC_AMATCH ]= "<amatch>" ,// autocommand match name 7547 [SPEC_SFLNUM ]= "<sflnum>" ,// script file line number 7548 [SPEC_SID ]= "<SID>" ,// script ID: <SNR>123_ 7549// [SPEC_CLIENT] = "<client>", 7550 }; 7551 7552for (size_t i = 0 ;i < ARRAY_SIZE (spec_str );i ++ ) { 7553size_t len = strlen (spec_str [i ]); 7554if (strncmp (src ,spec_str [i ],len )== 0 ) { 7555* usedlen = len ; 7556assert (i <=SSIZE_MAX ); 7557return (ssize_t )i ; 7558 } 7559 } 7560return -1 ; 7561} 7562 7563/// Evaluate cmdline variables. 7564/// 7565/// change "%" to curbuf->b_ffname 7566/// "#" to curwin->w_alt_fnum 7567/// "<cword>" to word under the cursor 7568/// "<cWORD>" to WORD under the cursor 7569/// "<cexpr>" to C-expression under the cursor 7570/// "<cfile>" to path name under the cursor 7571/// "<sfile>" to sourced file name 7572/// "<stack>" to call stack 7573/// "<script>" to current script name 7574/// "<slnum>" to sourced file line number 7575/// "<afile>" to file name for autocommand 7576/// "<abuf>" to buffer number for autocommand 7577/// "<amatch>" to matching name for autocommand 7578/// 7579/// When an error is detected, "errormsg" is set to a non-NULL pointer (may be 7580/// "" for error without a message) and NULL is returned. 7581/// 7582/// @param src pointer into commandline 7583/// @param srcstart beginning of valid memory for src 7584/// @param usedlen characters after src that are used 7585/// @param lnump line number for :e command, or NULL 7586/// @param errormsg pointer to error message 7587/// @param escaped return value has escaped white space (can be NULL) 7588/// @param empty_is_error empty result is considered an error 7589/// 7590/// @return an allocated string if a valid match was found. 7591/// Returns NULL if no match was found. "usedlen" then still contains the 7592/// number of characters to skip. 7593char * eval_vars (char * src ,const char * srcstart ,size_t * usedlen ,linenr_T * lnump , 7594const char ** errormsg ,int * escaped ,bool empty_is_error ) 7595{ 7596char * result = "" ; 7597char * resultbuf = NULL ; 7598size_t resultlen ; 7599int valid = VALID_HEAD |VALID_PATH ;// Assume valid result. 7600bool tilde_file = false; 7601bool skip_mod = false; 7602char strbuf [30 ]; 7603// we want 'shellslash' for path expansions like "%", "#", "<afile>", etc. 7604bool use_shellslash = false; 7605 7606* errormsg = NULL ; 7607if (escaped != NULL ) { 7608* escaped = false; 7609 } 7610 7611// Check if there is something to do. 7612ssize_t spec_idx = find_cmdline_var (src ,usedlen ); 7613if (spec_idx < 0 ) {// no match 7614* usedlen = 1 ; 7615return NULL ; 7616 } 7617 7618// Skip when preceded with a backslash "\%" and "\#". 7619// Note: In "\\%" the % is also not recognized! 7620if (src > srcstart && src [-1 ]== '\\' ) { 7621* usedlen = 0 ; 7622STRMOVE (src - 1 ,src );// remove backslash 7623return NULL ; 7624 } 7625 7626// word or WORD under cursor 7627if (spec_idx == SPEC_CWORD 7628|| spec_idx == SPEC_CCWORD 7629|| spec_idx == SPEC_CEXPR ) { 7630resultlen = find_ident_under_cursor (& result , 7631spec_idx == SPEC_CWORD 7632 ? (FIND_IDENT |FIND_STRING ) 7633 : (spec_idx == SPEC_CEXPR 7634 ? (FIND_IDENT |FIND_STRING |FIND_EVAL ) 7635 :FIND_STRING ),NULL ); 7636if (resultlen == 0 ) { 7637* errormsg = "" ; 7638return NULL ; 7639 } 7640// 7641// '#': Alternate file name 7642// '%': Current file name 7643// File name under the cursor 7644// File name for autocommand 7645// and following modifiers 7646// 7647 }else { 7648switch (spec_idx ) { 7649case SPEC_PERC :// '%': current file 7650if (curbuf -> b_fname == NULL ) { 7651result = "" ; 7652valid = 0 ;// Must have ":p:h" to be valid 7653 }else { 7654result = curbuf -> b_fname ; 7655tilde_file = strcmp (result ,"~" )== 0 ; 7656 } 7657use_shellslash = true; 7658break ; 7659 7660case SPEC_HASH :// '#' or "#99": alternate file 7661if (src [1 ]== '#' ) {// "##": the argument list 7662result = arg_all (); 7663resultbuf = result ; 7664* usedlen = 2 ; 7665if (escaped != NULL ) { 7666* escaped = true; 7667 } 7668skip_mod = true; 7669break ; 7670 } 7671use_shellslash = true; 7672char * s = src + 1 ; 7673if (* s == '<' ) {// "#<99" uses v:oldfiles. 7674s ++ ; 7675 } 7676int i = getdigits_int (& s , false,0 ); 7677if (s == src + 2 && src [1 ]== '-' ) { 7678// just a minus sign, don't skip over it 7679s -- ; 7680 } 7681* usedlen = (size_t )(s - src );// length of what we expand 7682 7683if (src [1 ]== '<' && i != 0 ) { 7684if (* usedlen < 2 ) { 7685// Should we give an error message for #<text? 7686* usedlen = 1 ; 7687return NULL ; 7688 } 7689result = (char * )tv_list_find_str (get_vim_var_list (VV_OLDFILES ),i - 1 ); 7690if (result == NULL ) { 7691* errormsg = "" ; 7692return NULL ; 7693 } 7694 }else { 7695if (i == 0 && src [1 ]== '<' && * usedlen > 1 ) { 7696* usedlen = 1 ; 7697 } 7698buf_T * buf = buflist_findnr (i ); 7699if (buf == NULL ) { 7700* errormsg = _ ("E194: No alternate file name to substitute for '#'" ); 7701return NULL ; 7702 } 7703if (lnump != NULL ) { 7704* lnump = ECMD_LAST ; 7705 } 7706if (buf -> b_fname == NULL ) { 7707result = "" ; 7708valid = 0 ;// Must have ":p:h" to be valid 7709 }else { 7710result = buf -> b_fname ; 7711tilde_file = strcmp (result ,"~" )== 0 ; 7712 } 7713 } 7714break ; 7715 7716case SPEC_CFILE :// file name under cursor 7717result = file_name_at_cursor (FNAME_MESS |FNAME_HYP ,1 ,NULL ); 7718if (result == NULL ) { 7719* errormsg = "" ; 7720return NULL ; 7721 } 7722resultbuf = result ;// remember allocated string 7723break ; 7724 7725case SPEC_AFILE :// file name for autocommand 7726use_shellslash = !autocmd_fname_full ; 7727if (autocmd_fname != NULL && !autocmd_fname_full ) { 7728// Still need to turn the fname into a full path. It was 7729// postponed to avoid a delay when <afile> is not used. 7730autocmd_fname_full = true; 7731result = FullName_save (autocmd_fname , false); 7732// Copy into `autocmd_fname`, don't reassign it. #8165 7733xstrlcpy (autocmd_fname ,result ,MAXPATHL ); 7734xfree (result ); 7735 } 7736result = autocmd_fname ; 7737if (result == NULL ) { 7738* errormsg = _ (e_no_autocommand_file_name_to_substitute_for_afile ); 7739return NULL ; 7740 } 7741result = path_try_shorten_fname (result ); 7742break ; 7743 7744case SPEC_ABUF :// buffer number for autocommand 7745if (autocmd_bufnr <=0 ) { 7746* errormsg = _ (e_no_autocommand_buffer_number_to_substitute_for_abuf ); 7747return NULL ; 7748 } 7749snprintf (strbuf ,sizeof (strbuf ),"%d" ,autocmd_bufnr ); 7750result = strbuf ; 7751break ; 7752 7753case SPEC_AMATCH :// match name for autocommand 7754result = autocmd_match ; 7755if (result == NULL ) { 7756* errormsg = _ (e_no_autocommand_match_name_to_substitute_for_amatch ); 7757return NULL ; 7758 } 7759break ; 7760 7761case SPEC_SFILE :// file name for ":so" command 7762use_shellslash = true; 7763result = estack_sfile (ESTACK_SFILE ); 7764if (result == NULL ) { 7765* errormsg = _ (e_no_source_file_name_to_substitute_for_sfile ); 7766return NULL ; 7767 } 7768resultbuf = result ;// remember allocated string 7769break ; 7770case SPEC_STACK :// call stack 7771use_shellslash = true; 7772result = estack_sfile (ESTACK_STACK ); 7773if (result == NULL ) { 7774* errormsg = _ (e_no_call_stack_to_substitute_for_stack ); 7775return NULL ; 7776 } 7777resultbuf = result ;// remember allocated string 7778break ; 7779case SPEC_SCRIPT :// script file name 7780use_shellslash = true; 7781result = estack_sfile (ESTACK_SCRIPT ); 7782if (result == NULL ) { 7783* errormsg = _ (e_no_script_file_name_to_substitute_for_script ); 7784return NULL ; 7785 } 7786resultbuf = result ;// remember allocated string 7787break ; 7788 7789case SPEC_SLNUM :// line in file for ":so" command 7790if (SOURCING_NAME == NULL || SOURCING_LNUM == 0 ) { 7791* errormsg = _ (e_no_line_number_to_use_for_slnum ); 7792return NULL ; 7793 } 7794snprintf (strbuf ,sizeof (strbuf ),"%" PRIdLINENR ,SOURCING_LNUM ); 7795result = strbuf ; 7796break ; 7797 7798case SPEC_SFLNUM :// line in script file 7799if (current_sctx .sc_lnum + SOURCING_LNUM == 0 ) { 7800* errormsg = _ (e_no_line_number_to_use_for_sflnum ); 7801return NULL ; 7802 } 7803snprintf (strbuf ,sizeof (strbuf ),"%" PRIdLINENR , 7804current_sctx .sc_lnum + SOURCING_LNUM ); 7805result = strbuf ; 7806break ; 7807 7808case SPEC_SID : 7809if (current_sctx .sc_sid <=0 ) { 7810* errormsg = _ (e_usingsid ); 7811return NULL ; 7812 } 7813snprintf (strbuf ,sizeof (strbuf ),"<SNR>%" PRIdSCID "_" ,current_sctx .sc_sid ); 7814result = strbuf ; 7815break ; 7816 7817default : 7818// should not happen 7819* errormsg = "" ; 7820break ; 7821 } 7822 7823// Length of new string. 7824resultlen = strlen (result ); 7825// Remove the file name extension. 7826if (src [* usedlen ]== '<' ) { 7827 (* usedlen )++ ; 7828char * s ; 7829if ((s = strrchr (result ,'.' ))!= NULL 7830&& s >=path_tail (result )) { 7831resultlen = (size_t )(s - result ); 7832 } 7833 }else if (!skip_mod ) { 7834valid |=modify_fname (src ,tilde_file ,usedlen ,& result , 7835& resultbuf ,& resultlen ,use_shellslash ); 7836if (result == NULL ) { 7837* errormsg = "" ; 7838return NULL ; 7839 } 7840 } 7841 } 7842 7843if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH ) { 7844if (empty_is_error ) { 7845if (valid != VALID_HEAD + VALID_PATH ) { 7846// xgettext:no-c-format 7847* errormsg = _ ("E499: Empty file name for '%' or '#', only works with \":p:h\"" ); 7848 }else { 7849* errormsg = _ ("E500: Evaluates to an empty string" ); 7850 } 7851 } 7852result = NULL ; 7853 }else { 7854result = xmemdupz (result ,resultlen ); 7855 } 7856xfree (resultbuf ); 7857return result ; 7858} 7859 7860/// Expand the <sfile> string in "arg". 7861/// 7862/// @return an allocated string, or NULL for any error. 7863char * expand_sfile (char * arg ) 7864{ 7865char * result = xstrdup (arg ); 7866 7867for (char * p = result ;* p ;) { 7868if (strncmp (p ,"<sfile>" ,7 )!= 0 ) { 7869p ++ ; 7870 }else { 7871// replace "<sfile>" with the sourced file name, and do ":" stuff 7872size_t srclen ; 7873const char * errormsg ; 7874char * repl = eval_vars (p ,result ,& srclen ,NULL ,& errormsg ,NULL , true); 7875if (errormsg != NULL ) { 7876if (* errormsg ) { 7877emsg (errormsg ); 7878 } 7879xfree (result ); 7880return NULL ; 7881 } 7882if (repl == NULL ) {// no match (cannot happen) 7883p += srclen ; 7884continue ; 7885 } 7886size_t len = strlen (result )- srclen + strlen (repl )+ 1 ; 7887char * newres = xmalloc (len ); 7888memmove (newres ,result , (size_t )(p - result )); 7889STRCPY (newres + (p - result ),repl ); 7890len = strlen (newres ); 7891strcat (newres ,p + srclen ); 7892xfree (repl ); 7893xfree (result ); 7894result = newres ; 7895p = newres + len ;// continue after the match 7896 } 7897 } 7898 7899return result ; 7900} 7901 7902/// ":rshada" and ":wshada". 7903static void ex_shada (exarg_T * eap ) 7904{ 7905char * save_shada = p_shada ; 7906if (* p_shada == NUL ) { 7907p_shada = "'100" ; 7908 } 7909if (eap -> cmdidx == CMD_rviminfo || eap -> cmdidx == CMD_rshada ) { 7910shada_read_everything (eap -> arg ,eap -> forceit , false); 7911 }else { 7912shada_write_file (eap -> arg ,eap -> forceit ); 7913 } 7914p_shada = save_shada ; 7915} 7916 7917static TriState filetype_detect = kNone ; 7918static TriState filetype_plugin = kNone ; 7919static TriState filetype_indent = kNone ; 7920 7921/// ":filetype [plugin] [indent] {on,off,detect}" 7922/// on: Load the filetype.vim file to install autocommands for file types. 7923/// off: Load the ftoff.vim file to remove all autocommands for file types. 7924/// plugin on: load filetype.vim and ftplugin.vim 7925/// plugin off: load ftplugof.vim 7926/// indent on: load filetype.vim and indent.vim 7927/// indent off: load indoff.vim 7928static void ex_filetype (exarg_T * eap ) 7929{ 7930if (* eap -> arg == NUL ) { 7931// Print current status. 7932smsg (0 ,"filetype detection:%s plugin:%s indent:%s" , 7933filetype_detect == kTrue ?"ON" :"OFF" , 7934filetype_plugin == kTrue ? (filetype_detect == kTrue ?"ON" :"(on)" ) :"OFF" , 7935filetype_indent == kTrue ? (filetype_detect == kTrue ?"ON" :"(on)" ) :"OFF" ); 7936return ; 7937 } 7938 7939char * arg = eap -> arg ; 7940bool plugin = false; 7941bool indent = false; 7942 7943// Accept "plugin" and "indent" in any order. 7944while (true) { 7945if (strncmp (arg ,"plugin" ,6 )== 0 ) { 7946plugin = true; 7947arg = skipwhite (arg + 6 ); 7948continue ; 7949 } 7950if (strncmp (arg ,"indent" ,6 )== 0 ) { 7951indent = true; 7952arg = skipwhite (arg + 6 ); 7953continue ; 7954 } 7955break ; 7956 } 7957if (strcmp (arg ,"on" )== 0 || strcmp (arg ,"detect" )== 0 ) { 7958if (* arg == 'o' || filetype_detect != kTrue ) { 7959source_runtime (FILETYPE_FILE ,DIP_ALL ); 7960filetype_detect = kTrue ; 7961if (plugin ) { 7962source_runtime (FTPLUGIN_FILE ,DIP_ALL ); 7963filetype_plugin = kTrue ; 7964 } 7965if (indent ) { 7966source_runtime (INDENT_FILE ,DIP_ALL ); 7967filetype_indent = kTrue ; 7968 } 7969 } 7970if (* arg == 'd' ) { 7971do_doautocmd ("filetypedetect BufRead" , true,NULL ); 7972do_modelines (0 ); 7973 } 7974 }else if (strcmp (arg ,"off" )== 0 ) { 7975if (plugin || indent ) { 7976if (plugin ) { 7977source_runtime (FTPLUGOF_FILE ,DIP_ALL ); 7978filetype_plugin = kFalse ; 7979 } 7980if (indent ) { 7981source_runtime (INDOFF_FILE ,DIP_ALL ); 7982filetype_indent = kFalse ; 7983 } 7984 }else { 7985source_runtime (FTOFF_FILE ,DIP_ALL ); 7986filetype_detect = kFalse ; 7987 } 7988 }else { 7989semsg (_ (e_invarg2 ),arg ); 7990 } 7991} 7992 7993/// Source ftplugin.vim and indent.vim to create the necessary FileType 7994/// autocommands. We do this separately from filetype.vim so that these 7995/// autocommands will always fire first (and thus can be overridden) while still 7996/// allowing general filetype detection to be disabled in the user's init file. 7997void filetype_plugin_enable (void ) 7998{ 7999if (filetype_plugin == kNone ) { 8000source_runtime (FTPLUGIN_FILE ,DIP_ALL ); 8001filetype_plugin = kTrue ; 8002 } 8003if (filetype_indent == kNone ) { 8004source_runtime (INDENT_FILE ,DIP_ALL ); 8005filetype_indent = kTrue ; 8006 } 8007} 8008 8009/// Enable filetype detection if the user did not explicitly disable it. 8010void filetype_maybe_enable (void ) 8011{ 8012if (filetype_detect == kNone ) { 8013// Normally .vim files are sourced before .lua files when both are 8014// supported, but we reverse the order here because we want the Lua 8015// autocommand to be defined first so that it runs first 8016source_runtime (FILETYPE_FILE ,DIP_ALL ); 8017filetype_detect = kTrue ; 8018 } 8019} 8020 8021/// ":setfiletype [FALLBACK] {name}" 8022static void ex_setfiletype (exarg_T * eap ) 8023{ 8024if (curbuf -> b_did_filetype ) { 8025return ; 8026 } 8027 8028char * arg = eap -> arg ; 8029if (strncmp (arg ,"FALLBACK " ,9 )== 0 ) { 8030arg += 9 ; 8031 } 8032 8033set_option_value_give_err (kOptFiletype ,CSTR_AS_OBJ (arg ),OPT_LOCAL ); 8034if (arg != eap -> arg ) { 8035curbuf -> b_did_filetype = false; 8036 } 8037} 8038 8039static void ex_digraphs (exarg_T * eap ) 8040{ 8041if (* eap -> arg != NUL ) { 8042putdigraph (eap -> arg ); 8043 }else { 8044listdigraphs (eap -> forceit ); 8045 } 8046} 8047 8048void set_no_hlsearch (bool flag ) 8049{ 8050Search .no_hlsearch = flag ; 8051set_vim_var_nr (VV_HLSEARCH , !Search .no_hlsearch && p_hls ); 8052} 8053 8054/// ":nohlsearch" 8055static void ex_nohlsearch (exarg_T * eap ) 8056{ 8057set_no_hlsearch (true); 8058redraw_all_later (UPD_SOME_VALID ); 8059} 8060 8061static void ex_fold (exarg_T * eap ) 8062{ 8063if (foldManualAllowed (true)) { 8064pos_T start = {eap -> line1 ,1 ,0 }; 8065pos_T end = {eap -> line2 ,1 ,0 }; 8066foldCreate (curwin ,start ,end ); 8067 } 8068} 8069 8070static void ex_foldopen (exarg_T * eap ) 8071{ 8072pos_T start = {eap -> line1 ,1 ,0 }; 8073pos_T end = {eap -> line2 ,1 ,0 }; 8074opFoldRange (start ,end ,eap -> cmdidx == CMD_foldopen ,eap -> forceit , false); 8075} 8076 8077static void ex_folddo (exarg_T * eap ) 8078{ 8079// First set the marks for all lines closed/open. 8080for (linenr_T lnum = eap -> line1 ;lnum <=eap -> line2 ;lnum ++ ) { 8081if (hasFolding (curwin ,lnum ,NULL ,NULL )== (eap -> cmdidx == CMD_folddoclosed )) { 8082ml_setmarked (lnum ); 8083 } 8084 } 8085 8086global_exe (eap -> arg );// Execute the command on the marked lines. 8087ml_clearmarked ();// clear rest of the marks 8088} 8089 8090/// @return true if the supplied Ex cmdidx is for a location list command 8091/// instead of a quickfix command. 8092bool is_loclist_cmd (int cmdidx ) 8093FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 8094{ 8095if (cmdidx < 0 || cmdidx >=CMD_SIZE ) { 8096return false; 8097 } 8098return cmdnames [cmdidx ].cmd_name [0 ]== 'l' ; 8099} 8100 8101/// ":checkhealth [plugins]" 8102static void ex_checkhealth (exarg_T * eap ) 8103{ 8104// Suppress the Lua error (E5108) so the VIMRUNTIME diagnostic is the primary error. 8105emsg_off ++ ; 8106bool ok = nlua_call_excmd ("vim.health" ,"_check" ,eap ,& cmdmod ,NULL ); 8107emsg_off -- ; 8108if (ok ) { 8109return ; 8110 } 8111 8112char * vimruntime_env = os_getenv_noalloc ("VIMRUNTIME" ); 8113if (!vimruntime_env ) { 8114emsg (_ ("E5009: $VIMRUNTIME is empty or unset" )); 8115 }else { 8116bool rtp_ok = NULL != strstr (p_rtp ,vimruntime_env ); 8117if (rtp_ok ) { 8118semsg (_ ("E5009: Invalid $VIMRUNTIME: %s" ),vimruntime_env ); 8119 }else { 8120emsg (_ ("E5009: Invalid 'runtimepath'" )); 8121 } 8122 } 8123} 8124 8125static void ex_terminal (exarg_T * eap ) 8126{ 8127const int scroll_save = msg_scroll ; 8128msg_scroll = false; 8129autowrite_all (); 8130msg_scroll = scroll_save ; 8131 8132if (* eap -> arg != NUL ) { 8133nlua_call_excmd ("vim._core.ex_cmd" ,"ex_terminal" ,eap ,& cmdmod ,NULL ); 8134 }else { 8135// No cmd given, run 'shell'. 8136if (* p_sh == NUL ) { 8137emsg (_ (e_shellempty )); 8138return ; 8139 } 8140// Tokenize 'shell' via shell_build_argv (handles quoting) and pass as arg2. 8141char ** argv = shell_build_argv (NULL ,NULL ); 8142typval_T shell_tv ; 8143tv_list_alloc_ret (& shell_tv ,0 ); 8144for (char ** p = argv ;* p ;p ++ ) { 8145tv_list_append_allocated_string (shell_tv .vval .v_list ,* p ); 8146 } 8147xfree (argv ); 8148nlua_call_excmd ("vim._core.ex_cmd" ,"ex_terminal" ,eap ,& cmdmod ,& shell_tv ); 8149tv_clear (& shell_tv ); 8150 } 8151} 8152 8153/// ":log {name}" 8154static void ex_log (exarg_T * eap ) 8155{ 8156nlua_call_excmd ("vim._core.ex_cmd" ,"ex_log" ,eap ,& cmdmod ,NULL ); 8157} 8158 8159/// ":lsp {subcmd} {clients}" 8160static void ex_lsp (exarg_T * eap ) 8161{ 8162nlua_call_excmd ("vim._core.ex_cmd" ,"ex_lsp" ,eap ,& cmdmod ,NULL ); 8163} 8164 8165/// ":packdel {name}" 8166static void ex_packdel (exarg_T * eap ) 8167{ 8168nlua_call_excmd ("vim._core.ex_cmd" ,"ex_packdel" ,eap ,& cmdmod ,NULL ); 8169} 8170 8171/// ":packupdate {name}" 8172static void ex_packupdate (exarg_T * eap ) 8173{ 8174nlua_call_excmd ("vim._core.ex_cmd" ,"ex_packupdate" ,eap ,& cmdmod ,NULL ); 8175} 8176 8177/// ":uptime" 8178static void ex_uptime (exarg_T * eap ) 8179{ 8180nlua_call_excmd ("vim._core.ex_cmd" ,"ex_uptime" ,eap ,& cmdmod ,NULL ); 8181} 8182 8183/// ":fclose" 8184static void ex_fclose (exarg_T * eap ) 8185{ 8186win_float_remove (eap -> forceit ,eap -> line1 ); 8187} 8188 8189void verify_command (char * cmd ) 8190{ 8191if (strcmp ("smile" ,cmd )!= 0 ) { 8192return ;// acceptable non-existing command 8193 } 8194int a = HLF_E ; 8195msg (" #xxn` #xnxx` ,+x@##@Mz;` .xxx" 8196"xxxxxxnz+, znnnnnnnnnnnnnnnn." ,a ); 8197msg (" n###z x####` :x##########W+` ,###" 8198"##########M; W################." ,a ); 8199msg (" n####; x####` `z##############W: ,###" 8200"############# W################." ,a ); 8201msg (" n####W. x####` ,W#################+ ,###" 8202"############## W################." ,a ); 8203msg (" n#####n x####` @################### ,###" 8204"##############i W################." ,a ); 8205msg (" n######i x####` .#########@W@########* ,###" 8206"##############W`W################." ,a ); 8207msg (" n######@. x####` x######W*. `;n#######: ,###" 8208"#x,,,,:*M######iW###@:,,,,,,,,,,,`" ,a ); 8209msg (" n#######n x####` *######+` :M#####M ,###" 8210"#n `x#####xW###@`" ,a ); 8211msg (" n########* x####``@####@; `x#####i ,###" 8212"#n ,#####@W###@`" ,a ); 8213msg (" n########@ x####`*#####i `M####M ,###" 8214"#n x#########@`" ,a ); 8215msg (" n######### x####`M####z :#####:,###" 8216"#n z#########@`" ,a ); 8217msg (" n#########* x####,#####. n####+,###" 8218"#n n#########@`" ,a ); 8219msg (" n####@####@, x####i####x ;####x,###" 8220"#n `W#####@####+++++++++++i" ,a ); 8221msg (" n####*#####M` x#########* `####@,###" 8222"#n i#####MW###############W" ,a ); 8223msg (" n####.######+ x####z####; W####,###" 8224"#n i@######W###############W" ,a ); 8225msg (" n####.`W#####: x####n####: M####:###" 8226"#@nnnnnW#######,W###############W" ,a ); 8227msg (" n####. :#####M`x####z####; W####,###" 8228"##############z W###############W" ,a ); 8229msg (" n####. #######x#########* `####W,###" 8230"#############W` W###############W" ,a ); 8231msg (" n####. `M#####W####i####x ;####x,###" 8232"############W, W####+**********i" ,a ); 8233msg (" n####. ,##########,#####. n####+,###" 8234"###########n. W###@`" ,a ); 8235msg (" n####. ##########`M####z :#####:,###" 8236"########Wz: W###@`" ,a ); 8237msg (" n####. x#########`*#####i `M####M ,###" 8238"#x.....` W###@`" ,a ); 8239msg (" n####. ,@########``@####@; `x#####i ,###" 8240"#n W###@`" ,a ); 8241msg (" n####. *########` *#####@+` ,M#####M ,###" 8242"#n W###@`" ,a ); 8243msg (" n####. x#######` x######W*. `;n######@: ,###" 8244"#n W###@,,,,,,,,,,,,`" ,a ); 8245msg (" n####. .@######` .#########@W@########* ,###" 8246"#n W################," ,a ); 8247msg (" n####. i######` @################### ,###" 8248"#n W################," ,a ); 8249msg (" n####. n#####` ,W#################+ ,###" 8250"#n W################," ,a ); 8251msg (" n####. .@####` .n##############W; ,###" 8252"#n W################," ,a ); 8253msg (" n####. i####` :x##########W+` ,###" 8254"#n W################," ,a ); 8255msg (" +nnnn` +nnn` ,+x@##@Mz;` .nnn" 8256"n+ zxxxxxxxxxxxxxxxx." ,a ); 8257msg (" " ,a ); 8258msg (" " 8259" ,+M@#Mi" ,a ); 8260msg (" " 8261" .z########" ,a ); 8262msg (" " 8263" i@#########i" ,a ); 8264msg (" " 8265" `############W`" ,a ); 8266msg (" " 8267" `n#############i" ,a ); 8268msg (" " 8269" `n##############n" ,a ); 8270msg (" `` " 8271" z###############@`" ,a ); 8272msg (" `W@z, " 8273" ##################," ,a ); 8274msg (" *#####` " 8275" i############@x@###i" ,a ); 8276msg (" ######M. " 8277" :#############n`,W##+" ,a ); 8278msg (" +######@: " 8279" .W#########M@##+ *##z" ,a ); 8280msg (" :#######@: " 8281" `x########@#x###* ,##n" ,a ); 8282msg (" `@#######@; " 8283" z#########M*@nW#i .##x" ,a ); 8284msg (" z########@i " 8285" *###########WM#@#, `##x" ,a ); 8286msg (" i##########+ " 8287" ;###########*n###@ `##x" ,a ); 8288msg (" `@#MM#######x, " 8289" ,@#########zM,`z##M `@#x" ,a ); 8290msg (" n##M#W#######n. " 8291" `.:i*+#zzzz##+i:.` ,W#########Wii,`n@#@` n@##n" ,a ); 8292msg (" ;###@#x#######n `,i" 8293"#nW@#####@@WWW@@####@Mzi. ,W##########@z.. ;zM#+i####z" ,a ); 8294msg (" x####nz######## .;#x@##" 8295"@Wn#*;,.` ``,:*#x@##M+, ;@########xz@WM+#` `n@#######" ,a ); 8296msg (" ,@####M########xi#@##@Mzi," 8297"` .+x###Mi:n##########Mz```.:i *@######*" ,a ); 8298msg (" *#####W#########ix+:` " 8299" :n#############z: `*.`M######i" ,a ); 8300msg (" i#W##nW@+@##@#M@; " 8301" ;W@@##########W, i`x@#####," ,a ); 8302msg (" `@@n@Wn#@iMW*#*: " 8303" `iz#z@######x. M######`" ,a ); 8304msg (" z##zM###x`*, .` " 8305" `iW#####W;:` +#####M" ,a ); 8306msg (" ,###nn##n` " 8307" ,#####x;` ,;@######" ,a ); 8308msg (" x###xz#. " 8309" in###+ `:######@." ,a ); 8310msg (" ;####n+ " 8311" `Mnx##xi` , zM#######" ,a ); 8312msg (" `W####+ " 8313"i. `.+x###@#. :n,z######:" ,a ); 8314msg (" z####@` ;" 8315"#: .ii@###@;.*M*z####@`" ,a ); 8316msg (" i####M ` `i@" 8317"#, :: +#n##@+@##W####n" ,a ); 8318msg (" :####x ,i. ##xzM###" 8319"@` i. .@@, .z####x#######*" ,a ); 8320msg (" ,###W; i##Wz########" 8321"# :## z##n ,@########x###:" ,a ); 8322msg (" n##n `W###########M" 8323"`;n, i#x ,###@i *W########W#@`" ,a ); 8324msg (" .@##+ `x###########@." 8325" z#+ .M#W``x#####n` `;#######@z#x" ,a ); 8326msg (" n###z :W############@ " 8327" z#* @##xM#######@n; `########nW+" ,a ); 8328msg (" ;####nW##############W " 8329":@#* `@#############* :########z@i`" ,a ); 8330msg (" M##################### " 8331"M##: @#############@: *W########M#" ,a ); 8332msg (" ;#####################i." 8333"##x` W#############W, :n########zx" ,a ); 8334msg (" x####################@.`" 8335"x; @#############z. .@########W#" ,a ); 8336msg (" ,######################` " 8337" W###############x*,` W######zM#i" ,a ); 8338msg (" #######################: " 8339" z##################@x+*#zzi `@#########." ,a ); 8340msg (" W########W#z#M#########; " 8341" *##########################z :@#######@`" ,a ); 8342msg (" `@#######x`;#z ,x#######; " 8343" z###########M###xnM@########* :M######@" ,a ); 8344msg (" i########, x#@` z######; " 8345" *##########i *#@` `+########+` n######." ,a ); 8346msg (" n#######@` M##, `W#####. " 8347" *#########z ###; z########M: :W####n" ,a ); 8348msg (" M#######M n##. x####x " 8349" `x########: z##+ M#########@; .n###+" ,a ); 8350msg (" W#######@` :#W `@####: " 8351" `@######W i### ;###########@. n##n" ,a ); 8352msg (" W########z` ,, .x####z " 8353" @######@` `W#; `W############* *###;" ,a ); 8354msg (" `@#########Mi,:*n@####W` " 8355" W#######* .. `n#############i i###x" ,a ); 8356msg (" .#####################z " 8357" `@#######@*` .x############n:` ;####." ,a ); 8358msg (" :####################x`,,` " 8359" `W#########@x#+#@#############i ,####:" ,a ); 8360msg (" ;###################x#@###x" 8361"i` *############################: `####i" ,a ); 8362msg (" i##################+#######" 8363"#M, x##########################@` W###i" ,a ); 8364msg (" *################@; @######" 8365"##@, .W#########################@ x###:" ,a ); 8366msg (" .+M#############z. M######" 8367"###x ,W########################@` ####." ,a ); 8368msg (" *M*;z@########x: :W#####" 8369"##i .M########################i i###:" ,a ); 8370msg (" *##@z;#@####x: :z###" 8371"@i `########################x .###;" ,a ); 8372msg (" *#####n;#@## ;##" 8373"* ,x#####################@` W##*" ,a ); 8374msg (" *#######n;* :M##" 8375"W*, *W####################` n##z" ,a ); 8376msg (" i########@. ,*n####" 8377"###M*` `###################M *##M" ,a ); 8378msg (" i########n `z#####@@" 8379"#####Wi ,M################; ,##@`" ,a ); 8380msg (" ;WMWW@###* .x##@ni.``" 8381".:+zW##z` `n##############z @##," ,a ); 8382msg (" .*++*i;;;. .M#@+` " 8383" .##n `x############x` n##i" ,a ); 8384msg (" :########* x#W, " 8385" *#+ *###########M` +##+" ,a ); 8386msg (" ,######### :#@: " 8387" ##: #nzzzzzzzzzz. :##x" ,a ); 8388msg (" .#####Wz+` ##+ " 8389" `MM` .znnnnnnnnn. `@#@`" ,a ); 8390msg (" `@@ni;*nMz` @W` " 8391" :#+ .x#######n x##," ,a ); 8392msg (" i;z@#####, .#* " 8393" z#: ;;;*zW##; ###i" ,a ); 8394msg (" z########: :#; " 8395" `Wx +###Wni;n. ;##z" ,a ); 8396msg (" n########W: .#* " 8397" ,#, ;#######@+ `@#M" ,a ); 8398msg (" .###########n;.MM " 8399" n* ;iM#######* x#@`" ,a ); 8400msg (" :#############@;; " 8401" .n` ,#W*iW#####W` +##," ,a ); 8402msg (" ,##############. " 8403" ix. `x###M;####### ,##i" ,a ); 8404msg (" .#############@` " 8405" x@n**#W######z;M###@. W##" ,a ); 8406msg (" .##############W: " 8407" .x############@*;zW#; z#x" ,a ); 8408msg (" ,###############@; " 8409" `##############@n*;. i#@" ,a ); 8410msg (" ,#################i " 8411" :n##############W` .##," ,a ); 8412msg (" ,###################` " 8413" .+W##########W, `##i" ,a ); 8414msg (" :###################@zi,` " 8415" ;zM@@@WMn*` @#z" ,a ); 8416msg (" :#######################@x+" 8417"*i;;:i#M, `` M#W" ,a ); 8418msg (" ;##########################" 8419"######@x. n##," ,a ); 8420msg (" i#####################@W@@@" 8421"@Wxz*:` *##+" ,a ); 8422msg (" *######################+```" 8423" :##M" ,a ); 8424msg (" ########################M; " 8425" `@##," ,a ); 8426msg (" z#########################x" 8427", z###" ,a ); 8428msg (" n##########################" 8429"#n: ;##W`" ,a ); 8430msg (" x##########################" 8431"###Mz#++##* `W##i" ,a ); 8432msg (" M##########################" 8433"##########@` ###x" ,a ); 8434msg (" W##########################" 8435"###########` .###," ,a ); 8436msg (" @##########################" 8437"##########M n##z" ,a ); 8438msg (" @##################z*i@WMMM" 8439"x#x@#####,. :##@." ,a ); 8440msg (" `#####################@xi` " 8441" `::,* x##+" ,a ); 8442msg (" .#####################@#M. " 8443" ;##@`" ,a ); 8444msg (" ,#####################:. " 8445" M##i" ,a ); 8446msg (" ;###################ni` " 8447" i##M" ,a ); 8448msg (" *#################W#` " 8449" `W##," ,a ); 8450msg (" z#################@Wx+. " 8451" +###" ,a ); 8452msg (" x######################z. " 8453" .@#@`" ,a ); 8454msg (" `@#######################@; " 8455" z##;" ,a ); 8456msg (" :##########################: " 8457" :##z" ,a ); 8458msg (" +#########################W# " 8459" M#W" ,a ); 8460msg (" W################@n+*i;:,` " 8461" +##," ,a ); 8462msg (" :##################WMxz+, " 8463" ,##i" ,a ); 8464msg (" n#######################W.., " 8465" W##" ,a ); 8466msg (" +#########################WW@+. .:. " 8467" z#x" ,a ); 8468msg (" `@#############################@@###: " 8469" *#W" ,a ); 8470msg (" #################################Wz: " 8471" :#@" ,a ); 8472msg (",@###############################i " 8473" .##" ,a ); 8474msg ("n@@@@@@@#########################+ " 8475" `##" ,a ); 8476msg ("` `.:.`.,:iii;;;;;;;;iii;;;:` `.`` " 8477" `nW" ,a ); 8478} 8479 8480/// Check if a command is a :map/:abbrev command. 8481bool is_map_cmd (cmdidx_T cmdidx ) 8482{ 8483if (IS_USER_CMDIDX (cmdidx )) { 8484return false; 8485 } 8486 8487ex_func_T func = cmdnames [cmdidx ].cmd_func ; 8488return func == ex_map // :map, :nmap, :noremap, etc. 8489|| func == ex_unmap // :unmap, :nunmap, etc. 8490|| func == ex_mapclear // :mapclear, :nmapclear, etc. 8491|| func == ex_abbreviate // :abbreviate, :iabbrev, etc. 8492|| func == ex_abclear ;// :abclear, :iabclear, etc. 8493}