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>
22 #include "mutt_menu.h"
23 #include "mutt_curses.h"
25 #include "mutt_crypt.h"
32 #define MUTT_FUNCTIONS_VALUES
33 #include "functions.def"
35 struct mapping_t Menus[] = {
36 {"alias", MENU_ALIAS},
37 {"attach", MENU_ATTACH},
38 {"browser", MENU_FOLDER},
39 {"compose", MENU_COMPOSE},
40 {"editor", MENU_EDITOR},
42 {"pager", MENU_PAGER},
43 {"postpone", MENU_POST},
45 {"smime", MENU_SMIME},
48 {"key_select_pgp", MENU_KEY_SELECT_PGP},
49 {"key_select_smime", MENU_KEY_SELECT_SMIME},
58 {"query", MENU_QUERY},
59 {"generic", MENU_GENERIC},
63 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
65 static struct mapping_t KeyNames[] = {
66 {"<PageUp>", KEY_PPAGE},
67 {"<PageDown>", KEY_NPAGE},
70 {"<Right>", KEY_RIGHT},
73 {"<BackSpace>", KEY_BACKSPACE},
78 {"<Enter>", KEY_ENTER},
80 {"<Return>", M_ENTER_C},
85 {"<BackTab>", KEY_BTAB},
93 /* contains the last key the user pressed */
96 struct keymap_t *Keymaps[MENU_MAX];
98 static struct keymap_t *allocKeys(int len, keycode_t *keys)
102 p = p_new(struct keymap_t, 1);
104 p->keys = p_dup(keys, len);
108 static int parse_fkey (char *s)
113 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
116 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
128 * This function parses the string <NNN> and uses the octal value as the key
131 static int parse_keycode (const char *s)
133 if (isdigit ((unsigned char) s[1]) &&
134 isdigit ((unsigned char) s[2]) &&
135 isdigit ((unsigned char) s[3]) && s[4] == '>') {
136 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
141 static int parsekeys (const char *str, keycode_t * d, int max)
144 char buff[SHORT_STRING];
148 m_strcpy(buff, sizeof(buff), str);
153 if (*s == '<' && (t = strchr (s, '>'))) {
158 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
162 else if ((n = parse_fkey (s)) > 0) {
166 else if ((n = parse_keycode (s)) > 0) {
175 *d = (unsigned char) *s;
185 /* insert a key sequence into the specified map. the map is sorted by ASCII
186 * value (lowest to highest)
188 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
190 struct keymap_t *map, *tmp, *last = NULL, *next;
191 keycode_t buf[MAX_SEQ];
192 int len, pos = 0, lastpos = 0;
194 len = parsekeys (s, buf, MAX_SEQ);
196 map = allocKeys (len, buf);
198 map->macro = m_strdup(macro);
199 map->descr = m_strdup(descr);
204 if (pos >= len || pos >= tmp->len) {
205 /* map and tmp match, but have different lengths, so overwrite */
209 p_delete(&tmp->macro);
210 p_delete(&tmp->keys);
211 p_delete(&tmp->descr);
215 while (tmp && len >= pos);
219 else if (buf[pos] == tmp->keys[pos])
221 else if (buf[pos] < tmp->keys[pos]) {
222 /* found location to insert between last and tmp */
226 else { /* buf[pos] > tmp->keys[pos] */
245 void km_bindkey (const char *s, int menu, int op)
247 km_bind (s, menu, op, NULL, NULL);
250 static int get_op (struct binding_t *bindings, const char *start, size_t len)
254 for (i = 0; bindings[i].name; i++) {
255 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
256 m_strlen(bindings[i].name) == len)
257 return bindings[i].op;
263 static const char *get_func (struct binding_t *bindings, int op)
267 for (i = 0; bindings[i].name; i++) {
268 if (bindings[i].op == op) {
269 return bindings[i].name;
276 static void push_string(const char *s)
278 const char *pp, *p = s + m_strlen(s) - 1;
283 /* if we see something like "<PageUp>", look to see if it is a real
284 function name and return the corresponding value */
286 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
288 if ((i = parse_fkey (pp)) > 0) {
289 mutt_ungetch (KEY_F (i), 0);
295 for (i = 0; KeyNames[i].name; i++) {
296 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
299 if (KeyNames[i].name) {
301 mutt_ungetch (KeyNames[i].value, 0);
306 /* See if it is a valid command
307 * skip the '<' and the '>' when comparing */
308 for (i = 0; Menus[i].name; i++) {
309 struct binding_t *binding = km_get_table (Menus[i].value);
312 op = get_op (binding, pp + 1, l - 2);
319 mutt_ungetch (0, op);
325 mutt_ungetch ((unsigned char) *p--, 0);
329 static int retry_generic (int menu, keycode_t * keys, int keyslen,
332 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
334 mutt_ungetch (lastkey, 0);
335 for (; keyslen; keyslen--)
336 mutt_ungetch (keys[keyslen - 1], 0);
337 return (km_dokey (MENU_GENERIC));
339 if (menu != MENU_EDITOR) {
340 /* probably a good idea to flush input here so we can abort macros */
347 * >0 function to execute
348 * OP_NULL no function bound to key sequence
349 * -1 error occured while reading input
351 int km_dokey (int menu)
354 struct keymap_t *map = Keymaps[menu];
361 return (retry_generic (menu, NULL, 0, 0));
364 /* ncurses doesn't return on resized screen when timeout is set to zero */
365 if (menu != MENU_EDITOR)
366 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
370 if (menu != MENU_EDITOR)
371 timeout (-1); /* restore blocking operation */
377 /* do we have an op already? */
379 const char *func = NULL;
380 struct binding_t *bindings;
382 /* is this a valid op for this menu? */
383 if ((bindings = km_get_table (menu)) &&
384 (func = get_func (bindings, tmp.op)))
387 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
390 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
391 /* check generic menu */
392 bindings = OpGeneric;
393 if ((func = get_func (bindings, tmp.op)))
397 /* Sigh. Valid function but not in this context.
398 * Find the literal string and push it back */
399 for (i = 0; Menus[i].name; i++) {
400 bindings = km_get_table (Menus[i].value);
402 func = get_func (bindings, tmp.op);
404 /* careful not to feed the <..> as one token. otherwise
405 * push_string() will push the bogus op right back! */
406 mutt_ungetch ('>', 0);
408 mutt_ungetch ('<', 0);
413 /* continue to chew */
418 /* Nope. Business as usual */
419 while (LastKey > map->keys[pos]) {
420 if (pos > map->eq || !map->next)
421 return (retry_generic (menu, map->keys, pos, LastKey));
425 if (LastKey != map->keys[pos])
426 return (retry_generic (menu, map->keys, pos, LastKey));
428 if (++pos == map->len) {
430 if (map->op != OP_MACRO)
435 mutt_error _("Macro loop detected.");
440 push_string (map->macro);
449 static void create_bindings (struct binding_t *map, int menu)
453 for (i = 0; map[i].name; i++)
455 km_bindkey (map[i].seq, menu, map[i].op);
458 const char *km_keyname(int c)
463 if ((p = mutt_getnamebyvalue (c, KeyNames)))
466 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
472 buf[1] = (c + '@') & 0x7f;
476 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
478 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
479 sprintf (buf, "<F%d>", c - KEY_F0);
480 else if (IsPrint (c))
481 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
483 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
487 int km_expand_key (char *s, size_t len, struct keymap_t *map)
496 m_strcpy(s, len, km_keyname(map->keys[p]));
497 len -= (l = m_strlen(s));
499 if (++p >= map->len || !len)
508 struct keymap_t *km_find_func (int menu, int func)
510 struct keymap_t *map = Keymaps[menu];
512 for (; map; map = map->next)
520 p_clear(Keymaps, MENU_MAX);
522 create_bindings (OpAttach, MENU_ATTACH);
523 create_bindings (OpBrowser, MENU_FOLDER);
524 create_bindings (OpCompose, MENU_COMPOSE);
525 create_bindings (OpMain, MENU_MAIN);
526 create_bindings (OpPager, MENU_PAGER);
527 create_bindings (OpPost, MENU_POST);
528 create_bindings (OpQuery, MENU_QUERY);
529 create_bindings (OpAlias, MENU_ALIAS);
532 if ((WithCrypto & APPLICATION_PGP))
533 create_bindings (OpPgp, MENU_PGP);
535 if ((WithCrypto & APPLICATION_SMIME))
536 create_bindings (OpSmime, MENU_SMIME);
538 #ifdef CRYPT_BACKEND_GPGME
539 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
540 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
544 create_bindings (OpMix, MENU_MIX);
546 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
547 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
548 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
551 /* bindings for the line editor */
552 create_bindings (OpEditor, MENU_EDITOR);
554 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
555 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
556 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
557 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
558 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
559 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
560 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
561 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
562 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
564 /* generic menu keymap */
565 create_bindings (OpGeneric, MENU_GENERIC);
567 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
568 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
569 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
570 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
571 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
572 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
573 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
574 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
575 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
576 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
577 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
578 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
579 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
580 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
581 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
582 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
583 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
585 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
587 /* Miscellaneous extra bindings */
589 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
590 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
591 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
592 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
593 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
594 km_bindkey ("x", MENU_MAIN, OP_EXIT);
596 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
598 km_bindkey ("x", MENU_PAGER, OP_EXIT);
599 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
600 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
601 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
602 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
603 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
604 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
605 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
606 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
607 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
608 km_bindkey ("1", MENU_PAGER, OP_JUMP);
609 km_bindkey ("2", MENU_PAGER, OP_JUMP);
610 km_bindkey ("3", MENU_PAGER, OP_JUMP);
611 km_bindkey ("4", MENU_PAGER, OP_JUMP);
612 km_bindkey ("5", MENU_PAGER, OP_JUMP);
613 km_bindkey ("6", MENU_PAGER, OP_JUMP);
614 km_bindkey ("7", MENU_PAGER, OP_JUMP);
615 km_bindkey ("8", MENU_PAGER, OP_JUMP);
616 km_bindkey ("9", MENU_PAGER, OP_JUMP);
618 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
620 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
621 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
622 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
624 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
625 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
627 /* edit-to (default "t") hides generic tag-entry in Compose menu
628 This will bind tag-entry to "T" in the Compose menu */
629 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
632 void km_error_key (int menu)
634 char buf[SHORT_STRING];
635 struct keymap_t *key;
637 if (!(key = km_find_func (menu, OP_HELP)))
638 key = km_find_func (MENU_GENERIC, OP_HELP);
640 if (!(km_expand_key (buf, sizeof (buf), key))) {
641 mutt_error _("Key is not bound.");
646 /* make sure the key is really the help key in this menu */
648 if (km_dokey (menu) != OP_HELP) {
649 mutt_error _("Key is not bound.");
654 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
658 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
663 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
665 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
669 push_string (buf->data);
673 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
674 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
684 mutt_extract_token (&buf, s, 0);
687 while (i < maxmenus) {
692 if ((menu[i] = mutt_check_menu (p)) == -1) {
693 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
704 mutt_extract_token (&buf, s, 0);
707 m_strcpy(err->data, err->dsize, _("null key sequence"));
709 else if (MoreArgs (s))
713 m_strcpy(err->data, err->dsize, _("too few arguments"));
721 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
725 for (i = 0; bindings[i].name; i++)
726 if (m_strcmp(func, bindings[i].name) == 0) {
727 km_bindkey (key, menu, bindings[i].op);
733 struct binding_t *km_get_table (int menu)
758 return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
760 #ifdef CRYPT_BACKEND_GPGME
761 case MENU_KEY_SELECT_PGP:
763 case MENU_KEY_SELECT_SMIME:
776 /* bind menu-name '<key_sequence>' function-name */
777 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
780 struct binding_t *bindings = NULL;
782 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
785 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
786 &nummenus, err)) == NULL)
789 /* function to execute */
790 mutt_extract_token (buf, s, 0);
792 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
795 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
796 for (i = 0; i < nummenus; ++i) {
797 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
801 for (i = 0; i < nummenus; ++i) {
802 /* First check the "generic" list of commands */
803 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
804 menu[i] == MENU_GENERIC ||
805 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
806 /* Now check the menu-specific list of commands (if they exist) */
807 bindings = km_get_table (menu[i]);
808 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
809 snprintf (err->data, err->dsize, _("%s: no such function in map"),
820 /* macro <menu> <key> <macro> <description> */
821 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
824 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
830 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
834 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
835 /* make sure the macro sequence is not an empty string */
837 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
841 seq = m_strdup(buf->data);
842 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
845 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
848 for (i = 0; i < nummenus; ++i) {
849 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
857 for (i = 0; i < nummenus; ++i) {
858 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
867 /* exec function-name */
868 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
873 struct binding_t *bindings = NULL;
877 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
882 mutt_extract_token (buf, s, 0);
883 function = buf->data;
885 if ((bindings = km_get_table (CurrentMenu)) == NULL
886 && CurrentMenu != MENU_PAGER)
887 bindings = OpGeneric;
889 ops[nops] = get_op (bindings, function, m_strlen(function));
890 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
891 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
893 if (ops[nops] == OP_NULL) {
895 mutt_error (_("%s: no such function"), function);
900 while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
903 mutt_ungetch (0, ops[--nops]);
909 * prompts the user to enter a keystroke, and displays the octal value back
912 void mutt_what_key (void)
916 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
919 if (ch != ERR && ch != ctrl ('G')) {
920 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
921 km_keyname (ch), ch, ch);
924 while (ch != ERR && ch != ctrl ('G'));