blob: d3db4a601bbacc5579ecb577e5607794da314c0f [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 */
1007 qf_mark_adjust(line1, line2, amount, amount_after);
1008#endif
1009
1010#ifdef FEAT_SIGNS
1011 sign_mark_adjust(line1, line2, amount, amount_after);
1012#endif
1013 }
1014
1015 /* previous context mark */
1016 one_adjust(&(curwin->w_pcmark.lnum));
1017
1018 /* previous pcmark */
1019 one_adjust(&(curwin->w_prev_pcmark.lnum));
1020
1021 /* saved cursor for formatting */
1022 if (saved_cursor.lnum != 0)
1023 one_adjust_nodel(&(saved_cursor.lnum));
1024
1025 /*
1026 * Adjust items in all windows related to the current buffer.
1027 */
1028 FOR_ALL_WINDOWS(win)
1029 {
1030#ifdef FEAT_JUMPLIST
1031 if (!cmdmod.lockmarks)
1032 /* Marks in the jumplist. When deleting lines, this may create
1033 * duplicate marks in the jumplist, they will be removed later. */
1034 for (i = 0; i < win->w_jumplistlen; ++i)
1035 if (win->w_jumplist[i].fmark.fnum == fnum)
1036 one_adjust_nodel(&(win->w_jumplist[i].fmark.mark.lnum));
1037#endif
1038
1039 if (win->w_buffer == curbuf)
1040 {
1041 if (!cmdmod.lockmarks)
1042 /* marks in the tag stack */
1043 for (i = 0; i < win->w_tagstacklen; i++)
1044 if (win->w_tagstack[i].fmark.fnum == fnum)
1045 one_adjust_nodel(&(win->w_tagstack[i].fmark.mark.lnum));
1046
1047#ifdef FEAT_VISUAL
1048 /* the displayed Visual area */
1049 if (win->w_old_cursor_lnum != 0)
1050 {
1051 one_adjust_nodel(&(win->w_old_cursor_lnum));
1052 one_adjust_nodel(&(win->w_old_visual_lnum));
1053 }
1054#endif
1055
1056 /* topline and cursor position for windows with the same buffer
1057 * other than the current window */
1058 if (win != curwin)
1059 {
1060 if (win->w_topline >= line1 && win->w_topline <= line2)
1061 {
1062 if (amount == MAXLNUM) /* topline is deleted */
1063 {
1064 if (line1 <= 1)
1065 win->w_topline = 1;
1066 else
1067 win->w_topline = line1 - 1;
1068 }
1069 else /* keep topline on the same line */
1070 win->w_topline += amount;
1071#ifdef FEAT_DIFF
1072 win->w_topfill = 0;
1073#endif
1074 }
1075 else if (amount_after && win->w_topline > line2)
1076 {
1077 win->w_topline += amount_after;
1078#ifdef FEAT_DIFF
1079 win->w_topfill = 0;
1080#endif
1081 }
1082 if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
1083 {
1084 if (amount == MAXLNUM) /* line with cursor is deleted */
1085 {
1086 if (line1 <= 1)
1087 win->w_cursor.lnum = 1;
1088 else
1089 win->w_cursor.lnum = line1 - 1;
1090 win->w_cursor.col = 0;
1091 }
1092 else /* keep cursor on the same line */
1093 win->w_cursor.lnum += amount;
1094 }
1095 else if (amount_after && win->w_cursor.lnum > line2)
1096 win->w_cursor.lnum += amount_after;
1097 }
1098
1099#ifdef FEAT_FOLDING
1100 /* adjust folds */
1101 foldMarkAdjust(win, line1, line2, amount, amount_after);
1102#endif
1103 }
1104 }
1105
1106#ifdef FEAT_DIFF
1107 /* adjust diffs */
1108 diff_mark_adjust(line1, line2, amount, amount_after);
1109#endif
1110}
1111
1112/* This code is used often, needs to be fast. */
1113#define col_adjust(pp) \
1114 { \
1115 posp = pp; \
1116 if (posp->lnum == lnum && posp->col >= mincol) \
1117 { \
1118 posp->lnum += lnum_amount; \
1119 if (col_amount < 0 && posp->col <= (colnr_T)-col_amount) \
1120 posp->col = 0; \
1121 else \
1122 posp->col += col_amount; \
1123 } \
1124 }
1125
1126/*
1127 * Adjust marks in line "lnum" at column "mincol" and further: add
1128 * "lnum_amount" to the line number and add "col_amount" to the column
1129 * position.
1130 */
1131 void
1132mark_col_adjust(lnum, mincol, lnum_amount, col_amount)
1133 linenr_T lnum;
1134 colnr_T mincol;
1135 long lnum_amount;
1136 long col_amount;
1137{
1138 int i;
1139 int fnum = curbuf->b_fnum;
1140 win_T *win;
1141 pos_T *posp;
1142
1143 if ((col_amount == 0L && lnum_amount == 0L) || cmdmod.lockmarks)
1144 return; /* nothing to do */
1145
1146 /* named marks, lower case and upper case */
1147 for (i = 0; i < NMARKS; i++)
1148 {
1149 col_adjust(&(curbuf->b_namedm[i]));
1150 if (namedfm[i].fmark.fnum == fnum)
1151 col_adjust(&(namedfm[i].fmark.mark));
1152 }
1153 for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
1154 {
1155 if (namedfm[i].fmark.fnum == fnum)
1156 col_adjust(&(namedfm[i].fmark.mark));
1157 }
1158
1159 /* last Insert position */
1160 col_adjust(&(curbuf->b_last_insert));
1161
1162 /* last change position */
1163 col_adjust(&(curbuf->b_last_change));
1164
1165#ifdef FEAT_JUMPLIST
1166 /* list of change positions */
1167 for (i = 0; i < curbuf->b_changelistlen; ++i)
1168 col_adjust(&(curbuf->b_changelist[i]));
1169#endif
1170
1171#ifdef FEAT_VISUAL
1172 /* Visual area */
1173 col_adjust(&(curbuf->b_visual_start));
1174 col_adjust(&(curbuf->b_visual_end));
1175#endif
1176
1177 /* previous context mark */
1178 col_adjust(&(curwin->w_pcmark));
1179
1180 /* previous pcmark */
1181 col_adjust(&(curwin->w_prev_pcmark));
1182
1183 /* saved cursor for formatting */
1184 col_adjust(&saved_cursor);
1185
1186 /*
1187 * Adjust items in all windows related to the current buffer.
1188 */
1189 FOR_ALL_WINDOWS(win)
1190 {
1191#ifdef FEAT_JUMPLIST
1192 /* marks in the jumplist */
1193 for (i = 0; i < win->w_jumplistlen; ++i)
1194 if (win->w_jumplist[i].fmark.fnum == fnum)
1195 col_adjust(&(win->w_jumplist[i].fmark.mark));
1196#endif
1197
1198 if (win->w_buffer == curbuf)
1199 {
1200 /* marks in the tag stack */
1201 for (i = 0; i < win->w_tagstacklen; i++)
1202 if (win->w_tagstack[i].fmark.fnum == fnum)
1203 col_adjust(&(win->w_tagstack[i].fmark.mark));
1204
1205 /* cursor position for other windows with the same buffer */
1206 if (win != curwin)
1207 col_adjust(&win->w_cursor);
1208 }
1209 }
1210}
1211
1212#ifdef FEAT_JUMPLIST
1213/*
1214 * When deleting lines, this may create duplicate marks in the
1215 * jumplist. They will be removed here for the current window.
1216 */
1217 static void
1218cleanup_jumplist()
1219{
1220 int i;
1221 int from, to;
1222
1223 to = 0;
1224 for (from = 0; from < curwin->w_jumplistlen; ++from)
1225 {
1226 if (curwin->w_jumplistidx == from)
1227 curwin->w_jumplistidx = to;
1228 for (i = from + 1; i < curwin->w_jumplistlen; ++i)
1229 if (curwin->w_jumplist[i].fmark.fnum
1230 == curwin->w_jumplist[from].fmark.fnum
1231 && curwin->w_jumplist[from].fmark.fnum != 0
1232 && curwin->w_jumplist[i].fmark.mark.lnum
1233 == curwin->w_jumplist[from].fmark.mark.lnum)
1234 break;
1235 if (i >= curwin->w_jumplistlen) /* no duplicate */
1236 curwin->w_jumplist[to++] = curwin->w_jumplist[from];
1237 else
1238 vim_free(curwin->w_jumplist[from].fname);
1239 }
1240 if (curwin->w_jumplistidx == curwin->w_jumplistlen)
1241 curwin->w_jumplistidx = to;
1242 curwin->w_jumplistlen = to;
1243}
1244
1245# if defined(FEAT_WINDOWS) || defined(PROTO)
1246/*
1247 * Copy the jumplist from window "from" to window "to".
1248 */
1249 void
1250copy_jumplist(from, to)
1251 win_T *from;
1252 win_T *to;
1253{
1254 int i;
1255
1256 for (i = 0; i < from->w_jumplistlen; ++i)
1257 {
1258 to->w_jumplist[i] = from->w_jumplist[i];
1259 if (from->w_jumplist[i].fname != NULL)
1260 to->w_jumplist[i].fname = vim_strsave(from->w_jumplist[i].fname);
1261 }
1262 to->w_jumplistlen = from->w_jumplistlen;
1263 to->w_jumplistidx = from->w_jumplistidx;
1264}
1265
1266/*
1267 * Free items in the jumplist of window "wp".
1268 */
1269 void
1270free_jumplist(wp)
1271 win_T *wp;
1272{
1273 int i;
1274
1275 for (i = 0; i < wp->w_jumplistlen; ++i)
1276 vim_free(wp->w_jumplist[i].fname);
1277}
1278# endif
1279#endif /* FEAT_JUMPLIST */
1280
1281 void
1282set_last_cursor(win)
1283 win_T *win;
1284{
1285 win->w_buffer->b_last_cursor = win->w_cursor;
1286}
1287
Bram Moolenaarea408852005-06-25 22:49:46 +00001288#if defined(EXITFREE) || defined(PROTO)
1289 void
1290free_all_marks()
1291{
1292 int i;
1293
1294 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1295 if (namedfm[i].fmark.mark.lnum != 0)
1296 vim_free(namedfm[i].fname);
1297}
1298#endif
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#if defined(FEAT_VIMINFO) || defined(PROTO)
1301 int
1302read_viminfo_filemark(virp, force)
1303 vir_T *virp;
1304 int force;
1305{
1306 char_u *str;
1307 xfmark_T *fm;
1308 int i;
1309
1310 /* We only get here if line[0] == '\'' or '-'.
1311 * Illegal mark names are ignored (for future expansion). */
1312 str = virp->vir_line + 1;
1313 if (
1314#ifndef EBCDIC
1315 *str <= 127 &&
1316#endif
1317 ((*virp->vir_line == '\'' && (VIM_ISDIGIT(*str) || isupper(*str)))
1318 || (*virp->vir_line == '-' && *str == '\'')))
1319 {
1320 if (*str == '\'')
1321 {
1322#ifdef FEAT_JUMPLIST
1323 /* If the jumplist isn't full insert fmark as oldest entry */
1324 if (curwin->w_jumplistlen == JUMPLISTSIZE)
1325 fm = NULL;
1326 else
1327 {
1328 for (i = curwin->w_jumplistlen; i > 0; --i)
1329 curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
1330 ++curwin->w_jumplistidx;
1331 ++curwin->w_jumplistlen;
1332 fm = &curwin->w_jumplist[0];
1333 fm->fmark.mark.lnum = 0;
1334 fm->fname = NULL;
1335 }
1336#else
1337 fm = NULL;
1338#endif
1339 }
1340 else if (VIM_ISDIGIT(*str))
1341 fm = &namedfm[*str - '0' + NMARKS];
1342 else
1343 fm = &namedfm[*str - 'A'];
1344 if (fm != NULL && (fm->fmark.mark.lnum == 0 || force))
1345 {
1346 str = skipwhite(str + 1);
1347 fm->fmark.mark.lnum = getdigits(&str);
1348 str = skipwhite(str);
1349 fm->fmark.mark.col = getdigits(&str);
1350#ifdef FEAT_VIRTUALEDIT
1351 fm->fmark.mark.coladd = 0;
1352#endif
1353 fm->fmark.fnum = 0;
1354 str = skipwhite(str);
1355 vim_free(fm->fname);
1356 fm->fname = viminfo_readstring(virp, (int)(str - virp->vir_line),
1357 FALSE);
1358 }
1359 }
1360 return vim_fgets(virp->vir_line, LSIZE, virp->vir_fd);
1361}
1362
1363 void
1364write_viminfo_filemarks(fp)
1365 FILE *fp;
1366{
1367 int i;
1368 char_u *name;
1369 buf_T *buf;
1370 xfmark_T *fm;
1371
1372 if (get_viminfo_parameter('f') == 0)
1373 return;
1374
1375 fprintf(fp, _("\n# File marks:\n"));
1376
1377 /*
1378 * Find a mark that is the same file and position as the cursor.
1379 * That one, or else the last one is deleted.
1380 * Move '0 to '1, '1 to '2, etc. until the matching one or '9
1381 * Set '0 mark to current cursor position.
1382 */
1383 if (curbuf->b_ffname != NULL && !removable(curbuf->b_ffname))
1384 {
1385 name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
1386 for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
1387 if (namedfm[i].fmark.mark.lnum == curwin->w_cursor.lnum
1388 && (namedfm[i].fname == NULL
1389 ? namedfm[i].fmark.fnum == curbuf->b_fnum
1390 : (name != NULL
1391 && STRCMP(name, namedfm[i].fname) == 0)))
1392 break;
1393 vim_free(name);
1394
1395 vim_free(namedfm[i].fname);
1396 for ( ; i > NMARKS; --i)
1397 namedfm[i] = namedfm[i - 1];
1398 namedfm[NMARKS].fmark.mark = curwin->w_cursor;
1399 namedfm[NMARKS].fmark.fnum = curbuf->b_fnum;
1400 namedfm[NMARKS].fname = NULL;
1401 }
1402
1403 /* Write the filemarks '0 - '9 and 'A - 'Z */
1404 for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
1405 write_one_filemark(fp, &namedfm[i], '\'',
1406 i < NMARKS ? i + 'A' : i - NMARKS + '0');
1407
1408#ifdef FEAT_JUMPLIST
1409 /* Write the jumplist with -' */
1410 fprintf(fp, _("\n# Jumplist (newest first):\n"));
1411 setpcmark(); /* add current cursor position */
1412 cleanup_jumplist();
1413 for (fm = &curwin->w_jumplist[curwin->w_jumplistlen - 1];
1414 fm >= &curwin->w_jumplist[0]; --fm)
1415 {
1416 if (fm->fmark.fnum == 0
1417 || ((buf = buflist_findnr(fm->fmark.fnum)) != NULL
1418 && !removable(buf->b_ffname)))
1419 write_one_filemark(fp, fm, '-', '\'');
1420 }
1421#endif
1422}
1423
1424 static void
1425write_one_filemark(fp, fm, c1, c2)
1426 FILE *fp;
1427 xfmark_T *fm;
1428 int c1;
1429 int c2;
1430{
1431 char_u *name;
1432
1433 if (fm->fmark.mark.lnum == 0) /* not set */
1434 return;
1435
1436 if (fm->fmark.fnum != 0) /* there is a buffer */
1437 name = buflist_nr2name(fm->fmark.fnum, TRUE, FALSE);
1438 else
1439 name = fm->fname; /* use name from .viminfo */
1440 if (name != NULL && *name != NUL)
1441 {
1442 fprintf(fp, "%c%c %ld %ld ", c1, c2, (long)fm->fmark.mark.lnum,
1443 (long)fm->fmark.mark.col);
1444 viminfo_writestring(fp, name);
1445 }
1446
1447 if (fm->fmark.fnum != 0)
1448 vim_free(name);
1449}
1450
1451/*
1452 * Return TRUE if "name" is on removable media (depending on 'viminfo').
1453 */
1454 int
1455removable(name)
1456 char_u *name;
1457{
1458 char_u *p;
1459 char_u part[51];
1460 int retval = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001461 size_t n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 name = home_replace_save(NULL, name);
1464 if (name != NULL)
1465 {
1466 for (p = p_viminfo; *p; )
1467 {
1468 copy_option_part(&p, part, 51, ", ");
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001469 if (part[0] == 'r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001471 n = STRLEN(part + 1);
1472 if (MB_STRNICMP(part + 1, name, n) == 0)
1473 {
1474 retval = TRUE;
1475 break;
1476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478 }
1479 vim_free(name);
1480 }
1481 return retval;
1482}
1483
1484static void write_one_mark __ARGS((FILE *fp_out, int c, pos_T *pos));
1485
1486/*
1487 * Write all the named marks for all buffers.
1488 * Return the number of buffers for which marks have been written.
1489 */
1490 int
1491write_viminfo_marks(fp_out)
1492 FILE *fp_out;
1493{
1494 int count;
1495 buf_T *buf;
1496 int is_mark_set;
1497 int i;
1498#ifdef FEAT_WINDOWS
1499 win_T *win;
1500
1501 /*
1502 * Set b_last_cursor for the all buffers that have a window.
1503 */
1504 for (win = firstwin; win != NULL; win = win->w_next)
1505 set_last_cursor(win);
1506#else
1507 set_last_cursor(curwin);
1508#endif
1509
1510 fprintf(fp_out, _("\n# History of marks within files (newest to oldest):\n"));
1511 count = 0;
1512 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1513 {
1514 /*
1515 * Only write something if buffer has been loaded and at least one
1516 * mark is set.
1517 */
1518 if (buf->b_marks_read)
1519 {
1520 if (buf->b_last_cursor.lnum != 0)
1521 is_mark_set = TRUE;
1522 else
1523 {
1524 is_mark_set = FALSE;
1525 for (i = 0; i < NMARKS; i++)
1526 if (buf->b_namedm[i].lnum != 0)
1527 {
1528 is_mark_set = TRUE;
1529 break;
1530 }
1531 }
1532 if (is_mark_set && buf->b_ffname != NULL
1533 && buf->b_ffname[0] != NUL && !removable(buf->b_ffname))
1534 {
1535 home_replace(NULL, buf->b_ffname, IObuff, IOSIZE, TRUE);
1536 fprintf(fp_out, "\n> ");
1537 viminfo_writestring(fp_out, IObuff);
1538 write_one_mark(fp_out, '"', &buf->b_last_cursor);
1539 write_one_mark(fp_out, '^', &buf->b_last_insert);
1540 write_one_mark(fp_out, '.', &buf->b_last_change);
1541#ifdef FEAT_JUMPLIST
1542 /* changelist positions are stored oldest first */
1543 for (i = 0; i < buf->b_changelistlen; ++i)
1544 write_one_mark(fp_out, '+', &buf->b_changelist[i]);
1545#endif
1546 for (i = 0; i < NMARKS; i++)
1547 write_one_mark(fp_out, 'a' + i, &buf->b_namedm[i]);
1548 count++;
1549 }
1550 }
1551 }
1552
1553 return count;
1554}
1555
1556 static void
1557write_one_mark(fp_out, c, pos)
1558 FILE *fp_out;
1559 int c;
1560 pos_T *pos;
1561{
1562 if (pos->lnum != 0)
1563 fprintf(fp_out, "\t%c\t%ld\t%d\n", c, (long)pos->lnum, (int)pos->col);
1564}
1565
1566/*
1567 * Handle marks in the viminfo file:
1568 * fp_out == NULL read marks for current buffer only
1569 * fp_out != NULL copy marks for buffers not in buffer list
1570 */
1571 void
1572copy_viminfo_marks(virp, fp_out, count, eof)
1573 vir_T *virp;
1574 FILE *fp_out;
1575 int count;
1576 int eof;
1577{
1578 char_u *line = virp->vir_line;
1579 buf_T *buf;
1580 int num_marked_files;
1581 int load_marks;
1582 int copy_marks_out;
1583 char_u *str;
1584 int i;
1585 char_u *p;
1586 char_u *name_buf;
1587 pos_T pos;
1588
1589 if ((name_buf = alloc(LSIZE)) == NULL)
1590 return;
1591 *name_buf = NUL;
1592 num_marked_files = get_viminfo_parameter('\'');
1593 while (!eof && (count < num_marked_files || fp_out == NULL))
1594 {
1595 if (line[0] != '>')
1596 {
1597 if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1598 {
1599 if (viminfo_error("E576: ", _("Missing '>'"), line))
1600 break; /* too many errors, return now */
1601 }
1602 eof = vim_fgets(line, LSIZE, virp->vir_fd);
1603 continue; /* Skip this dud line */
1604 }
1605
1606 /*
1607 * Handle long line and translate escaped characters.
1608 * Find file name, set str to start.
1609 * Ignore leading and trailing white space.
1610 */
1611 str = skipwhite(line + 1);
1612 str = viminfo_readstring(virp, (int)(str - virp->vir_line), FALSE);
1613 if (str == NULL)
1614 continue;
1615 p = str + STRLEN(str);
1616 while (p != str && (*p == NUL || vim_isspace(*p)))
1617 p--;
1618 if (*p)
1619 p++;
1620 *p = NUL;
1621
1622 /*
1623 * If fp_out == NULL, load marks for current buffer.
1624 * If fp_out != NULL, copy marks for buffers not in buflist.
1625 */
1626 load_marks = copy_marks_out = FALSE;
1627 if (fp_out == NULL)
1628 {
1629 if (curbuf->b_ffname != NULL)
1630 {
1631 if (*name_buf == NUL) /* only need to do this once */
1632 home_replace(NULL, curbuf->b_ffname, name_buf, LSIZE, TRUE);
1633 if (fnamecmp(str, name_buf) == 0)
1634 load_marks = TRUE;
1635 }
1636 }
1637 else /* fp_out != NULL */
1638 {
1639 /* This is slow if there are many buffers!! */
1640 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1641 if (buf->b_ffname != NULL)
1642 {
1643 home_replace(NULL, buf->b_ffname, name_buf, LSIZE, TRUE);
1644 if (fnamecmp(str, name_buf) == 0)
1645 break;
1646 }
1647
1648 /*
1649 * copy marks if the buffer has not been loaded
1650 */
1651 if (buf == NULL || !buf->b_marks_read)
1652 {
1653 copy_marks_out = TRUE;
1654 fputs("\n> ", fp_out);
1655 viminfo_writestring(fp_out, str);
1656 count++;
1657 }
1658 }
1659 vim_free(str);
1660
1661#ifdef FEAT_VIRTUALEDIT
1662 pos.coladd = 0;
1663#endif
1664 while (!(eof = viminfo_readline(virp)) && line[0] == TAB)
1665 {
1666 if (load_marks)
1667 {
1668 if (line[1] != NUL)
1669 {
1670 sscanf((char *)line + 2, "%ld %u", &pos.lnum, &pos.col);
1671 switch (line[1])
1672 {
1673 case '"': curbuf->b_last_cursor = pos; break;
1674 case '^': curbuf->b_last_insert = pos; break;
1675 case '.': curbuf->b_last_change = pos; break;
1676 case '+':
1677#ifdef FEAT_JUMPLIST
1678 /* changelist positions are stored oldest
1679 * first */
1680 if (curbuf->b_changelistlen == JUMPLISTSIZE)
1681 /* list is full, remove oldest entry */
1682 mch_memmove(curbuf->b_changelist,
1683 curbuf->b_changelist + 1,
1684 sizeof(pos_T) * (JUMPLISTSIZE - 1));
1685 else
1686 ++curbuf->b_changelistlen;
1687 curbuf->b_changelist[
1688 curbuf->b_changelistlen - 1] = pos;
1689#endif
1690 break;
1691 default: if ((i = line[1] - 'a') >= 0 && i < NMARKS)
1692 curbuf->b_namedm[i] = pos;
1693 }
1694 }
1695 }
1696 else if (copy_marks_out)
1697 fputs((char *)line, fp_out);
1698 }
1699 if (load_marks)
1700 {
1701#ifdef FEAT_JUMPLIST
1702 win_T *wp;
1703
1704 FOR_ALL_WINDOWS(wp)
1705 {
1706 if (wp->w_buffer == curbuf)
1707 wp->w_changelistidx = curbuf->b_changelistlen;
1708 }
1709#endif
1710 break;
1711 }
1712 }
1713 vim_free(name_buf);
1714}
1715#endif /* FEAT_VIMINFO */