mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 06:37:14 +09:00
Add timed undo
This commit is contained in:
@@ -94,24 +94,85 @@ func (eh *EventHandler) Execute(t *TextEvent) {
|
|||||||
|
|
||||||
// Undo the first event in the undo stack
|
// Undo the first event in the undo stack
|
||||||
func (eh *EventHandler) Undo() {
|
func (eh *EventHandler) Undo() {
|
||||||
|
t := eh.undo.Peek()
|
||||||
|
if t == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
te := t.(*TextEvent)
|
||||||
|
startTime := t.(*TextEvent).time.UnixNano() / int64(time.Millisecond)
|
||||||
|
|
||||||
|
eh.UndoOneEvent()
|
||||||
|
|
||||||
|
for {
|
||||||
|
t = eh.undo.Peek()
|
||||||
|
if t == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
te = t.(*TextEvent)
|
||||||
|
|
||||||
|
if startTime-(te.time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eh.UndoOneEvent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UndoOneEvent undoes one event
|
||||||
|
func (eh *EventHandler) UndoOneEvent() {
|
||||||
|
// This event should be undone
|
||||||
|
// Pop it off the stack
|
||||||
t := eh.undo.Pop()
|
t := eh.undo.Pop()
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
te := t.(*TextEvent)
|
te := t.(*TextEvent)
|
||||||
|
// Undo it
|
||||||
// Modifies the text event
|
// Modifies the text event
|
||||||
UndoTextEvent(te)
|
UndoTextEvent(te)
|
||||||
|
|
||||||
|
// Set the cursor in the right place
|
||||||
teCursor := te.c
|
teCursor := te.c
|
||||||
te.c = eh.v.cursor
|
te.c = eh.v.cursor
|
||||||
eh.v.cursor = teCursor
|
eh.v.cursor = teCursor
|
||||||
|
|
||||||
|
// Push it to the redo stack
|
||||||
eh.redo.Push(te)
|
eh.redo.Push(te)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redo the first event in the redo stack
|
// Redo the first event in the redo stack
|
||||||
func (eh *EventHandler) Redo() {
|
func (eh *EventHandler) Redo() {
|
||||||
|
t := eh.redo.Peek()
|
||||||
|
if t == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
te := t.(*TextEvent)
|
||||||
|
startTime := t.(*TextEvent).time.UnixNano() / int64(time.Millisecond)
|
||||||
|
|
||||||
|
eh.RedoOneEvent()
|
||||||
|
|
||||||
|
for {
|
||||||
|
t = eh.redo.Peek()
|
||||||
|
if t == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
te = t.(*TextEvent)
|
||||||
|
|
||||||
|
if (te.time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
eh.RedoOneEvent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RedoOneEvent redoes one event
|
||||||
|
func (eh *EventHandler) RedoOneEvent() {
|
||||||
t := eh.redo.Pop()
|
t := eh.redo.Pop()
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const (
|
|||||||
synLinesUp = 75 // How many lines up to look to do syntax highlighting
|
synLinesUp = 75 // How many lines up to look to do syntax highlighting
|
||||||
synLinesDown = 75 // How many lines down to look to do syntax highlighting
|
synLinesDown = 75 // How many lines down to look to do syntax highlighting
|
||||||
doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
|
doubleClickThreshold = 400 // How many milliseconds to wait before a second click is not a double click
|
||||||
|
undoThreshold = 500 // If two events are less than n milliseconds apart, undo both of them
|
||||||
)
|
)
|
||||||
|
|
||||||
// The main screen
|
// The main screen
|
||||||
|
|||||||
@@ -33,3 +33,11 @@ func (s *Stack) Pop() (value interface{}) {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Peek returns the top element of the stack without removing it
|
||||||
|
func (s *Stack) Peek() interface{} {
|
||||||
|
if s.size > 0 {
|
||||||
|
return s.top.value
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user