1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/* Key encoding and decoding functions
Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Zile.
GNU Zile 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 3, or (at your option)
any later version.
GNU Zile 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 GNU Zile; see the file COPYING. If not, write to the
Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
MA 02111-1301, USA. */
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gl_array_list.h"
#include "main.h"
#include "extern.h"
/*
* Convert a key chord into its ASCII representation
*/
astr
chordtostr (size_t key)
{
astr as = astr_new ();
if (key & KBD_CTRL)
astr_cat_cstr (as, "C-");
if (key & KBD_META)
astr_cat_cstr (as, "M-");
key &= ~(KBD_CTRL | KBD_META);
switch (key)
{
case KBD_PGUP:
astr_cat_cstr (as, "<prior>");
break;
case KBD_PGDN:
astr_cat_cstr (as, "<next>");
break;
case KBD_HOME:
astr_cat_cstr (as, "<home>");
break;
case KBD_END:
astr_cat_cstr (as, "<end>");
break;
case KBD_DEL:
astr_cat_cstr (as, "<delete>");
break;
case KBD_BS:
astr_cat_cstr (as, "<backspace>");
break;
case KBD_INS:
astr_cat_cstr (as, "<insert>");
break;
case KBD_LEFT:
astr_cat_cstr (as, "<left>");
break;
case KBD_RIGHT:
astr_cat_cstr (as, "<right>");
break;
case KBD_UP:
astr_cat_cstr (as, "<up>");
break;
case KBD_DOWN:
astr_cat_cstr (as, "<down>");
break;
case KBD_RET:
astr_cat_cstr (as, "<RET>");
break;
case KBD_TAB:
astr_cat_cstr (as, "<TAB>");
break;
case KBD_F1:
astr_cat_cstr (as, "<f1>");
break;
case KBD_F2:
astr_cat_cstr (as, "<f2>");
break;
case KBD_F3:
astr_cat_cstr (as, "<f3>");
break;
case KBD_F4:
astr_cat_cstr (as, "<f4>");
break;
case KBD_F5:
astr_cat_cstr (as, "<f5>");
break;
case KBD_F6:
astr_cat_cstr (as, "<f6>");
break;
case KBD_F7:
astr_cat_cstr (as, "<f7>");
break;
case KBD_F8:
astr_cat_cstr (as, "<f8>");
break;
case KBD_F9:
astr_cat_cstr (as, "<f9>");
break;
case KBD_F10:
astr_cat_cstr (as, "<f10>");
break;
case KBD_F11:
astr_cat_cstr (as, "<f11>");
break;
case KBD_F12:
astr_cat_cstr (as, "<f12>");
break;
case ' ':
astr_cat_cstr (as, "SPC");
break;
default:
if (key <= 0xff && isgraph (key))
astr_cat_char (as, (int) key);
else
astr_afmt (as, "<%x>", key);
}
return as;
}
/*
* Array of key names
*/
static const char *keyname[] = {
"\\BACKSPACE",
"\\C-",
"\\DELETE",
"\\DOWN",
"\\END",
"\\F1",
"\\F10",
"\\F11",
"\\F12",
"\\F2",
"\\F3",
"\\F4",
"\\F5",
"\\F6",
"\\F7",
"\\F8",
"\\F9",
"\\HOME",
"\\INSERT",
"\\LEFT",
"\\M-",
"\\NEXT",
"\\PAGEDOWN",
"\\PAGEUP",
"\\PRIOR",
"\\r", /* FIXME: Kludge to make keystrings work in both Emacs and Zile. */
"\\RET",
"\\RIGHT",
"\\SPC",
"\\TAB",
"\\UP",
"\\\\",
};
/*
* Array of key codes in the same order as keyname above
*/
static int keycode[] = {
KBD_BS,
KBD_CTRL,
KBD_DEL,
KBD_DOWN,
KBD_END,
KBD_F1,
KBD_F10,
KBD_F11,
KBD_F12,
KBD_F2,
KBD_F3,
KBD_F4,
KBD_F5,
KBD_F6,
KBD_F7,
KBD_F8,
KBD_F9,
KBD_HOME,
KBD_INS,
KBD_LEFT,
KBD_META,
KBD_PGDN,
KBD_PGDN,
KBD_PGUP,
KBD_PGUP,
KBD_RET,
KBD_RET,
KBD_RIGHT,
' ',
KBD_TAB,
KBD_UP,
'\\',
};
/*
* Convert a key string to its key code.
*/
static size_t
strtokey (const char *buf, size_t * len)
{
if (*buf == '\\')
{
size_t i;
char **p = NULL;
for (i = 0; i < sizeof (keyname) / sizeof (keyname[0]); i++)
if (strncmp (keyname[i], buf, strlen (keyname[i])) == 0)
p = (char **) &keyname[i];
if (p == NULL)
{
*len = 0;
return KBD_NOKEY;
}
else
{
*len = strlen (*p);
return keycode[p - (char **) keyname];
}
}
else
{
*len = 1;
return (size_t) * (unsigned char *) buf;
}
}
/*
* Convert a key chord string to its key code.
*/
static size_t
strtochord (const char *buf, size_t * len)
{
size_t key = 0, k;
*len = 0;
do
{
size_t l;
k = strtokey (buf + *len, &l);
if (k == KBD_NOKEY)
{
*len = 0;
return KBD_NOKEY;
}
*len += l;
key |= k;
}
while (k == KBD_CTRL || k == KBD_META);
return key;
}
/*
* Convert a key sequence string into a key code sequence, or NULL if
* it can't be converted.
*/
gl_list_t
keystrtovec (const char *key)
{
gl_list_t keys = gl_list_create_empty (GL_ARRAY_LIST,
NULL, NULL, NULL, true);
while (*key != '\0')
{
size_t len, code = strtochord (key, &len);
if (code == KBD_NOKEY)
{
gl_list_free (keys);
return NULL;
}
gl_list_add_last (keys, (void *) code);
key += len;
}
return keys;
}
/*
* Convert a key code sequence into a key code sequence string.
*/
astr
keyvectostr (gl_list_t keys)
{
size_t i;
astr as = astr_new ();
for (i = 0; i < gl_list_size (keys); i++)
{
astr key = chordtostr ((size_t) gl_list_get_at (keys, i));
astr_cat (as, key);
astr_delete (key);
if (i < gl_list_size (keys) - 1)
astr_cat_char (as, ' ');
}
return as;
}
|