Annotation of src/usr.bin/vim/mark.c, Revision 1.2
1.2 ! downsj 1: /* $OpenBSD: mark.c,v 1.1.1.1 1996/09/07 21:40:26 downsj Exp $ */
1.1 downsj 2: /* vi:set ts=4 sw=4:
3: *
4: * VIM - Vi IMproved by Bram Moolenaar
5: *
6: * Do ":help uganda" in Vim to read copying and usage conditions.
7: * Do ":help credits" in Vim to see a list of people who contributed.
8: */
9:
10: /*
11: * mark.c: functions for setting marks and jumping to them
12: */
13:
14: #include "vim.h"
15: #include "globals.h"
16: #include "proto.h"
17: #include "option.h"
18:
19: /*
20: * This file contains routines to maintain and manipulate marks.
21: */
22:
23: /*
24: * If a named file mark's lnum is non-zero, it is valid.
25: * If a named file mark's fnum is non-zero, it is for an existing buffer,
26: * otherwise it is from .viminfo and namedfm_names[n] is the file name.
27: * There are marks 'A - 'Z (set by user) and '0 to '9 (set when writing
28: * viminfo).
29: */
30: #define EXTRA_MARKS 10 /* marks 0-9 */
31: static struct filemark namedfm[NMARKS + EXTRA_MARKS]; /* marks with file nr */
32: static char_u *namedfm_names[NMARKS + EXTRA_MARKS]; /* name for namedfm[] */
33:
34: static void show_one_mark __ARGS((int, char_u *, FPOS *, char_u *));
35: static void cleanup_jumplist __ARGS((void));
36: #ifdef VIMINFO
37: static int removable __ARGS((char_u *name));
38: #endif
39:
40: /*
41: * setmark(c) - set named mark 'c' at current cursor position
42: *
43: * Returns OK on success, FAIL if no room for mark or bad name given.
44: */
45: int
46: setmark(c)
47: int c;
48: {
49: int i;
50:
51: if (c > 'z') /* some islower() and isupper() cannot handle
52: characters above 127 */
53: return FAIL;
54: if (islower(c))
55: {
56: i = c - 'a';
57: curbuf->b_namedm[i] = curwin->w_cursor;
58: return OK;
59: }
60: if (isupper(c))
61: {
62: i = c - 'A';
63: namedfm[i].mark = curwin->w_cursor;
64: namedfm[i].fnum = curbuf->b_fnum;
65: return OK;
66: }
67: return FAIL;
68: }
69:
70: /*
71: * setpcmark() - set the previous context mark to the current position
72: * and add it to the jump list
73: */
74: void
75: setpcmark()
76: {
77: int i;
78: #ifdef ROTATE
79: struct filemark tempmark;
80: #endif
81:
82: /* for :global the mark is set only once */
83: if (global_busy)
84: return;
85:
86: curwin->w_prev_pcmark = curwin->w_pcmark;
87: curwin->w_pcmark = curwin->w_cursor;
88:
89: #ifdef ROTATE
90: /*
91: * If last used entry is not at the top, put it at the top by rotating
92: * the stack until it is (the newer entries will be at the bottom).
93: * Keep one entry (the last used one) at the top.
94: */
95: if (curwin->w_jumplistidx < curwin->w_jumplistlen)
96: ++curwin->w_jumplistidx;
97: while (curwin->w_jumplistidx < curwin->w_jumplistlen)
98: {
99: tempmark = curwin->w_jumplist[curwin->w_jumplistlen - 1];
100: for (i = curwin->w_jumplistlen - 1; i > 0; --i)
101: curwin->w_jumplist[i] = curwin->w_jumplist[i - 1];
102: curwin->w_jumplist[0] = tempmark;
103: ++curwin->w_jumplistidx;
104: }
105: #endif
106:
107: /* If jumplist is full: remove oldest entry */
108: if (++curwin->w_jumplistlen > JUMPLISTSIZE)
109: {
110: curwin->w_jumplistlen = JUMPLISTSIZE;
111: for (i = 1; i < JUMPLISTSIZE; ++i)
112: curwin->w_jumplist[i - 1] = curwin->w_jumplist[i];
113: }
114: curwin->w_jumplistidx = curwin->w_jumplistlen - 1;
115:
116: #ifdef ARCHIE
117: /* Workaround for a bug in gcc 2.4.5 R2 on the Archimedes
118: * Should be fixed in 2.5.x.
119: */
120: curwin->w_jumplist[curwin->w_jumplistidx].mark.ptr = curwin->w_pcmark.ptr;
121: curwin->w_jumplist[curwin->w_jumplistidx].mark.col = curwin->w_pcmark.col;
122: #else
123: curwin->w_jumplist[curwin->w_jumplistidx].mark = curwin->w_pcmark;
124: #endif
125: curwin->w_jumplist[curwin->w_jumplistidx].fnum = curbuf->b_fnum;
126: ++curwin->w_jumplistidx;
127:
128: /* remove any duplicates, from the new entry or from previous deletes */
129: cleanup_jumplist();
130: }
131:
132: /*
133: * checkpcmark() - To change context, call setpcmark(), then move the current
134: * position to where ever, then call checkpcmark(). This
135: * ensures that the previous context will only be changed if
136: * the cursor moved to a different line. -- webb.
137: * If pcmark was deleted (with "dG") the previous mark is
138: * restored.
139: */
140: void
141: checkpcmark()
142: {
143: if (curwin->w_prev_pcmark.lnum != 0 &&
144: (equal(curwin->w_pcmark, curwin->w_cursor) ||
145: curwin->w_pcmark.lnum == 0))
146: {
147: curwin->w_pcmark = curwin->w_prev_pcmark;
148: curwin->w_prev_pcmark.lnum = 0; /* Show it has been checked */
149: }
150: }
151:
152: /*
153: * move "count" positions in the jump list (count may be negative)
154: */
155: FPOS *
156: movemark(count)
157: int count;
158: {
159: FPOS *pos;
160:
161: cleanup_jumplist();
162:
163: if (curwin->w_jumplistlen == 0) /* nothing to jump to */
164: return (FPOS *)NULL;
165:
166: if (curwin->w_jumplistidx + count < 0 ||
167: curwin->w_jumplistidx + count >= curwin->w_jumplistlen)
168: return (FPOS *)NULL;
169:
170: /*
171: * if first CTRL-O or CTRL-I command after a jump, add cursor position to
172: * list. Careful: If there are duplicates (CTRL-O immidiately after
173: * starting Vim on a file), another entry may have been removed.
174: */
175: if (curwin->w_jumplistidx == curwin->w_jumplistlen)
176: {
177: setpcmark();
178: --curwin->w_jumplistidx; /* skip the new entry */
179: if (curwin->w_jumplistidx + count < 0)
180: return (FPOS *)NULL;
181: }
182:
183: curwin->w_jumplistidx += count;
184: /* jump to other file */
185: if (curwin->w_jumplist[curwin->w_jumplistidx].fnum != curbuf->b_fnum)
186: {
187: if (buflist_getfile(curwin->w_jumplist[curwin->w_jumplistidx].fnum,
188: curwin->w_jumplist[curwin->w_jumplistidx].mark.lnum,
1.2 ! downsj 189: 0, FALSE) == FAIL)
1.1 downsj 190: return (FPOS *)NULL;
191: curwin->w_cursor.col =
192: curwin->w_jumplist[curwin->w_jumplistidx].mark.col;
193: pos = (FPOS *)-1;
194: }
195: else
196: pos = &(curwin->w_jumplist[curwin->w_jumplistidx].mark);
197: return pos;
198: }
199:
200: /*
201: * getmark(c) - find mark for char 'c'
202: *
203: * Return pointer to FPOS if found (caller needs to check lnum!)
204: * NULL if there is no mark called 'c'.
205: * -1 if mark is in other file (only if changefile is TRUE)
206: */
207: FPOS *
208: getmark(c, changefile)
209: int c;
210: int changefile;
211: {
212: FPOS *posp;
213: FPOS *startp, *endp;
214: static FPOS pos_copy;
215: int len;
216: char_u *p;
217:
218: posp = NULL;
219: if (c > '~') /* check for islower()/isupper() */
220: ;
221: else if (c == '\'' || c == '`') /* previous context mark */
222: {
223: pos_copy = curwin->w_pcmark; /* need to make a copy because */
224: posp = &pos_copy; /* w_pcmark may be changed soon */
225: }
226: else if (c == '"') /* to pos when leaving buffer */
227: posp = &(curbuf->b_last_cursor);
228: else if (c == '[') /* to start of previous operator */
229: posp = &(curbuf->b_op_start);
230: else if (c == ']') /* to end of previous operator */
231: posp = &(curbuf->b_op_end);
232: else if (c == '<' || c == '>') /* start/end of visual area */
233: {
234: if (VIsual_active)
235: startp = &VIsual_save;
236: else
237: startp = &VIsual;
238: endp = &VIsual_end;
239: if ((c == '<') == lt(*startp, *endp))
240: posp = startp;
241: else
242: posp = endp;
243: }
244: else if (islower(c)) /* normal named mark */
245: posp = &(curbuf->b_namedm[c - 'a']);
246: else if (isupper(c) || isdigit(c)) /* named file mark */
247: {
248: if (isdigit(c))
249: c = c - '0' + NMARKS;
250: else
251: c -= 'A';
252: posp = &(namedfm[c].mark);
253:
254: if (namedfm[c].fnum == 0 && namedfm_names[c] != NULL)
255: {
256: /*
257: * First expand "~/" in the file name to the home directory.
258: * Try to find the shortname by comparing the fullname with the
259: * current directory.
260: */
261: expand_env(namedfm_names[c], NameBuff, MAXPATHL);
262: mch_dirname(IObuff, IOSIZE);
263: len = STRLEN(IObuff);
264: if (fnamencmp(IObuff, NameBuff, len) == 0)
265: {
266: p = NameBuff + len;
267: if (ispathsep(*p))
268: ++p;
269: }
270: else
271: p = NULL;
272: /* buflist_new will call fmarks_check_names() */
273: (void)buflist_new(NameBuff, p, (linenr_t)1, FALSE);
274: }
275:
276: if (namedfm[c].fnum != curbuf->b_fnum) /* mark is in other file */
277: {
278: if (namedfm[c].mark.lnum != 0 && changefile && namedfm[c].fnum)
279: {
280: if (buflist_getfile(namedfm[c].fnum,
1.2 ! downsj 281: namedfm[c].mark.lnum, GETF_SETMARK, FALSE) == OK)
1.1 downsj 282: {
283: curwin->w_cursor.col = namedfm[c].mark.col;
284: return (FPOS *)-1;
285: }
286: }
287: posp = &pos_copy; /* mark exists, but is not valid in
288: current buffer */
289: pos_copy.lnum = 0;
290: }
291: }
292: return posp;
293: }
294:
295: /*
296: * Check all file marks for a name that matches the file name in buf.
297: * May replace the name with an fnum.
298: */
299: void
300: fmarks_check_names(buf)
301: BUF *buf;
302: {
303: char_u *name;
304: int i;
305:
306: if (buf->b_filename == NULL)
307: return;
308:
309: name = home_replace_save(buf, buf->b_filename);
310: if (name == NULL)
311: return;
312:
313: for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
314: {
315: if (namedfm[i].fnum == 0 && namedfm_names[i] != NULL &&
316: fnamecmp(name, namedfm_names[i]) == 0)
317: {
318: namedfm[i].fnum = buf->b_fnum;
319: vim_free(namedfm_names[i]);
320: namedfm_names[i] = NULL;
321: }
322: }
323: vim_free(name);
324: }
325:
326: /*
327: * Check a if a position from a mark is valid.
328: * Give and error message and return FAIL if not.
329: */
330: int
331: check_mark(pos)
332: FPOS *pos;
333: {
334: if (pos == NULL)
335: {
336: emsg(e_umark);
337: return FAIL;
338: }
339: if (pos->lnum == 0)
340: {
341: emsg(e_marknotset);
342: return FAIL;
343: }
344: if (pos->lnum > curbuf->b_ml.ml_line_count)
345: {
346: emsg(e_markinval);
347: return FAIL;
348: }
349: return OK;
350: }
351:
352: /*
353: * clrallmarks() - clear all marks in the buffer 'buf'
354: *
355: * Used mainly when trashing the entire buffer during ":e" type commands
356: */
357: void
358: clrallmarks(buf)
359: BUF *buf;
360: {
361: static int i = -1;
362:
363: if (i == -1) /* first call ever: initialize */
364: for (i = 0; i < NMARKS + 1; i++)
365: {
366: namedfm[i].mark.lnum = 0;
367: namedfm_names[i] = NULL;
368: }
369:
370: for (i = 0; i < NMARKS; i++)
371: buf->b_namedm[i].lnum = 0;
372: buf->b_op_start.lnum = 0; /* start/end op mark cleared */
373: buf->b_op_end.lnum = 0;
374: }
375:
376: /*
377: * get name of file from a filemark
378: * Careful: buflist_nr2name returns NameBuff.
379: */
380: char_u *
381: fm_getname(fmark)
382: struct filemark *fmark;
383: {
384: if (fmark->fnum != curbuf->b_fnum) /* not current file */
385: return buflist_nr2name(fmark->fnum, FALSE, TRUE);
386: return (char_u *)"-current-";
387: }
388:
389: /*
390: * print the marks
391: */
392: void
393: do_marks(arg)
394: char_u *arg;
395: {
396: int i;
397: char_u *name;
398:
399: if (arg != NULL && *arg == NUL)
400: arg = NULL;
401:
402: show_one_mark('\'', arg, &curwin->w_pcmark, NULL);
403: for (i = 0; i < NMARKS; ++i)
404: show_one_mark(i + 'a', arg, &curbuf->b_namedm[i], NULL);
405: for (i = 0; i < NMARKS + EXTRA_MARKS; ++i)
406: {
407: name = namedfm[i].fnum == 0 ? namedfm_names[i] :
408: fm_getname(&namedfm[i]);
409: if (name != NULL)
410: show_one_mark(i >= NMARKS ? i - NMARKS + '0' : i + 'A',
411: arg, &namedfm[i].mark, name);
412: }
413: show_one_mark('"', arg, &curbuf->b_last_cursor, NULL);
414: show_one_mark('[', arg, &curbuf->b_op_start, NULL);
415: show_one_mark(']', arg, &curbuf->b_op_end, NULL);
416: show_one_mark('<', arg, &VIsual, NULL);
417: show_one_mark('>', arg, &VIsual_end, NULL);
418: show_one_mark(-1, arg, NULL, NULL);
419: }
420:
421: static void
422: show_one_mark(c, arg, p, name)
423: int c;
424: char_u *arg;
425: FPOS *p;
426: char_u *name;
427: {
428: static int did_title = FALSE;
429:
430: if (c == -1) /* finish up */
431: {
432: if (did_title)
433: did_title = FALSE;
434: else
435: {
436: if (arg == NULL)
437: MSG("No marks set");
438: else
439: EMSG2("No marks matching \"%s\"", arg);
440: }
441: }
442: /* don't output anything if 'q' typed at --more-- prompt */
443: else if (!got_int && (arg == NULL || vim_strchr(arg, c) != NULL) &&
444: p->lnum != 0)
445: {
446: if (!did_title)
447: {
448: set_highlight('t'); /* Highlight title */
449: start_highlight();
450: MSG_OUTSTR("\nmark line col file");
451: stop_highlight();
452: did_title = TRUE;
453: }
454: msg_outchar('\n');
455: if (!got_int)
456: {
457: sprintf((char *)IObuff, " %c %5ld %3d ", c, p->lnum, p->col);
458: if (name != NULL)
459: STRCAT(IObuff, name);
460: msg_outtrans(IObuff);
461: }
462: flushbuf(); /* show one line at a time */
463: }
464: }
465:
466: /*
467: * print the jumplist
468: */
469: void
470: do_jumps()
471: {
472: int i;
473: char_u *name;
474:
475: cleanup_jumplist();
476: set_highlight('t'); /* Highlight title */
477: start_highlight();
478: MSG_OUTSTR("\n jump line file");
479: stop_highlight();
480: for (i = 0; i < curwin->w_jumplistlen; ++i)
481: {
482: if (curwin->w_jumplist[i].mark.lnum != 0)
483: {
484: name = fm_getname(&curwin->w_jumplist[i]);
485: if (name == NULL) /* file name not available */
486: continue;
487:
488: msg_outchar('\n');
489: sprintf((char *)IObuff, "%c %2d %5ld %s",
490: i == curwin->w_jumplistidx ? '>' : ' ',
491: i + 1,
492: curwin->w_jumplist[i].mark.lnum,
493: name);
494: msg_outtrans(IObuff);
495: }
496: flushbuf();
497: }
498: if (curwin->w_jumplistidx == curwin->w_jumplistlen)
499: MSG_OUTSTR("\n>");
500: }
501:
502: /*
503: * adjust marks between line1 and line2 (inclusive) to move 'amount' lines
504: * If 'amount' is MAXLNUM the mark is made invalid.
505: * If 'amount_after' is non-zero adjust marks after line 2.
506: */
507:
508: #define one_adjust(add) \
509: { \
510: lp = add; \
511: if (*lp >= line1 && *lp <= line2) \
512: { \
513: if (amount == MAXLNUM) \
514: *lp = 0; \
515: else \
516: *lp += amount; \
517: } \
518: else if (amount_after && *lp > line2) \
519: *lp += amount_after; \
520: }
521:
522: /* don't delete the line, just put at first deleted line */
523: #define one_adjust_nodel(add) \
524: { \
525: lp = add; \
526: if (*lp >= line1 && *lp <= line2) \
527: { \
528: if (amount == MAXLNUM) \
529: *lp = line1; \
530: else \
531: *lp += amount; \
532: } \
533: else if (amount_after && *lp > line2) \
534: *lp += amount_after; \
535: }
536:
537: void
538: mark_adjust(line1, line2, amount, amount_after)
539: linenr_t line1;
540: linenr_t line2;
541: long amount;
542: long amount_after;
543: {
544: int i;
545: int fnum = curbuf->b_fnum;
546: linenr_t *lp;
547: WIN *win;
548:
549: if (line2 < line1 && amount_after == 0L) /* nothing to do */
550: return;
551:
552: /* named marks, lower case and upper case */
553: for (i = 0; i < NMARKS; i++)
554: {
555: one_adjust(&(curbuf->b_namedm[i].lnum));
556: if (namedfm[i].fnum == fnum)
557: one_adjust(&(namedfm[i].mark.lnum));
558: }
559: for (i = NMARKS; i < NMARKS + EXTRA_MARKS; i++)
560: {
561: if (namedfm[i].fnum == fnum)
562: one_adjust(&(namedfm[i].mark.lnum));
563: }
564:
565: /* previous context mark */
566: one_adjust(&(curwin->w_pcmark.lnum));
567:
568: /* previous pcmark */
569: one_adjust(&(curwin->w_prev_pcmark.lnum));
570:
571: /* Visual area */
572: one_adjust_nodel(&(VIsual.lnum));
573: one_adjust_nodel(&(VIsual_end.lnum));
574:
575: /* marks in the tag stack */
576: for (i = 0; i < curwin->w_tagstacklen; i++)
577: if (curwin->w_tagstack[i].fmark.fnum == fnum)
578: one_adjust_nodel(&(curwin->w_tagstack[i].fmark.mark.lnum));
579:
580: /* quickfix marks */
581: qf_mark_adjust(line1, line2, amount, amount_after);
582:
583: /* jumplist marks */
584: for (win = firstwin; win != NULL; win = win->w_next)
585: {
586: /*
587: * When deleting lines, this may create duplicate marks in the
588: * jumplist. They will be removed later.
589: */
590: for (i = 0; i < win->w_jumplistlen; ++i)
591: if (win->w_jumplist[i].fnum == fnum)
592: one_adjust_nodel(&(win->w_jumplist[i].mark.lnum));
593: /*
594: * also adjust the line at the top of the window and the cursor
595: * position for windows with the same buffer.
596: */
597: if (win != curwin && win->w_buffer == curbuf)
598: {
599: if (win->w_topline >= line1 && win->w_topline <= line2)
600: {
601: if (amount == MAXLNUM) /* topline is deleted */
602: {
603: if (line1 <= 1)
604: win->w_topline = 1;
605: else
606: win->w_topline = line1 - 1;
607: }
608: else /* keep topline on the same line */
609: win->w_topline += amount;
610: }
611: else if (amount_after && win->w_topline > line2)
612: win->w_topline += amount_after;
613: if (win->w_cursor.lnum >= line1 && win->w_cursor.lnum <= line2)
614: {
615: if (amount == MAXLNUM) /* line with cursor is deleted */
616: {
617: if (line1 <= 1)
618: win->w_cursor.lnum = 1;
619: else
620: win->w_cursor.lnum = line1 - 1;
621: win->w_cursor.col = 0;
622: }
623: else /* keep cursor on the same line */
624: win->w_cursor.lnum += amount;
625: }
626: else if (amount_after && win->w_cursor.lnum > line2)
627: win->w_cursor.lnum += amount_after;
628: }
629: }
630: }
631:
632: /*
633: * When deleting lines, this may create duplicate marks in the
634: * jumplist. They will be removed here for the current window.
635: */
636: static void
637: cleanup_jumplist()
638: {
639: int i;
640: int from, to;
641:
642: to = 0;
643: for (from = 0; from < curwin->w_jumplistlen; ++from)
644: {
645: if (curwin->w_jumplistidx == from)
646: curwin->w_jumplistidx = to;
647: for (i = from + 1; i < curwin->w_jumplistlen; ++i)
648: if (curwin->w_jumplist[i].fnum == curwin->w_jumplist[from].fnum &&
649: curwin->w_jumplist[i].mark.lnum ==
650: curwin->w_jumplist[from].mark.lnum)
651: break;
652: if (i >= curwin->w_jumplistlen) /* no duplicate */
653: curwin->w_jumplist[to++] = curwin->w_jumplist[from];
654: }
655: if (curwin->w_jumplistidx == curwin->w_jumplistlen)
656: curwin->w_jumplistidx = to;
657: curwin->w_jumplistlen = to;
658: }
659:
660: void
661: set_last_cursor(win)
662: WIN *win;
663: {
664: win->w_buffer->b_last_cursor = win->w_cursor;
665: }
666:
667: #ifdef VIMINFO
668: int
669: read_viminfo_filemark(line, fp, force)
670: char_u *line;
671: FILE *fp;
672: int force;
673: {
674: int idx;
675: char_u *str;
676:
677: /* We only get here (hopefully) if line[0] == '\'' */
678: str = line + 1;
679: if (*str > 127 || (!isdigit(*str) && !isupper(*str)))
1.2 ! downsj 680: {
! 681: if (viminfo_error("Illegal file mark name", line))
! 682: return TRUE; /* Too many errors, pretend end-of-file */
! 683: }
1.1 downsj 684: else
685: {
686: if (isdigit(*str))
687: idx = *str - '0' + NMARKS;
688: else
689: idx = *str - 'A';
690: if (namedfm[idx].mark.lnum == 0 || force)
691: {
692: str = skipwhite(str + 1);
693: namedfm[idx].mark.lnum = getdigits(&str);
694: str = skipwhite(str);
695: namedfm[idx].mark.col = getdigits(&str);
696: str = skipwhite(str);
697: viminfo_readstring(line);
698: namedfm_names[idx] = strsave(str);
699: }
700: }
701: return vim_fgets(line, LSIZE, fp);
702: }
703:
704: void
705: write_viminfo_filemarks(fp)
706: FILE *fp;
707: {
708: int i;
709: char_u *name;
710:
711: if (get_viminfo_parameter('\'') == 0)
712: return;
713:
714: fprintf(fp, "\n# File marks:\n");
715:
716: /*
717: * Find a mark that is the same file and position as the cursor.
718: * That one, or else the last one is deleted.
719: * Move '0 to '1, '1 to '2, etc. until the matching one or '9
720: * Set '0 mark to current cursor position.
721: */
722: if (curbuf->b_filename != NULL && !removable(curbuf->b_filename))
723: {
724: name = buflist_nr2name(curbuf->b_fnum, TRUE, FALSE);
725: for (i = NMARKS; i < NMARKS + EXTRA_MARKS - 1; ++i)
726: if (namedfm[i].mark.lnum == curwin->w_cursor.lnum &&
727: (namedfm_names[i] == NULL ?
728: namedfm[i].fnum == curbuf->b_fnum :
729: STRCMP(name, namedfm_names[i]) == 0))
730: break;
731:
732: vim_free(namedfm_names[i]);
733: for ( ; i > NMARKS; --i)
734: {
735: namedfm[i] = namedfm[i - 1];
736: namedfm_names[i] = namedfm_names[i - 1];
737: }
738: namedfm[NMARKS].mark = curwin->w_cursor;
739: namedfm[NMARKS].fnum = curbuf->b_fnum;
740: namedfm_names[NMARKS] = NULL;
741: }
742:
743: for (i = 0; i < NMARKS + EXTRA_MARKS; i++)
744: {
745: if (namedfm[i].mark.lnum == 0) /* not set */
746: continue;
747:
748: if (namedfm[i].fnum) /* there is a buffer */
749: name = buflist_nr2name(namedfm[i].fnum, TRUE, FALSE);
750: else
751: name = namedfm_names[i]; /* use name from .viminfo */
752: if (name == NULL)
753: continue;
754:
755: fprintf(fp, "'%c %ld %ld %s\n",
756: i < NMARKS ? i + 'A' : i - NMARKS + '0',
757: (long)namedfm[i].mark.lnum,
758: (long)namedfm[i].mark.col,
759: name);
760: }
761: }
762:
763: /*
764: * Return TRUE if "name" is on removable media (depending on 'viminfo').
765: */
766: static int
767: removable(name)
768: char_u *name;
769: {
770: char_u *p;
771: char_u part[51];
772: int retval = FALSE;
773:
774: name = home_replace_save(NULL, name);
775: if (name != NULL)
776: {
777: for (p = p_viminfo; *p; )
778: {
779: copy_option_part(&p, part, 51, ", ");
780: if (part[0] == 'r' && vim_strnicmp(part + 1, name,
781: (size_t)STRLEN(part + 1)) == 0)
782: {
783: retval = TRUE;
784: break;
785: }
786: }
787: vim_free(name);
788: }
789: return retval;
790: }
791:
792: /*
793: * Write all the named marks for all buffers.
794: * Return the number of buffers for which marks have been written.
795: */
796: int
797: write_viminfo_marks(fp_out)
798: FILE *fp_out;
799: {
800: int count;
801: BUF *buf;
802: WIN *win;
803: int is_mark_set;
804: int i;
805:
806: /*
807: * Set b_last_cursor for the all buffers that have a window.
808: */
809: for (win = firstwin; win != NULL; win = win->w_next)
810: set_last_cursor(win);
811:
812: fprintf(fp_out, "\n# History of marks within files (newest to oldest):\n");
813: count = 0;
814: for (buf = firstbuf; buf != NULL; buf = buf->b_next)
815: {
816: /*
817: * Only write something if buffer has been loaded and at least one
818: * mark is set.
819: */
820: if (buf->b_marks_read)
821: {
822: if (buf->b_last_cursor.lnum != 0)
823: is_mark_set = TRUE;
824: else
825: {
826: is_mark_set = FALSE;
827: for (i = 0; i < NMARKS; i++)
828: if (buf->b_namedm[i].lnum != 0)
829: {
830: is_mark_set = TRUE;
831: break;
832: }
833: }
834: if (is_mark_set && buf->b_filename != NULL &&
835: buf->b_filename[0] != NUL && !removable(buf->b_filename))
836: {
837: home_replace(NULL, buf->b_filename, IObuff, IOSIZE);
838: fprintf(fp_out, "\n> %s\n", (char *)IObuff);
839: if (buf->b_last_cursor.lnum != 0)
840: fprintf(fp_out, "\t\"\t%ld\t%d\n",
841: buf->b_last_cursor.lnum, buf->b_last_cursor.col);
842: for (i = 0; i < NMARKS; i++)
843: if (buf->b_namedm[i].lnum != 0)
844: fprintf(fp_out, "\t%c\t%ld\t%d\n", 'a' + i,
845: buf->b_namedm[i].lnum, buf->b_namedm[i].col);
846: count++;
847: }
848: }
849: }
850:
851: return count;
852: }
853:
854: /*
855: * Handle marks in the viminfo file:
856: * fp_out == NULL read marks for current buffer only
857: * fp_out != NULL copy marks for buffers not in buffer list
858: */
859: void
860: copy_viminfo_marks(line, fp_in, fp_out, count, eof)
861: char_u *line;
862: FILE *fp_in;
863: FILE *fp_out;
864: int count;
865: int eof;
866: {
867: BUF *buf;
868: int num_marked_files;
869: char_u save_char;
870: int load_marks;
871: int copy_marks_out;
872: char_u *str;
873: int i;
874: char_u *p;
875:
876: num_marked_files = get_viminfo_parameter('\'');
877: while (!eof && (count < num_marked_files || fp_out == NULL))
878: {
879: if (line[0] != '>')
880: {
881: if (line[0] != '\n' && line[0] != '\r' && line[0] != '#')
1.2 ! downsj 882: {
! 883: if (viminfo_error("Missing '>'", line))
! 884: return; /* too many errors, return now */
! 885: }
1.1 downsj 886: eof = vim_fgets(line, LSIZE, fp_in);
887: continue; /* Skip this dud line */
888: }
889:
890: /*
891: * Find filename, set str to start.
892: * Ignore leading and trailing white space.
893: */
894: str = skipwhite(line + 1);
895: p = str + STRLEN(str);
896: while (p != str && (*p == NUL || vim_isspace(*p)))
897: p--;
898: if (*p)
899: p++;
900: save_char = *p;
901: *p = NUL;
902:
903: /*
904: * If fp_out == NULL, load marks for current buffer.
905: * If fp_out != NULL, copy marks for buffers not in buflist.
906: */
907: load_marks = copy_marks_out = FALSE;
908: if (fp_out == NULL)
909: {
910: if (curbuf->b_filename != NULL &&
911: fullpathcmp(str, curbuf->b_filename) == FPC_SAME)
912: load_marks = TRUE;
913: }
914: else /* fp_out != NULL */
915: {
916: /* This is slow if there are many buffers!! */
917: for (buf = firstbuf; buf != NULL; buf = buf->b_next)
918: if (buf->b_filename != NULL &&
919: fullpathcmp(str, buf->b_filename) == FPC_SAME)
920: break;
921:
922: /*
923: * copy marks if the buffer has not been loaded
924: */
925: if (buf == NULL || !buf->b_marks_read)
926: {
927: copy_marks_out = TRUE;
928: *p = save_char;
929: fputs("\n", fp_out);
930: fputs((char *)line, fp_out);
931: count++;
932: }
933: }
934: while (!(eof = vim_fgets(line, LSIZE, fp_in)) && line[0] == TAB)
935: {
936: if (load_marks)
937: {
938: if (line[1] == '"')
939: sscanf((char *)line + 2, "%ld %d",
940: &curbuf->b_last_cursor.lnum,
941: &curbuf->b_last_cursor.col);
942: else if ((i = line[1] - 'a') >= 0 && i < NMARKS)
943: sscanf((char *)line + 2, "%ld %d",
944: &curbuf->b_namedm[i].lnum,
945: &curbuf->b_namedm[i].col);
946: }
947: else if (copy_marks_out)
948: fputs((char *)line, fp_out);
949: }
950: if (load_marks)
951: return;
952: }
953: }
954: #endif /* VIMINFO */