Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | * mark.c: functions for setting marks and jumping to them |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * This file contains routines to maintain and manipulate marks. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * If a named file mark's lnum is non-zero, it is valid. |
| 22 | * If a named file mark's fnum is non-zero, it is for an existing buffer, |
| 23 | * otherwise it is from .viminfo and namedfm[n].fname is the file name. |
| 24 | * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing |
| 25 | * viminfo). |
| 26 | */ |
| 27 | #define EXTRA_MARKS 10 /* marks 0-9 */ |
| 28 | static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */ |
| 29 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 30 | static void fname2fnum(xfmark_T *fm); |
| 31 | static void fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf); |
| 32 | static char_u *mark_line(pos_T *mp, int lead_len); |
| 33 | static void show_one_mark(int, char_u *, pos_T *, char_u *, int current); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | #ifdef FEAT_JUMPLIST |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 35 | static void cleanup_jumplist(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | #endif |
| 37 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 38 | static void write_one_filemark(FILE *fp, xfmark_T *fm, int c1, int c2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | #endif |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 40 | static void mark_adjust_internal(linenr_T line1, linenr_T line2, long amount, |
| 41 | long amount_after, int adjust_folds); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 44 | * Set named mark "c" at current cursor position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | * Returns OK on success, FAIL if bad name given. |
| 46 | */ |
| 47 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 48 | setmark(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | { |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 50 | return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Set named mark "c" to position "pos". |
| 55 | * When "c" is upper case use file "fnum". |
| 56 | * Returns OK on success, FAIL if bad name given. |
| 57 | */ |
| 58 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 59 | setmark_pos(int c, pos_T *pos, int fnum) |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 60 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | int i; |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 62 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 63 | |
| 64 | /* Check for a special key (may cause islower() to crash). */ |
| 65 | if (c < 0) |
| 66 | return FAIL; |
| 67 | |
| 68 | if (c == '\'' || c == '`') |
| 69 | { |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 70 | if (pos == &curwin->w_cursor) |
| 71 | { |
| 72 | setpcmark(); |
| 73 | /* keep it even when the cursor doesn't move */ |
| 74 | curwin->w_prev_pcmark = curwin->w_pcmark; |
| 75 | } |
| 76 | else |
| 77 | curwin->w_pcmark = *pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | return OK; |
| 79 | } |
| 80 | |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 81 | buf = buflist_findnr(fnum); |
| 82 | if (buf == NULL) |
| 83 | return FAIL; |
| 84 | |
Bram Moolenaar | 0825043 | 2008-02-13 11:42:46 +0000 | [diff] [blame] | 85 | if (c == '"') |
| 86 | { |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 87 | buf->b_last_cursor = *pos; |
Bram Moolenaar | 0825043 | 2008-02-13 11:42:46 +0000 | [diff] [blame] | 88 | return OK; |
| 89 | } |
| 90 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | /* Allow setting '[ and '] for an autocommand that simulates reading a |
| 92 | * file. */ |
| 93 | if (c == '[') |
| 94 | { |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 95 | buf->b_op_start = *pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | return OK; |
| 97 | } |
| 98 | if (c == ']') |
| 99 | { |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 100 | buf->b_op_end = *pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 101 | return OK; |
| 102 | } |
| 103 | |
Bram Moolenaar | bc88a27 | 2013-08-02 17:22:23 +0200 | [diff] [blame] | 104 | if (c == '<' || c == '>') |
Bram Moolenaar | 0306ac3 | 2012-07-06 17:51:28 +0200 | [diff] [blame] | 105 | { |
Bram Moolenaar | bc88a27 | 2013-08-02 17:22:23 +0200 | [diff] [blame] | 106 | if (c == '<') |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 107 | buf->b_visual.vi_start = *pos; |
Bram Moolenaar | bc88a27 | 2013-08-02 17:22:23 +0200 | [diff] [blame] | 108 | else |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 109 | buf->b_visual.vi_end = *pos; |
| 110 | if (buf->b_visual.vi_mode == NUL) |
Bram Moolenaar | bc88a27 | 2013-08-02 17:22:23 +0200 | [diff] [blame] | 111 | /* Visual_mode has not yet been set, use a sane default. */ |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 112 | buf->b_visual.vi_mode = 'v'; |
Bram Moolenaar | 0306ac3 | 2012-07-06 17:51:28 +0200 | [diff] [blame] | 113 | return OK; |
| 114 | } |
Bram Moolenaar | 0306ac3 | 2012-07-06 17:51:28 +0200 | [diff] [blame] | 115 | |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 116 | if (ASCII_ISLOWER(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | { |
| 118 | i = c - 'a'; |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 119 | buf->b_namedm[i] = *pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | return OK; |
| 121 | } |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 122 | if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | { |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 124 | if (VIM_ISDIGIT(c)) |
| 125 | i = c - '0' + NMARKS; |
| 126 | else |
| 127 | i = c - 'A'; |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 128 | namedfm[i].fmark.mark = *pos; |
| 129 | namedfm[i].fmark.fnum = fnum; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 130 | VIM_CLEAR(namedfm[i].fname); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 131 | #ifdef FEAT_VIMINFO |
| 132 | namedfm[i].time_set = vim_time(); |
| 133 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | return OK; |
| 135 | } |
| 136 | return FAIL; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Set the previous context mark to the current position and add it to the |
| 141 | * jump list. |
| 142 | */ |
| 143 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 144 | setpcmark(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | { |
| 146 | #ifdef FEAT_JUMPLIST |
| 147 | int i; |
| 148 | xfmark_T *fm; |
| 149 | #endif |
| 150 | #ifdef JUMPLIST_ROTATE |
| 151 | xfmark_T tempmark; |
| 152 | #endif |
| 153 | |
| 154 | /* for :global the mark is set only once */ |
| 155 | if (global_busy || listcmd_busy || cmdmod.keepjumps) |
| 156 | return; |
| 157 | |
| 158 | curwin->w_prev_pcmark = curwin->w_pcmark; |
| 159 | curwin->w_pcmark = curwin->w_cursor; |
| 160 | |
| 161 | #ifdef FEAT_JUMPLIST |
| 162 | # ifdef JUMPLIST_ROTATE |
| 163 | /* |
| 164 | * If last used entry is not at the top, put it at the top by rotating |
| 165 | * the stack until it is (the newer entries will be at the bottom). |
| 166 | * Keep one entry (the last used one) at the top. |
| 167 | */ |
| 168 | if (curwin->w_jumplistidx < curwin->w_jumplistlen) |
| 169 | ++curwin->w_jumplistidx; |
| 170 | while (curwin->w_jumplistidx < curwin->w_jumplistlen) |
| 171 | { |
| 172 | tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1]; |
| 173 | for (i = curwin->w_jumplistlen - 1; i > 0; --i) |
| 174 | curwin->w_jumplist[i] = curwin->w_jumplist[i - 1]; |
| 175 | curwin->w_jumplist[0] = tempmark; |
| 176 | ++curwin->w_jumplistidx; |
| 177 | } |
| 178 | # endif |
| 179 | |
| 180 | /* If jumplist is full: remove oldest entry */ |
| 181 | if (++curwin->w_jumplistlen > JUMPLISTSIZE) |
| 182 | { |
| 183 | curwin->w_jumplistlen = JUMPLISTSIZE; |
| 184 | vim_free(curwin->w_jumplist[0].fname); |
| 185 | for (i = 1; i < JUMPLISTSIZE; ++i) |
| 186 | curwin->w_jumplist[i - 1] = curwin->w_jumplist[i]; |
| 187 | } |
| 188 | curwin->w_jumplistidx = curwin->w_jumplistlen; |
| 189 | fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1]; |
| 190 | |
| 191 | fm->fmark.mark = curwin->w_pcmark; |
| 192 | fm->fmark.fnum = curbuf->b_fnum; |
| 193 | fm->fname = NULL; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 194 | # ifdef FEAT_VIMINFO |
| 195 | fm->time_set = vim_time(); |
| 196 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | #endif |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * To change context, call setpcmark(), then move the current position to |
| 202 | * where ever, then call checkpcmark(). This ensures that the previous |
| 203 | * context will only be changed if the cursor moved to a different line. |
| 204 | * If pcmark was deleted (with "dG") the previous mark is restored. |
| 205 | */ |
| 206 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 207 | checkpcmark(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | { |
| 209 | if (curwin->w_prev_pcmark.lnum != 0 |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 210 | && (EQUAL_POS(curwin->w_pcmark, curwin->w_cursor) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 211 | || curwin->w_pcmark.lnum == 0)) |
| 212 | { |
| 213 | curwin->w_pcmark = curwin->w_prev_pcmark; |
| 214 | curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */ |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | #if defined(FEAT_JUMPLIST) || defined(PROTO) |
| 219 | /* |
| 220 | * move "count" positions in the jump list (count may be negative) |
| 221 | */ |
| 222 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 223 | movemark(int count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | { |
| 225 | pos_T *pos; |
| 226 | xfmark_T *jmp; |
| 227 | |
| 228 | cleanup_jumplist(); |
| 229 | |
| 230 | if (curwin->w_jumplistlen == 0) /* nothing to jump to */ |
| 231 | return (pos_T *)NULL; |
| 232 | |
| 233 | for (;;) |
| 234 | { |
| 235 | if (curwin->w_jumplistidx + count < 0 |
| 236 | || curwin->w_jumplistidx + count >= curwin->w_jumplistlen) |
| 237 | return (pos_T *)NULL; |
| 238 | |
| 239 | /* |
| 240 | * if first CTRL-O or CTRL-I command after a jump, add cursor position |
Bram Moolenaar | f711faf | 2007-05-10 16:48:19 +0000 | [diff] [blame] | 241 | * to list. Careful: If there are duplicates (CTRL-O immediately after |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | * starting Vim on a file), another entry may have been removed. |
| 243 | */ |
| 244 | if (curwin->w_jumplistidx == curwin->w_jumplistlen) |
| 245 | { |
| 246 | setpcmark(); |
| 247 | --curwin->w_jumplistidx; /* skip the new entry */ |
| 248 | if (curwin->w_jumplistidx + count < 0) |
| 249 | return (pos_T *)NULL; |
| 250 | } |
| 251 | |
| 252 | curwin->w_jumplistidx += count; |
| 253 | |
| 254 | jmp = curwin->w_jumplist + curwin->w_jumplistidx; |
| 255 | if (jmp->fmark.fnum == 0) |
| 256 | fname2fnum(jmp); |
| 257 | if (jmp->fmark.fnum != curbuf->b_fnum) |
| 258 | { |
| 259 | /* jump to other file */ |
| 260 | if (buflist_findnr(jmp->fmark.fnum) == NULL) |
| 261 | { /* Skip this one .. */ |
| 262 | count += count < 0 ? -1 : 1; |
| 263 | continue; |
| 264 | } |
| 265 | if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum, |
| 266 | 0, FALSE) == FAIL) |
| 267 | return (pos_T *)NULL; |
| 268 | /* Set lnum again, autocommands my have changed it */ |
| 269 | curwin->w_cursor = jmp->fmark.mark; |
| 270 | pos = (pos_T *)-1; |
| 271 | } |
| 272 | else |
| 273 | pos = &(jmp->fmark.mark); |
| 274 | return pos; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Move "count" positions in the changelist (count may be negative). |
| 280 | */ |
| 281 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 282 | movechangelist(int count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 283 | { |
| 284 | int n; |
| 285 | |
| 286 | if (curbuf->b_changelistlen == 0) /* nothing to jump to */ |
| 287 | return (pos_T *)NULL; |
| 288 | |
| 289 | n = curwin->w_changelistidx; |
| 290 | if (n + count < 0) |
| 291 | { |
| 292 | if (n == 0) |
| 293 | return (pos_T *)NULL; |
| 294 | n = 0; |
| 295 | } |
| 296 | else if (n + count >= curbuf->b_changelistlen) |
| 297 | { |
| 298 | if (n == curbuf->b_changelistlen - 1) |
| 299 | return (pos_T *)NULL; |
| 300 | n = curbuf->b_changelistlen - 1; |
| 301 | } |
| 302 | else |
| 303 | n += count; |
| 304 | curwin->w_changelistidx = n; |
| 305 | return curbuf->b_changelist + n; |
| 306 | } |
| 307 | #endif |
| 308 | |
| 309 | /* |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 310 | * Find mark "c" in buffer pointed to by "buf". |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 311 | * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc. |
| 312 | * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit |
| 313 | * another file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | * Returns: |
| 315 | * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is |
| 316 | * in another file which can't be gotten. (caller needs to check lnum!) |
| 317 | * - NULL if there is no mark called 'c'. |
| 318 | * - -1 if mark is in other file and jumped there (only if changefile is TRUE) |
| 319 | */ |
| 320 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 321 | getmark_buf(buf_T *buf, int c, int changefile) |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 322 | { |
| 323 | return getmark_buf_fnum(buf, c, changefile, NULL); |
| 324 | } |
| 325 | |
| 326 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 327 | getmark(int c, int changefile) |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 328 | { |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 329 | return getmark_buf_fnum(curbuf, c, changefile, NULL); |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 333 | getmark_buf_fnum( |
| 334 | buf_T *buf, |
| 335 | int c, |
| 336 | int changefile, |
| 337 | int *fnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | { |
| 339 | pos_T *posp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | pos_T *startp, *endp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | static pos_T pos_copy; |
| 342 | |
| 343 | posp = NULL; |
| 344 | |
| 345 | /* Check for special key, can't be a mark name and might cause islower() |
| 346 | * to crash. */ |
| 347 | if (c < 0) |
| 348 | return posp; |
| 349 | #ifndef EBCDIC |
| 350 | if (c > '~') /* check for islower()/isupper() */ |
| 351 | ; |
| 352 | else |
| 353 | #endif |
| 354 | if (c == '\'' || c == '`') /* previous context mark */ |
| 355 | { |
| 356 | pos_copy = curwin->w_pcmark; /* need to make a copy because */ |
| 357 | posp = &pos_copy; /* w_pcmark may be changed soon */ |
| 358 | } |
| 359 | else if (c == '"') /* to pos when leaving buffer */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 360 | posp = &(buf->b_last_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 361 | else if (c == '^') /* to where Insert mode stopped */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 362 | posp = &(buf->b_last_insert); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 363 | else if (c == '.') /* to where last change was made */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 364 | posp = &(buf->b_last_change); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 365 | else if (c == '[') /* to start of previous operator */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 366 | posp = &(buf->b_op_start); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | else if (c == ']') /* to end of previous operator */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 368 | posp = &(buf->b_op_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 369 | else if (c == '{' || c == '}') /* to previous/next paragraph */ |
| 370 | { |
| 371 | pos_T pos; |
| 372 | oparg_T oa; |
| 373 | int slcb = listcmd_busy; |
| 374 | |
| 375 | pos = curwin->w_cursor; |
| 376 | listcmd_busy = TRUE; /* avoid that '' is changed */ |
Bram Moolenaar | 8b96d64 | 2005-09-05 22:05:30 +0000 | [diff] [blame] | 377 | if (findpar(&oa.inclusive, |
| 378 | c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | { |
| 380 | pos_copy = curwin->w_cursor; |
| 381 | posp = &pos_copy; |
| 382 | } |
| 383 | curwin->w_cursor = pos; |
| 384 | listcmd_busy = slcb; |
| 385 | } |
| 386 | else if (c == '(' || c == ')') /* to previous/next sentence */ |
| 387 | { |
| 388 | pos_T pos; |
| 389 | int slcb = listcmd_busy; |
| 390 | |
| 391 | pos = curwin->w_cursor; |
| 392 | listcmd_busy = TRUE; /* avoid that '' is changed */ |
| 393 | if (findsent(c == ')' ? FORWARD : BACKWARD, 1L)) |
| 394 | { |
| 395 | pos_copy = curwin->w_cursor; |
| 396 | posp = &pos_copy; |
| 397 | } |
| 398 | curwin->w_cursor = pos; |
| 399 | listcmd_busy = slcb; |
| 400 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 401 | else if (c == '<' || c == '>') /* start/end of visual area */ |
| 402 | { |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 403 | startp = &buf->b_visual.vi_start; |
| 404 | endp = &buf->b_visual.vi_end; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 405 | if (((c == '<') == LT_POS(*startp, *endp) || endp->lnum == 0) |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 406 | && startp->lnum != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | posp = startp; |
| 408 | else |
| 409 | posp = endp; |
| 410 | /* |
| 411 | * For Visual line mode, set mark at begin or end of line |
| 412 | */ |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 413 | if (buf->b_visual.vi_mode == 'V') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | { |
| 415 | pos_copy = *posp; |
| 416 | posp = &pos_copy; |
| 417 | if (c == '<') |
| 418 | pos_copy.col = 0; |
| 419 | else |
| 420 | pos_copy.col = MAXCOL; |
| 421 | #ifdef FEAT_VIRTUALEDIT |
| 422 | pos_copy.coladd = 0; |
| 423 | #endif |
| 424 | } |
| 425 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 426 | else if (ASCII_ISLOWER(c)) /* normal named mark */ |
| 427 | { |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 428 | posp = &(buf->b_namedm[c - 'a']); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 429 | } |
| 430 | else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */ |
| 431 | { |
| 432 | if (VIM_ISDIGIT(c)) |
| 433 | c = c - '0' + NMARKS; |
| 434 | else |
| 435 | c -= 'A'; |
| 436 | posp = &(namedfm[c].fmark.mark); |
| 437 | |
| 438 | if (namedfm[c].fmark.fnum == 0) |
| 439 | fname2fnum(&namedfm[c]); |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 440 | |
| 441 | if (fnum != NULL) |
| 442 | *fnum = namedfm[c].fmark.fnum; |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 443 | else if (namedfm[c].fmark.fnum != buf->b_fnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | { |
Bram Moolenaar | bfb2d40 | 2006-03-03 22:50:42 +0000 | [diff] [blame] | 445 | /* mark is in another file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | posp = &pos_copy; |
| 447 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 448 | if (namedfm[c].fmark.mark.lnum != 0 |
| 449 | && changefile && namedfm[c].fmark.fnum) |
| 450 | { |
| 451 | if (buflist_getfile(namedfm[c].fmark.fnum, |
| 452 | (linenr_T)1, GETF_SETMARK, FALSE) == OK) |
| 453 | { |
| 454 | /* Set the lnum now, autocommands could have changed it */ |
| 455 | curwin->w_cursor = namedfm[c].fmark.mark; |
| 456 | return (pos_T *)-1; |
| 457 | } |
| 458 | pos_copy.lnum = -1; /* can't get file */ |
| 459 | } |
| 460 | else |
| 461 | pos_copy.lnum = 0; /* mark exists, but is not valid in |
| 462 | current buffer */ |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return posp; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Search for the next named mark in the current file. |
| 471 | * |
| 472 | * Returns pointer to pos_T of the next mark or NULL if no mark is found. |
| 473 | */ |
| 474 | pos_T * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 475 | getnextmark( |
| 476 | pos_T *startpos, /* where to start */ |
| 477 | int dir, /* direction for search */ |
| 478 | int begin_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 479 | { |
| 480 | int i; |
| 481 | pos_T *result = NULL; |
| 482 | pos_T pos; |
| 483 | |
| 484 | pos = *startpos; |
| 485 | |
| 486 | /* When searching backward and leaving the cursor on the first non-blank, |
| 487 | * position must be in a previous line. |
| 488 | * When searching forward and leaving the cursor on the first non-blank, |
| 489 | * position must be in a next line. */ |
| 490 | if (dir == BACKWARD && begin_line) |
| 491 | pos.col = 0; |
| 492 | else if (dir == FORWARD && begin_line) |
| 493 | pos.col = MAXCOL; |
| 494 | |
| 495 | for (i = 0; i < NMARKS; i++) |
| 496 | { |
| 497 | if (curbuf->b_namedm[i].lnum > 0) |
| 498 | { |
| 499 | if (dir == FORWARD) |
| 500 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 501 | if ((result == NULL || LT_POS(curbuf->b_namedm[i], *result)) |
| 502 | && LT_POS(pos, curbuf->b_namedm[i])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | result = &curbuf->b_namedm[i]; |
| 504 | } |
| 505 | else |
| 506 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 507 | if ((result == NULL || LT_POS(*result, curbuf->b_namedm[i])) |
| 508 | && LT_POS(curbuf->b_namedm[i], pos)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 509 | result = &curbuf->b_namedm[i]; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return result; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * For an xtended filemark: set the fnum from the fname. |
| 519 | * This is used for marks obtained from the .viminfo file. It's postponed |
| 520 | * until the mark is used to avoid a long startup delay. |
| 521 | */ |
| 522 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 523 | fname2fnum(xfmark_T *fm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | { |
| 525 | char_u *p; |
| 526 | |
| 527 | if (fm->fname != NULL) |
| 528 | { |
| 529 | /* |
| 530 | * First expand "~/" in the file name to the home directory. |
Bram Moolenaar | 525ad4d | 2008-01-03 19:22:13 +0000 | [diff] [blame] | 531 | * Don't expand the whole name, it may contain other '~' chars. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | */ |
Bram Moolenaar | 525ad4d | 2008-01-03 19:22:13 +0000 | [diff] [blame] | 533 | if (fm->fname[0] == '~' && (fm->fname[1] == '/' |
| 534 | #ifdef BACKSLASH_IN_FILENAME |
| 535 | || fm->fname[1] == '\\' |
| 536 | #endif |
| 537 | )) |
| 538 | { |
| 539 | int len; |
| 540 | |
| 541 | expand_env((char_u *)"~/", NameBuff, MAXPATHL); |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 542 | len = (int)STRLEN(NameBuff); |
Bram Moolenaar | 525ad4d | 2008-01-03 19:22:13 +0000 | [diff] [blame] | 543 | vim_strncpy(NameBuff + len, fm->fname + 2, MAXPATHL - len - 1); |
| 544 | } |
| 545 | else |
| 546 | vim_strncpy(NameBuff, fm->fname, MAXPATHL - 1); |
| 547 | |
| 548 | /* Try to shorten the file name. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | mch_dirname(IObuff, IOSIZE); |
| 550 | p = shorten_fname(NameBuff, IObuff); |
| 551 | |
| 552 | /* buflist_new() will call fmarks_check_names() */ |
| 553 | (void)buflist_new(NameBuff, p, (linenr_T)1, 0); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Check all file marks for a name that matches the file name in buf. |
| 559 | * May replace the name with an fnum. |
| 560 | * Used for marks that come from the .viminfo file. |
| 561 | */ |
| 562 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 563 | fmarks_check_names(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | { |
| 565 | char_u *name; |
| 566 | int i; |
| 567 | #ifdef FEAT_JUMPLIST |
| 568 | win_T *wp; |
| 569 | #endif |
| 570 | |
| 571 | if (buf->b_ffname == NULL) |
| 572 | return; |
| 573 | |
| 574 | name = home_replace_save(buf, buf->b_ffname); |
| 575 | if (name == NULL) |
| 576 | return; |
| 577 | |
| 578 | for (i = 0; i < NMARKS + EXTRA_MARKS; ++i) |
| 579 | fmarks_check_one(&namedfm[i], name, buf); |
| 580 | |
| 581 | #ifdef FEAT_JUMPLIST |
| 582 | FOR_ALL_WINDOWS(wp) |
| 583 | { |
| 584 | for (i = 0; i < wp->w_jumplistlen; ++i) |
| 585 | fmarks_check_one(&wp->w_jumplist[i], name, buf); |
| 586 | } |
| 587 | #endif |
| 588 | |
| 589 | vim_free(name); |
| 590 | } |
| 591 | |
| 592 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 593 | fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | { |
| 595 | if (fm->fmark.fnum == 0 |
| 596 | && fm->fname != NULL |
| 597 | && fnamecmp(name, fm->fname) == 0) |
| 598 | { |
| 599 | fm->fmark.fnum = buf->b_fnum; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 600 | VIM_CLEAR(fm->fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Check a if a position from a mark is valid. |
| 606 | * Give and error message and return FAIL if not. |
| 607 | */ |
| 608 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 609 | check_mark(pos_T *pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 610 | { |
| 611 | if (pos == NULL) |
| 612 | { |
| 613 | EMSG(_(e_umark)); |
| 614 | return FAIL; |
| 615 | } |
| 616 | if (pos->lnum <= 0) |
| 617 | { |
| 618 | /* lnum is negative if mark is in another file can can't get that |
| 619 | * file, error message already give then. */ |
| 620 | if (pos->lnum == 0) |
| 621 | EMSG(_(e_marknotset)); |
| 622 | return FAIL; |
| 623 | } |
| 624 | if (pos->lnum > curbuf->b_ml.ml_line_count) |
| 625 | { |
| 626 | EMSG(_(e_markinval)); |
| 627 | return FAIL; |
| 628 | } |
| 629 | return OK; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * clrallmarks() - clear all marks in the buffer 'buf' |
| 634 | * |
| 635 | * Used mainly when trashing the entire buffer during ":e" type commands |
| 636 | */ |
| 637 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 638 | clrallmarks(buf_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 639 | { |
| 640 | static int i = -1; |
| 641 | |
| 642 | if (i == -1) /* first call ever: initialize */ |
| 643 | for (i = 0; i < NMARKS + 1; i++) |
| 644 | { |
| 645 | namedfm[i].fmark.mark.lnum = 0; |
| 646 | namedfm[i].fname = NULL; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 647 | #ifdef FEAT_VIMINFO |
| 648 | namedfm[i].time_set = 0; |
| 649 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | for (i = 0; i < NMARKS; i++) |
| 653 | buf->b_namedm[i].lnum = 0; |
| 654 | buf->b_op_start.lnum = 0; /* start/end op mark cleared */ |
| 655 | buf->b_op_end.lnum = 0; |
| 656 | buf->b_last_cursor.lnum = 1; /* '" mark cleared */ |
| 657 | buf->b_last_cursor.col = 0; |
| 658 | #ifdef FEAT_VIRTUALEDIT |
| 659 | buf->b_last_cursor.coladd = 0; |
| 660 | #endif |
| 661 | buf->b_last_insert.lnum = 0; /* '^ mark cleared */ |
| 662 | buf->b_last_change.lnum = 0; /* '. mark cleared */ |
| 663 | #ifdef FEAT_JUMPLIST |
| 664 | buf->b_changelistlen = 0; |
| 665 | #endif |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Get name of file from a filemark. |
| 670 | * When it's in the current buffer, return the text at the mark. |
| 671 | * Returns an allocated string. |
| 672 | */ |
| 673 | char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 674 | fm_getname(fmark_T *fmark, int lead_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | { |
| 676 | if (fmark->fnum == curbuf->b_fnum) /* current buffer */ |
| 677 | return mark_line(&(fmark->mark), lead_len); |
| 678 | return buflist_nr2name(fmark->fnum, FALSE, TRUE); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Return the line at mark "mp". Truncate to fit in window. |
| 683 | * The returned string has been allocated. |
| 684 | */ |
| 685 | static char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 686 | mark_line(pos_T *mp, int lead_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 687 | { |
| 688 | char_u *s, *p; |
| 689 | int len; |
| 690 | |
| 691 | if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count) |
| 692 | return vim_strsave((char_u *)"-invalid-"); |
| 693 | s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns); |
| 694 | if (s == NULL) |
| 695 | return NULL; |
| 696 | /* Truncate the line to fit it in the window */ |
| 697 | len = 0; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 698 | for (p = s; *p != NUL; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 699 | { |
| 700 | len += ptr2cells(p); |
| 701 | if (len >= Columns - lead_len) |
| 702 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 703 | } |
| 704 | *p = NUL; |
| 705 | return s; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * print the marks |
| 710 | */ |
| 711 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 712 | do_marks(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 713 | { |
| 714 | char_u *arg = eap->arg; |
| 715 | int i; |
| 716 | char_u *name; |
| 717 | |
| 718 | if (arg != NULL && *arg == NUL) |
| 719 | arg = NULL; |
| 720 | |
| 721 | show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE); |
| 722 | for (i = 0; i < NMARKS; ++i) |
| 723 | show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE); |
| 724 | for (i = 0; i < NMARKS + EXTRA_MARKS; ++i) |
| 725 | { |
| 726 | if (namedfm[i].fmark.fnum != 0) |
| 727 | name = fm_getname(&namedfm[i].fmark, 15); |
| 728 | else |
| 729 | name = namedfm[i].fname; |
| 730 | if (name != NULL) |
| 731 | { |
| 732 | show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A', |
| 733 | arg, &namedfm[i].fmark.mark, name, |
| 734 | namedfm[i].fmark.fnum == curbuf->b_fnum); |
| 735 | if (namedfm[i].fmark.fnum != 0) |
| 736 | vim_free(name); |
| 737 | } |
| 738 | } |
| 739 | show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE); |
| 740 | show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE); |
| 741 | show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE); |
| 742 | show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE); |
| 743 | show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE); |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 744 | show_one_mark('<', arg, &curbuf->b_visual.vi_start, NULL, TRUE); |
| 745 | show_one_mark('>', arg, &curbuf->b_visual.vi_end, NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | show_one_mark(-1, arg, NULL, NULL, FALSE); |
| 747 | } |
| 748 | |
| 749 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 750 | show_one_mark( |
| 751 | int c, |
| 752 | char_u *arg, |
| 753 | pos_T *p, |
| 754 | char_u *name, |
| 755 | int current) /* in current file */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 756 | { |
| 757 | static int did_title = FALSE; |
| 758 | int mustfree = FALSE; |
| 759 | |
| 760 | if (c == -1) /* finish up */ |
| 761 | { |
| 762 | if (did_title) |
| 763 | did_title = FALSE; |
| 764 | else |
| 765 | { |
| 766 | if (arg == NULL) |
| 767 | MSG(_("No marks set")); |
| 768 | else |
| 769 | EMSG2(_("E283: No marks matching \"%s\""), arg); |
| 770 | } |
| 771 | } |
| 772 | /* don't output anything if 'q' typed at --more-- prompt */ |
| 773 | else if (!got_int |
| 774 | && (arg == NULL || vim_strchr(arg, c) != NULL) |
| 775 | && p->lnum != 0) |
| 776 | { |
| 777 | if (!did_title) |
| 778 | { |
| 779 | /* Highlight title */ |
| 780 | MSG_PUTS_TITLE(_("\nmark line col file/text")); |
| 781 | did_title = TRUE; |
| 782 | } |
| 783 | msg_putchar('\n'); |
| 784 | if (!got_int) |
| 785 | { |
| 786 | sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col); |
| 787 | msg_outtrans(IObuff); |
| 788 | if (name == NULL && current) |
| 789 | { |
| 790 | name = mark_line(p, 15); |
| 791 | mustfree = TRUE; |
| 792 | } |
| 793 | if (name != NULL) |
| 794 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 795 | msg_outtrans_attr(name, current ? HL_ATTR(HLF_D) : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | if (mustfree) |
| 797 | vim_free(name); |
| 798 | } |
| 799 | } |
| 800 | out_flush(); /* show one line at a time */ |
| 801 | } |
| 802 | } |
| 803 | |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 804 | /* |
| 805 | * ":delmarks[!] [marks]" |
| 806 | */ |
| 807 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 808 | ex_delmarks(exarg_T *eap) |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 809 | { |
| 810 | char_u *p; |
| 811 | int from, to; |
| 812 | int i; |
| 813 | int lower; |
| 814 | int digit; |
| 815 | int n; |
| 816 | |
| 817 | if (*eap->arg == NUL && eap->forceit) |
| 818 | /* clear all marks */ |
| 819 | clrallmarks(curbuf); |
| 820 | else if (eap->forceit) |
| 821 | EMSG(_(e_invarg)); |
| 822 | else if (*eap->arg == NUL) |
| 823 | EMSG(_(e_argreq)); |
| 824 | else |
| 825 | { |
| 826 | /* clear specified marks only */ |
| 827 | for (p = eap->arg; *p != NUL; ++p) |
| 828 | { |
| 829 | lower = ASCII_ISLOWER(*p); |
| 830 | digit = VIM_ISDIGIT(*p); |
| 831 | if (lower || digit || ASCII_ISUPPER(*p)) |
| 832 | { |
| 833 | if (p[1] == '-') |
| 834 | { |
| 835 | /* clear range of marks */ |
| 836 | from = *p; |
| 837 | to = p[2]; |
| 838 | if (!(lower ? ASCII_ISLOWER(p[2]) |
| 839 | : (digit ? VIM_ISDIGIT(p[2]) |
| 840 | : ASCII_ISUPPER(p[2]))) |
| 841 | || to < from) |
| 842 | { |
| 843 | EMSG2(_(e_invarg2), p); |
| 844 | return; |
| 845 | } |
| 846 | p += 2; |
| 847 | } |
| 848 | else |
| 849 | /* clear one lower case mark */ |
| 850 | from = to = *p; |
| 851 | |
| 852 | for (i = from; i <= to; ++i) |
| 853 | { |
| 854 | if (lower) |
| 855 | curbuf->b_namedm[i - 'a'].lnum = 0; |
| 856 | else |
| 857 | { |
| 858 | if (digit) |
| 859 | n = i - '0' + NMARKS; |
| 860 | else |
| 861 | n = i - 'A'; |
| 862 | namedfm[n].fmark.mark.lnum = 0; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 863 | VIM_CLEAR(namedfm[n].fname); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 864 | #ifdef FEAT_VIMINFO |
| 865 | namedfm[n].time_set = 0; |
| 866 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | } |
| 870 | else |
| 871 | switch (*p) |
| 872 | { |
| 873 | case '"': curbuf->b_last_cursor.lnum = 0; break; |
| 874 | case '^': curbuf->b_last_insert.lnum = 0; break; |
| 875 | case '.': curbuf->b_last_change.lnum = 0; break; |
| 876 | case '[': curbuf->b_op_start.lnum = 0; break; |
| 877 | case ']': curbuf->b_op_end.lnum = 0; break; |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 878 | case '<': curbuf->b_visual.vi_start.lnum = 0; break; |
| 879 | case '>': curbuf->b_visual.vi_end.lnum = 0; break; |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 880 | case ' ': break; |
| 881 | default: EMSG2(_(e_invarg2), p); |
| 882 | return; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 888 | #if defined(FEAT_JUMPLIST) || defined(PROTO) |
| 889 | /* |
| 890 | * print the jumplist |
| 891 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 893 | ex_jumps(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 894 | { |
| 895 | int i; |
| 896 | char_u *name; |
| 897 | |
| 898 | cleanup_jumplist(); |
| 899 | /* Highlight title */ |
| 900 | MSG_PUTS_TITLE(_("\n jump line col file/text")); |
| 901 | for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i) |
| 902 | { |
| 903 | if (curwin->w_jumplist[i].fmark.mark.lnum != 0) |
| 904 | { |
| 905 | if (curwin->w_jumplist[i].fmark.fnum == 0) |
| 906 | fname2fnum(&curwin->w_jumplist[i]); |
| 907 | name = fm_getname(&curwin->w_jumplist[i].fmark, 16); |
| 908 | if (name == NULL) /* file name not available */ |
| 909 | continue; |
| 910 | |
| 911 | msg_putchar('\n'); |
| 912 | if (got_int) |
Bram Moolenaar | ed39e1d | 2008-08-09 17:55:22 +0000 | [diff] [blame] | 913 | { |
| 914 | vim_free(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 915 | break; |
Bram Moolenaar | ed39e1d | 2008-08-09 17:55:22 +0000 | [diff] [blame] | 916 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 917 | sprintf((char *)IObuff, "%c %2d %5ld %4d ", |
| 918 | i == curwin->w_jumplistidx ? '>' : ' ', |
| 919 | i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx |
| 920 | : curwin->w_jumplistidx - i, |
| 921 | curwin->w_jumplist[i].fmark.mark.lnum, |
| 922 | curwin->w_jumplist[i].fmark.mark.col); |
| 923 | msg_outtrans(IObuff); |
| 924 | msg_outtrans_attr(name, |
| 925 | curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 926 | ? HL_ATTR(HLF_D) : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | vim_free(name); |
| 928 | ui_breakcheck(); |
| 929 | } |
| 930 | out_flush(); |
| 931 | } |
| 932 | if (curwin->w_jumplistidx == curwin->w_jumplistlen) |
| 933 | MSG_PUTS("\n>"); |
| 934 | } |
| 935 | |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 936 | void |
| 937 | ex_clearjumps(exarg_T *eap UNUSED) |
| 938 | { |
| 939 | free_jumplist(curwin); |
| 940 | curwin->w_jumplistlen = 0; |
| 941 | curwin->w_jumplistidx = 0; |
| 942 | } |
| 943 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 944 | /* |
| 945 | * print the changelist |
| 946 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 948 | ex_changes(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 949 | { |
| 950 | int i; |
| 951 | char_u *name; |
| 952 | |
| 953 | /* Highlight title */ |
| 954 | MSG_PUTS_TITLE(_("\nchange line col text")); |
| 955 | |
| 956 | for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i) |
| 957 | { |
| 958 | if (curbuf->b_changelist[i].lnum != 0) |
| 959 | { |
| 960 | msg_putchar('\n'); |
| 961 | if (got_int) |
| 962 | break; |
| 963 | sprintf((char *)IObuff, "%c %3d %5ld %4d ", |
| 964 | i == curwin->w_changelistidx ? '>' : ' ', |
| 965 | i > curwin->w_changelistidx ? i - curwin->w_changelistidx |
| 966 | : curwin->w_changelistidx - i, |
| 967 | (long)curbuf->b_changelist[i].lnum, |
| 968 | curbuf->b_changelist[i].col); |
| 969 | msg_outtrans(IObuff); |
| 970 | name = mark_line(&curbuf->b_changelist[i], 17); |
| 971 | if (name == NULL) |
| 972 | break; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 973 | msg_outtrans_attr(name, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | vim_free(name); |
| 975 | ui_breakcheck(); |
| 976 | } |
| 977 | out_flush(); |
| 978 | } |
| 979 | if (curwin->w_changelistidx == curbuf->b_changelistlen) |
| 980 | MSG_PUTS("\n>"); |
| 981 | } |
| 982 | #endif |
| 983 | |
| 984 | #define one_adjust(add) \ |
| 985 | { \ |
| 986 | lp = add; \ |
| 987 | if (*lp >= line1 && *lp <= line2) \ |
| 988 | { \ |
| 989 | if (amount == MAXLNUM) \ |
| 990 | *lp = 0; \ |
| 991 | else \ |
| 992 | *lp += amount; \ |
| 993 | } \ |
| 994 | else if (amount_after && *lp > line2) \ |
| 995 | *lp += amount_after; \ |
| 996 | } |
| 997 | |
| 998 | /* don't delete the line, just put at first deleted line */ |
| 999 | #define one_adjust_nodel(add) \ |
| 1000 | { \ |
| 1001 | lp = add; \ |
| 1002 | if (*lp >= line1 && *lp <= line2) \ |
| 1003 | { \ |
| 1004 | if (amount == MAXLNUM) \ |
| 1005 | *lp = line1; \ |
| 1006 | else \ |
| 1007 | *lp += amount; \ |
| 1008 | } \ |
| 1009 | else if (amount_after && *lp > line2) \ |
| 1010 | *lp += amount_after; \ |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines. |
| 1015 | * Must be called before changed_*(), appended_lines() or deleted_lines(). |
| 1016 | * May be called before or after changing the text. |
| 1017 | * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks |
| 1018 | * within this range are made invalid. |
| 1019 | * If 'amount_after' is non-zero adjust marks after line2. |
| 1020 | * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2); |
| 1021 | * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0); |
| 1022 | * or: mark_adjust(56, 55, MAXLNUM, 2); |
| 1023 | */ |
| 1024 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1025 | mark_adjust( |
| 1026 | linenr_T line1, |
| 1027 | linenr_T line2, |
| 1028 | long amount, |
| 1029 | long amount_after) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1030 | { |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 1031 | mark_adjust_internal(line1, line2, amount, amount_after, TRUE); |
| 1032 | } |
| 1033 | |
| 1034 | void |
| 1035 | mark_adjust_nofold( |
| 1036 | linenr_T line1, |
| 1037 | linenr_T line2, |
| 1038 | long amount, |
| 1039 | long amount_after) |
| 1040 | { |
| 1041 | mark_adjust_internal(line1, line2, amount, amount_after, FALSE); |
| 1042 | } |
| 1043 | |
| 1044 | static void |
| 1045 | mark_adjust_internal( |
| 1046 | linenr_T line1, |
| 1047 | linenr_T line2, |
| 1048 | long amount, |
| 1049 | long amount_after, |
| 1050 | int adjust_folds UNUSED) |
| 1051 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1052 | int i; |
| 1053 | int fnum = curbuf->b_fnum; |
| 1054 | linenr_T *lp; |
| 1055 | win_T *win; |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 1056 | tabpage_T *tab; |
Bram Moolenaar | b6a76ff | 2013-02-06 12:33:21 +0100 | [diff] [blame] | 1057 | static pos_T initpos = INIT_POS_T(1, 0, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1058 | |
| 1059 | if (line2 < line1 && amount_after == 0L) /* nothing to do */ |
| 1060 | return; |
| 1061 | |
| 1062 | if (!cmdmod.lockmarks) |
| 1063 | { |
| 1064 | /* named marks, lower case and upper case */ |
| 1065 | for (i = 0; i < NMARKS; i++) |
| 1066 | { |
| 1067 | one_adjust(&(curbuf->b_namedm[i].lnum)); |
| 1068 | if (namedfm[i].fmark.fnum == fnum) |
| 1069 | one_adjust_nodel(&(namedfm[i].fmark.mark.lnum)); |
| 1070 | } |
| 1071 | for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++) |
| 1072 | { |
| 1073 | if (namedfm[i].fmark.fnum == fnum) |
| 1074 | one_adjust_nodel(&(namedfm[i].fmark.mark.lnum)); |
| 1075 | } |
| 1076 | |
| 1077 | /* last Insert position */ |
| 1078 | one_adjust(&(curbuf->b_last_insert.lnum)); |
| 1079 | |
| 1080 | /* last change position */ |
| 1081 | one_adjust(&(curbuf->b_last_change.lnum)); |
| 1082 | |
Bram Moolenaar | b6a76ff | 2013-02-06 12:33:21 +0100 | [diff] [blame] | 1083 | /* last cursor position, if it was set */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1084 | if (!EQUAL_POS(curbuf->b_last_cursor, initpos)) |
Bram Moolenaar | b6a76ff | 2013-02-06 12:33:21 +0100 | [diff] [blame] | 1085 | one_adjust(&(curbuf->b_last_cursor.lnum)); |
| 1086 | |
| 1087 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | #ifdef FEAT_JUMPLIST |
| 1089 | /* list of change positions */ |
| 1090 | for (i = 0; i < curbuf->b_changelistlen; ++i) |
| 1091 | one_adjust_nodel(&(curbuf->b_changelist[i].lnum)); |
| 1092 | #endif |
| 1093 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1094 | /* Visual area */ |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 1095 | one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum)); |
| 1096 | one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1097 | |
| 1098 | #ifdef FEAT_QUICKFIX |
| 1099 | /* quickfix marks */ |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 1100 | qf_mark_adjust(NULL, line1, line2, amount, amount_after); |
| 1101 | /* location lists */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 1102 | FOR_ALL_TAB_WINDOWS(tab, win) |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 1103 | qf_mark_adjust(win, line1, line2, amount, amount_after); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1104 | #endif |
| 1105 | |
| 1106 | #ifdef FEAT_SIGNS |
| 1107 | sign_mark_adjust(line1, line2, amount, amount_after); |
| 1108 | #endif |
| 1109 | } |
| 1110 | |
| 1111 | /* previous context mark */ |
| 1112 | one_adjust(&(curwin->w_pcmark.lnum)); |
| 1113 | |
| 1114 | /* previous pcmark */ |
| 1115 | one_adjust(&(curwin->w_prev_pcmark.lnum)); |
| 1116 | |
| 1117 | /* saved cursor for formatting */ |
| 1118 | if (saved_cursor.lnum != 0) |
| 1119 | one_adjust_nodel(&(saved_cursor.lnum)); |
| 1120 | |
| 1121 | /* |
| 1122 | * Adjust items in all windows related to the current buffer. |
| 1123 | */ |
Bram Moolenaar | bd1e5d2 | 2009-04-29 09:02:44 +0000 | [diff] [blame] | 1124 | FOR_ALL_TAB_WINDOWS(tab, win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | { |
| 1126 | #ifdef FEAT_JUMPLIST |
| 1127 | if (!cmdmod.lockmarks) |
| 1128 | /* Marks in the jumplist. When deleting lines, this may create |
| 1129 | * duplicate marks in the jumplist, they will be removed later. */ |
| 1130 | for (i = 0; i < win->w_jumplistlen; ++i) |
| 1131 | if (win->w_jumplist[i].fmark.fnum == fnum) |
| 1132 | one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum)); |
| 1133 | #endif |
| 1134 | |
| 1135 | if (win->w_buffer == curbuf) |
| 1136 | { |
| 1137 | if (!cmdmod.lockmarks) |
| 1138 | /* marks in the tag stack */ |
| 1139 | for (i = 0; i < win->w_tagstacklen; i++) |
| 1140 | if (win->w_tagstack[i].fmark.fnum == fnum) |
| 1141 | one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum)); |
| 1142 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1143 | /* the displayed Visual area */ |
| 1144 | if (win->w_old_cursor_lnum != 0) |
| 1145 | { |
| 1146 | one_adjust_nodel(&(win->w_old_cursor_lnum)); |
| 1147 | one_adjust_nodel(&(win->w_old_visual_lnum)); |
| 1148 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1149 | |
| 1150 | /* topline and cursor position for windows with the same buffer |
| 1151 | * other than the current window */ |
| 1152 | if (win != curwin) |
| 1153 | { |
| 1154 | if (win->w_topline >= line1 && win->w_topline <= line2) |
| 1155 | { |
| 1156 | if (amount == MAXLNUM) /* topline is deleted */ |
| 1157 | { |
| 1158 | if (line1 <= 1) |
| 1159 | win->w_topline = 1; |
| 1160 | else |
| 1161 | win->w_topline = line1 - 1; |
| 1162 | } |
| 1163 | else /* keep topline on the same line */ |
| 1164 | win->w_topline += amount; |
| 1165 | #ifdef FEAT_DIFF |
| 1166 | win->w_topfill = 0; |
| 1167 | #endif |
| 1168 | } |
| 1169 | else if (amount_after && win->w_topline > line2) |
| 1170 | { |
| 1171 | win->w_topline += amount_after; |
| 1172 | #ifdef FEAT_DIFF |
| 1173 | win->w_topfill = 0; |
| 1174 | #endif |
| 1175 | } |
| 1176 | if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2) |
| 1177 | { |
| 1178 | if (amount == MAXLNUM) /* line with cursor is deleted */ |
| 1179 | { |
| 1180 | if (line1 <= 1) |
| 1181 | win->w_cursor.lnum = 1; |
| 1182 | else |
| 1183 | win->w_cursor.lnum = line1 - 1; |
| 1184 | win->w_cursor.col = 0; |
| 1185 | } |
| 1186 | else /* keep cursor on the same line */ |
| 1187 | win->w_cursor.lnum += amount; |
| 1188 | } |
| 1189 | else if (amount_after && win->w_cursor.lnum > line2) |
| 1190 | win->w_cursor.lnum += amount_after; |
| 1191 | } |
| 1192 | |
| 1193 | #ifdef FEAT_FOLDING |
| 1194 | /* adjust folds */ |
Bram Moolenaar | 88d298a | 2017-03-14 21:53:58 +0100 | [diff] [blame] | 1195 | if (adjust_folds) |
| 1196 | foldMarkAdjust(win, line1, line2, amount, amount_after); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1197 | #endif |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | #ifdef FEAT_DIFF |
| 1202 | /* adjust diffs */ |
| 1203 | diff_mark_adjust(line1, line2, amount, amount_after); |
| 1204 | #endif |
| 1205 | } |
| 1206 | |
| 1207 | /* This code is used often, needs to be fast. */ |
| 1208 | #define col_adjust(pp) \ |
| 1209 | { \ |
| 1210 | posp = pp; \ |
| 1211 | if (posp->lnum == lnum && posp->col >= mincol) \ |
| 1212 | { \ |
| 1213 | posp->lnum += lnum_amount; \ |
| 1214 | if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \ |
| 1215 | posp->col = 0; \ |
| 1216 | else \ |
| 1217 | posp->col += col_amount; \ |
| 1218 | } \ |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Adjust marks in line "lnum" at column "mincol" and further: add |
| 1223 | * "lnum_amount" to the line number and add "col_amount" to the column |
| 1224 | * position. |
| 1225 | */ |
| 1226 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1227 | mark_col_adjust( |
| 1228 | linenr_T lnum, |
| 1229 | colnr_T mincol, |
| 1230 | long lnum_amount, |
| 1231 | long col_amount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | { |
| 1233 | int i; |
| 1234 | int fnum = curbuf->b_fnum; |
| 1235 | win_T *win; |
| 1236 | pos_T *posp; |
| 1237 | |
| 1238 | if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks) |
| 1239 | return; /* nothing to do */ |
| 1240 | |
| 1241 | /* named marks, lower case and upper case */ |
| 1242 | for (i = 0; i < NMARKS; i++) |
| 1243 | { |
| 1244 | col_adjust(&(curbuf->b_namedm[i])); |
| 1245 | if (namedfm[i].fmark.fnum == fnum) |
| 1246 | col_adjust(&(namedfm[i].fmark.mark)); |
| 1247 | } |
| 1248 | for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++) |
| 1249 | { |
| 1250 | if (namedfm[i].fmark.fnum == fnum) |
| 1251 | col_adjust(&(namedfm[i].fmark.mark)); |
| 1252 | } |
| 1253 | |
| 1254 | /* last Insert position */ |
| 1255 | col_adjust(&(curbuf->b_last_insert)); |
| 1256 | |
| 1257 | /* last change position */ |
| 1258 | col_adjust(&(curbuf->b_last_change)); |
| 1259 | |
| 1260 | #ifdef FEAT_JUMPLIST |
| 1261 | /* list of change positions */ |
| 1262 | for (i = 0; i < curbuf->b_changelistlen; ++i) |
| 1263 | col_adjust(&(curbuf->b_changelist[i])); |
| 1264 | #endif |
| 1265 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | /* Visual area */ |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 1267 | col_adjust(&(curbuf->b_visual.vi_start)); |
| 1268 | col_adjust(&(curbuf->b_visual.vi_end)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* previous context mark */ |
| 1271 | col_adjust(&(curwin->w_pcmark)); |
| 1272 | |
| 1273 | /* previous pcmark */ |
| 1274 | col_adjust(&(curwin->w_prev_pcmark)); |
| 1275 | |
| 1276 | /* saved cursor for formatting */ |
| 1277 | col_adjust(&saved_cursor); |
| 1278 | |
| 1279 | /* |
| 1280 | * Adjust items in all windows related to the current buffer. |
| 1281 | */ |
| 1282 | FOR_ALL_WINDOWS(win) |
| 1283 | { |
| 1284 | #ifdef FEAT_JUMPLIST |
| 1285 | /* marks in the jumplist */ |
| 1286 | for (i = 0; i < win->w_jumplistlen; ++i) |
| 1287 | if (win->w_jumplist[i].fmark.fnum == fnum) |
| 1288 | col_adjust(&(win->w_jumplist[i].fmark.mark)); |
| 1289 | #endif |
| 1290 | |
| 1291 | if (win->w_buffer == curbuf) |
| 1292 | { |
| 1293 | /* marks in the tag stack */ |
| 1294 | for (i = 0; i < win->w_tagstacklen; i++) |
| 1295 | if (win->w_tagstack[i].fmark.fnum == fnum) |
| 1296 | col_adjust(&(win->w_tagstack[i].fmark.mark)); |
| 1297 | |
| 1298 | /* cursor position for other windows with the same buffer */ |
| 1299 | if (win != curwin) |
| 1300 | col_adjust(&win->w_cursor); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | #ifdef FEAT_JUMPLIST |
| 1306 | /* |
| 1307 | * When deleting lines, this may create duplicate marks in the |
| 1308 | * jumplist. They will be removed here for the current window. |
| 1309 | */ |
| 1310 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1311 | cleanup_jumplist(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1312 | { |
| 1313 | int i; |
| 1314 | int from, to; |
| 1315 | |
| 1316 | to = 0; |
| 1317 | for (from = 0; from < curwin->w_jumplistlen; ++from) |
| 1318 | { |
| 1319 | if (curwin->w_jumplistidx == from) |
| 1320 | curwin->w_jumplistidx = to; |
| 1321 | for (i = from + 1; i < curwin->w_jumplistlen; ++i) |
| 1322 | if (curwin->w_jumplist[i].fmark.fnum |
| 1323 | == curwin->w_jumplist[from].fmark.fnum |
| 1324 | && curwin->w_jumplist[from].fmark.fnum != 0 |
| 1325 | && curwin->w_jumplist[i].fmark.mark.lnum |
| 1326 | == curwin->w_jumplist[from].fmark.mark.lnum) |
| 1327 | break; |
| 1328 | if (i >= curwin->w_jumplistlen) /* no duplicate */ |
| 1329 | curwin->w_jumplist[to++] = curwin->w_jumplist[from]; |
| 1330 | else |
| 1331 | vim_free(curwin->w_jumplist[from].fname); |
| 1332 | } |
| 1333 | if (curwin->w_jumplistidx == curwin->w_jumplistlen) |
| 1334 | curwin->w_jumplistidx = to; |
| 1335 | curwin->w_jumplistlen = to; |
| 1336 | } |
| 1337 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | /* |
| 1339 | * Copy the jumplist from window "from" to window "to". |
| 1340 | */ |
| 1341 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1342 | copy_jumplist(win_T *from, win_T *to) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1343 | { |
| 1344 | int i; |
| 1345 | |
| 1346 | for (i = 0; i < from->w_jumplistlen; ++i) |
| 1347 | { |
| 1348 | to->w_jumplist[i] = from->w_jumplist[i]; |
| 1349 | if (from->w_jumplist[i].fname != NULL) |
| 1350 | to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname); |
| 1351 | } |
| 1352 | to->w_jumplistlen = from->w_jumplistlen; |
| 1353 | to->w_jumplistidx = from->w_jumplistidx; |
| 1354 | } |
| 1355 | |
| 1356 | /* |
| 1357 | * Free items in the jumplist of window "wp". |
| 1358 | */ |
| 1359 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1360 | free_jumplist(win_T *wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | { |
| 1362 | int i; |
| 1363 | |
| 1364 | for (i = 0; i < wp->w_jumplistlen; ++i) |
| 1365 | vim_free(wp->w_jumplist[i].fname); |
| 1366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | #endif /* FEAT_JUMPLIST */ |
| 1368 | |
| 1369 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1370 | set_last_cursor(win_T *win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | { |
Bram Moolenaar | 9db1293 | 2013-11-03 00:20:52 +0100 | [diff] [blame] | 1372 | if (win->w_buffer != NULL) |
| 1373 | win->w_buffer->b_last_cursor = win->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1376 | #if defined(EXITFREE) || defined(PROTO) |
| 1377 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1378 | free_all_marks(void) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1379 | { |
| 1380 | int i; |
| 1381 | |
| 1382 | for (i = 0; i < NMARKS + EXTRA_MARKS; i++) |
| 1383 | if (namedfm[i].fmark.mark.lnum != 0) |
| 1384 | vim_free(namedfm[i].fname); |
| 1385 | } |
| 1386 | #endif |
| 1387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | #if defined(FEAT_VIMINFO) || defined(PROTO) |
| 1389 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1390 | read_viminfo_filemark(vir_T *virp, int force) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | { |
| 1392 | char_u *str; |
| 1393 | xfmark_T *fm; |
| 1394 | int i; |
| 1395 | |
| 1396 | /* We only get here if line[0] == '\'' or '-'. |
| 1397 | * Illegal mark names are ignored (for future expansion). */ |
| 1398 | str = virp->vir_line + 1; |
| 1399 | if ( |
| 1400 | #ifndef EBCDIC |
| 1401 | *str <= 127 && |
| 1402 | #endif |
| 1403 | ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str))) |
| 1404 | || (*virp->vir_line == '-' && *str == '\''))) |
| 1405 | { |
| 1406 | if (*str == '\'') |
| 1407 | { |
| 1408 | #ifdef FEAT_JUMPLIST |
| 1409 | /* If the jumplist isn't full insert fmark as oldest entry */ |
| 1410 | if (curwin->w_jumplistlen == JUMPLISTSIZE) |
| 1411 | fm = NULL; |
| 1412 | else |
| 1413 | { |
| 1414 | for (i = curwin->w_jumplistlen; i > 0; --i) |
| 1415 | curwin->w_jumplist[i] = curwin->w_jumplist[i - 1]; |
| 1416 | ++curwin->w_jumplistidx; |
| 1417 | ++curwin->w_jumplistlen; |
| 1418 | fm = &curwin->w_jumplist[0]; |
| 1419 | fm->fmark.mark.lnum = 0; |
| 1420 | fm->fname = NULL; |
| 1421 | } |
| 1422 | #else |
| 1423 | fm = NULL; |
| 1424 | #endif |
| 1425 | } |
| 1426 | else if (VIM_ISDIGIT(*str)) |
| 1427 | fm = &namedfm[*str - '0' + NMARKS]; |
| 1428 | else |
| 1429 | fm = &namedfm[*str - 'A']; |
| 1430 | if (fm != NULL && (fm->fmark.mark.lnum == 0 || force)) |
| 1431 | { |
| 1432 | str = skipwhite(str + 1); |
| 1433 | fm->fmark.mark.lnum = getdigits(&str); |
| 1434 | str = skipwhite(str); |
| 1435 | fm->fmark.mark.col = getdigits(&str); |
| 1436 | #ifdef FEAT_VIRTUALEDIT |
| 1437 | fm->fmark.mark.coladd = 0; |
| 1438 | #endif |
| 1439 | fm->fmark.fnum = 0; |
| 1440 | str = skipwhite(str); |
| 1441 | vim_free(fm->fname); |
| 1442 | fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line), |
| 1443 | FALSE); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1444 | fm->time_set = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd); |
| 1448 | } |
| 1449 | |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1450 | static xfmark_T *vi_namedfm = NULL; |
| 1451 | #ifdef FEAT_JUMPLIST |
| 1452 | static xfmark_T *vi_jumplist = NULL; |
| 1453 | static int vi_jumplist_len = 0; |
| 1454 | #endif |
| 1455 | |
| 1456 | /* |
| 1457 | * Prepare for reading viminfo marks when writing viminfo later. |
| 1458 | */ |
| 1459 | void |
| 1460 | prepare_viminfo_marks(void) |
| 1461 | { |
| 1462 | vi_namedfm = (xfmark_T *)alloc_clear((NMARKS + EXTRA_MARKS) |
| 1463 | * (int)sizeof(xfmark_T)); |
| 1464 | #ifdef FEAT_JUMPLIST |
| 1465 | vi_jumplist = (xfmark_T *)alloc_clear(JUMPLISTSIZE |
| 1466 | * (int)sizeof(xfmark_T)); |
| 1467 | vi_jumplist_len = 0; |
| 1468 | #endif |
| 1469 | } |
| 1470 | |
| 1471 | void |
| 1472 | finish_viminfo_marks(void) |
| 1473 | { |
| 1474 | int i; |
| 1475 | |
| 1476 | if (vi_namedfm != NULL) |
| 1477 | { |
| 1478 | for (i = 0; i < NMARKS + EXTRA_MARKS; ++i) |
| 1479 | vim_free(vi_namedfm[i].fname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1480 | VIM_CLEAR(vi_namedfm); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1481 | } |
| 1482 | #ifdef FEAT_JUMPLIST |
| 1483 | if (vi_jumplist != NULL) |
| 1484 | { |
| 1485 | for (i = 0; i < vi_jumplist_len; ++i) |
| 1486 | vim_free(vi_jumplist[i].fname); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1487 | VIM_CLEAR(vi_jumplist); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1488 | } |
| 1489 | #endif |
| 1490 | } |
| 1491 | |
| 1492 | /* |
| 1493 | * Accept a new style mark line from the viminfo, store it when it's new. |
| 1494 | */ |
| 1495 | void |
| 1496 | handle_viminfo_mark(garray_T *values, int force) |
| 1497 | { |
| 1498 | bval_T *vp = (bval_T *)values->ga_data; |
| 1499 | int name; |
| 1500 | linenr_T lnum; |
| 1501 | colnr_T col; |
| 1502 | time_t timestamp; |
| 1503 | xfmark_T *fm = NULL; |
| 1504 | |
| 1505 | /* Check the format: |
| 1506 | * |{bartype},{name},{lnum},{col},{timestamp},{filename} */ |
| 1507 | if (values->ga_len < 5 |
| 1508 | || vp[0].bv_type != BVAL_NR |
| 1509 | || vp[1].bv_type != BVAL_NR |
| 1510 | || vp[2].bv_type != BVAL_NR |
| 1511 | || vp[3].bv_type != BVAL_NR |
| 1512 | || vp[4].bv_type != BVAL_STRING) |
| 1513 | return; |
| 1514 | |
| 1515 | name = vp[0].bv_nr; |
| 1516 | if (name != '\'' && !VIM_ISDIGIT(name) && !ASCII_ISUPPER(name)) |
| 1517 | return; |
| 1518 | lnum = vp[1].bv_nr; |
| 1519 | col = vp[2].bv_nr; |
| 1520 | if (lnum <= 0 || col < 0) |
| 1521 | return; |
| 1522 | timestamp = (time_t)vp[3].bv_nr; |
| 1523 | |
| 1524 | if (name == '\'') |
| 1525 | { |
| 1526 | #ifdef FEAT_JUMPLIST |
| 1527 | if (vi_jumplist != NULL) |
| 1528 | { |
| 1529 | if (vi_jumplist_len < JUMPLISTSIZE) |
| 1530 | fm = &vi_jumplist[vi_jumplist_len++]; |
| 1531 | } |
| 1532 | else |
| 1533 | { |
| 1534 | int idx; |
| 1535 | int i; |
| 1536 | |
| 1537 | /* If we have a timestamp insert it in the right place. */ |
| 1538 | if (timestamp != 0) |
| 1539 | { |
| 1540 | for (idx = curwin->w_jumplistlen - 1; idx >= 0; --idx) |
| 1541 | if (curwin->w_jumplist[idx].time_set < timestamp) |
Bram Moolenaar | ece74ab | 2016-06-13 22:22:15 +0200 | [diff] [blame] | 1542 | { |
| 1543 | ++idx; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1544 | break; |
Bram Moolenaar | ece74ab | 2016-06-13 22:22:15 +0200 | [diff] [blame] | 1545 | } |
Bram Moolenaar | 678e480 | 2016-06-17 22:38:46 +0200 | [diff] [blame] | 1546 | /* idx cannot be zero now */ |
Bram Moolenaar | ece74ab | 2016-06-13 22:22:15 +0200 | [diff] [blame] | 1547 | if (idx < 0 && curwin->w_jumplistlen < JUMPLISTSIZE) |
| 1548 | /* insert as the oldest entry */ |
| 1549 | idx = 0; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1550 | } |
| 1551 | else if (curwin->w_jumplistlen < JUMPLISTSIZE) |
| 1552 | /* insert as oldest entry */ |
| 1553 | idx = 0; |
| 1554 | else |
| 1555 | idx = -1; |
| 1556 | |
| 1557 | if (idx >= 0) |
| 1558 | { |
| 1559 | if (curwin->w_jumplistlen == JUMPLISTSIZE) |
| 1560 | { |
| 1561 | /* Drop the oldest entry. */ |
Bram Moolenaar | 28607ba | 2016-06-15 21:44:51 +0200 | [diff] [blame] | 1562 | --idx; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1563 | vim_free(curwin->w_jumplist[0].fname); |
| 1564 | for (i = 0; i < idx; ++i) |
| 1565 | curwin->w_jumplist[i] = curwin->w_jumplist[i + 1]; |
| 1566 | } |
| 1567 | else |
| 1568 | { |
| 1569 | /* Move newer entries forward. */ |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1570 | for (i = curwin->w_jumplistlen; i > idx; --i) |
| 1571 | curwin->w_jumplist[i] = curwin->w_jumplist[i - 1]; |
| 1572 | ++curwin->w_jumplistidx; |
| 1573 | ++curwin->w_jumplistlen; |
| 1574 | } |
| 1575 | fm = &curwin->w_jumplist[idx]; |
| 1576 | fm->fmark.mark.lnum = 0; |
| 1577 | fm->fname = NULL; |
| 1578 | fm->time_set = 0; |
| 1579 | } |
| 1580 | } |
| 1581 | #endif |
| 1582 | } |
| 1583 | else |
| 1584 | { |
| 1585 | int idx; |
| 1586 | |
| 1587 | if (VIM_ISDIGIT(name)) |
| 1588 | { |
| 1589 | if (vi_namedfm != NULL) |
| 1590 | idx = name - '0' + NMARKS; |
| 1591 | else |
| 1592 | { |
| 1593 | int i; |
| 1594 | |
| 1595 | /* Do not use the name from the viminfo file, insert in time |
| 1596 | * order. */ |
| 1597 | for (idx = NMARKS; idx < NMARKS + EXTRA_MARKS; ++idx) |
| 1598 | if (namedfm[idx].time_set < timestamp) |
| 1599 | break; |
| 1600 | if (idx == NMARKS + EXTRA_MARKS) |
| 1601 | /* All existing entries are newer. */ |
| 1602 | return; |
| 1603 | i = NMARKS + EXTRA_MARKS - 1; |
| 1604 | |
| 1605 | vim_free(namedfm[i].fname); |
| 1606 | for ( ; i > idx; --i) |
| 1607 | namedfm[i] = namedfm[i - 1]; |
| 1608 | namedfm[idx].fname = NULL; |
| 1609 | } |
| 1610 | } |
| 1611 | else |
| 1612 | idx = name - 'A'; |
| 1613 | if (vi_namedfm != NULL) |
| 1614 | fm = &vi_namedfm[idx]; |
| 1615 | else |
| 1616 | fm = &namedfm[idx]; |
| 1617 | } |
| 1618 | |
| 1619 | if (fm != NULL) |
| 1620 | { |
Bram Moolenaar | 156919f | 2016-10-15 20:46:20 +0200 | [diff] [blame] | 1621 | if (vi_namedfm != NULL || fm->fmark.mark.lnum == 0 |
| 1622 | || fm->time_set < timestamp || force) |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1623 | { |
| 1624 | fm->fmark.mark.lnum = lnum; |
| 1625 | fm->fmark.mark.col = col; |
| 1626 | #ifdef FEAT_VIRTUALEDIT |
| 1627 | fm->fmark.mark.coladd = 0; |
| 1628 | #endif |
| 1629 | fm->fmark.fnum = 0; |
| 1630 | vim_free(fm->fname); |
| 1631 | if (vp[4].bv_allocated) |
| 1632 | { |
| 1633 | fm->fname = vp[4].bv_string; |
| 1634 | vp[4].bv_string = NULL; |
| 1635 | } |
| 1636 | else |
| 1637 | fm->fname = vim_strsave(vp[4].bv_string); |
| 1638 | fm->time_set = timestamp; |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
Bram Moolenaar | e627805 | 2017-08-13 18:11:17 +0200 | [diff] [blame] | 1643 | /* |
| 1644 | * Return TRUE if marks for "buf" should not be written. |
| 1645 | */ |
| 1646 | static int |
| 1647 | skip_for_viminfo(buf_T *buf) |
| 1648 | { |
| 1649 | return |
| 1650 | #ifdef FEAT_TERMINAL |
| 1651 | bt_terminal(buf) || |
| 1652 | #endif |
| 1653 | removable(buf->b_ffname); |
| 1654 | } |
| 1655 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1656 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1657 | write_viminfo_filemarks(FILE *fp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | { |
| 1659 | int i; |
| 1660 | char_u *name; |
| 1661 | buf_T *buf; |
| 1662 | xfmark_T *fm; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1663 | int vi_idx; |
| 1664 | int idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (get_viminfo_parameter('f') == 0) |
| 1667 | return; |
| 1668 | |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 1669 | fputs(_("\n# File marks:\n"), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1670 | |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1671 | /* Write the filemarks 'A - 'Z */ |
| 1672 | for (i = 0; i < NMARKS; i++) |
| 1673 | { |
| 1674 | if (vi_namedfm != NULL && (vi_namedfm[i].time_set > namedfm[i].time_set |
| 1675 | || namedfm[i].fmark.mark.lnum == 0)) |
| 1676 | fm = &vi_namedfm[i]; |
| 1677 | else |
| 1678 | fm = &namedfm[i]; |
| 1679 | write_one_filemark(fp, fm, '\'', i + 'A'); |
| 1680 | } |
| 1681 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1682 | /* |
| 1683 | * Find a mark that is the same file and position as the cursor. |
| 1684 | * That one, or else the last one is deleted. |
| 1685 | * Move '0 to '1, '1 to '2, etc. until the matching one or '9 |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1686 | * Set the '0 mark to current cursor position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | */ |
Bram Moolenaar | e627805 | 2017-08-13 18:11:17 +0200 | [diff] [blame] | 1688 | if (curbuf->b_ffname != NULL && !skip_for_viminfo(curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | { |
| 1690 | name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE); |
| 1691 | for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i) |
| 1692 | if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum |
| 1693 | && (namedfm[i].fname == NULL |
| 1694 | ? namedfm[i].fmark.fnum == curbuf->b_fnum |
| 1695 | : (name != NULL |
| 1696 | && STRCMP(name, namedfm[i].fname) == 0))) |
| 1697 | break; |
| 1698 | vim_free(name); |
| 1699 | |
| 1700 | vim_free(namedfm[i].fname); |
| 1701 | for ( ; i > NMARKS; --i) |
| 1702 | namedfm[i] = namedfm[i - 1]; |
| 1703 | namedfm[NMARKS].fmark.mark = curwin->w_cursor; |
| 1704 | namedfm[NMARKS].fmark.fnum = curbuf->b_fnum; |
| 1705 | namedfm[NMARKS].fname = NULL; |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1706 | namedfm[NMARKS].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1709 | /* Write the filemarks '0 - '9. Newest (highest timestamp) first. */ |
| 1710 | vi_idx = NMARKS; |
| 1711 | idx = NMARKS; |
| 1712 | for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++) |
| 1713 | { |
Bram Moolenaar | 36f0f06 | 2016-06-14 23:02:46 +0200 | [diff] [blame] | 1714 | xfmark_T *vi_fm = vi_namedfm != NULL ? &vi_namedfm[vi_idx] : NULL; |
| 1715 | |
| 1716 | if (vi_fm != NULL |
| 1717 | && vi_fm->fmark.mark.lnum != 0 |
| 1718 | && (vi_fm->time_set > namedfm[idx].time_set |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1719 | || namedfm[idx].fmark.mark.lnum == 0)) |
Bram Moolenaar | 36f0f06 | 2016-06-14 23:02:46 +0200 | [diff] [blame] | 1720 | { |
| 1721 | fm = vi_fm; |
| 1722 | ++vi_idx; |
| 1723 | } |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1724 | else |
Bram Moolenaar | 36f0f06 | 2016-06-14 23:02:46 +0200 | [diff] [blame] | 1725 | { |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1726 | fm = &namedfm[idx++]; |
Bram Moolenaar | 36f0f06 | 2016-06-14 23:02:46 +0200 | [diff] [blame] | 1727 | if (vi_fm != NULL |
| 1728 | && vi_fm->fmark.mark.lnum == fm->fmark.mark.lnum |
| 1729 | && vi_fm->time_set == fm->time_set |
| 1730 | && ((vi_fm->fmark.fnum != 0 |
| 1731 | && vi_fm->fmark.fnum == fm->fmark.fnum) |
| 1732 | || (vi_fm->fname != NULL |
| 1733 | && fm->fname != NULL |
| 1734 | && STRCMP(vi_fm->fname, fm->fname) == 0))) |
| 1735 | ++vi_idx; /* skip duplicate */ |
| 1736 | } |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1737 | write_one_filemark(fp, fm, '\'', i - NMARKS + '0'); |
| 1738 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | |
| 1740 | #ifdef FEAT_JUMPLIST |
| 1741 | /* Write the jumplist with -' */ |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 1742 | fputs(_("\n# Jumplist (newest first):\n"), fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1743 | setpcmark(); /* add current cursor position */ |
| 1744 | cleanup_jumplist(); |
Bram Moolenaar | ece74ab | 2016-06-13 22:22:15 +0200 | [diff] [blame] | 1745 | vi_idx = 0; |
| 1746 | idx = curwin->w_jumplistlen - 1; |
| 1747 | for (i = 0; i < JUMPLISTSIZE; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | { |
Bram Moolenaar | ece74ab | 2016-06-13 22:22:15 +0200 | [diff] [blame] | 1749 | xfmark_T *vi_fm; |
| 1750 | |
| 1751 | fm = idx >= 0 ? &curwin->w_jumplist[idx] : NULL; |
| 1752 | vi_fm = vi_idx < vi_jumplist_len ? &vi_jumplist[vi_idx] : NULL; |
| 1753 | if (fm == NULL && vi_fm == NULL) |
| 1754 | break; |
| 1755 | if (fm == NULL || (vi_fm != NULL && fm->time_set < vi_fm->time_set)) |
| 1756 | { |
| 1757 | fm = vi_fm; |
| 1758 | ++vi_idx; |
| 1759 | } |
| 1760 | else |
| 1761 | --idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1762 | if (fm->fmark.fnum == 0 |
| 1763 | || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL |
Bram Moolenaar | e627805 | 2017-08-13 18:11:17 +0200 | [diff] [blame] | 1764 | && !skip_for_viminfo(buf))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1765 | write_one_filemark(fp, fm, '-', '\''); |
| 1766 | } |
| 1767 | #endif |
| 1768 | } |
| 1769 | |
| 1770 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1771 | write_one_filemark( |
| 1772 | FILE *fp, |
| 1773 | xfmark_T *fm, |
| 1774 | int c1, |
| 1775 | int c2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | { |
| 1777 | char_u *name; |
| 1778 | |
| 1779 | if (fm->fmark.mark.lnum == 0) /* not set */ |
| 1780 | return; |
| 1781 | |
| 1782 | if (fm->fmark.fnum != 0) /* there is a buffer */ |
| 1783 | name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE); |
| 1784 | else |
| 1785 | name = fm->fname; /* use name from .viminfo */ |
| 1786 | if (name != NULL && *name != NUL) |
| 1787 | { |
| 1788 | fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum, |
| 1789 | (long)fm->fmark.mark.col); |
| 1790 | viminfo_writestring(fp, name); |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* Barline: |{bartype},{name},{lnum},{col},{timestamp},{filename} |
| 1793 | * size up to filename: 8 + 3 * 20 */ |
| 1794 | fprintf(fp, "|%d,%d,%ld,%ld,%ld,", BARTYPE_MARK, c2, |
| 1795 | (long)fm->fmark.mark.lnum, (long)fm->fmark.mark.col, |
| 1796 | (long)fm->time_set); |
| 1797 | barline_writestring(fp, name, LSIZE - 70); |
| 1798 | putc('\n', fp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | if (fm->fmark.fnum != 0) |
| 1802 | vim_free(name); |
| 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * Return TRUE if "name" is on removable media (depending on 'viminfo'). |
| 1807 | */ |
| 1808 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1809 | removable(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | { |
| 1811 | char_u *p; |
| 1812 | char_u part[51]; |
| 1813 | int retval = FALSE; |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1814 | size_t n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1815 | |
| 1816 | name = home_replace_save(NULL, name); |
| 1817 | if (name != NULL) |
| 1818 | { |
| 1819 | for (p = p_viminfo; *p; ) |
| 1820 | { |
| 1821 | copy_option_part(&p, part, 51, ", "); |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1822 | if (part[0] == 'r') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1823 | { |
Bram Moolenaar | cfc6c43 | 2005-06-06 21:50:35 +0000 | [diff] [blame] | 1824 | n = STRLEN(part + 1); |
| 1825 | if (MB_STRNICMP(part + 1, name, n) == 0) |
| 1826 | { |
| 1827 | retval = TRUE; |
| 1828 | break; |
| 1829 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | vim_free(name); |
| 1833 | } |
| 1834 | return retval; |
| 1835 | } |
| 1836 | |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1837 | static void |
| 1838 | write_one_mark(FILE *fp_out, int c, pos_T *pos) |
| 1839 | { |
| 1840 | if (pos->lnum != 0) |
| 1841 | fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col); |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | static void |
| 1846 | write_buffer_marks(buf_T *buf, FILE *fp_out) |
| 1847 | { |
| 1848 | int i; |
| 1849 | pos_T pos; |
| 1850 | |
| 1851 | home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE); |
| 1852 | fprintf(fp_out, "\n> "); |
| 1853 | viminfo_writestring(fp_out, IObuff); |
| 1854 | |
| 1855 | /* Write the last used timestamp as the lnum of the non-existing mark '*'. |
| 1856 | * Older Vims will ignore it and/or copy it. */ |
| 1857 | pos.lnum = (linenr_T)buf->b_last_used; |
| 1858 | pos.col = 0; |
| 1859 | write_one_mark(fp_out, '*', &pos); |
| 1860 | |
| 1861 | write_one_mark(fp_out, '"', &buf->b_last_cursor); |
| 1862 | write_one_mark(fp_out, '^', &buf->b_last_insert); |
| 1863 | write_one_mark(fp_out, '.', &buf->b_last_change); |
| 1864 | #ifdef FEAT_JUMPLIST |
| 1865 | /* changelist positions are stored oldest first */ |
| 1866 | for (i = 0; i < buf->b_changelistlen; ++i) |
| 1867 | { |
| 1868 | /* skip duplicates */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1869 | if (i == 0 || !EQUAL_POS(buf->b_changelist[i - 1], |
| 1870 | buf->b_changelist[i])) |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1871 | write_one_mark(fp_out, '+', &buf->b_changelist[i]); |
| 1872 | } |
| 1873 | #endif |
| 1874 | for (i = 0; i < NMARKS; i++) |
| 1875 | write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]); |
| 1876 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* |
| 1879 | * Write all the named marks for all buffers. |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1880 | * When "buflist" is not NULL fill it with the buffers for which marks are to |
| 1881 | * be written. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | */ |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1883 | void |
| 1884 | write_viminfo_marks(FILE *fp_out, garray_T *buflist) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1885 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1886 | buf_T *buf; |
| 1887 | int is_mark_set; |
| 1888 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1889 | win_T *win; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1890 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1891 | |
| 1892 | /* |
| 1893 | * Set b_last_cursor for the all buffers that have a window. |
| 1894 | */ |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1895 | FOR_ALL_TAB_WINDOWS(tp, win) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1896 | set_last_cursor(win); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1897 | |
Bram Moolenaar | 2f1e050 | 2010-08-13 11:18:02 +0200 | [diff] [blame] | 1898 | fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1899 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1900 | { |
| 1901 | /* |
| 1902 | * Only write something if buffer has been loaded and at least one |
| 1903 | * mark is set. |
| 1904 | */ |
| 1905 | if (buf->b_marks_read) |
| 1906 | { |
| 1907 | if (buf->b_last_cursor.lnum != 0) |
| 1908 | is_mark_set = TRUE; |
| 1909 | else |
| 1910 | { |
| 1911 | is_mark_set = FALSE; |
| 1912 | for (i = 0; i < NMARKS; i++) |
| 1913 | if (buf->b_namedm[i].lnum != 0) |
| 1914 | { |
| 1915 | is_mark_set = TRUE; |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | if (is_mark_set && buf->b_ffname != NULL |
Bram Moolenaar | e627805 | 2017-08-13 18:11:17 +0200 | [diff] [blame] | 1920 | && buf->b_ffname[0] != NUL |
| 1921 | && !skip_for_viminfo(buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | { |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1923 | if (buflist == NULL) |
| 1924 | write_buffer_marks(buf, fp_out); |
| 1925 | else if (ga_grow(buflist, 1) == OK) |
| 1926 | ((buf_T **)buflist->ga_data)[buflist->ga_len++] = buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1932 | /* |
| 1933 | * Compare functions for qsort() below, that compares b_last_used. |
| 1934 | */ |
| 1935 | static int |
| 1936 | #ifdef __BORLANDC__ |
| 1937 | _RTLENTRYF |
| 1938 | #endif |
| 1939 | buf_compare(const void *s1, const void *s2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1940 | { |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1941 | buf_T *buf1 = *(buf_T **)s1; |
| 1942 | buf_T *buf2 = *(buf_T **)s2; |
| 1943 | |
| 1944 | if (buf1->b_last_used == buf2->b_last_used) |
| 1945 | return 0; |
| 1946 | return buf1->b_last_used > buf2->b_last_used ? -1 : 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * Handle marks in the viminfo file: |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1951 | * fp_out != NULL: copy marks, in time order with buffers in "buflist". |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1952 | * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf only |
| 1953 | * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1954 | */ |
| 1955 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1956 | copy_viminfo_marks( |
| 1957 | vir_T *virp, |
| 1958 | FILE *fp_out, |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1959 | garray_T *buflist, |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1960 | int eof, |
| 1961 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1962 | { |
| 1963 | char_u *line = virp->vir_line; |
| 1964 | buf_T *buf; |
| 1965 | int num_marked_files; |
| 1966 | int load_marks; |
| 1967 | int copy_marks_out; |
| 1968 | char_u *str; |
| 1969 | int i; |
| 1970 | char_u *p; |
| 1971 | char_u *name_buf; |
| 1972 | pos_T pos; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1973 | #ifdef FEAT_EVAL |
| 1974 | list_T *list = NULL; |
| 1975 | #endif |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1976 | int count = 0; |
| 1977 | int buflist_used = 0; |
| 1978 | buf_T *buflist_buf = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | |
| 1980 | if ((name_buf = alloc(LSIZE)) == NULL) |
| 1981 | return; |
| 1982 | *name_buf = NUL; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1983 | |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1984 | if (fp_out != NULL && buflist->ga_len > 0) |
| 1985 | { |
| 1986 | /* Sort the list of buffers on b_last_used. */ |
| 1987 | qsort(buflist->ga_data, (size_t)buflist->ga_len, |
| 1988 | sizeof(buf_T *), buf_compare); |
| 1989 | buflist_buf = ((buf_T **)buflist->ga_data)[0]; |
| 1990 | } |
| 1991 | |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 1992 | #ifdef FEAT_EVAL |
| 1993 | if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT))) |
| 1994 | { |
| 1995 | list = list_alloc(); |
| 1996 | if (list != NULL) |
| 1997 | set_vim_var_list(VV_OLDFILES, list); |
| 1998 | } |
| 1999 | #endif |
| 2000 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | num_marked_files = get_viminfo_parameter('\''); |
| 2002 | while (!eof && (count < num_marked_files || fp_out == NULL)) |
| 2003 | { |
| 2004 | if (line[0] != '>') |
| 2005 | { |
| 2006 | if (line[0] != '\n' && line[0] != '\r' && line[0] != '#') |
| 2007 | { |
| 2008 | if (viminfo_error("E576: ", _("Missing '>'"), line)) |
| 2009 | break; /* too many errors, return now */ |
| 2010 | } |
| 2011 | eof = vim_fgets(line, LSIZE, virp->vir_fd); |
| 2012 | continue; /* Skip this dud line */ |
| 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * Handle long line and translate escaped characters. |
| 2017 | * Find file name, set str to start. |
| 2018 | * Ignore leading and trailing white space. |
| 2019 | */ |
| 2020 | str = skipwhite(line + 1); |
| 2021 | str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE); |
| 2022 | if (str == NULL) |
| 2023 | continue; |
| 2024 | p = str + STRLEN(str); |
| 2025 | while (p != str && (*p == NUL || vim_isspace(*p))) |
| 2026 | p--; |
| 2027 | if (*p) |
| 2028 | p++; |
| 2029 | *p = NUL; |
| 2030 | |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2031 | #ifdef FEAT_EVAL |
| 2032 | if (list != NULL) |
| 2033 | list_append_string(list, str, -1); |
| 2034 | #endif |
| 2035 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | /* |
| 2037 | * If fp_out == NULL, load marks for current buffer. |
| 2038 | * If fp_out != NULL, copy marks for buffers not in buflist. |
| 2039 | */ |
| 2040 | load_marks = copy_marks_out = FALSE; |
| 2041 | if (fp_out == NULL) |
| 2042 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 2043 | if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | { |
| 2045 | if (*name_buf == NUL) /* only need to do this once */ |
| 2046 | home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE); |
| 2047 | if (fnamecmp(str, name_buf) == 0) |
| 2048 | load_marks = TRUE; |
| 2049 | } |
| 2050 | } |
| 2051 | else /* fp_out != NULL */ |
| 2052 | { |
| 2053 | /* This is slow if there are many buffers!! */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2054 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2055 | if (buf->b_ffname != NULL) |
| 2056 | { |
| 2057 | home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE); |
| 2058 | if (fnamecmp(str, name_buf) == 0) |
| 2059 | break; |
| 2060 | } |
| 2061 | |
| 2062 | /* |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2063 | * Copy marks if the buffer has not been loaded. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | */ |
| 2065 | if (buf == NULL || !buf->b_marks_read) |
| 2066 | { |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2067 | int did_read_line = FALSE; |
| 2068 | |
| 2069 | if (buflist_buf != NULL) |
| 2070 | { |
| 2071 | /* Read the next line. If it has the "*" mark compare the |
| 2072 | * time stamps. Write entries from "buflist" that are |
| 2073 | * newer. */ |
| 2074 | if (!(eof = viminfo_readline(virp)) && line[0] == TAB) |
| 2075 | { |
| 2076 | did_read_line = TRUE; |
| 2077 | if (line[1] == '*') |
| 2078 | { |
| 2079 | long ltime; |
| 2080 | |
| 2081 | sscanf((char *)line + 2, "%ld ", <ime); |
| 2082 | while ((time_T)ltime < buflist_buf->b_last_used) |
| 2083 | { |
| 2084 | write_buffer_marks(buflist_buf, fp_out); |
| 2085 | if (++count >= num_marked_files) |
| 2086 | break; |
| 2087 | if (++buflist_used == buflist->ga_len) |
| 2088 | { |
| 2089 | buflist_buf = NULL; |
| 2090 | break; |
| 2091 | } |
| 2092 | buflist_buf = |
| 2093 | ((buf_T **)buflist->ga_data)[buflist_used]; |
| 2094 | } |
| 2095 | } |
| 2096 | else |
| 2097 | { |
| 2098 | /* No timestamp, must be written by an older Vim. |
| 2099 | * Assume all remaining buffers are older then |
| 2100 | * ours. */ |
| 2101 | while (count < num_marked_files |
| 2102 | && buflist_used < buflist->ga_len) |
| 2103 | { |
| 2104 | buflist_buf = ((buf_T **)buflist->ga_data) |
| 2105 | [buflist_used++]; |
| 2106 | write_buffer_marks(buflist_buf, fp_out); |
| 2107 | ++count; |
| 2108 | } |
| 2109 | buflist_buf = NULL; |
| 2110 | } |
| 2111 | |
| 2112 | if (count >= num_marked_files) |
| 2113 | { |
| 2114 | vim_free(str); |
| 2115 | break; |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2120 | fputs("\n> ", fp_out); |
| 2121 | viminfo_writestring(fp_out, str); |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2122 | if (did_read_line) |
| 2123 | fputs((char *)line, fp_out); |
| 2124 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2125 | count++; |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2126 | copy_marks_out = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | vim_free(str); |
| 2130 | |
| 2131 | #ifdef FEAT_VIRTUALEDIT |
| 2132 | pos.coladd = 0; |
| 2133 | #endif |
| 2134 | while (!(eof = viminfo_readline(virp)) && line[0] == TAB) |
| 2135 | { |
| 2136 | if (load_marks) |
| 2137 | { |
| 2138 | if (line[1] != NUL) |
| 2139 | { |
Bram Moolenaar | e698add | 2011-02-25 15:11:22 +0100 | [diff] [blame] | 2140 | unsigned u; |
| 2141 | |
| 2142 | sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u); |
| 2143 | pos.col = u; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2144 | switch (line[1]) |
| 2145 | { |
| 2146 | case '"': curbuf->b_last_cursor = pos; break; |
| 2147 | case '^': curbuf->b_last_insert = pos; break; |
| 2148 | case '.': curbuf->b_last_change = pos; break; |
| 2149 | case '+': |
| 2150 | #ifdef FEAT_JUMPLIST |
| 2151 | /* changelist positions are stored oldest |
| 2152 | * first */ |
| 2153 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 2154 | /* list is full, remove oldest entry */ |
| 2155 | mch_memmove(curbuf->b_changelist, |
| 2156 | curbuf->b_changelist + 1, |
| 2157 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
| 2158 | else |
| 2159 | ++curbuf->b_changelistlen; |
| 2160 | curbuf->b_changelist[ |
| 2161 | curbuf->b_changelistlen - 1] = pos; |
| 2162 | #endif |
| 2163 | break; |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2164 | |
| 2165 | /* Using the line number for the last-used |
| 2166 | * timestamp. */ |
| 2167 | case '*': curbuf->b_last_used = pos.lnum; break; |
| 2168 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2169 | default: if ((i = line[1] - 'a') >= 0 && i < NMARKS) |
| 2170 | curbuf->b_namedm[i] = pos; |
| 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | else if (copy_marks_out) |
| 2175 | fputs((char *)line, fp_out); |
| 2176 | } |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2177 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2178 | if (load_marks) |
| 2179 | { |
| 2180 | #ifdef FEAT_JUMPLIST |
| 2181 | win_T *wp; |
| 2182 | |
| 2183 | FOR_ALL_WINDOWS(wp) |
| 2184 | { |
| 2185 | if (wp->w_buffer == curbuf) |
| 2186 | wp->w_changelistidx = curbuf->b_changelistlen; |
| 2187 | } |
| 2188 | #endif |
| 2189 | break; |
| 2190 | } |
| 2191 | } |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 2192 | |
| 2193 | if (fp_out != NULL) |
| 2194 | /* Write any remaining entries from buflist. */ |
| 2195 | while (count < num_marked_files && buflist_used < buflist->ga_len) |
| 2196 | { |
| 2197 | buflist_buf = ((buf_T **)buflist->ga_data)[buflist_used++]; |
| 2198 | write_buffer_marks(buflist_buf, fp_out); |
| 2199 | ++count; |
| 2200 | } |
| 2201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | vim_free(name_buf); |
| 2203 | } |
| 2204 | #endif /* FEAT_VIMINFO */ |