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