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