/
buffer.go
331 lines (298 loc) · 6.1 KB
/
buffer.go
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2011 David Forsythe.
* See LICENSE file for license details.
*/
package main
import (
"bufio"
"fmt"
"os"
"utf8"
)
type Line struct {
text []byte // string
displen uint
next *Line
prev *Line
dirty bool
// attributes
}
func (l *Line) length() int {
return utf8.RuneCount(l.text)
}
func NewLine(text []byte) *Line {
dst := make([]byte, len(text))
copy(dst, text)
return &Line{
text: dst,
next: nil,
prev: nil,
}
}
const (
ADD = iota
REMOVE
)
type Action struct {
op int // the action that happened, do the opposite to undo
s, e *Position
text []byte
char *Action // multi op actions?
}
type Buffer struct {
name string
dirty bool
temp bool
first, last, disp *Line
pos *Position
line bool // line mode buffer, for yank/put
next, prev *Buffer
db []*Line
ro bool
}
func newBuffer() *Buffer {
l := NewLine([]byte{})
return &Buffer{
first: l,
last: l,
pos: &Position{
line: l,
off: 0,
},
}
}
func (b *Buffer) resetPos() {
b.pos.line = b.first
b.pos.off = 0
}
func (b *Buffer) rename(name string) {
b.name = name
}
func insertLineBelow(l, n *Line) {
if l.next != nil {
l.next.prev = n
n.next = l.next
}
n.prev = l
l.next = n
}
func insertLineAbove(l, n *Line) {
if l.prev != nil {
l.prev.next = n
n.prev = l.prev
}
n.next = l
l.prev = n
}
func (b *Buffer) insertLineAbove(l, n *Line) {
insertLineAbove(l, n)
if l == b.first {
b.first = n
}
}
func (b *Buffer) insertLineBelow(l, n *Line) {
insertLineBelow(l, n)
if l == b.last {
b.last = n
}
}
func (b *Buffer) add(p Position, text []byte) *Position {
for _, c := range text {
if c != '\n' && c != '\r' {
l := p.line
l.text = append(l.text[:p.off], append([]byte{c}, l.text[p.off:]...)...)
p.off++
} else {
linetext := p.line.text
l := NewLine(linetext[p.off:])
p.line.text = linetext[:p.off]
/*
if p.line.next != nil {
p.line.next.prev = l
l.next = p.line.next
}
l.prev = p.line
p.line.next = l
*/
b.insertLineBelow(p.line, l)
p.line = l
p.off = 0
}
}
return &p
}
func (b *Buffer) lineCount() int {
i := 0
for l := b.first; l != b.last.next; l = l.next {
i++
}
return i
}
func (b *Buffer) lineNumber(l *Line) int {
for i, bl := 1, b.first; bl != nil && l != nil; i, bl = i+1, bl.next {
if bl == l {
return i
}
}
return -1
}
func (b *Buffer) getLine(lno int) *Line {
l := b.first
for i := 1; i < lno; i, l = i+1, l.next {
if l.next == nil || l == b.last {
break
}
}
return l
}
func (b *Buffer) remove(start, end Position, line bool) {
// XXX This function returns b.pos. It should actually just return the first safe
// position after (or before) the removed chunk.
if start.line == end.line && !line {
start.line.text = append(start.line.text[:start.off], start.line.text[end.off:]...)
/*return &Position{
line: start.line,
off: start.off,
}*/
} else {
if !line {
// If we aren't in line mode, check if only the 0th char is marked by end.
// If it is, move back to the end of the prev, because we don't actually
// want to delete the line.
if end.off == 0 && end.line.length() > 0 {
end.line = end.line.prev
end.off = end.line.length()
}
for l := start.line; l != end.line; l = l.next {
start.line.next = l.next
start.line.next.prev = start.line
}
start.line.text = append(start.line.text[:start.off], end.line.text[end.off:]...)
if end.line.next != nil {
end.line.next.prev = start.line
}
start.line.next = end.line.next
} else {
for l := start.line; l != end.line.next; l = l.next {
if l != b.first /* l.prev != nil */ {
l.prev.next = l.next
} else {
b.first = l.next
}
if l != b.last /* l.next != nil */ {
l.next.prev = l.prev
} else {
b.last = l.prev
}
if b.disp == l {
if l.next != nil {
b.disp = l.next
} else {
b.disp = l.prev
}
}
}
// we deleted the entire file
if b.first == nil {
b.first = NewLine([]byte{})
b.last = b.first
b.disp = b.first
b.pos.line = b.first
b.pos.off = 0
} else if !b.contains(b.pos.line) {
b.pos.line = b.first
b.pos.off = 0
}
}
}
// return b.pos
}
func (b *Buffer) removeLine(l *Line) *Line {
return nil
}
func (b *Buffer) contains(l *Line) bool {
for c := b.first; c != b.last.next; c = c.next {
if l == c {
return true
}
}
return false
}
func get(a, b *Position) []byte {
// doesn't need to be on a buffer
text := []byte{}
for l, s := a.line, a.off; l != b.line.next; l, s = l.next, 0 {
if l == b.line {
text = append(text, l.text[s:b.off]...)
} else {
text = append(text, l.text[s:]...)
if l.next != nil {
text = append(text, '\n')
}
}
}
return text
}
func (b *Buffer) getAll() []byte {
return get(&Position{b.first, 0}, &Position{b.last, b.last.length()})
}
func (b *Buffer) clear() {
b.first = NewLine([]byte{})
b.last = b.first
}
func (b *Buffer) loadFile(f *os.File) os.Error {
rdr := bufio.NewReader(f)
buf := make([]byte, 4096)
var e os.Error
var n int
for n, e = rdr.Read(buf); n > 0 && e == nil; n, e = rdr.Read(buf) {
b.pos = b.add(*b.pos, buf[:n])
}
if e != os.EOF {
return e
}
if b.last.length() == 0 && b.last != b.first {
p := b.last.prev
p.next = nil
b.last.prev = nil
b.last = p
}
return nil
}
func (b *Buffer) writeFile() os.Error {
wf, e := os.Create(b.name)
if e != nil {
return e
}
defer wf.Close()
for l := b.first; l != nil; l = l.next {
if _, e := wf.Write(append(l.text, '\n')); e != nil {
return e
}
}
return nil
}
func (b *Buffer) information() string {
var name string
var ro string
var modified string
if len(b.name) > 0 {
name = b.name
} else {
name = "no pathname"
}
// a buffer shouldn't have less than 1 line
lno := b.lineNumber(b.pos.line)
tln := b.lineNumber(b.last)
if b.ro {
ro = "readonly"
} else {
ro = "writeable"
}
if b.dirty {
modified = "modified"
} else {
modified = "unmodified"
}
return fmt.Sprintf("%s: %s: line %d of %d: %s", name, modified, lno, tln, ro)
}