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
24 * \brief Source: editor book mark handling
38 #include <sys/types.h>
42 #include "../src/global.h"
44 #include "edit-impl.h"
45 #include "edit-widget.h"
48 /* note, if there is more than one bookmark on a line, then they are
49 appended after each other and the last one is always the one found
50 by book_mark_found() i.e. last in is the one seen */
52 static struct _book_mark
*double_marks (WEdit
* edit
, struct _book_mark
*p
)
57 while (p
->next
->line
== p
->line
)
62 /* returns the first bookmark on or before this line */
63 struct _book_mark
*book_mark_find (WEdit
* edit
, int line
)
66 if (!edit
->book_mark
) {
67 /* must have an imaginary top bookmark at line -1 to make things less complicated */
68 edit
->book_mark
= g_malloc0 (sizeof (struct _book_mark
));
69 edit
->book_mark
->line
= -1;
70 return edit
->book_mark
;
72 for (p
= edit
->book_mark
; p
; p
= p
->next
) {
74 break; /* gone past it going downward */
75 if (p
->line
<= line
) {
77 if (p
->next
->line
> line
) {
79 return double_marks (edit
, p
);
83 return double_marks (edit
, p
);
87 for (p
= edit
->book_mark
; p
; p
= p
->prev
) {
89 if (p
->next
->line
<= line
)
90 break; /* gone past it going upward */
91 if (p
->line
<= line
) {
93 if (p
->next
->line
> line
) {
95 return double_marks (edit
, p
);
99 return double_marks (edit
, p
);
103 return 0; /* can't get here */
106 /* returns true if a bookmark exists at this line of color c */
107 int book_mark_query_color (WEdit
* edit
, int line
, int c
)
109 struct _book_mark
*p
;
110 if (!edit
->book_mark
)
112 for (p
= book_mark_find (edit
, line
); p
; p
= p
->prev
) {
121 /* insert a bookmark at this line */
123 book_mark_insert (WEdit
*edit
, int line
, int c
)
125 struct _book_mark
*p
, *q
;
126 p
= book_mark_find (edit
, line
);
128 if (p
->line
== line
) {
129 /* already exists, so just change the color */
131 edit
->force
|= REDRAW_LINE
;
137 edit
->force
|= REDRAW_LINE
;
138 /* create list entry */
139 q
= g_malloc0 (sizeof (struct _book_mark
));
143 /* insert into list */
150 /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
151 /* returns non-zero on not-found */
152 int book_mark_clear (WEdit
* edit
, int line
, int c
)
154 struct _book_mark
*p
, *q
;
156 if (!edit
->book_mark
)
158 for (p
= book_mark_find (edit
, line
); p
; p
= q
) {
160 if (p
->line
== line
&& (p
->c
== c
|| c
== -1)) {
162 edit
->force
|= REDRAW_LINE
;
163 edit
->book_mark
= p
->prev
;
164 p
->prev
->next
= p
->next
;
166 p
->next
->prev
= p
->prev
;
171 /* if there is only our dummy book mark left, clear it for speed */
172 if (edit
->book_mark
->line
== -1 && !edit
->book_mark
->next
) {
173 g_free (edit
->book_mark
);
179 /* clear all bookmarks matching this color, if c is -1 clears all */
180 void book_mark_flush (WEdit
* edit
, int c
)
182 struct _book_mark
*p
, *q
;
183 if (!edit
->book_mark
)
185 edit
->force
|= REDRAW_PAGE
;
186 while (edit
->book_mark
->prev
)
187 edit
->book_mark
= edit
->book_mark
->prev
;
188 for (q
= edit
->book_mark
->next
; q
; q
= p
) {
190 if (q
->c
== c
|| c
== -1) {
191 q
->prev
->next
= q
->next
;
197 if (!edit
->book_mark
->next
) {
198 g_free (edit
->book_mark
);
203 /* shift down bookmarks after this line */
204 void book_mark_inc (WEdit
* edit
, int line
)
206 if (edit
->book_mark
) {
207 struct _book_mark
*p
;
208 p
= book_mark_find (edit
, line
);
209 for (p
= p
->next
; p
; p
= p
->next
) {
215 /* shift up bookmarks after this line */
216 void book_mark_dec (WEdit
* edit
, int line
)
218 if (edit
->book_mark
) {
219 struct _book_mark
*p
;
220 p
= book_mark_find (edit
, line
);
221 for (p
= p
->next
; p
; p
= p
->next
) {