const char *vis_keys_next(Vis *vis, const char *keys) { if (!keys || !*keys) return NULL; TermKeyKey key; TermKey *termkey = vis->ui->termkey_get(vis->ui); const char *next = NULL; /* first try to parse a special key of the form <Key> */ if (*keys == '<' && (next = termkey_strpkey(termkey, keys+1, &key, TERMKEY_FORMAT_VIM)) && *next == '>') return next+1; if (*keys == '<') { const char *start = keys + 1, *end = start; while (*end && *end != '>') end++; if (end > start && end - start - 1 < 64 && *end == '>') { char key[64]; memcpy(key, start, end - start); key[end - start] = '\0'; if (map_get(vis->actions, key)) return end + 1; } } while (!ISUTF8(*keys)) keys++; return termkey_strpkey(termkey, keys, &key, TERMKEY_FORMAT_VIM); }
bool KeyConfig::BindKey(const char *context, const char *action, const char *key) { TermKeyKey tkey; const char *res = termkey_strpkey(COREMANAGER->GetTermKeyHandle(), key, &tkey, TERMKEY_FORMAT_LONGMOD); if (!res || res[0]) return false; binds[context][tkey] = action; return true; }
const char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyFormat format) { struct modnames *mods = &modnames[!!(format & TERMKEY_FORMAT_LONGMOD) + !!(format & TERMKEY_FORMAT_ALTISMETA) * 2 + !!(format & TERMKEY_FORMAT_LOWERMOD) * 4]; key->modifiers = 0; if((format & TERMKEY_FORMAT_CARETCTRL) && str[0] == '^' && str[1]) { str = termkey_strpkey(tk, str+1, key, format & ~TERMKEY_FORMAT_CARETCTRL); if(!str || key->type != TERMKEY_TYPE_UNICODE || key->code.codepoint < '@' || key->code.codepoint > '_' || key->modifiers != 0) return NULL; if(key->code.codepoint >= 'A' && key->code.codepoint <= 'Z') key->code.codepoint += 0x20; key->modifiers = TERMKEY_KEYMOD_CTRL; fill_utf8(key); return (char *)str; } const char *sep_at; while((sep_at = strchr(str, (format & TERMKEY_FORMAT_SPACEMOD) ? ' ' : '-'))) { size_t n = sep_at - 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 = sep_at + 1; } size_t nbytes; ssize_t snbytes; const char *endstr; if((endstr = termkey_lookup_keyname_format(tk, str, &key->code.sym, format))) { key->type = TERMKEY_TYPE_KEYSYM; str = endstr; } else if(sscanf(str, "F%d%zn", &key->code.number, &snbytes) == 1) { key->type = TERMKEY_TYPE_FUNCTION; str += snbytes; } // Unicode must be last else if(parse_utf8((unsigned const char *)str, strlen(str), &key->code.codepoint, &nbytes) == TERMKEY_RES_KEY) { key->type = TERMKEY_TYPE_UNICODE; fill_utf8(key); str += nbytes; } // TODO: Consider mouse events? else return NULL; termkey_canonicalise(tk, key); return (char *)str; }
int main(int argc, char *argv[]) { TermKey *tk; TermKeyKey key; char *endp; #define CLEAR_KEY do { key.type = -1; key.code.codepoint = -1; key.modifiers = -1; key.utf8[0] = 0; } while(0) plan_tests(53); tk = termkey_new_abstract("vt100", 0); CLEAR_KEY; endp = termkey_strpkey(tk, "A", &key, 0); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/A/0"); is_int(key.code.codepoint, 'A', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/A/0"); CLEAR_KEY; endp = termkey_strpkey(tk, "A and more", &key, 0); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/A/0 trailing"); is_int(key.code.codepoint, 'A', "key.code.codepoint for unicode/A/0 trailing"); is_int(key.modifiers, 0, "key.modifiers for unicode/A/0 trailing"); is_str(key.utf8, "A", "key.utf8 for unicode/A/0 trailing"); is_str(endp, " and more", "points at string tail for unicode/A/0 trailing"); CLEAR_KEY; endp = termkey_strpkey(tk, "C-b", &key, 0); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL"); is_int(key.code.codepoint, 'b', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/b/CTRL"); CLEAR_KEY; endp = termkey_strpkey(tk, "Ctrl-b", &key, TERMKEY_FORMAT_LONGMOD); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL longmod"); is_int(key.code.codepoint, 'b', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/b/CTRL longmod"); CLEAR_KEY; endp = termkey_strpkey(tk, "^B", &key, TERMKEY_FORMAT_CARETCTRL); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL caretctrl"); is_int(key.code.codepoint, 'b', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/b/CTRL caretctrl"); CLEAR_KEY; endp = termkey_strpkey(tk, "A-c", &key, 0); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT"); is_int(key.code.codepoint, 'c', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/c/ALT"); CLEAR_KEY; endp = termkey_strpkey(tk, "Alt-c", &key, TERMKEY_FORMAT_LONGMOD); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT longmod"); is_int(key.code.codepoint, 'c', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/c/ALT longmod"); CLEAR_KEY; endp = termkey_strpkey(tk, "M-c", &key, TERMKEY_FORMAT_ALTISMETA); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta"); is_int(key.code.codepoint, 'c', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/c/ALT altismeta"); CLEAR_KEY; endp = termkey_strpkey(tk, "Meta-c", &key, TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_LONGMOD); is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta+longmod"); is_int(key.code.codepoint, 'c', "key.code.codepoint 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"); is_str(endp, "", "consumed entire input for unicode/c/ALT altismeta+longmod"); CLEAR_KEY; endp = termkey_strpkey(tk, "Up", &key, 0); is_int(key.type, TERMKEY_TYPE_KEYSYM, "key.type for sym/Up/0"); is_int(key.code.sym, TERMKEY_SYM_UP, "key.code.codepoint for sym/Up/0"); is_int(key.modifiers, 0, "key.modifiers for sym/Up/0"); is_str(endp, "", "consumed entire input for sym/Up/0"); CLEAR_KEY; endp = termkey_strpkey(tk, "F5", &key, 0); is_int(key.type, TERMKEY_TYPE_FUNCTION, "key.type for func/5/0"); is_int(key.code.number, 5, "key.code.number for func/5/0"); is_int(key.modifiers, 0, "key.modifiers for func/5/0"); is_str(endp, "", "consumed entire input for func/5/0"); termkey_destroy(tk); return exit_status(); }