aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-04 19:55:31 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-04 19:55:31 +0200
commit1377a957ce7a7fe70405f8ec1e6cff5a74b9c40a (patch)
tree2611865fa244e12d19a09ebc4944de5562e8a590
parent92e67cc5a3d17fe33a1af66961d7e70873572741 (diff)
downloadvis-1377a957ce7a7fe70405f8ec1e6cff5a74b9c40a.tar.gz
vis-1377a957ce7a7fe70405f8ec1e6cff5a74b9c40a.tar.xz
Factor out getkey logic
-rw-r--r--main.c57
1 files changed, 30 insertions, 27 deletions
diff --git a/main.c b/main.c
index f314810..68bcb0c 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@ int ESCDELAY;
# define set_escdelay(d) (ESCDELAY = (d))
#endif
+static Key getkey(void);
static void cursor(const Arg *arg);
static void call(const Arg *arg);
static void insert(const Arg *arg);
@@ -122,6 +123,31 @@ static KeyBinding *keybinding(Mode *mode, Key *key0, Key *key1) {
return NULL;
}
+static Key getkey(void) {
+ Key key = { .str = "\0\0\0\0\0\0", .code = 0 };
+ int keycode = getch();
+ if (keycode == ERR)
+ return key;
+
+ // TODO verbatim insert mode
+ int len = 0;
+ if (keycode >= KEY_MIN) {
+ key.code = keycode;
+ } else {
+ char keychar = keycode;
+ key.str[len++] = keychar;
+
+ if (!ISASCII(keychar) || keychar == '\e') {
+ nodelay(stdscr, TRUE);
+ for (int t; len < LENGTH(key.str) && (t = getch()) != ERR; len++)
+ key.str[len] = t;
+ nodelay(stdscr, FALSE);
+ }
+ }
+
+ return key;
+}
+
int main(int argc, char *argv[]) {
/* decide which key configuration to use based on argv[0] */
char *arg0 = argv[0];
@@ -177,32 +203,9 @@ int main(int argc, char *argv[]) {
continue;
}
- int keycode = getch();
- if (keycode == ERR)
- continue;
-
- // TODO verbatim insert mode
- int len = 0;
- if (keycode >= KEY_MIN) {
- key.code = keycode;
- key.str[0] = '\0';
- } else {
- char keychar = keycode;
- key.str[len++] = keychar;
- key.code = 0;
-
- if (!ISASCII(keychar) || keychar == '\e') {
- nodelay(stdscr, TRUE);
- for (int t; len < LENGTH(key.str) && (t = getch()) != ERR; len++)
- key.str[len] = t;
- nodelay(stdscr, FALSE);
- }
- }
-
- for (size_t i = len; i < LENGTH(key.str); i++)
- key.str[i] = '\0';
-
+ key = getkey();
KeyBinding *action = keybinding(mode, key_mod ? key_mod : &key, key_mod ? &key : NULL);
+
if (!action && key_mod) {
/* second char of a combination was invalid, search again without the prefix */
action = keybinding(mode, &key, NULL);
@@ -220,10 +223,10 @@ int main(int argc, char *argv[]) {
continue;
}
- if (keycode >= KEY_MIN)
+ if (key.code)
continue;
- if (mode->input && mode->input(key.str, len))
+ if (mode->input && mode->input(key.str, strlen(key.str)))
timeout = &idle;
}