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