From a8bc492b8a12118a23f19f6312e13b148f434dfc Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Sat, 12 Dec 2009 22:21:20 +0000
Subject: [PATCH] 2009-12-12 Chris Allegretta <chrisa@asty.org>         *
 text.c (do_delete), nano.c (do_output): Add check for length of current line 
          before and after adding/deleting text, and do full refresh if it is
 now           a different multiple of COLS.  Also get rid of superfluous
 do_refresh           vars now that we have edit_refresh_needed.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4462 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog  |  8 +++++++-
 src/nano.c | 31 ++++++++++++++-----------------
 src/text.c | 17 ++++++++++-------
 3 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ae3ff1f3..36fd038e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,10 @@
-2009-12-07 David Lawrence Ramsey <pooka109@gmail.com>
+2009-12-12 Chris Allegretta <chrisa@asty.org>
+	* text.c (do_delete), nano.c (do_output): Add check for length of current line 
+	  before and after adding/deleting text, and do full refresh if it is now
+	  a different multiple of COLS.  Also get rid of superfluous do_refresh
+	  vars now that we have edit_refresh_needed.
+
+2009-12-09 David Lawrence Ramsey <pooka109@gmail.com>
 	* global.c (shortcut_init), browser.c (do_browser): Fix M-W not being bound to 
 	  research in either main menu or browser.
 
diff --git a/src/nano.c b/src/nano.c
index 8e5d60ce..d6a21d10 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -1893,17 +1893,15 @@ precalc_cleanup:
  * TRUE. */
 void do_output(char *output, size_t output_len, bool allow_cntrls)
 {
-    size_t current_len, i = 0;
-    bool do_refresh = FALSE;
-	/* Do we have to call edit_refresh(), or can we get away with
-	 * just update_line()? */
-
+    size_t current_len, orig_lenpt, i = 0;
     char *char_buf = charalloc(mb_cur_max());
     int char_buf_len;
 
     assert(openfile->current != NULL && openfile->current->data != NULL);
 
     current_len = strlen(openfile->current->data);
+    if (ISSET(SOFTWRAP))
+	orig_lenpt = strlenpt(openfile->current->data);
 
     while (i < output_len) {
 	/* If allow_cntrls is TRUE, convert nulls and newlines
@@ -1967,26 +1965,25 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
 
 #ifndef DISABLE_WRAPPING
 	/* If we're wrapping text, we need to call edit_refresh(). */
-	if (!ISSET(NO_WRAP)) {
-	    bool do_refresh_save = do_refresh;
-
-	    do_refresh = do_wrap(openfile->current, FALSE);
-
-	    /* If we needed to call edit_refresh() before this, we'll
-	     * still need to after this. */
-	    if (do_refresh_save)
-		do_refresh = TRUE;
-	}
+	if (!ISSET(NO_WRAP))
+	    if (do_wrap(openfile->current, FALSE))
+		edit_refresh_needed = TRUE;
 #endif
 
 #ifdef ENABLE_COLOR
 	/* If color syntaxes are available and turned on, we need to
 	 * call edit_refresh(). */
 	if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
-	    do_refresh = TRUE;
+	    edit_refresh_needed = TRUE;
 #endif
     }
 
+    /* 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) && edit_refresh_needed == FALSE)
+	if (strlenpt(openfile->current->data) / COLS  != orig_lenpt / COLS)
+	    edit_refresh_needed = TRUE;
+
     free(char_buf);
 
     openfile->placewewant = xplustabs();
@@ -1995,7 +1992,7 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
 #ifdef ENABLE_COLOR
     reset_multis(openfile->current, FALSE);
 #endif
-    if (do_refresh) {
+    if (edit_refresh_needed == TRUE) {
 	edit_refresh();
 	edit_refresh_needed = FALSE;
     } else
diff --git a/src/text.c b/src/text.c
index 1d657d58..38b9338c 100644
--- a/src/text.c
+++ b/src/text.c
@@ -66,9 +66,7 @@ void do_mark(void)
 /* Delete the character under the cursor. */
 void do_delete(void)
 {
-    bool do_refresh = FALSE;
-	/* Do we have to call edit_refresh(), or can we get away with
-	 * just update_line()? */
+    size_t orig_lenpt = 0;
 
 #ifndef NANO_TINY
     update_undo(DEL);
@@ -86,6 +84,9 @@ void do_delete(void)
 
 	assert(openfile->current_x < strlen(openfile->current->data));
 
+	if (ISSET(SOFTWRAP))
+	    orig_lenpt = strlenpt(openfile->current->data);
+
 	/* Let's get dangerous. */
 	charmove(&openfile->current->data[openfile->current_x],
 		&openfile->current->data[openfile->current_x +
@@ -108,7 +109,7 @@ void do_delete(void)
 	/* If we're deleting at the end of a line, we need to call
 	 * edit_refresh(). */
 	if (openfile->current->data[openfile->current_x] == '\0')
-	    do_refresh = TRUE;
+	    edit_refresh_needed = TRUE;
 
 	openfile->current->data = charealloc(openfile->current->data,
 		openfile->current_x + strlen(foo->data) + 1);
@@ -138,11 +139,13 @@ void do_delete(void)
     } else
 	return;
 
+    if (ISSET(SOFTWRAP) && edit_refresh_needed == FALSE)
+	if (strlenpt(openfile->current->data) / COLS != orig_lenpt / COLS)
+	    edit_refresh_needed  = TRUE;
+
     set_modified();
 
-    if (do_refresh)
-	edit_refresh_needed = TRUE;
-    else
+    if (edit_refresh_needed  == FALSE)
 	update_line(openfile->current, openfile->current_x);
 }