From dfca1c4ea6caa78ac8f78b83dc59b789d71e283d Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Wed, 25 Aug 2004 16:37:06 +0000
Subject: [PATCH] add unget_kbinput(), a wrapper for ungetch()

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

diff --git a/ChangeLog b/ChangeLog
index 43678407..6992d669 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -16,6 +16,8 @@ CVS code -
 	  parsing the numeric argument after "tabsize" works properly
 	  again. (DLR, found by Mike Frysinger)
 - winio.c:
+  unget_kbinput()
+	- New function used as a wrapper for ungetch(). (DLR)
   get_mouseinput()
 	- Consolidate two if statements to increase efficiency. (DLR)
   do_yesno()
diff --git a/src/files.c b/src/files.c
index 25b49ebb..6d9e5492 100644
--- a/src/files.c
+++ b/src/files.c
@@ -2619,7 +2619,7 @@ char *do_browser(const char *inpath)
 		if (selected > numents - 1)
 		    selected = numents - 1;
 		else if (selectedbackup == selected)
-		    ungetch('s');	/* Unget the 'select' key */
+		    unget_kbinput('s', FALSE);	/* Unget the 'select' key */
 	    } else {	/* Must be clicking a shortcut */
 		int mouse_x, mouse_y;
 		get_mouseinput(&mouse_x, &mouse_y, TRUE);
diff --git a/src/nano.c b/src/nano.c
index 8493ba2d..06c67a82 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2609,9 +2609,7 @@ void do_justify(bool full_justify)
 	edit_refresh();
     } else {
 	placewewant = 0;
-	ungetch(kbinput);
-	if (meta_key)
-	    ungetch(NANO_CONTROL_3);
+	unget_kbinput(kbinput, meta_key);
     }
 
     cutbuffer = cutbuffer_save;
diff --git a/src/proto.h b/src/proto.h
index cf4a853f..50752d15 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -491,6 +491,7 @@ int check_wildcard_match(const char *text, const char *pattern);
 #ifndef NANO_SMALL
 void reset_kbinput(void);
 #endif
+void unget_kbinput(int kbinput, bool meta_key);
 int get_kbinput(WINDOW *win, bool *meta_key);
 int get_translated_kbinput(int kbinput, bool *es
 #ifndef NANO_SMALL
diff --git a/src/winio.c b/src/winio.c
index 3f901477..02464669 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -99,6 +99,15 @@ void reset_kbinput(void)
 }
 #endif
 
+/* Put back the input character stored in kbinput.  If meta_key is TRUE,
+ * put back the Escape character after putting back kbinput. */
+void unget_kbinput(int kbinput, bool meta_key)
+{
+    ungetch(kbinput);
+    if (meta_key)
+	ungetch(NANO_CONTROL_3);
+}
+
 /* Read in a single input character.  If it's ignored, swallow it and go
  * on.  Otherwise, try to translate it from ASCII, extended keypad
  * values, and/or escape sequences.  Set meta_key to TRUE when we get a
@@ -147,7 +156,7 @@ int get_kbinput(WINDOW *win, bool *meta_key)
 
 	    /* Next, send back the character we got and read in the
 	     * complete escape sequence. */
-	    ungetch(kbinput);
+	    unget_kbinput(kbinput, FALSE);
 	    escape_seq = get_verbatim_kbinput(win, escape_seq, &es_len,
 		FALSE);
 
@@ -166,7 +175,7 @@ int get_kbinput(WINDOW *win, bool *meta_key)
 		    /* This escape sequence is unrecognized.  Send it
 		     * back. */
 		    for (; es_len > 1; es_len--)
-			ungetch(escape_seq[es_len - 1]);
+			unget_kbinput(escape_seq[es_len - 1], FALSE);
 		    retval = escape_seq[0];
 		}
 	    }
@@ -1243,9 +1252,9 @@ int get_untranslated_kbinput(int kbinput, size_t position, bool
  * coordinates where it took place in mouse_x and mouse_y.  After that,
  * assuming allow_shortcuts is FALSE, if the shortcut list on the
  * bottom two lines of the screen is visible and the mouse event took
- * place on it, figure out which shortcut was clicked and ungetch() the 
- * equivalent keystroke(s).  Return FALSE if no keystrokes were 
- * ungetch()ed, or TRUE if at least one was.  Assume that KEY_MOUSE has 
+ * place on it, figure out which shortcut was clicked and put back the
+ * equivalent keystroke(s).  Return FALSE if no keystrokes were
+ * put back, or TRUE if at least one was.  Assume that KEY_MOUSE has
  * already been read in. */
 bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
 {
@@ -1265,7 +1274,7 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
     /* If we're allowing shortcuts, the current shortcut list is being
      * displayed on the last two lines of the screen, and the mouse
      * event took place inside it, we need to figure out which shortcut
-     * was clicked and ungetch() the equivalent keystroke(s) for it. */
+     * was clicked and put back the equivalent keystroke(s) for it. */
     if (allow_shortcuts && !ISSET(NO_HELP) && wenclose(bottomwin,
 	*mouse_y, *mouse_x)) {
 	int i, j;
@@ -1306,16 +1315,12 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
 	for (; j > 0; j--)
 	    s = s->next;
 
-	/* And ungetch() the equivalent control key.  If it's a meta key
-	 * sequence, we need to ungetch() Escape too.  Assume that the
-	 * shortcut has an equivalent control key, meta key sequence, or
-	 * both. */
+	/* And put back the equivalent key.  Assume that the shortcut
+	 * has an equivalent control key, meta key sequence, or both. */
 	if (s->ctrlval != NANO_NO_KEY)
-	    ungetch(s->ctrlval);
-	else if (s->ctrlval != NANO_NO_KEY) {
-	    ungetch(s->metaval);
-	    ungetch(NANO_CONTROL_3);
-	}
+	    unget_kbinput(s->ctrlval, FALSE);
+	else if (s->ctrlval != NANO_NO_KEY)
+	    unget_kbinput(s->metaval, TRUE);
 
 	return TRUE;
     }
@@ -2022,7 +2027,7 @@ int nanogetstr(int allowtabs, const char *buf, const char *def,
 #endif
 		    if (meta_key && (kbinput == t->metaval || kbinput == t->miscval))
 			/* We hit a meta key.  Do like above.  We don't
-			 * just ungetch() the letter and let it get
+			 * just put back the letter and let it get
 			 * caught above cause that screws the
 			 * keypad... */
 			return kbinput;
-- 
GitLab