From d851ccdd413e6265a34f56c3d2a2edf227d4f9b7 Mon Sep 17 00:00:00 2001
From: Mike Scalora <mike@scalora.org>
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 <mike@scalora.org>
Signed-off-by: Thomas Rosenau <thomasr@fantasymail.de>
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
 src/winio.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/winio.c b/src/winio.c
index 1516738c..acaae86b 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -330,6 +330,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;
@@ -390,7 +391,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)) {
@@ -462,6 +486,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
-- 
GitLab