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