updates.
[silc.git] / apps / irssi / src / fe-text / textbuffer.c
1 /*
2  textbuffer.c : Text buffer handling
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #define G_LOG_DOMAIN "TextBuffer"
22
23 #include "module.h"
24 #include "misc.h"
25 #include "formats.h"
26
27 #include "textbuffer.h"
28
29 #ifdef HAVE_REGEX_H
30 #  include <regex.h>
31 #endif
32
33 #define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
34
35 static GMemChunk *buffer_chunk, *line_chunk, *text_chunk;
36
37 TEXT_BUFFER_REC *textbuffer_create(void)
38 {
39         TEXT_BUFFER_REC *buffer;
40
41         buffer = g_mem_chunk_alloc0(buffer_chunk);
42         buffer->last_eol = TRUE;
43         return buffer;
44 }
45
46 void textbuffer_destroy(TEXT_BUFFER_REC *buffer)
47 {
48         g_return_if_fail(buffer != NULL);
49
50         textbuffer_remove_all_lines(buffer);
51         g_mem_chunk_free(buffer_chunk, buffer);
52 }
53
54 static TEXT_CHUNK_REC *text_chunk_find(TEXT_BUFFER_REC *buffer,
55                                        const unsigned char *data)
56 {
57         GSList *tmp;
58
59         for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next) {
60                 TEXT_CHUNK_REC *rec = tmp->data;
61
62                 if (data >= rec->buffer &&
63                     data < rec->buffer+sizeof(rec->buffer))
64                         return rec;
65         }
66
67         return NULL;
68 }
69
70 #define mark_temp_eol(chunk) G_STMT_START { \
71         (chunk)->buffer[(chunk)->pos] = 0; \
72         (chunk)->buffer[(chunk)->pos+1] = LINE_CMD_EOL; \
73         } G_STMT_END
74
75 static TEXT_CHUNK_REC *text_chunk_create(TEXT_BUFFER_REC *buffer)
76 {
77         TEXT_CHUNK_REC *rec;
78         unsigned char *buf, *ptr, **pptr;
79
80         rec = g_mem_chunk_alloc(text_chunk);
81         rec->pos = 0;
82         rec->refcount = 0;
83
84         if (buffer->cur_line != NULL && buffer->cur_line->text != NULL) {
85                 /* create a link to new block from the old block */
86                 buf = buffer->cur_text->buffer + buffer->cur_text->pos;
87                 *buf++ = 0; *buf++ = (char) LINE_CMD_CONTINUE;
88
89                 /* we want to store pointer to beginning of the new text
90                    block to char* buffer. this probably isn't ANSI-C
91                    compatible, and trying this without the pptr variable
92                    breaks at least NetBSD/Alpha, so don't go "optimize"
93                    it :) */
94                 ptr = rec->buffer; pptr = &ptr;
95                 memcpy(buf, pptr, sizeof(unsigned char *));
96         } else {
97                 /* just to be safe */
98                 mark_temp_eol(rec);
99         }
100
101         buffer->cur_text = rec;
102         buffer->text_chunks = g_slist_append(buffer->text_chunks, rec);
103         return rec;
104 }
105
106 static void text_chunk_destroy(TEXT_BUFFER_REC *buffer, TEXT_CHUNK_REC *chunk)
107 {
108         buffer->text_chunks = g_slist_remove(buffer->text_chunks, chunk);
109         g_mem_chunk_free(text_chunk, chunk);
110 }
111
112 static void text_chunk_line_free(TEXT_BUFFER_REC *buffer, LINE_REC *line)
113 {
114         TEXT_CHUNK_REC *chunk;
115         const unsigned char *text;
116         unsigned char *tmp = NULL;
117
118         for (text = line->text;; text++) {
119                 if (*text != '\0')
120                         continue;
121
122                 text++;
123                 if (*text == LINE_CMD_CONTINUE || *text == LINE_CMD_EOL) {
124                         if (*text == LINE_CMD_CONTINUE)
125                                 memcpy(&tmp, text+1, sizeof(char *));
126
127                         /* free the previous block */
128                         chunk = text_chunk_find(buffer, text);
129                         if (--chunk->refcount == 0) {
130                                 if (buffer->cur_text == chunk)
131                                         chunk->pos = 0;
132                                 else
133                                         text_chunk_destroy(buffer, chunk);
134                         }
135
136                         if (*text == LINE_CMD_EOL)
137                                 break;
138
139                         text = tmp-1;
140                 }
141         }
142 }
143
144 static void text_chunk_append(TEXT_BUFFER_REC *buffer,
145                               const unsigned char *data, int len)
146 {
147         TEXT_CHUNK_REC *chunk;
148         int left;
149
150         if (len == 0)
151                 return;
152
153         chunk = buffer->cur_text;
154         while (chunk->pos + len >= TEXT_CHUNK_USABLE_SIZE) {
155                 left = TEXT_CHUNK_USABLE_SIZE - chunk->pos;
156                 if (left > 0 && data[left-1] == 0)
157                         left--; /* don't split the commands */
158
159                 memcpy(chunk->buffer + chunk->pos, data, left);
160                 chunk->pos += left;
161
162                 chunk = text_chunk_create(buffer);
163                 chunk->refcount++;
164                 len -= left; data += left;
165         }
166
167         memcpy(chunk->buffer + chunk->pos, data, len);
168         chunk->pos += len;
169
170         mark_temp_eol(chunk);
171 }
172
173 static LINE_REC *textbuffer_line_create(TEXT_BUFFER_REC *buffer)
174 {
175         LINE_REC *rec;
176
177         if (buffer->cur_text == NULL)
178                 text_chunk_create(buffer);
179
180         rec = g_mem_chunk_alloc(line_chunk);
181         rec->refcount = 1;
182         rec->text = buffer->cur_text->buffer + buffer->cur_text->pos;
183
184         buffer->cur_text->refcount++;
185         return rec;
186 }
187
188 static LINE_REC *textbuffer_line_insert(TEXT_BUFFER_REC *buffer,
189                                         LINE_REC *prev)
190 {
191         LINE_REC *line;
192
193         line = textbuffer_line_create(buffer);
194         line->prev = prev;
195         if (prev == NULL) {
196                 line->next = buffer->first_line;
197                 if (buffer->first_line != NULL)
198                         buffer->first_line->prev = line;
199                 buffer->first_line = line;
200         } else {
201                 line->next = prev->next;
202                 if (line->next != NULL)
203                         line->next->prev = line;
204                 prev->next = line;
205         }
206
207         if (prev == buffer->cur_line)
208                 buffer->cur_line = line;
209         buffer->lines_count++;
210
211         return line;
212 }
213
214 void textbuffer_line_ref(LINE_REC *line)
215 {
216         g_return_if_fail(line != NULL);
217
218         if (++line->refcount == 255)
219                 g_error("line reference counter wrapped - shouldn't happen");
220 }
221
222 void textbuffer_line_unref(TEXT_BUFFER_REC *buffer, LINE_REC *line)
223 {
224         g_return_if_fail(buffer != NULL);
225         g_return_if_fail(line != NULL);
226
227         if (--line->refcount == 0) {
228                 text_chunk_line_free(buffer, line);
229                 g_mem_chunk_free(line_chunk, line);
230         }
231 }
232
233 void textbuffer_line_unref_list(TEXT_BUFFER_REC *buffer, GList *list)
234 {
235         g_return_if_fail(buffer != NULL);
236
237         while (list != NULL) {
238                 if (list->data != NULL)
239                         textbuffer_line_unref(buffer, list->data);
240                 list = list->next;
241         }
242 }
243
244 LINE_REC *textbuffer_line_last(TEXT_BUFFER_REC *buffer)
245 {
246         LINE_REC *line;
247
248         line = buffer->cur_line;
249         if (line != NULL) {
250                 while (line->next != NULL)
251                         line = line->next;
252         }
253         return line;
254 }
255
256 int textbuffer_line_exists_after(LINE_REC *line, LINE_REC *search)
257 {
258         while (line != NULL) {
259                 if (line == search)
260                         return TRUE;
261                 line = line->next;
262         }
263         return FALSE;
264 }
265
266 LINE_REC *textbuffer_append(TEXT_BUFFER_REC *buffer,
267                             const unsigned char *data, int len,
268                             LINE_INFO_REC *info)
269 {
270         return textbuffer_insert(buffer, buffer->cur_line, data, len, info);
271 }
272
273 LINE_REC *textbuffer_insert(TEXT_BUFFER_REC *buffer, LINE_REC *insert_after,
274                             const unsigned char *data, int len,
275                             LINE_INFO_REC *info)
276 {
277         LINE_REC *line;
278
279         g_return_val_if_fail(buffer != NULL, NULL);
280         g_return_val_if_fail(data != NULL, NULL);
281
282         if (len == 0)
283                 return insert_after;
284
285         line = !buffer->last_eol ? insert_after :
286                 textbuffer_line_insert(buffer, insert_after);
287
288         if (info != NULL)
289                 memcpy(&line->info, info, sizeof(line->info));
290
291         text_chunk_append(buffer, data, len);
292
293         buffer->last_eol = len >= 2 &&
294                 data[len-2] == 0 && data[len-1] == LINE_CMD_EOL;
295
296         return line;
297 }
298
299 void textbuffer_remove(TEXT_BUFFER_REC *buffer, LINE_REC *line)
300 {
301         g_return_if_fail(buffer != NULL);
302         g_return_if_fail(line != NULL);
303
304         if (buffer->first_line == line)
305                 buffer->first_line = line->next;
306         if (line->prev != NULL)
307                 line->prev->next = line->next;
308         if (line->next != NULL)
309                 line->next->prev = line->prev;
310
311         if (buffer->cur_line == line) {
312                 buffer->cur_line = line->next != NULL ?
313                         line->next : line->prev;
314         }
315
316         line->prev = line->next = NULL;
317
318         buffer->lines_count--;
319         textbuffer_line_unref(buffer, line);
320 }
321
322 /* Removes all lines from buffer, ignoring reference counters */
323 void textbuffer_remove_all_lines(TEXT_BUFFER_REC *buffer)
324 {
325         GSList *tmp;
326         LINE_REC *line;
327
328         g_return_if_fail(buffer != NULL);
329
330         for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next)
331                 g_mem_chunk_free(text_chunk, tmp->data);
332         g_slist_free(buffer->text_chunks);
333         buffer->text_chunks = NULL;
334
335         while (buffer->first_line != NULL) {
336                 line = buffer->first_line->next;
337                 g_mem_chunk_free(line_chunk, buffer->first_line);
338                 buffer->first_line = line;
339         }
340         buffer->lines_count = 0;
341
342         buffer->cur_line = NULL;
343         buffer->cur_text = NULL;
344
345         buffer->last_eol = TRUE;
346 }
347
348 static void set_color(GString *str, int cmd, int *last_fg, int *last_bg)
349 {
350         if (cmd & LINE_COLOR_DEFAULT) {
351                 g_string_sprintfa(str, "\004%c", FORMAT_STYLE_DEFAULTS);
352
353                 /* need to reset the fg/bg color */
354                 if (cmd & LINE_COLOR_BG) {
355                         *last_bg = -1;
356                         if (*last_fg != -1) {
357                                 g_string_sprintfa(str, "\004%c%c",
358                                                   *last_fg,
359                                                   FORMAT_COLOR_NOCHANGE);
360                         }
361                 } else {
362                         *last_fg = -1;
363                         if (*last_bg != -1) {
364                                 g_string_sprintfa(str, "\004%c%c",
365                                                   FORMAT_COLOR_NOCHANGE,
366                                                   *last_bg);
367                         }
368                 }
369                 return;
370         }
371
372         if ((cmd & LINE_COLOR_BG) == 0) {
373                 /* change foreground color */
374                 *last_fg = (cmd & 0x0f)+'0';
375                 g_string_sprintfa(str, "\004%c%c", *last_fg,
376                                   FORMAT_COLOR_NOCHANGE);
377         } else {
378                 /* change background color */
379                 *last_bg = (cmd & 0x0f)+'0';
380                 g_string_sprintfa(str, "\004%c%c",
381                                   FORMAT_COLOR_NOCHANGE, *last_bg);
382         }
383 }
384
385 void textbuffer_line2text(LINE_REC *line, int coloring, GString *str)
386 {
387         unsigned char cmd, *ptr, *tmp;
388         int last_fg, last_bg;
389
390         g_return_if_fail(line != NULL);
391         g_return_if_fail(str != NULL);
392
393         g_string_truncate(str, 0);
394
395         last_fg = last_bg = -1;
396         for (ptr = line->text;;) {
397                 if (*ptr != 0) {
398                         g_string_append_c(str, (char) *ptr);
399                         ptr++;
400                         continue;
401                 }
402
403                 ptr++;
404                 cmd = *ptr;
405                 ptr++;
406
407                 if (cmd == LINE_CMD_EOL || cmd == LINE_CMD_FORMAT) {
408                         /* end of line */
409                         break;
410                 }
411
412                 if (cmd == LINE_CMD_CONTINUE) {
413                         /* line continues in another address.. */
414                         memcpy(&tmp, ptr, sizeof(unsigned char *));
415                         ptr = tmp;
416                         continue;
417                 }
418
419                 if (!coloring) {
420                         /* no colors, skip coloring commands */
421                         continue;
422                 }
423
424                 if ((cmd & 0x80) == 0) {
425                         /* set color */
426                         set_color(str, cmd, &last_fg, &last_bg);
427                 } else switch (cmd) {
428                 case LINE_CMD_UNDERLINE:
429                         g_string_append_c(str, 31);
430                         break;
431                 case LINE_CMD_REVERSE:
432                         g_string_append_c(str, 22);
433                         break;
434                 case LINE_CMD_COLOR0:
435                         g_string_sprintfa(str, "\004%c%c",
436                                           '0', FORMAT_COLOR_NOCHANGE);
437                         break;
438                 case LINE_CMD_INDENT:
439                         break;
440                 case LINE_CMD_INDENT_FUNC:
441                         ptr += sizeof(void *);
442                         break;
443                 }
444         }
445 }
446
447 GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
448                             int level, int nolevel, const char *text,
449                             int before, int after,
450                             int regexp, int fullword, int case_sensitive)
451 {
452 #ifdef HAVE_REGEX_H
453         regex_t preg;
454 #endif
455         LINE_REC *line, *pre_line;
456         GList *matches;
457         GString *str;
458         int i, match_after, line_matched;
459
460         g_return_val_if_fail(buffer != NULL, NULL);
461         g_return_val_if_fail(text != NULL, NULL);
462
463         if (regexp) {
464 #ifdef HAVE_REGEX_H
465                 int flags = REG_EXTENDED | REG_NOSUB |
466                         (case_sensitive ? 0 : REG_ICASE);
467                 if (regcomp(&preg, text, flags) != 0)
468                         return NULL;
469 #else
470                 return NULL;
471 #endif
472         }
473
474         matches = NULL; match_after = 0;
475         str = g_string_new(NULL);
476
477         line = startline != NULL ? startline : buffer->first_line;
478
479         for (; line != NULL; line = line->next) {
480                 if ((line->info.level & level) == 0 ||
481                     (line->info.level & nolevel) != 0)
482                         continue;
483
484                 if (*text == '\0') {
485                         /* no search word, everything matches */
486                         textbuffer_line_ref(line);
487                         matches = g_list_append(matches, line);
488                         continue;
489                 }
490
491                 textbuffer_line2text(line, FALSE, str);
492
493                 line_matched =
494 #ifdef HAVE_REGEX_H
495                         regexp ? regexec(&preg, str->str, 0, NULL, 0) == 0 :
496 #endif
497                         fullword ? strstr_full_case(str->str, text, !case_sensitive) != NULL :
498                         case_sensitive ? strstr(str->str, text) != NULL :
499                         stristr(str->str, text) != NULL;
500                 if (line_matched) {
501                         /* add the -before lines */
502                         pre_line = line;
503                         for (i = 0; i < before; i++) {
504                                 if (pre_line->prev == NULL ||
505                                     g_list_find(matches, pre_line->prev) != NULL)
506                                         break;
507                                 pre_line = pre_line->prev;
508                         }
509
510                         for (; pre_line != line; pre_line = pre_line->next) {
511                                 textbuffer_line_ref(pre_line);
512                                 matches = g_list_append(matches, pre_line);
513                         }
514
515                         match_after = after;
516                 }
517
518                 if (line_matched || match_after > 0) {
519                         /* matched */
520                         textbuffer_line_ref(line);
521                         matches = g_list_append(matches, line);
522
523                         if ((!line_matched && --match_after == 0) ||
524                             (line_matched && match_after == 0 && before > 0))
525                                 matches = g_list_append(matches, NULL);
526                 }
527         }
528 #ifdef HAVE_REGEX_H
529         if (regexp) regfree(&preg);
530 #endif
531         g_string_free(str, TRUE);
532         return matches;
533 }
534
535 void textbuffer_init(void)
536 {
537         buffer_chunk = g_mem_chunk_new("text buffer chunk",
538                                        sizeof(TEXT_BUFFER_REC),
539                                        sizeof(TEXT_BUFFER_REC)*32, G_ALLOC_AND_FREE);
540         line_chunk = g_mem_chunk_new("line chunk", sizeof(LINE_REC),
541                                      sizeof(LINE_REC)*1024, G_ALLOC_AND_FREE);
542         text_chunk = g_mem_chunk_new("text chunk", sizeof(TEXT_CHUNK_REC),
543                                      sizeof(TEXT_CHUNK_REC)*32, G_ALLOC_AND_FREE);
544 }
545
546 void textbuffer_deinit(void)
547 {
548         g_mem_chunk_destroy(buffer_chunk);
549         g_mem_chunk_destroy(line_chunk);
550         g_mem_chunk_destroy(text_chunk);
551 }