8 // TextEventInsert repreasents an insertion event
10 // TextEventRemove represents a deletion event
14 // TextEvent holds data for a manipulation on some text that can be undone
15 type TextEvent struct {
26 // ExecuteTextEvent runs a text event
27 func ExecuteTextEvent(t *TextEvent) {
28 if t.eventType == TextEventInsert {
29 t.buf.Insert(t.start, t.text)
30 } else if t.eventType == TextEventRemove {
31 t.text = t.buf.Remove(t.start, t.end)
35 // UndoTextEvent undoes a text event
36 func UndoTextEvent(t *TextEvent) {
37 t.eventType = -t.eventType
41 // EventHandler executes text manipulations and allows undoing and redoing
42 type EventHandler struct {
48 // NewEventHandler returns a new EventHandler
49 func NewEventHandler(v *View) *EventHandler {
50 eh := new(EventHandler)
57 // Insert creates an insert text event and executes it
58 func (eh *EventHandler) Insert(start int, text string) {
61 eventType: TextEventInsert,
64 end: start + Count(text),
71 // Remove creates a remove text event and executes it
72 func (eh *EventHandler) Remove(start, end int) {
75 eventType: TextEventRemove,
84 // Execute a textevent and add it to the undo stack
85 func (eh *EventHandler) Execute(t *TextEvent) {
86 if eh.redo.Len() > 0 {
93 // Undo the first event in the undo stack
94 func (eh *EventHandler) Undo() {
101 // Modifies the text event
106 eh.v.cursor = teCursor
111 // Redo the first event in the redo stack
112 func (eh *EventHandler) Redo() {
119 // Modifies the text event
124 eh.v.cursor = teCursor