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