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