Merged Irssi 0.8.2 from irssi.org cvs.
[silc.git] / apps / irssi / src / fe-text / gui-readline.c
1 /*
2  gui-readline.c : irssi
3
4     Copyright (C) 1999 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 "misc.h"
24 #include "settings.h"
25 #include "special-vars.h"
26 #include "servers.h"
27
28 #include "completion.h"
29 #include "command-history.h"
30 #include "keyboard.h"
31 #include "translation.h"
32
33 #include "term.h"
34 #include "gui-entry.h"
35 #include "gui-windows.h"
36 #include "utf8.h"
37
38 #include <signal.h>
39
40 typedef void (*ENTRY_REDIRECT_KEY_FUNC) (int key, void *data, SERVER_REC *server, WI_ITEM_REC *item);
41 typedef void (*ENTRY_REDIRECT_ENTRY_FUNC) (const char *line, void *data, SERVER_REC *server, WI_ITEM_REC *item);
42
43 typedef struct {
44         SIGNAL_FUNC func;
45         int flags;
46         void *data;
47 } ENTRY_REDIRECT_REC;
48
49 static KEYBOARD_REC *keyboard;
50 static ENTRY_REDIRECT_REC *redir;
51 static int escape_next_key;
52
53 static int readtag;
54 static time_t idle_time;
55
56 static void sig_input(void);
57
58 void input_listen_init(int handle)
59 {
60         GIOChannel *stdin_channel;
61
62         stdin_channel = g_io_channel_unix_new(handle);
63         readtag = g_input_add_full(stdin_channel,
64                                    G_PRIORITY_HIGH, G_INPUT_READ,
65                                    (GInputFunction) sig_input, NULL);
66         g_io_channel_unref(stdin_channel);
67 }
68
69 void input_listen_deinit(void)
70 {
71         g_source_remove(readtag);
72         readtag = -1;
73 }
74
75 static void handle_key_redirect(int key)
76 {
77         ENTRY_REDIRECT_KEY_FUNC func;
78         void *data;
79
80         func = (ENTRY_REDIRECT_KEY_FUNC) redir->func;
81         data = redir->data;
82         g_free_and_null(redir);
83
84         if (func != NULL)
85                 func(key, data, active_win->active_server, active_win->active);
86
87         gui_entry_set_prompt(active_entry, "");
88 }
89
90 static void handle_entry_redirect(const char *line)
91 {
92         ENTRY_REDIRECT_ENTRY_FUNC func;
93         void *data;
94
95         gui_entry_set_hidden(active_entry, FALSE);
96
97         func = (ENTRY_REDIRECT_ENTRY_FUNC) redir->func;
98         data = redir->data;
99         g_free_and_null(redir);
100
101         if (func != NULL) {
102                 func(line, data, active_win->active_server,
103                      active_win->active);
104         }
105
106         gui_entry_set_prompt(active_entry, "");
107 }
108
109 static int get_scroll_count(void)
110 {
111         const char *str;
112         double count;
113
114         str = settings_get_str("scroll_page_count");
115         count = atof(str + (*str == '/'));
116         if (count <= 0)
117                 count = 1;
118         else if (count < 1)
119                 count = 1.0/count;
120
121         if (*str == '/') {
122                 count = (active_mainwin->height-active_mainwin->statusbar_lines)/count;
123         }
124         return (int)count;
125 }
126
127 static void window_prev_page(void)
128 {
129         gui_window_scroll(active_win, -get_scroll_count());
130 }
131
132 static void window_next_page(void)
133 {
134         gui_window_scroll(active_win, get_scroll_count());
135 }
136
137 void handle_key(unichar key)
138 {
139         char str[20];
140
141         idle_time = time(NULL);
142
143         if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HOTKEY) {
144                 handle_key_redirect(key);
145                 return;
146         }
147
148         if (key < 32) {
149                 /* control key */
150                 str[0] = '^';
151                 str[1] = (char)key+'@';
152                 str[2] = '\0';
153         } else if (key == 127) {
154                 str[0] = '^';
155                 str[1] = '?';
156                 str[2] = '\0';
157         } else if (!active_entry->utf8) {
158                 str[0] = (char)key;
159                 str[1] = '\0';
160         } else {
161                 /* need to convert to utf8 */
162                 str[utf16_char_to_utf8(key, str)] = '\0';
163         }
164
165         if (escape_next_key || !key_pressed(keyboard, str)) {
166                 /* key wasn't used for anything, print it */
167                 escape_next_key = FALSE;
168                 gui_entry_insert_char(active_entry, key);
169         }
170 }
171
172 static void key_send_line(void)
173 {
174         HISTORY_REC *history;
175         char *str, *add_history;
176
177         str = gui_entry_get_text(active_entry);
178         if (str == NULL || (*str == '\0' && redir == NULL)) {
179                 g_free(str);
180                 return;
181         }
182
183         /* we can't use gui_entry_get_text() later, since the entry might
184            have been destroyed after we get back */
185         add_history = g_strdup(str);
186         history = command_history_current(active_win);
187
188         translate_output(str);
189
190         if (redir == NULL) {
191                 signal_emit("send command", 3, str,
192                             active_win->active_server,
193                             active_win->active);
194         } else {
195                 if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
196                         g_free_and_null(add_history);
197                 handle_entry_redirect(str);
198         }
199
200         if (add_history != NULL) {
201                 history = command_history_find(history);
202                 if (history != NULL)
203                         command_history_add(history, add_history);
204                 g_free(add_history);
205         }
206
207         if (active_entry != NULL)
208                 gui_entry_set_text(active_entry, "");
209         command_history_clear_pos(active_win);
210
211         g_free(str);
212 }
213
214 static void key_combo(void)
215 {
216 }
217
218 static void key_backward_history(void)
219 {
220         const char *text;
221         char *line;
222
223         line = gui_entry_get_text(active_entry);
224         text = command_history_prev(active_win, line);
225         gui_entry_set_text(active_entry, text);
226         g_free(line);
227 }
228
229 static void key_forward_history(void)
230 {
231         const char *text;
232         char *line;
233
234         line = gui_entry_get_text(active_entry);
235         text = command_history_next(active_win, line);
236         gui_entry_set_text(active_entry, text);
237         g_free(line);
238 }
239
240 static void key_beginning_of_line(void)
241 {
242         gui_entry_set_pos(active_entry, 0);
243 }
244
245 static void key_end_of_line(void)
246 {
247         gui_entry_set_pos(active_entry, active_entry->text_len);
248 }
249
250 static void key_backward_character(void)
251 {
252         gui_entry_move_pos(active_entry, -1);
253 }
254
255 static void key_forward_character(void)
256 {
257         gui_entry_move_pos(active_entry, 1);
258 }
259
260 static void key_backward_word(void)
261 {
262         gui_entry_move_words(active_entry, -1, FALSE);
263 }
264
265 static void key_forward_word(void)
266 {
267         gui_entry_move_words(active_entry, 1, FALSE);
268 }
269
270 static void key_backward_to_space(void)
271 {
272         gui_entry_move_words(active_entry, -1, TRUE);
273 }
274
275 static void key_forward_to_space(void)
276 {
277         gui_entry_move_words(active_entry, 1, TRUE);
278 }
279
280 static void key_erase_line(void)
281 {
282         gui_entry_set_pos(active_entry, active_entry->text_len);
283         gui_entry_erase(active_entry, active_entry->text_len, TRUE);
284 }
285
286 static void key_erase_to_beg_of_line(void)
287 {
288         int pos;
289
290         pos = gui_entry_get_pos(active_entry);
291         gui_entry_erase(active_entry, pos, TRUE);
292 }
293
294 static void key_erase_to_end_of_line(void)
295 {
296         int pos;
297
298         pos = gui_entry_get_pos(active_entry);
299         gui_entry_set_pos(active_entry, active_entry->text_len);
300         gui_entry_erase(active_entry, active_entry->text_len - pos, TRUE);
301 }
302
303 static void key_yank_from_cutbuffer(void)
304 {
305         char *cutbuffer;
306
307         cutbuffer = gui_entry_get_cutbuffer(active_entry);
308         if (cutbuffer != NULL) {
309                 gui_entry_insert_text(active_entry, cutbuffer);
310                 g_free(cutbuffer);
311         }
312 }
313
314 static void key_transpose_characters(void)
315 {
316         gui_entry_transpose_chars(active_entry);
317 }
318
319 static void key_delete_character(void)
320 {
321         if (gui_entry_get_pos(active_entry) < active_entry->text_len) {
322                 gui_entry_move_pos(active_entry, 1);
323                 gui_entry_erase(active_entry, 1, FALSE);
324         }
325 }
326
327 static void key_backspace(void)
328 {
329         gui_entry_erase(active_entry, 1, FALSE);
330 }
331
332 static void key_delete_previous_word(void)
333 {
334         gui_entry_erase_word(active_entry, FALSE);
335 }
336
337 static void key_delete_next_word(void)
338 {
339         gui_entry_erase_next_word(active_entry, FALSE);
340 }
341
342 static void key_delete_to_previous_space(void)
343 {
344         gui_entry_erase_word(active_entry, TRUE);
345 }
346
347 static void key_delete_to_next_space(void)
348 {
349         gui_entry_erase_next_word(active_entry, TRUE);
350 }
351
352 static void sig_input(void)
353 {
354         unichar buffer[128];
355         int ret, i;
356
357         if (!active_entry) {
358                 /* no active entry yet - wait until we have it */
359                 return;
360         }
361
362         ret = term_gets(buffer, sizeof(buffer)/sizeof(buffer[0]));
363         if (ret == -1) {
364                 /* lost terminal */
365                 if (!term_detached)
366                         signal_emit("command quit", 1, "Lost terminal");
367         } else {
368                 for (i = 0; i < ret; i++)
369                         handle_key(buffer[i]);
370         }
371 }
372
373 time_t get_idle_time(void)
374 {
375         return idle_time;
376 }
377
378 static void key_scroll_backward(void)
379 {
380         window_prev_page();
381 }
382
383 static void key_scroll_forward(void)
384 {
385         window_next_page();
386 }
387
388 static void key_scroll_start(void)
389 {
390         signal_emit("command scrollback home", 3, NULL, active_win->active_server, active_win->active);
391 }
392
393 static void key_scroll_end(void)
394 {
395         signal_emit("command scrollback end", 3, NULL, active_win->active_server, active_win->active);
396 }
397
398 static void key_change_window(const char *data)
399 {
400         signal_emit("command window goto", 3, data, active_win->active_server, active_win->active);
401 }
402
403 static void key_completion(int erase)
404 {
405         char *text, *line;
406         int pos;
407
408         pos = gui_entry_get_pos(active_entry);
409
410         text = gui_entry_get_text(active_entry);
411         line = word_complete(active_win, text, &pos, erase);
412         g_free(text);
413
414         if (line != NULL) {
415                 gui_entry_set_text(active_entry, line);
416                 gui_entry_set_pos(active_entry, pos);
417                 g_free(line);
418         }
419 }
420
421 static void key_word_completion(void)
422 {
423         key_completion(FALSE);
424 }
425
426 static void key_erase_completion(void)
427 {
428         key_completion(TRUE);
429 }
430
431 static void key_check_replaces(void)
432 {
433         char *text, *line;
434         int pos;
435
436         pos = gui_entry_get_pos(active_entry);
437
438         text = gui_entry_get_text(active_entry);
439         line = auto_word_complete(text, &pos);
440         g_free(text);
441
442         if (line != NULL) {
443                 gui_entry_set_text(active_entry, line);
444                 gui_entry_set_pos(active_entry, pos);
445                 g_free(line);
446         }
447 }
448
449 static void key_previous_window(void)
450 {
451         signal_emit("command window previous", 3, "", active_win->active_server, active_win->active);
452 }
453
454 static void key_next_window(void)
455 {
456         signal_emit("command window next", 3, "", active_win->active_server, active_win->active);
457 }
458
459 static void key_left_window(void)
460 {
461         signal_emit("command window left", 3, "", active_win->active_server, active_win->active);
462 }
463
464 static void key_right_window(void)
465 {
466         signal_emit("command window right", 3, "", active_win->active_server, active_win->active);
467 }
468
469 static void key_upper_window(void)
470 {
471         signal_emit("command window up", 3, "", active_win->active_server, active_win->active);
472 }
473
474 static void key_lower_window(void)
475 {
476         signal_emit("command window down", 3, "", active_win->active_server, active_win->active);
477 }
478
479 static void key_active_window(void)
480 {
481         signal_emit("command window goto", 3, "active", active_win->active_server, active_win->active);
482 }
483
484 static void key_previous_window_item(void)
485 {
486         SERVER_REC *server;
487         GSList *pos;
488
489         if (active_win->items != NULL)
490                 signal_emit("command window item prev", 3, "", active_win->active_server, active_win->active);
491         else if (servers != NULL) {
492                 /* change server */
493                 if (active_win->active_server == NULL)
494                         server = servers->data;
495                 else {
496                         pos = g_slist_find(servers, active_win->active_server);
497                         server = pos->next != NULL ? pos->next->data : servers->data;
498                 }
499                 signal_emit("command window server", 3, server->tag, active_win->active_server, active_win->active);
500         }
501 }
502
503 static void key_next_window_item(void)
504 {
505         SERVER_REC *server;
506         int index;
507
508         if (active_win->items != NULL) {
509                 signal_emit("command window item next", 3, "",
510                             active_win->active_server, active_win->active);
511         }
512         else if (servers != NULL) {
513                 /* change server */
514                 if (active_win->active_server == NULL)
515                         server = servers->data;
516                 else {
517                         index = g_slist_index(servers, active_win->active_server);
518                         server = index > 0 ? g_slist_nth(servers, index-1)->data :
519                                 g_slist_last(servers)->data;
520                 }
521                 signal_emit("command window server", 3, server->tag,
522                             active_win->active_server, active_win->active);
523         }
524 }
525
526 static void key_escape(void)
527 {
528         escape_next_key = TRUE;
529 }
530
531 static void key_insert_text(const char *data)
532 {
533         char *str;
534
535         str = parse_special_string(data, active_win->active_server,
536                                    active_win->active, "", NULL, 0);
537         gui_entry_insert_text(active_entry, str);
538         g_free(str);
539 }
540
541 static void key_sig_stop(void)
542 {
543         term_stop();
544 }
545
546 static void sig_window_auto_changed(void)
547 {
548         char *text;
549
550         if (active_entry == NULL)
551                 return;
552
553         text = gui_entry_get_text(active_entry);
554         command_history_next(active_win, text);
555         gui_entry_set_text(active_entry, "");
556         g_free(text);
557 }
558
559 static void sig_gui_entry_redirect(SIGNAL_FUNC func, const char *entry,
560                                    void *flags, void *data)
561 {
562         redir = g_new0(ENTRY_REDIRECT_REC, 1);
563         redir->func = func;
564         redir->flags = GPOINTER_TO_INT(flags);
565         redir->data = data;
566
567         if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
568                 gui_entry_set_hidden(active_entry, TRUE);
569         gui_entry_set_prompt(active_entry, entry);
570 }
571
572 void gui_readline_init(void)
573 {
574         static char changekeys[] = "1234567890qwertyuio";
575         char *key, data[MAX_INT_STRLEN];
576         int n;
577
578         escape_next_key = FALSE;
579         redir = NULL;
580         idle_time = time(NULL);
581         input_listen_init(STDIN_FILENO);
582
583         settings_add_str("history", "scroll_page_count", "/2");
584
585         keyboard = keyboard_create(NULL);
586         key_configure_freeze();
587
588         key_bind("key", NULL, " ", "space", (SIGNAL_FUNC) key_combo);
589         key_bind("key", NULL, "^M", "return", (SIGNAL_FUNC) key_combo);
590         key_bind("key", NULL, "^J", "return", (SIGNAL_FUNC) key_combo);
591         key_bind("key", NULL, "^H", "backspace", (SIGNAL_FUNC) key_combo);
592         key_bind("key", NULL, "^?", "backspace", (SIGNAL_FUNC) key_combo);
593         key_bind("key", NULL, "^I", "tab", (SIGNAL_FUNC) key_combo);
594
595         /* meta */
596         key_bind("key", NULL, "^[", "meta", (SIGNAL_FUNC) key_combo);
597         key_bind("key", NULL, "meta-[", "meta2", (SIGNAL_FUNC) key_combo);
598         key_bind("key", NULL, "meta-O", "meta2", (SIGNAL_FUNC) key_combo);
599         key_bind("key", NULL, "meta-[O", "meta2", (SIGNAL_FUNC) key_combo);
600
601         /* arrow keys */
602         key_bind("key", NULL, "meta2-A", "up", (SIGNAL_FUNC) key_combo);
603         key_bind("key", NULL, "meta2-B", "down", (SIGNAL_FUNC) key_combo);
604         key_bind("key", NULL, "meta2-C", "right", (SIGNAL_FUNC) key_combo);
605         key_bind("key", NULL, "meta2-D", "left", (SIGNAL_FUNC) key_combo);
606
607         key_bind("key", NULL, "meta2-1~", "home", (SIGNAL_FUNC) key_combo);
608         key_bind("key", NULL, "meta2-7~", "home", (SIGNAL_FUNC) key_combo);
609         key_bind("key", NULL, "meta2-H", "home", (SIGNAL_FUNC) key_combo);
610
611         key_bind("key", NULL, "meta2-4~", "end", (SIGNAL_FUNC) key_combo);
612         key_bind("key", NULL, "meta2-8~", "end", (SIGNAL_FUNC) key_combo);
613         key_bind("key", NULL, "meta2-F", "end", (SIGNAL_FUNC) key_combo);
614
615         key_bind("key", NULL, "meta2-5~", "prior", (SIGNAL_FUNC) key_combo);
616         key_bind("key", NULL, "meta2-I", "prior", (SIGNAL_FUNC) key_combo);
617         key_bind("key", NULL, "meta2-6~", "next", (SIGNAL_FUNC) key_combo);
618         key_bind("key", NULL, "meta2-G", "next", (SIGNAL_FUNC) key_combo);
619
620         key_bind("key", NULL, "meta2-2~", "insert", (SIGNAL_FUNC) key_combo);
621         key_bind("key", NULL, "meta2-3~", "delete", (SIGNAL_FUNC) key_combo);
622
623         key_bind("key", NULL, "meta2-d", "cleft", (SIGNAL_FUNC) key_combo);
624         key_bind("key", NULL, "meta2-c", "cright", (SIGNAL_FUNC) key_combo);
625         key_bind("key", NULL, "meta2-5D", "cleft", (SIGNAL_FUNC) key_combo);
626         key_bind("key", NULL, "meta2-5C", "cright", (SIGNAL_FUNC) key_combo);
627
628         /* cursor movement */
629         key_bind("backward_character", "", "left", NULL, (SIGNAL_FUNC) key_backward_character);
630         key_bind("forward_character", "", "right", NULL, (SIGNAL_FUNC) key_forward_character);
631         key_bind("backward_word", "", "cleft", NULL, (SIGNAL_FUNC) key_backward_word);
632         key_bind("backward_word", NULL, "meta-b", NULL, (SIGNAL_FUNC) key_backward_word);
633         key_bind("forward_word", "", "cright", NULL, (SIGNAL_FUNC) key_forward_word);
634         key_bind("forward_word", NULL, "meta-f", NULL, (SIGNAL_FUNC) key_forward_word);
635         key_bind("backward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_backward_to_space);
636         key_bind("forward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_forward_to_space);
637         key_bind("beginning_of_line", "", "home", NULL, (SIGNAL_FUNC) key_beginning_of_line);
638         key_bind("beginning_of_line", NULL, "^A", NULL, (SIGNAL_FUNC) key_beginning_of_line);
639         key_bind("end_of_line", "", "end", NULL, (SIGNAL_FUNC) key_end_of_line);
640         key_bind("end_of_line", NULL, "^E", NULL, (SIGNAL_FUNC) key_end_of_line);
641
642         /* history */
643         key_bind("backward_history", "", "up", NULL, (SIGNAL_FUNC) key_backward_history);
644         key_bind("forward_history", "", "down", NULL, (SIGNAL_FUNC) key_forward_history);
645
646         /* line editing */
647         key_bind("backspace", "", "backspace", NULL, (SIGNAL_FUNC) key_backspace);
648         key_bind("delete_character", "", "delete", NULL, (SIGNAL_FUNC) key_delete_character);
649         key_bind("delete_character", NULL, "^D", NULL, (SIGNAL_FUNC) key_delete_character);
650         key_bind("delete_next_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_next_word);
651         key_bind("delete_previous_word", "meta-backspace", NULL, NULL, (SIGNAL_FUNC) key_delete_previous_word);
652         key_bind("delete_to_previous_space", "", "^W", NULL, (SIGNAL_FUNC) key_delete_to_previous_space);
653         key_bind("delete_to_next_space", "", "", NULL, (SIGNAL_FUNC) key_delete_to_next_space);
654         key_bind("erase_line", "", "^U", NULL, (SIGNAL_FUNC) key_erase_line);
655         key_bind("erase_to_beg_of_line", "", NULL, NULL, (SIGNAL_FUNC) key_erase_to_beg_of_line);
656         key_bind("erase_to_end_of_line", "", "^K", NULL, (SIGNAL_FUNC) key_erase_to_end_of_line);
657         key_bind("yank_from_cutbuffer", "", "^Y", NULL, (SIGNAL_FUNC) key_yank_from_cutbuffer);
658         key_bind("transpose_characters", "Swap current and previous character", "^T", NULL, (SIGNAL_FUNC) key_transpose_characters);
659
660         /* line transmitting */
661         key_bind("send_line", "Execute the input line", "return", NULL, (SIGNAL_FUNC) key_send_line);
662         key_bind("word_completion", "", "tab", NULL, (SIGNAL_FUNC) key_word_completion);
663         key_bind("erase_completion", "", "meta-k", NULL, (SIGNAL_FUNC) key_erase_completion);
664         key_bind("check_replaces", "Check word replaces", NULL, NULL, (SIGNAL_FUNC) key_check_replaces);
665
666         /* window managing */
667         key_bind("previous_window", "Previous window", "^P", NULL, (SIGNAL_FUNC) key_previous_window);
668         key_bind("left_window", "Window in left", "meta-left", NULL, (SIGNAL_FUNC) key_left_window);
669         key_bind("next_window", "Next window", "^N", NULL, (SIGNAL_FUNC) key_next_window);
670         key_bind("right_window", "Window in right", "meta-right", NULL, (SIGNAL_FUNC) key_right_window);
671         key_bind("upper_window", "Upper window", "meta-up", NULL, (SIGNAL_FUNC) key_upper_window);
672         key_bind("lower_window", "Lower window", "meta-down", NULL, (SIGNAL_FUNC) key_lower_window);
673         key_bind("active_window", "Go to next window with the highest activity", "meta-a", NULL, (SIGNAL_FUNC) key_active_window);
674         key_bind("next_window_item", "Next channel/query", "^X", NULL, (SIGNAL_FUNC) key_next_window_item);
675         key_bind("previous_window_item", "Previous channel/query", NULL, NULL, (SIGNAL_FUNC) key_previous_window_item);
676
677         key_bind("refresh_screen", "Redraw screen", "^L", NULL, (SIGNAL_FUNC) irssi_redraw);
678         key_bind("scroll_backward", "Previous page", "prior", NULL, (SIGNAL_FUNC) key_scroll_backward);
679         key_bind("scroll_backward", NULL, "meta-p", NULL, (SIGNAL_FUNC) key_scroll_backward);
680         key_bind("scroll_forward", "Next page", "next", NULL, (SIGNAL_FUNC) key_scroll_forward);
681         key_bind("scroll_forward", NULL, "meta-n", NULL, (SIGNAL_FUNC) key_scroll_forward);
682         key_bind("scroll_start", "Beginning of the window", "", NULL, (SIGNAL_FUNC) key_scroll_start);
683         key_bind("scroll_end", "End of the window", "", NULL, (SIGNAL_FUNC) key_scroll_end);
684
685         /* inserting special input characters to line.. */
686         key_bind("escape_char", "Escape the next keypress", NULL, NULL, (SIGNAL_FUNC) key_escape);
687         key_bind("insert_text", "Append text to line", NULL, NULL, (SIGNAL_FUNC) key_insert_text);
688
689         /* autoreplaces */
690         key_bind("multi", NULL, "return", "check_replaces;send_line", NULL);
691         key_bind("multi", NULL, "space", "check_replaces;insert_text  ", NULL);
692
693         /* moving between windows */
694         for (n = 0; changekeys[n] != '\0'; n++) {
695                 key = g_strdup_printf("meta-%c", changekeys[n]);
696                 ltoa(data, n+1);
697                 key_bind("change_window", "Change window", key, data, (SIGNAL_FUNC) key_change_window);
698                 g_free(key);
699         }
700
701         /* misc */
702         key_bind("stop_irc", "Send SIGSTOP to client", "^Z", NULL, (SIGNAL_FUNC) key_sig_stop);
703
704         key_configure_thaw();
705
706         signal_add("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
707         signal_add("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
708 }
709
710 void gui_readline_deinit(void)
711 {
712         input_listen_deinit();
713
714         key_configure_freeze();
715
716         key_unbind("backward_character", (SIGNAL_FUNC) key_backward_character);
717         key_unbind("forward_character", (SIGNAL_FUNC) key_forward_character);
718         key_unbind("backward_word", (SIGNAL_FUNC) key_backward_word);
719         key_unbind("forward_word", (SIGNAL_FUNC) key_forward_word);
720         key_unbind("backward_to_space", (SIGNAL_FUNC) key_backward_to_space);
721         key_unbind("forward_to_space", (SIGNAL_FUNC) key_forward_to_space);
722         key_unbind("beginning_of_line", (SIGNAL_FUNC) key_beginning_of_line);
723         key_unbind("end_of_line", (SIGNAL_FUNC) key_end_of_line);
724
725         key_unbind("backward_history", (SIGNAL_FUNC) key_backward_history);
726         key_unbind("forward_history", (SIGNAL_FUNC) key_forward_history);
727
728         key_unbind("backspace", (SIGNAL_FUNC) key_backspace);
729         key_unbind("delete_character", (SIGNAL_FUNC) key_delete_character);
730         key_unbind("delete_next_word", (SIGNAL_FUNC) key_delete_next_word);
731         key_unbind("delete_previous_word", (SIGNAL_FUNC) key_delete_previous_word);
732         key_unbind("delete_to_next_space", (SIGNAL_FUNC) key_delete_to_next_space);
733         key_unbind("delete_to_previous_space", (SIGNAL_FUNC) key_delete_to_previous_space);
734         key_unbind("erase_line", (SIGNAL_FUNC) key_erase_line);
735         key_unbind("erase_to_beg_of_line", (SIGNAL_FUNC) key_erase_to_beg_of_line);
736         key_unbind("erase_to_end_of_line", (SIGNAL_FUNC) key_erase_to_end_of_line);
737         key_unbind("yank_from_cutbuffer", (SIGNAL_FUNC) key_yank_from_cutbuffer);
738         key_unbind("transpose_characters", (SIGNAL_FUNC) key_transpose_characters);
739
740         key_unbind("word_completion", (SIGNAL_FUNC) key_word_completion);
741         key_unbind("check_replaces", (SIGNAL_FUNC) key_check_replaces);
742
743         key_unbind("previous_window", (SIGNAL_FUNC) key_previous_window);
744         key_unbind("next_window", (SIGNAL_FUNC) key_next_window);
745         key_unbind("upper_window", (SIGNAL_FUNC) key_upper_window);
746         key_unbind("lower_window", (SIGNAL_FUNC) key_lower_window);
747         key_unbind("active_window", (SIGNAL_FUNC) key_active_window);
748         key_unbind("next_window_item", (SIGNAL_FUNC) key_next_window_item);
749         key_unbind("previous_window_item", (SIGNAL_FUNC) key_previous_window_item);
750
751         key_unbind("refresh_screen", (SIGNAL_FUNC) irssi_redraw);
752         key_unbind("scroll_backward", (SIGNAL_FUNC) key_scroll_backward);
753         key_unbind("scroll_forward", (SIGNAL_FUNC) key_scroll_forward);
754         key_unbind("scroll_start", (SIGNAL_FUNC) key_scroll_start);
755         key_unbind("scroll_end", (SIGNAL_FUNC) key_scroll_end);
756
757         key_unbind("escape_char", (SIGNAL_FUNC) key_escape);
758         key_unbind("insert_text", (SIGNAL_FUNC) key_insert_text);
759         key_unbind("change_window", (SIGNAL_FUNC) key_change_window);
760         key_unbind("stop_irc", (SIGNAL_FUNC) key_sig_stop);
761         keyboard_destroy(keyboard);
762
763         key_configure_thaw();
764
765         signal_remove("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
766         signal_remove("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
767 }