From bfed7386be9082d047af94edebb28e3297acc473 Mon Sep 17 00:00:00 2001
From: Caleb Sander <caleb.sander@gmail.com>
Date: Tue, 1 Oct 2019 22:00:21 -0700
Subject: [PATCH] Insert matching ), ], and } characters

---
 src/nano.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index ec0bb038..acafd27a 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1862,14 +1862,21 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
 			continue;
 
 		/* Make room for the new character and copy it into the line. */
-		openfile->current->data = charealloc(openfile->current->data,
-										current_len + char_len + 1);
-		charmove(openfile->current->data + openfile->current_x + char_len,
-						openfile->current->data + openfile->current_x,
+		char matching_char =
+			*onechar == '(' ? ')' :
+			*onechar == '[' ? ']' :
+			*onechar == '{' ? '}' : '\0';
+		size_t insert_length = matching_char ? char_len + 1 : char_len;
+		openfile->current->data =
+			charealloc(openfile->current->data, current_len + insert_length + 1);
+		char *insert_start = openfile->current->data + openfile->current_x;
+		charmove(insert_start + insert_length, insert_start,
 						current_len - openfile->current_x + 1);
-		strncpy(openfile->current->data + openfile->current_x, onechar,
-						char_len);
-		current_len += char_len;
+		strncpy(insert_start, onechar, char_len);
+		if (matching_char) {
+			insert_start[char_len] = matching_char;
+		}
+		current_len += insert_length;
 		openfile->totsize++;
 		set_modified();
 
-- 
GitLab