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"
35 static void cmd_clear(const char *data)
41 g_return_if_fail(data != NULL);
43 if (!cmd_get_params(data, &free_arg, PARAM_FLAG_OPTIONS,
44 "clear", &optlist)) return;
46 if (g_hash_table_lookup(optlist, "all") == NULL) {
47 /* clear active window */
48 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
50 /* clear all windows */
51 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
52 WINDOW_REC *window = tmp->data;
54 textbuffer_view_clear(WINDOW_GUI(window)->view);
58 cmd_params_free(free_arg);
61 static void cmd_window_scroll(const char *data)
65 gui = WINDOW_GUI(active_win);
66 if (g_strcasecmp(data, "default") == 0) {
67 gui->use_scroll = FALSE;
68 } else if (g_strcasecmp(data, "on") == 0) {
69 gui->use_scroll = TRUE;
71 } else if (g_strcasecmp(data, "off") == 0) {
72 gui->use_scroll = TRUE;
74 } else if (*data != '\0') {
75 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
76 TXT_WINDOW_SCROLL_UNKNOWN, data);
80 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
81 TXT_WINDOW_SCROLL, !gui->use_scroll ? "DEFAULT" :
82 gui->scroll ? "ON" : "OFF");
83 textbuffer_view_set_scroll(gui->view, gui->use_scroll ?
84 gui->scroll : settings_get_bool("scroll"));
87 static void cmd_scrollback(const char *data, SERVER_REC *server,
90 command_runsub("scrollback", data, server, item);
93 /* SYNTAX: SCROLLBACK CLEAR */
94 static void cmd_scrollback_clear(void)
96 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
99 static void scrollback_goto_line(int linenum)
101 TEXT_BUFFER_VIEW_REC *view;
103 view = WINDOW_GUI(active_win)->view;
104 if (view->buffer->lines_count == 0)
107 textbuffer_view_scroll_line(view, view->buffer->first_line);
108 gui_window_scroll(active_win, linenum);
111 static void scrollback_goto_time(const char *datearg, const char *timearg)
118 /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
119 now = stamp = time(NULL);
120 if (*datearg == '-') {
122 stamp -= atoi(datearg+1) * 3600*24;
123 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
124 } else if (*timearg != '\0') {
126 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
129 sscanf(datearg, "%d.%d", &day, &month);
130 if (day <= 0) return;
133 /* month not given */
134 if (day > tm.tm_mday) {
135 /* last month's day */
139 /* last year's day.. */
146 if (month > tm.tm_mon)
156 /* only time given, move it to timearg */
161 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
163 sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
166 if (stamp > now && timearg == datearg) {
167 /* we used /SB GOTO 23:59 or something, we want to jump to
168 previous day's 23:59 time instead of into future. */
173 /* we're still looking into future, don't bother checking */
177 /* scroll to first line after timestamp */
178 line = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
179 for (; line != NULL; line = line->next) {
180 if (line->info.time >= stamp) {
181 gui_window_scroll_line(active_win, line);
187 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
188 static void cmd_scrollback_goto(const char *data)
190 char *datearg, *timearg;
194 if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
197 if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
198 /* go forward/backward n lines */
199 lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
200 gui_window_scroll(active_win, lines);
201 } else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
202 /* go to n'th line. */
203 scrollback_goto_line(atoi(datearg));
205 /* should be timestamp */
206 scrollback_goto_time(datearg, timearg);
209 cmd_params_free(free_arg);
212 /* SYNTAX: SCROLLBACK HOME */
213 static void cmd_scrollback_home(const char *data)
215 TEXT_BUFFER_REC *buffer;
217 buffer = WINDOW_GUI(active_win)->view->buffer;
218 if (buffer->lines_count > 0)
219 gui_window_scroll_line(active_win, buffer->first_line);
222 /* SYNTAX: SCROLLBACK END */
223 static void cmd_scrollback_end(const char *data)
225 TEXT_BUFFER_VIEW_REC *view;
227 view = WINDOW_GUI(active_win)->view;
228 if (view->bottom_startline == NULL)
231 textbuffer_view_scroll_line(view, view->bottom_startline);
232 gui_window_scroll(active_win, view->bottom_subline);
235 /* SYNTAX: SCROLLBACK REDRAW */
236 static void cmd_scrollback_redraw(void)
239 LINE_REC *line, *next;
241 gui = WINDOW_GUI(active_win);
243 term_refresh_freeze();
244 line = textbuffer_view_get_lines(gui->view);
245 while (line != NULL) {
247 textbuffer_reformat_line(active_win, line);
251 gui_window_redraw(active_win);
255 static void cmd_scrollback_status(void)
258 int window_kb, total_lines, total_kb;
260 total_lines = 0; total_kb = 0;
261 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
262 WINDOW_REC *window = tmp->data;
263 TEXT_BUFFER_VIEW_REC *view;
265 view = WINDOW_GUI(window)->view;
267 window_kb = g_slist_length(view->buffer->text_chunks)*
268 LINE_TEXT_CHUNK_SIZE/1024;
269 total_lines += view->buffer->lines_count;
270 total_kb += window_kb;
271 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
272 "Window %d: %d lines, %dkB of data",
273 window->refnum, view->buffer->lines_count,
277 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
278 "Total: %d lines, %dkB of data",
279 total_lines, total_kb);
282 static void sig_away_changed(SERVER_REC *server)
286 if (!server->usermode_away)
289 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
290 WINDOW_REC *rec = tmp->data;
292 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
293 "lastlog_last_away");
297 void textbuffer_commands_init(void)
299 command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
300 command_bind("window scroll", NULL, (SIGNAL_FUNC) cmd_window_scroll);
301 command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
302 command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
303 command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
304 command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
305 command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
306 command_bind("scrollback redraw", NULL, (SIGNAL_FUNC) cmd_scrollback_redraw);
307 command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
309 command_set_options("clear", "all");
311 signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
314 void textbuffer_commands_deinit(void)
316 command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
317 command_unbind("window scroll", (SIGNAL_FUNC) cmd_window_scroll);
318 command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
319 command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
320 command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
321 command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
322 command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
323 command_unbind("scrollback redraw", (SIGNAL_FUNC) cmd_scrollback_redraw);
324 command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
326 signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);