2 textbuffer.c : Text buffer handling
4 Copyright (C) 1999-2001 Timo Sirainen
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.
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.
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
21 #define G_LOG_DOMAIN "TextBuffer"
27 #include "textbuffer.h"
33 #define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
35 static GMemChunk *buffer_chunk, *line_chunk, *text_chunk;
37 TEXT_BUFFER_REC *textbuffer_create(void)
39 TEXT_BUFFER_REC *buffer;
41 buffer = g_mem_chunk_alloc0(buffer_chunk);
42 buffer->last_eol = TRUE;
46 void textbuffer_destroy(TEXT_BUFFER_REC *buffer)
48 g_return_if_fail(buffer != NULL);
50 textbuffer_remove_all_lines(buffer);
51 g_mem_chunk_free(buffer_chunk, buffer);
54 static TEXT_CHUNK_REC *text_chunk_find(TEXT_BUFFER_REC *buffer,
55 const unsigned char *data)
59 for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next) {
60 TEXT_CHUNK_REC *rec = tmp->data;
62 if (data >= rec->buffer &&
63 data < rec->buffer+sizeof(rec->buffer))
70 #define mark_temp_eol(chunk) G_STMT_START { \
71 (chunk)->buffer[(chunk)->pos] = 0; \
72 (chunk)->buffer[(chunk)->pos+1] = LINE_CMD_EOL; \
75 static TEXT_CHUNK_REC *text_chunk_create(TEXT_BUFFER_REC *buffer)
78 unsigned char *buf, *ptr, **pptr;
80 rec = g_mem_chunk_alloc(text_chunk);
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;
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"
94 ptr = rec->buffer; pptr = &ptr;
95 memcpy(buf, pptr, sizeof(unsigned char *));
101 buffer->cur_text = rec;
102 buffer->text_chunks = g_slist_append(buffer->text_chunks, rec);
106 static void text_chunk_destroy(TEXT_BUFFER_REC *buffer, TEXT_CHUNK_REC *chunk)
108 buffer->text_chunks = g_slist_remove(buffer->text_chunks, chunk);
109 g_mem_chunk_free(text_chunk, chunk);
112 static void text_chunk_line_free(TEXT_BUFFER_REC *buffer, LINE_REC *line)
114 TEXT_CHUNK_REC *chunk;
115 const unsigned char *text;
116 unsigned char *tmp = NULL;
118 for (text = line->text;; text++) {
123 if (*text == LINE_CMD_CONTINUE || *text == LINE_CMD_EOL) {
124 if (*text == LINE_CMD_CONTINUE)
125 memcpy(&tmp, text+1, sizeof(char *));
127 /* free the previous block */
128 chunk = text_chunk_find(buffer, text);
129 if (--chunk->refcount == 0) {
130 if (buffer->cur_text == chunk)
133 text_chunk_destroy(buffer, chunk);
136 if (*text == LINE_CMD_EOL)
144 static void text_chunk_append(TEXT_BUFFER_REC *buffer,
145 const unsigned char *data, int len)
147 TEXT_CHUNK_REC *chunk;
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 */
159 memcpy(chunk->buffer + chunk->pos, data, left);
162 chunk = text_chunk_create(buffer);
164 len -= left; data += left;
167 memcpy(chunk->buffer + chunk->pos, data, len);
170 mark_temp_eol(chunk);
173 static LINE_REC *textbuffer_line_create(TEXT_BUFFER_REC *buffer)
177 if (buffer->cur_text == NULL)
178 text_chunk_create(buffer);
180 rec = g_mem_chunk_alloc(line_chunk);
182 rec->text = buffer->cur_text->buffer + buffer->cur_text->pos;
184 buffer->cur_text->refcount++;
188 static LINE_REC *textbuffer_line_insert(TEXT_BUFFER_REC *buffer,
193 line = textbuffer_line_create(buffer);
196 line->next = buffer->first_line;
197 if (buffer->first_line != NULL)
198 buffer->first_line->prev = line;
199 buffer->first_line = line;
201 line->next = prev->next;
202 if (line->next != NULL)
203 line->next->prev = line;
207 if (prev == buffer->cur_line)
208 buffer->cur_line = line;
209 buffer->lines_count++;
214 void textbuffer_line_ref(LINE_REC *line)
216 g_return_if_fail(line != NULL);
218 if (++line->refcount == 255)
219 g_error("line reference counter wrapped - shouldn't happen");
222 void textbuffer_line_unref(TEXT_BUFFER_REC *buffer, LINE_REC *line)
224 g_return_if_fail(buffer != NULL);
225 g_return_if_fail(line != NULL);
227 if (--line->refcount == 0) {
228 text_chunk_line_free(buffer, line);
229 g_mem_chunk_free(line_chunk, line);
233 void textbuffer_line_unref_list(TEXT_BUFFER_REC *buffer, GList *list)
235 g_return_if_fail(buffer != NULL);
237 while (list != NULL) {
238 if (list->data != NULL)
239 textbuffer_line_unref(buffer, list->data);
244 LINE_REC *textbuffer_line_last(TEXT_BUFFER_REC *buffer)
248 line = buffer->cur_line;
250 while (line->next != NULL)
256 int textbuffer_line_exists_after(LINE_REC *line, LINE_REC *search)
258 while (line != NULL) {
266 LINE_REC *textbuffer_append(TEXT_BUFFER_REC *buffer,
267 const unsigned char *data, int len,
270 return textbuffer_insert(buffer, buffer->cur_line, data, len, info);
273 LINE_REC *textbuffer_insert(TEXT_BUFFER_REC *buffer, LINE_REC *insert_after,
274 const unsigned char *data, int len,
279 g_return_val_if_fail(buffer != NULL, NULL);
280 g_return_val_if_fail(data != NULL, NULL);
285 line = !buffer->last_eol ? insert_after :
286 textbuffer_line_insert(buffer, insert_after);
289 memcpy(&line->info, info, sizeof(line->info));
291 text_chunk_append(buffer, data, len);
293 buffer->last_eol = len >= 2 &&
294 data[len-2] == 0 && data[len-1] == LINE_CMD_EOL;
299 void textbuffer_remove(TEXT_BUFFER_REC *buffer, LINE_REC *line)
301 g_return_if_fail(buffer != NULL);
302 g_return_if_fail(line != NULL);
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;
311 if (buffer->cur_line == line) {
312 buffer->cur_line = line->next != NULL ?
313 line->next : line->prev;
316 line->prev = line->next = NULL;
318 buffer->lines_count--;
319 textbuffer_line_unref(buffer, line);
322 /* Removes all lines from buffer, ignoring reference counters */
323 void textbuffer_remove_all_lines(TEXT_BUFFER_REC *buffer)
328 g_return_if_fail(buffer != NULL);
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;
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;
340 buffer->lines_count = 0;
342 buffer->cur_line = NULL;
343 buffer->cur_text = NULL;
345 buffer->last_eol = TRUE;
348 static void set_color(GString *str, int cmd, int *last_fg, int *last_bg)
350 if (cmd & LINE_COLOR_DEFAULT) {
351 g_string_sprintfa(str, "\004%c", FORMAT_STYLE_DEFAULTS);
353 /* need to reset the fg/bg color */
354 if (cmd & LINE_COLOR_BG) {
356 if (*last_fg != -1) {
357 g_string_sprintfa(str, "\004%c%c",
359 FORMAT_COLOR_NOCHANGE);
363 if (*last_bg != -1) {
364 g_string_sprintfa(str, "\004%c%c",
365 FORMAT_COLOR_NOCHANGE,
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);
378 /* change background color */
379 *last_bg = (cmd & 0x0f)+'0';
380 g_string_sprintfa(str, "\004%c%c",
381 FORMAT_COLOR_NOCHANGE, *last_bg);
385 void textbuffer_line2text(LINE_REC *line, int coloring, GString *str)
387 unsigned char cmd, *ptr, *tmp;
388 int last_fg, last_bg;
390 g_return_if_fail(line != NULL);
391 g_return_if_fail(str != NULL);
393 g_string_truncate(str, 0);
395 last_fg = last_bg = -1;
396 for (ptr = line->text;;) {
398 g_string_append_c(str, (char) *ptr);
407 if (cmd == LINE_CMD_EOL || cmd == LINE_CMD_FORMAT) {
412 if (cmd == LINE_CMD_CONTINUE) {
413 /* line continues in another address.. */
414 memcpy(&tmp, ptr, sizeof(unsigned char *));
420 /* no colors, skip coloring commands */
424 if ((cmd & 0x80) == 0) {
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);
431 case LINE_CMD_REVERSE:
432 g_string_append_c(str, 22);
434 case LINE_CMD_COLOR0:
435 g_string_sprintfa(str, "\004%c%c",
436 '0', FORMAT_COLOR_NOCHANGE);
438 case LINE_CMD_INDENT:
440 case LINE_CMD_INDENT_FUNC:
441 ptr += sizeof(void *);
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)
455 LINE_REC *line, *pre_line;
458 int i, match_after, line_matched;
460 g_return_val_if_fail(buffer != NULL, NULL);
461 g_return_val_if_fail(text != NULL, NULL);
465 int flags = REG_EXTENDED | REG_NOSUB |
466 (case_sensitive ? 0 : REG_ICASE);
467 if (regcomp(&preg, text, flags) != 0)
474 matches = NULL; match_after = 0;
475 str = g_string_new(NULL);
477 line = startline != NULL ? startline : buffer->first_line;
479 for (; line != NULL; line = line->next) {
480 if ((line->info.level & level) == 0 ||
481 (line->info.level & nolevel) != 0)
485 /* no search word, everything matches */
486 textbuffer_line_ref(line);
487 matches = g_list_append(matches, line);
491 textbuffer_line2text(line, FALSE, str);
495 regexp ? regexec(&preg, str->str, 0, NULL, 0) == 0 :
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;
501 /* add the -before lines */
503 for (i = 0; i < before; i++) {
504 if (pre_line->prev == NULL ||
505 g_list_find(matches, pre_line->prev) != NULL)
507 pre_line = pre_line->prev;
510 for (; pre_line != line; pre_line = pre_line->next) {
511 textbuffer_line_ref(pre_line);
512 matches = g_list_append(matches, pre_line);
518 if (line_matched || match_after > 0) {
520 textbuffer_line_ref(line);
521 matches = g_list_append(matches, line);
523 if ((!line_matched && --match_after == 0) ||
524 (line_matched && match_after == 0 && before > 0))
525 matches = g_list_append(matches, NULL);
529 if (regexp) regfree(&preg);
531 g_string_free(str, TRUE);
535 void textbuffer_init(void)
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);
546 void textbuffer_deinit(void)
548 g_mem_chunk_destroy(buffer_chunk);
549 g_mem_chunk_destroy(line_chunk);
550 g_mem_chunk_destroy(text_chunk);