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