-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathundo.go
More file actions
257 lines (243 loc) · 5.87 KB
/
undo.go
File metadata and controls
257 lines (243 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"strings"
glisp "github.com/glycerine/zygomys/zygo"
)
type EditorUndo struct {
ins bool
region bool
startl int
endl int
startc int
endc int
str string
prev *EditorUndo
paired bool
}
// Discards the last undo. Useful for the region functions, as they're made of
// regular insertion functions (which take care of their own undo)
func editorPopUndo() {
old := Global.CurrentB.Undo
if old == nil {
return
}
Global.CurrentB.Undo = old.prev
}
func editorAddRegionUndo(ins bool, startc, endc, startl, endl int, str string) {
old := Global.CurrentB.Undo
ret := new(EditorUndo)
ret.endl = endl
ret.startl = startl
ret.endc = endc
ret.startc = startc
ret.str = str
ret.ins = ins
ret.region = true
if old == nil {
ret.prev = nil
} else {
ret.prev = old
}
Global.CurrentB.Undo = ret
Global.CurrentB.Redo = nil
}
func editorAddInsertUndo(startc, startl int, str string) {
old := Global.CurrentB.Undo
newlines := strings.Count(str, "\n")
lastnl := 0
if 0 < newlines {
lastnl = strings.LastIndex(str, "\n") + 1
}
if old != nil && old != Global.CurrentB.SaveUndo &&
old.ins && old.endc == startc && old.endl == startl {
old.str += str
old.endl += newlines
if newlines <= 0 {
old.endc += len(str)
} else {
old.endc = len(str[lastnl:])
}
old.region = old.region || newlines > 0
} else {
ret := new(EditorUndo)
ret.startl = startl
ret.startc = startc
ret.str = str
ret.ins = true
ret.region = newlines > 0
if newlines == 0 {
ret.endl = startl
ret.endc = startc + len(str)
} else {
ret.endl = startl + newlines
ret.endc = len(str[lastnl:])
}
ret.prev = old
Global.CurrentB.Undo = ret
}
Global.CurrentB.Redo = nil
}
func editorAddDeleteUndo(startc, endc, startl, endl int, str string) {
old := Global.CurrentB.Undo
ins := false
app := false
if old != nil {
app = old.startl == startl && old.endl == endl && old.ins == ins && old != Global.CurrentB.SaveUndo
if app {
if ins {
app = old.endc == startc
} else {
app = old.startc == endc
}
}
}
if app {
Global.CurrentB.Redo = nil
//append to group things together, ala gnu
if ins {
old.str += str
old.endc = endc
} else {
old.str = str + old.str
old.startc = startc
}
} else {
ret := new(EditorUndo)
ret.endl = endl
ret.startl = startl
ret.endc = endc
ret.startc = startc
ret.str = str
ret.ins = ins
ret.region = false
if old == nil {
ret.prev = nil
} else {
ret.prev = old
}
Global.CurrentB.Undo = ret
Global.CurrentB.Redo = nil
}
}
func editorDoUndo(tree *EditorUndo) bool {
if tree == nil {
return false
}
if tree.region {
if tree.ins {
bufKillRegion(Global.CurrentB, tree.startc, tree.endc, tree.startl, tree.endl)
} else {
spitRegion(tree.startc, tree.startl, tree.str)
}
return true
}
if tree.ins {
// Insertion
if tree.startl == tree.endl {
// Basic string insertion
editorRowDelChar(Global.CurrentB.Rows[tree.startl], Global.CurrentB,
tree.startc, len(tree.str))
Global.CurrentB.cx = tree.startc
Global.CurrentB.cy = tree.startl
return true
} else {
// inserting a line
Global.CurrentB.cx = tree.startc
Global.CurrentB.cy = tree.startl
editorRowAppendStr(Global.CurrentB.Rows[tree.startl], Global.CurrentB, tree.str)
editorDelRow(tree.endl)
return true
}
} else {
// Deletion
if tree.startl == tree.endl {
// Character or word deletion
editorRowInsertStr(Global.CurrentB.Rows[tree.startl], Global.CurrentB,
tree.startc, tree.str)
Global.CurrentB.cx = tree.endc
Global.CurrentB.cy = tree.startl
return true
} else {
// deleting a line
editorInsertRow(tree.startl, Global.CurrentB.Rows[tree.startl].Data[:tree.endc])
row := Global.CurrentB.Rows[tree.endl]
row.Data = tree.str
row.Size = len(row.Data)
Global.CurrentB.Rows[tree.startl].Size = len(Global.CurrentB.Rows[tree.startl].Data)
editorUpdateRow(row, Global.CurrentB)
editorUpdateRow(Global.CurrentB.Rows[tree.startl], Global.CurrentB)
return true
}
}
}
func editorDoRedo(tree *EditorUndo) {
if tree.region {
if tree.ins {
spitRegion(tree.startc, tree.startl, tree.str)
} else {
bufKillRegion(Global.CurrentB, tree.startc, tree.endc, tree.startl, tree.endl)
}
return
}
if tree.ins {
if tree.startl == tree.endl {
spitRegion(tree.startc, tree.startl, tree.str)
} else if tree.startl == -1 {
editorAppendRow(tree.str)
Global.CurrentB.cx = tree.endc
Global.CurrentB.cy = Global.CurrentB.NumRows - 1
} else {
Global.CurrentB.cx = tree.startc
Global.CurrentB.cy = tree.startl
editorInsertNewline(false)
editorPopUndo()
}
} else {
if tree.startl == tree.endl {
bufKillRegion(Global.CurrentB, tree.startc, tree.endc, tree.startl, tree.endl)
} else {
Global.CurrentB.cy = tree.endl
Global.CurrentB.cx = 0
editorDelChar()
editorPopUndo()
}
}
}
func editorUndoAction() {
r := Global.CurrentB.Redo
succ := editorDoUndo(Global.CurrentB.Undo)
paired := Global.CurrentB.Undo != nil && Global.CurrentB.Undo.paired
if succ {
Global.CurrentB.Redo = Global.CurrentB.Undo
Global.CurrentB.Undo = Global.CurrentB.Undo.prev
Global.CurrentB.Redo.prev = r
if paired {
editorUndoAction()
}
} else {
Global.Input = "No further undo information."
}
if Global.CurrentB.Undo == Global.CurrentB.SaveUndo {
Global.CurrentB.Dirty = false
}
}
func doOneRedo(env *glisp.Zlisp) {
if Global.CurrentB.Redo == nil {
Global.Input = "No further redo information."
} else {
r := Global.CurrentB.Redo
editorDoRedo(Global.CurrentB.Redo)
Global.CurrentB.Redo = r.prev
r.prev = Global.CurrentB.Undo
Global.CurrentB.Undo = r
if r == Global.CurrentB.SaveUndo {
Global.CurrentB.Dirty = false
}
if Global.CurrentB.Redo != nil && Global.CurrentB.Redo.paired {
doOneRedo(env)
}
}
}
func editorRedoAction(env *glisp.Zlisp) {
micromode("C-_", "Press C-_ or C-/ to redo again", env, doOneRedo)
}