From 6424282c4ddb129d6987a0f8c03f3d23132b842c Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Fri, 1 Apr 2011 00:50:51 +0100 Subject: Partial implementation of termkey_strpkey(3); missing FORMAT_WRAPBRACKET and TYPE_FUNCTION support --- t/12strpkey.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ termkey.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++- termkey.h.in | 3 +- 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 t/12strpkey.c diff --git a/t/12strpkey.c b/t/12strpkey.c new file mode 100644 index 0000000..648e1c1 --- /dev/null +++ b/t/12strpkey.c @@ -0,0 +1,90 @@ +#include "../termkey.h" +#include "taplib.h" + +int main(int argc, char *argv[]) +{ + TermKey *tk; + TermKeyKey key; + TermKeyResult res; + +#define CLEAR_KEY do { key.type = -1; key.code.number = -1; key.modifiers = -1; key.utf8[0] = 0; } while(0) + + plan_tests(44); + + tk = termkey_new(0, TERMKEY_FLAG_NOTERMIOS); + + CLEAR_KEY; + res = termkey_strpkey(tk, "A", &key, 0); + is_int(res, TERMKEY_RES_KEY, "result for unicode/A/0"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/A/0"); + is_int(key.code.number, 'A', "key.code.number for unicode/A/0"); + is_int(key.modifiers, 0, "key.modifiers for unicode/A/0"); + is_str(key.utf8, "A", "key.utf8 for unicode/A/0"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "C-b", &key, 0); + is_int(res, TERMKEY_RES_KEY, "result for unicode/b/CTRL"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL"); + is_int(key.code.number, 'b', "key.code.number for unicode/b/CTRL"); + is_int(key.modifiers, TERMKEY_KEYMOD_CTRL, "key.modifiers for unicode/b/CTRL"); + is_str(key.utf8, "b", "key.utf8 for unicode/b/CTRL"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "Ctrl-b", &key, TERMKEY_FORMAT_LONGMOD); + is_int(res, TERMKEY_RES_KEY, "result for unicode/b/CTRL longmod"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL longmod"); + is_int(key.code.number, 'b', "key.code.number for unicode/b/CTRL longmod"); + is_int(key.modifiers, TERMKEY_KEYMOD_CTRL, "key.modifiers for unicode/b/CTRL longmod"); + is_str(key.utf8, "b", "key.utf8 for unicode/b/CTRL longmod"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "^B", &key, TERMKEY_FORMAT_CARETCTRL); + is_int(res, TERMKEY_RES_KEY, "result for unicode/b/CTRL caretctrl"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL caretctrl"); + is_int(key.code.number, 'b', "key.code.number for unicode/b/CTRL caretctrl"); + is_int(key.modifiers, TERMKEY_KEYMOD_CTRL, "key.modifiers for unicode/b/CTRL caretctrl"); + is_str(key.utf8, "b", "key.utf8 for unicode/b/CTRL caretctrl"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "A-c", &key, 0); + is_int(res, TERMKEY_RES_KEY, "result for unicode/c/ALT"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT"); + is_int(key.code.number, 'c', "key.code.number for unicode/c/ALT"); + is_int(key.modifiers, TERMKEY_KEYMOD_ALT, "key.modifiers for unicode/c/ALT"); + is_str(key.utf8, "c", "key.utf8 for unicode/c/ALT"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "Alt-c", &key, TERMKEY_FORMAT_LONGMOD); + is_int(res, TERMKEY_RES_KEY, "result for unicode/c/ALT longmod"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT longmod"); + is_int(key.code.number, 'c', "key.code.number for unicode/c/ALT longmod"); + is_int(key.modifiers, TERMKEY_KEYMOD_ALT, "key.modifiers for unicode/c/ALT longmod"); + is_str(key.utf8, "c", "key.utf8 for unicode/c/ALT longmod"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "M-c", &key, TERMKEY_FORMAT_ALTISMETA); + is_int(res, TERMKEY_RES_KEY, "result for unicode/c/ALT altismeta"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta"); + is_int(key.code.number, 'c', "key.code.number for unicode/c/ALT altismeta"); + is_int(key.modifiers, TERMKEY_KEYMOD_ALT, "key.modifiers for unicode/c/ALT altismeta"); + is_str(key.utf8, "c", "key.utf8 for unicode/c/ALT altismeta"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "Meta-c", &key, TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_LONGMOD); + is_int(res, TERMKEY_RES_KEY, "result for unicode/c/ALT altismeta+longmod"); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta+longmod"); + is_int(key.code.number, 'c', "key.code.number for unicode/c/ALT altismeta+longmod"); + is_int(key.modifiers, TERMKEY_KEYMOD_ALT, "key.modifiers for unicode/c/ALT altismeta+longmod"); + is_str(key.utf8, "c", "key.utf8 for unicode/c/ALT altismeta+longmod"); + + CLEAR_KEY; + res = termkey_strpkey(tk, "Up", &key, 0); + is_int(res, TERMKEY_RES_KEY, "result for sym/Up/0"); + is_int(key.type, TERMKEY_TYPE_KEYSYM, "key.type for sym/Up/0"); + is_int(key.code.sym, TERMKEY_SYM_UP, "key.code.number for sym/Up/0"); + is_int(key.modifiers, 0, "key.modifiers for sym/Up/0"); + + termkey_destroy(tk); + + return exit_status(); +} diff --git a/termkey.c b/termkey.c index 0258c86..b1a01ce 100644 --- a/termkey.c +++ b/termkey.c @@ -429,7 +429,13 @@ static TermKeyResult parse_utf8(const unsigned char *bytes, size_t len, long *cp unsigned char b0 = bytes[0]; - if(b0 < 0xc0) { + if(b0 < 0x80) { + // Single byte ASCII + *cp = b0; + *nbytep = 1; + return TERMKEY_RES_KEY; + } + else if(b0 < 0xc0) { // Starts with a continuation byte - that's not right *cp = UTF8_INVALID; *nbytep = 1; @@ -1061,3 +1067,67 @@ size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, T return pos; } + +TermKeyResult termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyFormat format) +{ + struct modnames *mods = &modnames[!!(format & TERMKEY_FORMAT_LONGMOD) + + !!(format & TERMKEY_FORMAT_ALTISMETA) * 2]; + + key->modifiers = 0; + + if((format & TERMKEY_FORMAT_CARETCTRL) && str[0] == '^' && str[1]) { + TermKeyResult res = termkey_strpkey(tk, str+1, key, format & ~TERMKEY_FORMAT_CARETCTRL); + + if(res != TERMKEY_RES_KEY) + return res; + if(key->type != TERMKEY_TYPE_UNICODE) + return TERMKEY_RES_NONE; + if(key->code.number < '@' || key->code.number > '_') + return TERMKEY_RES_NONE; + if(key->modifiers != 0) + return TERMKEY_RES_NONE; + + if(key->code.number >= 'A' && key->code.number <= 'Z') + key->code.number += 0x20; + key->modifiers = TERMKEY_KEYMOD_CTRL; + fill_utf8(key); + return res; + } + + const char *hyphen; + + while((hyphen = strchr(str, '-'))) { + size_t n = hyphen - str; + + if(n == strlen(mods->alt) && strncmp(mods->alt, str, n) == 0) + key->modifiers |= TERMKEY_KEYMOD_ALT; + else if(n == strlen(mods->ctrl) && strncmp(mods->ctrl, str, n) == 0) + key->modifiers |= TERMKEY_KEYMOD_CTRL; + else if(n == strlen(mods->shift) && strncmp(mods->shift, str, n) == 0) + key->modifiers |= TERMKEY_KEYMOD_SHIFT; + + else + break; + + str = hyphen + 1; + } + + long codepoint; + size_t nbytes; + + if(parse_utf8((unsigned char *)str, strlen(str), &codepoint, &nbytes) == TERMKEY_RES_KEY && + nbytes == strlen(str)) { + key->type = TERMKEY_TYPE_UNICODE; + key->code.number = codepoint; + fill_utf8(key); + } + else if((key->code.sym = termkey_keyname2sym(tk, str)) != TERMKEY_SYM_UNKNOWN) { + key->type = TERMKEY_TYPE_KEYSYM; + } + // TODO: Consider function keys + else { + return TERMKEY_RES_NONE; + } + + return TERMKEY_RES_KEY; +} diff --git a/termkey.h.in b/termkey.h.in index 5405036..3c0665f 100644 --- a/termkey.h.in +++ b/termkey.h.in @@ -189,7 +189,8 @@ typedef enum { #define TERMKEY_FORMAT_VIM (TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_WRAPBRACKET) -size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format); +size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format); +TermKeyResult termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyFormat format); // Old name for termkey_strfkey() size_t termkey_snprint_key(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format); -- cgit v1.2.3-70-g09d2