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.
17 #include "mutt_menu.h"
18 #include "mutt_curses.h"
21 #include "mutt_crypt.h"
31 #include "functions.h"
33 struct mapping_t Menus[] = {
34 {"alias", MENU_ALIAS},
35 {"attach", MENU_ATTACH},
36 {"browser", MENU_FOLDER},
37 {"compose", MENU_COMPOSE},
38 {"editor", MENU_EDITOR},
40 {"pager", MENU_PAGER},
41 {"postpone", MENU_POST},
43 {"smime", MENU_SMIME},
46 {"key_select_pgp", MENU_KEY_SELECT_PGP},
47 {"key_select_smime", MENU_KEY_SELECT_SMIME},
56 {"query", MENU_QUERY},
57 {"generic", MENU_GENERIC},
61 #define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
63 static struct mapping_t KeyNames[] = {
64 {"<PageUp>", KEY_PPAGE},
65 {"<PageDown>", KEY_NPAGE},
68 {"<Right>", KEY_RIGHT},
71 {"<BackSpace>", KEY_BACKSPACE},
76 {"<Enter>", KEY_ENTER},
78 {"<Return>", M_ENTER_C},
83 {"<BackTab>", KEY_BTAB},
88 /* contains the last key the user pressed */
91 struct keymap_t *Keymaps[MENU_MAX];
93 static struct keymap_t *allocKeys (int len, keycode_t * keys)
97 p = mem_calloc (1, sizeof (struct keymap_t));
99 p->keys = mem_malloc (len * sizeof (keycode_t));
100 memcpy (p->keys, keys, len * sizeof (keycode_t));
104 static int parse_fkey (char *s)
109 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
112 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
124 * This function parses the string <NNN> and uses the octal value as the key
127 static int parse_keycode (const char *s)
129 if (isdigit ((unsigned char) s[1]) &&
130 isdigit ((unsigned char) s[2]) &&
131 isdigit ((unsigned char) s[3]) && s[4] == '>') {
132 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
137 static int parsekeys (char *str, keycode_t * d, int max)
140 char buff[SHORT_STRING];
144 strfcpy (buff, str, sizeof (buff));
149 if (*s == '<' && (t = strchr (s, '>'))) {
154 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
158 else if ((n = parse_fkey (s)) > 0) {
162 else if ((n = parse_keycode (s)) > 0) {
171 *d = (unsigned char) *s;
181 /* insert a key sequence into the specified map. the map is sorted by ASCII
182 * value (lowest to highest)
184 void km_bind (char *s, int menu, int op, char *macro, char *descr)
186 struct keymap_t *map, *tmp, *last = NULL, *next;
187 keycode_t buf[MAX_SEQ];
188 int len, pos = 0, lastpos = 0;
190 len = parsekeys (s, buf, MAX_SEQ);
192 map = allocKeys (len, buf);
194 map->macro = str_dup (macro);
195 map->descr = str_dup (descr);
200 if (pos >= len || pos >= tmp->len) {
201 /* map and tmp match, but have different lengths, so overwrite */
205 mem_free (&tmp->macro);
206 mem_free (&tmp->keys);
207 mem_free (&tmp->descr);
211 while (tmp && len >= pos);
215 else if (buf[pos] == tmp->keys[pos])
217 else if (buf[pos] < tmp->keys[pos]) {
218 /* found location to insert between last and tmp */
222 else { /* buf[pos] > tmp->keys[pos] */
241 void km_bindkey (char *s, int menu, int op)
243 km_bind (s, menu, op, NULL, NULL);
246 static int get_op (struct binding_t *bindings, const char *start, size_t len)
250 for (i = 0; bindings[i].name; i++) {
251 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
252 str_len (bindings[i].name) == len)
253 return bindings[i].op;
259 static char *get_func (struct binding_t *bindings, int op)
263 for (i = 0; bindings[i].name; i++) {
264 if (bindings[i].op == op) {
265 return bindings[i].name;
272 static void push_string (char *s)
274 char *pp, *p = s + str_len (s) - 1;
279 /* if we see something like "<PageUp>", look to see if it is a real
280 function name and return the corresponding value */
282 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
284 if ((i = parse_fkey (pp)) > 0) {
285 mutt_ungetch (KEY_F (i), 0);
291 for (i = 0; KeyNames[i].name; i++) {
292 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
295 if (KeyNames[i].name) {
297 mutt_ungetch (KeyNames[i].value, 0);
302 /* See if it is a valid command
303 * skip the '<' and the '>' when comparing */
304 for (i = 0; Menus[i].name; i++) {
305 struct binding_t *binding = km_get_table (Menus[i].value);
308 op = get_op (binding, pp + 1, l - 2);
315 mutt_ungetch (0, op);
321 mutt_ungetch (*p--, 0);
325 static int retry_generic (int menu, keycode_t * keys, int keyslen,
328 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
330 mutt_ungetch (lastkey, 0);
331 for (; keyslen; keyslen--)
332 mutt_ungetch (keys[keyslen - 1], 0);
333 return (km_dokey (MENU_GENERIC));
335 if (menu != MENU_EDITOR) {
336 /* probably a good idea to flush input here so we can abort macros */
343 * >0 function to execute
344 * OP_NULL no function bound to key sequence
345 * -1 error occured while reading input
347 int km_dokey (int menu)
350 struct keymap_t *map = Keymaps[menu];
357 return (retry_generic (menu, NULL, 0, 0));
360 /* ncurses doesn't return on resized screen when timeout is set to zero */
361 if (menu != MENU_EDITOR)
362 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
366 if (menu != MENU_EDITOR)
367 timeout (-1); /* restore blocking operation */
373 /* do we have an op already? */
376 struct binding_t *bindings;
378 /* is this a valid op for this menu? */
379 if ((bindings = km_get_table (menu)) &&
380 (func = get_func (bindings, tmp.op)))
383 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
386 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
387 /* check generic menu */
388 bindings = OpGeneric;
389 if ((func = get_func (bindings, tmp.op)))
393 /* Sigh. Valid function but not in this context.
394 * Find the literal string and push it back */
395 for (i = 0; Menus[i].name; i++) {
396 bindings = km_get_table (Menus[i].value);
398 func = get_func (bindings, tmp.op);
400 /* careful not to feed the <..> as one token. otherwise
401 * push_string() will push the bogus op right back! */
402 mutt_ungetch ('>', 0);
404 mutt_ungetch ('<', 0);
409 /* continue to chew */
414 /* Nope. Business as usual */
415 while (LastKey > map->keys[pos]) {
416 if (pos > map->eq || !map->next)
417 return (retry_generic (menu, map->keys, pos, LastKey));
421 if (LastKey != map->keys[pos])
422 return (retry_generic (menu, map->keys, pos, LastKey));
424 if (++pos == map->len) {
426 if (map->op != OP_MACRO)
431 mutt_error _("Macro loop detected.");
436 push_string (map->macro);
445 static void create_bindings (struct binding_t *map, int menu)
449 for (i = 0; map[i].name; i++)
451 km_bindkey (map[i].seq, menu, map[i].op);
454 char *km_keyname (int c)
459 if ((p = mutt_getnamebyvalue (c, KeyNames)))
462 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
468 buf[1] = (c + '@') & 0x7f;
472 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
474 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
475 sprintf (buf, "<F%d>", c - KEY_F0);
476 else if (IsPrint (c))
477 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
479 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
483 int km_expand_key (char *s, size_t len, struct keymap_t *map)
492 strfcpy (s, km_keyname (map->keys[p]), len);
493 len -= (l = str_len (s));
495 if (++p >= map->len || !len)
504 struct keymap_t *km_find_func (int menu, int func)
506 struct keymap_t *map = Keymaps[menu];
508 for (; map; map = map->next)
516 memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX);
518 create_bindings (OpAttach, MENU_ATTACH);
519 create_bindings (OpBrowser, MENU_FOLDER);
520 create_bindings (OpCompose, MENU_COMPOSE);
521 create_bindings (OpMain, MENU_MAIN);
522 create_bindings (OpPager, MENU_PAGER);
523 create_bindings (OpPost, MENU_POST);
524 create_bindings (OpQuery, MENU_QUERY);
525 create_bindings (OpAlias, MENU_ALIAS);
528 if ((WithCrypto & APPLICATION_PGP))
529 create_bindings (OpPgp, MENU_PGP);
531 if ((WithCrypto & APPLICATION_SMIME))
532 create_bindings (OpSmime, MENU_SMIME);
534 #ifdef CRYPT_BACKEND_GPGME
535 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
536 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
540 create_bindings (OpMix, MENU_MIX);
542 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
543 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
544 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
547 /* bindings for the line editor */
548 create_bindings (OpEditor, MENU_EDITOR);
550 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
551 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
552 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
553 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
554 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
555 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
556 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
557 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
558 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
560 /* generic menu keymap */
561 create_bindings (OpGeneric, MENU_GENERIC);
563 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
564 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
565 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
566 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
567 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
568 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
569 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
570 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
571 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
572 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
573 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
574 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
575 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
576 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
577 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
578 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
579 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
581 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
583 /* Miscellaneous extra bindings */
585 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
586 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
587 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
588 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
589 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
590 km_bindkey ("x", MENU_MAIN, OP_EXIT);
592 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
594 km_bindkey ("x", MENU_PAGER, OP_EXIT);
595 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
596 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
597 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
598 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
599 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
600 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
601 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
602 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
603 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
604 km_bindkey ("1", MENU_PAGER, OP_JUMP);
605 km_bindkey ("2", MENU_PAGER, OP_JUMP);
606 km_bindkey ("3", MENU_PAGER, OP_JUMP);
607 km_bindkey ("4", MENU_PAGER, OP_JUMP);
608 km_bindkey ("5", MENU_PAGER, OP_JUMP);
609 km_bindkey ("6", MENU_PAGER, OP_JUMP);
610 km_bindkey ("7", MENU_PAGER, OP_JUMP);
611 km_bindkey ("8", MENU_PAGER, OP_JUMP);
612 km_bindkey ("9", MENU_PAGER, OP_JUMP);
614 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
616 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
617 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
618 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
620 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
621 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
623 /* edit-to (default "t") hides generic tag-entry in Compose menu
624 This will bind tag-entry to "T" in the Compose menu */
625 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
628 void km_error_key (int menu)
630 char buf[SHORT_STRING];
631 struct keymap_t *key;
633 if (!(key = km_find_func (menu, OP_HELP)))
634 key = km_find_func (MENU_GENERIC, OP_HELP);
636 if (!(km_expand_key (buf, sizeof (buf), key))) {
637 mutt_error _("Key is not bound.");
642 /* make sure the key is really the help key in this menu */
644 if (km_dokey (menu) != OP_HELP) {
645 mutt_error _("Key is not bound.");
650 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
654 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data,
659 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
661 strfcpy (err->data, _("push: too many arguments"), err->dsize);
665 push_string (buf->data);
669 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
670 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
677 memset (&buf, 0, sizeof (buf));
680 mutt_extract_token (&buf, s, 0);
683 while (i < maxmenus) {
688 if ((menu[i] = mutt_check_menu (p)) == -1) {
689 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
700 mutt_extract_token (&buf, s, 0);
703 strfcpy (err->data, _("null key sequence"), err->dsize);
705 else if (MoreArgs (s))
709 strfcpy (err->data, _("too few arguments"), err->dsize);
712 mem_free (&buf.data);
717 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
721 for (i = 0; bindings[i].name; i++)
722 if (str_cmp (func, bindings[i].name) == 0) {
723 km_bindkey (key, menu, bindings[i].op);
729 struct binding_t *km_get_table (int menu)
754 return (WithCrypto & APPLICATION_PGP) ? OpPgp : NULL;
756 #ifdef CRYPT_BACKEND_GPGME
757 case MENU_KEY_SELECT_PGP:
759 case MENU_KEY_SELECT_SMIME:
772 /* bind menu-name '<key_sequence>' function-name */
773 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data,
776 struct binding_t *bindings = NULL;
778 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
781 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
782 &nummenus, err)) == NULL)
785 /* function to execute */
786 mutt_extract_token (buf, s, 0);
788 strfcpy (err->data, _("bind: too many arguments"), err->dsize);
791 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
792 for (i = 0; i < nummenus; ++i) {
793 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
797 for (i = 0; i < nummenus; ++i) {
798 /* First check the "generic" list of commands */
799 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
800 menu[i] == MENU_GENERIC ||
801 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
802 /* Now check the menu-specific list of commands (if they exist) */
803 bindings = km_get_table (menu[i]);
804 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
805 snprintf (err->data, err->dsize, _("%s: no such function in map"),
816 /* macro <menu> <key> <macro> <description> */
817 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data,
820 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
826 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
830 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
831 /* make sure the macro sequence is not an empty string */
833 strfcpy (err->data, _("macro: empty key sequence"), err->dsize);
837 seq = str_dup (buf->data);
838 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
841 strfcpy (err->data, _("macro: too many arguments"), err->dsize);
844 for (i = 0; i < nummenus; ++i) {
845 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
853 for (i = 0; i < nummenus; ++i) {
854 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
863 /* exec function-name */
864 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data,
869 struct binding_t *bindings = NULL;
873 strfcpy (err->data, _("exec: no arguments"), err->dsize);
878 mutt_extract_token (buf, s, 0);
879 function = buf->data;
881 if ((bindings = km_get_table (CurrentMenu)) == NULL
882 && CurrentMenu != MENU_PAGER)
883 bindings = OpGeneric;
885 ops[nops] = get_op (bindings, function, str_len (function));
886 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
887 ops[nops] = get_op (OpGeneric, function, str_len (function));
889 if (ops[nops] == OP_NULL) {
891 mutt_error (_("%s: no such function"), function);
896 while (MoreArgs (s) && nops < sizeof (ops) / sizeof (ops[0]));
899 mutt_ungetch (0, ops[--nops]);
905 * prompts the user to enter a keystroke, and displays the octal value back
908 void mutt_what_key (void)
912 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
915 if (ch != ERR && ch != ctrl ('G')) {
916 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
917 km_keyname (ch), ch, ch);
920 while (ch != ERR && ch != ctrl ('G'));