blob: e1b800eb18324dc1a12564502c6330a745328fa6 [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/*
42 * Set named mark 'c' at current cursor position.
43 * Returns OK on success, FAIL if bad name given.
44 */
45 int
46setmark(c)
47 int c;
48{
49 int i;
50
51 /* Check for a special key (may cause islower() to crash). */
52 if (c < 0)
53 return FAIL;
54
55 if (c == '\'' || c == '`')
56 {
57 setpcmark();
58 /* keep it even when the cursor doesn't move */
59 curwin->w_prev_pcmark = curwin->w_pcmark;
60 return OK;
61 }
62
63 /* Allow setting '[ and '] for an autocommand that simulates reading a
64 * file. */
65 if (c == '[')
66 {
67 curbuf->b_op_start = curwin->w_cursor;
68 return OK;
69 }
70 if (c == ']')
71 {
72 curbuf->b_op_end = curwin->w_cursor;
73 return OK;
74 }
75
76#ifndef EBCDIC
77 if (c > 'z') /* some islower() and isupper() cannot handle
78 characters above 127 */
79 return FAIL;
80#endif
81 if (islower(c))
82 {
83 i = c - 'a';
84 curbuf->b_namedm[i] = curwin->w_cursor;
85 return OK;
86 }
87 if (isupper(c))
88 {
89 i = c - 'A';
90 namedfm[i].fmark.mark = curwin->w_cursor;
91 namedfm[i].fmark.fnum = curbuf->b_fnum;
92 vim_free(namedfm[i].fname);
93 namedfm[i].fname = NULL;
94 return OK;
95 }
96 return FAIL;
97}
98
99/*
100 * Set the previous context mark to the current position and add it to the
101 * jump list.
102 */
103 void
104setpcmark()
105{
106#ifdef FEAT_JUMPLIST
107 int i;
108 xfmark_T *fm;
109#endif
110#ifdef JUMPLIST_ROTATE
111 xfmark_T tempmark;
112#endif
113
114 /* for :global the mark is set only once */
115 if (global_busy || listcmd_busy || cmdmod.keepjumps)
116 return;
117
118 curwin->w_prev_pcmark = curwin->w_pcmark;
119 curwin->w_pcmark = curwin->w_cursor;
120
121#ifdef FEAT_JUMPLIST
122# ifdef JUMPLIST_ROTATE
123 /*
124 * If last used entry is not at the top, put it at the top by rotating
125 * the stack until it is (the newer entries will be at the bottom).
126 * Keep one entry (the last used one) at the top.
127 */
128 if (curwin->w_jumplistidx < curwin->w_jumplistlen)
129 ++curwin->w_jumplistidx;
130 while (curwin->w_jumplistidx < curwin->w_jumplistlen)
131 {
132 tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
133 for (i = curwin->w_jumplistlen - 1; i > 0; --i)
134 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
135 curwin->w_jumplist[0] = tempmark;
136 ++curwin->w_jumplistidx;
137 }
138# endif
139
140 /* If jumplist is full: remove oldest entry */
141 if (++curwin->w_jumplistlen > JUMPLISTSIZE)
142 {
143 curwin->w_jumplistlen = JUMPLISTSIZE;
144 vim_free(curwin->w_jumplist[0].fname);
145 for (i = 1; i < JUMPLISTSIZE; ++i)
146 curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
147 }
148 curwin->w_jumplistidx = curwin->w_jumplistlen;
149 fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
150
151 fm->fmark.mark = curwin->w_pcmark;
152 fm->fmark.fnum = curbuf->b_fnum;
153 fm->fname = NULL;
154#endif
155}
156
157/*
158 * To change context, call setpcmark(), then move the current position to
159 * where ever, then call checkpcmark(). This ensures that the previous
160 * context will only be changed if the cursor moved to a different line.
161 * If pcmark was deleted (with "dG") the previous mark is restored.
162 */
163 void
164checkpcmark()
165{
166 if (curwin->w_prev_pcmark.lnum != 0
167 && (equalpos(curwin->w_pcmark, curwin->w_cursor)
168 || curwin->w_pcmark.lnum == 0))
169 {
170 curwin->w_pcmark = curwin->w_prev_pcmark;
171 curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */
172 }
173}
174
175#if defined(FEAT_JUMPLIST) || defined(PROTO)
176/*
177 * move "count" positions in the jump list (count may be negative)
178 */
179 pos_T *
180movemark(count)
181 int count;
182{
183 pos_T *pos;
184 xfmark_T *jmp;
185
186 cleanup_jumplist();
187
188 if (curwin->w_jumplistlen == 0) /* nothing to jump to */
189 return (pos_T *)NULL;
190
191 for (;;)
192 {
193 if (curwin->w_jumplistidx + count < 0
194 || curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
195 return (pos_T *)NULL;
196
197 /*
198 * if first CTRL-O or CTRL-I command after a jump, add cursor position
199 * to list. Careful: If there are duplicates (CTRL-O immidiately after
200 * starting Vim on a file), another entry may have been removed.
201 */
202 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
203 {
204 setpcmark();
205 --curwin->w_jumplistidx; /* skip the new entry */
206 if (curwin->w_jumplistidx + count < 0)
207 return (pos_T *)NULL;
208 }
209
210 curwin->w_jumplistidx += count;
211
212 jmp = curwin->w_jumplist + curwin->w_jumplistidx;
213 if (jmp->fmark.fnum == 0)
214 fname2fnum(jmp);
215 if (jmp->fmark.fnum != curbuf->b_fnum)
216 {
217 /* jump to other file */
218 if (buflist_findnr(jmp->fmark.fnum) == NULL)
219 { /* Skip this one .. */
220 count += count < 0 ? -1 : 1;
221 continue;
222 }
223 if (buflist_getfile(jmp->fmark.fnum, jmp->fmark.mark.lnum,
224 0, FALSE) == FAIL)
225 return (pos_T *)NULL;
226 /* Set lnum again, autocommands my have changed it */
227 curwin->w_cursor = jmp->fmark.mark;
228 pos = (pos_T *)-1;
229 }
230 else
231 pos = &(jmp->fmark.mark);
232 return pos;
233 }
234}
235
236/*
237 * Move "count" positions in the changelist (count may be negative).
238 */
239 pos_T *
240movechangelist(count)
241 int count;
242{
243 int n;
244
245 if (curbuf->b_changelistlen == 0) /* nothing to jump to */
246 return (pos_T *)NULL;
247
248 n = curwin->w_changelistidx;
249 if (n + count < 0)
250 {
251 if (n == 0)
252 return (pos_T *)NULL;
253 n = 0;
254 }
255 else if (n + count >= curbuf->b_changelistlen)
256 {
257 if (n == curbuf->b_changelistlen - 1)
258 return (pos_T *)NULL;
259 n = curbuf->b_changelistlen - 1;
260 }
261 else
262 n += count;
263 curwin->w_changelistidx = n;
264 return curbuf->b_changelist + n;
265}
266#endif
267
268/*
269 * Find mark "c".
270 * Returns:
271 * - pointer to pos_T if found. lnum is 0 when mark not set, -1 when mark is
272 * in another file which can't be gotten. (caller needs to check lnum!)
273 * - NULL if there is no mark called 'c'.
274 * - -1 if mark is in other file and jumped there (only if changefile is TRUE)
275 */
276 pos_T *
277getmark(c, changefile)
278 int c;
279 int changefile; /* allowed to edit another file */
280{
281 pos_T *posp;
282#ifdef FEAT_VISUAL
283 pos_T *startp, *endp;
284#endif
285 static pos_T pos_copy;
286
287 posp = NULL;
288
289 /* Check for special key, can't be a mark name and might cause islower()
290 * to crash. */
291 if (c < 0)
292 return posp;
293#ifndef EBCDIC
294 if (c > '~') /* check for islower()/isupper() */
295 ;
296 else
297#endif
298 if (c == '\'' || c == '`') /* previous context mark */
299 {
300 pos_copy = curwin->w_pcmark; /* need to make a copy because */
301 posp = &pos_copy; /* w_pcmark may be changed soon */
302 }
303 else if (c == '"') /* to pos when leaving buffer */
304 posp = &(curbuf->b_last_cursor);
305 else if (c == '^') /* to where Insert mode stopped */
306 posp = &(curbuf->b_last_insert);
307 else if (c == '.') /* to where last change was made */
308 posp = &(curbuf->b_last_change);
309 else if (c == '[') /* to start of previous operator */
310 posp = &(curbuf->b_op_start);
311 else if (c == ']') /* to end of previous operator */
312 posp = &(curbuf->b_op_end);
313 else if (c == '{' || c == '}') /* to previous/next paragraph */
314 {
315 pos_T pos;
316 oparg_T oa;
317 int slcb = listcmd_busy;
318
319 pos = curwin->w_cursor;
320 listcmd_busy = TRUE; /* avoid that '' is changed */
321 if (findpar(&oa, c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
322 {
323 pos_copy = curwin->w_cursor;
324 posp = &pos_copy;
325 }
326 curwin->w_cursor = pos;
327 listcmd_busy = slcb;
328 }
329 else if (c == '(' || c == ')') /* to previous/next sentence */
330 {
331 pos_T pos;
332 int slcb = listcmd_busy;
333
334 pos = curwin->w_cursor;
335 listcmd_busy = TRUE; /* avoid that '' is changed */
336 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
337 {
338 pos_copy = curwin->w_cursor;
339 posp = &pos_copy;
340 }
341 curwin->w_cursor = pos;
342 listcmd_busy = slcb;
343 }
344#ifdef FEAT_VISUAL
345 else if (c == '<' || c == '>') /* start/end of visual area */
346 {
347 startp = &curbuf->b_visual_start;
348 endp = &curbuf->b_visual_end;
349 if ((c == '<') == lt(*startp, *endp))
350 posp = startp;
351 else
352 posp = endp;
353 /*
354 * For Visual line mode, set mark at begin or end of line
355 */
356 if (curbuf->b_visual_mode == 'V')
357 {
358 pos_copy = *posp;
359 posp = &pos_copy;
360 if (c == '<')
361 pos_copy.col = 0;
362 else
363 pos_copy.col = MAXCOL;
364#ifdef FEAT_VIRTUALEDIT
365 pos_copy.coladd = 0;
366#endif
367 }
368 }
369#endif
370 else if (ASCII_ISLOWER(c)) /* normal named mark */
371 {
372 posp = &(curbuf->b_namedm[c - 'a']);
373 }
374 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */
375 {
376 if (VIM_ISDIGIT(c))
377 c = c - '0' + NMARKS;
378 else
379 c -= 'A';
380 posp = &(namedfm[c].fmark.mark);
381
382 if (namedfm[c].fmark.fnum == 0)
383 fname2fnum(&namedfm[c]);
384 if (namedfm[c].fmark.fnum != curbuf->b_fnum)
385 {
386 posp = &pos_copy;
387
388 /* mark is in another file */
389 if (namedfm[c].fmark.mark.lnum != 0
390 && changefile && namedfm[c].fmark.fnum)
391 {
392 if (buflist_getfile(namedfm[c].fmark.fnum,
393 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
394 {
395 /* Set the lnum now, autocommands could have changed it */
396 curwin->w_cursor = namedfm[c].fmark.mark;
397 return (pos_T *)-1;
398 }
399 pos_copy.lnum = -1; /* can't get file */
400 }
401 else
402 pos_copy.lnum = 0; /* mark exists, but is not valid in
403 current buffer */
404 }
405 }
406
407 return posp;
408}
409
410/*
411 * Search for the next named mark in the current file.
412 *
413 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
414 */
415 pos_T *
416getnextmark(startpos, dir, begin_line)
417 pos_T *startpos; /* where to start */
418 int dir; /* direction for search */
419 int begin_line;
420{
421 int i;
422 pos_T *result = NULL;
423 pos_T pos;
424
425 pos = *startpos;
426
427 /* When searching backward and leaving the cursor on the first non-blank,
428 * position must be in a previous line.
429 * When searching forward and leaving the cursor on the first non-blank,
430 * position must be in a next line. */
431 if (dir == BACKWARD && begin_line)
432 pos.col = 0;
433 else if (dir == FORWARD && begin_line)
434 pos.col = MAXCOL;
435
436 for (i = 0; i < NMARKS; i++)
437 {
438 if (curbuf->b_namedm[i].lnum > 0)
439 {
440 if (dir == FORWARD)
441 {
442 if ((result == NULL || lt(curbuf->b_namedm[i], *result))
443 && lt(pos, curbuf->b_namedm[i]))
444 result = &curbuf->b_namedm[i];
445 }
446 else
447 {
448 if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
449 && lt(curbuf->b_namedm[i], pos))
450 result = &curbuf->b_namedm[i];
451 }
452 }
453 }
454
455 return result;
456}
457
458/*
459 * For an xtended filemark: set the fnum from the fname.
460 * This is used for marks obtained from the .viminfo file. It's postponed
461 * until the mark is used to avoid a long startup delay.
462 */
463 static void
464fname2fnum(fm)
465 xfmark_T *fm;
466{
467 char_u *p;
468
469 if (fm->fname != NULL)
470 {
471 /*
472 * First expand "~/" in the file name to the home directory.
473 * Try to shorten the file name.
474 */
475 expand_env(fm->fname, NameBuff, MAXPATHL);
476 mch_dirname(IObuff, IOSIZE);
477 p = shorten_fname(NameBuff, IObuff);
478
479 /* buflist_new() will call fmarks_check_names() */
480 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
481 }
482}
483
484/*
485 * Check all file marks for a name that matches the file name in buf.
486 * May replace the name with an fnum.
487 * Used for marks that come from the .viminfo file.
488 */
489 void
490fmarks_check_names(buf)
491 buf_T *buf;
492{
493 char_u *name;
494 int i;
495#ifdef FEAT_JUMPLIST
496 win_T *wp;
497#endif
498
499 if (buf->b_ffname == NULL)
500 return;
501
502 name = home_replace_save(buf, buf->b_ffname);
503 if (name == NULL)
504 return;
505
506 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
507 fmarks_check_one(&namedfm[i], name, buf);
508
509#ifdef FEAT_JUMPLIST
510 FOR_ALL_WINDOWS(wp)
511 {
512 for (i = 0; i < wp->w_jumplistlen; ++i)
513 fmarks_check_one(&wp->w_jumplist[i], name, buf);
514 }
515#endif
516
517 vim_free(name);
518}
519
520 static void
521fmarks_check_one(fm, name, buf)
522 xfmark_T *fm;
523 char_u *name;
524 buf_T *buf;
525{
526 if (fm->fmark.fnum == 0
527 && fm->fname != NULL
528 && fnamecmp(name, fm->fname) == 0)
529 {
530 fm->fmark.fnum = buf->b_fnum;
531 vim_free(fm->fname);
532 fm->fname = NULL;
533 }
534}
535
536/*
537 * Check a if a position from a mark is valid.
538 * Give and error message and return FAIL if not.
539 */
540 int
541check_mark(pos)
542 pos_T *pos;
543{
544 if (pos == NULL)
545 {
546 EMSG(_(e_umark));
547 return FAIL;
548 }
549 if (pos->lnum <= 0)
550 {
551 /* lnum is negative if mark is in another file can can't get that
552 * file, error message already give then. */
553 if (pos->lnum == 0)
554 EMSG(_(e_marknotset));
555 return FAIL;
556 }
557 if (pos->lnum > curbuf->b_ml.ml_line_count)
558 {
559 EMSG(_(e_markinval));
560 return FAIL;
561 }
562 return OK;
563}
564
565/*
566 * clrallmarks() - clear all marks in the buffer 'buf'
567 *
568 * Used mainly when trashing the entire buffer during ":e" type commands
569 */
570 void
571clrallmarks(buf)
572 buf_T *buf;
573{
574 static int i = -1;
575
576 if (i == -1) /* first call ever: initialize */
577 for (i = 0; i < NMARKS + 1; i++)
578 {
579 namedfm[i].fmark.mark.lnum = 0;
580 namedfm[i].fname = NULL;
581 }
582
583 for (i = 0; i < NMARKS; i++)
584 buf->b_namedm[i].lnum = 0;
585 buf->b_op_start.lnum = 0; /* start/end op mark cleared */
586 buf->b_op_end.lnum = 0;
587 buf->b_last_cursor.lnum = 1; /* '" mark cleared */
588 buf->b_last_cursor.col = 0;
589#ifdef FEAT_VIRTUALEDIT
590 buf->b_last_cursor.coladd = 0;
591#endif
592 buf->b_last_insert.lnum = 0; /* '^ mark cleared */
593 buf->b_last_change.lnum = 0; /* '. mark cleared */
594#ifdef FEAT_JUMPLIST
595 buf->b_changelistlen = 0;
596#endif
597}
598
599/*
600 * Get name of file from a filemark.
601 * When it's in the current buffer, return the text at the mark.
602 * Returns an allocated string.
603 */
604 char_u *
605fm_getname(fmark, lead_len)
606 fmark_T *fmark;
607 int lead_len;
608{
609 if (fmark->fnum == curbuf->b_fnum) /* current buffer */
610 return mark_line(&(fmark->mark), lead_len);
611 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
612}
613
614/*
615 * Return the line at mark "mp". Truncate to fit in window.
616 * The returned string has been allocated.
617 */
618 static char_u *
619mark_line(mp, lead_len)
620 pos_T *mp;
621 int lead_len;
622{
623 char_u *s, *p;
624 int len;
625
626 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
627 return vim_strsave((char_u *)"-invalid-");
628 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
629 if (s == NULL)
630 return NULL;
631 /* Truncate the line to fit it in the window */
632 len = 0;
633 for (p = s; *p != NUL; ++p)
634 {
635 len += ptr2cells(p);
636 if (len >= Columns - lead_len)
637 break;
638#ifdef FEAT_MBYTE
639 if (has_mbyte)
640 p += (*mb_ptr2len_check)(p) - 1;
641#endif
642 }
643 *p = NUL;
644 return s;
645}
646
647/*
648 * print the marks
649 */
650 void
651do_marks(eap)
652 exarg_T *eap;
653{
654 char_u *arg = eap->arg;
655 int i;
656 char_u *name;
657
658 if (arg != NULL && *arg == NUL)
659 arg = NULL;
660
661 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
662 for (i = 0; i < NMARKS; ++i)
663 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
664 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
665 {
666 if (namedfm[i].fmark.fnum != 0)
667 name = fm_getname(&namedfm[i].fmark, 15);
668 else
669 name = namedfm[i].fname;
670 if (name != NULL)
671 {
672 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
673 arg, &namedfm[i].fmark.mark, name,
674 namedfm[i].fmark.fnum == curbuf->b_fnum);
675 if (namedfm[i].fmark.fnum != 0)
676 vim_free(name);
677 }
678 }
679 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
680 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
681 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
682 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
683 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
684#ifdef FEAT_VISUAL
685 show_one_mark('<', arg, &curbuf->b_visual_start, NULL, TRUE);
686 show_one_mark('>', arg, &curbuf->b_visual_end, NULL, TRUE);
687#endif
688 show_one_mark(-1, arg, NULL, NULL, FALSE);
689}
690
691 static void
692show_one_mark(c, arg, p, name, current)
693 int c;
694 char_u *arg;
695 pos_T *p;
696 char_u *name;
697 int current; /* in current file */
698{
699 static int did_title = FALSE;
700 int mustfree = FALSE;
701
702 if (c == -1) /* finish up */
703 {
704 if (did_title)
705 did_title = FALSE;
706 else
707 {
708 if (arg == NULL)
709 MSG(_("No marks set"));
710 else
711 EMSG2(_("E283: No marks matching \"%s\""), arg);
712 }
713 }
714 /* don't output anything if 'q' typed at --more-- prompt */
715 else if (!got_int
716 && (arg == NULL || vim_strchr(arg, c) != NULL)
717 && p->lnum != 0)
718 {
719 if (!did_title)
720 {
721 /* Highlight title */
722 MSG_PUTS_TITLE(_("\nmark line col file/text"));
723 did_title = TRUE;
724 }
725 msg_putchar('\n');
726 if (!got_int)
727 {
728 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
729 msg_outtrans(IObuff);
730 if (name == NULL && current)
731 {
732 name = mark_line(p, 15);
733 mustfree = TRUE;
734 }
735 if (name != NULL)
736 {
737 msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
738 if (mustfree)
739 vim_free(name);
740 }
741 }
742 out_flush(); /* show one line at a time */
743 }
744}
745
746#if defined(FEAT_JUMPLIST) || defined(PROTO)
747/*
748 * print the jumplist
749 */
750/*ARGSUSED*/
751 void
752ex_jumps(eap)
753 exarg_T *eap;
754{
755 int i;
756 char_u *name;
757
758 cleanup_jumplist();
759 /* Highlight title */
760 MSG_PUTS_TITLE(_("\n jump line col file/text"));
761 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
762 {
763 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
764 {
765 if (curwin->w_jumplist[i].fmark.fnum == 0)
766 fname2fnum(&curwin->w_jumplist[i]);
767 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
768 if (name == NULL) /* file name not available */
769 continue;
770
771 msg_putchar('\n');
772 if (got_int)
773 break;
774 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
775 i == curwin->w_jumplistidx ? '>' : ' ',
776 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
777 : curwin->w_jumplistidx - i,
778 curwin->w_jumplist[i].fmark.mark.lnum,
779 curwin->w_jumplist[i].fmark.mark.col);
780 msg_outtrans(IObuff);
781 msg_outtrans_attr(name,
782 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
783 ? hl_attr(HLF_D) : 0);
784 vim_free(name);
785 ui_breakcheck();
786 }
787 out_flush();
788 }
789 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
790 MSG_PUTS("\n>");
791}
792
793/*
794 * print the changelist
795 */
796/*ARGSUSED*/
797 void
798ex_changes(eap)
799 exarg_T *eap;
800{
801 int i;
802 char_u *name;
803
804 /* Highlight title */
805 MSG_PUTS_TITLE(_("\nchange line col text"));
806
807 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
808 {
809 if (curbuf->b_changelist[i].lnum != 0)
810 {
811 msg_putchar('\n');
812 if (got_int)
813 break;
814 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
815 i == curwin->w_changelistidx ? '>' : ' ',
816 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
817 : curwin->w_changelistidx - i,
818 (long)curbuf->b_changelist[i].lnum,
819 curbuf->b_changelist[i].col);
820 msg_outtrans(IObuff);
821 name = mark_line(&curbuf->b_changelist[i], 17);
822 if (name == NULL)
823 break;
824 msg_outtrans_attr(name, hl_attr(HLF_D));
825 vim_free(name);
826 ui_breakcheck();
827 }
828 out_flush();
829 }
830 if (curwin->w_changelistidx == curbuf->b_changelistlen)
831 MSG_PUTS("\n>");
832}
833#endif
834
835#define one_adjust(add) \
836 { \
837 lp = add; \
838 if (*lp >= line1 && *lp <= line2) \
839 { \
840 if (amount == MAXLNUM) \
841 *lp = 0; \
842 else \
843 *lp += amount; \
844 } \
845 else if (amount_after && *lp > line2) \
846 *lp += amount_after; \
847 }
848
849/* don't delete the line, just put at first deleted line */
850#define one_adjust_nodel(add) \
851 { \
852 lp = add; \
853 if (*lp >= line1 && *lp <= line2) \
854 { \
855 if (amount == MAXLNUM) \
856 *lp = line1; \
857 else \
858 *lp += amount; \
859 } \
860 else if (amount_after && *lp > line2) \
861 *lp += amount_after; \
862 }
863
864/*
865 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
866 * Must be called before changed_*(), appended_lines() or deleted_lines().
867 * May be called before or after changing the text.
868 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
869 * within this range are made invalid.
870 * If 'amount_after' is non-zero adjust marks after line2.
871 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
872 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
873 * or: mark_adjust(56, 55, MAXLNUM, 2);
874 */
875 void
876mark_adjust(line1, line2, amount, amount_after)
877 linenr_T line1;
878 linenr_T line2;
879 long amount;
880 long amount_after;
881{
882 int i;
883 int fnum = curbuf->b_fnum;
884 linenr_T *lp;
885 win_T *win;
886
887 if (line2 < line1 && amount_after == 0L) /* nothing to do */
888 return;
889
890 if (!cmdmod.lockmarks)
891 {
892 /* named marks, lower case and upper case */
893 for (i = 0; i < NMARKS; i++)
894 {
895 one_adjust(&(curbuf->b_namedm[i].lnum));
896 if (namedfm[i].fmark.fnum == fnum)
897 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
898 }
899 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
900 {
901 if (namedfm[i].fmark.fnum == fnum)
902 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
903 }
904
905 /* last Insert position */
906 one_adjust(&(curbuf->b_last_insert.lnum));
907
908 /* last change position */
909 one_adjust(&(curbuf->b_last_change.lnum));
910
911#ifdef FEAT_JUMPLIST
912 /* list of change positions */
913 for (i = 0; i < curbuf->b_changelistlen; ++i)
914 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
915#endif
916
917#ifdef FEAT_VISUAL
918 /* Visual area */
919 one_adjust_nodel(&(curbuf->b_visual_start.lnum));
920 one_adjust_nodel(&(curbuf->b_visual_end.lnum));
921#endif
922
923#ifdef FEAT_QUICKFIX
924 /* quickfix marks */
925 qf_mark_adjust(line1, line2, amount, amount_after);
926#endif
927
928#ifdef FEAT_SIGNS
929 sign_mark_adjust(line1, line2, amount, amount_after);
930#endif
931 }
932
933 /* previous context mark */
934 one_adjust(&(curwin->w_pcmark.lnum));
935
936 /* previous pcmark */
937 one_adjust(&(curwin->w_prev_pcmark.lnum));
938
939 /* saved cursor for formatting */
940 if (saved_cursor.lnum != 0)
941 one_adjust_nodel(&(saved_cursor.lnum));
942
943 /*
944 * Adjust items in all windows related to the current buffer.
945 */
946 FOR_ALL_WINDOWS(win)
947 {
948#ifdef FEAT_JUMPLIST
949 if (!cmdmod.lockmarks)
950 /* Marks in the jumplist. When deleting lines, this may create
951 * duplicate marks in the jumplist, they will be removed later. */
952 for (i = 0; i < win->w_jumplistlen; ++i)
953 if (win->w_jumplist[i].fmark.fnum == fnum)
954 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
955#endif
956
957 if (win->w_buffer == curbuf)
958 {
959 if (!cmdmod.lockmarks)
960 /* marks in the tag stack */
961 for (i = 0; i < win->w_tagstacklen; i++)
962 if (win->w_tagstack[i].fmark.fnum == fnum)
963 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
964
965#ifdef FEAT_VISUAL
966 /* the displayed Visual area */
967 if (win->w_old_cursor_lnum != 0)
968 {
969 one_adjust_nodel(&(win->w_old_cursor_lnum));
970 one_adjust_nodel(&(win->w_old_visual_lnum));
971 }
972#endif
973
974 /* topline and cursor position for windows with the same buffer
975 * other than the current window */
976 if (win != curwin)
977 {
978 if (win->w_topline >= line1 && win->w_topline <= line2)
979 {
980 if (amount == MAXLNUM) /* topline is deleted */
981 {
982 if (line1 <= 1)
983 win->w_topline = 1;
984 else
985 win->w_topline = line1 - 1;
986 }
987 else /* keep topline on the same line */
988 win->w_topline += amount;
989#ifdef FEAT_DIFF
990 win->w_topfill = 0;
991#endif
992 }
993 else if (amount_after && win->w_topline > line2)
994 {
995 win->w_topline += amount_after;
996#ifdef FEAT_DIFF
997 win->w_topfill = 0;
998#endif
999 }
1000 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1001 {
1002 if (amount == MAXLNUM) /* line with cursor is deleted */
1003 {
1004 if (line1 <= 1)
1005 win->w_cursor.lnum = 1;
1006 else
1007 win->w_cursor.lnum = line1 - 1;
1008 win->w_cursor.col = 0;
1009 }
1010 else /* keep cursor on the same line */
1011 win->w_cursor.lnum += amount;
1012 }
1013 else if (amount_after && win->w_cursor.lnum > line2)
1014 win->w_cursor.lnum += amount_after;
1015 }
1016
1017#ifdef FEAT_FOLDING
1018 /* adjust folds */
1019 foldMarkAdjust(win, line1, line2, amount, amount_after);
1020#endif
1021 }
1022 }
1023
1024#ifdef FEAT_DIFF
1025 /* adjust diffs */
1026 diff_mark_adjust(line1, line2, amount, amount_after);
1027#endif
1028}
1029
1030/* This code is used often, needs to be fast. */
1031#define col_adjust(pp) \
1032 { \
1033 posp = pp; \
1034 if (posp->lnum == lnum && posp->col >= mincol) \
1035 { \
1036 posp->lnum += lnum_amount; \
1037 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1038 posp->col = 0; \
1039 else \
1040 posp->col += col_amount; \
1041 } \
1042 }
1043
1044/*
1045 * Adjust marks in line "lnum" at column "mincol" and further: add
1046 * "lnum_amount" to the line number and add "col_amount" to the column
1047 * position.
1048 */
1049 void
1050mark_col_adjust(lnum, mincol, lnum_amount, col_amount)
1051 linenr_T lnum;
1052 colnr_T mincol;
1053 long lnum_amount;
1054 long col_amount;
1055{
1056 int i;
1057 int fnum = curbuf->b_fnum;
1058 win_T *win;
1059 pos_T *posp;
1060
1061 if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks)
1062 return; /* nothing to do */
1063
1064 /* named marks, lower case and upper case */
1065 for (i = 0; i < NMARKS; i++)
1066 {
1067 col_adjust(&(curbuf->b_namedm[i]));
1068 if (namedfm[i].fmark.fnum == fnum)
1069 col_adjust(&(namedfm[i].fmark.mark));
1070 }
1071 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1072 {
1073 if (namedfm[i].fmark.fnum == fnum)
1074 col_adjust(&(namedfm[i].fmark.mark));
1075 }
1076
1077 /* last Insert position */
1078 col_adjust(&(curbuf->b_last_insert));
1079
1080 /* last change position */
1081 col_adjust(&(curbuf->b_last_change));
1082
1083#ifdef FEAT_JUMPLIST
1084 /* list of change positions */
1085 for (i = 0; i < curbuf->b_changelistlen; ++i)
1086 col_adjust(&(curbuf->b_changelist[i]));
1087#endif
1088
1089#ifdef FEAT_VISUAL
1090 /* Visual area */
1091 col_adjust(&(curbuf->b_visual_start));
1092 col_adjust(&(curbuf->b_visual_end));
1093#endif
1094
1095 /* previous context mark */
1096 col_adjust(&(curwin->w_pcmark));
1097
1098 /* previous pcmark */
1099 col_adjust(&(curwin->w_prev_pcmark));
1100
1101 /* saved cursor for formatting */
1102 col_adjust(&saved_cursor);
1103
1104 /*
1105 * Adjust items in all windows related to the current buffer.
1106 */
1107 FOR_ALL_WINDOWS(win)
1108 {
1109#ifdef FEAT_JUMPLIST
1110 /* marks in the jumplist */
1111 for (i = 0; i < win->w_jumplistlen; ++i)
1112 if (win->w_jumplist[i].fmark.fnum == fnum)
1113 col_adjust(&(win->w_jumplist[i].fmark.mark));
1114#endif
1115
1116 if (win->w_buffer == curbuf)
1117 {
1118 /* marks in the tag stack */
1119 for (i = 0; i < win->w_tagstacklen; i++)
1120 if (win->w_tagstack[i].fmark.fnum == fnum)
1121 col_adjust(&(win->w_tagstack[i].fmark.mark));
1122
1123 /* cursor position for other windows with the same buffer */
1124 if (win != curwin)
1125 col_adjust(&win->w_cursor);
1126 }
1127 }
1128}
1129
1130#ifdef FEAT_JUMPLIST
1131/*
1132 * When deleting lines, this may create duplicate marks in the
1133 * jumplist. They will be removed here for the current window.
1134 */
1135 static void
1136cleanup_jumplist()
1137{
1138 int i;
1139 int from, to;
1140
1141 to = 0;
1142 for (from = 0; from < curwin->w_jumplistlen; ++from)
1143 {
1144 if (curwin->w_jumplistidx == from)
1145 curwin->w_jumplistidx = to;
1146 for (i = from + 1; i < curwin->w_jumplistlen; ++i)
1147 if (curwin->w_jumplist[i].fmark.fnum
1148 == curwin->w_jumplist[from].fmark.fnum
1149 && curwin->w_jumplist[from].fmark.fnum != 0
1150 && curwin->w_jumplist[i].fmark.mark.lnum
1151 == curwin->w_jumplist[from].fmark.mark.lnum)
1152 break;
1153 if (i >= curwin->w_jumplistlen) /* no duplicate */
1154 curwin->w_jumplist[to++] = curwin->w_jumplist[from];
1155 else
1156 vim_free(curwin->w_jumplist[from].fname);
1157 }
1158 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
1159 curwin->w_jumplistidx = to;
1160 curwin->w_jumplistlen = to;
1161}
1162
1163# if defined(FEAT_WINDOWS) || defined(PROTO)
1164/*
1165 * Copy the jumplist from window "from" to window "to".
1166 */
1167 void
1168copy_jumplist(from, to)
1169 win_T *from;
1170 win_T *to;
1171{
1172 int i;
1173
1174 for (i = 0; i < from->w_jumplistlen; ++i)
1175 {
1176 to->w_jumplist[i] = from->w_jumplist[i];
1177 if (from->w_jumplist[i].fname != NULL)
1178 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1179 }
1180 to->w_jumplistlen = from->w_jumplistlen;
1181 to->w_jumplistidx = from->w_jumplistidx;
1182}
1183
1184/*
1185 * Free items in the jumplist of window "wp".
1186 */
1187 void
1188free_jumplist(wp)
1189 win_T *wp;
1190{
1191 int i;
1192
1193 for (i = 0; i < wp->w_jumplistlen; ++i)
1194 vim_free(wp->w_jumplist[i].fname);
1195}
1196# endif
1197#endif /* FEAT_JUMPLIST */
1198
1199 void
1200set_last_cursor(win)
1201 win_T *win;
1202{
1203 win->w_buffer->b_last_cursor = win->w_cursor;
1204}
1205
1206#if defined(FEAT_VIMINFO) || defined(PROTO)
1207 int
1208read_viminfo_filemark(virp, force)
1209 vir_T *virp;
1210 int force;
1211{
1212 char_u *str;
1213 xfmark_T *fm;
1214 int i;
1215
1216 /* We only get here if line[0] == '\'' or '-'.
1217 * Illegal mark names are ignored (for future expansion). */
1218 str = virp->vir_line + 1;
1219 if (
1220#ifndef EBCDIC
1221 *str <= 127 &&
1222#endif
1223 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1224 || (*virp->vir_line == '-' && *str == '\'')))
1225 {
1226 if (*str == '\'')
1227 {
1228#ifdef FEAT_JUMPLIST
1229 /* If the jumplist isn't full insert fmark as oldest entry */
1230 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1231 fm = NULL;
1232 else
1233 {
1234 for (i = curwin->w_jumplistlen; i > 0; --i)
1235 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1236 ++curwin->w_jumplistidx;
1237 ++curwin->w_jumplistlen;
1238 fm = &curwin->w_jumplist[0];
1239 fm->fmark.mark.lnum = 0;
1240 fm->fname = NULL;
1241 }
1242#else
1243 fm = NULL;
1244#endif
1245 }
1246 else if (VIM_ISDIGIT(*str))
1247 fm = &namedfm[*str - '0' + NMARKS];
1248 else
1249 fm = &namedfm[*str - 'A'];
1250 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1251 {
1252 str = skipwhite(str + 1);
1253 fm->fmark.mark.lnum = getdigits(&str);
1254 str = skipwhite(str);
1255 fm->fmark.mark.col = getdigits(&str);
1256#ifdef FEAT_VIRTUALEDIT
1257 fm->fmark.mark.coladd = 0;
1258#endif
1259 fm->fmark.fnum = 0;
1260 str = skipwhite(str);
1261 vim_free(fm->fname);
1262 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1263 FALSE);
1264 }
1265 }
1266 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1267}
1268
1269 void
1270write_viminfo_filemarks(fp)
1271 FILE *fp;
1272{
1273 int i;
1274 char_u *name;
1275 buf_T *buf;
1276 xfmark_T *fm;
1277
1278 if (get_viminfo_parameter('f') == 0)
1279 return;
1280
1281 fprintf(fp, _("\n# File marks:\n"));
1282
1283 /*
1284 * Find a mark that is the same file and position as the cursor.
1285 * That one, or else the last one is deleted.
1286 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1287 * Set '0 mark to current cursor position.
1288 */
1289 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1290 {
1291 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1292 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1293 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1294 && (namedfm[i].fname == NULL
1295 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1296 : (name != NULL
1297 && STRCMP(name, namedfm[i].fname) == 0)))
1298 break;
1299 vim_free(name);
1300
1301 vim_free(namedfm[i].fname);
1302 for ( ; i > NMARKS; --i)
1303 namedfm[i] = namedfm[i - 1];
1304 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1305 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1306 namedfm[NMARKS].fname = NULL;
1307 }
1308
1309 /* Write the filemarks '0 - '9 and 'A - 'Z */
1310 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1311 write_one_filemark(fp, &namedfm[i], '\'',
1312 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1313
1314#ifdef FEAT_JUMPLIST
1315 /* Write the jumplist with -' */
1316 fprintf(fp, _("\n# Jumplist (newest first):\n"));
1317 setpcmark(); /* add current cursor position */
1318 cleanup_jumplist();
1319 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1320 fm >= &curwin->w_jumplist[0]; --fm)
1321 {
1322 if (fm->fmark.fnum == 0
1323 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1324 && !removable(buf->b_ffname)))
1325 write_one_filemark(fp, fm, '-', '\'');
1326 }
1327#endif
1328}
1329
1330 static void
1331write_one_filemark(fp, fm, c1, c2)
1332 FILE *fp;
1333 xfmark_T *fm;
1334 int c1;
1335 int c2;
1336{
1337 char_u *name;
1338
1339 if (fm->fmark.mark.lnum == 0) /* not set */
1340 return;
1341
1342 if (fm->fmark.fnum != 0) /* there is a buffer */
1343 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1344 else
1345 name = fm->fname; /* use name from .viminfo */
1346 if (name != NULL && *name != NUL)
1347 {
1348 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1349 (long)fm->fmark.mark.col);
1350 viminfo_writestring(fp, name);
1351 }
1352
1353 if (fm->fmark.fnum != 0)
1354 vim_free(name);
1355}
1356
1357/*
1358 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1359 */
1360 int
1361removable(name)
1362 char_u *name;
1363{
1364 char_u *p;
1365 char_u part[51];
1366 int retval = FALSE;
1367
1368 name = home_replace_save(NULL, name);
1369 if (name != NULL)
1370 {
1371 for (p = p_viminfo; *p; )
1372 {
1373 copy_option_part(&p, part, 51, ", ");
1374 if (part[0] == 'r'
1375 && MB_STRNICMP(part + 1, name, STRLEN(part + 1)) == 0)
1376 {
1377 retval = TRUE;
1378 break;
1379 }
1380 }
1381 vim_free(name);
1382 }
1383 return retval;
1384}
1385
1386static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1387
1388/*
1389 * Write all the named marks for all buffers.
1390 * Return the number of buffers for which marks have been written.
1391 */
1392 int
1393write_viminfo_marks(fp_out)
1394 FILE *fp_out;
1395{
1396 int count;
1397 buf_T *buf;
1398 int is_mark_set;
1399 int i;
1400#ifdef FEAT_WINDOWS
1401 win_T *win;
1402
1403 /*
1404 * Set b_last_cursor for the all buffers that have a window.
1405 */
1406 for (win = firstwin; win != NULL; win = win->w_next)
1407 set_last_cursor(win);
1408#else
1409 set_last_cursor(curwin);
1410#endif
1411
1412 fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
1413 count = 0;
1414 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1415 {
1416 /*
1417 * Only write something if buffer has been loaded and at least one
1418 * mark is set.
1419 */
1420 if (buf->b_marks_read)
1421 {
1422 if (buf->b_last_cursor.lnum != 0)
1423 is_mark_set = TRUE;
1424 else
1425 {
1426 is_mark_set = FALSE;
1427 for (i = 0; i < NMARKS; i++)
1428 if (buf->b_namedm[i].lnum != 0)
1429 {
1430 is_mark_set = TRUE;
1431 break;
1432 }
1433 }
1434 if (is_mark_set && buf->b_ffname != NULL
1435 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1436 {
1437 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1438 fprintf(fp_out, "\n> ");
1439 viminfo_writestring(fp_out, IObuff);
1440 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1441 write_one_mark(fp_out, '^', &buf->b_last_insert);
1442 write_one_mark(fp_out, '.', &buf->b_last_change);
1443#ifdef FEAT_JUMPLIST
1444 /* changelist positions are stored oldest first */
1445 for (i = 0; i < buf->b_changelistlen; ++i)
1446 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1447#endif
1448 for (i = 0; i < NMARKS; i++)
1449 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1450 count++;
1451 }
1452 }
1453 }
1454
1455 return count;
1456}
1457
1458 static void
1459write_one_mark(fp_out, c, pos)
1460 FILE *fp_out;
1461 int c;
1462 pos_T *pos;
1463{
1464 if (pos->lnum != 0)
1465 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1466}
1467
1468/*
1469 * Handle marks in the viminfo file:
1470 * fp_out == NULL read marks for current buffer only
1471 * fp_out != NULL copy marks for buffers not in buffer list
1472 */
1473 void
1474copy_viminfo_marks(virp, fp_out, count, eof)
1475 vir_T *virp;
1476 FILE *fp_out;
1477 int count;
1478 int eof;
1479{
1480 char_u *line = virp->vir_line;
1481 buf_T *buf;
1482 int num_marked_files;
1483 int load_marks;
1484 int copy_marks_out;
1485 char_u *str;
1486 int i;
1487 char_u *p;
1488 char_u *name_buf;
1489 pos_T pos;
1490
1491 if ((name_buf = alloc(LSIZE)) == NULL)
1492 return;
1493 *name_buf = NUL;
1494 num_marked_files = get_viminfo_parameter('\'');
1495 while (!eof && (count < num_marked_files || fp_out == NULL))
1496 {
1497 if (line[0] != '>')
1498 {
1499 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1500 {
1501 if (viminfo_error("E576: ", _("Missing '>'"), line))
1502 break; /* too many errors, return now */
1503 }
1504 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1505 continue; /* Skip this dud line */
1506 }
1507
1508 /*
1509 * Handle long line and translate escaped characters.
1510 * Find file name, set str to start.
1511 * Ignore leading and trailing white space.
1512 */
1513 str = skipwhite(line + 1);
1514 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1515 if (str == NULL)
1516 continue;
1517 p = str + STRLEN(str);
1518 while (p != str && (*p == NUL || vim_isspace(*p)))
1519 p--;
1520 if (*p)
1521 p++;
1522 *p = NUL;
1523
1524 /*
1525 * If fp_out == NULL, load marks for current buffer.
1526 * If fp_out != NULL, copy marks for buffers not in buflist.
1527 */
1528 load_marks = copy_marks_out = FALSE;
1529 if (fp_out == NULL)
1530 {
1531 if (curbuf->b_ffname != NULL)
1532 {
1533 if (*name_buf == NUL) /* only need to do this once */
1534 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1535 if (fnamecmp(str, name_buf) == 0)
1536 load_marks = TRUE;
1537 }
1538 }
1539 else /* fp_out != NULL */
1540 {
1541 /* This is slow if there are many buffers!! */
1542 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1543 if (buf->b_ffname != NULL)
1544 {
1545 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1546 if (fnamecmp(str, name_buf) == 0)
1547 break;
1548 }
1549
1550 /*
1551 * copy marks if the buffer has not been loaded
1552 */
1553 if (buf == NULL || !buf->b_marks_read)
1554 {
1555 copy_marks_out = TRUE;
1556 fputs("\n> ", fp_out);
1557 viminfo_writestring(fp_out, str);
1558 count++;
1559 }
1560 }
1561 vim_free(str);
1562
1563#ifdef FEAT_VIRTUALEDIT
1564 pos.coladd = 0;
1565#endif
1566 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1567 {
1568 if (load_marks)
1569 {
1570 if (line[1] != NUL)
1571 {
1572 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &pos.col);
1573 switch (line[1])
1574 {
1575 case '"': curbuf->b_last_cursor = pos; break;
1576 case '^': curbuf->b_last_insert = pos; break;
1577 case '.': curbuf->b_last_change = pos; break;
1578 case '+':
1579#ifdef FEAT_JUMPLIST
1580 /* changelist positions are stored oldest
1581 * first */
1582 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1583 /* list is full, remove oldest entry */
1584 mch_memmove(curbuf->b_changelist,
1585 curbuf->b_changelist + 1,
1586 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1587 else
1588 ++curbuf->b_changelistlen;
1589 curbuf->b_changelist[
1590 curbuf->b_changelistlen - 1] = pos;
1591#endif
1592 break;
1593 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1594 curbuf->b_namedm[i] = pos;
1595 }
1596 }
1597 }
1598 else if (copy_marks_out)
1599 fputs((char *)line, fp_out);
1600 }
1601 if (load_marks)
1602 {
1603#ifdef FEAT_JUMPLIST
1604 win_T *wp;
1605
1606 FOR_ALL_WINDOWS(wp)
1607 {
1608 if (wp->w_buffer == curbuf)
1609 wp->w_changelistidx = curbuf->b_changelistlen;
1610 }
1611#endif
1612 break;
1613 }
1614 }
1615 vim_free(name_buf);
1616}
1617#endif /* FEAT_VIMINFO */