blob: 1ac91d8b1b7ed64a30c2d2936c4522946118cae7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * 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
30static void fname2fnum __ARGS((xfmark_T *fm));
31static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
32static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
33static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
34#ifdef FEAT_JUMPLIST
35static void cleanup_jumplist __ARGS((void));
36#endif
37#ifdef FEAT_VIMINFO
38static void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));
39#endif
40
41/*
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000042 * Set named mark "c" at current cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 * Returns OK on success, FAIL if bad name given.
44 */
45 int
46setmark(c)
47 int c;
48{
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000049 return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum);
50}
51
52/*
53 * Set named mark "c" to position "pos".
54 * When "c" is upper case use file "fnum".
55 * Returns OK on success, FAIL if bad name given.
56 */
57 int
58setmark_pos(c, pos, fnum)
59 int c;
60 pos_T *pos;
61 int fnum;
62{
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 int i;
64
65 /* Check for a special key (may cause islower() to crash). */
66 if (c < 0)
67 return FAIL;
68
69 if (c == '\'' || c == '`')
70 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000071 if (pos == &curwin->w_cursor)
72 {
73 setpcmark();
74 /* keep it even when the cursor doesn't move */
75 curwin->w_prev_pcmark = curwin->w_pcmark;
76 }
77 else
78 curwin->w_pcmark = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 return OK;
80 }
81
Bram Moolenaar08250432008-02-13 11:42:46 +000082 if (c == '"')
83 {
84 curbuf->b_last_cursor = *pos;
85 return OK;
86 }
87
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 /* Allow setting '[ and '] for an autocommand that simulates reading a
89 * file. */
90 if (c == '[')
91 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000092 curbuf->b_op_start = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 return OK;
94 }
95 if (c == ']')
96 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +000097 curbuf->b_op_end = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 return OK;
99 }
100
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200101#ifdef FEAT_VISUAL
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200102 if (c == '<' || c == '>')
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200103 {
Bram Moolenaarbc88a272013-08-02 17:22:23 +0200104 if (c == '<')
105 curbuf->b_visual.vi_start = *pos;
106 else
107 curbuf->b_visual.vi_end = *pos;
108 if (curbuf->b_visual.vi_mode == NUL)
109 /* Visual_mode has not yet been set, use a sane default. */
110 curbuf->b_visual.vi_mode = 'v';
Bram Moolenaar0306ac32012-07-06 17:51:28 +0200111 return OK;
112 }
113#endif
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifndef EBCDIC
116 if (c > 'z') /* some islower() and isupper() cannot handle
117 characters above 127 */
118 return FAIL;
119#endif
120 if (islower(c))
121 {
122 i = c - 'a';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000123 curbuf->b_namedm[i] = *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 return OK;
125 }
126 if (isupper(c))
127 {
128 i = c - 'A';
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000129 namedfm[i].fmark.mark = *pos;
130 namedfm[i].fmark.fnum = fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 vim_free(namedfm[i].fname);
132 namedfm[i].fname = NULL;
133 return OK;
134 }
135 return FAIL;
136}
137
138/*
139 * Set the previous context mark to the current position and add it to the
140 * jump list.
141 */
142 void
143setpcmark()
144{
145#ifdef FEAT_JUMPLIST
146 int i;
147 xfmark_T *fm;
148#endif
149#ifdef JUMPLIST_ROTATE
150 xfmark_T tempmark;
151#endif
152
153 /* for :global the mark is set only once */
154 if (global_busy || listcmd_busy || cmdmod.keepjumps)
155 return;
156
157 curwin->w_prev_pcmark = curwin->w_pcmark;
158 curwin->w_pcmark = curwin->w_cursor;
159
160#ifdef FEAT_JUMPLIST
161# ifdef JUMPLIST_ROTATE
162 /*
163 * If last used entry is not at the top, put it at the top by rotating
164 * the stack until it is (the newer entries will be at the bottom).
165 * Keep one entry (the last used one) at the top.
166 */
167 if (curwin->w_jumplistidx < curwin->w_jumplistlen)
168 ++curwin->w_jumplistidx;
169 while (curwin->w_jumplistidx < curwin->w_jumplistlen)
170 {
171 tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
172 for (i = curwin->w_jumplistlen - 1; i > 0; --i)
173 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
174 curwin->w_jumplist[0] = tempmark;
175 ++curwin->w_jumplistidx;
176 }
177# endif
178
179 /* If jumplist is full: remove oldest entry */
180 if (++curwin->w_jumplistlen > JUMPLISTSIZE)
181 {
182 curwin->w_jumplistlen = JUMPLISTSIZE;
183 vim_free(curwin->w_jumplist[0].fname);
184 for (i = 1; i < JUMPLISTSIZE; ++i)
185 curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
186 }
187 curwin->w_jumplistidx = curwin->w_jumplistlen;
188 fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
189
190 fm->fmark.mark = curwin->w_pcmark;
191 fm->fmark.fnum = curbuf->b_fnum;
192 fm->fname = NULL;
193#endif
194}
195
196/*
197 * To change context, call setpcmark(), then move the current position to
198 * where ever, then call checkpcmark(). This ensures that the previous
199 * context will only be changed if the cursor moved to a different line.
200 * If pcmark was deleted (with "dG") the previous mark is restored.
201 */
202 void
203checkpcmark()
204{
205 if (curwin->w_prev_pcmark.lnum != 0
206 && (equalpos(curwin->w_pcmark, curwin->w_cursor)
207 || curwin->w_pcmark.lnum == 0))
208 {
209 curwin->w_pcmark = curwin->w_prev_pcmark;
210 curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */
211 }
212}
213
214#if defined(FEAT_JUMPLIST) || defined(PROTO)
215/*
216 * move "count" positions in the jump list (count may be negative)
217 */
218 pos_T *
219movemark(count)
220 int count;
221{
222 pos_T *pos;
223 xfmark_T *jmp;
224
225 cleanup_jumplist();
226
227 if (curwin->w_jumplistlen == 0) /* nothing to jump to */
228 return (pos_T *)NULL;
229
230 for (;;)
231 {
232 if (curwin->w_jumplistidx + count < 0
233 || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
234 return (pos_T *)NULL;
235
236 /*
237 * if first CTRL-O or CTRL-I command after a jump, add cursor position
Bram Moolenaarf711faf2007-05-10 16:48:19 +0000238 * to list. Careful: If there are duplicates (CTRL-O immediately after
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 * starting Vim on a file), another entry may have been removed.
240 */
241 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
242 {
243 setpcmark();
244 --curwin->w_jumplistidx; /* skip the new entry */
245 if (curwin->w_jumplistidx + count < 0)
246 return (pos_T *)NULL;
247 }
248
249 curwin->w_jumplistidx += count;
250
251 jmp = curwin->w_jumplist + curwin->w_jumplistidx;
252 if (jmp->fmark.fnum == 0)
253 fname2fnum(jmp);
254 if (jmp->fmark.fnum != curbuf->b_fnum)
255 {
256 /* jump to other file */
257 if (buflist_findnr(jmp->fmark.fnum) == NULL)
258 { /* Skip this one .. */
259 count += count < 0 ? -1 : 1;
260 continue;
261 }
262 if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
263 0, FALSE) == FAIL)
264 return (pos_T *)NULL;
265 /* Set lnum again, autocommands my have changed it */
266 curwin->w_cursor = jmp->fmark.mark;
267 pos = (pos_T *)-1;
268 }
269 else
270 pos = &(jmp->fmark.mark);
271 return pos;
272 }
273}
274
275/*
276 * Move "count" positions in the changelist (count may be negative).
277 */
278 pos_T *
279movechangelist(count)
280 int count;
281{
282 int n;
283
284 if (curbuf->b_changelistlen == 0) /* nothing to jump to */
285 return (pos_T *)NULL;
286
287 n = curwin->w_changelistidx;
288 if (n + count < 0)
289 {
290 if (n == 0)
291 return (pos_T *)NULL;
292 n = 0;
293 }
294 else if (n + count >= curbuf->b_changelistlen)
295 {
296 if (n == curbuf->b_changelistlen - 1)
297 return (pos_T *)NULL;
298 n = curbuf->b_changelistlen - 1;
299 }
300 else
301 n += count;
302 curwin->w_changelistidx = n;
303 return curbuf->b_changelist + n;
304}
305#endif
306
307/*
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100308 * Find mark "c" in buffer pointed to by "buf".
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000309 * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc.
310 * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit
311 * another file.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 * Returns:
313 * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is
314 * in another file which can't be gotten. (caller needs to check lnum!)
315 * - NULL if there is no mark called 'c'.
316 * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
317 */
318 pos_T *
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100319getmark_buf(buf, c, changefile)
320 buf_T *buf;
321 int c;
322 int changefile;
323{
324 return getmark_buf_fnum(buf, c, changefile, NULL);
325}
326
327 pos_T *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328getmark(c, changefile)
329 int c;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000330 int changefile;
331{
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100332 return getmark_buf_fnum(curbuf, c, changefile, NULL);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000333}
334
335 pos_T *
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100336getmark_buf_fnum(buf, c, changefile, fnum)
337 buf_T *buf;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000338 int c;
339 int changefile;
340 int *fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
342 pos_T *posp;
343#ifdef FEAT_VISUAL
344 pos_T *startp, *endp;
345#endif
346 static pos_T pos_copy;
347
348 posp = NULL;
349
350 /* Check for special key, can't be a mark name and might cause islower()
351 * to crash. */
352 if (c < 0)
353 return posp;
354#ifndef EBCDIC
355 if (c > '~') /* check for islower()/isupper() */
356 ;
357 else
358#endif
359 if (c == '\'' || c == '`') /* previous context mark */
360 {
361 pos_copy = curwin->w_pcmark; /* need to make a copy because */
362 posp = &pos_copy; /* w_pcmark may be changed soon */
363 }
364 else if (c == '"') /* to pos when leaving buffer */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100365 posp = &(buf->b_last_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 else if (c == '^') /* to where Insert mode stopped */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100367 posp = &(buf->b_last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 else if (c == '.') /* to where last change was made */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100369 posp = &(buf->b_last_change);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 else if (c == '[') /* to start of previous operator */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100371 posp = &(buf->b_op_start);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 else if (c == ']') /* to end of previous operator */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100373 posp = &(buf->b_op_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 else if (c == '{' || c == '}') /* to previous/next paragraph */
375 {
376 pos_T pos;
377 oparg_T oa;
378 int slcb = listcmd_busy;
379
380 pos = curwin->w_cursor;
381 listcmd_busy = TRUE; /* avoid that '' is changed */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000382 if (findpar(&oa.inclusive,
383 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
385 pos_copy = curwin->w_cursor;
386 posp = &pos_copy;
387 }
388 curwin->w_cursor = pos;
389 listcmd_busy = slcb;
390 }
391 else if (c == '(' || c == ')') /* to previous/next sentence */
392 {
393 pos_T pos;
394 int slcb = listcmd_busy;
395
396 pos = curwin->w_cursor;
397 listcmd_busy = TRUE; /* avoid that '' is changed */
398 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
399 {
400 pos_copy = curwin->w_cursor;
401 posp = &pos_copy;
402 }
403 curwin->w_cursor = pos;
404 listcmd_busy = slcb;
405 }
406#ifdef FEAT_VISUAL
407 else if (c == '<' || c == '>') /* start/end of visual area */
408 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100409 startp = &buf->b_visual.vi_start;
410 endp = &buf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 if ((c == '<') == lt(*startp, *endp))
412 posp = startp;
413 else
414 posp = endp;
415 /*
416 * For Visual line mode, set mark at begin or end of line
417 */
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100418 if (buf->b_visual.vi_mode == 'V')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
420 pos_copy = *posp;
421 posp = &pos_copy;
422 if (c == '<')
423 pos_copy.col = 0;
424 else
425 pos_copy.col = MAXCOL;
426#ifdef FEAT_VIRTUALEDIT
427 pos_copy.coladd = 0;
428#endif
429 }
430 }
431#endif
432 else if (ASCII_ISLOWER(c)) /* normal named mark */
433 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100434 posp = &(buf->b_namedm[c - 'a']);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 }
436 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */
437 {
438 if (VIM_ISDIGIT(c))
439 c = c - '0' + NMARKS;
440 else
441 c -= 'A';
442 posp = &(namedfm[c].fmark.mark);
443
444 if (namedfm[c].fmark.fnum == 0)
445 fname2fnum(&namedfm[c]);
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000446
447 if (fnum != NULL)
448 *fnum = namedfm[c].fmark.fnum;
Bram Moolenaar9d182dd2013-01-23 15:53:15 +0100449 else if (namedfm[c].fmark.fnum != buf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {
Bram Moolenaarbfb2d402006-03-03 22:50:42 +0000451 /* mark is in another file */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 posp = &pos_copy;
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (namedfm[c].fmark.mark.lnum != 0
455 && changefile && namedfm[c].fmark.fnum)
456 {
457 if (buflist_getfile(namedfm[c].fmark.fnum,
458 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
459 {
460 /* Set the lnum now, autocommands could have changed it */
461 curwin->w_cursor = namedfm[c].fmark.mark;
462 return (pos_T *)-1;
463 }
464 pos_copy.lnum = -1; /* can't get file */
465 }
466 else
467 pos_copy.lnum = 0; /* mark exists, but is not valid in
468 current buffer */
469 }
470 }
471
472 return posp;
473}
474
475/*
476 * Search for the next named mark in the current file.
477 *
478 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
479 */
480 pos_T *
481getnextmark(startpos, dir, begin_line)
482 pos_T *startpos; /* where to start */
483 int dir; /* direction for search */
484 int begin_line;
485{
486 int i;
487 pos_T *result = NULL;
488 pos_T pos;
489
490 pos = *startpos;
491
492 /* When searching backward and leaving the cursor on the first non-blank,
493 * position must be in a previous line.
494 * When searching forward and leaving the cursor on the first non-blank,
495 * position must be in a next line. */
496 if (dir == BACKWARD && begin_line)
497 pos.col = 0;
498 else if (dir == FORWARD && begin_line)
499 pos.col = MAXCOL;
500
501 for (i = 0; i < NMARKS; i++)
502 {
503 if (curbuf->b_namedm[i].lnum > 0)
504 {
505 if (dir == FORWARD)
506 {
507 if ((result == NULL || lt(curbuf->b_namedm[i], *result))
508 && lt(pos, curbuf->b_namedm[i]))
509 result = &curbuf->b_namedm[i];
510 }
511 else
512 {
513 if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
514 && lt(curbuf->b_namedm[i], pos))
515 result = &curbuf->b_namedm[i];
516 }
517 }
518 }
519
520 return result;
521}
522
523/*
524 * For an xtended filemark: set the fnum from the fname.
525 * This is used for marks obtained from the .viminfo file. It's postponed
526 * until the mark is used to avoid a long startup delay.
527 */
528 static void
529fname2fnum(fm)
530 xfmark_T *fm;
531{
532 char_u *p;
533
534 if (fm->fname != NULL)
535 {
536 /*
537 * First expand "~/" in the file name to the home directory.
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000538 * Don't expand the whole name, it may contain other '~' chars.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 */
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000540 if (fm->fname[0] == '~' && (fm->fname[1] == '/'
541#ifdef BACKSLASH_IN_FILENAME
542 || fm->fname[1] == '\\'
543#endif
544 ))
545 {
546 int len;
547
548 expand_env((char_u *)"~/", NameBuff, MAXPATHL);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000549 len = (int)STRLEN(NameBuff);
Bram Moolenaar525ad4d2008-01-03 19:22:13 +0000550 vim_strncpy(NameBuff + len, fm->fname + 2, MAXPATHL - len - 1);
551 }
552 else
553 vim_strncpy(NameBuff, fm->fname, MAXPATHL - 1);
554
555 /* Try to shorten the file name. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 mch_dirname(IObuff, IOSIZE);
557 p = shorten_fname(NameBuff, IObuff);
558
559 /* buflist_new() will call fmarks_check_names() */
560 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
561 }
562}
563
564/*
565 * Check all file marks for a name that matches the file name in buf.
566 * May replace the name with an fnum.
567 * Used for marks that come from the .viminfo file.
568 */
569 void
570fmarks_check_names(buf)
571 buf_T *buf;
572{
573 char_u *name;
574 int i;
575#ifdef FEAT_JUMPLIST
576 win_T *wp;
577#endif
578
579 if (buf->b_ffname == NULL)
580 return;
581
582 name = home_replace_save(buf, buf->b_ffname);
583 if (name == NULL)
584 return;
585
586 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
587 fmarks_check_one(&namedfm[i], name, buf);
588
589#ifdef FEAT_JUMPLIST
590 FOR_ALL_WINDOWS(wp)
591 {
592 for (i = 0; i < wp->w_jumplistlen; ++i)
593 fmarks_check_one(&wp->w_jumplist[i], name, buf);
594 }
595#endif
596
597 vim_free(name);
598}
599
600 static void
601fmarks_check_one(fm, name, buf)
602 xfmark_T *fm;
603 char_u *name;
604 buf_T *buf;
605{
606 if (fm->fmark.fnum == 0
607 && fm->fname != NULL
608 && fnamecmp(name, fm->fname) == 0)
609 {
610 fm->fmark.fnum = buf->b_fnum;
611 vim_free(fm->fname);
612 fm->fname = NULL;
613 }
614}
615
616/*
617 * Check a if a position from a mark is valid.
618 * Give and error message and return FAIL if not.
619 */
620 int
621check_mark(pos)
622 pos_T *pos;
623{
624 if (pos == NULL)
625 {
626 EMSG(_(e_umark));
627 return FAIL;
628 }
629 if (pos->lnum <= 0)
630 {
631 /* lnum is negative if mark is in another file can can't get that
632 * file, error message already give then. */
633 if (pos->lnum == 0)
634 EMSG(_(e_marknotset));
635 return FAIL;
636 }
637 if (pos->lnum > curbuf->b_ml.ml_line_count)
638 {
639 EMSG(_(e_markinval));
640 return FAIL;
641 }
642 return OK;
643}
644
645/*
646 * clrallmarks() - clear all marks in the buffer 'buf'
647 *
648 * Used mainly when trashing the entire buffer during ":e" type commands
649 */
650 void
651clrallmarks(buf)
652 buf_T *buf;
653{
654 static int i = -1;
655
656 if (i == -1) /* first call ever: initialize */
657 for (i = 0; i < NMARKS + 1; i++)
658 {
659 namedfm[i].fmark.mark.lnum = 0;
660 namedfm[i].fname = NULL;
661 }
662
663 for (i = 0; i < NMARKS; i++)
664 buf->b_namedm[i].lnum = 0;
665 buf->b_op_start.lnum = 0; /* start/end op mark cleared */
666 buf->b_op_end.lnum = 0;
667 buf->b_last_cursor.lnum = 1; /* '" mark cleared */
668 buf->b_last_cursor.col = 0;
669#ifdef FEAT_VIRTUALEDIT
670 buf->b_last_cursor.coladd = 0;
671#endif
672 buf->b_last_insert.lnum = 0; /* '^ mark cleared */
673 buf->b_last_change.lnum = 0; /* '. mark cleared */
674#ifdef FEAT_JUMPLIST
675 buf->b_changelistlen = 0;
676#endif
677}
678
679/*
680 * Get name of file from a filemark.
681 * When it's in the current buffer, return the text at the mark.
682 * Returns an allocated string.
683 */
684 char_u *
685fm_getname(fmark, lead_len)
686 fmark_T *fmark;
687 int lead_len;
688{
689 if (fmark->fnum == curbuf->b_fnum) /* current buffer */
690 return mark_line(&(fmark->mark), lead_len);
691 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
692}
693
694/*
695 * Return the line at mark "mp". Truncate to fit in window.
696 * The returned string has been allocated.
697 */
698 static char_u *
699mark_line(mp, lead_len)
700 pos_T *mp;
701 int lead_len;
702{
703 char_u *s, *p;
704 int len;
705
706 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
707 return vim_strsave((char_u *)"-invalid-");
708 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
709 if (s == NULL)
710 return NULL;
711 /* Truncate the line to fit it in the window */
712 len = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000713 for (p = s; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 len += ptr2cells(p);
716 if (len >= Columns - lead_len)
717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719 *p = NUL;
720 return s;
721}
722
723/*
724 * print the marks
725 */
726 void
727do_marks(eap)
728 exarg_T *eap;
729{
730 char_u *arg = eap->arg;
731 int i;
732 char_u *name;
733
734 if (arg != NULL && *arg == NUL)
735 arg = NULL;
736
737 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
738 for (i = 0; i < NMARKS; ++i)
739 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
740 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
741 {
742 if (namedfm[i].fmark.fnum != 0)
743 name = fm_getname(&namedfm[i].fmark, 15);
744 else
745 name = namedfm[i].fname;
746 if (name != NULL)
747 {
748 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
749 arg, &namedfm[i].fmark.mark, name,
750 namedfm[i].fmark.fnum == curbuf->b_fnum);
751 if (namedfm[i].fmark.fnum != 0)
752 vim_free(name);
753 }
754 }
755 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
756 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
757 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
758 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
759 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
760#ifdef FEAT_VISUAL
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000761 show_one_mark('<', arg, &curbuf->b_visual.vi_start, NULL, TRUE);
762 show_one_mark('>', arg, &curbuf->b_visual.vi_end, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
764 show_one_mark(-1, arg, NULL, NULL, FALSE);
765}
766
767 static void
768show_one_mark(c, arg, p, name, current)
769 int c;
770 char_u *arg;
771 pos_T *p;
772 char_u *name;
773 int current; /* in current file */
774{
775 static int did_title = FALSE;
776 int mustfree = FALSE;
777
778 if (c == -1) /* finish up */
779 {
780 if (did_title)
781 did_title = FALSE;
782 else
783 {
784 if (arg == NULL)
785 MSG(_("No marks set"));
786 else
787 EMSG2(_("E283: No marks matching \"%s\""), arg);
788 }
789 }
790 /* don't output anything if 'q' typed at --more-- prompt */
791 else if (!got_int
792 && (arg == NULL || vim_strchr(arg, c) != NULL)
793 && p->lnum != 0)
794 {
795 if (!did_title)
796 {
797 /* Highlight title */
798 MSG_PUTS_TITLE(_("\nmark line col file/text"));
799 did_title = TRUE;
800 }
801 msg_putchar('\n');
802 if (!got_int)
803 {
804 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
805 msg_outtrans(IObuff);
806 if (name == NULL && current)
807 {
808 name = mark_line(p, 15);
809 mustfree = TRUE;
810 }
811 if (name != NULL)
812 {
813 msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
814 if (mustfree)
815 vim_free(name);
816 }
817 }
818 out_flush(); /* show one line at a time */
819 }
820}
821
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000822/*
823 * ":delmarks[!] [marks]"
824 */
825 void
826ex_delmarks(eap)
827 exarg_T *eap;
828{
829 char_u *p;
830 int from, to;
831 int i;
832 int lower;
833 int digit;
834 int n;
835
836 if (*eap->arg == NUL && eap->forceit)
837 /* clear all marks */
838 clrallmarks(curbuf);
839 else if (eap->forceit)
840 EMSG(_(e_invarg));
841 else if (*eap->arg == NUL)
842 EMSG(_(e_argreq));
843 else
844 {
845 /* clear specified marks only */
846 for (p = eap->arg; *p != NUL; ++p)
847 {
848 lower = ASCII_ISLOWER(*p);
849 digit = VIM_ISDIGIT(*p);
850 if (lower || digit || ASCII_ISUPPER(*p))
851 {
852 if (p[1] == '-')
853 {
854 /* clear range of marks */
855 from = *p;
856 to = p[2];
857 if (!(lower ? ASCII_ISLOWER(p[2])
858 : (digit ? VIM_ISDIGIT(p[2])
859 : ASCII_ISUPPER(p[2])))
860 || to < from)
861 {
862 EMSG2(_(e_invarg2), p);
863 return;
864 }
865 p += 2;
866 }
867 else
868 /* clear one lower case mark */
869 from = to = *p;
870
871 for (i = from; i <= to; ++i)
872 {
873 if (lower)
874 curbuf->b_namedm[i - 'a'].lnum = 0;
875 else
876 {
877 if (digit)
878 n = i - '0' + NMARKS;
879 else
880 n = i - 'A';
881 namedfm[n].fmark.mark.lnum = 0;
882 vim_free(namedfm[n].fname);
883 namedfm[n].fname = NULL;
884 }
885 }
886 }
887 else
888 switch (*p)
889 {
890 case '"': curbuf->b_last_cursor.lnum = 0; break;
891 case '^': curbuf->b_last_insert.lnum = 0; break;
892 case '.': curbuf->b_last_change.lnum = 0; break;
893 case '[': curbuf->b_op_start.lnum = 0; break;
894 case ']': curbuf->b_op_end.lnum = 0; break;
895#ifdef FEAT_VISUAL
Bram Moolenaara226a6d2006-02-26 23:59:20 +0000896 case '<': curbuf->b_visual.vi_start.lnum = 0; break;
897 case '>': curbuf->b_visual.vi_end.lnum = 0; break;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000898#endif
899 case ' ': break;
900 default: EMSG2(_(e_invarg2), p);
901 return;
902 }
903 }
904 }
905}
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#if defined(FEAT_JUMPLIST) || defined(PROTO)
908/*
909 * print the jumplist
910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 void
912ex_jumps(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000913 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914{
915 int i;
916 char_u *name;
917
918 cleanup_jumplist();
919 /* Highlight title */
920 MSG_PUTS_TITLE(_("\n jump line col file/text"));
921 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
922 {
923 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
924 {
925 if (curwin->w_jumplist[i].fmark.fnum == 0)
926 fname2fnum(&curwin->w_jumplist[i]);
927 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
928 if (name == NULL) /* file name not available */
929 continue;
930
931 msg_putchar('\n');
932 if (got_int)
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000933 {
934 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 break;
Bram Moolenaared39e1d2008-08-09 17:55:22 +0000936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
938 i == curwin->w_jumplistidx ? '>' : ' ',
939 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
940 : curwin->w_jumplistidx - i,
941 curwin->w_jumplist[i].fmark.mark.lnum,
942 curwin->w_jumplist[i].fmark.mark.col);
943 msg_outtrans(IObuff);
944 msg_outtrans_attr(name,
945 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
946 ? hl_attr(HLF_D) : 0);
947 vim_free(name);
948 ui_breakcheck();
949 }
950 out_flush();
951 }
952 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
953 MSG_PUTS("\n>");
954}
955
956/*
957 * print the changelist
958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 void
960ex_changes(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +0000961 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
963 int i;
964 char_u *name;
965
966 /* Highlight title */
967 MSG_PUTS_TITLE(_("\nchange line col text"));
968
969 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
970 {
971 if (curbuf->b_changelist[i].lnum != 0)
972 {
973 msg_putchar('\n');
974 if (got_int)
975 break;
976 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
977 i == curwin->w_changelistidx ? '>' : ' ',
978 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
979 : curwin->w_changelistidx - i,
980 (long)curbuf->b_changelist[i].lnum,
981 curbuf->b_changelist[i].col);
982 msg_outtrans(IObuff);
983 name = mark_line(&curbuf->b_changelist[i], 17);
984 if (name == NULL)
985 break;
986 msg_outtrans_attr(name, hl_attr(HLF_D));
987 vim_free(name);
988 ui_breakcheck();
989 }
990 out_flush();
991 }
992 if (curwin->w_changelistidx == curbuf->b_changelistlen)
993 MSG_PUTS("\n>");
994}
995#endif
996
997#define one_adjust(add) \
998 { \
999 lp = add; \
1000 if (*lp >= line1 && *lp <= line2) \
1001 { \
1002 if (amount == MAXLNUM) \
1003 *lp = 0; \
1004 else \
1005 *lp += amount; \
1006 } \
1007 else if (amount_after && *lp > line2) \
1008 *lp += amount_after; \
1009 }
1010
1011/* don't delete the line, just put at first deleted line */
1012#define one_adjust_nodel(add) \
1013 { \
1014 lp = add; \
1015 if (*lp >= line1 && *lp <= line2) \
1016 { \
1017 if (amount == MAXLNUM) \
1018 *lp = line1; \
1019 else \
1020 *lp += amount; \
1021 } \
1022 else if (amount_after && *lp > line2) \
1023 *lp += amount_after; \
1024 }
1025
1026/*
1027 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
1028 * Must be called before changed_*(), appended_lines() or deleted_lines().
1029 * May be called before or after changing the text.
1030 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
1031 * within this range are made invalid.
1032 * If 'amount_after' is non-zero adjust marks after line2.
1033 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
1034 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
1035 * or: mark_adjust(56, 55, MAXLNUM, 2);
1036 */
1037 void
1038mark_adjust(line1, line2, amount, amount_after)
1039 linenr_T line1;
1040 linenr_T line2;
1041 long amount;
1042 long amount_after;
1043{
1044 int i;
1045 int fnum = curbuf->b_fnum;
1046 linenr_T *lp;
1047 win_T *win;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001048#ifdef FEAT_WINDOWS
1049 tabpage_T *tab;
1050#endif
Bram Moolenaarb6a76ff2013-02-06 12:33:21 +01001051 static pos_T initpos = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053 if (line2 < line1 && amount_after == 0L) /* nothing to do */
1054 return;
1055
1056 if (!cmdmod.lockmarks)
1057 {
1058 /* named marks, lower case and upper case */
1059 for (i = 0; i < NMARKS; i++)
1060 {
1061 one_adjust(&(curbuf->b_namedm[i].lnum));
1062 if (namedfm[i].fmark.fnum == fnum)
1063 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1064 }
1065 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1066 {
1067 if (namedfm[i].fmark.fnum == fnum)
1068 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1069 }
1070
1071 /* last Insert position */
1072 one_adjust(&(curbuf->b_last_insert.lnum));
1073
1074 /* last change position */
1075 one_adjust(&(curbuf->b_last_change.lnum));
1076
Bram Moolenaarb6a76ff2013-02-06 12:33:21 +01001077 /* last cursor position, if it was set */
1078 if (!equalpos(curbuf->b_last_cursor, initpos))
1079 one_adjust(&(curbuf->b_last_cursor.lnum));
1080
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#ifdef FEAT_JUMPLIST
1083 /* list of change positions */
1084 for (i = 0; i < curbuf->b_changelistlen; ++i)
1085 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
1086#endif
1087
1088#ifdef FEAT_VISUAL
1089 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001090 one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum));
1091 one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#endif
1093
1094#ifdef FEAT_QUICKFIX
1095 /* quickfix marks */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001096 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
1097 /* location lists */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001098 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001099 qf_mark_adjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100#endif
1101
1102#ifdef FEAT_SIGNS
1103 sign_mark_adjust(line1, line2, amount, amount_after);
1104#endif
1105 }
1106
1107 /* previous context mark */
1108 one_adjust(&(curwin->w_pcmark.lnum));
1109
1110 /* previous pcmark */
1111 one_adjust(&(curwin->w_prev_pcmark.lnum));
1112
1113 /* saved cursor for formatting */
1114 if (saved_cursor.lnum != 0)
1115 one_adjust_nodel(&(saved_cursor.lnum));
1116
1117 /*
1118 * Adjust items in all windows related to the current buffer.
1119 */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00001120 FOR_ALL_TAB_WINDOWS(tab, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
1122#ifdef FEAT_JUMPLIST
1123 if (!cmdmod.lockmarks)
1124 /* Marks in the jumplist. When deleting lines, this may create
1125 * duplicate marks in the jumplist, they will be removed later. */
1126 for (i = 0; i < win->w_jumplistlen; ++i)
1127 if (win->w_jumplist[i].fmark.fnum == fnum)
1128 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
1129#endif
1130
1131 if (win->w_buffer == curbuf)
1132 {
1133 if (!cmdmod.lockmarks)
1134 /* marks in the tag stack */
1135 for (i = 0; i < win->w_tagstacklen; i++)
1136 if (win->w_tagstack[i].fmark.fnum == fnum)
1137 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
1138
1139#ifdef FEAT_VISUAL
1140 /* the displayed Visual area */
1141 if (win->w_old_cursor_lnum != 0)
1142 {
1143 one_adjust_nodel(&(win->w_old_cursor_lnum));
1144 one_adjust_nodel(&(win->w_old_visual_lnum));
1145 }
1146#endif
1147
1148 /* topline and cursor position for windows with the same buffer
1149 * other than the current window */
1150 if (win != curwin)
1151 {
1152 if (win->w_topline >= line1 && win->w_topline <= line2)
1153 {
1154 if (amount == MAXLNUM) /* topline is deleted */
1155 {
1156 if (line1 <= 1)
1157 win->w_topline = 1;
1158 else
1159 win->w_topline = line1 - 1;
1160 }
1161 else /* keep topline on the same line */
1162 win->w_topline += amount;
1163#ifdef FEAT_DIFF
1164 win->w_topfill = 0;
1165#endif
1166 }
1167 else if (amount_after && win->w_topline > line2)
1168 {
1169 win->w_topline += amount_after;
1170#ifdef FEAT_DIFF
1171 win->w_topfill = 0;
1172#endif
1173 }
1174 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1175 {
1176 if (amount == MAXLNUM) /* line with cursor is deleted */
1177 {
1178 if (line1 <= 1)
1179 win->w_cursor.lnum = 1;
1180 else
1181 win->w_cursor.lnum = line1 - 1;
1182 win->w_cursor.col = 0;
1183 }
1184 else /* keep cursor on the same line */
1185 win->w_cursor.lnum += amount;
1186 }
1187 else if (amount_after && win->w_cursor.lnum > line2)
1188 win->w_cursor.lnum += amount_after;
1189 }
1190
1191#ifdef FEAT_FOLDING
1192 /* adjust folds */
1193 foldMarkAdjust(win, line1, line2, amount, amount_after);
1194#endif
1195 }
1196 }
1197
1198#ifdef FEAT_DIFF
1199 /* adjust diffs */
1200 diff_mark_adjust(line1, line2, amount, amount_after);
1201#endif
1202}
1203
1204/* This code is used often, needs to be fast. */
1205#define col_adjust(pp) \
1206 { \
1207 posp = pp; \
1208 if (posp->lnum == lnum && posp->col >= mincol) \
1209 { \
1210 posp->lnum += lnum_amount; \
1211 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1212 posp->col = 0; \
1213 else \
1214 posp->col += col_amount; \
1215 } \
1216 }
1217
1218/*
1219 * Adjust marks in line "lnum" at column "mincol" and further: add
1220 * "lnum_amount" to the line number and add "col_amount" to the column
1221 * position.
1222 */
1223 void
1224mark_col_adjust(lnum, mincol, lnum_amount, col_amount)
1225 linenr_T lnum;
1226 colnr_T mincol;
1227 long lnum_amount;
1228 long col_amount;
1229{
1230 int i;
1231 int fnum = curbuf->b_fnum;
1232 win_T *win;
1233 pos_T *posp;
1234
1235 if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks)
1236 return; /* nothing to do */
1237
1238 /* named marks, lower case and upper case */
1239 for (i = 0; i < NMARKS; i++)
1240 {
1241 col_adjust(&(curbuf->b_namedm[i]));
1242 if (namedfm[i].fmark.fnum == fnum)
1243 col_adjust(&(namedfm[i].fmark.mark));
1244 }
1245 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1246 {
1247 if (namedfm[i].fmark.fnum == fnum)
1248 col_adjust(&(namedfm[i].fmark.mark));
1249 }
1250
1251 /* last Insert position */
1252 col_adjust(&(curbuf->b_last_insert));
1253
1254 /* last change position */
1255 col_adjust(&(curbuf->b_last_change));
1256
1257#ifdef FEAT_JUMPLIST
1258 /* list of change positions */
1259 for (i = 0; i < curbuf->b_changelistlen; ++i)
1260 col_adjust(&(curbuf->b_changelist[i]));
1261#endif
1262
1263#ifdef FEAT_VISUAL
1264 /* Visual area */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001265 col_adjust(&(curbuf->b_visual.vi_start));
1266 col_adjust(&(curbuf->b_visual.vi_end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#endif
1268
1269 /* previous context mark */
1270 col_adjust(&(curwin->w_pcmark));
1271
1272 /* previous pcmark */
1273 col_adjust(&(curwin->w_prev_pcmark));
1274
1275 /* saved cursor for formatting */
1276 col_adjust(&saved_cursor);
1277
1278 /*
1279 * Adjust items in all windows related to the current buffer.
1280 */
1281 FOR_ALL_WINDOWS(win)
1282 {
1283#ifdef FEAT_JUMPLIST
1284 /* marks in the jumplist */
1285 for (i = 0; i < win->w_jumplistlen; ++i)
1286 if (win->w_jumplist[i].fmark.fnum == fnum)
1287 col_adjust(&(win->w_jumplist[i].fmark.mark));
1288#endif
1289
1290 if (win->w_buffer == curbuf)
1291 {
1292 /* marks in the tag stack */
1293 for (i = 0; i < win->w_tagstacklen; i++)
1294 if (win->w_tagstack[i].fmark.fnum == fnum)
1295 col_adjust(&(win->w_tagstack[i].fmark.mark));
1296
1297 /* cursor position for other windows with the same buffer */
1298 if (win != curwin)
1299 col_adjust(&win->w_cursor);
1300 }
1301 }
1302}
1303
1304#ifdef FEAT_JUMPLIST
1305/*
1306 * When deleting lines, this may create duplicate marks in the
1307 * jumplist. They will be removed here for the current window.
1308 */
1309 static void
1310cleanup_jumplist()
1311{
1312 int i;
1313 int from, to;
1314
1315 to = 0;
1316 for (from = 0; from < curwin->w_jumplistlen; ++from)
1317 {
1318 if (curwin->w_jumplistidx == from)
1319 curwin->w_jumplistidx = to;
1320 for (i = from + 1; i < curwin->w_jumplistlen; ++i)
1321 if (curwin->w_jumplist[i].fmark.fnum
1322 == curwin->w_jumplist[from].fmark.fnum
1323 && curwin->w_jumplist[from].fmark.fnum != 0
1324 && curwin->w_jumplist[i].fmark.mark.lnum
1325 == curwin->w_jumplist[from].fmark.mark.lnum)
1326 break;
1327 if (i >= curwin->w_jumplistlen) /* no duplicate */
1328 curwin->w_jumplist[to++] = curwin->w_jumplist[from];
1329 else
1330 vim_free(curwin->w_jumplist[from].fname);
1331 }
1332 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
1333 curwin->w_jumplistidx = to;
1334 curwin->w_jumplistlen = to;
1335}
1336
1337# if defined(FEAT_WINDOWS) || defined(PROTO)
1338/*
1339 * Copy the jumplist from window "from" to window "to".
1340 */
1341 void
1342copy_jumplist(from, to)
1343 win_T *from;
1344 win_T *to;
1345{
1346 int i;
1347
1348 for (i = 0; i < from->w_jumplistlen; ++i)
1349 {
1350 to->w_jumplist[i] = from->w_jumplist[i];
1351 if (from->w_jumplist[i].fname != NULL)
1352 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1353 }
1354 to->w_jumplistlen = from->w_jumplistlen;
1355 to->w_jumplistidx = from->w_jumplistidx;
1356}
1357
1358/*
1359 * Free items in the jumplist of window "wp".
1360 */
1361 void
1362free_jumplist(wp)
1363 win_T *wp;
1364{
1365 int i;
1366
1367 for (i = 0; i < wp->w_jumplistlen; ++i)
1368 vim_free(wp->w_jumplist[i].fname);
1369}
1370# endif
1371#endif /* FEAT_JUMPLIST */
1372
1373 void
1374set_last_cursor(win)
1375 win_T *win;
1376{
1377 win->w_buffer->b_last_cursor = win->w_cursor;
1378}
1379
Bram Moolenaarea408852005-06-25 22:49:46 +00001380#if defined(EXITFREE) || defined(PROTO)
1381 void
1382free_all_marks()
1383{
1384 int i;
1385
1386 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1387 if (namedfm[i].fmark.mark.lnum != 0)
1388 vim_free(namedfm[i].fname);
1389}
1390#endif
1391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#if defined(FEAT_VIMINFO) || defined(PROTO)
1393 int
1394read_viminfo_filemark(virp, force)
1395 vir_T *virp;
1396 int force;
1397{
1398 char_u *str;
1399 xfmark_T *fm;
1400 int i;
1401
1402 /* We only get here if line[0] == '\'' or '-'.
1403 * Illegal mark names are ignored (for future expansion). */
1404 str = virp->vir_line + 1;
1405 if (
1406#ifndef EBCDIC
1407 *str <= 127 &&
1408#endif
1409 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1410 || (*virp->vir_line == '-' && *str == '\'')))
1411 {
1412 if (*str == '\'')
1413 {
1414#ifdef FEAT_JUMPLIST
1415 /* If the jumplist isn't full insert fmark as oldest entry */
1416 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1417 fm = NULL;
1418 else
1419 {
1420 for (i = curwin->w_jumplistlen; i > 0; --i)
1421 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1422 ++curwin->w_jumplistidx;
1423 ++curwin->w_jumplistlen;
1424 fm = &curwin->w_jumplist[0];
1425 fm->fmark.mark.lnum = 0;
1426 fm->fname = NULL;
1427 }
1428#else
1429 fm = NULL;
1430#endif
1431 }
1432 else if (VIM_ISDIGIT(*str))
1433 fm = &namedfm[*str - '0' + NMARKS];
1434 else
1435 fm = &namedfm[*str - 'A'];
1436 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1437 {
1438 str = skipwhite(str + 1);
1439 fm->fmark.mark.lnum = getdigits(&str);
1440 str = skipwhite(str);
1441 fm->fmark.mark.col = getdigits(&str);
1442#ifdef FEAT_VIRTUALEDIT
1443 fm->fmark.mark.coladd = 0;
1444#endif
1445 fm->fmark.fnum = 0;
1446 str = skipwhite(str);
1447 vim_free(fm->fname);
1448 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1449 FALSE);
1450 }
1451 }
1452 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1453}
1454
1455 void
1456write_viminfo_filemarks(fp)
1457 FILE *fp;
1458{
1459 int i;
1460 char_u *name;
1461 buf_T *buf;
1462 xfmark_T *fm;
1463
1464 if (get_viminfo_parameter('f') == 0)
1465 return;
1466
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001467 fputs(_("\n# File marks:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
1469 /*
1470 * Find a mark that is the same file and position as the cursor.
1471 * That one, or else the last one is deleted.
1472 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1473 * Set '0 mark to current cursor position.
1474 */
1475 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1476 {
1477 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1478 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1479 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1480 && (namedfm[i].fname == NULL
1481 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1482 : (name != NULL
1483 && STRCMP(name, namedfm[i].fname) == 0)))
1484 break;
1485 vim_free(name);
1486
1487 vim_free(namedfm[i].fname);
1488 for ( ; i > NMARKS; --i)
1489 namedfm[i] = namedfm[i - 1];
1490 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1491 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1492 namedfm[NMARKS].fname = NULL;
1493 }
1494
1495 /* Write the filemarks '0 - '9 and 'A - 'Z */
1496 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1497 write_one_filemark(fp, &namedfm[i], '\'',
1498 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1499
1500#ifdef FEAT_JUMPLIST
1501 /* Write the jumplist with -' */
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001502 fputs(_("\n# Jumplist (newest first):\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 setpcmark(); /* add current cursor position */
1504 cleanup_jumplist();
1505 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1506 fm >= &curwin->w_jumplist[0]; --fm)
1507 {
1508 if (fm->fmark.fnum == 0
1509 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1510 && !removable(buf->b_ffname)))
1511 write_one_filemark(fp, fm, '-', '\'');
1512 }
1513#endif
1514}
1515
1516 static void
1517write_one_filemark(fp, fm, c1, c2)
1518 FILE *fp;
1519 xfmark_T *fm;
1520 int c1;
1521 int c2;
1522{
1523 char_u *name;
1524
1525 if (fm->fmark.mark.lnum == 0) /* not set */
1526 return;
1527
1528 if (fm->fmark.fnum != 0) /* there is a buffer */
1529 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1530 else
1531 name = fm->fname; /* use name from .viminfo */
1532 if (name != NULL && *name != NUL)
1533 {
1534 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1535 (long)fm->fmark.mark.col);
1536 viminfo_writestring(fp, name);
1537 }
1538
1539 if (fm->fmark.fnum != 0)
1540 vim_free(name);
1541}
1542
1543/*
1544 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1545 */
1546 int
1547removable(name)
1548 char_u *name;
1549{
1550 char_u *p;
1551 char_u part[51];
1552 int retval = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001553 size_t n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 name = home_replace_save(NULL, name);
1556 if (name != NULL)
1557 {
1558 for (p = p_viminfo; *p; )
1559 {
1560 copy_option_part(&p, part, 51, ", ");
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001561 if (part[0] == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001563 n = STRLEN(part + 1);
1564 if (MB_STRNICMP(part + 1, name, n) == 0)
1565 {
1566 retval = TRUE;
1567 break;
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 }
1571 vim_free(name);
1572 }
1573 return retval;
1574}
1575
1576static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1577
1578/*
1579 * Write all the named marks for all buffers.
1580 * Return the number of buffers for which marks have been written.
1581 */
1582 int
1583write_viminfo_marks(fp_out)
1584 FILE *fp_out;
1585{
1586 int count;
1587 buf_T *buf;
1588 int is_mark_set;
1589 int i;
1590#ifdef FEAT_WINDOWS
1591 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001592 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 /*
1595 * Set b_last_cursor for the all buffers that have a window.
1596 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001597 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 set_last_cursor(win);
1599#else
1600 set_last_cursor(curwin);
1601#endif
1602
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02001603 fputs(_("\n# History of marks within files (newest to oldest):\n"), fp_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 count = 0;
1605 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1606 {
1607 /*
1608 * Only write something if buffer has been loaded and at least one
1609 * mark is set.
1610 */
1611 if (buf->b_marks_read)
1612 {
1613 if (buf->b_last_cursor.lnum != 0)
1614 is_mark_set = TRUE;
1615 else
1616 {
1617 is_mark_set = FALSE;
1618 for (i = 0; i < NMARKS; i++)
1619 if (buf->b_namedm[i].lnum != 0)
1620 {
1621 is_mark_set = TRUE;
1622 break;
1623 }
1624 }
1625 if (is_mark_set && buf->b_ffname != NULL
1626 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1627 {
1628 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1629 fprintf(fp_out, "\n> ");
1630 viminfo_writestring(fp_out, IObuff);
1631 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1632 write_one_mark(fp_out, '^', &buf->b_last_insert);
1633 write_one_mark(fp_out, '.', &buf->b_last_change);
1634#ifdef FEAT_JUMPLIST
1635 /* changelist positions are stored oldest first */
1636 for (i = 0; i < buf->b_changelistlen; ++i)
1637 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1638#endif
1639 for (i = 0; i < NMARKS; i++)
1640 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1641 count++;
1642 }
1643 }
1644 }
1645
1646 return count;
1647}
1648
1649 static void
1650write_one_mark(fp_out, c, pos)
1651 FILE *fp_out;
1652 int c;
1653 pos_T *pos;
1654{
1655 if (pos->lnum != 0)
1656 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1657}
1658
1659/*
1660 * Handle marks in the viminfo file:
Bram Moolenaard812df62008-11-09 12:46:09 +00001661 * fp_out != NULL: copy marks for buffers not in buffer list
1662 * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf only
1663 * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 */
1665 void
Bram Moolenaard812df62008-11-09 12:46:09 +00001666copy_viminfo_marks(virp, fp_out, count, eof, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 vir_T *virp;
1668 FILE *fp_out;
1669 int count;
1670 int eof;
Bram Moolenaard812df62008-11-09 12:46:09 +00001671 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 char_u *line = virp->vir_line;
1674 buf_T *buf;
1675 int num_marked_files;
1676 int load_marks;
1677 int copy_marks_out;
1678 char_u *str;
1679 int i;
1680 char_u *p;
1681 char_u *name_buf;
1682 pos_T pos;
Bram Moolenaard812df62008-11-09 12:46:09 +00001683#ifdef FEAT_EVAL
1684 list_T *list = NULL;
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687 if ((name_buf = alloc(LSIZE)) == NULL)
1688 return;
1689 *name_buf = NUL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001690
1691#ifdef FEAT_EVAL
1692 if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT)))
1693 {
1694 list = list_alloc();
1695 if (list != NULL)
1696 set_vim_var_list(VV_OLDFILES, list);
1697 }
1698#endif
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 num_marked_files = get_viminfo_parameter('\'');
1701 while (!eof && (count < num_marked_files || fp_out == NULL))
1702 {
1703 if (line[0] != '>')
1704 {
1705 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1706 {
1707 if (viminfo_error("E576: ", _("Missing '>'"), line))
1708 break; /* too many errors, return now */
1709 }
1710 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1711 continue; /* Skip this dud line */
1712 }
1713
1714 /*
1715 * Handle long line and translate escaped characters.
1716 * Find file name, set str to start.
1717 * Ignore leading and trailing white space.
1718 */
1719 str = skipwhite(line + 1);
1720 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1721 if (str == NULL)
1722 continue;
1723 p = str + STRLEN(str);
1724 while (p != str && (*p == NUL || vim_isspace(*p)))
1725 p--;
1726 if (*p)
1727 p++;
1728 *p = NUL;
1729
Bram Moolenaard812df62008-11-09 12:46:09 +00001730#ifdef FEAT_EVAL
1731 if (list != NULL)
1732 list_append_string(list, str, -1);
1733#endif
1734
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 /*
1736 * If fp_out == NULL, load marks for current buffer.
1737 * If fp_out != NULL, copy marks for buffers not in buflist.
1738 */
1739 load_marks = copy_marks_out = FALSE;
1740 if (fp_out == NULL)
1741 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001742 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 {
1744 if (*name_buf == NUL) /* only need to do this once */
1745 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1746 if (fnamecmp(str, name_buf) == 0)
1747 load_marks = TRUE;
1748 }
1749 }
1750 else /* fp_out != NULL */
1751 {
1752 /* This is slow if there are many buffers!! */
1753 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1754 if (buf->b_ffname != NULL)
1755 {
1756 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1757 if (fnamecmp(str, name_buf) == 0)
1758 break;
1759 }
1760
1761 /*
1762 * copy marks if the buffer has not been loaded
1763 */
1764 if (buf == NULL || !buf->b_marks_read)
1765 {
1766 copy_marks_out = TRUE;
1767 fputs("\n> ", fp_out);
1768 viminfo_writestring(fp_out, str);
1769 count++;
1770 }
1771 }
1772 vim_free(str);
1773
1774#ifdef FEAT_VIRTUALEDIT
1775 pos.coladd = 0;
1776#endif
1777 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1778 {
1779 if (load_marks)
1780 {
1781 if (line[1] != NUL)
1782 {
Bram Moolenaare698add2011-02-25 15:11:22 +01001783 unsigned u;
1784
1785 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &u);
1786 pos.col = u;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 switch (line[1])
1788 {
1789 case '"': curbuf->b_last_cursor = pos; break;
1790 case '^': curbuf->b_last_insert = pos; break;
1791 case '.': curbuf->b_last_change = pos; break;
1792 case '+':
1793#ifdef FEAT_JUMPLIST
1794 /* changelist positions are stored oldest
1795 * first */
1796 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1797 /* list is full, remove oldest entry */
1798 mch_memmove(curbuf->b_changelist,
1799 curbuf->b_changelist + 1,
1800 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1801 else
1802 ++curbuf->b_changelistlen;
1803 curbuf->b_changelist[
1804 curbuf->b_changelistlen - 1] = pos;
1805#endif
1806 break;
1807 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1808 curbuf->b_namedm[i] = pos;
1809 }
1810 }
1811 }
1812 else if (copy_marks_out)
1813 fputs((char *)line, fp_out);
1814 }
1815 if (load_marks)
1816 {
1817#ifdef FEAT_JUMPLIST
1818 win_T *wp;
1819
1820 FOR_ALL_WINDOWS(wp)
1821 {
1822 if (wp->w_buffer == curbuf)
1823 wp->w_changelistidx = curbuf->b_changelistlen;
1824 }
1825#endif
1826 break;
1827 }
1828 }
1829 vim_free(name_buf);
1830}
1831#endif /* FEAT_VIMINFO */