Added SILC Thread Queue API
[runtime.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 #ifdef HAVE_CUIX
34 #include "cuix.h"
35 #endif
36
37 /* SYNTAX: CLEAR [-all] [<refnum>] */
38 static void cmd_clear(const char *data)
39 {
40         WINDOW_REC *window;
41         GHashTable *optlist;
42         char *refnum;
43         void *free_arg;
44         GSList *tmp;
45
46         g_return_if_fail(data != NULL);
47
48         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
49                             "clear", &optlist, &refnum)) return;
50
51         if (g_hash_table_lookup(optlist, "all") != NULL) {
52                 /* clear all windows */
53                 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
54                         window = tmp->data;
55                         textbuffer_view_clear(WINDOW_GUI(window)->view);
56                 }
57         } else if (*refnum != '\0') {
58                 /* clear specified window */
59                 window = window_find_refnum(atoi(refnum));
60                 if (window != NULL)
61                         textbuffer_view_clear(WINDOW_GUI(window)->view);
62         } else {
63                 /* clear active window */
64                 textbuffer_view_clear(WINDOW_GUI(active_win)->view);
65         }
66
67         cmd_params_free(free_arg);
68 }
69
70 static void cmd_window_scroll(const char *data)
71 {
72         GUI_WINDOW_REC *gui;
73
74         gui = WINDOW_GUI(active_win);
75         if (g_strcasecmp(data, "default") == 0) {
76                 gui->use_scroll = FALSE;
77         } else if (g_strcasecmp(data, "on") == 0) {
78                 gui->use_scroll = TRUE;
79                 gui->scroll = TRUE;
80         } else if (g_strcasecmp(data, "off") == 0) {
81                 gui->use_scroll = TRUE;
82                 gui->scroll = FALSE;
83         } else if (*data != '\0') {
84                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
85                             TXT_WINDOW_SCROLL_UNKNOWN, data);
86                 return;
87         }
88
89         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
90                            TXT_WINDOW_SCROLL, !gui->use_scroll ? "DEFAULT" :
91                            gui->scroll ? "ON" : "OFF");
92         textbuffer_view_set_scroll(gui->view, gui->use_scroll ?
93                                    gui->scroll : settings_get_bool("scroll"));
94 }
95
96 static void cmd_scrollback(const char *data, SERVER_REC *server,
97                            WI_ITEM_REC *item)
98 {
99         command_runsub("scrollback", data, server, item);
100 }
101
102 /* SYNTAX: SCROLLBACK CLEAR [-all] [<refnum>] */
103 static void cmd_scrollback_clear(const char *data)
104 {
105         WINDOW_REC *window;
106         GHashTable *optlist;
107         char *refnum;
108         void *free_arg;
109         GSList *tmp;
110
111         g_return_if_fail(data != NULL);
112
113         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
114                             "scrollback clear", &optlist, &refnum)) return;
115
116         if (g_hash_table_lookup(optlist, "all") != NULL) {
117                 /* clear all windows */
118                 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
119                         window = tmp->data;
120                         textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
121                 }
122         } else if (*refnum != '\0') {
123                 /* clear specified window */
124                 window = window_find_refnum(atoi(refnum));
125                 if (window != NULL)
126                         textbuffer_view_remove_all_lines(WINDOW_GUI(window)->view);
127         } else {
128                 /* clear active window */
129                 textbuffer_view_remove_all_lines(WINDOW_GUI(active_win)->view);
130         }
131
132         cmd_params_free(free_arg);
133 }
134
135 static void scrollback_goto_line(int linenum)
136 {
137         TEXT_BUFFER_VIEW_REC *view;
138
139         view = WINDOW_GUI(active_win)->view;
140         if (view->buffer->lines_count == 0)
141                 return;
142
143         textbuffer_view_scroll_line(view, view->buffer->first_line);
144         gui_window_scroll(active_win, linenum);
145 }
146
147 static void scrollback_goto_time(const char *datearg, const char *timearg)
148 {
149         LINE_REC *line;
150         struct tm tm;
151         time_t now, stamp;
152         int day, month;
153
154         /* [dd[.mm] | -<days ago>] hh:mi[:ss] */
155         now = stamp = time(NULL);
156         if (*datearg == '-') {
157                 /* -<days ago> */
158                 stamp -= atoi(datearg+1) * 3600*24;
159                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
160         } else if (*timearg != '\0') {
161                 /* dd[.mm] */
162                 memcpy(&tm, localtime(&stamp), sizeof(struct tm));
163
164                 day = month = 0;
165                 sscanf(datearg, "%d.%d", &day, &month);
166                 if (day <= 0) return;
167
168                 if (month <= 0) {
169                         /* month not given */
170                         if (day > tm.tm_mday) {
171                                 /* last month's day */
172                                 if (tm.tm_mon > 0)
173                                         tm.tm_mon--;
174                                 else {
175                                         /* last year's day.. */
176                                         tm.tm_year--;
177                                         tm.tm_mon = 11;
178                                 }
179                         }
180                 } else {
181                         month--;
182                         if (month > tm.tm_mon)
183                                 tm.tm_year--;
184                         tm.tm_mon = month;
185                 }
186
187                 tm.tm_mday = day;
188                 stamp = mktime(&tm);
189         }
190         else
191         {
192                 /* only time given, move it to timearg */
193                 timearg = datearg;
194         }
195
196         /* hh:mi[:ss] */
197         memcpy(&tm, localtime(&stamp), sizeof(struct tm));
198         tm.tm_sec = 0;
199         sscanf(timearg, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
200         stamp = mktime(&tm);
201
202         if (stamp > now && timearg == datearg) {
203                 /* we used /SB GOTO 23:59 or something, we want to jump to
204                    previous day's 23:59 time instead of into future. */
205                 stamp -= 3600*24;
206         }
207
208         if (stamp > now) {
209                 /* we're still looking into future, don't bother checking */
210                 return;
211         }
212
213         /* scroll to first line after timestamp */
214         line = textbuffer_view_get_lines(WINDOW_GUI(active_win)->view);
215         for (; line != NULL; line = line->next) {
216                 if (line->info.time >= stamp) {
217                         gui_window_scroll_line(active_win, line);
218                         break;
219                 }
220         }
221 }
222
223 /* SYNTAX: SCROLLBACK GOTO <+|-linecount>|<linenum>|<timestamp> */
224 static void cmd_scrollback_goto(const char *data)
225 {
226         char *datearg, *timearg;
227         void *free_arg;
228         int lines;
229
230         if (!cmd_get_params(data, &free_arg, 2, &datearg, &timearg))
231                 return;
232
233         if (*timearg == '\0' && (*datearg == '-' || *datearg == '+')) {
234                 /* go forward/backward n lines */
235                 lines = atoi(datearg + (*datearg == '-' ? 0 : 1));
236                 gui_window_scroll(active_win, lines);
237         } else if (*timearg == '\0' && is_numeric(datearg, '\0')) {
238                 /* go to n'th line. */
239                 scrollback_goto_line(atoi(datearg));
240         } else {
241                 /* should be timestamp */
242                 scrollback_goto_time(datearg, timearg);
243         }
244
245         cmd_params_free(free_arg);
246 }
247
248 /* SYNTAX: SCROLLBACK HOME */
249 static void cmd_scrollback_home(const char *data)
250 {
251         TEXT_BUFFER_REC *buffer;
252
253         buffer = WINDOW_GUI(active_win)->view->buffer;
254         if (buffer->lines_count > 0)
255                 gui_window_scroll_line(active_win, buffer->first_line);
256 }
257
258 /* SYNTAX: SCROLLBACK END */
259 static void cmd_scrollback_end(const char *data)
260 {
261         TEXT_BUFFER_VIEW_REC *view;
262
263         view = WINDOW_GUI(active_win)->view;
264         if (view->bottom_startline == NULL ||
265             (view->bottom_startline == view->startline &&
266              view->bottom_subline == view->subline))
267                 return;
268
269         textbuffer_view_scroll_line(view, view->bottom_startline);
270         gui_window_scroll(active_win, view->bottom_subline);
271 }
272
273 /* SYNTAX: SCROLLBACK REDRAW */
274 static void cmd_scrollback_redraw(void)
275 {
276         GUI_WINDOW_REC *gui;
277         LINE_REC *line, *next;
278
279         gui = WINDOW_GUI(active_win);
280
281         term_refresh_freeze();
282         line = textbuffer_view_get_lines(gui->view);
283         while (line != NULL) {
284                 next = line->next;
285                 textbuffer_reformat_line(active_win, line);
286                 line = next;
287         }
288
289         gui_window_redraw(active_win);
290         term_refresh_thaw();
291 }
292
293 static void cmd_scrollback_status(void)
294 {
295         GSList *tmp;
296         int window_kb, total_lines, total_kb;
297
298         total_lines = 0; total_kb = 0;
299         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
300                 WINDOW_REC *window = tmp->data;
301                 TEXT_BUFFER_VIEW_REC *view;
302
303                 view = WINDOW_GUI(window)->view;
304
305                 window_kb = g_slist_length(view->buffer->text_chunks)*
306                         LINE_TEXT_CHUNK_SIZE/1024;
307                 total_lines += view->buffer->lines_count;
308                 total_kb += window_kb;
309                 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
310                           "Window %d: %d lines, %dkB of data",
311                           window->refnum, view->buffer->lines_count,
312                           window_kb);
313         }
314
315         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
316                   "Total: %d lines, %dkB of data",
317                   total_lines, total_kb);
318 }
319
320 static void sig_away_changed(SERVER_REC *server)
321 {
322         GSList *tmp;
323
324         if (!server->usermode_away)
325                 return;
326
327         for (tmp = windows; tmp != NULL; tmp = tmp->next) {
328                 WINDOW_REC *rec = tmp->data;
329
330                 textbuffer_view_set_bookmark_bottom(WINDOW_GUI(rec)->view,
331                                                     "lastlog_last_away");
332         }
333 }
334
335 #ifdef HAVE_CUIX
336 static void cmd_cuix(void)
337 {
338     if (!cuix_active)
339     {
340         /* textbuffer_view_clear(WINDOW_GUI(active_win)->view); */
341         cuix_active = 1;
342         cuix_create();
343     } else {
344         /* should never been called */
345         /* cuix_destroy (); */
346         cuix_active = 0;
347         /* textbuffer_view_clear(WINDOW_GUI(active_win)->view); */
348     }
349 }
350 #endif
351
352 void textbuffer_commands_init(void)
353 {
354         command_bind("clear", NULL, (SIGNAL_FUNC) cmd_clear);
355         command_bind("window scroll", NULL, (SIGNAL_FUNC) cmd_window_scroll);
356         command_bind("scrollback", NULL, (SIGNAL_FUNC) cmd_scrollback);
357         command_bind("scrollback clear", NULL, (SIGNAL_FUNC) cmd_scrollback_clear);
358         command_bind("scrollback goto", NULL, (SIGNAL_FUNC) cmd_scrollback_goto);
359         command_bind("scrollback home", NULL, (SIGNAL_FUNC) cmd_scrollback_home);
360         command_bind("scrollback end", NULL, (SIGNAL_FUNC) cmd_scrollback_end);
361         command_bind("scrollback redraw", NULL, (SIGNAL_FUNC) cmd_scrollback_redraw);
362         command_bind("scrollback status", NULL, (SIGNAL_FUNC) cmd_scrollback_status);
363 #ifdef HAVE_CUIX
364         command_bind("cuix", NULL, (SIGNAL_FUNC) cmd_cuix);
365 #endif
366
367         command_set_options("clear", "all");
368         command_set_options("scrollback clear", "all");
369
370         signal_add("away mode changed", (SIGNAL_FUNC) sig_away_changed);
371 }
372
373 void textbuffer_commands_deinit(void)
374 {
375         command_unbind("clear", (SIGNAL_FUNC) cmd_clear);
376         command_unbind("window scroll", (SIGNAL_FUNC) cmd_window_scroll);
377         command_unbind("scrollback", (SIGNAL_FUNC) cmd_scrollback);
378         command_unbind("scrollback clear", (SIGNAL_FUNC) cmd_scrollback_clear);
379         command_unbind("scrollback goto", (SIGNAL_FUNC) cmd_scrollback_goto);
380         command_unbind("scrollback home", (SIGNAL_FUNC) cmd_scrollback_home);
381         command_unbind("scrollback end", (SIGNAL_FUNC) cmd_scrollback_end);
382         command_unbind("scrollback redraw", (SIGNAL_FUNC) cmd_scrollback_redraw);
383         command_unbind("scrollback status", (SIGNAL_FUNC) cmd_scrollback_status);
384 #ifdef HAVE_CUIX
385         command_unbind("cuix", (SIGNAL_FUNC) cmd_cuix);
386 #endif
387
388         signal_remove("away mode changed", (SIGNAL_FUNC) sig_away_changed);
389 }