Simplify xterm modifier detection by treating as a bitmask + 1. Spotted

by and diff from Emanuele Giaquinta.
pull/1/head
Nicholas Marriott 2010-09-01 21:11:14 +00:00
parent 5309252053
commit de68c2a7da
1 changed files with 19 additions and 36 deletions

View File

@ -114,27 +114,21 @@ int
xterm_keys_modifiers(const char *template, const char *buf, size_t len) xterm_keys_modifiers(const char *template, const char *buf, size_t len)
{ {
size_t idx; size_t idx;
int param, modifiers;
idx = strcspn(template, "_"); idx = strcspn(template, "_");
if (idx >= len) if (idx >= len)
return (0); return (0);
switch (buf[idx]) { param = buf[idx] - '1';
case '2':
return (KEYC_SHIFT); modifiers = 0;
case '3': if (param & 1)
return (KEYC_ESCAPE); modifiers |= KEYC_SHIFT;
case '4': if (param & 2)
return (KEYC_SHIFT|KEYC_ESCAPE); modifiers |= KEYC_ESCAPE;
case '5': if (param & 4)
return (KEYC_CTRL); modifiers |= KEYC_CTRL;
case '6': return (modifiers);
return (KEYC_SHIFT|KEYC_CTRL);
case '7':
return (KEYC_ESCAPE|KEYC_CTRL);
case '8':
return (KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL);
}
return (0);
} }
/* /*
@ -171,30 +165,19 @@ xterm_keys_lookup(int key)
int modifiers; int modifiers;
char *out; char *out;
#define KEY_MODIFIERS(key, modifiers) \ modifiers = 1;
(((key) & (KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL)) == (modifiers)) if (key & KEYC_SHIFT)
modifiers = 0; modifiers += 1;
if (KEY_MODIFIERS(key, KEYC_SHIFT)) if (key & KEYC_ESCAPE)
modifiers = 2; modifiers += 2;
else if (KEY_MODIFIERS(key, KEYC_ESCAPE)) if (key & KEYC_CTRL)
modifiers = 3; modifiers += 4;
else if (KEY_MODIFIERS(key, KEYC_SHIFT|KEYC_ESCAPE))
modifiers = 4;
else if (KEY_MODIFIERS(key, KEYC_CTRL))
modifiers = 5;
else if (KEY_MODIFIERS(key, KEYC_SHIFT|KEYC_CTRL))
modifiers = 6;
else if (KEY_MODIFIERS(key, KEYC_ESCAPE|KEYC_CTRL))
modifiers = 7;
else if (KEY_MODIFIERS(key, KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL))
modifiers = 8;
#undef KEY_MODIFIERS
/* /*
* If the key has no modifiers, return NULL and let it fall through to * If the key has no modifiers, return NULL and let it fall through to
* the normal lookup. * the normal lookup.
*/ */
if (modifiers == 0) if (modifiers == 1)
return (NULL); return (NULL);
/* Otherwise, find the key in the table. */ /* Otherwise, find the key in the table. */