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},
43 {"query", MENU_QUERY},
44 {"generic", MENU_GENERIC},
48 static struct mapping_t KeyNames[] = {
49 {"<PageUp>", KEY_PPAGE},
50 {"<PageDown>", KEY_NPAGE},
53 {"<Right>", KEY_RIGHT},
56 {"<BackSpace>", KEY_BACKSPACE},
61 {"<Enter>", KEY_ENTER},
63 {"<Return>", M_ENTER_C},
68 {"<BackTab>", KEY_BTAB},
76 /* contains the last key the user pressed */
79 struct keymap_t *Keymaps[MENU_MAX];
81 static struct keymap_t *allocKeys(int len, keycode_t *keys)
85 p = p_new(struct keymap_t, 1);
87 p->keys = p_dup(keys, len);
91 static int parse_fkey (const char *s)
96 if (s[0] != '<' || ascii_tolower (s[1]) != 'f')
99 for (t = s + 2; *t && isdigit ((unsigned char) *t); t++) {
111 * This function parses the string <NNN> and uses the octal value as the key
114 static int parse_keycode (const char *s)
116 if (isdigit ((unsigned char) s[1]) &&
117 isdigit ((unsigned char) s[2]) &&
118 isdigit ((unsigned char) s[3]) && s[4] == '>') {
119 return (s[3] - '0') + (s[2] - '0') * 8 + (s[1] - '0') * 64;
124 static int parsekeys (const char *str, keycode_t * d, int max)
127 char buff[SHORT_STRING];
131 m_strcpy(buff, sizeof(buff), str);
136 if (*s == '<' && (t = strchr (s, '>'))) {
141 if ((n = mutt_getvaluebyname (s, KeyNames)) != -1) {
145 else if ((n = parse_fkey (s)) > 0) {
149 else if ((n = parse_keycode (s)) > 0) {
158 *d = (unsigned char) *s;
168 /* insert a key sequence into the specified map. the map is sorted by ASCII
169 * value (lowest to highest)
171 void km_bind (const char *s, int menu, int op, char *macro, char *descr)
173 struct keymap_t *map, *tmp, *last = NULL, *next;
174 keycode_t buf[MAX_SEQ];
175 int len, pos = 0, lastpos = 0;
177 len = parsekeys (s, buf, MAX_SEQ);
179 map = allocKeys (len, buf);
181 map->macro = m_strdup(macro);
182 map->descr = m_strdup(descr);
187 if (pos >= len || pos >= tmp->len) {
188 /* map and tmp match, but have different lengths, so overwrite */
192 p_delete(&tmp->macro);
193 p_delete(&tmp->keys);
194 p_delete(&tmp->descr);
198 while (tmp && len >= pos);
202 else if (buf[pos] == tmp->keys[pos])
204 else if (buf[pos] < tmp->keys[pos]) {
205 /* found location to insert between last and tmp */
209 else { /* buf[pos] > tmp->keys[pos] */
228 void km_bindkey (const char *s, int menu, int op)
230 km_bind (s, menu, op, NULL, NULL);
233 static int get_op (struct binding_t *bindings, const char *start, ssize_t len)
237 for (i = 0; bindings[i].name; i++) {
238 if (!ascii_strncasecmp (start, bindings[i].name, len) &&
239 m_strlen(bindings[i].name) == len)
240 return bindings[i].op;
246 static const char *get_func (struct binding_t *bindings, int op)
250 for (i = 0; bindings[i].name; i++) {
251 if (bindings[i].op == op) {
252 return bindings[i].name;
259 static void push_string(const char *s)
261 const char *pp, *p = s + m_strlen(s) - 1;
266 /* if we see something like "<PageUp>", look to see if it is a real
267 function name and return the corresponding value */
269 for (pp = p - 1; pp >= s && *pp != '<'; pp--);
271 if ((i = parse_fkey (pp)) > 0) {
272 mutt_ungetch (KEY_F (i), 0);
278 for (i = 0; KeyNames[i].name; i++) {
279 if (!ascii_strncasecmp (pp, KeyNames[i].name, l))
282 if (KeyNames[i].name) {
284 mutt_ungetch (KeyNames[i].value, 0);
289 /* See if it is a valid command
290 * skip the '<' and the '>' when comparing */
291 for (i = 0; Menus[i].name; i++) {
292 struct binding_t *binding = km_get_table (Menus[i].value);
295 op = get_op (binding, pp + 1, l - 2);
302 mutt_ungetch (0, op);
308 mutt_ungetch ((unsigned char) *p--, 0);
312 static int retry_generic (int menu, keycode_t * keys, int keyslen,
315 if (menu != MENU_EDITOR && menu != MENU_GENERIC && menu != MENU_PAGER) {
317 mutt_ungetch (lastkey, 0);
318 for (; keyslen; keyslen--)
319 mutt_ungetch (keys[keyslen - 1], 0);
320 return (km_dokey (MENU_GENERIC));
322 if (menu != MENU_EDITOR) {
323 /* probably a good idea to flush input here so we can abort macros */
330 * >0 function to execute
331 * OP_NULL no function bound to key sequence
332 * -1 error occured while reading input
334 int km_dokey (int menu)
337 struct keymap_t *map = Keymaps[menu];
344 return (retry_generic (menu, NULL, 0, 0));
347 /* ncurses doesn't return on resized screen when timeout is set to zero */
348 if (menu != MENU_EDITOR)
349 timeout ((Timeout > 0 ? Timeout : 60) * 1000);
353 if (menu != MENU_EDITOR)
354 timeout (-1); /* restore blocking operation */
360 /* do we have an op already? */
362 const char *func = NULL;
363 struct binding_t *bindings;
365 /* is this a valid op for this menu? */
366 if ((bindings = km_get_table (menu)) &&
367 (func = get_func (bindings, tmp.op)))
370 if (menu == MENU_EDITOR && get_func (OpEditor, tmp.op))
373 if (menu != MENU_EDITOR && menu != MENU_PAGER) {
374 /* check generic menu */
375 bindings = OpGeneric;
376 if ((func = get_func (bindings, tmp.op)))
380 /* Sigh. Valid function but not in this context.
381 * Find the literal string and push it back */
382 for (i = 0; Menus[i].name; i++) {
383 bindings = km_get_table (Menus[i].value);
385 func = get_func (bindings, tmp.op);
387 /* careful not to feed the <..> as one token. otherwise
388 * push_string() will push the bogus op right back! */
389 mutt_ungetch ('>', 0);
391 mutt_ungetch ('<', 0);
396 /* continue to chew */
401 /* Nope. Business as usual */
402 while (LastKey > map->keys[pos]) {
403 if (pos > map->eq || !map->next)
404 return (retry_generic (menu, map->keys, pos, LastKey));
408 if (LastKey != map->keys[pos])
409 return (retry_generic (menu, map->keys, pos, LastKey));
411 if (++pos == map->len) {
413 if (map->op != OP_MACRO)
418 mutt_error _("Macro loop detected.");
423 push_string (map->macro);
432 static void create_bindings (struct binding_t *map, int menu)
436 for (i = 0; map[i].name; i++)
438 km_bindkey (map[i].seq, menu, map[i].op);
441 const char *km_keyname(int c)
446 if ((p = mutt_getnamebyvalue (c, KeyNames)))
449 if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) {
455 buf[1] = (c + '@') & 0x7f;
459 snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7);
461 else if (c >= KEY_F0 && c < KEY_F (256)) /* this maximum is just a guess */
462 sprintf (buf, "<F%d>", c - KEY_F0);
463 else if (isprint((unsigned char)c))
464 snprintf (buf, sizeof (buf), "%c", (unsigned char) c);
466 snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c);
470 int km_expand_key (char *s, size_t len, struct keymap_t *map)
479 m_strcpy(s, len, km_keyname(map->keys[p]));
480 len -= (l = m_strlen(s));
482 if (++p >= map->len || !len)
491 struct keymap_t *km_find_func (int menu, int func)
493 struct keymap_t *map = Keymaps[menu];
495 for (; map; map = map->next)
503 p_clear(Keymaps, MENU_MAX);
505 create_bindings (OpAttach, MENU_ATTACH);
506 create_bindings (OpBrowser, MENU_FOLDER);
507 create_bindings (OpCompose, MENU_COMPOSE);
508 create_bindings (OpMain, MENU_MAIN);
509 create_bindings (OpPager, MENU_PAGER);
510 create_bindings (OpPost, MENU_POST);
511 create_bindings (OpQuery, MENU_QUERY);
512 create_bindings (OpAlias, MENU_ALIAS);
515 create_bindings (OpPgp, MENU_PGP);
516 create_bindings (OpSmime, MENU_SMIME);
518 #ifdef CRYPT_BACKEND_GPGME
519 create_bindings (OpPgp, MENU_KEY_SELECT_PGP);
520 create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);
524 create_bindings (OpMix, MENU_MIX);
526 km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY);
527 km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV);
528 km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);
531 /* bindings for the line editor */
532 create_bindings (OpEditor, MENU_EDITOR);
534 km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP);
535 km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN);
536 km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR);
537 km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR);
538 km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL);
539 km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL);
540 km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
541 km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE);
542 km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE);
544 /* generic menu keymap */
545 create_bindings (OpGeneric, MENU_GENERIC);
547 km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY);
548 km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY);
549 km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE);
550 km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE);
551 km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE);
552 km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE);
553 km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY);
554 km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY);
555 km_bindkey ("1", MENU_GENERIC, OP_JUMP);
556 km_bindkey ("2", MENU_GENERIC, OP_JUMP);
557 km_bindkey ("3", MENU_GENERIC, OP_JUMP);
558 km_bindkey ("4", MENU_GENERIC, OP_JUMP);
559 km_bindkey ("5", MENU_GENERIC, OP_JUMP);
560 km_bindkey ("6", MENU_GENERIC, OP_JUMP);
561 km_bindkey ("7", MENU_GENERIC, OP_JUMP);
562 km_bindkey ("8", MENU_GENERIC, OP_JUMP);
563 km_bindkey ("9", MENU_GENERIC, OP_JUMP);
565 km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY);
567 /* Miscellaneous extra bindings */
569 km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE);
570 km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED);
571 km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED);
572 km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY);
573 km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY);
574 km_bindkey ("x", MENU_MAIN, OP_EXIT);
576 km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
578 km_bindkey ("x", MENU_PAGER, OP_EXIT);
579 km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
580 km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
581 km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
582 km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
583 km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
584 km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED);
585 km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED);
586 km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP);
587 km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM);
588 km_bindkey ("1", MENU_PAGER, OP_JUMP);
589 km_bindkey ("2", MENU_PAGER, OP_JUMP);
590 km_bindkey ("3", MENU_PAGER, OP_JUMP);
591 km_bindkey ("4", MENU_PAGER, OP_JUMP);
592 km_bindkey ("5", MENU_PAGER, OP_JUMP);
593 km_bindkey ("6", MENU_PAGER, OP_JUMP);
594 km_bindkey ("7", MENU_PAGER, OP_JUMP);
595 km_bindkey ("8", MENU_PAGER, OP_JUMP);
596 km_bindkey ("9", MENU_PAGER, OP_JUMP);
598 km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE);
600 km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
601 km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY);
602 km_bindkey ("<space>", MENU_ALIAS, OP_TAG);
604 km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH);
605 km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH);
607 /* edit-to (default "t") hides generic tag-entry in Compose menu
608 This will bind tag-entry to "T" in the Compose menu */
609 km_bindkey ("T", MENU_COMPOSE, OP_TAG);
612 void km_error_key (int menu)
614 char buf[SHORT_STRING];
615 struct keymap_t *key;
617 if (!(key = km_find_func (menu, OP_HELP)))
618 key = km_find_func (MENU_GENERIC, OP_HELP);
620 if (!(km_expand_key (buf, sizeof (buf), key))) {
621 mutt_error _("Key is not bound.");
626 /* make sure the key is really the help key in this menu */
628 if (km_dokey (menu) != OP_HELP) {
629 mutt_error _("Key is not bound.");
634 mutt_error (_("Key is not bound. Press '%s' for help."), buf);
638 int mutt_parse_push (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
643 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
645 m_strcpy(err->data, err->dsize, _("push: too many arguments"));
649 push_string (buf->data);
653 /* expects to see: <menu-string>,<menu-string>,... <key-string> */
654 static char *parse_keymap (int *menu, BUFFER * s, int maxmenus, int *nummenus,
664 mutt_extract_token (&buf, s, 0);
667 while (i < maxmenus) {
672 if ((menu[i] = mutt_getvaluebyname(p, Menus)) == -1) {
673 snprintf (err->data, err->dsize, _("%s: no such menu"), p);
684 mutt_extract_token (&buf, s, 0);
687 m_strcpy(err->data, err->dsize, _("null key sequence"));
689 else if (MoreArgs (s))
693 m_strcpy(err->data, err->dsize, _("too few arguments"));
701 try_bind (char *key, int menu, char *func, struct binding_t *bindings)
705 for (i = 0; bindings[i].name; i++)
706 if (m_strcmp(func, bindings[i].name) == 0) {
707 km_bindkey (key, menu, bindings[i].op);
713 struct binding_t *km_get_table (int menu)
740 #ifdef CRYPT_BACKEND_GPGME
741 case MENU_KEY_SELECT_PGP:
743 case MENU_KEY_SELECT_SMIME:
756 /* bind menu-name '<key_sequence>' function-name */
757 int mutt_parse_bind (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
760 struct binding_t *bindings = NULL;
762 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
765 if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]),
766 &nummenus, err)) == NULL)
769 /* function to execute */
770 mutt_extract_token (buf, s, 0);
772 m_strcpy(err->data, err->dsize, _("bind: too many arguments"));
775 else if (ascii_strcasecmp ("noop", buf->data) == 0) {
776 for (i = 0; i < nummenus; ++i) {
777 km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */
781 for (i = 0; i < nummenus; ++i) {
782 /* First check the "generic" list of commands */
783 if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR ||
784 menu[i] == MENU_GENERIC ||
785 try_bind (key, menu[i], buf->data, OpGeneric) != 0) {
786 /* Now check the menu-specific list of commands (if they exist) */
787 bindings = km_get_table (menu[i]);
788 if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) {
789 snprintf (err->data, err->dsize, _("%s: no such function in map"),
800 /* macro <menu> <key> <macro> <description> */
801 int mutt_parse_macro (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
804 int menu[sizeof (Menus) / sizeof (struct mapping_t) - 1], r =
810 parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus,
814 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
815 /* make sure the macro sequence is not an empty string */
817 m_strcpy(err->data, err->dsize, _("macro: empty key sequence"));
821 seq = m_strdup(buf->data);
822 mutt_extract_token (buf, s, M_TOKEN_CONDENSE);
825 m_strcpy(err->data, err->dsize, _("macro: too many arguments"));
828 for (i = 0; i < nummenus; ++i) {
829 km_bind (key, menu[i], OP_MACRO, seq, buf->data);
837 for (i = 0; i < nummenus; ++i) {
838 km_bind (key, menu[i], OP_MACRO, buf->data, NULL);
847 /* exec function-name */
848 int mutt_parse_exec (BUFFER * buf, BUFFER * s, unsigned long data __attribute__ ((unused)),
853 struct binding_t *bindings = NULL;
857 m_strcpy(err->data, err->dsize, _("exec: no arguments"));
862 mutt_extract_token (buf, s, 0);
863 function = buf->data;
865 if ((bindings = km_get_table (CurrentMenu)) == NULL
866 && CurrentMenu != MENU_PAGER)
867 bindings = OpGeneric;
869 ops[nops] = get_op (bindings, function, m_strlen(function));
870 if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER)
871 ops[nops] = get_op (OpGeneric, function, m_strlen(function));
873 if (ops[nops] == OP_NULL) {
875 mutt_error (_("%s: no such function"), function);
880 while (MoreArgs (s) && nops < ssizeof (ops) / ssizeof (ops[0]));
883 mutt_ungetch (0, ops[--nops]);
889 * prompts the user to enter a keystroke, and displays the octal value back
892 void mutt_what_key (void)
896 mvprintw (LINES - 1, 0, _("Enter keys (^G to abort): "));
899 if (ch != ERR && ch != ctrl ('G')) {
900 mutt_message (_("Char = %s, Octal = %o, Decimal = %d"),
901 km_keyname (ch), ch, ch);
904 while (ch != ERR && ch != ctrl ('G'));