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