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"
27 #include "completion.h"
28 #include "command-history.h"
30 #include "translation.h"
33 #include "gui-entry.h"
34 #include "gui-windows.h"
38 typedef void (*ENTRY_REDIRECT_KEY_FUNC) (int key, void *data, SERVER_REC *server, WI_ITEM_REC *item);
39 typedef void (*ENTRY_REDIRECT_ENTRY_FUNC) (const char *line, void *data, SERVER_REC *server, WI_ITEM_REC *item);
47 static KEYBOARD_REC *keyboard;
48 static ENTRY_REDIRECT_REC *redir;
52 static time_t idle_time;
54 static void handle_key_redirect(int key)
56 ENTRY_REDIRECT_KEY_FUNC func;
59 func = (ENTRY_REDIRECT_KEY_FUNC) redir->func;
61 g_free_and_null(redir);
64 func(key, data, active_win->active_server, active_win->active);
66 gui_entry_remove_perm_prompt();
67 window_update_prompt();
70 static void handle_entry_redirect(const char *line)
72 ENTRY_REDIRECT_ENTRY_FUNC func;
75 gui_entry_set_hidden(FALSE);
77 func = (ENTRY_REDIRECT_ENTRY_FUNC) redir->func;
79 g_free_and_null(redir);
82 func(line, data, active_win->active_server,
86 gui_entry_remove_perm_prompt();
87 window_update_prompt();
90 static int get_scroll_count(void)
95 str = settings_get_str("scroll_page_count");
96 count = atof(str + (*str == '/'));
103 count = WINDOW_GUI(active_win)->parent->height/count;
107 static void window_prev_page(void)
109 gui_window_scroll(active_win, -get_scroll_count());
112 static void window_next_page(void)
114 gui_window_scroll(active_win, get_scroll_count());
117 void handle_key(int key)
121 idle_time = time(NULL);
123 if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HOTKEY) {
124 handle_key_redirect(key);
128 if (key >= 0 && key < 32) {
133 } else if (key == 127) {
142 if (!key_pressed(keyboard, str)) {
143 /* key wasn't used for anything, print it */
144 gui_entry_insert_char((char) key);
148 static void key_send_line(void)
153 str = gui_entry_get_text();
154 if (*str == '\0') return;
156 translate_output(str);
160 signal_emit("send command", 3, str,
161 active_win->active_server,
164 if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
166 handle_entry_redirect(str);
170 command_history_add(active_win, gui_entry_get_text(),
173 gui_entry_set_text("");
174 command_history_clear_pos(active_win);
177 static void key_combo(void)
181 static void key_backward_history(void)
185 text = command_history_prev(active_win, gui_entry_get_text());
186 gui_entry_set_text(text);
189 static void key_forward_history(void)
193 text = command_history_next(active_win, gui_entry_get_text());
194 gui_entry_set_text(text);
197 static void key_beginning_of_line(void)
199 gui_entry_set_pos(0);
202 static void key_end_of_line(void)
204 gui_entry_set_pos(strlen(gui_entry_get_text()));
207 static void key_backward_character(void)
209 gui_entry_move_pos(-1);
212 static void key_forward_character(void)
214 gui_entry_move_pos(1);
217 static void key_backward_word(void)
219 gui_entry_move_words(-1);
222 static void key_forward_word(void)
224 gui_entry_move_words(1);
227 static void key_erase_line(void)
229 g_free_not_null(cutbuffer);
230 cutbuffer = g_strdup(gui_entry_get_text());
232 gui_entry_set_text("");
235 static void key_erase_to_beg_of_line(void)
239 pos = gui_entry_get_pos();
240 g_free_not_null(cutbuffer);
241 cutbuffer = g_strndup(gui_entry_get_text(), pos);
243 gui_entry_erase(pos);
246 static void key_erase_to_end_of_line(void)
250 pos = gui_entry_get_pos();
251 g_free_not_null(cutbuffer);
252 cutbuffer = g_strdup(gui_entry_get_text()+pos);
254 gui_entry_set_pos(strlen(gui_entry_get_text()));
255 gui_entry_erase(strlen(gui_entry_get_text()) - pos);
258 static void key_yank_from_cutbuffer(void)
260 if (cutbuffer != NULL)
261 gui_entry_insert_text(cutbuffer);
264 static void key_transpose_characters(void)
269 pos = gui_entry_get_pos();
270 line = gui_entry_get_text();
271 if (pos == 0 || strlen(line) < 2)
274 if (line[pos] != '\0')
275 gui_entry_move_pos(1);
276 c = line[gui_entry_get_pos()-1];
278 gui_entry_move_pos(-1);
279 gui_entry_insert_char(c);
280 gui_entry_set_pos(pos);
283 static void key_delete_character(void)
285 if (gui_entry_get_pos() < (int)strlen(gui_entry_get_text())) {
286 gui_entry_move_pos(1);
291 static void key_backspace(void)
296 static void key_delete_previous_word(void)
298 gui_entry_erase_word();
301 static void key_delete_next_word(void)
303 gui_entry_erase_next_word();
306 static void key_delete_to_previous_space(void)
308 gui_entry_erase_word();
327 time_t get_idle_time(void)
332 static void key_scroll_backward(void)
337 static void key_scroll_forward(void)
342 static void key_scroll_start(void)
344 signal_emit("command scrollback home", 3, NULL, active_win->active_server, active_win->active);
347 static void key_scroll_end(void)
349 signal_emit("command scrollback end", 3, NULL, active_win->active_server, active_win->active);
352 static void key_change_window(const char *data)
354 signal_emit("command window goto", 3, data, active_win->active_server, active_win->active);
357 static void key_word_completion(void)
362 pos = gui_entry_get_pos();
364 line = word_complete(active_win, gui_entry_get_text(), &pos);
366 gui_entry_set_text(line);
367 gui_entry_set_pos(pos);
372 static void key_check_replaces(void)
377 pos = gui_entry_get_pos();
379 line = auto_word_complete(gui_entry_get_text(), &pos);
381 gui_entry_set_text(line);
382 gui_entry_set_pos(pos);
387 static void key_previous_window(void)
389 signal_emit("command window previous", 3, "", active_win->active_server, active_win->active);
392 static void key_next_window(void)
394 signal_emit("command window next", 3, "", active_win->active_server, active_win->active);
397 static void key_left_window(void)
399 signal_emit("command window left", 3, "", active_win->active_server, active_win->active);
402 static void key_right_window(void)
404 signal_emit("command window right", 3, "", active_win->active_server, active_win->active);
407 static void key_upper_window(void)
409 signal_emit("command window up", 3, "", active_win->active_server, active_win->active);
412 static void key_lower_window(void)
414 signal_emit("command window down", 3, "", active_win->active_server, active_win->active);
417 static void key_active_window(void)
419 signal_emit("command window goto", 3, "active", active_win->active_server, active_win->active);
422 static void key_previous_window_item(void)
427 if (active_win->items != NULL)
428 signal_emit("command window item prev", 3, "", active_win->active_server, active_win->active);
429 else if (servers != NULL) {
431 if (active_win->active_server == NULL)
432 server = servers->data;
434 pos = g_slist_find(servers, active_win->active_server);
435 server = pos->next != NULL ? pos->next->data : servers->data;
437 signal_emit("command window server", 3, server->tag, active_win->active_server, active_win->active);
441 static void key_next_window_item(void)
446 if (active_win->items != NULL) {
447 signal_emit("command window item next", 3, "",
448 active_win->active_server, active_win->active);
450 else if (servers != NULL) {
452 if (active_win->active_server == NULL)
453 server = servers->data;
455 index = g_slist_index(servers, active_win->active_server);
456 server = index > 0 ? g_slist_nth(servers, index-1)->data :
457 g_slist_last(servers)->data;
459 signal_emit("command window server", 3, server->tag,
460 active_win->active_server, active_win->active);
464 static void key_insert_text(const char *data)
468 str = parse_special_string(data, active_win->active_server,
469 active_win->active, "", NULL, 0);
470 gui_entry_insert_text(str);
474 static void sig_window_auto_changed(void)
476 command_history_next(active_win, gui_entry_get_text());
477 gui_entry_set_text("");
480 static void sig_gui_entry_redirect(SIGNAL_FUNC func, const char *entry,
481 void *flags, void *data)
483 redir = g_new0(ENTRY_REDIRECT_REC, 1);
485 redir->flags = GPOINTER_TO_INT(flags);
488 if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN)
489 gui_entry_set_hidden(TRUE);
490 gui_entry_set_perm_prompt(entry);
493 void gui_readline_init(void)
495 static char changekeys[] = "1234567890qwertyuio";
496 char *key, data[MAX_INT_STRLEN];
501 idle_time = time(NULL);
502 readtag = g_input_add_full(g_io_channel_unix_new(0),
503 G_PRIORITY_HIGH, G_INPUT_READ,
504 (GInputFunction) readline, NULL);
506 settings_add_str("history", "scroll_page_count", "/2");
508 keyboard = keyboard_create(NULL);
509 key_configure_freeze();
511 key_bind("key", NULL, "^M", "return", (SIGNAL_FUNC) key_combo);
512 key_bind("key", NULL, "^J", "return", (SIGNAL_FUNC) key_combo);
515 key_bind("key", NULL, "^[", "meta", (SIGNAL_FUNC) key_combo);
516 key_bind("key", NULL, "meta-[", "meta2", (SIGNAL_FUNC) key_combo);
517 key_bind("key", NULL, "meta-O", "meta2", (SIGNAL_FUNC) key_combo);
518 key_bind("key", NULL, "meta-[O", "meta2", (SIGNAL_FUNC) key_combo);
521 key_bind("key", NULL, "meta2-A", "up", (SIGNAL_FUNC) key_combo);
522 key_bind("key", NULL, "meta2-B", "down", (SIGNAL_FUNC) key_combo);
523 key_bind("key", NULL, "meta2-C", "right", (SIGNAL_FUNC) key_combo);
524 key_bind("key", NULL, "meta2-D", "left", (SIGNAL_FUNC) key_combo);
526 key_bind("key", NULL, "meta2-1~", "home", (SIGNAL_FUNC) key_combo);
527 key_bind("key", NULL, "meta2-7~", "home", (SIGNAL_FUNC) key_combo);
528 key_bind("key", NULL, "meta2-H", "home", (SIGNAL_FUNC) key_combo);
530 key_bind("key", NULL, "meta2-4~", "end", (SIGNAL_FUNC) key_combo);
531 key_bind("key", NULL, "meta2-8~", "end", (SIGNAL_FUNC) key_combo);
532 key_bind("key", NULL, "meta2-F", "end", (SIGNAL_FUNC) key_combo);
534 key_bind("key", NULL, "meta2-5~", "prior", (SIGNAL_FUNC) key_combo);
535 key_bind("key", NULL, "meta2-I", "prior", (SIGNAL_FUNC) key_combo);
536 key_bind("key", NULL, "meta2-6~", "next", (SIGNAL_FUNC) key_combo);
537 key_bind("key", NULL, "meta2-G", "next", (SIGNAL_FUNC) key_combo);
539 key_bind("key", NULL, "meta2-2~", "insert", (SIGNAL_FUNC) key_combo);
540 key_bind("key", NULL, "meta2-3~", "delete", (SIGNAL_FUNC) key_combo);
542 /* cursor movement */
543 key_bind("backward_character", "", "left", NULL, (SIGNAL_FUNC) key_backward_character);
544 key_bind("forward_character", "", "right", NULL, (SIGNAL_FUNC) key_forward_character);
545 key_bind("backward_word", "", "meta2-d", NULL, (SIGNAL_FUNC) key_backward_word);
546 key_bind("forward_word", "", "meta2-c", NULL, (SIGNAL_FUNC) key_forward_word);
547 key_bind("beginning_of_line", "", "home", NULL, (SIGNAL_FUNC) key_beginning_of_line);
548 key_bind("beginning_of_line", NULL, "^A", NULL, (SIGNAL_FUNC) key_beginning_of_line);
549 key_bind("end_of_line", "", "end", NULL, (SIGNAL_FUNC) key_end_of_line);
550 key_bind("end_of_line", NULL, "^E", NULL, (SIGNAL_FUNC) key_end_of_line);
553 key_bind("backward_history", "", "up", NULL, (SIGNAL_FUNC) key_backward_history);
554 key_bind("forward_history", "", "down", NULL, (SIGNAL_FUNC) key_forward_history);
557 key_bind("backspace", "", "^H", NULL, (SIGNAL_FUNC) key_backspace);
558 key_bind("backspace", "", "^?", NULL, (SIGNAL_FUNC) key_backspace);
559 key_bind("delete_character", "", "delete", NULL, (SIGNAL_FUNC) key_delete_character);
560 key_bind("delete_character", NULL, "^D", NULL, (SIGNAL_FUNC) key_delete_character);
561 key_bind("delete_next_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_next_word);
562 key_bind("delete_previous_word", "", NULL, NULL, (SIGNAL_FUNC) key_delete_previous_word);
563 key_bind("delete_to_previous_space", "", "^W", NULL, (SIGNAL_FUNC) key_delete_to_previous_space);
564 key_bind("erase_line", "", "^U", NULL, (SIGNAL_FUNC) key_erase_line);
565 key_bind("erase_to_beg_of_line", "", NULL, NULL, (SIGNAL_FUNC) key_erase_to_beg_of_line);
566 key_bind("erase_to_end_of_line", "", "^K", NULL, (SIGNAL_FUNC) key_erase_to_end_of_line);
567 key_bind("yank_from_cutbuffer", "", "^Y", NULL, (SIGNAL_FUNC) key_yank_from_cutbuffer);
568 key_bind("transpose_characters", "Swap current and previous character", "^T", NULL, (SIGNAL_FUNC) key_transpose_characters);
570 /* line transmitting */
571 key_bind("send_line", "Execute the input line", "return", NULL, (SIGNAL_FUNC) key_send_line);
572 key_bind("word_completion", "", "^I", NULL, (SIGNAL_FUNC) key_word_completion);
573 key_bind("check_replaces", "Check word replaces", " ", NULL, (SIGNAL_FUNC) key_check_replaces);
574 key_bind("check_replaces", NULL, NULL, NULL, (SIGNAL_FUNC) key_check_replaces);
576 /* window managing */
577 key_bind("previous_window", "Previous window", "^P", NULL, (SIGNAL_FUNC) key_previous_window);
578 key_bind("left_window", "Window in left", "meta-left", NULL, (SIGNAL_FUNC) key_left_window);
579 key_bind("next_window", "Next window", "^N", NULL, (SIGNAL_FUNC) key_next_window);
580 key_bind("right_window", "Window in right", "meta-right", NULL, (SIGNAL_FUNC) key_right_window);
581 key_bind("upper_window", "Upper window", "meta-up", NULL, (SIGNAL_FUNC) key_upper_window);
582 key_bind("lower_window", "Lower window", "meta-down", NULL, (SIGNAL_FUNC) key_lower_window);
583 key_bind("active_window", "Go to next window with the highest activity", "meta-a", NULL, (SIGNAL_FUNC) key_active_window);
584 key_bind("next_window_item", "Next channel/query", "^X", NULL, (SIGNAL_FUNC) key_next_window_item);
585 key_bind("previous_window_item", "Previous channel/query", NULL, NULL, (SIGNAL_FUNC) key_previous_window_item);
587 key_bind("refresh_screen", "Redraw screen", "^L", NULL, (SIGNAL_FUNC) irssi_redraw);
588 key_bind("scroll_backward", "Previous page", "prior", NULL, (SIGNAL_FUNC) key_scroll_backward);
589 key_bind("scroll_backward", NULL, "meta-p", NULL, (SIGNAL_FUNC) key_scroll_backward);
590 key_bind("scroll_forward", "Next page", "next", NULL, (SIGNAL_FUNC) key_scroll_forward);
591 key_bind("scroll_forward", NULL, "meta-n", NULL, (SIGNAL_FUNC) key_scroll_forward);
592 key_bind("scroll_start", "Beginning of the window", "", NULL, (SIGNAL_FUNC) key_scroll_start);
593 key_bind("scroll_end", "End of the window", "", NULL, (SIGNAL_FUNC) key_scroll_end);
595 /* inserting special input characters to line.. */
596 key_bind("insert_text", "Append text to line", NULL, NULL, (SIGNAL_FUNC) key_insert_text);
598 key_bind("multi", NULL, "return", "check_replaces;send_line", NULL);
600 for (n = 0; changekeys[n] != '\0'; n++) {
601 key = g_strdup_printf("meta-%c", changekeys[n]);
603 key_bind("change_window", "Change window", key, data, (SIGNAL_FUNC) key_change_window);
607 key_configure_thaw();
609 signal_add("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
610 signal_add("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);
613 void gui_readline_deinit(void)
615 g_free_not_null(cutbuffer);
616 g_source_remove(readtag);
618 key_configure_freeze();
620 key_unbind("backward_character", (SIGNAL_FUNC) key_backward_character);
621 key_unbind("forward_character", (SIGNAL_FUNC) key_forward_character);
622 key_unbind("backward_word", (SIGNAL_FUNC) key_backward_word);
623 key_unbind("forward_word", (SIGNAL_FUNC) key_forward_word);
624 key_unbind("beginning_of_line", (SIGNAL_FUNC) key_beginning_of_line);
625 key_unbind("end_of_line", (SIGNAL_FUNC) key_end_of_line);
627 key_unbind("backward_history", (SIGNAL_FUNC) key_backward_history);
628 key_unbind("forward_history", (SIGNAL_FUNC) key_forward_history);
630 key_unbind("backspace", (SIGNAL_FUNC) key_backspace);
631 key_unbind("delete_character", (SIGNAL_FUNC) key_delete_character);
632 key_unbind("delete_next_word", (SIGNAL_FUNC) key_delete_next_word);
633 key_unbind("delete_previous_word", (SIGNAL_FUNC) key_delete_previous_word);
634 key_unbind("delete_to_previous_space", (SIGNAL_FUNC) key_delete_to_previous_space);
635 key_unbind("erase_line", (SIGNAL_FUNC) key_erase_line);
636 key_unbind("erase_to_beg_of_line", (SIGNAL_FUNC) key_erase_to_beg_of_line);
637 key_unbind("erase_to_end_of_line", (SIGNAL_FUNC) key_erase_to_end_of_line);
638 key_unbind("yank_from_cutbuffer", (SIGNAL_FUNC) key_yank_from_cutbuffer);
639 key_unbind("transpose_characters", (SIGNAL_FUNC) key_transpose_characters);
641 key_unbind("word_completion", (SIGNAL_FUNC) key_word_completion);
642 key_unbind("check_replaces", (SIGNAL_FUNC) key_check_replaces);
644 key_unbind("previous_window", (SIGNAL_FUNC) key_previous_window);
645 key_unbind("next_window", (SIGNAL_FUNC) key_next_window);
646 key_unbind("upper_window", (SIGNAL_FUNC) key_upper_window);
647 key_unbind("lower_window", (SIGNAL_FUNC) key_lower_window);
648 key_unbind("active_window", (SIGNAL_FUNC) key_active_window);
649 key_unbind("next_window_item", (SIGNAL_FUNC) key_next_window_item);
650 key_unbind("previous_window_item", (SIGNAL_FUNC) key_previous_window_item);
652 key_unbind("refresh_screen", (SIGNAL_FUNC) irssi_redraw);
653 key_unbind("scroll_backward", (SIGNAL_FUNC) key_scroll_backward);
654 key_unbind("scroll_forward", (SIGNAL_FUNC) key_scroll_forward);
655 key_unbind("scroll_start", (SIGNAL_FUNC) key_scroll_start);
656 key_unbind("scroll_end", (SIGNAL_FUNC) key_scroll_end);
658 key_unbind("insert_text", (SIGNAL_FUNC) key_insert_text);
659 key_unbind("change_window", (SIGNAL_FUNC) key_change_window);
660 keyboard_destroy(keyboard);
662 key_configure_thaw();
664 signal_remove("window changed automatic", (SIGNAL_FUNC) sig_window_auto_changed);
665 signal_remove("gui entry redirect", (SIGNAL_FUNC) sig_gui_entry_redirect);