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