Merged from silc_1_0_branch (second merge).
[silc.git] / apps / irssi / src / fe-text / gui-readline.c
index bc3429d8b04d8ee0f16338146e86a2f484cdd36c..4de1936e3adc52b853ab2dbb6498172eea1a42cb 100644 (file)
 #include "misc.h"
 #include "settings.h"
 #include "special-vars.h"
+#include "servers.h"
 
 #include "completion.h"
 #include "command-history.h"
 #include "keyboard.h"
 #include "translation.h"
 
-#include "screen.h"
+#include "term.h"
 #include "gui-entry.h"
 #include "gui-windows.h"
+#include "utf8.h"
 
 #include <signal.h>
 
@@ -46,11 +48,30 @@ typedef struct {
 
 static KEYBOARD_REC *keyboard;
 static ENTRY_REDIRECT_REC *redir;
+static int escape_next_key;
 
-char *cutbuffer;
 static int readtag;
 static time_t idle_time;
 
+static void sig_input(void);
+
+void input_listen_init(int handle)
+{
+        GIOChannel *stdin_channel;
+
+       stdin_channel = g_io_channel_unix_new(handle);
+       readtag = g_input_add_full(stdin_channel,
+                                  G_PRIORITY_HIGH, G_INPUT_READ,
+                                  (GInputFunction) sig_input, NULL);
+        g_io_channel_unref(stdin_channel);
+}
+
+void input_listen_deinit(void)
+{
+       g_source_remove(readtag);
+        readtag = -1;
+}
+
 static void handle_key_redirect(int key)
 {
        ENTRY_REDIRECT_KEY_FUNC func;
@@ -60,11 +81,10 @@ static void handle_key_redirect(int key)
        data = redir->data;
        g_free_and_null(redir);
 
+       gui_entry_set_prompt(active_entry, "");
+
        if (func != NULL)
                func(key, data, active_win->active_server, active_win->active);
-
-       gui_entry_remove_perm_prompt();
-       window_update_prompt();
 }
 
 static void handle_entry_redirect(const char *line)
@@ -72,19 +92,18 @@ static void handle_entry_redirect(const char *line)
        ENTRY_REDIRECT_ENTRY_FUNC func;
        void *data;
 
-        gui_entry_set_hidden(FALSE);
+        gui_entry_set_hidden(active_entry, FALSE);
 
        func = (ENTRY_REDIRECT_ENTRY_FUNC) redir->func;
        data = redir->data;
        g_free_and_null(redir);
 
+       gui_entry_set_prompt(active_entry, "");
+
        if (func != NULL) {
                func(line, data, active_win->active_server,
                     active_win->active);
        }
-
-       gui_entry_remove_perm_prompt();
-       window_update_prompt();
 }
 
 static int get_scroll_count(void)
@@ -99,8 +118,9 @@ static int get_scroll_count(void)
        else if (count < 1)
                 count = 1.0/count;
 
-       if (*str == '/')
-               count = WINDOW_GUI(active_win)->parent->height/count;
+       if (*str == '/') {
+               count = (active_mainwin->height-active_mainwin->statusbar_lines)/count;
+       }
        return (int)count;
 }
 
@@ -114,64 +134,91 @@ static void window_next_page(void)
        gui_window_scroll(active_win, get_scroll_count());
 }
 
-void handle_key(int key)
+static void sig_gui_key_pressed(gpointer keyp)
 {
-       char str[3];
+        unichar key;
+       char str[20];
 
-       idle_time = time(NULL);
+       key = GPOINTER_TO_INT(keyp);
 
        if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HOTKEY) {
                handle_key_redirect(key);
                return;
        }
 
-       if (key >= 0 && key < 32) {
+       idle_time = time(NULL);
+
+       if (key < 32) {
                /* control key */
                 str[0] = '^';
-               str[1] = key+'@';
+               str[1] = (char)key+'@';
                 str[2] = '\0';
        } else if (key == 127) {
                 str[0] = '^';
                str[1] = '?';
                 str[2] = '\0';
-       } else {
-               str[0] = key;
+       } else if (!active_entry->utf8) {
+               str[0] = (char)key;
                str[1] = '\0';
+       } else {
+                /* need to convert to utf8 */
+               str[utf16_char_to_utf8(key, str)] = '\0';
+       }
+
+       if (strcmp(str, "^") == 0) {
+               /* change it as ^^ */
+               str[1] = '^';
+               str[2] = '\0';
        }
 
-       if (!key_pressed(keyboard, str)) {
-                /* key wasn't used for anything, print it */
-               gui_entry_insert_char((char) key);
+       if (escape_next_key || !key_pressed(keyboard, str)) {
+               /* key wasn't used for anything, print it */
+                escape_next_key = FALSE;
+               gui_entry_insert_char(active_entry, key);
        }
 }
 
 static void key_send_line(void)
 {
-       int add_history;
-        char *str;
+       HISTORY_REC *history;
+        char *str, *add_history;
+       gint flags = (redir ? redir->flags : 0);
+
+       str = gui_entry_get_text(active_entry);
 
-       str = gui_entry_get_text();
-       if (*str == '\0') return;
+       /* we can't use gui_entry_get_text() later, since the entry might
+          have been destroyed after we get back */
+       add_history = *str == '\0' ? NULL : g_strdup(str);
+       history = command_history_current(active_win);
 
        translate_output(str);
 
-       add_history = TRUE;
        if (redir == NULL) {
                signal_emit("send command", 3, str,
                            active_win->active_server,
                            active_win->active);
        } else {
-               if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
-                       add_history = FALSE;
+               if (flags & ENTRY_REDIRECT_FLAG_HIDDEN && add_history) {
+                       memset(add_history, 0, strlen(add_history));
+                        g_free_and_null(add_history);
+               }
                handle_entry_redirect(str);
+               if (flags & ENTRY_REDIRECT_FLAG_HIDDEN && str)
+                       memset(str, 0, strlen(str));
        }
 
-       if (add_history) {
-               command_history_add(active_win, gui_entry_get_text(),
-                                   FALSE);
+       if (add_history != NULL) {
+               history = command_history_find(history);
+               if (history != NULL)
+                       command_history_add(history, add_history);
+                g_free(add_history);
        }
-       gui_entry_set_text("");
+
+       if (active_entry != NULL)
+               gui_entry_set_text(active_entry, "");
        command_history_clear_pos(active_win);
+
+        g_free(str);
 }
 
 static void key_combo(void)
@@ -181,146 +228,157 @@ static void key_combo(void)
 static void key_backward_history(void)
 {
        const char *text;
+        char *line;
 
-       text = command_history_prev(active_win, gui_entry_get_text());
-       gui_entry_set_text(text);
+       line = gui_entry_get_text(active_entry);
+       text = command_history_prev(active_win, line);
+       gui_entry_set_text(active_entry, text);
+        g_free(line);
 }
 
 static void key_forward_history(void)
 {
        const char *text;
+       char *line;
 
-       text = command_history_next(active_win, gui_entry_get_text());
-       gui_entry_set_text(text);
+       line = gui_entry_get_text(active_entry);
+       text = command_history_next(active_win, line);
+       gui_entry_set_text(active_entry, text);
+        g_free(line);
 }
 
 static void key_beginning_of_line(void)
 {
-        gui_entry_set_pos(0);
+        gui_entry_set_pos(active_entry, 0);
 }
 
 static void key_end_of_line(void)
 {
-       gui_entry_set_pos(strlen(gui_entry_get_text()));
+       gui_entry_set_pos(active_entry, active_entry->text_len);
 }
 
 static void key_backward_character(void)
 {
-       gui_entry_move_pos(-1);
+       gui_entry_move_pos(active_entry, -1);
 }
 
 static void key_forward_character(void)
 {
-       gui_entry_move_pos(1);
+       gui_entry_move_pos(active_entry, 1);
 }
 
 static void key_backward_word(void)
 {
-       gui_entry_move_words(-1);
+       gui_entry_move_words(active_entry, -1, FALSE);
 }
 
 static void key_forward_word(void)
 {
-       gui_entry_move_words(1);
+       gui_entry_move_words(active_entry, 1, FALSE);
 }
 
-static void key_erase_line(void)
+static void key_backward_to_space(void)
 {
-       g_free_not_null(cutbuffer);
-       cutbuffer = g_strdup(gui_entry_get_text());
+       gui_entry_move_words(active_entry, -1, TRUE);
+}
 
-       gui_entry_set_text("");
+static void key_forward_to_space(void)
+{
+       gui_entry_move_words(active_entry, 1, TRUE);
+}
+
+static void key_erase_line(void)
+{
+       gui_entry_set_pos(active_entry, active_entry->text_len);
+       gui_entry_erase(active_entry, active_entry->text_len, TRUE);
 }
 
 static void key_erase_to_beg_of_line(void)
 {
        int pos;
 
-       pos = gui_entry_get_pos();
-       g_free_not_null(cutbuffer);
-       cutbuffer = g_strndup(gui_entry_get_text(), pos);
-
-       gui_entry_erase(pos);
+       pos = gui_entry_get_pos(active_entry);
+       gui_entry_erase(active_entry, pos, TRUE);
 }
 
 static void key_erase_to_end_of_line(void)
 {
        int pos;
 
-       pos = gui_entry_get_pos();
-       g_free_not_null(cutbuffer);
-       cutbuffer = g_strdup(gui_entry_get_text()+pos);
-
-       gui_entry_set_pos(strlen(gui_entry_get_text()));
-       gui_entry_erase(strlen(gui_entry_get_text()) - pos);
+       pos = gui_entry_get_pos(active_entry);
+       gui_entry_set_pos(active_entry, active_entry->text_len);
+       gui_entry_erase(active_entry, active_entry->text_len - pos, TRUE);
 }
 
 static void key_yank_from_cutbuffer(void)
 {
-       if (cutbuffer != NULL)
-               gui_entry_insert_text(cutbuffer);
+       char *cutbuffer;
+
+        cutbuffer = gui_entry_get_cutbuffer(active_entry);
+       if (cutbuffer != NULL) {
+               gui_entry_insert_text(active_entry, cutbuffer);
+                g_free(cutbuffer);
+       }
 }
 
 static void key_transpose_characters(void)
 {
-       char *line, c;
-       int pos;
-
-       pos = gui_entry_get_pos();
-       line = gui_entry_get_text();
-       if (pos == 0 || strlen(line) < 2)
-               return;
-
-       if (line[pos] != '\0')
-               gui_entry_move_pos(1);
-       c = line[gui_entry_get_pos()-1];
-        gui_entry_erase(1);
-       gui_entry_move_pos(-1);
-       gui_entry_insert_char(c);
-        gui_entry_set_pos(pos);
+       gui_entry_transpose_chars(active_entry);
 }
 
 static void key_delete_character(void)
 {
-       if (gui_entry_get_pos() < (int)strlen(gui_entry_get_text())) {
-               gui_entry_move_pos(1);
-               gui_entry_erase(1);
+       if (gui_entry_get_pos(active_entry) < active_entry->text_len) {
+               gui_entry_move_pos(active_entry, 1);
+               gui_entry_erase(active_entry, 1, FALSE);
        }
 }
 
 static void key_backspace(void)
 {
-       gui_entry_erase(1);
+       gui_entry_erase(active_entry, 1, FALSE);
 }
 
 static void key_delete_previous_word(void)
 {
-  gui_entry_erase_word();
+       gui_entry_erase_word(active_entry, FALSE);
 }
 
 static void key_delete_next_word(void)
 {
-       gui_entry_erase_next_word();
+       gui_entry_erase_next_word(active_entry, FALSE);
 }
 
 static void key_delete_to_previous_space(void)
 {
-       gui_entry_erase_word();
+       gui_entry_erase_word(active_entry, TRUE);
 }
 
-void readline(void)
+static void key_delete_to_next_space(void)
 {
-       int key;
+       gui_entry_erase_next_word(active_entry, TRUE);
+}
 
-       for (;;) {
-               key = getch();
-               if (key == ERR
-#ifdef KEY_RESIZE
-                   || key == KEY_RESIZE
-#endif
-                  ) break;
+static void sig_input(void)
+{
+        unichar buffer[128];
+       int ret, i;
 
-               handle_key(key);
+       if (!active_entry) {
+                /* no active entry yet - wait until we have it */
+               return;
+       }
+
+       ret = term_gets(buffer, sizeof(buffer)/sizeof(buffer[0]));
+       if (ret == -1) {
+               /* lost terminal */
+               if (!term_detached)
+                       signal_emit("command quit", 1, "Lost terminal");
+       } else {
+               for (i = 0; i < ret; i++) {
+                       signal_emit("gui key pressed", 1,
+                                   GINT_TO_POINTER(buffer[i]));
+               }
        }
 }
 
@@ -354,32 +412,48 @@ static void key_change_window(const char *data)
        signal_emit("command window goto", 3, data, active_win->active_server, active_win->active);
 }
 
-static void key_word_completion(void)
+static void key_completion(int erase)
 {
-       char *line;
+       char *text, *line;
        int pos;
 
-       pos = gui_entry_get_pos();
+       pos = gui_entry_get_pos(active_entry);
+
+        text = gui_entry_get_text(active_entry);
+       line = word_complete(active_win, text, &pos, erase);
+       g_free(text);
 
-       line = word_complete(active_win, gui_entry_get_text(), &pos);
        if (line != NULL) {
-               gui_entry_set_text(line);
-               gui_entry_set_pos(pos);
+               gui_entry_set_text(active_entry, line);
+               gui_entry_set_pos(active_entry, pos);
                g_free(line);
        }
 }
 
+static void key_word_completion(void)
+{
+        key_completion(FALSE);
+}
+
+static void key_erase_completion(void)
+{
+        key_completion(TRUE);
+}
+
 static void key_check_replaces(void)
 {
-       char *line;
+       char *text, *line;
        int pos;
 
-       pos = gui_entry_get_pos();
+       pos = gui_entry_get_pos(active_entry);
+
+        text = gui_entry_get_text(active_entry);
+       line = auto_word_complete(text, &pos);
+       g_free(text);
 
-       line = auto_word_complete(gui_entry_get_text(), &pos);
        if (line != NULL) {
-               gui_entry_set_text(line);
-               gui_entry_set_pos(pos);
+               gui_entry_set_text(active_entry, line);
+               gui_entry_set_pos(active_entry, pos);
                g_free(line);
        }
 }
@@ -419,62 +493,134 @@ static void key_active_window(void)
        signal_emit("command window goto", 3, "active", active_win->active_server, active_win->active);
 }
 
+static SERVER_REC *get_prev_server(SERVER_REC *current)
+{
+       int pos;
+
+       if (current == NULL) {
+               return servers != NULL ? g_slist_last(servers)->data :
+                       lookup_servers != NULL ?
+                       g_slist_last(lookup_servers)->data : NULL;
+       }
+
+       /* connect2 -> connect1 -> server2 -> server1 -> connect2 -> .. */
+
+       pos = g_slist_index(servers, current);
+       if (pos != -1) {
+               if (pos > 0)
+                       return g_slist_nth(servers, pos-1)->data;
+               if (lookup_servers != NULL)
+                       return g_slist_last(lookup_servers)->data;
+               return g_slist_last(servers)->data;
+       }
+
+       pos = g_slist_index(lookup_servers, current);
+       g_assert(pos >= 0);
+
+       if (pos > 0)
+               return g_slist_nth(lookup_servers, pos-1)->data;
+       if (servers != NULL)
+               return g_slist_last(servers)->data;
+       return g_slist_last(lookup_servers)->data;
+}
+
+static SERVER_REC *get_next_server(SERVER_REC *current)
+{
+       GSList *pos;
+
+       if (current == NULL) {
+               return servers != NULL ? servers->data :
+                       lookup_servers != NULL ? lookup_servers->data : NULL;
+       }
+
+       /* server1 -> server2 -> connect1 -> connect2 -> server1 -> .. */
+
+       pos = g_slist_find(servers, current);
+       if (pos != NULL) {
+               if (pos->next != NULL)
+                       return pos->next->data;
+               if (lookup_servers != NULL)
+                       return lookup_servers->data;
+               return servers->data;
+       }
+
+       pos = g_slist_find(lookup_servers, current);
+       g_assert(pos != NULL);
+
+       if (pos->next != NULL)
+               return pos->next->data;
+       if (servers != NULL)
+               return servers->data;
+       return lookup_servers->data;
+}
+
 static void key_previous_window_item(void)
 {
        SERVER_REC *server;
-       GSList *pos;
 
-       if (active_win->items != NULL)
-               signal_emit("command window item prev", 3, "", active_win->active_server, active_win->active);
-       else if (servers != NULL) {
+       if (active_win->items != NULL) {
+               signal_emit("command window item prev", 3, "",
+                           active_win->active_server, active_win->active);
+       } else if (servers != NULL || lookup_servers != NULL) {
                /* change server */
-               if (active_win->active_server == NULL)
-                       server = servers->data;
-               else {
-                       pos = g_slist_find(servers, active_win->active_server);
-                       server = pos->next != NULL ? pos->next->data : servers->data;
-               }
-               signal_emit("command window server", 3, server->tag, active_win->active_server, active_win->active);
+               server = active_win->active_server;
+               if (server == NULL)
+                       server = active_win->connect_server;
+               server = get_prev_server(server);
+               signal_emit("command window server", 3, server->tag,
+                           active_win->active_server, active_win->active);
        }
 }
 
 static void key_next_window_item(void)
 {
        SERVER_REC *server;
-       int index;
 
        if (active_win->items != NULL) {
                signal_emit("command window item next", 3, "",
                            active_win->active_server, active_win->active);
-       }
-       else if (servers != NULL) {
+       } else if (servers != NULL || lookup_servers != NULL) {
                /* change server */
-               if (active_win->active_server == NULL)
-                       server = servers->data;
-               else {
-                       index = g_slist_index(servers, active_win->active_server);
-                       server = index > 0 ? g_slist_nth(servers, index-1)->data :
-                               g_slist_last(servers)->data;
-               }
+               server = active_win->active_server;
+               if (server == NULL)
+                       server = active_win->connect_server;
+               server = get_next_server(server);
                signal_emit("command window server", 3, server->tag,
                            active_win->active_server, active_win->active);
        }
 }
 
+static void key_escape(void)
+{
+        escape_next_key = TRUE;
+}
+
 static void key_insert_text(const char *data)
 {
        char *str;
 
        str = parse_special_string(data, active_win->active_server,
                                   active_win->active, "", NULL, 0);
-       gui_entry_insert_text(str);
+       gui_entry_insert_text(active_entry, str);
         g_free(str);
 }
 
+static void key_sig_stop(void)
+{
+        term_stop();
+}
+
 static void sig_window_auto_changed(void)
 {
-       command_history_next(active_win, gui_entry_get_text());
-       gui_entry_set_text("");
+       char *text;
+
+       if (active_entry == NULL)
+               return;
+
+        text = gui_entry_get_text(active_entry);
+       command_history_next(active_win, text);
+       gui_entry_set_text(active_entry, "");
+        g_free(text);
 }
 
 static void sig_gui_entry_redirect(SIGNAL_FUNC func, const char *entry,
@@ -486,8 +632,8 @@ static void sig_gui_entry_redirect(SIGNAL_FUNC func, const char *entry,
        redir->data = data;
 
        if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
-               gui_entry_set_hidden(TRUE);
-       gui_entry_set_perm_prompt(entry);
+               gui_entry_set_hidden(active_entry, TRUE);
+       gui_entry_set_prompt(active_entry, entry);
 }
 
 void gui_readline_init(void)
@@ -496,20 +642,22 @@ void gui_readline_init(void)
        char *key, data[MAX_INT_STRLEN];
        int n;
 
-       cutbuffer = NULL;
+        escape_next_key = FALSE;
        redir = NULL;
        idle_time = time(NULL);
-       readtag = g_input_add_full(g_io_channel_unix_new(0),
-                                  G_PRIORITY_HIGH, G_INPUT_READ,
-                                  (GInputFunction) readline, NULL);
+        input_listen_init(STDIN_FILENO);
 
        settings_add_str("history", "scroll_page_count", "/2");
 
        keyboard = keyboard_create(NULL);
         key_configure_freeze();
 
+       key_bind("key", NULL, " ", "space", (SIGNAL_FUNC) key_combo);
        key_bind("key", NULL, "^M", "return", (SIGNAL_FUNC) key_combo);
        key_bind("key", NULL, "^J", "return", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "^H", "backspace", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "^?", "backspace", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "^I", "tab", (SIGNAL_FUNC) key_combo);
 
         /* meta */
        key_bind("key", NULL, "^[", "meta", (SIGNAL_FUNC) key_combo);
@@ -539,11 +687,20 @@ void gui_readline_init(void)
        key_bind("key", NULL, "meta2-2~", "insert", (SIGNAL_FUNC) key_combo);
        key_bind("key", NULL, "meta2-3~", "delete", (SIGNAL_FUNC) key_combo);
 
-        /* cursor movement */
+       key_bind("key", NULL, "meta2-d", "cleft", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "meta2-c", "cright", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "meta2-5D", "cleft", (SIGNAL_FUNC) key_combo);
+       key_bind("key", NULL, "meta2-5C", "cright", (SIGNAL_FUNC) key_combo);
+
+       /* cursor movement */
        key_bind("backward_character", "", "left", NULL, (SIGNAL_FUNC) key_backward_character);
        key_bind("forward_character", "", "right", NULL, (SIGNAL_FUNC) key_forward_character);
-       key_bind("backward_word", "", "meta2-d", NULL, (SIGNAL_FUNC) key_backward_word);
-       key_bind("forward_word", "", "meta2-c", NULL, (SIGNAL_FUNC) key_forward_word);
+       key_bind("backward_word", "", "cleft", NULL, (SIGNAL_FUNC) key_backward_word);
+       key_bind("backward_word", NULL, "meta-b", NULL, (SIGNAL_FUNC) key_backward_word);
+       key_bind("forward_word", "", "cright", NULL, (SIGNAL_FUNC) key_forward_word);
+       key_bind("forward_word", NULL, "meta-f", NULL, (SIGNAL_FUNC) key_forward_word);
+       key_bind("backward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_backward_to_space);
+       key_bind("forward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_forward_to_space);
        key_bind("beginning_of_line", "", "home", NULL, (SIGNAL_FUNC) key_beginning_of_line);
        key_bind("beginning_of_line", NULL, "^A", NULL, (SIGNAL_FUNC) key_beginning_of_line);
        key_bind("end_of_line", "", "end", NULL, (SIGNAL_FUNC) key_end_of_line);
@@ -554,13 +711,13 @@ void gui_readline_init(void)
        key_bind("forward_history", "", "down", NULL, (SIGNAL_FUNC) key_forward_history);
 
         /* line editing */
-       key_bind("backspace", "", "^H", NULL, (SIGNAL_FUNC) key_backspace);
-       key_bind("backspace", "", "^?", NULL, (SIGNAL_FUNC) key_backspace);
+       key_bind("backspace", "", "backspace", NULL, (SIGNAL_FUNC) key_backspace);
        key_bind("delete_character", "", "delete", NULL, (SIGNAL_FUNC) key_delete_character);
        key_bind("delete_character", NULL, "^D", NULL, (SIGNAL_FUNC) key_delete_character);
-       key_bind("delete_next_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_next_word);
-       key_bind("delete_previous_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_previous_word);
+       key_bind("delete_next_word", "meta-d", NULL, NULL, (SIGNAL_FUNC) key_delete_next_word);
+       key_bind("delete_previous_word", "meta-backspace", NULL, NULL, (SIGNAL_FUNC) key_delete_previous_word);
        key_bind("delete_to_previous_space", "", "^W", NULL, (SIGNAL_FUNC) key_delete_to_previous_space);
+       key_bind("delete_to_next_space", "", "", NULL, (SIGNAL_FUNC) key_delete_to_next_space);
        key_bind("erase_line", "", "^U", NULL, (SIGNAL_FUNC) key_erase_line);
        key_bind("erase_to_beg_of_line", "", NULL, NULL, (SIGNAL_FUNC) key_erase_to_beg_of_line);
        key_bind("erase_to_end_of_line", "", "^K", NULL, (SIGNAL_FUNC) key_erase_to_end_of_line);
@@ -569,17 +726,17 @@ void gui_readline_init(void)
 
         /* line transmitting */
        key_bind("send_line", "Execute the input line", "return", NULL, (SIGNAL_FUNC) key_send_line);
-       key_bind("word_completion", "", "^I", NULL, (SIGNAL_FUNC) key_word_completion);
-       key_bind("check_replaces", "Check word replaces", " ", NULL, (SIGNAL_FUNC) key_check_replaces);
-       key_bind("check_replaces", NULL, NULL, NULL, (SIGNAL_FUNC) key_check_replaces);
+       key_bind("word_completion", "", "tab", NULL, (SIGNAL_FUNC) key_word_completion);
+       key_bind("erase_completion", "", "meta-k", NULL, (SIGNAL_FUNC) key_erase_completion);
+       key_bind("check_replaces", "Check word replaces", NULL, NULL, (SIGNAL_FUNC) key_check_replaces);
 
         /* window managing */
        key_bind("previous_window", "Previous window", "^P", NULL, (SIGNAL_FUNC) key_previous_window);
-       key_bind("left_window", "Window in left", "meta-left", NULL, (SIGNAL_FUNC) key_left_window);
        key_bind("next_window", "Next window", "^N", NULL, (SIGNAL_FUNC) key_next_window);
-       key_bind("right_window", "Window in right", "meta-right", NULL, (SIGNAL_FUNC) key_right_window);
        key_bind("upper_window", "Upper window", "meta-up", NULL, (SIGNAL_FUNC) key_upper_window);
        key_bind("lower_window", "Lower window", "meta-down", NULL, (SIGNAL_FUNC) key_lower_window);
+       key_bind("left_window", "Window in left", "meta-left", NULL, (SIGNAL_FUNC) key_left_window);
+       key_bind("right_window", "Window in right", "meta-right", NULL, (SIGNAL_FUNC) key_right_window);
        key_bind("active_window", "Go to next window with the highest activity", "meta-a", NULL, (SIGNAL_FUNC) key_active_window);
        key_bind("next_window_item", "Next channel/query", "^X", NULL, (SIGNAL_FUNC) key_next_window_item);
        key_bind("previous_window_item", "Previous channel/query", NULL, NULL, (SIGNAL_FUNC) key_previous_window_item);
@@ -593,10 +750,14 @@ void gui_readline_init(void)
        key_bind("scroll_end", "End of the window", "", NULL, (SIGNAL_FUNC) key_scroll_end);
 
         /* inserting special input characters to line.. */
+       key_bind("escape_char", "Escape the next keypress", NULL, NULL, (SIGNAL_FUNC) key_escape);
        key_bind("insert_text", "Append text to line", NULL, NULL, (SIGNAL_FUNC) key_insert_text);
 
+        /* autoreplaces */
        key_bind("multi", NULL, "return", "check_replaces;send_line", NULL);
+       key_bind("multi", NULL, "space", "check_replaces;insert_text  ", NULL);
 
+        /* moving between windows */
        for (n = 0; changekeys[n] != '\0'; n++) {
                key = g_strdup_printf("meta-%c", changekeys[n]);
                ltoa(data, n+1);
@@ -604,16 +765,19 @@ void gui_readline_init(void)
                g_free(key);
        }
 
+        /* misc */
+       key_bind("stop_irc", "Send SIGSTOP to client", "^Z", NULL, (SIGNAL_FUNC) key_sig_stop);
+
         key_configure_thaw();
 
        signal_add("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
        signal_add("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
+       signal_add("gui key pressed", (SIGNAL_FUNC) sig_gui_key_pressed);
 }
 
 void gui_readline_deinit(void)
 {
-       g_free_not_null(cutbuffer);
-       g_source_remove(readtag);
+        input_listen_deinit();
 
         key_configure_freeze();
 
@@ -621,6 +785,8 @@ void gui_readline_deinit(void)
        key_unbind("forward_character", (SIGNAL_FUNC) key_forward_character);
        key_unbind("backward_word", (SIGNAL_FUNC) key_backward_word);
        key_unbind("forward_word", (SIGNAL_FUNC) key_forward_word);
+       key_unbind("backward_to_space", (SIGNAL_FUNC) key_backward_to_space);
+       key_unbind("forward_to_space", (SIGNAL_FUNC) key_forward_to_space);
        key_unbind("beginning_of_line", (SIGNAL_FUNC) key_beginning_of_line);
        key_unbind("end_of_line", (SIGNAL_FUNC) key_end_of_line);
 
@@ -631,6 +797,7 @@ void gui_readline_deinit(void)
        key_unbind("delete_character", (SIGNAL_FUNC) key_delete_character);
        key_unbind("delete_next_word", (SIGNAL_FUNC) key_delete_next_word);
        key_unbind("delete_previous_word", (SIGNAL_FUNC) key_delete_previous_word);
+       key_unbind("delete_to_next_space", (SIGNAL_FUNC) key_delete_to_next_space);
        key_unbind("delete_to_previous_space", (SIGNAL_FUNC) key_delete_to_previous_space);
        key_unbind("erase_line", (SIGNAL_FUNC) key_erase_line);
        key_unbind("erase_to_beg_of_line", (SIGNAL_FUNC) key_erase_to_beg_of_line);
@@ -638,13 +805,17 @@ void gui_readline_deinit(void)
        key_unbind("yank_from_cutbuffer", (SIGNAL_FUNC) key_yank_from_cutbuffer);
        key_unbind("transpose_characters", (SIGNAL_FUNC) key_transpose_characters);
 
+       key_unbind("send_line", (SIGNAL_FUNC) key_send_line);
        key_unbind("word_completion", (SIGNAL_FUNC) key_word_completion);
+       key_unbind("erase_completion", (SIGNAL_FUNC) key_erase_completion);
        key_unbind("check_replaces", (SIGNAL_FUNC) key_check_replaces);
 
        key_unbind("previous_window", (SIGNAL_FUNC) key_previous_window);
        key_unbind("next_window", (SIGNAL_FUNC) key_next_window);
        key_unbind("upper_window", (SIGNAL_FUNC) key_upper_window);
        key_unbind("lower_window", (SIGNAL_FUNC) key_lower_window);
+       key_unbind("left_window", (SIGNAL_FUNC) key_left_window);
+       key_unbind("right_window", (SIGNAL_FUNC) key_right_window);
        key_unbind("active_window", (SIGNAL_FUNC) key_active_window);
        key_unbind("next_window_item", (SIGNAL_FUNC) key_next_window_item);
        key_unbind("previous_window_item", (SIGNAL_FUNC) key_previous_window_item);
@@ -655,12 +826,15 @@ void gui_readline_deinit(void)
        key_unbind("scroll_start", (SIGNAL_FUNC) key_scroll_start);
        key_unbind("scroll_end", (SIGNAL_FUNC) key_scroll_end);
 
+       key_unbind("escape_char", (SIGNAL_FUNC) key_escape);
        key_unbind("insert_text", (SIGNAL_FUNC) key_insert_text);
        key_unbind("change_window", (SIGNAL_FUNC) key_change_window);
+       key_unbind("stop_irc", (SIGNAL_FUNC) key_sig_stop);
         keyboard_destroy(keyboard);
 
         key_configure_thaw();
 
        signal_remove("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
        signal_remove("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
+       signal_remove("gui key pressed", (SIGNAL_FUNC) sig_gui_key_pressed);
 }