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