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
31 #include <sys/types.h>
35 #include <mhl/memory.h>
37 #include "../src/global.h"
40 #include "edit-widget.h"
43 /* note, if there is more than one bookmark on a line, then they are
44 appended after each other and the last one is always the one found
45 by book_mark_found() i.e. last in is the one seen */
47 static inline struct _book_mark
*double_marks (WEdit
* edit
, struct _book_mark
*p
)
52 while (p
->next
->line
== p
->line
)
57 /* returns the first bookmark on or before this line */
58 struct _book_mark
*book_mark_find (WEdit
* edit
, int line
)
61 if (!edit
->book_mark
) {
62 /* must have an imaginary top bookmark at line -1 to make things less complicated */
63 edit
->book_mark
= g_malloc0 (sizeof (struct _book_mark
));
64 edit
->book_mark
->line
= -1;
65 return edit
->book_mark
;
67 for (p
= edit
->book_mark
; p
; p
= p
->next
) {
69 break; /* gone past it going downward */
70 if (p
->line
<= line
) {
72 if (p
->next
->line
> line
) {
74 return double_marks (edit
, p
);
78 return double_marks (edit
, p
);
82 for (p
= edit
->book_mark
; p
; p
= p
->prev
) {
84 if (p
->next
->line
<= line
)
85 break; /* gone past it going upward */
86 if (p
->line
<= line
) {
88 if (p
->next
->line
> line
) {
90 return double_marks (edit
, p
);
94 return double_marks (edit
, p
);
98 return 0; /* can't get here */
101 /* returns true if a bookmark exists at this line of color c */
102 int book_mark_query_color (WEdit
* edit
, int line
, int c
)
104 struct _book_mark
*p
;
105 if (!edit
->book_mark
)
107 for (p
= book_mark_find (edit
, line
); p
; p
= p
->prev
) {
116 /* insert a bookmark at this line */
118 book_mark_insert (WEdit
*edit
, int line
, int c
)
120 struct _book_mark
*p
, *q
;
121 p
= book_mark_find (edit
, line
);
123 if (p
->line
== line
) {
124 /* already exists, so just change the color */
126 edit
->force
|= REDRAW_LINE
;
132 edit
->force
|= REDRAW_LINE
;
133 /* create list entry */
134 q
= g_malloc0 (sizeof (struct _book_mark
));
138 /* insert into list */
145 /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
146 /* returns non-zero on not-found */
147 int book_mark_clear (WEdit
* edit
, int line
, int c
)
149 struct _book_mark
*p
, *q
;
151 if (!edit
->book_mark
)
153 for (p
= book_mark_find (edit
, line
); p
; p
= q
) {
155 if (p
->line
== line
&& (p
->c
== c
|| c
== -1)) {
157 edit
->force
|= REDRAW_LINE
;
158 edit
->book_mark
= p
->prev
;
159 p
->prev
->next
= p
->next
;
161 p
->next
->prev
= p
->prev
;
166 /* if there is only our dummy book mark left, clear it for speed */
167 if (edit
->book_mark
->line
== -1 && !edit
->book_mark
->next
) {
168 mhl_mem_free (edit
->book_mark
);
174 /* clear all bookmarks matching this color, if c is -1 clears all */
175 void book_mark_flush (WEdit
* edit
, int c
)
177 struct _book_mark
*p
, *q
;
178 if (!edit
->book_mark
)
180 edit
->force
|= REDRAW_PAGE
;
181 while (edit
->book_mark
->prev
)
182 edit
->book_mark
= edit
->book_mark
->prev
;
183 for (q
= edit
->book_mark
->next
; q
; q
= p
) {
185 if (q
->c
== c
|| c
== -1) {
186 q
->prev
->next
= q
->next
;
192 if (!edit
->book_mark
->next
) {
193 mhl_mem_free (edit
->book_mark
);
198 /* shift down bookmarks after this line */
199 void book_mark_inc (WEdit
* edit
, int line
)
201 if (edit
->book_mark
) {
202 struct _book_mark
*p
;
203 p
= book_mark_find (edit
, line
);
204 for (p
= p
->next
; p
; p
= p
->next
) {
210 /* shift up bookmarks after this line */
211 void book_mark_dec (WEdit
* edit
, int line
)
213 if (edit
->book_mark
) {
214 struct _book_mark
*p
;
215 p
= book_mark_find (edit
, line
);
216 for (p
= p
->next
; p
; p
= p
->next
) {