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