From 86b111ff89f6e05264d2eb819ce29dbe89f4a27f Mon Sep 17 00:00:00 2001 From: Mike Scalora 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 Signed-off-by: Thomas Rosenau Signed-off-by: Benno Schulenberg --- src/winio.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/winio.c b/src/winio.c index 8287ed5..4c60829 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,30 @@ 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; +#ifndef NANO_TINY + case 'C': + retval = controlright; + break; + case 'D': + retval = controlleft; + break; +#endif + 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 +487,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