blob: 1acdd12d65b97b5fde0b87a806d96b055a083f3b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 */
28static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */
29
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010030static void fname2fnum(xfmark_T *fm);
31static void fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf);
32static char_u *mark_line(pos_T *mp, int lead_len);
33static void show_one_mark(int, char_u *, pos_T *, char_u *, int current);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef FEAT_JUMPLIST
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void cleanup_jumplist(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#endif
37#ifdef FEAT_VIMINFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void write_one_filemark(FILE *fp, xfmark_T *fm, int c1, int c2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaar88d298a2017-03-14 21:53:58 +010040static void mark_adjust_internal(linenr_T line1, linenr_T line2, long amount,
41 long amount_after, int adjust_folds);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43/*
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000044 * Set named mark "c" at current cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 * Returns OK on success, FAIL if bad name given.
46 */
47 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010048setmark(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000049{
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000050 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 Moolenaar52ea13d2016-01-30 18:51:09 +010059setmark_pos(int c, pos_T *pos, int fnum)
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000060{
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 int i;
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010062 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
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 Moolenaarbfb2d402006-03-03 22:50:42 +000070 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 Moolenaar071d4272004-06-13 20:20:40 +000078 return OK;
79 }
80
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010081 buf = buflist_findnr(fnum);
82 if (buf == NULL)
83 return FAIL;
84
Bram Moolenaar08250432008-02-13 11:42:46 +000085 if (c == '"')
86 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010087 buf->b_last_cursor = *pos;
Bram Moolenaar08250432008-02-13 11:42:46 +000088 return OK;
89 }
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 /* Allow setting '[ and '] for an autocommand that simulates reading a
92 * file. */
93 if (c == '[')
94 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010095 buf->b_op_start = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 return OK;
97 }
98 if (c == ']')
99 {
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100100 buf->b_op_end = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 return OK;
102 }
103
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200104 if (c == '<' || c == '>')
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200105 {
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200106 if (c == '<')
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100107 buf->b_visual.vi_start = *pos;
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200108 else
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100109 buf->b_visual.vi_end = *pos;
110 if (buf->b_visual.vi_mode == NUL)
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200111 /* Visual_mode has not yet been set, use a sane default. */
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100112 buf->b_visual.vi_mode = 'v';
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200113 return OK;
114 }
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200115
Bram Moolenaar2d358992016-06-12 21:20:54 +0200116 if (ASCII_ISLOWER(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 {
118 i = c - 'a';
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100119 buf->b_namedm[i] = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 return OK;
121 }
Bram Moolenaar2d358992016-06-12 21:20:54 +0200122 if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 {
Bram Moolenaar2d358992016-06-12 21:20:54 +0200124 if (VIM_ISDIGIT(c))
125 i = c - '0' + NMARKS;
126 else
127 i = c - 'A';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000128 namedfm[i].fmark.mark = *pos;
129 namedfm[i].fmark.fnum = fnum;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100130 VIM_CLEAR(namedfm[i].fname);
Bram Moolenaar2d358992016-06-12 21:20:54 +0200131#ifdef FEAT_VIMINFO
132 namedfm[i].time_set = vim_time();
133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100144setpcmark(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145{
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 Moolenaar2d358992016-06-12 21:20:54 +0200194# ifdef FEAT_VIMINFO
195 fm->time_set = vim_time();
196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197#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 Moolenaar52ea13d2016-01-30 18:51:09 +0100207checkpcmark(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
209 if (curwin->w_prev_pcmark.lnum != 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100210 && (EQUAL_POS(curwin->w_pcmark, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 || 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100223movemark(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
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 Moolenaarf711faf2007-05-10 16:48:19 +0000241 * to list. Careful: If there are duplicates (CTRL-O immediately after
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 * 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100282movechangelist(int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283{
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 Moolenaar9d182dd2013-01-23 15:53:15 +0100310 * Find mark "c" in buffer pointed to by "buf".
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000311 * 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 Moolenaar071d4272004-06-13 20:20:40 +0000314 * 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100321getmark_buf(buf_T *buf, int c, int changefile)
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100322{
323 return getmark_buf_fnum(buf, c, changefile, NULL);
324}
325
326 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100327getmark(int c, int changefile)
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000328{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100329 return getmark_buf_fnum(curbuf, c, changefile, NULL);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000330}
331
332 pos_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100333getmark_buf_fnum(
334 buf_T *buf,
335 int c,
336 int changefile,
337 int *fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
339 pos_T *posp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 pos_T *startp, *endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 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 Moolenaar9d182dd2013-01-23 15:53:15 +0100360 posp = &(buf->b_last_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 else if (c == '^') /* to where Insert mode stopped */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100362 posp = &(buf->b_last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 else if (c == '.') /* to where last change was made */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100364 posp = &(buf->b_last_change);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 else if (c == '[') /* to start of previous operator */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100366 posp = &(buf->b_op_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 else if (c == ']') /* to end of previous operator */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100368 posp = &(buf->b_op_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 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 Moolenaar8b96d642005-09-05 22:05:30 +0000377 if (findpar(&oa.inclusive,
378 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {
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 Moolenaar071d4272004-06-13 20:20:40 +0000401 else if (c == '<' || c == '>') /* start/end of visual area */
402 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100403 startp = &buf->b_visual.vi_start;
404 endp = &buf->b_visual.vi_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100405 if (((c == '<') == LT_POS(*startp, *endp) || endp->lnum == 0)
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100406 && startp->lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 posp = startp;
408 else
409 posp = endp;
410 /*
411 * For Visual line mode, set mark at begin or end of line
412 */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100413 if (buf->b_visual.vi_mode == 'V')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {
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 Moolenaar071d4272004-06-13 20:20:40 +0000426 else if (ASCII_ISLOWER(c)) /* normal named mark */
427 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100428 posp = &(buf->b_namedm[c - 'a']);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 }
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 Moolenaarbfb2d402006-03-03 22:50:42 +0000440
441 if (fnum != NULL)
442 *fnum = namedfm[c].fmark.fnum;
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100443 else if (namedfm[c].fmark.fnum != buf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000445 /* mark is in another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 posp = &pos_copy;
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100475getnextmark(
476 pos_T *startpos, /* where to start */
477 int dir, /* direction for search */
478 int begin_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
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 Moolenaarb5aedf32017-03-12 18:23:53 +0100501 if ((result == NULL || LT_POS(curbuf->b_namedm[i], *result))
502 && LT_POS(pos, curbuf->b_namedm[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 result = &curbuf->b_namedm[i];
504 }
505 else
506 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100507 if ((result == NULL || LT_POS(*result, curbuf->b_namedm[i]))
508 && LT_POS(curbuf->b_namedm[i], pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100523fname2fnum(xfmark_T *fm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 char_u *p;
526
527 if (fm->fname != NULL)
528 {
529 /*
530 * First expand "~/" in the file name to the home directory.
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000531 * Don't expand the whole name, it may contain other '~' chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000533 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 Moolenaarcb4cef22008-03-16 15:04:34 +0000542 len = (int)STRLEN(NameBuff);
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000543 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 Moolenaar071d4272004-06-13 20:20:40 +0000549 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 Moolenaar52ea13d2016-01-30 18:51:09 +0100563fmarks_check_names(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100593fmarks_check_one(xfmark_T *fm, char_u *name, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
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 Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(fm->fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100609check_mark(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100638clrallmarks(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
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 Moolenaar2d358992016-06-12 21:20:54 +0200647#ifdef FEAT_VIMINFO
648 namedfm[i].time_set = 0;
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100674fm_getname(fmark_T *fmark, int lead_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
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 Moolenaar52ea13d2016-01-30 18:51:09 +0100686mark_line(pos_T *mp, int lead_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
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 Moolenaar91acfff2017-03-12 19:22:36 +0100698 for (p = s; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 len += ptr2cells(p);
701 if (len >= Columns - lead_len)
702 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704 *p = NUL;
705 return s;
706}
707
708/*
709 * print the marks
710 */
711 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100712do_marks(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
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 Moolenaara226a6d2006-02-26 23:59:20 +0000744 show_one_mark('<', arg, &curbuf->b_visual.vi_start, NULL, TRUE);
745 show_one_mark('>', arg, &curbuf->b_visual.vi_end, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 show_one_mark(-1, arg, NULL, NULL, FALSE);
747}
748
749 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100750show_one_mark(
751 int c,
752 char_u *arg,
753 pos_T *p,
754 char_u *name,
755 int current) /* in current file */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
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 Moolenaar8820b482017-03-16 17:23:31 +0100795 msg_outtrans_attr(name, current ? HL_ATTR(HLF_D) : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (mustfree)
797 vim_free(name);
798 }
799 }
800 out_flush(); /* show one line at a time */
801 }
802}
803
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000804/*
805 * ":delmarks[!] [marks]"
806 */
807 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100808ex_delmarks(exarg_T *eap)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000809{
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 Moolenaard23a8232018-02-10 18:45:26 +0100863 VIM_CLEAR(namedfm[n].fname);
Bram Moolenaar2d358992016-06-12 21:20:54 +0200864#ifdef FEAT_VIMINFO
865 namedfm[n].time_set = 0;
866#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000867 }
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 Moolenaara226a6d2006-02-26 23:59:20 +0000878 case '<': curbuf->b_visual.vi_start.lnum = 0; break;
879 case '>': curbuf->b_visual.vi_end.lnum = 0; break;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000880 case ' ': break;
881 default: EMSG2(_(e_invarg2), p);
882 return;
883 }
884 }
885 }
886}
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#if defined(FEAT_JUMPLIST) || defined(PROTO)
889/*
890 * print the jumplist
891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100893ex_jumps(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
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 Moolenaared39e1d2008-08-09 17:55:22 +0000913 {
914 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 break;
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 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 Moolenaar8820b482017-03-16 17:23:31 +0100926 ? HL_ATTR(HLF_D) : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 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 Moolenaar2d358992016-06-12 21:20:54 +0200936 void
937ex_clearjumps(exarg_T *eap UNUSED)
938{
939 free_jumplist(curwin);
940 curwin->w_jumplistlen = 0;
941 curwin->w_jumplistidx = 0;
942}
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944/*
945 * print the changelist
946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100948ex_changes(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
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 Moolenaar8820b482017-03-16 17:23:31 +0100973 msg_outtrans_attr(name, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 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 Moolenaar52ea13d2016-01-30 18:51:09 +01001025mark_adjust(
1026 linenr_T line1,
1027 linenr_T line2,
1028 long amount,
1029 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar88d298a2017-03-14 21:53:58 +01001031 mark_adjust_internal(line1, line2, amount, amount_after, TRUE);
1032}
1033
1034 void
1035mark_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
1045mark_adjust_internal(
1046 linenr_T line1,
1047 linenr_T line2,
1048 long amount,
1049 long amount_after,
1050 int adjust_folds UNUSED)
1051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 int i;
1053 int fnum = curbuf->b_fnum;
1054 linenr_T *lp;
1055 win_T *win;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001056 tabpage_T *tab;
Bram Moolenaarb6a76ff2013-02-06 12:33:21 +01001057 static pos_T initpos = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
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 Moolenaarb6a76ff2013-02-06 12:33:21 +01001083 /* last cursor position, if it was set */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001084 if (!EQUAL_POS(curbuf->b_last_cursor, initpos))
Bram Moolenaarb6a76ff2013-02-06 12:33:21 +01001085 one_adjust(&(curbuf->b_last_cursor.lnum));
1086
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#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 Moolenaar071d4272004-06-13 20:20:40 +00001094 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001095 one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum));
1096 one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098#ifdef FEAT_QUICKFIX
1099 /* quickfix marks */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001100 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
1101 /* location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001102 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001103 qf_mark_adjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#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 Moolenaarbd1e5d22009-04-29 09:02:44 +00001124 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
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 Moolenaar071d4272004-06-13 20:20:40 +00001143 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00001149
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 Moolenaar88d298a2017-03-14 21:53:58 +01001195 if (adjust_folds)
1196 foldMarkAdjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#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 Moolenaar52ea13d2016-01-30 18:51:09 +01001227mark_col_adjust(
1228 linenr_T lnum,
1229 colnr_T mincol,
1230 long lnum_amount,
1231 long col_amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232{
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 Moolenaar071d4272004-06-13 20:20:40 +00001266 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001267 col_adjust(&(curbuf->b_visual.vi_start));
1268 col_adjust(&(curbuf->b_visual.vi_end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
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 Moolenaar52ea13d2016-01-30 18:51:09 +01001311cleanup_jumplist(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
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 Moolenaar071d4272004-06-13 20:20:40 +00001338/*
1339 * Copy the jumplist from window "from" to window "to".
1340 */
1341 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001342copy_jumplist(win_T *from, win_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
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 Moolenaar52ea13d2016-01-30 18:51:09 +01001360free_jumplist(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int i;
1363
1364 for (i = 0; i < wp->w_jumplistlen; ++i)
1365 vim_free(wp->w_jumplist[i].fname);
1366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#endif /* FEAT_JUMPLIST */
1368
1369 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001370set_last_cursor(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar9db12932013-11-03 00:20:52 +01001372 if (win->w_buffer != NULL)
1373 win->w_buffer->b_last_cursor = win->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374}
1375
Bram Moolenaarea408852005-06-25 22:49:46 +00001376#if defined(EXITFREE) || defined(PROTO)
1377 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001378free_all_marks(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001379{
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 Moolenaar071d4272004-06-13 20:20:40 +00001388#if defined(FEAT_VIMINFO) || defined(PROTO)
1389 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001390read_viminfo_filemark(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
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 Moolenaar2d358992016-06-12 21:20:54 +02001444 fm->time_set = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 }
1447 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1448}
1449
Bram Moolenaar2d358992016-06-12 21:20:54 +02001450static xfmark_T *vi_namedfm = NULL;
1451#ifdef FEAT_JUMPLIST
1452static xfmark_T *vi_jumplist = NULL;
1453static int vi_jumplist_len = 0;
1454#endif
1455
1456/*
1457 * Prepare for reading viminfo marks when writing viminfo later.
1458 */
1459 void
1460prepare_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
1472finish_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 Moolenaard23a8232018-02-10 18:45:26 +01001480 VIM_CLEAR(vi_namedfm);
Bram Moolenaar2d358992016-06-12 21:20:54 +02001481 }
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 Moolenaard23a8232018-02-10 18:45:26 +01001487 VIM_CLEAR(vi_jumplist);
Bram Moolenaar2d358992016-06-12 21:20:54 +02001488 }
1489#endif
1490}
1491
1492/*
1493 * Accept a new style mark line from the viminfo, store it when it's new.
1494 */
1495 void
1496handle_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 Moolenaarece74ab2016-06-13 22:22:15 +02001542 {
1543 ++idx;
Bram Moolenaar2d358992016-06-12 21:20:54 +02001544 break;
Bram Moolenaarece74ab2016-06-13 22:22:15 +02001545 }
Bram Moolenaar678e4802016-06-17 22:38:46 +02001546 /* idx cannot be zero now */
Bram Moolenaarece74ab2016-06-13 22:22:15 +02001547 if (idx < 0 && curwin->w_jumplistlen < JUMPLISTSIZE)
1548 /* insert as the oldest entry */
1549 idx = 0;
Bram Moolenaar2d358992016-06-12 21:20:54 +02001550 }
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 Moolenaar28607ba2016-06-15 21:44:51 +02001562 --idx;
Bram Moolenaar2d358992016-06-12 21:20:54 +02001563 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 Moolenaar2d358992016-06-12 21:20:54 +02001570 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 Moolenaar156919f2016-10-15 20:46:20 +02001621 if (vi_namedfm != NULL || fm->fmark.mark.lnum == 0
1622 || fm->time_set < timestamp || force)
Bram Moolenaar2d358992016-06-12 21:20:54 +02001623 {
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 Moolenaare6278052017-08-13 18:11:17 +02001643/*
1644 * Return TRUE if marks for "buf" should not be written.
1645 */
1646 static int
1647skip_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 Moolenaar071d4272004-06-13 20:20:40 +00001656 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001657write_viminfo_filemarks(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 int i;
1660 char_u *name;
1661 buf_T *buf;
1662 xfmark_T *fm;
Bram Moolenaar2d358992016-06-12 21:20:54 +02001663 int vi_idx;
1664 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 if (get_viminfo_parameter('f') == 0)
1667 return;
1668
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001669 fputs(_("\n# File marks:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar2d358992016-06-12 21:20:54 +02001671 /* 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 Moolenaar071d4272004-06-13 20:20:40 +00001682 /*
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 Moolenaar2d358992016-06-12 21:20:54 +02001686 * Set the '0 mark to current cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
Bram Moolenaare6278052017-08-13 18:11:17 +02001688 if (curbuf->b_ffname != NULL && !skip_for_viminfo(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
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 Moolenaar2d358992016-06-12 21:20:54 +02001706 namedfm[NMARKS].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708
Bram Moolenaar2d358992016-06-12 21:20:54 +02001709 /* 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 Moolenaar36f0f062016-06-14 23:02:46 +02001714 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 Moolenaar2d358992016-06-12 21:20:54 +02001719 || namedfm[idx].fmark.mark.lnum == 0))
Bram Moolenaar36f0f062016-06-14 23:02:46 +02001720 {
1721 fm = vi_fm;
1722 ++vi_idx;
1723 }
Bram Moolenaar2d358992016-06-12 21:20:54 +02001724 else
Bram Moolenaar36f0f062016-06-14 23:02:46 +02001725 {
Bram Moolenaar2d358992016-06-12 21:20:54 +02001726 fm = &namedfm[idx++];
Bram Moolenaar36f0f062016-06-14 23:02:46 +02001727 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 Moolenaar2d358992016-06-12 21:20:54 +02001737 write_one_filemark(fp, fm, '\'', i - NMARKS + '0');
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740#ifdef FEAT_JUMPLIST
1741 /* Write the jumplist with -' */
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001742 fputs(_("\n# Jumplist (newest first):\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 setpcmark(); /* add current cursor position */
1744 cleanup_jumplist();
Bram Moolenaarece74ab2016-06-13 22:22:15 +02001745 vi_idx = 0;
1746 idx = curwin->w_jumplistlen - 1;
1747 for (i = 0; i < JUMPLISTSIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
Bram Moolenaarece74ab2016-06-13 22:22:15 +02001749 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 Moolenaar071d4272004-06-13 20:20:40 +00001762 if (fm->fmark.fnum == 0
1763 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
Bram Moolenaare6278052017-08-13 18:11:17 +02001764 && !skip_for_viminfo(buf)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 write_one_filemark(fp, fm, '-', '\'');
1766 }
1767#endif
1768}
1769
1770 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001771write_one_filemark(
1772 FILE *fp,
1773 xfmark_T *fm,
1774 int c1,
1775 int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776{
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 Moolenaar2d358992016-06-12 21:20:54 +02001791
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 Moolenaar071d4272004-06-13 20:20:40 +00001799 }
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 Moolenaar52ea13d2016-01-30 18:51:09 +01001809removable(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 char_u *p;
1812 char_u part[51];
1813 int retval = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001814 size_t n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
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 Moolenaarcfc6c432005-06-06 21:50:35 +00001822 if (part[0] == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001824 n = STRLEN(part + 1);
1825 if (MB_STRNICMP(part + 1, name, n) == 0)
1826 {
1827 retval = TRUE;
1828 break;
1829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
1831 }
1832 vim_free(name);
1833 }
1834 return retval;
1835}
1836
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001837 static void
1838write_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
1846write_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 Moolenaarb5aedf32017-03-12 18:23:53 +01001869 if (i == 0 || !EQUAL_POS(buf->b_changelist[i - 1],
1870 buf->b_changelist[i]))
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001871 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 Moolenaar071d4272004-06-13 20:20:40 +00001877
1878/*
1879 * Write all the named marks for all buffers.
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001880 * When "buflist" is not NULL fill it with the buffers for which marks are to
1881 * be written.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 */
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001883 void
1884write_viminfo_marks(FILE *fp_out, garray_T *buflist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 buf_T *buf;
1887 int is_mark_set;
1888 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001890 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
1892 /*
1893 * Set b_last_cursor for the all buffers that have a window.
1894 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001895 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001898 fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out);
Bram Moolenaar29323592016-07-24 22:04:11 +02001899 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
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 Moolenaare6278052017-08-13 18:11:17 +02001920 && buf->b_ffname[0] != NUL
1921 && !skip_for_viminfo(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001923 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 Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 }
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930}
1931
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001932/*
1933 * Compare functions for qsort() below, that compares b_last_used.
1934 */
1935 static int
1936#ifdef __BORLANDC__
1937_RTLENTRYF
1938#endif
1939buf_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001941 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 Moolenaar071d4272004-06-13 20:20:40 +00001947}
1948
1949/*
1950 * Handle marks in the viminfo file:
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001951 * fp_out != NULL: copy marks, in time order with buffers in "buflist".
Bram Moolenaard812df62008-11-09 12:46:09 +00001952 * 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 Moolenaar071d4272004-06-13 20:20:40 +00001954 */
1955 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001956copy_viminfo_marks(
1957 vir_T *virp,
1958 FILE *fp_out,
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001959 garray_T *buflist,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001960 int eof,
1961 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
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 Moolenaard812df62008-11-09 12:46:09 +00001973#ifdef FEAT_EVAL
1974 list_T *list = NULL;
1975#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001976 int count = 0;
1977 int buflist_used = 0;
1978 buf_T *buflist_buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 if ((name_buf = alloc(LSIZE)) == NULL)
1981 return;
1982 *name_buf = NUL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001983
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001984 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 Moolenaard812df62008-11-09 12:46:09 +00001992#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 Moolenaar071d4272004-06-13 20:20:40 +00002001 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 Moolenaard812df62008-11-09 12:46:09 +00002031#ifdef FEAT_EVAL
2032 if (list != NULL)
2033 list_append_string(list, str, -1);
2034#endif
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /*
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 Moolenaard812df62008-11-09 12:46:09 +00002043 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
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 Moolenaar29323592016-07-24 22:04:11 +02002054 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 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 Moolenaarab9c89b2016-07-03 17:47:26 +02002063 * Copy marks if the buffer has not been loaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
2065 if (buf == NULL || !buf->b_marks_read)
2066 {
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002067 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 ", &ltime);
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 Moolenaar071d4272004-06-13 20:20:40 +00002120 fputs("\n> ", fp_out);
2121 viminfo_writestring(fp_out, str);
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002122 if (did_read_line)
2123 fputs((char *)line, fp_out);
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 count++;
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002126 copy_marks_out = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
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 Moolenaare698add2011-02-25 15:11:22 +01002140 unsigned u;
2141
2142 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u);
2143 pos.col = u;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 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 Moolenaarab9c89b2016-07-03 17:47:26 +02002164
2165 /* Using the line number for the last-used
2166 * timestamp. */
2167 case '*': curbuf->b_last_used = pos.lnum; break;
2168
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 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 Moolenaarab9c89b2016-07-03 17:47:26 +02002177
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 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 Moolenaarab9c89b2016-07-03 17:47:26 +02002192
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 Moolenaar071d4272004-06-13 20:20:40 +00002202 vim_free(name_buf);
2203}
2204#endif /* FEAT_VIMINFO */