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