Update Services for Snow Leopard
[MacVim.git] / src / mark.c
blobae31cf0fec74b690e8699617257e119fb9f57690
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * mark.c: functions for setting marks and jumping to them
14 #include "vim.h"
17 * This file contains routines to maintain and manipulate marks.
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).
27 #define EXTRA_MARKS 10 /* marks 0-9 */
28 static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */
30 static void fname2fnum __ARGS((xfmark_T *fm));
31 static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
32 static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
33 static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
34 #ifdef FEAT_JUMPLIST
35 static void cleanup_jumplist __ARGS((void));
36 #endif
37 #ifdef FEAT_VIMINFO
38 static void write_one_filemark __ARGS((FILE *fp, xfmark_T *fm, int c1, int c2));
39 #endif
42 * Set named mark "c" at current cursor position.
43 * Returns OK on success, FAIL if bad name given.
45 int
46 setmark(c)
47 int c;
49 return setmark_pos(c, &curwin->w_cursor, curbuf->b_fnum);
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.
57 int
58 setmark_pos(c, pos, fnum)
59 int c;
60 pos_T *pos;
61 int fnum;
63 int i;
65 /* Check for a special key (may cause islower() to crash). */
66 if (c < 0)
67 return FAIL;
69 if (c == '\'' || c == '`')
71 if (pos == &curwin->w_cursor)
73 setpcmark();
74 /* keep it even when the cursor doesn't move */
75 curwin->w_prev_pcmark = curwin->w_pcmark;
77 else
78 curwin->w_pcmark = *pos;
79 return OK;
82 if (c == '"')
84 curbuf->b_last_cursor = *pos;
85 return OK;
88 /* Allow setting '[ and '] for an autocommand that simulates reading a
89 * file. */
90 if (c == '[')
92 curbuf->b_op_start = *pos;
93 return OK;
95 if (c == ']')
97 curbuf->b_op_end = *pos;
98 return OK;
101 #ifndef EBCDIC
102 if (c > 'z') /* some islower() and isupper() cannot handle
103 characters above 127 */
104 return FAIL;
105 #endif
106 if (islower(c))
108 i = c - 'a';
109 curbuf->b_namedm[i] = *pos;
110 return OK;
112 if (isupper(c))
114 i = c - 'A';
115 namedfm[i].fmark.mark = *pos;
116 namedfm[i].fmark.fnum = fnum;
117 vim_free(namedfm[i].fname);
118 namedfm[i].fname = NULL;
119 return OK;
121 return FAIL;
125 * Set the previous context mark to the current position and add it to the
126 * jump list.
128 void
129 setpcmark()
131 #ifdef FEAT_JUMPLIST
132 int i;
133 xfmark_T *fm;
134 #endif
135 #ifdef JUMPLIST_ROTATE
136 xfmark_T tempmark;
137 #endif
139 /* for :global the mark is set only once */
140 if (global_busy || listcmd_busy || cmdmod.keepjumps)
141 return;
143 curwin->w_prev_pcmark = curwin->w_pcmark;
144 curwin->w_pcmark = curwin->w_cursor;
146 #ifdef FEAT_JUMPLIST
147 # ifdef JUMPLIST_ROTATE
149 * If last used entry is not at the top, put it at the top by rotating
150 * the stack until it is (the newer entries will be at the bottom).
151 * Keep one entry (the last used one) at the top.
153 if (curwin->w_jumplistidx < curwin->w_jumplistlen)
154 ++curwin->w_jumplistidx;
155 while (curwin->w_jumplistidx < curwin->w_jumplistlen)
157 tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
158 for (i = curwin->w_jumplistlen - 1; i > 0; --i)
159 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
160 curwin->w_jumplist[0] = tempmark;
161 ++curwin->w_jumplistidx;
163 # endif
165 /* If jumplist is full: remove oldest entry */
166 if (++curwin->w_jumplistlen > JUMPLISTSIZE)
168 curwin->w_jumplistlen = JUMPLISTSIZE;
169 vim_free(curwin->w_jumplist[0].fname);
170 for (i = 1; i < JUMPLISTSIZE; ++i)
171 curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
173 curwin->w_jumplistidx = curwin->w_jumplistlen;
174 fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
176 fm->fmark.mark = curwin->w_pcmark;
177 fm->fmark.fnum = curbuf->b_fnum;
178 fm->fname = NULL;
179 #endif
183 * To change context, call setpcmark(), then move the current position to
184 * where ever, then call checkpcmark(). This ensures that the previous
185 * context will only be changed if the cursor moved to a different line.
186 * If pcmark was deleted (with "dG") the previous mark is restored.
188 void
189 checkpcmark()
191 if (curwin->w_prev_pcmark.lnum != 0
192 && (equalpos(curwin->w_pcmark, curwin->w_cursor)
193 || curwin->w_pcmark.lnum == 0))
195 curwin->w_pcmark = curwin->w_prev_pcmark;
196 curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */
200 #if defined(FEAT_JUMPLIST) || defined(PROTO)
202 * move "count" positions in the jump list (count may be negative)
204 pos_T *
205 movemark(count)
206 int count;
208 pos_T *pos;
209 xfmark_T *jmp;
211 cleanup_jumplist();
213 if (curwin->w_jumplistlen == 0) /* nothing to jump to */
214 return (pos_T *)NULL;
216 for (;;)
218 if (curwin->w_jumplistidx + count < 0
219 || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
220 return (pos_T *)NULL;
223 * if first CTRL-O or CTRL-I command after a jump, add cursor position
224 * to list. Careful: If there are duplicates (CTRL-O immediately after
225 * starting Vim on a file), another entry may have been removed.
227 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
229 setpcmark();
230 --curwin->w_jumplistidx; /* skip the new entry */
231 if (curwin->w_jumplistidx + count < 0)
232 return (pos_T *)NULL;
235 curwin->w_jumplistidx += count;
237 jmp = curwin->w_jumplist + curwin->w_jumplistidx;
238 if (jmp->fmark.fnum == 0)
239 fname2fnum(jmp);
240 if (jmp->fmark.fnum != curbuf->b_fnum)
242 /* jump to other file */
243 if (buflist_findnr(jmp->fmark.fnum) == NULL)
244 { /* Skip this one .. */
245 count += count < 0 ? -1 : 1;
246 continue;
248 if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
249 0, FALSE) == FAIL)
250 return (pos_T *)NULL;
251 /* Set lnum again, autocommands my have changed it */
252 curwin->w_cursor = jmp->fmark.mark;
253 pos = (pos_T *)-1;
255 else
256 pos = &(jmp->fmark.mark);
257 return pos;
262 * Move "count" positions in the changelist (count may be negative).
264 pos_T *
265 movechangelist(count)
266 int count;
268 int n;
270 if (curbuf->b_changelistlen == 0) /* nothing to jump to */
271 return (pos_T *)NULL;
273 n = curwin->w_changelistidx;
274 if (n + count < 0)
276 if (n == 0)
277 return (pos_T *)NULL;
278 n = 0;
280 else if (n + count >= curbuf->b_changelistlen)
282 if (n == curbuf->b_changelistlen - 1)
283 return (pos_T *)NULL;
284 n = curbuf->b_changelistlen - 1;
286 else
287 n += count;
288 curwin->w_changelistidx = n;
289 return curbuf->b_changelist + n;
291 #endif
294 * Find mark "c".
295 * If "changefile" is TRUE it's allowed to edit another file for '0, 'A, etc.
296 * If "fnum" is not NULL store the fnum there for '0, 'A etc., don't edit
297 * another file.
298 * Returns:
299 * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is
300 * in another file which can't be gotten. (caller needs to check lnum!)
301 * - NULL if there is no mark called 'c'.
302 * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
304 pos_T *
305 getmark(c, changefile)
306 int c;
307 int changefile;
309 return getmark_fnum(c, changefile, NULL);
312 pos_T *
313 getmark_fnum(c, changefile, fnum)
314 int c;
315 int changefile;
316 int *fnum;
318 pos_T *posp;
319 #ifdef FEAT_VISUAL
320 pos_T *startp, *endp;
321 #endif
322 static pos_T pos_copy;
324 posp = NULL;
326 /* Check for special key, can't be a mark name and might cause islower()
327 * to crash. */
328 if (c < 0)
329 return posp;
330 #ifndef EBCDIC
331 if (c > '~') /* check for islower()/isupper() */
333 else
334 #endif
335 if (c == '\'' || c == '`') /* previous context mark */
337 pos_copy = curwin->w_pcmark; /* need to make a copy because */
338 posp = &pos_copy; /* w_pcmark may be changed soon */
340 else if (c == '"') /* to pos when leaving buffer */
341 posp = &(curbuf->b_last_cursor);
342 else if (c == '^') /* to where Insert mode stopped */
343 posp = &(curbuf->b_last_insert);
344 else if (c == '.') /* to where last change was made */
345 posp = &(curbuf->b_last_change);
346 else if (c == '[') /* to start of previous operator */
347 posp = &(curbuf->b_op_start);
348 else if (c == ']') /* to end of previous operator */
349 posp = &(curbuf->b_op_end);
350 else if (c == '{' || c == '}') /* to previous/next paragraph */
352 pos_T pos;
353 oparg_T oa;
354 int slcb = listcmd_busy;
356 pos = curwin->w_cursor;
357 listcmd_busy = TRUE; /* avoid that '' is changed */
358 if (findpar(&oa.inclusive,
359 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
361 pos_copy = curwin->w_cursor;
362 posp = &pos_copy;
364 curwin->w_cursor = pos;
365 listcmd_busy = slcb;
367 else if (c == '(' || c == ')') /* to previous/next sentence */
369 pos_T pos;
370 int slcb = listcmd_busy;
372 pos = curwin->w_cursor;
373 listcmd_busy = TRUE; /* avoid that '' is changed */
374 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
376 pos_copy = curwin->w_cursor;
377 posp = &pos_copy;
379 curwin->w_cursor = pos;
380 listcmd_busy = slcb;
382 #ifdef FEAT_VISUAL
383 else if (c == '<' || c == '>') /* start/end of visual area */
385 startp = &curbuf->b_visual.vi_start;
386 endp = &curbuf->b_visual.vi_end;
387 if ((c == '<') == lt(*startp, *endp))
388 posp = startp;
389 else
390 posp = endp;
392 * For Visual line mode, set mark at begin or end of line
394 if (curbuf->b_visual.vi_mode == 'V')
396 pos_copy = *posp;
397 posp = &pos_copy;
398 if (c == '<')
399 pos_copy.col = 0;
400 else
401 pos_copy.col = MAXCOL;
402 #ifdef FEAT_VIRTUALEDIT
403 pos_copy.coladd = 0;
404 #endif
407 #endif
408 else if (ASCII_ISLOWER(c)) /* normal named mark */
410 posp = &(curbuf->b_namedm[c - 'a']);
412 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */
414 if (VIM_ISDIGIT(c))
415 c = c - '0' + NMARKS;
416 else
417 c -= 'A';
418 posp = &(namedfm[c].fmark.mark);
420 if (namedfm[c].fmark.fnum == 0)
421 fname2fnum(&namedfm[c]);
423 if (fnum != NULL)
424 *fnum = namedfm[c].fmark.fnum;
425 else if (namedfm[c].fmark.fnum != curbuf->b_fnum)
427 /* mark is in another file */
428 posp = &pos_copy;
430 if (namedfm[c].fmark.mark.lnum != 0
431 && changefile && namedfm[c].fmark.fnum)
433 if (buflist_getfile(namedfm[c].fmark.fnum,
434 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
436 /* Set the lnum now, autocommands could have changed it */
437 curwin->w_cursor = namedfm[c].fmark.mark;
438 return (pos_T *)-1;
440 pos_copy.lnum = -1; /* can't get file */
442 else
443 pos_copy.lnum = 0; /* mark exists, but is not valid in
444 current buffer */
448 return posp;
452 * Search for the next named mark in the current file.
454 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
456 pos_T *
457 getnextmark(startpos, dir, begin_line)
458 pos_T *startpos; /* where to start */
459 int dir; /* direction for search */
460 int begin_line;
462 int i;
463 pos_T *result = NULL;
464 pos_T pos;
466 pos = *startpos;
468 /* When searching backward and leaving the cursor on the first non-blank,
469 * position must be in a previous line.
470 * When searching forward and leaving the cursor on the first non-blank,
471 * position must be in a next line. */
472 if (dir == BACKWARD && begin_line)
473 pos.col = 0;
474 else if (dir == FORWARD && begin_line)
475 pos.col = MAXCOL;
477 for (i = 0; i < NMARKS; i++)
479 if (curbuf->b_namedm[i].lnum > 0)
481 if (dir == FORWARD)
483 if ((result == NULL || lt(curbuf->b_namedm[i], *result))
484 && lt(pos, curbuf->b_namedm[i]))
485 result = &curbuf->b_namedm[i];
487 else
489 if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
490 && lt(curbuf->b_namedm[i], pos))
491 result = &curbuf->b_namedm[i];
496 return result;
500 * For an xtended filemark: set the fnum from the fname.
501 * This is used for marks obtained from the .viminfo file. It's postponed
502 * until the mark is used to avoid a long startup delay.
504 static void
505 fname2fnum(fm)
506 xfmark_T *fm;
508 char_u *p;
510 if (fm->fname != NULL)
513 * First expand "~/" in the file name to the home directory.
514 * Don't expand the whole name, it may contain other '~' chars.
516 if (fm->fname[0] == '~' && (fm->fname[1] == '/'
517 #ifdef BACKSLASH_IN_FILENAME
518 || fm->fname[1] == '\\'
519 #endif
522 int len;
524 expand_env((char_u *)"~/", NameBuff, MAXPATHL);
525 len = (int)STRLEN(NameBuff);
526 vim_strncpy(NameBuff + len, fm->fname + 2, MAXPATHL - len - 1);
528 else
529 vim_strncpy(NameBuff, fm->fname, MAXPATHL - 1);
531 /* Try to shorten the file name. */
532 mch_dirname(IObuff, IOSIZE);
533 p = shorten_fname(NameBuff, IObuff);
535 /* buflist_new() will call fmarks_check_names() */
536 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
541 * Check all file marks for a name that matches the file name in buf.
542 * May replace the name with an fnum.
543 * Used for marks that come from the .viminfo file.
545 void
546 fmarks_check_names(buf)
547 buf_T *buf;
549 char_u *name;
550 int i;
551 #ifdef FEAT_JUMPLIST
552 win_T *wp;
553 #endif
555 if (buf->b_ffname == NULL)
556 return;
558 name = home_replace_save(buf, buf->b_ffname);
559 if (name == NULL)
560 return;
562 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
563 fmarks_check_one(&namedfm[i], name, buf);
565 #ifdef FEAT_JUMPLIST
566 FOR_ALL_WINDOWS(wp)
568 for (i = 0; i < wp->w_jumplistlen; ++i)
569 fmarks_check_one(&wp->w_jumplist[i], name, buf);
571 #endif
573 vim_free(name);
576 static void
577 fmarks_check_one(fm, name, buf)
578 xfmark_T *fm;
579 char_u *name;
580 buf_T *buf;
582 if (fm->fmark.fnum == 0
583 && fm->fname != NULL
584 && fnamecmp(name, fm->fname) == 0)
586 fm->fmark.fnum = buf->b_fnum;
587 vim_free(fm->fname);
588 fm->fname = NULL;
593 * Check a if a position from a mark is valid.
594 * Give and error message and return FAIL if not.
597 check_mark(pos)
598 pos_T *pos;
600 if (pos == NULL)
602 EMSG(_(e_umark));
603 return FAIL;
605 if (pos->lnum <= 0)
607 /* lnum is negative if mark is in another file can can't get that
608 * file, error message already give then. */
609 if (pos->lnum == 0)
610 EMSG(_(e_marknotset));
611 return FAIL;
613 if (pos->lnum > curbuf->b_ml.ml_line_count)
615 EMSG(_(e_markinval));
616 return FAIL;
618 return OK;
622 * clrallmarks() - clear all marks in the buffer 'buf'
624 * Used mainly when trashing the entire buffer during ":e" type commands
626 void
627 clrallmarks(buf)
628 buf_T *buf;
630 static int i = -1;
632 if (i == -1) /* first call ever: initialize */
633 for (i = 0; i < NMARKS + 1; i++)
635 namedfm[i].fmark.mark.lnum = 0;
636 namedfm[i].fname = NULL;
639 for (i = 0; i < NMARKS; i++)
640 buf->b_namedm[i].lnum = 0;
641 buf->b_op_start.lnum = 0; /* start/end op mark cleared */
642 buf->b_op_end.lnum = 0;
643 buf->b_last_cursor.lnum = 1; /* '" mark cleared */
644 buf->b_last_cursor.col = 0;
645 #ifdef FEAT_VIRTUALEDIT
646 buf->b_last_cursor.coladd = 0;
647 #endif
648 buf->b_last_insert.lnum = 0; /* '^ mark cleared */
649 buf->b_last_change.lnum = 0; /* '. mark cleared */
650 #ifdef FEAT_JUMPLIST
651 buf->b_changelistlen = 0;
652 #endif
656 * Get name of file from a filemark.
657 * When it's in the current buffer, return the text at the mark.
658 * Returns an allocated string.
660 char_u *
661 fm_getname(fmark, lead_len)
662 fmark_T *fmark;
663 int lead_len;
665 if (fmark->fnum == curbuf->b_fnum) /* current buffer */
666 return mark_line(&(fmark->mark), lead_len);
667 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
671 * Return the line at mark "mp". Truncate to fit in window.
672 * The returned string has been allocated.
674 static char_u *
675 mark_line(mp, lead_len)
676 pos_T *mp;
677 int lead_len;
679 char_u *s, *p;
680 int len;
682 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
683 return vim_strsave((char_u *)"-invalid-");
684 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
685 if (s == NULL)
686 return NULL;
687 /* Truncate the line to fit it in the window */
688 len = 0;
689 for (p = s; *p != NUL; mb_ptr_adv(p))
691 len += ptr2cells(p);
692 if (len >= Columns - lead_len)
693 break;
695 *p = NUL;
696 return s;
700 * print the marks
702 void
703 do_marks(eap)
704 exarg_T *eap;
706 char_u *arg = eap->arg;
707 int i;
708 char_u *name;
710 if (arg != NULL && *arg == NUL)
711 arg = NULL;
713 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
714 for (i = 0; i < NMARKS; ++i)
715 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
716 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
718 if (namedfm[i].fmark.fnum != 0)
719 name = fm_getname(&namedfm[i].fmark, 15);
720 else
721 name = namedfm[i].fname;
722 if (name != NULL)
724 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
725 arg, &namedfm[i].fmark.mark, name,
726 namedfm[i].fmark.fnum == curbuf->b_fnum);
727 if (namedfm[i].fmark.fnum != 0)
728 vim_free(name);
731 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
732 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
733 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
734 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
735 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
736 #ifdef FEAT_VISUAL
737 show_one_mark('<', arg, &curbuf->b_visual.vi_start, NULL, TRUE);
738 show_one_mark('>', arg, &curbuf->b_visual.vi_end, NULL, TRUE);
739 #endif
740 show_one_mark(-1, arg, NULL, NULL, FALSE);
743 static void
744 show_one_mark(c, arg, p, name, current)
745 int c;
746 char_u *arg;
747 pos_T *p;
748 char_u *name;
749 int current; /* in current file */
751 static int did_title = FALSE;
752 int mustfree = FALSE;
754 if (c == -1) /* finish up */
756 if (did_title)
757 did_title = FALSE;
758 else
760 if (arg == NULL)
761 MSG(_("No marks set"));
762 else
763 EMSG2(_("E283: No marks matching \"%s\""), arg);
766 /* don't output anything if 'q' typed at --more-- prompt */
767 else if (!got_int
768 && (arg == NULL || vim_strchr(arg, c) != NULL)
769 && p->lnum != 0)
771 if (!did_title)
773 /* Highlight title */
774 MSG_PUTS_TITLE(_("\nmark line col file/text"));
775 did_title = TRUE;
777 msg_putchar('\n');
778 if (!got_int)
780 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
781 msg_outtrans(IObuff);
782 if (name == NULL && current)
784 name = mark_line(p, 15);
785 mustfree = TRUE;
787 if (name != NULL)
789 msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
790 if (mustfree)
791 vim_free(name);
794 out_flush(); /* show one line at a time */
799 * ":delmarks[!] [marks]"
801 void
802 ex_delmarks(eap)
803 exarg_T *eap;
805 char_u *p;
806 int from, to;
807 int i;
808 int lower;
809 int digit;
810 int n;
812 if (*eap->arg == NUL && eap->forceit)
813 /* clear all marks */
814 clrallmarks(curbuf);
815 else if (eap->forceit)
816 EMSG(_(e_invarg));
817 else if (*eap->arg == NUL)
818 EMSG(_(e_argreq));
819 else
821 /* clear specified marks only */
822 for (p = eap->arg; *p != NUL; ++p)
824 lower = ASCII_ISLOWER(*p);
825 digit = VIM_ISDIGIT(*p);
826 if (lower || digit || ASCII_ISUPPER(*p))
828 if (p[1] == '-')
830 /* clear range of marks */
831 from = *p;
832 to = p[2];
833 if (!(lower ? ASCII_ISLOWER(p[2])
834 : (digit ? VIM_ISDIGIT(p[2])
835 : ASCII_ISUPPER(p[2])))
836 || to < from)
838 EMSG2(_(e_invarg2), p);
839 return;
841 p += 2;
843 else
844 /* clear one lower case mark */
845 from = to = *p;
847 for (i = from; i <= to; ++i)
849 if (lower)
850 curbuf->b_namedm[i - 'a'].lnum = 0;
851 else
853 if (digit)
854 n = i - '0' + NMARKS;
855 else
856 n = i - 'A';
857 namedfm[n].fmark.mark.lnum = 0;
858 vim_free(namedfm[n].fname);
859 namedfm[n].fname = NULL;
863 else
864 switch (*p)
866 case '"': curbuf->b_last_cursor.lnum = 0; break;
867 case '^': curbuf->b_last_insert.lnum = 0; break;
868 case '.': curbuf->b_last_change.lnum = 0; break;
869 case '[': curbuf->b_op_start.lnum = 0; break;
870 case ']': curbuf->b_op_end.lnum = 0; break;
871 #ifdef FEAT_VISUAL
872 case '<': curbuf->b_visual.vi_start.lnum = 0; break;
873 case '>': curbuf->b_visual.vi_end.lnum = 0; break;
874 #endif
875 case ' ': break;
876 default: EMSG2(_(e_invarg2), p);
877 return;
883 #if defined(FEAT_JUMPLIST) || defined(PROTO)
885 * print the jumplist
887 void
888 ex_jumps(eap)
889 exarg_T *eap UNUSED;
891 int i;
892 char_u *name;
894 cleanup_jumplist();
895 /* Highlight title */
896 MSG_PUTS_TITLE(_("\n jump line col file/text"));
897 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
899 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
901 if (curwin->w_jumplist[i].fmark.fnum == 0)
902 fname2fnum(&curwin->w_jumplist[i]);
903 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
904 if (name == NULL) /* file name not available */
905 continue;
907 msg_putchar('\n');
908 if (got_int)
910 vim_free(name);
911 break;
913 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
914 i == curwin->w_jumplistidx ? '>' : ' ',
915 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
916 : curwin->w_jumplistidx - i,
917 curwin->w_jumplist[i].fmark.mark.lnum,
918 curwin->w_jumplist[i].fmark.mark.col);
919 msg_outtrans(IObuff);
920 msg_outtrans_attr(name,
921 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
922 ? hl_attr(HLF_D) : 0);
923 vim_free(name);
924 ui_breakcheck();
926 out_flush();
928 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
929 MSG_PUTS("\n>");
933 * print the changelist
935 void
936 ex_changes(eap)
937 exarg_T *eap UNUSED;
939 int i;
940 char_u *name;
942 /* Highlight title */
943 MSG_PUTS_TITLE(_("\nchange line col text"));
945 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
947 if (curbuf->b_changelist[i].lnum != 0)
949 msg_putchar('\n');
950 if (got_int)
951 break;
952 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
953 i == curwin->w_changelistidx ? '>' : ' ',
954 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
955 : curwin->w_changelistidx - i,
956 (long)curbuf->b_changelist[i].lnum,
957 curbuf->b_changelist[i].col);
958 msg_outtrans(IObuff);
959 name = mark_line(&curbuf->b_changelist[i], 17);
960 if (name == NULL)
961 break;
962 msg_outtrans_attr(name, hl_attr(HLF_D));
963 vim_free(name);
964 ui_breakcheck();
966 out_flush();
968 if (curwin->w_changelistidx == curbuf->b_changelistlen)
969 MSG_PUTS("\n>");
971 #endif
973 #define one_adjust(add) \
975 lp = add; \
976 if (*lp >= line1 && *lp <= line2) \
978 if (amount == MAXLNUM) \
979 *lp = 0; \
980 else \
981 *lp += amount; \
983 else if (amount_after && *lp > line2) \
984 *lp += amount_after; \
987 /* don't delete the line, just put at first deleted line */
988 #define one_adjust_nodel(add) \
990 lp = add; \
991 if (*lp >= line1 && *lp <= line2) \
993 if (amount == MAXLNUM) \
994 *lp = line1; \
995 else \
996 *lp += amount; \
998 else if (amount_after && *lp > line2) \
999 *lp += amount_after; \
1003 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
1004 * Must be called before changed_*(), appended_lines() or deleted_lines().
1005 * May be called before or after changing the text.
1006 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
1007 * within this range are made invalid.
1008 * If 'amount_after' is non-zero adjust marks after line2.
1009 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
1010 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
1011 * or: mark_adjust(56, 55, MAXLNUM, 2);
1013 void
1014 mark_adjust(line1, line2, amount, amount_after)
1015 linenr_T line1;
1016 linenr_T line2;
1017 long amount;
1018 long amount_after;
1020 int i;
1021 int fnum = curbuf->b_fnum;
1022 linenr_T *lp;
1023 win_T *win;
1024 #ifdef FEAT_WINDOWS
1025 tabpage_T *tab;
1026 #endif
1028 if (line2 < line1 && amount_after == 0L) /* nothing to do */
1029 return;
1031 if (!cmdmod.lockmarks)
1033 /* named marks, lower case and upper case */
1034 for (i = 0; i < NMARKS; i++)
1036 one_adjust(&(curbuf->b_namedm[i].lnum));
1037 if (namedfm[i].fmark.fnum == fnum)
1038 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1040 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1042 if (namedfm[i].fmark.fnum == fnum)
1043 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
1046 /* last Insert position */
1047 one_adjust(&(curbuf->b_last_insert.lnum));
1049 /* last change position */
1050 one_adjust(&(curbuf->b_last_change.lnum));
1052 #ifdef FEAT_JUMPLIST
1053 /* list of change positions */
1054 for (i = 0; i < curbuf->b_changelistlen; ++i)
1055 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
1056 #endif
1058 #ifdef FEAT_VISUAL
1059 /* Visual area */
1060 one_adjust_nodel(&(curbuf->b_visual.vi_start.lnum));
1061 one_adjust_nodel(&(curbuf->b_visual.vi_end.lnum));
1062 #endif
1064 #ifdef FEAT_QUICKFIX
1065 /* quickfix marks */
1066 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
1067 /* location lists */
1068 FOR_ALL_TAB_WINDOWS(tab, win)
1069 qf_mark_adjust(win, line1, line2, amount, amount_after);
1070 #endif
1072 #ifdef FEAT_SIGNS
1073 sign_mark_adjust(line1, line2, amount, amount_after);
1074 #endif
1077 /* previous context mark */
1078 one_adjust(&(curwin->w_pcmark.lnum));
1080 /* previous pcmark */
1081 one_adjust(&(curwin->w_prev_pcmark.lnum));
1083 /* saved cursor for formatting */
1084 if (saved_cursor.lnum != 0)
1085 one_adjust_nodel(&(saved_cursor.lnum));
1088 * Adjust items in all windows related to the current buffer.
1090 FOR_ALL_TAB_WINDOWS(tab, win)
1092 #ifdef FEAT_JUMPLIST
1093 if (!cmdmod.lockmarks)
1094 /* Marks in the jumplist. When deleting lines, this may create
1095 * duplicate marks in the jumplist, they will be removed later. */
1096 for (i = 0; i < win->w_jumplistlen; ++i)
1097 if (win->w_jumplist[i].fmark.fnum == fnum)
1098 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
1099 #endif
1101 if (win->w_buffer == curbuf)
1103 if (!cmdmod.lockmarks)
1104 /* marks in the tag stack */
1105 for (i = 0; i < win->w_tagstacklen; i++)
1106 if (win->w_tagstack[i].fmark.fnum == fnum)
1107 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
1109 #ifdef FEAT_VISUAL
1110 /* the displayed Visual area */
1111 if (win->w_old_cursor_lnum != 0)
1113 one_adjust_nodel(&(win->w_old_cursor_lnum));
1114 one_adjust_nodel(&(win->w_old_visual_lnum));
1116 #endif
1118 /* topline and cursor position for windows with the same buffer
1119 * other than the current window */
1120 if (win != curwin)
1122 if (win->w_topline >= line1 && win->w_topline <= line2)
1124 if (amount == MAXLNUM) /* topline is deleted */
1126 if (line1 <= 1)
1127 win->w_topline = 1;
1128 else
1129 win->w_topline = line1 - 1;
1131 else /* keep topline on the same line */
1132 win->w_topline += amount;
1133 #ifdef FEAT_DIFF
1134 win->w_topfill = 0;
1135 #endif
1137 else if (amount_after && win->w_topline > line2)
1139 win->w_topline += amount_after;
1140 #ifdef FEAT_DIFF
1141 win->w_topfill = 0;
1142 #endif
1144 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1146 if (amount == MAXLNUM) /* line with cursor is deleted */
1148 if (line1 <= 1)
1149 win->w_cursor.lnum = 1;
1150 else
1151 win->w_cursor.lnum = line1 - 1;
1152 win->w_cursor.col = 0;
1154 else /* keep cursor on the same line */
1155 win->w_cursor.lnum += amount;
1157 else if (amount_after && win->w_cursor.lnum > line2)
1158 win->w_cursor.lnum += amount_after;
1161 #ifdef FEAT_FOLDING
1162 /* adjust folds */
1163 foldMarkAdjust(win, line1, line2, amount, amount_after);
1164 #endif
1168 #ifdef FEAT_DIFF
1169 /* adjust diffs */
1170 diff_mark_adjust(line1, line2, amount, amount_after);
1171 #endif
1174 /* This code is used often, needs to be fast. */
1175 #define col_adjust(pp) \
1177 posp = pp; \
1178 if (posp->lnum == lnum && posp->col >= mincol) \
1180 posp->lnum += lnum_amount; \
1181 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1182 posp->col = 0; \
1183 else \
1184 posp->col += col_amount; \
1189 * Adjust marks in line "lnum" at column "mincol" and further: add
1190 * "lnum_amount" to the line number and add "col_amount" to the column
1191 * position.
1193 void
1194 mark_col_adjust(lnum, mincol, lnum_amount, col_amount)
1195 linenr_T lnum;
1196 colnr_T mincol;
1197 long lnum_amount;
1198 long col_amount;
1200 int i;
1201 int fnum = curbuf->b_fnum;
1202 win_T *win;
1203 pos_T *posp;
1205 if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks)
1206 return; /* nothing to do */
1208 /* named marks, lower case and upper case */
1209 for (i = 0; i < NMARKS; i++)
1211 col_adjust(&(curbuf->b_namedm[i]));
1212 if (namedfm[i].fmark.fnum == fnum)
1213 col_adjust(&(namedfm[i].fmark.mark));
1215 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1217 if (namedfm[i].fmark.fnum == fnum)
1218 col_adjust(&(namedfm[i].fmark.mark));
1221 /* last Insert position */
1222 col_adjust(&(curbuf->b_last_insert));
1224 /* last change position */
1225 col_adjust(&(curbuf->b_last_change));
1227 #ifdef FEAT_JUMPLIST
1228 /* list of change positions */
1229 for (i = 0; i < curbuf->b_changelistlen; ++i)
1230 col_adjust(&(curbuf->b_changelist[i]));
1231 #endif
1233 #ifdef FEAT_VISUAL
1234 /* Visual area */
1235 col_adjust(&(curbuf->b_visual.vi_start));
1236 col_adjust(&(curbuf->b_visual.vi_end));
1237 #endif
1239 /* previous context mark */
1240 col_adjust(&(curwin->w_pcmark));
1242 /* previous pcmark */
1243 col_adjust(&(curwin->w_prev_pcmark));
1245 /* saved cursor for formatting */
1246 col_adjust(&saved_cursor);
1249 * Adjust items in all windows related to the current buffer.
1251 FOR_ALL_WINDOWS(win)
1253 #ifdef FEAT_JUMPLIST
1254 /* marks in the jumplist */
1255 for (i = 0; i < win->w_jumplistlen; ++i)
1256 if (win->w_jumplist[i].fmark.fnum == fnum)
1257 col_adjust(&(win->w_jumplist[i].fmark.mark));
1258 #endif
1260 if (win->w_buffer == curbuf)
1262 /* marks in the tag stack */
1263 for (i = 0; i < win->w_tagstacklen; i++)
1264 if (win->w_tagstack[i].fmark.fnum == fnum)
1265 col_adjust(&(win->w_tagstack[i].fmark.mark));
1267 /* cursor position for other windows with the same buffer */
1268 if (win != curwin)
1269 col_adjust(&win->w_cursor);
1274 #ifdef FEAT_JUMPLIST
1276 * When deleting lines, this may create duplicate marks in the
1277 * jumplist. They will be removed here for the current window.
1279 static void
1280 cleanup_jumplist()
1282 int i;
1283 int from, to;
1285 to = 0;
1286 for (from = 0; from < curwin->w_jumplistlen; ++from)
1288 if (curwin->w_jumplistidx == from)
1289 curwin->w_jumplistidx = to;
1290 for (i = from + 1; i < curwin->w_jumplistlen; ++i)
1291 if (curwin->w_jumplist[i].fmark.fnum
1292 == curwin->w_jumplist[from].fmark.fnum
1293 && curwin->w_jumplist[from].fmark.fnum != 0
1294 && curwin->w_jumplist[i].fmark.mark.lnum
1295 == curwin->w_jumplist[from].fmark.mark.lnum)
1296 break;
1297 if (i >= curwin->w_jumplistlen) /* no duplicate */
1298 curwin->w_jumplist[to++] = curwin->w_jumplist[from];
1299 else
1300 vim_free(curwin->w_jumplist[from].fname);
1302 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
1303 curwin->w_jumplistidx = to;
1304 curwin->w_jumplistlen = to;
1307 # if defined(FEAT_WINDOWS) || defined(PROTO)
1309 * Copy the jumplist from window "from" to window "to".
1311 void
1312 copy_jumplist(from, to)
1313 win_T *from;
1314 win_T *to;
1316 int i;
1318 for (i = 0; i < from->w_jumplistlen; ++i)
1320 to->w_jumplist[i] = from->w_jumplist[i];
1321 if (from->w_jumplist[i].fname != NULL)
1322 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1324 to->w_jumplistlen = from->w_jumplistlen;
1325 to->w_jumplistidx = from->w_jumplistidx;
1329 * Free items in the jumplist of window "wp".
1331 void
1332 free_jumplist(wp)
1333 win_T *wp;
1335 int i;
1337 for (i = 0; i < wp->w_jumplistlen; ++i)
1338 vim_free(wp->w_jumplist[i].fname);
1340 # endif
1341 #endif /* FEAT_JUMPLIST */
1343 void
1344 set_last_cursor(win)
1345 win_T *win;
1347 win->w_buffer->b_last_cursor = win->w_cursor;
1350 #if defined(EXITFREE) || defined(PROTO)
1351 void
1352 free_all_marks()
1354 int i;
1356 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1357 if (namedfm[i].fmark.mark.lnum != 0)
1358 vim_free(namedfm[i].fname);
1360 #endif
1362 #if defined(FEAT_VIMINFO) || defined(PROTO)
1364 read_viminfo_filemark(virp, force)
1365 vir_T *virp;
1366 int force;
1368 char_u *str;
1369 xfmark_T *fm;
1370 int i;
1372 /* We only get here if line[0] == '\'' or '-'.
1373 * Illegal mark names are ignored (for future expansion). */
1374 str = virp->vir_line + 1;
1375 if (
1376 #ifndef EBCDIC
1377 *str <= 127 &&
1378 #endif
1379 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1380 || (*virp->vir_line == '-' && *str == '\'')))
1382 if (*str == '\'')
1384 #ifdef FEAT_JUMPLIST
1385 /* If the jumplist isn't full insert fmark as oldest entry */
1386 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1387 fm = NULL;
1388 else
1390 for (i = curwin->w_jumplistlen; i > 0; --i)
1391 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1392 ++curwin->w_jumplistidx;
1393 ++curwin->w_jumplistlen;
1394 fm = &curwin->w_jumplist[0];
1395 fm->fmark.mark.lnum = 0;
1396 fm->fname = NULL;
1398 #else
1399 fm = NULL;
1400 #endif
1402 else if (VIM_ISDIGIT(*str))
1403 fm = &namedfm[*str - '0' + NMARKS];
1404 else
1405 fm = &namedfm[*str - 'A'];
1406 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1408 str = skipwhite(str + 1);
1409 fm->fmark.mark.lnum = getdigits(&str);
1410 str = skipwhite(str);
1411 fm->fmark.mark.col = getdigits(&str);
1412 #ifdef FEAT_VIRTUALEDIT
1413 fm->fmark.mark.coladd = 0;
1414 #endif
1415 fm->fmark.fnum = 0;
1416 str = skipwhite(str);
1417 vim_free(fm->fname);
1418 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1419 FALSE);
1422 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1425 void
1426 write_viminfo_filemarks(fp)
1427 FILE *fp;
1429 int i;
1430 char_u *name;
1431 buf_T *buf;
1432 xfmark_T *fm;
1434 if (get_viminfo_parameter('f') == 0)
1435 return;
1437 fprintf(fp, _("\n# File marks:\n"));
1440 * Find a mark that is the same file and position as the cursor.
1441 * That one, or else the last one is deleted.
1442 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1443 * Set '0 mark to current cursor position.
1445 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1447 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1448 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1449 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1450 && (namedfm[i].fname == NULL
1451 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1452 : (name != NULL
1453 && STRCMP(name, namedfm[i].fname) == 0)))
1454 break;
1455 vim_free(name);
1457 vim_free(namedfm[i].fname);
1458 for ( ; i > NMARKS; --i)
1459 namedfm[i] = namedfm[i - 1];
1460 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1461 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1462 namedfm[NMARKS].fname = NULL;
1465 /* Write the filemarks '0 - '9 and 'A - 'Z */
1466 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1467 write_one_filemark(fp, &namedfm[i], '\'',
1468 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1470 #ifdef FEAT_JUMPLIST
1471 /* Write the jumplist with -' */
1472 fprintf(fp, _("\n# Jumplist (newest first):\n"));
1473 setpcmark(); /* add current cursor position */
1474 cleanup_jumplist();
1475 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1476 fm >= &curwin->w_jumplist[0]; --fm)
1478 if (fm->fmark.fnum == 0
1479 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1480 && !removable(buf->b_ffname)))
1481 write_one_filemark(fp, fm, '-', '\'');
1483 #endif
1486 static void
1487 write_one_filemark(fp, fm, c1, c2)
1488 FILE *fp;
1489 xfmark_T *fm;
1490 int c1;
1491 int c2;
1493 char_u *name;
1495 if (fm->fmark.mark.lnum == 0) /* not set */
1496 return;
1498 if (fm->fmark.fnum != 0) /* there is a buffer */
1499 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1500 else
1501 name = fm->fname; /* use name from .viminfo */
1502 if (name != NULL && *name != NUL)
1504 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1505 (long)fm->fmark.mark.col);
1506 viminfo_writestring(fp, name);
1509 if (fm->fmark.fnum != 0)
1510 vim_free(name);
1514 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1517 removable(name)
1518 char_u *name;
1520 char_u *p;
1521 char_u part[51];
1522 int retval = FALSE;
1523 size_t n;
1525 name = home_replace_save(NULL, name);
1526 if (name != NULL)
1528 for (p = p_viminfo; *p; )
1530 copy_option_part(&p, part, 51, ", ");
1531 if (part[0] == 'r')
1533 n = STRLEN(part + 1);
1534 if (MB_STRNICMP(part + 1, name, n) == 0)
1536 retval = TRUE;
1537 break;
1541 vim_free(name);
1543 return retval;
1546 static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1549 * Write all the named marks for all buffers.
1550 * Return the number of buffers for which marks have been written.
1553 write_viminfo_marks(fp_out)
1554 FILE *fp_out;
1556 int count;
1557 buf_T *buf;
1558 int is_mark_set;
1559 int i;
1560 #ifdef FEAT_WINDOWS
1561 win_T *win;
1562 tabpage_T *tp;
1565 * Set b_last_cursor for the all buffers that have a window.
1567 FOR_ALL_TAB_WINDOWS(tp, win)
1568 set_last_cursor(win);
1569 #else
1570 set_last_cursor(curwin);
1571 #endif
1573 fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
1574 count = 0;
1575 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1578 * Only write something if buffer has been loaded and at least one
1579 * mark is set.
1581 if (buf->b_marks_read)
1583 if (buf->b_last_cursor.lnum != 0)
1584 is_mark_set = TRUE;
1585 else
1587 is_mark_set = FALSE;
1588 for (i = 0; i < NMARKS; i++)
1589 if (buf->b_namedm[i].lnum != 0)
1591 is_mark_set = TRUE;
1592 break;
1595 if (is_mark_set && buf->b_ffname != NULL
1596 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1598 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1599 fprintf(fp_out, "\n> ");
1600 viminfo_writestring(fp_out, IObuff);
1601 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1602 write_one_mark(fp_out, '^', &buf->b_last_insert);
1603 write_one_mark(fp_out, '.', &buf->b_last_change);
1604 #ifdef FEAT_JUMPLIST
1605 /* changelist positions are stored oldest first */
1606 for (i = 0; i < buf->b_changelistlen; ++i)
1607 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1608 #endif
1609 for (i = 0; i < NMARKS; i++)
1610 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1611 count++;
1616 return count;
1619 static void
1620 write_one_mark(fp_out, c, pos)
1621 FILE *fp_out;
1622 int c;
1623 pos_T *pos;
1625 if (pos->lnum != 0)
1626 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1630 * Handle marks in the viminfo file:
1631 * fp_out != NULL: copy marks for buffers not in buffer list
1632 * fp_out == NULL && (flags & VIF_WANT_MARKS): read marks for curbuf only
1633 * fp_out == NULL && (flags & VIF_GET_OLDFILES | VIF_FORCEIT): fill v:oldfiles
1635 void
1636 copy_viminfo_marks(virp, fp_out, count, eof, flags)
1637 vir_T *virp;
1638 FILE *fp_out;
1639 int count;
1640 int eof;
1641 int flags;
1643 char_u *line = virp->vir_line;
1644 buf_T *buf;
1645 int num_marked_files;
1646 int load_marks;
1647 int copy_marks_out;
1648 char_u *str;
1649 int i;
1650 char_u *p;
1651 char_u *name_buf;
1652 pos_T pos;
1653 #ifdef FEAT_EVAL
1654 list_T *list = NULL;
1655 #endif
1657 if ((name_buf = alloc(LSIZE)) == NULL)
1658 return;
1659 *name_buf = NUL;
1661 #ifdef FEAT_EVAL
1662 if (fp_out == NULL && (flags & (VIF_GET_OLDFILES | VIF_FORCEIT)))
1664 list = list_alloc();
1665 if (list != NULL)
1666 set_vim_var_list(VV_OLDFILES, list);
1668 #endif
1670 num_marked_files = get_viminfo_parameter('\'');
1671 while (!eof && (count < num_marked_files || fp_out == NULL))
1673 if (line[0] != '>')
1675 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1677 if (viminfo_error("E576: ", _("Missing '>'"), line))
1678 break; /* too many errors, return now */
1680 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1681 continue; /* Skip this dud line */
1685 * Handle long line and translate escaped characters.
1686 * Find file name, set str to start.
1687 * Ignore leading and trailing white space.
1689 str = skipwhite(line + 1);
1690 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1691 if (str == NULL)
1692 continue;
1693 p = str + STRLEN(str);
1694 while (p != str && (*p == NUL || vim_isspace(*p)))
1695 p--;
1696 if (*p)
1697 p++;
1698 *p = NUL;
1700 #ifdef FEAT_EVAL
1701 if (list != NULL)
1702 list_append_string(list, str, -1);
1703 #endif
1706 * If fp_out == NULL, load marks for current buffer.
1707 * If fp_out != NULL, copy marks for buffers not in buflist.
1709 load_marks = copy_marks_out = FALSE;
1710 if (fp_out == NULL)
1712 if ((flags & VIF_WANT_MARKS) && curbuf->b_ffname != NULL)
1714 if (*name_buf == NUL) /* only need to do this once */
1715 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1716 if (fnamecmp(str, name_buf) == 0)
1717 load_marks = TRUE;
1720 else /* fp_out != NULL */
1722 /* This is slow if there are many buffers!! */
1723 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1724 if (buf->b_ffname != NULL)
1726 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1727 if (fnamecmp(str, name_buf) == 0)
1728 break;
1732 * copy marks if the buffer has not been loaded
1734 if (buf == NULL || !buf->b_marks_read)
1736 copy_marks_out = TRUE;
1737 fputs("\n> ", fp_out);
1738 viminfo_writestring(fp_out, str);
1739 count++;
1742 vim_free(str);
1744 #ifdef FEAT_VIRTUALEDIT
1745 pos.coladd = 0;
1746 #endif
1747 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1749 if (load_marks)
1751 if (line[1] != NUL)
1753 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &pos.col);
1754 switch (line[1])
1756 case '"': curbuf->b_last_cursor = pos; break;
1757 case '^': curbuf->b_last_insert = pos; break;
1758 case '.': curbuf->b_last_change = pos; break;
1759 case '+':
1760 #ifdef FEAT_JUMPLIST
1761 /* changelist positions are stored oldest
1762 * first */
1763 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1764 /* list is full, remove oldest entry */
1765 mch_memmove(curbuf->b_changelist,
1766 curbuf->b_changelist + 1,
1767 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1768 else
1769 ++curbuf->b_changelistlen;
1770 curbuf->b_changelist[
1771 curbuf->b_changelistlen - 1] = pos;
1772 #endif
1773 break;
1774 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1775 curbuf->b_namedm[i] = pos;
1779 else if (copy_marks_out)
1780 fputs((char *)line, fp_out);
1782 if (load_marks)
1784 #ifdef FEAT_JUMPLIST
1785 win_T *wp;
1787 FOR_ALL_WINDOWS(wp)
1789 if (wp->w_buffer == curbuf)
1790 wp->w_changelistidx = curbuf->b_changelistlen;
1792 #endif
1793 break;
1796 vim_free(name_buf);
1798 #endif /* FEAT_VIMINFO */