From a9a6ce09d65e0a2827c4e67b07256b7a084738ce Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Sat, 5 Jun 2004 22:09:56 +0000
Subject: [PATCH] justification fix: if the last line of a justified paragraph
 has a space on the end of it, the space should be removed

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1804 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog  |  7 ++++---
 src/nano.c | 23 +++++++++++++++++------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dc54b9ef..ece44879 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -42,9 +42,10 @@ CVS code -
 	  feature is disabled if justification is disabled. (DLR)
 	- Modify the justification algorithm to work the same way as in
 	  the current version of Pico, i.e, add a space at the end of
-	  each line of the justified paragraph except for the last one.
-	  Changes to justify_format() and do_justify().  (Note that the
-	  addition of spaces to justified lines means that
+	  each line of the justified paragraph except for the last one,
+	  and if there was a space at the end of the last one, remove
+	  it.  Changes to justify_format() and do_justify().  (Note that
+	  the addition of spaces to justified lines means that
 	  first_mod_line can no longer be used to reliably detect the
 	  first modified line in a paragraph, since a line unmodified by
 	  justify_format() may get a space tacked onto the end of it
diff --git a/src/nano.c b/src/nano.c
index b6d75962..f580f159 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2594,14 +2594,25 @@ int do_justify(int full_justify)
 	    /* If the line we were on before still exists, and it was
 	     * not the last line of the paragraph, add a space to the
 	     * end of it to replace the one removed or left out by
-	     * justify_format(). */
-	    if (current->prev != NULL && par_len > 1) {
+	     * justify_format().  If it was the last line of the
+	     * paragraph, and justify_format() left a space on the end
+	     * of it, remove the space. */
+	    if (current->prev != NULL) {
 		size_t prev_line_len = strlen(current->prev->data);
-		current->prev->data = charealloc(current->prev->data,
+
+		if (par_len > 1) {
+		    current->prev->data = charealloc(current->prev->data,
 			prev_line_len + 2);
-		current->prev->data[prev_line_len] = ' ';
-		current->prev->data[prev_line_len + 1] = '\0';
-		totsize++;
+		    current->prev->data[prev_line_len] = ' ';
+		    current->prev->data[prev_line_len + 1] = '\0';
+		    totsize++;
+		} else if (par_len == 1 &&
+			current->prev->data[prev_line_len - 1] == ' ') {
+		    current->prev->data = charealloc(current->prev->data,
+			prev_line_len);
+		    current->prev->data[prev_line_len - 1] = '\0';
+		    totsize--;
+		}
 	    }
 	}
 
-- 
GitLab