From efff850e54d8cf4be7aca26bb97030ef281811b4 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 4 Sep 2017 15:47:24 -0400 Subject: [PATCH] Clear cursors before performing undo or redo This fix isn't ideal because it removes the cursors but it does work Fixes #798 --- cmd/micro/actions.go | 15 +++++++++------ cmd/micro/buffer.go | 9 +++++++++ cmd/micro/view.go | 18 ++++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 0042b00a..0b867878 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -996,6 +996,10 @@ func (v *View) Undo(usePlugin bool) bool { return false } + if v.Buf.curCursor == 0 { + v.Buf.clearCursors() + } + v.Buf.Undo() messenger.Message("Undid action") @@ -1011,6 +1015,10 @@ func (v *View) Redo(usePlugin bool) bool { return false } + if v.Buf.curCursor == 0 { + v.Buf.clearCursors() + } + v.Buf.Redo() messenger.Message("Redid action") @@ -2028,12 +2036,7 @@ func (v *View) RemoveAllMultiCursors(usePlugin bool) bool { return false } - for i := 1; i < len(v.Buf.cursors); i++ { - v.Buf.cursors[i] = nil - } - v.Buf.cursors = v.Buf.cursors[:1] - v.Buf.UpdateCursors() - v.Cursor.ResetSelection() + v.Buf.clearCursors() v.Relocate() if usePlugin { diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 0d4c652b..31d2be4d 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -562,3 +562,12 @@ func (b *Buffer) ClearMatches() { b.SetState(i, nil) } } + +func (b *Buffer) clearCursors() { + for i := 1; i < len(b.cursors); i++ { + b.cursors[i] = nil + } + b.cursors = b.cursors[:1] + b.UpdateCursors() + b.Cursor.ResetSelection() +} diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 92ae56b0..4560c30e 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -479,9 +479,14 @@ func (v *View) ExecuteActions(actions []func(*View, bool) bool) bool { return relocate } -func (v *View) SetCursor(c *Cursor) { +func (v *View) SetCursor(c *Cursor) bool { + if c == nil { + return false + } v.Cursor = c v.Buf.curCursor = c.Num + + return true } // HandleEvent handles an event passed by the main loop @@ -505,7 +510,10 @@ func (v *View) HandleEvent(event tcell.Event) { } if e.Modifiers() == key.modifiers { for _, c := range v.Buf.cursors { - v.SetCursor(c) + ok := v.SetCursor(c) + if !ok { + break + } relocate = false isBinding = true relocate = v.ExecuteActions(actions) || relocate @@ -553,7 +561,6 @@ func (v *View) HandleEvent(event tcell.Event) { for _, c := range v.Buf.cursors { v.SetCursor(c) v.paste(e.Text()) - } v.SetCursor(&v.Buf.Cursor) @@ -568,7 +575,10 @@ func (v *View) HandleEvent(event tcell.Event) { for key, actions := range bindings { if button == key.buttons && e.Modifiers() == key.modifiers { for _, c := range v.Buf.cursors { - v.SetCursor(c) + ok := v.SetCursor(c) + if !ok { + break + } relocate = v.ExecuteActions(actions) || relocate } v.SetCursor(&v.Buf.Cursor)