robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1/// @file ex_cmds2.c 2/// 3/// Some more functions for command line commands 4 5#include <assert.h> 6#include <inttypes.h> 7#include <stdbool.h> 8#include <stdio.h> 9#include <string.h> 10 11#include "nvim/arglist.h" 12#include "nvim/ascii_defs.h" 13#include "nvim/autocmd.h" 14#include "nvim/autocmd_defs.h" 15#include "nvim/buffer.h" 16#include "nvim/buffer_defs.h" 17#include "nvim/bufwrite.h" 18#include "nvim/change.h" 19#include "nvim/channel.h" 20#include "nvim/context.h" 21#include "nvim/dialog.h" 22#include "nvim/errors.h" 23#include "nvim/eval.h" 24#include "nvim/eval/typval.h" 25#include "nvim/eval/typval_defs.h" 26#include "nvim/eval/vars.h" 27#include "nvim/ex_cmds.h" 28#include "nvim/ex_cmds2.h" 29#include "nvim/ex_cmds_defs.h" 30#include "nvim/ex_docmd.h" 31#include "nvim/ex_getln.h" 32#include "nvim/fileio.h" 33#include "nvim/gettext_defs.h" 34#include "nvim/globals.h" 35#include "nvim/highlight_defs.h" 36#include "nvim/macros_defs.h" 37#include "nvim/mark.h" 38#include "nvim/memline_defs.h" 39#include "nvim/memory.h" 40#include "nvim/message.h" 41#include "nvim/move.h" 42#include "nvim/normal.h" 43#include "nvim/option_vars.h" 44#include "nvim/os/os_defs.h" 45#include "nvim/path.h" 46#include "nvim/pos_defs.h" 47#include "nvim/quickfix.h" 48#include "nvim/runtime.h" 49#include "nvim/types_defs.h" 50#include "nvim/ui.h" 51#include "nvim/undo.h" 52#include "nvim/vim_defs.h" 53#include "nvim/window.h" 54 55#include "ex_cmds2.c.generated.h" 56 57static const char e_compiler_not_supported_str [] 58= N_ ("E666: Compiler not supported: %s" ); 59 60void ex_ruby (exarg_T * eap ) 61{ 62script_host_execute ("ruby" ,eap ); 63} 64 65void ex_rubyfile (exarg_T * eap ) 66{ 67script_host_execute_file ("ruby" ,eap ); 68} 69 70void ex_rubydo (exarg_T * eap ) 71{ 72script_host_do_range ("ruby" ,eap ); 73} 74 75void ex_python3 (exarg_T * eap ) 76{ 77script_host_execute ("python3" ,eap ); 78} 79 80void ex_py3file (exarg_T * eap ) 81{ 82script_host_execute_file ("python3" ,eap ); 83} 84 85void ex_pydo3 (exarg_T * eap ) 86{ 87script_host_do_range ("python3" ,eap ); 88} 89 90void ex_perl (exarg_T * eap ) 91{ 92script_host_execute ("perl" ,eap ); 93} 94 95void ex_perlfile (exarg_T * eap ) 96{ 97script_host_execute_file ("perl" ,eap ); 98} 99 100void ex_perldo (exarg_T * eap ) 101{ 102script_host_do_range ("perl" ,eap ); 103} 104 105/// If 'autowrite' option set, try to write the file. 106/// Careful: autocommands may make "buf" invalid! 107/// 108/// @return FAIL for failure, OK otherwise 109int autowrite (buf_T * buf ,bool forceit ) 110{ 111bufref_T bufref ; 112 113if (!(p_aw || p_awa )|| !p_write 114// never autowrite a "nofile" or "nowrite" buffer 115|| bt_dontwrite (buf ) 116|| (!forceit && buf -> b_p_ro )|| buf -> b_ffname == NULL ) { 117return FAIL ; 118 } 119set_bufref (& bufref ,buf ); 120int r = buf_write_all (buf ,forceit ); 121 122// Writing may succeed but the buffer still changed, e.g., when there is a 123// conversion error. We do want to return FAIL then. 124if (bufref_valid (& bufref )&& bufIsChanged (buf )) { 125r = FAIL ; 126 } 127return r ; 128} 129 130/// Flush all buffers, except the ones that are readonly or are never written. 131void autowrite_all (void ) 132{ 133if (!(p_aw || p_awa )|| !p_write ) { 134return ; 135 } 136 137FOR_ALL_BUFFERS (buf ) { 138if (bufIsChanged (buf )&& !buf -> b_p_ro && !bt_dontwrite (buf )) { 139bufref_T bufref ; 140set_bufref (& bufref ,buf ); 141buf_write_all (buf , false); 142// an autocommand may have deleted the buffer 143if (!bufref_valid (& bufref )) { 144buf = firstbuf ; 145 } 146 } 147 } 148} 149 150/// @return true if buffer was changed and cannot be abandoned. 151/// For flags use the CCGD_ values. 152bool check_changed (buf_T * buf ,int flags ) 153{ 154bool forceit = (flags & CCGD_FORCEIT ); 155bufref_T bufref ; 156set_bufref (& bufref ,buf ); 157 158if (!forceit 159&& bufIsChanged (buf ) 160&& ((flags & CCGD_MULTWIN )|| buf -> b_nwindows <=1 ) 161&& (!(flags & CCGD_AW )|| autowrite (buf ,forceit )== FAIL )) { 162if ((p_confirm || (cmdmod .cmod_flags & CMOD_CONFIRM ))&& p_write ) { 163int count = 0 ; 164 165if (flags & CCGD_ALLBUF ) { 166FOR_ALL_BUFFERS (buf2 ) { 167if (bufIsChanged (buf2 )&& (buf2 -> b_ffname != NULL )) { 168count ++ ; 169 } 170 } 171 } 172if (!bufref_valid (& bufref )) { 173// Autocommand deleted buffer, oops! It's not changed now. 174return false; 175 } 176dialog_changed (buf ,count > 1 ); 177if (!bufref_valid (& bufref )) { 178// Autocommand deleted buffer, oops! It's not changed now. 179return false; 180 } 181return bufIsChanged (buf ); 182 } 183if (flags & CCGD_EXCMD ) { 184no_write_message (); 185 }else { 186no_write_message_nobang (curbuf ); 187 } 188return true; 189 } 190return false; 191} 192 193/// Ask the user what to do when abandoning a changed buffer. 194/// Must check 'write' option first! 195/// 196/// @param buf 197/// @param checkall may abandon all changed buffers 198void dialog_changed (buf_T * buf ,bool checkall ) 199{ 200char buff [DIALOG_MSG_SIZE ]; 201int ret ; 202// Init ea pseudo-structure, this is needed for the check_overwrite() 203// function. 204exarg_T ea = { 205 .append = false, 206 .forceit = false, 207 }; 208 209const char * fname = buf -> b_fname ?buf -> b_fname :_ ("Untitled" ); 210snprintf (buff ,sizeof buff ,_ ("Save changes to \"%s\"?" ),fname ); 211if (checkall ) { 212ret = vim_dialog_yesnoallcancel (VIM_QUESTION ,NULL ,buff ,1 ); 213 }else { 214ret = vim_dialog_yesnocancel (VIM_QUESTION ,NULL ,buff ,1 ); 215 } 216 217if (ret == VIM_YES ) { 218bool empty_bufname = buf -> b_fname == NULL ; 219if (empty_bufname ) { 220buf_set_name (buf -> b_fnum ,"Untitled" ); 221 } 222 223if (check_overwrite (& ea ,buf ,buf -> b_fname ,buf -> b_ffname , false)== OK ) { 224// didn't hit Cancel 225if (buf_write_all (buf , false)== OK ) { 226return ; 227 } 228 } 229 230// restore to empty when write failed or was cancelled 231if (empty_bufname ) { 232buf -> b_fname = NULL ; 233XFREE_CLEAR (buf -> b_ffname ); 234XFREE_CLEAR (buf -> b_sfname ); 235 } 236 }else if (ret == VIM_NO ) { 237unchanged (buf , true, false); 238 }else if (ret == VIM_ALL ) { 239// Write all modified files that can be written. 240// Skip readonly buffers, these need to be confirmed 241// individually. 242FOR_ALL_BUFFERS (buf2 ) { 243if (bufIsChanged (buf2 )&& (buf2 -> b_ffname != NULL )&& !buf2 -> b_p_ro ) { 244bufref_T bufref ; 245set_bufref (& bufref ,buf2 ); 246 247if (buf2 -> b_fname != NULL 248&& check_overwrite (& ea ,buf2 ,buf2 -> b_fname ,buf2 -> b_ffname , false)== OK ) { 249// didn't hit Cancel 250buf_write_all (buf2 , false); 251 } 252// an autocommand may have deleted the buffer 253if (!bufref_valid (& bufref )) { 254buf2 = firstbuf ; 255 } 256 } 257 } 258 }else if (ret == VIM_DISCARDALL ) { 259// mark all buffers as unchanged 260FOR_ALL_BUFFERS (buf2 ) { 261unchanged (buf2 , true, false); 262 } 263 } 264} 265 266/// Ask the user whether to close the terminal buffer or not. 267/// 268/// @param buf The terminal buffer. 269/// @return bool Whether to close the buffer or not. 270bool dialog_close_terminal (buf_T * buf ) 271{ 272char buff [DIALOG_MSG_SIZE ]; 273 274snprintf (buff ,sizeof buff ,_ ("Close \"%s\"?" ), 275 (buf -> b_fname != NULL ) ?buf -> b_fname :"?" ); 276 277int ret = vim_dialog_yesnocancel (VIM_QUESTION ,NULL ,buff ,1 ); 278 279return ret == VIM_YES ; 280} 281 282/// @return true if the buffer "buf" can be abandoned, either by making it 283/// hidden, autowriting it or unloading it. 284bool can_abandon (buf_T * buf ,bool forceit ) 285{ 286return buf_hide (buf ) 287|| !bufIsChanged (buf ) 288|| buf -> b_nwindows > 1 289|| autowrite (buf ,forceit )== OK 290|| forceit ; 291} 292 293/// Add a buffer number to "bufnrs", unless it's already there. 294static void add_bufnum (int * bufnrs ,int * bufnump ,int nr ) 295{ 296for (int i = 0 ;i < * bufnump ;i ++ ) { 297if (bufnrs [i ]== nr ) { 298return ; 299 } 300 } 301bufnrs [* bufnump ]= nr ; 302* bufnump = * bufnump + 1 ; 303} 304 305/// Check if any buffer was changed and cannot be abandoned. 306/// That changed buffer becomes the current buffer. 307/// When "unload" is true the current buffer is unloaded instead of making it 308/// hidden. This is used for ":q!". 309/// 310/// @param[in] hidden specifies whether to check only hidden buffers. 311/// @param[in] unload specifies whether to unload, instead of hide, the buffer. 312/// 313/// @returns true if any buffer is changed and cannot be abandoned 314bool check_changed_any (bool hidden ,bool unload ) 315{ 316bool ret = false; 317int i ; 318int bufnum = 0 ; 319size_t bufcount = 0 ; 320 321// Make a list of all buffers, with the most important ones first. 322FOR_ALL_BUFFERS (buf ) { 323bufcount ++ ; 324 } 325 326if (bufcount == 0 ) { 327return false; 328 } 329 330int * bufnrs = xmalloc (sizeof (* bufnrs )* bufcount ); 331 332// curbuf 333bufnrs [bufnum ++ ]= curbuf -> b_fnum ; 334 335// buffers in current tab 336FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 337if (wp -> w_buffer != curbuf ) { 338add_bufnum (bufnrs ,& bufnum ,wp -> w_buffer -> b_fnum ); 339 } 340 } 341 342// buffers in other tabs 343FOR_ALL_TABS (tp ) { 344if (tp != curtab ) { 345FOR_ALL_WINDOWS_IN_TAB (wp ,tp ) { 346add_bufnum (bufnrs ,& bufnum ,wp -> w_buffer -> b_fnum ); 347 } 348 } 349 } 350 351// any other buffer 352FOR_ALL_BUFFERS (buf ) { 353add_bufnum (bufnrs ,& bufnum ,buf -> b_fnum ); 354 } 355 356buf_T * buf = NULL ; 357for (i = 0 ;i < bufnum ;i ++ ) { 358buf = buflist_findnr (bufnrs [i ]); 359if (buf == NULL ) { 360continue ; 361 } 362if ((!hidden || buf -> b_nwindows == 0 )&& bufIsChanged (buf )) { 363bufref_T bufref ; 364set_bufref (& bufref ,buf ); 365 366// Try auto-writing the buffer. If this fails but the buffer no 367// longer exists it's not changed, that's OK. 368if (check_changed (buf , (p_awa ?CCGD_AW :0 ) 369 |CCGD_MULTWIN 370 |CCGD_ALLBUF )&& bufref_valid (& bufref )) { 371break ;// didn't save - still changes 372 } 373 } 374 } 375 376if (i >=bufnum ) { 377 gototheend ; 378 } 379 380// Get here if "buf" cannot be abandoned. 381ret = true; 382exiting = false; 383// When ":confirm" used, don't give an error message. 384if (!(p_confirm || (cmdmod .cmod_flags & CMOD_CONFIRM ))) { 385// There must be a wait_return() for this message, do_buffer() 386// may cause a redraw. But wait_return() is a no-op when vgetc() 387// is busy (Quit used from window menu), then make sure we don't 388// cause a scroll up. 389if (vgetc_busy > 0 ) { 390msg_row = cmdline_row ; 391msg_col = 0 ; 392msg_didout = false; 393 } 394if (((buf -> terminal && channel_job_running ((uint64_t )buf -> b_p_channel )) 395 ?semsg (_ ("E947: Job still running in buffer \"%s\"" ),buf -> b_fname ) 396 :semsg (_ ("E162: No write since last change for buffer \"%s\"" ), 397buf_spname (buf )!= NULL ?buf_spname (buf ) :buf -> b_fname )) 398// Only makes sense if error is shown, which cause_errthrow() may prevent. 399&& msg_didany ) { 400int save = no_wait_return ; 401no_wait_return = false; 402if (!ui_has (kUIMessages )) { 403wait_return (false); 404 } 405no_wait_return = save ; 406 } 407 } 408 409// Try to find a window that contains the buffer. 410if (buf != curbuf ) { 411FOR_ALL_TAB_WINDOWS (tp ,wp ) { 412if (wp -> w_buffer == buf ) { 413bufref_T bufref ; 414set_bufref (& bufref ,buf ); 415goto_tabpage_win (tp ,wp ); 416// Paranoia: did autocmds wipe out the buffer with changes? 417if (!bufref_valid (& bufref )) { 418 gototheend ; 419 } 420 gotobuf_found ; 421 } 422 } 423 } 424buf_found : 425 426// Open the changed buffer in the current window. 427if (buf != curbuf ) { 428set_curbuf (buf ,unload ?DOBUF_UNLOAD :DOBUF_GOTO , true); 429 } 430 431theend : 432xfree (bufnrs ); 433return ret ; 434} 435 436/// @return FAIL if there is no file name, OK if there is one. 437/// Give error message for FAIL. 438int check_fname (void ) 439{ 440if (curbuf -> b_ffname == NULL ) { 441emsg (_ (e_noname )); 442return FAIL ; 443 } 444return OK ; 445} 446 447/// Flush the contents of a buffer, unless it has no file name. 448/// 449/// @return FAIL for failure, OK otherwise 450int buf_write_all (buf_T * buf ,bool forceit ) 451{ 452buf_T * old_curbuf = curbuf ; 453 454int retval = (buf_write (buf ,buf -> b_ffname ,buf -> b_fname , 4551 ,buf -> b_ml .ml_line_count ,NULL , 456 false,forceit , true, false)); 457if (curbuf != old_curbuf ) { 458msg_source (HLF_W ); 459msg (_ ("Warning: Entered other buffer unexpectedly (check autocommands)" ),0 ); 460 } 461return retval ; 462} 463 464/// ":argdo", ":windo", ":bufdo", ":tabdo", ":cdo", ":ldo", ":cfdo" and ":lfdo" 465void ex_listdo (exarg_T * eap ) 466{ 467// ":windo" and ":tabdo" only visit existing windows/tabpages, they don't 468// change the current window's buffer, so they can't escape a 'winfixbuf' 469// window (which would create a split). 470if (curwin -> w_p_wfb && eap -> cmdidx != CMD_windo && eap -> cmdidx != CMD_tabdo ) { 471if ((eap -> cmdidx == CMD_ldo || eap -> cmdidx == CMD_lfdo )&& !eap -> forceit ) { 472// Disallow :ldo if 'winfixbuf' is applied 473emsg (_ (e_winfixbuf_cannot_go_to_buffer )); 474return ; 475 } 476 477if (win_valid (prevwin )&& !prevwin -> w_p_wfb ) { 478// 'winfixbuf' is set; attempt to change to a window without it. 479win_goto (prevwin ); 480 } 481if (curwin -> w_p_wfb ) { 482// Split the window, which will be 'nowinfixbuf', and set curwin to that 483 (void )win_split (0 ,0 ); 484 485if (curwin -> w_p_wfb ) { 486// Autocommands set 'winfixbuf' or sent us to another window 487// with it set, or we failed to split the window. Give up. 488emsg (_ (e_winfixbuf_cannot_go_to_buffer )); 489return ; 490 } 491 } 492 } 493 494char * save_ei = NULL ; 495 496// Temporarily override kShmOver and kShmOverall to avoid that file 497// message overwrites output from the command. 498msg_listdo_overwrite ++ ; 499 500if (eap -> cmdidx != CMD_windo && eap -> cmdidx != CMD_tabdo ) { 501// Don't do syntax HL autocommands. Skipping the syntax file is a 502// great speed improvement. 503save_ei = au_event_disable (",Syntax" ); 504 505FOR_ALL_BUFFERS (buf ) { 506buf -> b_flags &= ~BF_SYN_SET ; 507 } 508 } 509 510if (eap -> cmdidx == CMD_windo 511|| eap -> cmdidx == CMD_tabdo 512|| buf_hide (curbuf ) 513|| !check_changed (curbuf ,CCGD_AW 514 | (eap -> forceit ?CCGD_FORCEIT :0 ) 515 |CCGD_EXCMD )) { 516int next_fnum = 0 ; 517int i = 0 ; 518// start at the eap->line1 argument/window/buffer 519win_T * wp = firstwin ; 520tabpage_T * tp = first_tabpage ; 521switch (eap -> cmdidx ) { 522case CMD_windo : 523for (;wp != NULL && i + 1 < eap -> line1 ;wp = wp -> w_next ) { 524i ++ ; 525 } 526break ; 527case CMD_tabdo : 528for (;tp != NULL && i + 1 < eap -> line1 ;tp = tp -> tp_next ) { 529i ++ ; 530 } 531break ; 532case CMD_argdo : 533i = (int )eap -> line1 - 1 ; 534break ; 535default : 536break ; 537 } 538 539buf_T * buf = curbuf ; 540size_t qf_size = 0 ; 541 542// set pcmark now 543if (eap -> cmdidx == CMD_bufdo ) { 544// Advance to the first listed buffer after "eap->line1". 545for (buf = firstbuf ; 546buf != NULL && (buf -> b_fnum < eap -> line1 || !buf -> b_p_bl ); 547buf = buf -> b_next ) { 548if (buf -> b_fnum > eap -> line2 ) { 549buf = NULL ; 550break ; 551 } 552 } 553if (buf != NULL ) { 554goto_buffer (eap ,DOBUF_FIRST ,FORWARD ,buf -> b_fnum ); 555 } 556 }else if (eap -> cmdidx == CMD_cdo || eap -> cmdidx == CMD_ldo 557|| eap -> cmdidx == CMD_cfdo || eap -> cmdidx == CMD_lfdo ) { 558qf_size = qf_get_valid_size (eap ); 559assert (eap -> line1 >=0 ); 560if (qf_size == 0 || (size_t )eap -> line1 > qf_size ) { 561buf = NULL ; 562 }else { 563ex_cc (eap ); 564 565buf = curbuf ; 566i = (int )eap -> line1 - 1 ; 567if (eap -> addr_count <=0 ) { 568// Default to all quickfix/location list entries. 569assert (qf_size < MAXLNUM ); 570eap -> line2 = (linenr_T )qf_size ; 571 } 572 } 573 }else { 574setpcmark (); 575 } 576listcmd_busy = true;// avoids setting pcmark below 577 578while (!got_int && buf != NULL ) { 579bool execute = true; 580if (eap -> cmdidx == CMD_argdo ) { 581// go to argument "i" 582if (i == ARGCOUNT ) { 583break ; 584 } 585// Don't call do_argfile() when already there, it will try 586// reloading the file. 587if (curwin -> w_arg_idx != i || !editing_arg_idx (curwin )) { 588// Clear 'shm' to avoid that the file message overwrites 589// any output from the command. 590do_argfile (eap ,i ); 591 } 592if (curwin -> w_arg_idx != i ) { 593break ; 594 } 595 }else if (eap -> cmdidx == CMD_windo ) { 596// go to window "wp" 597if (!win_valid (wp )) { 598break ; 599 } 600assert (wp ); 601execute = !wp -> w_floating || (!wp -> w_config .hide && wp -> w_config .focusable ); 602if (execute ) { 603win_goto (wp ); 604if (curwin != wp ) { 605break ;// something must be wrong 606 } 607 } 608wp = wp -> w_next ; 609 }else if (eap -> cmdidx == CMD_tabdo ) { 610// go to window "tp" 611if (!valid_tabpage (tp )) { 612break ; 613 } 614assert (tp ); 615goto_tabpage_tp (tp , true, true); 616tp = tp -> tp_next ; 617 }else if (eap -> cmdidx == CMD_bufdo ) { 618// Remember the number of the next listed buffer, in case 619// ":bwipe" is used or autocommands do something strange. 620next_fnum = -1 ; 621for (buf_T * bp = curbuf -> b_next ;bp != NULL ;bp = bp -> b_next ) { 622if (bp -> b_p_bl ) { 623next_fnum = bp -> b_fnum ; 624break ; 625 } 626 } 627 } 628 629i ++ ; 630// execute the command 631if (execute ) { 632do_cmdline (eap -> arg ,eap -> ea_getline ,eap -> cookie ,DOCMD_VERBOSE + DOCMD_NOWAIT ); 633 } 634 635if (eap -> cmdidx == CMD_bufdo ) { 636// Done? 637if (next_fnum < 0 || next_fnum > eap -> line2 ) { 638break ; 639 } 640 641// Check if the buffer still exists. 642bool buf_still_exists = false; 643FOR_ALL_BUFFERS (bp ) { 644if (bp -> b_fnum == next_fnum ) { 645buf_still_exists = true; 646break ; 647 } 648 } 649if (!buf_still_exists ) { 650break ; 651 } 652 653// Go to the next buffer. 654goto_buffer (eap ,DOBUF_FIRST ,FORWARD ,next_fnum ); 655 656// If autocommands took us elsewhere, quit here. 657if (curbuf -> b_fnum != next_fnum ) { 658break ; 659 } 660 } 661 662if (eap -> cmdidx == CMD_cdo || eap -> cmdidx == CMD_ldo 663|| eap -> cmdidx == CMD_cfdo || eap -> cmdidx == CMD_lfdo ) { 664assert (i >=0 ); 665if ((size_t )i >=qf_size || i >=eap -> line2 ) { 666break ; 667 } 668 669size_t qf_idx = qf_get_cur_idx (eap ); 670 671ex_cnext (eap ); 672 673// If jumping to the next quickfix entry fails, quit here. 674if (qf_get_cur_idx (eap )== qf_idx ) { 675break ; 676 } 677 } 678 679if (eap -> cmdidx == CMD_windo && execute ) { 680validate_cursor (curwin );// cursor may have moved 681// required when 'scrollbind' has been set 682if (curwin -> w_p_scb ) { 683do_check_scrollbind (true); 684 } 685 } 686if (eap -> cmdidx == CMD_windo || eap -> cmdidx == CMD_tabdo ) { 687if (i + 1 > eap -> line2 ) { 688break ; 689 } 690 } 691if (eap -> cmdidx == CMD_argdo && i >=eap -> line2 ) { 692break ; 693 } 694 } 695listcmd_busy = false; 696 } 697 698msg_listdo_overwrite -- ; 699if (save_ei != NULL ) { 700buf_T * bnext ; 701CtxSwitch aco = {0 }; 702 703au_event_restore (save_ei ); 704 705for (buf_T * buf = firstbuf ;buf != NULL ;buf = bnext ) { 706bnext = buf -> b_next ; 707if (buf -> b_nwindows > 0 && (buf -> b_flags & BF_SYN_SET )) { 708buf -> b_flags &= ~BF_SYN_SET ; 709 710// buffer was opened while Syntax autocommands were disabled, 711// need to trigger them now. 712if (buf == curbuf ) { 713apply_autocmds (EVENT_SYNTAX ,curbuf -> b_p_syn ,curbuf -> b_fname , true, 714curbuf ); 715 }else { 716ctx_switch (& aco ,NULL ,NULL ,buf ,0 ); 717apply_autocmds (EVENT_SYNTAX ,buf -> b_p_syn ,buf -> b_fname , true,buf ); 718ctx_restore (& aco ); 719 } 720 721// start over, in case autocommands messed things up. 722bnext = firstbuf ; 723 } 724 } 725 } 726} 727 728/// ":compiler[!] {name}" 729void ex_compiler (exarg_T * eap ) 730{ 731char * old_cur_comp = NULL ; 732 733if (* eap -> arg == NUL ) { 734// List all compiler scripts. 735do_cmdline_cmd ("echo globpath(&rtp, 'compiler/*.vim')" );// NOLINT 736do_cmdline_cmd ("echo globpath(&rtp, 'compiler/*.lua')" );// NOLINT 737return ; 738 } 739 740size_t bufsize = strlen (eap -> arg )+ 14 ; 741char * buf = xmalloc (bufsize ); 742 743if (eap -> forceit ) { 744// ":compiler! {name}" sets global options 745do_cmdline_cmd ("command -nargs=* -keepscript CompilerSet set <args>" ); 746 }else { 747// ":compiler! {name}" sets local options. 748// To remain backwards compatible "current_compiler" is always 749// used. A user's compiler plugin may set it, the distributed 750// plugin will then skip the settings. Afterwards set 751// "b:current_compiler" and restore "current_compiler". 752// Explicitly prepend "g:" to make it work in a function. 753old_cur_comp = get_var_value ("g:current_compiler" ); 754if (old_cur_comp != NULL ) { 755old_cur_comp = xstrdup (old_cur_comp ); 756 } 757do_cmdline_cmd ("command -nargs=* -keepscript CompilerSet setlocal <args>" ); 758 } 759do_unlet (S_LEN ("g:current_compiler" ), true); 760do_unlet (S_LEN ("b:current_compiler" ), true); 761 762snprintf (buf ,bufsize ,"compiler/%s.*" ,eap -> arg ); 763if (source_runtime_vim_lua (buf ,DIP_ALL )== FAIL ) { 764semsg (_ (e_compiler_not_supported_str ),eap -> arg ); 765 } 766xfree (buf ); 767 768do_cmdline_cmd (":delcommand CompilerSet" ); 769 770// Set "b:current_compiler" from "current_compiler". 771char * p = get_var_value ("g:current_compiler" ); 772if (p != NULL ) { 773set_internal_string_var ("b:current_compiler" ,p ); 774 } 775 776// Restore "current_compiler" for ":compiler {name}". 777if (!eap -> forceit ) { 778if (old_cur_comp != NULL ) { 779set_internal_string_var ("g:current_compiler" ,old_cur_comp ); 780xfree (old_cur_comp ); 781 }else { 782do_unlet (S_LEN ("g:current_compiler" ), true); 783 } 784 } 785} 786 787/// ":checktime [buffer]" 788void ex_checktime (exarg_T * eap ) 789{ 790int save_no_check_timestamps = no_check_timestamps ; 791 792no_check_timestamps = 0 ; 793if (eap -> addr_count == 0 ) {// default is all buffers 794check_timestamps (false); 795 }else { 796buf_T * buf = buflist_findnr ((int )eap -> line2 ); 797if (buf != NULL ) {// cannot happen? 798buf_check_timestamp (buf ); 799 } 800 } 801no_check_timestamps = save_no_check_timestamps ; 802} 803 804static void script_host_execute (char * name ,exarg_T * eap ) 805{ 806size_t len ; 807char * const script = script_get (eap ,& len ); 808 809if (script != NULL ) { 810list_T * const args = tv_list_alloc (3 ); 811// script 812tv_list_append_allocated_string (args ,script ); 813// current range 814tv_list_append_number (args , (int )eap -> line1 ); 815tv_list_append_number (args , (int )eap -> line2 ); 816 817eval_call_provider (name ,"execute" ,args , true); 818 } 819} 820 821static void script_host_execute_file (char * name ,exarg_T * eap ) 822{ 823if (!eap -> skip ) { 824uint8_t buffer [MAXPATHL ]; 825vim_FullName (eap -> arg , (char * )buffer ,sizeof (buffer ), false); 826 827list_T * args = tv_list_alloc (3 ); 828// filename 829tv_list_append_string (args , (const char * )buffer ,-1 ); 830// current range 831tv_list_append_number (args , (int )eap -> line1 ); 832tv_list_append_number (args , (int )eap -> line2 ); 833eval_call_provider (name ,"execute_file" ,args , true); 834 } 835} 836 837static void script_host_do_range (char * name ,exarg_T * eap ) 838{ 839if (!eap -> skip ) { 840list_T * args = tv_list_alloc (3 ); 841tv_list_append_number (args , (int )eap -> line1 ); 842tv_list_append_number (args , (int )eap -> line2 ); 843tv_list_append_string (args ,eap -> arg ,-1 ); 844eval_call_provider (name ,"do_range" ,args , true); 845 } 846} 847 848/// ":drop" 849/// Opens the first argument in a window, and the argument list is redefined. 850void ex_drop (exarg_T * eap ) 851{ 852bool split = false; 853 854// Check if the first argument is already being edited in a window. If 855// so, jump to that window. 856// We would actually need to check all arguments, but that's complicated 857// and mostly only one file is dropped. 858// This also ignores wildcards, since it is very unlikely the user is 859// editing a file name with a wildcard character. 860set_arglist (eap -> arg ); 861 862// Expanding wildcards may result in an empty argument list. E.g. when 863// editing "foo.pyc" and ".pyc" is in 'wildignore'. Assume that we 864// already did an error message for this. 865if (ARGCOUNT == 0 ) { 866return ; 867 } 868 869if (cmdmod .cmod_tab ) { 870// ":tab drop file ...": open a tab for each argument that isn't 871// edited in a window yet. It's like ":tab all" but without closing 872// windows or tabs. 873ex_all (eap ); 874cmdmod .cmod_tab = 0 ; 875ex_rewind (eap ); 876return ; 877 } 878 879// ":drop file ...": Edit the first argument. Jump to an existing 880// window if possible, edit in current window if the current buffer 881// can be abandoned, otherwise open a new window. 882buf_T * buf = buflist_findnr (ARGLIST [0 ].ae_fnum ); 883 884FOR_ALL_TAB_WINDOWS (tp ,wp ) { 885if (wp -> w_buffer == buf ) { 886goto_tabpage_win (tp ,wp ); 887curwin -> w_arg_idx = 0 ; 888if (!bufIsChanged (curbuf )) { 889const int save_ar = curbuf -> b_p_ar ; 890 891// reload the file if it is newer 892curbuf -> b_p_ar = true; 893buf_check_timestamp (curbuf ); 894curbuf -> b_p_ar = save_ar ; 895 } 896if (curbuf -> b_ml .ml_flags & ML_EMPTY ) { 897ex_rewind (eap ); 898 } 899 900// execute [+cmd] 901if (eap -> do_ecmd_cmd ) { 902bool did_set_swapcommand = set_swapcommand (eap -> do_ecmd_cmd ,0 ); 903do_cmdline (eap -> do_ecmd_cmd ,NULL ,NULL ,DOCMD_VERBOSE ); 904if (did_set_swapcommand ) { 905set_vim_var_string (VV_SWAPCOMMAND ,NULL ,-1 ); 906 } 907 } 908 909// no need to execute [++opts] - they only apply for newly loaded buffers. 910return ; 911 } 912 } 913 914// Check whether the current buffer is changed. If so, we will need 915// to split the current window or data could be lost. 916// Skip the check if the 'hidden' option is set, as in this case the 917// buffer won't be lost. 918if (!buf_hide (curbuf )) { 919emsg_off ++ ; 920split = check_changed (curbuf ,CCGD_AW |CCGD_EXCMD ); 921emsg_off -- ; 922 } 923 924// Fake a ":sfirst" or ":first" command edit the first argument. 925if (split ) { 926eap -> cmdidx = CMD_sfirst ; 927eap -> cmd [0 ]= 's' ; 928 }else { 929eap -> cmdidx = CMD_first ; 930 } 931ex_rewind (eap ); 932}