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