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