1 /* editor book mark handling
3 Copyright (C) 2001, 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include <sys/types.h>
36 #include "../src/global.h"
39 #include "edit-widget.h"
42 /* note, if there is more than one bookmark on a line, then they are
43 appended after each other and the last one is always the one found
44 by book_mark_found() i.e. last in is the one seen */
46 static inline struct _book_mark
*double_marks (WEdit
* edit
, struct _book_mark
*p
)
51 while (p
->next
->line
== p
->line
)
56 /* returns the first bookmark on or before this line */
57 struct _book_mark
*book_mark_find (WEdit
* edit
, int line
)
60 if (!edit
->book_mark
) {
61 /* must have an imaginary top bookmark at line -1 to make things less complicated */
62 edit
->book_mark
= g_malloc0 (sizeof (struct _book_mark
));
63 edit
->book_mark
->line
= -1;
64 return edit
->book_mark
;
66 for (p
= edit
->book_mark
; p
; p
= p
->next
) {
68 break; /* gone past it going downward */
69 if (p
->line
<= line
) {
71 if (p
->next
->line
> line
) {
73 return double_marks (edit
, p
);
77 return double_marks (edit
, p
);
81 for (p
= edit
->book_mark
; p
; p
= p
->prev
) {
83 if (p
->next
->line
<= line
)
84 break; /* gone past it going upward */
85 if (p
->line
<= line
) {
87 if (p
->next
->line
> line
) {
89 return double_marks (edit
, p
);
93 return double_marks (edit
, p
);
97 return 0; /* can't get here */
100 /* returns true if a bookmark exists at this line of color c */
101 int book_mark_query_color (WEdit
* edit
, int line
, int c
)
103 struct _book_mark
*p
;
104 if (!edit
->book_mark
)
106 for (p
= book_mark_find (edit
, line
); p
; p
= p
->prev
) {
115 /* insert a bookmark at this line */
117 book_mark_insert (WEdit
*edit
, int line
, int c
)
119 struct _book_mark
*p
, *q
;
120 p
= book_mark_find (edit
, line
);
122 if (p
->line
== line
) {
123 /* already exists, so just change the color */
125 edit
->force
|= REDRAW_LINE
;
131 edit
->force
|= REDRAW_LINE
;
132 /* create list entry */
133 q
= g_malloc0 (sizeof (struct _book_mark
));
137 /* insert into list */
144 /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
145 /* returns non-zero on not-found */
146 int book_mark_clear (WEdit
* edit
, int line
, int c
)
148 struct _book_mark
*p
, *q
;
150 if (!edit
->book_mark
)
152 for (p
= book_mark_find (edit
, line
); p
; p
= q
) {
154 if (p
->line
== line
&& (p
->c
== c
|| c
== -1)) {
156 edit
->force
|= REDRAW_LINE
;
157 edit
->book_mark
= p
->prev
;
158 p
->prev
->next
= p
->next
;
160 p
->next
->prev
= p
->prev
;
165 /* if there is only our dummy book mark left, clear it for speed */
166 if (edit
->book_mark
->line
== -1 && !edit
->book_mark
->next
) {
167 g_free (edit
->book_mark
);
173 /* clear all bookmarks matching this color, if c is -1 clears all */
174 void book_mark_flush (WEdit
* edit
, int c
)
176 struct _book_mark
*p
, *q
;
177 if (!edit
->book_mark
)
179 edit
->force
|= REDRAW_PAGE
;
180 while (edit
->book_mark
->prev
)
181 edit
->book_mark
= edit
->book_mark
->prev
;
182 for (q
= edit
->book_mark
->next
; q
; q
= p
) {
184 if (q
->c
== c
|| c
== -1) {
185 q
->prev
->next
= q
->next
;
191 if (!edit
->book_mark
->next
) {
192 g_free (edit
->book_mark
);
197 /* shift down bookmarks after this line */
198 void book_mark_inc (WEdit
* edit
, int line
)
200 if (edit
->book_mark
) {
201 struct _book_mark
*p
;
202 p
= book_mark_find (edit
, line
);
203 for (p
= p
->next
; p
; p
= p
->next
) {
209 /* shift up bookmarks after this line */
210 void book_mark_dec (WEdit
* edit
, int line
)
212 if (edit
->book_mark
) {
213 struct _book_mark
*p
;
214 p
= book_mark_find (edit
, line
);
215 for (p
= p
->next
; p
; p
= p
->next
) {