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