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