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