blob: f077b113337715c666b02147f49144c32d377d0a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* 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 */
28static xfmark_T namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */
29
30static void fname2fnum __ARGS((xfmark_T *fm));
31static void fmarks_check_one __ARGS((xfmark_T *fm, char_u *name, buf_T *buf));
32static char_u *mark_line __ARGS((pos_T *mp, int lead_len));
33static void show_one_mark __ARGS((int, char_u *, pos_T *, char_u *, int current));
34#ifdef FEAT_JUMPLIST
35static void cleanup_jumplist __ARGS((void));
36#endif
37#ifdef FEAT_VIMINFO
38static 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
46setmark(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
104setpcmark()
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
164checkpcmark()
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 *
180movemark(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 *
240movechangelist(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 *
277getmark(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 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000321 if (findpar(&oa.inclusive,
322 c == '}' ? FORWARD : BACKWARD, 1L, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
324 pos_copy = curwin->w_cursor;
325 posp = &pos_copy;
326 }
327 curwin->w_cursor = pos;
328 listcmd_busy = slcb;
329 }
330 else if (c == '(' || c == ')') /* to previous/next sentence */
331 {
332 pos_T pos;
333 int slcb = listcmd_busy;
334
335 pos = curwin->w_cursor;
336 listcmd_busy = TRUE; /* avoid that '' is changed */
337 if (findsent(c == ')' ? FORWARD : BACKWARD, 1L))
338 {
339 pos_copy = curwin->w_cursor;
340 posp = &pos_copy;
341 }
342 curwin->w_cursor = pos;
343 listcmd_busy = slcb;
344 }
345#ifdef FEAT_VISUAL
346 else if (c == '<' || c == '>') /* start/end of visual area */
347 {
348 startp = &curbuf->b_visual_start;
349 endp = &curbuf->b_visual_end;
350 if ((c == '<') == lt(*startp, *endp))
351 posp = startp;
352 else
353 posp = endp;
354 /*
355 * For Visual line mode, set mark at begin or end of line
356 */
357 if (curbuf->b_visual_mode == 'V')
358 {
359 pos_copy = *posp;
360 posp = &pos_copy;
361 if (c == '<')
362 pos_copy.col = 0;
363 else
364 pos_copy.col = MAXCOL;
365#ifdef FEAT_VIRTUALEDIT
366 pos_copy.coladd = 0;
367#endif
368 }
369 }
370#endif
371 else if (ASCII_ISLOWER(c)) /* normal named mark */
372 {
373 posp = &(curbuf->b_namedm[c - 'a']);
374 }
375 else if (ASCII_ISUPPER(c) || VIM_ISDIGIT(c)) /* named file mark */
376 {
377 if (VIM_ISDIGIT(c))
378 c = c - '0' + NMARKS;
379 else
380 c -= 'A';
381 posp = &(namedfm[c].fmark.mark);
382
383 if (namedfm[c].fmark.fnum == 0)
384 fname2fnum(&namedfm[c]);
385 if (namedfm[c].fmark.fnum != curbuf->b_fnum)
386 {
387 posp = &pos_copy;
388
389 /* mark is in another file */
390 if (namedfm[c].fmark.mark.lnum != 0
391 && changefile && namedfm[c].fmark.fnum)
392 {
393 if (buflist_getfile(namedfm[c].fmark.fnum,
394 (linenr_T)1, GETF_SETMARK, FALSE) == OK)
395 {
396 /* Set the lnum now, autocommands could have changed it */
397 curwin->w_cursor = namedfm[c].fmark.mark;
398 return (pos_T *)-1;
399 }
400 pos_copy.lnum = -1; /* can't get file */
401 }
402 else
403 pos_copy.lnum = 0; /* mark exists, but is not valid in
404 current buffer */
405 }
406 }
407
408 return posp;
409}
410
411/*
412 * Search for the next named mark in the current file.
413 *
414 * Returns pointer to pos_T of the next mark or NULL if no mark is found.
415 */
416 pos_T *
417getnextmark(startpos, dir, begin_line)
418 pos_T *startpos; /* where to start */
419 int dir; /* direction for search */
420 int begin_line;
421{
422 int i;
423 pos_T *result = NULL;
424 pos_T pos;
425
426 pos = *startpos;
427
428 /* When searching backward and leaving the cursor on the first non-blank,
429 * position must be in a previous line.
430 * When searching forward and leaving the cursor on the first non-blank,
431 * position must be in a next line. */
432 if (dir == BACKWARD && begin_line)
433 pos.col = 0;
434 else if (dir == FORWARD && begin_line)
435 pos.col = MAXCOL;
436
437 for (i = 0; i < NMARKS; i++)
438 {
439 if (curbuf->b_namedm[i].lnum > 0)
440 {
441 if (dir == FORWARD)
442 {
443 if ((result == NULL || lt(curbuf->b_namedm[i], *result))
444 && lt(pos, curbuf->b_namedm[i]))
445 result = &curbuf->b_namedm[i];
446 }
447 else
448 {
449 if ((result == NULL || lt(*result, curbuf->b_namedm[i]))
450 && lt(curbuf->b_namedm[i], pos))
451 result = &curbuf->b_namedm[i];
452 }
453 }
454 }
455
456 return result;
457}
458
459/*
460 * For an xtended filemark: set the fnum from the fname.
461 * This is used for marks obtained from the .viminfo file. It's postponed
462 * until the mark is used to avoid a long startup delay.
463 */
464 static void
465fname2fnum(fm)
466 xfmark_T *fm;
467{
468 char_u *p;
469
470 if (fm->fname != NULL)
471 {
472 /*
473 * First expand "~/" in the file name to the home directory.
474 * Try to shorten the file name.
475 */
476 expand_env(fm->fname, NameBuff, MAXPATHL);
477 mch_dirname(IObuff, IOSIZE);
478 p = shorten_fname(NameBuff, IObuff);
479
480 /* buflist_new() will call fmarks_check_names() */
481 (void)buflist_new(NameBuff, p, (linenr_T)1, 0);
482 }
483}
484
485/*
486 * Check all file marks for a name that matches the file name in buf.
487 * May replace the name with an fnum.
488 * Used for marks that come from the .viminfo file.
489 */
490 void
491fmarks_check_names(buf)
492 buf_T *buf;
493{
494 char_u *name;
495 int i;
496#ifdef FEAT_JUMPLIST
497 win_T *wp;
498#endif
499
500 if (buf->b_ffname == NULL)
501 return;
502
503 name = home_replace_save(buf, buf->b_ffname);
504 if (name == NULL)
505 return;
506
507 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
508 fmarks_check_one(&namedfm[i], name, buf);
509
510#ifdef FEAT_JUMPLIST
511 FOR_ALL_WINDOWS(wp)
512 {
513 for (i = 0; i < wp->w_jumplistlen; ++i)
514 fmarks_check_one(&wp->w_jumplist[i], name, buf);
515 }
516#endif
517
518 vim_free(name);
519}
520
521 static void
522fmarks_check_one(fm, name, buf)
523 xfmark_T *fm;
524 char_u *name;
525 buf_T *buf;
526{
527 if (fm->fmark.fnum == 0
528 && fm->fname != NULL
529 && fnamecmp(name, fm->fname) == 0)
530 {
531 fm->fmark.fnum = buf->b_fnum;
532 vim_free(fm->fname);
533 fm->fname = NULL;
534 }
535}
536
537/*
538 * Check a if a position from a mark is valid.
539 * Give and error message and return FAIL if not.
540 */
541 int
542check_mark(pos)
543 pos_T *pos;
544{
545 if (pos == NULL)
546 {
547 EMSG(_(e_umark));
548 return FAIL;
549 }
550 if (pos->lnum <= 0)
551 {
552 /* lnum is negative if mark is in another file can can't get that
553 * file, error message already give then. */
554 if (pos->lnum == 0)
555 EMSG(_(e_marknotset));
556 return FAIL;
557 }
558 if (pos->lnum > curbuf->b_ml.ml_line_count)
559 {
560 EMSG(_(e_markinval));
561 return FAIL;
562 }
563 return OK;
564}
565
566/*
567 * clrallmarks() - clear all marks in the buffer 'buf'
568 *
569 * Used mainly when trashing the entire buffer during ":e" type commands
570 */
571 void
572clrallmarks(buf)
573 buf_T *buf;
574{
575 static int i = -1;
576
577 if (i == -1) /* first call ever: initialize */
578 for (i = 0; i < NMARKS + 1; i++)
579 {
580 namedfm[i].fmark.mark.lnum = 0;
581 namedfm[i].fname = NULL;
582 }
583
584 for (i = 0; i < NMARKS; i++)
585 buf->b_namedm[i].lnum = 0;
586 buf->b_op_start.lnum = 0; /* start/end op mark cleared */
587 buf->b_op_end.lnum = 0;
588 buf->b_last_cursor.lnum = 1; /* '" mark cleared */
589 buf->b_last_cursor.col = 0;
590#ifdef FEAT_VIRTUALEDIT
591 buf->b_last_cursor.coladd = 0;
592#endif
593 buf->b_last_insert.lnum = 0; /* '^ mark cleared */
594 buf->b_last_change.lnum = 0; /* '. mark cleared */
595#ifdef FEAT_JUMPLIST
596 buf->b_changelistlen = 0;
597#endif
598}
599
600/*
601 * Get name of file from a filemark.
602 * When it's in the current buffer, return the text at the mark.
603 * Returns an allocated string.
604 */
605 char_u *
606fm_getname(fmark, lead_len)
607 fmark_T *fmark;
608 int lead_len;
609{
610 if (fmark->fnum == curbuf->b_fnum) /* current buffer */
611 return mark_line(&(fmark->mark), lead_len);
612 return buflist_nr2name(fmark->fnum, FALSE, TRUE);
613}
614
615/*
616 * Return the line at mark "mp". Truncate to fit in window.
617 * The returned string has been allocated.
618 */
619 static char_u *
620mark_line(mp, lead_len)
621 pos_T *mp;
622 int lead_len;
623{
624 char_u *s, *p;
625 int len;
626
627 if (mp->lnum == 0 || mp->lnum > curbuf->b_ml.ml_line_count)
628 return vim_strsave((char_u *)"-invalid-");
629 s = vim_strnsave(skipwhite(ml_get(mp->lnum)), (int)Columns);
630 if (s == NULL)
631 return NULL;
632 /* Truncate the line to fit it in the window */
633 len = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000634 for (p = s; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 {
636 len += ptr2cells(p);
637 if (len >= Columns - lead_len)
638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 }
640 *p = NUL;
641 return s;
642}
643
644/*
645 * print the marks
646 */
647 void
648do_marks(eap)
649 exarg_T *eap;
650{
651 char_u *arg = eap->arg;
652 int i;
653 char_u *name;
654
655 if (arg != NULL && *arg == NUL)
656 arg = NULL;
657
658 show_one_mark('\'', arg, &curwin->w_pcmark, NULL, TRUE);
659 for (i = 0; i < NMARKS; ++i)
660 show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL, TRUE);
661 for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
662 {
663 if (namedfm[i].fmark.fnum != 0)
664 name = fm_getname(&namedfm[i].fmark, 15);
665 else
666 name = namedfm[i].fname;
667 if (name != NULL)
668 {
669 show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
670 arg, &namedfm[i].fmark.mark, name,
671 namedfm[i].fmark.fnum == curbuf->b_fnum);
672 if (namedfm[i].fmark.fnum != 0)
673 vim_free(name);
674 }
675 }
676 show_one_mark('"', arg, &curbuf->b_last_cursor, NULL, TRUE);
677 show_one_mark('[', arg, &curbuf->b_op_start, NULL, TRUE);
678 show_one_mark(']', arg, &curbuf->b_op_end, NULL, TRUE);
679 show_one_mark('^', arg, &curbuf->b_last_insert, NULL, TRUE);
680 show_one_mark('.', arg, &curbuf->b_last_change, NULL, TRUE);
681#ifdef FEAT_VISUAL
682 show_one_mark('<', arg, &curbuf->b_visual_start, NULL, TRUE);
683 show_one_mark('>', arg, &curbuf->b_visual_end, NULL, TRUE);
684#endif
685 show_one_mark(-1, arg, NULL, NULL, FALSE);
686}
687
688 static void
689show_one_mark(c, arg, p, name, current)
690 int c;
691 char_u *arg;
692 pos_T *p;
693 char_u *name;
694 int current; /* in current file */
695{
696 static int did_title = FALSE;
697 int mustfree = FALSE;
698
699 if (c == -1) /* finish up */
700 {
701 if (did_title)
702 did_title = FALSE;
703 else
704 {
705 if (arg == NULL)
706 MSG(_("No marks set"));
707 else
708 EMSG2(_("E283: No marks matching \"%s\""), arg);
709 }
710 }
711 /* don't output anything if 'q' typed at --more-- prompt */
712 else if (!got_int
713 && (arg == NULL || vim_strchr(arg, c) != NULL)
714 && p->lnum != 0)
715 {
716 if (!did_title)
717 {
718 /* Highlight title */
719 MSG_PUTS_TITLE(_("\nmark line col file/text"));
720 did_title = TRUE;
721 }
722 msg_putchar('\n');
723 if (!got_int)
724 {
725 sprintf((char *)IObuff, " %c %6ld %4d ", c, p->lnum, p->col);
726 msg_outtrans(IObuff);
727 if (name == NULL && current)
728 {
729 name = mark_line(p, 15);
730 mustfree = TRUE;
731 }
732 if (name != NULL)
733 {
734 msg_outtrans_attr(name, current ? hl_attr(HLF_D) : 0);
735 if (mustfree)
736 vim_free(name);
737 }
738 }
739 out_flush(); /* show one line at a time */
740 }
741}
742
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000743/*
744 * ":delmarks[!] [marks]"
745 */
746 void
747ex_delmarks(eap)
748 exarg_T *eap;
749{
750 char_u *p;
751 int from, to;
752 int i;
753 int lower;
754 int digit;
755 int n;
756
757 if (*eap->arg == NUL && eap->forceit)
758 /* clear all marks */
759 clrallmarks(curbuf);
760 else if (eap->forceit)
761 EMSG(_(e_invarg));
762 else if (*eap->arg == NUL)
763 EMSG(_(e_argreq));
764 else
765 {
766 /* clear specified marks only */
767 for (p = eap->arg; *p != NUL; ++p)
768 {
769 lower = ASCII_ISLOWER(*p);
770 digit = VIM_ISDIGIT(*p);
771 if (lower || digit || ASCII_ISUPPER(*p))
772 {
773 if (p[1] == '-')
774 {
775 /* clear range of marks */
776 from = *p;
777 to = p[2];
778 if (!(lower ? ASCII_ISLOWER(p[2])
779 : (digit ? VIM_ISDIGIT(p[2])
780 : ASCII_ISUPPER(p[2])))
781 || to < from)
782 {
783 EMSG2(_(e_invarg2), p);
784 return;
785 }
786 p += 2;
787 }
788 else
789 /* clear one lower case mark */
790 from = to = *p;
791
792 for (i = from; i <= to; ++i)
793 {
794 if (lower)
795 curbuf->b_namedm[i - 'a'].lnum = 0;
796 else
797 {
798 if (digit)
799 n = i - '0' + NMARKS;
800 else
801 n = i - 'A';
802 namedfm[n].fmark.mark.lnum = 0;
803 vim_free(namedfm[n].fname);
804 namedfm[n].fname = NULL;
805 }
806 }
807 }
808 else
809 switch (*p)
810 {
811 case '"': curbuf->b_last_cursor.lnum = 0; break;
812 case '^': curbuf->b_last_insert.lnum = 0; break;
813 case '.': curbuf->b_last_change.lnum = 0; break;
814 case '[': curbuf->b_op_start.lnum = 0; break;
815 case ']': curbuf->b_op_end.lnum = 0; break;
816#ifdef FEAT_VISUAL
817 case '<': curbuf->b_visual_start.lnum = 0; break;
818 case '>': curbuf->b_visual_end.lnum = 0; break;
819#endif
820 case ' ': break;
821 default: EMSG2(_(e_invarg2), p);
822 return;
823 }
824 }
825 }
826}
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#if defined(FEAT_JUMPLIST) || defined(PROTO)
829/*
830 * print the jumplist
831 */
832/*ARGSUSED*/
833 void
834ex_jumps(eap)
835 exarg_T *eap;
836{
837 int i;
838 char_u *name;
839
840 cleanup_jumplist();
841 /* Highlight title */
842 MSG_PUTS_TITLE(_("\n jump line col file/text"));
843 for (i = 0; i < curwin->w_jumplistlen && !got_int; ++i)
844 {
845 if (curwin->w_jumplist[i].fmark.mark.lnum != 0)
846 {
847 if (curwin->w_jumplist[i].fmark.fnum == 0)
848 fname2fnum(&curwin->w_jumplist[i]);
849 name = fm_getname(&curwin->w_jumplist[i].fmark, 16);
850 if (name == NULL) /* file name not available */
851 continue;
852
853 msg_putchar('\n');
854 if (got_int)
855 break;
856 sprintf((char *)IObuff, "%c %2d %5ld %4d ",
857 i == curwin->w_jumplistidx ? '>' : ' ',
858 i > curwin->w_jumplistidx ? i - curwin->w_jumplistidx
859 : curwin->w_jumplistidx - i,
860 curwin->w_jumplist[i].fmark.mark.lnum,
861 curwin->w_jumplist[i].fmark.mark.col);
862 msg_outtrans(IObuff);
863 msg_outtrans_attr(name,
864 curwin->w_jumplist[i].fmark.fnum == curbuf->b_fnum
865 ? hl_attr(HLF_D) : 0);
866 vim_free(name);
867 ui_breakcheck();
868 }
869 out_flush();
870 }
871 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
872 MSG_PUTS("\n>");
873}
874
875/*
876 * print the changelist
877 */
878/*ARGSUSED*/
879 void
880ex_changes(eap)
881 exarg_T *eap;
882{
883 int i;
884 char_u *name;
885
886 /* Highlight title */
887 MSG_PUTS_TITLE(_("\nchange line col text"));
888
889 for (i = 0; i < curbuf->b_changelistlen && !got_int; ++i)
890 {
891 if (curbuf->b_changelist[i].lnum != 0)
892 {
893 msg_putchar('\n');
894 if (got_int)
895 break;
896 sprintf((char *)IObuff, "%c %3d %5ld %4d ",
897 i == curwin->w_changelistidx ? '>' : ' ',
898 i > curwin->w_changelistidx ? i - curwin->w_changelistidx
899 : curwin->w_changelistidx - i,
900 (long)curbuf->b_changelist[i].lnum,
901 curbuf->b_changelist[i].col);
902 msg_outtrans(IObuff);
903 name = mark_line(&curbuf->b_changelist[i], 17);
904 if (name == NULL)
905 break;
906 msg_outtrans_attr(name, hl_attr(HLF_D));
907 vim_free(name);
908 ui_breakcheck();
909 }
910 out_flush();
911 }
912 if (curwin->w_changelistidx == curbuf->b_changelistlen)
913 MSG_PUTS("\n>");
914}
915#endif
916
917#define one_adjust(add) \
918 { \
919 lp = add; \
920 if (*lp >= line1 && *lp <= line2) \
921 { \
922 if (amount == MAXLNUM) \
923 *lp = 0; \
924 else \
925 *lp += amount; \
926 } \
927 else if (amount_after && *lp > line2) \
928 *lp += amount_after; \
929 }
930
931/* don't delete the line, just put at first deleted line */
932#define one_adjust_nodel(add) \
933 { \
934 lp = add; \
935 if (*lp >= line1 && *lp <= line2) \
936 { \
937 if (amount == MAXLNUM) \
938 *lp = line1; \
939 else \
940 *lp += amount; \
941 } \
942 else if (amount_after && *lp > line2) \
943 *lp += amount_after; \
944 }
945
946/*
947 * Adjust marks between line1 and line2 (inclusive) to move 'amount' lines.
948 * Must be called before changed_*(), appended_lines() or deleted_lines().
949 * May be called before or after changing the text.
950 * When deleting lines line1 to line2, use an 'amount' of MAXLNUM: The marks
951 * within this range are made invalid.
952 * If 'amount_after' is non-zero adjust marks after line2.
953 * Example: Delete lines 34 and 35: mark_adjust(34, 35, MAXLNUM, -2);
954 * Example: Insert two lines below 55: mark_adjust(56, MAXLNUM, 2, 0);
955 * or: mark_adjust(56, 55, MAXLNUM, 2);
956 */
957 void
958mark_adjust(line1, line2, amount, amount_after)
959 linenr_T line1;
960 linenr_T line2;
961 long amount;
962 long amount_after;
963{
964 int i;
965 int fnum = curbuf->b_fnum;
966 linenr_T *lp;
967 win_T *win;
968
969 if (line2 < line1 && amount_after == 0L) /* nothing to do */
970 return;
971
972 if (!cmdmod.lockmarks)
973 {
974 /* named marks, lower case and upper case */
975 for (i = 0; i < NMARKS; i++)
976 {
977 one_adjust(&(curbuf->b_namedm[i].lnum));
978 if (namedfm[i].fmark.fnum == fnum)
979 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
980 }
981 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
982 {
983 if (namedfm[i].fmark.fnum == fnum)
984 one_adjust_nodel(&(namedfm[i].fmark.mark.lnum));
985 }
986
987 /* last Insert position */
988 one_adjust(&(curbuf->b_last_insert.lnum));
989
990 /* last change position */
991 one_adjust(&(curbuf->b_last_change.lnum));
992
993#ifdef FEAT_JUMPLIST
994 /* list of change positions */
995 for (i = 0; i < curbuf->b_changelistlen; ++i)
996 one_adjust_nodel(&(curbuf->b_changelist[i].lnum));
997#endif
998
999#ifdef FEAT_VISUAL
1000 /* Visual area */
1001 one_adjust_nodel(&(curbuf->b_visual_start.lnum));
1002 one_adjust_nodel(&(curbuf->b_visual_end.lnum));
1003#endif
1004
1005#ifdef FEAT_QUICKFIX
1006 /* quickfix marks */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001007 qf_mark_adjust(NULL, line1, line2, amount, amount_after);
1008 /* location lists */
1009 FOR_ALL_WINDOWS(win)
1010 qf_mark_adjust(win, line1, line2, amount, amount_after);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#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
1135mark_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
1221cleanup_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
1253copy_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
1273free_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
1285set_last_cursor(win)
1286 win_T *win;
1287{
1288 win->w_buffer->b_last_cursor = win->w_cursor;
1289}
1290
Bram Moolenaarea408852005-06-25 22:49:46 +00001291#if defined(EXITFREE) || defined(PROTO)
1292 void
1293free_all_marks()
1294{
1295 int i;
1296
1297 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1298 if (namedfm[i].fmark.mark.lnum != 0)
1299 vim_free(namedfm[i].fname);
1300}
1301#endif
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#if defined(FEAT_VIMINFO) || defined(PROTO)
1304 int
1305read_viminfo_filemark(virp, force)
1306 vir_T *virp;
1307 int force;
1308{
1309 char_u *str;
1310 xfmark_T *fm;
1311 int i;
1312
1313 /* We only get here if line[0] == '\'' or '-'.
1314 * Illegal mark names are ignored (for future expansion). */
1315 str = virp->vir_line + 1;
1316 if (
1317#ifndef EBCDIC
1318 *str <= 127 &&
1319#endif
1320 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1321 || (*virp->vir_line == '-' && *str == '\'')))
1322 {
1323 if (*str == '\'')
1324 {
1325#ifdef FEAT_JUMPLIST
1326 /* If the jumplist isn't full insert fmark as oldest entry */
1327 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1328 fm = NULL;
1329 else
1330 {
1331 for (i = curwin->w_jumplistlen; i > 0; --i)
1332 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1333 ++curwin->w_jumplistidx;
1334 ++curwin->w_jumplistlen;
1335 fm = &curwin->w_jumplist[0];
1336 fm->fmark.mark.lnum = 0;
1337 fm->fname = NULL;
1338 }
1339#else
1340 fm = NULL;
1341#endif
1342 }
1343 else if (VIM_ISDIGIT(*str))
1344 fm = &namedfm[*str - '0' + NMARKS];
1345 else
1346 fm = &namedfm[*str - 'A'];
1347 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1348 {
1349 str = skipwhite(str + 1);
1350 fm->fmark.mark.lnum = getdigits(&str);
1351 str = skipwhite(str);
1352 fm->fmark.mark.col = getdigits(&str);
1353#ifdef FEAT_VIRTUALEDIT
1354 fm->fmark.mark.coladd = 0;
1355#endif
1356 fm->fmark.fnum = 0;
1357 str = skipwhite(str);
1358 vim_free(fm->fname);
1359 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1360 FALSE);
1361 }
1362 }
1363 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1364}
1365
1366 void
1367write_viminfo_filemarks(fp)
1368 FILE *fp;
1369{
1370 int i;
1371 char_u *name;
1372 buf_T *buf;
1373 xfmark_T *fm;
1374
1375 if (get_viminfo_parameter('f') == 0)
1376 return;
1377
1378 fprintf(fp, _("\n# File marks:\n"));
1379
1380 /*
1381 * Find a mark that is the same file and position as the cursor.
1382 * That one, or else the last one is deleted.
1383 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1384 * Set '0 mark to current cursor position.
1385 */
1386 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1387 {
1388 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1389 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1390 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1391 && (namedfm[i].fname == NULL
1392 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1393 : (name != NULL
1394 && STRCMP(name, namedfm[i].fname) == 0)))
1395 break;
1396 vim_free(name);
1397
1398 vim_free(namedfm[i].fname);
1399 for ( ; i > NMARKS; --i)
1400 namedfm[i] = namedfm[i - 1];
1401 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1402 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1403 namedfm[NMARKS].fname = NULL;
1404 }
1405
1406 /* Write the filemarks '0 - '9 and 'A - 'Z */
1407 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1408 write_one_filemark(fp, &namedfm[i], '\'',
1409 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1410
1411#ifdef FEAT_JUMPLIST
1412 /* Write the jumplist with -' */
1413 fprintf(fp, _("\n# Jumplist (newest first):\n"));
1414 setpcmark(); /* add current cursor position */
1415 cleanup_jumplist();
1416 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1417 fm >= &curwin->w_jumplist[0]; --fm)
1418 {
1419 if (fm->fmark.fnum == 0
1420 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1421 && !removable(buf->b_ffname)))
1422 write_one_filemark(fp, fm, '-', '\'');
1423 }
1424#endif
1425}
1426
1427 static void
1428write_one_filemark(fp, fm, c1, c2)
1429 FILE *fp;
1430 xfmark_T *fm;
1431 int c1;
1432 int c2;
1433{
1434 char_u *name;
1435
1436 if (fm->fmark.mark.lnum == 0) /* not set */
1437 return;
1438
1439 if (fm->fmark.fnum != 0) /* there is a buffer */
1440 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1441 else
1442 name = fm->fname; /* use name from .viminfo */
1443 if (name != NULL && *name != NUL)
1444 {
1445 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1446 (long)fm->fmark.mark.col);
1447 viminfo_writestring(fp, name);
1448 }
1449
1450 if (fm->fmark.fnum != 0)
1451 vim_free(name);
1452}
1453
1454/*
1455 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1456 */
1457 int
1458removable(name)
1459 char_u *name;
1460{
1461 char_u *p;
1462 char_u part[51];
1463 int retval = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001464 size_t n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 name = home_replace_save(NULL, name);
1467 if (name != NULL)
1468 {
1469 for (p = p_viminfo; *p; )
1470 {
1471 copy_option_part(&p, part, 51, ", ");
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001472 if (part[0] == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001474 n = STRLEN(part + 1);
1475 if (MB_STRNICMP(part + 1, name, n) == 0)
1476 {
1477 retval = TRUE;
1478 break;
1479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 }
1482 vim_free(name);
1483 }
1484 return retval;
1485}
1486
1487static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1488
1489/*
1490 * Write all the named marks for all buffers.
1491 * Return the number of buffers for which marks have been written.
1492 */
1493 int
1494write_viminfo_marks(fp_out)
1495 FILE *fp_out;
1496{
1497 int count;
1498 buf_T *buf;
1499 int is_mark_set;
1500 int i;
1501#ifdef FEAT_WINDOWS
1502 win_T *win;
1503
1504 /*
1505 * Set b_last_cursor for the all buffers that have a window.
1506 */
1507 for (win = firstwin; win != NULL; win = win->w_next)
1508 set_last_cursor(win);
1509#else
1510 set_last_cursor(curwin);
1511#endif
1512
1513 fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
1514 count = 0;
1515 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1516 {
1517 /*
1518 * Only write something if buffer has been loaded and at least one
1519 * mark is set.
1520 */
1521 if (buf->b_marks_read)
1522 {
1523 if (buf->b_last_cursor.lnum != 0)
1524 is_mark_set = TRUE;
1525 else
1526 {
1527 is_mark_set = FALSE;
1528 for (i = 0; i < NMARKS; i++)
1529 if (buf->b_namedm[i].lnum != 0)
1530 {
1531 is_mark_set = TRUE;
1532 break;
1533 }
1534 }
1535 if (is_mark_set && buf->b_ffname != NULL
1536 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1537 {
1538 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1539 fprintf(fp_out, "\n> ");
1540 viminfo_writestring(fp_out, IObuff);
1541 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1542 write_one_mark(fp_out, '^', &buf->b_last_insert);
1543 write_one_mark(fp_out, '.', &buf->b_last_change);
1544#ifdef FEAT_JUMPLIST
1545 /* changelist positions are stored oldest first */
1546 for (i = 0; i < buf->b_changelistlen; ++i)
1547 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1548#endif
1549 for (i = 0; i < NMARKS; i++)
1550 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1551 count++;
1552 }
1553 }
1554 }
1555
1556 return count;
1557}
1558
1559 static void
1560write_one_mark(fp_out, c, pos)
1561 FILE *fp_out;
1562 int c;
1563 pos_T *pos;
1564{
1565 if (pos->lnum != 0)
1566 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1567}
1568
1569/*
1570 * Handle marks in the viminfo file:
1571 * fp_out == NULL read marks for current buffer only
1572 * fp_out != NULL copy marks for buffers not in buffer list
1573 */
1574 void
1575copy_viminfo_marks(virp, fp_out, count, eof)
1576 vir_T *virp;
1577 FILE *fp_out;
1578 int count;
1579 int eof;
1580{
1581 char_u *line = virp->vir_line;
1582 buf_T *buf;
1583 int num_marked_files;
1584 int load_marks;
1585 int copy_marks_out;
1586 char_u *str;
1587 int i;
1588 char_u *p;
1589 char_u *name_buf;
1590 pos_T pos;
1591
1592 if ((name_buf = alloc(LSIZE)) == NULL)
1593 return;
1594 *name_buf = NUL;
1595 num_marked_files = get_viminfo_parameter('\'');
1596 while (!eof && (count < num_marked_files || fp_out == NULL))
1597 {
1598 if (line[0] != '>')
1599 {
1600 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1601 {
1602 if (viminfo_error("E576: ", _("Missing '>'"), line))
1603 break; /* too many errors, return now */
1604 }
1605 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1606 continue; /* Skip this dud line */
1607 }
1608
1609 /*
1610 * Handle long line and translate escaped characters.
1611 * Find file name, set str to start.
1612 * Ignore leading and trailing white space.
1613 */
1614 str = skipwhite(line + 1);
1615 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1616 if (str == NULL)
1617 continue;
1618 p = str + STRLEN(str);
1619 while (p != str && (*p == NUL || vim_isspace(*p)))
1620 p--;
1621 if (*p)
1622 p++;
1623 *p = NUL;
1624
1625 /*
1626 * If fp_out == NULL, load marks for current buffer.
1627 * If fp_out != NULL, copy marks for buffers not in buflist.
1628 */
1629 load_marks = copy_marks_out = FALSE;
1630 if (fp_out == NULL)
1631 {
1632 if (curbuf->b_ffname != NULL)
1633 {
1634 if (*name_buf == NUL) /* only need to do this once */
1635 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1636 if (fnamecmp(str, name_buf) == 0)
1637 load_marks = TRUE;
1638 }
1639 }
1640 else /* fp_out != NULL */
1641 {
1642 /* This is slow if there are many buffers!! */
1643 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1644 if (buf->b_ffname != NULL)
1645 {
1646 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1647 if (fnamecmp(str, name_buf) == 0)
1648 break;
1649 }
1650
1651 /*
1652 * copy marks if the buffer has not been loaded
1653 */
1654 if (buf == NULL || !buf->b_marks_read)
1655 {
1656 copy_marks_out = TRUE;
1657 fputs("\n> ", fp_out);
1658 viminfo_writestring(fp_out, str);
1659 count++;
1660 }
1661 }
1662 vim_free(str);
1663
1664#ifdef FEAT_VIRTUALEDIT
1665 pos.coladd = 0;
1666#endif
1667 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1668 {
1669 if (load_marks)
1670 {
1671 if (line[1] != NUL)
1672 {
1673 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &pos.col);
1674 switch (line[1])
1675 {
1676 case '"': curbuf->b_last_cursor = pos; break;
1677 case '^': curbuf->b_last_insert = pos; break;
1678 case '.': curbuf->b_last_change = pos; break;
1679 case '+':
1680#ifdef FEAT_JUMPLIST
1681 /* changelist positions are stored oldest
1682 * first */
1683 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1684 /* list is full, remove oldest entry */
1685 mch_memmove(curbuf->b_changelist,
1686 curbuf->b_changelist + 1,
1687 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1688 else
1689 ++curbuf->b_changelistlen;
1690 curbuf->b_changelist[
1691 curbuf->b_changelistlen - 1] = pos;
1692#endif
1693 break;
1694 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1695 curbuf->b_namedm[i] = pos;
1696 }
1697 }
1698 }
1699 else if (copy_marks_out)
1700 fputs((char *)line, fp_out);
1701 }
1702 if (load_marks)
1703 {
1704#ifdef FEAT_JUMPLIST
1705 win_T *wp;
1706
1707 FOR_ALL_WINDOWS(wp)
1708 {
1709 if (wp->w_buffer == curbuf)
1710 wp->w_changelistidx = curbuf->b_changelistlen;
1711 }
1712#endif
1713 break;
1714 }
1715 }
1716 vim_free(name_buf);
1717}
1718#endif /* FEAT_VIMINFO */