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