From 08c51cfd45aafc9a413ba0ac4a3a3549e0ad2814 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 16 Jul 2016 19:44:24 +0200
Subject: [PATCH] input: ingest as verbatim just one control code or one or two
 escapes

Leave the rest of any escape sequence to be processed normally, which
should be possible because those characters are all in ASCII range.

This fixes https://savannah.gnu.org/bugs/?48318.
---
 src/proto.h |  2 +-
 src/winio.c | 27 ++++++++++++++-------------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/proto.h b/src/proto.h
index 636793ea..96da1ec7 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -776,7 +776,7 @@ long get_unicode_kbinput(WINDOW *win, int kbinput);
 int get_control_kbinput(int kbinput);
 void unparse_kbinput(char *output, size_t output_len);
 int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len);
-int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len);
+int *parse_verbatim_kbinput(WINDOW *win, size_t *count);
 #ifndef DISABLE_MOUSE
 int get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts);
 #endif
diff --git a/src/winio.c b/src/winio.c
index 2e137fc4..43a4f93a 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -1311,13 +1311,13 @@ int *get_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
     return retval;
 }
 
-/* Read in a stream of all available characters, and return the length
- * of the string in kbinput_len.  Translate the first few characters of
- * the input into the corresponding multibyte value if possible.  After
- * that, leave the input as-is. */
-int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
+/* Read in one control character (or an iTerm double Escape), or convert a
+ * series of six digits into a Unicode codepoint.  Return in count either 1
+ * (for a control character or the first byte of a multibyte sequence), or 2
+ * (for an iTerm double Escape). */
+int *parse_verbatim_kbinput(WINDOW *win, size_t *count)
 {
-    int *kbinput, *retval;
+    int *kbinput;
 
     /* Read in the first code. */
     while ((kbinput = get_input(win, 1)) == NULL)
@@ -1326,8 +1326,8 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
 #ifndef NANO_TINY
     /* When the window was resized, abort and return nothing. */
     if (*kbinput == KEY_WINCH) {
-	*kbinput_len = 0;
 	free(kbinput);
+	*count = 0;
 	return NULL;
     }
 #endif
@@ -1369,18 +1369,19 @@ int *parse_verbatim_kbinput(WINDOW *win, size_t *kbinput_len)
 	}
     } else
 #endif /* ENABLE_UTF8 */
-
 	/* Put back the first code. */
 	unget_input(kbinput, 1);
 
     free(kbinput);
 
-    /* Get the complete sequence, and save the characters in it as the
-     * result. */
-    *kbinput_len = key_buffer_len;
-    retval = get_input(NULL, *kbinput_len);
+    *count = 1;
 
-    return retval;
+    /* If this is an iTerm double escape, take both Escapes. */
+    if (key_buffer_len > 3 && *key_buffer == ESC_CODE &&
+		key_buffer[1] == ESC_CODE && key_buffer[2] == '[')
+	*count = 2;
+
+    return get_input(NULL, *count);
 }
 
 #ifndef DISABLE_MOUSE
-- 
GitLab