robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8a0eb8f10b
master
1// arglist.c: functions for dealing with the argument list 2 3#include <assert.h> 4#include <stdbool.h> 5#include <stdint.h> 6#include <string.h> 7 8#include "auto/config.h" 9#include "nvim/arglist.h" 10#include "nvim/ascii_defs.h" 11#include "nvim/autocmd.h" 12#include "nvim/buffer.h" 13#include "nvim/buffer_defs.h" 14#include "nvim/charset.h" 15#include "nvim/cmdexpand_defs.h" 16#include "nvim/context.h" 17#include "nvim/errors.h" 18#include "nvim/eval/typval.h" 19#include "nvim/eval/typval_defs.h" 20#include "nvim/eval/window.h" 21#include "nvim/ex_cmds.h" 22#include "nvim/ex_cmds2.h" 23#include "nvim/ex_cmds_defs.h" 24#include "nvim/ex_getln.h" 25#include "nvim/fileio.h" 26#include "nvim/garray.h" 27#include "nvim/garray_defs.h" 28#include "nvim/gettext_defs.h" 29#include "nvim/globals.h" 30#include "nvim/macros_defs.h" 31#include "nvim/mark.h" 32#include "nvim/memline_defs.h" 33#include "nvim/memory.h" 34#include "nvim/message.h" 35#include "nvim/normal.h" 36#include "nvim/option.h" 37#include "nvim/option_vars.h" 38#include "nvim/os/input.h" 39#include "nvim/path.h" 40#include "nvim/pos_defs.h" 41#include "nvim/regexp.h" 42#include "nvim/regexp_defs.h" 43#include "nvim/types_defs.h" 44#include "nvim/undo.h" 45#include "nvim/version.h" 46#include "nvim/vim_defs.h" 47#include "nvim/window.h" 48 49/// State used by the :all command to open all the files in the argument list in 50/// separate windows. 51typedef struct { 52alist_T * alist ;///< argument list to be used 53int had_tab ; 54bool keep_tabs ; 55bool forceit ; 56 57bool use_firstwin ;///< use first window for arglist 58uint8_t * opened ;///< Array of weight for which args are open: 59///< 0: not opened 60///< 1: opened in other tab 61///< 2: opened in curtab 62///< 3: opened in curtab and curwin 63int opened_len ;///< length of opened[] 64win_T * new_curwin ; 65tabpage_T * new_curtab ; 66}arg_all_state_T ; 67 68#include "arglist.c.generated.h" 69 70static const char e_window_layout_changed_unexpectedly [] 71= N_ ("E249: Window layout changed unexpectedly" ); 72 73enum { 74AL_SET = 1 , 75AL_ADD = 2 , 76AL_DEL = 3 , 77}; 78 79/// This flag is set whenever the argument list is being changed and calling a 80/// function that might trigger an autocommand. 81static bool arglist_locked = false; 82 83static int check_arglist_locked (void ) 84{ 85if (arglist_locked ) { 86emsg (_ (e_cannot_change_arglist_recursively )); 87return FAIL ; 88 } 89return OK ; 90} 91 92/// Clear an argument list: free all file names and reset it to zero entries. 93void alist_clear (alist_T * al ) 94{ 95if (check_arglist_locked ()== FAIL ) { 96return ; 97 } 98#define FREE_AENTRY_FNAME (arg ) xfree((arg)->ae_fname) 99GA_DEEP_CLEAR (& al -> al_ga ,aentry_T ,FREE_AENTRY_FNAME ); 100} 101 102/// Init an argument list. 103void alist_init (alist_T * al ) 104{ 105ga_init (& al -> al_ga , (int )sizeof (aentry_T ),5 ); 106} 107 108/// Remove a reference from an argument list. 109/// Ignored when the argument list is the global one. 110/// If the argument list is no longer used by any window, free it. 111void alist_unlink (alist_T * al ) 112{ 113if (al != & global_alist && -- al -> al_refcount <=0 ) { 114alist_clear (al ); 115xfree (al ); 116 } 117} 118 119/// Create a new argument list and use it for the current window. 120void alist_new (void ) 121{ 122curwin -> w_alist = xmalloc (sizeof (* curwin -> w_alist )); 123curwin -> w_alist -> al_refcount = 1 ; 124curwin -> w_alist -> id = ++ max_alist_id ; 125alist_init (curwin -> w_alist ); 126} 127 128#ifndef UNIX 129 130/// Expand the file names in the global argument list. 131/// If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer 132/// numbers to be re-used. 133void alist_expand (int * fnum_list ,int fnum_len ) 134{ 135char * save_p_su = p_su ; 136 137char ** old_arg_files = xmalloc (sizeof (* old_arg_files )* GARGCOUNT ); 138 139// Don't use 'suffixes' here. This should work like the shell did the 140// expansion. Also, the vimrc file isn't read yet, thus the user 141// can't set the options. 142p_su = empty_string_option ; 143for (int i = 0 ;i < GARGCOUNT ;i ++ ) { 144old_arg_files [i ]= xstrdup (GARGLIST [i ].ae_fname ); 145 } 146int old_arg_count = GARGCOUNT ; 147char ** new_arg_files ; 148int new_arg_file_count ; 149if (expand_wildcards (old_arg_count ,old_arg_files , 150& new_arg_file_count ,& new_arg_files , 151EW_FILE |EW_NOTFOUND |EW_ADDSLASH |EW_NOERROR )== OK 152&& new_arg_file_count > 0 ) { 153alist_set (& global_alist ,new_arg_file_count ,new_arg_files , 154 true,fnum_list ,fnum_len ); 155FreeWild (old_arg_count ,old_arg_files ); 156 } 157p_su = save_p_su ; 158} 159#endif 160 161/// Set the argument list for the current window. 162/// Takes over the allocated files[] and the allocated fnames in it. 163void alist_set (alist_T * al ,int count ,char ** files ,int use_curbuf ,int * fnum_list ,int fnum_len ) 164{ 165if (check_arglist_locked ()== FAIL ) { 166return ; 167 } 168 169alist_clear (al ); 170ga_grow (& al -> al_ga ,count ); 171 { 172for (int i = 0 ;i < count ;i ++ ) { 173if (got_int ) { 174// When adding many buffers this can take a long time. Allow 175// interrupting here. 176while (i < count ) { 177xfree (files [i ++ ]); 178 } 179break ; 180 } 181 182// May set buffer name of a buffer previously used for the 183// argument list, so that it's re-used by alist_add. 184if (fnum_list != NULL && i < fnum_len ) { 185arglist_locked = true; 186buf_set_name (fnum_list [i ],files [i ]); 187arglist_locked = false; 188 } 189 190alist_add (al ,files [i ],use_curbuf ?2 :1 ); 191os_breakcheck (); 192 } 193xfree (files ); 194 } 195 196if (al == & global_alist ) { 197arg_had_last = false; 198 } 199} 200 201/// Add file "fname" to argument list "al". 202/// "fname" must have been allocated and "al" must have been checked for room. 203/// 204/// May trigger Buf* autocommands 205/// 206/// @param set_fnum 1: set buffer number; 2: re-use curbuf 207void alist_add (alist_T * al ,char * fname ,int set_fnum ) 208{ 209win_T * wp = curwin ; 210 211if (fname == NULL ) {// don't add NULL file names 212return ; 213 } 214if (check_arglist_locked ()== FAIL ) { 215return ; 216 } 217arglist_locked = true; 218wp -> w_locked ++ ; 219 220AARGLIST (al )[al -> al_ga .ga_len ].ae_fname = fname ; 221if (set_fnum > 0 ) { 222AARGLIST (al )[al -> al_ga .ga_len ].ae_fnum = 223buflist_add (fname ,BLN_LISTED | (set_fnum == 2 ?BLN_CURBUF :0 )); 224 } 225al -> al_ga .ga_len ++ ; 226 227arglist_locked = false; 228wp -> w_locked -- ; 229} 230 231/// Isolate one argument, taking backticks. 232/// Changes the argument in-place, puts a NUL after it. Backticks remain. 233/// 234/// @return a pointer to the start of the next argument. 235static char * do_one_arg (char * str ) 236{ 237char * p ; 238 239bool inbacktick = false; 240for (p = str ;* str ;str ++ ) { 241// When the backslash is used for escaping the special meaning of a 242// character we need to keep it until wildcard expansion. 243if (rem_backslash (str )) { 244* p ++ = * str ++ ; 245* p ++ = * str ; 246 }else { 247// An item ends at a space not in backticks 248if (!inbacktick && ascii_isspace (* str )) { 249break ; 250 } 251if (* str == '`' ) { 252inbacktick ^= true; 253 } 254* p ++ = * str ; 255 } 256 } 257str = skipwhite (str ); 258* p = NUL ; 259 260return str ; 261} 262 263/// Separate the arguments in "str" and return a list of pointers in the 264/// growarray "gap". 265static void get_arglist (garray_T * gap ,char * str ,bool escaped ) 266{ 267ga_init (gap , (int )sizeof (char * ),20 ); 268while (* str != NUL ) { 269GA_APPEND (char * ,gap ,str ); 270 271// If str is escaped, don't handle backslashes or spaces 272if (!escaped ) { 273return ; 274 } 275 276// Isolate one argument, change it in-place, put a NUL after it. 277str = do_one_arg (str ); 278 } 279} 280 281/// Parse a list of arguments (file names), expand them and return in 282/// "fnames[fcountp]". When "wig" is true, removes files matching 'wildignore'. 283/// 284/// @return FAIL or OK. 285int get_arglist_exp (char * str ,int * fcountp ,char *** fnamesp ,bool wig ) 286{ 287garray_T ga ; 288int i ; 289 290get_arglist (& ga ,str , true); 291 292if (wig ) { 293i = expand_wildcards (ga .ga_len ,ga .ga_data , 294fcountp ,fnamesp ,EW_FILE |EW_NOTFOUND |EW_NOTWILD ); 295 }else { 296i = gen_expand_wildcards (ga .ga_len ,ga .ga_data , 297fcountp ,fnamesp ,EW_FILE |EW_NOTFOUND |EW_NOTWILD ); 298 } 299 300ga_clear (& ga ); 301return i ; 302} 303 304/// Check the validity of the arg_idx for each other window. 305static void alist_check_arg_idx (void ) 306{ 307FOR_ALL_TAB_WINDOWS (tp ,win ) { 308if (win -> w_alist == curwin -> w_alist ) { 309check_arg_idx (win ); 310 } 311 } 312} 313 314/// Add files[count] to the arglist of the current window after arg "after". 315/// The file names in files[count] must have been allocated and are taken over. 316/// Files[] itself is not taken over. 317/// 318/// @param after: where to add: 0 = before first one 319/// @param will_edit will edit adding argument 320static void alist_add_list (int count ,char ** files ,int after ,bool will_edit ) 321FUNC_ATTR_NONNULL_ALL 322{ 323int old_argcount = ARGCOUNT ; 324 325if (check_arglist_locked ()!= FAIL ) { 326win_T * wp = curwin ; 327 328ga_grow (& ALIST (wp )-> al_ga ,count ); 329after = MIN (MAX (after ,0 ),ARGCOUNT ); 330if (after < ARGCOUNT ) { 331memmove (& (ARGLIST [after + count ]),& (ARGLIST [after ]), 332 (size_t )(ARGCOUNT - after )* sizeof (aentry_T )); 333 } 334arglist_locked = true; 335wp -> w_locked ++ ; 336for (int i = 0 ;i < count ;i ++ ) { 337const int flags = BLN_LISTED | (will_edit ?BLN_CURBUF :0 ); 338ARGLIST [after + i ].ae_fname = files [i ]; 339ARGLIST [after + i ].ae_fnum = buflist_add (files [i ],flags ); 340 } 341arglist_locked = false; 342wp -> w_locked -- ; 343ALIST (wp )-> al_ga .ga_len += count ; 344if (old_argcount > 0 && wp -> w_arg_idx >=after ) { 345wp -> w_arg_idx += count ; 346 } 347return ; 348 } 349} 350 351/// Delete the file names in "alist_ga" from the argument list. 352static void arglist_del_files (garray_T * alist_ga ) 353{ 354regmatch_T regmatch ; 355 356// Delete the items: use each item as a regexp and find a match in the 357// argument list. 358regmatch .rm_ic = p_fic ;// ignore case when 'fileignorecase' is set 359for (int i = 0 ;i < alist_ga -> ga_len && !got_int ;i ++ ) { 360char * p = ((char ** )alist_ga -> ga_data )[i ]; 361p = file_pat_to_reg_pat (p ,NULL ,NULL , false); 362if (p == NULL ) { 363break ; 364 } 365regmatch .regprog = vim_regcomp (p ,p_magic ?RE_MAGIC :0 ); 366if (regmatch .regprog == NULL ) { 367xfree (p ); 368break ; 369 } 370 371bool didone = false; 372for (int match = 0 ;match < ARGCOUNT ;match ++ ) { 373if (vim_regexec (& regmatch ,alist_name (& ARGLIST [match ]),0 )) { 374didone = true; 375xfree (ARGLIST [match ].ae_fname ); 376memmove (ARGLIST + match ,ARGLIST + match + 1 , 377 (size_t )(ARGCOUNT - match - 1 )* sizeof (aentry_T )); 378ARGCOUNT -- ; 379if (curwin -> w_arg_idx > match ) { 380curwin -> w_arg_idx -- ; 381 } 382match -- ; 383 } 384 } 385 386vim_regfree (regmatch .regprog ); 387xfree (p ); 388if (!didone ) { 389semsg (_ (e_nomatch2 ), ((char ** )alist_ga -> ga_data )[i ]); 390 } 391 } 392ga_clear (alist_ga ); 393} 394 395/// @param str 396/// @param what 397/// AL_SET: Redefine the argument list to 'str'. 398/// AL_ADD: add files in 'str' to the argument list after "after". 399/// AL_DEL: remove files in 'str' from the argument list. 400/// @param after 401/// 0 means before first one 402/// @param will_edit will edit added argument 403/// 404/// @return FAIL for failure, OK otherwise. 405static int do_arglist (char * str ,int what ,int after ,bool will_edit ) 406FUNC_ATTR_NONNULL_ALL 407{ 408garray_T new_ga ; 409int exp_count ; 410char ** exp_files ; 411bool arg_escaped = true; 412 413if (check_arglist_locked ()== FAIL ) { 414return FAIL ; 415 } 416 417// Set default argument for ":argadd" command. 418if (what == AL_ADD && * str == NUL ) { 419if (curbuf -> b_ffname == NULL ) { 420return FAIL ; 421 } 422str = curbuf -> b_fname ; 423arg_escaped = false; 424 } 425 426// Collect all file name arguments in "new_ga". 427get_arglist (& new_ga ,str ,arg_escaped ); 428 429if (what == AL_DEL ) { 430arglist_del_files (& new_ga ); 431 }else { 432int i = expand_wildcards (new_ga .ga_len ,new_ga .ga_data , 433& exp_count ,& exp_files , 434EW_DIR |EW_FILE |EW_ADDSLASH |EW_NOTFOUND ); 435ga_clear (& new_ga ); 436if (i == FAIL || exp_count == 0 ) { 437emsg (_ (e_nomatch )); 438return FAIL ; 439 } 440 441if (what == AL_ADD ) { 442alist_add_list (exp_count ,exp_files ,after ,will_edit ); 443xfree (exp_files ); 444 }else { 445assert (what == AL_SET ); 446alist_set (ALIST (curwin ),exp_count ,exp_files ,will_edit ,NULL ,0 ); 447 } 448 } 449 450alist_check_arg_idx (); 451 452return OK ; 453} 454 455/// Redefine the argument list. 456void set_arglist (char * str ) 457{ 458do_arglist (str ,AL_SET ,0 , true); 459} 460 461/// @return true if window "win" is editing the file at the current argument 462/// index. 463bool editing_arg_idx (win_T * win ) 464{ 465return !(win -> w_arg_idx >=WARGCOUNT (win ) 466|| (win -> w_buffer -> b_fnum 467!= WARGLIST (win )[win -> w_arg_idx ].ae_fnum 468&& (win -> w_buffer -> b_ffname == NULL 469|| !path_equal (alist_name (& WARGLIST (win )[win -> w_arg_idx ]), 470win -> w_buffer -> b_ffname ,kPathCmpExpand |kPathCmpFull )))); 471} 472 473/// Check if window "win" is editing the w_arg_idx file in its argument list. 474void check_arg_idx (win_T * win ) 475{ 476if (WARGCOUNT (win )> 1 && !editing_arg_idx (win )) { 477// We are not editing the current entry in the argument list. 478// Set "arg_had_last" if we are editing the last one. 479win -> w_arg_idx_invalid = true; 480if (win -> w_arg_idx != WARGCOUNT (win )- 1 481&& arg_had_last == false 482&& ALIST (win )== & global_alist 483&& GARGCOUNT > 0 484&& win -> w_arg_idx < GARGCOUNT 485&& (win -> w_buffer -> b_fnum == GARGLIST [GARGCOUNT - 1 ].ae_fnum 486|| (win -> w_buffer -> b_ffname != NULL 487&& path_equal (alist_name (& GARGLIST [GARGCOUNT - 1 ]), 488win -> w_buffer -> b_ffname ,kPathCmpExpand |kPathCmpFull )))) { 489arg_had_last = true; 490 } 491 }else { 492// We are editing the current entry in the argument list. 493// Set "arg_had_last" if it's also the last one 494win -> w_arg_idx_invalid = false; 495if (win -> w_arg_idx == WARGCOUNT (win )- 1 && win -> w_alist == & global_alist ) { 496arg_had_last = true; 497 } 498 } 499} 500 501/// ":args", ":arglocal" and ":argglobal". 502void ex_args (exarg_T * eap ) 503{ 504if (eap -> cmdidx != CMD_args ) { 505if (check_arglist_locked ()== FAIL ) { 506return ; 507 } 508alist_unlink (ALIST (curwin )); 509if (eap -> cmdidx == CMD_argglobal ) { 510ALIST (curwin )= & global_alist ; 511 }else {// eap->cmdidx == CMD_arglocal 512alist_new (); 513 } 514 } 515 516// ":args file ..": define new argument list, handle like ":next" 517// Also for ":argslocal file .." and ":argsglobal file ..". 518if (* eap -> arg != NUL ) { 519if (check_arglist_locked ()== FAIL ) { 520return ; 521 } 522ex_next (eap ); 523return ; 524 } 525 526// ":args": list arguments. 527if (eap -> cmdidx == CMD_args ) { 528if (ARGCOUNT <=0 ) { 529return ;// empty argument list 530 } 531 532char ** items = xmalloc (sizeof (char * )* (size_t )ARGCOUNT ); 533 534// Overwrite the command, for a short list there is no scrolling 535// required and no wait_return(). 536gotocmdline (true); 537 538for (int i = 0 ;i < ARGCOUNT ;i ++ ) { 539items [i ]= alist_name (& ARGLIST [i ]); 540 } 541list_in_columns (items ,ARGCOUNT ,curwin -> w_arg_idx ); 542xfree (items ); 543 544return ; 545 } 546 547// ":argslocal": make a local copy of the global argument list. 548if (eap -> cmdidx == CMD_arglocal ) { 549garray_T * gap = & curwin -> w_alist -> al_ga ; 550 551ga_grow (gap ,GARGCOUNT ); 552 553for (int i = 0 ;i < GARGCOUNT ;i ++ ) { 554if (GARGLIST [i ].ae_fname != NULL ) { 555AARGLIST (curwin -> w_alist )[gap -> ga_len ].ae_fname = xstrdup (GARGLIST [i ].ae_fname ); 556AARGLIST (curwin -> w_alist )[gap -> ga_len ].ae_fnum = GARGLIST [i ].ae_fnum ; 557gap -> ga_len ++ ; 558 } 559 } 560 } 561} 562 563/// ":previous", ":sprevious", ":Next" and ":sNext". 564void ex_previous (exarg_T * eap ) 565{ 566// If past the last one already, go to the last one. 567if (curwin -> w_arg_idx - (int )eap -> line2 >=ARGCOUNT ) { 568do_argfile (eap ,ARGCOUNT - 1 ); 569 }else { 570do_argfile (eap ,curwin -> w_arg_idx - (int )eap -> line2 ); 571 } 572} 573 574/// ":rewind", ":first", ":sfirst" and ":srewind". 575void ex_rewind (exarg_T * eap ) 576{ 577do_argfile (eap ,0 ); 578} 579 580/// ":last" and ":slast". 581void ex_last (exarg_T * eap ) 582{ 583do_argfile (eap ,ARGCOUNT - 1 ); 584} 585 586/// ":argument" and ":sargument". 587void ex_argument (exarg_T * eap ) 588{ 589int i ; 590 591if (eap -> addr_count > 0 ) { 592i = (int )eap -> line2 - 1 ; 593 }else { 594i = curwin -> w_arg_idx ; 595 } 596do_argfile (eap ,i ); 597} 598 599/// Edit file "argn" of the argument lists. 600void do_argfile (exarg_T * eap ,int argn ) 601{ 602bool is_split_cmd = * eap -> cmd == 's' ; 603 604int old_arg_idx = curwin -> w_arg_idx ; 605 606if (argn < 0 || argn >=ARGCOUNT ) { 607if (ARGCOUNT <=1 ) { 608emsg (_ ("E163: There is only one file to edit" )); 609 }else if (argn < 0 ) { 610emsg (_ ("E164: Cannot go before first file" )); 611 }else { 612emsg (_ ("E165: Cannot go beyond last file" )); 613 } 614 615return ; 616 } 617 618if (!is_split_cmd 619&& (& ARGLIST [argn ])-> ae_fnum != curbuf -> b_fnum 620&& !check_can_set_curbuf_forceit (eap -> forceit )) { 621return ; 622 } 623 624setpcmark (); 625 626// split window or create new tab page first 627if (is_split_cmd || cmdmod .cmod_tab != 0 ) { 628if (win_split (0 ,0 )== FAIL ) { 629return ; 630 } 631RESET_BINDING (curwin ); 632 }else { 633// if 'hidden' set, only check for changed file when re-editing 634// the same buffer 635int other = true; 636if (buf_hide (curbuf )) { 637char * p = fix_fname (alist_name (& ARGLIST [argn ])); 638other = otherfile (p ); 639xfree (p ); 640 } 641if ((!buf_hide (curbuf )|| !other ) 642&& check_changed (curbuf ,CCGD_AW 643 | (other ?0 :CCGD_MULTWIN ) 644 | (eap -> forceit ?CCGD_FORCEIT :0 ) 645 |CCGD_EXCMD )) { 646return ; 647 } 648 } 649 650curwin -> w_arg_idx = argn ; 651if (argn == ARGCOUNT - 1 && curwin -> w_alist == & global_alist ) { 652arg_had_last = true; 653 } 654 655// Edit the file; always use the last known line number. 656// When it fails (e.g. Abort for already edited file) restore the 657// argument index. 658if (do_ecmd (0 ,alist_name (& ARGLIST [curwin -> w_arg_idx ]),NULL , 659eap ,ECMD_LAST , 660 (buf_hide (curwin -> w_buffer ) ?ECMD_HIDE :0 ) 661+ (eap -> forceit ?ECMD_FORCEIT :0 ),curwin )== FAIL ) { 662curwin -> w_arg_idx = old_arg_idx ; 663 }else if (eap -> cmdidx != CMD_argdo ) { 664// like Vi: set the mark where the cursor is in the file. 665setmark ('\'' ); 666 } 667} 668 669/// ":next", and commands that behave like it. 670void ex_next (exarg_T * eap ) 671{ 672// check for changed buffer now, if this fails the argument list is not 673// redefined. 674if (buf_hide (curbuf ) 675|| eap -> cmdidx == CMD_snext 676|| !check_changed (curbuf ,CCGD_AW 677 | (eap -> forceit ?CCGD_FORCEIT :0 ) 678 |CCGD_EXCMD )) { 679int i ; 680if (* eap -> arg != NUL ) {// redefine file list 681if (do_arglist (eap -> arg ,AL_SET ,0 , true)== FAIL ) { 682return ; 683 } 684i = 0 ; 685 }else { 686i = curwin -> w_arg_idx + (int )eap -> line2 ; 687 } 688do_argfile (eap ,i ); 689 } 690} 691 692/// ":argdedupe" 693void ex_argdedupe (exarg_T * eap FUNC_ATTR_UNUSED ) 694{ 695for (int i = 0 ;i < ARGCOUNT ;i ++ ) { 696// Expand each argument to a full path to catch different paths leading 697// to the same file. 698char * firstFullname = FullName_save (ARGLIST [i ].ae_fname , false); 699 700for (int j = i + 1 ;j < ARGCOUNT ;j ++ ) { 701char * secondFullname = FullName_save (ARGLIST [j ].ae_fname , false); 702bool areNamesDuplicate = path_equal (firstFullname ,secondFullname ,kPathCmpLiteral ); 703xfree (secondFullname ); 704 705if (areNamesDuplicate ) { 706// remove one duplicate argument 707xfree (ARGLIST [j ].ae_fname ); 708memmove (ARGLIST + j ,ARGLIST + j + 1 , 709 (size_t )(ARGCOUNT - j - 1 )* sizeof (aentry_T )); 710ARGCOUNT -- ; 711 712if (curwin -> w_arg_idx == j ) { 713curwin -> w_arg_idx = i ; 714 }else if (curwin -> w_arg_idx > j ) { 715curwin -> w_arg_idx -- ; 716 } 717 718j -- ; 719 } 720 } 721 722xfree (firstFullname ); 723 } 724} 725 726/// ":argedit" 727void ex_argedit (exarg_T * eap ) 728{ 729int i = eap -> addr_count ? (int )eap -> line2 :curwin -> w_arg_idx + 1 ; 730// Whether curbuf will be reused, curbuf->b_ffname will be set. 731bool curbuf_is_reusable = curbuf_reusable (); 732 733if (do_arglist (eap -> arg ,AL_ADD ,i , true)== FAIL ) { 734return ; 735 } 736maketitle (); 737 738if (curwin -> w_arg_idx == 0 739&& (curbuf -> b_ml .ml_flags & ML_EMPTY ) 740&& (curbuf -> b_ffname == NULL || curbuf_is_reusable )) { 741i = 0 ; 742 } 743// Edit the argument. 744if (i < ARGCOUNT ) { 745do_argfile (eap ,i ); 746 } 747} 748 749/// ":argadd" 750void ex_argadd (exarg_T * eap ) 751{ 752do_arglist (eap -> arg ,AL_ADD , 753eap -> addr_count > 0 ? (int )eap -> line2 :curwin -> w_arg_idx + 1 , 754 false); 755maketitle (); 756} 757 758/// ":argdelete" 759void ex_argdelete (exarg_T * eap ) 760{ 761if (check_arglist_locked ()== FAIL ) { 762return ; 763 } 764 765if (eap -> addr_count > 0 || * eap -> arg == NUL ) { 766// ":argdel" works like ":.argdel" 767if (eap -> addr_count == 0 ) { 768if (curwin -> w_arg_idx >=ARGCOUNT ) { 769emsg (_ ("E610: No argument to delete" )); 770return ; 771 } 772eap -> line1 = eap -> line2 = curwin -> w_arg_idx + 1 ; 773 }else if (eap -> line2 > ARGCOUNT ) { 774// ":1,4argdel": Delete all arguments in the range. 775eap -> line2 = ARGCOUNT ; 776 } 777linenr_T n = eap -> line2 - eap -> line1 + 1 ; 778if (* eap -> arg != NUL ) { 779// Can't have both a range and an argument. 780emsg (_ (e_invarg )); 781return ; 782 } 783if (n <=0 ) { 784// Don't give an error for ":%argdel" if the list is empty. 785if (eap -> line1 != 1 || eap -> line2 != 0 ) { 786emsg (_ (e_invrange )); 787 } 788return ; 789 } 790 791for (linenr_T i = eap -> line1 ;i <=eap -> line2 ;i ++ ) { 792xfree (ARGLIST [i - 1 ].ae_fname ); 793 } 794memmove (ARGLIST + eap -> line1 - 1 ,ARGLIST + eap -> line2 , 795 (size_t )(ARGCOUNT - eap -> line2 )* sizeof (aentry_T )); 796ARGCOUNT -= n ; 797if (curwin -> w_arg_idx >=eap -> line2 ) { 798curwin -> w_arg_idx -= n ; 799 }else if (curwin -> w_arg_idx > eap -> line1 ) { 800curwin -> w_arg_idx = eap -> line1 ; 801 } 802 }else { 803do_arglist (eap -> arg ,AL_DEL ,0 , false); 804 } 805 806if (ARGCOUNT == 0 ) { 807curwin -> w_arg_idx = 0 ; 808 }else if (curwin -> w_arg_idx >=ARGCOUNT ) { 809curwin -> w_arg_idx = ARGCOUNT - 1 ; 810 } 811 812maketitle (); 813} 814 815/// Function given to ExpandGeneric() to obtain the possible arguments of the 816/// argedit and argdelete commands. 817char * get_arglist_name (expand_T * xp FUNC_ATTR_UNUSED ,int idx ) 818{ 819if (idx >=ARGCOUNT ) { 820return NULL ; 821 } 822return alist_name (& ARGLIST [idx ]); 823} 824 825/// Get the file name for an argument list entry. 826char * alist_name (aentry_T * aep ) 827{ 828// Use the name from the associated buffer if it exists. 829buf_T * bp = buflist_findnr (aep -> ae_fnum ); 830if (bp == NULL || bp -> b_fname == NULL ) { 831return aep -> ae_fname ; 832 } 833return bp -> b_fname ; 834} 835 836/// Close all the windows containing files which are not in the argument list. 837/// Used by the ":all" command. 838static void arg_all_close_unused_windows (arg_all_state_T * aall ) 839{ 840win_T * old_curwin = curwin ; 841tabpage_T * old_curtab = curtab ; 842 843if (aall -> had_tab > 0 ) { 844goto_tabpage_tp (first_tabpage , true, true); 845 } 846 847// moving tabpages around in an autocommand may cause an endless loop 848tabpage_move_disallowed ++ ; 849while (true) { 850win_T * wpnext = NULL ; 851tabpage_T * tpnext = curtab -> tp_next ; 852// Try to close floating windows first 853for (win_T * wp = lastwin -> w_floating ?lastwin :firstwin ;wp != NULL ;wp = wpnext ) { 854int i ; 855wpnext = wp -> w_floating 856 ?wp -> w_prev -> w_floating ?wp -> w_prev :firstwin 857 : (wp -> w_next == NULL || wp -> w_next -> w_floating ) ?NULL :wp -> w_next ; 858buf_T * buf = wp -> w_buffer ; 859if (buf -> b_ffname == NULL 860|| (!aall -> keep_tabs 861&& (buf -> b_nwindows > 1 || wp -> w_width != Columns 862|| (wp -> w_floating && !is_ctx_win (wp ))))) { 863i = aall -> opened_len ; 864 }else { 865// check if the buffer in this window is in the arglist 866for (i = 0 ;i < aall -> opened_len ;i ++ ) { 867if (i < aall -> alist -> al_ga .ga_len 868&& (AARGLIST (aall -> alist )[i ].ae_fnum == buf -> b_fnum 869|| path_equal (alist_name (& AARGLIST (aall -> alist )[i ]), 870buf -> b_ffname ,kPathCmpExpand |kPathCmpFull ))) { 871int weight = 1 ; 872 873if (old_curtab == curtab ) { 874weight ++ ; 875if (old_curwin == wp ) { 876weight ++ ; 877 } 878 } 879 880if (weight > (int )aall -> opened [i ]) { 881aall -> opened [i ]= (uint8_t )weight ; 882if (i == 0 ) { 883if (aall -> new_curwin != NULL ) { 884aall -> new_curwin -> w_arg_idx = aall -> opened_len ; 885 } 886aall -> new_curwin = wp ; 887aall -> new_curtab = curtab ; 888 } 889 }else if (aall -> keep_tabs ) { 890i = aall -> opened_len ; 891 } 892 893if (wp -> w_alist != aall -> alist ) { 894// Use the current argument list for all windows 895// containing a file from it. 896alist_unlink (wp -> w_alist ); 897wp -> w_alist = aall -> alist ; 898wp -> w_alist -> al_refcount ++ ; 899 } 900break ; 901 } 902 } 903 } 904wp -> w_arg_idx = i ; 905 906if (i == aall -> opened_len && !aall -> keep_tabs ) {// close this window 907if (buf_hide (buf )|| aall -> forceit || buf -> b_nwindows > 1 908|| !bufIsChanged (buf )) { 909// If the buffer was changed, and we would like to hide it, try autowriting. 910if (!buf_hide (buf )&& buf -> b_nwindows <=1 && bufIsChanged (buf )) { 911bufref_T bufref ; 912set_bufref (& bufref ,buf ); 913autowrite (buf , false); 914// Check if autocommands removed the window. 915if (!win_valid (wp )|| !bufref_valid (& bufref )) { 916wpnext = lastwin -> w_floating ?lastwin :firstwin ;// Start all over... 917continue ; 918 } 919 } 920// don't close last window 921if (ONE_WINDOW 922&& (first_tabpage -> tp_next == NULL || !aall -> had_tab )) { 923aall -> use_firstwin = true; 924 }else { 925win_close (wp , !buf_hide (buf )&& !bufIsChanged (buf ), false); 926// check if autocommands removed the next window 927if (!win_valid (wpnext )) { 928// start all over... 929wpnext = lastwin -> w_floating ?lastwin :firstwin ; 930 } 931 } 932 } 933 } 934 } 935 936// Without the ":tab" modifier only do the current tab page. 937if (aall -> had_tab == 0 || tpnext == NULL ) { 938break ; 939 } 940 941// check if autocommands removed the next tab page 942if (!valid_tabpage (tpnext )) { 943tpnext = first_tabpage ;// start all over... 944 } 945goto_tabpage_tp (tpnext , true, true); 946 } 947tabpage_move_disallowed -- ; 948} 949 950/// Open up to "count" windows for the files in the argument list "aall->alist". 951static void arg_all_open_windows (arg_all_state_T * aall ,int count ) 952{ 953bool tab_drop_empty_window = false; 954 955// ":tab drop file" should re-use an empty window to avoid "--remote-tab" 956// leaving an empty tab page when executed locally. 957if (aall -> keep_tabs && buf_is_empty (curbuf )&& curbuf -> b_nwindows == 1 958&& curbuf -> b_ffname == NULL && !curbuf -> b_changed ) { 959aall -> use_firstwin = true; 960tab_drop_empty_window = true; 961 } 962 963int split_ret = OK ; 964 965for (int i = 0 ;i < count && !got_int ;i ++ ) { 966if (aall -> alist == & global_alist && i == global_alist .al_ga .ga_len - 1 ) { 967arg_had_last = true; 968 } 969if (aall -> opened [i ]> 0 ) { 970// Move the already present window to below the current window 971if (curwin -> w_arg_idx != i ) { 972FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 973if (wp -> w_arg_idx == i ) { 974if (aall -> keep_tabs ) { 975aall -> new_curwin = wp ; 976aall -> new_curtab = curtab ; 977 }else if (wp -> w_floating ) { 978break ; 979 }else if (wp -> w_frame -> fr_parent != curwin -> w_frame -> fr_parent ) { 980emsg (_ (e_window_layout_changed_unexpectedly )); 981i = count ; 982break ; 983 }else { 984win_move_after (wp ,curwin ); 985 } 986break ; 987 } 988 } 989 } 990 }else if (split_ret == OK ) { 991// trigger events for tab drop 992if (tab_drop_empty_window && i == count - 1 ) { 993autocmd_no_enter -- ; 994 } 995if (!aall -> use_firstwin ) {// split current window 996bool p_ea_save = p_ea ; 997p_ea = true;// use space from all windows 998split_ret = win_split (0 ,WSP_ROOM |WSP_BELOW ); 999p_ea = p_ea_save ; 1000if (split_ret == FAIL ) { 1001continue ; 1002 } 1003 }else {// first window: do autocmd for leaving this buffer 1004autocmd_no_leave -- ; 1005 } 1006 1007// edit file "i" 1008curwin -> w_arg_idx = i ; 1009if (i == 0 ) { 1010aall -> new_curwin = curwin ; 1011aall -> new_curtab = curtab ; 1012 } 1013do_ecmd (0 ,alist_name (& AARGLIST (aall -> alist )[i ]),NULL ,NULL ,ECMD_ONE , 1014 ((buf_hide (curwin -> w_buffer ) 1015|| bufIsChanged (curwin -> w_buffer )) ?ECMD_HIDE :0 )+ ECMD_OLDBUF , 1016curwin ); 1017if (tab_drop_empty_window && i == count - 1 ) { 1018autocmd_no_enter ++ ; 1019 } 1020if (aall -> use_firstwin ) { 1021autocmd_no_leave ++ ; 1022 } 1023aall -> use_firstwin = false; 1024 } 1025os_breakcheck (); 1026 1027// When ":tab" was used open a new tab for a new window repeatedly. 1028if (aall -> had_tab > 0 && tabpage_index (NULL ) <=p_tpm ) { 1029cmdmod .cmod_tab = 9999 ; 1030 } 1031 } 1032} 1033 1034/// do_arg_all(): Open up to 'count' windows, one for each argument. 1035/// 1036/// @param forceit hide buffers in current windows 1037/// @param keep_tabs keep current tabs, for ":tab drop file" 1038static void do_arg_all (int count ,int forceit ,int keep_tabs ) 1039{ 1040win_T * last_curwin ; 1041tabpage_T * last_curtab ; 1042bool prev_arglist_locked = arglist_locked ; 1043 1044assert (firstwin != NULL );// satisfy coverity 1045 1046if (ARGCOUNT <=0 ) { 1047// Don't give an error message. We don't want it when the ":all" 1048// command is in the .vimrc. 1049return ; 1050 } 1051setpcmark (); 1052 1053arg_all_state_T aall = { 1054 .use_firstwin = false, 1055 .had_tab = cmdmod .cmod_tab , 1056 .new_curwin = NULL , 1057 .new_curtab = NULL , 1058 .forceit = forceit , 1059 .keep_tabs = keep_tabs , 1060 .opened_len = ARGCOUNT , 1061 .opened = xcalloc ((size_t )ARGCOUNT ,1 ), 1062 }; 1063 1064// Autocommands may do anything to the argument list. Make sure it's not 1065// freed while we are working here by "locking" it. We still have to 1066// watch out for its size to be changed. 1067aall .alist = curwin -> w_alist ; 1068aall .alist -> al_refcount ++ ; 1069arglist_locked = true; 1070 1071tabpage_T * const new_lu_tp = curtab ; 1072 1073// Stop Visual mode, the cursor and `Visual.start` may very well be invalid after 1074// switching to another buffer. 1075reset_VIsual_and_resel (); 1076 1077// Try closing all windows that are not in the argument list. 1078// Also close windows that are not full width; 1079// When 'hidden' or "forceit" set the buffer becomes hidden. 1080// Windows that have a changed buffer and can't be hidden won't be closed. 1081// When the ":tab" modifier was used do this for all tab pages. 1082arg_all_close_unused_windows (& aall ); 1083 1084// Open a window for files in the argument list that don't have one. 1085// ARGCOUNT may change while doing this, because of autocommands. 1086if (count > aall .opened_len || count <=0 ) { 1087count = aall .opened_len ; 1088 } 1089 1090// Don't execute Win/Buf Enter/Leave autocommands here. 1091autocmd_no_enter ++ ; 1092autocmd_no_leave ++ ; 1093last_curwin = curwin ; 1094last_curtab = curtab ; 1095// lastwin may be ctx_win 1096win_enter (lastwin_nofloating (NULL ), false); 1097 1098// Open up to "count" windows. 1099arg_all_open_windows (& aall ,count ); 1100 1101// Remove the "lock" on the argument list. 1102alist_unlink (aall .alist ); 1103arglist_locked = prev_arglist_locked ; 1104 1105autocmd_no_enter -- ; 1106 1107// restore last referenced tabpage's curwin 1108if (last_curtab != aall .new_curtab ) { 1109if (valid_tabpage (last_curtab )) { 1110goto_tabpage_tp (last_curtab , true, true); 1111 } 1112if (win_valid (last_curwin )) { 1113win_enter (last_curwin , false); 1114 } 1115 } 1116// to window with first arg 1117if (valid_tabpage (aall .new_curtab )) { 1118goto_tabpage_tp (aall .new_curtab , true, true); 1119 } 1120 1121// Now set the last used tabpage to where we started. 1122if (valid_tabpage (new_lu_tp )) { 1123lastused_tabpage = new_lu_tp ; 1124 } 1125 1126if (win_valid (aall .new_curwin )) { 1127win_enter (aall .new_curwin , false); 1128 } 1129 1130autocmd_no_leave -- ; 1131xfree (aall .opened ); 1132} 1133 1134/// ":all" and ":sall". 1135/// Also used for ":tab drop file ..." after setting the argument list. 1136void ex_all (exarg_T * eap ) 1137{ 1138if (eap -> addr_count == 0 ) { 1139eap -> line2 = 9999 ; 1140 } 1141do_arg_all ((int )eap -> line2 ,eap -> forceit ,eap -> cmdidx == CMD_drop ); 1142} 1143 1144/// Concatenate all files in the argument list, separated by spaces, and return 1145/// it in one allocated string. 1146/// Spaces and backslashes in the file names are escaped with a backslash. 1147char * arg_all (void ) 1148{ 1149char * retval = NULL ; 1150 1151// Do this loop two times: 1152// first time: compute the total length 1153// second time: concatenate the names 1154while (true) { 1155int len = 0 ; 1156for (int idx = 0 ;idx < ARGCOUNT ;idx ++ ) { 1157char * p = alist_name (& ARGLIST [idx ]); 1158if (p == NULL ) { 1159continue ; 1160 } 1161if (len > 0 ) { 1162// insert a space in between names 1163if (retval != NULL ) { 1164retval [len ]= ' ' ; 1165 } 1166len ++ ; 1167 } 1168for (;* p != NUL ;p ++ ) { 1169if (* p == ' ' 1170#ifndef BACKSLASH_IN_FILENAME 1171|| * p == '\\' 1172#endif 1173|| * p == '`' ) { 1174// insert a backslash 1175if (retval != NULL ) { 1176retval [len ]= '\\' ; 1177 } 1178len ++ ; 1179 } 1180if (retval != NULL ) { 1181retval [len ]= * p ; 1182 } 1183len ++ ; 1184 } 1185 } 1186 1187// second time: break here 1188if (retval != NULL ) { 1189retval [len ]= NUL ; 1190break ; 1191 } 1192 1193// allocate memory 1194retval = xmalloc ((size_t )len + 1 ); 1195 } 1196 1197return retval ; 1198} 1199 1200/// "argc([window id])" function 1201void f_argc (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1202{ 1203if (argvars [0 ].v_type == VAR_UNKNOWN ) { 1204// use the current window 1205rettv -> vval .v_number = ARGCOUNT ; 1206 }else if (argvars [0 ].v_type == VAR_NUMBER 1207&& tv_get_number (& argvars [0 ])== -1 ) { 1208// use the global argument list 1209rettv -> vval .v_number = GARGCOUNT ; 1210 }else { 1211// use the argument list of the specified window 1212win_T * wp = find_win_by_nr_or_id (& argvars [0 ]); 1213if (wp != NULL ) { 1214rettv -> vval .v_number = WARGCOUNT (wp ); 1215 }else { 1216rettv -> vval .v_number = -1 ; 1217 } 1218 } 1219} 1220 1221/// "argidx()" function 1222void f_argidx (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1223{ 1224rettv -> vval .v_number = curwin -> w_arg_idx ; 1225} 1226 1227/// "arglistid()" function 1228void f_arglistid (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1229{ 1230rettv -> vval .v_number = -1 ; 1231win_T * wp = find_tabwin (& argvars [0 ],& argvars [1 ]); 1232if (wp != NULL ) { 1233rettv -> vval .v_number = wp -> w_alist -> id ; 1234 } 1235} 1236 1237/// Get the argument list for a given window 1238static void get_arglist_as_rettv (aentry_T * arglist ,int argcount ,typval_T * rettv ) 1239{ 1240tv_list_alloc_ret (rettv ,argcount ); 1241if (arglist != NULL ) { 1242for (int idx = 0 ;idx < argcount ;idx ++ ) { 1243tv_list_append_string (rettv -> vval .v_list ,alist_name (& arglist [idx ]),-1 ); 1244 } 1245 } 1246} 1247 1248/// "argv(nr)" function 1249void f_argv (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1250{ 1251aentry_T * arglist = NULL ; 1252int argcount = -1 ; 1253 1254if (argvars [0 ].v_type == VAR_UNKNOWN ) { 1255get_arglist_as_rettv (ARGLIST ,ARGCOUNT ,rettv ); 1256return ; 1257 } 1258 1259if (argvars [1 ].v_type == VAR_UNKNOWN ) { 1260arglist = ARGLIST ; 1261argcount = ARGCOUNT ; 1262 }else if (argvars [1 ].v_type == VAR_NUMBER 1263&& tv_get_number (& argvars [1 ])== -1 ) { 1264arglist = GARGLIST ; 1265argcount = GARGCOUNT ; 1266 }else { 1267win_T * wp = find_win_by_nr_or_id (& argvars [1 ]); 1268if (wp != NULL ) { 1269// Use the argument list of the specified window 1270arglist = WARGLIST (wp ); 1271argcount = WARGCOUNT (wp ); 1272 } 1273 } 1274 1275rettv -> v_type = VAR_STRING ; 1276rettv -> vval .v_string = NULL ; 1277int idx = (int )tv_get_number_chk (& argvars [0 ],NULL ); 1278if (arglist != NULL && idx >=0 && idx < argcount ) { 1279rettv -> vval .v_string = xstrdup (alist_name (& arglist [idx ])); 1280 }else if (idx == -1 ) { 1281get_arglist_as_rettv (arglist ,argcount ,rettv ); 1282 } 1283}