2 Editor book mark handling
4 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2011
5 The Free Software Foundation, Inc.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * \brief Source: editor book mark handling
41 #include <sys/types.h>
45 #include "lib/global.h"
46 #include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
48 #include "editwidget.h"
50 /*** global variables ****************************************************************************/
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 /** note, if there is more than one bookmark on a line, then they are
62 appended after each other and the last one is always the one found
63 by book_mark_found() i.e. last in is the one seen */
65 static edit_book_mark_t
*
66 double_marks (WEdit
* edit
, edit_book_mark_t
* p
)
71 while (p
->next
->line
== p
->line
)
76 /* --------------------------------------------------------------------------------------------- */
77 /** returns the first bookmark on or before this line */
80 book_mark_find (WEdit
* edit
, long line
)
84 if (edit
->book_mark
== NULL
)
86 /* must have an imaginary top bookmark at line -1 to make things less complicated */
87 edit
->book_mark
= g_new0 (edit_book_mark_t
, 1);
88 edit
->book_mark
->line
= -1;
89 return edit
->book_mark
;
92 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->next
)
95 break; /* gone past it going downward */
99 if (p
->next
->line
> line
)
102 return double_marks (edit
, p
);
108 return double_marks (edit
, p
);
112 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->prev
)
114 if (p
->next
!= NULL
&& p
->next
->line
<= line
)
115 break; /* gone past it going upward */
121 if (p
->next
->line
> line
)
124 return double_marks (edit
, p
);
130 return double_marks (edit
, p
);
135 return NULL
; /* can't get here */
138 /* --------------------------------------------------------------------------------------------- */
139 /*** public functions ****************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
143 * Check if bookmark bookmark exists at this line of this color
145 * @param edit editor object
146 * @param line line where book mark is
147 * @param c color of book mark
148 * @return TRUE if bookmark exists at this line of color c, FALSE otherwise
152 book_mark_query_color (WEdit
* edit
, long line
, int c
)
154 if (edit
->book_mark
!= NULL
)
158 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= p
->prev
)
170 /* --------------------------------------------------------------------------------------------- */
171 /** insert a bookmark at this line */
174 book_mark_insert (WEdit
* edit
, long line
, int c
)
176 edit_book_mark_t
*p
, *q
;
178 p
= book_mark_find (edit
, line
);
182 /* already exists, so just change the color */
186 edit
->force
|= REDRAW_LINE
;
191 /* create list entry */
192 q
= g_new (edit_book_mark_t
, 1);
196 /* insert into list */
202 edit
->force
|= REDRAW_LINE
;
205 /* --------------------------------------------------------------------------------------------- */
207 * Remove a bookmark if there is one at this line matching this color - c of -1 clear all
209 * @param edit editor object
210 * @param line line where book mark is
211 * @param c color of book mark or -1 to clear all book marks on this line
212 * @return FALSE if not found, TRUE otherwise
216 book_mark_clear (WEdit
* edit
, long line
, int c
)
218 edit_book_mark_t
*p
, *q
;
221 if (edit
->book_mark
== NULL
)
224 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= q
)
227 if (p
->line
== line
&& (p
->c
== c
|| c
== -1))
230 edit
->book_mark
= p
->prev
;
231 p
->prev
->next
= p
->next
;
233 p
->next
->prev
= p
->prev
;
235 edit
->force
|= REDRAW_LINE
;
239 /* if there is only our dummy book mark left, clear it for speed */
240 if (edit
->book_mark
->line
== -1 && edit
->book_mark
->next
== NULL
)
242 g_free (edit
->book_mark
);
243 edit
->book_mark
= NULL
;
248 /* --------------------------------------------------------------------------------------------- */
249 /** clear all bookmarks matching this color, if c is -1 clears all */
252 book_mark_flush (WEdit
* edit
, int c
)
254 edit_book_mark_t
*p
, *q
;
256 if (edit
->book_mark
== NULL
)
259 while (edit
->book_mark
->prev
!= NULL
)
260 edit
->book_mark
= edit
->book_mark
->prev
;
262 for (q
= edit
->book_mark
->next
; q
!= NULL
; q
= p
)
265 if (q
->c
== c
|| c
== -1)
267 q
->prev
->next
= q
->next
;
273 if (edit
->book_mark
->next
== NULL
)
275 g_free (edit
->book_mark
);
276 edit
->book_mark
= NULL
;
279 edit
->force
|= REDRAW_PAGE
;
282 /* --------------------------------------------------------------------------------------------- */
283 /** shift down bookmarks after this line */
286 book_mark_inc (WEdit
* edit
, long line
)
288 if (edit
->book_mark
!= NULL
)
292 p
= book_mark_find (edit
, line
);
293 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
298 /* --------------------------------------------------------------------------------------------- */
299 /** shift up bookmarks after this line */
302 book_mark_dec (WEdit
* edit
, long line
)
304 if (edit
->book_mark
!= NULL
)
308 p
= book_mark_find (edit
, line
);
309 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
314 /* --------------------------------------------------------------------------------------------- */
315 /** prepare line positions of bookmarks to be saved to file */
318 book_mark_serialize (WEdit
* edit
, int color
)
320 if (edit
->serialized_bookmarks
!= NULL
)
321 g_array_set_size (edit
->serialized_bookmarks
, 0);
323 if (edit
->book_mark
!= NULL
)
327 if (edit
->serialized_bookmarks
== NULL
)
328 edit
->serialized_bookmarks
= g_array_sized_new (FALSE
, FALSE
, sizeof (size_t),
329 MAX_SAVED_BOOKMARKS
);
331 for (p
= book_mark_find (edit
, 0); p
!= NULL
; p
= p
->next
)
332 if (p
->c
== color
&& p
->line
>= 0)
333 g_array_append_val (edit
->serialized_bookmarks
, p
->line
);
337 /* --------------------------------------------------------------------------------------------- */
338 /** restore bookmarks from saved line positions */
341 book_mark_restore (WEdit
* edit
, int color
)
343 if (edit
->serialized_bookmarks
!= NULL
)
347 for (i
= 0; i
< edit
->serialized_bookmarks
->len
; i
++)
348 book_mark_insert (edit
, g_array_index (edit
->serialized_bookmarks
, size_t, i
), color
);
352 /* --------------------------------------------------------------------------------------------- */