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