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