First bunch of mhl_mem_free removal patches
[midnight-commander.git] / edit / bookmark.c
blob403b93c3396a988899db5e2731214f4d7a62ec51
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 #include <config.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
36 #include "../src/global.h"
38 #include "edit.h"
39 #include "edit-widget.h"
42 /* note, if there is more than one bookmark on a line, then they are
43 appended after each other and the last one is always the one found
44 by book_mark_found() i.e. last in is the one seen */
46 static inline struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
48 (void) edit;
50 if (p->next)
51 while (p->next->line == p->line)
52 p = p->next;
53 return p;
56 /* returns the first bookmark on or before this line */
57 struct _book_mark *book_mark_find (WEdit * edit, int line)
59 struct _book_mark *p;
60 if (!edit->book_mark) {
61 /* must have an imaginary top bookmark at line -1 to make things less complicated */
62 edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
63 edit->book_mark->line = -1;
64 return edit->book_mark;
66 for (p = edit->book_mark; p; p = p->next) {
67 if (p->line > line)
68 break; /* gone past it going downward */
69 if (p->line <= line) {
70 if (p->next) {
71 if (p->next->line > line) {
72 edit->book_mark = p;
73 return double_marks (edit, p);
75 } else {
76 edit->book_mark = p;
77 return double_marks (edit, p);
81 for (p = edit->book_mark; p; p = p->prev) {
82 if (p->next)
83 if (p->next->line <= line)
84 break; /* gone past it going upward */
85 if (p->line <= line) {
86 if (p->next) {
87 if (p->next->line > line) {
88 edit->book_mark = p;
89 return double_marks (edit, p);
91 } else {
92 edit->book_mark = p;
93 return double_marks (edit, p);
97 return 0; /* can't get here */
100 /* returns true if a bookmark exists at this line of color c */
101 int book_mark_query_color (WEdit * edit, int line, int c)
103 struct _book_mark *p;
104 if (!edit->book_mark)
105 return 0;
106 for (p = book_mark_find (edit, line); p; p = p->prev) {
107 if (p->line != line)
108 return 0;
109 if (p->c == c)
110 return 1;
112 return 0;
115 /* insert a bookmark at this line */
116 void
117 book_mark_insert (WEdit *edit, int line, int c)
119 struct _book_mark *p, *q;
120 p = book_mark_find (edit, line);
121 #if 0
122 if (p->line == line) {
123 /* already exists, so just change the color */
124 if (p->c != c) {
125 edit->force |= REDRAW_LINE;
126 p->c = c;
128 return;
130 #endif
131 edit->force |= REDRAW_LINE;
132 /* create list entry */
133 q = g_malloc0 (sizeof (struct _book_mark));
134 q->line = line;
135 q->c = c;
136 q->next = p->next;
137 /* insert into list */
138 q->prev = p;
139 if (p->next)
140 p->next->prev = q;
141 p->next = q;
144 /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
145 /* returns non-zero on not-found */
146 int book_mark_clear (WEdit * edit, int line, int c)
148 struct _book_mark *p, *q;
149 int r = 1;
150 if (!edit->book_mark)
151 return r;
152 for (p = book_mark_find (edit, line); p; p = q) {
153 q = p->prev;
154 if (p->line == line && (p->c == c || c == -1)) {
155 r = 0;
156 edit->force |= REDRAW_LINE;
157 edit->book_mark = p->prev;
158 p->prev->next = p->next;
159 if (p->next)
160 p->next->prev = p->prev;
161 g_free (p);
162 break;
165 /* if there is only our dummy book mark left, clear it for speed */
166 if (edit->book_mark->line == -1 && !edit->book_mark->next) {
167 g_free (edit->book_mark);
168 edit->book_mark = 0;
170 return r;
173 /* clear all bookmarks matching this color, if c is -1 clears all */
174 void book_mark_flush (WEdit * edit, int c)
176 struct _book_mark *p, *q;
177 if (!edit->book_mark)
178 return;
179 edit->force |= REDRAW_PAGE;
180 while (edit->book_mark->prev)
181 edit->book_mark = edit->book_mark->prev;
182 for (q = edit->book_mark->next; q; q = p) {
183 p = q->next;
184 if (q->c == c || c == -1) {
185 q->prev->next = q->next;
186 if (p)
187 p->prev = q->prev;
188 g_free (q);
191 if (!edit->book_mark->next) {
192 g_free (edit->book_mark);
193 edit->book_mark = 0;
197 /* shift down bookmarks after this line */
198 void book_mark_inc (WEdit * edit, int line)
200 if (edit->book_mark) {
201 struct _book_mark *p;
202 p = book_mark_find (edit, line);
203 for (p = p->next; p; p = p->next) {
204 p->line++;
209 /* shift up bookmarks after this line */
210 void book_mark_dec (WEdit * edit, int line)
212 if (edit->book_mark) {
213 struct _book_mark *p;
214 p = book_mark_find (edit, line);
215 for (p = p->next; p; p = p->next) {
216 p->line--;