1 /* vi:set ts=8 sts=4 sw=4: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * buffer.c: functions for dealing with the buffer structure 12 */ 13 14 /* 15 * The buffer list is a double linked list of all buffers. 16 * Each buffer can be in one of these states: 17 * never loaded: BF_NEVERLOADED is set, only the file name is valid 18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated 19 * hidden: b_nwindows == 0, loaded but not displayed in a window 20 * normal: loaded and displayed in a window 21 * 22 * Instead of storing file names all over the place, each file name is 23 * stored in the buffer list. It can be referenced by a number. 24 * 25 * The current implementation remembers all file names ever used. 26 */ 27 28 #include "vim.h" 29 30 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) 31 static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf)); 32 # define HAVE_BUFLIST_MATCH 33 static char_u *fname_match __ARGS((regprog_T *prog, char_u *name)); 34 #endif 35 static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options)); 36 static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer)); 37 #ifdef UNIX 38 static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st)); 39 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp)); 40 static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp)); 41 #else 42 static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname)); 43 #endif 44 #ifdef FEAT_TITLE 45 static int ti_change __ARGS((char_u *str, char_u **last)); 46 #endif 47 static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file)); 48 static void free_buffer __ARGS((buf_T *)); 49 static void free_buffer_stuff __ARGS((buf_T *buf, int free_options)); 50 static void clear_wininfo __ARGS((buf_T *buf)); 51 52 #ifdef UNIX 53 # define dev_T dev_t 54 #else 55 # define dev_T unsigned 56 #endif 57 58 #if defined(FEAT_SIGNS) 59 static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr)); 60 #endif 61 62 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 63 static char *msg_loclist = N_("[Location List]"); 64 static char *msg_qflist = N_("[Quickfix List]"); 65 #endif 66 #ifdef FEAT_AUTOCMD 67 static char *e_auabort = N_("E855: Autocommands caused command to abort"); 68 #endif 69 70 /* 71 * Open current buffer, that is: open the memfile and read the file into 72 * memory. 73 * Return FAIL for failure, OK otherwise. 74 */ 75 int 76 open_buffer(read_stdin, eap, flags) 77 int read_stdin; /* read file from stdin */ 78 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */ 79 int flags; /* extra flags for readfile() */ 80 { 81 int retval = OK; 82 #ifdef FEAT_AUTOCMD 83 buf_T *old_curbuf; 84 #endif 85 86 /* 87 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset. 88 * When re-entering the same buffer, it should not change, because the 89 * user may have reset the flag by hand. 90 */ 91 if (readonlymode && curbuf->b_ffname != NULL 92 && (curbuf->b_flags & BF_NEVERLOADED)) 93 curbuf->b_p_ro = TRUE; 94 95 if (ml_open(curbuf) == FAIL) 96 { 97 /* 98 * There MUST be a memfile, otherwise we can't do anything 99 * If we can't create one for the current buffer, take another buffer 100 */ 101 close_buffer(NULL, curbuf, 0, FALSE); 102 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next) 103 if (curbuf->b_ml.ml_mfp != NULL) 104 break; 105 /* 106 * if there is no memfile at all, exit 107 * This is OK, since there are no changes to lose. 108 */ 109 if (curbuf == NULL) 110 { 111 EMSG(_("E82: Cannot allocate any buffer, exiting...")); 112 getout(2); 113 } 114 EMSG(_("E83: Cannot allocate buffer, using other one...")); 115 enter_buffer(curbuf); 116 return FAIL; 117 } 118 119 #ifdef FEAT_AUTOCMD 120 /* The autocommands in readfile() may change the buffer, but only AFTER 121 * reading the file. */ 122 old_curbuf = curbuf; 123 modified_was_set = FALSE; 124 #endif 125 126 /* mark cursor position as being invalid */ 127 curwin->w_valid = 0; 128 129 if (curbuf->b_ffname != NULL 130 #ifdef FEAT_NETBEANS_INTG 131 && netbeansReadFile 132 #endif 133 ) 134 { 135 #ifdef FEAT_NETBEANS_INTG 136 int oldFire = netbeansFireChanges; 137 138 netbeansFireChanges = 0; 139 #endif 140 retval = readfile(curbuf->b_ffname, curbuf->b_fname, 141 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap, 142 flags | READ_NEW); 143 #ifdef FEAT_NETBEANS_INTG 144 netbeansFireChanges = oldFire; 145 #endif 146 /* Help buffer is filtered. */ 147 if (curbuf->b_help) 148 fix_help_buffer(); 149 } 150 else if (read_stdin) 151 { 152 int save_bin = curbuf->b_p_bin; 153 linenr_T line_count; 154 155 /* 156 * First read the text in binary mode into the buffer. 157 * Then read from that same buffer and append at the end. This makes 158 * it possible to retry when 'fileformat' or 'fileencoding' was 159 * guessed wrong. 160 */ 161 curbuf->b_p_bin = TRUE; 162 retval = readfile(NULL, NULL, (linenr_T)0, 163 (linenr_T)0, (linenr_T)MAXLNUM, NULL, 164 flags | (READ_NEW + READ_STDIN)); 165 curbuf->b_p_bin = save_bin; 166 if (retval == OK) 167 { 168 line_count = curbuf->b_ml.ml_line_count; 169 retval = readfile(NULL, NULL, (linenr_T)line_count, 170 (linenr_T)0, (linenr_T)MAXLNUM, eap, 171 flags | READ_BUFFER); 172 if (retval == OK) 173 { 174 /* Delete the binary lines. */ 175 while (--line_count >= 0) 176 ml_delete((linenr_T)1, FALSE); 177 } 178 else 179 { 180 /* Delete the converted lines. */ 181 while (curbuf->b_ml.ml_line_count > line_count) 182 ml_delete(line_count, FALSE); 183 } 184 /* Put the cursor on the first line. */ 185 curwin->w_cursor.lnum = 1; 186 curwin->w_cursor.col = 0; 187 188 /* Set or reset 'modified' before executing autocommands, so that 189 * it can be changed there. */ 190 if (!readonlymode && !bufempty()) 191 changed(); 192 else if (retval != FAIL) 193 unchanged(curbuf, FALSE); 194 #ifdef FEAT_AUTOCMD 195 # ifdef FEAT_EVAL 196 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE, 197 curbuf, &retval); 198 # else 199 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf); 200 # endif 201 #endif 202 } 203 } 204 205 /* if first time loading this buffer, init b_chartab[] */ 206 if (curbuf->b_flags & BF_NEVERLOADED) 207 (void)buf_init_chartab(curbuf, FALSE); 208 209 /* 210 * Set/reset the Changed flag first, autocmds may change the buffer. 211 * Apply the automatic commands, before processing the modelines. 212 * So the modelines have priority over auto commands. 213 */ 214 /* When reading stdin, the buffer contents always needs writing, so set 215 * the changed flag. Unless in readonly mode: "ls | gview -". 216 * When interrupted and 'cpoptions' contains 'i' set changed flag. */ 217 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL) 218 #ifdef FEAT_AUTOCMD 219 || modified_was_set /* ":set modified" used in autocmd */ 220 # ifdef FEAT_EVAL 221 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL) 222 # endif 223 #endif 224 ) 225 changed(); 226 else if (retval != FAIL && !read_stdin) 227 unchanged(curbuf, FALSE); 228 save_file_ff(curbuf); /* keep this fileformat */ 229 230 /* require "!" to overwrite the file, because it wasn't read completely */ 231 #ifdef FEAT_EVAL 232 if (aborting()) 233 #else 234 if (got_int) 235 #endif 236 curbuf->b_flags |= BF_READERR; 237 238 #ifdef FEAT_FOLDING 239 /* Need to update automatic folding. Do this before the autocommands, 240 * they may use the fold info. */ 241 foldUpdateAll(curwin); 242 #endif 243 244 #ifdef FEAT_AUTOCMD 245 /* need to set w_topline, unless some autocommand already did that. */ 246 if (!(curwin->w_valid & VALID_TOPLINE)) 247 { 248 curwin->w_topline = 1; 249 # ifdef FEAT_DIFF 250 curwin->w_topfill = 0; 251 # endif 252 } 253 # ifdef FEAT_EVAL 254 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval); 255 # else 256 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 257 # endif 258 #endif 259 260 if (retval != FAIL) 261 { 262 #ifdef FEAT_AUTOCMD 263 /* 264 * The autocommands may have changed the current buffer. Apply the 265 * modelines to the correct buffer, if it still exists and is loaded. 266 */ 267 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) 268 { 269 aco_save_T aco; 270 271 /* Go to the buffer that was opened. */ 272 aucmd_prepbuf(&aco, old_curbuf); 273 #endif 274 do_modelines(0); 275 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); 276 277 #ifdef FEAT_AUTOCMD 278 # ifdef FEAT_EVAL 279 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, 280 &retval); 281 # else 282 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); 283 # endif 284 285 /* restore curwin/curbuf and a few other things */ 286 aucmd_restbuf(&aco); 287 } 288 #endif 289 } 290 291 return retval; 292 } 293 294 /* 295 * Return TRUE if "buf" points to a valid buffer (in the buffer list). 296 */ 297 int 298 buf_valid(buf) 299 buf_T *buf; 300 { 301 buf_T *bp; 302 303 for (bp = firstbuf; bp != NULL; bp = bp->b_next) 304 if (bp == buf) 305 return TRUE; 306 return FALSE; 307 } 308 309 /* 310 * Close the link to a buffer. 311 * "action" is used when there is no longer a window for the buffer. 312 * It can be: 313 * 0 buffer becomes hidden 314 * DOBUF_UNLOAD buffer is unloaded 315 * DOBUF_DELETE buffer is unloaded and removed from buffer list 316 * DOBUF_WIPE buffer is unloaded and really deleted 317 * When doing all but the first one on the current buffer, the caller should 318 * get a new buffer very soon! 319 * 320 * The 'bufhidden' option can force freeing and deleting. 321 * 322 * When "abort_if_last" is TRUE then do not close the buffer if autocommands 323 * cause there to be only one window with this buffer. e.g. when ":quit" is 324 * supposed to close the window but autocommands close all other windows. 325 */ 326 void 327 close_buffer(win, buf, action, abort_if_last) 328 win_T *win; /* if not NULL, set b_last_cursor */ 329 buf_T *buf; 330 int action; 331 int abort_if_last UNUSED; 332 { 333 #ifdef FEAT_AUTOCMD 334 int is_curbuf; 335 int nwindows; 336 #endif 337 int unload_buf = (action != 0); 338 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE); 339 int wipe_buf = (action == DOBUF_WIPE); 340 341 #ifdef FEAT_QUICKFIX 342 /* 343 * Force unloading or deleting when 'bufhidden' says so. 344 * The caller must take care of NOT deleting/freeing when 'bufhidden' is 345 * "hide" (otherwise we could never free or delete a buffer). 346 */ 347 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */ 348 { 349 del_buf = TRUE; 350 unload_buf = TRUE; 351 } 352 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */ 353 { 354 del_buf = TRUE; 355 unload_buf = TRUE; 356 wipe_buf = TRUE; 357 } 358 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */ 359 unload_buf = TRUE; 360 #endif 361 362 if (win != NULL) 363 { 364 /* Set b_last_cursor when closing the last window for the buffer. 365 * Remember the last cursor position and window options of the buffer. 366 * This used to be only for the current window, but then options like 367 * 'foldmethod' may be lost with a ":only" command. */ 368 if (buf->b_nwindows == 1) 369 set_last_cursor(win); 370 buflist_setfpos(buf, win, 371 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum, 372 win->w_cursor.col, TRUE); 373 } 374 375 #ifdef FEAT_AUTOCMD 376 /* When the buffer is no longer in a window, trigger BufWinLeave */ 377 if (buf->b_nwindows == 1) 378 { 379 buf->b_closing = TRUE; 380 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname, 381 FALSE, buf); 382 if (!buf_valid(buf)) 383 { 384 /* Autocommands deleted the buffer. */ 385 aucmd_abort: 386 EMSG(_(e_auabort)); 387 return; 388 } 389 buf->b_closing = FALSE; 390 if (abort_if_last && one_window()) 391 /* Autocommands made this the only window. */ 392 goto aucmd_abort; 393 394 /* When the buffer becomes hidden, but is not unloaded, trigger 395 * BufHidden */ 396 if (!unload_buf) 397 { 398 buf->b_closing = TRUE; 399 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname, 400 FALSE, buf); 401 if (!buf_valid(buf)) 402 /* Autocommands deleted the buffer. */ 403 goto aucmd_abort; 404 buf->b_closing = FALSE; 405 if (abort_if_last && one_window()) 406 /* Autocommands made this the only window. */ 407 goto aucmd_abort; 408 } 409 # ifdef FEAT_EVAL 410 if (aborting()) /* autocmds may abort script processing */ 411 return; 412 # endif 413 } 414 nwindows = buf->b_nwindows; 415 #endif 416 417 /* decrease the link count from windows (unless not in any window) */ 418 if (buf->b_nwindows > 0) 419 --buf->b_nwindows; 420 421 /* Return when a window is displaying the buffer or when it's not 422 * unloaded. */ 423 if (buf->b_nwindows > 0 || !unload_buf) 424 return; 425 426 /* Always remove the buffer when there is no file name. */ 427 if (buf->b_ffname == NULL) 428 del_buf = TRUE; 429 430 /* 431 * Free all things allocated for this buffer. 432 * Also calls the "BufDelete" autocommands when del_buf is TRUE. 433 */ 434 #ifdef FEAT_AUTOCMD 435 /* Remember if we are closing the current buffer. Restore the number of 436 * windows, so that autocommands in buf_freeall() don't get confused. */ 437 is_curbuf = (buf == curbuf); 438 buf->b_nwindows = nwindows; 439 #endif 440 441 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0)); 442 if ( 443 #ifdef FEAT_WINDOWS 444 win_valid(win) && 445 #else 446 win != NULL && 447 #endif 448 win->w_buffer == buf) 449 win->w_buffer = NULL; /* make sure we don't use the buffer now */ 450 451 #ifdef FEAT_AUTOCMD 452 /* Autocommands may have deleted the buffer. */ 453 if (!buf_valid(buf)) 454 return; 455 # ifdef FEAT_EVAL 456 if (aborting()) /* autocmds may abort script processing */ 457 return; 458 # endif 459 460 /* Autocommands may have opened or closed windows for this buffer. 461 * Decrement the count for the close we do here. */ 462 if (buf->b_nwindows > 0) 463 --buf->b_nwindows; 464 465 /* 466 * It's possible that autocommands change curbuf to the one being deleted. 467 * This might cause the previous curbuf to be deleted unexpectedly. But 468 * in some cases it's OK to delete the curbuf, because a new one is 469 * obtained anyway. Therefore only return if curbuf changed to the 470 * deleted buffer. 471 */ 472 if (buf == curbuf && !is_curbuf) 473 return; 474 #endif 475 476 /* Change directories when the 'acd' option is set. */ 477 DO_AUTOCHDIR 478 479 /* 480 * Remove the buffer from the list. 481 */ 482 if (wipe_buf) 483 { 484 #ifdef FEAT_SUN_WORKSHOP 485 if (usingSunWorkShop) 486 workshop_file_closed_lineno((char *)buf->b_ffname, 487 (int)buf->b_last_cursor.lnum); 488 #endif 489 vim_free(buf->b_ffname); 490 vim_free(buf->b_sfname); 491 if (buf->b_prev == NULL) 492 firstbuf = buf->b_next; 493 else 494 buf->b_prev->b_next = buf->b_next; 495 if (buf->b_next == NULL) 496 lastbuf = buf->b_prev; 497 else 498 buf->b_next->b_prev = buf->b_prev; 499 free_buffer(buf); 500 } 501 else 502 { 503 if (del_buf) 504 { 505 /* Free all internal variables and reset option values, to make 506 * ":bdel" compatible with Vim 5.7. */ 507 free_buffer_stuff(buf, TRUE); 508 509 /* Make it look like a new buffer. */ 510 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; 511 512 /* Init the options when loaded again. */ 513 buf->b_p_initialized = FALSE; 514 } 515 buf_clear_file(buf); 516 if (del_buf) 517 buf->b_p_bl = FALSE; 518 } 519 } 520 521 /* 522 * Make buffer not contain a file. 523 */ 524 void 525 buf_clear_file(buf) 526 buf_T *buf; 527 { 528 buf->b_ml.ml_line_count = 1; 529 unchanged(buf, TRUE); 530 #ifndef SHORT_FNAME 531 buf->b_shortname = FALSE; 532 #endif 533 buf->b_p_eol = TRUE; 534 buf->b_start_eol = TRUE; 535 #ifdef FEAT_MBYTE 536 buf->b_p_bomb = FALSE; 537 buf->b_start_bomb = FALSE; 538 #endif 539 buf->b_ml.ml_mfp = NULL; 540 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */ 541 #ifdef FEAT_NETBEANS_INTG 542 netbeans_deleted_all_lines(buf); 543 #endif 544 } 545 546 /* 547 * buf_freeall() - free all things allocated for a buffer that are related to 548 * the file. flags: 549 * BFA_DEL buffer is going to be deleted 550 * BFA_WIPE buffer is going to be wiped out 551 * BFA_KEEP_UNDO do not free undo information 552 */ 553 void 554 buf_freeall(buf, flags) 555 buf_T *buf; 556 int flags; 557 { 558 #ifdef FEAT_AUTOCMD 559 int is_curbuf = (buf == curbuf); 560 561 buf->b_closing = TRUE; 562 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf); 563 if (!buf_valid(buf)) /* autocommands may delete the buffer */ 564 return; 565 if ((flags & BFA_DEL) && buf->b_p_bl) 566 { 567 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf); 568 if (!buf_valid(buf)) /* autocommands may delete the buffer */ 569 return; 570 } 571 if (flags & BFA_WIPE) 572 { 573 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname, 574 FALSE, buf); 575 if (!buf_valid(buf)) /* autocommands may delete the buffer */ 576 return; 577 } 578 buf->b_closing = FALSE; 579 # ifdef FEAT_EVAL 580 if (aborting()) /* autocmds may abort script processing */ 581 return; 582 # endif 583 584 /* 585 * It's possible that autocommands change curbuf to the one being deleted. 586 * This might cause curbuf to be deleted unexpectedly. But in some cases 587 * it's OK to delete the curbuf, because a new one is obtained anyway. 588 * Therefore only return if curbuf changed to the deleted buffer. 589 */ 590 if (buf == curbuf && !is_curbuf) 591 return; 592 #endif 593 #ifdef FEAT_DIFF 594 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */ 595 #endif 596 #ifdef FEAT_SYN_HL 597 /* Remove any ownsyntax, unless exiting. */ 598 if (firstwin != NULL && curwin->w_buffer == buf) 599 reset_synblock(curwin); 600 #endif 601 602 #ifdef FEAT_FOLDING 603 /* No folds in an empty buffer. */ 604 # ifdef FEAT_WINDOWS 605 { 606 win_T *win; 607 tabpage_T *tp; 608 609 FOR_ALL_TAB_WINDOWS(tp, win) 610 if (win->w_buffer == buf) 611 clearFolding(win); 612 } 613 # else 614 if (curwin->w_buffer == buf) 615 clearFolding(curwin); 616 # endif 617 #endif 618 619 #ifdef FEAT_TCL 620 tcl_buffer_free(buf); 621 #endif 622 ml_close(buf, TRUE); /* close and delete the memline/memfile */ 623 buf->b_ml.ml_line_count = 0; /* no lines in buffer */ 624 if ((flags & BFA_KEEP_UNDO) == 0) 625 { 626 u_blockfree(buf); /* free the memory allocated for undo */ 627 u_clearall(buf); /* reset all undo information */ 628 } 629 #ifdef FEAT_SYN_HL 630 syntax_clear(&buf->b_s); /* reset syntax info */ 631 #endif 632 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */ 633 } 634 635 /* 636 * Free a buffer structure and the things it contains related to the buffer 637 * itself (not the file, that must have been done already). 638 */ 639 static void 640 free_buffer(buf) 641 buf_T *buf; 642 { 643 free_buffer_stuff(buf, TRUE); 644 #ifdef FEAT_LUA 645 lua_buffer_free(buf); 646 #endif 647 #ifdef FEAT_MZSCHEME 648 mzscheme_buffer_free(buf); 649 #endif 650 #ifdef FEAT_PERL 651 perl_buf_free(buf); 652 #endif 653 #ifdef FEAT_PYTHON 654 python_buffer_free(buf); 655 #endif 656 #ifdef FEAT_PYTHON3 657 python3_buffer_free(buf); 658 #endif 659 #ifdef FEAT_RUBY 660 ruby_buffer_free(buf); 661 #endif 662 #ifdef FEAT_AUTOCMD 663 aubuflocal_remove(buf); 664 #endif 665 vim_free(buf); 666 } 667 668 /* 669 * Free stuff in the buffer for ":bdel" and when wiping out the buffer. 670 */ 671 static void 672 free_buffer_stuff(buf, free_options) 673 buf_T *buf; 674 int free_options; /* free options as well */ 675 { 676 if (free_options) 677 { 678 clear_wininfo(buf); /* including window-local options */ 679 free_buf_options(buf, TRUE); 680 #ifdef FEAT_SPELL 681 ga_clear(&buf->b_s.b_langp); 682 #endif 683 } 684 #ifdef FEAT_EVAL 685 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */ 686 hash_init(&buf->b_vars.dv_hashtab); 687 #endif 688 #ifdef FEAT_USR_CMDS 689 uc_clear(&buf->b_ucmds); /* clear local user commands */ 690 #endif 691 #ifdef FEAT_SIGNS 692 buf_delete_signs(buf); /* delete any signs */ 693 #endif 694 #ifdef FEAT_NETBEANS_INTG 695 netbeans_file_killed(buf); 696 #endif 697 #ifdef FEAT_LOCALMAP 698 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */ 699 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */ 700 #endif 701 #ifdef FEAT_MBYTE 702 vim_free(buf->b_start_fenc); 703 buf->b_start_fenc = NULL; 704 #endif 705 } 706 707 /* 708 * Free the b_wininfo list for buffer "buf". 709 */ 710 static void 711 clear_wininfo(buf) 712 buf_T *buf; 713 { 714 wininfo_T *wip; 715 716 while (buf->b_wininfo != NULL) 717 { 718 wip = buf->b_wininfo; 719 buf->b_wininfo = wip->wi_next; 720 if (wip->wi_optset) 721 { 722 clear_winopt(&wip->wi_opt); 723 #ifdef FEAT_FOLDING 724 deleteFoldRecurse(&wip->wi_folds); 725 #endif 726 } 727 vim_free(wip); 728 } 729 } 730 731 #if defined(FEAT_LISTCMDS) || defined(PROTO) 732 /* 733 * Go to another buffer. Handles the result of the ATTENTION dialog. 734 */ 735 void 736 goto_buffer(eap, start, dir, count) 737 exarg_T *eap; 738 int start; 739 int dir; 740 int count; 741 { 742 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) 743 buf_T *old_curbuf = curbuf; 744 745 swap_exists_action = SEA_DIALOG; 746 # endif 747 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, 748 start, dir, count, eap->forceit); 749 # if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION) 750 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's') 751 { 752 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 753 cleanup_T cs; 754 755 /* Reset the error/interrupt/exception state here so that 756 * aborting() returns FALSE when closing a window. */ 757 enter_cleanup(&cs); 758 # endif 759 760 /* Quitting means closing the split window, nothing else. */ 761 win_close(curwin, TRUE); 762 swap_exists_action = SEA_NONE; 763 swap_exists_did_quit = TRUE; 764 765 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 766 /* Restore the error/interrupt/exception state if not discarded by a 767 * new aborting error, interrupt, or uncaught exception. */ 768 leave_cleanup(&cs); 769 # endif 770 } 771 else 772 handle_swap_exists(old_curbuf); 773 # endif 774 } 775 #endif 776 777 #if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO) 778 /* 779 * Handle the situation of swap_exists_action being set. 780 * It is allowed for "old_curbuf" to be NULL or invalid. 781 */ 782 void 783 handle_swap_exists(old_curbuf) 784 buf_T *old_curbuf; 785 { 786 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 787 cleanup_T cs; 788 # endif 789 790 if (swap_exists_action == SEA_QUIT) 791 { 792 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 793 /* Reset the error/interrupt/exception state here so that 794 * aborting() returns FALSE when closing a buffer. */ 795 enter_cleanup(&cs); 796 # endif 797 798 /* User selected Quit at ATTENTION prompt. Go back to previous 799 * buffer. If that buffer is gone or the same as the current one, 800 * open a new, empty buffer. */ 801 swap_exists_action = SEA_NONE; /* don't want it again */ 802 swap_exists_did_quit = TRUE; 803 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE); 804 if (!buf_valid(old_curbuf) || old_curbuf == curbuf) 805 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED); 806 if (old_curbuf != NULL) 807 enter_buffer(old_curbuf); 808 /* If "old_curbuf" is NULL we are in big trouble here... */ 809 810 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 811 /* Restore the error/interrupt/exception state if not discarded by a 812 * new aborting error, interrupt, or uncaught exception. */ 813 leave_cleanup(&cs); 814 # endif 815 } 816 else if (swap_exists_action == SEA_RECOVER) 817 { 818 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 819 /* Reset the error/interrupt/exception state here so that 820 * aborting() returns FALSE when closing a buffer. */ 821 enter_cleanup(&cs); 822 # endif 823 824 /* User selected Recover at ATTENTION prompt. */ 825 msg_scroll = TRUE; 826 ml_recover(); 827 MSG_PUTS("\n"); /* don't overwrite the last message */ 828 cmdline_row = msg_row; 829 do_modelines(0); 830 831 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 832 /* Restore the error/interrupt/exception state if not discarded by a 833 * new aborting error, interrupt, or uncaught exception. */ 834 leave_cleanup(&cs); 835 # endif 836 } 837 swap_exists_action = SEA_NONE; 838 } 839 #endif 840 841 #if defined(FEAT_LISTCMDS) || defined(PROTO) 842 /* 843 * do_bufdel() - delete or unload buffer(s) 844 * 845 * addr_count == 0: ":bdel" - delete current buffer 846 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete 847 * buffer "end_bnr", then any other arguments. 848 * addr_count == 2: ":N,N bdel" - delete buffers in range 849 * 850 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or 851 * DOBUF_DEL (":bdel") 852 * 853 * Returns error message or NULL 854 */ 855 char_u * 856 do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit) 857 int command; 858 char_u *arg; /* pointer to extra arguments */ 859 int addr_count; 860 int start_bnr; /* first buffer number in a range */ 861 int end_bnr; /* buffer nr or last buffer nr in a range */ 862 int forceit; 863 { 864 int do_current = 0; /* delete current buffer? */ 865 int deleted = 0; /* number of buffers deleted */ 866 char_u *errormsg = NULL; /* return value */ 867 int bnr; /* buffer number */ 868 char_u *p; 869 870 if (addr_count == 0) 871 { 872 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit); 873 } 874 else 875 { 876 if (addr_count == 2) 877 { 878 if (*arg) /* both range and argument is not allowed */ 879 return (char_u *)_(e_trailing); 880 bnr = start_bnr; 881 } 882 else /* addr_count == 1 */ 883 bnr = end_bnr; 884 885 for ( ;!got_int; ui_breakcheck()) 886 { 887 /* 888 * delete the current buffer last, otherwise when the 889 * current buffer is deleted, the next buffer becomes 890 * the current one and will be loaded, which may then 891 * also be deleted, etc. 892 */ 893 if (bnr == curbuf->b_fnum) 894 do_current = bnr; 895 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr, 896 forceit) == OK) 897 ++deleted; 898 899 /* 900 * find next buffer number to delete/unload 901 */ 902 if (addr_count == 2) 903 { 904 if (++bnr > end_bnr) 905 break; 906 } 907 else /* addr_count == 1 */ 908 { 909 arg = skipwhite(arg); 910 if (*arg == NUL) 911 break; 912 if (!VIM_ISDIGIT(*arg)) 913 { 914 p = skiptowhite_esc(arg); 915 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE); 916 if (bnr < 0) /* failed */ 917 break; 918 arg = p; 919 } 920 else 921 bnr = getdigits(&arg); 922 } 923 } 924 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST, 925 FORWARD, do_current, forceit) == OK) 926 ++deleted; 927 928 if (deleted == 0) 929 { 930 if (command == DOBUF_UNLOAD) 931 STRCPY(IObuff, _("E515: No buffers were unloaded")); 932 else if (command == DOBUF_DEL) 933 STRCPY(IObuff, _("E516: No buffers were deleted")); 934 else 935 STRCPY(IObuff, _("E517: No buffers were wiped out")); 936 errormsg = IObuff; 937 } 938 else if (deleted >= p_report) 939 { 940 if (command == DOBUF_UNLOAD) 941 { 942 if (deleted == 1) 943 MSG(_("1 buffer unloaded")); 944 else 945 smsg((char_u *)_("%d buffers unloaded"), deleted); 946 } 947 else if (command == DOBUF_DEL) 948 { 949 if (deleted == 1) 950 MSG(_("1 buffer deleted")); 951 else 952 smsg((char_u *)_("%d buffers deleted"), deleted); 953 } 954 else 955 { 956 if (deleted == 1) 957 MSG(_("1 buffer wiped out")); 958 else 959 smsg((char_u *)_("%d buffers wiped out"), deleted); 960 } 961 } 962 } 963 964 965 return errormsg; 966 } 967 968 /* 969 * Implementation of the commands for the buffer list. 970 * 971 * action == DOBUF_GOTO go to specified buffer 972 * action == DOBUF_SPLIT split window and go to specified buffer 973 * action == DOBUF_UNLOAD unload specified buffer(s) 974 * action == DOBUF_DEL delete specified buffer(s) from buffer list 975 * action == DOBUF_WIPE delete specified buffer(s) really 976 * 977 * start == DOBUF_CURRENT go to "count" buffer from current buffer 978 * start == DOBUF_FIRST go to "count" buffer from first buffer 979 * start == DOBUF_LAST go to "count" buffer from last buffer 980 * start == DOBUF_MOD go to "count" modified buffer from current buffer 981 * 982 * Return FAIL or OK. 983 */ 984 int 985 do_buffer(action, start, dir, count, forceit) 986 int action; 987 int start; 988 int dir; /* FORWARD or BACKWARD */ 989 int count; /* buffer number or number of buffers */ 990 int forceit; /* TRUE for :...! */ 991 { 992 buf_T *buf; 993 buf_T *bp; 994 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL 995 || action == DOBUF_WIPE); 996 997 switch (start) 998 { 999 case DOBUF_FIRST: buf = firstbuf; break; 1000 case DOBUF_LAST: buf = lastbuf; break; 1001 default: buf = curbuf; break; 1002 } 1003 if (start == DOBUF_MOD) /* find next modified buffer */ 1004 { 1005 while (count-- > 0) 1006 { 1007 do 1008 { 1009 buf = buf->b_next; 1010 if (buf == NULL) 1011 buf = firstbuf; 1012 } 1013 while (buf != curbuf && !bufIsChanged(buf)); 1014 } 1015 if (!bufIsChanged(buf)) 1016 { 1017 EMSG(_("E84: No modified buffer found")); 1018 return FAIL; 1019 } 1020 } 1021 else if (start == DOBUF_FIRST && count) /* find specified buffer number */ 1022 { 1023 while (buf != NULL && buf->b_fnum != count) 1024 buf = buf->b_next; 1025 } 1026 else 1027 { 1028 bp = NULL; 1029 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf)) 1030 { 1031 /* remember the buffer where we start, we come back there when all 1032 * buffers are unlisted. */ 1033 if (bp == NULL) 1034 bp = buf; 1035 if (dir == FORWARD) 1036 { 1037 buf = buf->b_next; 1038 if (buf == NULL) 1039 buf = firstbuf; 1040 } 1041 else 1042 { 1043 buf = buf->b_prev; 1044 if (buf == NULL) 1045 buf = lastbuf; 1046 } 1047 /* don't count unlisted buffers */ 1048 if (unload || buf->b_p_bl) 1049 { 1050 --count; 1051 bp = NULL; /* use this buffer as new starting point */ 1052 } 1053 if (bp == buf) 1054 { 1055 /* back where we started, didn't find anything. */ 1056 EMSG(_("E85: There is no listed buffer")); 1057 return FAIL; 1058 } 1059 } 1060 } 1061 1062 if (buf == NULL) /* could not find it */ 1063 { 1064 if (start == DOBUF_FIRST) 1065 { 1066 /* don't warn when deleting */ 1067 if (!unload) 1068 EMSGN(_("E86: Buffer %ld does not exist"), count); 1069 } 1070 else if (dir == FORWARD) 1071 EMSG(_("E87: Cannot go beyond last buffer")); 1072 else 1073 EMSG(_("E88: Cannot go before first buffer")); 1074 return FAIL; 1075 } 1076 1077 #ifdef FEAT_GUI 1078 need_mouse_correct = TRUE; 1079 #endif 1080 1081 #ifdef FEAT_LISTCMDS 1082 /* 1083 * delete buffer buf from memory and/or the list 1084 */ 1085 if (unload) 1086 { 1087 int forward; 1088 int retval; 1089 1090 /* When unloading or deleting a buffer that's already unloaded and 1091 * unlisted: fail silently. */ 1092 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl) 1093 return FAIL; 1094 1095 if (!forceit && bufIsChanged(buf)) 1096 { 1097 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 1098 if ((p_confirm || cmdmod.confirm) && p_write) 1099 { 1100 dialog_changed(buf, FALSE); 1101 # ifdef FEAT_AUTOCMD 1102 if (!buf_valid(buf)) 1103 /* Autocommand deleted buffer, oops! It's not changed 1104 * now. */ 1105 return FAIL; 1106 # endif 1107 /* If it's still changed fail silently, the dialog already 1108 * mentioned why it fails. */ 1109 if (bufIsChanged(buf)) 1110 return FAIL; 1111 } 1112 else 1113 #endif 1114 { 1115 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"), 1116 buf->b_fnum); 1117 return FAIL; 1118 } 1119 } 1120 1121 /* 1122 * If deleting the last (listed) buffer, make it empty. 1123 * The last (listed) buffer cannot be unloaded. 1124 */ 1125 for (bp = firstbuf; bp != NULL; bp = bp->b_next) 1126 if (bp->b_p_bl && bp != buf) 1127 break; 1128 if (bp == NULL && buf == curbuf) 1129 { 1130 if (action == DOBUF_UNLOAD) 1131 { 1132 EMSG(_("E90: Cannot unload last buffer")); 1133 return FAIL; 1134 } 1135 1136 /* Close any other windows on this buffer, then make it empty. */ 1137 #ifdef FEAT_WINDOWS 1138 close_windows(buf, TRUE); 1139 #endif 1140 setpcmark(); 1141 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, 1142 forceit ? ECMD_FORCEIT : 0, curwin); 1143 1144 /* 1145 * do_ecmd() may create a new buffer, then we have to delete 1146 * the old one. But do_ecmd() may have done that already, check 1147 * if the buffer still exists. 1148 */ 1149 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0) 1150 close_buffer(NULL, buf, action, FALSE); 1151 return retval; 1152 } 1153 1154 #ifdef FEAT_WINDOWS 1155 /* 1156 * If the deleted buffer is the current one, close the current window 1157 * (unless it's the only window). Repeat this so long as we end up in 1158 * a window with this buffer. 1159 */ 1160 while (buf == curbuf 1161 # ifdef FEAT_AUTOCMD 1162 && !(curwin->w_closing || curwin->w_buffer->b_closing) 1163 # endif 1164 && (firstwin != lastwin || first_tabpage->tp_next != NULL)) 1165 win_close(curwin, FALSE); 1166 #endif 1167 1168 /* 1169 * If the buffer to be deleted is not the current one, delete it here. 1170 */ 1171 if (buf != curbuf) 1172 { 1173 #ifdef FEAT_WINDOWS 1174 close_windows(buf, FALSE); 1175 #endif 1176 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0) 1177 close_buffer(NULL, buf, action, FALSE); 1178 return OK; 1179 } 1180 1181 /* 1182 * Deleting the current buffer: Need to find another buffer to go to. 1183 * There must be another, otherwise it would have been handled above. 1184 * First use au_new_curbuf, if it is valid. 1185 * Then prefer the buffer we most recently visited. 1186 * Else try to find one that is loaded, after the current buffer, 1187 * then before the current buffer. 1188 * Finally use any buffer. 1189 */ 1190 buf = NULL; /* selected buffer */ 1191 bp = NULL; /* used when no loaded buffer found */ 1192 #ifdef FEAT_AUTOCMD 1193 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf)) 1194 buf = au_new_curbuf; 1195 # ifdef FEAT_JUMPLIST 1196 else 1197 # endif 1198 #endif 1199 #ifdef FEAT_JUMPLIST 1200 if (curwin->w_jumplistlen > 0) 1201 { 1202 int jumpidx; 1203 1204 jumpidx = curwin->w_jumplistidx - 1; 1205 if (jumpidx < 0) 1206 jumpidx = curwin->w_jumplistlen - 1; 1207 1208 forward = jumpidx; 1209 while (jumpidx != curwin->w_jumplistidx) 1210 { 1211 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum); 1212 if (buf != NULL) 1213 { 1214 if (buf == curbuf || !buf->b_p_bl) 1215 buf = NULL; /* skip current and unlisted bufs */ 1216 else if (buf->b_ml.ml_mfp == NULL) 1217 { 1218 /* skip unloaded buf, but may keep it for later */ 1219 if (bp == NULL) 1220 bp = buf; 1221 buf = NULL; 1222 } 1223 } 1224 if (buf != NULL) /* found a valid buffer: stop searching */ 1225 break; 1226 /* advance to older entry in jump list */ 1227 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen) 1228 break; 1229 if (--jumpidx < 0) 1230 jumpidx = curwin->w_jumplistlen - 1; 1231 if (jumpidx == forward) /* List exhausted for sure */ 1232 break; 1233 } 1234 } 1235 #endif 1236 1237 if (buf == NULL) /* No previous buffer, Try 2'nd approach */ 1238 { 1239 forward = TRUE; 1240 buf = curbuf->b_next; 1241 for (;;) 1242 { 1243 if (buf == NULL) 1244 { 1245 if (!forward) /* tried both directions */ 1246 break; 1247 buf = curbuf->b_prev; 1248 forward = FALSE; 1249 continue; 1250 } 1251 /* in non-help buffer, try to skip help buffers, and vv */ 1252 if (buf->b_help == curbuf->b_help && buf->b_p_bl) 1253 { 1254 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */ 1255 break; 1256 if (bp == NULL) /* remember unloaded buf for later */ 1257 bp = buf; 1258 } 1259 if (forward) 1260 buf = buf->b_next; 1261 else 1262 buf = buf->b_prev; 1263 } 1264 } 1265 if (buf == NULL) /* No loaded buffer, use unloaded one */ 1266 buf = bp; 1267 if (buf == NULL) /* No loaded buffer, find listed one */ 1268 { 1269 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 1270 if (buf->b_p_bl && buf != curbuf) 1271 break; 1272 } 1273 if (buf == NULL) /* Still no buffer, just take one */ 1274 { 1275 if (curbuf->b_next != NULL) 1276 buf = curbuf->b_next; 1277 else 1278 buf = curbuf->b_prev; 1279 } 1280 } 1281 1282 /* 1283 * make buf current buffer 1284 */ 1285 if (action == DOBUF_SPLIT) /* split window first */ 1286 { 1287 # ifdef FEAT_WINDOWS 1288 /* If 'switchbuf' contains "useopen": jump to first window containing 1289 * "buf" if one exists */ 1290 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf)) 1291 return OK; 1292 /* If 'switchbuf' contains "usetab": jump to first window in any tab 1293 * page containing "buf" if one exists */ 1294 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf)) 1295 return OK; 1296 if (win_split(0, 0) == FAIL) 1297 # endif 1298 return FAIL; 1299 } 1300 #endif 1301 1302 /* go to current buffer - nothing to do */ 1303 if (buf == curbuf) 1304 return OK; 1305 1306 /* 1307 * Check if the current buffer may be abandoned. 1308 */ 1309 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit)) 1310 { 1311 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 1312 if ((p_confirm || cmdmod.confirm) && p_write) 1313 { 1314 dialog_changed(curbuf, FALSE); 1315 # ifdef FEAT_AUTOCMD 1316 if (!buf_valid(buf)) 1317 /* Autocommand deleted buffer, oops! */ 1318 return FAIL; 1319 # endif 1320 } 1321 if (bufIsChanged(curbuf)) 1322 #endif 1323 { 1324 EMSG(_(e_nowrtmsg)); 1325 return FAIL; 1326 } 1327 } 1328 1329 /* Go to the other buffer. */ 1330 set_curbuf(buf, action); 1331 1332 #if defined(FEAT_LISTCMDS) \ 1333 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND)) 1334 if (action == DOBUF_SPLIT) 1335 { 1336 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */ 1337 } 1338 #endif 1339 1340 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1341 if (aborting()) /* autocmds may abort script processing */ 1342 return FAIL; 1343 #endif 1344 1345 return OK; 1346 } 1347 1348 #endif /* FEAT_LISTCMDS */ 1349 1350 /* 1351 * Set current buffer to "buf". Executes autocommands and closes current 1352 * buffer. "action" tells how to close the current buffer: 1353 * DOBUF_GOTO free or hide it 1354 * DOBUF_SPLIT nothing 1355 * DOBUF_UNLOAD unload it 1356 * DOBUF_DEL delete it 1357 * DOBUF_WIPE wipe it out 1358 */ 1359 void 1360 set_curbuf(buf, action) 1361 buf_T *buf; 1362 int action; 1363 { 1364 buf_T *prevbuf; 1365 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL 1366 || action == DOBUF_WIPE); 1367 1368 setpcmark(); 1369 if (!cmdmod.keepalt) 1370 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */ 1371 buflist_altfpos(curwin); /* remember curpos */ 1372 1373 #ifdef FEAT_VISUAL 1374 /* Don't restart Select mode after switching to another buffer. */ 1375 VIsual_reselect = FALSE; 1376 #endif 1377 1378 /* close_windows() or apply_autocmds() may change curbuf */ 1379 prevbuf = curbuf; 1380 1381 #ifdef FEAT_AUTOCMD 1382 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf); 1383 # ifdef FEAT_EVAL 1384 if (buf_valid(prevbuf) && !aborting()) 1385 # else 1386 if (buf_valid(prevbuf)) 1387 # endif 1388 #endif 1389 { 1390 #ifdef FEAT_SYN_HL 1391 if (prevbuf == curwin->w_buffer) 1392 reset_synblock(curwin); 1393 #endif 1394 #ifdef FEAT_WINDOWS 1395 if (unload) 1396 close_windows(prevbuf, FALSE); 1397 #endif 1398 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1399 if (buf_valid(prevbuf) && !aborting()) 1400 #else 1401 if (buf_valid(prevbuf)) 1402 #endif 1403 { 1404 #ifdef FEAT_WINDOWS 1405 win_T *previouswin = curwin; 1406 #endif 1407 if (prevbuf == curbuf) 1408 u_sync(FALSE); 1409 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf, 1410 unload ? action : (action == DOBUF_GOTO 1411 && !P_HID(prevbuf) 1412 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE); 1413 #ifdef FEAT_WINDOWS 1414 if (curwin != previouswin && win_valid(previouswin)) 1415 /* autocommands changed curwin, Grr! */ 1416 curwin = previouswin; 1417 #endif 1418 } 1419 } 1420 #ifdef FEAT_AUTOCMD 1421 /* An autocommand may have deleted "buf", already entered it (e.g., when 1422 * it did ":bunload") or aborted the script processing! 1423 * If curwin->w_buffer is null, enter_buffer() will make it valid again */ 1424 if ((buf_valid(buf) && buf != curbuf 1425 # ifdef FEAT_EVAL 1426 && !aborting() 1427 # endif 1428 # ifdef FEAT_WINDOWS 1429 ) || curwin->w_buffer == NULL 1430 # endif 1431 ) 1432 #endif 1433 enter_buffer(buf); 1434 } 1435 1436 /* 1437 * Enter a new current buffer. 1438 * Old curbuf must have been abandoned already! 1439 */ 1440 void 1441 enter_buffer(buf) 1442 buf_T *buf; 1443 { 1444 #ifdef FEAT_SYN_HL 1445 long old_tw = curbuf->b_p_tw; 1446 #endif 1447 1448 /* Copy buffer and window local option values. Not for a help buffer. */ 1449 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); 1450 if (!buf->b_help) 1451 get_winopts(buf); 1452 #ifdef FEAT_FOLDING 1453 else 1454 /* Remove all folds in the window. */ 1455 clearFolding(curwin); 1456 foldUpdateAll(curwin); /* update folds (later). */ 1457 #endif 1458 1459 /* Get the buffer in the current window. */ 1460 curwin->w_buffer = buf; 1461 curbuf = buf; 1462 ++curbuf->b_nwindows; 1463 1464 #ifdef FEAT_DIFF 1465 if (curwin->w_p_diff) 1466 diff_buf_add(curbuf); 1467 #endif 1468 1469 #ifdef FEAT_SYN_HL 1470 curwin->w_s = &(buf->b_s); 1471 if (old_tw != buf->b_p_tw) 1472 check_colorcolumn(curwin); 1473 #endif 1474 1475 /* Cursor on first line by default. */ 1476 curwin->w_cursor.lnum = 1; 1477 curwin->w_cursor.col = 0; 1478 #ifdef FEAT_VIRTUALEDIT 1479 curwin->w_cursor.coladd = 0; 1480 #endif 1481 curwin->w_set_curswant = TRUE; 1482 #ifdef FEAT_AUTOCMD 1483 curwin->w_topline_was_set = FALSE; 1484 #endif 1485 1486 /* mark cursor position as being invalid */ 1487 curwin->w_valid = 0; 1488 1489 /* Make sure the buffer is loaded. */ 1490 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */ 1491 { 1492 #ifdef FEAT_AUTOCMD 1493 /* If there is no filetype, allow for detecting one. Esp. useful for 1494 * ":ball" used in a autocommand. If there already is a filetype we 1495 * might prefer to keep it. */ 1496 if (*curbuf->b_p_ft == NUL) 1497 did_filetype = FALSE; 1498 #endif 1499 1500 open_buffer(FALSE, NULL, 0); 1501 } 1502 else 1503 { 1504 if (!msg_silent) 1505 need_fileinfo = TRUE; /* display file info after redraw */ 1506 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */ 1507 #ifdef FEAT_AUTOCMD 1508 curwin->w_topline = 1; 1509 # ifdef FEAT_DIFF 1510 curwin->w_topfill = 0; 1511 # endif 1512 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); 1513 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf); 1514 #endif 1515 } 1516 1517 /* If autocommands did not change the cursor position, restore cursor lnum 1518 * and possibly cursor col. */ 1519 if (curwin->w_cursor.lnum == 1 && inindent(0)) 1520 buflist_getfpos(); 1521 1522 check_arg_idx(curwin); /* check for valid arg_idx */ 1523 #ifdef FEAT_TITLE 1524 maketitle(); 1525 #endif 1526 #ifdef FEAT_AUTOCMD 1527 /* when autocmds didn't change it */ 1528 if (curwin->w_topline == 1 && !curwin->w_topline_was_set) 1529 #endif 1530 scroll_cursor_halfway(FALSE); /* redisplay at correct position */ 1531 1532 #ifdef FEAT_NETBEANS_INTG 1533 /* Send fileOpened event because we've changed buffers. */ 1534 netbeans_file_activated(curbuf); 1535 #endif 1536 1537 /* Change directories when the 'acd' option is set. */ 1538 DO_AUTOCHDIR 1539 1540 #ifdef FEAT_KEYMAP 1541 if (curbuf->b_kmap_state & KEYMAP_INIT) 1542 (void)keymap_init(); 1543 #endif 1544 #ifdef FEAT_SPELL 1545 /* May need to set the spell language. Can only do this after the buffer 1546 * has been properly setup. */ 1547 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL) 1548 (void)did_set_spelllang(curwin); 1549 #endif 1550 1551 redraw_later(NOT_VALID); 1552 } 1553 1554 #if defined(FEAT_AUTOCHDIR) || defined(PROTO) 1555 /* 1556 * Change to the directory of the current buffer. 1557 */ 1558 void 1559 do_autochdir() 1560 { 1561 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK) 1562 shorten_fnames(TRUE); 1563 } 1564 #endif 1565 1566 /* 1567 * functions for dealing with the buffer list 1568 */ 1569 1570 /* 1571 * Add a file name to the buffer list. Return a pointer to the buffer. 1572 * If the same file name already exists return a pointer to that buffer. 1573 * If it does not exist, or if fname == NULL, a new entry is created. 1574 * If (flags & BLN_CURBUF) is TRUE, may use current buffer. 1575 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list. 1576 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer. 1577 * This is the ONLY way to create a new buffer. 1578 */ 1579 static int top_file_num = 1; /* highest file number */ 1580 1581 buf_T * 1582 buflist_new(ffname, sfname, lnum, flags) 1583 char_u *ffname; /* full path of fname or relative */ 1584 char_u *sfname; /* short fname or NULL */ 1585 linenr_T lnum; /* preferred cursor line */ 1586 int flags; /* BLN_ defines */ 1587 { 1588 buf_T *buf; 1589 #ifdef UNIX 1590 struct stat st; 1591 #endif 1592 1593 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */ 1594 1595 /* 1596 * If file name already exists in the list, update the entry. 1597 */ 1598 #ifdef UNIX 1599 /* On Unix we can use inode numbers when the file exists. Works better 1600 * for hard links. */ 1601 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0) 1602 st.st_dev = (dev_T)-1; 1603 #endif 1604 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf = 1605 #ifdef UNIX 1606 buflist_findname_stat(ffname, &st) 1607 #else 1608 buflist_findname(ffname) 1609 #endif 1610 ) != NULL) 1611 { 1612 vim_free(ffname); 1613 if (lnum != 0) 1614 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE); 1615 /* copy the options now, if 'cpo' doesn't have 's' and not done 1616 * already */ 1617 buf_copy_options(buf, 0); 1618 if ((flags & BLN_LISTED) && !buf->b_p_bl) 1619 { 1620 buf->b_p_bl = TRUE; 1621 #ifdef FEAT_AUTOCMD 1622 if (!(flags & BLN_DUMMY)) 1623 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); 1624 #endif 1625 } 1626 return buf; 1627 } 1628 1629 /* 1630 * If the current buffer has no name and no contents, use the current 1631 * buffer. Otherwise: Need to allocate a new buffer structure. 1632 * 1633 * This is the ONLY place where a new buffer structure is allocated! 1634 * (A spell file buffer is allocated in spell.c, but that's not a normal 1635 * buffer.) 1636 */ 1637 buf = NULL; 1638 if ((flags & BLN_CURBUF) 1639 && curbuf != NULL 1640 && curbuf->b_ffname == NULL 1641 && curbuf->b_nwindows <= 1 1642 && (curbuf->b_ml.ml_mfp == NULL || bufempty())) 1643 { 1644 buf = curbuf; 1645 #ifdef FEAT_AUTOCMD 1646 /* It's like this buffer is deleted. Watch out for autocommands that 1647 * change curbuf! If that happens, allocate a new buffer anyway. */ 1648 if (curbuf->b_p_bl) 1649 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); 1650 if (buf == curbuf) 1651 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); 1652 # ifdef FEAT_EVAL 1653 if (aborting()) /* autocmds may abort script processing */ 1654 return NULL; 1655 # endif 1656 #endif 1657 #ifdef FEAT_QUICKFIX 1658 # ifdef FEAT_AUTOCMD 1659 if (buf == curbuf) 1660 # endif 1661 { 1662 /* Make sure 'bufhidden' and 'buftype' are empty */ 1663 clear_string_option(&buf->b_p_bh); 1664 clear_string_option(&buf->b_p_bt); 1665 } 1666 #endif 1667 } 1668 if (buf != curbuf || curbuf == NULL) 1669 { 1670 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T)); 1671 if (buf == NULL) 1672 { 1673 vim_free(ffname); 1674 return NULL; 1675 } 1676 } 1677 1678 if (ffname != NULL) 1679 { 1680 buf->b_ffname = ffname; 1681 buf->b_sfname = vim_strsave(sfname); 1682 } 1683 1684 clear_wininfo(buf); 1685 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); 1686 1687 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL)) 1688 || buf->b_wininfo == NULL) 1689 { 1690 vim_free(buf->b_ffname); 1691 buf->b_ffname = NULL; 1692 vim_free(buf->b_sfname); 1693 buf->b_sfname = NULL; 1694 if (buf != curbuf) 1695 free_buffer(buf); 1696 return NULL; 1697 } 1698 1699 if (buf == curbuf) 1700 { 1701 /* free all things allocated for this buffer */ 1702 buf_freeall(buf, 0); 1703 if (buf != curbuf) /* autocommands deleted the buffer! */ 1704 return NULL; 1705 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1706 if (aborting()) /* autocmds may abort script processing */ 1707 return NULL; 1708 #endif 1709 /* buf->b_nwindows = 0; why was this here? */ 1710 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */ 1711 1712 /* Init the options. */ 1713 buf->b_p_initialized = FALSE; 1714 buf_copy_options(buf, BCO_ENTER); 1715 1716 #ifdef FEAT_KEYMAP 1717 /* need to reload lmaps and set b:keymap_name */ 1718 curbuf->b_kmap_state |= KEYMAP_INIT; 1719 #endif 1720 } 1721 else 1722 { 1723 /* 1724 * put new buffer at the end of the buffer list 1725 */ 1726 buf->b_next = NULL; 1727 if (firstbuf == NULL) /* buffer list is empty */ 1728 { 1729 buf->b_prev = NULL; 1730 firstbuf = buf; 1731 } 1732 else /* append new buffer at end of list */ 1733 { 1734 lastbuf->b_next = buf; 1735 buf->b_prev = lastbuf; 1736 } 1737 lastbuf = buf; 1738 1739 buf->b_fnum = top_file_num++; 1740 if (top_file_num < 0) /* wrap around (may cause duplicates) */ 1741 { 1742 EMSG(_("W14: Warning: List of file names overflow")); 1743 if (emsg_silent == 0) 1744 { 1745 out_flush(); 1746 ui_delay(3000L, TRUE); /* make sure it is noticed */ 1747 } 1748 top_file_num = 1; 1749 } 1750 1751 /* 1752 * Always copy the options from the current buffer. 1753 */ 1754 buf_copy_options(buf, BCO_ALWAYS); 1755 } 1756 1757 buf->b_wininfo->wi_fpos.lnum = lnum; 1758 buf->b_wininfo->wi_win = curwin; 1759 1760 #ifdef FEAT_EVAL 1761 /* init b: variables */ 1762 init_var_dict(&buf->b_vars, &buf->b_bufvar, VAR_SCOPE); 1763 #endif 1764 #ifdef FEAT_SYN_HL 1765 hash_init(&buf->b_s.b_keywtab); 1766 hash_init(&buf->b_s.b_keywtab_ic); 1767 #endif 1768 1769 buf->b_fname = buf->b_sfname; 1770 #ifdef UNIX 1771 if (st.st_dev == (dev_T)-1) 1772 buf->b_dev_valid = FALSE; 1773 else 1774 { 1775 buf->b_dev_valid = TRUE; 1776 buf->b_dev = st.st_dev; 1777 buf->b_ino = st.st_ino; 1778 } 1779 #endif 1780 buf->b_u_synced = TRUE; 1781 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; 1782 if (flags & BLN_DUMMY) 1783 buf->b_flags |= BF_DUMMY; 1784 buf_clear_file(buf); 1785 clrallmarks(buf); /* clear marks */ 1786 fmarks_check_names(buf); /* check file marks for this file */ 1787 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */ 1788 #ifdef FEAT_AUTOCMD 1789 if (!(flags & BLN_DUMMY)) 1790 { 1791 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf); 1792 if (flags & BLN_LISTED) 1793 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); 1794 # ifdef FEAT_EVAL 1795 if (aborting()) /* autocmds may abort script processing */ 1796 return NULL; 1797 # endif 1798 } 1799 #endif 1800 1801 return buf; 1802 } 1803 1804 /* 1805 * Free the memory for the options of a buffer. 1806 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and 1807 * 'fileencoding'. 1808 */ 1809 void 1810 free_buf_options(buf, free_p_ff) 1811 buf_T *buf; 1812 int free_p_ff; 1813 { 1814 if (free_p_ff) 1815 { 1816 #ifdef FEAT_MBYTE 1817 clear_string_option(&buf->b_p_fenc); 1818 #endif 1819 clear_string_option(&buf->b_p_ff); 1820 #ifdef FEAT_QUICKFIX 1821 clear_string_option(&buf->b_p_bh); 1822 clear_string_option(&buf->b_p_bt); 1823 #endif 1824 } 1825 #ifdef FEAT_FIND_ID 1826 clear_string_option(&buf->b_p_def); 1827 clear_string_option(&buf->b_p_inc); 1828 # ifdef FEAT_EVAL 1829 clear_string_option(&buf->b_p_inex); 1830 # endif 1831 #endif 1832 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL) 1833 clear_string_option(&buf->b_p_inde); 1834 clear_string_option(&buf->b_p_indk); 1835 #endif 1836 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) 1837 clear_string_option(&buf->b_p_bexpr); 1838 #endif 1839 #if defined(FEAT_CRYPT) 1840 clear_string_option(&buf->b_p_cm); 1841 #endif 1842 #if defined(FEAT_EVAL) 1843 clear_string_option(&buf->b_p_fex); 1844 #endif 1845 #ifdef FEAT_CRYPT 1846 clear_string_option(&buf->b_p_key); 1847 #endif 1848 clear_string_option(&buf->b_p_kp); 1849 clear_string_option(&buf->b_p_mps); 1850 clear_string_option(&buf->b_p_fo); 1851 clear_string_option(&buf->b_p_flp); 1852 clear_string_option(&buf->b_p_isk); 1853 #ifdef FEAT_KEYMAP 1854 clear_string_option(&buf->b_p_keymap); 1855 ga_clear(&buf->b_kmap_ga); 1856 #endif 1857 #ifdef FEAT_COMMENTS 1858 clear_string_option(&buf->b_p_com); 1859 #endif 1860 #ifdef FEAT_FOLDING 1861 clear_string_option(&buf->b_p_cms); 1862 #endif 1863 clear_string_option(&buf->b_p_nf); 1864 #ifdef FEAT_SYN_HL 1865 clear_string_option(&buf->b_p_syn); 1866 #endif 1867 #ifdef FEAT_SPELL 1868 clear_string_option(&buf->b_s.b_p_spc); 1869 clear_string_option(&buf->b_s.b_p_spf); 1870 vim_free(buf->b_s.b_cap_prog); 1871 buf->b_s.b_cap_prog = NULL; 1872 clear_string_option(&buf->b_s.b_p_spl); 1873 #endif 1874 #ifdef FEAT_SEARCHPATH 1875 clear_string_option(&buf->b_p_sua); 1876 #endif 1877 #ifdef FEAT_AUTOCMD 1878 clear_string_option(&buf->b_p_ft); 1879 #endif 1880 #ifdef FEAT_CINDENT 1881 clear_string_option(&buf->b_p_cink); 1882 clear_string_option(&buf->b_p_cino); 1883 #endif 1884 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) 1885 clear_string_option(&buf->b_p_cinw); 1886 #endif 1887 #ifdef FEAT_INS_EXPAND 1888 clear_string_option(&buf->b_p_cpt); 1889 #endif 1890 #ifdef FEAT_COMPL_FUNC 1891 clear_string_option(&buf->b_p_cfu); 1892 clear_string_option(&buf->b_p_ofu); 1893 #endif 1894 #ifdef FEAT_QUICKFIX 1895 clear_string_option(&buf->b_p_gp); 1896 clear_string_option(&buf->b_p_mp); 1897 clear_string_option(&buf->b_p_efm); 1898 #endif 1899 clear_string_option(&buf->b_p_ep); 1900 clear_string_option(&buf->b_p_path); 1901 clear_string_option(&buf->b_p_tags); 1902 #ifdef FEAT_INS_EXPAND 1903 clear_string_option(&buf->b_p_dict); 1904 clear_string_option(&buf->b_p_tsr); 1905 #endif 1906 #ifdef FEAT_TEXTOBJ 1907 clear_string_option(&buf->b_p_qe); 1908 #endif 1909 buf->b_p_ar = -1; 1910 } 1911 1912 /* 1913 * get alternate file n 1914 * set linenr to lnum or altfpos.lnum if lnum == 0 1915 * also set cursor column to altfpos.col if 'startofline' is not set. 1916 * if (options & GETF_SETMARK) call setpcmark() 1917 * if (options & GETF_ALT) we are jumping to an alternate file. 1918 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping 1919 * 1920 * return FAIL for failure, OK for success 1921 */ 1922 int 1923 buflist_getfile(n, lnum, options, forceit) 1924 int n; 1925 linenr_T lnum; 1926 int options; 1927 int forceit; 1928 { 1929 buf_T *buf; 1930 #ifdef FEAT_WINDOWS 1931 win_T *wp = NULL; 1932 #endif 1933 pos_T *fpos; 1934 colnr_T col; 1935 1936 buf = buflist_findnr(n); 1937 if (buf == NULL) 1938 { 1939 if ((options & GETF_ALT) && n == 0) 1940 EMSG(_(e_noalt)); 1941 else 1942 EMSGN(_("E92: Buffer %ld not found"), n); 1943 return FAIL; 1944 } 1945 1946 /* if alternate file is the current buffer, nothing to do */ 1947 if (buf == curbuf) 1948 return OK; 1949 1950 if (text_locked()) 1951 { 1952 text_locked_msg(); 1953 return FAIL; 1954 } 1955 #ifdef FEAT_AUTOCMD 1956 if (curbuf_locked()) 1957 return FAIL; 1958 #endif 1959 1960 /* altfpos may be changed by getfile(), get it now */ 1961 if (lnum == 0) 1962 { 1963 fpos = buflist_findfpos(buf); 1964 lnum = fpos->lnum; 1965 col = fpos->col; 1966 } 1967 else 1968 col = 0; 1969 1970 #ifdef FEAT_WINDOWS 1971 if (options & GETF_SWITCH) 1972 { 1973 /* If 'switchbuf' contains "useopen": jump to first window containing 1974 * "buf" if one exists */ 1975 if (swb_flags & SWB_USEOPEN) 1976 wp = buf_jump_open_win(buf); 1977 /* If 'switchbuf' contians "usetab": jump to first window in any tab 1978 * page containing "buf" if one exists */ 1979 if (wp == NULL && (swb_flags & SWB_USETAB)) 1980 wp = buf_jump_open_tab(buf); 1981 /* If 'switchbuf' contains "split" or "newtab" and the current buffer 1982 * isn't empty: open new window */ 1983 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty()) 1984 { 1985 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */ 1986 tabpage_new(); 1987 else if (win_split(0, 0) == FAIL) /* Open in a new window */ 1988 return FAIL; 1989 RESET_BINDING(curwin); 1990 } 1991 } 1992 #endif 1993 1994 ++RedrawingDisabled; 1995 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK), 1996 lnum, forceit) <= 0) 1997 { 1998 --RedrawingDisabled; 1999 2000 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */ 2001 if (!p_sol && col != 0) 2002 { 2003 curwin->w_cursor.col = col; 2004 check_cursor_col(); 2005 #ifdef FEAT_VIRTUALEDIT 2006 curwin->w_cursor.coladd = 0; 2007 #endif 2008 curwin->w_set_curswant = TRUE; 2009 } 2010 return OK; 2011 } 2012 --RedrawingDisabled; 2013 return FAIL; 2014 } 2015 2016 /* 2017 * go to the last know line number for the current buffer 2018 */ 2019 void 2020 buflist_getfpos() 2021 { 2022 pos_T *fpos; 2023 2024 fpos = buflist_findfpos(curbuf); 2025 2026 curwin->w_cursor.lnum = fpos->lnum; 2027 check_cursor_lnum(); 2028 2029 if (p_sol) 2030 curwin->w_cursor.col = 0; 2031 else 2032 { 2033 curwin->w_cursor.col = fpos->col; 2034 check_cursor_col(); 2035 #ifdef FEAT_VIRTUALEDIT 2036 curwin->w_cursor.coladd = 0; 2037 #endif 2038 curwin->w_set_curswant = TRUE; 2039 } 2040 } 2041 2042 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO) 2043 /* 2044 * Find file in buffer list by name (it has to be for the current window). 2045 * Returns NULL if not found. 2046 */ 2047 buf_T * 2048 buflist_findname_exp(fname) 2049 char_u *fname; 2050 { 2051 char_u *ffname; 2052 buf_T *buf = NULL; 2053 2054 /* First make the name into a full path name */ 2055 ffname = FullName_save(fname, 2056 #ifdef UNIX 2057 TRUE /* force expansion, get rid of symbolic links */ 2058 #else 2059 FALSE 2060 #endif 2061 ); 2062 if (ffname != NULL) 2063 { 2064 buf = buflist_findname(ffname); 2065 vim_free(ffname); 2066 } 2067 return buf; 2068 } 2069 #endif 2070 2071 /* 2072 * Find file in buffer list by name (it has to be for the current window). 2073 * "ffname" must have a full path. 2074 * Skips dummy buffers. 2075 * Returns NULL if not found. 2076 */ 2077 buf_T * 2078 buflist_findname(ffname) 2079 char_u *ffname; 2080 { 2081 #ifdef UNIX 2082 struct stat st; 2083 2084 if (mch_stat((char *)ffname, &st) < 0) 2085 st.st_dev = (dev_T)-1; 2086 return buflist_findname_stat(ffname, &st); 2087 } 2088 2089 /* 2090 * Same as buflist_findname(), but pass the stat structure to avoid getting it 2091 * twice for the same file. 2092 * Returns NULL if not found. 2093 */ 2094 static buf_T * 2095 buflist_findname_stat(ffname, stp) 2096 char_u *ffname; 2097 struct stat *stp; 2098 { 2099 #endif 2100 buf_T *buf; 2101 2102 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2103 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname 2104 #ifdef UNIX 2105 , stp 2106 #endif 2107 )) 2108 return buf; 2109 return NULL; 2110 } 2111 2112 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO) 2113 /* 2114 * Find file in buffer list by a regexp pattern. 2115 * Return fnum of the found buffer. 2116 * Return < 0 for error. 2117 */ 2118 int 2119 buflist_findpat(pattern, pattern_end, unlisted, diffmode) 2120 char_u *pattern; 2121 char_u *pattern_end; /* pointer to first char after pattern */ 2122 int unlisted; /* find unlisted buffers */ 2123 int diffmode UNUSED; /* find diff-mode buffers only */ 2124 { 2125 buf_T *buf; 2126 regprog_T *prog; 2127 int match = -1; 2128 int find_listed; 2129 char_u *pat; 2130 char_u *patend; 2131 int attempt; 2132 char_u *p; 2133 int toggledollar; 2134 2135 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) 2136 { 2137 if (*pattern == '%') 2138 match = curbuf->b_fnum; 2139 else 2140 match = curwin->w_alt_fnum; 2141 #ifdef FEAT_DIFF 2142 if (diffmode && !diff_mode_buf(buflist_findnr(match))) 2143 match = -1; 2144 #endif 2145 } 2146 2147 /* 2148 * Try four ways of matching a listed buffer: 2149 * attempt == 0: without '^' or '$' (at any position) 2150 * attempt == 1: with '^' at start (only at position 0) 2151 * attempt == 2: with '$' at end (only match at end) 2152 * attempt == 3: with '^' at start and '$' at end (only full match) 2153 * Repeat this for finding an unlisted buffer if there was no matching 2154 * listed buffer. 2155 */ 2156 else 2157 { 2158 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE); 2159 if (pat == NULL) 2160 return -1; 2161 patend = pat + STRLEN(pat) - 1; 2162 toggledollar = (patend > pat && *patend == '$'); 2163 2164 /* First try finding a listed buffer. If not found and "unlisted" 2165 * is TRUE, try finding an unlisted buffer. */ 2166 find_listed = TRUE; 2167 for (;;) 2168 { 2169 for (attempt = 0; attempt <= 3; ++attempt) 2170 { 2171 /* may add '^' and '$' */ 2172 if (toggledollar) 2173 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */ 2174 p = pat; 2175 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */ 2176 ++p; 2177 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); 2178 if (prog == NULL) 2179 { 2180 vim_free(pat); 2181 return -1; 2182 } 2183 2184 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2185 if (buf->b_p_bl == find_listed 2186 #ifdef FEAT_DIFF 2187 && (!diffmode || diff_mode_buf(buf)) 2188 #endif 2189 && buflist_match(prog, buf) != NULL) 2190 { 2191 if (match >= 0) /* already found a match */ 2192 { 2193 match = -2; 2194 break; 2195 } 2196 match = buf->b_fnum; /* remember first match */ 2197 } 2198 2199 vim_free(prog); 2200 if (match >= 0) /* found one match */ 2201 break; 2202 } 2203 2204 /* Only search for unlisted buffers if there was no match with 2205 * a listed buffer. */ 2206 if (!unlisted || !find_listed || match != -1) 2207 break; 2208 find_listed = FALSE; 2209 } 2210 2211 vim_free(pat); 2212 } 2213 2214 if (match == -2) 2215 EMSG2(_("E93: More than one match for %s"), pattern); 2216 else if (match < 0) 2217 EMSG2(_("E94: No matching buffer for %s"), pattern); 2218 return match; 2219 } 2220 #endif 2221 2222 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2223 2224 /* 2225 * Find all buffer names that match. 2226 * For command line expansion of ":buf" and ":sbuf". 2227 * Return OK if matches found, FAIL otherwise. 2228 */ 2229 int 2230 ExpandBufnames(pat, num_file, file, options) 2231 char_u *pat; 2232 int *num_file; 2233 char_u ***file; 2234 int options; 2235 { 2236 int count = 0; 2237 buf_T *buf; 2238 int round; 2239 char_u *p; 2240 int attempt; 2241 regprog_T *prog; 2242 char_u *patc; 2243 2244 *num_file = 0; /* return values in case of FAIL */ 2245 *file = NULL; 2246 2247 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */ 2248 if (*pat == '^') 2249 { 2250 patc = alloc((unsigned)STRLEN(pat) + 11); 2251 if (patc == NULL) 2252 return FAIL; 2253 STRCPY(patc, "\\(^\\|[\\/]\\)"); 2254 STRCPY(patc + 11, pat + 1); 2255 } 2256 else 2257 patc = pat; 2258 2259 /* 2260 * attempt == 0: try match with '\<', match at start of word 2261 * attempt == 1: try match without '\<', match anywhere 2262 */ 2263 for (attempt = 0; attempt <= 1; ++attempt) 2264 { 2265 if (attempt > 0 && patc == pat) 2266 break; /* there was no anchor, no need to try again */ 2267 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC); 2268 if (prog == NULL) 2269 { 2270 if (patc != pat) 2271 vim_free(patc); 2272 return FAIL; 2273 } 2274 2275 /* 2276 * round == 1: Count the matches. 2277 * round == 2: Build the array to keep the matches. 2278 */ 2279 for (round = 1; round <= 2; ++round) 2280 { 2281 count = 0; 2282 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2283 { 2284 if (!buf->b_p_bl) /* skip unlisted buffers */ 2285 continue; 2286 p = buflist_match(prog, buf); 2287 if (p != NULL) 2288 { 2289 if (round == 1) 2290 ++count; 2291 else 2292 { 2293 if (options & WILD_HOME_REPLACE) 2294 p = home_replace_save(buf, p); 2295 else 2296 p = vim_strsave(p); 2297 (*file)[count++] = p; 2298 } 2299 } 2300 } 2301 if (count == 0) /* no match found, break here */ 2302 break; 2303 if (round == 1) 2304 { 2305 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); 2306 if (*file == NULL) 2307 { 2308 vim_free(prog); 2309 if (patc != pat) 2310 vim_free(patc); 2311 return FAIL; 2312 } 2313 } 2314 } 2315 vim_free(prog); 2316 if (count) /* match(es) found, break here */ 2317 break; 2318 } 2319 2320 if (patc != pat) 2321 vim_free(patc); 2322 2323 *num_file = count; 2324 return (count == 0 ? FAIL : OK); 2325 } 2326 2327 #endif /* FEAT_CMDL_COMPL */ 2328 2329 #ifdef HAVE_BUFLIST_MATCH 2330 /* 2331 * Check for a match on the file name for buffer "buf" with regprog "prog". 2332 */ 2333 static char_u * 2334 buflist_match(prog, buf) 2335 regprog_T *prog; 2336 buf_T *buf; 2337 { 2338 char_u *match; 2339 2340 /* First try the short file name, then the long file name. */ 2341 match = fname_match(prog, buf->b_sfname); 2342 if (match == NULL) 2343 match = fname_match(prog, buf->b_ffname); 2344 2345 return match; 2346 } 2347 2348 /* 2349 * Try matching the regexp in "prog" with file name "name". 2350 * Return "name" when there is a match, NULL when not. 2351 */ 2352 static char_u * 2353 fname_match(prog, name) 2354 regprog_T *prog; 2355 char_u *name; 2356 { 2357 char_u *match = NULL; 2358 char_u *p; 2359 regmatch_T regmatch; 2360 2361 if (name != NULL) 2362 { 2363 regmatch.regprog = prog; 2364 #ifdef CASE_INSENSITIVE_FILENAME 2365 regmatch.rm_ic = TRUE; /* Always ignore case */ 2366 #else 2367 regmatch.rm_ic = FALSE; /* Never ignore case */ 2368 #endif 2369 2370 if (vim_regexec(®match, name, (colnr_T)0)) 2371 match = name; 2372 else 2373 { 2374 /* Replace $(HOME) with '~' and try matching again. */ 2375 p = home_replace_save(NULL, name); 2376 if (p != NULL && vim_regexec(®match, p, (colnr_T)0)) 2377 match = name; 2378 vim_free(p); 2379 } 2380 } 2381 2382 return match; 2383 } 2384 #endif 2385 2386 /* 2387 * find file in buffer list by number 2388 */ 2389 buf_T * 2390 buflist_findnr(nr) 2391 int nr; 2392 { 2393 buf_T *buf; 2394 2395 if (nr == 0) 2396 nr = curwin->w_alt_fnum; 2397 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2398 if (buf->b_fnum == nr) 2399 return (buf); 2400 return NULL; 2401 } 2402 2403 /* 2404 * Get name of file 'n' in the buffer list. 2405 * When the file has no name an empty string is returned. 2406 * home_replace() is used to shorten the file name (used for marks). 2407 * Returns a pointer to allocated memory, of NULL when failed. 2408 */ 2409 char_u * 2410 buflist_nr2name(n, fullname, helptail) 2411 int n; 2412 int fullname; 2413 int helptail; /* for help buffers return tail only */ 2414 { 2415 buf_T *buf; 2416 2417 buf = buflist_findnr(n); 2418 if (buf == NULL) 2419 return NULL; 2420 return home_replace_save(helptail ? buf : NULL, 2421 fullname ? buf->b_ffname : buf->b_fname); 2422 } 2423 2424 /* 2425 * Set the "lnum" and "col" for the buffer "buf" and the current window. 2426 * When "copy_options" is TRUE save the local window option values. 2427 * When "lnum" is 0 only do the options. 2428 */ 2429 static void 2430 buflist_setfpos(buf, win, lnum, col, copy_options) 2431 buf_T *buf; 2432 win_T *win; 2433 linenr_T lnum; 2434 colnr_T col; 2435 int copy_options; 2436 { 2437 wininfo_T *wip; 2438 2439 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2440 if (wip->wi_win == win) 2441 break; 2442 if (wip == NULL) 2443 { 2444 /* allocate a new entry */ 2445 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); 2446 if (wip == NULL) 2447 return; 2448 wip->wi_win = win; 2449 if (lnum == 0) /* set lnum even when it's 0 */ 2450 lnum = 1; 2451 } 2452 else 2453 { 2454 /* remove the entry from the list */ 2455 if (wip->wi_prev) 2456 wip->wi_prev->wi_next = wip->wi_next; 2457 else 2458 buf->b_wininfo = wip->wi_next; 2459 if (wip->wi_next) 2460 wip->wi_next->wi_prev = wip->wi_prev; 2461 if (copy_options && wip->wi_optset) 2462 { 2463 clear_winopt(&wip->wi_opt); 2464 #ifdef FEAT_FOLDING 2465 deleteFoldRecurse(&wip->wi_folds); 2466 #endif 2467 } 2468 } 2469 if (lnum != 0) 2470 { 2471 wip->wi_fpos.lnum = lnum; 2472 wip->wi_fpos.col = col; 2473 } 2474 if (copy_options) 2475 { 2476 /* Save the window-specific option values. */ 2477 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); 2478 #ifdef FEAT_FOLDING 2479 wip->wi_fold_manual = win->w_fold_manual; 2480 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds); 2481 #endif 2482 wip->wi_optset = TRUE; 2483 } 2484 2485 /* insert the entry in front of the list */ 2486 wip->wi_next = buf->b_wininfo; 2487 buf->b_wininfo = wip; 2488 wip->wi_prev = NULL; 2489 if (wip->wi_next) 2490 wip->wi_next->wi_prev = wip; 2491 2492 return; 2493 } 2494 2495 #ifdef FEAT_DIFF 2496 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip)); 2497 2498 /* 2499 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab 2500 * page. That's because a diff is local to a tab page. 2501 */ 2502 static int 2503 wininfo_other_tab_diff(wip) 2504 wininfo_T *wip; 2505 { 2506 win_T *wp; 2507 2508 if (wip->wi_opt.wo_diff) 2509 { 2510 for (wp = firstwin; wp != NULL; wp = wp->w_next) 2511 /* return FALSE when it's a window in the current tab page, thus 2512 * the buffer was in diff mode here */ 2513 if (wip->wi_win == wp) 2514 return FALSE; 2515 return TRUE; 2516 } 2517 return FALSE; 2518 } 2519 #endif 2520 2521 /* 2522 * Find info for the current window in buffer "buf". 2523 * If not found, return the info for the most recently used window. 2524 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in 2525 * another tab page. 2526 * Returns NULL when there isn't any info. 2527 */ 2528 static wininfo_T * 2529 find_wininfo(buf, skip_diff_buffer) 2530 buf_T *buf; 2531 int skip_diff_buffer UNUSED; 2532 { 2533 wininfo_T *wip; 2534 2535 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2536 if (wip->wi_win == curwin 2537 #ifdef FEAT_DIFF 2538 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip)) 2539 #endif 2540 ) 2541 break; 2542 2543 /* If no wininfo for curwin, use the first in the list (that doesn't have 2544 * 'diff' set and is in another tab page). */ 2545 if (wip == NULL) 2546 { 2547 #ifdef FEAT_DIFF 2548 if (skip_diff_buffer) 2549 { 2550 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2551 if (!wininfo_other_tab_diff(wip)) 2552 break; 2553 } 2554 else 2555 #endif 2556 wip = buf->b_wininfo; 2557 } 2558 return wip; 2559 } 2560 2561 /* 2562 * Reset the local window options to the values last used in this window. 2563 * If the buffer wasn't used in this window before, use the values from 2564 * the most recently used window. If the values were never set, use the 2565 * global values for the window. 2566 */ 2567 void 2568 get_winopts(buf) 2569 buf_T *buf; 2570 { 2571 wininfo_T *wip; 2572 2573 clear_winopt(&curwin->w_onebuf_opt); 2574 #ifdef FEAT_FOLDING 2575 clearFolding(curwin); 2576 #endif 2577 2578 wip = find_wininfo(buf, TRUE); 2579 if (wip != NULL && wip->wi_optset) 2580 { 2581 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt); 2582 #ifdef FEAT_FOLDING 2583 curwin->w_fold_manual = wip->wi_fold_manual; 2584 curwin->w_foldinvalid = TRUE; 2585 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds); 2586 #endif 2587 } 2588 else 2589 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); 2590 2591 #ifdef FEAT_FOLDING 2592 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ 2593 if (p_fdls >= 0) 2594 curwin->w_p_fdl = p_fdls; 2595 #endif 2596 #ifdef FEAT_SYN_HL 2597 check_colorcolumn(curwin); 2598 #endif 2599 } 2600 2601 /* 2602 * Find the position (lnum and col) for the buffer 'buf' for the current 2603 * window. 2604 * Returns a pointer to no_position if no position is found. 2605 */ 2606 pos_T * 2607 buflist_findfpos(buf) 2608 buf_T *buf; 2609 { 2610 wininfo_T *wip; 2611 static pos_T no_position = INIT_POS_T(1, 0, 0); 2612 2613 wip = find_wininfo(buf, FALSE); 2614 if (wip != NULL) 2615 return &(wip->wi_fpos); 2616 else 2617 return &no_position; 2618 } 2619 2620 /* 2621 * Find the lnum for the buffer 'buf' for the current window. 2622 */ 2623 linenr_T 2624 buflist_findlnum(buf) 2625 buf_T *buf; 2626 { 2627 return buflist_findfpos(buf)->lnum; 2628 } 2629 2630 #if defined(FEAT_LISTCMDS) || defined(PROTO) 2631 /* 2632 * List all know file names (for :files and :buffers command). 2633 */ 2634 void 2635 buflist_list(eap) 2636 exarg_T *eap; 2637 { 2638 buf_T *buf; 2639 int len; 2640 int i; 2641 2642 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next) 2643 { 2644 /* skip unlisted buffers, unless ! was used */ 2645 if (!buf->b_p_bl && !eap->forceit) 2646 continue; 2647 msg_putchar('\n'); 2648 if (buf_spname(buf) != NULL) 2649 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1); 2650 else 2651 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE); 2652 2653 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"", 2654 buf->b_fnum, 2655 buf->b_p_bl ? ' ' : 'u', 2656 buf == curbuf ? '%' : 2657 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), 2658 buf->b_ml.ml_mfp == NULL ? ' ' : 2659 (buf->b_nwindows == 0 ? 'h' : 'a'), 2660 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), 2661 (buf->b_flags & BF_READERR) ? 'x' 2662 : (bufIsChanged(buf) ? '+' : ' '), 2663 NameBuff); 2664 2665 /* put "line 999" in column 40 or after the file name */ 2666 i = 40 - vim_strsize(IObuff); 2667 do 2668 { 2669 IObuff[len++] = ' '; 2670 } while (--i > 0 && len < IOSIZE - 18); 2671 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len), 2672 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum 2673 : (long)buflist_findlnum(buf)); 2674 msg_outtrans(IObuff); 2675 out_flush(); /* output one line at a time */ 2676 ui_breakcheck(); 2677 } 2678 } 2679 #endif 2680 2681 /* 2682 * Get file name and line number for file 'fnum'. 2683 * Used by DoOneCmd() for translating '%' and '#'. 2684 * Used by insert_reg() and cmdline_paste() for '#' register. 2685 * Return FAIL if not found, OK for success. 2686 */ 2687 int 2688 buflist_name_nr(fnum, fname, lnum) 2689 int fnum; 2690 char_u **fname; 2691 linenr_T *lnum; 2692 { 2693 buf_T *buf; 2694 2695 buf = buflist_findnr(fnum); 2696 if (buf == NULL || buf->b_fname == NULL) 2697 return FAIL; 2698 2699 *fname = buf->b_fname; 2700 *lnum = buflist_findlnum(buf); 2701 2702 return OK; 2703 } 2704 2705 /* 2706 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'. 2707 * The file name with the full path is also remembered, for when :cd is used. 2708 * Returns FAIL for failure (file name already in use by other buffer) 2709 * OK otherwise. 2710 */ 2711 int 2712 setfname(buf, ffname, sfname, message) 2713 buf_T *buf; 2714 char_u *ffname, *sfname; 2715 int message; /* give message when buffer already exists */ 2716 { 2717 buf_T *obuf = NULL; 2718 #ifdef UNIX 2719 struct stat st; 2720 #endif 2721 2722 if (ffname == NULL || *ffname == NUL) 2723 { 2724 /* Removing the name. */ 2725 vim_free(buf->b_ffname); 2726 vim_free(buf->b_sfname); 2727 buf->b_ffname = NULL; 2728 buf->b_sfname = NULL; 2729 #ifdef UNIX 2730 st.st_dev = (dev_T)-1; 2731 #endif 2732 } 2733 else 2734 { 2735 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */ 2736 if (ffname == NULL) /* out of memory */ 2737 return FAIL; 2738 2739 /* 2740 * if the file name is already used in another buffer: 2741 * - if the buffer is loaded, fail 2742 * - if the buffer is not loaded, delete it from the list 2743 */ 2744 #ifdef UNIX 2745 if (mch_stat((char *)ffname, &st) < 0) 2746 st.st_dev = (dev_T)-1; 2747 #endif 2748 if (!(buf->b_flags & BF_DUMMY)) 2749 #ifdef UNIX 2750 obuf = buflist_findname_stat(ffname, &st); 2751 #else 2752 obuf = buflist_findname(ffname); 2753 #endif 2754 if (obuf != NULL && obuf != buf) 2755 { 2756 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */ 2757 { 2758 if (message) 2759 EMSG(_("E95: Buffer with this name already exists")); 2760 vim_free(ffname); 2761 return FAIL; 2762 } 2763 /* delete from the list */ 2764 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); 2765 } 2766 sfname = vim_strsave(sfname); 2767 if (ffname == NULL || sfname == NULL) 2768 { 2769 vim_free(sfname); 2770 vim_free(ffname); 2771 return FAIL; 2772 } 2773 #ifdef USE_FNAME_CASE 2774 # ifdef USE_LONG_FNAME 2775 if (USE_LONG_FNAME) 2776 # endif 2777 fname_case(sfname, 0); /* set correct case for short file name */ 2778 #endif 2779 vim_free(buf->b_ffname); 2780 vim_free(buf->b_sfname); 2781 buf->b_ffname = ffname; 2782 buf->b_sfname = sfname; 2783 } 2784 buf->b_fname = buf->b_sfname; 2785 #ifdef UNIX 2786 if (st.st_dev == (dev_T)-1) 2787 buf->b_dev_valid = FALSE; 2788 else 2789 { 2790 buf->b_dev_valid = TRUE; 2791 buf->b_dev = st.st_dev; 2792 buf->b_ino = st.st_ino; 2793 } 2794 #endif 2795 2796 #ifndef SHORT_FNAME 2797 buf->b_shortname = FALSE; 2798 #endif 2799 2800 buf_name_changed(buf); 2801 return OK; 2802 } 2803 2804 /* 2805 * Crude way of changing the name of a buffer. Use with care! 2806 * The name should be relative to the current directory. 2807 */ 2808 void 2809 buf_set_name(fnum, name) 2810 int fnum; 2811 char_u *name; 2812 { 2813 buf_T *buf; 2814 2815 buf = buflist_findnr(fnum); 2816 if (buf != NULL) 2817 { 2818 vim_free(buf->b_sfname); 2819 vim_free(buf->b_ffname); 2820 buf->b_ffname = vim_strsave(name); 2821 buf->b_sfname = NULL; 2822 /* Allocate ffname and expand into full path. Also resolves .lnk 2823 * files on Win32. */ 2824 fname_expand(buf, &buf->b_ffname, &buf->b_sfname); 2825 buf->b_fname = buf->b_sfname; 2826 } 2827 } 2828 2829 /* 2830 * Take care of what needs to be done when the name of buffer "buf" has 2831 * changed. 2832 */ 2833 void 2834 buf_name_changed(buf) 2835 buf_T *buf; 2836 { 2837 /* 2838 * If the file name changed, also change the name of the swapfile 2839 */ 2840 if (buf->b_ml.ml_mfp != NULL) 2841 ml_setname(buf); 2842 2843 if (curwin->w_buffer == buf) 2844 check_arg_idx(curwin); /* check file name for arg list */ 2845 #ifdef FEAT_TITLE 2846 maketitle(); /* set window title */ 2847 #endif 2848 #ifdef FEAT_WINDOWS 2849 status_redraw_all(); /* status lines need to be redrawn */ 2850 #endif 2851 fmarks_check_names(buf); /* check named file marks */ 2852 ml_timestamp(buf); /* reset timestamp */ 2853 } 2854 2855 /* 2856 * set alternate file name for current window 2857 * 2858 * Used by do_one_cmd(), do_write() and do_ecmd(). 2859 * Return the buffer. 2860 */ 2861 buf_T * 2862 setaltfname(ffname, sfname, lnum) 2863 char_u *ffname; 2864 char_u *sfname; 2865 linenr_T lnum; 2866 { 2867 buf_T *buf; 2868 2869 /* Create a buffer. 'buflisted' is not set if it's a new buffer */ 2870 buf = buflist_new(ffname, sfname, lnum, 0); 2871 if (buf != NULL && !cmdmod.keepalt) 2872 curwin->w_alt_fnum = buf->b_fnum; 2873 return buf; 2874 } 2875 2876 /* 2877 * Get alternate file name for current window. 2878 * Return NULL if there isn't any, and give error message if requested. 2879 */ 2880 char_u * 2881 getaltfname(errmsg) 2882 int errmsg; /* give error message */ 2883 { 2884 char_u *fname; 2885 linenr_T dummy; 2886 2887 if (buflist_name_nr(0, &fname, &dummy) == FAIL) 2888 { 2889 if (errmsg) 2890 EMSG(_(e_noalt)); 2891 return NULL; 2892 } 2893 return fname; 2894 } 2895 2896 /* 2897 * Add a file name to the buflist and return its number. 2898 * Uses same flags as buflist_new(), except BLN_DUMMY. 2899 * 2900 * used by qf_init(), main() and doarglist() 2901 */ 2902 int 2903 buflist_add(fname, flags) 2904 char_u *fname; 2905 int flags; 2906 { 2907 buf_T *buf; 2908 2909 buf = buflist_new(fname, NULL, (linenr_T)0, flags); 2910 if (buf != NULL) 2911 return buf->b_fnum; 2912 return 0; 2913 } 2914 2915 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) 2916 /* 2917 * Adjust slashes in file names. Called after 'shellslash' was set. 2918 */ 2919 void 2920 buflist_slash_adjust() 2921 { 2922 buf_T *bp; 2923 2924 for (bp = firstbuf; bp != NULL; bp = bp->b_next) 2925 { 2926 if (bp->b_ffname != NULL) 2927 slash_adjust(bp->b_ffname); 2928 if (bp->b_sfname != NULL) 2929 slash_adjust(bp->b_sfname); 2930 } 2931 } 2932 #endif 2933 2934 /* 2935 * Set alternate cursor position for the current buffer and window "win". 2936 * Also save the local window option values. 2937 */ 2938 void 2939 buflist_altfpos(win) 2940 win_T *win; 2941 { 2942 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE); 2943 } 2944 2945 /* 2946 * Return TRUE if 'ffname' is not the same file as current file. 2947 * Fname must have a full path (expanded by mch_FullName()). 2948 */ 2949 int 2950 otherfile(ffname) 2951 char_u *ffname; 2952 { 2953 return otherfile_buf(curbuf, ffname 2954 #ifdef UNIX 2955 , NULL 2956 #endif 2957 ); 2958 } 2959 2960 static int 2961 otherfile_buf(buf, ffname 2962 #ifdef UNIX 2963 , stp 2964 #endif 2965 ) 2966 buf_T *buf; 2967 char_u *ffname; 2968 #ifdef UNIX 2969 struct stat *stp; 2970 #endif 2971 { 2972 /* no name is different */ 2973 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) 2974 return TRUE; 2975 if (fnamecmp(ffname, buf->b_ffname) == 0) 2976 return FALSE; 2977 #ifdef UNIX 2978 { 2979 struct stat st; 2980 2981 /* If no struct stat given, get it now */ 2982 if (stp == NULL) 2983 { 2984 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0) 2985 st.st_dev = (dev_T)-1; 2986 stp = &st; 2987 } 2988 /* Use dev/ino to check if the files are the same, even when the names 2989 * are different (possible with links). Still need to compare the 2990 * name above, for when the file doesn't exist yet. 2991 * Problem: The dev/ino changes when a file is deleted (and created 2992 * again) and remains the same when renamed/moved. We don't want to 2993 * mch_stat() each buffer each time, that would be too slow. Get the 2994 * dev/ino again when they appear to match, but not when they appear 2995 * to be different: Could skip a buffer when it's actually the same 2996 * file. */ 2997 if (buf_same_ino(buf, stp)) 2998 { 2999 buf_setino(buf); 3000 if (buf_same_ino(buf, stp)) 3001 return FALSE; 3002 } 3003 } 3004 #endif 3005 return TRUE; 3006 } 3007 3008 #if defined(UNIX) || defined(PROTO) 3009 /* 3010 * Set inode and device number for a buffer. 3011 * Must always be called when b_fname is changed!. 3012 */ 3013 void 3014 buf_setino(buf) 3015 buf_T *buf; 3016 { 3017 struct stat st; 3018 3019 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0) 3020 { 3021 buf->b_dev_valid = TRUE; 3022 buf->b_dev = st.st_dev; 3023 buf->b_ino = st.st_ino; 3024 } 3025 else 3026 buf->b_dev_valid = FALSE; 3027 } 3028 3029 /* 3030 * Return TRUE if dev/ino in buffer "buf" matches with "stp". 3031 */ 3032 static int 3033 buf_same_ino(buf, stp) 3034 buf_T *buf; 3035 struct stat *stp; 3036 { 3037 return (buf->b_dev_valid 3038 && stp->st_dev == buf->b_dev 3039 && stp->st_ino == buf->b_ino); 3040 } 3041 #endif 3042 3043 /* 3044 * Print info about the current buffer. 3045 */ 3046 void 3047 fileinfo(fullname, shorthelp, dont_truncate) 3048 int fullname; /* when non-zero print full path */ 3049 int shorthelp; 3050 int dont_truncate; 3051 { 3052 char_u *name; 3053 int n; 3054 char_u *p; 3055 char_u *buffer; 3056 size_t len; 3057 3058 buffer = alloc(IOSIZE); 3059 if (buffer == NULL) 3060 return; 3061 3062 if (fullname > 1) /* 2 CTRL-G: include buffer number */ 3063 { 3064 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum); 3065 p = buffer + STRLEN(buffer); 3066 } 3067 else 3068 p = buffer; 3069 3070 *p++ = '"'; 3071 if (buf_spname(curbuf) != NULL) 3072 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1); 3073 else 3074 { 3075 if (!fullname && curbuf->b_fname != NULL) 3076 name = curbuf->b_fname; 3077 else 3078 name = curbuf->b_ffname; 3079 home_replace(shorthelp ? curbuf : NULL, name, p, 3080 (int)(IOSIZE - (p - buffer)), TRUE); 3081 } 3082 3083 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", 3084 curbufIsChanged() ? (shortmess(SHM_MOD) 3085 ? " [+]" : _(" [Modified]")) : " ", 3086 (curbuf->b_flags & BF_NOTEDITED) 3087 #ifdef FEAT_QUICKFIX 3088 && !bt_dontwrite(curbuf) 3089 #endif 3090 ? _("[Not edited]") : "", 3091 (curbuf->b_flags & BF_NEW) 3092 #ifdef FEAT_QUICKFIX 3093 && !bt_dontwrite(curbuf) 3094 #endif 3095 ? _("[New file]") : "", 3096 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", 3097 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]" 3098 : _("[readonly]")) : "", 3099 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) 3100 || curbuf->b_p_ro) ? 3101 " " : ""); 3102 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100 3103 * causes an overflow, thus for large numbers divide instead. */ 3104 if (curwin->w_cursor.lnum > 1000000L) 3105 n = (int)(((long)curwin->w_cursor.lnum) / 3106 ((long)curbuf->b_ml.ml_line_count / 100L)); 3107 else 3108 n = (int)(((long)curwin->w_cursor.lnum * 100L) / 3109 (long)curbuf->b_ml.ml_line_count); 3110 if (curbuf->b_ml.ml_flags & ML_EMPTY) 3111 { 3112 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg)); 3113 } 3114 #ifdef FEAT_CMDL_INFO 3115 else if (p_ru) 3116 { 3117 /* Current line and column are already on the screen -- webb */ 3118 if (curbuf->b_ml.ml_line_count == 1) 3119 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n); 3120 else 3121 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"), 3122 (long)curbuf->b_ml.ml_line_count, n); 3123 } 3124 #endif 3125 else 3126 { 3127 vim_snprintf_add((char *)buffer, IOSIZE, 3128 _("line %ld of %ld --%d%%-- col "), 3129 (long)curwin->w_cursor.lnum, 3130 (long)curbuf->b_ml.ml_line_count, 3131 n); 3132 validate_virtcol(); 3133 len = STRLEN(buffer); 3134 col_print(buffer + len, IOSIZE - len, 3135 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1); 3136 } 3137 3138 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); 3139 3140 if (dont_truncate) 3141 { 3142 /* Temporarily set msg_scroll to avoid the message being truncated. 3143 * First call msg_start() to get the message in the right place. */ 3144 msg_start(); 3145 n = msg_scroll; 3146 msg_scroll = TRUE; 3147 msg(buffer); 3148 msg_scroll = n; 3149 } 3150 else 3151 { 3152 p = msg_trunc_attr(buffer, FALSE, 0); 3153 if (restart_edit != 0 || (msg_scrolled && !need_wait_return)) 3154 /* Need to repeat the message after redrawing when: 3155 * - When restart_edit is set (otherwise there will be a delay 3156 * before redrawing). 3157 * - When the screen was scrolled but there is no wait-return 3158 * prompt. */ 3159 set_keep_msg(p, 0); 3160 } 3161 3162 vim_free(buffer); 3163 } 3164 3165 void 3166 col_print(buf, buflen, col, vcol) 3167 char_u *buf; 3168 size_t buflen; 3169 int col; 3170 int vcol; 3171 { 3172 if (col == vcol) 3173 vim_snprintf((char *)buf, buflen, "%d", col); 3174 else 3175 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol); 3176 } 3177 3178 #if defined(FEAT_TITLE) || defined(PROTO) 3179 /* 3180 * put file name in title bar of window and in icon title 3181 */ 3182 3183 static char_u *lasttitle = NULL; 3184 static char_u *lasticon = NULL; 3185 3186 void 3187 maketitle() 3188 { 3189 char_u *p; 3190 char_u *t_str = NULL; 3191 char_u *i_name; 3192 char_u *i_str = NULL; 3193 int maxlen = 0; 3194 int len; 3195 int mustset; 3196 char_u buf[IOSIZE]; 3197 int off; 3198 3199 if (!redrawing()) 3200 { 3201 /* Postpone updating the title when 'lazyredraw' is set. */ 3202 need_maketitle = TRUE; 3203 return; 3204 } 3205 3206 need_maketitle = FALSE; 3207 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL) 3208 return; 3209 3210 if (p_title) 3211 { 3212 if (p_titlelen > 0) 3213 { 3214 maxlen = p_titlelen * Columns / 100; 3215 if (maxlen < 10) 3216 maxlen = 10; 3217 } 3218 3219 t_str = buf; 3220 if (*p_titlestring != NUL) 3221 { 3222 #ifdef FEAT_STL_OPT 3223 if (stl_syntax & STL_IN_TITLE) 3224 { 3225 int use_sandbox = FALSE; 3226 int save_called_emsg = called_emsg; 3227 3228 # ifdef FEAT_EVAL 3229 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0); 3230 # endif 3231 called_emsg = FALSE; 3232 build_stl_str_hl(curwin, t_str, sizeof(buf), 3233 p_titlestring, use_sandbox, 3234 0, maxlen, NULL, NULL); 3235 if (called_emsg) 3236 set_string_option_direct((char_u *)"titlestring", -1, 3237 (char_u *)"", OPT_FREE, SID_ERROR); 3238 called_emsg |= save_called_emsg; 3239 } 3240 else 3241 #endif 3242 t_str = p_titlestring; 3243 } 3244 else 3245 { 3246 /* format: "fname + (path) (1 of 2) - VIM" */ 3247 3248 #define SPACE_FOR_FNAME (IOSIZE - 100) 3249 #define SPACE_FOR_DIR (IOSIZE - 20) 3250 #define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */ 3251 if (curbuf->b_fname == NULL) 3252 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME); 3253 else 3254 { 3255 p = transstr(gettail(curbuf->b_fname)); 3256 vim_strncpy(buf, p, SPACE_FOR_FNAME); 3257 vim_free(p); 3258 } 3259 3260 switch (bufIsChanged(curbuf) 3261 + (curbuf->b_p_ro * 2) 3262 + (!curbuf->b_p_ma * 4)) 3263 { 3264 case 1: STRCAT(buf, " +"); break; 3265 case 2: STRCAT(buf, " ="); break; 3266 case 3: STRCAT(buf, " =+"); break; 3267 case 4: 3268 case 6: STRCAT(buf, " -"); break; 3269 case 5: 3270 case 7: STRCAT(buf, " -+"); break; 3271 } 3272 3273 if (curbuf->b_fname != NULL) 3274 { 3275 /* Get path of file, replace home dir with ~ */ 3276 off = (int)STRLEN(buf); 3277 buf[off++] = ' '; 3278 buf[off++] = '('; 3279 home_replace(curbuf, curbuf->b_ffname, 3280 buf + off, SPACE_FOR_DIR - off, TRUE); 3281 #ifdef BACKSLASH_IN_FILENAME 3282 /* avoid "c:/name" to be reduced to "c" */ 3283 if (isalpha(buf[off]) && buf[off + 1] == ':') 3284 off += 2; 3285 #endif 3286 /* remove the file name */ 3287 p = gettail_sep(buf + off); 3288 if (p == buf + off) 3289 /* must be a help buffer */ 3290 vim_strncpy(buf + off, (char_u *)_("help"), 3291 (size_t)(SPACE_FOR_DIR - off - 1)); 3292 else 3293 *p = NUL; 3294 3295 /* Translate unprintable chars and concatenate. Keep some 3296 * room for the server name. When there is no room (very long 3297 * file name) use (...). */ 3298 if (off < SPACE_FOR_DIR) 3299 { 3300 p = transstr(buf + off); 3301 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off)); 3302 vim_free(p); 3303 } 3304 else 3305 { 3306 vim_strncpy(buf + off, (char_u *)"...", 3307 (size_t)(SPACE_FOR_ARGNR - off)); 3308 } 3309 STRCAT(buf, ")"); 3310 } 3311 3312 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE); 3313 3314 #if defined(FEAT_CLIENTSERVER) 3315 if (serverName != NULL) 3316 { 3317 STRCAT(buf, " - "); 3318 vim_strcat(buf, serverName, IOSIZE); 3319 } 3320 else 3321 #endif 3322 STRCAT(buf, " - VIM"); 3323 3324 if (maxlen > 0) 3325 { 3326 /* make it shorter by removing a bit in the middle */ 3327 if (vim_strsize(buf) > maxlen) 3328 trunc_string(buf, buf, maxlen, IOSIZE); 3329 } 3330 } 3331 } 3332 mustset = ti_change(t_str, &lasttitle); 3333 3334 if (p_icon) 3335 { 3336 i_str = buf; 3337 if (*p_iconstring != NUL) 3338 { 3339 #ifdef FEAT_STL_OPT 3340 if (stl_syntax & STL_IN_ICON) 3341 { 3342 int use_sandbox = FALSE; 3343 int save_called_emsg = called_emsg; 3344 3345 # ifdef FEAT_EVAL 3346 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0); 3347 # endif 3348 called_emsg = FALSE; 3349 build_stl_str_hl(curwin, i_str, sizeof(buf), 3350 p_iconstring, use_sandbox, 3351 0, 0, NULL, NULL); 3352 if (called_emsg) 3353 set_string_option_direct((char_u *)"iconstring", -1, 3354 (char_u *)"", OPT_FREE, SID_ERROR); 3355 called_emsg |= save_called_emsg; 3356 } 3357 else 3358 #endif 3359 i_str = p_iconstring; 3360 } 3361 else 3362 { 3363 if (buf_spname(curbuf) != NULL) 3364 i_name = buf_spname(curbuf); 3365 else /* use file name only in icon */ 3366 i_name = gettail(curbuf->b_ffname); 3367 *i_str = NUL; 3368 /* Truncate name at 100 bytes. */ 3369 len = (int)STRLEN(i_name); 3370 if (len > 100) 3371 { 3372 len -= 100; 3373 #ifdef FEAT_MBYTE 3374 if (has_mbyte) 3375 len += (*mb_tail_off)(i_name, i_name + len) + 1; 3376 #endif 3377 i_name += len; 3378 } 3379 STRCPY(i_str, i_name); 3380 trans_characters(i_str, IOSIZE); 3381 } 3382 } 3383 3384 mustset |= ti_change(i_str, &lasticon); 3385 3386 if (mustset) 3387 resettitle(); 3388 } 3389 3390 /* 3391 * Used for title and icon: Check if "str" differs from "*last". Set "*last" 3392 * from "str" if it does. 3393 * Return TRUE when "*last" changed. 3394 */ 3395 static int 3396 ti_change(str, last) 3397 char_u *str; 3398 char_u **last; 3399 { 3400 if ((str == NULL) != (*last == NULL) 3401 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0)) 3402 { 3403 vim_free(*last); 3404 if (str == NULL) 3405 *last = NULL; 3406 else 3407 *last = vim_strsave(str); 3408 return TRUE; 3409 } 3410 return FALSE; 3411 } 3412 3413 /* 3414 * Put current window title back (used after calling a shell) 3415 */ 3416 void 3417 resettitle() 3418 { 3419 mch_settitle(lasttitle, lasticon); 3420 } 3421 3422 # if defined(EXITFREE) || defined(PROTO) 3423 void 3424 free_titles() 3425 { 3426 vim_free(lasttitle); 3427 vim_free(lasticon); 3428 } 3429 # endif 3430 3431 #endif /* FEAT_TITLE */ 3432 3433 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO) 3434 /* 3435 * Build a string from the status line items in "fmt". 3436 * Return length of string in screen cells. 3437 * 3438 * Normally works for window "wp", except when working for 'tabline' then it 3439 * is "curwin". 3440 * 3441 * Items are drawn interspersed with the text that surrounds it 3442 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation 3443 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional 3444 * 3445 * If maxwidth is not zero, the string will be filled at any middle marker 3446 * or truncated if too long, fillchar is used for all whitespace. 3447 */ 3448 int 3449 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, 3450 maxwidth, hltab, tabtab) 3451 win_T *wp; 3452 char_u *out; /* buffer to write into != NameBuff */ 3453 size_t outlen; /* length of out[] */ 3454 char_u *fmt; 3455 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */ 3456 int fillchar; 3457 int maxwidth; 3458 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */ 3459 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */ 3460 { 3461 char_u *p; 3462 char_u *s; 3463 char_u *t; 3464 char_u *linecont; 3465 #ifdef FEAT_EVAL 3466 win_T *o_curwin; 3467 buf_T *o_curbuf; 3468 #endif 3469 int empty_line; 3470 colnr_T virtcol; 3471 long l; 3472 long n; 3473 int prevchar_isflag; 3474 int prevchar_isitem; 3475 int itemisflag; 3476 int fillable; 3477 char_u *str; 3478 long num; 3479 int width; 3480 int itemcnt; 3481 int curitem; 3482 int groupitem[STL_MAX_ITEM]; 3483 int groupdepth; 3484 struct stl_item 3485 { 3486 char_u *start; 3487 int minwid; 3488 int maxwid; 3489 enum 3490 { 3491 Normal, 3492 Empty, 3493 Group, 3494 Middle, 3495 Highlight, 3496 TabPage, 3497 Trunc 3498 } type; 3499 } item[STL_MAX_ITEM]; 3500 int minwid; 3501 int maxwid; 3502 int zeropad; 3503 char_u base; 3504 char_u opt; 3505 #define TMPLEN 70 3506 char_u tmp[TMPLEN]; 3507 char_u *usefmt = fmt; 3508 struct stl_hlrec *sp; 3509 3510 #ifdef FEAT_EVAL 3511 /* 3512 * When the format starts with "%!" then evaluate it as an expression and 3513 * use the result as the actual format string. 3514 */ 3515 if (fmt[0] == '%' && fmt[1] == '!') 3516 { 3517 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); 3518 if (usefmt == NULL) 3519 usefmt = fmt; 3520 } 3521 #endif 3522 3523 if (fillchar == 0) 3524 fillchar = ' '; 3525 #ifdef FEAT_MBYTE 3526 /* Can't handle a multi-byte fill character yet. */ 3527 else if (mb_char2len(fillchar) > 1) 3528 fillchar = '-'; 3529 #endif 3530 3531 /* 3532 * Get line & check if empty (cursorpos will show "0-1"). 3533 * If inversion is possible we use it. Else '=' characters are used. 3534 */ 3535 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); 3536 empty_line = (*linecont == NUL); 3537 3538 groupdepth = 0; 3539 p = out; 3540 curitem = 0; 3541 prevchar_isflag = TRUE; 3542 prevchar_isitem = FALSE; 3543 for (s = usefmt; *s; ) 3544 { 3545 if (curitem == STL_MAX_ITEM) 3546 { 3547 /* There are too many items. Add the error code to the statusline 3548 * to give the user a hint about what went wrong. */ 3549 if (p + 6 < out + outlen) 3550 { 3551 mch_memmove(p, " E541", (size_t)5); 3552 p += 5; 3553 } 3554 break; 3555 } 3556 3557 if (*s != NUL && *s != '%') 3558 prevchar_isflag = prevchar_isitem = FALSE; 3559 3560 /* 3561 * Handle up to the next '%' or the end. 3562 */ 3563 while (*s != NUL && *s != '%' && p + 1 < out + outlen) 3564 *p++ = *s++; 3565 if (*s == NUL || p + 1 >= out + outlen) 3566 break; 3567 3568 /* 3569 * Handle one '%' item. 3570 */ 3571 s++; 3572 if (*s == NUL) /* ignore trailing % */ 3573 break; 3574 if (*s == '%') 3575 { 3576 if (p + 1 >= out + outlen) 3577 break; 3578 *p++ = *s++; 3579 prevchar_isflag = prevchar_isitem = FALSE; 3580 continue; 3581 } 3582 if (*s == STL_MIDDLEMARK) 3583 { 3584 s++; 3585 if (groupdepth > 0) 3586 continue; 3587 item[curitem].type = Middle; 3588 item[curitem++].start = p; 3589 continue; 3590 } 3591 if (*s == STL_TRUNCMARK) 3592 { 3593 s++; 3594 item[curitem].type = Trunc; 3595 item[curitem++].start = p; 3596 continue; 3597 } 3598 if (*s == ')') 3599 { 3600 s++; 3601 if (groupdepth < 1) 3602 continue; 3603 groupdepth--; 3604 3605 t = item[groupitem[groupdepth]].start; 3606 *p = NUL; 3607 l = vim_strsize(t); 3608 if (curitem > groupitem[groupdepth] + 1 3609 && item[groupitem[groupdepth]].minwid == 0) 3610 { 3611 /* remove group if all items are empty */ 3612 for (n = groupitem[groupdepth] + 1; n < curitem; n++) 3613 if (item[n].type == Normal) 3614 break; 3615 if (n == curitem) 3616 { 3617 p = t; 3618 l = 0; 3619 } 3620 } 3621 if (l > item[groupitem[groupdepth]].maxwid) 3622 { 3623 /* truncate, remove n bytes of text at the start */ 3624 #ifdef FEAT_MBYTE 3625 if (has_mbyte) 3626 { 3627 /* Find the first character that should be included. */ 3628 n = 0; 3629 while (l >= item[groupitem[groupdepth]].maxwid) 3630 { 3631 l -= ptr2cells(t + n); 3632 n += (*mb_ptr2len)(t + n); 3633 } 3634 } 3635 else 3636 #endif 3637 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; 3638 3639 *t = '<'; 3640 mch_memmove(t + 1, t + n, (size_t)(p - (t + n))); 3641 p = p - n + 1; 3642 #ifdef FEAT_MBYTE 3643 /* Fill up space left over by half a double-wide char. */ 3644 while (++l < item[groupitem[groupdepth]].minwid) 3645 *p++ = fillchar; 3646 #endif 3647 3648 /* correct the start of the items for the truncation */ 3649 for (l = groupitem[groupdepth] + 1; l < curitem; l++) 3650 { 3651 item[l].start -= n; 3652 if (item[l].start < t) 3653 item[l].start = t; 3654 } 3655 } 3656 else if (abs(item[groupitem[groupdepth]].minwid) > l) 3657 { 3658 /* fill */ 3659 n = item[groupitem[groupdepth]].minwid; 3660 if (n < 0) 3661 { 3662 /* fill by appending characters */ 3663 n = 0 - n; 3664 while (l++ < n && p + 1 < out + outlen) 3665 *p++ = fillchar; 3666 } 3667 else 3668 { 3669 /* fill by inserting characters */ 3670 mch_memmove(t + n - l, t, (size_t)(p - t)); 3671 l = n - l; 3672 if (p + l >= out + outlen) 3673 l = (long)((out + outlen) - p - 1); 3674 p += l; 3675 for (n = groupitem[groupdepth] + 1; n < curitem; n++) 3676 item[n].start += l; 3677 for ( ; l > 0; l--) 3678 *t++ = fillchar; 3679 } 3680 } 3681 continue; 3682 } 3683 minwid = 0; 3684 maxwid = 9999; 3685 zeropad = FALSE; 3686 l = 1; 3687 if (*s == '0') 3688 { 3689 s++; 3690 zeropad = TRUE; 3691 } 3692 if (*s == '-') 3693 { 3694 s++; 3695 l = -1; 3696 } 3697 if (VIM_ISDIGIT(*s)) 3698 { 3699 minwid = (int)getdigits(&s); 3700 if (minwid < 0) /* overflow */ 3701 minwid = 0; 3702 } 3703 if (*s == STL_USER_HL) 3704 { 3705 item[curitem].type = Highlight; 3706 item[curitem].start = p; 3707 item[curitem].minwid = minwid > 9 ? 1 : minwid; 3708 s++; 3709 curitem++; 3710 continue; 3711 } 3712 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) 3713 { 3714 if (*s == STL_TABCLOSENR) 3715 { 3716 if (minwid == 0) 3717 { 3718 /* %X ends the close label, go back to the previously 3719 * define tab label nr. */ 3720 for (n = curitem - 1; n >= 0; --n) 3721 if (item[n].type == TabPage && item[n].minwid >= 0) 3722 { 3723 minwid = item[n].minwid; 3724 break; 3725 } 3726 } 3727 else 3728 /* close nrs are stored as negative values */ 3729 minwid = - minwid; 3730 } 3731 item[curitem].type = TabPage; 3732 item[curitem].start = p; 3733 item[curitem].minwid = minwid; 3734 s++; 3735 curitem++; 3736 continue; 3737 } 3738 if (*s == '.') 3739 { 3740 s++; 3741 if (VIM_ISDIGIT(*s)) 3742 { 3743 maxwid = (int)getdigits(&s); 3744 if (maxwid <= 0) /* overflow */ 3745 maxwid = 50; 3746 } 3747 } 3748 minwid = (minwid > 50 ? 50 : minwid) * l; 3749 if (*s == '(') 3750 { 3751 groupitem[groupdepth++] = curitem; 3752 item[curitem].type = Group; 3753 item[curitem].start = p; 3754 item[curitem].minwid = minwid; 3755 item[curitem].maxwid = maxwid; 3756 s++; 3757 curitem++; 3758 continue; 3759 } 3760 if (vim_strchr(STL_ALL, *s) == NULL) 3761 { 3762 s++; 3763 continue; 3764 } 3765 opt = *s++; 3766 3767 /* OK - now for the real work */ 3768 base = 'D'; 3769 itemisflag = FALSE; 3770 fillable = TRUE; 3771 num = -1; 3772 str = NULL; 3773 switch (opt) 3774 { 3775 case STL_FILEPATH: 3776 case STL_FULLPATH: 3777 case STL_FILENAME: 3778 fillable = FALSE; /* don't change ' ' to fillchar */ 3779 if (buf_spname(wp->w_buffer) != NULL) 3780 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1); 3781 else 3782 { 3783 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname 3784 : wp->w_buffer->b_fname; 3785 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); 3786 } 3787 trans_characters(NameBuff, MAXPATHL); 3788 if (opt != STL_FILENAME) 3789 str = NameBuff; 3790 else 3791 str = gettail(NameBuff); 3792 break; 3793 3794 case STL_VIM_EXPR: /* '{' */ 3795 itemisflag = TRUE; 3796 t = p; 3797 while (*s != '}' && *s != NUL && p + 1 < out + outlen) 3798 *p++ = *s++; 3799 if (*s != '}') /* missing '}' or out of space */ 3800 break; 3801 s++; 3802 *p = 0; 3803 p = t; 3804 3805 #ifdef FEAT_EVAL 3806 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); 3807 set_internal_string_var((char_u *)"actual_curbuf", tmp); 3808 3809 o_curbuf = curbuf; 3810 o_curwin = curwin; 3811 curwin = wp; 3812 curbuf = wp->w_buffer; 3813 3814 str = eval_to_string_safe(p, &t, use_sandbox); 3815 3816 curwin = o_curwin; 3817 curbuf = o_curbuf; 3818 do_unlet((char_u *)"g:actual_curbuf", TRUE); 3819 3820 if (str != NULL && *str != 0) 3821 { 3822 if (*skipdigits(str) == NUL) 3823 { 3824 num = atoi((char *)str); 3825 vim_free(str); 3826 str = NULL; 3827 itemisflag = FALSE; 3828 } 3829 } 3830 #endif 3831 break; 3832 3833 case STL_LINE: 3834 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) 3835 ? 0L : (long)(wp->w_cursor.lnum); 3836 break; 3837 3838 case STL_NUMLINES: 3839 num = wp->w_buffer->b_ml.ml_line_count; 3840 break; 3841 3842 case STL_COLUMN: 3843 num = !(State & INSERT) && empty_line 3844 ? 0 : (int)wp->w_cursor.col + 1; 3845 break; 3846 3847 case STL_VIRTCOL: 3848 case STL_VIRTCOL_ALT: 3849 /* In list mode virtcol needs to be recomputed */ 3850 virtcol = wp->w_virtcol; 3851 if (wp->w_p_list && lcs_tab1 == NUL) 3852 { 3853 wp->w_p_list = FALSE; 3854 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); 3855 wp->w_p_list = TRUE; 3856 } 3857 ++virtcol; 3858 /* Don't display %V if it's the same as %c. */ 3859 if (opt == STL_VIRTCOL_ALT 3860 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line 3861 ? 0 : (int)wp->w_cursor.col + 1))) 3862 break; 3863 num = (long)virtcol; 3864 break; 3865 3866 case STL_PERCENTAGE: 3867 num = (int)(((long)wp->w_cursor.lnum * 100L) / 3868 (long)wp->w_buffer->b_ml.ml_line_count); 3869 break; 3870 3871 case STL_ALTPERCENT: 3872 str = tmp; 3873 get_rel_pos(wp, str, TMPLEN); 3874 break; 3875 3876 case STL_ARGLISTSTAT: 3877 fillable = FALSE; 3878 tmp[0] = 0; 3879 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) 3880 str = tmp; 3881 break; 3882 3883 case STL_KEYMAP: 3884 fillable = FALSE; 3885 if (get_keymap_str(wp, tmp, TMPLEN)) 3886 str = tmp; 3887 break; 3888 case STL_PAGENUM: 3889 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE) 3890 num = printer_page_num; 3891 #else 3892 num = 0; 3893 #endif 3894 break; 3895 3896 case STL_BUFNO: 3897 num = wp->w_buffer->b_fnum; 3898 break; 3899 3900 case STL_OFFSET_X: 3901 base = 'X'; 3902 case STL_OFFSET: 3903 #ifdef FEAT_BYTEOFF 3904 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); 3905 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? 3906 0L : l + 1 + (!(State & INSERT) && empty_line ? 3907 0 : (int)wp->w_cursor.col); 3908 #endif 3909 break; 3910 3911 case STL_BYTEVAL_X: 3912 base = 'X'; 3913 case STL_BYTEVAL: 3914 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont)) 3915 num = 0; 3916 else 3917 { 3918 #ifdef FEAT_MBYTE 3919 num = (*mb_ptr2char)(linecont + wp->w_cursor.col); 3920 #else 3921 num = linecont[wp->w_cursor.col]; 3922 #endif 3923 } 3924 if (num == NL) 3925 num = 0; 3926 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC) 3927 num = NL; 3928 break; 3929 3930 case STL_ROFLAG: 3931 case STL_ROFLAG_ALT: 3932 itemisflag = TRUE; 3933 if (wp->w_buffer->b_p_ro) 3934 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]"); 3935 break; 3936 3937 case STL_HELPFLAG: 3938 case STL_HELPFLAG_ALT: 3939 itemisflag = TRUE; 3940 if (wp->w_buffer->b_help) 3941 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" 3942 : _("[Help]")); 3943 break; 3944 3945 #ifdef FEAT_AUTOCMD 3946 case STL_FILETYPE: 3947 if (*wp->w_buffer->b_p_ft != NUL 3948 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) 3949 { 3950 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", 3951 wp->w_buffer->b_p_ft); 3952 str = tmp; 3953 } 3954 break; 3955 3956 case STL_FILETYPE_ALT: 3957 itemisflag = TRUE; 3958 if (*wp->w_buffer->b_p_ft != NUL 3959 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) 3960 { 3961 vim_snprintf((char *)tmp, sizeof(tmp), ",%s", 3962 wp->w_buffer->b_p_ft); 3963 for (t = tmp; *t != 0; t++) 3964 *t = TOUPPER_LOC(*t); 3965 str = tmp; 3966 } 3967 break; 3968 #endif 3969 3970 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 3971 case STL_PREVIEWFLAG: 3972 case STL_PREVIEWFLAG_ALT: 3973 itemisflag = TRUE; 3974 if (wp->w_p_pvw) 3975 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" 3976 : _("[Preview]")); 3977 break; 3978 3979 case STL_QUICKFIX: 3980 if (bt_quickfix(wp->w_buffer)) 3981 str = (char_u *)(wp->w_llist_ref 3982 ? _(msg_loclist) 3983 : _(msg_qflist)); 3984 break; 3985 #endif 3986 3987 case STL_MODIFIED: 3988 case STL_MODIFIED_ALT: 3989 itemisflag = TRUE; 3990 switch ((opt == STL_MODIFIED_ALT) 3991 + bufIsChanged(wp->w_buffer) * 2 3992 + (!wp->w_buffer->b_p_ma) * 4) 3993 { 3994 case 2: str = (char_u *)"[+]"; break; 3995 case 3: str = (char_u *)",+"; break; 3996 case 4: str = (char_u *)"[-]"; break; 3997 case 5: str = (char_u *)",-"; break; 3998 case 6: str = (char_u *)"[+-]"; break; 3999 case 7: str = (char_u *)",+-"; break; 4000 } 4001 break; 4002 4003 case STL_HIGHLIGHT: 4004 t = s; 4005 while (*s != '#' && *s != NUL) 4006 ++s; 4007 if (*s == '#') 4008 { 4009 item[curitem].type = Highlight; 4010 item[curitem].start = p; 4011 item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); 4012 curitem++; 4013 } 4014 ++s; 4015 continue; 4016 } 4017 4018 item[curitem].start = p; 4019 item[curitem].type = Normal; 4020 if (str != NULL && *str) 4021 { 4022 t = str; 4023 if (itemisflag) 4024 { 4025 if ((t[0] && t[1]) 4026 && ((!prevchar_isitem && *t == ',') 4027 || (prevchar_isflag && *t == ' '))) 4028 t++; 4029 prevchar_isflag = TRUE; 4030 } 4031 l = vim_strsize(t); 4032 if (l > 0) 4033 prevchar_isitem = TRUE; 4034 if (l > maxwid) 4035 { 4036 while (l >= maxwid) 4037 #ifdef FEAT_MBYTE 4038 if (has_mbyte) 4039 { 4040 l -= ptr2cells(t); 4041 t += (*mb_ptr2len)(t); 4042 } 4043 else 4044 #endif 4045 l -= byte2cells(*t++); 4046 if (p + 1 >= out + outlen) 4047 break; 4048 *p++ = '<'; 4049 } 4050 if (minwid > 0) 4051 { 4052 for (; l < minwid && p + 1 < out + outlen; l++) 4053 { 4054 /* Don't put a "-" in front of a digit. */ 4055 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t)) 4056 *p++ = ' '; 4057 else 4058 *p++ = fillchar; 4059 } 4060 minwid = 0; 4061 } 4062 else 4063 minwid *= -1; 4064 while (*t && p + 1 < out + outlen) 4065 { 4066 *p++ = *t++; 4067 /* Change a space by fillchar, unless fillchar is '-' and a 4068 * digit follows. */ 4069 if (fillable && p[-1] == ' ' 4070 && (!VIM_ISDIGIT(*t) || fillchar != '-')) 4071 p[-1] = fillchar; 4072 } 4073 for (; l < minwid && p + 1 < out + outlen; l++) 4074 *p++ = fillchar; 4075 } 4076 else if (num >= 0) 4077 { 4078 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); 4079 char_u nstr[20]; 4080 4081 if (p + 20 >= out + outlen) 4082 break; /* not sufficient space */ 4083 prevchar_isitem = TRUE; 4084 t = nstr; 4085 if (opt == STL_VIRTCOL_ALT) 4086 { 4087 *t++ = '-'; 4088 minwid--; 4089 } 4090 *t++ = '%'; 4091 if (zeropad) 4092 *t++ = '0'; 4093 *t++ = '*'; 4094 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); 4095 *t = 0; 4096 4097 for (n = num, l = 1; n >= nbase; n /= nbase) 4098 l++; 4099 if (opt == STL_VIRTCOL_ALT) 4100 l++; 4101 if (l > maxwid) 4102 { 4103 l += 2; 4104 n = l - maxwid; 4105 while (l-- > maxwid) 4106 num /= nbase; 4107 *t++ = '>'; 4108 *t++ = '%'; 4109 *t = t[-3]; 4110 *++t = 0; 4111 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, 4112 0, num, n); 4113 } 4114 else 4115 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, 4116 minwid, num); 4117 p += STRLEN(p); 4118 } 4119 else 4120 item[curitem].type = Empty; 4121 4122 if (opt == STL_VIM_EXPR) 4123 vim_free(str); 4124 4125 if (num >= 0 || (!itemisflag && str && *str)) 4126 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ 4127 curitem++; 4128 } 4129 *p = NUL; 4130 itemcnt = curitem; 4131 4132 #ifdef FEAT_EVAL 4133 if (usefmt != fmt) 4134 vim_free(usefmt); 4135 #endif 4136 4137 width = vim_strsize(out); 4138 if (maxwidth > 0 && width > maxwidth) 4139 { 4140 /* Result is too long, must truncate somewhere. */ 4141 l = 0; 4142 if (itemcnt == 0) 4143 s = out; 4144 else 4145 { 4146 for ( ; l < itemcnt; l++) 4147 if (item[l].type == Trunc) 4148 { 4149 /* Truncate at %< item. */ 4150 s = item[l].start; 4151 break; 4152 } 4153 if (l == itemcnt) 4154 { 4155 /* No %< item, truncate first item. */ 4156 s = item[0].start; 4157 l = 0; 4158 } 4159 } 4160 4161 if (width - vim_strsize(s) >= maxwidth) 4162 { 4163 /* Truncation mark is beyond max length */ 4164 #ifdef FEAT_MBYTE 4165 if (has_mbyte) 4166 { 4167 s = out; 4168 width = 0; 4169 for (;;) 4170 { 4171 width += ptr2cells(s); 4172 if (width >= maxwidth) 4173 break; 4174 s += (*mb_ptr2len)(s); 4175 } 4176 /* Fill up for half a double-wide character. */ 4177 while (++width < maxwidth) 4178 *s++ = fillchar; 4179 } 4180 else 4181 #endif 4182 s = out + maxwidth - 1; 4183 for (l = 0; l < itemcnt; l++) 4184 if (item[l].start > s) 4185 break; 4186 itemcnt = l; 4187 *s++ = '>'; 4188 *s = 0; 4189 } 4190 else 4191 { 4192 #ifdef FEAT_MBYTE 4193 if (has_mbyte) 4194 { 4195 n = 0; 4196 while (width >= maxwidth) 4197 { 4198 width -= ptr2cells(s + n); 4199 n += (*mb_ptr2len)(s + n); 4200 } 4201 } 4202 else 4203 #endif 4204 n = width - maxwidth + 1; 4205 p = s + n; 4206 STRMOVE(s + 1, p); 4207 *s = '<'; 4208 4209 /* Fill up for half a double-wide character. */ 4210 while (++width < maxwidth) 4211 { 4212 s = s + STRLEN(s); 4213 *s++ = fillchar; 4214 *s = NUL; 4215 } 4216 4217 --n; /* count the '<' */ 4218 for (; l < itemcnt; l++) 4219 { 4220 if (item[l].start - n >= s) 4221 item[l].start -= n; 4222 else 4223 item[l].start = s; 4224 } 4225 } 4226 width = maxwidth; 4227 } 4228 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) 4229 { 4230 /* Apply STL_MIDDLE if any */ 4231 for (l = 0; l < itemcnt; l++) 4232 if (item[l].type == Middle) 4233 break; 4234 if (l < itemcnt) 4235 { 4236 p = item[l].start + maxwidth - width; 4237 STRMOVE(p, item[l].start); 4238 for (s = item[l].start; s < p; s++) 4239 *s = fillchar; 4240 for (l++; l < itemcnt; l++) 4241 item[l].start += maxwidth - width; 4242 width = maxwidth; 4243 } 4244 } 4245 4246 /* Store the info about highlighting. */ 4247 if (hltab != NULL) 4248 { 4249 sp = hltab; 4250 for (l = 0; l < itemcnt; l++) 4251 { 4252 if (item[l].type == Highlight) 4253 { 4254 sp->start = item[l].start; 4255 sp->userhl = item[l].minwid; 4256 sp++; 4257 } 4258 } 4259 sp->start = NULL; 4260 sp->userhl = 0; 4261 } 4262 4263 /* Store the info about tab pages labels. */ 4264 if (tabtab != NULL) 4265 { 4266 sp = tabtab; 4267 for (l = 0; l < itemcnt; l++) 4268 { 4269 if (item[l].type == TabPage) 4270 { 4271 sp->start = item[l].start; 4272 sp->userhl = item[l].minwid; 4273 sp++; 4274 } 4275 } 4276 sp->start = NULL; 4277 sp->userhl = 0; 4278 } 4279 4280 return width; 4281 } 4282 #endif /* FEAT_STL_OPT */ 4283 4284 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \ 4285 || defined(FEAT_GUI_TABLINE) || defined(PROTO) 4286 /* 4287 * Get relative cursor position in window into "buf[buflen]", in the form 99%, 4288 * using "Top", "Bot" or "All" when appropriate. 4289 */ 4290 void 4291 get_rel_pos(wp, buf, buflen) 4292 win_T *wp; 4293 char_u *buf; 4294 int buflen; 4295 { 4296 long above; /* number of lines above window */ 4297 long below; /* number of lines below window */ 4298 4299 above = wp->w_topline - 1; 4300 #ifdef FEAT_DIFF 4301 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; 4302 #endif 4303 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1; 4304 if (below <= 0) 4305 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")), 4306 (size_t)(buflen - 1)); 4307 else if (above <= 0) 4308 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1)); 4309 else 4310 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L 4311 ? (int)(above / ((above + below) / 100L)) 4312 : (int)(above * 100L / (above + below))); 4313 } 4314 #endif 4315 4316 /* 4317 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file. 4318 * Return TRUE if it was appended. 4319 */ 4320 static int 4321 append_arg_number(wp, buf, buflen, add_file) 4322 win_T *wp; 4323 char_u *buf; 4324 int buflen; 4325 int add_file; /* Add "file" before the arg number */ 4326 { 4327 char_u *p; 4328 4329 if (ARGCOUNT <= 1) /* nothing to do */ 4330 return FALSE; 4331 4332 p = buf + STRLEN(buf); /* go to the end of the buffer */ 4333 if (p - buf + 35 >= buflen) /* getting too long */ 4334 return FALSE; 4335 *p++ = ' '; 4336 *p++ = '('; 4337 if (add_file) 4338 { 4339 STRCPY(p, "file "); 4340 p += 5; 4341 } 4342 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), 4343 wp->w_arg_idx_invalid ? "(%d) of %d)" 4344 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT); 4345 return TRUE; 4346 } 4347 4348 /* 4349 * If fname is not a full path, make it a full path. 4350 * Returns pointer to allocated memory (NULL for failure). 4351 */ 4352 char_u * 4353 fix_fname(fname) 4354 char_u *fname; 4355 { 4356 /* 4357 * Force expanding the path always for Unix, because symbolic links may 4358 * mess up the full path name, even though it starts with a '/'. 4359 * Also expand when there is ".." in the file name, try to remove it, 4360 * because "c:/src/../README" is equal to "c:/README". 4361 * Similarly "c:/src//file" is equal to "c:/src/file". 4362 * For MS-Windows also expand names like "longna~1" to "longname". 4363 */ 4364 #ifdef UNIX 4365 return FullName_save(fname, TRUE); 4366 #else 4367 if (!vim_isAbsName(fname) 4368 || strstr((char *)fname, "..") != NULL 4369 || strstr((char *)fname, "//") != NULL 4370 # ifdef BACKSLASH_IN_FILENAME 4371 || strstr((char *)fname, "\\\\") != NULL 4372 # endif 4373 # if defined(MSWIN) || defined(DJGPP) 4374 || vim_strchr(fname, '~') != NULL 4375 # endif 4376 ) 4377 return FullName_save(fname, FALSE); 4378 4379 fname = vim_strsave(fname); 4380 4381 # ifdef USE_FNAME_CASE 4382 # ifdef USE_LONG_FNAME 4383 if (USE_LONG_FNAME) 4384 # endif 4385 { 4386 if (fname != NULL) 4387 fname_case(fname, 0); /* set correct case for file name */ 4388 } 4389 # endif 4390 4391 return fname; 4392 #endif 4393 } 4394 4395 /* 4396 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL. 4397 * "ffname" becomes a pointer to allocated memory (or NULL). 4398 */ 4399 void 4400 fname_expand(buf, ffname, sfname) 4401 buf_T *buf UNUSED; 4402 char_u **ffname; 4403 char_u **sfname; 4404 { 4405 if (*ffname == NULL) /* if no file name given, nothing to do */ 4406 return; 4407 if (*sfname == NULL) /* if no short file name given, use ffname */ 4408 *sfname = *ffname; 4409 *ffname = fix_fname(*ffname); /* expand to full path */ 4410 4411 #ifdef FEAT_SHORTCUT 4412 if (!buf->b_p_bin) 4413 { 4414 char_u *rfname; 4415 4416 /* If the file name is a shortcut file, use the file it links to. */ 4417 rfname = mch_resolve_shortcut(*ffname); 4418 if (rfname != NULL) 4419 { 4420 vim_free(*ffname); 4421 *ffname = rfname; 4422 *sfname = rfname; 4423 } 4424 } 4425 #endif 4426 } 4427 4428 /* 4429 * Get the file name for an argument list entry. 4430 */ 4431 char_u * 4432 alist_name(aep) 4433 aentry_T *aep; 4434 { 4435 buf_T *bp; 4436 4437 /* Use the name from the associated buffer if it exists. */ 4438 bp = buflist_findnr(aep->ae_fnum); 4439 if (bp == NULL || bp->b_fname == NULL) 4440 return aep->ae_fname; 4441 return bp->b_fname; 4442 } 4443 4444 #if defined(FEAT_WINDOWS) || defined(PROTO) 4445 /* 4446 * do_arg_all(): Open up to 'count' windows, one for each argument. 4447 */ 4448 void 4449 do_arg_all(count, forceit, keep_tabs) 4450 int count; 4451 int forceit; /* hide buffers in current windows */ 4452 int keep_tabs; /* keep current tabs, for ":tab drop file" */ 4453 { 4454 int i; 4455 win_T *wp, *wpnext; 4456 char_u *opened; /* Array of weight for which args are open: 4457 * 0: not opened 4458 * 1: opened in other tab 4459 * 2: opened in curtab 4460 * 3: opened in curtab and curwin 4461 */ 4462 int opened_len; /* length of opened[] */ 4463 int use_firstwin = FALSE; /* use first window for arglist */ 4464 int split_ret = OK; 4465 int p_ea_save; 4466 alist_T *alist; /* argument list to be used */ 4467 buf_T *buf; 4468 tabpage_T *tpnext; 4469 int had_tab = cmdmod.tab; 4470 win_T *old_curwin, *last_curwin; 4471 tabpage_T *old_curtab, *last_curtab; 4472 win_T *new_curwin = NULL; 4473 tabpage_T *new_curtab = NULL; 4474 4475 if (ARGCOUNT <= 0) 4476 { 4477 /* Don't give an error message. We don't want it when the ":all" 4478 * command is in the .vimrc. */ 4479 return; 4480 } 4481 setpcmark(); 4482 4483 opened_len = ARGCOUNT; 4484 opened = alloc_clear((unsigned)opened_len); 4485 if (opened == NULL) 4486 return; 4487 4488 /* Autocommands may do anything to the argument list. Make sure it's not 4489 * freed while we are working here by "locking" it. We still have to 4490 * watch out for its size to be changed. */ 4491 alist = curwin->w_alist; 4492 ++alist->al_refcount; 4493 4494 old_curwin = curwin; 4495 old_curtab = curtab; 4496 4497 #ifdef FEAT_GUI 4498 need_mouse_correct = TRUE; 4499 #endif 4500 4501 /* 4502 * Try closing all windows that are not in the argument list. 4503 * Also close windows that are not full width; 4504 * When 'hidden' or "forceit" set the buffer becomes hidden. 4505 * Windows that have a changed buffer and can't be hidden won't be closed. 4506 * When the ":tab" modifier was used do this for all tab pages. 4507 */ 4508 if (had_tab > 0) 4509 goto_tabpage_tp(first_tabpage, TRUE); 4510 for (;;) 4511 { 4512 tpnext = curtab->tp_next; 4513 for (wp = firstwin; wp != NULL; wp = wpnext) 4514 { 4515 wpnext = wp->w_next; 4516 buf = wp->w_buffer; 4517 if (buf->b_ffname == NULL 4518 || (!keep_tabs && buf->b_nwindows > 1) 4519 #ifdef FEAT_VERTSPLIT 4520 || wp->w_width != Columns 4521 #endif 4522 ) 4523 i = opened_len; 4524 else 4525 { 4526 /* check if the buffer in this window is in the arglist */ 4527 for (i = 0; i < opened_len; ++i) 4528 { 4529 if (i < alist->al_ga.ga_len 4530 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum 4531 || fullpathcmp(alist_name(&AARGLIST(alist)[i]), 4532 buf->b_ffname, TRUE) & FPC_SAME)) 4533 { 4534 int weight = 1; 4535 4536 if (old_curtab == curtab) 4537 { 4538 ++weight; 4539 if (old_curwin == wp) 4540 ++weight; 4541 } 4542 4543 if (weight > (int)opened[i]) 4544 { 4545 opened[i] = (char_u)weight; 4546 if (i == 0) 4547 { 4548 if (new_curwin != NULL) 4549 new_curwin->w_arg_idx = opened_len; 4550 new_curwin = wp; 4551 new_curtab = curtab; 4552 } 4553 } 4554 else if (keep_tabs) 4555 i = opened_len; 4556 4557 if (wp->w_alist != alist) 4558 { 4559 /* Use the current argument list for all windows 4560 * containing a file from it. */ 4561 alist_unlink(wp->w_alist); 4562 wp->w_alist = alist; 4563 ++wp->w_alist->al_refcount; 4564 } 4565 break; 4566 } 4567 } 4568 } 4569 wp->w_arg_idx = i; 4570 4571 if (i == opened_len && !keep_tabs)/* close this window */ 4572 { 4573 if (P_HID(buf) || forceit || buf->b_nwindows > 1 4574 || !bufIsChanged(buf)) 4575 { 4576 /* If the buffer was changed, and we would like to hide it, 4577 * try autowriting. */ 4578 if (!P_HID(buf) && buf->b_nwindows <= 1 4579 && bufIsChanged(buf)) 4580 { 4581 (void)autowrite(buf, FALSE); 4582 #ifdef FEAT_AUTOCMD 4583 /* check if autocommands removed the window */ 4584 if (!win_valid(wp) || !buf_valid(buf)) 4585 { 4586 wpnext = firstwin; /* start all over... */ 4587 continue; 4588 } 4589 #endif 4590 } 4591 #ifdef FEAT_WINDOWS 4592 /* don't close last window */ 4593 if (firstwin == lastwin 4594 && (first_tabpage->tp_next == NULL || !had_tab)) 4595 #endif 4596 use_firstwin = TRUE; 4597 #ifdef FEAT_WINDOWS 4598 else 4599 { 4600 win_close(wp, !P_HID(buf) && !bufIsChanged(buf)); 4601 # ifdef FEAT_AUTOCMD 4602 /* check if autocommands removed the next window */ 4603 if (!win_valid(wpnext)) 4604 wpnext = firstwin; /* start all over... */ 4605 # endif 4606 } 4607 #endif 4608 } 4609 } 4610 } 4611 4612 /* Without the ":tab" modifier only do the current tab page. */ 4613 if (had_tab == 0 || tpnext == NULL) 4614 break; 4615 4616 # ifdef FEAT_AUTOCMD 4617 /* check if autocommands removed the next tab page */ 4618 if (!valid_tabpage(tpnext)) 4619 tpnext = first_tabpage; /* start all over...*/ 4620 # endif 4621 goto_tabpage_tp(tpnext, TRUE); 4622 } 4623 4624 /* 4625 * Open a window for files in the argument list that don't have one. 4626 * ARGCOUNT may change while doing this, because of autocommands. 4627 */ 4628 if (count > opened_len || count <= 0) 4629 count = opened_len; 4630 4631 #ifdef FEAT_AUTOCMD 4632 /* Don't execute Win/Buf Enter/Leave autocommands here. */ 4633 ++autocmd_no_enter; 4634 ++autocmd_no_leave; 4635 #endif 4636 last_curwin = curwin; 4637 last_curtab = curtab; 4638 win_enter(lastwin, FALSE); 4639 #ifdef FEAT_WINDOWS 4640 /* ":drop all" should re-use an empty window to avoid "--remote-tab" 4641 * leaving an empty tab page when executed locally. */ 4642 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1 4643 && curbuf->b_ffname == NULL && !curbuf->b_changed) 4644 use_firstwin = TRUE; 4645 #endif 4646 4647 for (i = 0; i < count && i < opened_len && !got_int; ++i) 4648 { 4649 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1) 4650 arg_had_last = TRUE; 4651 if (opened[i] > 0) 4652 { 4653 /* Move the already present window to below the current window */ 4654 if (curwin->w_arg_idx != i) 4655 { 4656 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next) 4657 { 4658 if (wpnext->w_arg_idx == i) 4659 { 4660 if (keep_tabs) 4661 { 4662 new_curwin = wpnext; 4663 new_curtab = curtab; 4664 } 4665 else 4666 win_move_after(wpnext, curwin); 4667 break; 4668 } 4669 } 4670 } 4671 } 4672 else if (split_ret == OK) 4673 { 4674 if (!use_firstwin) /* split current window */ 4675 { 4676 p_ea_save = p_ea; 4677 p_ea = TRUE; /* use space from all windows */ 4678 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); 4679 p_ea = p_ea_save; 4680 if (split_ret == FAIL) 4681 continue; 4682 } 4683 #ifdef FEAT_AUTOCMD 4684 else /* first window: do autocmd for leaving this buffer */ 4685 --autocmd_no_leave; 4686 #endif 4687 4688 /* 4689 * edit file "i" 4690 */ 4691 curwin->w_arg_idx = i; 4692 if (i == 0) 4693 { 4694 new_curwin = curwin; 4695 new_curtab = curtab; 4696 } 4697 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL, 4698 ECMD_ONE, 4699 ((P_HID(curwin->w_buffer) 4700 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0) 4701 + ECMD_OLDBUF, curwin); 4702 #ifdef FEAT_AUTOCMD 4703 if (use_firstwin) 4704 ++autocmd_no_leave; 4705 #endif 4706 use_firstwin = FALSE; 4707 } 4708 ui_breakcheck(); 4709 4710 /* When ":tab" was used open a new tab for a new window repeatedly. */ 4711 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) 4712 cmdmod.tab = 9999; 4713 } 4714 4715 /* Remove the "lock" on the argument list. */ 4716 alist_unlink(alist); 4717 4718 #ifdef FEAT_AUTOCMD 4719 --autocmd_no_enter; 4720 #endif 4721 /* restore last referenced tabpage's curwin */ 4722 if (last_curtab != new_curtab) 4723 { 4724 if (valid_tabpage(last_curtab)) 4725 goto_tabpage_tp(last_curtab, TRUE); 4726 if (win_valid(last_curwin)) 4727 win_enter(last_curwin, FALSE); 4728 } 4729 /* to window with first arg */ 4730 if (valid_tabpage(new_curtab)) 4731 goto_tabpage_tp(new_curtab, TRUE); 4732 if (win_valid(new_curwin)) 4733 win_enter(new_curwin, FALSE); 4734 4735 #ifdef FEAT_AUTOCMD 4736 --autocmd_no_leave; 4737 #endif 4738 vim_free(opened); 4739 } 4740 4741 # if defined(FEAT_LISTCMDS) || defined(PROTO) 4742 /* 4743 * Open a window for a number of buffers. 4744 */ 4745 void 4746 ex_buffer_all(eap) 4747 exarg_T *eap; 4748 { 4749 buf_T *buf; 4750 win_T *wp, *wpnext; 4751 int split_ret = OK; 4752 int p_ea_save; 4753 int open_wins = 0; 4754 int r; 4755 int count; /* Maximum number of windows to open. */ 4756 int all; /* When TRUE also load inactive buffers. */ 4757 #ifdef FEAT_WINDOWS 4758 int had_tab = cmdmod.tab; 4759 tabpage_T *tpnext; 4760 #endif 4761 4762 if (eap->addr_count == 0) /* make as many windows as possible */ 4763 count = 9999; 4764 else 4765 count = eap->line2; /* make as many windows as specified */ 4766 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) 4767 all = FALSE; 4768 else 4769 all = TRUE; 4770 4771 setpcmark(); 4772 4773 #ifdef FEAT_GUI 4774 need_mouse_correct = TRUE; 4775 #endif 4776 4777 /* 4778 * Close superfluous windows (two windows for the same buffer). 4779 * Also close windows that are not full-width. 4780 */ 4781 #ifdef FEAT_WINDOWS 4782 if (had_tab > 0) 4783 goto_tabpage_tp(first_tabpage, TRUE); 4784 for (;;) 4785 { 4786 #endif 4787 tpnext = curtab->tp_next; 4788 for (wp = firstwin; wp != NULL; wp = wpnext) 4789 { 4790 wpnext = wp->w_next; 4791 if ((wp->w_buffer->b_nwindows > 1 4792 #ifdef FEAT_VERTSPLIT 4793 || ((cmdmod.split & WSP_VERT) 4794 ? wp->w_height + wp->w_status_height < Rows - p_ch 4795 - tabline_height() 4796 : wp->w_width != Columns) 4797 #endif 4798 #ifdef FEAT_WINDOWS 4799 || (had_tab > 0 && wp != firstwin) 4800 #endif 4801 ) && firstwin != lastwin 4802 #ifdef FEAT_AUTOCMD 4803 && !(wp->w_closing || wp->w_buffer->b_closing) 4804 #endif 4805 ) 4806 { 4807 win_close(wp, FALSE); 4808 #ifdef FEAT_AUTOCMD 4809 wpnext = firstwin; /* just in case an autocommand does 4810 something strange with windows */ 4811 tpnext = first_tabpage; /* start all over...*/ 4812 open_wins = 0; 4813 #endif 4814 } 4815 else 4816 ++open_wins; 4817 } 4818 4819 #ifdef FEAT_WINDOWS 4820 /* Without the ":tab" modifier only do the current tab page. */ 4821 if (had_tab == 0 || tpnext == NULL) 4822 break; 4823 goto_tabpage_tp(tpnext, TRUE); 4824 } 4825 #endif 4826 4827 /* 4828 * Go through the buffer list. When a buffer doesn't have a window yet, 4829 * open one. Otherwise move the window to the right position. 4830 * Watch out for autocommands that delete buffers or windows! 4831 */ 4832 #ifdef FEAT_AUTOCMD 4833 /* Don't execute Win/Buf Enter/Leave autocommands here. */ 4834 ++autocmd_no_enter; 4835 #endif 4836 win_enter(lastwin, FALSE); 4837 #ifdef FEAT_AUTOCMD 4838 ++autocmd_no_leave; 4839 #endif 4840 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next) 4841 { 4842 /* Check if this buffer needs a window */ 4843 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl) 4844 continue; 4845 4846 #ifdef FEAT_WINDOWS 4847 if (had_tab != 0) 4848 { 4849 /* With the ":tab" modifier don't move the window. */ 4850 if (buf->b_nwindows > 0) 4851 wp = lastwin; /* buffer has a window, skip it */ 4852 else 4853 wp = NULL; 4854 } 4855 else 4856 #endif 4857 { 4858 /* Check if this buffer already has a window */ 4859 for (wp = firstwin; wp != NULL; wp = wp->w_next) 4860 if (wp->w_buffer == buf) 4861 break; 4862 /* If the buffer already has a window, move it */ 4863 if (wp != NULL) 4864 win_move_after(wp, curwin); 4865 } 4866 4867 if (wp == NULL && split_ret == OK) 4868 { 4869 /* Split the window and put the buffer in it */ 4870 p_ea_save = p_ea; 4871 p_ea = TRUE; /* use space from all windows */ 4872 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); 4873 ++open_wins; 4874 p_ea = p_ea_save; 4875 if (split_ret == FAIL) 4876 continue; 4877 4878 /* Open the buffer in this window. */ 4879 #if defined(HAS_SWAP_EXISTS_ACTION) 4880 swap_exists_action = SEA_DIALOG; 4881 #endif 4882 set_curbuf(buf, DOBUF_GOTO); 4883 #ifdef FEAT_AUTOCMD 4884 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */ 4885 { 4886 #if defined(HAS_SWAP_EXISTS_ACTION) 4887 swap_exists_action = SEA_NONE; 4888 # endif 4889 break; 4890 } 4891 #endif 4892 #if defined(HAS_SWAP_EXISTS_ACTION) 4893 if (swap_exists_action == SEA_QUIT) 4894 { 4895 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4896 cleanup_T cs; 4897 4898 /* Reset the error/interrupt/exception state here so that 4899 * aborting() returns FALSE when closing a window. */ 4900 enter_cleanup(&cs); 4901 # endif 4902 4903 /* User selected Quit at ATTENTION prompt; close this window. */ 4904 win_close(curwin, TRUE); 4905 --open_wins; 4906 swap_exists_action = SEA_NONE; 4907 swap_exists_did_quit = TRUE; 4908 4909 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4910 /* Restore the error/interrupt/exception state if not 4911 * discarded by a new aborting error, interrupt, or uncaught 4912 * exception. */ 4913 leave_cleanup(&cs); 4914 # endif 4915 } 4916 else 4917 handle_swap_exists(NULL); 4918 #endif 4919 } 4920 4921 ui_breakcheck(); 4922 if (got_int) 4923 { 4924 (void)vgetc(); /* only break the file loading, not the rest */ 4925 break; 4926 } 4927 #ifdef FEAT_EVAL 4928 /* Autocommands deleted the buffer or aborted script processing!!! */ 4929 if (aborting()) 4930 break; 4931 #endif 4932 #ifdef FEAT_WINDOWS 4933 /* When ":tab" was used open a new tab for a new window repeatedly. */ 4934 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) 4935 cmdmod.tab = 9999; 4936 #endif 4937 } 4938 #ifdef FEAT_AUTOCMD 4939 --autocmd_no_enter; 4940 #endif 4941 win_enter(firstwin, FALSE); /* back to first window */ 4942 #ifdef FEAT_AUTOCMD 4943 --autocmd_no_leave; 4944 #endif 4945 4946 /* 4947 * Close superfluous windows. 4948 */ 4949 for (wp = lastwin; open_wins > count; ) 4950 { 4951 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer) 4952 || autowrite(wp->w_buffer, FALSE) == OK); 4953 #ifdef FEAT_AUTOCMD 4954 if (!win_valid(wp)) 4955 { 4956 /* BufWrite Autocommands made the window invalid, start over */ 4957 wp = lastwin; 4958 } 4959 else 4960 #endif 4961 if (r) 4962 { 4963 win_close(wp, !P_HID(wp->w_buffer)); 4964 --open_wins; 4965 wp = lastwin; 4966 } 4967 else 4968 { 4969 wp = wp->w_prev; 4970 if (wp == NULL) 4971 break; 4972 } 4973 } 4974 } 4975 # endif /* FEAT_LISTCMDS */ 4976 4977 #endif /* FEAT_WINDOWS */ 4978 4979 static int chk_modeline __ARGS((linenr_T, int)); 4980 4981 /* 4982 * do_modelines() - process mode lines for the current file 4983 * 4984 * "flags" can be: 4985 * OPT_WINONLY only set options local to window 4986 * OPT_NOWIN don't set options local to window 4987 * 4988 * Returns immediately if the "ml" option isn't set. 4989 */ 4990 void 4991 do_modelines(flags) 4992 int flags; 4993 { 4994 linenr_T lnum; 4995 int nmlines; 4996 static int entered = 0; 4997 4998 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0) 4999 return; 5000 5001 /* Disallow recursive entry here. Can happen when executing a modeline 5002 * triggers an autocommand, which reloads modelines with a ":do". */ 5003 if (entered) 5004 return; 5005 5006 ++entered; 5007 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines; 5008 ++lnum) 5009 if (chk_modeline(lnum, flags) == FAIL) 5010 nmlines = 0; 5011 5012 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines 5013 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum) 5014 if (chk_modeline(lnum, flags) == FAIL) 5015 nmlines = 0; 5016 --entered; 5017 } 5018 5019 #include "version.h" /* for version number */ 5020 5021 /* 5022 * chk_modeline() - check a single line for a mode string 5023 * Return FAIL if an error encountered. 5024 */ 5025 static int 5026 chk_modeline(lnum, flags) 5027 linenr_T lnum; 5028 int flags; /* Same as for do_modelines(). */ 5029 { 5030 char_u *s; 5031 char_u *e; 5032 char_u *linecopy; /* local copy of any modeline found */ 5033 int prev; 5034 int vers; 5035 int end; 5036 int retval = OK; 5037 char_u *save_sourcing_name; 5038 linenr_T save_sourcing_lnum; 5039 #ifdef FEAT_EVAL 5040 scid_T save_SID; 5041 #endif 5042 5043 prev = -1; 5044 for (s = ml_get(lnum); *s != NUL; ++s) 5045 { 5046 if (prev == -1 || vim_isspace(prev)) 5047 { 5048 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) 5049 || STRNCMP(s, "vi:", (size_t)3) == 0) 5050 break; 5051 if (STRNCMP(s, "vim", 3) == 0) 5052 { 5053 if (s[3] == '<' || s[3] == '=' || s[3] == '>') 5054 e = s + 4; 5055 else 5056 e = s + 3; 5057 vers = getdigits(&e); 5058 if (*e == ':' 5059 && (s[3] == ':' 5060 || (VIM_VERSION_100 >= vers && isdigit(s[3])) 5061 || (VIM_VERSION_100 < vers && s[3] == '<') 5062 || (VIM_VERSION_100 > vers && s[3] == '>') 5063 || (VIM_VERSION_100 == vers && s[3] == '='))) 5064 break; 5065 } 5066 } 5067 prev = *s; 5068 } 5069 5070 if (*s) 5071 { 5072 do /* skip over "ex:", "vi:" or "vim:" */ 5073 ++s; 5074 while (s[-1] != ':'); 5075 5076 s = linecopy = vim_strsave(s); /* copy the line, it will change */ 5077 if (linecopy == NULL) 5078 return FAIL; 5079 5080 save_sourcing_lnum = sourcing_lnum; 5081 save_sourcing_name = sourcing_name; 5082 sourcing_lnum = lnum; /* prepare for emsg() */ 5083 sourcing_name = (char_u *)"modelines"; 5084 5085 end = FALSE; 5086 while (end == FALSE) 5087 { 5088 s = skipwhite(s); 5089 if (*s == NUL) 5090 break; 5091 5092 /* 5093 * Find end of set command: ':' or end of line. 5094 * Skip over "\:", replacing it with ":". 5095 */ 5096 for (e = s; *e != ':' && *e != NUL; ++e) 5097 if (e[0] == '\\' && e[1] == ':') 5098 STRMOVE(e, e + 1); 5099 if (*e == NUL) 5100 end = TRUE; 5101 5102 /* 5103 * If there is a "set" command, require a terminating ':' and 5104 * ignore the stuff after the ':'. 5105 * "vi:set opt opt opt: foo" -- foo not interpreted 5106 * "vi:opt opt opt: foo" -- foo interpreted 5107 * Accept "se" for compatibility with Elvis. 5108 */ 5109 if (STRNCMP(s, "set ", (size_t)4) == 0 5110 || STRNCMP(s, "se ", (size_t)3) == 0) 5111 { 5112 if (*e != ':') /* no terminating ':'? */ 5113 break; 5114 end = TRUE; 5115 s = vim_strchr(s, ' ') + 1; 5116 } 5117 *e = NUL; /* truncate the set command */ 5118 5119 if (*s != NUL) /* skip over an empty "::" */ 5120 { 5121 #ifdef FEAT_EVAL 5122 save_SID = current_SID; 5123 current_SID = SID_MODELINE; 5124 #endif 5125 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); 5126 #ifdef FEAT_EVAL 5127 current_SID = save_SID; 5128 #endif 5129 if (retval == FAIL) /* stop if error found */ 5130 break; 5131 } 5132 s = e + 1; /* advance to next part */ 5133 } 5134 5135 sourcing_lnum = save_sourcing_lnum; 5136 sourcing_name = save_sourcing_name; 5137 5138 vim_free(linecopy); 5139 } 5140 return retval; 5141 } 5142 5143 #if defined(FEAT_VIMINFO) || defined(PROTO) 5144 int 5145 read_viminfo_bufferlist(virp, writing) 5146 vir_T *virp; 5147 int writing; 5148 { 5149 char_u *tab; 5150 linenr_T lnum; 5151 colnr_T col; 5152 buf_T *buf; 5153 char_u *sfname; 5154 char_u *xline; 5155 5156 /* Handle long line and escaped characters. */ 5157 xline = viminfo_readstring(virp, 1, FALSE); 5158 5159 /* don't read in if there are files on the command-line or if writing: */ 5160 if (xline != NULL && !writing && ARGCOUNT == 0 5161 && find_viminfo_parameter('%') != NULL) 5162 { 5163 /* Format is: <fname> Tab <lnum> Tab <col>. 5164 * Watch out for a Tab in the file name, work from the end. */ 5165 lnum = 0; 5166 col = 0; 5167 tab = vim_strrchr(xline, '\t'); 5168 if (tab != NULL) 5169 { 5170 *tab++ = '\0'; 5171 col = (colnr_T)atoi((char *)tab); 5172 tab = vim_strrchr(xline, '\t'); 5173 if (tab != NULL) 5174 { 5175 *tab++ = '\0'; 5176 lnum = atol((char *)tab); 5177 } 5178 } 5179 5180 /* Expand "~/" in the file name at "line + 1" to a full path. 5181 * Then try shortening it by comparing with the current directory */ 5182 expand_env(xline, NameBuff, MAXPATHL); 5183 sfname = shorten_fname1(NameBuff); 5184 5185 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED); 5186 if (buf != NULL) /* just in case... */ 5187 { 5188 buf->b_last_cursor.lnum = lnum; 5189 buf->b_last_cursor.col = col; 5190 buflist_setfpos(buf, curwin, lnum, col, FALSE); 5191 } 5192 } 5193 vim_free(xline); 5194 5195 return viminfo_readline(virp); 5196 } 5197 5198 void 5199 write_viminfo_bufferlist(fp) 5200 FILE *fp; 5201 { 5202 buf_T *buf; 5203 #ifdef FEAT_WINDOWS 5204 win_T *win; 5205 tabpage_T *tp; 5206 #endif 5207 char_u *line; 5208 int max_buffers; 5209 5210 if (find_viminfo_parameter('%') == NULL) 5211 return; 5212 5213 /* Without a number -1 is returned: do all buffers. */ 5214 max_buffers = get_viminfo_parameter('%'); 5215 5216 /* Allocate room for the file name, lnum and col. */ 5217 #define LINE_BUF_LEN (MAXPATHL + 40) 5218 line = alloc(LINE_BUF_LEN); 5219 if (line == NULL) 5220 return; 5221 5222 #ifdef FEAT_WINDOWS 5223 FOR_ALL_TAB_WINDOWS(tp, win) 5224 set_last_cursor(win); 5225 #else 5226 set_last_cursor(curwin); 5227 #endif 5228 5229 fputs(_("\n# Buffer list:\n"), fp); 5230 for (buf = firstbuf; buf != NULL ; buf = buf->b_next) 5231 { 5232 if (buf->b_fname == NULL 5233 || !buf->b_p_bl 5234 #ifdef FEAT_QUICKFIX 5235 || bt_quickfix(buf) 5236 #endif 5237 || removable(buf->b_ffname)) 5238 continue; 5239 5240 if (max_buffers-- == 0) 5241 break; 5242 putc('%', fp); 5243 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE); 5244 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d", 5245 (long)buf->b_last_cursor.lnum, 5246 buf->b_last_cursor.col); 5247 viminfo_writestring(fp, line); 5248 } 5249 vim_free(line); 5250 } 5251 #endif 5252 5253 5254 /* 5255 * Return special buffer name. 5256 * Returns NULL when the buffer has a normal file name. 5257 */ 5258 char_u * 5259 buf_spname(buf) 5260 buf_T *buf; 5261 { 5262 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS) 5263 if (bt_quickfix(buf)) 5264 { 5265 win_T *win = NULL; 5266 tabpage_T *tp; 5267 5268 /* 5269 * For location list window, w_llist_ref points to the location list. 5270 * For quickfix window, w_llist_ref is NULL. 5271 */ 5272 FOR_ALL_TAB_WINDOWS(tp, win) 5273 if (win->w_buffer == buf) 5274 goto win_found; 5275 win_found: 5276 if (win != NULL && win->w_llist_ref != NULL) 5277 return (char_u *)_(msg_loclist); 5278 else 5279 return (char_u *)_(msg_qflist); 5280 } 5281 #endif 5282 #ifdef FEAT_QUICKFIX 5283 /* There is no _file_ when 'buftype' is "nofile", b_sfname 5284 * contains the name as specified by the user */ 5285 if (bt_nofile(buf)) 5286 { 5287 if (buf->b_sfname != NULL) 5288 return buf->b_sfname; 5289 return (char_u *)_("[Scratch]"); 5290 } 5291 #endif 5292 if (buf->b_fname == NULL) 5293 return (char_u *)_("[No Name]"); 5294 return NULL; 5295 } 5296 5297 5298 #if defined(FEAT_SIGNS) || defined(PROTO) 5299 /* 5300 * Insert the sign into the signlist. 5301 */ 5302 static void 5303 insert_sign(buf, prev, next, id, lnum, typenr) 5304 buf_T *buf; /* buffer to store sign in */ 5305 signlist_T *prev; /* previous sign entry */ 5306 signlist_T *next; /* next sign entry */ 5307 int id; /* sign ID */ 5308 linenr_T lnum; /* line number which gets the mark */ 5309 int typenr; /* typenr of sign we are adding */ 5310 { 5311 signlist_T *newsign; 5312 5313 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE); 5314 if (newsign != NULL) 5315 { 5316 newsign->id = id; 5317 newsign->lnum = lnum; 5318 newsign->typenr = typenr; 5319 newsign->next = next; 5320 #ifdef FEAT_NETBEANS_INTG 5321 newsign->prev = prev; 5322 if (next != NULL) 5323 next->prev = newsign; 5324 #endif 5325 5326 if (prev == NULL) 5327 { 5328 /* When adding first sign need to redraw the windows to create the 5329 * column for signs. */ 5330 if (buf->b_signlist == NULL) 5331 { 5332 redraw_buf_later(buf, NOT_VALID); 5333 changed_cline_bef_curs(); 5334 } 5335 5336 /* first sign in signlist */ 5337 buf->b_signlist = newsign; 5338 } 5339 else 5340 prev->next = newsign; 5341 } 5342 } 5343 5344 /* 5345 * Add the sign into the signlist. Find the right spot to do it though. 5346 */ 5347 void 5348 buf_addsign(buf, id, lnum, typenr) 5349 buf_T *buf; /* buffer to store sign in */ 5350 int id; /* sign ID */ 5351 linenr_T lnum; /* line number which gets the mark */ 5352 int typenr; /* typenr of sign we are adding */ 5353 { 5354 signlist_T *sign; /* a sign in the signlist */ 5355 signlist_T *prev; /* the previous sign */ 5356 5357 prev = NULL; 5358 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5359 { 5360 if (lnum == sign->lnum && id == sign->id) 5361 { 5362 sign->typenr = typenr; 5363 return; 5364 } 5365 else if ( 5366 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */ 5367 id < 0 && 5368 #endif 5369 lnum < sign->lnum) 5370 { 5371 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ 5372 /* XXX - GRP: Is this because of sign slide problem? Or is it 5373 * really needed? Or is it because we allow multiple signs per 5374 * line? If so, should I add that feature to FEAT_SIGNS? 5375 */ 5376 while (prev != NULL && prev->lnum == lnum) 5377 prev = prev->prev; 5378 if (prev == NULL) 5379 sign = buf->b_signlist; 5380 else 5381 sign = prev->next; 5382 #endif 5383 insert_sign(buf, prev, sign, id, lnum, typenr); 5384 return; 5385 } 5386 prev = sign; 5387 } 5388 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ 5389 /* XXX - GRP: See previous comment */ 5390 while (prev != NULL && prev->lnum == lnum) 5391 prev = prev->prev; 5392 if (prev == NULL) 5393 sign = buf->b_signlist; 5394 else 5395 sign = prev->next; 5396 #endif 5397 insert_sign(buf, prev, sign, id, lnum, typenr); 5398 5399 return; 5400 } 5401 5402 linenr_T 5403 buf_change_sign_type(buf, markId, typenr) 5404 buf_T *buf; /* buffer to store sign in */ 5405 int markId; /* sign ID */ 5406 int typenr; /* typenr of sign we are adding */ 5407 { 5408 signlist_T *sign; /* a sign in the signlist */ 5409 5410 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5411 { 5412 if (sign->id == markId) 5413 { 5414 sign->typenr = typenr; 5415 return sign->lnum; 5416 } 5417 } 5418 5419 return (linenr_T)0; 5420 } 5421 5422 int 5423 buf_getsigntype(buf, lnum, type) 5424 buf_T *buf; 5425 linenr_T lnum; 5426 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */ 5427 { 5428 signlist_T *sign; /* a sign in a b_signlist */ 5429 5430 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5431 if (sign->lnum == lnum 5432 && (type == SIGN_ANY 5433 # ifdef FEAT_SIGN_ICONS 5434 || (type == SIGN_ICON 5435 && sign_get_image(sign->typenr) != NULL) 5436 # endif 5437 || (type == SIGN_TEXT 5438 && sign_get_text(sign->typenr) != NULL) 5439 || (type == SIGN_LINEHL 5440 && sign_get_attr(sign->typenr, TRUE) != 0))) 5441 return sign->typenr; 5442 return 0; 5443 } 5444 5445 5446 linenr_T 5447 buf_delsign(buf, id) 5448 buf_T *buf; /* buffer sign is stored in */ 5449 int id; /* sign id */ 5450 { 5451 signlist_T **lastp; /* pointer to pointer to current sign */ 5452 signlist_T *sign; /* a sign in a b_signlist */ 5453 signlist_T *next; /* the next sign in a b_signlist */ 5454 linenr_T lnum; /* line number whose sign was deleted */ 5455 5456 lastp = &buf->b_signlist; 5457 lnum = 0; 5458 for (sign = buf->b_signlist; sign != NULL; sign = next) 5459 { 5460 next = sign->next; 5461 if (sign->id == id) 5462 { 5463 *lastp = next; 5464 #ifdef FEAT_NETBEANS_INTG 5465 if (next != NULL) 5466 next->prev = sign->prev; 5467 #endif 5468 lnum = sign->lnum; 5469 vim_free(sign); 5470 break; 5471 } 5472 else 5473 lastp = &sign->next; 5474 } 5475 5476 /* When deleted the last sign need to redraw the windows to remove the 5477 * sign column. */ 5478 if (buf->b_signlist == NULL) 5479 { 5480 redraw_buf_later(buf, NOT_VALID); 5481 changed_cline_bef_curs(); 5482 } 5483 5484 return lnum; 5485 } 5486 5487 5488 /* 5489 * Find the line number of the sign with the requested id. If the sign does 5490 * not exist, return 0 as the line number. This will still let the correct file 5491 * get loaded. 5492 */ 5493 int 5494 buf_findsign(buf, id) 5495 buf_T *buf; /* buffer to store sign in */ 5496 int id; /* sign ID */ 5497 { 5498 signlist_T *sign; /* a sign in the signlist */ 5499 5500 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5501 if (sign->id == id) 5502 return sign->lnum; 5503 5504 return 0; 5505 } 5506 5507 int 5508 buf_findsign_id(buf, lnum) 5509 buf_T *buf; /* buffer whose sign we are searching for */ 5510 linenr_T lnum; /* line number of sign */ 5511 { 5512 signlist_T *sign; /* a sign in the signlist */ 5513 5514 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5515 if (sign->lnum == lnum) 5516 return sign->id; 5517 5518 return 0; 5519 } 5520 5521 5522 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO) 5523 /* see if a given type of sign exists on a specific line */ 5524 int 5525 buf_findsigntype_id(buf, lnum, typenr) 5526 buf_T *buf; /* buffer whose sign we are searching for */ 5527 linenr_T lnum; /* line number of sign */ 5528 int typenr; /* sign type number */ 5529 { 5530 signlist_T *sign; /* a sign in the signlist */ 5531 5532 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5533 if (sign->lnum == lnum && sign->typenr == typenr) 5534 return sign->id; 5535 5536 return 0; 5537 } 5538 5539 5540 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) 5541 /* return the number of icons on the given line */ 5542 int 5543 buf_signcount(buf, lnum) 5544 buf_T *buf; 5545 linenr_T lnum; 5546 { 5547 signlist_T *sign; /* a sign in the signlist */ 5548 int count = 0; 5549 5550 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5551 if (sign->lnum == lnum) 5552 if (sign_get_image(sign->typenr) != NULL) 5553 count++; 5554 5555 return count; 5556 } 5557 # endif /* FEAT_SIGN_ICONS */ 5558 # endif /* FEAT_NETBEANS_INTG */ 5559 5560 5561 /* 5562 * Delete signs in buffer "buf". 5563 */ 5564 void 5565 buf_delete_signs(buf) 5566 buf_T *buf; 5567 { 5568 signlist_T *next; 5569 5570 while (buf->b_signlist != NULL) 5571 { 5572 next = buf->b_signlist->next; 5573 vim_free(buf->b_signlist); 5574 buf->b_signlist = next; 5575 } 5576 } 5577 5578 /* 5579 * Delete all signs in all buffers. 5580 */ 5581 void 5582 buf_delete_all_signs() 5583 { 5584 buf_T *buf; /* buffer we are checking for signs */ 5585 5586 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 5587 if (buf->b_signlist != NULL) 5588 { 5589 /* Need to redraw the windows to remove the sign column. */ 5590 redraw_buf_later(buf, NOT_VALID); 5591 buf_delete_signs(buf); 5592 } 5593 } 5594 5595 /* 5596 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. 5597 */ 5598 void 5599 sign_list_placed(rbuf) 5600 buf_T *rbuf; 5601 { 5602 buf_T *buf; 5603 signlist_T *p; 5604 char lbuf[BUFSIZ]; 5605 5606 MSG_PUTS_TITLE(_("\n--- Signs ---")); 5607 msg_putchar('\n'); 5608 if (rbuf == NULL) 5609 buf = firstbuf; 5610 else 5611 buf = rbuf; 5612 while (buf != NULL && !got_int) 5613 { 5614 if (buf->b_signlist != NULL) 5615 { 5616 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname); 5617 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D)); 5618 msg_putchar('\n'); 5619 } 5620 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next) 5621 { 5622 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"), 5623 (long)p->lnum, p->id, sign_typenr2name(p->typenr)); 5624 MSG_PUTS(lbuf); 5625 msg_putchar('\n'); 5626 } 5627 if (rbuf != NULL) 5628 break; 5629 buf = buf->b_next; 5630 } 5631 } 5632 5633 /* 5634 * Adjust a placed sign for inserted/deleted lines. 5635 */ 5636 void 5637 sign_mark_adjust(line1, line2, amount, amount_after) 5638 linenr_T line1; 5639 linenr_T line2; 5640 long amount; 5641 long amount_after; 5642 { 5643 signlist_T *sign; /* a sign in a b_signlist */ 5644 5645 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next) 5646 { 5647 if (sign->lnum >= line1 && sign->lnum <= line2) 5648 { 5649 if (amount == MAXLNUM) 5650 sign->lnum = line1; 5651 else 5652 sign->lnum += amount; 5653 } 5654 else if (sign->lnum > line2) 5655 sign->lnum += amount_after; 5656 } 5657 } 5658 #endif /* FEAT_SIGNS */ 5659 5660 /* 5661 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed. 5662 */ 5663 void 5664 set_buflisted(on) 5665 int on; 5666 { 5667 if (on != curbuf->b_p_bl) 5668 { 5669 curbuf->b_p_bl = on; 5670 #ifdef FEAT_AUTOCMD 5671 if (on) 5672 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); 5673 else 5674 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); 5675 #endif 5676 } 5677 } 5678 5679 /* 5680 * Read the file for "buf" again and check if the contents changed. 5681 * Return TRUE if it changed or this could not be checked. 5682 */ 5683 int 5684 buf_contents_changed(buf) 5685 buf_T *buf; 5686 { 5687 buf_T *newbuf; 5688 int differ = TRUE; 5689 linenr_T lnum; 5690 aco_save_T aco; 5691 exarg_T ea; 5692 5693 /* Allocate a buffer without putting it in the buffer list. */ 5694 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); 5695 if (newbuf == NULL) 5696 return TRUE; 5697 5698 /* Force the 'fileencoding' and 'fileformat' to be equal. */ 5699 if (prep_exarg(&ea, buf) == FAIL) 5700 { 5701 wipe_buffer(newbuf, FALSE); 5702 return TRUE; 5703 } 5704 5705 /* set curwin/curbuf to buf and save a few things */ 5706 aucmd_prepbuf(&aco, newbuf); 5707 5708 if (ml_open(curbuf) == OK 5709 && readfile(buf->b_ffname, buf->b_fname, 5710 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, 5711 &ea, READ_NEW | READ_DUMMY) == OK) 5712 { 5713 /* compare the two files line by line */ 5714 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count) 5715 { 5716 differ = FALSE; 5717 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) 5718 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0) 5719 { 5720 differ = TRUE; 5721 break; 5722 } 5723 } 5724 } 5725 vim_free(ea.cmd); 5726 5727 /* restore curwin/curbuf and a few other things */ 5728 aucmd_restbuf(&aco); 5729 5730 if (curbuf != newbuf) /* safety check */ 5731 wipe_buffer(newbuf, FALSE); 5732 5733 return differ; 5734 } 5735 5736 /* 5737 * Wipe out a buffer and decrement the last buffer number if it was used for 5738 * this buffer. Call this to wipe out a temp buffer that does not contain any 5739 * marks. 5740 */ 5741 void 5742 wipe_buffer(buf, aucmd) 5743 buf_T *buf; 5744 int aucmd UNUSED; /* When TRUE trigger autocommands. */ 5745 { 5746 if (buf->b_fnum == top_file_num - 1) 5747 --top_file_num; 5748 5749 #ifdef FEAT_AUTOCMD 5750 if (!aucmd) /* Don't trigger BufDelete autocommands here. */ 5751 block_autocmds(); 5752 #endif 5753 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); 5754 #ifdef FEAT_AUTOCMD 5755 if (!aucmd) 5756 unblock_autocmds(); 5757 #endif 5758 } 5759