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((regmatch_T *rmp, buf_T *buf, int ignore_case)); 32 # define HAVE_BUFLIST_MATCH 33 static char_u *fname_match __ARGS((regmatch_T *rmp, char_u *name, int ignore_case)); 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(_(e_nobufnr), 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 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */ 1798 1799 /* Init the options. */ 1800 buf->b_p_initialized = FALSE; 1801 buf_copy_options(buf, BCO_ENTER); 1802 1803 #ifdef FEAT_KEYMAP 1804 /* need to reload lmaps and set b:keymap_name */ 1805 curbuf->b_kmap_state |= KEYMAP_INIT; 1806 #endif 1807 } 1808 else 1809 { 1810 /* 1811 * put new buffer at the end of the buffer list 1812 */ 1813 buf->b_next = NULL; 1814 if (firstbuf == NULL) /* buffer list is empty */ 1815 { 1816 buf->b_prev = NULL; 1817 firstbuf = buf; 1818 } 1819 else /* append new buffer at end of list */ 1820 { 1821 lastbuf->b_next = buf; 1822 buf->b_prev = lastbuf; 1823 } 1824 lastbuf = buf; 1825 1826 buf->b_fnum = top_file_num++; 1827 if (top_file_num < 0) /* wrap around (may cause duplicates) */ 1828 { 1829 EMSG(_("W14: Warning: List of file names overflow")); 1830 if (emsg_silent == 0) 1831 { 1832 out_flush(); 1833 ui_delay(3000L, TRUE); /* make sure it is noticed */ 1834 } 1835 top_file_num = 1; 1836 } 1837 1838 /* 1839 * Always copy the options from the current buffer. 1840 */ 1841 buf_copy_options(buf, BCO_ALWAYS); 1842 } 1843 1844 buf->b_wininfo->wi_fpos.lnum = lnum; 1845 buf->b_wininfo->wi_win = curwin; 1846 1847 #ifdef FEAT_SYN_HL 1848 hash_init(&buf->b_s.b_keywtab); 1849 hash_init(&buf->b_s.b_keywtab_ic); 1850 #endif 1851 1852 buf->b_fname = buf->b_sfname; 1853 #ifdef UNIX 1854 if (st.st_dev == (dev_T)-1) 1855 buf->b_dev_valid = FALSE; 1856 else 1857 { 1858 buf->b_dev_valid = TRUE; 1859 buf->b_dev = st.st_dev; 1860 buf->b_ino = st.st_ino; 1861 } 1862 #endif 1863 buf->b_u_synced = TRUE; 1864 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED; 1865 if (flags & BLN_DUMMY) 1866 buf->b_flags |= BF_DUMMY; 1867 buf_clear_file(buf); 1868 clrallmarks(buf); /* clear marks */ 1869 fmarks_check_names(buf); /* check file marks for this file */ 1870 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */ 1871 #ifdef FEAT_AUTOCMD 1872 if (!(flags & BLN_DUMMY)) 1873 { 1874 /* Tricky: these autocommands may change the buffer list. They could 1875 * also split the window with re-using the one empty buffer. This may 1876 * result in unexpectedly losing the empty buffer. */ 1877 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf); 1878 if (!buf_valid(buf)) 1879 return NULL; 1880 if (flags & BLN_LISTED) 1881 { 1882 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf); 1883 if (!buf_valid(buf)) 1884 return NULL; 1885 } 1886 # ifdef FEAT_EVAL 1887 if (aborting()) /* autocmds may abort script processing */ 1888 return NULL; 1889 # endif 1890 } 1891 #endif 1892 1893 return buf; 1894 } 1895 1896 /* 1897 * Free the memory for the options of a buffer. 1898 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and 1899 * 'fileencoding'. 1900 */ 1901 void 1902 free_buf_options(buf, free_p_ff) 1903 buf_T *buf; 1904 int free_p_ff; 1905 { 1906 if (free_p_ff) 1907 { 1908 #ifdef FEAT_MBYTE 1909 clear_string_option(&buf->b_p_fenc); 1910 #endif 1911 clear_string_option(&buf->b_p_ff); 1912 #ifdef FEAT_QUICKFIX 1913 clear_string_option(&buf->b_p_bh); 1914 clear_string_option(&buf->b_p_bt); 1915 #endif 1916 } 1917 #ifdef FEAT_FIND_ID 1918 clear_string_option(&buf->b_p_def); 1919 clear_string_option(&buf->b_p_inc); 1920 # ifdef FEAT_EVAL 1921 clear_string_option(&buf->b_p_inex); 1922 # endif 1923 #endif 1924 #if defined(FEAT_CINDENT) && defined(FEAT_EVAL) 1925 clear_string_option(&buf->b_p_inde); 1926 clear_string_option(&buf->b_p_indk); 1927 #endif 1928 #if defined(FEAT_BEVAL) && defined(FEAT_EVAL) 1929 clear_string_option(&buf->b_p_bexpr); 1930 #endif 1931 #if defined(FEAT_CRYPT) 1932 clear_string_option(&buf->b_p_cm); 1933 #endif 1934 #if defined(FEAT_EVAL) 1935 clear_string_option(&buf->b_p_fex); 1936 #endif 1937 #ifdef FEAT_CRYPT 1938 clear_string_option(&buf->b_p_key); 1939 #endif 1940 clear_string_option(&buf->b_p_kp); 1941 clear_string_option(&buf->b_p_mps); 1942 clear_string_option(&buf->b_p_fo); 1943 clear_string_option(&buf->b_p_flp); 1944 clear_string_option(&buf->b_p_isk); 1945 #ifdef FEAT_KEYMAP 1946 clear_string_option(&buf->b_p_keymap); 1947 ga_clear(&buf->b_kmap_ga); 1948 #endif 1949 #ifdef FEAT_COMMENTS 1950 clear_string_option(&buf->b_p_com); 1951 #endif 1952 #ifdef FEAT_FOLDING 1953 clear_string_option(&buf->b_p_cms); 1954 #endif 1955 clear_string_option(&buf->b_p_nf); 1956 #ifdef FEAT_SYN_HL 1957 clear_string_option(&buf->b_p_syn); 1958 #endif 1959 #ifdef FEAT_SPELL 1960 clear_string_option(&buf->b_s.b_p_spc); 1961 clear_string_option(&buf->b_s.b_p_spf); 1962 vim_regfree(buf->b_s.b_cap_prog); 1963 buf->b_s.b_cap_prog = NULL; 1964 clear_string_option(&buf->b_s.b_p_spl); 1965 #endif 1966 #ifdef FEAT_SEARCHPATH 1967 clear_string_option(&buf->b_p_sua); 1968 #endif 1969 #ifdef FEAT_AUTOCMD 1970 clear_string_option(&buf->b_p_ft); 1971 #endif 1972 #ifdef FEAT_CINDENT 1973 clear_string_option(&buf->b_p_cink); 1974 clear_string_option(&buf->b_p_cino); 1975 #endif 1976 #if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT) 1977 clear_string_option(&buf->b_p_cinw); 1978 #endif 1979 #ifdef FEAT_INS_EXPAND 1980 clear_string_option(&buf->b_p_cpt); 1981 #endif 1982 #ifdef FEAT_COMPL_FUNC 1983 clear_string_option(&buf->b_p_cfu); 1984 clear_string_option(&buf->b_p_ofu); 1985 #endif 1986 #ifdef FEAT_QUICKFIX 1987 clear_string_option(&buf->b_p_gp); 1988 clear_string_option(&buf->b_p_mp); 1989 clear_string_option(&buf->b_p_efm); 1990 #endif 1991 clear_string_option(&buf->b_p_ep); 1992 clear_string_option(&buf->b_p_path); 1993 clear_string_option(&buf->b_p_tags); 1994 #ifdef FEAT_INS_EXPAND 1995 clear_string_option(&buf->b_p_dict); 1996 clear_string_option(&buf->b_p_tsr); 1997 #endif 1998 #ifdef FEAT_TEXTOBJ 1999 clear_string_option(&buf->b_p_qe); 2000 #endif 2001 buf->b_p_ar = -1; 2002 buf->b_p_ul = NO_LOCAL_UNDOLEVEL; 2003 #ifdef FEAT_LISP 2004 clear_string_option(&buf->b_p_lw); 2005 #endif 2006 clear_string_option(&buf->b_p_bkc); 2007 } 2008 2009 /* 2010 * get alternate file n 2011 * set linenr to lnum or altfpos.lnum if lnum == 0 2012 * also set cursor column to altfpos.col if 'startofline' is not set. 2013 * if (options & GETF_SETMARK) call setpcmark() 2014 * if (options & GETF_ALT) we are jumping to an alternate file. 2015 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping 2016 * 2017 * return FAIL for failure, OK for success 2018 */ 2019 int 2020 buflist_getfile(n, lnum, options, forceit) 2021 int n; 2022 linenr_T lnum; 2023 int options; 2024 int forceit; 2025 { 2026 buf_T *buf; 2027 #ifdef FEAT_WINDOWS 2028 win_T *wp = NULL; 2029 #endif 2030 pos_T *fpos; 2031 colnr_T col; 2032 2033 buf = buflist_findnr(n); 2034 if (buf == NULL) 2035 { 2036 if ((options & GETF_ALT) && n == 0) 2037 EMSG(_(e_noalt)); 2038 else 2039 EMSGN(_("E92: Buffer %ld not found"), n); 2040 return FAIL; 2041 } 2042 2043 /* if alternate file is the current buffer, nothing to do */ 2044 if (buf == curbuf) 2045 return OK; 2046 2047 if (text_locked()) 2048 { 2049 text_locked_msg(); 2050 return FAIL; 2051 } 2052 #ifdef FEAT_AUTOCMD 2053 if (curbuf_locked()) 2054 return FAIL; 2055 #endif 2056 2057 /* altfpos may be changed by getfile(), get it now */ 2058 if (lnum == 0) 2059 { 2060 fpos = buflist_findfpos(buf); 2061 lnum = fpos->lnum; 2062 col = fpos->col; 2063 } 2064 else 2065 col = 0; 2066 2067 #ifdef FEAT_WINDOWS 2068 if (options & GETF_SWITCH) 2069 { 2070 /* If 'switchbuf' contains "useopen": jump to first window containing 2071 * "buf" if one exists */ 2072 if (swb_flags & SWB_USEOPEN) 2073 wp = buf_jump_open_win(buf); 2074 2075 /* If 'switchbuf' contains "usetab": jump to first window in any tab 2076 * page containing "buf" if one exists */ 2077 if (wp == NULL && (swb_flags & SWB_USETAB)) 2078 wp = buf_jump_open_tab(buf); 2079 2080 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the 2081 * current buffer isn't empty: open new tab or window */ 2082 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB)) 2083 && !bufempty()) 2084 { 2085 if (swb_flags & SWB_NEWTAB) 2086 tabpage_new(); 2087 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0) 2088 == FAIL) 2089 return FAIL; 2090 RESET_BINDING(curwin); 2091 } 2092 } 2093 #endif 2094 2095 ++RedrawingDisabled; 2096 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK), 2097 lnum, forceit) <= 0) 2098 { 2099 --RedrawingDisabled; 2100 2101 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */ 2102 if (!p_sol && col != 0) 2103 { 2104 curwin->w_cursor.col = col; 2105 check_cursor_col(); 2106 #ifdef FEAT_VIRTUALEDIT 2107 curwin->w_cursor.coladd = 0; 2108 #endif 2109 curwin->w_set_curswant = TRUE; 2110 } 2111 return OK; 2112 } 2113 --RedrawingDisabled; 2114 return FAIL; 2115 } 2116 2117 /* 2118 * go to the last know line number for the current buffer 2119 */ 2120 void 2121 buflist_getfpos() 2122 { 2123 pos_T *fpos; 2124 2125 fpos = buflist_findfpos(curbuf); 2126 2127 curwin->w_cursor.lnum = fpos->lnum; 2128 check_cursor_lnum(); 2129 2130 if (p_sol) 2131 curwin->w_cursor.col = 0; 2132 else 2133 { 2134 curwin->w_cursor.col = fpos->col; 2135 check_cursor_col(); 2136 #ifdef FEAT_VIRTUALEDIT 2137 curwin->w_cursor.coladd = 0; 2138 #endif 2139 curwin->w_set_curswant = TRUE; 2140 } 2141 } 2142 2143 #if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO) 2144 /* 2145 * Find file in buffer list by name (it has to be for the current window). 2146 * Returns NULL if not found. 2147 */ 2148 buf_T * 2149 buflist_findname_exp(fname) 2150 char_u *fname; 2151 { 2152 char_u *ffname; 2153 buf_T *buf = NULL; 2154 2155 /* First make the name into a full path name */ 2156 ffname = FullName_save(fname, 2157 #ifdef UNIX 2158 TRUE /* force expansion, get rid of symbolic links */ 2159 #else 2160 FALSE 2161 #endif 2162 ); 2163 if (ffname != NULL) 2164 { 2165 buf = buflist_findname(ffname); 2166 vim_free(ffname); 2167 } 2168 return buf; 2169 } 2170 #endif 2171 2172 /* 2173 * Find file in buffer list by name (it has to be for the current window). 2174 * "ffname" must have a full path. 2175 * Skips dummy buffers. 2176 * Returns NULL if not found. 2177 */ 2178 buf_T * 2179 buflist_findname(ffname) 2180 char_u *ffname; 2181 { 2182 #ifdef UNIX 2183 struct stat st; 2184 2185 if (mch_stat((char *)ffname, &st) < 0) 2186 st.st_dev = (dev_T)-1; 2187 return buflist_findname_stat(ffname, &st); 2188 } 2189 2190 /* 2191 * Same as buflist_findname(), but pass the stat structure to avoid getting it 2192 * twice for the same file. 2193 * Returns NULL if not found. 2194 */ 2195 static buf_T * 2196 buflist_findname_stat(ffname, stp) 2197 char_u *ffname; 2198 struct stat *stp; 2199 { 2200 #endif 2201 buf_T *buf; 2202 2203 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2204 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname 2205 #ifdef UNIX 2206 , stp 2207 #endif 2208 )) 2209 return buf; 2210 return NULL; 2211 } 2212 2213 #if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \ 2214 || defined(PROTO) 2215 /* 2216 * Find file in buffer list by a regexp pattern. 2217 * Return fnum of the found buffer. 2218 * Return < 0 for error. 2219 */ 2220 int 2221 buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only) 2222 char_u *pattern; 2223 char_u *pattern_end; /* pointer to first char after pattern */ 2224 int unlisted; /* find unlisted buffers */ 2225 int diffmode UNUSED; /* find diff-mode buffers only */ 2226 int curtab_only; /* find buffers in current tab only */ 2227 { 2228 buf_T *buf; 2229 int match = -1; 2230 int find_listed; 2231 char_u *pat; 2232 char_u *patend; 2233 int attempt; 2234 char_u *p; 2235 int toggledollar; 2236 2237 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) 2238 { 2239 if (*pattern == '%') 2240 match = curbuf->b_fnum; 2241 else 2242 match = curwin->w_alt_fnum; 2243 #ifdef FEAT_DIFF 2244 if (diffmode && !diff_mode_buf(buflist_findnr(match))) 2245 match = -1; 2246 #endif 2247 } 2248 2249 /* 2250 * Try four ways of matching a listed buffer: 2251 * attempt == 0: without '^' or '$' (at any position) 2252 * attempt == 1: with '^' at start (only at position 0) 2253 * attempt == 2: with '$' at end (only match at end) 2254 * attempt == 3: with '^' at start and '$' at end (only full match) 2255 * Repeat this for finding an unlisted buffer if there was no matching 2256 * listed buffer. 2257 */ 2258 else 2259 { 2260 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE); 2261 if (pat == NULL) 2262 return -1; 2263 patend = pat + STRLEN(pat) - 1; 2264 toggledollar = (patend > pat && *patend == '$'); 2265 2266 /* First try finding a listed buffer. If not found and "unlisted" 2267 * is TRUE, try finding an unlisted buffer. */ 2268 find_listed = TRUE; 2269 for (;;) 2270 { 2271 for (attempt = 0; attempt <= 3; ++attempt) 2272 { 2273 regmatch_T regmatch; 2274 2275 /* may add '^' and '$' */ 2276 if (toggledollar) 2277 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */ 2278 p = pat; 2279 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */ 2280 ++p; 2281 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); 2282 if (regmatch.regprog == NULL) 2283 { 2284 vim_free(pat); 2285 return -1; 2286 } 2287 2288 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2289 if (buf->b_p_bl == find_listed 2290 #ifdef FEAT_DIFF 2291 && (!diffmode || diff_mode_buf(buf)) 2292 #endif 2293 && buflist_match(®match, buf, FALSE) != NULL) 2294 { 2295 if (curtab_only) 2296 { 2297 /* Ignore the match if the buffer is not open in 2298 * the current tab. */ 2299 #ifdef FEAT_WINDOWS 2300 win_T *wp; 2301 2302 for (wp = firstwin; wp != NULL; wp = wp->w_next) 2303 if (wp->w_buffer == buf) 2304 break; 2305 if (wp == NULL) 2306 continue; 2307 #else 2308 if (curwin->w_buffer != buf) 2309 continue; 2310 #endif 2311 } 2312 if (match >= 0) /* already found a match */ 2313 { 2314 match = -2; 2315 break; 2316 } 2317 match = buf->b_fnum; /* remember first match */ 2318 } 2319 2320 vim_regfree(regmatch.regprog); 2321 if (match >= 0) /* found one match */ 2322 break; 2323 } 2324 2325 /* Only search for unlisted buffers if there was no match with 2326 * a listed buffer. */ 2327 if (!unlisted || !find_listed || match != -1) 2328 break; 2329 find_listed = FALSE; 2330 } 2331 2332 vim_free(pat); 2333 } 2334 2335 if (match == -2) 2336 EMSG2(_("E93: More than one match for %s"), pattern); 2337 else if (match < 0) 2338 EMSG2(_("E94: No matching buffer for %s"), pattern); 2339 return match; 2340 } 2341 #endif 2342 2343 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2344 2345 /* 2346 * Find all buffer names that match. 2347 * For command line expansion of ":buf" and ":sbuf". 2348 * Return OK if matches found, FAIL otherwise. 2349 */ 2350 int 2351 ExpandBufnames(pat, num_file, file, options) 2352 char_u *pat; 2353 int *num_file; 2354 char_u ***file; 2355 int options; 2356 { 2357 int count = 0; 2358 buf_T *buf; 2359 int round; 2360 char_u *p; 2361 int attempt; 2362 char_u *patc; 2363 2364 *num_file = 0; /* return values in case of FAIL */ 2365 *file = NULL; 2366 2367 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */ 2368 if (*pat == '^') 2369 { 2370 patc = alloc((unsigned)STRLEN(pat) + 11); 2371 if (patc == NULL) 2372 return FAIL; 2373 STRCPY(patc, "\\(^\\|[\\/]\\)"); 2374 STRCPY(patc + 11, pat + 1); 2375 } 2376 else 2377 patc = pat; 2378 2379 /* 2380 * attempt == 0: try match with '\<', match at start of word 2381 * attempt == 1: try match without '\<', match anywhere 2382 */ 2383 for (attempt = 0; attempt <= 1; ++attempt) 2384 { 2385 regmatch_T regmatch; 2386 2387 if (attempt > 0 && patc == pat) 2388 break; /* there was no anchor, no need to try again */ 2389 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC); 2390 if (regmatch.regprog == NULL) 2391 { 2392 if (patc != pat) 2393 vim_free(patc); 2394 return FAIL; 2395 } 2396 2397 /* 2398 * round == 1: Count the matches. 2399 * round == 2: Build the array to keep the matches. 2400 */ 2401 for (round = 1; round <= 2; ++round) 2402 { 2403 count = 0; 2404 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2405 { 2406 if (!buf->b_p_bl) /* skip unlisted buffers */ 2407 continue; 2408 p = buflist_match(®match, buf, p_wic); 2409 if (p != NULL) 2410 { 2411 if (round == 1) 2412 ++count; 2413 else 2414 { 2415 if (options & WILD_HOME_REPLACE) 2416 p = home_replace_save(buf, p); 2417 else 2418 p = vim_strsave(p); 2419 (*file)[count++] = p; 2420 } 2421 } 2422 } 2423 if (count == 0) /* no match found, break here */ 2424 break; 2425 if (round == 1) 2426 { 2427 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); 2428 if (*file == NULL) 2429 { 2430 vim_regfree(regmatch.regprog); 2431 if (patc != pat) 2432 vim_free(patc); 2433 return FAIL; 2434 } 2435 } 2436 } 2437 vim_regfree(regmatch.regprog); 2438 if (count) /* match(es) found, break here */ 2439 break; 2440 } 2441 2442 if (patc != pat) 2443 vim_free(patc); 2444 2445 *num_file = count; 2446 return (count == 0 ? FAIL : OK); 2447 } 2448 2449 #endif /* FEAT_CMDL_COMPL */ 2450 2451 #ifdef HAVE_BUFLIST_MATCH 2452 /* 2453 * Check for a match on the file name for buffer "buf" with regprog "prog". 2454 */ 2455 static char_u * 2456 buflist_match(rmp, buf, ignore_case) 2457 regmatch_T *rmp; 2458 buf_T *buf; 2459 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */ 2460 { 2461 char_u *match; 2462 2463 /* First try the short file name, then the long file name. */ 2464 match = fname_match(rmp, buf->b_sfname, ignore_case); 2465 if (match == NULL) 2466 match = fname_match(rmp, buf->b_ffname, ignore_case); 2467 2468 return match; 2469 } 2470 2471 /* 2472 * Try matching the regexp in "prog" with file name "name". 2473 * Return "name" when there is a match, NULL when not. 2474 */ 2475 static char_u * 2476 fname_match(rmp, name, ignore_case) 2477 regmatch_T *rmp; 2478 char_u *name; 2479 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */ 2480 { 2481 char_u *match = NULL; 2482 char_u *p; 2483 2484 if (name != NULL) 2485 { 2486 /* Ignore case when 'fileignorecase' or the argument is set. */ 2487 rmp->rm_ic = p_fic || ignore_case; 2488 if (vim_regexec(rmp, name, (colnr_T)0)) 2489 match = name; 2490 else 2491 { 2492 /* Replace $(HOME) with '~' and try matching again. */ 2493 p = home_replace_save(NULL, name); 2494 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0)) 2495 match = name; 2496 vim_free(p); 2497 } 2498 } 2499 2500 return match; 2501 } 2502 #endif 2503 2504 /* 2505 * find file in buffer list by number 2506 */ 2507 buf_T * 2508 buflist_findnr(nr) 2509 int nr; 2510 { 2511 buf_T *buf; 2512 2513 if (nr == 0) 2514 nr = curwin->w_alt_fnum; 2515 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 2516 if (buf->b_fnum == nr) 2517 return (buf); 2518 return NULL; 2519 } 2520 2521 /* 2522 * Get name of file 'n' in the buffer list. 2523 * When the file has no name an empty string is returned. 2524 * home_replace() is used to shorten the file name (used for marks). 2525 * Returns a pointer to allocated memory, of NULL when failed. 2526 */ 2527 char_u * 2528 buflist_nr2name(n, fullname, helptail) 2529 int n; 2530 int fullname; 2531 int helptail; /* for help buffers return tail only */ 2532 { 2533 buf_T *buf; 2534 2535 buf = buflist_findnr(n); 2536 if (buf == NULL) 2537 return NULL; 2538 return home_replace_save(helptail ? buf : NULL, 2539 fullname ? buf->b_ffname : buf->b_fname); 2540 } 2541 2542 /* 2543 * Set the "lnum" and "col" for the buffer "buf" and the current window. 2544 * When "copy_options" is TRUE save the local window option values. 2545 * When "lnum" is 0 only do the options. 2546 */ 2547 static void 2548 buflist_setfpos(buf, win, lnum, col, copy_options) 2549 buf_T *buf; 2550 win_T *win; 2551 linenr_T lnum; 2552 colnr_T col; 2553 int copy_options; 2554 { 2555 wininfo_T *wip; 2556 2557 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2558 if (wip->wi_win == win) 2559 break; 2560 if (wip == NULL) 2561 { 2562 /* allocate a new entry */ 2563 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T)); 2564 if (wip == NULL) 2565 return; 2566 wip->wi_win = win; 2567 if (lnum == 0) /* set lnum even when it's 0 */ 2568 lnum = 1; 2569 } 2570 else 2571 { 2572 /* remove the entry from the list */ 2573 if (wip->wi_prev) 2574 wip->wi_prev->wi_next = wip->wi_next; 2575 else 2576 buf->b_wininfo = wip->wi_next; 2577 if (wip->wi_next) 2578 wip->wi_next->wi_prev = wip->wi_prev; 2579 if (copy_options && wip->wi_optset) 2580 { 2581 clear_winopt(&wip->wi_opt); 2582 #ifdef FEAT_FOLDING 2583 deleteFoldRecurse(&wip->wi_folds); 2584 #endif 2585 } 2586 } 2587 if (lnum != 0) 2588 { 2589 wip->wi_fpos.lnum = lnum; 2590 wip->wi_fpos.col = col; 2591 } 2592 if (copy_options) 2593 { 2594 /* Save the window-specific option values. */ 2595 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt); 2596 #ifdef FEAT_FOLDING 2597 wip->wi_fold_manual = win->w_fold_manual; 2598 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds); 2599 #endif 2600 wip->wi_optset = TRUE; 2601 } 2602 2603 /* insert the entry in front of the list */ 2604 wip->wi_next = buf->b_wininfo; 2605 buf->b_wininfo = wip; 2606 wip->wi_prev = NULL; 2607 if (wip->wi_next) 2608 wip->wi_next->wi_prev = wip; 2609 2610 return; 2611 } 2612 2613 #ifdef FEAT_DIFF 2614 static int wininfo_other_tab_diff __ARGS((wininfo_T *wip)); 2615 2616 /* 2617 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab 2618 * page. That's because a diff is local to a tab page. 2619 */ 2620 static int 2621 wininfo_other_tab_diff(wip) 2622 wininfo_T *wip; 2623 { 2624 win_T *wp; 2625 2626 if (wip->wi_opt.wo_diff) 2627 { 2628 for (wp = firstwin; wp != NULL; wp = wp->w_next) 2629 /* return FALSE when it's a window in the current tab page, thus 2630 * the buffer was in diff mode here */ 2631 if (wip->wi_win == wp) 2632 return FALSE; 2633 return TRUE; 2634 } 2635 return FALSE; 2636 } 2637 #endif 2638 2639 /* 2640 * Find info for the current window in buffer "buf". 2641 * If not found, return the info for the most recently used window. 2642 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in 2643 * another tab page. 2644 * Returns NULL when there isn't any info. 2645 */ 2646 static wininfo_T * 2647 find_wininfo(buf, skip_diff_buffer) 2648 buf_T *buf; 2649 int skip_diff_buffer UNUSED; 2650 { 2651 wininfo_T *wip; 2652 2653 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2654 if (wip->wi_win == curwin 2655 #ifdef FEAT_DIFF 2656 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip)) 2657 #endif 2658 ) 2659 break; 2660 2661 /* If no wininfo for curwin, use the first in the list (that doesn't have 2662 * 'diff' set and is in another tab page). */ 2663 if (wip == NULL) 2664 { 2665 #ifdef FEAT_DIFF 2666 if (skip_diff_buffer) 2667 { 2668 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next) 2669 if (!wininfo_other_tab_diff(wip)) 2670 break; 2671 } 2672 else 2673 #endif 2674 wip = buf->b_wininfo; 2675 } 2676 return wip; 2677 } 2678 2679 /* 2680 * Reset the local window options to the values last used in this window. 2681 * If the buffer wasn't used in this window before, use the values from 2682 * the most recently used window. If the values were never set, use the 2683 * global values for the window. 2684 */ 2685 void 2686 get_winopts(buf) 2687 buf_T *buf; 2688 { 2689 wininfo_T *wip; 2690 2691 clear_winopt(&curwin->w_onebuf_opt); 2692 #ifdef FEAT_FOLDING 2693 clearFolding(curwin); 2694 #endif 2695 2696 wip = find_wininfo(buf, TRUE); 2697 if (wip != NULL && wip->wi_optset) 2698 { 2699 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt); 2700 #ifdef FEAT_FOLDING 2701 curwin->w_fold_manual = wip->wi_fold_manual; 2702 curwin->w_foldinvalid = TRUE; 2703 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds); 2704 #endif 2705 } 2706 else 2707 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt); 2708 2709 #ifdef FEAT_FOLDING 2710 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ 2711 if (p_fdls >= 0) 2712 curwin->w_p_fdl = p_fdls; 2713 #endif 2714 #ifdef FEAT_SYN_HL 2715 check_colorcolumn(curwin); 2716 #endif 2717 } 2718 2719 /* 2720 * Find the position (lnum and col) for the buffer 'buf' for the current 2721 * window. 2722 * Returns a pointer to no_position if no position is found. 2723 */ 2724 pos_T * 2725 buflist_findfpos(buf) 2726 buf_T *buf; 2727 { 2728 wininfo_T *wip; 2729 static pos_T no_position = INIT_POS_T(1, 0, 0); 2730 2731 wip = find_wininfo(buf, FALSE); 2732 if (wip != NULL) 2733 return &(wip->wi_fpos); 2734 else 2735 return &no_position; 2736 } 2737 2738 /* 2739 * Find the lnum for the buffer 'buf' for the current window. 2740 */ 2741 linenr_T 2742 buflist_findlnum(buf) 2743 buf_T *buf; 2744 { 2745 return buflist_findfpos(buf)->lnum; 2746 } 2747 2748 #if defined(FEAT_LISTCMDS) || defined(PROTO) 2749 /* 2750 * List all know file names (for :files and :buffers command). 2751 */ 2752 void 2753 buflist_list(eap) 2754 exarg_T *eap; 2755 { 2756 buf_T *buf; 2757 int len; 2758 int i; 2759 2760 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next) 2761 { 2762 /* skip unlisted buffers, unless ! was used */ 2763 if (!buf->b_p_bl && !eap->forceit) 2764 continue; 2765 msg_putchar('\n'); 2766 if (buf_spname(buf) != NULL) 2767 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1); 2768 else 2769 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE); 2770 2771 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"", 2772 buf->b_fnum, 2773 buf->b_p_bl ? ' ' : 'u', 2774 buf == curbuf ? '%' : 2775 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '), 2776 buf->b_ml.ml_mfp == NULL ? ' ' : 2777 (buf->b_nwindows == 0 ? 'h' : 'a'), 2778 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '), 2779 (buf->b_flags & BF_READERR) ? 'x' 2780 : (bufIsChanged(buf) ? '+' : ' '), 2781 NameBuff); 2782 2783 /* put "line 999" in column 40 or after the file name */ 2784 i = 40 - vim_strsize(IObuff); 2785 do 2786 { 2787 IObuff[len++] = ' '; 2788 } while (--i > 0 && len < IOSIZE - 18); 2789 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len), 2790 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum 2791 : (long)buflist_findlnum(buf)); 2792 msg_outtrans(IObuff); 2793 out_flush(); /* output one line at a time */ 2794 ui_breakcheck(); 2795 } 2796 } 2797 #endif 2798 2799 /* 2800 * Get file name and line number for file 'fnum'. 2801 * Used by DoOneCmd() for translating '%' and '#'. 2802 * Used by insert_reg() and cmdline_paste() for '#' register. 2803 * Return FAIL if not found, OK for success. 2804 */ 2805 int 2806 buflist_name_nr(fnum, fname, lnum) 2807 int fnum; 2808 char_u **fname; 2809 linenr_T *lnum; 2810 { 2811 buf_T *buf; 2812 2813 buf = buflist_findnr(fnum); 2814 if (buf == NULL || buf->b_fname == NULL) 2815 return FAIL; 2816 2817 *fname = buf->b_fname; 2818 *lnum = buflist_findlnum(buf); 2819 2820 return OK; 2821 } 2822 2823 /* 2824 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'. 2825 * The file name with the full path is also remembered, for when :cd is used. 2826 * Returns FAIL for failure (file name already in use by other buffer) 2827 * OK otherwise. 2828 */ 2829 int 2830 setfname(buf, ffname, sfname, message) 2831 buf_T *buf; 2832 char_u *ffname, *sfname; 2833 int message; /* give message when buffer already exists */ 2834 { 2835 buf_T *obuf = NULL; 2836 #ifdef UNIX 2837 struct stat st; 2838 #endif 2839 2840 if (ffname == NULL || *ffname == NUL) 2841 { 2842 /* Removing the name. */ 2843 vim_free(buf->b_ffname); 2844 vim_free(buf->b_sfname); 2845 buf->b_ffname = NULL; 2846 buf->b_sfname = NULL; 2847 #ifdef UNIX 2848 st.st_dev = (dev_T)-1; 2849 #endif 2850 } 2851 else 2852 { 2853 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */ 2854 if (ffname == NULL) /* out of memory */ 2855 return FAIL; 2856 2857 /* 2858 * if the file name is already used in another buffer: 2859 * - if the buffer is loaded, fail 2860 * - if the buffer is not loaded, delete it from the list 2861 */ 2862 #ifdef UNIX 2863 if (mch_stat((char *)ffname, &st) < 0) 2864 st.st_dev = (dev_T)-1; 2865 #endif 2866 if (!(buf->b_flags & BF_DUMMY)) 2867 #ifdef UNIX 2868 obuf = buflist_findname_stat(ffname, &st); 2869 #else 2870 obuf = buflist_findname(ffname); 2871 #endif 2872 if (obuf != NULL && obuf != buf) 2873 { 2874 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */ 2875 { 2876 if (message) 2877 EMSG(_("E95: Buffer with this name already exists")); 2878 vim_free(ffname); 2879 return FAIL; 2880 } 2881 /* delete from the list */ 2882 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE); 2883 } 2884 sfname = vim_strsave(sfname); 2885 if (ffname == NULL || sfname == NULL) 2886 { 2887 vim_free(sfname); 2888 vim_free(ffname); 2889 return FAIL; 2890 } 2891 #ifdef USE_FNAME_CASE 2892 # ifdef USE_LONG_FNAME 2893 if (USE_LONG_FNAME) 2894 # endif 2895 fname_case(sfname, 0); /* set correct case for short file name */ 2896 #endif 2897 vim_free(buf->b_ffname); 2898 vim_free(buf->b_sfname); 2899 buf->b_ffname = ffname; 2900 buf->b_sfname = sfname; 2901 } 2902 buf->b_fname = buf->b_sfname; 2903 #ifdef UNIX 2904 if (st.st_dev == (dev_T)-1) 2905 buf->b_dev_valid = FALSE; 2906 else 2907 { 2908 buf->b_dev_valid = TRUE; 2909 buf->b_dev = st.st_dev; 2910 buf->b_ino = st.st_ino; 2911 } 2912 #endif 2913 2914 #ifndef SHORT_FNAME 2915 buf->b_shortname = FALSE; 2916 #endif 2917 2918 buf_name_changed(buf); 2919 return OK; 2920 } 2921 2922 /* 2923 * Crude way of changing the name of a buffer. Use with care! 2924 * The name should be relative to the current directory. 2925 */ 2926 void 2927 buf_set_name(fnum, name) 2928 int fnum; 2929 char_u *name; 2930 { 2931 buf_T *buf; 2932 2933 buf = buflist_findnr(fnum); 2934 if (buf != NULL) 2935 { 2936 vim_free(buf->b_sfname); 2937 vim_free(buf->b_ffname); 2938 buf->b_ffname = vim_strsave(name); 2939 buf->b_sfname = NULL; 2940 /* Allocate ffname and expand into full path. Also resolves .lnk 2941 * files on Win32. */ 2942 fname_expand(buf, &buf->b_ffname, &buf->b_sfname); 2943 buf->b_fname = buf->b_sfname; 2944 } 2945 } 2946 2947 /* 2948 * Take care of what needs to be done when the name of buffer "buf" has 2949 * changed. 2950 */ 2951 void 2952 buf_name_changed(buf) 2953 buf_T *buf; 2954 { 2955 /* 2956 * If the file name changed, also change the name of the swapfile 2957 */ 2958 if (buf->b_ml.ml_mfp != NULL) 2959 ml_setname(buf); 2960 2961 if (curwin->w_buffer == buf) 2962 check_arg_idx(curwin); /* check file name for arg list */ 2963 #ifdef FEAT_TITLE 2964 maketitle(); /* set window title */ 2965 #endif 2966 #ifdef FEAT_WINDOWS 2967 status_redraw_all(); /* status lines need to be redrawn */ 2968 #endif 2969 fmarks_check_names(buf); /* check named file marks */ 2970 ml_timestamp(buf); /* reset timestamp */ 2971 } 2972 2973 /* 2974 * set alternate file name for current window 2975 * 2976 * Used by do_one_cmd(), do_write() and do_ecmd(). 2977 * Return the buffer. 2978 */ 2979 buf_T * 2980 setaltfname(ffname, sfname, lnum) 2981 char_u *ffname; 2982 char_u *sfname; 2983 linenr_T lnum; 2984 { 2985 buf_T *buf; 2986 2987 /* Create a buffer. 'buflisted' is not set if it's a new buffer */ 2988 buf = buflist_new(ffname, sfname, lnum, 0); 2989 if (buf != NULL && !cmdmod.keepalt) 2990 curwin->w_alt_fnum = buf->b_fnum; 2991 return buf; 2992 } 2993 2994 /* 2995 * Get alternate file name for current window. 2996 * Return NULL if there isn't any, and give error message if requested. 2997 */ 2998 char_u * 2999 getaltfname(errmsg) 3000 int errmsg; /* give error message */ 3001 { 3002 char_u *fname; 3003 linenr_T dummy; 3004 3005 if (buflist_name_nr(0, &fname, &dummy) == FAIL) 3006 { 3007 if (errmsg) 3008 EMSG(_(e_noalt)); 3009 return NULL; 3010 } 3011 return fname; 3012 } 3013 3014 /* 3015 * Add a file name to the buflist and return its number. 3016 * Uses same flags as buflist_new(), except BLN_DUMMY. 3017 * 3018 * used by qf_init(), main() and doarglist() 3019 */ 3020 int 3021 buflist_add(fname, flags) 3022 char_u *fname; 3023 int flags; 3024 { 3025 buf_T *buf; 3026 3027 buf = buflist_new(fname, NULL, (linenr_T)0, flags); 3028 if (buf != NULL) 3029 return buf->b_fnum; 3030 return 0; 3031 } 3032 3033 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) 3034 /* 3035 * Adjust slashes in file names. Called after 'shellslash' was set. 3036 */ 3037 void 3038 buflist_slash_adjust() 3039 { 3040 buf_T *bp; 3041 3042 for (bp = firstbuf; bp != NULL; bp = bp->b_next) 3043 { 3044 if (bp->b_ffname != NULL) 3045 slash_adjust(bp->b_ffname); 3046 if (bp->b_sfname != NULL) 3047 slash_adjust(bp->b_sfname); 3048 } 3049 } 3050 #endif 3051 3052 /* 3053 * Set alternate cursor position for the current buffer and window "win". 3054 * Also save the local window option values. 3055 */ 3056 void 3057 buflist_altfpos(win) 3058 win_T *win; 3059 { 3060 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE); 3061 } 3062 3063 /* 3064 * Return TRUE if 'ffname' is not the same file as current file. 3065 * Fname must have a full path (expanded by mch_FullName()). 3066 */ 3067 int 3068 otherfile(ffname) 3069 char_u *ffname; 3070 { 3071 return otherfile_buf(curbuf, ffname 3072 #ifdef UNIX 3073 , NULL 3074 #endif 3075 ); 3076 } 3077 3078 static int 3079 otherfile_buf(buf, ffname 3080 #ifdef UNIX 3081 , stp 3082 #endif 3083 ) 3084 buf_T *buf; 3085 char_u *ffname; 3086 #ifdef UNIX 3087 struct stat *stp; 3088 #endif 3089 { 3090 /* no name is different */ 3091 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) 3092 return TRUE; 3093 if (fnamecmp(ffname, buf->b_ffname) == 0) 3094 return FALSE; 3095 #ifdef UNIX 3096 { 3097 struct stat st; 3098 3099 /* If no struct stat given, get it now */ 3100 if (stp == NULL) 3101 { 3102 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0) 3103 st.st_dev = (dev_T)-1; 3104 stp = &st; 3105 } 3106 /* Use dev/ino to check if the files are the same, even when the names 3107 * are different (possible with links). Still need to compare the 3108 * name above, for when the file doesn't exist yet. 3109 * Problem: The dev/ino changes when a file is deleted (and created 3110 * again) and remains the same when renamed/moved. We don't want to 3111 * mch_stat() each buffer each time, that would be too slow. Get the 3112 * dev/ino again when they appear to match, but not when they appear 3113 * to be different: Could skip a buffer when it's actually the same 3114 * file. */ 3115 if (buf_same_ino(buf, stp)) 3116 { 3117 buf_setino(buf); 3118 if (buf_same_ino(buf, stp)) 3119 return FALSE; 3120 } 3121 } 3122 #endif 3123 return TRUE; 3124 } 3125 3126 #if defined(UNIX) || defined(PROTO) 3127 /* 3128 * Set inode and device number for a buffer. 3129 * Must always be called when b_fname is changed!. 3130 */ 3131 void 3132 buf_setino(buf) 3133 buf_T *buf; 3134 { 3135 struct stat st; 3136 3137 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0) 3138 { 3139 buf->b_dev_valid = TRUE; 3140 buf->b_dev = st.st_dev; 3141 buf->b_ino = st.st_ino; 3142 } 3143 else 3144 buf->b_dev_valid = FALSE; 3145 } 3146 3147 /* 3148 * Return TRUE if dev/ino in buffer "buf" matches with "stp". 3149 */ 3150 static int 3151 buf_same_ino(buf, stp) 3152 buf_T *buf; 3153 struct stat *stp; 3154 { 3155 return (buf->b_dev_valid 3156 && stp->st_dev == buf->b_dev 3157 && stp->st_ino == buf->b_ino); 3158 } 3159 #endif 3160 3161 /* 3162 * Print info about the current buffer. 3163 */ 3164 void 3165 fileinfo(fullname, shorthelp, dont_truncate) 3166 int fullname; /* when non-zero print full path */ 3167 int shorthelp; 3168 int dont_truncate; 3169 { 3170 char_u *name; 3171 int n; 3172 char_u *p; 3173 char_u *buffer; 3174 size_t len; 3175 3176 buffer = alloc(IOSIZE); 3177 if (buffer == NULL) 3178 return; 3179 3180 if (fullname > 1) /* 2 CTRL-G: include buffer number */ 3181 { 3182 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum); 3183 p = buffer + STRLEN(buffer); 3184 } 3185 else 3186 p = buffer; 3187 3188 *p++ = '"'; 3189 if (buf_spname(curbuf) != NULL) 3190 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1); 3191 else 3192 { 3193 if (!fullname && curbuf->b_fname != NULL) 3194 name = curbuf->b_fname; 3195 else 3196 name = curbuf->b_ffname; 3197 home_replace(shorthelp ? curbuf : NULL, name, p, 3198 (int)(IOSIZE - (p - buffer)), TRUE); 3199 } 3200 3201 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s", 3202 curbufIsChanged() ? (shortmess(SHM_MOD) 3203 ? " [+]" : _(" [Modified]")) : " ", 3204 (curbuf->b_flags & BF_NOTEDITED) 3205 #ifdef FEAT_QUICKFIX 3206 && !bt_dontwrite(curbuf) 3207 #endif 3208 ? _("[Not edited]") : "", 3209 (curbuf->b_flags & BF_NEW) 3210 #ifdef FEAT_QUICKFIX 3211 && !bt_dontwrite(curbuf) 3212 #endif 3213 ? _("[New file]") : "", 3214 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", 3215 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]") 3216 : _("[readonly]")) : "", 3217 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) 3218 || curbuf->b_p_ro) ? 3219 " " : ""); 3220 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100 3221 * causes an overflow, thus for large numbers divide instead. */ 3222 if (curwin->w_cursor.lnum > 1000000L) 3223 n = (int)(((long)curwin->w_cursor.lnum) / 3224 ((long)curbuf->b_ml.ml_line_count / 100L)); 3225 else 3226 n = (int)(((long)curwin->w_cursor.lnum * 100L) / 3227 (long)curbuf->b_ml.ml_line_count); 3228 if (curbuf->b_ml.ml_flags & ML_EMPTY) 3229 { 3230 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg)); 3231 } 3232 #ifdef FEAT_CMDL_INFO 3233 else if (p_ru) 3234 { 3235 /* Current line and column are already on the screen -- webb */ 3236 if (curbuf->b_ml.ml_line_count == 1) 3237 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n); 3238 else 3239 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"), 3240 (long)curbuf->b_ml.ml_line_count, n); 3241 } 3242 #endif 3243 else 3244 { 3245 vim_snprintf_add((char *)buffer, IOSIZE, 3246 _("line %ld of %ld --%d%%-- col "), 3247 (long)curwin->w_cursor.lnum, 3248 (long)curbuf->b_ml.ml_line_count, 3249 n); 3250 validate_virtcol(); 3251 len = STRLEN(buffer); 3252 col_print(buffer + len, IOSIZE - len, 3253 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1); 3254 } 3255 3256 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE)); 3257 3258 if (dont_truncate) 3259 { 3260 /* Temporarily set msg_scroll to avoid the message being truncated. 3261 * First call msg_start() to get the message in the right place. */ 3262 msg_start(); 3263 n = msg_scroll; 3264 msg_scroll = TRUE; 3265 msg(buffer); 3266 msg_scroll = n; 3267 } 3268 else 3269 { 3270 p = msg_trunc_attr(buffer, FALSE, 0); 3271 if (restart_edit != 0 || (msg_scrolled && !need_wait_return)) 3272 /* Need to repeat the message after redrawing when: 3273 * - When restart_edit is set (otherwise there will be a delay 3274 * before redrawing). 3275 * - When the screen was scrolled but there is no wait-return 3276 * prompt. */ 3277 set_keep_msg(p, 0); 3278 } 3279 3280 vim_free(buffer); 3281 } 3282 3283 void 3284 col_print(buf, buflen, col, vcol) 3285 char_u *buf; 3286 size_t buflen; 3287 int col; 3288 int vcol; 3289 { 3290 if (col == vcol) 3291 vim_snprintf((char *)buf, buflen, "%d", col); 3292 else 3293 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol); 3294 } 3295 3296 #if defined(FEAT_TITLE) || defined(PROTO) 3297 /* 3298 * put file name in title bar of window and in icon title 3299 */ 3300 3301 static char_u *lasttitle = NULL; 3302 static char_u *lasticon = NULL; 3303 3304 void 3305 maketitle() 3306 { 3307 char_u *p; 3308 char_u *t_str = NULL; 3309 char_u *i_name; 3310 char_u *i_str = NULL; 3311 int maxlen = 0; 3312 int len; 3313 int mustset; 3314 char_u buf[IOSIZE]; 3315 int off; 3316 3317 if (!redrawing()) 3318 { 3319 /* Postpone updating the title when 'lazyredraw' is set. */ 3320 need_maketitle = TRUE; 3321 return; 3322 } 3323 3324 need_maketitle = FALSE; 3325 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL) 3326 return; 3327 3328 if (p_title) 3329 { 3330 if (p_titlelen > 0) 3331 { 3332 maxlen = p_titlelen * Columns / 100; 3333 if (maxlen < 10) 3334 maxlen = 10; 3335 } 3336 3337 t_str = buf; 3338 if (*p_titlestring != NUL) 3339 { 3340 #ifdef FEAT_STL_OPT 3341 if (stl_syntax & STL_IN_TITLE) 3342 { 3343 int use_sandbox = FALSE; 3344 int save_called_emsg = called_emsg; 3345 3346 # ifdef FEAT_EVAL 3347 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0); 3348 # endif 3349 called_emsg = FALSE; 3350 build_stl_str_hl(curwin, t_str, sizeof(buf), 3351 p_titlestring, use_sandbox, 3352 0, maxlen, NULL, NULL); 3353 if (called_emsg) 3354 set_string_option_direct((char_u *)"titlestring", -1, 3355 (char_u *)"", OPT_FREE, SID_ERROR); 3356 called_emsg |= save_called_emsg; 3357 } 3358 else 3359 #endif 3360 t_str = p_titlestring; 3361 } 3362 else 3363 { 3364 /* format: "fname + (path) (1 of 2) - VIM" */ 3365 3366 #define SPACE_FOR_FNAME (IOSIZE - 100) 3367 #define SPACE_FOR_DIR (IOSIZE - 20) 3368 #define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */ 3369 if (curbuf->b_fname == NULL) 3370 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME); 3371 else 3372 { 3373 p = transstr(gettail(curbuf->b_fname)); 3374 vim_strncpy(buf, p, SPACE_FOR_FNAME); 3375 vim_free(p); 3376 } 3377 3378 switch (bufIsChanged(curbuf) 3379 + (curbuf->b_p_ro * 2) 3380 + (!curbuf->b_p_ma * 4)) 3381 { 3382 case 1: STRCAT(buf, " +"); break; 3383 case 2: STRCAT(buf, " ="); break; 3384 case 3: STRCAT(buf, " =+"); break; 3385 case 4: 3386 case 6: STRCAT(buf, " -"); break; 3387 case 5: 3388 case 7: STRCAT(buf, " -+"); break; 3389 } 3390 3391 if (curbuf->b_fname != NULL) 3392 { 3393 /* Get path of file, replace home dir with ~ */ 3394 off = (int)STRLEN(buf); 3395 buf[off++] = ' '; 3396 buf[off++] = '('; 3397 home_replace(curbuf, curbuf->b_ffname, 3398 buf + off, SPACE_FOR_DIR - off, TRUE); 3399 #ifdef BACKSLASH_IN_FILENAME 3400 /* avoid "c:/name" to be reduced to "c" */ 3401 if (isalpha(buf[off]) && buf[off + 1] == ':') 3402 off += 2; 3403 #endif 3404 /* remove the file name */ 3405 p = gettail_sep(buf + off); 3406 if (p == buf + off) 3407 /* must be a help buffer */ 3408 vim_strncpy(buf + off, (char_u *)_("help"), 3409 (size_t)(SPACE_FOR_DIR - off - 1)); 3410 else 3411 *p = NUL; 3412 3413 /* Translate unprintable chars and concatenate. Keep some 3414 * room for the server name. When there is no room (very long 3415 * file name) use (...). */ 3416 if (off < SPACE_FOR_DIR) 3417 { 3418 p = transstr(buf + off); 3419 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off)); 3420 vim_free(p); 3421 } 3422 else 3423 { 3424 vim_strncpy(buf + off, (char_u *)"...", 3425 (size_t)(SPACE_FOR_ARGNR - off)); 3426 } 3427 STRCAT(buf, ")"); 3428 } 3429 3430 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE); 3431 3432 #if defined(FEAT_CLIENTSERVER) 3433 if (serverName != NULL) 3434 { 3435 STRCAT(buf, " - "); 3436 vim_strcat(buf, serverName, IOSIZE); 3437 } 3438 else 3439 #endif 3440 STRCAT(buf, " - VIM"); 3441 3442 if (maxlen > 0) 3443 { 3444 /* make it shorter by removing a bit in the middle */ 3445 if (vim_strsize(buf) > maxlen) 3446 trunc_string(buf, buf, maxlen, IOSIZE); 3447 } 3448 } 3449 } 3450 mustset = ti_change(t_str, &lasttitle); 3451 3452 if (p_icon) 3453 { 3454 i_str = buf; 3455 if (*p_iconstring != NUL) 3456 { 3457 #ifdef FEAT_STL_OPT 3458 if (stl_syntax & STL_IN_ICON) 3459 { 3460 int use_sandbox = FALSE; 3461 int save_called_emsg = called_emsg; 3462 3463 # ifdef FEAT_EVAL 3464 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0); 3465 # endif 3466 called_emsg = FALSE; 3467 build_stl_str_hl(curwin, i_str, sizeof(buf), 3468 p_iconstring, use_sandbox, 3469 0, 0, NULL, NULL); 3470 if (called_emsg) 3471 set_string_option_direct((char_u *)"iconstring", -1, 3472 (char_u *)"", OPT_FREE, SID_ERROR); 3473 called_emsg |= save_called_emsg; 3474 } 3475 else 3476 #endif 3477 i_str = p_iconstring; 3478 } 3479 else 3480 { 3481 if (buf_spname(curbuf) != NULL) 3482 i_name = buf_spname(curbuf); 3483 else /* use file name only in icon */ 3484 i_name = gettail(curbuf->b_ffname); 3485 *i_str = NUL; 3486 /* Truncate name at 100 bytes. */ 3487 len = (int)STRLEN(i_name); 3488 if (len > 100) 3489 { 3490 len -= 100; 3491 #ifdef FEAT_MBYTE 3492 if (has_mbyte) 3493 len += (*mb_tail_off)(i_name, i_name + len) + 1; 3494 #endif 3495 i_name += len; 3496 } 3497 STRCPY(i_str, i_name); 3498 trans_characters(i_str, IOSIZE); 3499 } 3500 } 3501 3502 mustset |= ti_change(i_str, &lasticon); 3503 3504 if (mustset) 3505 resettitle(); 3506 } 3507 3508 /* 3509 * Used for title and icon: Check if "str" differs from "*last". Set "*last" 3510 * from "str" if it does. 3511 * Return TRUE when "*last" changed. 3512 */ 3513 static int 3514 ti_change(str, last) 3515 char_u *str; 3516 char_u **last; 3517 { 3518 if ((str == NULL) != (*last == NULL) 3519 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0)) 3520 { 3521 vim_free(*last); 3522 if (str == NULL) 3523 *last = NULL; 3524 else 3525 *last = vim_strsave(str); 3526 return TRUE; 3527 } 3528 return FALSE; 3529 } 3530 3531 /* 3532 * Put current window title back (used after calling a shell) 3533 */ 3534 void 3535 resettitle() 3536 { 3537 mch_settitle(lasttitle, lasticon); 3538 } 3539 3540 # if defined(EXITFREE) || defined(PROTO) 3541 void 3542 free_titles() 3543 { 3544 vim_free(lasttitle); 3545 vim_free(lasticon); 3546 } 3547 # endif 3548 3549 #endif /* FEAT_TITLE */ 3550 3551 #if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO) 3552 /* 3553 * Build a string from the status line items in "fmt". 3554 * Return length of string in screen cells. 3555 * 3556 * Normally works for window "wp", except when working for 'tabline' then it 3557 * is "curwin". 3558 * 3559 * Items are drawn interspersed with the text that surrounds it 3560 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation 3561 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional 3562 * 3563 * If maxwidth is not zero, the string will be filled at any middle marker 3564 * or truncated if too long, fillchar is used for all whitespace. 3565 */ 3566 int 3567 build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar, 3568 maxwidth, hltab, tabtab) 3569 win_T *wp; 3570 char_u *out; /* buffer to write into != NameBuff */ 3571 size_t outlen; /* length of out[] */ 3572 char_u *fmt; 3573 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */ 3574 int fillchar; 3575 int maxwidth; 3576 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */ 3577 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */ 3578 { 3579 char_u *p; 3580 char_u *s; 3581 char_u *t; 3582 int byteval; 3583 #ifdef FEAT_EVAL 3584 win_T *o_curwin; 3585 buf_T *o_curbuf; 3586 #endif 3587 int empty_line; 3588 colnr_T virtcol; 3589 long l; 3590 long n; 3591 int prevchar_isflag; 3592 int prevchar_isitem; 3593 int itemisflag; 3594 int fillable; 3595 char_u *str; 3596 long num; 3597 int width; 3598 int itemcnt; 3599 int curitem; 3600 int groupitem[STL_MAX_ITEM]; 3601 int groupdepth; 3602 struct stl_item 3603 { 3604 char_u *start; 3605 int minwid; 3606 int maxwid; 3607 enum 3608 { 3609 Normal, 3610 Empty, 3611 Group, 3612 Middle, 3613 Highlight, 3614 TabPage, 3615 Trunc 3616 } type; 3617 } item[STL_MAX_ITEM]; 3618 int minwid; 3619 int maxwid; 3620 int zeropad; 3621 char_u base; 3622 char_u opt; 3623 #define TMPLEN 70 3624 char_u tmp[TMPLEN]; 3625 char_u *usefmt = fmt; 3626 struct stl_hlrec *sp; 3627 3628 #ifdef FEAT_EVAL 3629 /* 3630 * When the format starts with "%!" then evaluate it as an expression and 3631 * use the result as the actual format string. 3632 */ 3633 if (fmt[0] == '%' && fmt[1] == '!') 3634 { 3635 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox); 3636 if (usefmt == NULL) 3637 usefmt = fmt; 3638 } 3639 #endif 3640 3641 if (fillchar == 0) 3642 fillchar = ' '; 3643 #ifdef FEAT_MBYTE 3644 /* Can't handle a multi-byte fill character yet. */ 3645 else if (mb_char2len(fillchar) > 1) 3646 fillchar = '-'; 3647 #endif 3648 3649 /* Get line & check if empty (cursorpos will show "0-1"). Note that 3650 * p will become invalid when getting another buffer line. */ 3651 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE); 3652 empty_line = (*p == NUL); 3653 3654 /* Get the byte value now, in case we need it below. This is more 3655 * efficient than making a copy of the line. */ 3656 if (wp->w_cursor.col > (colnr_T)STRLEN(p)) 3657 byteval = 0; 3658 else 3659 #ifdef FEAT_MBYTE 3660 byteval = (*mb_ptr2char)(p + wp->w_cursor.col); 3661 #else 3662 byteval = p[wp->w_cursor.col]; 3663 #endif 3664 3665 groupdepth = 0; 3666 p = out; 3667 curitem = 0; 3668 prevchar_isflag = TRUE; 3669 prevchar_isitem = FALSE; 3670 for (s = usefmt; *s; ) 3671 { 3672 if (curitem == STL_MAX_ITEM) 3673 { 3674 /* There are too many items. Add the error code to the statusline 3675 * to give the user a hint about what went wrong. */ 3676 if (p + 6 < out + outlen) 3677 { 3678 mch_memmove(p, " E541", (size_t)5); 3679 p += 5; 3680 } 3681 break; 3682 } 3683 3684 if (*s != NUL && *s != '%') 3685 prevchar_isflag = prevchar_isitem = FALSE; 3686 3687 /* 3688 * Handle up to the next '%' or the end. 3689 */ 3690 while (*s != NUL && *s != '%' && p + 1 < out + outlen) 3691 *p++ = *s++; 3692 if (*s == NUL || p + 1 >= out + outlen) 3693 break; 3694 3695 /* 3696 * Handle one '%' item. 3697 */ 3698 s++; 3699 if (*s == NUL) /* ignore trailing % */ 3700 break; 3701 if (*s == '%') 3702 { 3703 if (p + 1 >= out + outlen) 3704 break; 3705 *p++ = *s++; 3706 prevchar_isflag = prevchar_isitem = FALSE; 3707 continue; 3708 } 3709 if (*s == STL_MIDDLEMARK) 3710 { 3711 s++; 3712 if (groupdepth > 0) 3713 continue; 3714 item[curitem].type = Middle; 3715 item[curitem++].start = p; 3716 continue; 3717 } 3718 if (*s == STL_TRUNCMARK) 3719 { 3720 s++; 3721 item[curitem].type = Trunc; 3722 item[curitem++].start = p; 3723 continue; 3724 } 3725 if (*s == ')') 3726 { 3727 s++; 3728 if (groupdepth < 1) 3729 continue; 3730 groupdepth--; 3731 3732 t = item[groupitem[groupdepth]].start; 3733 *p = NUL; 3734 l = vim_strsize(t); 3735 if (curitem > groupitem[groupdepth] + 1 3736 && item[groupitem[groupdepth]].minwid == 0) 3737 { 3738 /* remove group if all items are empty */ 3739 for (n = groupitem[groupdepth] + 1; n < curitem; n++) 3740 if (item[n].type == Normal) 3741 break; 3742 if (n == curitem) 3743 { 3744 p = t; 3745 l = 0; 3746 } 3747 } 3748 if (l > item[groupitem[groupdepth]].maxwid) 3749 { 3750 /* truncate, remove n bytes of text at the start */ 3751 #ifdef FEAT_MBYTE 3752 if (has_mbyte) 3753 { 3754 /* Find the first character that should be included. */ 3755 n = 0; 3756 while (l >= item[groupitem[groupdepth]].maxwid) 3757 { 3758 l -= ptr2cells(t + n); 3759 n += (*mb_ptr2len)(t + n); 3760 } 3761 } 3762 else 3763 #endif 3764 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1; 3765 3766 *t = '<'; 3767 mch_memmove(t + 1, t + n, (size_t)(p - (t + n))); 3768 p = p - n + 1; 3769 #ifdef FEAT_MBYTE 3770 /* Fill up space left over by half a double-wide char. */ 3771 while (++l < item[groupitem[groupdepth]].minwid) 3772 *p++ = fillchar; 3773 #endif 3774 3775 /* correct the start of the items for the truncation */ 3776 for (l = groupitem[groupdepth] + 1; l < curitem; l++) 3777 { 3778 item[l].start -= n; 3779 if (item[l].start < t) 3780 item[l].start = t; 3781 } 3782 } 3783 else if (abs(item[groupitem[groupdepth]].minwid) > l) 3784 { 3785 /* fill */ 3786 n = item[groupitem[groupdepth]].minwid; 3787 if (n < 0) 3788 { 3789 /* fill by appending characters */ 3790 n = 0 - n; 3791 while (l++ < n && p + 1 < out + outlen) 3792 *p++ = fillchar; 3793 } 3794 else 3795 { 3796 /* fill by inserting characters */ 3797 mch_memmove(t + n - l, t, (size_t)(p - t)); 3798 l = n - l; 3799 if (p + l >= out + outlen) 3800 l = (long)((out + outlen) - p - 1); 3801 p += l; 3802 for (n = groupitem[groupdepth] + 1; n < curitem; n++) 3803 item[n].start += l; 3804 for ( ; l > 0; l--) 3805 *t++ = fillchar; 3806 } 3807 } 3808 continue; 3809 } 3810 minwid = 0; 3811 maxwid = 9999; 3812 zeropad = FALSE; 3813 l = 1; 3814 if (*s == '0') 3815 { 3816 s++; 3817 zeropad = TRUE; 3818 } 3819 if (*s == '-') 3820 { 3821 s++; 3822 l = -1; 3823 } 3824 if (VIM_ISDIGIT(*s)) 3825 { 3826 minwid = (int)getdigits(&s); 3827 if (minwid < 0) /* overflow */ 3828 minwid = 0; 3829 } 3830 if (*s == STL_USER_HL) 3831 { 3832 item[curitem].type = Highlight; 3833 item[curitem].start = p; 3834 item[curitem].minwid = minwid > 9 ? 1 : minwid; 3835 s++; 3836 curitem++; 3837 continue; 3838 } 3839 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR) 3840 { 3841 if (*s == STL_TABCLOSENR) 3842 { 3843 if (minwid == 0) 3844 { 3845 /* %X ends the close label, go back to the previously 3846 * define tab label nr. */ 3847 for (n = curitem - 1; n >= 0; --n) 3848 if (item[n].type == TabPage && item[n].minwid >= 0) 3849 { 3850 minwid = item[n].minwid; 3851 break; 3852 } 3853 } 3854 else 3855 /* close nrs are stored as negative values */ 3856 minwid = - minwid; 3857 } 3858 item[curitem].type = TabPage; 3859 item[curitem].start = p; 3860 item[curitem].minwid = minwid; 3861 s++; 3862 curitem++; 3863 continue; 3864 } 3865 if (*s == '.') 3866 { 3867 s++; 3868 if (VIM_ISDIGIT(*s)) 3869 { 3870 maxwid = (int)getdigits(&s); 3871 if (maxwid <= 0) /* overflow */ 3872 maxwid = 50; 3873 } 3874 } 3875 minwid = (minwid > 50 ? 50 : minwid) * l; 3876 if (*s == '(') 3877 { 3878 groupitem[groupdepth++] = curitem; 3879 item[curitem].type = Group; 3880 item[curitem].start = p; 3881 item[curitem].minwid = minwid; 3882 item[curitem].maxwid = maxwid; 3883 s++; 3884 curitem++; 3885 continue; 3886 } 3887 if (vim_strchr(STL_ALL, *s) == NULL) 3888 { 3889 s++; 3890 continue; 3891 } 3892 opt = *s++; 3893 3894 /* OK - now for the real work */ 3895 base = 'D'; 3896 itemisflag = FALSE; 3897 fillable = TRUE; 3898 num = -1; 3899 str = NULL; 3900 switch (opt) 3901 { 3902 case STL_FILEPATH: 3903 case STL_FULLPATH: 3904 case STL_FILENAME: 3905 fillable = FALSE; /* don't change ' ' to fillchar */ 3906 if (buf_spname(wp->w_buffer) != NULL) 3907 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1); 3908 else 3909 { 3910 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname 3911 : wp->w_buffer->b_fname; 3912 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE); 3913 } 3914 trans_characters(NameBuff, MAXPATHL); 3915 if (opt != STL_FILENAME) 3916 str = NameBuff; 3917 else 3918 str = gettail(NameBuff); 3919 break; 3920 3921 case STL_VIM_EXPR: /* '{' */ 3922 itemisflag = TRUE; 3923 t = p; 3924 while (*s != '}' && *s != NUL && p + 1 < out + outlen) 3925 *p++ = *s++; 3926 if (*s != '}') /* missing '}' or out of space */ 3927 break; 3928 s++; 3929 *p = 0; 3930 p = t; 3931 3932 #ifdef FEAT_EVAL 3933 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum); 3934 set_internal_string_var((char_u *)"actual_curbuf", tmp); 3935 3936 o_curbuf = curbuf; 3937 o_curwin = curwin; 3938 curwin = wp; 3939 curbuf = wp->w_buffer; 3940 3941 str = eval_to_string_safe(p, &t, use_sandbox); 3942 3943 curwin = o_curwin; 3944 curbuf = o_curbuf; 3945 do_unlet((char_u *)"g:actual_curbuf", TRUE); 3946 3947 if (str != NULL && *str != 0) 3948 { 3949 if (*skipdigits(str) == NUL) 3950 { 3951 num = atoi((char *)str); 3952 vim_free(str); 3953 str = NULL; 3954 itemisflag = FALSE; 3955 } 3956 } 3957 #endif 3958 break; 3959 3960 case STL_LINE: 3961 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) 3962 ? 0L : (long)(wp->w_cursor.lnum); 3963 break; 3964 3965 case STL_NUMLINES: 3966 num = wp->w_buffer->b_ml.ml_line_count; 3967 break; 3968 3969 case STL_COLUMN: 3970 num = !(State & INSERT) && empty_line 3971 ? 0 : (int)wp->w_cursor.col + 1; 3972 break; 3973 3974 case STL_VIRTCOL: 3975 case STL_VIRTCOL_ALT: 3976 /* In list mode virtcol needs to be recomputed */ 3977 virtcol = wp->w_virtcol; 3978 if (wp->w_p_list && lcs_tab1 == NUL) 3979 { 3980 wp->w_p_list = FALSE; 3981 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL); 3982 wp->w_p_list = TRUE; 3983 } 3984 ++virtcol; 3985 /* Don't display %V if it's the same as %c. */ 3986 if (opt == STL_VIRTCOL_ALT 3987 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line 3988 ? 0 : (int)wp->w_cursor.col + 1))) 3989 break; 3990 num = (long)virtcol; 3991 break; 3992 3993 case STL_PERCENTAGE: 3994 num = (int)(((long)wp->w_cursor.lnum * 100L) / 3995 (long)wp->w_buffer->b_ml.ml_line_count); 3996 break; 3997 3998 case STL_ALTPERCENT: 3999 str = tmp; 4000 get_rel_pos(wp, str, TMPLEN); 4001 break; 4002 4003 case STL_ARGLISTSTAT: 4004 fillable = FALSE; 4005 tmp[0] = 0; 4006 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE)) 4007 str = tmp; 4008 break; 4009 4010 case STL_KEYMAP: 4011 fillable = FALSE; 4012 if (get_keymap_str(wp, tmp, TMPLEN)) 4013 str = tmp; 4014 break; 4015 case STL_PAGENUM: 4016 #if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE) 4017 num = printer_page_num; 4018 #else 4019 num = 0; 4020 #endif 4021 break; 4022 4023 case STL_BUFNO: 4024 num = wp->w_buffer->b_fnum; 4025 break; 4026 4027 case STL_OFFSET_X: 4028 base = 'X'; 4029 case STL_OFFSET: 4030 #ifdef FEAT_BYTEOFF 4031 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); 4032 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? 4033 0L : l + 1 + (!(State & INSERT) && empty_line ? 4034 0 : (int)wp->w_cursor.col); 4035 #endif 4036 break; 4037 4038 case STL_BYTEVAL_X: 4039 base = 'X'; 4040 case STL_BYTEVAL: 4041 num = byteval; 4042 if (num == NL) 4043 num = 0; 4044 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC) 4045 num = NL; 4046 break; 4047 4048 case STL_ROFLAG: 4049 case STL_ROFLAG_ALT: 4050 itemisflag = TRUE; 4051 if (wp->w_buffer->b_p_ro) 4052 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]")); 4053 break; 4054 4055 case STL_HELPFLAG: 4056 case STL_HELPFLAG_ALT: 4057 itemisflag = TRUE; 4058 if (wp->w_buffer->b_help) 4059 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP" 4060 : _("[Help]")); 4061 break; 4062 4063 #ifdef FEAT_AUTOCMD 4064 case STL_FILETYPE: 4065 if (*wp->w_buffer->b_p_ft != NUL 4066 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3) 4067 { 4068 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]", 4069 wp->w_buffer->b_p_ft); 4070 str = tmp; 4071 } 4072 break; 4073 4074 case STL_FILETYPE_ALT: 4075 itemisflag = TRUE; 4076 if (*wp->w_buffer->b_p_ft != NUL 4077 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2) 4078 { 4079 vim_snprintf((char *)tmp, sizeof(tmp), ",%s", 4080 wp->w_buffer->b_p_ft); 4081 for (t = tmp; *t != 0; t++) 4082 *t = TOUPPER_LOC(*t); 4083 str = tmp; 4084 } 4085 break; 4086 #endif 4087 4088 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 4089 case STL_PREVIEWFLAG: 4090 case STL_PREVIEWFLAG_ALT: 4091 itemisflag = TRUE; 4092 if (wp->w_p_pvw) 4093 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV" 4094 : _("[Preview]")); 4095 break; 4096 4097 case STL_QUICKFIX: 4098 if (bt_quickfix(wp->w_buffer)) 4099 str = (char_u *)(wp->w_llist_ref 4100 ? _(msg_loclist) 4101 : _(msg_qflist)); 4102 break; 4103 #endif 4104 4105 case STL_MODIFIED: 4106 case STL_MODIFIED_ALT: 4107 itemisflag = TRUE; 4108 switch ((opt == STL_MODIFIED_ALT) 4109 + bufIsChanged(wp->w_buffer) * 2 4110 + (!wp->w_buffer->b_p_ma) * 4) 4111 { 4112 case 2: str = (char_u *)"[+]"; break; 4113 case 3: str = (char_u *)",+"; break; 4114 case 4: str = (char_u *)"[-]"; break; 4115 case 5: str = (char_u *)",-"; break; 4116 case 6: str = (char_u *)"[+-]"; break; 4117 case 7: str = (char_u *)",+-"; break; 4118 } 4119 break; 4120 4121 case STL_HIGHLIGHT: 4122 t = s; 4123 while (*s != '#' && *s != NUL) 4124 ++s; 4125 if (*s == '#') 4126 { 4127 item[curitem].type = Highlight; 4128 item[curitem].start = p; 4129 item[curitem].minwid = -syn_namen2id(t, (int)(s - t)); 4130 curitem++; 4131 } 4132 if (*s != NUL) 4133 ++s; 4134 continue; 4135 } 4136 4137 item[curitem].start = p; 4138 item[curitem].type = Normal; 4139 if (str != NULL && *str) 4140 { 4141 t = str; 4142 if (itemisflag) 4143 { 4144 if ((t[0] && t[1]) 4145 && ((!prevchar_isitem && *t == ',') 4146 || (prevchar_isflag && *t == ' '))) 4147 t++; 4148 prevchar_isflag = TRUE; 4149 } 4150 l = vim_strsize(t); 4151 if (l > 0) 4152 prevchar_isitem = TRUE; 4153 if (l > maxwid) 4154 { 4155 while (l >= maxwid) 4156 #ifdef FEAT_MBYTE 4157 if (has_mbyte) 4158 { 4159 l -= ptr2cells(t); 4160 t += (*mb_ptr2len)(t); 4161 } 4162 else 4163 #endif 4164 l -= byte2cells(*t++); 4165 if (p + 1 >= out + outlen) 4166 break; 4167 *p++ = '<'; 4168 } 4169 if (minwid > 0) 4170 { 4171 for (; l < minwid && p + 1 < out + outlen; l++) 4172 { 4173 /* Don't put a "-" in front of a digit. */ 4174 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t)) 4175 *p++ = ' '; 4176 else 4177 *p++ = fillchar; 4178 } 4179 minwid = 0; 4180 } 4181 else 4182 minwid *= -1; 4183 while (*t && p + 1 < out + outlen) 4184 { 4185 *p++ = *t++; 4186 /* Change a space by fillchar, unless fillchar is '-' and a 4187 * digit follows. */ 4188 if (fillable && p[-1] == ' ' 4189 && (!VIM_ISDIGIT(*t) || fillchar != '-')) 4190 p[-1] = fillchar; 4191 } 4192 for (; l < minwid && p + 1 < out + outlen; l++) 4193 *p++ = fillchar; 4194 } 4195 else if (num >= 0) 4196 { 4197 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16)); 4198 char_u nstr[20]; 4199 4200 if (p + 20 >= out + outlen) 4201 break; /* not sufficient space */ 4202 prevchar_isitem = TRUE; 4203 t = nstr; 4204 if (opt == STL_VIRTCOL_ALT) 4205 { 4206 *t++ = '-'; 4207 minwid--; 4208 } 4209 *t++ = '%'; 4210 if (zeropad) 4211 *t++ = '0'; 4212 *t++ = '*'; 4213 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd'); 4214 *t = 0; 4215 4216 for (n = num, l = 1; n >= nbase; n /= nbase) 4217 l++; 4218 if (opt == STL_VIRTCOL_ALT) 4219 l++; 4220 if (l > maxwid) 4221 { 4222 l += 2; 4223 n = l - maxwid; 4224 while (l-- > maxwid) 4225 num /= nbase; 4226 *t++ = '>'; 4227 *t++ = '%'; 4228 *t = t[-3]; 4229 *++t = 0; 4230 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, 4231 0, num, n); 4232 } 4233 else 4234 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr, 4235 minwid, num); 4236 p += STRLEN(p); 4237 } 4238 else 4239 item[curitem].type = Empty; 4240 4241 if (opt == STL_VIM_EXPR) 4242 vim_free(str); 4243 4244 if (num >= 0 || (!itemisflag && str && *str)) 4245 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */ 4246 curitem++; 4247 } 4248 *p = NUL; 4249 itemcnt = curitem; 4250 4251 #ifdef FEAT_EVAL 4252 if (usefmt != fmt) 4253 vim_free(usefmt); 4254 #endif 4255 4256 width = vim_strsize(out); 4257 if (maxwidth > 0 && width > maxwidth) 4258 { 4259 /* Result is too long, must truncate somewhere. */ 4260 l = 0; 4261 if (itemcnt == 0) 4262 s = out; 4263 else 4264 { 4265 for ( ; l < itemcnt; l++) 4266 if (item[l].type == Trunc) 4267 { 4268 /* Truncate at %< item. */ 4269 s = item[l].start; 4270 break; 4271 } 4272 if (l == itemcnt) 4273 { 4274 /* No %< item, truncate first item. */ 4275 s = item[0].start; 4276 l = 0; 4277 } 4278 } 4279 4280 if (width - vim_strsize(s) >= maxwidth) 4281 { 4282 /* Truncation mark is beyond max length */ 4283 #ifdef FEAT_MBYTE 4284 if (has_mbyte) 4285 { 4286 s = out; 4287 width = 0; 4288 for (;;) 4289 { 4290 width += ptr2cells(s); 4291 if (width >= maxwidth) 4292 break; 4293 s += (*mb_ptr2len)(s); 4294 } 4295 /* Fill up for half a double-wide character. */ 4296 while (++width < maxwidth) 4297 *s++ = fillchar; 4298 } 4299 else 4300 #endif 4301 s = out + maxwidth - 1; 4302 for (l = 0; l < itemcnt; l++) 4303 if (item[l].start > s) 4304 break; 4305 itemcnt = l; 4306 *s++ = '>'; 4307 *s = 0; 4308 } 4309 else 4310 { 4311 #ifdef FEAT_MBYTE 4312 if (has_mbyte) 4313 { 4314 n = 0; 4315 while (width >= maxwidth) 4316 { 4317 width -= ptr2cells(s + n); 4318 n += (*mb_ptr2len)(s + n); 4319 } 4320 } 4321 else 4322 #endif 4323 n = width - maxwidth + 1; 4324 p = s + n; 4325 STRMOVE(s + 1, p); 4326 *s = '<'; 4327 4328 /* Fill up for half a double-wide character. */ 4329 while (++width < maxwidth) 4330 { 4331 s = s + STRLEN(s); 4332 *s++ = fillchar; 4333 *s = NUL; 4334 } 4335 4336 --n; /* count the '<' */ 4337 for (; l < itemcnt; l++) 4338 { 4339 if (item[l].start - n >= s) 4340 item[l].start -= n; 4341 else 4342 item[l].start = s; 4343 } 4344 } 4345 width = maxwidth; 4346 } 4347 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen) 4348 { 4349 /* Apply STL_MIDDLE if any */ 4350 for (l = 0; l < itemcnt; l++) 4351 if (item[l].type == Middle) 4352 break; 4353 if (l < itemcnt) 4354 { 4355 p = item[l].start + maxwidth - width; 4356 STRMOVE(p, item[l].start); 4357 for (s = item[l].start; s < p; s++) 4358 *s = fillchar; 4359 for (l++; l < itemcnt; l++) 4360 item[l].start += maxwidth - width; 4361 width = maxwidth; 4362 } 4363 } 4364 4365 /* Store the info about highlighting. */ 4366 if (hltab != NULL) 4367 { 4368 sp = hltab; 4369 for (l = 0; l < itemcnt; l++) 4370 { 4371 if (item[l].type == Highlight) 4372 { 4373 sp->start = item[l].start; 4374 sp->userhl = item[l].minwid; 4375 sp++; 4376 } 4377 } 4378 sp->start = NULL; 4379 sp->userhl = 0; 4380 } 4381 4382 /* Store the info about tab pages labels. */ 4383 if (tabtab != NULL) 4384 { 4385 sp = tabtab; 4386 for (l = 0; l < itemcnt; l++) 4387 { 4388 if (item[l].type == TabPage) 4389 { 4390 sp->start = item[l].start; 4391 sp->userhl = item[l].minwid; 4392 sp++; 4393 } 4394 } 4395 sp->start = NULL; 4396 sp->userhl = 0; 4397 } 4398 4399 return width; 4400 } 4401 #endif /* FEAT_STL_OPT */ 4402 4403 #if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \ 4404 || defined(FEAT_GUI_TABLINE) || defined(PROTO) 4405 /* 4406 * Get relative cursor position in window into "buf[buflen]", in the form 99%, 4407 * using "Top", "Bot" or "All" when appropriate. 4408 */ 4409 void 4410 get_rel_pos(wp, buf, buflen) 4411 win_T *wp; 4412 char_u *buf; 4413 int buflen; 4414 { 4415 long above; /* number of lines above window */ 4416 long below; /* number of lines below window */ 4417 4418 if (buflen < 3) /* need at least 3 chars for writing */ 4419 return; 4420 above = wp->w_topline - 1; 4421 #ifdef FEAT_DIFF 4422 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill; 4423 #endif 4424 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1; 4425 if (below <= 0) 4426 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")), 4427 (size_t)(buflen - 1)); 4428 else if (above <= 0) 4429 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1)); 4430 else 4431 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L 4432 ? (int)(above / ((above + below) / 100L)) 4433 : (int)(above * 100L / (above + below))); 4434 } 4435 #endif 4436 4437 /* 4438 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file. 4439 * Return TRUE if it was appended. 4440 */ 4441 static int 4442 append_arg_number(wp, buf, buflen, add_file) 4443 win_T *wp; 4444 char_u *buf; 4445 int buflen; 4446 int add_file; /* Add "file" before the arg number */ 4447 { 4448 char_u *p; 4449 4450 if (ARGCOUNT <= 1) /* nothing to do */ 4451 return FALSE; 4452 4453 p = buf + STRLEN(buf); /* go to the end of the buffer */ 4454 if (p - buf + 35 >= buflen) /* getting too long */ 4455 return FALSE; 4456 *p++ = ' '; 4457 *p++ = '('; 4458 if (add_file) 4459 { 4460 STRCPY(p, "file "); 4461 p += 5; 4462 } 4463 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), 4464 wp->w_arg_idx_invalid ? "(%d) of %d)" 4465 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT); 4466 return TRUE; 4467 } 4468 4469 /* 4470 * If fname is not a full path, make it a full path. 4471 * Returns pointer to allocated memory (NULL for failure). 4472 */ 4473 char_u * 4474 fix_fname(fname) 4475 char_u *fname; 4476 { 4477 /* 4478 * Force expanding the path always for Unix, because symbolic links may 4479 * mess up the full path name, even though it starts with a '/'. 4480 * Also expand when there is ".." in the file name, try to remove it, 4481 * because "c:/src/../README" is equal to "c:/README". 4482 * Similarly "c:/src//file" is equal to "c:/src/file". 4483 * For MS-Windows also expand names like "longna~1" to "longname". 4484 */ 4485 #ifdef UNIX 4486 return FullName_save(fname, TRUE); 4487 #else 4488 if (!vim_isAbsName(fname) 4489 || strstr((char *)fname, "..") != NULL 4490 || strstr((char *)fname, "//") != NULL 4491 # ifdef BACKSLASH_IN_FILENAME 4492 || strstr((char *)fname, "\\\\") != NULL 4493 # endif 4494 # if defined(MSWIN) || defined(DJGPP) 4495 || vim_strchr(fname, '~') != NULL 4496 # endif 4497 ) 4498 return FullName_save(fname, FALSE); 4499 4500 fname = vim_strsave(fname); 4501 4502 # ifdef USE_FNAME_CASE 4503 # ifdef USE_LONG_FNAME 4504 if (USE_LONG_FNAME) 4505 # endif 4506 { 4507 if (fname != NULL) 4508 fname_case(fname, 0); /* set correct case for file name */ 4509 } 4510 # endif 4511 4512 return fname; 4513 #endif 4514 } 4515 4516 /* 4517 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL. 4518 * "ffname" becomes a pointer to allocated memory (or NULL). 4519 */ 4520 void 4521 fname_expand(buf, ffname, sfname) 4522 buf_T *buf UNUSED; 4523 char_u **ffname; 4524 char_u **sfname; 4525 { 4526 if (*ffname == NULL) /* if no file name given, nothing to do */ 4527 return; 4528 if (*sfname == NULL) /* if no short file name given, use ffname */ 4529 *sfname = *ffname; 4530 *ffname = fix_fname(*ffname); /* expand to full path */ 4531 4532 #ifdef FEAT_SHORTCUT 4533 if (!buf->b_p_bin) 4534 { 4535 char_u *rfname; 4536 4537 /* If the file name is a shortcut file, use the file it links to. */ 4538 rfname = mch_resolve_shortcut(*ffname); 4539 if (rfname != NULL) 4540 { 4541 vim_free(*ffname); 4542 *ffname = rfname; 4543 *sfname = rfname; 4544 } 4545 } 4546 #endif 4547 } 4548 4549 /* 4550 * Get the file name for an argument list entry. 4551 */ 4552 char_u * 4553 alist_name(aep) 4554 aentry_T *aep; 4555 { 4556 buf_T *bp; 4557 4558 /* Use the name from the associated buffer if it exists. */ 4559 bp = buflist_findnr(aep->ae_fnum); 4560 if (bp == NULL || bp->b_fname == NULL) 4561 return aep->ae_fname; 4562 return bp->b_fname; 4563 } 4564 4565 #if defined(FEAT_WINDOWS) || defined(PROTO) 4566 /* 4567 * do_arg_all(): Open up to 'count' windows, one for each argument. 4568 */ 4569 void 4570 do_arg_all(count, forceit, keep_tabs) 4571 int count; 4572 int forceit; /* hide buffers in current windows */ 4573 int keep_tabs; /* keep current tabs, for ":tab drop file" */ 4574 { 4575 int i; 4576 win_T *wp, *wpnext; 4577 char_u *opened; /* Array of weight for which args are open: 4578 * 0: not opened 4579 * 1: opened in other tab 4580 * 2: opened in curtab 4581 * 3: opened in curtab and curwin 4582 */ 4583 int opened_len; /* length of opened[] */ 4584 int use_firstwin = FALSE; /* use first window for arglist */ 4585 int split_ret = OK; 4586 int p_ea_save; 4587 alist_T *alist; /* argument list to be used */ 4588 buf_T *buf; 4589 tabpage_T *tpnext; 4590 int had_tab = cmdmod.tab; 4591 win_T *old_curwin, *last_curwin; 4592 tabpage_T *old_curtab, *last_curtab; 4593 win_T *new_curwin = NULL; 4594 tabpage_T *new_curtab = NULL; 4595 4596 if (ARGCOUNT <= 0) 4597 { 4598 /* Don't give an error message. We don't want it when the ":all" 4599 * command is in the .vimrc. */ 4600 return; 4601 } 4602 setpcmark(); 4603 4604 opened_len = ARGCOUNT; 4605 opened = alloc_clear((unsigned)opened_len); 4606 if (opened == NULL) 4607 return; 4608 4609 /* Autocommands may do anything to the argument list. Make sure it's not 4610 * freed while we are working here by "locking" it. We still have to 4611 * watch out for its size to be changed. */ 4612 alist = curwin->w_alist; 4613 ++alist->al_refcount; 4614 4615 old_curwin = curwin; 4616 old_curtab = curtab; 4617 4618 #ifdef FEAT_GUI 4619 need_mouse_correct = TRUE; 4620 #endif 4621 4622 /* 4623 * Try closing all windows that are not in the argument list. 4624 * Also close windows that are not full width; 4625 * When 'hidden' or "forceit" set the buffer becomes hidden. 4626 * Windows that have a changed buffer and can't be hidden won't be closed. 4627 * When the ":tab" modifier was used do this for all tab pages. 4628 */ 4629 if (had_tab > 0) 4630 goto_tabpage_tp(first_tabpage, TRUE, TRUE); 4631 for (;;) 4632 { 4633 tpnext = curtab->tp_next; 4634 for (wp = firstwin; wp != NULL; wp = wpnext) 4635 { 4636 wpnext = wp->w_next; 4637 buf = wp->w_buffer; 4638 if (buf->b_ffname == NULL 4639 || (!keep_tabs && buf->b_nwindows > 1) 4640 #ifdef FEAT_VERTSPLIT 4641 || wp->w_width != Columns 4642 #endif 4643 ) 4644 i = opened_len; 4645 else 4646 { 4647 /* check if the buffer in this window is in the arglist */ 4648 for (i = 0; i < opened_len; ++i) 4649 { 4650 if (i < alist->al_ga.ga_len 4651 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum 4652 || fullpathcmp(alist_name(&AARGLIST(alist)[i]), 4653 buf->b_ffname, TRUE) & FPC_SAME)) 4654 { 4655 int weight = 1; 4656 4657 if (old_curtab == curtab) 4658 { 4659 ++weight; 4660 if (old_curwin == wp) 4661 ++weight; 4662 } 4663 4664 if (weight > (int)opened[i]) 4665 { 4666 opened[i] = (char_u)weight; 4667 if (i == 0) 4668 { 4669 if (new_curwin != NULL) 4670 new_curwin->w_arg_idx = opened_len; 4671 new_curwin = wp; 4672 new_curtab = curtab; 4673 } 4674 } 4675 else if (keep_tabs) 4676 i = opened_len; 4677 4678 if (wp->w_alist != alist) 4679 { 4680 /* Use the current argument list for all windows 4681 * containing a file from it. */ 4682 alist_unlink(wp->w_alist); 4683 wp->w_alist = alist; 4684 ++wp->w_alist->al_refcount; 4685 } 4686 break; 4687 } 4688 } 4689 } 4690 wp->w_arg_idx = i; 4691 4692 if (i == opened_len && !keep_tabs)/* close this window */ 4693 { 4694 if (P_HID(buf) || forceit || buf->b_nwindows > 1 4695 || !bufIsChanged(buf)) 4696 { 4697 /* If the buffer was changed, and we would like to hide it, 4698 * try autowriting. */ 4699 if (!P_HID(buf) && buf->b_nwindows <= 1 4700 && bufIsChanged(buf)) 4701 { 4702 (void)autowrite(buf, FALSE); 4703 #ifdef FEAT_AUTOCMD 4704 /* check if autocommands removed the window */ 4705 if (!win_valid(wp) || !buf_valid(buf)) 4706 { 4707 wpnext = firstwin; /* start all over... */ 4708 continue; 4709 } 4710 #endif 4711 } 4712 #ifdef FEAT_WINDOWS 4713 /* don't close last window */ 4714 if (firstwin == lastwin 4715 && (first_tabpage->tp_next == NULL || !had_tab)) 4716 #endif 4717 use_firstwin = TRUE; 4718 #ifdef FEAT_WINDOWS 4719 else 4720 { 4721 win_close(wp, !P_HID(buf) && !bufIsChanged(buf)); 4722 # ifdef FEAT_AUTOCMD 4723 /* check if autocommands removed the next window */ 4724 if (!win_valid(wpnext)) 4725 wpnext = firstwin; /* start all over... */ 4726 # endif 4727 } 4728 #endif 4729 } 4730 } 4731 } 4732 4733 /* Without the ":tab" modifier only do the current tab page. */ 4734 if (had_tab == 0 || tpnext == NULL) 4735 break; 4736 4737 # ifdef FEAT_AUTOCMD 4738 /* check if autocommands removed the next tab page */ 4739 if (!valid_tabpage(tpnext)) 4740 tpnext = first_tabpage; /* start all over...*/ 4741 # endif 4742 goto_tabpage_tp(tpnext, TRUE, TRUE); 4743 } 4744 4745 /* 4746 * Open a window for files in the argument list that don't have one. 4747 * ARGCOUNT may change while doing this, because of autocommands. 4748 */ 4749 if (count > opened_len || count <= 0) 4750 count = opened_len; 4751 4752 #ifdef FEAT_AUTOCMD 4753 /* Don't execute Win/Buf Enter/Leave autocommands here. */ 4754 ++autocmd_no_enter; 4755 ++autocmd_no_leave; 4756 #endif 4757 last_curwin = curwin; 4758 last_curtab = curtab; 4759 win_enter(lastwin, FALSE); 4760 #ifdef FEAT_WINDOWS 4761 /* ":drop all" should re-use an empty window to avoid "--remote-tab" 4762 * leaving an empty tab page when executed locally. */ 4763 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1 4764 && curbuf->b_ffname == NULL && !curbuf->b_changed) 4765 use_firstwin = TRUE; 4766 #endif 4767 4768 for (i = 0; i < count && i < opened_len && !got_int; ++i) 4769 { 4770 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1) 4771 arg_had_last = TRUE; 4772 if (opened[i] > 0) 4773 { 4774 /* Move the already present window to below the current window */ 4775 if (curwin->w_arg_idx != i) 4776 { 4777 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next) 4778 { 4779 if (wpnext->w_arg_idx == i) 4780 { 4781 if (keep_tabs) 4782 { 4783 new_curwin = wpnext; 4784 new_curtab = curtab; 4785 } 4786 else 4787 win_move_after(wpnext, curwin); 4788 break; 4789 } 4790 } 4791 } 4792 } 4793 else if (split_ret == OK) 4794 { 4795 if (!use_firstwin) /* split current window */ 4796 { 4797 p_ea_save = p_ea; 4798 p_ea = TRUE; /* use space from all windows */ 4799 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); 4800 p_ea = p_ea_save; 4801 if (split_ret == FAIL) 4802 continue; 4803 } 4804 #ifdef FEAT_AUTOCMD 4805 else /* first window: do autocmd for leaving this buffer */ 4806 --autocmd_no_leave; 4807 #endif 4808 4809 /* 4810 * edit file "i" 4811 */ 4812 curwin->w_arg_idx = i; 4813 if (i == 0) 4814 { 4815 new_curwin = curwin; 4816 new_curtab = curtab; 4817 } 4818 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL, 4819 ECMD_ONE, 4820 ((P_HID(curwin->w_buffer) 4821 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0) 4822 + ECMD_OLDBUF, curwin); 4823 #ifdef FEAT_AUTOCMD 4824 if (use_firstwin) 4825 ++autocmd_no_leave; 4826 #endif 4827 use_firstwin = FALSE; 4828 } 4829 ui_breakcheck(); 4830 4831 /* When ":tab" was used open a new tab for a new window repeatedly. */ 4832 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) 4833 cmdmod.tab = 9999; 4834 } 4835 4836 /* Remove the "lock" on the argument list. */ 4837 alist_unlink(alist); 4838 4839 #ifdef FEAT_AUTOCMD 4840 --autocmd_no_enter; 4841 #endif 4842 /* restore last referenced tabpage's curwin */ 4843 if (last_curtab != new_curtab) 4844 { 4845 if (valid_tabpage(last_curtab)) 4846 goto_tabpage_tp(last_curtab, TRUE, TRUE); 4847 if (win_valid(last_curwin)) 4848 win_enter(last_curwin, FALSE); 4849 } 4850 /* to window with first arg */ 4851 if (valid_tabpage(new_curtab)) 4852 goto_tabpage_tp(new_curtab, TRUE, TRUE); 4853 if (win_valid(new_curwin)) 4854 win_enter(new_curwin, FALSE); 4855 4856 #ifdef FEAT_AUTOCMD 4857 --autocmd_no_leave; 4858 #endif 4859 vim_free(opened); 4860 } 4861 4862 # if defined(FEAT_LISTCMDS) || defined(PROTO) 4863 /* 4864 * Open a window for a number of buffers. 4865 */ 4866 void 4867 ex_buffer_all(eap) 4868 exarg_T *eap; 4869 { 4870 buf_T *buf; 4871 win_T *wp, *wpnext; 4872 int split_ret = OK; 4873 int p_ea_save; 4874 int open_wins = 0; 4875 int r; 4876 int count; /* Maximum number of windows to open. */ 4877 int all; /* When TRUE also load inactive buffers. */ 4878 #ifdef FEAT_WINDOWS 4879 int had_tab = cmdmod.tab; 4880 tabpage_T *tpnext; 4881 #endif 4882 4883 if (eap->addr_count == 0) /* make as many windows as possible */ 4884 count = 9999; 4885 else 4886 count = eap->line2; /* make as many windows as specified */ 4887 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) 4888 all = FALSE; 4889 else 4890 all = TRUE; 4891 4892 setpcmark(); 4893 4894 #ifdef FEAT_GUI 4895 need_mouse_correct = TRUE; 4896 #endif 4897 4898 /* 4899 * Close superfluous windows (two windows for the same buffer). 4900 * Also close windows that are not full-width. 4901 */ 4902 #ifdef FEAT_WINDOWS 4903 if (had_tab > 0) 4904 goto_tabpage_tp(first_tabpage, TRUE, TRUE); 4905 for (;;) 4906 { 4907 #endif 4908 tpnext = curtab->tp_next; 4909 for (wp = firstwin; wp != NULL; wp = wpnext) 4910 { 4911 wpnext = wp->w_next; 4912 if ((wp->w_buffer->b_nwindows > 1 4913 #ifdef FEAT_VERTSPLIT 4914 || ((cmdmod.split & WSP_VERT) 4915 ? wp->w_height + wp->w_status_height < Rows - p_ch 4916 - tabline_height() 4917 : wp->w_width != Columns) 4918 #endif 4919 #ifdef FEAT_WINDOWS 4920 || (had_tab > 0 && wp != firstwin) 4921 #endif 4922 ) && firstwin != lastwin 4923 #ifdef FEAT_AUTOCMD 4924 && !(wp->w_closing || wp->w_buffer->b_closing) 4925 #endif 4926 ) 4927 { 4928 win_close(wp, FALSE); 4929 #ifdef FEAT_AUTOCMD 4930 wpnext = firstwin; /* just in case an autocommand does 4931 something strange with windows */ 4932 tpnext = first_tabpage; /* start all over...*/ 4933 open_wins = 0; 4934 #endif 4935 } 4936 else 4937 ++open_wins; 4938 } 4939 4940 #ifdef FEAT_WINDOWS 4941 /* Without the ":tab" modifier only do the current tab page. */ 4942 if (had_tab == 0 || tpnext == NULL) 4943 break; 4944 goto_tabpage_tp(tpnext, TRUE, TRUE); 4945 } 4946 #endif 4947 4948 /* 4949 * Go through the buffer list. When a buffer doesn't have a window yet, 4950 * open one. Otherwise move the window to the right position. 4951 * Watch out for autocommands that delete buffers or windows! 4952 */ 4953 #ifdef FEAT_AUTOCMD 4954 /* Don't execute Win/Buf Enter/Leave autocommands here. */ 4955 ++autocmd_no_enter; 4956 #endif 4957 win_enter(lastwin, FALSE); 4958 #ifdef FEAT_AUTOCMD 4959 ++autocmd_no_leave; 4960 #endif 4961 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next) 4962 { 4963 /* Check if this buffer needs a window */ 4964 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl) 4965 continue; 4966 4967 #ifdef FEAT_WINDOWS 4968 if (had_tab != 0) 4969 { 4970 /* With the ":tab" modifier don't move the window. */ 4971 if (buf->b_nwindows > 0) 4972 wp = lastwin; /* buffer has a window, skip it */ 4973 else 4974 wp = NULL; 4975 } 4976 else 4977 #endif 4978 { 4979 /* Check if this buffer already has a window */ 4980 for (wp = firstwin; wp != NULL; wp = wp->w_next) 4981 if (wp->w_buffer == buf) 4982 break; 4983 /* If the buffer already has a window, move it */ 4984 if (wp != NULL) 4985 win_move_after(wp, curwin); 4986 } 4987 4988 if (wp == NULL && split_ret == OK) 4989 { 4990 /* Split the window and put the buffer in it */ 4991 p_ea_save = p_ea; 4992 p_ea = TRUE; /* use space from all windows */ 4993 split_ret = win_split(0, WSP_ROOM | WSP_BELOW); 4994 ++open_wins; 4995 p_ea = p_ea_save; 4996 if (split_ret == FAIL) 4997 continue; 4998 4999 /* Open the buffer in this window. */ 5000 #if defined(HAS_SWAP_EXISTS_ACTION) 5001 swap_exists_action = SEA_DIALOG; 5002 #endif 5003 set_curbuf(buf, DOBUF_GOTO); 5004 #ifdef FEAT_AUTOCMD 5005 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */ 5006 { 5007 #if defined(HAS_SWAP_EXISTS_ACTION) 5008 swap_exists_action = SEA_NONE; 5009 # endif 5010 break; 5011 } 5012 #endif 5013 #if defined(HAS_SWAP_EXISTS_ACTION) 5014 if (swap_exists_action == SEA_QUIT) 5015 { 5016 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 5017 cleanup_T cs; 5018 5019 /* Reset the error/interrupt/exception state here so that 5020 * aborting() returns FALSE when closing a window. */ 5021 enter_cleanup(&cs); 5022 # endif 5023 5024 /* User selected Quit at ATTENTION prompt; close this window. */ 5025 win_close(curwin, TRUE); 5026 --open_wins; 5027 swap_exists_action = SEA_NONE; 5028 swap_exists_did_quit = TRUE; 5029 5030 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 5031 /* Restore the error/interrupt/exception state if not 5032 * discarded by a new aborting error, interrupt, or uncaught 5033 * exception. */ 5034 leave_cleanup(&cs); 5035 # endif 5036 } 5037 else 5038 handle_swap_exists(NULL); 5039 #endif 5040 } 5041 5042 ui_breakcheck(); 5043 if (got_int) 5044 { 5045 (void)vgetc(); /* only break the file loading, not the rest */ 5046 break; 5047 } 5048 #ifdef FEAT_EVAL 5049 /* Autocommands deleted the buffer or aborted script processing!!! */ 5050 if (aborting()) 5051 break; 5052 #endif 5053 #ifdef FEAT_WINDOWS 5054 /* When ":tab" was used open a new tab for a new window repeatedly. */ 5055 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm) 5056 cmdmod.tab = 9999; 5057 #endif 5058 } 5059 #ifdef FEAT_AUTOCMD 5060 --autocmd_no_enter; 5061 #endif 5062 win_enter(firstwin, FALSE); /* back to first window */ 5063 #ifdef FEAT_AUTOCMD 5064 --autocmd_no_leave; 5065 #endif 5066 5067 /* 5068 * Close superfluous windows. 5069 */ 5070 for (wp = lastwin; open_wins > count; ) 5071 { 5072 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer) 5073 || autowrite(wp->w_buffer, FALSE) == OK); 5074 #ifdef FEAT_AUTOCMD 5075 if (!win_valid(wp)) 5076 { 5077 /* BufWrite Autocommands made the window invalid, start over */ 5078 wp = lastwin; 5079 } 5080 else 5081 #endif 5082 if (r) 5083 { 5084 win_close(wp, !P_HID(wp->w_buffer)); 5085 --open_wins; 5086 wp = lastwin; 5087 } 5088 else 5089 { 5090 wp = wp->w_prev; 5091 if (wp == NULL) 5092 break; 5093 } 5094 } 5095 } 5096 # endif /* FEAT_LISTCMDS */ 5097 5098 #endif /* FEAT_WINDOWS */ 5099 5100 static int chk_modeline __ARGS((linenr_T, int)); 5101 5102 /* 5103 * do_modelines() - process mode lines for the current file 5104 * 5105 * "flags" can be: 5106 * OPT_WINONLY only set options local to window 5107 * OPT_NOWIN don't set options local to window 5108 * 5109 * Returns immediately if the "ml" option isn't set. 5110 */ 5111 void 5112 do_modelines(flags) 5113 int flags; 5114 { 5115 linenr_T lnum; 5116 int nmlines; 5117 static int entered = 0; 5118 5119 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0) 5120 return; 5121 5122 /* Disallow recursive entry here. Can happen when executing a modeline 5123 * triggers an autocommand, which reloads modelines with a ":do". */ 5124 if (entered) 5125 return; 5126 5127 ++entered; 5128 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines; 5129 ++lnum) 5130 if (chk_modeline(lnum, flags) == FAIL) 5131 nmlines = 0; 5132 5133 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines 5134 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum) 5135 if (chk_modeline(lnum, flags) == FAIL) 5136 nmlines = 0; 5137 --entered; 5138 } 5139 5140 #include "version.h" /* for version number */ 5141 5142 /* 5143 * chk_modeline() - check a single line for a mode string 5144 * Return FAIL if an error encountered. 5145 */ 5146 static int 5147 chk_modeline(lnum, flags) 5148 linenr_T lnum; 5149 int flags; /* Same as for do_modelines(). */ 5150 { 5151 char_u *s; 5152 char_u *e; 5153 char_u *linecopy; /* local copy of any modeline found */ 5154 int prev; 5155 int vers; 5156 int end; 5157 int retval = OK; 5158 char_u *save_sourcing_name; 5159 linenr_T save_sourcing_lnum; 5160 #ifdef FEAT_EVAL 5161 scid_T save_SID; 5162 #endif 5163 5164 prev = -1; 5165 for (s = ml_get(lnum); *s != NUL; ++s) 5166 { 5167 if (prev == -1 || vim_isspace(prev)) 5168 { 5169 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) 5170 || STRNCMP(s, "vi:", (size_t)3) == 0) 5171 break; 5172 /* Accept both "vim" and "Vim". */ 5173 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm') 5174 { 5175 if (s[3] == '<' || s[3] == '=' || s[3] == '>') 5176 e = s + 4; 5177 else 5178 e = s + 3; 5179 vers = getdigits(&e); 5180 if (*e == ':' 5181 && (s[0] != 'V' 5182 || STRNCMP(skipwhite(e + 1), "set", 3) == 0) 5183 && (s[3] == ':' 5184 || (VIM_VERSION_100 >= vers && isdigit(s[3])) 5185 || (VIM_VERSION_100 < vers && s[3] == '<') 5186 || (VIM_VERSION_100 > vers && s[3] == '>') 5187 || (VIM_VERSION_100 == vers && s[3] == '='))) 5188 break; 5189 } 5190 } 5191 prev = *s; 5192 } 5193 5194 if (*s) 5195 { 5196 do /* skip over "ex:", "vi:" or "vim:" */ 5197 ++s; 5198 while (s[-1] != ':'); 5199 5200 s = linecopy = vim_strsave(s); /* copy the line, it will change */ 5201 if (linecopy == NULL) 5202 return FAIL; 5203 5204 save_sourcing_lnum = sourcing_lnum; 5205 save_sourcing_name = sourcing_name; 5206 sourcing_lnum = lnum; /* prepare for emsg() */ 5207 sourcing_name = (char_u *)"modelines"; 5208 5209 end = FALSE; 5210 while (end == FALSE) 5211 { 5212 s = skipwhite(s); 5213 if (*s == NUL) 5214 break; 5215 5216 /* 5217 * Find end of set command: ':' or end of line. 5218 * Skip over "\:", replacing it with ":". 5219 */ 5220 for (e = s; *e != ':' && *e != NUL; ++e) 5221 if (e[0] == '\\' && e[1] == ':') 5222 STRMOVE(e, e + 1); 5223 if (*e == NUL) 5224 end = TRUE; 5225 5226 /* 5227 * If there is a "set" command, require a terminating ':' and 5228 * ignore the stuff after the ':'. 5229 * "vi:set opt opt opt: foo" -- foo not interpreted 5230 * "vi:opt opt opt: foo" -- foo interpreted 5231 * Accept "se" for compatibility with Elvis. 5232 */ 5233 if (STRNCMP(s, "set ", (size_t)4) == 0 5234 || STRNCMP(s, "se ", (size_t)3) == 0) 5235 { 5236 if (*e != ':') /* no terminating ':'? */ 5237 break; 5238 end = TRUE; 5239 s = vim_strchr(s, ' ') + 1; 5240 } 5241 *e = NUL; /* truncate the set command */ 5242 5243 if (*s != NUL) /* skip over an empty "::" */ 5244 { 5245 #ifdef FEAT_EVAL 5246 save_SID = current_SID; 5247 current_SID = SID_MODELINE; 5248 #endif 5249 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); 5250 #ifdef FEAT_EVAL 5251 current_SID = save_SID; 5252 #endif 5253 if (retval == FAIL) /* stop if error found */ 5254 break; 5255 } 5256 s = e + 1; /* advance to next part */ 5257 } 5258 5259 sourcing_lnum = save_sourcing_lnum; 5260 sourcing_name = save_sourcing_name; 5261 5262 vim_free(linecopy); 5263 } 5264 return retval; 5265 } 5266 5267 #if defined(FEAT_VIMINFO) || defined(PROTO) 5268 int 5269 read_viminfo_bufferlist(virp, writing) 5270 vir_T *virp; 5271 int writing; 5272 { 5273 char_u *tab; 5274 linenr_T lnum; 5275 colnr_T col; 5276 buf_T *buf; 5277 char_u *sfname; 5278 char_u *xline; 5279 5280 /* Handle long line and escaped characters. */ 5281 xline = viminfo_readstring(virp, 1, FALSE); 5282 5283 /* don't read in if there are files on the command-line or if writing: */ 5284 if (xline != NULL && !writing && ARGCOUNT == 0 5285 && find_viminfo_parameter('%') != NULL) 5286 { 5287 /* Format is: <fname> Tab <lnum> Tab <col>. 5288 * Watch out for a Tab in the file name, work from the end. */ 5289 lnum = 0; 5290 col = 0; 5291 tab = vim_strrchr(xline, '\t'); 5292 if (tab != NULL) 5293 { 5294 *tab++ = '\0'; 5295 col = (colnr_T)atoi((char *)tab); 5296 tab = vim_strrchr(xline, '\t'); 5297 if (tab != NULL) 5298 { 5299 *tab++ = '\0'; 5300 lnum = atol((char *)tab); 5301 } 5302 } 5303 5304 /* Expand "~/" in the file name at "line + 1" to a full path. 5305 * Then try shortening it by comparing with the current directory */ 5306 expand_env(xline, NameBuff, MAXPATHL); 5307 sfname = shorten_fname1(NameBuff); 5308 5309 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED); 5310 if (buf != NULL) /* just in case... */ 5311 { 5312 buf->b_last_cursor.lnum = lnum; 5313 buf->b_last_cursor.col = col; 5314 buflist_setfpos(buf, curwin, lnum, col, FALSE); 5315 } 5316 } 5317 vim_free(xline); 5318 5319 return viminfo_readline(virp); 5320 } 5321 5322 void 5323 write_viminfo_bufferlist(fp) 5324 FILE *fp; 5325 { 5326 buf_T *buf; 5327 #ifdef FEAT_WINDOWS 5328 win_T *win; 5329 tabpage_T *tp; 5330 #endif 5331 char_u *line; 5332 int max_buffers; 5333 5334 if (find_viminfo_parameter('%') == NULL) 5335 return; 5336 5337 /* Without a number -1 is returned: do all buffers. */ 5338 max_buffers = get_viminfo_parameter('%'); 5339 5340 /* Allocate room for the file name, lnum and col. */ 5341 #define LINE_BUF_LEN (MAXPATHL + 40) 5342 line = alloc(LINE_BUF_LEN); 5343 if (line == NULL) 5344 return; 5345 5346 #ifdef FEAT_WINDOWS 5347 FOR_ALL_TAB_WINDOWS(tp, win) 5348 set_last_cursor(win); 5349 #else 5350 set_last_cursor(curwin); 5351 #endif 5352 5353 fputs(_("\n# Buffer list:\n"), fp); 5354 for (buf = firstbuf; buf != NULL ; buf = buf->b_next) 5355 { 5356 if (buf->b_fname == NULL 5357 || !buf->b_p_bl 5358 #ifdef FEAT_QUICKFIX 5359 || bt_quickfix(buf) 5360 #endif 5361 || removable(buf->b_ffname)) 5362 continue; 5363 5364 if (max_buffers-- == 0) 5365 break; 5366 putc('%', fp); 5367 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE); 5368 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d", 5369 (long)buf->b_last_cursor.lnum, 5370 buf->b_last_cursor.col); 5371 viminfo_writestring(fp, line); 5372 } 5373 vim_free(line); 5374 } 5375 #endif 5376 5377 5378 /* 5379 * Return special buffer name. 5380 * Returns NULL when the buffer has a normal file name. 5381 */ 5382 char_u * 5383 buf_spname(buf) 5384 buf_T *buf; 5385 { 5386 #if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS) 5387 if (bt_quickfix(buf)) 5388 { 5389 win_T *win; 5390 tabpage_T *tp; 5391 5392 /* 5393 * For location list window, w_llist_ref points to the location list. 5394 * For quickfix window, w_llist_ref is NULL. 5395 */ 5396 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL) 5397 return (char_u *)_(msg_loclist); 5398 else 5399 return (char_u *)_(msg_qflist); 5400 } 5401 #endif 5402 #ifdef FEAT_QUICKFIX 5403 /* There is no _file_ when 'buftype' is "nofile", b_sfname 5404 * contains the name as specified by the user */ 5405 if (bt_nofile(buf)) 5406 { 5407 if (buf->b_sfname != NULL) 5408 return buf->b_sfname; 5409 return (char_u *)_("[Scratch]"); 5410 } 5411 #endif 5412 if (buf->b_fname == NULL) 5413 return (char_u *)_("[No Name]"); 5414 return NULL; 5415 } 5416 5417 #if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \ 5418 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \ 5419 || defined(PROTO) 5420 /* 5421 * Find a window for buffer "buf". 5422 * If found OK is returned and "wp" and "tp" are set to the window and tabpage. 5423 * If not found FAIL is returned. 5424 */ 5425 int 5426 find_win_for_buf(buf, wp, tp) 5427 buf_T *buf; 5428 win_T **wp; 5429 tabpage_T **tp; 5430 { 5431 FOR_ALL_TAB_WINDOWS(*tp, *wp) 5432 if ((*wp)->w_buffer == buf) 5433 goto win_found; 5434 return FAIL; 5435 win_found: 5436 return OK; 5437 } 5438 #endif 5439 5440 #if defined(FEAT_SIGNS) || defined(PROTO) 5441 /* 5442 * Insert the sign into the signlist. 5443 */ 5444 static void 5445 insert_sign(buf, prev, next, id, lnum, typenr) 5446 buf_T *buf; /* buffer to store sign in */ 5447 signlist_T *prev; /* previous sign entry */ 5448 signlist_T *next; /* next sign entry */ 5449 int id; /* sign ID */ 5450 linenr_T lnum; /* line number which gets the mark */ 5451 int typenr; /* typenr of sign we are adding */ 5452 { 5453 signlist_T *newsign; 5454 5455 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE); 5456 if (newsign != NULL) 5457 { 5458 newsign->id = id; 5459 newsign->lnum = lnum; 5460 newsign->typenr = typenr; 5461 newsign->next = next; 5462 #ifdef FEAT_NETBEANS_INTG 5463 newsign->prev = prev; 5464 if (next != NULL) 5465 next->prev = newsign; 5466 #endif 5467 5468 if (prev == NULL) 5469 { 5470 /* When adding first sign need to redraw the windows to create the 5471 * column for signs. */ 5472 if (buf->b_signlist == NULL) 5473 { 5474 redraw_buf_later(buf, NOT_VALID); 5475 changed_cline_bef_curs(); 5476 } 5477 5478 /* first sign in signlist */ 5479 buf->b_signlist = newsign; 5480 #ifdef FEAT_NETBEANS_INTG 5481 if (netbeans_active()) 5482 buf->b_has_sign_column = TRUE; 5483 #endif 5484 } 5485 else 5486 prev->next = newsign; 5487 } 5488 } 5489 5490 /* 5491 * Add the sign into the signlist. Find the right spot to do it though. 5492 */ 5493 void 5494 buf_addsign(buf, id, lnum, typenr) 5495 buf_T *buf; /* buffer to store sign in */ 5496 int id; /* sign ID */ 5497 linenr_T lnum; /* line number which gets the mark */ 5498 int typenr; /* typenr of sign we are adding */ 5499 { 5500 signlist_T *sign; /* a sign in the signlist */ 5501 signlist_T *prev; /* the previous sign */ 5502 5503 prev = NULL; 5504 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5505 { 5506 if (lnum == sign->lnum && id == sign->id) 5507 { 5508 sign->typenr = typenr; 5509 return; 5510 } 5511 else if ( 5512 #ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */ 5513 id < 0 && 5514 #endif 5515 lnum < sign->lnum) 5516 { 5517 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ 5518 /* XXX - GRP: Is this because of sign slide problem? Or is it 5519 * really needed? Or is it because we allow multiple signs per 5520 * line? If so, should I add that feature to FEAT_SIGNS? 5521 */ 5522 while (prev != NULL && prev->lnum == lnum) 5523 prev = prev->prev; 5524 if (prev == NULL) 5525 sign = buf->b_signlist; 5526 else 5527 sign = prev->next; 5528 #endif 5529 insert_sign(buf, prev, sign, id, lnum, typenr); 5530 return; 5531 } 5532 prev = sign; 5533 } 5534 #ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */ 5535 /* XXX - GRP: See previous comment */ 5536 while (prev != NULL && prev->lnum == lnum) 5537 prev = prev->prev; 5538 if (prev == NULL) 5539 sign = buf->b_signlist; 5540 else 5541 sign = prev->next; 5542 #endif 5543 insert_sign(buf, prev, sign, id, lnum, typenr); 5544 5545 return; 5546 } 5547 5548 /* 5549 * For an existing, placed sign "markId" change the type to "typenr". 5550 * Returns the line number of the sign, or zero if the sign is not found. 5551 */ 5552 linenr_T 5553 buf_change_sign_type(buf, markId, typenr) 5554 buf_T *buf; /* buffer to store sign in */ 5555 int markId; /* sign ID */ 5556 int typenr; /* typenr of sign we are adding */ 5557 { 5558 signlist_T *sign; /* a sign in the signlist */ 5559 5560 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5561 { 5562 if (sign->id == markId) 5563 { 5564 sign->typenr = typenr; 5565 return sign->lnum; 5566 } 5567 } 5568 5569 return (linenr_T)0; 5570 } 5571 5572 int 5573 buf_getsigntype(buf, lnum, type) 5574 buf_T *buf; 5575 linenr_T lnum; 5576 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */ 5577 { 5578 signlist_T *sign; /* a sign in a b_signlist */ 5579 5580 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5581 if (sign->lnum == lnum 5582 && (type == SIGN_ANY 5583 # ifdef FEAT_SIGN_ICONS 5584 || (type == SIGN_ICON 5585 && sign_get_image(sign->typenr) != NULL) 5586 # endif 5587 || (type == SIGN_TEXT 5588 && sign_get_text(sign->typenr) != NULL) 5589 || (type == SIGN_LINEHL 5590 && sign_get_attr(sign->typenr, TRUE) != 0))) 5591 return sign->typenr; 5592 return 0; 5593 } 5594 5595 5596 linenr_T 5597 buf_delsign(buf, id) 5598 buf_T *buf; /* buffer sign is stored in */ 5599 int id; /* sign id */ 5600 { 5601 signlist_T **lastp; /* pointer to pointer to current sign */ 5602 signlist_T *sign; /* a sign in a b_signlist */ 5603 signlist_T *next; /* the next sign in a b_signlist */ 5604 linenr_T lnum; /* line number whose sign was deleted */ 5605 5606 lastp = &buf->b_signlist; 5607 lnum = 0; 5608 for (sign = buf->b_signlist; sign != NULL; sign = next) 5609 { 5610 next = sign->next; 5611 if (sign->id == id) 5612 { 5613 *lastp = next; 5614 #ifdef FEAT_NETBEANS_INTG 5615 if (next != NULL) 5616 next->prev = sign->prev; 5617 #endif 5618 lnum = sign->lnum; 5619 vim_free(sign); 5620 break; 5621 } 5622 else 5623 lastp = &sign->next; 5624 } 5625 5626 /* When deleted the last sign need to redraw the windows to remove the 5627 * sign column. */ 5628 if (buf->b_signlist == NULL) 5629 { 5630 redraw_buf_later(buf, NOT_VALID); 5631 changed_cline_bef_curs(); 5632 } 5633 5634 return lnum; 5635 } 5636 5637 5638 /* 5639 * Find the line number of the sign with the requested id. If the sign does 5640 * not exist, return 0 as the line number. This will still let the correct file 5641 * get loaded. 5642 */ 5643 int 5644 buf_findsign(buf, id) 5645 buf_T *buf; /* buffer to store sign in */ 5646 int id; /* sign ID */ 5647 { 5648 signlist_T *sign; /* a sign in the signlist */ 5649 5650 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5651 if (sign->id == id) 5652 return sign->lnum; 5653 5654 return 0; 5655 } 5656 5657 int 5658 buf_findsign_id(buf, lnum) 5659 buf_T *buf; /* buffer whose sign we are searching for */ 5660 linenr_T lnum; /* line number of sign */ 5661 { 5662 signlist_T *sign; /* a sign in the signlist */ 5663 5664 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5665 if (sign->lnum == lnum) 5666 return sign->id; 5667 5668 return 0; 5669 } 5670 5671 5672 # if defined(FEAT_NETBEANS_INTG) || defined(PROTO) 5673 /* see if a given type of sign exists on a specific line */ 5674 int 5675 buf_findsigntype_id(buf, lnum, typenr) 5676 buf_T *buf; /* buffer whose sign we are searching for */ 5677 linenr_T lnum; /* line number of sign */ 5678 int typenr; /* sign type number */ 5679 { 5680 signlist_T *sign; /* a sign in the signlist */ 5681 5682 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5683 if (sign->lnum == lnum && sign->typenr == typenr) 5684 return sign->id; 5685 5686 return 0; 5687 } 5688 5689 5690 # if defined(FEAT_SIGN_ICONS) || defined(PROTO) 5691 /* return the number of icons on the given line */ 5692 int 5693 buf_signcount(buf, lnum) 5694 buf_T *buf; 5695 linenr_T lnum; 5696 { 5697 signlist_T *sign; /* a sign in the signlist */ 5698 int count = 0; 5699 5700 for (sign = buf->b_signlist; sign != NULL; sign = sign->next) 5701 if (sign->lnum == lnum) 5702 if (sign_get_image(sign->typenr) != NULL) 5703 count++; 5704 5705 return count; 5706 } 5707 # endif /* FEAT_SIGN_ICONS */ 5708 # endif /* FEAT_NETBEANS_INTG */ 5709 5710 5711 /* 5712 * Delete signs in buffer "buf". 5713 */ 5714 void 5715 buf_delete_signs(buf) 5716 buf_T *buf; 5717 { 5718 signlist_T *next; 5719 5720 /* When deleting the last sign need to redraw the windows to remove the 5721 * sign column. Not when curwin is NULL (this means we're exiting). */ 5722 if (buf->b_signlist != NULL && curwin != NULL) 5723 { 5724 redraw_buf_later(buf, NOT_VALID); 5725 changed_cline_bef_curs(); 5726 } 5727 5728 while (buf->b_signlist != NULL) 5729 { 5730 next = buf->b_signlist->next; 5731 vim_free(buf->b_signlist); 5732 buf->b_signlist = next; 5733 } 5734 } 5735 5736 /* 5737 * Delete all signs in all buffers. 5738 */ 5739 void 5740 buf_delete_all_signs() 5741 { 5742 buf_T *buf; /* buffer we are checking for signs */ 5743 5744 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 5745 if (buf->b_signlist != NULL) 5746 buf_delete_signs(buf); 5747 } 5748 5749 /* 5750 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers. 5751 */ 5752 void 5753 sign_list_placed(rbuf) 5754 buf_T *rbuf; 5755 { 5756 buf_T *buf; 5757 signlist_T *p; 5758 char lbuf[BUFSIZ]; 5759 5760 MSG_PUTS_TITLE(_("\n--- Signs ---")); 5761 msg_putchar('\n'); 5762 if (rbuf == NULL) 5763 buf = firstbuf; 5764 else 5765 buf = rbuf; 5766 while (buf != NULL && !got_int) 5767 { 5768 if (buf->b_signlist != NULL) 5769 { 5770 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname); 5771 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D)); 5772 msg_putchar('\n'); 5773 } 5774 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next) 5775 { 5776 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"), 5777 (long)p->lnum, p->id, sign_typenr2name(p->typenr)); 5778 MSG_PUTS(lbuf); 5779 msg_putchar('\n'); 5780 } 5781 if (rbuf != NULL) 5782 break; 5783 buf = buf->b_next; 5784 } 5785 } 5786 5787 /* 5788 * Adjust a placed sign for inserted/deleted lines. 5789 */ 5790 void 5791 sign_mark_adjust(line1, line2, amount, amount_after) 5792 linenr_T line1; 5793 linenr_T line2; 5794 long amount; 5795 long amount_after; 5796 { 5797 signlist_T *sign; /* a sign in a b_signlist */ 5798 5799 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next) 5800 { 5801 if (sign->lnum >= line1 && sign->lnum <= line2) 5802 { 5803 if (amount == MAXLNUM) 5804 sign->lnum = line1; 5805 else 5806 sign->lnum += amount; 5807 } 5808 else if (sign->lnum > line2) 5809 sign->lnum += amount_after; 5810 } 5811 } 5812 #endif /* FEAT_SIGNS */ 5813 5814 /* 5815 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed. 5816 */ 5817 void 5818 set_buflisted(on) 5819 int on; 5820 { 5821 if (on != curbuf->b_p_bl) 5822 { 5823 curbuf->b_p_bl = on; 5824 #ifdef FEAT_AUTOCMD 5825 if (on) 5826 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); 5827 else 5828 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); 5829 #endif 5830 } 5831 } 5832 5833 /* 5834 * Read the file for "buf" again and check if the contents changed. 5835 * Return TRUE if it changed or this could not be checked. 5836 */ 5837 int 5838 buf_contents_changed(buf) 5839 buf_T *buf; 5840 { 5841 buf_T *newbuf; 5842 int differ = TRUE; 5843 linenr_T lnum; 5844 aco_save_T aco; 5845 exarg_T ea; 5846 5847 /* Allocate a buffer without putting it in the buffer list. */ 5848 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); 5849 if (newbuf == NULL) 5850 return TRUE; 5851 5852 /* Force the 'fileencoding' and 'fileformat' to be equal. */ 5853 if (prep_exarg(&ea, buf) == FAIL) 5854 { 5855 wipe_buffer(newbuf, FALSE); 5856 return TRUE; 5857 } 5858 5859 /* set curwin/curbuf to buf and save a few things */ 5860 aucmd_prepbuf(&aco, newbuf); 5861 5862 if (ml_open(curbuf) == OK 5863 && readfile(buf->b_ffname, buf->b_fname, 5864 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, 5865 &ea, READ_NEW | READ_DUMMY) == OK) 5866 { 5867 /* compare the two files line by line */ 5868 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count) 5869 { 5870 differ = FALSE; 5871 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum) 5872 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0) 5873 { 5874 differ = TRUE; 5875 break; 5876 } 5877 } 5878 } 5879 vim_free(ea.cmd); 5880 5881 /* restore curwin/curbuf and a few other things */ 5882 aucmd_restbuf(&aco); 5883 5884 if (curbuf != newbuf) /* safety check */ 5885 wipe_buffer(newbuf, FALSE); 5886 5887 return differ; 5888 } 5889 5890 /* 5891 * Wipe out a buffer and decrement the last buffer number if it was used for 5892 * this buffer. Call this to wipe out a temp buffer that does not contain any 5893 * marks. 5894 */ 5895 void 5896 wipe_buffer(buf, aucmd) 5897 buf_T *buf; 5898 int aucmd UNUSED; /* When TRUE trigger autocommands. */ 5899 { 5900 if (buf->b_fnum == top_file_num - 1) 5901 --top_file_num; 5902 5903 #ifdef FEAT_AUTOCMD 5904 if (!aucmd) /* Don't trigger BufDelete autocommands here. */ 5905 block_autocmds(); 5906 #endif 5907 close_buffer(NULL, buf, DOBUF_WIPE, FALSE); 5908 #ifdef FEAT_AUTOCMD 5909 if (!aucmd) 5910 unblock_autocmds(); 5911 #endif 5912 } 5913