4 Copyright (C) 1999 Timo Sirainen
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.
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.
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
25 #include "special-vars.h"
28 #include "completion.h"
29 #include "command-history.h"
31 #include "translation.h"
34 #include "gui-entry.h"
35 #include "gui-windows.h"
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);
49 static KEYBOARD_REC *keyboard;
50 static ENTRY_REDIRECT_REC *redir;
53 static time_t idle_time;
55 static void sig_input(void);
57 void input_listen_init(int handle)
59 GIOChannel *stdin_channel;
61 stdin_channel = g_io_channel_unix_new(handle);
62 readtag = g_input_add_full(stdin_channel,
63 G_PRIORITY_HIGH, G_INPUT_READ,
64 (GInputFunction) sig_input, NULL);
65 g_io_channel_unref(stdin_channel);
68 void input_listen_deinit(void)
70 g_source_remove(readtag);
74 static void handle_key_redirect(int key)
76 ENTRY_REDIRECT_KEY_FUNC func;
79 func = (ENTRY_REDIRECT_KEY_FUNC) redir->func;
81 g_free_and_null(redir);
84 func(key, data, active_win->active_server, active_win->active);
86 gui_entry_set_prompt(active_entry, "");
89 static void handle_entry_redirect(const char *line)
91 ENTRY_REDIRECT_ENTRY_FUNC func;
94 gui_entry_set_hidden(active_entry, FALSE);
96 func = (ENTRY_REDIRECT_ENTRY_FUNC) redir->func;
98 g_free_and_null(redir);
101 func(line, data, active_win->active_server,
105 gui_entry_set_prompt(active_entry, "");
108 static int get_scroll_count(void)
113 str = settings_get_str("scroll_page_count");
114 count = atof(str + (*str == '/'));
121 count = (active_mainwin->height-active_mainwin->statusbar_lines)/count;
126 static void window_prev_page(void)
128 gui_window_scroll(active_win, -get_scroll_count());
131 static void window_next_page(void)
133 gui_window_scroll(active_win, get_scroll_count());
136 void handle_key(unichar key)
140 idle_time = time(NULL);
142 if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HOTKEY) {
143 handle_key_redirect(key);
150 str[1] = (char)key+'@';
152 } else if (key == 127) {
156 } else if (!active_entry->utf8) {
160 /* need to convert to utf8 */
161 str[utf16_char_to_utf8(key, str)] = '\0';
164 if (!key_pressed(keyboard, str)) {
165 /* key wasn't used for anything, print it */
166 gui_entry_insert_char(active_entry, key);
170 static void key_send_line(void)
172 HISTORY_REC *history;
173 char *str, *add_history;
175 str = gui_entry_get_text(active_entry);
176 if (str == NULL || *str == '\0') {
181 /* we can't use gui_entry_get_text() later, since the entry might
182 have been destroyed after we get back */
183 add_history = g_strdup(str);
184 history = command_history_current(active_win);
186 translate_output(str);
189 signal_emit("send command", 3, str,
190 active_win->active_server,
193 if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
194 g_free_and_null(add_history);
195 handle_entry_redirect(str);
198 if (add_history != NULL) {
199 history = command_history_find(history);
201 command_history_add(history, add_history);
205 if (active_entry != NULL)
206 gui_entry_set_text(active_entry, "");
207 command_history_clear_pos(active_win);
212 static void key_combo(void)
216 static void key_backward_history(void)
221 line = gui_entry_get_text(active_entry);
222 text = command_history_prev(active_win, line);
223 gui_entry_set_text(active_entry, text);
227 static void key_forward_history(void)
232 line = gui_entry_get_text(active_entry);
233 text = command_history_next(active_win, line);
234 gui_entry_set_text(active_entry, text);
238 static void key_beginning_of_line(void)
240 gui_entry_set_pos(active_entry, 0);
243 static void key_end_of_line(void)
245 gui_entry_set_pos(active_entry, active_entry->text_len);
248 static void key_backward_character(void)
250 gui_entry_move_pos(active_entry, -1);
253 static void key_forward_character(void)
255 gui_entry_move_pos(active_entry, 1);
258 static void key_backward_word(void)
260 gui_entry_move_words(active_entry, -1, FALSE);
263 static void key_forward_word(void)
265 gui_entry_move_words(active_entry, 1, FALSE);
268 static void key_backward_to_space(void)
270 gui_entry_move_words(active_entry, -1, TRUE);
273 static void key_forward_to_space(void)
275 gui_entry_move_words(active_entry, 1, TRUE);
278 static void key_erase_line(void)
280 gui_entry_set_pos(active_entry, active_entry->text_len);
281 gui_entry_erase(active_entry, active_entry->text_len);
284 static void key_erase_to_beg_of_line(void)
288 pos = gui_entry_get_pos(active_entry);
289 gui_entry_erase(active_entry, pos);
292 static void key_erase_to_end_of_line(void)
296 pos = gui_entry_get_pos(active_entry);
297 gui_entry_set_pos(active_entry, active_entry->text_len);
298 gui_entry_erase(active_entry, active_entry->text_len - pos);
301 static void key_yank_from_cutbuffer(void)
305 cutbuffer = gui_entry_get_cutbuffer(active_entry);
306 if (cutbuffer != NULL)
307 gui_entry_insert_text(active_entry, cutbuffer);
310 static void key_transpose_characters(void)
312 gui_entry_transpose_chars(active_entry);
315 static void key_delete_character(void)
317 if (gui_entry_get_pos(active_entry) < active_entry->text_len) {
318 gui_entry_move_pos(active_entry, 1);
319 gui_entry_erase(active_entry, 1);
323 static void key_backspace(void)
325 gui_entry_erase(active_entry, 1);
328 static void key_delete_previous_word(void)
330 gui_entry_erase_word(active_entry, FALSE);
333 static void key_delete_next_word(void)
335 gui_entry_erase_next_word(active_entry, FALSE);
338 static void key_delete_to_previous_space(void)
340 gui_entry_erase_word(active_entry, TRUE);
343 static void key_delete_to_next_space(void)
345 gui_entry_erase_next_word(active_entry, TRUE);
348 static void sig_input(void)
354 /* no active entry yet - wait until we have it */
358 ret = term_gets(buffer, sizeof(buffer)/sizeof(buffer[0]));
362 signal_emit("command quit", 1, "Lost terminal");
364 for (i = 0; i < ret; i++)
365 handle_key(buffer[i]);
369 time_t get_idle_time(void)
374 static void key_scroll_backward(void)
379 static void key_scroll_forward(void)
384 static void key_scroll_start(void)
386 signal_emit("command scrollback home", 3, NULL, active_win->active_server, active_win->active);
389 static void key_scroll_end(void)
391 signal_emit("command scrollback end", 3, NULL, active_win->active_server, active_win->active);
394 static void key_change_window(const char *data)
396 signal_emit("command window goto", 3, data, active_win->active_server, active_win->active);
399 static void key_completion(int erase)
404 pos = gui_entry_get_pos(active_entry);
406 text = gui_entry_get_text(active_entry);
407 line = word_complete(active_win, text, &pos, erase);
411 gui_entry_set_text(active_entry, line);
412 gui_entry_set_pos(active_entry, pos);
417 static void key_word_completion(void)
419 key_completion(FALSE);
422 static void key_erase_completion(void)
424 key_completion(TRUE);
427 static void key_check_replaces(void)
432 pos = gui_entry_get_pos(active_entry);
434 text = gui_entry_get_text(active_entry);
435 line = auto_word_complete(text, &pos);
439 gui_entry_set_text(active_entry, line);
440 gui_entry_set_pos(active_entry, pos);
445 static void key_previous_window(void)
447 signal_emit("command window previous", 3, "", active_win->active_server, active_win->active);
450 static void key_next_window(void)
452 signal_emit("command window next", 3, "", active_win->active_server, active_win->active);
455 static void key_left_window(void)
457 signal_emit("command window left", 3, "", active_win->active_server, active_win->active);
460 static void key_right_window(void)
462 signal_emit("command window right", 3, "", active_win->active_server, active_win->active);
465 static void key_upper_window(void)
467 signal_emit("command window up", 3, "", active_win->active_server, active_win->active);
470 static void key_lower_window(void)
472 signal_emit("command window down", 3, "", active_win->active_server, active_win->active);
475 static void key_active_window(void)
477 signal_emit("command window goto", 3, "active", active_win->active_server, active_win->active);
480 static void key_previous_window_item(void)
485 if (active_win->items != NULL)
486 signal_emit("command window item prev", 3, "", active_win->active_server, active_win->active);
487 else if (servers != NULL) {
489 if (active_win->active_server == NULL)
490 server = servers->data;
492 pos = g_slist_find(servers, active_win->active_server);
493 server = pos->next != NULL ? pos->next->data : servers->data;
495 signal_emit("command window server", 3, server->tag, active_win->active_server, active_win->active);
499 static void key_next_window_item(void)
504 if (active_win->items != NULL) {
505 signal_emit("command window item next", 3, "",
506 active_win->active_server, active_win->active);
508 else if (servers != NULL) {
510 if (active_win->active_server == NULL)
511 server = servers->data;
513 index = g_slist_index(servers, active_win->active_server);
514 server = index > 0 ? g_slist_nth(servers, index-1)->data :
515 g_slist_last(servers)->data;
517 signal_emit("command window server", 3, server->tag,
518 active_win->active_server, active_win->active);
522 static void key_insert_text(const char *data)
526 str = parse_special_string(data, active_win->active_server,
527 active_win->active, "", NULL, 0);
528 gui_entry_insert_text(active_entry, str);
532 static void key_sig_stop(void)
537 static void sig_window_auto_changed(void)
541 if (active_entry == NULL)
544 text = gui_entry_get_text(active_entry);
545 command_history_next(active_win, text);
546 gui_entry_set_text(active_entry, "");
550 static void sig_gui_entry_redirect(SIGNAL_FUNC func, const char *entry,
551 void *flags, void *data)
553 redir = g_new0(ENTRY_REDIRECT_REC, 1);
555 redir->flags = GPOINTER_TO_INT(flags);
558 if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
559 gui_entry_set_hidden(active_entry, TRUE);
560 gui_entry_set_prompt(active_entry, entry);
563 void gui_readline_init(void)
565 static char changekeys[] = "1234567890qwertyuio";
566 char *key, data[MAX_INT_STRLEN];
570 idle_time = time(NULL);
571 input_listen_init(STDIN_FILENO);
573 settings_add_str("history", "scroll_page_count", "/2");
575 keyboard = keyboard_create(NULL);
576 key_configure_freeze();
578 key_bind("key", NULL, " ", "space", (SIGNAL_FUNC) key_combo);
579 key_bind("key", NULL, "^M", "return", (SIGNAL_FUNC) key_combo);
580 key_bind("key", NULL, "^J", "return", (SIGNAL_FUNC) key_combo);
581 key_bind("key", NULL, "^H", "backspace", (SIGNAL_FUNC) key_combo);
582 key_bind("key", NULL, "^?", "backspace", (SIGNAL_FUNC) key_combo);
583 key_bind("key", NULL, "^I", "tab", (SIGNAL_FUNC) key_combo);
586 key_bind("key", NULL, "^[", "meta", (SIGNAL_FUNC) key_combo);
587 key_bind("key", NULL, "meta-[", "meta2", (SIGNAL_FUNC) key_combo);
588 key_bind("key", NULL, "meta-O", "meta2", (SIGNAL_FUNC) key_combo);
589 key_bind("key", NULL, "meta-[O", "meta2", (SIGNAL_FUNC) key_combo);
592 key_bind("key", NULL, "meta2-A", "up", (SIGNAL_FUNC) key_combo);
593 key_bind("key", NULL, "meta2-B", "down", (SIGNAL_FUNC) key_combo);
594 key_bind("key", NULL, "meta2-C", "right", (SIGNAL_FUNC) key_combo);
595 key_bind("key", NULL, "meta2-D", "left", (SIGNAL_FUNC) key_combo);
597 key_bind("key", NULL, "meta2-1~", "home", (SIGNAL_FUNC) key_combo);
598 key_bind("key", NULL, "meta2-7~", "home", (SIGNAL_FUNC) key_combo);
599 key_bind("key", NULL, "meta2-H", "home", (SIGNAL_FUNC) key_combo);
601 key_bind("key", NULL, "meta2-4~", "end", (SIGNAL_FUNC) key_combo);
602 key_bind("key", NULL, "meta2-8~", "end", (SIGNAL_FUNC) key_combo);
603 key_bind("key", NULL, "meta2-F", "end", (SIGNAL_FUNC) key_combo);
605 key_bind("key", NULL, "meta2-5~", "prior", (SIGNAL_FUNC) key_combo);
606 key_bind("key", NULL, "meta2-I", "prior", (SIGNAL_FUNC) key_combo);
607 key_bind("key", NULL, "meta2-6~", "next", (SIGNAL_FUNC) key_combo);
608 key_bind("key", NULL, "meta2-G", "next", (SIGNAL_FUNC) key_combo);
610 key_bind("key", NULL, "meta2-2~", "insert", (SIGNAL_FUNC) key_combo);
611 key_bind("key", NULL, "meta2-3~", "delete", (SIGNAL_FUNC) key_combo);
613 key_bind("key", NULL, "meta2-d", "cleft", (SIGNAL_FUNC) key_combo);
614 key_bind("key", NULL, "meta2-c", "cright", (SIGNAL_FUNC) key_combo);
615 key_bind("key", NULL, "meta2-5D", "cleft", (SIGNAL_FUNC) key_combo);
616 key_bind("key", NULL, "meta2-5C", "cright", (SIGNAL_FUNC) key_combo);
618 /* cursor movement */
619 key_bind("backward_character", "", "left", NULL, (SIGNAL_FUNC) key_backward_character);
620 key_bind("forward_character", "", "right", NULL, (SIGNAL_FUNC) key_forward_character);
621 key_bind("backward_word", "", "cleft", NULL, (SIGNAL_FUNC) key_backward_word);
622 key_bind("backward_word", NULL, "meta-b", NULL, (SIGNAL_FUNC) key_backward_word);
623 key_bind("forward_word", "", "cright", NULL, (SIGNAL_FUNC) key_forward_word);
624 key_bind("forward_word", NULL, "meta-f", NULL, (SIGNAL_FUNC) key_forward_word);
625 key_bind("backward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_backward_to_space);
626 key_bind("forward_to_space", "", NULL, NULL, (SIGNAL_FUNC) key_forward_to_space);
627 key_bind("beginning_of_line", "", "home", NULL, (SIGNAL_FUNC) key_beginning_of_line);
628 key_bind("beginning_of_line", NULL, "^A", NULL, (SIGNAL_FUNC) key_beginning_of_line);
629 key_bind("end_of_line", "", "end", NULL, (SIGNAL_FUNC) key_end_of_line);
630 key_bind("end_of_line", NULL, "^E", NULL, (SIGNAL_FUNC) key_end_of_line);
633 key_bind("backward_history", "", "up", NULL, (SIGNAL_FUNC) key_backward_history);
634 key_bind("forward_history", "", "down", NULL, (SIGNAL_FUNC) key_forward_history);
637 key_bind("backspace", "", "backspace", NULL, (SIGNAL_FUNC) key_backspace);
638 key_bind("delete_character", "", "delete", NULL, (SIGNAL_FUNC) key_delete_character);
639 key_bind("delete_character", NULL, "^D", NULL, (SIGNAL_FUNC) key_delete_character);
640 key_bind("delete_next_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_next_word);
641 key_bind("delete_previous_word", "meta-backspace", NULL, NULL, (SIGNAL_FUNC) key_delete_previous_word);
642 key_bind("delete_to_previous_space", "", "^W", NULL, (SIGNAL_FUNC) key_delete_to_previous_space);
643 key_bind("delete_to_next_space", "", "", NULL, (SIGNAL_FUNC) key_delete_to_next_space);
644 key_bind("erase_line", "", "^U", NULL, (SIGNAL_FUNC) key_erase_line);
645 key_bind("erase_to_beg_of_line", "", NULL, NULL, (SIGNAL_FUNC) key_erase_to_beg_of_line);
646 key_bind("erase_to_end_of_line", "", "^K", NULL, (SIGNAL_FUNC) key_erase_to_end_of_line);
647 key_bind("yank_from_cutbuffer", "", "^Y", NULL, (SIGNAL_FUNC) key_yank_from_cutbuffer);
648 key_bind("transpose_characters", "Swap current and previous character", "^T", NULL, (SIGNAL_FUNC) key_transpose_characters);
650 /* line transmitting */
651 key_bind("send_line", "Execute the input line", "return", NULL, (SIGNAL_FUNC) key_send_line);
652 key_bind("word_completion", "", "tab", NULL, (SIGNAL_FUNC) key_word_completion);
653 key_bind("erase_completion", "", "meta-k", NULL, (SIGNAL_FUNC) key_erase_completion);
654 key_bind("check_replaces", "Check word replaces", NULL, NULL, (SIGNAL_FUNC) key_check_replaces);
656 /* window managing */
657 key_bind("previous_window", "Previous window", "^P", NULL, (SIGNAL_FUNC) key_previous_window);
658 key_bind("left_window", "Window in left", "meta-left", NULL, (SIGNAL_FUNC) key_left_window);
659 key_bind("next_window", "Next window", "^N", NULL, (SIGNAL_FUNC) key_next_window);
660 key_bind("right_window", "Window in right", "meta-right", NULL, (SIGNAL_FUNC) key_right_window);
661 key_bind("upper_window", "Upper window", "meta-up", NULL, (SIGNAL_FUNC) key_upper_window);
662 key_bind("lower_window", "Lower window", "meta-down", NULL, (SIGNAL_FUNC) key_lower_window);
663 key_bind("active_window", "Go to next window with the highest activity", "meta-a", NULL, (SIGNAL_FUNC) key_active_window);
664 key_bind("next_window_item", "Next channel/query", "^X", NULL, (SIGNAL_FUNC) key_next_window_item);
665 key_bind("previous_window_item", "Previous channel/query", NULL, NULL, (SIGNAL_FUNC) key_previous_window_item);
667 key_bind("refresh_screen", "Redraw screen", "^L", NULL, (SIGNAL_FUNC) irssi_redraw);
668 key_bind("scroll_backward", "Previous page", "prior", NULL, (SIGNAL_FUNC) key_scroll_backward);
669 key_bind("scroll_backward", NULL, "meta-p", NULL, (SIGNAL_FUNC) key_scroll_backward);
670 key_bind("scroll_forward", "Next page", "next", NULL, (SIGNAL_FUNC) key_scroll_forward);
671 key_bind("scroll_forward", NULL, "meta-n", NULL, (SIGNAL_FUNC) key_scroll_forward);
672 key_bind("scroll_start", "Beginning of the window", "", NULL, (SIGNAL_FUNC) key_scroll_start);
673 key_bind("scroll_end", "End of the window", "", NULL, (SIGNAL_FUNC) key_scroll_end);
675 /* inserting special input characters to line.. */
676 key_bind("insert_text", "Append text to line", NULL, NULL, (SIGNAL_FUNC) key_insert_text);
679 key_bind("multi", NULL, "return", "check_replaces;send_line", NULL);
680 key_bind("multi", NULL, "space", "check_replaces;insert_text ", NULL);
682 /* moving between windows */
683 for (n = 0; changekeys[n] != '\0'; n++) {
684 key = g_strdup_printf("meta-%c", changekeys[n]);
686 key_bind("change_window", "Change window", key, data, (SIGNAL_FUNC) key_change_window);
691 key_bind("stop_irc", "Send SIGSTOP to client", "^Z", NULL, (SIGNAL_FUNC) key_sig_stop);
693 key_configure_thaw();
695 signal_add("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
696 signal_add("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
699 void gui_readline_deinit(void)
701 input_listen_deinit();
703 key_configure_freeze();
705 key_unbind("backward_character", (SIGNAL_FUNC) key_backward_character);
706 key_unbind("forward_character", (SIGNAL_FUNC) key_forward_character);
707 key_unbind("backward_word", (SIGNAL_FUNC) key_backward_word);
708 key_unbind("forward_word", (SIGNAL_FUNC) key_forward_word);
709 key_unbind("backward_to_space", (SIGNAL_FUNC) key_backward_to_space);
710 key_unbind("forward_to_space", (SIGNAL_FUNC) key_forward_to_space);
711 key_unbind("beginning_of_line", (SIGNAL_FUNC) key_beginning_of_line);
712 key_unbind("end_of_line", (SIGNAL_FUNC) key_end_of_line);
714 key_unbind("backward_history", (SIGNAL_FUNC) key_backward_history);
715 key_unbind("forward_history", (SIGNAL_FUNC) key_forward_history);
717 key_unbind("backspace", (SIGNAL_FUNC) key_backspace);
718 key_unbind("delete_character", (SIGNAL_FUNC) key_delete_character);
719 key_unbind("delete_next_word", (SIGNAL_FUNC) key_delete_next_word);
720 key_unbind("delete_previous_word", (SIGNAL_FUNC) key_delete_previous_word);
721 key_unbind("delete_to_next_space", (SIGNAL_FUNC) key_delete_to_next_space);
722 key_unbind("delete_to_previous_space", (SIGNAL_FUNC) key_delete_to_previous_space);
723 key_unbind("erase_line", (SIGNAL_FUNC) key_erase_line);
724 key_unbind("erase_to_beg_of_line", (SIGNAL_FUNC) key_erase_to_beg_of_line);
725 key_unbind("erase_to_end_of_line", (SIGNAL_FUNC) key_erase_to_end_of_line);
726 key_unbind("yank_from_cutbuffer", (SIGNAL_FUNC) key_yank_from_cutbuffer);
727 key_unbind("transpose_characters", (SIGNAL_FUNC) key_transpose_characters);
729 key_unbind("word_completion", (SIGNAL_FUNC) key_word_completion);
730 key_unbind("check_replaces", (SIGNAL_FUNC) key_check_replaces);
732 key_unbind("previous_window", (SIGNAL_FUNC) key_previous_window);
733 key_unbind("next_window", (SIGNAL_FUNC) key_next_window);
734 key_unbind("upper_window", (SIGNAL_FUNC) key_upper_window);
735 key_unbind("lower_window", (SIGNAL_FUNC) key_lower_window);
736 key_unbind("active_window", (SIGNAL_FUNC) key_active_window);
737 key_unbind("next_window_item", (SIGNAL_FUNC) key_next_window_item);
738 key_unbind("previous_window_item", (SIGNAL_FUNC) key_previous_window_item);
740 key_unbind("refresh_screen", (SIGNAL_FUNC) irssi_redraw);
741 key_unbind("scroll_backward", (SIGNAL_FUNC) key_scroll_backward);
742 key_unbind("scroll_forward", (SIGNAL_FUNC) key_scroll_forward);
743 key_unbind("scroll_start", (SIGNAL_FUNC) key_scroll_start);
744 key_unbind("scroll_end", (SIGNAL_FUNC) key_scroll_end);
746 key_unbind("insert_text", (SIGNAL_FUNC) key_insert_text);
747 key_unbind("change_window", (SIGNAL_FUNC) key_change_window);
748 key_unbind("stop_irc", (SIGNAL_FUNC) key_sig_stop);
749 keyboard_destroy(keyboard);
751 key_configure_thaw();
753 signal_remove("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
754 signal_remove("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);