From 43f35fc7a9b8ced075ce43e0e6c598992a430e1f Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Tue, 18 Oct 2016 13:03:01 +0200
Subject: [PATCH] softwrap: ensure the current line is fully visible when
 moving in it

This fixes https://savannah.gnu.org/bugs/?49099,
and fixes the unreported corresponding bugs for
<End> and <Del> and typing extra characters.
---
 src/move.c  | 17 +++++++++++++++++
 src/nano.c  |  2 ++
 src/proto.h |  1 +
 src/text.c  |  2 ++
 4 files changed, 22 insertions(+)

diff --git a/src/move.c b/src/move.c
index 533abaf5..afce2a42 100644
--- a/src/move.c
+++ b/src/move.c
@@ -362,6 +362,19 @@ void do_next_word_void(void)
     do_next_word(ISSET(WORD_BOUNDS), TRUE);
 }
 
+/* Make sure that the current line, when it is partially scrolled off the
+ * screen in softwrap mode, is scrolled fully into view. */
+void ensure_line_is_visible(void)
+{
+#ifndef NANO_TINY
+    if (ISSET(SOFTWRAP) && strlenpt(openfile->current->data) / COLS +
+				openfile->current_y >= editwinrows) {
+	edit_update(ISSET(SMOOTH_SCROLL) ? FLOWING : CENTERING);
+	refresh_needed = TRUE;
+    }
+#endif
+}
+
 /* Move to the beginning of the current line.  If the SMART_HOME flag is
  * set, move to the first non-whitespace character of the current line
  * if we aren't already there, or to the beginning of the current line
@@ -399,6 +412,8 @@ void do_end(void)
 
     if (need_horizontal_scroll(was_column, openfile->placewewant))
 	update_line(openfile->current, openfile->current_x);
+
+    ensure_line_is_visible();
 }
 
 /* If scroll_only is FALSE, move up one line.  If scroll_only is TRUE,
@@ -597,4 +612,6 @@ void do_right(void)
 
     if (openfile->current_x == 0)
 	do_down_void();
+    else
+	ensure_line_is_visible();
 }
diff --git a/src/nano.c b/src/nano.c
index a5420738..6cd68daa 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1926,6 +1926,8 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
     }
 
 #ifndef NANO_TINY
+    ensure_line_is_visible();
+
     /* Well, we might also need a full refresh if we've changed the
      * line length to be a new multiple of COLS. */
     if (ISSET(SOFTWRAP) && refresh_needed == FALSE)
diff --git a/src/proto.h b/src/proto.h
index e5588e99..2d1593d8 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -409,6 +409,7 @@ void do_prev_word(bool allow_punct, bool allow_update);
 void do_prev_word_void(void);
 bool do_next_word(bool allow_punct, bool allow_update);
 void do_next_word_void(void);
+void ensure_line_is_visible(void);
 void do_home(void);
 void do_end(void);
 void do_up(bool scroll_only);
diff --git a/src/text.c b/src/text.c
index cd63cf75..bdc45178 100644
--- a/src/text.c
+++ b/src/text.c
@@ -171,6 +171,8 @@ void do_deletion(undo_type action)
 	return;
 
 #ifndef NANO_TINY
+    ensure_line_is_visible();
+
     if (ISSET(SOFTWRAP) && refresh_needed == FALSE)
 	if (strlenpt(openfile->current->data) / COLS != orig_lenpt / COLS)
 	    refresh_needed = TRUE;
-- 
GitLab