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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-ui/curses.h>
13 #include <lib-ui/menu.h>
17 #include <lib-crypt/crypt.h>
19 #define MUTT_FUNCTIONS_VALUES
20 #include "functions.def"
22 struct mapping_t Menus[] = {
23 {"alias", MENU_ALIAS},
24 {"attach", MENU_ATTACH},
25 {"browser", MENU_FOLDER},
26 {"compose", MENU_COMPOSE},
27 {"editor", MENU_EDITOR},
29 {"pager", MENU_PAGER},
30 {"postpone", MENU_POST},
32 {"smime", MENU_SMIME},
35 {"key_select_pgp", MENU_KEY_SELECT_PGP},
36 {"key_select_smime", MENU_KEY_SELECT_SMIME},
40 {"query", MENU_QUERY},
41 {"generic", MENU_GENERIC},
45 static struct mapping_t KeyNames[] = {
46 {"<PageUp>", KEY_PPAGE},
47 {"<PageDown>", KEY_NPAGE},
50 {"<Right>", KEY_RIGHT},
53 {"<BackSpace>", KEY_BACKSPACE},
58 {"<Enter>", KEY_ENTER},
60 {"<Return>", M_ENTER_C},
65 {"<BackTab>", KEY_BTAB},
73 /* contains the last key the user pressed */
76 struct keymap_t *Keymaps[MENU_MAX];
78 static struct keymap_t *allocKeys(int len, keycode_t *keys)
82 p = p_new(struct keymap_t, 1);
84 p->keys = p_dup(keys, len);
88 static int parse_fkey (const char *s)
92 if (s[0] != '<' || ascii_tolower(s[1]) != 'f')
95 n = strtol(s + 2, (char **)&s, 10);
96 return *s == '>' ? n : -1;
100 * This function parses the string <NNN> and uses the octal value as the key
103 static int parse_keycode (const char *s)
109 n = strtol(s + 1, (char **)&s, 8);
110 return *s == '>' ? n : -1;
113 static int parsekeys (const char *str, keycode_t * d, int max)
120 m_strcpy(buff, sizeof(buff), str);
125 if (*s == '<' && (t = strchr (s, '>'))) {
130 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
134 else if ((n = parse_fkey (s)) > 0) {
138 else if ((n = parse_keycode (s)) > 0) {
147 *d = (unsigned char) *s;
157 /* insert a key sequence into the specified map. the map is sorted by ASCII
158 * value (lowest to highest)
160 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
162 struct keymap_t *map, *tmp, *last = NULL, *next;
163 keycode_t buf[MAX_SEQ];
164 int len, pos = 0, lastpos = 0;
166 len = parsekeys (s, buf, MAX_SEQ);
168 map = allocKeys (len, buf);
170 map->macro = m_strdup(macro);
171 map->descr = m_strdup(descr);
176 if (pos >= len || pos >= tmp->len) {
177 /* map and tmp match, but have different lengths, so overwrite */
181 p_delete(&tmp->macro);
182 p_delete(&tmp->keys);
183 p_delete(&tmp->descr);
187 while (tmp && len >= pos);
191 else if (buf[pos] == tmp->keys[pos])
193 else if (buf[pos] < tmp->keys[pos]) {
194 /* found location to insert between last and tmp */
198 else { /* buf[pos] > tmp->keys[pos] */
217 void km_bindkey (const char *s, int menu, int op)
219 km_bind (s, menu, op, NULL, NULL);
222 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
226 for (i = 0; bindings[i].name; i++) {
227 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
228 m_strlen(bindings[i].name) == len)
229 return bindings[i].op;
235 static const char *get_func (struct binding_t *bindings, int op)
239 for (i = 0; bindings[i].name; i++) {
240 if (bindings[i].op == op) {
241 return bindings[i].name;
248 static void push_string(const char *s)
250 const char *pp, *p = s + m_strlen(s) - 1;
255 /* if we see something like "<PageUp>", look to see if it is a real
256 function name and return the corresponding value */
258 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
260 if ((i = parse_fkey (pp)) > 0) {
261 mutt_ungetch (KEY_F (i), 0);
267 for (i = 0; KeyNames[i].name; i++) {
268 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
271 if (KeyNames[i].name) {
273 mutt_ungetch (KeyNames[i].value, 0);
278 /* See if it is a valid command
279 * skip the '<' and the '>' when comparing */
280 for (i = 0; Menus[i].name; i++) {
281 struct binding_t *binding = km_get_table (Menus[i].value);
284 op = get_op (binding, pp + 1, l - 2);
291 mutt_ungetch (0, op);
297 mutt_ungetch ((unsigned char) *p--, 0);
301 static int retry_generic (int menu, keycode_t * keys, int keyslen,
304 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
306 mutt_ungetch (lastkey, 0);
307 for (; keyslen; keyslen--)
308 mutt_ungetch (keys[keyslen - 1], 0);
309 return (km_dokey (MENU_GENERIC));
311 if (menu != MENU_EDITOR) {
312 /* probably a good idea to flush input here so we can abort macros */
319 * >0 function to execute
320 * OP_NULL no function bound to key sequence
321 * -1 error occured while reading input
323 int km_dokey (int menu)
326 struct keymap_t *map = Keymaps[menu];
333 return (retry_generic (menu, NULL, 0, 0));
336 /* ncurses doesn't return on resized screen when timeout is set to zero */
337 if (menu != MENU_EDITOR)
338 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
342 if (menu != MENU_EDITOR)
343 timeout (-1); /* restore blocking operation */
349 /* do we have an op already? */
351 const char *func = NULL;
352 struct binding_t *bindings;
354 /* is this a valid op for this menu? */
355 if ((bindings = km_get_table (menu)) &&
356 (func = get_func (bindings, tmp.op)))
359 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
362 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
363 /* check generic menu */
364 bindings = OpGeneric;
365 if ((func = get_func (bindings, tmp.op)))
369 /* Sigh. Valid function but not in this context.
370 * Find the literal string and push it back */
371 for (i = 0; Menus[i].name; i++) {
372 bindings = km_get_table (Menus[i].value);
374 func = get_func (bindings, tmp.op);
376 /* careful not to feed the <..> as one token. otherwise
377 * push_string() will push the bogus op right back! */
378 mutt_ungetch ('>', 0);
380 mutt_ungetch ('<', 0);
385 /* continue to chew */
390 /* Nope. Business as usual */
391 while (LastKey > map->keys[pos]) {
392 if (pos > map->eq || !map->next)
393 return (retry_generic (menu, map->keys, pos, LastKey));
397 if (LastKey != map->keys[pos])
398 return (retry_generic (menu, map->keys, pos, LastKey));
400 if (++pos == map->len) {
402 if (map->op != OP_MACRO)
407 mutt_error _("Macro loop detected.");
412 push_string (map->macro);
421 static void create_bindings (struct binding_t *map, int menu)
425 for (i = 0; map[i].name; i++)
427 km_bindkey (map[i].seq, menu, map[i].op);
430 const char *km_keyname(int c)
435 if ((p = mutt_getnamebyvalue (c, KeyNames)))
438 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
444 buf[1] = (c + '@') & 0x7f;
448 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
450 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
451 sprintf (buf, "<F%d>", c - KEY_F0);
452 else if (isprint((unsigned char)c))
453 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
455 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
459 int km_expand_key (char *s, size_t len, struct keymap_t *map)
468 m_strcpy(s, len, km_keyname(map->keys[p]));
469 len -= (l = m_strlen(s));
471 if (++p >= map->len || !len)
480 struct keymap_t *km_find_func (int menu, int func)
482 struct keymap_t *map = Keymaps[menu];
484 for (; map; map = map->next)
492 p_clear(Keymaps, MENU_MAX);
494 create_bindings (OpAttach, MENU_ATTACH);
495 create_bindings (OpBrowser, MENU_FOLDER);
496 create_bindings (OpCompose, MENU_COMPOSE);
497 create_bindings (OpMain, MENU_MAIN);
498 create_bindings (OpPager, MENU_PAGER);
499 create_bindings (OpPost, MENU_POST);
500 create_bindings (OpQuery, MENU_QUERY);
501 create_bindings (OpAlias, MENU_ALIAS);
502 create_bindings (OpPgp, MENU_PGP);
503 create_bindings (OpSmime, MENU_SMIME);
505 #ifdef CRYPT_BACKEND_GPGME
506 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
507 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
510 create_bindings (OpMix, MENU_MIX);
512 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
513 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
514 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
516 /* bindings for the line editor */
517 create_bindings (OpEditor, MENU_EDITOR);
519 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
520 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
521 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
522 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
523 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
524 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
525 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
526 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
527 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
529 /* generic menu keymap */
530 create_bindings (OpGeneric, MENU_GENERIC);
532 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
533 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
534 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
535 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
536 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
537 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
538 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
539 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
540 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
541 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
542 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
543 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
544 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
545 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
546 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
547 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
548 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
550 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
552 /* Miscellaneous extra bindings */
554 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
555 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
556 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
557 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
558 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
559 km_bindkey ("x", MENU_MAIN, OP_EXIT);
561 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
563 km_bindkey ("x", MENU_PAGER, OP_EXIT);
564 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
565 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
566 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
567 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
568 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
569 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
570 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
571 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
572 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
573 km_bindkey ("1", MENU_PAGER, OP_JUMP);
574 km_bindkey ("2", MENU_PAGER, OP_JUMP);
575 km_bindkey ("3", MENU_PAGER, OP_JUMP);
576 km_bindkey ("4", MENU_PAGER, OP_JUMP);
577 km_bindkey ("5", MENU_PAGER, OP_JUMP);
578 km_bindkey ("6", MENU_PAGER, OP_JUMP);
579 km_bindkey ("7", MENU_PAGER, OP_JUMP);
580 km_bindkey ("8", MENU_PAGER, OP_JUMP);
581 km_bindkey ("9", MENU_PAGER, OP_JUMP);
583 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
585 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
586 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
587 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
589 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
590 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
592 /* edit-to (default "t") hides generic tag-entry in Compose menu
593 This will bind tag-entry to "T" in the Compose menu */
594 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
597 void km_error_key (int menu)
600 struct keymap_t *key;
602 if (!(key = km_find_func (menu, OP_HELP)))
603 key = km_find_func (MENU_GENERIC, OP_HELP);
605 if (!(km_expand_key (buf, sizeof (buf), key))) {
606 mutt_error _("Key is not bound.");
611 /* make sure the key is really the help key in this menu */
613 if (km_dokey (menu) != OP_HELP) {
614 mutt_error _("Key is not bound.");
619 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
623 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
628 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
630 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
634 push_string (buf->data);
638 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
639 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
649 mutt_extract_token (&buf, s, 0);
652 while (i < maxmenus) {
657 if ((menu[i] = mutt_getvaluebyname(p, Menus)) == -1) {
658 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
669 mutt_extract_token (&buf, s, 0);
672 m_strcpy(err->data, err->dsize, _("null key sequence"));
674 else if (MoreArgs (s))
678 m_strcpy(err->data, err->dsize, _("too few arguments"));
686 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
690 for (i = 0; bindings[i].name; i++)
691 if (m_strcmp(func, bindings[i].name) == 0) {
692 km_bindkey (key, menu, bindings[i].op);
698 struct binding_t *km_get_table (int menu)
724 #ifdef CRYPT_BACKEND_GPGME
725 case MENU_KEY_SELECT_PGP:
727 case MENU_KEY_SELECT_SMIME:
737 /* bind menu-name '<key_sequence>' function-name */
738 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
741 struct binding_t *bindings = NULL;
743 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
746 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
747 &nummenus, err)) == NULL)
750 /* function to execute */
751 mutt_extract_token (buf, s, 0);
753 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
756 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
757 for (i = 0; i < nummenus; ++i) {
758 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
762 for (i = 0; i < nummenus; ++i) {
763 /* First check the "generic" list of commands */
764 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
765 menu[i] == MENU_GENERIC ||
766 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
767 /* Now check the menu-specific list of commands (if they exist) */
768 bindings = km_get_table (menu[i]);
769 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
770 snprintf (err->data, err->dsize, _("%s: no such function in map"),
781 /* macro <menu> <key> <macro> <description> */
782 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
785 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
791 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
795 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
796 /* make sure the macro sequence is not an empty string */
798 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
802 seq = m_strdup(buf->data);
803 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
806 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
809 for (i = 0; i < nummenus; ++i) {
810 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
818 for (i = 0; i < nummenus; ++i) {
819 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
828 /* exec function-name */
829 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
834 struct binding_t *bindings = NULL;
838 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
843 mutt_extract_token (buf, s, 0);
844 function = buf->data;
846 if ((bindings = km_get_table (CurrentMenu)) == NULL
847 && CurrentMenu != MENU_PAGER)
848 bindings = OpGeneric;
850 ops[nops] = get_op (bindings, function, m_strlen(function));
851 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
852 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
854 if (ops[nops] == OP_NULL) {
856 mutt_error (_("%s: no such function"), function);
861 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
864 mutt_ungetch (0, ops[--nops]);
870 * prompts the user to enter a keystroke, and displays the octal value back
873 void mutt_what_key (void)
877 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
880 if (ch != ERR && ch != ctrl ('G')) {
881 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
882 km_keyname (ch), ch, ch);
885 while (ch != ERR && ch != ctrl ('G'));