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