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