Revert second bunch of mhl patches (see 9b9cab58749217101ab16504a77efb301812cfbf)
[midnight-commander.git] / edit / bookmark.c
blobbb5c6b1f45dbe46b71b3f930040be2db3addbca3
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>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include <mhl/memory.h>
37 #include "../src/global.h"
39 #include "edit.h"
40 #include "edit-widget.h"
43 /* note, if there is more than one bookmark on a line, then they are
44 appended after each other and the last one is always the one found
45 by book_mark_found() i.e. last in is the one seen */
47 static inline struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
49 (void) edit;
51 if (p->next)
52 while (p->next->line == p->line)
53 p = p->next;
54 return p;
57 /* returns the first bookmark on or before this line */
58 struct _book_mark *book_mark_find (WEdit * edit, int line)
60 struct _book_mark *p;
61 if (!edit->book_mark) {
62 /* must have an imaginary top bookmark at line -1 to make things less complicated */
63 edit->book_mark = g_malloc0 (sizeof (struct _book_mark));
64 edit->book_mark->line = -1;
65 return edit->book_mark;
67 for (p = edit->book_mark; p; p = p->next) {
68 if (p->line > line)
69 break; /* gone past it going downward */
70 if (p->line <= line) {
71 if (p->next) {
72 if (p->next->line > line) {
73 edit->book_mark = p;
74 return double_marks (edit, p);
76 } else {
77 edit->book_mark = p;
78 return double_marks (edit, p);
82 for (p = edit->book_mark; p; p = p->prev) {
83 if (p->next)
84 if (p->next->line <= line)
85 break; /* gone past it going upward */
86 if (p->line <= line) {
87 if (p->next) {
88 if (p->next->line > line) {
89 edit->book_mark = p;
90 return double_marks (edit, p);
92 } else {
93 edit->book_mark = p;
94 return double_marks (edit, p);
98 return 0; /* can't get here */
101 /* returns true if a bookmark exists at this line of color c */
102 int book_mark_query_color (WEdit * edit, int line, int c)
104 struct _book_mark *p;
105 if (!edit->book_mark)
106 return 0;
107 for (p = book_mark_find (edit, line); p; p = p->prev) {
108 if (p->line != line)
109 return 0;
110 if (p->c == c)
111 return 1;
113 return 0;
116 /* insert a bookmark at this line */
117 void
118 book_mark_insert (WEdit *edit, int line, int c)
120 struct _book_mark *p, *q;
121 p = book_mark_find (edit, line);
122 #if 0
123 if (p->line == line) {
124 /* already exists, so just change the color */
125 if (p->c != c) {
126 edit->force |= REDRAW_LINE;
127 p->c = c;
129 return;
131 #endif
132 edit->force |= REDRAW_LINE;
133 /* create list entry */
134 q = g_malloc0 (sizeof (struct _book_mark));
135 q->line = line;
136 q->c = c;
137 q->next = p->next;
138 /* insert into list */
139 q->prev = p;
140 if (p->next)
141 p->next->prev = q;
142 p->next = q;
145 /* remove a bookmark if there is one at this line matching this color - c of -1 clear all */
146 /* returns non-zero on not-found */
147 int book_mark_clear (WEdit * edit, int line, int c)
149 struct _book_mark *p, *q;
150 int r = 1;
151 if (!edit->book_mark)
152 return r;
153 for (p = book_mark_find (edit, line); p; p = q) {
154 q = p->prev;
155 if (p->line == line && (p->c == c || c == -1)) {
156 r = 0;
157 edit->force |= REDRAW_LINE;
158 edit->book_mark = p->prev;
159 p->prev->next = p->next;
160 if (p->next)
161 p->next->prev = p->prev;
162 mhl_mem_free (p);
163 break;
166 /* if there is only our dummy book mark left, clear it for speed */
167 if (edit->book_mark->line == -1 && !edit->book_mark->next) {
168 mhl_mem_free (edit->book_mark);
169 edit->book_mark = 0;
171 return r;
174 /* clear all bookmarks matching this color, if c is -1 clears all */
175 void book_mark_flush (WEdit * edit, int c)
177 struct _book_mark *p, *q;
178 if (!edit->book_mark)
179 return;
180 edit->force |= REDRAW_PAGE;
181 while (edit->book_mark->prev)
182 edit->book_mark = edit->book_mark->prev;
183 for (q = edit->book_mark->next; q; q = p) {
184 p = q->next;
185 if (q->c == c || c == -1) {
186 q->prev->next = q->next;
187 if (p)
188 p->prev = q->prev;
189 mhl_mem_free (q);
192 if (!edit->book_mark->next) {
193 mhl_mem_free (edit->book_mark);
194 edit->book_mark = 0;
198 /* shift down bookmarks after this line */
199 void book_mark_inc (WEdit * edit, int line)
201 if (edit->book_mark) {
202 struct _book_mark *p;
203 p = book_mark_find (edit, line);
204 for (p = p->next; p; p = p->next) {
205 p->line++;
210 /* shift up bookmarks after this line */
211 void book_mark_dec (WEdit * edit, int line)
213 if (edit->book_mark) {
214 struct _book_mark *p;
215 p = book_mark_find (edit, line);
216 for (p = p->next; p; p = p->next) {
217 p->line--;