2 textbuffer-reformat.c : Reformatting lines in text buffer
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
27 #include "gui-windows.h"
28 #include "gui-printtext.h"
29 #include "textbuffer.h"
31 static GString *format;
32 static int scrollback_save_formats;
34 /* Read one block between \0<format>s */
35 static char *line_read_format(unsigned const char **text)
40 str = g_string_new(NULL);
43 if ((*text)[1] == LINE_CMD_EOL) {
44 /* leave text at \0<eof> */
47 if ((*text)[1] == LINE_CMD_FORMAT_CONT) {
48 /* leave text at \0<format_cont> */
53 if (**text == LINE_CMD_FORMAT) {
54 /* move text to start after \0<format> */
59 if (**text == LINE_CMD_CONTINUE) {
62 memcpy(&tmp, (*text)+1, sizeof(char *));
65 } else if (**text & 0x80)
70 g_string_append_c(str, (char) **text);
75 g_string_free(str, FALSE);
79 static char *textbuffer_line_get_format(WINDOW_REC *window, LINE_REC *line,
82 const unsigned char *text;
83 char *module, *format_name, *args[MAX_FORMAT_PARAMS], *ret;
85 int formatnum, argcount;
87 text = (const unsigned char *) line->text;
89 /* skip the beginning of the line until we find the format */
90 format_name = line_read_format(&text);
92 if (text[1] == LINE_CMD_FORMAT_CONT) {
94 g_string_append_c(raw, '\0');
95 g_string_append_c(raw, (char)LINE_CMD_FORMAT_CONT);
100 /* read format information */
101 module = line_read_format(&text);
102 format_name = line_read_format(&text);
105 g_string_append_c(raw, '\0');
106 g_string_append_c(raw, (char)LINE_CMD_FORMAT);
108 g_string_append(raw, module);
110 g_string_append_c(raw, '\0');
111 g_string_append_c(raw, (char)LINE_CMD_FORMAT);
113 g_string_append(raw, format_name);
116 formatnum = format_find_tag(module, format_name);
121 memset(args, 0, sizeof(args));
122 while (*text != '\0' || text[1] != LINE_CMD_EOL) {
123 args[argcount] = line_read_format(&text);
125 g_string_append_c(raw, '\0');
126 g_string_append_c(raw,
127 (char)LINE_CMD_FORMAT);
129 g_string_append(raw, args[argcount]);
134 /* get the format text */
135 format_create_dest(&dest, NULL, NULL, line->info.level, window);
136 ret = format_get_text_theme_charargs(current_theme,
140 g_free(args[--argcount]);
149 void textbuffer_reformat_line(WINDOW_REC *window, LINE_REC *line)
154 LINE_INFO_REC line_info;
156 char *str, *tmp, *prestr, *linestart, *leveltag;
158 gui = WINDOW_GUI(window);
160 raw = g_string_new(NULL);
161 str = textbuffer_line_get_format(window, line, raw);
163 if (str == NULL && raw->len == 2 &&
164 raw->str[1] == (char)LINE_CMD_FORMAT_CONT) {
165 /* multiline format, format explained in one the
166 following lines. remove this line. */
167 textbuffer_view_remove_line(gui->view, line);
168 } else if (str != NULL) {
169 /* FIXME: ugly ugly .. and this can't handle
170 unformatted lines.. */
171 g_string_append_c(raw, '\0');
172 g_string_append_c(raw, (char)LINE_CMD_EOL);
174 line_prev = line->prev;
175 memcpy(&line_info, &line->info, sizeof(line_info));
176 textbuffer_view_remove_line(gui->view, line); line = NULL;
178 format_create_dest(&dest, NULL, NULL, line_info.level, window);
180 linestart = format_get_line_start(current_theme, &dest, line_info.time);
181 leveltag = format_get_level_tag(current_theme, &dest);
183 prestr = g_strconcat(linestart == NULL ? "" : linestart,
185 g_free_not_null(linestart);
186 g_free_not_null(leveltag);
188 tmp = format_add_linestart(str, prestr);
192 gui_printtext_after(&dest, line_prev, tmp);
195 line = textbuffer_insert(gui->view->buffer, gui->insert_after,
196 (unsigned char *) raw->str,
197 raw->len, &line_info);
198 textbuffer_view_insert_line(gui->view, line);
200 g_string_free(raw, TRUE);
203 static void sig_print_format(THEME_REC *theme, const char *module,
204 TEXT_DEST_REC *dest, void *formatnump,
210 if (!scrollback_save_formats)
213 formatnum = GPOINTER_TO_INT(formatnump);
214 formats = g_hash_table_lookup(default_formats, module);
216 /* <module><format_name><arg...> */
217 g_string_truncate(format, 0);
219 g_string_append_c(format, '\0');
220 g_string_append_c(format, (char)LINE_CMD_FORMAT);
222 g_string_append(format, module);
224 g_string_append_c(format, '\0');
225 g_string_append_c(format, (char)LINE_CMD_FORMAT);
227 g_string_append(format, formats[formatnum].tag);
229 for (n = 0; n < formats[formatnum].params; n++) {
230 g_string_append_c(format, '\0');
231 g_string_append_c(format, (char)LINE_CMD_FORMAT);
234 g_string_append(format, args[n]);
238 static void sig_gui_printtext_finished(WINDOW_REC *window)
241 LINE_REC *insert_after;
243 if (format->len == 0)
246 /* save format of the line */
247 gui = WINDOW_GUI(window);
248 insert_after = gui->use_insert_after ?
249 gui->insert_after : gui->view->buffer->cur_line;
251 textbuffer_insert(gui->view->buffer, insert_after,
252 (unsigned char *) format->str,
255 g_string_truncate(format, 0);
258 static void read_settings(void)
260 scrollback_save_formats = settings_get_bool("scrollback_save_formats");
263 void textbuffer_reformat_init(void)
265 format = g_string_new(NULL);
266 settings_add_bool("history", "scrollback_save_formats", FALSE);
269 signal_add("print format", (SIGNAL_FUNC) sig_print_format);
270 signal_add_first("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
271 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
274 void textbuffer_reformat_deinit(void)
276 g_string_free(format, TRUE);
278 signal_remove("print format", (SIGNAL_FUNC) sig_print_format);
279 signal_remove("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
280 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);