-
Notifications
You must be signed in to change notification settings - Fork 34
/
wind.go
572 lines (510 loc) · 13.6 KB
/
wind.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package main
import (
"bytes"
"fmt"
"image"
"os"
"path/filepath"
"sync"
"github.com/rjkroege/edwood/draw"
"github.com/rjkroege/edwood/file"
"github.com/rjkroege/edwood/frame"
"github.com/rjkroege/edwood/util"
)
type Window struct {
display draw.Display
lk sync.Mutex
ref Ref
tag Text
body Text
r image.Rectangle
// isdir bool // true if this Window is showing a directory in its body.
filemenu bool
autoindent bool
showdel bool
id int
addr Range
limit Range
nopen [QMAX]byte // number of open Fid for each file in the file server
nomark bool
wrselrange Range
rdselfd *os.File // temporary file for rdsel read requests
col *Column
eventx *Xfid
events []byte
owner int // TODO(fhs): change type to rune
maxlines int
dirnames []string
widths []int
incl []string
ctrllock sync.Mutex // used for lock/unlock ctl mesage
ctlfid uint32 // ctl file Fid which has the ctrllock
dumpstr string
dumpdir string
utflastqid int // Qid of last read request (QWbody or QWtag)
utflastboff uint64 // Byte offset of last read of body or tag
utflastq int // Rune offset of last read of body or tag
tagfilenameend int
tagfilenamechanged bool
tagsetting bool
tagsafe bool // What is tagsafe for?
tagexpand bool
taglines int
tagtop image.Rectangle
editoutlk chan bool
}
var (
_ file.TagStatusObserver = (*Window)(nil) // Enforce at compile time that Window implements BufferObserver
_ file.BufferObserver = (*Window)(nil) // Enforce at compile time that TagIndex implements BufferObserver
)
func NewWindow() *Window {
return &Window{}
}
// Initialize the headless parts of the window.
func (w *Window) initHeadless(clone *Window) *Window {
w.tag.w = w
w.taglines = 1
w.tagsafe = false
w.tagexpand = true
w.body.w = w
w.incl = []string{}
global.WinID++
w.id = global.WinID
w.ref.Inc()
if global.globalincref {
w.ref.Inc()
}
w.ctlfid = MaxFid
w.utflastqid = -1
// Tag setup.
f := file.MakeObservableEditableBuffer("", nil)
if clone != nil {
// TODO(rjk): Support something nicer like initializing from a Reader.
// (Can refactor ObservableEditableBuffer.Load perhaps.
clonebuff := make([]rune, clone.tag.Nc())
clone.tag.file.Read(0, clonebuff)
f = file.MakeObservableEditableBuffer("", clonebuff)
}
f.AddObserver(&w.tag)
// w observes tag to update the tag index.
// TODO(rjk): Add the tag index facility.
f.AddObserver(w)
w.tag.file = f
// Body setup.
f = file.MakeObservableEditableBuffer("", nil)
if clone != nil {
f = clone.body.file
w.body.org = clone.body.org
}
f.AddObserver(&w.body)
w.body.file = f
w.filemenu = true
w.autoindent = *globalAutoIndent
// w observes body to update the tag in response to actions on the body.
f.AddTagStatusObserver(w)
if clone != nil {
w.autoindent = clone.autoindent
}
w.editoutlk = make(chan bool, 1)
return w
}
func (w *Window) Init(clone *Window, r image.Rectangle, dis draw.Display) {
w.initHeadless(clone)
w.display = dis
r1 := r
w.tagtop = r
w.tagtop.Max.Y = r.Min.Y + fontget(global.tagfont, w.display).Height()
r1.Max.Y = r1.Min.Y + w.taglines*fontget(global.tagfont, w.display).Height()
w.tag.Init(r1, global.tagfont, global.tagcolors, w.display)
w.tag.what = Tag
// When cloning, we copy the tag so that the tag contents can evolve
// independently.
if clone != nil {
w.tag.SetSelect(w.tag.Nc(), w.tag.Nc())
}
r1 = r
r1.Min.Y += w.taglines*fontget(global.tagfont, w.display).Height() + 1
if r1.Max.Y < r1.Min.Y {
r1.Max.Y = r1.Min.Y
}
var rf string
if clone != nil {
rf = clone.body.font
} else {
rf = global.tagfont
}
w.body.Init(r1, rf, global.textcolors, w.display)
w.body.what = Body
r1.Min.Y--
r1.Max.Y = r1.Min.Y + 1
if w.display != nil {
w.display.ScreenImage().Draw(r1, global.tagcolors[frame.ColBord], nil, image.Point{})
}
w.body.ScrDraw(w.body.fr.GetFrameFillStatus().Nchars)
w.r = r
var br image.Rectangle
br.Min = w.tag.scrollr.Min
br.Max.X = br.Min.X + global.button.R().Dx()
br.Max.Y = br.Min.Y + global.button.R().Dy()
if w.display != nil {
w.display.ScreenImage().Draw(br, global.button, nil, global.button.R().Min)
}
w.maxlines = w.body.fr.GetFrameFillStatus().Maxlines
if clone != nil {
w.body.SetSelect(clone.body.q0, clone.body.q1)
}
}
func (w *Window) DrawButton() {
b := global.button
if w.body.file.SaveableAndDirty() {
b = global.modbutton
}
var br image.Rectangle
br.Min = w.tag.scrollr.Min
br.Max.X = br.Min.X + b.R().Dx()
br.Max.Y = br.Min.Y + b.R().Dy()
if w.display != nil {
w.display.ScreenImage().Draw(br, b, nil, b.R().Min)
}
}
func (w *Window) delRunePos() int {
i := w.tagfilenameend + 2
if i >= w.tag.Nc() {
return -1
}
return i
}
func (w *Window) moveToDel() {
n := w.delRunePos()
if n < 0 {
return
}
if w.display != nil {
w.display.MoveTo(w.tag.fr.Ptofchar(n).Add(image.Pt(4, w.tag.fr.DefaultFontHeight()-4)))
}
}
// TagLines computes the number of lines in the tag that can fit in r.
func (w *Window) TagLines(r image.Rectangle) int {
if !w.tagexpand && !w.showdel {
return 1
}
w.showdel = false
w.tag.Resize(r, true, true /* noredraw */)
w.tagsafe = false
if !w.tagexpand {
// use just as many lines as needed to show the Del
n := w.delRunePos()
if n < 0 {
return 1
}
p := w.tag.fr.Ptofchar(n).Sub(w.tag.fr.Rect().Min)
return 1 + p.Y/w.tag.fr.DefaultFontHeight()
}
// can't use more than we have
if w.tag.fr.GetFrameFillStatus().Nlines >= w.tag.fr.GetFrameFillStatus().Maxlines {
return w.tag.fr.GetFrameFillStatus().Maxlines
}
// if tag ends with \n, include empty line at end for typing
n := w.tag.fr.GetFrameFillStatus().Nlines
if w.tag.file.Nr() > 0 {
c := w.tag.file.ReadC(w.tag.file.Nr() - 1)
if c == '\n' {
n++
}
}
if n == 0 {
n = 1
}
return n
}
// Resize the specified Window to rectangle r.
// TODO(rjk): when collapsing the tag, this is called twice. Once would seem
// sufficient.
// TODO(rjk): This function does not appear to update the Window's rect correctly
// in all cases.
func (w *Window) Resize(r image.Rectangle, safe, keepextra bool) int {
// log.Printf("Window.Resize r=%v safe=%v keepextra=%v\n", r, safe, keepextra)
// defer log.Println("Window.Resize End\n")
// TODO(rjk): Do not leak global event state into this function.
mouseintag := global.mouse.Point.In(w.tag.all)
mouseinbody := global.mouse.Point.In(w.body.all)
// Tagtop is a rectangle corresponding to one line of tag.
w.tagtop = r
w.tagtop.Max.Y = r.Min.Y + fontget(global.tagfont, w.display).Height()
r1 := r
r1.Max.Y = util.Min(r.Max.Y, r1.Min.Y+w.taglines*fontget(global.tagfont, w.display).Height())
// If needed, recompute number of lines in tag.
if !safe || !w.tagsafe || !w.tag.all.Eq(r1) {
w.taglines = w.TagLines(r)
r1.Max.Y = util.Min(r.Max.Y, r1.Min.Y+w.taglines*fontget(global.tagfont, w.display).Height())
}
// Resize/redraw tag TODO(flux)
y := r1.Max.Y
if !safe || !w.tagsafe || !w.tag.all.Eq(r1) {
w.tag.Resize(r1, true, false /* noredraw */)
y = w.tag.fr.Rect().Max.Y
w.DrawButton()
w.tagsafe = true
// If mouse is in tag, pull up as tag closes.
if mouseintag && !global.mouse.Point.In(w.tag.all) {
p := global.mouse.Point
p.Y = w.tag.all.Max.Y - 3
if w.display != nil {
w.display.MoveTo(p)
}
}
// If mouse is in body, push down as tag expands.
if mouseinbody && global.mouse.Point.In(w.tag.all) {
p := global.mouse.Point
p.Y = w.tag.all.Max.Y + 3
if w.display != nil {
w.display.MoveTo(p)
}
}
}
// Redraw body
r1 = r
r1.Min.Y = y
if !safe || !w.body.all.Eq(r1) {
oy := y
if y+1+w.body.fr.DefaultFontHeight() <= r.Max.Y { // room for one line
r1.Min.Y = y
r1.Max.Y = y + 1
if w.display != nil {
w.display.ScreenImage().Draw(r1, global.tagcolors[frame.ColBord], nil, image.Point{})
}
y++
r1.Min.Y = util.Min(y, r.Max.Y)
r1.Max.Y = r.Max.Y
} else {
r1.Min.Y = y
r1.Max.Y = y
}
y = w.body.Resize(r1, keepextra, false /* noredraw */)
w.r = r
w.r.Max.Y = y
w.body.ScrDraw(w.body.fr.GetFrameFillStatus().Nchars)
w.body.all.Min.Y = oy
}
w.maxlines = util.Min(w.body.fr.GetFrameFillStatus().Nlines, util.Max(w.maxlines, w.body.fr.GetFrameFillStatus().Maxlines))
// TODO(rjk): this value doesn't make sense when we've collapsed
// the tag if the rectangle update block is not executed.
return w.r.Max.Y
}
// Lock1 locks just this Window. This is a helper for Lock.
// TODO(rjk): This should be an internal detail of Window.
func (w *Window) lock1(owner int) {
w.lk.Lock()
w.ref.Inc()
w.owner = owner
}
// Lock locks every text/clone of w
func (w *Window) Lock(owner int) {
w.lk.Lock()
w.ref.Inc()
w.owner = owner
f := w.body.file
f.AllObservers(func(i interface{}) {
if t, ok := i.(*Text); ok && t.w != w {
t.w.lock1(owner)
}
})
}
// unlock1 unlocks a single window.
func (w *Window) unlock1() {
w.owner = 0
w.Close()
w.lk.Unlock()
}
// Unlock releases the lock on each clone of w
func (w *Window) Unlock() {
w.body.file.AllObservers(func(i interface{}) {
if t, ok := i.(*Text); ok && t.w != w {
t.w.unlock1()
}
})
w.unlock1()
}
func (w *Window) MouseBut() {
if w.display != nil {
w.display.MoveTo(w.tag.scrollr.Min.Add(
image.Pt(w.tag.scrollr.Dx(), fontget(global.tagfont, w.display).Height()).Div(2)))
}
}
func (w *Window) Close() {
if w.ref.Dec() == 0 {
xfidlog(w, "del")
w.tag.file.DelObserver(w)
w.body.file.DelTagStatusObserver(w)
w.tag.Close()
w.body.Close()
if global.activewin == w {
global.activewin = nil
}
}
}
func (w *Window) Delete() {
x := w.eventx
if x != nil {
w.events = w.events[0:0]
w.eventx = nil
x.c <- nil // wake him up
}
}
func (w *Window) Undo(isundo bool) {
w.utflastqid = -1
body := &w.body
if q0, q1, ok := body.file.Undo(isundo); ok {
body.q0, body.q1 = q0, q1
}
// TODO(rjk): Updates the scrollbar and selection.
// Be sure not to do this inside of the Undo operation's callbacks.
body.Show(body.q0, body.q1, true)
}
func (w *Window) SetName(name string) {
t := &w.body
t.file.SetName(name)
}
func (w *Window) Type(t *Text, r rune) {
t.Type(r)
}
// TODO(rjk): In the future of File's replacement with undo buffer,
// this method could be renamed to something like "UpdateTag"?
func (w *Window) Commit(t *Text) {
t.Commit()
if t.what == Body {
return
}
// TODO(rjk): By virtue of being an observer, we know when this has
// changed. No need to extract it here unless its changed.
if w.tagfilenamechanged {
filename := w.ParseTag()
if filename != w.body.file.Name() {
global.seq++
w.body.file.Mark(global.seq)
w.SetName(filename)
}
w.tagfilenamechanged = false
}
}
func isDir(r string) (bool, error) {
f, err := os.Open(r)
if err != nil {
return false, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return false, err
}
if fi.IsDir() {
return true, nil
}
return false, nil
}
// Should include file lookup be built-in? Or provided by a helper?
// TODO(rjk): This should be provided by an external helper.
func (w *Window) AddIncl(r string) {
// Tries to open absolute paths, and if fails, tries
// to use dirname instead.
d, err := isDir(r)
if !d {
if filepath.IsAbs(r) {
warning(nil, "%s: Not a directory: %v", r, err)
return
}
r = w.body.DirName(r)
d, err := isDir(r)
if !d {
warning(nil, "%s: Not a directory: %v", r, err)
return
}
}
w.incl = append(w.incl, r)
}
// Clean returns true iff w can be treated as unmodified.
// This will modify the File so that the next call to Clean will return true
// even if this one returned false.
func (w *Window) Clean(conservative bool) bool {
if w.body.file.IsDirOrScratch() { // don't whine if it's a guide file, error window, etc.
return true
}
if !conservative && w.nopen[QWevent] > 0 {
return true
}
if w.body.file.TreatAsDirty() {
if w.body.file.Name() != "" {
warning(nil, "%v modified\n", w.body.file.Name())
} else {
if w.body.Nc() < 100 { // don't whine if it's too small
return true
}
warning(nil, "unnamed file modified\n")
}
// This toggle permits checking if we can safely destroy the window.
w.body.file.TreatAsClean()
return false
}
return true
}
// CtlPrint generates the contents of the fsys's acme/<id>/ctl pseduo-file if fonts is true.
// Otherwise, it emits a portion of the per-window dump file contents.
func (w *Window) CtlPrint(fonts bool) string {
isdir := 0
if w.body.file.IsDir() {
isdir = 1
}
dirty := 0
if w.body.file.Dirty() {
dirty = 1
}
buf := fmt.Sprintf("%11d %11d %11d %11d %11d ", w.id, w.tag.Nc(),
w.body.Nc(), isdir, dirty)
if fonts {
// fsys exposes the actual physical font name.
buf = fmt.Sprintf("%s%11d %s %11d ", buf, w.body.fr.Rect().Dx(),
quote(fontget(w.body.font, w.display).Name()), w.body.fr.GetMaxtab())
}
return buf
}
func (w *Window) Eventf(format string, args ...interface{}) {
var (
x *Xfid
)
if w.nopen[QWevent] == 0 {
return
}
if w.owner == 0 {
util.AcmeError("no window owner", nil)
}
buffy := new(bytes.Buffer)
fmt.Fprintf(buffy, format, args...)
b := buffy.Bytes()
// TODO(rjk): events should be a bytes.Buffer?
w.events = append(w.events, byte(w.owner))
w.events = append(w.events, b...)
x = w.eventx
if x != nil {
w.eventx = nil
x.c <- nil
}
}
// ClampAddr clamps address range based on the body buffer.
func (w *Window) ClampAddr() {
if w.addr.q0 < 0 {
w.addr.q0 = 0
}
if w.addr.q1 < 0 {
w.addr.q1 = 0
}
if w.addr.q0 > w.body.Nc() {
w.addr.q0 = w.body.Nc()
}
if w.addr.q1 > w.body.Nc() {
w.addr.q1 = w.body.Nc()
}
}
func (w *Window) UpdateTag(newtagstatus file.TagStatus) {
// log.Printf("Window.UpdateTag, status %+v", newtagstatus)
w.setTag1()
}