1 /* editor book mark handling
3 Copyright (C) 1996, 1997 the Free Software Foundation
5 Authors: 1996, 1997 Paul Sheer
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 /* note, if there is more than one bookmark on a line, then they are
28 appended after each other and the last one is always the one found
29 by book_mark_found() i.e. last in is the one seen */
31 static inline struct _book_mark
*double_marks (WEdit
* edit
, struct _book_mark
*p
)
34 while (p
->next
->line
== p
->line
)
39 /* returns the first bookmark on or before this line */
40 struct _book_mark
*book_mark_find (WEdit
* edit
, int line
)
43 if (!edit
->book_mark
) {
44 /* must have an imaginary top bookmark at line -1 to make things less complicated */
45 edit
->book_mark
= malloc (sizeof (struct _book_mark
));
46 memset (edit
->book_mark
, 0, sizeof (struct _book_mark
));
47 edit
->book_mark
->line
= -1;
48 return edit
->book_mark
;
50 for (p
= edit
->book_mark
; p
; p
= p
->next
) {
52 break; /* gone past it going downward */
53 if (p
->line
<= line
) {
55 if (p
->next
->line
> line
) {
57 return double_marks (edit
, p
);
61 return double_marks (edit
, p
);
65 for (p
= edit
->book_mark
; p
; p
= p
->prev
) {
67 if (p
->next
->line
<= line
)
68 break; /* gone past it going upward */
69 if (p
->line
<= line
) {
71 if (p
->next
->line
> line
) {
73 return double_marks (edit
, p
);
77 return double_marks (edit
, p
);
81 return 0; /* can't get here */
84 /* returns true if a bookmark exists at this line of colour c */
85 int book_mark_query_color (WEdit
* edit
, int line
, int c
)
90 for (p
= book_mark_find (edit
, line
); p
; p
= p
->prev
) {
99 /* returns the number of bookmarks at this line and a list of their colours in c
100 up to a maximum of 8 colours */
101 int book_mark_query_all (WEdit
* edit
, int line
, int *c
)
104 struct _book_mark
*p
;
105 if (!edit
->book_mark
)
107 for (i
= 0, p
= book_mark_find (edit
, line
); p
&& i
< 8; p
= p
->prev
, i
++) {
115 /* insert a bookmark at this line */
116 void book_mark_insert (WEdit
* edit
, int line
, int c
)
118 struct _book_mark
*p
, *q
;
119 p
= book_mark_find (edit
, line
);
121 if (p
->line
== line
) { /* already exists, so just change the colour */
123 edit
->force
|= REDRAW_LINE
;
129 edit
->force
|= REDRAW_LINE
;
130 /* create list entry */
131 q
= malloc (sizeof (struct _book_mark
));
132 memset (q
, 0, sizeof (struct _book_mark
));
136 /* insert into list */
143 /* remove a bookmark if there is one at this line matching this colour - c of -1 clear all */
144 /* returns non-zero on not-found */
145 int book_mark_clear (WEdit
* edit
, int line
, int c
)
147 struct _book_mark
*p
, *q
;
149 if (!edit
->book_mark
)
151 for (p
= book_mark_find (edit
, line
); p
; p
= q
) {
153 if (p
->line
== line
&& (p
->c
== c
|| c
== -1)) {
155 edit
->force
|= REDRAW_LINE
;
156 edit
->book_mark
= p
->prev
;
157 p
->prev
->next
= p
->next
;
159 p
->next
->prev
= p
->prev
;
164 /* if there is only our dummy book mark left, clear it for speed */
165 if (edit
->book_mark
->line
== -1 && !edit
->book_mark
->next
) {
166 free (edit
->book_mark
);
172 /* clear all bookmarks matching this colour, if c is -1 clears all */
173 void book_mark_flush (WEdit
* edit
, int c
)
175 struct _book_mark
*p
, *q
;
176 if (!edit
->book_mark
)
178 edit
->force
|= REDRAW_PAGE
;
179 while (edit
->book_mark
->prev
)
180 edit
->book_mark
= edit
->book_mark
->prev
;
181 for (q
= edit
->book_mark
->next
; q
; q
= p
) {
183 if (q
->c
== c
|| c
== -1) {
184 q
->prev
->next
= q
->next
;
190 if (!edit
->book_mark
->next
) {
191 free (edit
->book_mark
);
196 /* shift down bookmarks after this line */
197 void book_mark_inc (WEdit
* edit
, int line
)
199 if (edit
->book_mark
) {
200 struct _book_mark
*p
;
201 p
= book_mark_find (edit
, line
);
202 for (p
= p
->next
; p
; p
= p
->next
) {
208 /* shift up bookmarks after this line */
209 void book_mark_dec (WEdit
* edit
, int line
)
211 if (edit
->book_mark
) {
212 struct _book_mark
*p
;
213 p
= book_mark_find (edit
, line
);
214 for (p
= p
->next
; p
; p
= p
->next
) {