2001-04-14 18:24:56 -04:00
|
|
|
/*
|
|
|
|
textbuffer.c : Text buffer handling
|
|
|
|
|
|
|
|
Copyright (C) 1999-2001 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-04-14 18:24:56 -04:00
|
|
|
*/
|
|
|
|
|
2002-05-13 13:07:37 -04:00
|
|
|
#define G_LOG_DOMAIN "TextBuffer"
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
#include "module.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "formats.h"
|
2014-01-08 09:03:25 -05:00
|
|
|
#include "utf8.h"
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
#include "textbuffer.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
# include <regex.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
|
|
|
|
|
|
|
|
TEXT_BUFFER_REC *textbuffer_create(void)
|
|
|
|
{
|
|
|
|
TEXT_BUFFER_REC *buffer;
|
|
|
|
|
2009-01-08 07:39:11 -05:00
|
|
|
buffer = g_slice_new0(TEXT_BUFFER_REC);
|
2001-04-14 18:24:56 -04:00
|
|
|
buffer->last_eol = TRUE;
|
2014-01-09 09:20:29 -05:00
|
|
|
buffer->last_fg = -1;
|
|
|
|
buffer->last_bg = -1;
|
2001-04-14 18:24:56 -04:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void textbuffer_destroy(TEXT_BUFFER_REC *buffer)
|
|
|
|
{
|
2001-04-16 12:46:34 -04:00
|
|
|
g_return_if_fail(buffer != NULL);
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
textbuffer_remove_all_lines(buffer);
|
2009-01-08 07:39:11 -05:00
|
|
|
g_slice_free(TEXT_BUFFER_REC, buffer);
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static TEXT_CHUNK_REC *text_chunk_find(TEXT_BUFFER_REC *buffer,
|
|
|
|
const unsigned char *data)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next) {
|
|
|
|
TEXT_CHUNK_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (data >= rec->buffer &&
|
|
|
|
data < rec->buffer+sizeof(rec->buffer))
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define mark_temp_eol(chunk) G_STMT_START { \
|
|
|
|
(chunk)->buffer[(chunk)->pos] = 0; \
|
|
|
|
(chunk)->buffer[(chunk)->pos+1] = LINE_CMD_EOL; \
|
|
|
|
} G_STMT_END
|
|
|
|
|
|
|
|
static TEXT_CHUNK_REC *text_chunk_create(TEXT_BUFFER_REC *buffer)
|
|
|
|
{
|
|
|
|
TEXT_CHUNK_REC *rec;
|
2002-02-01 15:14:30 -05:00
|
|
|
unsigned char *buf, *ptr, **pptr;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2009-01-08 07:39:11 -05:00
|
|
|
rec = g_slice_new(TEXT_CHUNK_REC);
|
2001-04-14 18:24:56 -04:00
|
|
|
rec->pos = 0;
|
|
|
|
rec->refcount = 0;
|
|
|
|
|
|
|
|
if (buffer->cur_line != NULL && buffer->cur_line->text != NULL) {
|
|
|
|
/* create a link to new block from the old block */
|
|
|
|
buf = buffer->cur_text->buffer + buffer->cur_text->pos;
|
|
|
|
*buf++ = 0; *buf++ = (char) LINE_CMD_CONTINUE;
|
|
|
|
|
|
|
|
/* we want to store pointer to beginning of the new text
|
|
|
|
block to char* buffer. this probably isn't ANSI-C
|
|
|
|
compatible, and trying this without the pptr variable
|
|
|
|
breaks at least NetBSD/Alpha, so don't go "optimize"
|
|
|
|
it :) */
|
|
|
|
ptr = rec->buffer; pptr = &ptr;
|
2002-02-01 15:14:30 -05:00
|
|
|
memcpy(buf, pptr, sizeof(unsigned char *));
|
2001-04-14 18:24:56 -04:00
|
|
|
} else {
|
|
|
|
/* just to be safe */
|
|
|
|
mark_temp_eol(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->cur_text = rec;
|
|
|
|
buffer->text_chunks = g_slist_append(buffer->text_chunks, rec);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void text_chunk_destroy(TEXT_BUFFER_REC *buffer, TEXT_CHUNK_REC *chunk)
|
|
|
|
{
|
|
|
|
buffer->text_chunks = g_slist_remove(buffer->text_chunks, chunk);
|
2009-01-08 07:39:11 -05:00
|
|
|
g_slice_free(TEXT_CHUNK_REC, chunk);
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void text_chunk_line_free(TEXT_BUFFER_REC *buffer, LINE_REC *line)
|
|
|
|
{
|
|
|
|
TEXT_CHUNK_REC *chunk;
|
|
|
|
const unsigned char *text;
|
2002-12-30 18:10:57 -05:00
|
|
|
unsigned char cmd, *tmp = NULL;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
for (text = line->text;; text++) {
|
|
|
|
if (*text != '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
text++;
|
2002-12-30 18:10:57 -05:00
|
|
|
cmd = *text;
|
|
|
|
if (cmd == LINE_CMD_CONTINUE || cmd == LINE_CMD_EOL) {
|
|
|
|
if (cmd == LINE_CMD_CONTINUE)
|
2001-04-14 18:24:56 -04:00
|
|
|
memcpy(&tmp, text+1, sizeof(char *));
|
|
|
|
|
|
|
|
/* free the previous block */
|
|
|
|
chunk = text_chunk_find(buffer, text);
|
|
|
|
if (--chunk->refcount == 0) {
|
|
|
|
if (buffer->cur_text == chunk)
|
|
|
|
chunk->pos = 0;
|
|
|
|
else
|
|
|
|
text_chunk_destroy(buffer, chunk);
|
|
|
|
}
|
|
|
|
|
2002-12-30 18:10:57 -05:00
|
|
|
if (cmd == LINE_CMD_EOL)
|
2001-04-14 18:24:56 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
text = tmp-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void text_chunk_append(TEXT_BUFFER_REC *buffer,
|
2002-02-01 15:14:30 -05:00
|
|
|
const unsigned char *data, int len)
|
2001-04-14 18:24:56 -04:00
|
|
|
{
|
|
|
|
TEXT_CHUNK_REC *chunk;
|
|
|
|
int left;
|
2014-01-09 09:20:29 -05:00
|
|
|
int i;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
chunk = buffer->cur_text;
|
|
|
|
while (chunk->pos + len >= TEXT_CHUNK_USABLE_SIZE) {
|
|
|
|
left = TEXT_CHUNK_USABLE_SIZE - chunk->pos;
|
2014-01-08 09:03:25 -05:00
|
|
|
|
|
|
|
/* don't split utf-8 character. (assume we can split non-utf8 anywhere.) */
|
|
|
|
if (left < len && !is_utf8_leading(data[left])) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < 4 && left >= i; i++)
|
|
|
|
if (is_utf8_leading(data[left - i])) {
|
|
|
|
left -= i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 09:20:29 -05:00
|
|
|
for (i = 5; i > 0; --i) {
|
|
|
|
if (left >= i && data[left-i] == 0) {
|
|
|
|
left -= i; /* don't split the commands */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
memcpy(chunk->buffer + chunk->pos, data, left);
|
|
|
|
chunk->pos += left;
|
|
|
|
|
|
|
|
chunk = text_chunk_create(buffer);
|
|
|
|
chunk->refcount++;
|
|
|
|
len -= left; data += left;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(chunk->buffer + chunk->pos, data, len);
|
|
|
|
chunk->pos += len;
|
|
|
|
|
|
|
|
mark_temp_eol(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LINE_REC *textbuffer_line_create(TEXT_BUFFER_REC *buffer)
|
|
|
|
{
|
|
|
|
LINE_REC *rec;
|
|
|
|
|
|
|
|
if (buffer->cur_text == NULL)
|
|
|
|
text_chunk_create(buffer);
|
|
|
|
|
2009-01-08 07:39:11 -05:00
|
|
|
rec = g_slice_new(LINE_REC);
|
2001-04-14 18:24:56 -04:00
|
|
|
rec->text = buffer->cur_text->buffer + buffer->cur_text->pos;
|
|
|
|
|
|
|
|
buffer->cur_text->refcount++;
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LINE_REC *textbuffer_line_insert(TEXT_BUFFER_REC *buffer,
|
|
|
|
LINE_REC *prev)
|
|
|
|
{
|
|
|
|
LINE_REC *line;
|
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
line = textbuffer_line_create(buffer);
|
|
|
|
line->prev = prev;
|
|
|
|
if (prev == NULL) {
|
|
|
|
line->next = buffer->first_line;
|
|
|
|
if (buffer->first_line != NULL)
|
|
|
|
buffer->first_line->prev = line;
|
|
|
|
buffer->first_line = line;
|
2001-04-14 18:24:56 -04:00
|
|
|
} else {
|
2001-07-12 17:44:01 -04:00
|
|
|
line->next = prev->next;
|
|
|
|
if (line->next != NULL)
|
|
|
|
line->next->prev = line;
|
|
|
|
prev->next = line;
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
2001-07-12 17:44:01 -04:00
|
|
|
|
|
|
|
if (prev == buffer->cur_line)
|
|
|
|
buffer->cur_line = line;
|
2001-04-14 18:24:56 -04:00
|
|
|
buffer->lines_count++;
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
LINE_REC *textbuffer_line_last(TEXT_BUFFER_REC *buffer)
|
|
|
|
{
|
2009-01-04 10:56:54 -05:00
|
|
|
return buffer->cur_line;
|
2001-07-12 17:44:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int textbuffer_line_exists_after(LINE_REC *line, LINE_REC *search)
|
|
|
|
{
|
|
|
|
while (line != NULL) {
|
|
|
|
if (line == search)
|
|
|
|
return TRUE;
|
|
|
|
line = line->next;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-09 09:20:29 -05:00
|
|
|
#ifdef TERM_TRUECOLOR
|
|
|
|
static void format_24bit_line_color(unsigned char *out, int *pos, int bg, unsigned int color)
|
|
|
|
{
|
|
|
|
unsigned char rgb[] = { color >> 16, color >> 8, color };
|
|
|
|
unsigned char x = bg ? 0x1 : 0;
|
|
|
|
unsigned int i;
|
|
|
|
out[(*pos)++] = LINE_COLOR_24;
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
if (rgb[i] > 0x20)
|
|
|
|
out[(*pos)++] = rgb[i];
|
|
|
|
else {
|
|
|
|
out[(*pos)++] = 0x20 + rgb[i];
|
|
|
|
x |= 0x10 << i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[(*pos)++] = 0x20 + x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-10 08:05:43 -04:00
|
|
|
void textbuffer_line_add_colors(TEXT_BUFFER_REC *buffer, LINE_REC **line,
|
|
|
|
int fg, int bg, int flags)
|
|
|
|
{
|
2014-01-09 09:20:29 -05:00
|
|
|
unsigned char data[22];
|
|
|
|
int pos;
|
2011-05-03 10:40:34 -04:00
|
|
|
|
2014-01-09 09:20:29 -05:00
|
|
|
pos = 0;
|
|
|
|
if (fg != buffer->last_fg
|
|
|
|
|| (flags & GUI_PRINT_FLAG_COLOR_24_FG) != (buffer->last_flags & GUI_PRINT_FLAG_COLOR_24_FG)) {
|
2008-04-16 16:32:36 -04:00
|
|
|
buffer->last_fg = fg;
|
2008-03-10 08:05:43 -04:00
|
|
|
data[pos++] = 0;
|
2014-01-09 09:20:29 -05:00
|
|
|
#ifdef TERM_TRUECOLOR
|
|
|
|
if (flags & GUI_PRINT_FLAG_COLOR_24_FG)
|
|
|
|
format_24bit_line_color(data, &pos, 0, fg);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (fg < 0)
|
|
|
|
data[pos++] = LINE_COLOR_DEFAULT;
|
|
|
|
else if (fg < 16)
|
|
|
|
data[pos++] = fg == 0 ? LINE_CMD_COLOR0 : fg;
|
|
|
|
else if (fg < 256) {
|
|
|
|
data[pos++] = LINE_COLOR_EXT;
|
|
|
|
data[pos++] = fg;
|
|
|
|
}
|
2008-03-10 08:05:43 -04:00
|
|
|
}
|
2014-01-09 09:20:29 -05:00
|
|
|
if (bg != buffer->last_bg
|
|
|
|
|| (flags & GUI_PRINT_FLAG_COLOR_24_BG) != (buffer->last_flags & GUI_PRINT_FLAG_COLOR_24_BG)) {
|
2008-04-16 16:32:36 -04:00
|
|
|
buffer->last_bg = bg;
|
2008-03-10 08:05:43 -04:00
|
|
|
data[pos++] = 0;
|
2014-01-09 09:20:29 -05:00
|
|
|
#ifdef TERM_TRUECOLOR
|
|
|
|
if (flags & GUI_PRINT_FLAG_COLOR_24_BG)
|
|
|
|
format_24bit_line_color(data, &pos, 1, bg);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (bg < 0)
|
|
|
|
data[pos++] = LINE_COLOR_BG | LINE_COLOR_DEFAULT;
|
|
|
|
else if (bg < 16)
|
|
|
|
data[pos++] = LINE_COLOR_BG | bg;
|
|
|
|
else if (bg < 256) {
|
|
|
|
data[pos++] = LINE_COLOR_EXT_BG;
|
|
|
|
data[pos++] = bg;
|
|
|
|
}
|
2008-03-10 08:05:43 -04:00
|
|
|
}
|
|
|
|
|
2008-04-16 16:32:36 -04:00
|
|
|
if ((flags & GUI_PRINT_FLAG_UNDERLINE) != (buffer->last_flags & GUI_PRINT_FLAG_UNDERLINE)) {
|
2008-03-10 08:05:43 -04:00
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_UNDERLINE;
|
|
|
|
}
|
2008-04-16 16:32:36 -04:00
|
|
|
if ((flags & GUI_PRINT_FLAG_REVERSE) != (buffer->last_flags & GUI_PRINT_FLAG_REVERSE)) {
|
2008-03-10 08:05:43 -04:00
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_REVERSE;
|
|
|
|
}
|
2008-11-15 16:51:07 -05:00
|
|
|
if ((flags & GUI_PRINT_FLAG_BLINK) != (buffer->last_flags & GUI_PRINT_FLAG_BLINK)) {
|
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_BLINK;
|
|
|
|
}
|
|
|
|
if ((flags & GUI_PRINT_FLAG_BOLD) != (buffer->last_flags & GUI_PRINT_FLAG_BOLD)) {
|
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_BOLD;
|
|
|
|
}
|
2011-11-06 14:40:25 -05:00
|
|
|
if ((flags & GUI_PRINT_FLAG_ITALIC) != (buffer->last_flags & GUI_PRINT_FLAG_ITALIC)) {
|
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_ITALIC;
|
|
|
|
}
|
2008-03-10 08:05:43 -04:00
|
|
|
if (flags & GUI_PRINT_FLAG_INDENT) {
|
|
|
|
data[pos++] = 0;
|
|
|
|
data[pos++] = LINE_CMD_INDENT;
|
2011-05-03 10:40:34 -04:00
|
|
|
}
|
|
|
|
|
2014-01-09 09:20:29 -05:00
|
|
|
if (pos > 0)
|
2008-03-10 08:05:43 -04:00
|
|
|
*line = textbuffer_insert(buffer, *line, data, pos, NULL);
|
|
|
|
|
2008-04-16 16:32:36 -04:00
|
|
|
buffer->last_flags = flags;
|
2008-03-10 08:05:43 -04:00
|
|
|
}
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
LINE_REC *textbuffer_append(TEXT_BUFFER_REC *buffer,
|
|
|
|
const unsigned char *data, int len,
|
|
|
|
LINE_INFO_REC *info)
|
|
|
|
{
|
|
|
|
return textbuffer_insert(buffer, buffer->cur_line, data, len, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
LINE_REC *textbuffer_insert(TEXT_BUFFER_REC *buffer, LINE_REC *insert_after,
|
|
|
|
const unsigned char *data, int len,
|
|
|
|
LINE_INFO_REC *info)
|
|
|
|
{
|
|
|
|
LINE_REC *line;
|
|
|
|
|
2001-04-16 12:46:34 -04:00
|
|
|
g_return_val_if_fail(buffer != NULL, NULL);
|
|
|
|
g_return_val_if_fail(data != NULL, NULL);
|
|
|
|
|
2001-11-15 17:36:12 -05:00
|
|
|
if (len == 0)
|
|
|
|
return insert_after;
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
line = !buffer->last_eol ? insert_after :
|
|
|
|
textbuffer_line_insert(buffer, insert_after);
|
|
|
|
|
|
|
|
if (info != NULL)
|
|
|
|
memcpy(&line->info, info, sizeof(line->info));
|
|
|
|
|
|
|
|
text_chunk_append(buffer, data, len);
|
|
|
|
|
|
|
|
buffer->last_eol = len >= 2 &&
|
|
|
|
data[len-2] == 0 && data[len-1] == LINE_CMD_EOL;
|
|
|
|
|
2008-03-10 08:05:43 -04:00
|
|
|
if (buffer->last_eol) {
|
2014-01-09 09:20:29 -05:00
|
|
|
buffer->last_fg = -1;
|
|
|
|
buffer->last_bg = -1;
|
2008-04-16 16:32:36 -04:00
|
|
|
buffer->last_flags = 0;
|
2008-03-10 08:05:43 -04:00
|
|
|
}
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void textbuffer_remove(TEXT_BUFFER_REC *buffer, LINE_REC *line)
|
|
|
|
{
|
2001-04-16 12:46:34 -04:00
|
|
|
g_return_if_fail(buffer != NULL);
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
if (buffer->first_line == line)
|
|
|
|
buffer->first_line = line->next;
|
|
|
|
if (line->prev != NULL)
|
|
|
|
line->prev->next = line->next;
|
|
|
|
if (line->next != NULL)
|
|
|
|
line->next->prev = line->prev;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
if (buffer->cur_line == line) {
|
2009-01-04 10:56:54 -05:00
|
|
|
buffer->cur_line = line->prev;
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
|
2001-08-12 14:25:43 -04:00
|
|
|
line->prev = line->next = NULL;
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
buffer->lines_count--;
|
2008-11-01 13:56:56 -04:00
|
|
|
text_chunk_line_free(buffer, line);
|
2009-01-08 07:39:11 -05:00
|
|
|
g_slice_free(LINE_REC, line);
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
|
2008-11-01 13:56:56 -04:00
|
|
|
/* Removes all lines from buffer */
|
2001-04-14 18:24:56 -04:00
|
|
|
void textbuffer_remove_all_lines(TEXT_BUFFER_REC *buffer)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
2001-07-12 17:44:01 -04:00
|
|
|
LINE_REC *line;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2001-04-16 12:46:34 -04:00
|
|
|
g_return_if_fail(buffer != NULL);
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
for (tmp = buffer->text_chunks; tmp != NULL; tmp = tmp->next)
|
2009-01-08 07:39:11 -05:00
|
|
|
g_slice_free(TEXT_CHUNK_REC, tmp->data);
|
2001-04-14 18:24:56 -04:00
|
|
|
g_slist_free(buffer->text_chunks);
|
2001-08-08 11:48:54 -04:00
|
|
|
buffer->text_chunks = NULL;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
while (buffer->first_line != NULL) {
|
|
|
|
line = buffer->first_line->next;
|
2009-01-08 07:39:11 -05:00
|
|
|
g_slice_free(LINE_REC, buffer->first_line);
|
2001-07-12 17:44:01 -04:00
|
|
|
buffer->first_line = line;
|
|
|
|
}
|
2001-08-08 11:48:54 -04:00
|
|
|
buffer->lines_count = 0;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
buffer->cur_line = NULL;
|
2001-08-08 11:48:54 -04:00
|
|
|
buffer->cur_text = NULL;
|
|
|
|
|
2001-08-08 11:43:07 -04:00
|
|
|
buffer->last_eol = TRUE;
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
|
2008-11-15 16:51:07 -05:00
|
|
|
static void set_color(GString *str, int cmd)
|
2001-10-28 06:30:26 -05:00
|
|
|
{
|
2014-01-09 09:20:29 -05:00
|
|
|
int color = -1;
|
2008-11-15 16:51:07 -05:00
|
|
|
|
|
|
|
if (!(cmd & LINE_COLOR_DEFAULT))
|
2014-01-09 09:20:29 -05:00
|
|
|
color = (cmd & 0x0f)+'0';
|
2001-10-28 06:30:26 -05:00
|
|
|
|
|
|
|
if ((cmd & LINE_COLOR_BG) == 0) {
|
|
|
|
/* change foreground color */
|
2014-01-09 09:20:29 -05:00
|
|
|
g_string_append_printf(str, "\004%c%c",
|
2008-11-15 16:51:07 -05:00
|
|
|
color, FORMAT_COLOR_NOCHANGE);
|
2001-10-28 06:30:26 -05:00
|
|
|
} else {
|
|
|
|
/* change background color */
|
2014-01-09 09:20:29 -05:00
|
|
|
g_string_append_printf(str, "\004%c%c",
|
2008-11-15 16:51:07 -05:00
|
|
|
FORMAT_COLOR_NOCHANGE, color);
|
2001-10-28 06:30:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
void textbuffer_line2text(LINE_REC *line, int coloring, GString *str)
|
|
|
|
{
|
2002-02-01 15:14:30 -05:00
|
|
|
unsigned char cmd, *ptr, *tmp;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
g_return_if_fail(line != NULL);
|
|
|
|
g_return_if_fail(str != NULL);
|
|
|
|
|
|
|
|
g_string_truncate(str, 0);
|
|
|
|
|
|
|
|
for (ptr = line->text;;) {
|
|
|
|
if (*ptr != 0) {
|
2002-02-01 15:14:30 -05:00
|
|
|
g_string_append_c(str, (char) *ptr);
|
2001-04-14 18:24:56 -04:00
|
|
|
ptr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
2002-02-01 15:14:30 -05:00
|
|
|
cmd = *ptr;
|
2001-04-14 18:24:56 -04:00
|
|
|
ptr++;
|
|
|
|
|
2009-01-10 13:38:08 -05:00
|
|
|
if (cmd == LINE_CMD_EOL) {
|
2001-04-14 18:24:56 -04:00
|
|
|
/* end of line */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == LINE_CMD_CONTINUE) {
|
|
|
|
/* line continues in another address.. */
|
2002-02-01 15:14:30 -05:00
|
|
|
memcpy(&tmp, ptr, sizeof(unsigned char *));
|
2001-04-14 18:24:56 -04:00
|
|
|
ptr = tmp;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!coloring) {
|
|
|
|
/* no colors, skip coloring commands */
|
2014-01-09 09:20:29 -05:00
|
|
|
if (cmd == LINE_COLOR_EXT || cmd == LINE_COLOR_EXT_BG)
|
|
|
|
ptr++;
|
|
|
|
#ifdef TERM_TRUECOLOR
|
|
|
|
else if (cmd == LINE_COLOR_24)
|
|
|
|
ptr+=4;
|
|
|
|
#endif
|
|
|
|
|
2001-04-14 18:24:56 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-09 09:20:29 -05:00
|
|
|
if ((cmd & LINE_CMD_EOL) == 0) {
|
2001-04-14 18:24:56 -04:00
|
|
|
/* set color */
|
2008-11-15 16:51:07 -05:00
|
|
|
set_color(str, cmd);
|
2001-04-14 18:24:56 -04:00
|
|
|
} else switch (cmd) {
|
|
|
|
case LINE_CMD_UNDERLINE:
|
|
|
|
g_string_append_c(str, 31);
|
|
|
|
break;
|
2001-10-14 06:14:32 -04:00
|
|
|
case LINE_CMD_REVERSE:
|
|
|
|
g_string_append_c(str, 22);
|
|
|
|
break;
|
2008-11-15 16:51:07 -05:00
|
|
|
case LINE_CMD_BLINK:
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "\004%c",
|
2008-11-15 16:51:07 -05:00
|
|
|
FORMAT_STYLE_BLINK);
|
|
|
|
break;
|
|
|
|
case LINE_CMD_BOLD:
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "\004%c",
|
2008-11-15 16:51:07 -05:00
|
|
|
FORMAT_STYLE_BOLD);
|
|
|
|
break;
|
2011-11-06 14:40:25 -05:00
|
|
|
case LINE_CMD_ITALIC:
|
|
|
|
g_string_append_printf(str, "\004%c",
|
|
|
|
FORMAT_STYLE_ITALIC);
|
|
|
|
break;
|
2001-04-14 18:24:56 -04:00
|
|
|
case LINE_CMD_COLOR0:
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "\004%c%c",
|
2001-04-14 18:24:56 -04:00
|
|
|
'0', FORMAT_COLOR_NOCHANGE);
|
|
|
|
break;
|
|
|
|
case LINE_CMD_INDENT:
|
2009-02-08 12:22:42 -05:00
|
|
|
g_string_append_printf(str, "\004%c",
|
2008-11-15 17:04:57 -05:00
|
|
|
FORMAT_STYLE_INDENT);
|
2001-04-14 18:24:56 -04:00
|
|
|
break;
|
2014-01-09 09:20:29 -05:00
|
|
|
case LINE_COLOR_EXT:
|
|
|
|
format_ext_color(str, 0, *ptr++);
|
|
|
|
break;
|
|
|
|
case LINE_COLOR_EXT_BG:
|
|
|
|
format_ext_color(str, 1, *ptr++);
|
|
|
|
break;
|
|
|
|
#ifdef TERM_TRUECOLOR
|
|
|
|
case LINE_COLOR_24:
|
|
|
|
g_string_append_printf(str, "\004%c", FORMAT_COLOR_24);
|
|
|
|
break;
|
|
|
|
#endif
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
|
|
|
|
int level, int nolevel, const char *text,
|
2002-01-20 11:57:06 -05:00
|
|
|
int before, int after,
|
2001-04-14 18:24:56 -04:00
|
|
|
int regexp, int fullword, int case_sensitive)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
regex_t preg;
|
|
|
|
#endif
|
2002-01-20 11:57:06 -05:00
|
|
|
LINE_REC *line, *pre_line;
|
2001-04-14 18:24:56 -04:00
|
|
|
GList *matches;
|
2002-01-20 11:57:06 -05:00
|
|
|
GString *str;
|
|
|
|
int i, match_after, line_matched;
|
2008-03-28 08:42:27 -04:00
|
|
|
char * (*match_func)(const char *, const char *);
|
2001-04-14 18:24:56 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(buffer != NULL, NULL);
|
|
|
|
g_return_val_if_fail(text != NULL, NULL);
|
|
|
|
|
2007-06-28 18:50:58 -04:00
|
|
|
if (regexp) {
|
2001-04-14 18:24:56 -04:00
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
int flags = REG_EXTENDED | REG_NOSUB |
|
|
|
|
(case_sensitive ? 0 : REG_ICASE);
|
|
|
|
if (regcomp(&preg, text, flags) != 0)
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-01-20 11:57:06 -05:00
|
|
|
matches = NULL; match_after = 0;
|
2001-04-14 18:24:56 -04:00
|
|
|
str = g_string_new(NULL);
|
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
line = startline != NULL ? startline : buffer->first_line;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2008-03-28 08:42:27 -04:00
|
|
|
if (fullword)
|
|
|
|
match_func = case_sensitive ? strstr_full : stristr_full;
|
|
|
|
else
|
|
|
|
match_func = case_sensitive ? strstr : stristr;
|
|
|
|
|
2001-07-12 17:44:01 -04:00
|
|
|
for (; line != NULL; line = line->next) {
|
2007-06-28 18:50:58 -04:00
|
|
|
line_matched = (line->info.level & level) != 0 &&
|
|
|
|
(line->info.level & nolevel) == 0;
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2007-08-22 15:31:47 -04:00
|
|
|
if (*text != '\0') {
|
|
|
|
textbuffer_line2text(line, FALSE, str);
|
2001-04-14 18:24:56 -04:00
|
|
|
|
2007-08-22 15:31:47 -04:00
|
|
|
if (line_matched)
|
|
|
|
line_matched =
|
2001-04-14 18:24:56 -04:00
|
|
|
#ifdef HAVE_REGEX_H
|
2002-01-20 11:57:06 -05:00
|
|
|
regexp ? regexec(&preg, str->str, 0, NULL, 0) == 0 :
|
2001-04-14 18:24:56 -04:00
|
|
|
#endif
|
2008-03-28 08:42:27 -04:00
|
|
|
match_func(str->str, text) != NULL;
|
2007-08-22 15:31:47 -04:00
|
|
|
}
|
|
|
|
|
2002-01-20 11:57:06 -05:00
|
|
|
if (line_matched) {
|
|
|
|
/* add the -before lines */
|
|
|
|
pre_line = line;
|
|
|
|
for (i = 0; i < before; i++) {
|
|
|
|
if (pre_line->prev == NULL ||
|
|
|
|
g_list_find(matches, pre_line->prev) != NULL)
|
|
|
|
break;
|
|
|
|
pre_line = pre_line->prev;
|
|
|
|
}
|
|
|
|
|
2008-11-01 13:56:56 -04:00
|
|
|
for (; pre_line != line; pre_line = pre_line->next)
|
2002-01-20 11:57:06 -05:00
|
|
|
matches = g_list_append(matches, pre_line);
|
|
|
|
|
|
|
|
match_after = after;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_matched || match_after > 0) {
|
2001-04-14 18:24:56 -04:00
|
|
|
/* matched */
|
2001-07-12 17:44:01 -04:00
|
|
|
matches = g_list_append(matches, line);
|
2002-01-20 11:57:06 -05:00
|
|
|
|
2002-02-15 04:22:35 -05:00
|
|
|
if ((!line_matched && --match_after == 0) ||
|
|
|
|
(line_matched && match_after == 0 && before > 0))
|
2002-01-20 11:57:06 -05:00
|
|
|
matches = g_list_append(matches, NULL);
|
2001-04-14 18:24:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef HAVE_REGEX_H
|
|
|
|
if (regexp) regfree(&preg);
|
|
|
|
#endif
|
|
|
|
g_string_free(str, TRUE);
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
void textbuffer_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void textbuffer_deinit(void)
|
|
|
|
{
|
|
|
|
}
|