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