From 658c20ff2a20f62fb2927b6e778244a948e317f5 Mon Sep 17 00:00:00 2001
From: Dmytro Maluka <dmitrymaluka@gmail.com>
Date: Sun, 4 Aug 2024 13:36:59 +0200
Subject: [PATCH 1/2] undo/redo: Don't change remembered cursor location

Remember the cursor location in TextEvent just once - when the original
text event happens, so that when we redo after an undo, the cursor is
placed at the location where the actual redone modification happens (as
the user would expect), not at the location where the cursor was before
the undo (which may be a completely unrelated location and may be far
away).

Fixes #3411
---
 internal/buffer/eventhandler.go | 2 --
 1 file changed, 2 deletions(-)

diff --git a/internal/buffer/eventhandler.go b/internal/buffer/eventhandler.go
index f1fe2a0750..2f3183ee2c 100644
--- a/internal/buffer/eventhandler.go
+++ b/internal/buffer/eventhandler.go
@@ -293,7 +293,6 @@ func (eh *EventHandler) UndoOneEvent() {
 	// Set the cursor in the right place
 	teCursor := t.C
 	if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
-		t.C = *eh.cursors[teCursor.Num]
 		eh.cursors[teCursor.Num].Goto(teCursor)
 		eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
 	} else {
@@ -338,7 +337,6 @@ func (eh *EventHandler) RedoOneEvent() {
 
 	teCursor := t.C
 	if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
-		t.C = *eh.cursors[teCursor.Num]
 		eh.cursors[teCursor.Num].Goto(teCursor)
 		eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
 	} else {

From 0d51035acd4419b12886e7cec5e651c489d0ef44 Mon Sep 17 00:00:00 2001
From: Dmytro Maluka <dmitrymaluka@gmail.com>
Date: Sun, 4 Aug 2024 13:56:03 +0200
Subject: [PATCH 2/2] undo/redo: Remove no longer needed teCursor temp var

---
 internal/buffer/eventhandler.go | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/internal/buffer/eventhandler.go b/internal/buffer/eventhandler.go
index 2f3183ee2c..10104f9c45 100644
--- a/internal/buffer/eventhandler.go
+++ b/internal/buffer/eventhandler.go
@@ -291,12 +291,9 @@ func (eh *EventHandler) UndoOneEvent() {
 	eh.UndoTextEvent(t)
 
 	// Set the cursor in the right place
-	teCursor := t.C
-	if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
-		eh.cursors[teCursor.Num].Goto(teCursor)
-		eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
-	} else {
-		teCursor.Num = -1
+	if t.C.Num >= 0 && t.C.Num < len(eh.cursors) {
+		eh.cursors[t.C.Num].Goto(t.C)
+		eh.cursors[t.C.Num].NewTrailingWsY = t.C.NewTrailingWsY
 	}
 
 	// Push it to the redo stack
@@ -335,12 +332,9 @@ func (eh *EventHandler) RedoOneEvent() {
 		return
 	}
 
-	teCursor := t.C
-	if teCursor.Num >= 0 && teCursor.Num < len(eh.cursors) {
-		eh.cursors[teCursor.Num].Goto(teCursor)
-		eh.cursors[teCursor.Num].NewTrailingWsY = teCursor.NewTrailingWsY
-	} else {
-		teCursor.Num = -1
+	if t.C.Num >= 0 && t.C.Num < len(eh.cursors) {
+		eh.cursors[t.C.Num].Goto(t.C)
+		eh.cursors[t.C.Num].NewTrailingWsY = t.C.NewTrailingWsY
 	}
 
 	// Modifies the text event