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