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