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.14 2011/07/04 14:42:58 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static LMARK
*mark_find(SCR
*, ARG_CHAR_T
);
32 * Marks are maintained in a key sorted singly linked list. We can't
33 * use arrays because we have no idea how big an index key could be.
34 * The underlying assumption is that users don't have more than, say,
35 * 10 marks at any one time, so this will be is fast enough.
37 * Marks are fixed, and modifications to the line don't update the mark's
38 * position in the line. This can be hard. If you add text to the line,
39 * place a mark in that text, undo the addition and use ` to move to the
40 * mark, the location will have disappeared. It's tempting to try to adjust
41 * the mark with the changes in the line, but this is hard to do, especially
42 * if we've given the line to v_ntext.c:v_ntext() for editing. Historic vi
43 * would move to the first non-blank on the line when the mark location was
44 * past the end of the line. This can be complicated by deleting to a mark
45 * that has disappeared using the ` command. Historic vi treated this as
46 * a line-mode motion and deleted the line. This implementation complains to
49 * In historic vi, marks returned if the operation was undone, unless the
50 * mark had been subsequently reset. Tricky. This is hard to start with,
51 * but in the presence of repeated undo it gets nasty. When a line is
52 * deleted, we delete (and log) any marks on that line. An undo will create
53 * the mark. Any mark creations are noted as to whether the user created
54 * it or if it was created by an undo. The former cannot be reset by another
55 * undo, but the latter may.
57 * All of these routines translate ABSMARK2 to ABSMARK1. Setting either of
58 * the absolute mark locations sets both, so that "m'" and "m`" work like
59 * they, ah, for lack of a better word, "should".
66 * PUBLIC: int mark_init(SCR *, EXF *);
75 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
79 SLIST_INIT(ep
->marks
);
87 * PUBLIC: int mark_end(SCR *, EXF *);
98 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
100 while ((lmp
= SLIST_FIRST(ep
->marks
)) != NULL
) {
101 SLIST_REMOVE_HEAD(ep
->marks
, q
);
109 * Get the location referenced by a mark.
111 * PUBLIC: int mark_get(SCR *, ARG_CHAR_T, MARK *, mtype_t);
125 lmp
= mark_find(sp
, key
);
126 if (lmp
== NULL
|| lmp
->name
!= key
) {
127 msgq(sp
, mtype
, "017|Mark %s: not set", KEY_NAME(sp
, key
));
130 if (F_ISSET(lmp
, MARK_DELETED
)) {
132 "018|Mark %s: the line was deleted", KEY_NAME(sp
, key
));
138 * The absolute mark is initialized to lno 1/cno 0, and historically
139 * you could use it in an empty file. Make such a mark always work.
141 if ((lmp
->lno
!= 1 || lmp
->cno
!= 0) && !db_exist(sp
, lmp
->lno
)) {
143 "019|Mark %s: cursor position no longer exists",
154 * Set the location referenced by a mark.
156 * PUBLIC: int mark_set(SCR *, ARG_CHAR_T, MARK *, int);
171 * The rules are simple. If the user is setting a mark (if it's a
172 * new mark this is always true), it always happens. If not, it's
173 * an undo, and we set it if it's not already set or if it was set
174 * by a previous undo.
176 lmp
= mark_find(sp
, key
);
177 if (lmp
== NULL
|| lmp
->name
!= key
) {
178 MALLOC_RET(sp
, lmt
, LMARK
*, sizeof(LMARK
));
180 SLIST_INSERT_HEAD(sp
->ep
->marks
, lmt
, q
);
182 SLIST_INSERT_AFTER(lmp
, lmt
, q
);
184 } else if (!userset
&&
185 !F_ISSET(lmp
, MARK_DELETED
) && F_ISSET(lmp
, MARK_USERSET
))
188 lmp
->lno
= value
->lno
;
189 lmp
->cno
= value
->cno
;
191 lmp
->flags
= userset
? MARK_USERSET
: 0;
197 * Find the requested mark, or, the slot immediately before
205 LMARK
*lmp
, *lastlmp
= NULL
;
208 * Return the requested mark or the slot immediately before
209 * where it should go.
211 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
) {
212 if (lmp
->name
>= key
)
213 return (lmp
->name
== key
? lmp
: lastlmp
);
221 * Update the marks based on an insertion or deletion.
223 * PUBLIC: int mark_insdel(SCR *, lnop_t, recno_t);
236 /* All insert/append operations are done as inserts. */
239 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)
241 if (lmp
->lno
== lno
) {
242 F_SET(lmp
, MARK_DELETED
);
243 (void)log_mark(sp
, lmp
);
250 * Very nasty special case. If the file was empty, then we're
251 * adding the first line, which is a replacement. So, we don't
252 * modify the marks. This is a hack to make:
254 * mz:r!echo foo<carriage-return>'z
256 * work, i.e. historically you could mark the "line" in an empty
257 * file and replace it, and continue to use the mark. Insane,
258 * well, yes, I know, but someone complained.
260 * Check for line #2 before going to the end of the file.
262 if (!db_exist(sp
, 2)) {
263 if (db_last(sp
, &lline
))
269 SLIST_FOREACH(lmp
, sp
->ep
->marks
, q
)