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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "module-formats.h"
30 #include "printtext.h"
31 #include "gui-windows.h"
33 /* SYNTAX: CLEAR [-all] [<refnum>] */
34 static void cmd_clear(const char *data)
42 g_return_if_fail(data != NULL);
44 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
45 "clear", &optlist, &refnum)) return;
47 if (g_hash_table_lookup(optlist, "all") != NULL) {
48 /* clear all windows */
49 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
51 textbuffer_view_clear(WINDOW_GUI(window)->view);
53 } else if (*refnum != '\0') {
54 /* clear specified window */
55 window = window_find_refnum(atoi(refnum));
57 textbuffer_view_clear(WINDOW_GUI(window)->view);
59 /* clear active window */
60 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
63 cmd_params_free(free_arg);
66 static void cmd_window_scroll(const char *data)
70 gui = WINDOW_GUI(active_win);
71 if (g_ascii_strcasecmp(data, "default") == 0) {
72 gui->use_scroll = FALSE;
73 } else if (g_ascii_strcasecmp(data, "on") == 0) {
74 gui->use_scroll = TRUE;
76 } else if (g_ascii_strcasecmp(data, "off") == 0) {
77 gui->use_scroll = TRUE;
79 } else if (*data != '\0') {
80 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
81 TXT_WINDOW_SCROLL_UNKNOWN, data);
85 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
86 TXT_WINDOW_SCROLL, !gui->use_scroll ? "DEFAULT" :
87 gui->scroll ? "ON" : "OFF");
88 textbuffer_view_set_scroll(gui->view, gui->use_scroll ?
89 gui->scroll : settings_get_bool("scroll"));
92 static void cmd_scrollback(const char *data, SERVER_REC *server,
95 command_runsub("scrollback", data, server, item);
98 /* SYNTAX: SCROLLBACK CLEAR [-all] [<refnum>] */
99 static void cmd_scrollback_clear(const char *data)
107 g_return_if_fail(data != NULL);
109 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
110 "scrollback clear", &optlist, &refnum)) return;
112 if (g_hash_table_lookup(optlist, "all") != NULL) {
113 /* clear all windows */
114 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
116 textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
118 } else if (*refnum != '\0') {
119 /* clear specified window */
120 window = window_find_refnum(atoi(refnum));
122 textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
124 /* clear active window */
125 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
128 cmd_params_free(free_arg);
131 /* SYNTAX: SCROLLBACK LEVELCLEAR [-all] [-level <level>] [<refnum>] */
132 static void cmd_scrollback_levelclear(const char *data)
142 g_return_if_fail(data != NULL);
144 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
145 "scrollback levelclear", &optlist, &refnum)) return;
147 levelarg = g_hash_table_lookup(optlist, "level");
148 level = (levelarg == NULL || *levelarg == '\0') ? 0 :
149 level2bits(replace_chars(levelarg, ',', ' '), NULL);
151 cmd_params_free(free_arg);
155 if (g_hash_table_lookup(optlist, "all") != NULL) {
156 /* clear all windows */
157 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
159 textbuffer_view_remove_lines_by_level(WINDOW_GUI(window)->view, level);
161 } else if (*refnum != '\0') {
162 /* clear specified window */
163 window = window_find_refnum(atoi(refnum));
165 textbuffer_view_remove_lines_by_level(WINDOW_GUI(window)->view, level);
167 /* clear active window */
168 textbuffer_view_remove_lines_by_level(WINDOW_GUI(active_win)->view, level);
171 cmd_params_free(free_arg);
174 static void scrollback_goto_line(int linenum)
176 TEXT_BUFFER_VIEW_REC *view;
178 view = WINDOW_GUI(active_win)->view;
179 if (view->buffer->lines_count == 0)
182 textbuffer_view_scroll_line(view, view->buffer->first_line);
183 gui_window_scroll(active_win, linenum);
186 static void scrollback_goto_time(const char *datearg, const char *timearg)
193 /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
194 now = stamp = time(NULL);
195 if (*datearg == '-') {
197 stamp -= atoi(datearg+1) * 3600*24;
198 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
199 } else if (*timearg != '\0') {
201 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
204 sscanf(datearg, "%d.%d", &day, &month);
205 if (day <= 0) return;
208 /* month not given */
209 if (day > tm.tm_mday) {
210 /* last month's day */
214 /* last year's day.. */
221 if (month > tm.tm_mon)
231 /* only time given, move it to timearg */
236 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
238 sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
241 if (stamp > now && timearg == datearg) {
242 /* we used /SB GOTO 23:59 or something, we want to jump to
243 previous day's 23:59 time instead of into future. */
248 /* we're still looking into future, don't bother checking */
252 /* scroll to first line after timestamp */
253 line = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
254 for (; line != NULL; line = line->next) {
255 if (line->info.time >= stamp) {
256 gui_window_scroll_line(active_win, line);
262 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
263 static void cmd_scrollback_goto(const char *data)
265 char *datearg, *timearg;
269 if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
272 if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
273 /* go forward/backward n lines */
274 lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
275 gui_window_scroll(active_win, lines);
276 } else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
277 /* go to n'th line. */
278 scrollback_goto_line(atoi(datearg));
280 /* should be timestamp */
281 scrollback_goto_time(datearg, timearg);
284 cmd_params_free(free_arg);
287 /* SYNTAX: SCROLLBACK HOME */
288 static void cmd_scrollback_home(const char *data)
290 TEXT_BUFFER_REC *buffer;
292 buffer = WINDOW_GUI(active_win)->view->buffer;
293 if (buffer->lines_count > 0)
294 gui_window_scroll_line(active_win, buffer->first_line);
297 /* SYNTAX: SCROLLBACK END */
298 static void cmd_scrollback_end(const char *data)
300 TEXT_BUFFER_VIEW_REC *view;
302 view = WINDOW_GUI(active_win)->view;
303 if (view->bottom_startline == NULL ||
304 (view->bottom_startline == view->startline &&
305 view->bottom_subline == view->subline))
308 textbuffer_view_scroll_line(view, view->bottom_startline);
309 gui_window_scroll(active_win, view->bottom_subline);
312 static void cmd_scrollback_status(void)
316 size_t window_mem, total_mem;
318 total_lines = 0; total_mem = 0;
319 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
320 WINDOW_REC *window = tmp->data;
321 TEXT_BUFFER_VIEW_REC *view;
323 view = WINDOW_GUI(window)->view;
325 window_mem = sizeof(TEXT_BUFFER_REC);
326 window_mem += g_slist_length(view->buffer->text_chunks) *
327 sizeof(TEXT_CHUNK_REC);
328 window_mem += view->buffer->lines_count * sizeof(LINE_REC);
329 total_lines += view->buffer->lines_count;
330 total_mem += window_mem;
331 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
332 "Window %d: %d lines, %dkB of data",
333 window->refnum, view->buffer->lines_count,
334 (int)(window_mem / 1024));
337 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
338 "Total: %d lines, %dkB of data",
339 total_lines, (int)(total_mem / 1024));
342 static void sig_away_changed(SERVER_REC *server)
346 if (!server->usermode_away)
349 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
350 WINDOW_REC *rec = tmp->data;
352 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
353 "lastlog_last_away");
357 void textbuffer_commands_init(void)
359 command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
360 command_bind("window scroll", NULL, (SIGNAL_FUNC) cmd_window_scroll);
361 command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
362 command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
363 command_bind("scrollback levelclear", NULL, (SIGNAL_FUNC) cmd_scrollback_levelclear);
364 command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
365 command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
366 command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
367 command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
369 command_set_options("clear", "all");
370 command_set_options("scrollback clear", "all");
371 command_set_options("scrollback levelclear", "all -level");
373 signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
376 void textbuffer_commands_deinit(void)
378 command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
379 command_unbind("window scroll", (SIGNAL_FUNC) cmd_window_scroll);
380 command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
381 command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
382 command_unbind("scrollback levelclear", (SIGNAL_FUNC) cmd_scrollback_levelclear);
383 command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
384 command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
385 command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
386 command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
388 signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);