-
Notifications
You must be signed in to change notification settings - Fork 77
/
buffer.go
317 lines (273 loc) · 5.79 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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"unicode/utf8"
)
//----------------------------------------------------------------------------
// line
//----------------------------------------------------------------------------
type line struct {
data []byte
next *line
prev *line
}
// Find a set of closest offsets for a given visual offset
func (l *line) find_closest_offsets(voffset int) (bo, co, vo int) {
data := l.data
for len(data) > 0 {
var vodif int
r, rlen := utf8.DecodeRune(data)
data = data[rlen:]
vodif = rune_advance_len(r, vo)
if vo+vodif > voffset {
return
}
bo += rlen
co += 1
vo += vodif
}
return
}
//----------------------------------------------------------------------------
// buffer
//----------------------------------------------------------------------------
type buffer struct {
views []*view
first_line *line
last_line *line
loc view_location
lines_n int
bytes_n int
history *action_group
on_disk *action_group
mark cursor_location
// absoulte path of the file, if it's empty string, then the file has no
// on-disk representation
path string
// buffer name (displayed in the status line), must be unique,
// uniqueness is maintained by godit methods
name string
// cache for local buffer autocompletion
words_cache llrb_tree
words_cache_valid bool
}
func new_empty_buffer() *buffer {
b := new(buffer)
l := new(line)
l.next = nil
l.prev = nil
b.first_line = l
b.last_line = l
b.lines_n = 1
b.loc = view_location{
top_line: l,
top_line_num: 1,
cursor: cursor_location{
line: l,
line_num: 1,
},
}
b.init_history()
return b
}
func new_buffer(r io.Reader) (*buffer, error) {
var err error
var prevline *line
br := bufio.NewReader(r)
l := new(line)
b := new(buffer)
b.loc = view_location{
top_line: l,
top_line_num: 1,
cursor: cursor_location{
line: l,
line_num: 1,
},
}
b.lines_n = 1
b.first_line = l
for {
l.data, err = br.ReadBytes('\n')
if err != nil {
// last line was read
break
} else {
b.bytes_n += len(l.data)
// cut off the '\n' character
l.data = l.data[:len(l.data)-1]
}
b.lines_n++
l.next = new(line)
l.prev = prevline
prevline = l
l = l.next
}
l.prev = prevline
b.last_line = l
// io.EOF is not an error
if err == io.EOF {
err = nil
}
// history
b.init_history()
return b, err
}
func (b *buffer) add_view(v *view) {
b.views = append(b.views, v)
}
func (b *buffer) delete_view(v *view) {
vi := -1
for i, n := 0, len(b.views); i < n; i++ {
if b.views[i] == v {
vi = i
break
}
}
if vi != -1 {
lasti := len(b.views) - 1
b.views[vi], b.views[lasti] = b.views[lasti], b.views[vi]
b.views = b.views[:lasti]
}
}
func (b *buffer) other_views(v *view, cb func(*view)) {
for _, ov := range b.views {
if v == ov {
continue
}
cb(ov)
}
}
func (b *buffer) init_history() {
// the trick here is that I set 'sentinel' as 'history', it is required
// to maintain an invariant, where 'history' is a sentinel or is not
// empty
sentinel := new(action_group)
first := new(action_group)
sentinel.next = first
first.prev = sentinel
b.history = sentinel
b.on_disk = sentinel
}
func (b *buffer) is_mark_set() bool {
return b.mark.line != nil
}
func (b *buffer) dump_history() {
cur := b.history
for cur.prev != nil {
cur = cur.prev
}
p := func(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
}
i := 0
for cur != nil {
p("action group %d: %d actions\n", i, len(cur.actions))
for _, a := range cur.actions {
switch a.what {
case action_insert:
p(" + insert")
case action_delete:
p(" - delete")
}
p(" (%2d,%2d):%q\n", a.cursor.line_num,
a.cursor.boffset, string(a.data))
}
cur = cur.next
i++
}
}
func (b *buffer) save() error {
return b.save_as(b.path)
}
func (b *buffer) save_as(filename string) error {
r := b.reader()
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, r)
if err != nil {
return err
}
b.on_disk = b.history
for _, v := range b.views {
v.dirty |= dirty_status
}
return nil
}
func (b *buffer) synced_with_disk() bool {
return b.on_disk == b.history
}
func (b *buffer) reader() *buffer_reader {
return new_buffer_reader(b)
}
func (b *buffer) contents() []byte {
data, _ := ioutil.ReadAll(b.reader())
return data
}
func (b *buffer) refill_words_cache() {
b.words_cache.clear()
line := b.first_line
for line != nil {
iter_words(line.data, func(word []byte) {
b.words_cache.insert_maybe(word)
})
line = line.next
}
}
func (b *buffer) update_words_cache() {
if b.words_cache_valid {
return
}
b.refill_words_cache()
b.words_cache_valid = true
}
//----------------------------------------------------------------------------
// buffer_reader
//----------------------------------------------------------------------------
type buffer_reader struct {
buffer *buffer
line *line
offset int
}
func new_buffer_reader(buffer *buffer) *buffer_reader {
br := new(buffer_reader)
br.buffer = buffer
br.line = buffer.first_line
br.offset = 0
return br
}
func (br *buffer_reader) Read(data []byte) (int, error) {
nread := 0
for len(data) > 0 {
if br.line == nil {
return nread, io.EOF
}
// how much can we read from current line
can_read := len(br.line.data) - br.offset
if len(data) <= can_read {
// if this is all we need, return
n := copy(data, br.line.data[br.offset:])
nread += n
br.offset += n
break
}
// otherwise try to read '\n' and jump to the next line
n := copy(data, br.line.data[br.offset:])
nread += n
data = data[n:]
if len(data) > 0 && br.line != br.buffer.last_line {
data[0] = '\n'
data = data[1:]
nread++
}
br.line = br.line.next
br.offset = 0
}
return nread, nil
}