patch 8.2.4941: '[ and '] marks may be wrong after undo
Problem: '[ and '] marks may be wrong after undo.
Solution: Adjust the '[ and '] marks if needed. (closes #10407, closes #1281)
diff --git a/src/undo.c b/src/undo.c
index 56efb44..cac09f0 100644
--- a/src/undo.c
+++ b/src/undo.c
@@ -2831,9 +2831,10 @@
if (oldsize > 0 || newsize > 0)
changed_lines(top + 1, 0, bot, newsize - oldsize);
- // set '[ and '] mark
+ // Set the '[ mark.
if (top + 1 < curbuf->b_op_start.lnum)
curbuf->b_op_start.lnum = top + 1;
+ // Set the '] mark.
if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum)
curbuf->b_op_end.lnum = top + 1;
else if (top + newsize > curbuf->b_op_end.lnum)
@@ -2853,6 +2854,12 @@
newlist = uep;
}
+ // Ensure the '[ and '] marks are within bounds.
+ if (curbuf->b_op_start.lnum > curbuf->b_ml.ml_line_count)
+ curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count;
+ if (curbuf->b_op_end.lnum > curbuf->b_ml.ml_line_count)
+ curbuf->b_op_end.lnum = curbuf->b_ml.ml_line_count;
+
// Set the cursor to the desired position. Check that the line is valid.
curwin->w_cursor = new_curpos;
check_cursor_lnum();