From c59979f06722ef3ab64f04b889c389939713b92a Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sat, 23 Oct 2004 02:47:39 +0000
Subject: [PATCH] add UTF-8 support to unget_kbinput(), and fix a minor memory
 leak in the UTF-8 support code in get_kbinput()

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2015 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog   |  5 +++--
 src/files.c |  6 ++++--
 src/nano.c  |  2 +-
 src/proto.h |  2 +-
 src/winio.c | 35 +++++++++++++++++++++++++++++------
 5 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a0fea0f3..2d61bb2b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -53,8 +53,9 @@ CVS code -
 	- Add a func_key flag to the low-level input functions and the
 	  currently existing high-level input functions, to indicate
 	  extended keypad values.  This is needed for UTF-8 support.
-	  Changes to get_kbinput(), get_translated_kbinput(),
-	  get_shortcut(), get_edit_input(), etc. (DLR)
+	  Changes to unget_kbinput(), get_kbinput(),
+	  get_translated_kbinput(), get_shortcut(), get_edit_input(),
+	  etc. (DLR)
 	- Add a multibuffer mode toggle to the "Execute Command" prompt,
 	  for consistency with the "Read File" prompt.  Changes to
 	  do_insertfile() and shortcut_init(). (DLR)
diff --git a/src/files.c b/src/files.c
index cc0bf201..0ffc4014 100644
--- a/src/files.c
+++ b/src/files.c
@@ -2624,8 +2624,10 @@ char *do_browser(const char *inpath)
 		if (selected > numents - 1)
 		    selected = numents - 1;
 		else if (selectedbackup == selected)
-		    unget_kbinput('s', FALSE);	/* Unget the 'select' key */
-	    } else {	/* Must be clicking a shortcut */
+		    /* Unget the 'select' key */
+		    unget_kbinput('s', FALSE, FALSE);
+	    } 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 77a74747..45cbe9ae 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2671,7 +2671,7 @@ void do_justify(bool full_justify)
 	edit_refresh();
     } else {
 	placewewant = 0;
-	unget_kbinput(kbinput, meta_key);
+	unget_kbinput(kbinput, meta_key, func_key);
     }
 
     cutbuffer = cutbuffer_save;
diff --git a/src/proto.h b/src/proto.h
index 1449be89..0726ca0b 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -502,7 +502,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);
+void unget_kbinput(int kbinput, bool meta_key, bool func_key);
 int get_kbinput(WINDOW *win, bool *meta_key, bool *func_key);
 int get_translated_kbinput(int kbinput, seq_type *seq
 #ifndef NANO_SMALL
diff --git a/src/winio.c b/src/winio.c
index 6ca9b84d..fda42a73 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -104,9 +104,29 @@ void reset_kbinput(void)
 
 /* 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)
+void unget_kbinput(int kbinput, bool meta_key, bool func_key)
 {
-    ungetch(kbinput);
+    /* If this character is outside the ASCII range and func_key is
+     * FALSE, treat it as a wide character and put back its equivalent
+     * multibyte sequence. */
+    if (kbinput > 255 && !func_key)
+    {
+	int i;
+	char *s = charalloc(MB_CUR_MAX + 1);
+	wchar_t wc = (wchar_t)kbinput;
+
+	i = wctomb(s, wc);
+
+	if (i == -1)
+	    /* This wide character is unrecognized.  Send it back. */
+	    ungetch(kbinput);
+	else {
+	    for (; i > 0; i--)
+		ungetch(s[i - 1]);
+	}
+	free(s);
+    } else
+	ungetch(kbinput);
     if (meta_key)
 	ungetch(NANO_CONTROL_3);
 }
@@ -181,7 +201,8 @@ int get_kbinput(WINDOW *win, bool *meta_key, bool *func_key)
 			/* This escape sequence is unrecognized.  Send
 			 * it back. */
 			for (; seq_len > 1; seq_len--)
-			    unget_kbinput(sequence[seq_len - 1], FALSE);
+			    unget_kbinput(sequence[seq_len - 1], FALSE,
+				FALSE);
 			retval = sequence[0];
 		    }
 		}
@@ -203,10 +224,12 @@ int get_kbinput(WINDOW *win, bool *meta_key, bool *func_key)
 		    /* This UTF-8 sequence is unrecognized.  Send it
 		     * back. */
 		    for (; seq_len > 1; seq_len--)
-			unget_kbinput(sequence[seq_len - 1], FALSE);
+			unget_kbinput(sequence[seq_len - 1], FALSE,
+				FALSE);
 		    retval = sequence[0];
 		} else
 		    retval = wc;
+		free(s);
 	    }
 	    free(sequence);
 	}
@@ -1365,9 +1388,9 @@ bool get_mouseinput(int *mouse_x, int *mouse_y, bool allow_shortcuts)
 	 * has, at the very least, an equivalent control key, an
 	 * equivalent primary meta key sequence, or both. */
 	if (s->ctrlval != NANO_NO_KEY)
-	    unget_kbinput(s->ctrlval, FALSE);
+	    unget_kbinput(s->ctrlval, FALSE, FALSE);
 	else if (s->metaval != NANO_NO_KEY)
-	    unget_kbinput(s->metaval, TRUE);
+	    unget_kbinput(s->metaval, TRUE, FALSE);
 
 	return TRUE;
     }
-- 
GitLab