forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
554 lines (499 loc) · 16.2 KB
/
file.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"fmt"
"github.com/rjkroege/edwood/internal/elog"
"github.com/rjkroege/edwood/internal/util"
)
// File is an editable text buffer with undo. Many Text can share one
// File (to implement Zerox). The File is responsible for updating the
// Text instances. File is a model in MVC parlance while Text is a
// View-Controller.
//
// A File tracks several related concepts. First it is a text buffer with
// undo/redo back to an initial state. Mark (undo.RuneArray.Commit) notes
// an undo point.
//
// Next, a File might have a backing to a disk file.
//
// Lastly the text buffer might be clean/dirty. A clean buffer is possibly
// the same as its disk backing. A specific point in the undo record is
// considered clean.
//
// TODO(rjk): File will be a facade pattern composing an undo.RuneArray
// and a wrapping utf8string.String indexing wrapper.
// TODO(rjk): my version of undo.RuneArray will implement Reader, Writer,
// RuneReader, Seeker and I will restructure this code to follow the
// patterns of the Go I/O libraries. I will probably want to provide a cache
// around undo.RuneArray.
// Observe: Character motion routines in Text can be written
// in terms of any object that is Seeker and RuneReader.
// Observe: Frame can report addresses in byte and rune offsets.
type File struct {
b RuneArray
delta []*Undo // [private]
epsilon []*Undo // [private]
oeb *ObservableEditableBuffer
// Tracks the Edit sequence.
seq int // undo sequencing [private]
putseq int // seq on last put [private]
mod bool // true if the file has been changed. [private]
treatasclean bool // Window Clean tests should succeed if set. [private]
isdir bool // Used to track if this File is populated from a directory list. [private]
// cache holds that are not yet part of an undo record.
cache []rune // [private]
// cq0 tracks the insertion point for the cache.
cq0 int // [private]
}
// Remember that the high-level goal is to slowly coerce this into looking like
// a scrawny wrapper around the Undo implementation. As a result, we should
// expect to see the following entry points:
//
// func (b *RuneArray) Clean()
//func (b *RuneArray) Commit()
//func (b *RuneArray) Delete(off, length int64) error
//func (b *RuneArray) Dirty() bool
//func (b *RuneArray) Insert(off int64, data []byte) error
//func (b *RuneArray) ReadAt(data []byte, off int64) (n int, err error)
//func (b *RuneArray) Redo() (off, n int64)
//func (b *RuneArray) Size() int64
//func (b *RuneArray) Undo() (off, n int64)
//
// NB how the cache is folded into RuneArray.
//TODO(rjk): make undo.RuneArray implement Reader and Writer.
// HasUncommitedChanges returns true if there are changes that
// have been made to the File after the last Commit.
func (t *File) HasUncommitedChanges() bool {
return len(t.cache) != 0
}
// HasUndoableChanges returns true if there are changes to the File
// that can be undone.
// Has no analog in buffer.Undo. It will require modification.
func (f *File) HasUndoableChanges() bool {
return len(f.delta) > 0 || len(f.cache) != 0
}
// HasRedoableChanges returns true if there are entries in the Redo
// log that can be redone.
// Has no analog in buffer.Undo. It will require modification.
func (f *File) HasRedoableChanges() bool {
return len(f.epsilon) > 0
}
// IsDirOrScratch returns true if the File has a synthetic backing of
// a directory listing or has a name pattern that excludes it from
// being saved under typical circumstances.
func (f *File) IsDirOrScratch() bool {
return f.oeb.isscratch || f.isdir
}
// IsDir returns true if the File has a synthetic backing of
// a directory.
// TODO(rjk): File is a facade that subsumes the entire Model
// of an Edwood MVC. As such, it should look like a text buffer for
// view/controller code. isdir is true for a specific kind of File innards
// where we automatically alter the contents in various ways.
// Automatically altering the contents should be expressed differently.
// Directory listings should not be special cased throughout.
func (f *File) IsDir() bool {
return f.isdir
}
// SetDir updates the setting of the isdir flag.
func (f *File) SetDir(flag bool) {
f.isdir = flag
}
// Size returns the complete size of the buffer including both committed
// and uncommitted runes.
// NB: naturally forwards to undo.RuneArray.Size()
// TODO(rjk): Switch all callers to Nr() as would be the number of
// bytes when backed by undo.RuneArray.
func (f *File) Size() int {
return int(f.b.nc()) + len(f.cache)
}
// Nr returns the number of valid runes in the RuneArray.
// At the moment, this is the same as Size. But when File is backed
// with utf8, this will require adjustment.
// TODO(rjk): utf8 adjustment
func (f *File) Nr() int {
return f.Size()
}
// ReadC reads a single rune from the File.
// Can be easily converted to being utf8 backed but
// every caller will require adjustment.
// TODO(rjk): File needs to implement RuneReader and code should
// use that interface instead.
// TODO(rjk): Better name to align with utf8string.String.At().
func (f *File) ReadC(q int) rune {
if f.cq0 <= q && q < f.cq0+len(f.cache) {
return f.cache[q-f.cq0]
}
return f.b.ReadC(q)
}
// ReadAtRune reads at most len(r) runes from File at rune off.
// It returns the number of runes read and an error if something goes wrong.
func (f *File) ReadAtRune(r []rune, off int) (n int, err error) {
// TODO(rjk): This should include cache contents but currently
// callers do not require it to.
return f.b.Read(off, r)
}
// SaveableAndDirty returns true if the File's contents differ from the
// backing diskfile File.name, and the diskfile is plausibly writable
// (not a directory or scratch file).
//
// When this is true, the tag's button should
// be drawn in the modified state if appropriate to the window type
// and Edit commands should treat the file as modified.
//
// TODO(rjk): figure out how this overlaps with hash. (hash would appear
// to be used to determine the "if the contents differ")
//
// Latest thought: there are two separate issues: are we at a point marked
// as clean and is this File writable to a backing. They are combined in this
// this method.
func (f *File) SaveableAndDirty() bool {
return (f.mod || f.Dirty() || len(f.cache) > 0) && !f.IsDirOrScratch()
}
// Commit writes the in-progress edits to the real buffer instead of
// keeping them in the cache. Does not map to undo.RuneArray.Commit (that
// method is Mark). Remove this method.
func (f *File) Commit() {
f.treatasclean = false
if !f.HasUncommitedChanges() {
return
}
if f.cq0 > f.b.nc() {
// TODO(rjk): Generate a better error message.
panic("internal error: File.Commit")
}
if f.seq > 0 {
f.Uninsert(&f.delta, f.cq0, len(f.cache))
}
f.b.Insert(f.cq0, f.cache)
if len(f.cache) != 0 {
f.Modded()
}
f.cache = f.cache[:0]
}
type Undo struct {
t int
mod bool
seq int
p0 int
n int
buf []rune
}
// Load inserts fd's contents into File at location q0. Load will always
// mark the file as modified so follow this up with a call to f.Clean() to
// indicate that the file corresponds to its disk file backing.
// TODO(rjk): hypothesis: we can make this API cleaner: we will only
// compute a hash when the file corresponds to its diskfile right?
// TODO(rjk): Consider renaming InsertAtFromFd or something similar.
// TODO(rjk): Read and insert in chunks.
// TODO(flux): Innefficient to load the file, then copy into the slice,
// but I need the UTF-8 interpretation. I could fix this by using a
// UTF-8 -> []rune reader on top of the os.File instead.
func (f *File) Load(q0 int, d []byte) (n int, hasNulls bool, err error) {
runes, _, hasNulls := cvttorunes(d, len(d))
// Would appear to require a commit operation.
// NB: Runs the observers.
f.InsertAt(q0, runes)
return len(runes), hasNulls, err
}
// SnapshotSeq saves the current seq to putseq. Call this on Put actions.
// TODO(rjk): switching to undo.RuneArray will require removing use of seq
// TODO(rjk): This function maps to undo.RuneArray.Clean()
func (f *File) SnapshotSeq() {
f.putseq = f.seq
}
// Dirty reports whether the current state of the File is different from
// the initial state or from the one at the time of calling Clean.
//
// TODO(rjk): switching to undo.RuneArray will require removing external uses
// of seq.
func (f *File) Dirty() bool {
return f.seq != f.putseq
}
// InsertAt inserts s runes at rune address p0.
// TODO(rjk): run the observers here to simplify the Text code.
// TODO(rjk): In terms of the undo.RuneArray conversion, this correponds
// to undo.RuneArray.Insert.
// NB: At suffix is to correspond to utf8string.String.At().
func (f *File) InsertAt(p0 int, s []rune) {
f.treatasclean = false
if p0 > f.b.nc() {
panic("internal error: fileinsert")
}
if f.seq > 0 {
f.Uninsert(&f.delta, p0, len(s))
}
f.b.Insert(p0, s)
if len(s) != 0 {
f.Modded()
}
f.oeb.inserted(p0, s)
}
// InsertAtWithoutCommit inserts s at p0 without creating
// an undo record.
// TODO(rjk): Remove this as a prelude to converting to undo.RuneArray
// But preserve the cache. Every "small" insert should go into the cache.
// It almost certainly greatly improves performance for a series of single
// character insertions.
func (f *File) InsertAtWithoutCommit(p0 int, s []rune) {
f.treatasclean = false
if p0 > f.b.nc()+len(f.cache) {
panic("File.InsertAtWithoutCommit insertion off the end")
}
if len(f.cache) == 0 {
f.cq0 = p0
} else {
if p0 != f.cq0+len(f.cache) {
// TODO(rjk): actually print something useful here
util.Acmeerror("File.InsertAtWithoutCommit cq0", nil)
}
}
f.cache = append(f.cache, s...)
f.oeb.inserted(p0, s)
}
// Uninsert generates an action record that deletes runes from the File
// to undo an insertion.
func (f *File) Uninsert(delta *[]*Undo, q0, ns int) {
var u Undo
// undo an insertion by deleting
u.t = elog.Delete
u.mod = f.mod
u.seq = f.seq
u.p0 = q0
u.n = ns
*delta = append(*delta, &u)
}
// DeleteAt removes the rune range [p0,p1) from File.
// TODO(rjk): Currently, adds an Undo record. It shouldn't
// TODO(rjk): should map onto undo.RuneArray.Delete
// TODO(rjk): DeleteAt has an implied Commit operation
// that makes it not match with undo.RuneArray.Delete
func (f *File) DeleteAt(p0, p1 int) {
f.treatasclean = false
if !(p0 <= p1 && p0 <= f.b.nc() && p1 <= f.b.nc()) {
util.Acmeerror("internal error: DeleteAt", nil)
}
if len(f.cache) > 0 {
util.Acmeerror("internal error: DeleteAt", nil)
}
if f.seq > 0 {
f.Undelete(&f.delta, p0, p1)
}
f.b.Delete(p0, p1)
// Validate if this is right.
if p1 > p0 {
f.Modded()
}
f.oeb.deleted(p0, p1)
}
// Undelete generates an action record that inserts runes into the File
// to undo a deletion.
func (f *File) Undelete(delta *[]*Undo, p0, p1 int) {
// undo a deletion by inserting
var u Undo
u.t = elog.Insert
u.mod = f.mod
u.seq = f.seq
u.p0 = p0
u.n = p1 - p0
u.buf = make([]rune, u.n)
f.b.Read(p0, u.buf)
*delta = append(*delta, &u)
}
// A File can have a spcific name that permit it to be persisted to disk
// but typically would not be. These two constants are suffixes of File
// names that have this property.
const (
slashguide = "/guide"
plusErrors = "+Errors"
)
// SetName sets the name of the backing for this file.
// Some backings that opt them out of typically being persisted.
// Resetting a file name to a new value does not have any effect.
func (f *File) SetName(name string) {
if f.oeb.Name() == name {
return
}
if f.seq > 0 {
f.UnsetName(&f.delta)
}
f.oeb.Setnameandisscratch(name)
}
func (f *File) UnsetName(delta *[]*Undo) {
var u Undo
// undo a file name change by restoring old name
u.t = elog.Filename
u.mod = f.mod
u.seq = f.seq
u.p0 = 0 // unused
u.n = len(f.oeb.Name())
u.buf = []rune(f.oeb.Name())
*delta = append(*delta, &u)
}
func NewFile() *File {
return &File{
b: NewBuffer(),
delta: []*Undo{},
epsilon: []*Undo{},
// seq int
mod: false,
// ntext int
}
}
func NewTagFile() *File {
return &File{
b: NewBuffer(),
delta: []*Undo{},
epsilon: []*Undo{},
// qidpath uint64
// mtime uint64
// dev int
// seq int
mod: false,
// curtext *Text
// text **Text
// ntext int
}
}
// RedoSeq finds the seq of the last redo record. TODO(rjk): This has no
// analog in undo.RuneArray. The value of seq is used to track intra and
// inter File edit actions so that cross-File changes via Edit X can be
// undone with a single action. An implementation of File that wraps
// undo.RuneArray will need to to preserve seq tracking.
func (f *File) RedoSeq() int {
delta := &f.epsilon
if len(*delta) == 0 {
return 0
}
u := (*delta)[len(*delta)-1]
return u.seq
}
// Seq returns the current value of seq.
func (f *File) Seq() int {
return f.seq
}
// Undo undoes edits if isundo is true or redoes edits if isundo is false.
// It returns the new selection q0, q1 and a bool indicating if the
// returned selection is meaningful.
//
// TODO(rjk): Separate Undo and Redo for better alignment with undo.RuneArray
// TODO(rjk): This Undo implementation may Undo/Redo multiple changes.
// The number actually processed is controlled by mutations to File.seq.
// This does not align with the semantics of undo.RuneArray.
// Each "Mark" needs to have a seq value provided.
// TODO(rjk): Consider providing the target seq value as an argument.
func (f *File) Undo(isundo bool) (q0, q1 int, ok bool) {
var (
stop int
delta, epsilon *[]*Undo
)
if isundo {
// undo; reverse delta onto epsilon, seq decreases
delta = &f.delta
epsilon = &f.epsilon
stop = f.seq
} else {
// redo; reverse epsilon onto delta, seq increases
delta = &f.epsilon
epsilon = &f.delta
stop = 0 // don't know yet
}
for len(*delta) > 0 {
u := (*delta)[len(*delta)-1]
if isundo {
if u.seq < stop {
f.seq = u.seq
return
}
} else {
if stop == 0 {
stop = u.seq
}
if u.seq > stop {
return
}
}
switch u.t {
default:
panic(fmt.Sprintf("undo: 0x%x\n", u.t))
case elog.Delete:
f.seq = u.seq
f.Undelete(epsilon, u.p0, u.p0+u.n)
f.mod = u.mod
f.treatasclean = false
f.b.Delete(u.p0, u.p0+u.n)
f.oeb.deleted(u.p0, u.p0+u.n)
q0 = u.p0
q1 = u.p0
ok = true
case elog.Insert:
f.seq = u.seq
f.Uninsert(epsilon, u.p0, u.n)
f.mod = u.mod
f.treatasclean = false
f.b.Insert(u.p0, u.buf)
f.oeb.inserted(u.p0, u.buf)
q0 = u.p0
q1 = u.p0 + u.n
ok = true
case Filename:
// TODO(rjk): Fix Undo on Filename once the code has matured, removing broken code in the meantime.
// TODO(rjk): If I have a zerox, does undo a filename change update?
f.seq = u.seq
f.UnsetName(epsilon)
f.mod = u.mod
f.treatasclean = false
newfname := string(u.buf)
f.oeb.Setnameandisscratch(newfname)
}
*delta = (*delta)[0 : len(*delta)-1]
}
// TODO(rjk): Why do we do this?
if isundo {
f.seq = 0
}
return q0, q1, ok
}
// Reset removes all Undo records for this File.
// TODO(rjk): This concept doesn't particularly exist in undo.RuneArray.
// Why can't I just create a new File?
func (f *File) Reset() {
f.delta = f.delta[0:0]
f.epsilon = f.epsilon[0:0]
f.seq = 0
}
// Mark sets an Undo point and
// and discards Redo records. Call this at the beginning
// of a set of edits that ought to be undo-able as a unit. This
// is equivalent to undo.RuneArray.Commit()
// NB: current implementation permits calling Mark on an empty
// file to indicate that one can undo to the file state at the time of
// calling Mark.
// TODO(rjk): Consider renaming to SetUndoPoint
// TODO(rjk): Don't pass in seq. (Remove seq entirely?)
func (f *File) Mark(seq int) {
f.epsilon = f.epsilon[0:0]
f.seq = seq
}
// TreatAsDirty returns true if the File should be considered modified
// for the purpose of warning the user if Del-ing a Dirty() file.
func (f *File) TreatAsDirty() bool {
return !f.treatasclean && f.Dirty()
}
// TreatAsClean notes that the File should be considered as not Dirty
// until its next modification.
func (f *File) TreatAsClean() {
f.treatasclean = true
}
// Modded marks the File if we know that its backing is different from
// its contents. This is needed to track when Edwood has modified the
// backing without changing the File (e.g. via the Edit w command.
func (f *File) Modded() {
f.mod = true
f.treatasclean = false
}
// Clean marks File as being non-dirty: the backing is the same as File.
func (f *File) Clean() {
f.mod = false
f.treatasclean = false
f.SnapshotSeq()
}