distrib: run libtoolize
[nvi.git] / common / mark.c
blobab063c5d127328ce2d2ed993f165a4ae47128852
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: mark.c,v 10.15 2001/06/25 15:19:11 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:11 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "common.h"
28 static LMARK *mark_find __P((SCR *, ARG_CHAR_T));
31 * Marks are maintained in a key sorted doubly linked list. We can't
32 * use arrays because we have no idea how big an index key could be.
33 * The underlying assumption is that users don't have more than, say,
34 * 10 marks at any one time, so this will be is fast enough.
36 * Marks are fixed, and modifications to the line don't update the mark's
37 * position in the line. This can be hard. If you add text to the line,
38 * place a mark in that text, undo the addition and use ` to move to the
39 * mark, the location will have disappeared. It's tempting to try to adjust
40 * the mark with the changes in the line, but this is hard to do, especially
41 * if we've given the line to v_ntext.c:v_ntext() for editing. Historic vi
42 * would move to the first non-blank on the line when the mark location was
43 * past the end of the line. This can be complicated by deleting to a mark
44 * that has disappeared using the ` command. Historic vi treated this as
45 * a line-mode motion and deleted the line. This implementation complains to
46 * the user.
48 * In historic vi, marks returned if the operation was undone, unless the
49 * mark had been subsequently reset. Tricky. This is hard to start with,
50 * but in the presence of repeated undo it gets nasty. When a line is
51 * deleted, we delete (and log) any marks on that line. An undo will create
52 * the mark. Any mark creations are noted as to whether the user created
53 * it or if it was created by an undo. The former cannot be reset by another
54 * undo, but the latter may.
56 * All of these routines translate ABSMARK2 to ABSMARK1. Setting either of
57 * the absolute mark locations sets both, so that "m'" and "m`" work like
58 * they, ah, for lack of a better word, "should".
62 * mark_init --
63 * Set up the marks.
65 * PUBLIC: int mark_init __P((SCR *, EXF *));
67 int
68 mark_init(SCR *sp, EXF *ep)
71 * !!!
72 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
74 * Set up the marks.
76 LIST_INIT(&ep->marks);
77 return (0);
81 * mark_end --
82 * Free up the marks.
84 * PUBLIC: int mark_end __P((SCR *, EXF *));
86 int
87 mark_end(SCR *sp, EXF *ep)
89 LMARK *lmp;
92 * !!!
93 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
95 while ((lmp = ep->marks.lh_first) != NULL) {
96 LIST_REMOVE(lmp, q);
97 free(lmp);
99 return (0);
103 * mark_get --
104 * Get the location referenced by a mark.
106 * PUBLIC: int mark_get __P((SCR *, ARG_CHAR_T, MARK *, mtype_t));
109 mark_get(SCR *sp, ARG_CHAR_T key, MARK *mp, mtype_t mtype)
111 LMARK *lmp;
113 if (key == ABSMARK2)
114 key = ABSMARK1;
116 lmp = mark_find(sp, key);
117 if (lmp == NULL || lmp->name != key) {
118 msgq(sp, mtype, "017|Mark %s: not set", KEY_NAME(sp, key));
119 return (1);
121 if (F_ISSET(lmp, MARK_DELETED)) {
122 msgq(sp, mtype,
123 "018|Mark %s: the line was deleted", KEY_NAME(sp, key));
124 return (1);
128 * !!!
129 * The absolute mark is initialized to lno 1/cno 0, and historically
130 * you could use it in an empty file. Make such a mark always work.
132 if ((lmp->lno != 1 || lmp->cno != 0) && !db_exist(sp, lmp->lno)) {
133 msgq(sp, mtype,
134 "019|Mark %s: cursor position no longer exists",
135 KEY_NAME(sp, key));
136 return (1);
138 mp->lno = lmp->lno;
139 mp->cno = lmp->cno;
140 return (0);
144 * mark_set --
145 * Set the location referenced by a mark.
147 * PUBLIC: int mark_set __P((SCR *, ARG_CHAR_T, MARK *, int));
150 mark_set(SCR *sp, ARG_CHAR_T key, MARK *value, int userset)
152 LMARK *lmp, *lmt;
154 if (key == ABSMARK2)
155 key = ABSMARK1;
158 * The rules are simple. If the user is setting a mark (if it's a
159 * new mark this is always true), it always happens. If not, it's
160 * an undo, and we set it if it's not already set or if it was set
161 * by a previous undo.
163 lmp = mark_find(sp, key);
164 if (lmp == NULL || lmp->name != key) {
165 MALLOC_RET(sp, lmt, LMARK *, sizeof(LMARK));
166 if (lmp == NULL) {
167 LIST_INSERT_HEAD(&sp->ep->marks, lmt, q);
168 } else
169 LIST_INSERT_AFTER(lmp, lmt, q);
170 lmp = lmt;
171 } else if (!userset &&
172 !F_ISSET(lmp, MARK_DELETED) && F_ISSET(lmp, MARK_USERSET))
173 return (0);
175 lmp->lno = value->lno;
176 lmp->cno = value->cno;
177 lmp->name = key;
178 lmp->flags = userset ? MARK_USERSET : 0;
179 return (0);
183 * mark_find --
184 * Find the requested mark, or, the slot immediately before
185 * where it would go.
187 static LMARK *
188 mark_find(SCR *sp, ARG_CHAR_T key)
190 LMARK *lmp, *lastlmp;
193 * Return the requested mark or the slot immediately before
194 * where it should go.
196 for (lastlmp = NULL, lmp = sp->ep->marks.lh_first;
197 lmp != NULL; lastlmp = lmp, lmp = lmp->q.le_next)
198 if (lmp->name >= key)
199 return (lmp->name == key ? lmp : lastlmp);
200 return (lastlmp);
204 * mark_insdel --
205 * Update the marks based on an insertion or deletion.
207 * PUBLIC: int mark_insdel __P((SCR *, lnop_t, db_recno_t));
210 mark_insdel(SCR *sp, lnop_t op, db_recno_t lno)
212 LMARK *lmp;
213 db_recno_t lline;
215 switch (op) {
216 case LINE_APPEND:
217 /* All insert/append operations are done as inserts. */
218 abort();
219 case LINE_DELETE:
220 for (lmp = sp->ep->marks.lh_first;
221 lmp != NULL; lmp = lmp->q.le_next)
222 if (lmp->lno >= lno)
223 if (lmp->lno == lno) {
224 F_SET(lmp, MARK_DELETED);
225 (void)log_mark(sp, lmp);
226 } else
227 --lmp->lno;
228 break;
229 case LINE_INSERT:
231 * XXX
232 * Very nasty special case. If the file was empty, then we're
233 * adding the first line, which is a replacement. So, we don't
234 * modify the marks. This is a hack to make:
236 * mz:r!echo foo<carriage-return>'z
238 * work, i.e. historically you could mark the "line" in an empty
239 * file and replace it, and continue to use the mark. Insane,
240 * well, yes, I know, but someone complained.
242 * Check for line #2 before going to the end of the file.
244 if (!db_exist(sp, 2)) {
245 if (db_last(sp, &lline))
246 return (1);
247 if (lline == 1)
248 return (0);
251 for (lmp = sp->ep->marks.lh_first;
252 lmp != NULL; lmp = lmp->q.le_next)
253 if (lmp->lno >= lno)
254 ++lmp->lno;
255 break;
256 case LINE_RESET:
257 break;
259 return (0);