(make_fullname): removed. Use mc_build_filename() instead.
[midnight-commander.git] / src / editor / bookmark.c
blob8d9b2293e38c3c13d5b80089d73c24da240c1b72
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
20 02110-1301, USA.
23 /** \file
24 * \brief Source: editor book mark handling
25 * \author Paul Sheer
26 * \date 1996, 1997
29 #include <config.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.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)
65 (void) edit;
67 if (p->next != NULL)
68 while (p->next->line == p->line)
69 p = p->next;
70 return p;
73 /* --------------------------------------------------------------------------------------------- */
74 /** returns the first bookmark on or before this line */
76 struct _book_mark *
77 book_mark_find (WEdit * edit, int line)
79 struct _book_mark *p;
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)
91 if (p->line > line)
92 break; /* gone past it going downward */
94 if (p->next != NULL)
96 if (p->next->line > line)
98 edit->book_mark = p;
99 return double_marks (edit, p);
102 else
104 edit->book_mark = 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 */
114 if (p->line <= line)
116 if (p->next != NULL)
118 if (p->next->line > line)
120 edit->book_mark = p;
121 return double_marks (edit, p);
124 else
126 edit->book_mark = 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)
147 return 0;
149 for (p = book_mark_find (edit, line); p != NULL; p = p->prev)
151 if (p->line != line)
152 return 0;
153 if (p->c == c)
154 return 1;
156 return 0;
159 /* --------------------------------------------------------------------------------------------- */
160 /** insert a bookmark at this line */
162 void
163 book_mark_insert (WEdit * edit, size_t line, int c)
165 struct _book_mark *p, *q;
167 p = book_mark_find (edit, line);
168 #if 0
169 if (p->line == line)
171 /* already exists, so just change the color */
172 if (p->c != c)
174 p->c = c;
175 edit->force |= REDRAW_LINE;
177 return;
179 #endif
180 edit->force |= REDRAW_LINE;
181 /* create list entry */
182 q = g_malloc0 (sizeof (struct _book_mark));
183 q->line = (int) line;
184 q->c = c;
185 q->next = p->next;
186 /* insert into list */
187 q->prev = p;
188 if (p->next != NULL)
189 p->next->prev = q;
190 p->next = q;
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;
202 int r = 1;
204 if (edit->book_mark == NULL)
205 return r;
207 for (p = book_mark_find (edit, line); p != NULL; p = q)
209 q = p->prev;
210 if (p->line == line && (p->c == c || c == -1))
212 r = 0;
213 edit->book_mark = p->prev;
214 p->prev->next = p->next;
215 if (p->next != NULL)
216 p->next->prev = p->prev;
217 g_free (p);
218 edit->force |= REDRAW_LINE;
219 break;
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;
228 return r;
231 /* --------------------------------------------------------------------------------------------- */
232 /** clear all bookmarks matching this color, if c is -1 clears all */
234 void
235 book_mark_flush (WEdit * edit, int c)
237 struct _book_mark *p, *q;
239 if (edit->book_mark == NULL)
240 return;
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)
247 p = q->next;
248 if (q->c == c || c == -1)
250 q->prev->next = q->next;
251 if (p != NULL)
252 p->prev = q->prev;
253 g_free (q);
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 */
268 void
269 book_mark_inc (WEdit * edit, int line)
271 if (edit->book_mark)
273 struct _book_mark *p;
274 p = book_mark_find (edit, line);
275 for (p = p->next; p != NULL; p = p->next)
276 p->line++;
280 /* --------------------------------------------------------------------------------------------- */
281 /** shift up bookmarks after this line */
283 void
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)
291 p->line--;
295 /* --------------------------------------------------------------------------------------------- */
296 /** prepare line positions of bookmarks to be saved to file */
298 void
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 */
321 void
322 book_mark_restore (WEdit * edit, int color)
324 if (edit->serialized_bookmarks != NULL)
326 size_t i;
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 /* --------------------------------------------------------------------------------------------- */