1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
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.
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.
*/
#define G_LOG_DOMAIN "TextBuffer"
#include "module.h"
#include <irssi/src/core/misc.h>
#include <irssi/src/fe-common/core/formats.h>
#include <irssi/src/core/utf8.h>
#include <irssi/src/core/iregex.h>
#include <irssi/src/fe-text/textbuffer-formats.h>
#include <irssi/src/fe-text/textbuffer.h>
#define TEXT_CHUNK_USABLE_SIZE (LINE_TEXT_CHUNK_SIZE-2-(int)sizeof(char*))
TEXT_BUFFER_REC *textbuffer_create(WINDOW_REC *window)
{
TEXT_BUFFER_REC *buffer;
buffer = g_slice_new0(TEXT_BUFFER_REC);
buffer->window = window;
buffer->last_eol = TRUE;
buffer->last_fg = -1;
buffer->last_bg = -1;
buffer->cur_text = g_string_sized_new(TEXT_CHUNK_USABLE_SIZE);
return buffer;
}
void textbuffer_destroy(TEXT_BUFFER_REC *buffer)
{
GSList *tmp;
g_return_if_fail(buffer != NULL);
textbuffer_remove_all_lines(buffer);
g_string_free(buffer->cur_text, TRUE);
for (tmp = buffer->cur_info; tmp != NULL; tmp = tmp->next) {
LINE_INFO_REC *info = buffer->cur_info->data;
textbuffer_line_info_free1(info);
g_free(info);
}
g_slist_free(buffer->cur_info);
buffer->window = NULL;
g_slice_free(TEXT_BUFFER_REC, buffer);
}
void textbuffer_line_info_free1(LINE_INFO_REC *info)
{
textbuffer_format_rec_free(info->format);
textbuffer_meta_rec_free(info->meta);
g_free(info->text);
}
static void text_chunk_append(TEXT_BUFFER_REC *buffer,
const unsigned char *data, int len)
{
if (len == 0)
return;
/* g_string_append_len(buffer->cur_text, (const char *)data, len); */
g_string_append(buffer->cur_text, (const char *) data);
}
static LINE_REC *textbuffer_line_create(TEXT_BUFFER_REC *buffer)
{
LINE_REC *rec;
rec = g_slice_new0(LINE_REC);
return rec;
}
static LINE_REC *textbuffer_line_insert(TEXT_BUFFER_REC *buffer,
LINE_REC *prev)
{
LINE_REC *line;
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;
} else {
line->next = prev->next;
if (line->next != NULL)
line->next->prev = line;
prev->next = line;
}
if (prev == buffer->cur_line)
buffer->cur_line = line;
buffer->lines_count++;
return line;
}
LINE_REC *textbuffer_line_last(TEXT_BUFFER_REC *buffer)
{
return buffer->cur_line;
}
/* returns TRUE if `search' comes on or after `line' in the buffer */
int textbuffer_line_exists_after(LINE_REC *line, LINE_REC *search)
{
while (line != NULL) {
if (line == search)
return TRUE;
line = line->next;
}
return FALSE;
}
void textbuffer_line_add_colors(TEXT_BUFFER_REC *buffer, LINE_REC **line,
int fg, int bg, int flags)
{
GString *out = g_string_new(NULL);
format_gui_flags(out, &buffer->last_fg, &buffer->last_bg, &buffer->last_flags, fg, bg,
flags);
if (*(out->str) != '\0') {
*line =
textbuffer_insert(buffer, *line, (unsigned char *) out->str, out->len, NULL);
}
g_string_free(out, TRUE);
}
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;
g_return_val_if_fail(buffer != NULL, NULL);
g_return_val_if_fail(data != NULL, NULL);
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;
if (buffer->last_eol) {
if (!line->info.format) {
line->info.text = g_strdup(buffer->cur_text->str);
g_string_truncate(buffer->cur_text, 0);
}
buffer->last_fg = -1;
buffer->last_bg = -1;
buffer->last_flags = 0;
}
return line;
}
void textbuffer_remove(TEXT_BUFFER_REC *buffer, LINE_REC *line)
{
g_return_if_fail(buffer != NULL);
g_return_if_fail(line != NULL);
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;
if (buffer->cur_line == line) {
buffer->cur_line = line->prev;
}
line->prev = line->next = NULL;
buffer->lines_count--;
textbuffer_line_info_free1(&line->info);
g_slice_free(LINE_REC, line);
}
/* Removes all lines from buffer */
void textbuffer_remove_all_lines(TEXT_BUFFER_REC *buffer)
{
LINE_REC *line;
g_return_if_fail(buffer != NULL);
while (buffer->first_line != NULL) {
line = buffer->first_line->next;
textbuffer_line_info_free1(&buffer->first_line->info);
g_slice_free(LINE_REC, buffer->first_line);
buffer->first_line = line;
}
buffer->lines_count = 0;
buffer->cur_line = NULL;
g_string_truncate(buffer->cur_text, 0);
buffer->last_eol = TRUE;
}
void textbuffer_line2text(TEXT_BUFFER_REC *buffer, LINE_REC *line, int coloring, GString *str)
{
char *ptr, *tmp;
g_return_if_fail(line != NULL);
g_return_if_fail(str != NULL);
g_string_truncate(str, 0);
if ((ptr = textbuffer_line_get_text(buffer, line, coloring == COLORING_RAW)) != NULL) {
if (coloring == COLORING_STRIP) {
tmp = ptr;
ptr = strip_codes(tmp);
g_free(tmp);
} else if (coloring == COLORING_UNEXPAND) {
tmp = ptr;
ptr = format_string_unexpand(tmp, 0);
g_free(tmp);
}
g_string_append(str, ptr);
g_free(ptr);
}
}
GList *textbuffer_find_text(TEXT_BUFFER_REC *buffer, LINE_REC *startline,
int level, int nolevel, const char *text,
int before, int after,
int regexp, int fullword, int case_sensitive)
{
Regex *preg;
LINE_REC *line, *pre_line;
GList *matches;
GString *str;
int i, match_after, line_matched;
char * (*match_func)(const char *, const char *);
g_return_val_if_fail(buffer != NULL, NULL);
g_return_val_if_fail(text != NULL, NULL);
preg = NULL;
if (regexp) {
preg = i_regex_new(text, case_sensitive ? 0 : G_REGEX_CASELESS, 0, NULL);
if (preg == NULL)
return NULL;
}
matches = NULL; match_after = 0;
str = g_string_new(NULL);
line = startline != NULL ? startline : buffer->first_line;
if (fullword)
match_func = case_sensitive ? strstr_full : stristr_full;
else
match_func = case_sensitive ? strstr : stristr;
for (; line != NULL; line = line->next) {
line_matched = (line->info.level & level) != 0 &&
(line->info.level & nolevel) == 0;
if (*text != '\0') {
textbuffer_line2text(buffer, line, 0, str);
if (line_matched) {
line_matched = regexp ?
i_regex_match(preg, str->str, 0, NULL)
: match_func(str->str, text) != NULL;
}
}
if (line_matched) {
/* add the -before lines */
pre_line = line;
for (i = 0; i < before; i++) {
if (pre_line->prev == NULL ||
g_list_nth_data(matches, 0) == pre_line->prev ||
g_list_nth_data(matches, 1) == pre_line->prev)
break;
pre_line = pre_line->prev;
}
for (; pre_line != line; pre_line = pre_line->next)
matches = g_list_prepend(matches, pre_line);
match_after = after;
}
if (line_matched || match_after > 0) {
/* matched */
matches = g_list_prepend(matches, line);
if ((!line_matched && --match_after == 0) ||
(line_matched && match_after == 0 && before > 0))
matches = g_list_prepend(matches, NULL);
}
}
matches = g_list_reverse(matches);
if (preg != NULL)
i_regex_unref(preg);
g_string_free(str, TRUE);
return matches;
}
void textbuffer_init(void)
{
}
void textbuffer_deinit(void)
{
}
|