From db2dc81cee2cf4f7a95c3ccb32c18e6d07b17462 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Fri, 6 Jan 2006 22:35:52 +0000
Subject: [PATCH] fix multibyte bracket search breakage, and add documentation
 fixes

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3261 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 doc/man/nanorc.5  |  5 +++--
 doc/nanorc.sample |  4 ++--
 src/prompt.c      | 20 ++++++++++++++++----
 src/search.c      | 20 ++++++++++++++++----
 4 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/doc/man/nanorc.5 b/doc/man/nanorc.5
index 7cbbef40..2de05bd9 100644
--- a/doc/man/nanorc.5
+++ b/doc/man/nanorc.5
@@ -79,8 +79,9 @@ strings.
 .TP
 .B set matchbrackets "\fIstring\fP"
 Set the opening and closing brackets that can be found by bracket
-searches.  The former set must come before the latter set, and both must
-be in the same order.  The default value is "\fI(<[{)>]}\fP".
+searches.  They cannot contain blank characters.  The former set must
+come before the latter set, and both must be in the same order.  The
+default value is "\fI(<[{)>]}\fP".
 .TP
 .B set/unset morespace
 Allow use of the blank line below the titlebar as extra editing space.
diff --git a/doc/nanorc.sample b/doc/nanorc.sample
index f3e0623e..ecce99c4 100644
--- a/doc/nanorc.sample
+++ b/doc/nanorc.sample
@@ -46,8 +46,8 @@
 # set historylog
 
 ## The opening and closing brackets that can be found by bracket
-## searches.  The former set must come before the latter set, and both
-## must be in the same order.
+## searches.  They cannot contain blank characters.  The former set must
+## come before the latter set, and both must be in the same order.
 ##
 # set matchbrackets "(<[{)>]}"
 
diff --git a/src/prompt.c b/src/prompt.c
index 820800ab..18e41fb7 100644
--- a/src/prompt.c
+++ b/src/prompt.c
@@ -723,8 +723,14 @@ void do_statusbar_find_bracket(void)
 	/* The length of wanted_ch in bytes. */
     char *bracket_set;
 	/* The pair of characters in ch and wanted_ch. */
+    size_t i;
+	/* Generic loop variable. */
     size_t matchhalf;
-	/* The number of characters in one half of matchbrackets. */
+	/* The number of single-byte characters in one half of
+	 * matchbrackets. */
+    size_t mbmatchhalf;
+	/* The number of multibyte characters in one half of
+	 * matchbrackets. */
     size_t count = 1;
 	/* The initial bracket count. */
     bool reverse;
@@ -748,7 +754,13 @@ void do_statusbar_find_bracket(void)
      * bracket.  If we're on a closing bracket, which must be in the
      * second half of matchbrackets, we want to search backwards for an
      * opening bracket. */
-    matchhalf = mbstrlen(matchbrackets) / 2;
+    matchhalf = 0;
+    mbmatchhalf = mbstrlen(matchbrackets) / 2;
+
+    for (i = 0; i < mbmatchhalf; i++)
+	matchhalf += parse_mbchar(matchbrackets + matchhalf, NULL,
+		NULL);
+
     reverse = ((ch - matchbrackets) > matchhalf);
 
     /* If we're on an opening bracket, set wanted_ch to the character
@@ -757,14 +769,14 @@ void do_statusbar_find_bracket(void)
      * characters before ch. */
     wanted_ch = ch;
 
-    while (matchhalf > 0) {
+    while (mbmatchhalf > 0) {
 	if (reverse)
 	    wanted_ch = matchbrackets + move_mbleft(matchbrackets,
 		wanted_ch - matchbrackets);
 	else
 	    wanted_ch += move_mbright(wanted_ch, 0);
 
-	matchhalf--;
+	mbmatchhalf--;
     }
 
     ch_len = parse_mbchar(ch, NULL, NULL);
diff --git a/src/search.c b/src/search.c
index 4a4f46fc..beab6063 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1142,8 +1142,14 @@ void do_find_bracket(void)
 	/* The length of wanted_ch in bytes. */
     char *bracket_set;
 	/* The pair of characters in ch and wanted_ch. */
+    size_t i;
+	/* Generic loop variable. */
     size_t matchhalf;
-	/* The number of characters in one half of matchbrackets. */
+	/* The number of single-byte characters in one half of
+	 * matchbrackets. */
+    size_t mbmatchhalf;
+	/* The number of multibyte characters in one half of
+	 * matchbrackets. */
     size_t count = 1;
 	/* The initial bracket count. */
     bool reverse;
@@ -1170,7 +1176,13 @@ void do_find_bracket(void)
      * bracket.  If we're on a closing bracket, which must be in the
      * second half of matchbrackets, we want to search backwards for an
      * opening bracket. */
-    matchhalf = mbstrlen(matchbrackets) / 2;
+    matchhalf = 0;
+    mbmatchhalf = mbstrlen(matchbrackets) / 2;
+
+    for (i = 0; i < mbmatchhalf; i++)
+	matchhalf += parse_mbchar(matchbrackets + matchhalf, NULL,
+		NULL);
+
     reverse = ((ch - matchbrackets) > matchhalf);
 
     /* If we're on an opening bracket, set wanted_ch to the character
@@ -1179,14 +1191,14 @@ void do_find_bracket(void)
      * characters before ch. */
     wanted_ch = ch;
 
-    while (matchhalf > 0) {
+    while (mbmatchhalf > 0) {
 	if (reverse)
 	    wanted_ch = matchbrackets + move_mbleft(matchbrackets,
 		wanted_ch - matchbrackets);
 	else
 	    wanted_ch += move_mbright(wanted_ch, 0);
 
-	matchhalf--;
+	mbmatchhalf--;
     }
 
     ch_len = parse_mbchar(ch, NULL, NULL);
-- 
GitLab