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