lint
[nvi.git] / common / mark.c
blob293dba487136bb83132c225f9374ea9e480bbeda
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: mark.c,v 8.11 1993/12/09 19:42:11 bostic Exp $ (Berkeley) $Date: 1993/12/09 19:42:11 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "vi.h"
20 static MARK *mark_find __P((SCR *, EXF *, ARG_CHAR_T));
23 * Marks are maintained in a key sorted doubly linked list. We can't
24 * use arrays because we have no idea how big an index key could be.
25 * The underlying assumption is that users don't have more than, say,
26 * 10 marks at any one time, so this will be is fast enough.
28 * Marks are fixed, and modifications to the line don't update the mark's
29 * position in the line. This can be hard. If you add text to the line,
30 * place a mark in that text, undo the addition and use ` to move to the
31 * mark, the location will have disappeared. It's tempting to try to adjust
32 * the mark with the changes in the line, but this is hard to do, especially
33 * if we've given the line to v_ntext.c:v_ntext() for editing. Historic vi
34 * would move to the first non-blank on the line when the mark location was
35 * past the end of the line. This can be complicated by deleting to a mark
36 * that has disappeared using the ` command. Historic vi vi treated this as
37 * a line-mode motion and deleted the line. This implementation complains to
38 * the user.
40 * In historic vi, marks returned if the operation was undone, unless the
41 * mark had been subsequently reset. Tricky. This is hard to start with,
42 * but in the presence of repeated undo it gets nasty. When a line is
43 * deleted, we delete (and log) any marks on that line. An undo will create
44 * the mark. Any mark creations are noted as to whether the user created
45 * it or if it was created by an undo. The former cannot be reset by another
46 * undo, but the latter may.
48 * All of these routines translate ABSMARK2 to ABSMARK1. Setting either of
49 * the absolute mark locations sets both, so that "m'" and "m`" work like
50 * they, ah, for lack of a better word, "should".
54 * mark_init --
55 * Set up the marks.
57 int
58 mark_init(sp, ep)
59 SCR *sp;
60 EXF *ep;
62 MARK *mp;
65 * Make sure the marks have been set up. If they
66 * haven't, do so, and create the absolute mark.
68 MALLOC_RET(sp, mp, MARK *, sizeof(MARK));
69 mp->lno = 1;
70 mp->cno = 0;
71 mp->name = ABSMARK1;
72 mp->flags = 0;
73 LIST_INSERT_HEAD(&ep->marks, mp, q);
74 return (0);
78 * mark_end --
79 * Free up the marks.
81 int
82 mark_end(sp, ep)
83 SCR *sp;
84 EXF *ep;
86 MARK *mp;
88 while ((mp = ep->marks.lh_first) != NULL) {
89 LIST_REMOVE(mp, q);
90 FREE(mp, sizeof(MARK));
92 return (0);
96 * mark_get --
97 * Get the location referenced by a mark.
99 MARK *
100 mark_get(sp, ep, key)
101 SCR *sp;
102 EXF *ep;
103 ARG_CHAR_T key;
105 MARK *mp;
106 size_t len;
107 char *p;
109 if (key == ABSMARK2)
110 key = ABSMARK1;
112 mp = mark_find(sp, ep, key);
113 if (mp == NULL || mp->name != key) {
114 msgq(sp, M_BERR, "Mark %s: not set.", charname(sp, key));
115 return (NULL);
117 if (F_ISSET(mp, MARK_DELETED)) {
118 msgq(sp, M_BERR,
119 "Mark %s: the line was deleted.", charname(sp, key));
120 return (NULL);
122 if ((p = file_gline(sp, ep, mp->lno, &len)) == NULL ||
123 mp->cno > len || mp->cno == len && len != 0) {
124 msgq(sp, M_BERR, "Mark %s: cursor position no longer exists.",
125 charname(sp, key));
126 return (NULL);
128 return (mp);
132 * mark_set --
133 * Set the location referenced by a mark.
136 mark_set(sp, ep, key, value, userset)
137 SCR *sp;
138 EXF *ep;
139 ARG_CHAR_T key;
140 MARK *value;
141 int userset;
143 MARK *mp, *mt;
145 if (key == ABSMARK2)
146 key = ABSMARK1;
149 * The rules are simple. If the user is setting a mark (if it's a
150 * new mark this is always true), it always happens. If not, it's
151 * an undo, and we set it if it's not already set or if it was set
152 * by a previous undo.
154 mp = mark_find(sp, ep, key);
155 if (mp == NULL || mp->name != key) {
156 MALLOC_RET(sp, mt, MARK *, sizeof(MARK));
157 if (mp == NULL) {
158 LIST_INSERT_HEAD(&ep->marks, mt, q);
159 } else
160 LIST_INSERT_AFTER(mp, mt, q);
161 mp = mt;
162 } else if (!userset &&
163 !F_ISSET(mp, MARK_DELETED) && F_ISSET(mp, MARK_USERSET))
164 return (0);
166 mp->lno = value->lno;
167 mp->cno = value->cno;
168 mp->name = key;
169 mp->flags = userset ? MARK_USERSET : 0;
170 return (0);
174 * mark_find --
175 * Find the requested mark, or, the slot immediately before
176 * where it would go.
178 static MARK *
179 mark_find(sp, ep, key)
180 SCR *sp;
181 EXF *ep;
182 ARG_CHAR_T key;
184 MARK *mp, *lastmp;
187 * Return the requested mark or the slot immediately before
188 * where it should go.
190 for (lastmp = NULL, mp = ep->marks.lh_first;
191 mp != NULL; lastmp = mp, mp = mp->q.le_next)
192 if (mp->name >= key)
193 return (mp->name == key ? mp : lastmp);
194 return (lastmp);
198 * mark_delete --
199 * Update the marks based on a deletion.
201 void
202 mark_delete(sp, ep, lno)
203 SCR *sp;
204 EXF *ep;
205 recno_t lno;
207 MARK *mp;
209 for (mp = ep->marks.lh_first; mp != NULL; mp = mp->q.le_next)
210 if (mp->lno >= lno)
211 if (mp->lno == lno) {
212 F_SET(mp, MARK_DELETED);
213 (void)log_mark(sp, ep, mp);
214 } else
215 --mp->lno;
219 * mark_insert --
220 * Update the marks based on an insertion.
222 void
223 mark_insert(sp, ep, lno)
224 SCR *sp;
225 EXF *ep;
226 recno_t lno;
228 MARK *mp;
230 for (mp = ep->marks.lh_first; mp != NULL; mp = mp->q.le_next)
231 if (mp->lno >= lno)
232 ++mp->lno;