fixed doxygen.cfg, excluded directory /tests/ from doxygen path's
[midnight-commander.git] / src / editor / bookmark.c
blobfde8e566379fadd8d7aa5e6538ae946622f19053
1 /*
2 Editor book mark handling
4 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Paul Sheer, 1996, 1997
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/>.
26 /** \file
27 * \brief Source: editor book mark handling
28 * \author Paul Sheer
29 * \date 1996, 1997
32 #include <config.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <unistd.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 struct _book_mark *
66 double_marks (WEdit * edit, struct _book_mark *p)
68 (void) edit;
70 if (p->next != NULL)
71 while (p->next->line == p->line)
72 p = p->next;
73 return p;
76 /* --------------------------------------------------------------------------------------------- */
77 /** returns the first bookmark on or before this line */
79 struct _book_mark *
80 book_mark_find (WEdit * edit, long line)
82 struct _book_mark *p;
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_malloc0 (sizeof (struct _book_mark));
88 edit->book_mark->line = -1;
89 return edit->book_mark;
92 for (p = edit->book_mark; p != NULL; p = p->next)
94 if (p->line > line)
95 break; /* gone past it going downward */
97 if (p->next != NULL)
99 if (p->next->line > line)
101 edit->book_mark = p;
102 return double_marks (edit, p);
105 else
107 edit->book_mark = 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 */
117 if (p->line <= line)
119 if (p->next != NULL)
121 if (p->next->line > line)
123 edit->book_mark = p;
124 return double_marks (edit, p);
127 else
129 edit->book_mark = p;
130 return double_marks (edit, p);
135 return NULL; /* can't get here */
138 /* --------------------------------------------------------------------------------------------- */
139 /*** public functions ****************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
142 /** returns true if a bookmark exists at this line of color c */
145 book_mark_query_color (WEdit * edit, long line, int c)
147 struct _book_mark *p;
149 if (edit->book_mark == NULL)
150 return 0;
152 for (p = book_mark_find (edit, line); p != NULL; p = p->prev)
154 if (p->line != line)
155 return 0;
156 if (p->c == c)
157 return 1;
159 return 0;
162 /* --------------------------------------------------------------------------------------------- */
163 /** insert a bookmark at this line */
165 void
166 book_mark_insert (WEdit * edit, long line, int c)
168 struct _book_mark *p, *q;
170 p = book_mark_find (edit, line);
171 #if 0
172 if (p->line == line)
174 /* already exists, so just change the color */
175 if (p->c != c)
177 p->c = c;
178 edit->force |= REDRAW_LINE;
180 return;
182 #endif
183 /* create list entry */
184 q = g_malloc0 (sizeof (struct _book_mark));
185 q->line = line;
186 q->c = c;
187 q->next = p->next;
188 /* insert into list */
189 q->prev = p;
190 if (p->next != NULL)
191 p->next->prev = q;
192 p->next = q;
194 edit->force |= REDRAW_LINE;
197 /* --------------------------------------------------------------------------------------------- */
198 /** remove a bookmark if there is one at this line matching this color - c of -1 clear all
199 * @returns non-zero on not-found
203 book_mark_clear (WEdit * edit, long line, int c)
205 struct _book_mark *p, *q;
206 int r = 1;
208 if (edit->book_mark == NULL)
209 return r;
211 for (p = book_mark_find (edit, line); p != NULL; p = q)
213 q = p->prev;
214 if (p->line == line && (p->c == c || c == -1))
216 r = 0;
217 edit->book_mark = p->prev;
218 p->prev->next = p->next;
219 if (p->next != NULL)
220 p->next->prev = p->prev;
221 g_free (p);
222 edit->force |= REDRAW_LINE;
223 break;
226 /* if there is only our dummy book mark left, clear it for speed */
227 if (edit->book_mark->line == -1 && !edit->book_mark->next)
229 g_free (edit->book_mark);
230 edit->book_mark = NULL;
232 return r;
235 /* --------------------------------------------------------------------------------------------- */
236 /** clear all bookmarks matching this color, if c is -1 clears all */
238 void
239 book_mark_flush (WEdit * edit, int c)
241 struct _book_mark *p, *q;
243 if (edit->book_mark == NULL)
244 return;
246 while (edit->book_mark->prev != NULL)
247 edit->book_mark = edit->book_mark->prev;
249 for (q = edit->book_mark->next; q != NULL; q = p)
251 p = q->next;
252 if (q->c == c || c == -1)
254 q->prev->next = q->next;
255 if (p != NULL)
256 p->prev = q->prev;
257 g_free (q);
260 if (edit->book_mark->next == NULL)
262 g_free (edit->book_mark);
263 edit->book_mark = NULL;
266 edit->force |= REDRAW_PAGE;
269 /* --------------------------------------------------------------------------------------------- */
270 /** shift down bookmarks after this line */
272 void
273 book_mark_inc (WEdit * edit, long line)
275 if (edit->book_mark != NULL)
277 struct _book_mark *p;
279 p = book_mark_find (edit, line);
280 for (p = p->next; p != NULL; p = p->next)
281 p->line++;
285 /* --------------------------------------------------------------------------------------------- */
286 /** shift up bookmarks after this line */
288 void
289 book_mark_dec (WEdit * edit, long line)
291 if (edit->book_mark != NULL)
293 struct _book_mark *p;
295 p = book_mark_find (edit, line);
296 for (p = p->next; p != NULL; p = p->next)
297 p->line--;
301 /* --------------------------------------------------------------------------------------------- */
302 /** prepare line positions of bookmarks to be saved to file */
304 void
305 book_mark_serialize (WEdit * edit, int color)
307 struct _book_mark *p;
309 if (edit->serialized_bookmarks != NULL)
310 g_array_set_size (edit->serialized_bookmarks, 0);
312 if (edit->book_mark != NULL)
314 if (edit->serialized_bookmarks == NULL)
315 edit->serialized_bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t),
316 MAX_SAVED_BOOKMARKS);
318 for (p = book_mark_find (edit, 0); p != NULL; p = p->next)
319 if (p->c == color && p->line >= 0)
320 g_array_append_val (edit->serialized_bookmarks, p->line);
324 /* --------------------------------------------------------------------------------------------- */
325 /** restore bookmarks from saved line positions */
327 void
328 book_mark_restore (WEdit * edit, int color)
330 if (edit->serialized_bookmarks != NULL)
332 size_t i;
334 for (i = 0; i < edit->serialized_bookmarks->len; i++)
335 book_mark_insert (edit, g_array_index (edit->serialized_bookmarks, size_t, i), color);
339 /* --------------------------------------------------------------------------------------------- */