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
[] = "$Id: mark.c,v 10.15 2001/06/25 15:19:11 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:11 $";
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 *));
68 mark_init(SCR
*sp
, EXF
*ep
)
72 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
76 LIST_INIT(&ep
->marks
);
84 * PUBLIC: int mark_end __P((SCR *, EXF *));
87 mark_end(SCR
*sp
, EXF
*ep
)
93 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
95 while ((lmp
= ep
->marks
.lh_first
) != NULL
) {
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
)
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
));
121 if (F_ISSET(lmp
, MARK_DELETED
)) {
123 "018|Mark %s: the line was deleted", KEY_NAME(sp
, key
));
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
)) {
134 "019|Mark %s: cursor position no longer exists",
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
)
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
));
167 LIST_INSERT_HEAD(&sp
->ep
->marks
, lmt
, q
);
169 LIST_INSERT_AFTER(lmp
, lmt
, q
);
171 } else if (!userset
&&
172 !F_ISSET(lmp
, MARK_DELETED
) && F_ISSET(lmp
, MARK_USERSET
))
175 lmp
->lno
= value
->lno
;
176 lmp
->cno
= value
->cno
;
178 lmp
->flags
= userset
? MARK_USERSET
: 0;
184 * Find the requested mark, or, the slot immediately before
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
);
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
)
217 /* All insert/append operations are done as inserts. */
220 for (lmp
= sp
->ep
->marks
.lh_first
;
221 lmp
!= NULL
; lmp
= lmp
->q
.le_next
)
223 if (lmp
->lno
== lno
) {
224 F_SET(lmp
, MARK_DELETED
);
225 (void)log_mark(sp
, lmp
);
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
))
251 for (lmp
= sp
->ep
->marks
.lh_first
;
252 lmp
!= NULL
; lmp
= lmp
->q
.le_next
)