Merge Irssi 0.8.16-rc1
[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 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.
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
33 /* SYNTAX: CLEAR [-all] [<refnum>] */
34 static void cmd_clear(const char *data)
35 {
36         WINDOW_REC *window;
37         GHashTable *optlist;
38         char *refnum;
39         void *free_arg;
40         GSList *tmp;
41
42         g_return_if_fail(data != NULL);
43
44         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
45                             "clear", &optlist, &refnum)) return;
46
47         if (g_hash_table_lookup(optlist, "all") != NULL) {
48                 /* clear all windows */
49                 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
50                         window = tmp->data;
51                         textbuffer_view_clear(WINDOW_GUI(window)->view);
52                 }
53         } else if (*refnum != '\0') {
54                 /* clear specified window */
55                 window = window_find_refnum(atoi(refnum));
56                 if (window != NULL)
57                         textbuffer_view_clear(WINDOW_GUI(window)->view);
58         } else {
59                 /* clear active window */
60                 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
61         }
62
63         cmd_params_free(free_arg);
64 }
65
66 static void cmd_window_scroll(const char *data)
67 {
68         GUI_WINDOW_REC *gui;
69
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;
75                 gui->scroll = TRUE;
76         } else if (g_ascii_strcasecmp(data, "off") == 0) {
77                 gui->use_scroll = TRUE;
78                 gui->scroll = FALSE;
79         } else if (*data != '\0') {
80                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
81                             TXT_WINDOW_SCROLL_UNKNOWN, data);
82                 return;
83         }
84
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"));
90 }
91
92 static void cmd_scrollback(const char *data, SERVER_REC *server,
93                            WI_ITEM_REC *item)
94 {
95         command_runsub("scrollback", data, server, item);
96 }
97
98 /* SYNTAX: SCROLLBACK CLEAR [-all] [<refnum>] */
99 static void cmd_scrollback_clear(const char *data)
100 {
101         WINDOW_REC *window;
102         GHashTable *optlist;
103         char *refnum;
104         void *free_arg;
105         GSList *tmp;
106
107         g_return_if_fail(data != NULL);
108
109         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
110                             "scrollback clear", &optlist, &refnum)) return;
111
112         if (g_hash_table_lookup(optlist, "all") != NULL) {
113                 /* clear all windows */
114                 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
115                         window = tmp->data;
116                         textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
117                 }
118         } else if (*refnum != '\0') {
119                 /* clear specified window */
120                 window = window_find_refnum(atoi(refnum));
121                 if (window != NULL)
122                         textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
123         } else {
124                 /* clear active window */
125                 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
126         }
127
128         cmd_params_free(free_arg);
129 }
130
131 /* SYNTAX: SCROLLBACK LEVELCLEAR [-all] [-level <level>] [<refnum>] */
132 static void cmd_scrollback_levelclear(const char *data)
133 {
134         WINDOW_REC *window;
135         GHashTable *optlist;
136         char *refnum;
137         void *free_arg;
138         GSList *tmp;
139         int level;
140         char *levelarg;
141
142         g_return_if_fail(data != NULL);
143
144         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
145                             "scrollback levelclear", &optlist, &refnum)) return;
146
147         levelarg = g_hash_table_lookup(optlist, "level");
148         level = (levelarg == NULL || *levelarg == '\0') ? 0 :
149                 level2bits(replace_chars(levelarg, ',', ' '), NULL);
150         if (level == 0) {
151                 cmd_params_free(free_arg);
152                 return;
153         }
154
155         if (g_hash_table_lookup(optlist, "all") != NULL) {
156                 /* clear all windows */
157                 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
158                         window = tmp->data;
159                         textbuffer_view_remove_lines_by_level(WINDOW_GUI(window)->view, level);
160                 }
161         } else if (*refnum != '\0') {
162                 /* clear specified window */
163                 window = window_find_refnum(atoi(refnum));
164                 if (window != NULL)
165                         textbuffer_view_remove_lines_by_level(WINDOW_GUI(window)->view, level);
166         } else {
167                 /* clear active window */
168                 textbuffer_view_remove_lines_by_level(WINDOW_GUI(active_win)->view, level);
169         }
170
171         cmd_params_free(free_arg);
172 }
173
174 static void scrollback_goto_line(int linenum)
175 {
176         TEXT_BUFFER_VIEW_REC *view;
177
178         view = WINDOW_GUI(active_win)->view;
179         if (view->buffer->lines_count == 0)
180                 return;
181
182         textbuffer_view_scroll_line(view, view->buffer->first_line);
183         gui_window_scroll(active_win, linenum);
184 }
185
186 static void scrollback_goto_time(const char *datearg, const char *timearg)
187 {
188         LINE_REC *line;
189         struct tm tm;
190         time_t now, stamp;
191         int day, month;
192
193         /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
194         now = stamp = time(NULL);
195         if (*datearg == '-') {
196                 /* -<days ago> */
197                 stamp -= atoi(datearg+1) * 3600*24;
198                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
199         } else if (*timearg != '\0') {
200                 /* dd[.mm] */
201                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
202
203                 day = month = 0;
204                 sscanf(datearg, "%d.%d", &day, &month);
205                 if (day <= 0) return;
206
207                 if (month <= 0) {
208                         /* month not given */
209                         if (day > tm.tm_mday) {
210                                 /* last month's day */
211                                 if (tm.tm_mon > 0)
212                                         tm.tm_mon--;
213                                 else {
214                                         /* last year's day.. */
215                                         tm.tm_year--;
216                                         tm.tm_mon = 11;
217                                 }
218                         }
219                 } else {
220                         month--;
221                         if (month > tm.tm_mon)
222                                 tm.tm_year--;
223                         tm.tm_mon = month;
224                 }
225
226                 tm.tm_mday = day;
227                 stamp = mktime(&tm);
228         }
229         else
230         {
231                 /* only time given, move it to timearg */
232                 timearg = datearg;
233         }
234
235         /* hh:mi[:ss] */
236         memcpy(&tm, localtime(&stamp), sizeof(struct tm));
237         tm.tm_sec = 0;
238         sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
239         stamp = mktime(&tm);
240
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. */
244                 stamp -= 3600*24;
245         }
246
247         if (stamp > now) {
248                 /* we're still looking into future, don't bother checking */
249                 return;
250         }
251
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);
257                         break;
258                 }
259         }
260 }
261
262 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
263 static void cmd_scrollback_goto(const char *data)
264 {
265         char *datearg, *timearg;
266         void *free_arg;
267         int lines;
268
269         if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
270                 return;
271
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));
279         } else {
280                 /* should be timestamp */
281                 scrollback_goto_time(datearg, timearg);
282         }
283
284         cmd_params_free(free_arg);
285 }
286
287 /* SYNTAX: SCROLLBACK HOME */
288 static void cmd_scrollback_home(const char *data)
289 {
290         TEXT_BUFFER_REC *buffer;
291
292         buffer = WINDOW_GUI(active_win)->view->buffer;
293         if (buffer->lines_count > 0)
294                 gui_window_scroll_line(active_win, buffer->first_line);
295 }
296
297 /* SYNTAX: SCROLLBACK END */
298 static void cmd_scrollback_end(const char *data)
299 {
300         TEXT_BUFFER_VIEW_REC *view;
301
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))
306                 return;
307
308         textbuffer_view_scroll_line(view, view->bottom_startline);
309         gui_window_scroll(active_win, view->bottom_subline);
310 }
311
312 static void cmd_scrollback_status(void)
313 {
314         GSList *tmp;
315         int total_lines;
316         size_t window_mem, total_mem;
317
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;
322
323                 view = WINDOW_GUI(window)->view;
324
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));
335         }
336
337         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
338                   "Total: %d lines, %dkB of data",
339                   total_lines, (int)(total_mem / 1024));
340 }
341
342 static void sig_away_changed(SERVER_REC *server)
343 {
344         GSList *tmp;
345
346         if (!server->usermode_away)
347                 return;
348
349         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
350                 WINDOW_REC *rec = tmp->data;
351
352                 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
353                                                     "lastlog_last_away");
354         }
355 }
356
357 void textbuffer_commands_init(void)
358 {
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);
368
369         command_set_options("clear", "all");
370         command_set_options("scrollback clear", "all");
371         command_set_options("scrollback levelclear", "all -level");
372
373         signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
374 }
375
376 void textbuffer_commands_deinit(void)
377 {
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);
387
388         signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);
389 }