From 6763bc7db880da27b8adbe8b40c7aa27de2c83f7 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sat, 2 Apr 2016 12:13:24 +0200 Subject: [PATCH] keyboard: recognize four escape sequences produced by iTerm2 On iTerm2 on OS X, the Option+Arrow keys produce special sequences that start with two escapes. Catch these sequences and interpret them appropriately as WordLeft / WordRight / Home / End. Signed-off-by: Mike Scalora Tested-by: Thomas Rosenau Signed-off-by: Benno Schulenberg --- src/winio.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/winio.c b/src/winio.c index 8287ed5..3678ef6 100644 --- a/src/winio.c +++ b/src/winio.c @@ -331,6 +331,7 @@ int get_kbinput(WINDOW *win) int parse_kbinput(WINDOW *win) { static int escapes = 0, byte_digits = 0; + static bool double_esc = FALSE; int *kbinput, retval = ERR; meta_key = FALSE; @@ -391,7 +392,28 @@ int parse_kbinput(WINDOW *win) retval = parse_escape_sequence(win, *kbinput); break; case 2: - if (get_key_buffer_len() == 0) { + if (double_esc) { + /* An "ESC ESC [ X" sequence from Option+arrow. */ + switch (*kbinput) { + case 'A': + retval = KEY_HOME; + break; + case 'B': + retval = KEY_END; + break; + case 'C': + retval = controlright; + break; + case 'D': + retval = controlleft; + break; + default: + retval = ERR; + break; + } + double_esc = FALSE; + escapes = 0; + } else if (get_key_buffer_len() == 0) { if (('0' <= *kbinput && *kbinput <= '2' && byte_digits == 0) || ('0' <= *kbinput && *kbinput <= '9' && byte_digits > 0)) { @@ -463,6 +485,9 @@ int parse_kbinput(WINDOW *win) retval = *kbinput; } } + } else if (*kbinput=='[') { + /* This is an iTerm2 sequence: ^[ ^[ [ X. */ + double_esc = TRUE; } else { /* Two escapes followed by a non-escape, and * there are other keystrokes waiting: combined -- 2.7.4