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
27 #include "printtext.h"
28 #include "gui-windows.h"
31 static void cmd_clear(const char *data)
37 g_return_if_fail(data != NULL);
39 if (!cmd_get_params(data, &free_arg, PARAM_FLAG_OPTIONS,
40 "clear", &optlist)) return;
42 if (g_hash_table_lookup(optlist, "all") == NULL) {
43 /* clear active window */
44 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
46 /* clear all windows */
47 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
48 WINDOW_REC *window = tmp->data;
50 textbuffer_view_clear(WINDOW_GUI(window)->view);
54 cmd_params_free(free_arg);
57 static void cmd_scrollback(const char *data, SERVER_REC *server,
60 command_runsub("scrollback", data, server, item);
63 /* SYNTAX: SCROLLBACK CLEAR */
64 static void cmd_scrollback_clear(void)
66 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
69 static void scrollback_goto_line(int linenum)
71 TEXT_BUFFER_VIEW_REC *view;
73 view = WINDOW_GUI(active_win)->view;
74 if (view->buffer->lines_count == 0)
77 textbuffer_view_scroll_line(view, view->buffer->lines->data);
78 gui_window_scroll(active_win, linenum);
81 static void scrollback_goto_time(const char *datearg, const char *timearg)
88 /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
89 now = stamp = time(NULL);
90 if (*datearg == '-') {
92 stamp -= atoi(datearg+1) * 3600*24;
93 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
94 } else if (*timearg != '\0') {
96 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
99 sscanf(datearg, "%d.%d", &day, &month);
100 if (day <= 0) return;
103 /* month not given */
104 if (day > tm.tm_mday) {
105 /* last month's day */
109 /* last year's day.. */
116 if (month > tm.tm_mon)
126 /* only time given, move it to timearg */
131 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
133 sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
136 if (stamp > now && timearg == datearg) {
137 /* we used /SB GOTO 23:59 or something, we want to jump to
138 previous day's 23:59 time instead of into future. */
143 /* we're still looking into future, don't bother checking */
147 /* scroll to first line after timestamp */
148 tmp = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
149 for (; tmp != NULL; tmp = tmp->next) {
150 LINE_REC *rec = tmp->data;
152 if (rec->info.time >= stamp) {
153 gui_window_scroll_line(active_win, rec);
159 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
160 static void cmd_scrollback_goto(const char *data)
162 char *datearg, *timearg;
166 if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
169 if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
170 /* go forward/backward n lines */
171 lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
172 gui_window_scroll(active_win, lines);
173 } else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
174 /* go to n'th line. */
175 scrollback_goto_line(atoi(datearg));
177 /* should be timestamp */
178 scrollback_goto_time(datearg, timearg);
181 cmd_params_free(free_arg);
184 /* SYNTAX: SCROLLBACK HOME */
185 static void cmd_scrollback_home(const char *data)
187 TEXT_BUFFER_REC *buffer;
189 buffer = WINDOW_GUI(active_win)->view->buffer;
190 if (buffer->lines_count > 0)
191 gui_window_scroll_line(active_win, buffer->lines->data);
194 /* SYNTAX: SCROLLBACK END */
195 static void cmd_scrollback_end(const char *data)
197 TEXT_BUFFER_VIEW_REC *view;
199 view = WINDOW_GUI(active_win)->view;
200 if (view->bottom_startline == NULL)
203 textbuffer_view_scroll_line(view, view->bottom_startline->data);
204 gui_window_scroll(active_win, view->bottom_subline);
207 /* SYNTAX: SCROLLBACK REDRAW */
208 static void cmd_scrollback_redraw(void)
214 gui = WINDOW_GUI(active_win);
216 screen_refresh_freeze();
217 for (tmp = gui->lines; tmp != NULL; tmp = next) {
219 gui_window_reformat_line(active_win, tmp->data);
222 gui_window_redraw(active_win);
223 screen_refresh_thaw();
227 static void cmd_scrollback_status(void)
230 int window_kb, total_lines, total_kb;
232 total_lines = 0; total_kb = 0;
233 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
234 WINDOW_REC *window = tmp->data;
235 TEXT_BUFFER_VIEW_REC *view;
237 view = WINDOW_GUI(window)->view;
239 window_kb = g_slist_length(view->buffer->text_chunks)*
240 LINE_TEXT_CHUNK_SIZE/1024;
241 total_lines += view->buffer->lines_count;
242 total_kb += window_kb;
243 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
244 "Window %d: %d lines, %dkB of data",
245 window->refnum, view->buffer->lines_count,
249 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
250 "Total: %d lines, %dkB of data",
251 total_lines, total_kb);
254 static void sig_away_changed(SERVER_REC *server)
258 if (!server->usermode_away)
261 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
262 WINDOW_REC *rec = tmp->data;
264 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
265 "lastlog_last_away");
269 void textbuffer_commands_init(void)
271 command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
272 command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
273 command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
274 command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
275 command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
276 command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
277 command_bind("scrollback redraw", NULL, (SIGNAL_FUNC) cmd_scrollback_redraw);
278 command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
280 command_set_options("clear", "all");
282 signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
285 void textbuffer_commands_deinit(void)
287 command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
288 command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
289 command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
290 command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
291 command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
292 command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
293 command_unbind("scrollback redraw", (SIGNAL_FUNC) cmd_scrollback_redraw);
294 command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
296 signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);