From cb4dc9f4db6d4bf6a3c337301dd4458b5528c484 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@telfort.nl>
Date: Sun, 21 Apr 2019 17:31:29 +0200
Subject: [PATCH] wrapping: improve the persistence of the prepending behavior

Now you can have a look elsewhere in the buffer (and even delete
or paste stuff there) and when you return to the original line
and continue typing, any spillover from automatic hard-wrapping
will spill over onto the same line it spilled over to before.

In the bargain, this gets rid of twenty lines of flag-resetting code
that were called for many keystrokes, and in most cases needlessly.
---
 src/nano.c  | 11 -----------
 src/proto.h |  1 -
 src/text.c  | 23 ++++++-----------------
 3 files changed, 6 insertions(+), 29 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index e775a763..d5dd1e1b 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1781,9 +1781,6 @@ void do_input(void)
 		} else
 #endif
 		{
-#ifdef ENABLE_WRAPPING
-			linestruct *was_next = openfile->current->next;
-#endif
 #ifndef NANO_TINY
 			linestruct *was_current = openfile->current;
 			size_t was_x = openfile->current_x;
@@ -1813,14 +1810,6 @@ void do_input(void)
 					also_the_last = FALSE;
 			}
 #endif
-#ifdef ENABLE_WRAPPING
-			/* If the cursor moved to another line and this was not caused
-			 * by adding characters to the buffer, clear the prepend flag. */
-			if (openfile->current->next != was_next &&
-							shortcut->func != do_tab &&
-							shortcut->func != do_verbatim_input)
-				wrap_reset();
-#endif
 #ifdef ENABLE_COLOR
 			if (!refresh_needed && !okay_for_view(shortcut))
 				check_the_multis(openfile->current);
diff --git a/src/proto.h b/src/proto.h
index 5fb1ca74..2609416b 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -523,7 +523,6 @@ void update_multiline_undo(ssize_t lineno, char *indentation);
 void update_undo(undo_type action);
 #endif /* !NANO_TINY */
 #ifdef ENABLE_WRAPPING
-void wrap_reset(void);
 bool do_wrap(void);
 #endif
 #if defined(ENABLE_HELP) || defined(ENABLED_WRAPORJUSTIFY)
diff --git a/src/text.c b/src/text.c
index 16c48fdf..76f5d80c 100644
--- a/src/text.c
+++ b/src/text.c
@@ -35,11 +35,6 @@
 static pid_t pid_of_command = -1;
 		/* The PID of the forked process -- needed when wanting to abort it. */
 #endif
-#ifdef ENABLE_WRAPPING
-static bool prepend_wrap = FALSE;
-		/* Should we prepend wrapped text to the next line? */
-#endif
-
 #ifdef ENABLE_WORDCOMPLETION
 static int pletion_x = 0;
 		/* The x position in pletion_line of the last found completion. */
@@ -1412,13 +1407,6 @@ void update_undo(undo_type action)
 #endif /* !NANO_TINY */
 
 #ifdef ENABLE_WRAPPING
-/* Unset the prepend_wrap flag.  We need to do this as soon as we do
- * something other than type text. */
-void wrap_reset(void)
-{
-	prepend_wrap = FALSE;
-}
-
 /* When the current line is overlong, hard-wrap it at the furthest possible
  * whitespace character, and (if possible) prepend the remainder of the line
  * to the next line.  Return TRUE if wrapping occurred, and FALSE otherwise. */
@@ -1436,6 +1424,8 @@ bool do_wrap(void)
 		/* The text after the wrap point. */
 	size_t rest_length;
 		/* The length of the remainder. */
+	static linestruct *spillage_line = NULL;
+		/* The line, if any, to which the remainder will be prepended. */
 
 	/* First find the last blank character where we can break the line. */
 	wrap_loc = break_line(line->data, wrap_at, FALSE);
@@ -1466,7 +1456,8 @@ bool do_wrap(void)
 	/* When prepending and the remainder of this line will not make the next
 	 * line too long, then join the two lines, so that, after the line wrap,
 	 * the remainder will effectively have been prefixed to the next line. */
-	if (prepend_wrap && rest_length + strlenpt(line->next->data) <= wrap_at) {
+	if (line->next == spillage_line && spillage_line != NULL &&
+				rest_length + strlenpt(spillage_line->data) <= wrap_at) {
 		/* Go to the end of this line. */
 		openfile->current_x = line_len;
 
@@ -1512,13 +1503,11 @@ bool do_wrap(void)
 	do_enter();
 
 	if (cursor_x < wrap_loc) {
+		spillage_line = openfile->current;
 		openfile->current = openfile->current->prev;
 		openfile->current_x = cursor_x;
-		prepend_wrap = TRUE;
-	} else {
+	} else
 		openfile->current_x += (cursor_x - wrap_loc);
-		prepend_wrap = FALSE;
-	}
 
 	openfile->placewewant = xplustabs();
 
-- 
2.20.1

