]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/eventhandler.go
Rename to tabstospaces for consistency
[micro.git] / cmd / micro / eventhandler.go
index c54319eb437662637ca69991b5dfbca519f9765f..2c5076d1d2d1d9ffc5f657ba600f069420b3b51b 100644 (file)
@@ -21,50 +21,48 @@ type TextEvent struct {
        text      string
        start     int
        end       int
-       buf       *Buffer
        time      time.Time
 }
 
 // ExecuteTextEvent runs a text event
-func ExecuteTextEvent(t *TextEvent) {
+func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
        if t.eventType == TextEventInsert {
-               t.buf.Insert(t.start, t.text)
+               buf.insert(t.start, t.text)
        } else if t.eventType == TextEventRemove {
-               t.text = t.buf.Remove(t.start, t.end)
+               t.text = buf.remove(t.start, t.end)
        }
 }
 
 // UndoTextEvent undoes a text event
-func UndoTextEvent(t *TextEvent) {
+func UndoTextEvent(t *TextEvent, buf *Buffer) {
        t.eventType = -t.eventType
-       ExecuteTextEvent(t)
+       ExecuteTextEvent(t, buf)
 }
 
 // EventHandler executes text manipulations and allows undoing and redoing
 type EventHandler struct {
-       v    *View
+       buf  *Buffer
        undo *Stack
        redo *Stack
 }
 
 // NewEventHandler returns a new EventHandler
-func NewEventHandler(v *View) *EventHandler {
+func NewEventHandler(buf *Buffer) *EventHandler {
        eh := new(EventHandler)
        eh.undo = new(Stack)
        eh.redo = new(Stack)
-       eh.v = v
+       eh.buf = buf
        return eh
 }
 
 // Insert creates an insert text event and executes it
 func (eh *EventHandler) Insert(start int, text string) {
        e := &TextEvent{
-               c:         eh.v.cursor,
+               c:         eh.buf.Cursor,
                eventType: TextEventInsert,
                text:      text,
                start:     start,
                end:       start + Count(text),
-               buf:       eh.v.buf,
                time:      time.Now(),
        }
        eh.Execute(e)
@@ -73,11 +71,10 @@ func (eh *EventHandler) Insert(start int, text string) {
 // Remove creates a remove text event and executes it
 func (eh *EventHandler) Remove(start, end int) {
        e := &TextEvent{
-               c:         eh.v.cursor,
+               c:         eh.buf.Cursor,
                eventType: TextEventRemove,
                start:     start,
                end:       end,
-               buf:       eh.v.buf,
                time:      time.Now(),
        }
        eh.Execute(e)
@@ -95,7 +92,7 @@ func (eh *EventHandler) Execute(t *TextEvent) {
                eh.redo = new(Stack)
        }
        eh.undo.Push(t)
-       ExecuteTextEvent(t)
+       ExecuteTextEvent(t, eh.buf)
 }
 
 // Undo the first event in the undo stack
@@ -138,12 +135,12 @@ func (eh *EventHandler) UndoOneEvent() {
        te := t.(*TextEvent)
        // Undo it
        // Modifies the text event
-       UndoTextEvent(te)
+       UndoTextEvent(te, eh.buf)
 
        // Set the cursor in the right place
        teCursor := te.c
-       te.c = eh.v.cursor
-       eh.v.cursor = teCursor
+       te.c = eh.buf.Cursor
+       eh.buf.Cursor = teCursor
 
        // Push it to the redo stack
        eh.redo.Push(te)
@@ -186,11 +183,11 @@ func (eh *EventHandler) RedoOneEvent() {
 
        te := t.(*TextEvent)
        // Modifies the text event
-       UndoTextEvent(te)
+       UndoTextEvent(te, eh.buf)
 
        teCursor := te.c
-       te.c = eh.v.cursor
-       eh.v.cursor = teCursor
+       te.c = eh.buf.Cursor
+       eh.buf.Cursor = teCursor
 
        eh.undo.Push(te)
 }