/*
* Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*/
#include "mutt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "keymap.h"
#include "mapping.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "functions.h"
struct mapping_t Menus[] = {
{ "alias", MENU_ALIAS },
{ "attach", MENU_ATTACH },
{ "browser", MENU_FOLDER },
{ "compose", MENU_COMPOSE },
{ "editor", MENU_EDITOR },
{ "index", MENU_MAIN },
{ "pager", MENU_PAGER },
{ "postpone", MENU_POST },
#ifdef HAVE_PGP
{ "pgp", MENU_PGP },
#endif
#ifdef MIXMASTER
{ "mix", MENU_MIX },
#endif
{ "query", MENU_QUERY },
{ "generic", MENU_GENERIC },
{ NULL, 0 }
};
#define mutt_check_menu(s) mutt_getvaluebyname(s, Menus)
static struct mapping_t KeyNames[] = {
{ "<PageUp>", KEY_PPAGE },
{ "<PageDown>", KEY_NPAGE },
{ "<Up>", KEY_UP },
{ "<Down>", KEY_DOWN },
{ "<Right>", KEY_RIGHT },
{ "<Left>", KEY_LEFT },
{ "<Delete>", KEY_DC },
{ "<BackSpace>",KEY_BACKSPACE },
{ "<Insert>", KEY_IC },
{ "<Home>", KEY_HOME },
{ "<End>", KEY_END },
#ifdef KEY_ENTER
{ "<Enter>", KEY_ENTER },
#endif
{ "<Return>", M_ENTER_C },
{ "<Esc>", '\033' },
{ "<Tab>", '\t' },
{ "<Space>", ' ' },
{ NULL, 0 }
};
/* contains the last key the user pressed */
int LastKey;
struct keymap_t *Keymaps[MENU_MAX];
static struct keymap_t *allocKeys (int len, keycode_t *keys)
{
struct keymap_t *p;
p = safe_calloc (1, sizeof (struct keymap_t));
p->len = len;
p->keys = safe_malloc (len * sizeof (keycode_t));
memcpy (p->keys, keys, len * sizeof (keycode_t));
return (p);
}
static int parse_fkey(char *s)
{
char *t;
int n = 0;
if(s[0] != '<' || tolower(s[1]) != 'f')
return -1;
for(t = s + 2; *t && isdigit((unsigned char) *t); t++)
{
n *= 10;
n += *t - '0';
}
if(*t != '>')
return -1;
else
return n;
}
static int parsekeys (char *str, keycode_t *d, int max)
{
int n, len = max;
char buff[SHORT_STRING];
char c;
char *s, *t;
strfcpy(buff, str, sizeof(buff));
s = buff;
while (*s && len)
{
*d = '\0';
if(*s == '<' && (t = strchr(s, '>')))
{
t++; c = *t; *t = '\0';
if ((n = mutt_getvaluebyname (s, KeyNames)) != -1)
{
s = t;
*d = n;
}
else if ((n = parse_fkey(s)) > 0)
{
s = t;
*d = KEY_F (n);
}
*t = c;
}
if(!*d)
{
*d = *s;
s++;
}
d++;
len--;
}
return (max - len);
}
/* insert a key sequence into the specified map. the map is sorted by ASCII
* value (lowest to highest)
*/
void km_bind (char *s, int menu, int op, char *macro, char *descr)
{
struct keymap_t *map, *tmp, *last = NULL, *next;
keycode_t buf[MAX_SEQ];
int len, pos = 0, lastpos = 0;
len = parsekeys (s, buf