From aabc81ed7325781d20e61d2683b76e2221117a9d Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 19 Mar 2016 20:32:14 -0400 Subject: [PATCH] Fix bugs with undo/redo --- cursor.go | 3 +-- eventhandler.go | 20 +++++++++++++++----- todolist.md | 3 +-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cursor.go b/cursor.go index 15a6d657..d207dded 100644 --- a/cursor.go +++ b/cursor.go @@ -207,8 +207,7 @@ func (c *Cursor) Display() { if c.y-c.v.topline < 0 || c.y-c.v.topline > c.v.height-1 { c.v.s.HideCursor() } else { - voffset := NumOccurences(c.v.buf.lines[c.y][:c.x], '\t') * (tabSize - 1) - c.v.s.ShowCursor(c.x+voffset+c.v.lineNumOffset, c.y-c.v.topline) + c.v.s.ShowCursor(c.GetVisualX()+c.v.lineNumOffset, c.y-c.v.topline) // cursorStyle := tcell.StyleDefault.Reverse(true) // c.v.s.SetContent(c.x+voffset, c.y-c.v.topline, c.runeUnder(), nil, cursorStyle) } diff --git a/eventhandler.go b/eventhandler.go index ff886c0a..825542d3 100644 --- a/eventhandler.go +++ b/eventhandler.go @@ -61,7 +61,7 @@ func (eh *EventHandler) Insert(start int, text string) { eventType: TextEventInsert, text: text, start: start, - end: start + len(text), + end: start + Count(text), buf: eh.v.buf, time: time.Now(), } @@ -83,6 +83,9 @@ func (eh *EventHandler) Remove(start, end int) { // Execute a textevent and add it to the undo stack func (eh *EventHandler) Execute(t *TextEvent) { + if eh.redo.Len() > 0 { + eh.redo = new(Stack) + } eh.undo.Push(t) ExecuteTextEvent(t) } @@ -97,9 +100,12 @@ func (eh *EventHandler) Undo() { te := t.(*TextEvent) // Modifies the text event UndoTextEvent(te) - eh.redo.Push(t) - eh.v.cursor = te.c + teCursor := te.c + te.c = eh.v.cursor + eh.v.cursor = teCursor + + eh.redo.Push(te) } // Redo the first event in the redo stack @@ -112,6 +118,10 @@ func (eh *EventHandler) Redo() { te := t.(*TextEvent) // Modifies the text event UndoTextEvent(te) - eh.undo.Push(t) - eh.v.cursor = te.c + + teCursor := te.c + te.c = eh.v.cursor + eh.v.cursor = teCursor + + eh.undo.Push(te) } diff --git a/todolist.md b/todolist.md index e8078867..a31a50f4 100644 --- a/todolist.md +++ b/todolist.md @@ -21,9 +21,8 @@ - [ ] Help screen which lists keybindings and commands - [ ] Opened with Ctrl-h -- [ ] Undo/redo +- [x] Undo/redo - [x] Undo/redo stack - - [ ] Functionality similar to nano - [x] Clipboard support - [x] Ctrl-v, Ctrl-c, and Ctrl-x