diff --git a/man/termkey_strfkey.3 b/man/termkey_strfkey.3 index 2bbf2ec..60c5f3d 100644 --- a/man/termkey_strfkey.3 +++ b/man/termkey_strfkey.3 @@ -27,6 +27,9 @@ Use the name "\f(CWMeta\fP" or the letter "\f(CWM\fP" instead of "\f(CWAlt\fP" o .B TERMKEY_FORMAT_WRAPBRACKET If the key event is a special key instead of unmodified Unicode, wrap it in "\f(CW\fP". .TP +.B TERMKEY_FORMAT_SPACEMOD +Use spaces instead of hyphens to separate the modifier name(s) from the base key name. +.TP .B TERMKEY_FORMAT_MOUSE_POS If the event is a mouse event, include the position rendered as "\f(CW@ (col,line)\fP". .PP diff --git a/man/termkey_strpkey.3 b/man/termkey_strpkey.3 index 4e4f5eb..6c7d0b0 100644 --- a/man/termkey_strpkey.3 +++ b/man/termkey_strpkey.3 @@ -23,6 +23,9 @@ If the only modifier is \fBTERMKEY_MOD_CTRL\fP on a plain letter, accept it as " .TP .B TERMKEY_FORMAT_ALTISMETA Use the name "\f(CWMeta\fP" or the letter "\f(CWM\fP" instead of "\f(CWAlt\fP" or "\f(CWA\fP". +.TP +.B TERMKEY_FORMAT_SPACEMOD +Expect spaces instead of hyphens to separate the modifer name(s) from the base key name. .PP Before returning, this function canonicalises the \fIkey\fP structure according to the rules given for \fBtermkey_canonicalise\fP(3). .PP diff --git a/t/11strfkey.c b/t/11strfkey.c index bea3a22..20d822f 100644 --- a/t/11strfkey.c +++ b/t/11strfkey.c @@ -8,7 +8,7 @@ int main(int argc, char *argv[]) char buffer[16]; size_t len; - plan_tests(28); + plan_tests(32); tk = termkey_new_abstract("vt100", 0); @@ -38,6 +38,11 @@ int main(int argc, char *argv[]) is_int(len, 6, "length for unicode/b/CTRL longmod"); is_str(buffer, "Ctrl-b", "buffer for unicode/b/CTRL longmod"); + len = termkey_strfkey(tk, buffer, sizeof buffer, &key, + TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_SPACEMOD); + is_int(len, 6, "length for unicode/b/CTRL longmod|spacemod"); + is_str(buffer, "Ctrl b", "buffer for unicode/b/CTRL longmod|spacemod"); + len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_CARETCTRL); is_int(len, 2, "length for unicode/b/CTRL caretctrl"); is_str(buffer, "^B", "buffer for unicode/b/CTRL caretctrl"); @@ -79,6 +84,14 @@ int main(int argc, char *argv[]) is_int(len, 4, "length for sym/Up/0 wrapbracket"); is_str(buffer, "", "buffer for sym/Up/0 wrapbracket"); + key.type = TERMKEY_TYPE_KEYSYM; + key.code.sym = TERMKEY_SYM_PAGEUP; + key.modifiers = 0; + + len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0); + is_int(len, 6, "length for sym/PageUp/0"); + is_str(buffer, "PageUp", "buffer for sym/PageUp/0"); + key.type = TERMKEY_TYPE_FUNCTION; key.code.number = 5; key.modifiers = 0; diff --git a/t/12strpkey.c b/t/12strpkey.c index 28c32f5..9f20883 100644 --- a/t/12strpkey.c +++ b/t/12strpkey.c @@ -9,7 +9,7 @@ int main(int argc, char *argv[]) #define CLEAR_KEY do { key.type = -1; key.code.codepoint = -1; key.modifiers = -1; key.utf8[0] = 0; } while(0) - plan_tests(53); + plan_tests(58); tk = termkey_new_abstract("vt100", 0); @@ -85,6 +85,14 @@ int main(int argc, char *argv[]) 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, "Meta c", &key, TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_SPACEMOD); + is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta+long/lowermod"); + is_int(key.code.codepoint, 'c', "key.code.codepoint for unicode/c/ALT altismeta+long/lowermod"); + is_int(key.modifiers, TERMKEY_KEYMOD_ALT, "key.modifiers for unicode/c/ALT altismeta+long/lowermod"); + is_str(key.utf8, "c", "key.utf8 for unicode/c/ALT altismeta+long/lowermod"); + 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"); diff --git a/termkey.c b/termkey.c index 5813b9e..a45dc56 100644 --- a/termkey.c +++ b/termkey.c @@ -1191,6 +1191,8 @@ size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, T int wrapbracket = (format & TERMKEY_FORMAT_WRAPBRACKET) && (key->type != TERMKEY_TYPE_UNICODE || key->modifiers != 0); + char sep = (format & TERMKEY_FORMAT_SPACEMOD) ? ' ' : '-'; + if(format & TERMKEY_FORMAT_CARETCTRL && key->type == TERMKEY_TYPE_UNICODE && key->modifiers == TERMKEY_KEYMOD_CTRL) { @@ -1219,19 +1221,19 @@ size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, T } if(key->modifiers & TERMKEY_KEYMOD_ALT) { - l = snprintf(buffer + pos, len - pos, "%s-", mods->alt); + l = snprintf(buffer + pos, len - pos, "%s%c", mods->alt, sep); if(l <= 0) return pos; pos += l; } if(key->modifiers & TERMKEY_KEYMOD_CTRL) { - l = snprintf(buffer + pos, len - pos, "%s-", mods->ctrl); + l = snprintf(buffer + pos, len - pos, "%s%c", mods->ctrl, sep); if(l <= 0) return pos; pos += l; } if(key->modifiers & TERMKEY_KEYMOD_SHIFT) { - l = snprintf(buffer + pos, len - pos, "%s-", mods->shift); + l = snprintf(buffer + pos, len - pos, "%s%c", mods->shift, sep); if(l <= 0) return pos; pos += l; } @@ -1320,10 +1322,10 @@ const char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermK return (char *)str; } - const char *hyphen; + const char *sep_at; - while((hyphen = strchr(str, '-'))) { - size_t n = hyphen - str; + 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; @@ -1335,7 +1337,7 @@ const char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermK else break; - str = hyphen + 1; + str = sep_at + 1; } size_t nbytes; diff --git a/termkey.h.in b/termkey.h.in index 677d5e7..b893cbc 100644 --- a/termkey.h.in +++ b/termkey.h.in @@ -215,6 +215,7 @@ typedef enum { TERMKEY_FORMAT_CARETCTRL = 1 << 1, /* ^X instead of C-X */ TERMKEY_FORMAT_ALTISMETA = 1 << 2, /* Meta- or M- instead of Alt- or A- */ TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, /* Wrap special keys in brackets like */ + TERMKEY_FORMAT_SPACEMOD = 1 << 4, /* M Foo instead of M-Foo */ TERMKEY_FORMAT_MOUSE_POS = 1 << 8 /* Include mouse position if relevant; @ col,line */ } TermKeyFormat;