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