Merges from Irssi CVS.
[runtime.git] / apps / irssi / src / fe-text / gui-readline.c
index 96e7e074b4d58db8dbe1991fc8c1907e8bb30cc2..c23b42c79d245ca753bfac0e62731d591112d875 100644 (file)
@@ -48,6 +48,7 @@ typedef struct {
 
 static KEYBOARD_REC *keyboard;
 static ENTRY_REDIRECT_REC *redir;
+static int escape_next_key;
 
 static int readtag;
 static time_t idle_time;
@@ -161,8 +162,9 @@ void handle_key(unichar key)
                str[utf16_char_to_utf8(key, str)] = '\0';
        }
 
-       if (!key_pressed(keyboard, str)) {
-                /* key wasn't used for anything, print it */
+       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);
        }
 }
@@ -173,7 +175,7 @@ static void key_send_line(void)
         char *str, *add_history;
 
        str = gui_entry_get_text(active_entry);
-       if (str == NULL || *str == '\0') {
+       if (str == NULL || (*str == '\0' && redir == NULL)) {
                 g_free(str);
                return;
        }
@@ -278,7 +280,7 @@ static void key_forward_to_space(void)
 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);
+       gui_entry_erase(active_entry, active_entry->text_len, TRUE);
 }
 
 static void key_erase_to_beg_of_line(void)
@@ -286,7 +288,7 @@ static void key_erase_to_beg_of_line(void)
        int pos;
 
        pos = gui_entry_get_pos(active_entry);
-       gui_entry_erase(active_entry, pos);
+       gui_entry_erase(active_entry, pos, TRUE);
 }
 
 static void key_erase_to_end_of_line(void)
@@ -295,7 +297,7 @@ static void key_erase_to_end_of_line(void)
 
        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);
+       gui_entry_erase(active_entry, active_entry->text_len - pos, TRUE);
 }
 
 static void key_yank_from_cutbuffer(void)
@@ -303,8 +305,10 @@ static void key_yank_from_cutbuffer(void)
        char *cutbuffer;
 
         cutbuffer = gui_entry_get_cutbuffer(active_entry);
-       if (cutbuffer != NULL)
+       if (cutbuffer != NULL) {
                gui_entry_insert_text(active_entry, cutbuffer);
+                g_free(cutbuffer);
+       }
 }
 
 static void key_transpose_characters(void)
@@ -316,13 +320,13 @@ static void key_delete_character(void)
 {
        if (gui_entry_get_pos(active_entry) < active_entry->text_len) {
                gui_entry_move_pos(active_entry, 1);
-               gui_entry_erase(active_entry, 1);
+               gui_entry_erase(active_entry, 1, FALSE);
        }
 }
 
 static void key_backspace(void)
 {
-       gui_entry_erase(active_entry, 1);
+       gui_entry_erase(active_entry, 1, FALSE);
 }
 
 static void key_delete_previous_word(void)
@@ -519,6 +523,11 @@ static void key_next_window_item(void)
        }
 }
 
+static void key_escape(void)
+{
+        escape_next_key = TRUE;
+}
+
 static void key_insert_text(const char *data)
 {
        char *str;
@@ -566,6 +575,7 @@ void gui_readline_init(void)
        char *key, data[MAX_INT_STRLEN];
        int n;
 
+        escape_next_key = FALSE;
        redir = NULL;
        idle_time = time(NULL);
         input_listen_init(STDIN_FILENO);
@@ -655,11 +665,11 @@ void gui_readline_init(void)
 
         /* 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);
@@ -673,6 +683,7 @@ 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 */
@@ -726,13 +737,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);
@@ -743,6 +758,7 @@ 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);