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