2 textbuffer-commands.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
22 #include "module-formats.h"
30 #include "printtext.h"
31 #include "gui-windows.h"
32 #include "textbuffer-reformat.h"
37 /* SYNTAX: CLEAR [-all] [<refnum>] */
38 static void cmd_clear(const char *data)
46 g_return_if_fail(data != NULL);
48 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
49 "clear", &optlist, &refnum)) return;
51 if (g_hash_table_lookup(optlist, "all") != NULL) {
52 /* clear all windows */
53 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
55 textbuffer_view_clear(WINDOW_GUI(window)->view);
57 } else if (*refnum != '\0') {
58 /* clear specified window */
59 window = window_find_refnum(atoi(refnum));
61 textbuffer_view_clear(WINDOW_GUI(window)->view);
63 /* clear active window */
64 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
67 cmd_params_free(free_arg);
70 static void cmd_window_scroll(const char *data)
74 gui = WINDOW_GUI(active_win);
75 if (g_strcasecmp(data, "default") == 0) {
76 gui->use_scroll = FALSE;
77 } else if (g_strcasecmp(data, "on") == 0) {
78 gui->use_scroll = TRUE;
80 } else if (g_strcasecmp(data, "off") == 0) {
81 gui->use_scroll = TRUE;
83 } else if (*data != '\0') {
84 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
85 TXT_WINDOW_SCROLL_UNKNOWN, data);
89 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
90 TXT_WINDOW_SCROLL, !gui->use_scroll ? "DEFAULT" :
91 gui->scroll ? "ON" : "OFF");
92 textbuffer_view_set_scroll(gui->view, gui->use_scroll ?
93 gui->scroll : settings_get_bool("scroll"));
96 static void cmd_scrollback(const char *data, SERVER_REC *server,
99 command_runsub("scrollback", data, server, item);
102 /* SYNTAX: SCROLLBACK CLEAR [-all] [<refnum>] */
103 static void cmd_scrollback_clear(const char *data)
111 g_return_if_fail(data != NULL);
113 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
114 "scrollback clear", &optlist, &refnum)) return;
116 if (g_hash_table_lookup(optlist, "all") != NULL) {
117 /* clear all windows */
118 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
120 textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
122 } else if (*refnum != '\0') {
123 /* clear specified window */
124 window = window_find_refnum(atoi(refnum));
126 textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
128 /* clear active window */
129 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
132 cmd_params_free(free_arg);
135 static void scrollback_goto_line(int linenum)
137 TEXT_BUFFER_VIEW_REC *view;
139 view = WINDOW_GUI(active_win)->view;
140 if (view->buffer->lines_count == 0)
143 textbuffer_view_scroll_line(view, view->buffer->first_line);
144 gui_window_scroll(active_win, linenum);
147 static void scrollback_goto_time(const char *datearg, const char *timearg)
154 /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
155 now = stamp = time(NULL);
156 if (*datearg == '-') {
158 stamp -= atoi(datearg+1) * 3600*24;
159 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
160 } else if (*timearg != '\0') {
162 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
165 sscanf(datearg, "%d.%d", &day, &month);
166 if (day <= 0) return;
169 /* month not given */
170 if (day > tm.tm_mday) {
171 /* last month's day */
175 /* last year's day.. */
182 if (month > tm.tm_mon)
192 /* only time given, move it to timearg */
197 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
199 sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
202 if (stamp > now && timearg == datearg) {
203 /* we used /SB GOTO 23:59 or something, we want to jump to
204 previous day's 23:59 time instead of into future. */
209 /* we're still looking into future, don't bother checking */
213 /* scroll to first line after timestamp */
214 line = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
215 for (; line != NULL; line = line->next) {
216 if (line->info.time >= stamp) {
217 gui_window_scroll_line(active_win, line);
223 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
224 static void cmd_scrollback_goto(const char *data)
226 char *datearg, *timearg;
230 if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
233 if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
234 /* go forward/backward n lines */
235 lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
236 gui_window_scroll(active_win, lines);
237 } else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
238 /* go to n'th line. */
239 scrollback_goto_line(atoi(datearg));
241 /* should be timestamp */
242 scrollback_goto_time(datearg, timearg);
245 cmd_params_free(free_arg);
248 /* SYNTAX: SCROLLBACK HOME */
249 static void cmd_scrollback_home(const char *data)
251 TEXT_BUFFER_REC *buffer;
253 buffer = WINDOW_GUI(active_win)->view->buffer;
254 if (buffer->lines_count > 0)
255 gui_window_scroll_line(active_win, buffer->first_line);
258 /* SYNTAX: SCROLLBACK END */
259 static void cmd_scrollback_end(const char *data)
261 TEXT_BUFFER_VIEW_REC *view;
263 view = WINDOW_GUI(active_win)->view;
264 if (view->bottom_startline == NULL ||
265 (view->bottom_startline == view->startline &&
266 view->bottom_subline == view->subline))
269 textbuffer_view_scroll_line(view, view->bottom_startline);
270 gui_window_scroll(active_win, view->bottom_subline);
273 /* SYNTAX: SCROLLBACK REDRAW */
274 static void cmd_scrollback_redraw(void)
277 LINE_REC *line, *next;
279 gui = WINDOW_GUI(active_win);
281 term_refresh_freeze();
282 line = textbuffer_view_get_lines(gui->view);
283 while (line != NULL) {
285 textbuffer_reformat_line(active_win, line);
289 gui_window_redraw(active_win);
293 static void cmd_scrollback_status(void)
296 int window_kb, total_lines, total_kb;
298 total_lines = 0; total_kb = 0;
299 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
300 WINDOW_REC *window = tmp->data;
301 TEXT_BUFFER_VIEW_REC *view;
303 view = WINDOW_GUI(window)->view;
305 window_kb = g_slist_length(view->buffer->text_chunks)*
306 LINE_TEXT_CHUNK_SIZE/1024;
307 total_lines += view->buffer->lines_count;
308 total_kb += window_kb;
309 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
310 "Window %d: %d lines, %dkB of data",
311 window->refnum, view->buffer->lines_count,
315 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
316 "Total: %d lines, %dkB of data",
317 total_lines, total_kb);
320 static void sig_away_changed(SERVER_REC *server)
324 if (!server->usermode_away)
327 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
328 WINDOW_REC *rec = tmp->data;
330 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
331 "lastlog_last_away");
336 static void cmd_cuix(void)
340 /* textbuffer_view_clear(WINDOW_GUI(active_win)->view); */
344 /* should never been called */
345 /* cuix_destroy (); */
347 /* textbuffer_view_clear(WINDOW_GUI(active_win)->view); */
352 void textbuffer_commands_init(void)
354 command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
355 command_bind("window scroll", NULL, (SIGNAL_FUNC) cmd_window_scroll);
356 command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
357 command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
358 command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
359 command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
360 command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
361 command_bind("scrollback redraw", NULL, (SIGNAL_FUNC) cmd_scrollback_redraw);
362 command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
364 command_bind("cuix", NULL, (SIGNAL_FUNC) cmd_cuix);
367 command_set_options("clear", "all");
368 command_set_options("scrollback clear", "all");
370 signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
373 void textbuffer_commands_deinit(void)
375 command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
376 command_unbind("window scroll", (SIGNAL_FUNC) cmd_window_scroll);
377 command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
378 command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
379 command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
380 command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
381 command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
382 command_unbind("scrollback redraw", (SIGNAL_FUNC) cmd_scrollback_redraw);
383 command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
385 command_unbind("cuix", (SIGNAL_FUNC) cmd_cuix);
388 signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);