2 Editor book mark handling
4 Copyright (C) 2001-2024
5 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
40 #include <sys/types.h>
44 #include "lib/global.h"
45 #include "lib/util.h" /* MAX_SAVED_BOOKMARKS */
47 #include "editwidget.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** forward declarations (file scope functions) *************************************************/
57 /*** file scope variables ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /** note, if there is more than one bookmark on a line, then they are
64 appended after each other and the last one is always the one found
65 by book_mark_found() i.e. last in is the one seen */
67 static edit_book_mark_t
*
68 double_marks (WEdit
*edit
, edit_book_mark_t
*p
)
73 while (p
->next
->line
== p
->line
)
78 /* --------------------------------------------------------------------------------------------- */
79 /** returns the first bookmark on or before this line */
82 book_mark_find (WEdit
*edit
, long line
)
86 if (edit
->book_mark
== NULL
)
88 /* must have an imaginary top bookmark at line -1 to make things less complicated */
89 edit
->book_mark
= g_new0 (edit_book_mark_t
, 1);
90 edit
->book_mark
->line
= -1;
91 return edit
->book_mark
;
94 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->next
)
97 break; /* gone past it going downward */
101 if (p
->next
->line
> line
)
104 return double_marks (edit
, p
);
110 return double_marks (edit
, p
);
114 for (p
= edit
->book_mark
; p
!= NULL
; p
= p
->prev
)
116 if (p
->next
!= NULL
&& p
->next
->line
<= line
)
117 break; /* gone past it going upward */
123 if (p
->next
->line
> line
)
126 return double_marks (edit
, p
);
132 return double_marks (edit
, p
);
137 return NULL
; /* can't get here */
140 /* --------------------------------------------------------------------------------------------- */
141 /*** public functions ****************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
145 * Check if bookmark bookmark exists at this line of this color
147 * @param edit editor object
148 * @param line line where book mark is
149 * @param c color of book mark
150 * @return TRUE if bookmark exists at this line of color c, FALSE otherwise
154 book_mark_query_color (WEdit
*edit
, long line
, int c
)
156 if (edit
->book_mark
!= NULL
)
160 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= p
->prev
)
172 /* --------------------------------------------------------------------------------------------- */
173 /** insert a bookmark at this line */
176 book_mark_insert (WEdit
*edit
, long line
, int c
)
178 edit_book_mark_t
*p
, *q
;
180 p
= book_mark_find (edit
, line
);
184 /* already exists, so just change the color */
188 edit
->force
|= REDRAW_LINE
;
193 /* create list entry */
194 q
= g_new (edit_book_mark_t
, 1);
198 /* insert into list */
204 edit
->force
|= REDRAW_LINE
;
207 /* --------------------------------------------------------------------------------------------- */
209 * Remove a bookmark if there is one at this line matching this color - c of -1 clear all
211 * @param edit editor object
212 * @param line line where book mark is
213 * @param c color of book mark or -1 to clear all book marks on this line
214 * @return FALSE if not found, TRUE otherwise
218 book_mark_clear (WEdit
*edit
, long line
, int c
)
220 edit_book_mark_t
*p
, *q
;
223 if (edit
->book_mark
== NULL
)
226 for (p
= book_mark_find (edit
, line
); p
!= NULL
; p
= q
)
229 if (p
->line
== line
&& (p
->c
== c
|| c
== -1))
232 edit
->book_mark
= p
->prev
;
233 p
->prev
->next
= p
->next
;
235 p
->next
->prev
= p
->prev
;
237 edit
->force
|= REDRAW_LINE
;
241 /* if there is only our dummy book mark left, clear it for speed */
242 if (edit
->book_mark
->line
== -1 && edit
->book_mark
->next
== NULL
)
243 MC_PTR_FREE (edit
->book_mark
);
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
)
274 MC_PTR_FREE (edit
->book_mark
);
276 edit
->force
|= REDRAW_PAGE
;
279 /* --------------------------------------------------------------------------------------------- */
280 /** shift down bookmarks after this line */
283 book_mark_inc (WEdit
*edit
, long line
)
285 if (edit
->book_mark
!= NULL
)
289 p
= book_mark_find (edit
, line
);
290 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
295 /* --------------------------------------------------------------------------------------------- */
296 /** shift up bookmarks after this line */
299 book_mark_dec (WEdit
*edit
, long line
)
301 if (edit
->book_mark
!= NULL
)
305 p
= book_mark_find (edit
, line
);
306 for (p
= p
->next
; p
!= NULL
; p
= p
->next
)
311 /* --------------------------------------------------------------------------------------------- */
312 /** prepare line positions of bookmarks to be saved to file */
315 book_mark_serialize (WEdit
*edit
, int color
)
317 if (edit
->serialized_bookmarks
!= NULL
)
318 g_array_set_size (edit
->serialized_bookmarks
, 0);
320 if (edit
->book_mark
!= NULL
)
324 if (edit
->serialized_bookmarks
== NULL
)
325 edit
->serialized_bookmarks
= g_array_sized_new (FALSE
, FALSE
, sizeof (size_t),
326 MAX_SAVED_BOOKMARKS
);
328 for (p
= book_mark_find (edit
, 0); p
!= NULL
; p
= p
->next
)
329 if (p
->c
== color
&& p
->line
>= 0)
330 g_array_append_val (edit
->serialized_bookmarks
, p
->line
);
334 /* --------------------------------------------------------------------------------------------- */
335 /** restore bookmarks from saved line positions */
338 book_mark_restore (WEdit
*edit
, int color
)
340 if (edit
->serialized_bookmarks
!= NULL
)
344 for (i
= 0; i
< edit
->serialized_bookmarks
->len
; i
++)
345 book_mark_insert (edit
, g_array_index (edit
->serialized_bookmarks
, size_t, i
), color
);
349 /* --------------------------------------------------------------------------------------------- */