diff --git a/ChangeLog b/ChangeLog
index ee71843eab8ae780b120a09c5d5ae8e0f109837f..1a916ea3449ca0480eafd06b23eec16576b942bf 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 843729ff47655b426fc3f1bff7579da66499097d..0562f98589238071d74fa44fa272d432fd4298e4 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)