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"
34
/* SYNTAX: CLEAR [-all] [<refnum>] */
35
static void cmd_clear(const char *data)
43
g_return_if_fail(data != NULL);
45
if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
46
"clear", &optlist, &refnum)) return;
48
if (g_hash_table_lookup(optlist, "all") != NULL) {
49
/* clear all windows */
50
for (tmp = windows; tmp != NULL; tmp = tmp->next) {
51
WINDOW_REC *window = tmp->data;
53
textbuffer_view_clear(WINDOW_GUI(window)->view);
55
} else if (*refnum != '\0') {
56
/* clear specified window */
57
window = window_find_refnum(atoi(refnum));
59
textbuffer_view_clear(WINDOW_GUI(window)->view);
61
/* clear active window */
62
textbuffer_view_clear(WINDOW_GUI(active_win)->view);
65
cmd_params_free(free_arg);
68
static void cmd_window_scroll(const char *data)
72
gui = WINDOW_GUI(active_win);
73
if (g_strcasecmp(data, "default") == 0) {
74
gui->use_scroll = FALSE;
75
} else if (g_strcasecmp(data, "on") == 0) {
76
gui->use_scroll = TRUE;
78
} else if (g_strcasecmp(data, "off") == 0) {
79
gui->use_scroll = TRUE;
81
} else if (*data != '\0') {
82
printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
83
TXT_WINDOW_SCROLL_UNKNOWN, data);
87
printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
88
TXT_WINDOW_SCROLL, !gui->use_scroll ? "DEFAULT" :
89
gui->scroll ? "ON" : "OFF");
90
textbuffer_view_set_scroll(gui->view, gui->use_scroll ?
91
gui->scroll : settings_get_bool("scroll"));
94
static void cmd_scrollback(const char *data, SERVER_REC *server,
97
command_runsub("scrollback", data, server, item);
100
/* SYNTAX: SCROLLBACK CLEAR */
101
static void cmd_scrollback_clear(void)
103
textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
106
static void scrollback_goto_line(int linenum)
108
TEXT_BUFFER_VIEW_REC *view;
110
view = WINDOW_GUI(active_win)->view;
111
if (view->buffer->lines_count == 0)
114
textbuffer_view_scroll_line(view, view->buffer->first_line);
115
gui_window_scroll(active_win, linenum);
118
static void scrollback_goto_time(const char *datearg, const char *timearg)
125
/* [dd[.mm] | -<days ago>] hh:mi[:ss] */
126
now = stamp = time(NULL);
127
if (*datearg == '-') {
129
stamp -= atoi(datearg+1) * 3600*24;
130
memcpy(&tm, localtime(&stamp), sizeof(struct tm));
131
} else if (*timearg != '\0') {
133
memcpy(&tm, localtime(&stamp), sizeof(struct tm));
136
sscanf(datearg, "%d.%d", &day, &month);
137
if (day <= 0) return;
140
/* month not given */
141
if (day > tm.tm_mday) {
142
/* last month's day */
146
/* last year's day.. */
153
if (month > tm.tm_mon)
163
/* only time given, move it to timearg */
168
memcpy(&tm, localtime(&stamp), sizeof(struct tm));
170
sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
173
if (stamp > now && timearg == datearg) {
174
/* we used /SB GOTO 23:59 or something, we want to jump to
175
previous day's 23:59 time instead of into future. */
180
/* we're still looking into future, don't bother checking */
184
/* scroll to first line after timestamp */
185
line = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
186
for (; line != NULL; line = line->next) {
187
if (line->info.time >= stamp) {
188
gui_window_scroll_line(active_win, line);
194
/* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
195
static void cmd_scrollback_goto(const char *data)
197
char *datearg, *timearg;
201
if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
204
if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
205
/* go forward/backward n lines */
206
lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
207
gui_window_scroll(active_win, lines);
208
} else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
209
/* go to n'th line. */
210
scrollback_goto_line(atoi(datearg));
212
/* should be timestamp */
213
scrollback_goto_time(datearg, timearg);
216
cmd_params_free(free_arg);
219
/* SYNTAX: SCROLLBACK HOME */
220
static void cmd_scrollback_home(const char *data)
222
TEXT_BUFFER_REC *buffer;
224
buffer = WINDOW_GUI(active_win)->view->buffer;
225
if (buffer->lines_count > 0)
226
gui_window_scroll_line(active_win, buffer->first_line);
229
/* SYNTAX: SCROLLBACK END */
230
static void cmd_scrollback_end(const char *data)
232
TEXT_BUFFER_VIEW_REC *view;
234
view = WINDOW_GUI(active_win)->view;
235
if (view->bottom_startline == NULL ||
236
(view->bottom_startline == view->startline &&
237
view->bottom_subline == view->subline))
240
textbuffer_view_scroll_line(view, view->bottom_startline);
241
gui_window_scroll(active_win, view->bottom_subline);
244
/* SYNTAX: SCROLLBACK REDRAW */
245
static void cmd_scrollback_redraw(void)
248
LINE_REC *line, *next;
250
gui = WINDOW_GUI(active_win);
252
term_refresh_freeze();
253
line = textbuffer_view_get_lines(gui->view);
254
while (line != NULL) {
256
textbuffer_reformat_line(active_win, line);
260
gui_window_redraw(active_win);
264
static void cmd_scrollback_status(void)
267
int window_kb, total_lines, total_kb;
269
total_lines = 0; total_kb = 0;
270
for (tmp = windows; tmp != NULL; tmp = tmp->next) {
271
WINDOW_REC *window = tmp->data;
272
TEXT_BUFFER_VIEW_REC *view;
274
view = WINDOW_GUI(window)->view;
276
window_kb = g_slist_length(view->buffer->text_chunks)*
277
LINE_TEXT_CHUNK_SIZE/1024;
278
total_lines += view->buffer->lines_count;
279
total_kb += window_kb;
280
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
281
"Window %d: %d lines, %dkB of data",
282
window->refnum, view->buffer->lines_count,
286
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
287
"Total: %d lines, %dkB of data",
288
total_lines, total_kb);
291
static void sig_away_changed(SERVER_REC *server)
295
if (!server->usermode_away)
298
for (tmp = windows; tmp != NULL; tmp = tmp->next) {
299
WINDOW_REC *rec = tmp->data;
301
textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
302
"lastlog_last_away");
306
void textbuffer_commands_init(void)
308
command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
309
command_bind("window scroll", NULL, (SIGNAL_FUNC) cmd_window_scroll);
310
command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
311
command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
312
command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
313
command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
314
command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
315
command_bind("scrollback redraw", NULL, (SIGNAL_FUNC) cmd_scrollback_redraw);
316
command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
318
command_set_options("clear", "all");
320
signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
323
void textbuffer_commands_deinit(void)
325
command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
326
command_unbind("window scroll", (SIGNAL_FUNC) cmd_window_scroll);
327
command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
328
command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
329
command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
330
command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
331
command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
332
command_unbind("scrollback redraw", (SIGNAL_FUNC) cmd_scrollback_redraw);
333
command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
335
signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);