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