ru.po: Heavily updated translation
[midnight-commander.git] / gtkedit / bookmark.c
blob1ed660449759d9fe8a478c877d67bcad03081777
1 /* editor book mark handling
3 Copyright (C) 1996, 1997 the Free Software Foundation
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., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
24 #include <config.h>
25 #include "edit.h"
26 #if defined (HAVE_MAD) && ! defined (MIDNIGHT) && ! defined (GTK)
27 #include "mad.h"
28 #endif
30 /* note, if there is more than one bookmark on a line, then they are
31 appended after each other and the last one is always the one found
32 by book_mark_found() i.e. last in is the one seen */
34 static inline struct _book_mark *double_marks (WEdit * edit, struct _book_mark *p)
36 if (p->next)
37 while (p->next->line == p->line)
38 p = p->next;
39 return p;
42 /* returns the first bookmark on or before this line */
43 struct _book_mark *book_mark_find (WEdit * edit, int line)
45 struct _book_mark *p;
46 if (!edit->book_mark) {
47 /* must have an imaginary top bookmark at line -1 to make things less complicated */
48 edit->book_mark = malloc (sizeof (struct _book_mark));
49 memset (edit->book_mark, 0, sizeof (struct _book_mark));
50 edit->book_mark->line = -1;
51 return edit->book_mark;
53 for (p = edit->book_mark; p; p = p->next) {
54 if (p->line > line)
55 break; /* gone past it going downward */
56 if (p->line <= line) {
57 if (p->next) {
58 if (p->next->line > line) {
59 edit->book_mark = p;
60 return double_marks (edit, p);
62 } else {
63 edit->book_mark = p;
64 return double_marks (edit, p);
68 for (p = edit->book_mark; p; p = p->prev) {
69 if (p->next)
70 if (p->next->line <= line)
71 break; /* gone past it going upward */
72 if (p->line <= line) {
73 if (p->next) {
74 if (p->next->line > line) {
75 edit->book_mark = p;
76 return double_marks (edit, p);
78 } else {
79 edit->book_mark = p;
80 return double_marks (edit, p);
84 return 0; /* can't get here */
87 /* returns true if a bookmark exists at this line of colour c */
88 int book_mark_query_color (WEdit * edit, int line, int c)
90 struct _book_mark *p;
91 if (!edit->book_mark)
92 return 0;
93 for (p = book_mark_find (edit, line); p; p = p->prev) {
94 if (p->line != line)
95 return 0;
96 if (p->c == c)
97 return 1;
99 return 0;
102 /* returns the number of bookmarks at this line and a list of their colours in c
103 up to a maximum of 8 colours */
104 int book_mark_query_all (WEdit * edit, int line, int *c)
106 int i;
107 struct _book_mark *p;
108 if (!edit->book_mark)
109 return 0;
110 for (i = 0, p = book_mark_find (edit, line); p && i < 8; p = p->prev, i++) {
111 if (p->line != line)
112 return i;
113 c[i] = p->c;
115 return i;
118 /* insert a bookmark at this line */
119 void book_mark_insert (WEdit * edit, int line, int c)
121 struct _book_mark *p, *q;
122 p = book_mark_find (edit, line);
123 #if 0
124 if (p->line == line) { /* already exists, so just change the colour */
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 = malloc (sizeof (struct _book_mark));
135 memset (q, 0, sizeof (struct _book_mark));
136 q->line = line;
137 q->c = c;
138 q->next = p->next;
139 /* insert into list */
140 q->prev = p;
141 if (p->next)
142 p->next->prev = q;
143 p->next = q;
144 #if !defined (GTK) && !defined (MIDNIGHT)
145 render_scrollbar (edit->widget->vert_scrollbar);
146 #endif
149 /* remove a bookmark if there is one at this line matching this colour - c of -1 clear all */
150 /* returns non-zero on not-found */
151 int book_mark_clear (WEdit * edit, int line, int c)
153 struct _book_mark *p, *q;
154 int r = 1;
155 int rend = 0;
156 if (!edit->book_mark)
157 return r;
158 for (p = book_mark_find (edit, line); p; p = q) {
159 q = p->prev;
160 if (p->line == line && (p->c == c || c == -1)) {
161 r = 0;
162 edit->force |= REDRAW_LINE;
163 edit->book_mark = p->prev;
164 p->prev->next = p->next;
165 if (p->next)
166 p->next->prev = p->prev;
167 rend = 1;
168 free (p);
169 break;
172 /* if there is only our dummy book mark left, clear it for speed */
173 if (edit->book_mark->line == -1 && !edit->book_mark->next) {
174 free (edit->book_mark);
175 edit->book_mark = 0;
177 #if !defined (GTK) && !defined (MIDNIGHT)
178 if (rend)
179 render_scrollbar (edit->widget->vert_scrollbar);
180 #endif
181 return r;
184 /* clear all bookmarks matching this colour, if c is -1 clears all */
185 void book_mark_flush (WEdit * edit, int c)
187 struct _book_mark *p, *q;
188 int rend = 0;
189 if (!edit->book_mark)
190 return;
191 edit->force |= REDRAW_PAGE;
192 while (edit->book_mark->prev)
193 edit->book_mark = edit->book_mark->prev;
194 for (q = edit->book_mark->next; q; q = p) {
195 p = q->next;
196 if (q->c == c || c == -1) {
197 q->prev->next = q->next;
198 if (p)
199 p->prev = q->prev;
200 rend = 1;
201 free (q);
204 if (!edit->book_mark->next) {
205 free (edit->book_mark);
206 edit->book_mark = 0;
208 #if !defined (GTK) && !defined (MIDNIGHT)
209 if (rend)
210 render_scrollbar (edit->widget->vert_scrollbar);
211 #endif
214 /* shift down bookmarks after this line */
215 void book_mark_inc (WEdit * edit, int line)
217 int rend = 0;
218 if (edit->book_mark) {
219 struct _book_mark *p;
220 p = book_mark_find (edit, line);
221 for (p = p->next; p; p = p->next) {
222 p->line++;
223 rend = 1;
226 #if !defined (GTK) && !defined (MIDNIGHT)
227 if (rend)
228 render_scrollbar (edit->widget->vert_scrollbar);
229 #endif
232 /* shift up bookmarks after this line */
233 void book_mark_dec (WEdit * edit, int line)
235 int rend = 0;
236 if (edit->book_mark) {
237 struct _book_mark *p;
238 p = book_mark_find (edit, line);
239 for (p = p->next; p; p = p->next) {
240 p->line--;
241 rend = 1;
244 #if !defined (GTK) && !defined (MIDNIGHT)
245 if (rend)
246 render_scrollbar (edit->widget->vert_scrollbar);
247 #endif