Polish translation was updated.
[elinks.git] / src / config / kbdbind.c
blob378ba8faea6f4da784634bd069cb06aa91141c5c
1 /* Keybinding implementation */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <ctype.h>
8 #include <string.h>
10 #include "elinks.h"
12 #include "bfu/dialog.h"
13 #include "config/conf.h"
14 #include "config/dialogs.h"
15 #include "config/kbdbind.h"
16 #include "config/options.h"
17 #include "intl/gettext/libintl.h"
18 #include "main/event.h"
19 #include "main/module.h"
20 #include "terminal/kbd.h"
21 #include "util/memory.h"
22 #include "util/string.h"
25 /* Fix namespace clash on MacOS. */
26 #define table table_elinks
28 static struct action_list action_table[KEYMAP_MAX];
29 static struct keymap keymap_table[KEYMAP_MAX];
30 static struct list_head keymaps[KEYMAP_MAX]; /* struct keybinding */
32 static void add_default_keybindings(void);
34 static int
35 delete_keybinding(enum keymap_id keymap_id, struct term_event_keyboard *kbd)
37 struct keybinding *keybinding;
39 foreach (keybinding, keymaps[keymap_id]) {
40 int was_default = 0;
42 if (!kbd_key_is(&keybinding->kbd, kbd->key))
43 continue;
45 if (!kbd_modifier_is(&keybinding->kbd, kbd->modifier))
46 continue;
49 if (keybinding->flags & KBDB_DEFAULT_KEY) {
50 keybinding->flags &= ~KBDB_DEFAULT_KEY;
51 was_default = 1;
54 free_keybinding(keybinding);
56 return 1 + was_default;
59 return 0;
62 static int keybinding_is_default(struct keybinding *keybinding);
64 struct keybinding *
65 add_keybinding(enum keymap_id keymap_id, action_id_T action_id,
66 struct term_event_keyboard *kbd, int event)
68 struct keybinding *keybinding;
69 struct listbox_item *root;
70 int is_default;
72 is_default = (delete_keybinding(keymap_id, kbd) == 2);
74 keybinding = mem_calloc(1, sizeof(*keybinding));
75 if (!keybinding) return NULL;
77 keybinding->keymap_id = keymap_id;
78 keybinding->action_id = action_id;
79 copy_struct(&keybinding->kbd, kbd);
80 keybinding->event = event;
81 keybinding->flags = is_default * KBDB_DEFAULT_KEY;
82 if (keybinding_is_default(keybinding))
83 keybinding->flags |= KBDB_DEFAULT_BINDING;
85 object_nolock(keybinding, "keybinding");
86 add_to_list(keymaps[keymap_id], keybinding);
88 if (action_id == ACT_MAIN_NONE) {
89 /* We don't want such a listbox_item, do we? */
90 return NULL; /* Or goto. */
93 root = get_keybinding_action_box_item(keymap_id, action_id);
94 if (!root) {
95 return NULL; /* Or goto ;-). */
97 keybinding->box_item = add_listbox_leaf(&keybinding_browser, root, keybinding);
99 return keybinding;
102 void
103 free_keybinding(struct keybinding *keybinding)
105 if (keybinding->box_item) {
106 done_listbox_item(&keybinding_browser, keybinding->box_item);
107 keybinding->box_item = NULL;
110 #ifdef CONFIG_SCRIPTING
111 /* TODO: unref function must be implemented. This is part of bug 810. */
112 /* if (keybinding->event != EVENT_NONE)
113 scripting_unref(keybinding->event); */
114 #endif
116 if (keybinding->flags & KBDB_DEFAULT_KEY) {
117 /* We cannot just delete a default keybinding, instead we have
118 * to rebind it to ACT_MAIN_NONE so that it gets written so to the
119 * config file. */
120 keybinding->flags &= ~KBDB_DEFAULT_BINDING;
121 keybinding->action_id = ACT_MAIN_NONE;
122 return;
125 del_from_list(keybinding);
126 mem_free(keybinding);
130 keybinding_exists(enum keymap_id keymap_id, struct term_event_keyboard *kbd,
131 action_id_T *action_id)
133 struct keybinding *keybinding;
135 foreach (keybinding, keymaps[keymap_id]) {
136 if (!kbd_key_is(&keybinding->kbd, kbd->key))
137 continue;
139 if (!kbd_modifier_is(&keybinding->kbd, kbd->modifier))
140 continue;
142 if (action_id) *action_id = keybinding->action_id;
144 return 1;
147 return 0;
151 action_id_T
152 kbd_action(enum keymap_id keymap_id, struct term_event *ev, int *event)
154 struct keybinding *keybinding;
156 if (ev->ev != EVENT_KBD) return -1;
158 keybinding = kbd_ev_lookup(keymap_id, &ev->info.keyboard, event);
159 return keybinding ? keybinding->action_id : -1;
162 struct keybinding *
163 kbd_ev_lookup(enum keymap_id keymap_id, struct term_event_keyboard *kbd, int *event)
165 struct keybinding *keybinding;
167 foreach (keybinding, keymaps[keymap_id]) {
168 if (!kbd_key_is(&keybinding->kbd, kbd->key))
169 continue;
171 if (!kbd_modifier_is(&keybinding->kbd, kbd->modifier))
172 continue;
174 if (keybinding->action_id == ACT_MAIN_SCRIPTING_FUNCTION && event)
175 *event = keybinding->event;
177 return keybinding;
180 return NULL;
183 static struct keybinding *
184 kbd_act_lookup(enum keymap_id keymap_id, action_id_T action_id)
186 struct keybinding *keybinding;
188 foreach (keybinding, keymaps[keymap_id]) {
189 if (action_id != keybinding->action_id)
190 continue;
192 return keybinding;
195 return NULL;
198 struct keybinding *
199 kbd_nm_lookup(enum keymap_id keymap_id, unsigned char *name)
201 action_id_T action_id = get_action_from_string(keymap_id, name);
203 if (action_id < 0) return NULL;
205 return kbd_act_lookup(keymap_id, action_id);
208 static struct keybinding *
209 kbd_stroke_lookup(enum keymap_id keymap_id, const unsigned char *keystroke_str)
211 struct term_event_keyboard kbd;
213 if (parse_keystroke(keystroke_str, &kbd) < 0)
214 return NULL;
216 return kbd_ev_lookup(keymap_id, &kbd, NULL);
220 static struct keymap keymap_table[] = {
221 { "main", KEYMAP_MAIN, N_("Main mapping") },
222 { "edit", KEYMAP_EDIT, N_("Edit mapping") },
223 { "menu", KEYMAP_MENU, N_("Menu mapping") },
228 * Config file helpers.
231 static struct action *
232 get_action_from_keystroke(enum keymap_id keymap_id,
233 const unsigned char *keystroke_str)
235 struct keybinding *keybinding = kbd_stroke_lookup(keymap_id,
236 keystroke_str);
238 return keybinding ? get_action(keymap_id, keybinding->action_id) : NULL;
241 unsigned char *
242 get_action_name_from_keystroke(enum keymap_id keymap_id,
243 const unsigned char *keystroke_str)
245 struct action *action = get_action_from_keystroke(keymap_id,
246 keystroke_str);
248 return action ? action->str : NULL;
251 action_id_T
252 get_action_from_string(enum keymap_id keymap_id, unsigned char *str)
254 struct action *action;
256 assert(keymap_id >= 0 && keymap_id < KEYMAP_MAX);
258 for (action = action_table[keymap_id].actions; action->str; action++)
259 if (!strcmp(action->str, str))
260 return action->num;
262 return -1;
265 struct action *
266 get_action(enum keymap_id keymap_id, action_id_T action_id)
268 assert(keymap_id >= 0 && keymap_id < KEYMAP_MAX);
270 if (action_id >= 0 && action_id < action_table[keymap_id].num_actions)
271 return &action_table[keymap_id].actions[action_id];
273 return NULL;
276 unsigned char *
277 get_action_name(enum keymap_id keymap_id, action_id_T action_id)
279 struct action *action = get_action(keymap_id, action_id);
281 return action ? action->str : NULL;
284 static unsigned char *
285 get_action_desc(enum keymap_id keymap_id, action_id_T action_id)
287 struct action *action = get_action(keymap_id, action_id);
289 return action ? (action->desc ? action->desc : action->str)
290 : NULL;
294 static struct keymap *
295 get_keymap(enum keymap_id keymap_id)
297 assert(keymap_id >= 0 && keymap_id < KEYMAP_MAX);
299 return &keymap_table[keymap_id];
302 static enum keymap_id
303 get_keymap_id(unsigned char *keymap_str)
305 enum keymap_id keymap_id;
307 for (keymap_id = 0; keymap_id < KEYMAP_MAX; keymap_id++)
308 if (!strcmp(keymap_table[keymap_id].str, keymap_str))
309 return keymap_id;
311 return -1;
314 unsigned char *
315 get_keymap_name(enum keymap_id keymap_id)
317 return get_keymap(keymap_id)->str;
321 struct key {
322 unsigned char *str;
323 int num;
326 static struct key key_table[] = {
327 { "Enter", KBD_ENTER },
328 { "Space", ' ' },
329 { "Backspace", KBD_BS },
330 { "Tab", KBD_TAB },
331 { "Escape", KBD_ESC },
332 { "Left", KBD_LEFT },
333 { "Right", KBD_RIGHT },
334 { "Up", KBD_UP },
335 { "Down", KBD_DOWN },
336 { "Insert", KBD_INS },
337 { "Delete", KBD_DEL },
338 { "Home", KBD_HOME },
339 { "End", KBD_END },
340 { "PageUp", KBD_PAGE_UP },
341 { "PageDown", KBD_PAGE_DOWN },
342 { "F1", KBD_F1 },
343 { "F2", KBD_F2 },
344 { "F3", KBD_F3 },
345 { "F4", KBD_F4 },
346 { "F5", KBD_F5 },
347 { "F6", KBD_F6 },
348 { "F7", KBD_F7 },
349 { "F8", KBD_F8 },
350 { "F9", KBD_F9 },
351 { "F10", KBD_F10 },
352 { "F11", KBD_F11 },
353 { "F12", KBD_F12 },
354 { NULL, 0 }
357 term_event_key_T
358 read_key(const unsigned char *key_str)
360 struct key *key;
362 if (key_str[0] && !key_str[1])
363 return key_str[0];
365 for (key = key_table; key->str; key++)
366 if (!strcasecmp(key->str, key_str))
367 return key->num;
369 return KBD_UNDEF;
372 /* Parse the string @s as the name of a keystroke.
373 * Write the parsed key and modifiers to *@kbd.
374 * Return >=0 on success, <0 on error. */
376 parse_keystroke(const unsigned char *s, struct term_event_keyboard *kbd)
378 kbd->modifier = KBD_MOD_NONE;
379 while (1) {
380 if (!strncasecmp(s, "Shift", 5) && (s[5] == '-' || s[5] == '+')) {
381 /* Shift+a == shiFt-a == Shift-a */
382 kbd->modifier |= KBD_MOD_SHIFT;
383 s += 6;
385 } else if (!strncasecmp(s, "Ctrl", 4) && (s[4] == '-' || s[4] == '+')) {
386 /* Ctrl+a == ctRl-a == Ctrl-a */
387 kbd->modifier |= KBD_MOD_CTRL;
388 s += 5;
390 } else if (!strncasecmp(s, "Alt", 3) && (s[3] == '-' || s[3] == '+')) {
391 /* Alt+a == aLt-a == Alt-a */
392 kbd->modifier |= KBD_MOD_ALT;
393 s += 4;
395 } else {
396 /* No modifier. */
397 break;
401 kbd->key = read_key(s);
403 if ((kbd->modifier & KBD_MOD_CTRL) != 0
404 && is_kbd_character(kbd->key) && kbd->key <= 0x7F) {
405 /* Convert Ctrl-letter keystrokes to upper case,
406 * e.g. Ctrl-a to Ctrl-A. Do this because terminals
407 * typically generate the same ASCII control
408 * characters regardless of Shift and Caps Lock.
410 * However, that applies only to ASCII letters. For
411 * other Ctrl-letter combinations, there seems to be
412 * no standard of what the terminal should generate.
413 * Because it is possible that the terminal does not
414 * fold case then, ELinks should treat upper-case and
415 * lower-case variants of such keystrokes as different
416 * and let the user bind them to separate actions.
417 * Besides, toupper() might not understand the charset
418 * of kbd->key.
420 * FIXME: Actually, it is possible that some terminals
421 * preserve case in all Ctrl-letter keystrokes, even
422 * for ASCII letters. In particular, the Win32
423 * console API looks like it might do this. When the
424 * terminal handling part of ELinks is eventually
425 * extended to fully support that, it could be a good
426 * idea to change parse_keystroke() not to fold case,
427 * and instead make kbd_ev_lookup() or its callers
428 * search for different variants of the keystroke if
429 * the original one is not bound to any action. */
430 kbd->key = toupper(kbd->key);
433 return (kbd->key == KBD_UNDEF) ? -1 : 0;
436 void
437 add_keystroke_to_string(struct string *str, struct term_event_keyboard *kbd,
438 int escape)
440 unsigned char key_buffer[3] = "\\x";
441 unsigned char *key_string = NULL;
442 struct key *key;
444 if (kbd->key == KBD_UNDEF) return;
446 if (kbd->modifier & KBD_MOD_SHIFT)
447 add_to_string(str, "Shift-");
448 if (kbd->modifier & KBD_MOD_CTRL)
449 add_to_string(str, "Ctrl-");
450 if (kbd->modifier & KBD_MOD_ALT)
451 add_to_string(str, "Alt-");
453 for (key = key_table; key->str; key++) {
454 if (kbd->key == key->num) {
455 key_string = key->str;
456 break;
460 if (!key_string) {
461 key_string = key_buffer + 1;
462 *key_string = (unsigned char) kbd->key;
463 if (escape && strchr("'\"\\", kbd->key))
464 key_string--;
467 add_to_string(str, key_string);
470 void
471 add_keystroke_action_to_string(struct string *string, action_id_T action_id,
472 enum keymap_id keymap_id)
474 struct keybinding *keybinding = kbd_act_lookup(keymap_id, action_id);
476 if (keybinding)
477 add_keystroke_to_string(string, &keybinding->kbd, 0);
480 unsigned char *
481 get_keystroke(action_id_T action_id, enum keymap_id keymap_id)
483 struct string keystroke;
485 if (!init_string(&keystroke)) return NULL;
487 add_keystroke_action_to_string(&keystroke, action_id, keymap_id);
489 /* Never return empty string */
490 if (!keystroke.length) done_string(&keystroke);
492 return keystroke.source;
495 void
496 add_actions_to_string(struct string *string, action_id_T action_ids[],
497 enum keymap_id keymap_id, struct terminal *term)
499 int i;
501 assert(keymap_id >= 0 && keymap_id < KEYMAP_MAX);
503 add_format_to_string(string, "%s:\n", _(keymap_table[keymap_id].desc, term));
505 for (i = 0; action_ids[i] != ACT_MAIN_NONE; i++) {
506 struct keybinding *keybinding = kbd_act_lookup(keymap_id, action_ids[i]);
507 int keystrokelen = string->length;
508 unsigned char *desc = get_action_desc(keymap_id, action_ids[i]);
510 if (!keybinding) continue;
512 add_char_to_string(string, '\n');
513 add_keystroke_to_string(string, &keybinding->kbd, 0);
514 keystrokelen = string->length - keystrokelen;
515 add_xchar_to_string(string, ' ', int_max(15 - keystrokelen, 1));
516 add_to_string(string, _(desc, term));
520 #define ACTION_(map, name, action, caption, flags) \
521 { name, ACT_##map##_##action, KEYMAP_ID, caption, flags }
523 #undef KEYMAP_ID
524 #define KEYMAP_ID KEYMAP_MAIN
525 static struct action main_action_table[MAIN_ACTIONS + 1] = {
526 #include "config/actions-main.inc"
529 #undef KEYMAP_ID
530 #define KEYMAP_ID KEYMAP_EDIT
531 static struct action edit_action_table[EDIT_ACTIONS + 1] = {
532 #include "config/actions-edit.inc"
535 #undef KEYMAP_ID
536 #define KEYMAP_ID KEYMAP_MENU
537 static struct action menu_action_table[MENU_ACTIONS + 1] = {
538 #include "config/actions-menu.inc"
541 static struct action_list action_table[KEYMAP_MAX] = {
542 { main_action_table, sizeof_array(main_action_table) },
543 { edit_action_table, sizeof_array(edit_action_table) },
544 { menu_action_table, sizeof_array(menu_action_table) },
547 #undef KEYMAP_ID
548 #undef ACTION_
551 static void
552 init_keymaps(struct module *xxx)
554 enum keymap_id keymap_id;
556 for (keymap_id = 0; keymap_id < KEYMAP_MAX; keymap_id++)
557 init_list(keymaps[keymap_id]);
559 init_keybinding_listboxes(keymap_table, action_table);
560 add_default_keybindings();
563 static void
564 free_keymaps(struct module *xxx)
566 enum keymap_id keymap_id;
568 done_keybinding_listboxes();
570 for (keymap_id = 0; keymap_id < KEYMAP_MAX; keymap_id++)
571 free_list(keymaps[keymap_id]);
576 * Bind to Lua function.
579 #ifdef CONFIG_SCRIPTING
580 static unsigned char *
581 bind_key_to_event(unsigned char *ckmap, const unsigned char *ckey, int event)
583 struct term_event_keyboard kbd;
584 action_id_T action_id;
585 enum keymap_id keymap_id = get_keymap_id(ckmap);
587 if (keymap_id < 0)
588 return gettext("Unrecognised keymap");
590 if (parse_keystroke(ckey, &kbd) < 0)
591 return gettext("Error parsing keystroke");
593 action_id = get_action_from_string(keymap_id, " *scripting-function*");
594 if (action_id < 0)
595 return gettext("Unrecognised action (internal error)");
597 add_keybinding(keymap_id, action_id, &kbd, event);
599 return NULL;
603 bind_key_to_event_name(unsigned char *ckmap, const unsigned char *ckey,
604 unsigned char *event_name, unsigned char **err)
606 int event_id;
608 event_id = register_event(event_name);
610 if (event_id == EVENT_NONE) {
611 *err = gettext("Error registering event");
612 return EVENT_NONE;
615 *err = bind_key_to_event(ckmap, ckey, event_id);
617 return event_id;
619 #endif
623 * Default keybindings.
626 struct default_kb {
627 struct term_event_keyboard kbd;
628 action_id_T action_id;
631 static struct default_kb default_main_keymap[] = {
632 { { ' ', KBD_MOD_NONE }, ACT_MAIN_MOVE_PAGE_DOWN },
633 { { '#', KBD_MOD_NONE }, ACT_MAIN_SEARCH_TYPEAHEAD },
634 { { '%', KBD_MOD_NONE }, ACT_MAIN_TOGGLE_DOCUMENT_COLORS },
635 { { '*', KBD_MOD_NONE }, ACT_MAIN_TOGGLE_DISPLAY_IMAGES },
636 { { ',', KBD_MOD_NONE }, ACT_MAIN_LUA_CONSOLE },
637 { { '.', KBD_MOD_NONE }, ACT_MAIN_TOGGLE_NUMBERED_LINKS },
638 { { '/', KBD_MOD_NONE }, ACT_MAIN_SEARCH },
639 { { ':', KBD_MOD_NONE }, ACT_MAIN_EXMODE },
640 { { '<', KBD_MOD_NONE }, ACT_MAIN_TAB_PREV },
641 { { '<', KBD_MOD_ALT }, ACT_MAIN_TAB_MOVE_LEFT },
642 { { '=', KBD_MOD_NONE }, ACT_MAIN_DOCUMENT_INFO },
643 { { '>', KBD_MOD_NONE }, ACT_MAIN_TAB_NEXT },
644 { { '>', KBD_MOD_ALT }, ACT_MAIN_TAB_MOVE_RIGHT },
645 { { '?', KBD_MOD_NONE }, ACT_MAIN_SEARCH_BACK },
646 { { 'A', KBD_MOD_NONE }, ACT_MAIN_ADD_BOOKMARK_LINK },
647 { { 'A', KBD_MOD_CTRL }, ACT_MAIN_MOVE_DOCUMENT_START },
648 { { 'B', KBD_MOD_CTRL }, ACT_MAIN_MOVE_PAGE_UP },
649 { { 'C', KBD_MOD_NONE }, ACT_MAIN_CACHE_MANAGER },
650 { { 'D', KBD_MOD_NONE }, ACT_MAIN_DOWNLOAD_MANAGER },
651 { { 'E', KBD_MOD_NONE }, ACT_MAIN_GOTO_URL_CURRENT_LINK },
652 { { 'E', KBD_MOD_CTRL }, ACT_MAIN_MOVE_DOCUMENT_END },
653 { { 'F', KBD_MOD_NONE }, ACT_MAIN_FORMHIST_MANAGER },
654 { { 'F', KBD_MOD_CTRL }, ACT_MAIN_MOVE_PAGE_DOWN },
655 { { 'G', KBD_MOD_NONE }, ACT_MAIN_GOTO_URL_CURRENT },
656 { { 'H', KBD_MOD_NONE }, ACT_MAIN_GOTO_URL_HOME },
657 { { 'K', KBD_MOD_NONE }, ACT_MAIN_COOKIE_MANAGER },
658 { { 'K', KBD_MOD_CTRL }, ACT_MAIN_COOKIES_LOAD },
659 { { 'L', KBD_MOD_NONE }, ACT_MAIN_LINK_MENU },
660 { { 'L', KBD_MOD_CTRL }, ACT_MAIN_REDRAW },
661 { { 'N', KBD_MOD_NONE }, ACT_MAIN_FIND_NEXT_BACK },
662 { { 'N', KBD_MOD_CTRL }, ACT_MAIN_SCROLL_DOWN },
663 { { 'P', KBD_MOD_CTRL }, ACT_MAIN_SCROLL_UP },
664 { { 'Q', KBD_MOD_NONE }, ACT_MAIN_REALLY_QUIT },
665 { { 'R', KBD_MOD_CTRL }, ACT_MAIN_RELOAD },
666 { { 'T', KBD_MOD_NONE }, ACT_MAIN_OPEN_LINK_IN_NEW_TAB_IN_BACKGROUND },
667 { { 'W', KBD_MOD_NONE }, ACT_MAIN_TOGGLE_WRAP_TEXT },
668 { { '[', KBD_MOD_NONE }, ACT_MAIN_SCROLL_LEFT },
669 { { '\'', KBD_MOD_NONE }, ACT_MAIN_MARK_GOTO },
670 { { '\\', KBD_MOD_NONE }, ACT_MAIN_TOGGLE_HTML_PLAIN },
671 { { ']', KBD_MOD_NONE }, ACT_MAIN_SCROLL_RIGHT },
672 { { 'a', KBD_MOD_NONE }, ACT_MAIN_ADD_BOOKMARK },
673 { { 'b', KBD_MOD_NONE }, ACT_MAIN_MOVE_PAGE_UP },
674 { { 'c', KBD_MOD_NONE }, ACT_MAIN_TAB_CLOSE },
675 { { 'd', KBD_MOD_NONE }, ACT_MAIN_LINK_DOWNLOAD },
676 { { 'e', KBD_MOD_NONE }, ACT_MAIN_TAB_MENU },
677 { { 'f', KBD_MOD_NONE }, ACT_MAIN_FRAME_MAXIMIZE },
678 { { 'g', KBD_MOD_NONE }, ACT_MAIN_GOTO_URL },
679 { { 'h', KBD_MOD_NONE }, ACT_MAIN_HISTORY_MANAGER },
680 { { 'k', KBD_MOD_NONE }, ACT_MAIN_KEYBINDING_MANAGER },
681 { { 'l', KBD_MOD_NONE }, ACT_MAIN_JUMP_TO_LINK },
682 { { 'm', KBD_MOD_NONE }, ACT_MAIN_MARK_SET },
683 { { 'n', KBD_MOD_NONE }, ACT_MAIN_FIND_NEXT },
684 { { 'o', KBD_MOD_NONE }, ACT_MAIN_OPTIONS_MANAGER },
685 { { 'q', KBD_MOD_NONE }, ACT_MAIN_QUIT },
686 { { 'r', KBD_MOD_NONE }, ACT_MAIN_LINK_DOWNLOAD_RESUME },
687 { { 's', KBD_MOD_NONE }, ACT_MAIN_BOOKMARK_MANAGER },
688 { { 't', KBD_MOD_NONE }, ACT_MAIN_OPEN_NEW_TAB },
689 { { 'u', KBD_MOD_NONE }, ACT_MAIN_HISTORY_MOVE_FORWARD },
690 { { 'v', KBD_MOD_NONE }, ACT_MAIN_VIEW_IMAGE },
691 { { 'x', KBD_MOD_NONE }, ACT_MAIN_LINK_FOLLOW_RELOAD },
692 { { 'z', KBD_MOD_NONE }, ACT_MAIN_ABORT_CONNECTION },
693 { { '{', KBD_MOD_NONE }, ACT_MAIN_SCROLL_LEFT },
694 { { '|', KBD_MOD_NONE }, ACT_MAIN_HEADER_INFO },
695 { { '}', KBD_MOD_NONE }, ACT_MAIN_SCROLL_RIGHT },
696 { { KBD_BS, KBD_MOD_NONE }, ACT_MAIN_BACKSPACE_PREFIX },
697 { { KBD_DEL, KBD_MOD_NONE }, ACT_MAIN_SCROLL_DOWN },
698 { { KBD_DOWN, KBD_MOD_NONE }, ACT_MAIN_MOVE_LINK_NEXT },
699 { { KBD_END, KBD_MOD_NONE }, ACT_MAIN_MOVE_DOCUMENT_END },
700 { { KBD_ENTER, KBD_MOD_NONE }, ACT_MAIN_LINK_FOLLOW },
701 { { KBD_ENTER, KBD_MOD_CTRL }, ACT_MAIN_LINK_FOLLOW_RELOAD },
702 { { KBD_ESC, KBD_MOD_NONE }, ACT_MAIN_MENU },
703 { { KBD_F10, KBD_MOD_NONE }, ACT_MAIN_FILE_MENU },
704 { { KBD_F9, KBD_MOD_NONE }, ACT_MAIN_MENU },
705 { { KBD_HOME, KBD_MOD_NONE }, ACT_MAIN_MOVE_DOCUMENT_START },
706 { { KBD_INS, KBD_MOD_NONE }, ACT_MAIN_SCROLL_UP },
707 { { KBD_INS, KBD_MOD_CTRL }, ACT_MAIN_COPY_CLIPBOARD },
708 { { KBD_LEFT, KBD_MOD_NONE }, ACT_MAIN_HISTORY_MOVE_BACK },
709 { { KBD_PAGE_DOWN, KBD_MOD_NONE }, ACT_MAIN_MOVE_PAGE_DOWN },
710 { { KBD_PAGE_UP, KBD_MOD_NONE }, ACT_MAIN_MOVE_PAGE_UP },
711 { { KBD_RIGHT, KBD_MOD_NONE }, ACT_MAIN_LINK_FOLLOW },
712 { { KBD_RIGHT, KBD_MOD_CTRL }, ACT_MAIN_LINK_FOLLOW_RELOAD },
713 { { KBD_TAB, KBD_MOD_NONE }, ACT_MAIN_FRAME_NEXT },
714 { { KBD_TAB, KBD_MOD_ALT }, ACT_MAIN_FRAME_PREV },
715 { { KBD_TAB, KBD_MOD_SHIFT}, ACT_MAIN_FRAME_PREV },
716 { { KBD_UP, KBD_MOD_NONE }, ACT_MAIN_MOVE_LINK_PREV },
717 { { 0, 0 }, 0 }
720 static struct default_kb default_edit_keymap[] = {
721 { { '<', KBD_MOD_ALT }, ACT_EDIT_BEGINNING_OF_BUFFER },
722 { { '>', KBD_MOD_ALT }, ACT_EDIT_END_OF_BUFFER },
723 { { 'A', KBD_MOD_CTRL }, ACT_EDIT_HOME },
724 { { 'b', KBD_MOD_ALT }, ACT_EDIT_MOVE_BACKWARD_WORD },
725 { { 'D', KBD_MOD_CTRL }, ACT_EDIT_DELETE },
726 { { 'E', KBD_MOD_CTRL }, ACT_EDIT_END },
727 { { 'f', KBD_MOD_ALT }, ACT_EDIT_MOVE_FORWARD_WORD },
728 { { 'H', KBD_MOD_CTRL }, ACT_EDIT_BACKSPACE },
729 { { 'K', KBD_MOD_CTRL }, ACT_EDIT_KILL_TO_EOL },
730 { { 'L', KBD_MOD_CTRL }, ACT_EDIT_REDRAW },
731 { { 'r', KBD_MOD_ALT }, ACT_EDIT_SEARCH_TOGGLE_REGEX },
732 { { 'F', KBD_MOD_CTRL }, ACT_EDIT_AUTO_COMPLETE_FILE },
733 { { 'R', KBD_MOD_CTRL }, ACT_EDIT_AUTO_COMPLETE_UNAMBIGUOUS },
734 { { 'T', KBD_MOD_CTRL }, ACT_EDIT_OPEN_EXTERNAL },
735 { { 'U', KBD_MOD_CTRL }, ACT_EDIT_KILL_TO_BOL },
736 { { 'V', KBD_MOD_CTRL }, ACT_EDIT_PASTE_CLIPBOARD },
737 { { 'W', KBD_MOD_CTRL }, ACT_EDIT_AUTO_COMPLETE },
738 { { 'X', KBD_MOD_CTRL }, ACT_EDIT_CUT_CLIPBOARD },
739 { { KBD_BS, KBD_MOD_ALT }, ACT_EDIT_KILL_WORD_BACK },
740 { { KBD_BS, KBD_MOD_NONE }, ACT_EDIT_BACKSPACE },
741 { { KBD_DEL, KBD_MOD_NONE }, ACT_EDIT_DELETE },
742 { { KBD_DOWN, KBD_MOD_NONE }, ACT_EDIT_DOWN },
743 { { KBD_END, KBD_MOD_NONE }, ACT_EDIT_END },
744 { { KBD_ENTER, KBD_MOD_NONE }, ACT_EDIT_ENTER },
745 { { KBD_ESC, KBD_MOD_NONE }, ACT_EDIT_CANCEL },
746 { { KBD_F4, KBD_MOD_NONE }, ACT_EDIT_OPEN_EXTERNAL },
747 { { KBD_HOME, KBD_MOD_NONE }, ACT_EDIT_HOME },
748 { { KBD_INS, KBD_MOD_CTRL }, ACT_EDIT_COPY_CLIPBOARD },
749 { { KBD_LEFT, KBD_MOD_NONE }, ACT_EDIT_LEFT },
750 { { KBD_RIGHT, KBD_MOD_NONE }, ACT_EDIT_RIGHT },
751 { { KBD_TAB, KBD_MOD_NONE }, ACT_EDIT_NEXT_ITEM },
752 { { KBD_TAB, KBD_MOD_ALT }, ACT_EDIT_PREVIOUS_ITEM },
753 { { KBD_TAB, KBD_MOD_SHIFT}, ACT_EDIT_PREVIOUS_ITEM },
754 { { KBD_UP, KBD_MOD_NONE }, ACT_EDIT_UP },
755 { { 0, 0 }, 0 }
758 static struct default_kb default_menu_keymap[] = {
759 { { ' ', KBD_MOD_NONE }, ACT_MENU_SELECT },
760 { { '*', KBD_MOD_NONE }, ACT_MENU_MARK_ITEM },
761 { { '+', KBD_MOD_NONE }, ACT_MENU_EXPAND },
762 { { '-', KBD_MOD_NONE }, ACT_MENU_UNEXPAND },
763 { { '/', KBD_MOD_NONE }, ACT_MENU_SEARCH },
764 { { '=', KBD_MOD_NONE }, ACT_MENU_EXPAND },
765 { { 'A', KBD_MOD_CTRL }, ACT_MENU_HOME },
766 { { 'B', KBD_MOD_CTRL }, ACT_MENU_PAGE_UP },
767 { { 'E', KBD_MOD_CTRL }, ACT_MENU_END },
768 { { 'F', KBD_MOD_CTRL }, ACT_MENU_PAGE_DOWN },
769 { { 'L', KBD_MOD_CTRL }, ACT_MENU_REDRAW },
770 { { 'N', KBD_MOD_CTRL }, ACT_MENU_DOWN },
771 { { 'P', KBD_MOD_CTRL }, ACT_MENU_UP },
772 { { 'V', KBD_MOD_ALT }, ACT_MENU_PAGE_UP },
773 { { 'V', KBD_MOD_CTRL }, ACT_MENU_PAGE_DOWN },
774 { { '[', KBD_MOD_NONE }, ACT_MENU_EXPAND },
775 { { ']', KBD_MOD_NONE }, ACT_MENU_UNEXPAND },
776 { { '_', KBD_MOD_NONE }, ACT_MENU_UNEXPAND },
777 { { KBD_DEL, KBD_MOD_NONE }, ACT_MENU_DELETE },
778 { { KBD_DOWN, KBD_MOD_NONE }, ACT_MENU_DOWN },
779 { { KBD_END, KBD_MOD_NONE }, ACT_MENU_END },
780 { { KBD_ENTER, KBD_MOD_NONE }, ACT_MENU_ENTER },
781 { { KBD_ESC, KBD_MOD_NONE }, ACT_MENU_CANCEL },
782 { { KBD_HOME, KBD_MOD_NONE }, ACT_MENU_HOME },
783 { { KBD_INS, KBD_MOD_NONE }, ACT_MENU_MARK_ITEM },
784 { { KBD_LEFT, KBD_MOD_NONE }, ACT_MENU_LEFT },
785 { { KBD_PAGE_DOWN, KBD_MOD_NONE }, ACT_MENU_PAGE_DOWN },
786 { { KBD_PAGE_UP, KBD_MOD_NONE }, ACT_MENU_PAGE_UP },
787 { { KBD_RIGHT, KBD_MOD_NONE }, ACT_MENU_RIGHT },
788 { { KBD_TAB, KBD_MOD_NONE }, ACT_MENU_NEXT_ITEM },
789 { { KBD_TAB, KBD_MOD_ALT }, ACT_MENU_PREVIOUS_ITEM },
790 { { KBD_TAB, KBD_MOD_SHIFT}, ACT_MENU_PREVIOUS_ITEM },
791 { { KBD_UP, KBD_MOD_NONE }, ACT_MENU_UP },
792 { { 0, 0 }, 0}
795 static struct default_kb *default_keybindings[] = {
796 default_main_keymap,
797 default_edit_keymap,
798 default_menu_keymap,
801 static int
802 keybinding_is_default(struct keybinding *keybinding)
804 struct default_kb default_keybinding = {
806 keybinding->kbd.key,
807 keybinding->kbd.modifier
809 keybinding->action_id
811 struct default_kb *pos;
813 for (pos = default_keybindings[keybinding->keymap_id]; pos->kbd.key; pos++)
814 if (!memcmp(&default_keybinding, pos, sizeof(default_keybinding)))
815 return 1;
817 return 0;
820 static void
821 add_default_keybindings(void)
823 /* Maybe we shouldn't delete old keybindings. But on the other side, we
824 * can't trust clueless users what they'll push into sources modifying
825 * defaults, can we? ;)) */
826 enum keymap_id keymap_id;
828 for (keymap_id = 0; keymap_id < KEYMAP_MAX; keymap_id++) {
829 struct default_kb *kb;
831 for (kb = default_keybindings[keymap_id]; kb->kbd.key; kb++) {
832 struct keybinding *keybinding;
834 keybinding = add_keybinding(keymap_id, kb->action_id, &kb->kbd, EVENT_NONE);
835 keybinding->flags |= KBDB_DEFAULT_KEY | KBDB_DEFAULT_BINDING;
842 * Config file tools.
845 struct action_alias {
846 unsigned char *str;
847 action_id_T action_id;
850 static struct action_alias main_action_aliases[] = {
851 { "back", ACT_MAIN_HISTORY_MOVE_BACK },
852 { "down", ACT_MAIN_MOVE_LINK_NEXT },
853 { "download", ACT_MAIN_LINK_DOWNLOAD },
854 { "download-image", ACT_MAIN_LINK_DOWNLOAD_IMAGE },
855 { "end", ACT_MAIN_MOVE_DOCUMENT_END },
856 { "enter", ACT_MAIN_LINK_FOLLOW },
857 { "enter-reload", ACT_MAIN_LINK_FOLLOW_RELOAD },
858 { "home", ACT_MAIN_MOVE_DOCUMENT_START },
859 { "next-frame", ACT_MAIN_FRAME_NEXT },
860 { "page-down", ACT_MAIN_MOVE_PAGE_DOWN },
861 { "page-up", ACT_MAIN_MOVE_PAGE_UP },
862 { "previous-frame", ACT_MAIN_FRAME_PREV },
863 { "resume-download", ACT_MAIN_LINK_DOWNLOAD_RESUME },
864 { "unback", ACT_MAIN_HISTORY_MOVE_FORWARD },
865 { "up", ACT_MAIN_MOVE_LINK_PREV },
866 { "zoom-frame", ACT_MAIN_FRAME_MAXIMIZE },
868 { NULL, 0 }
871 static struct action_alias edit_action_aliases[] = {
872 { "edit", ACT_EDIT_OPEN_EXTERNAL },
874 { NULL, 0 }
877 static struct action_alias *action_aliases[KEYMAP_MAX] = {
878 main_action_aliases,
879 edit_action_aliases,
880 NULL,
883 static action_id_T
884 get_aliased_action(enum keymap_id keymap_id, unsigned char *action_str)
886 assert(keymap_id >= 0 && keymap_id < KEYMAP_MAX);
888 if (action_aliases[keymap_id]) {
889 struct action_alias *alias;
891 for (alias = action_aliases[keymap_id]; alias->str; alias++)
892 if (!strcmp(alias->str, action_str))
893 return alias->action_id;
896 return get_action_from_string(keymap_id, action_str);
899 /* Return 0 when ok, something strange otherwise. */
901 bind_do(unsigned char *keymap_str, const unsigned char *keystroke_str,
902 unsigned char *action_str, int is_system_conf)
904 enum keymap_id keymap_id;
905 action_id_T action_id;
906 struct term_event_keyboard kbd;
907 struct keybinding *keybinding;
909 keymap_id = get_keymap_id(keymap_str);
910 if (keymap_id < 0) return 1;
912 if (parse_keystroke(keystroke_str, &kbd) < 0) return 2;
914 action_id = get_aliased_action(keymap_id, action_str);
915 if (action_id < 0) return 77 / 9 - 5;
917 keybinding = add_keybinding(keymap_id, action_id, &kbd, EVENT_NONE);
918 if (keybinding && is_system_conf)
919 keybinding->flags |= KBDB_DEFAULT_KEY | KBDB_DEFAULT_BINDING;
921 return 0;
924 unsigned char *
925 bind_act(unsigned char *keymap_str, const unsigned char *keystroke_str)
927 enum keymap_id keymap_id;
928 unsigned char *action;
929 struct keybinding *keybinding;
931 keymap_id = get_keymap_id(keymap_str);
932 if (keymap_id < 0)
933 return NULL;
935 keybinding = kbd_stroke_lookup(keymap_id, keystroke_str);
936 if (!keybinding) return NULL;
938 action = get_action_name(keymap_id, keybinding->action_id);
939 if (!action)
940 return NULL;
942 keybinding->flags |= KBDB_WATERMARK;
943 return straconcat("\"", action, "\"", NULL);
946 static void
947 single_bind_config_string(struct string *file, enum keymap_id keymap_id,
948 struct keybinding *keybinding)
950 unsigned char *keymap_str = get_keymap_name(keymap_id);
951 unsigned char *action_str = get_action_name(keymap_id, keybinding->action_id);
953 if (!keymap_str || !action_str || action_str[0] == ' ')
954 return;
956 if (keybinding->flags & KBDB_WATERMARK) {
957 keybinding->flags &= ~KBDB_WATERMARK;
958 return;
961 /* TODO: Maybe we should use string.write.. */
962 add_to_string(file, "bind \"");
963 add_to_string(file, keymap_str);
964 add_to_string(file, "\" \"");
965 add_keystroke_to_string(file, &keybinding->kbd, 1);
966 add_to_string(file, "\" = \"");
967 add_to_string(file, action_str);
968 add_char_to_string(file, '\"');
969 add_char_to_string(file, '\n');
972 void
973 bind_config_string(struct string *file)
975 enum keymap_id keymap_id;
977 for (keymap_id = 0; keymap_id < KEYMAP_MAX; keymap_id++) {
978 struct keybinding *keybinding;
980 foreach (keybinding, keymaps[keymap_id]) {
981 /* Don't save default keybindings that has not been
982 * deleted (rebound to none action) (Bug 337). */
983 if (keybinding->flags & KBDB_DEFAULT_BINDING)
984 continue;
986 single_bind_config_string(file, keymap_id, keybinding);
991 struct module kbdbind_module = struct_module(
992 /* name: */ "Keyboard Bindings",
993 /* options: */ NULL,
994 /* hooks: */ NULL,
995 /* submodules: */ NULL,
996 /* data: */ NULL,
997 /* init: */ init_keymaps,
998 /* done: */ free_keymaps