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