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