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