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 "lib/global.h"
43 #include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
45 #include "edit-widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
58 /** note, if there is more than one bookmark on a line, then they are
59 appended after each other and the last one is always the one found
60 by book_mark_found() i.e. last in is the one seen */
62 static struct _book_mark
*
63 double_marks (WEdit
* edit
, struct _book_mark
*p
)
68 while (p
->next
->line
== p
->line
)
73 /* --------------------------------------------------------------------------------------------- */
74 /** returns the first bookmark on or before this line */
77 book_mark_find (WEdit
* edit
, int line
)
81 if (edit
->book_mark
== NULL
)
83 /* must have an imaginary top bookmark at line -1 to make things less complicated */
84 edit
->book_mark
= g_malloc0 (sizeof (struct _book_mark
));
85 edit
->book_mark
->line
= -1;
86 return edit
->book_mark
;
89 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->next
)
92 break; /* gone past it going downward */
96 if (p
->next
->line
> line
)
99 return double_marks (edit
, p
);
105 return double_marks (edit
, p
);
109 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->prev
)
111 if (p
->next
!= NULL
&& p
->next
->line
<= line
)
112 break; /* gone past it going upward */
118 if (p
->next
->line
> line
)
121 return double_marks (edit
, p
);
127 return double_marks (edit
, p
);
132 return NULL
; /* can't get here */
135 /* --------------------------------------------------------------------------------------------- */
136 /*** public functions ****************************************************************************/
137 /* --------------------------------------------------------------------------------------------- */
139 /** returns true if a bookmark exists at this line of color c */
142 book_mark_query_color (WEdit
* edit
, int line
, int c
)
144 struct _book_mark
*p
;
146 if (edit
->book_mark
== NULL
)
149 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= p
->prev
)
159 /* --------------------------------------------------------------------------------------------- */
160 /** insert a bookmark at this line */
163 book_mark_insert (WEdit
* edit
, size_t line
, int c
)
165 struct _book_mark
*p
, *q
;
167 p
= book_mark_find (edit
, line
);
171 /* already exists, so just change the color */
175 edit
->force
|= REDRAW_LINE
;
180 edit
->force
|= REDRAW_LINE
;
181 /* create list entry */
182 q
= g_malloc0 (sizeof (struct _book_mark
));
183 q
->line
= (int) line
;
186 /* insert into list */
193 /* --------------------------------------------------------------------------------------------- */
194 /** remove a bookmark if there is one at this line matching this color - c of -1 clear all
195 * @returns non-zero on not-found
199 book_mark_clear (WEdit
* edit
, int line
, int c
)
201 struct _book_mark
*p
, *q
;
204 if (edit
->book_mark
== NULL
)
207 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= q
)
210 if (p
->line
== line
&& (p
->c
== c
|| c
== -1))
213 edit
->book_mark
= p
->prev
;
214 p
->prev
->next
= p
->next
;
216 p
->next
->prev
= p
->prev
;
218 edit
->force
|= REDRAW_LINE
;
222 /* if there is only our dummy book mark left, clear it for speed */
223 if (edit
->book_mark
->line
== -1 && !edit
->book_mark
->next
)
225 g_free (edit
->book_mark
);
226 edit
->book_mark
= NULL
;
231 /* --------------------------------------------------------------------------------------------- */
232 /** clear all bookmarks matching this color, if c is -1 clears all */
235 book_mark_flush (WEdit
* edit
, int c
)
237 struct _book_mark
*p
, *q
;
239 if (edit
->book_mark
== NULL
)
242 while (edit
->book_mark
->prev
!= NULL
)
243 edit
->book_mark
= edit
->book_mark
->prev
;
245 for (q
= edit
->book_mark
->next
; q
!= NULL
; q
= p
)
248 if (q
->c
== c
|| c
== -1)
250 q
->prev
->next
= q
->next
;
256 if (edit
->book_mark
->next
== NULL
)
258 g_free (edit
->book_mark
);
259 edit
->book_mark
= NULL
;
262 edit
->force
|= REDRAW_PAGE
;
265 /* --------------------------------------------------------------------------------------------- */
266 /** shift down bookmarks after this line */
269 book_mark_inc (WEdit
* edit
, int line
)
273 struct _book_mark
*p
;
274 p
= book_mark_find (edit
, line
);
275 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
280 /* --------------------------------------------------------------------------------------------- */
281 /** shift up bookmarks after this line */
284 book_mark_dec (WEdit
* edit
, int line
)
286 if (edit
->book_mark
!= NULL
)
288 struct _book_mark
*p
;
289 p
= book_mark_find (edit
, line
);
290 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
295 /* --------------------------------------------------------------------------------------------- */
296 /** prepare line positions of bookmarks to be saved to file */
299 book_mark_serialize (WEdit
* edit
, int color
)
301 struct _book_mark
*p
;
303 if (edit
->serialized_bookmarks
!= NULL
)
304 g_array_set_size (edit
->serialized_bookmarks
, 0);
306 if (edit
->book_mark
!= NULL
)
308 if (edit
->serialized_bookmarks
== NULL
)
309 edit
->serialized_bookmarks
= g_array_sized_new (FALSE
, FALSE
, sizeof (size_t),
310 MAX_SAVED_BOOKMARKS
);
312 for (p
= book_mark_find (edit
, 0); p
!= NULL
; p
= p
->next
)
313 if (p
->c
== color
&& p
->line
>= 0)
314 g_array_append_val (edit
->serialized_bookmarks
, p
->line
);
318 /* --------------------------------------------------------------------------------------------- */
319 /** restore bookmarks from saved line positions */
322 book_mark_restore (WEdit
* edit
, int color
)
324 if (edit
->serialized_bookmarks
!= NULL
)
328 for (i
= 0; i
< edit
->serialized_bookmarks
->len
; i
++)
329 book_mark_insert (edit
, g_array_index (edit
->serialized_bookmarks
, size_t, i
), color
);
333 /* --------------------------------------------------------------------------------------------- */