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