]> git.lizzy.rs Git - micro.git/blob - cmd/micro/eventhandler.go
efeca821a1c531eff1a5e2c62c88412bc8be376d
[micro.git] / cmd / micro / eventhandler.go
1 package main
2
3 import (
4         "strings"
5         "time"
6
7         dmp "github.com/sergi/go-diff/diffmatchpatch"
8         "github.com/yuin/gopher-lua"
9 )
10
11 const (
12         // Opposite and undoing events must have opposite values
13
14         // TextEventInsert repreasents an insertion event
15         TextEventInsert = 1
16         // TextEventRemove represents a deletion event
17         TextEventRemove = -1
18 )
19
20 // TextEvent holds data for a manipulation on some text that can be undone
21 type TextEvent struct {
22         C Cursor
23
24         EventType int
25         Text      string
26         Start     Loc
27         End       Loc
28         Time      time.Time
29 }
30
31 // ExecuteTextEvent runs a text event
32 func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
33         if t.EventType == TextEventInsert {
34                 buf.insert(t.Start, []byte(t.Text))
35         } else if t.EventType == TextEventRemove {
36                 t.Text = buf.remove(t.Start, t.End)
37         }
38 }
39
40 // UndoTextEvent undoes a text event
41 func UndoTextEvent(t *TextEvent, buf *Buffer) {
42         t.EventType = -t.EventType
43         ExecuteTextEvent(t, buf)
44 }
45
46 // EventHandler executes text manipulations and allows undoing and redoing
47 type EventHandler struct {
48         buf       *Buffer
49         UndoStack *Stack
50         RedoStack *Stack
51 }
52
53 // NewEventHandler returns a new EventHandler
54 func NewEventHandler(buf *Buffer) *EventHandler {
55         eh := new(EventHandler)
56         eh.UndoStack = new(Stack)
57         eh.RedoStack = new(Stack)
58         eh.buf = buf
59         return eh
60 }
61
62 // ApplyDiff takes a string and runs the necessary insertion and deletion events to make
63 // the buffer equal to that string
64 // This means that we can transform the buffer into any string and still preserve undo/redo
65 // through insert and delete events
66 func (eh *EventHandler) ApplyDiff(new string) {
67         differ := dmp.New()
68         diff := differ.DiffMain(eh.buf.String(), new, false)
69         loc := eh.buf.Start()
70         for _, d := range diff {
71                 if d.Type == dmp.DiffDelete {
72                         eh.Remove(loc, loc.Move(Count(d.Text), eh.buf))
73                 } else {
74                         if d.Type == dmp.DiffInsert {
75                                 eh.Insert(loc, d.Text)
76                         }
77                         loc = loc.Move(Count(d.Text), eh.buf)
78                 }
79         }
80 }
81
82 // Insert creates an insert text event and executes it
83 func (eh *EventHandler) Insert(start Loc, text string) {
84         e := &TextEvent{
85                 C:         eh.buf.Cursor,
86                 EventType: TextEventInsert,
87                 Text:      text,
88                 Start:     start,
89                 Time:      time.Now(),
90         }
91         eh.Execute(e)
92         e.End = start.Move(Count(text), eh.buf)
93 }
94
95 // Remove creates a remove text event and executes it
96 func (eh *EventHandler) Remove(start, end Loc) {
97         e := &TextEvent{
98                 C:         eh.buf.Cursor,
99                 EventType: TextEventRemove,
100                 Start:     start,
101                 End:       end,
102                 Time:      time.Now(),
103         }
104         eh.Execute(e)
105 }
106
107 // Replace deletes from start to end and replaces it with the given string
108 func (eh *EventHandler) Replace(start, end Loc, replace string) {
109         eh.Remove(start, end)
110         eh.Insert(start, replace)
111 }
112
113 // Execute a textevent and add it to the undo stack
114 func (eh *EventHandler) Execute(t *TextEvent) {
115         if eh.RedoStack.Len() > 0 {
116                 eh.RedoStack = new(Stack)
117         }
118         eh.UndoStack.Push(t)
119
120         for _, pl := range loadedPlugins {
121                 ret, err := Call(pl+".onBeforeTextEvent", t)
122                 if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
123                         TermMessage(err)
124                 }
125                 if val, ok := ret.(lua.LBool); ok && val == lua.LFalse {
126                         return
127                 }
128         }
129
130         ExecuteTextEvent(t, eh.buf)
131 }
132
133 // Undo the first event in the undo stack
134 func (eh *EventHandler) Undo() {
135         t := eh.UndoStack.Peek()
136         if t == nil {
137                 return
138         }
139
140         startTime := t.Time.UnixNano() / int64(time.Millisecond)
141
142         eh.UndoOneEvent()
143
144         for {
145                 t = eh.UndoStack.Peek()
146                 if t == nil {
147                         return
148                 }
149
150                 if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
151                         return
152                 }
153                 startTime = t.Time.UnixNano() / int64(time.Millisecond)
154
155                 eh.UndoOneEvent()
156         }
157 }
158
159 // UndoOneEvent undoes one event
160 func (eh *EventHandler) UndoOneEvent() {
161         // This event should be undone
162         // Pop it off the stack
163         t := eh.UndoStack.Pop()
164         if t == nil {
165                 return
166         }
167
168         // Undo it
169         // Modifies the text event
170         UndoTextEvent(t, eh.buf)
171
172         // Set the cursor in the right place
173         teCursor := t.C
174         t.C = eh.buf.Cursor
175         eh.buf.Cursor.Goto(teCursor)
176
177         // Push it to the redo stack
178         eh.RedoStack.Push(t)
179 }
180
181 // Redo the first event in the redo stack
182 func (eh *EventHandler) Redo() {
183         t := eh.RedoStack.Peek()
184         if t == nil {
185                 return
186         }
187
188         startTime := t.Time.UnixNano() / int64(time.Millisecond)
189
190         eh.RedoOneEvent()
191
192         for {
193                 t = eh.RedoStack.Peek()
194                 if t == nil {
195                         return
196                 }
197
198                 if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
199                         return
200                 }
201
202                 eh.RedoOneEvent()
203         }
204 }
205
206 // RedoOneEvent redoes one event
207 func (eh *EventHandler) RedoOneEvent() {
208         t := eh.RedoStack.Pop()
209         if t == nil {
210                 return
211         }
212
213         // Modifies the text event
214         UndoTextEvent(t, eh.buf)
215
216         teCursor := t.C
217         t.C = eh.buf.Cursor
218         eh.buf.Cursor.Goto(teCursor)
219
220         eh.UndoStack.Push(t)
221 }