Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / fe-text / textbuffer-commands.c
1 /*
2  textbuffer-commands.c : Text buffer handling
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "misc.h"
26 #include "levels.h"
27 #include "settings.h"
28 #include "servers.h"
29
30 #include "printtext.h"
31 #include "gui-windows.h"
32 #include "textbuffer-reformat.h"
33
34 /* SYNTAX: CLEAR [-all] [<refnum>] */
35 static void cmd_clear(const char *data)
36 {
37         WINDOW_REC *window;
38         GHashTable *optlist;
39         char *refnum;
40         void *free_arg;
41         GSList *tmp;
42
43         g_return_if_fail(data != NULL);
44
45         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
46                             "clear", &optlist, &refnum)) return;
47
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;
52
53                         textbuffer_view_clear(WINDOW_GUI(window)->view);
54                 }
55         } else if (*refnum != '\0') {
56                 /* clear specified window */
57                 window = window_find_refnum(atoi(refnum));
58                 if (window != NULL)
59                         textbuffer_view_clear(WINDOW_GUI(window)->view);
60         } else {
61                 /* clear active window */
62                 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
63         }
64
65         cmd_params_free(free_arg);
66 }
67
68 static void cmd_window_scroll(const char *data)
69 {
70         GUI_WINDOW_REC *gui;
71
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;
77                 gui->scroll = TRUE;
78         } else if (g_strcasecmp(data, "off") == 0) {
79                 gui->use_scroll = TRUE;
80                 gui->scroll = FALSE;
81         } else if (*data != '\0') {
82                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
83                             TXT_WINDOW_SCROLL_UNKNOWN, data);
84                 return;
85         }
86
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"));
92 }
93
94 static void cmd_scrollback(const char *data, SERVER_REC *server,
95                            WI_ITEM_REC *item)
96 {
97         command_runsub("scrollback", data, server, item);
98 }
99
100 /* SYNTAX: SCROLLBACK CLEAR */
101 static void cmd_scrollback_clear(void)
102 {
103         textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
104 }
105
106 static void scrollback_goto_line(int linenum)
107 {
108         TEXT_BUFFER_VIEW_REC *view;
109
110         view = WINDOW_GUI(active_win)->view;
111         if (view->buffer->lines_count == 0)
112                 return;
113
114         textbuffer_view_scroll_line(view, view->buffer->first_line);
115         gui_window_scroll(active_win, linenum);
116 }
117
118 static void scrollback_goto_time(const char *datearg, const char *timearg)
119 {
120         LINE_REC *line;
121         struct tm tm;
122         time_t now, stamp;
123         int day, month;
124
125         /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
126         now = stamp = time(NULL);
127         if (*datearg == '-') {
128                 /* -<days ago> */
129                 stamp -= atoi(datearg+1) * 3600*24;
130                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
131         } else if (*timearg != '\0') {
132                 /* dd[.mm] */
133                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
134
135                 day = month = 0;
136                 sscanf(datearg, "%d.%d", &day, &month);
137                 if (day <= 0) return;
138
139                 if (month <= 0) {
140                         /* month not given */
141                         if (day > tm.tm_mday) {
142                                 /* last month's day */
143                                 if (tm.tm_mon > 0)
144                                         tm.tm_mon--;
145                                 else {
146                                         /* last year's day.. */
147                                         tm.tm_year--;
148                                         tm.tm_mon = 11;
149                                 }
150                         }
151                 } else {
152                         month--;
153                         if (month > tm.tm_mon)
154                                 tm.tm_year--;
155                         tm.tm_mon = month;
156                 }
157
158                 tm.tm_mday = day;
159                 stamp = mktime(&tm);
160         }
161         else
162         {
163                 /* only time given, move it to timearg */
164                 timearg = datearg;
165         }
166
167         /* hh:mi[:ss] */
168         memcpy(&tm, localtime(&stamp), sizeof(struct tm));
169         tm.tm_sec = 0;
170         sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
171         stamp = mktime(&tm);
172
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. */
176                 stamp -= 3600*24;
177         }
178
179         if (stamp > now) {
180                 /* we're still looking into future, don't bother checking */
181                 return;
182         }
183
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);
189                         break;
190                 }
191         }
192 }
193
194 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
195 static void cmd_scrollback_goto(const char *data)
196 {
197         char *datearg, *timearg;
198         void *free_arg;
199         int lines;
200
201         if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
202                 return;
203
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));
211         } else {
212                 /* should be timestamp */
213                 scrollback_goto_time(datearg, timearg);
214         }
215
216         cmd_params_free(free_arg);
217 }
218
219 /* SYNTAX: SCROLLBACK HOME */
220 static void cmd_scrollback_home(const char *data)
221 {
222         TEXT_BUFFER_REC *buffer;
223
224         buffer = WINDOW_GUI(active_win)->view->buffer;
225         if (buffer->lines_count > 0)
226                 gui_window_scroll_line(active_win, buffer->first_line);
227 }
228
229 /* SYNTAX: SCROLLBACK END */
230 static void cmd_scrollback_end(const char *data)
231 {
232         TEXT_BUFFER_VIEW_REC *view;
233
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))
238                 return;
239
240         textbuffer_view_scroll_line(view, view->bottom_startline);
241         gui_window_scroll(active_win, view->bottom_subline);
242 }
243
244 /* SYNTAX: SCROLLBACK REDRAW */
245 static void cmd_scrollback_redraw(void)
246 {
247         GUI_WINDOW_REC *gui;
248         LINE_REC *line, *next;
249
250         gui = WINDOW_GUI(active_win);
251
252         term_refresh_freeze();
253         line = textbuffer_view_get_lines(gui->view);
254         while (line != NULL) {
255                 next = line->next;
256                 textbuffer_reformat_line(active_win, line);
257                 line = next;
258         }
259
260         gui_window_redraw(active_win);
261         term_refresh_thaw();
262 }
263
264 static void cmd_scrollback_status(void)
265 {
266         GSList *tmp;
267         int window_kb, total_lines, total_kb;
268
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;
273
274                 view = WINDOW_GUI(window)->view;
275
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,
283                           window_kb);
284         }
285
286         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
287                   "Total: %d lines, %dkB of data",
288                   total_lines, total_kb);
289 }
290
291 static void sig_away_changed(SERVER_REC *server)
292 {
293         GSList *tmp;
294
295         if (!server->usermode_away)
296                 return;
297
298         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
299                 WINDOW_REC *rec = tmp->data;
300
301                 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
302                                                     "lastlog_last_away");
303         }
304 }
305
306 void textbuffer_commands_init(void)
307 {
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);
317
318         command_set_options("clear", "all");
319
320         signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
321 }
322
323 void textbuffer_commands_deinit(void)
324 {
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);
334
335         signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);
336 }