2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/ascii.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/buffer.h>
19 #include <lib-lib/mapping.h>
21 #include <lib-ui/curses.h>
22 #include <lib-ui/menu.h>
26 #include <lib-crypt/crypt.h>
33 #define MUTT_FUNCTIONS_VALUES
34 #include "functions.def"
36 struct mapping_t Menus[] = {
37 {"alias", MENU_ALIAS},
38 {"attach", MENU_ATTACH},
39 {"browser", MENU_FOLDER},
40 {"compose", MENU_COMPOSE},
41 {"editor", MENU_EDITOR},
43 {"pager", MENU_PAGER},
44 {"postpone", MENU_POST},
46 {"smime", MENU_SMIME},
49 {"key_select_pgp", MENU_KEY_SELECT_PGP},
50 {"key_select_smime", MENU_KEY_SELECT_SMIME},
59 {"query", MENU_QUERY},
60 {"generic", MENU_GENERIC},
64 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
66 static struct mapping_t KeyNames[] = {
67 {"<PageUp>", KEY_PPAGE},
68 {"<PageDown>", KEY_NPAGE},
71 {"<Right>", KEY_RIGHT},
74 {"<BackSpace>", KEY_BACKSPACE},
79 {"<Enter>", KEY_ENTER},
81 {"<Return>", M_ENTER_C},
86 {"<BackTab>", KEY_BTAB},
94 /* contains the last key the user pressed */
97 struct keymap_t *Keymaps[MENU_MAX];
99 static struct keymap_t *allocKeys(int len, keycode_t *keys)
103 p = p_new(struct keymap_t, 1);
105 p->keys = p_dup(keys, len);
109 static int parse_fkey (const char *s)
114 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
117 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
129 * This function parses the string <NNN> and uses the octal value as the key
132 static int parse_keycode (const char *s)
134 if (isdigit ((unsigned char) s[1]) &&
135 isdigit ((unsigned char) s[2]) &&
136 isdigit ((unsigned char) s[3]) && s[4] == '>') {
137 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
142 static int parsekeys (const char *str, keycode_t * d, int max)
145 char buff[SHORT_STRING];
149 m_strcpy(buff, sizeof(buff), str);
154 if (*s == '<' && (t = strchr (s, '>'))) {
159 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
163 else if ((n = parse_fkey (s)) > 0) {
167 else if ((n = parse_keycode (s)) > 0) {
176 *d = (unsigned char) *s;
186 /* insert a key sequence into the specified map. the map is sorted by ASCII
187 * value (lowest to highest)
189 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
191 struct keymap_t *map, *tmp, *last = NULL, *next;
192 keycode_t buf[MAX_SEQ];
193 int len, pos = 0, lastpos = 0;
195 len = parsekeys (s, buf, MAX_SEQ);
197 map = allocKeys (len, buf);
199 map->macro = m_strdup(macro);
200 map->descr = m_strdup(descr);
205 if (pos >= len || pos >= tmp->len) {
206 /* map and tmp match, but have different lengths, so overwrite */
210 p_delete(&tmp->macro);
211 p_delete(&tmp->keys);
212 p_delete(&tmp->descr);
216 while (tmp && len >= pos);
220 else if (buf[pos] == tmp->keys[pos])
222 else if (buf[pos] < tmp->keys[pos]) {
223 /* found location to insert between last and tmp */
227 else { /* buf[pos] > tmp->keys[pos] */
246 void km_bindkey (const char *s, int menu, int op)
248 km_bind (s, menu, op, NULL, NULL);
251 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
255 for (i = 0; bindings[i].name; i++) {
256 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
257 m_strlen(bindings[i].name) == len)
258 return bindings[i].op;
264 static const char *get_func (struct binding_t *bindings, int op)
268 for (i = 0; bindings[i].name; i++) {
269 if (bindings[i].op == op) {
270 return bindings[i].name;
277 static void push_string(const char *s)
279 const char *pp, *p = s + m_strlen(s) - 1;
284 /* if we see something like "<PageUp>", look to see if it is a real
285 function name and return the corresponding value */
287 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
289 if ((i = parse_fkey (pp)) > 0) {
290 mutt_ungetch (KEY_F (i), 0);
296 for (i = 0; KeyNames[i].name; i++) {
297 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
300 if (KeyNames[i].name) {
302 mutt_ungetch (KeyNames[i].value, 0);
307 /* See if it is a valid command
308 * skip the '<' and the '>' when comparing */
309 for (i = 0; Menus[i].name; i++) {
310 struct binding_t *binding = km_get_table (Menus[i].value);
313 op = get_op (binding, pp + 1, l - 2);
320 mutt_ungetch (0, op);
326 mutt_ungetch ((unsigned char) *p--, 0);
330 static int retry_generic (int menu, keycode_t * keys, int keyslen,
333 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
335 mutt_ungetch (lastkey, 0);
336 for (; keyslen; keyslen--)
337 mutt_ungetch (keys[keyslen - 1], 0);
338 return (km_dokey (MENU_GENERIC));
340 if (menu != MENU_EDITOR) {
341 /* probably a good idea to flush input here so we can abort macros */
348 * >0 function to execute
349 * OP_NULL no function bound to key sequence
350 * -1 error occured while reading input
352 int km_dokey (int menu)
355 struct keymap_t *map = Keymaps[menu];
362 return (retry_generic (menu, NULL, 0, 0));
365 /* ncurses doesn't return on resized screen when timeout is set to zero */
366 if (menu != MENU_EDITOR)
367 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
371 if (menu != MENU_EDITOR)
372 timeout (-1); /* restore blocking operation */
378 /* do we have an op already? */
380 const char *func = NULL;
381 struct binding_t *bindings;
383 /* is this a valid op for this menu? */
384 if ((bindings = km_get_table (menu)) &&
385 (func = get_func (bindings, tmp.op)))
388 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
391 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
392 /* check generic menu */
393 bindings = OpGeneric;
394 if ((func = get_func (bindings, tmp.op)))
398 /* Sigh. Valid function but not in this context.
399 * Find the literal string and push it back */
400 for (i = 0; Menus[i].name; i++) {
401 bindings = km_get_table (Menus[i].value);
403 func = get_func (bindings, tmp.op);
405 /* careful not to feed the <..> as one token. otherwise
406 * push_string() will push the bogus op right back! */
407 mutt_ungetch ('>', 0);
409 mutt_ungetch ('<', 0);
414 /* continue to chew */
419 /* Nope. Business as usual */
420 while (LastKey > map->keys[pos]) {
421 if (pos > map->eq || !map->next)
422 return (retry_generic (menu, map->keys, pos, LastKey));
426 if (LastKey != map->keys[pos])
427 return (retry_generic (menu, map->keys, pos, LastKey));
429 if (++pos == map->len) {
431 if (map->op != OP_MACRO)
436 mutt_error _("Macro loop detected.");
441 push_string (map->macro);
450 static void create_bindings (struct binding_t *map, int menu)
454 for (i = 0; map[i].name; i++)
456 km_bindkey (map[i].seq, menu, map[i].op);
459 const char *km_keyname(int c)
464 if ((p = mutt_getnamebyvalue (c, KeyNames)))
467 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
473 buf[1] = (c + '@') & 0x7f;
477 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
479 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
480 sprintf (buf, "<F%d>", c - KEY_F0);
481 else if (IsPrint (c))
482 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
484 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
488 int km_expand_key (char *s, size_t len, struct keymap_t *map)
497 m_strcpy(s, len, km_keyname(map->keys[p]));
498 len -= (l = m_strlen(s));
500 if (++p >= map->len || !len)
509 struct keymap_t *km_find_func (int menu, int func)
511 struct keymap_t *map = Keymaps[menu];
513 for (; map; map = map->next)
521 p_clear(Keymaps, MENU_MAX);
523 create_bindings (OpAttach, MENU_ATTACH);
524 create_bindings (OpBrowser, MENU_FOLDER);
525 create_bindings (OpCompose, MENU_COMPOSE);
526 create_bindings (OpMain, MENU_MAIN);
527 create_bindings (OpPager, MENU_PAGER);
528 create_bindings (OpPost, MENU_POST);
529 create_bindings (OpQuery, MENU_QUERY);
530 create_bindings (OpAlias, MENU_ALIAS);
533 create_bindings (OpPgp, MENU_PGP);
534 create_bindings (OpSmime, MENU_SMIME);
536 #ifdef CRYPT_BACKEND_GPGME
537 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
538 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
542 create_bindings (OpMix, MENU_MIX);
544 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
545 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
546 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
549 /* bindings for the line editor */
550 create_bindings (OpEditor, MENU_EDITOR);
552 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
553 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
554 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
555 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
556 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
557 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
558 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
559 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
560 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
562 /* generic menu keymap */
563 create_bindings (OpGeneric, MENU_GENERIC);
565 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
566 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
567 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
568 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
569 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
570 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
571 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
572 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
573 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
574 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
575 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
576 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
577 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
578 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
579 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
580 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
581 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
583 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
585 /* Miscellaneous extra bindings */
587 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
588 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
589 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
590 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
591 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
592 km_bindkey ("x", MENU_MAIN, OP_EXIT);
594 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
596 km_bindkey ("x", MENU_PAGER, OP_EXIT);
597 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
598 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
599 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
600 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
601 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
602 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
603 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
604 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
605 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
606 km_bindkey ("1", MENU_PAGER, OP_JUMP);
607 km_bindkey ("2", MENU_PAGER, OP_JUMP);
608 km_bindkey ("3", MENU_PAGER, OP_JUMP);
609 km_bindkey ("4", MENU_PAGER, OP_JUMP);
610 km_bindkey ("5", MENU_PAGER, OP_JUMP);
611 km_bindkey ("6", MENU_PAGER, OP_JUMP);
612 km_bindkey ("7", MENU_PAGER, OP_JUMP);
613 km_bindkey ("8", MENU_PAGER, OP_JUMP);
614 km_bindkey ("9", MENU_PAGER, OP_JUMP);
616 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
618 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
619 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
620 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
622 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
623 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
625 /* edit-to (default "t") hides generic tag-entry in Compose menu
626 This will bind tag-entry to "T" in the Compose menu */
627 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
630 void km_error_key (int menu)
632 char buf[SHORT_STRING];
633 struct keymap_t *key;
635 if (!(key = km_find_func (menu, OP_HELP)))
636 key = km_find_func (MENU_GENERIC, OP_HELP);
638 if (!(km_expand_key (buf, sizeof (buf), key))) {
639 mutt_error _("Key is not bound.");
644 /* make sure the key is really the help key in this menu */
646 if (km_dokey (menu) != OP_HELP) {
647 mutt_error _("Key is not bound.");
652 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
656 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
661 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
663 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
667 push_string (buf->data);
671 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
672 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
682 mutt_extract_token (&buf, s, 0);
685 while (i < maxmenus) {
690 if ((menu[i] = mutt_check_menu (p)) == -1) {
691 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
702 mutt_extract_token (&buf, s, 0);
705 m_strcpy(err->data, err->dsize, _("null key sequence"));
707 else if (MoreArgs (s))
711 m_strcpy(err->data, err->dsize, _("too few arguments"));
719 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
723 for (i = 0; bindings[i].name; i++)
724 if (m_strcmp(func, bindings[i].name) == 0) {
725 km_bindkey (key, menu, bindings[i].op);
731 struct binding_t *km_get_table (int menu)
758 #ifdef CRYPT_BACKEND_GPGME
759 case MENU_KEY_SELECT_PGP:
761 case MENU_KEY_SELECT_SMIME:
774 /* bind menu-name '<key_sequence>' function-name */
775 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
778 struct binding_t *bindings = NULL;
780 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
783 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
784 &nummenus, err)) == NULL)
787 /* function to execute */
788 mutt_extract_token (buf, s, 0);
790 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
793 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
794 for (i = 0; i < nummenus; ++i) {
795 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
799 for (i = 0; i < nummenus; ++i) {
800 /* First check the "generic" list of commands */
801 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
802 menu[i] == MENU_GENERIC ||
803 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
804 /* Now check the menu-specific list of commands (if they exist) */
805 bindings = km_get_table (menu[i]);
806 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
807 snprintf (err->data, err->dsize, _("%s: no such function in map"),
818 /* macro <menu> <key> <macro> <description> */
819 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
822 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
828 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
832 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
833 /* make sure the macro sequence is not an empty string */
835 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
839 seq = m_strdup(buf->data);
840 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
843 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
846 for (i = 0; i < nummenus; ++i) {
847 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
855 for (i = 0; i < nummenus; ++i) {
856 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
865 /* exec function-name */
866 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
871 struct binding_t *bindings = NULL;
875 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
880 mutt_extract_token (buf, s, 0);
881 function = buf->data;
883 if ((bindings = km_get_table (CurrentMenu)) == NULL
884 && CurrentMenu != MENU_PAGER)
885 bindings = OpGeneric;
887 ops[nops] = get_op (bindings, function, m_strlen(function));
888 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
889 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
891 if (ops[nops] == OP_NULL) {
893 mutt_error (_("%s: no such function"), function);
898 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
901 mutt_ungetch (0, ops[--nops]);
907 * prompts the user to enter a keystroke, and displays the octal value back
910 void mutt_what_key (void)
914 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
917 if (ch != ERR && ch != ctrl ('G')) {
918 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
919 km_keyname (ch), ch, ch);
922 while (ch != ERR && ch != ctrl ('G'));