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.
13 static const char sccsid[] = "@(#)mark.c 10.13 (Berkeley) 7/19/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.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
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".
65 * PUBLIC: int mark_init __P((SCR *, EXF *));
74 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
78 LIST_INIT(&ep->marks);
86 * PUBLIC: int mark_end __P((SCR *, EXF *));
97 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
99 while ((lmp = ep->marks.lh_first) != NULL) {
108 * Get the location referenced by a mark.
110 * PUBLIC: int mark_get __P((SCR *, ARG_CHAR_T, MARK *, mtype_t));
113 mark_get(sp, key, mp, mtype)
124 lmp = mark_find(sp, key);
125 if (lmp == NULL || lmp->name != key) {
126 msgq(sp, mtype, "017|Mark %s: not set", KEY_NAME(sp, key));
129 if (F_ISSET(lmp, MARK_DELETED)) {
131 "018|Mark %s: the line was deleted", KEY_NAME(sp, key));
137 * The absolute mark is initialized to lno 1/cno 0, and historically
138 * you could use it in an empty file. Make such a mark always work.
140 if ((lmp->lno != 1 || lmp->cno != 0) && !db_exist(sp, lmp->lno)) {
142 "019|Mark %s: cursor position no longer exists",
153 * Set the location referenced by a mark.
155 * PUBLIC: int mark_set __P((SCR *, ARG_CHAR_T, MARK *, int));
158 mark_set(sp, key, value, userset)
170 * The rules are simple. If the user is setting a mark (if it's a
171 * new mark this is always true), it always happens. If not, it's
172 * an undo, and we set it if it's not already set or if it was set
173 * by a previous undo.
175 lmp = mark_find(sp, key);
176 if (lmp == NULL || lmp->name != key) {
177 MALLOC_RET(sp, lmt, LMARK *, sizeof(LMARK));
179 LIST_INSERT_HEAD(&sp->ep->marks, lmt, q);
181 LIST_INSERT_AFTER(lmp, lmt, q);
183 } else if (!userset &&
184 !F_ISSET(lmp, MARK_DELETED) && F_ISSET(lmp, MARK_USERSET))
187 lmp->lno = value->lno;
188 lmp->cno = value->cno;
190 lmp->flags = userset ? MARK_USERSET : 0;
196 * Find the requested mark, or, the slot immediately before
204 LMARK *lmp, *lastlmp;
207 * Return the requested mark or the slot immediately before
208 * where it should go.
210 for (lastlmp = NULL, lmp = sp->ep->marks.lh_first;
211 lmp != NULL; lastlmp = lmp, lmp = lmp->q.le_next)
212 if (lmp->name >= key)
213 return (lmp->name == key ? lmp : lastlmp);
219 * Update the marks based on an insertion or deletion.
221 * PUBLIC: int mark_insdel __P((SCR *, lnop_t, recno_t));
224 mark_insdel(sp, op, lno)
234 /* All insert/append operations are done as inserts. */
237 for (lmp = sp->ep->marks.lh_first;
238 lmp != NULL; lmp = lmp->q.le_next)
240 if (lmp->lno == lno) {
241 F_SET(lmp, MARK_DELETED);
242 (void)log_mark(sp, lmp);
249 * Very nasty special case. If the file was empty, then we're
250 * adding the first line, which is a replacement. So, we don't
251 * modify the marks. This is a hack to make:
253 * mz:r!echo foo<carriage-return>'z
255 * work, i.e. historically you could mark the "line" in an empty
256 * file and replace it, and continue to use the mark. Insane,
257 * well, yes, I know, but someone complained.
259 * Check for line #2 before going to the end of the file.
261 if (!db_exist(sp, 2)) {
262 if (db_last(sp, &lline))
268 for (lmp = sp->ep->marks.lh_first;
269 lmp != NULL; lmp = lmp->q.le_next)