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