📄 keymap.c
字号:
char *p; if ((p = mutt_getnamebyvalue (c, KeyNames))) return p; if (c < 256 && c > -128 && iscntrl ((unsigned char) c)) { if (c < 0) c += 256; if (c < 128) { buf[0] = '^'; buf[1] = (c + '@') & 0x7f; buf[2] = 0; } else snprintf (buf, sizeof (buf), "\\%d%d%d", c >> 6, (c >> 3) & 7, c & 7); } else if (c >= KEY_F0 && c < KEY_F(256)) /* this maximum is just a guess */ sprintf (buf, "<F%d>", c - KEY_F0); else if (IsPrint (c)) snprintf (buf, sizeof (buf), "%c", (unsigned char) c); else snprintf (buf, sizeof (buf), "\\x%hx", (unsigned short) c); return (buf);}int km_expand_key (char *s, size_t len, struct keymap_t *map){ size_t l; int p = 0; if (!map) return (0); FOREVER { strfcpy (s, km_keyname (map->keys[p]), len); len -= (l = mutt_strlen (s)); if (++p >= map->len || !len) return (1); s += l; } /* not reached */}struct keymap_t *km_find_func (int menu, int func){ struct keymap_t *map = Keymaps[menu]; for (; map; map = map->next) if (map->op == func) break; return (map);}void km_init (void){ memset (Keymaps, 0, sizeof (struct keymap_t *) * MENU_MAX); create_bindings (OpAttach, MENU_ATTACH); create_bindings (OpBrowser, MENU_FOLDER); create_bindings (OpCompose, MENU_COMPOSE); create_bindings (OpMain, MENU_MAIN); create_bindings (OpPager, MENU_PAGER); create_bindings (OpPost, MENU_POST); create_bindings (OpQuery, MENU_QUERY); create_bindings (OpAlias, MENU_ALIAS); if ((WithCrypto & APPLICATION_PGP)) create_bindings (OpPgp, MENU_PGP); if ((WithCrypto & APPLICATION_SMIME)) create_bindings (OpSmime, MENU_SMIME);#ifdef CRYPT_BACKEND_GPGME create_bindings (OpPgp, MENU_KEY_SELECT_PGP); create_bindings (OpSmime, MENU_KEY_SELECT_SMIME);#endif#ifdef MIXMASTER create_bindings (OpMix, MENU_MIX); km_bindkey ("<space>", MENU_MIX, OP_GENERIC_SELECT_ENTRY); km_bindkey ("h", MENU_MIX, OP_MIX_CHAIN_PREV); km_bindkey ("l", MENU_MIX, OP_MIX_CHAIN_NEXT);#endif /* bindings for the line editor */ create_bindings (OpEditor, MENU_EDITOR); km_bindkey ("<up>", MENU_EDITOR, OP_EDITOR_HISTORY_UP); km_bindkey ("<down>", MENU_EDITOR, OP_EDITOR_HISTORY_DOWN); km_bindkey ("<left>", MENU_EDITOR, OP_EDITOR_BACKWARD_CHAR); km_bindkey ("<right>", MENU_EDITOR, OP_EDITOR_FORWARD_CHAR); km_bindkey ("<home>", MENU_EDITOR, OP_EDITOR_BOL); km_bindkey ("<end>", MENU_EDITOR, OP_EDITOR_EOL); km_bindkey ("<backspace>", MENU_EDITOR, OP_EDITOR_BACKSPACE); km_bindkey ("<delete>", MENU_EDITOR, OP_EDITOR_BACKSPACE); km_bindkey ("\177", MENU_EDITOR, OP_EDITOR_BACKSPACE); /* generic menu keymap */ create_bindings (OpGeneric, MENU_GENERIC); km_bindkey ("<home>", MENU_GENERIC, OP_FIRST_ENTRY); km_bindkey ("<end>", MENU_GENERIC, OP_LAST_ENTRY); km_bindkey ("<pagedown>", MENU_GENERIC, OP_NEXT_PAGE); km_bindkey ("<pageup>", MENU_GENERIC, OP_PREV_PAGE); km_bindkey ("<right>", MENU_GENERIC, OP_NEXT_PAGE); km_bindkey ("<left>", MENU_GENERIC, OP_PREV_PAGE); km_bindkey ("<up>", MENU_GENERIC, OP_PREV_ENTRY); km_bindkey ("<down>", MENU_GENERIC, OP_NEXT_ENTRY); km_bindkey ("1", MENU_GENERIC, OP_JUMP); km_bindkey ("2", MENU_GENERIC, OP_JUMP); km_bindkey ("3", MENU_GENERIC, OP_JUMP); km_bindkey ("4", MENU_GENERIC, OP_JUMP); km_bindkey ("5", MENU_GENERIC, OP_JUMP); km_bindkey ("6", MENU_GENERIC, OP_JUMP); km_bindkey ("7", MENU_GENERIC, OP_JUMP); km_bindkey ("8", MENU_GENERIC, OP_JUMP); km_bindkey ("9", MENU_GENERIC, OP_JUMP); km_bindkey ("<enter>", MENU_GENERIC, OP_GENERIC_SELECT_ENTRY); /* Miscellaneous extra bindings */ km_bindkey (" ", MENU_MAIN, OP_DISPLAY_MESSAGE); km_bindkey ("<up>", MENU_MAIN, OP_MAIN_PREV_UNDELETED); km_bindkey ("<down>", MENU_MAIN, OP_MAIN_NEXT_UNDELETED); km_bindkey ("J", MENU_MAIN, OP_NEXT_ENTRY); km_bindkey ("K", MENU_MAIN, OP_PREV_ENTRY); km_bindkey ("x", MENU_MAIN, OP_EXIT); km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE); km_bindkey ("x", MENU_PAGER, OP_EXIT); km_bindkey ("i", MENU_PAGER, OP_EXIT); km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE); km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE); km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE); km_bindkey ("<up>", MENU_PAGER, OP_MAIN_PREV_UNDELETED); km_bindkey ("<right>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED); km_bindkey ("<down>", MENU_PAGER, OP_MAIN_NEXT_UNDELETED); km_bindkey ("<left>", MENU_PAGER, OP_MAIN_PREV_UNDELETED); km_bindkey ("<home>", MENU_PAGER, OP_PAGER_TOP); km_bindkey ("<end>", MENU_PAGER, OP_PAGER_BOTTOM); km_bindkey ("1", MENU_PAGER, OP_JUMP); km_bindkey ("2", MENU_PAGER, OP_JUMP); km_bindkey ("3", MENU_PAGER, OP_JUMP); km_bindkey ("4", MENU_PAGER, OP_JUMP); km_bindkey ("5", MENU_PAGER, OP_JUMP); km_bindkey ("6", MENU_PAGER, OP_JUMP); km_bindkey ("7", MENU_PAGER, OP_JUMP); km_bindkey ("8", MENU_PAGER, OP_JUMP); km_bindkey ("9", MENU_PAGER, OP_JUMP); km_bindkey ("<enter>", MENU_PAGER, OP_NEXT_LINE); km_bindkey ("<return>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY); km_bindkey ("<enter>", MENU_ALIAS, OP_GENERIC_SELECT_ENTRY); km_bindkey ("<space>", MENU_ALIAS, OP_TAG); km_bindkey ("<enter>", MENU_ATTACH, OP_VIEW_ATTACH); km_bindkey ("<enter>", MENU_COMPOSE, OP_VIEW_ATTACH); /* edit-to (default "t") hides generic tag-entry in Compose menu This will bind tag-entry to "T" in the Compose menu */ km_bindkey ("T", MENU_COMPOSE, OP_TAG);}void km_error_key (int menu){ char buf[SHORT_STRING]; struct keymap_t *key; if(!(key = km_find_func(menu, OP_HELP))) key = km_find_func(MENU_GENERIC, OP_HELP); if(!(km_expand_key(buf, sizeof(buf), key))) { mutt_error _("Key is not bound."); return; } /* make sure the key is really the help key in this menu */ push_string (buf); if (km_dokey (menu) != OP_HELP) { mutt_error _("Key is not bound."); return; } mutt_error (_("Key is not bound. Press '%s' for help."), buf); return;}int mutt_parse_push (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err){ int r = 0; mutt_extract_token (buf, s, M_TOKEN_CONDENSE); if (MoreArgs (s)) { strfcpy (err->data, _("push: too many arguments"), err->dsize); r = -1; } else push_string (buf->data); return (r);}/* expects to see: <menu-string>,<menu-string>,... <key-string> */static char *parse_keymap (int *menu, BUFFER *s, int maxmenus, int *nummenus, BUFFER *err){ BUFFER buf; int i=0; char *p, *q; memset (&buf, 0, sizeof (buf)); /* menu name */ mutt_extract_token (&buf, s, 0); p = buf.data; if (MoreArgs (s)) { while (i < maxmenus) { q = strchr(p,','); if (q) *q = '\0'; if ((menu[i] = mutt_check_menu (p)) == -1) { snprintf (err->data, err->dsize, _("%s: no such menu"), p); goto error; } ++i; if (q) p = q+1; else break; } *nummenus=i; /* key sequence */ mutt_extract_token (&buf, s, 0); if (!*buf.data) { strfcpy (err->data, _("null key sequence"), err->dsize); } else if (MoreArgs (s)) return (buf.data); } else { strfcpy (err->data, _("too few arguments"), err->dsize); }error: FREE (&buf.data); return (NULL);}static inttry_bind (char *key, int menu, char *func, struct binding_t *bindings){ int i; for (i = 0; bindings[i].name; i++) if (mutt_strcmp (func, bindings[i].name) == 0) { km_bindkey (key, menu, bindings[i].op); return (0); } return (-1);}struct binding_t *km_get_table (int menu){ switch (menu) { case MENU_MAIN: return OpMain; case MENU_GENERIC: return OpGeneric; case MENU_COMPOSE: return OpCompose; case MENU_PAGER: return OpPager; case MENU_POST: return OpPost; case MENU_FOLDER: return OpBrowser; case MENU_ALIAS: return OpAlias; case MENU_ATTACH: return OpAttach; case MENU_EDITOR: return OpEditor; case MENU_QUERY: return OpQuery; case MENU_PGP: return (WithCrypto & APPLICATION_PGP)? OpPgp:NULL;#ifdef CRYPT_BACKEND_GPGME case MENU_KEY_SELECT_PGP: return OpPgp; case MENU_KEY_SELECT_SMIME: return OpSmime;#endif#ifdef MIXMASTER case MENU_MIX: return OpMix;#endif } return NULL;}/* bind menu-name '<key_sequence>' function-name */int mutt_parse_bind (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err){ struct binding_t *bindings = NULL; char *key; int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = 0, nummenus, i; if ((key = parse_keymap (menu, s, sizeof (menu)/sizeof (menu[0]), &nummenus, err)) == NULL) return (-1); /* function to execute */ mutt_extract_token (buf, s, 0); if (MoreArgs (s)) { strfcpy (err->data, _("bind: too many arguments"), err->dsize); r = -1; } else if (ascii_strcasecmp ("noop", buf->data) == 0) { for (i = 0; i < nummenus; ++i) { km_bindkey (key, menu[i], OP_NULL); /* the `unbind' command */ } } else { for (i = 0; i < nummenus; ++i) { /* First check the "generic" list of commands */ if (menu[i] == MENU_PAGER || menu[i] == MENU_EDITOR || menu[i] == MENU_GENERIC || try_bind (key, menu[i], buf->data, OpGeneric) != 0) { /* Now check the menu-specific list of commands (if they exist) */ bindings = km_get_table (menu[i]); if (bindings && try_bind (key, menu[i], buf->data, bindings) != 0) { snprintf (err->data, err->dsize, _("%s: no such function in map"), buf->data); r = -1; } } } } FREE (&key); return (r);}/* macro <menu> <key> <macro> <description> */int mutt_parse_macro (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err){ int menu[sizeof(Menus)/sizeof(struct mapping_t)-1], r = -1, nummenus, i; char *seq = NULL; char *key; if ((key = parse_keymap (menu, s, sizeof (menu) / sizeof (menu[0]), &nummenus, err)) == NULL) return (-1); mutt_extract_token (buf, s, M_TOKEN_CONDENSE); /* make sure the macro sequence is not an empty string */ if (!*buf->data) { strfcpy (err->data, _("macro: empty key sequence"), err->dsize); } else { if (MoreArgs (s)) { seq = safe_strdup (buf->data); mutt_extract_token (buf, s, M_TOKEN_CONDENSE); if (MoreArgs (s)) { strfcpy (err->data, _("macro: too many arguments"), err->dsize); } else { for (i = 0; i < nummenus; ++i) { km_bind (key, menu[i], OP_MACRO, seq, buf->data); r = 0; } } FREE (&seq); } else { for (i = 0; i < nummenus; ++i) { km_bind (key, menu[i], OP_MACRO, buf->data, NULL); r = 0; } } } FREE (&key); return (r);}/* exec function-name */int mutt_parse_exec (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err){ int ops[128]; int nops = 0; struct binding_t *bindings = NULL; char *function; if (!MoreArgs (s)) { strfcpy (err->data, _("exec: no arguments"), err->dsize); return (-1); } do { mutt_extract_token (buf, s, 0); function = buf->data; if ((bindings = km_get_table (CurrentMenu)) == NULL && CurrentMenu != MENU_PAGER) bindings = OpGeneric; ops[nops] = get_op (bindings, function, mutt_strlen(function)); if (ops[nops] == OP_NULL && CurrentMenu != MENU_PAGER) ops[nops] = get_op (OpGeneric, function, mutt_strlen(function)); if (ops[nops] == OP_NULL) { mutt_flushinp (); mutt_error (_("%s: no such function"), function); return (-1); } nops++; } while(MoreArgs(s) && nops < sizeof(ops)/sizeof(ops[0])); while(nops) mutt_ungetch(0, ops[--nops]); return 0;}/* * prompts the user to enter a keystroke, and displays the octal value back * to the user. */void mutt_what_key (void){ int ch; mvprintw(LINES-1,0, _("Enter keys (^G to abort): ")); do { ch = getch(); if (ch != ERR && ch != ctrl ('G')) { mutt_message(_("Char = %s, Octal = %o, Decimal = %d"), km_keyname(ch), ch, ch); } } while (ch != ERR && ch != ctrl ('G')); mutt_flushinp();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -