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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
25 static LMARK
*mark_find(SCR
*, ARG_CHAR_T
);
28 * Marks are maintained in a key sorted singly linked list. We can't
29 * use arrays because we have no idea how big an index key could be.
30 * The underlying assumption is that users don't have more than, say,
31 * 10 marks at any one time, so this will be is fast enough.
33 * Marks are fixed, and modifications to the line don't update the mark's
34 * position in the line. This can be hard. If you add text to the line,
35 * place a mark in that text, undo the addition and use ` to move to the
36 * mark, the location will have disappeared. It's tempting to try to adjust
37 * the mark with the changes in the line, but this is hard to do, especially
38 * if we've given the line to v_ntext.c:v_ntext() for editing. Historic vi
39 * would move to the first non-blank on the line when the mark location was
40 * past the end of the line. This can be complicated by deleting to a mark
41 * that has disappeared using the ` command. Historic vi treated this as
42 * a line-mode motion and deleted the line. This implementation complains to
45 * In historic vi, marks returned if the operation was undone, unless the
46 * mark had been subsequently reset. Tricky. This is hard to start with,
47 * but in the presence of repeated undo it gets nasty. When a line is
48 * deleted, we delete (and log) any marks on that line. An undo will create
49 * the mark. Any mark creations are noted as to whether the user created
50 * it or if it was created by an undo. The former cannot be reset by another
51 * undo, but the latter may.
53 * All of these routines translate ABSMARK2 to ABSMARK1. Setting either of
54 * the absolute mark locations sets both, so that "m'" and "m`" work like
55 * they, ah, for lack of a better word, "should".
62 * PUBLIC: int mark_init(SCR *, EXF *);
65 mark_init(SCR
*sp
, EXF
*ep
)
69 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
73 SLIST_INIT(ep
->marks
);
81 * PUBLIC: int mark_end(SCR *, EXF *);
84 mark_end(SCR
*sp
, EXF
*ep
)
90 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
92 while ((lmp
= SLIST_FIRST(ep
->marks
)) != NULL
) {
93 SLIST_REMOVE_HEAD(ep
->marks
, q
);
101 * Get the location referenced by a mark.
103 * PUBLIC: int mark_get(SCR *, ARG_CHAR_T, MARK *, mtype_t);
106 mark_get(SCR
*sp
, ARG_CHAR_T key
, MARK
*mp
, mtype_t mtype
)
113 lmp
= mark_find(sp
, key
);
114 if (lmp
== NULL
|| lmp
->name
!= key
) {
115 msgq(sp
, mtype
, "017|Mark %s: not set", KEY_NAME(sp
, key
));
118 if (F_ISSET(lmp
, MARK_DELETED
)) {
120 "018|Mark %s: the line was deleted", KEY_NAME(sp
, key
));
126 * The absolute mark is initialized to lno 1/cno 0, and historically
127 * you could use it in an empty file. Make such a mark always work.
129 if ((lmp
->lno
!= 1 || lmp
->cno
!= 0) && !db_exist(sp
, lmp
->lno
)) {
131 "019|Mark %s: cursor position no longer exists",
142 * Set the location referenced by a mark.
144 * PUBLIC: int mark_set(SCR *, ARG_CHAR_T, MARK *, int);
147 mark_set(SCR
*sp
, ARG_CHAR_T key
, MARK
*value
, int userset
)
155 * The rules are simple. If the user is setting a mark (if it's a
156 * new mark this is always true), it always happens. If not, it's
157 * an undo, and we set it if it's not already set or if it was set
158 * by a previous undo.
160 lmp
= mark_find(sp
, key
);
161 if (lmp
== NULL
|| lmp
->name
!= key
) {
162 MALLOC_RET(sp
, lmt
, sizeof(LMARK
));
164 SLIST_INSERT_HEAD(sp
->ep
->marks
, lmt
, q
);
166 SLIST_INSERT_AFTER(lmp
, lmt
, q
);
168 } else if (!userset
&&
169 !F_ISSET(lmp
, MARK_DELETED
) && F_ISSET(lmp
, MARK_USERSET
))
172 lmp
->lno
= value
->lno
;
173 lmp
->cno
= value
->cno
;
175 lmp
->flags
= userset
? MARK_USERSET
: 0;
181 * Find the requested mark, or, the slot immediately before
185 mark_find(SCR
*sp
, ARG_CHAR_T key
)
187 LMARK
*lmp
, *lastlmp
= NULL
;
190 * Return the requested mark or the slot immediately before
191 * where it should go.
193 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
) {
194 if (lmp
->name
>= key
)
195 return (lmp
->name
== key
? lmp
: lastlmp
);
203 * Update the marks based on an insertion or deletion.
205 * PUBLIC: int mark_insdel(SCR *, lnop_t, recno_t);
208 mark_insdel(SCR
*sp
, lnop_t op
, recno_t lno
)
215 /* All insert/append operations are done as inserts. */
218 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
220 if (lmp
->lno
== lno
) {
221 F_SET(lmp
, MARK_DELETED
);
222 (void)log_mark(sp
, lmp
);
229 * Very nasty special case. If the file was empty, then we're
230 * adding the first line, which is a replacement. So, we don't
231 * modify the marks. This is a hack to make:
233 * mz:r!echo foo<carriage-return>'z
235 * work, i.e. historically you could mark the "line" in an empty
236 * file and replace it, and continue to use the mark. Insane,
237 * well, yes, I know, but someone complained.
239 * Check for line #2 before going to the end of the file.
241 if (!db_exist(sp
, 2)) {
242 if (db_last(sp
, &lline
))
248 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)