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