From 6da969e44bcc4465a7a7de8cdc44c96f00846f4b Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sat, 28 Aug 2004 15:51:07 +0000
Subject: [PATCH] work around the need to put back the first non-escape
 character when reading an escape sequence

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1920 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  9 +++++++++
 src/nano.c  |  2 +-
 src/proto.h |  4 ++--
 src/winio.c | 47 ++++++++++++++++++++++++-----------------------
 4 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dbcc8f85..25067921 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,15 @@ CVS code -
 - winio.c:
   unget_kbinput()
 	- New function used as a wrapper for ungetch(). (DLR)
+  get_kbinput()
+	- When reading an escape sequence, set get_verbatim_kbinput()'s
+	  new first parameter to the first character of the sequence
+	  instead of putting that character back and reading the entire
+	  sequence afterwards. (DLR)
+  get_verbatim_kbinput()
+	- Add new parameter first.  If first isn't ERR, make it the
+	  first character in the returned sequence instead of reading
+	  the first character in via blocking input. (DLR)
   get_mouseinput()
 	- Consolidate two if statements to increase efficiency. (DLR)
 	- Check kbinput against metaval instead of (erroneously) ctrlval
diff --git a/src/nano.c b/src/nano.c
index 15ee2821..f458a37b 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -946,7 +946,7 @@ void do_verbatim_input(void)
 
     statusbar(_("Verbatim input"));
 
-    v_kbinput = get_verbatim_kbinput(edit, v_kbinput, &v_len, TRUE);
+    v_kbinput = get_verbatim_kbinput(edit, ERR, v_kbinput, &v_len, TRUE);
 
     /* Turn on DISABLE_CURPOS while inserting character(s) and turn it
      * off afterwards, so that if constant cursor position display is
diff --git a/src/proto.h b/src/proto.h
index 93c710b0..5a6c5c0b 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -508,8 +508,8 @@ int get_control_kbinput(int kbinput);
 int get_escape_seq_kbinput(int *escape_seq, size_t es_len, bool
 	*ignore_seq);
 int get_escape_seq_abcd(int kbinput);
-int *get_verbatim_kbinput(WINDOW *win, int *v_kbinput, size_t *v_len,
-	bool allow_ascii);
+int *get_verbatim_kbinput(WINDOW *win, int first, int *v_kbinput, size_t
+	*v_len, bool allow_ascii);
 int get_untranslated_kbinput(int kbinput, size_t position, bool
 	allow_ascii
 #ifndef NANO_SMALL
diff --git a/src/winio.c b/src/winio.c
index 3411c17c..ada4a4b6 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -145,26 +145,22 @@ int get_kbinput(WINDOW *win, bool *meta_key)
 	    int *escape_seq = NULL;
 	    size_t es_len;
 
-	    /* First, assume that we got a meta sequence.  Set meta_key
-	     * to TRUE and save the character we got as the result.  We
-	     * do this so that if the keyboard buffer is full when we
-	     * send back the character we got below (in which case we'll
-	     * lose that character), it'll still be properly interpreted
-	     * as a meta sequence. */
-	    *meta_key = TRUE;
-	    retval = tolower(kbinput);
-
-	    /* Next, send back the character we got and read in the
-	     * complete escape sequence. */
-	    unget_kbinput(kbinput, FALSE);
-	    escape_seq = get_verbatim_kbinput(win, escape_seq, &es_len,
-		FALSE);
-
+	    /* Read in the complete escape sequence, putting the initial
+	     * non-escape at the beginning of it. */
+	    escape_seq = get_verbatim_kbinput(win, kbinput, escape_seq,
+		&es_len, FALSE);
+
+	    /* If the escape sequence is one character long, set
+	     * meta_key to TRUE, make the non-escape character
+	     * lowercase, and save that as the result. */
+	    if (es_len == 1) {
+		*meta_key = TRUE;
+		retval = tolower(kbinput);
 	    /* If the escape sequence is more than one character
 	     * long, set meta_key to FALSE, translate the escape
 	     * sequence into the corresponding key value, and save
 	     * that as the result. */
-	    if (es_len > 1) {
+	    } else if (es_len > 1) {
 		bool ignore_seq;
 
 		*meta_key = FALSE;
@@ -1078,10 +1074,11 @@ int get_escape_seq_abcd(int kbinput)
 }
 
 /* Read in a string of input characters (e.g. an escape sequence)
- * verbatim.  Store the string in v_kbinput and return the length
- * of the string in v_len.  Assume nodelay(win) is FALSE. */
-int *get_verbatim_kbinput(WINDOW *win, int *v_kbinput, size_t *v_len,
-	bool allow_ascii)
+ * verbatim.  If first isn't ERR, make it the first character of the
+ * string.  Store the string in v_kbinput and return the length of the
+ * string in v_len.  Assume nodelay(win) is FALSE. */
+int *get_verbatim_kbinput(WINDOW *win, int first, int *v_kbinput, size_t
+	*v_len, bool allow_ascii)
 {
     int kbinput;
     size_t i = 0, v_newlen = 0;
@@ -1100,10 +1097,14 @@ int *get_verbatim_kbinput(WINDOW *win, int *v_kbinput, size_t *v_len,
 	disable_flow_control();
     keypad(win, FALSE);
 
-    /* Read the first character using blocking input, since using
-     * non-blocking input will eat up all unused CPU.  Then increment
+    /* If first is ERR, read the first character using blocking input,
+     * since using non-blocking input will eat up all unused CPU.
+     * Otherwise, treat first as the first character.  Then increment
      * v_len and save the character in v_kbinput. */
-    kbinput = wgetch(win);
+    if (first == ERR)
+	kbinput = wgetch(win);
+    else
+	kbinput = first;
     (*v_len)++;
     v_kbinput[0] = kbinput;
 #ifdef DEBUG
-- 
GitLab