From a1c03ed5c1a96740f2395991262b1135b340fa94 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 7 Nov 2015 09:49:34 +0000
Subject: [PATCH] Correcting the logic for adjusting the x position of the
 mark, and improving the comments.

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

diff --git a/ChangeLog b/ChangeLog
index ee71843e..1a916ea3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2015-11-07  Benno Schulenberg  <bensberg@justemail.net>
+	* src/search.c (do_replace_loop): Correct the logic for adjusting the
+	x position of the mark -- it happened to work because 'mark_begin' is
+	NULL when 'old_mark_set' is FALSE.  Also improve the comments.
+
 2015-11-06  Benno Schulenberg  <bensberg@justemail.net>
 	* src/files.c (write_lockfile): Don't bail out when the hostname is
 	overlong, but instead truncate it properly and continue.  This fixes
diff --git a/src/search.c b/src/search.c
index 843729ff..0562f985 100644
--- a/src/search.c
+++ b/src/search.c
@@ -747,14 +747,12 @@ ssize_t do_replace_loop(
 	    length_change = strlen(copy) - strlen(openfile->current->data);
 
 #ifndef NANO_TINY
-	    /* If the mark was on and (mark_begin, mark_begin_x) was the
-	     * top of it, don't change mark_begin_x. */
-	    if (!old_mark_set || !right_side_up) {
-		/* Keep mark_begin_x in sync with the text changes. */
+	    /* If the mark was on and it was located after the cursor,
+	     * then adjust its x position for any text length changes. */
+	    if (old_mark_set && !right_side_up) {
 		if (openfile->current == openfile->mark_begin &&
 			openfile->mark_begin_x > openfile->current_x) {
-		    if (openfile->mark_begin_x < openfile->current_x +
-			match_len)
+		    if (openfile->mark_begin_x < openfile->current_x + match_len)
 			openfile->mark_begin_x = openfile->current_x;
 		    else
 			openfile->mark_begin_x += length_change;
@@ -762,11 +760,10 @@ ssize_t do_replace_loop(
 		}
 	    }
 
-	    /* If the mark was on and (current, current_x) was the top
-	     * of it, don't change real_current_x. */
+	    /* If the mark was not on or it was before the cursor, then
+	     * adjust the cursor's x position for any text length changes. */
 	    if (!old_mark_set || right_side_up) {
 #endif
-		/* Keep real_current_x in sync with the text changes. */
 		if (openfile->current == real_current &&
 			openfile->current_x <= *real_current_x) {
 		    if (*real_current_x < openfile->current_x + match_len)
-- 
GitLab