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