-
Notifications
You must be signed in to change notification settings - Fork 34
/
text.go
1687 lines (1573 loc) · 36.8 KB
/
text.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bytes"
"fmt"
"image"
"io"
"log"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/rjkroege/edwood/complete"
"github.com/rjkroege/edwood/draw"
"github.com/rjkroege/edwood/draw/drawutil"
"github.com/rjkroege/edwood/file"
"github.com/rjkroege/edwood/frame"
"github.com/rjkroege/edwood/runes"
"github.com/rjkroege/edwood/util"
)
const (
Ldot = "."
TABDIR = 3
)
var (
left1 = []rune{'{', '[', '(', '<', 0xab}
right1 = []rune{'}', ']', ')', '>', 0xbb}
left2 = []rune{'\n'}
left3 = []rune{'\'', '"', '`'}
left = [][]rune{
left1,
left2,
left3,
}
right = [][]rune{
right1,
left2,
left3,
}
_ file.BufferObserver = (*Text)(nil) // Enforce at compile time that Text implements BufferObserver
)
type TextKind byte
const (
Columntag TextKind = iota
Rowtag
Tag
Body
)
// Text is a view onto a buffer, managing a frame.
// Files have possible multiple texts corresponding to clones.
type Text struct {
display draw.Display
file *file.ObservableEditableBuffer
fr frame.Frame
font string
org int // Origin of the frame within the buffer
q0 int
q1 int
what TextKind
tabstop int
tabexpand bool
w *Window
scrollr image.Rectangle
lastsr image.Rectangle
all image.Rectangle
row *Row
col *Column
iq1 int
eq0 int // When 0, typing has started
nofill bool // When true, updates to the Text shouldn't update the frame.
lk sync.Mutex
}
// getfont is a convenience accessor that gets the draw.Font from the font
// used in this text.
func (t *Text) getfont() draw.Font {
return fontget(t.font, t.display)
}
func (t *Text) Init(r image.Rectangle, rf string, cols [frame.NumColours]draw.Image, dis draw.Display) *Text {
// log.Println("Text.Init start")
// defer log.Println("Text.Init end")
if t == nil {
t = new(Text)
}
t.display = dis
t.all = r
t.scrollr = r
t.scrollr.Max.X = r.Min.X + t.display.ScaleSize(Scrollwid)
t.lastsr = image.Rectangle{}
r.Min.X += t.display.ScaleSize(Scrollwid) + t.display.ScaleSize(Scrollgap)
t.eq0 = ^0
t.font = rf
t.tabstop = int(global.maxtab)
t.tabexpand = global.tabexpand
t.fr = frame.NewFrame(r, fontget(rf, t.display), t.display.ScreenImage(), cols)
t.Redraw(r, -1, false /* noredraw */)
return t
}
func (t *Text) Nc() int {
return t.file.Nr()
}
// String returns a string representation of the TextKind.
func (tk TextKind) String() string {
switch tk {
case Body:
return "Body"
case Columntag:
return "Columntag"
case Rowtag:
return "Rowtag"
case Tag:
return "Tag"
}
return fmt.Sprintf("TextKind(%v)", int(tk))
}
func (t *Text) Redraw(r image.Rectangle, odx int, noredraw bool) {
// log.Println("--- Text Redraw start", r, odx, "tag type:" , t.what)
// defer log.Println("--- Text Redraw end")
// use no wider than 3-space tabs in a directory
maxt := int(global.maxtab)
if t.what == Body {
if t.file.IsDir() {
maxt = util.Min(TABDIR, int(global.maxtab))
} else {
maxt = t.tabstop
}
}
t.fr.Init(r, frame.OptMaxTab(maxt))
if !noredraw {
enclosing := r
enclosing.Min.X -= t.display.ScaleSize(Scrollwid + Scrollgap)
t.fr.Redraw(enclosing)
}
if t.what == Body && t.file.IsDir() && odx != t.all.Dx() {
if t.fr.GetFrameFillStatus().Maxlines > 0 {
t.Reset()
t.Columnate(t.w.dirnames, t.w.widths)
t.Show(0, 0, false)
}
} else {
t.fill(t.fr)
t.SetSelect(t.q0, t.q1)
}
}
func (t *Text) Resize(r image.Rectangle, keepextra, noredraw bool) int {
// log.Println("--- Text Resize start", r, keepextra, t.what)
// defer log.Println("--- Text Resize end")
if r.Dy() <= 0 {
// TODO(rjk): Speculative change to draw better. Original:
// r.Max.Y = r.Min.Y
// log.Println("r.Dy() <= 0 case")
r = r.Canon()
} else {
if !keepextra {
r.Max.Y -= r.Dy() % t.fr.DefaultFontHeight()
}
}
odx := t.all.Dx()
t.all = r
t.scrollr = r
t.scrollr.Max.X = r.Min.X + t.display.ScaleSize(Scrollwid)
t.lastsr = image.Rectangle{}
r.Min.X += t.display.ScaleSize(Scrollwid + Scrollgap)
t.fr.Clear(false)
// TODO(rjk): Remove this Font accessor.
t.Redraw(r, odx, noredraw)
return t.all.Max.Y
}
func (t *Text) Close() {
t.fr.Clear(true)
if err := t.file.DelObserver(t); err != nil {
util.AcmeError(err.Error(), nil)
}
t.file = nil
if global.argtext == t {
global.argtext = nil
}
if global.typetext == t {
global.typetext = nil
}
if global.seltext == t {
global.seltext = nil
}
if global.mousetext == t {
global.mousetext = nil
}
if global.barttext == t {
global.barttext = nil
}
}
func (t *Text) Columnate(names []string, widths []int) {
var colw, mint, maxt, ncol, nrow int
q1 := 0
Lnl := []rune("\n")
Ltab := []rune("\t")
if t.file.HasMultipleObservers() {
panic("Text.Columnate is only for directories that can't have zerox")
}
mint = t.getfont().StringWidth("0")
// go for narrower tabs if set more than 3 wide
t.fr.Maxtab(util.Min(int(global.maxtab), TABDIR) * mint)
maxt = t.fr.GetMaxtab()
for _, w := range widths {
if maxt-w%maxt < mint || w%maxt == 0 {
w += mint
}
if w%maxt != 0 {
w += maxt - (w % maxt)
}
if w > colw {
colw = w
}
}
if colw == 0 {
ncol = 1
} else {
ncol = util.Max(1, t.fr.Rect().Dx()/colw)
}
nrow = (len(names) + ncol - 1) / ncol
q1 = 0
for i := 0; i < nrow; i++ {
for j := i; j < len(names); j += nrow {
dl := bytetorune([]byte(names[j]))
t.file.InsertAt(q1, dl)
q1 += len(dl)
if j+nrow >= len(names) {
break
}
w := widths[j]
if maxt-w%maxt < mint {
t.file.InsertAt(q1, Ltab)
q1++
w += mint
}
for {
t.file.InsertAt(q1, Ltab)
q1++
w += maxt - (w % maxt)
if !(w < colw) {
break
}
}
}
t.file.InsertAt(q1, Lnl)
q1++
}
}
func (t *Text) checkSafeToLoad(filename string) error {
if t.file.Nr() > 0 || t.w == nil || t != &t.w.body {
panic("text.load")
}
if t.file.IsDir() && t.file.Name() == "" {
return warnError(nil, "empty directory name")
}
if ismtpt(filename) {
return warnError(nil, "will not open self mount point %s", filename)
}
return nil
}
func (t *Text) loadReader(q0 int, filename string, rd io.Reader, sethash bool) (nread int, err error) {
t.file.SetDir(false)
t.w.filemenu = true
count, hasNulls, err := t.file.Load(q0, rd, sethash)
if err != nil {
return 0, warnError(nil, "error reading file %s: %v", filename, err)
}
if hasNulls {
warning(nil, "%s: NUL bytes elided\n", filename)
}
return count, nil
}
// LoadReader loads an io.Reader into the Text.file. Text must be of type body.
// Filename is only used for error reporting, not for access to the on-disk file.
func (t *Text) LoadReader(q0 int, filename string, rd io.Reader, sethash bool) (nread int, err error) {
if err := t.checkSafeToLoad(filename); err != nil {
return 0, err
}
return t.loadReader(q0, filename, rd, sethash)
}
// Load loads filename into the Text.file. Text must be of type body.
func (t *Text) Load(q0 int, filename string, setqid bool) (nread int, err error) {
if err := t.checkSafeToLoad(filename); err != nil {
return 0, err
}
fd, err := os.Open(filename)
if err != nil {
return 0, warnError(nil, "can't open %s: %v", filename, err)
}
defer fd.Close()
d, err := fd.Stat()
if err != nil {
return 0, warnError(nil, "can't fstat %s: %v", filename, err)
}
if setqid {
t.file.SetInfo(d)
}
if d.IsDir() {
// this is checked in get() but it's possible the file changed underfoot
if t.file.HasMultipleObservers() {
return 0, warnError(nil, "%s is a directory; can't read with multiple windows on it", filename)
}
t.file.SetDir(true)
t.w.filemenu = false
if len(t.file.Name()) > 0 && !strings.HasSuffix(t.file.Name(), string(filepath.Separator)) {
t.file.SetName(t.file.Name() + string(filepath.Separator))
t.w.SetName(t.file.Name())
}
dirNames, err := getDirNames(fd)
if err != nil {
return 0, warnError(nil, "getDirNames failed: %v", err)
}
widths := make([]int, len(dirNames))
dft := t.getfont()
for i, s := range dirNames {
widths[i] = dft.StringWidth(s)
}
t.Columnate(dirNames, widths)
t.w.dirnames = dirNames
t.w.widths = widths
q1 := t.file.Nr()
return q1 - q0, nil
}
return t.loadReader(q0, filename, fd, setqid && q0 == 0)
}
func getDirNames(f *os.File) ([]string, error) {
entries, err := f.Readdir(0)
if err != nil {
return nil, err
}
names := make([]string, len(entries))
for i, fi := range entries {
if fi.IsDir() {
names[i] = fi.Name() + string(filepath.Separator)
} else {
names[i] = fi.Name()
}
}
sort.Strings(names)
for i := range names {
names[i] = QuoteFilename(names[i])
}
return names, nil
}
// BsInsert inserts runes r at text position q0. If r contains backspaces ('\b'),
// they are interpreted, removing the runes preceding them.
// The final text position where r is inserted and the number of runes inserted
// after interpreting backspaces is returned.
func (t *Text) BsInsert(q0 int, r []rune, tofile bool) (q, nr int) {
n := len(r)
if t.what == Tag { // can't happen but safety first: mustn't backspace over file name
t.Insert(q0, r, tofile)
return q0, n
}
bp := 0 // bp indexes r
for i := 0; i < n; i++ {
if r[bp] == '\b' {
initial := 0
tp := make([]rune, n)
copy(tp, r[:i])
up := i // up indexes tp, starting at i
for ; i < n; i++ {
tp[up] = r[bp]
bp++
if tp[up] == '\b' {
if up == 0 {
initial++
} else {
up--
}
} else {
up++
}
}
if initial != 0 {
if initial > q0 {
initial = q0
}
q0 -= initial
t.Delete(q0, q0+initial, tofile)
}
n = up
t.Insert(q0, tp[:n], tofile)
return q0, n
}
bp++
}
t.Insert(q0, r, tofile)
return q0, n
}
// inserted is a callback invoked by File on Insert* to update each Text
// that is using a given File.
// TODO(rjk): Carefully scrub this for opportunities to not do work if the
// changes are not in the viewport. Also: minimize scrollbar redraws.
func (t *Text) Inserted(oq0 file.OffsetTuple, b []byte, nr int) {
q0 := oq0.R
if t.eq0 == -1 {
t.eq0 = q0
}
if t.what == Body {
t.w.utflastqid = -1
}
if q0 < t.iq1 {
t.iq1 += nr
}
if q0 < t.q1 {
t.q1 += nr
}
if q0 < t.q0 {
t.q0 += nr
}
if q0 < t.org {
t.org += nr
} else {
if t.fr != nil && q0 <= t.org+(t.fr.GetFrameFillStatus().Nchars) {
t.fr.InsertByte(b, q0-t.org)
}
}
t.logInsert(oq0, b, nr)
// TODO(rjk): The below should only be invoked once (at the end) of a
// sequence of modifications to the file.Buffer, not here per action.
t.SetSelect(t.q0, t.q1)
if t.fr != nil && t.display != nil {
t.ScrDraw(t.fr.GetFrameFillStatus().Nchars)
}
}
// writeEventLog emits an event log for an insertion.
// TODO(rjk): Refactor this with the other event log insertions.
// TODO(rjk): can be more stateless.
// TODO(rjk): Can express this more precisely with an interface
// that makes its state dependency obvious
func (t *Text) logInsert(oq0 file.OffsetTuple, b []byte, nr int) {
q0 := oq0.R
if t.w != nil {
c := 'i'
if t.what == Body {
c = 'I'
}
if nr <= EVENTSIZE {
// TODO(rjk): Does unnecessary work making a string from r if there's no
// event reader.
t.w.Eventf("%c%d %d 0 %d %s\n", c, q0, q0+nr, nr, b)
} else {
t.w.Eventf("%c%d %d 0 0 \n", c, q0, q0+nr)
}
}
}
// Insert inserts rune buffer r at q0. The selection values will be
// updated appropriately.
func (t *Text) Insert(q0 int, r []rune, tofile bool) {
if !tofile {
panic("text.insert")
}
if len(r) == 0 {
return
}
t.file.InsertAt(q0, r)
}
func (t *Text) TypeCommit() {
if t.w != nil {
t.w.Commit(t)
} else {
t.Commit()
}
}
func (t *Text) inSelection(q0 int) bool {
return t.q1 > t.q0 && t.q0 <= q0 && q0 <= t.q1
}
// Fill inserts additional text from t into the Frame object until the Frame object is full.
func (t *Text) fill(fr frame.SelectScrollUpdater) error {
// log.Println("Text.Fill Start", t.what)
// defer log.Println("Text.Fill End")
// Conceivably, LastLineFull should be true or would it only be true if there are no more
// characters possible?
if fr.IsLastLineFull() || t.nofill {
return nil
}
for {
n := t.file.Nr() - (t.org + fr.GetFrameFillStatus().Nchars)
if n < 0 {
log.Printf("Text.fill: negative slice length %v (file size %v, t.org %v, frame nchars %v)\n",
n, t.file.Nr(), t.org, fr.GetFrameFillStatus().Nchars)
return fmt.Errorf("fill: negative slice length %v", n)
}
if n == 0 {
break
}
if n > 2000 { // educated guess at reasonable amount
n = 2000
}
rp := make([]rune, n)
t.file.Read(t.org+fr.GetFrameFillStatus().Nchars, rp)
//
// it's expensive to frinsert more than we need, so
// count newlines.
//
nl := fr.GetFrameFillStatus().Maxlines - fr.GetFrameFillStatus().Nlines //+1
m := 0
var i int
for i = 0; i < n; {
i++
if rp[i-1] == '\n' {
m++
if m >= nl {
break
}
}
}
if lastlinefull := fr.Insert(rp[:i], fr.GetFrameFillStatus().Nchars); nl == 0 || lastlinefull {
break
}
}
return nil
}
// Delete removes runes [q0, q1). The selection values will be
// updated appropriately.
func (t *Text) Delete(q0, q1 int, _ bool) {
n := q1 - q0
if n == 0 {
return
}
t.file.DeleteAt(q0, q1)
}
// deleted implements the single-text deletion observer for this Text's
// backing File. It updates the Text (i.e. the view) for the removal of
// runes [q0, q1).
func (t *Text) Deleted(oq0, oq1 file.OffsetTuple) {
q0 := oq0.R
q1 := oq1.R
n := q1 - q0
if t.what == Body {
t.w.utflastqid = -1
}
if q0 < t.iq1 {
t.iq1 -= util.Min(n, t.iq1-q0)
}
if q0 < t.q0 {
t.q0 -= util.Min(n, t.q0-q0)
}
if q0 < t.q1 {
t.q1 -= util.Min(n, t.q1-q0)
}
if q1 <= t.org {
t.org -= n
} else if t.fr != nil && q0 < t.org+(t.fr.GetFrameFillStatus().Nchars) {
p1 := q1 - t.org
if p1 > (t.fr.GetFrameFillStatus().Nchars) {
p1 = t.fr.GetFrameFillStatus().Nchars
}
p0 := 0
if q0 < t.org {
t.org = q0
p0 = 0
} else {
p0 = q0 - t.org
}
t.fr.Delete(p0, p1)
t.fill(t.fr)
}
t.logInsertDelete(q0, q1)
t.SetSelect(t.q0, t.q1)
if t.fr != nil && t.display != nil {
t.ScrDraw(t.fr.GetFrameFillStatus().Nchars)
}
}
// TODO(rjk): Fold this into logInsert is a nice way.
func (t *Text) logInsertDelete(q0, q1 int) {
if t.w != nil {
c := 'd'
if t.what == Body {
c = 'D'
}
t.w.Eventf("%c%d %d 0 0 \n", c, q0, q1)
}
}
func (t *Text) ReadB(q int, r []rune) (n int, err error) { n, err = t.file.Read(q, r); return }
func (t *Text) nc() int { return t.file.Nr() }
func (t *Text) Q0() int { return t.q0 }
func (t *Text) Q1() int { return t.q1 }
func (t *Text) SetQ0(q0 int) { t.q0 = q0 }
func (t *Text) SetQ1(q1 int) { t.q1 = q1 }
func (t *Text) Constrain(q0, q1 int) (p0, p1 int) {
p0 = util.Min(q0, t.file.Nr())
p1 = util.Min(q1, t.file.Nr())
return p0, p1
}
func (t *Text) BsWidth(c rune) int {
// there is known to be at least one character to erase
if c == 0x08 { // ^H: erase character
return 1
}
q := t.q0
skipping := true
for q > 0 {
r := t.file.ReadC(q - 1)
if r == '\n' { // eat at most one more character
if q == t.q0 { // eat the newline
q--
}
break
}
if c == 0x17 {
eq := isalnum(r)
if eq && skipping { // found one; stop skipping
skipping = false
} else {
if !eq && !skipping {
break
}
}
}
q--
}
return t.q0 - q
}
func (t *Text) FileWidth(q0 int, oneelement bool) int {
q := q0
for q > 0 {
r := t.file.ReadC(q - 1)
if r <= ' ' {
break
}
if oneelement && r == '/' {
break
}
q--
}
return q0 - q
}
func (t *Text) Complete() []rune {
if t.q0 < t.Nc() && t.file.ReadC(t.q0) > ' ' { // must be at end of word
return nil
}
str := make([]rune, t.FileWidth(t.q0, true))
q := t.q0 - len(str)
for i := range str {
str[i] = t.file.ReadC(q)
q++
}
path := make([]rune, t.FileWidth(t.q0-len(str), false))
q = t.q0 - len(str) - len(path)
for i := range path {
path[i] = t.file.ReadC(q)
q++
}
// is path rooted? if not, we need to make it relative to window path
dir := string(path)
if !filepath.IsAbs(dir) {
dir = t.DirName("")
if len(dir) == 0 {
dir = Ldot
}
dir = filepath.Clean(filepath.Join(dir, string(path)))
}
c, err := complete.Complete(dir, string(str))
if err != nil {
warning(nil, "error attempting completion: %v\n", err)
return nil
}
if c.Advance {
return []rune(c.String)
}
var b bytes.Buffer
b.WriteString(dir)
if len(dir) > 0 && dir[len(dir)-1] != filepath.Separator {
b.WriteRune(filepath.Separator)
}
b.WriteString(string(str) + "*")
if c.NMatch == 0 {
b.WriteString(": no matches in:")
}
warning(nil, "%s\n", b.String())
for _, fn := range c.Filename {
warning(nil, " %s\n", fn)
}
return nil
}
func (t *Text) Type(r rune) {
var (
q0, q1 int
nnb, n, i int
nr int
)
// Avoid growing column and row tags.
if t.what != Body && t.what != Tag && r == '\n' {
return
}
if t.what == Tag {
t.w.tagsafe = false
}
nr = 1
rp := []rune{r}
Tagdown := func() {
// expand tag to show all text
if !t.w.tagexpand {
t.w.tagexpand = true
t.w.Resize(t.w.r, false, true)
}
}
Tagup := func() {
// shrink tag to single line
if t.w.tagexpand {
t.w.tagexpand = false
t.w.taglines = 1
t.w.Resize(t.w.r, false, true)
}
}
caseDown := func() {
q0 = t.org + t.fr.Charofpt(image.Pt(t.fr.Rect().Min.X, t.fr.Rect().Min.Y+n*t.fr.DefaultFontHeight()))
t.SetOrigin(q0, true)
}
caseUp := func() {
q0 = t.BackNL(t.org, n)
t.SetOrigin(q0, true)
}
setUndoPoint := func() {
if t.what == Body {
global.seq++
t.file.Mark(global.seq)
}
}
// This switch block contains all actions that don't mutate the buffer
// and hence there is no need to create an Undo record.
switch r {
case draw.KeyLeft:
t.TypeCommit()
if t.q0 > 0 {
if t.q0 != t.q1 {
t.Show(t.q0, t.q0, true)
} else {
t.Show(t.q0-1, t.q0-1, true)
}
}
return
case draw.KeyRight:
t.TypeCommit()
if t.q1 < t.file.Nr() {
// This is a departure from the plan9/plan9port acme
// Instead of always going right one char from q1, it
// collapses multi-character selections first, behaving
// like every other selection on modern systems. -flux
if t.q0 != t.q1 {
t.Show(t.q1, t.q1, true)
} else {
t.Show(t.q1+1, t.q1+1, true)
}
}
return
case draw.KeyDown, 0xF800:
if t.what == Tag {
Tagdown()
return
}
n = t.fr.GetFrameFillStatus().Maxlines / 3
caseDown()
return
case Kscrollonedown:
if t.what == Tag {
Tagdown()
return
}
n = drawutil.MouseScrollSize(t.fr.GetFrameFillStatus().Maxlines)
if n <= 0 {
n = 1
}
caseDown()
return
case draw.KeyPageDown:
n = 2 * t.fr.GetFrameFillStatus().Maxlines / 3
caseDown()
return
case draw.KeyUp:
if t.what == Tag {
Tagup()
return
}
n = t.fr.GetFrameFillStatus().Maxlines / 3
caseUp()
return
case Kscrolloneup:
if t.what == Tag {
Tagup()
return
}
n = drawutil.MouseScrollSize(t.fr.GetFrameFillStatus().Maxlines)
caseUp()
return
case draw.KeyPageUp:
n = 2 * t.fr.GetFrameFillStatus().Maxlines / 3
caseUp()
return
case draw.KeyHome:
t.TypeCommit()
if t.org > t.iq1 {
q0 = t.BackNL(t.iq1, 1)
t.SetOrigin(q0, true)
} else {
t.Show(0, 0, false)
}
return
case draw.KeyEnd:
t.TypeCommit()
if t.iq1 > t.org+t.fr.GetFrameFillStatus().Nchars {
if t.iq1 > t.file.Nr() {
// should not happen, but does. and it will crash textbacknl.
t.iq1 = t.file.Nr()
}
q0 = t.BackNL(t.iq1, 1)
t.SetOrigin(q0, true)
} else {
t.Show(t.file.Nr(), t.file.Nr(), false)
}
return
case '\t': // ^I (TAB)
if t.tabexpand {
for i := 0; i < t.tabstop; i++ {
t.Type(' ')
}
return
}
case 0x01: // ^A: beginning of line
t.TypeCommit()
// go to where ^U would erase, if not already at BOL
nnb = 0
if t.q0 > 0 && t.file.ReadC(t.q0-1) != '\n' {
nnb = t.BsWidth(0x15)
}
t.Show(t.q0-nnb, t.q0-nnb, true)
return
case 0x05: // ^E: end of line
t.TypeCommit()
q0 = t.q0
for q0 < t.file.Nr() && t.file.ReadC(q0) != '\n' {
q0++
}
t.Show(q0, q0, true)
return
case 0x3, draw.KeyCmd + 'c': // %C: copy
t.TypeCommit()
cut(t, t, nil, true, false, "")
return
case 0x1a, draw.KeyCmd + 'z': // %Z: undo
t.TypeCommit()
undo(t, nil, nil, true, false, "")
return
case draw.KeyCmd + 'Z': // %-shift-Z: redo
t.TypeCommit()
undo(t, nil, nil, false, false, "")
return
}
// Note the use of eq0 to always force an undo point at the start typing.
if t.what == Body && t.eq0 == -1 {
setUndoPoint()
}
// These following blocks contain mutating actions.
// cut/paste must be done after the seq++/filemark
switch r {
case 0x18, draw.KeyCmd + 'x': // %X: cut
setUndoPoint()
t.TypeCommit()
if t.what == Body {
global.seq++
t.file.Mark(global.seq)
}
cut(t, t, nil, true, true, "")
t.Show(t.q0, t.q0, true)
t.iq1 = t.q0
return
case 0x16, draw.KeyCmd + 'v': // %V: paste
setUndoPoint()
t.TypeCommit()
if t.what == Body {
global.seq++
t.file.Mark(global.seq)
}
paste(t, t, nil, true, false, "")
t.Show(t.q0, t.q1, true)
t.iq1 = t.q1
return
}
wasrange := t.q0 != t.q1
removedstuff := false
if t.q1 > t.q0 {
setUndoPoint()
cut(t, t, nil, true, true, "")
t.eq0 = ^0
removedstuff = true
}
t.Show(t.q0, t.q0, true)
switch r {
case 0x06:
fallthrough // ^F: complete
case draw.KeyInsert:
t.TypeCommit()
rp = t.Complete()
if rp == nil {
return
}
setUndoPoint()
nr = len(rp) // runestrlen(rp);
// break into normal insertion case
case 0x1B:
if t.eq0 != ^0 {
if t.eq0 <= t.q0 {
t.SetSelect(t.eq0, t.q0)
} else {
t.SetSelect(t.q0, t.eq0)
}
}
t.iq1 = t.q0
return
case 0x7F: // Del: erase character right
if t.q1 >= t.Nc()-1 {
return // End of file
}
setUndoPoint()
t.TypeCommit() // Avoid messing with the cache?
if !wasrange {
t.q1++
cut(t, t, nil, false, true, "")
}
return
case 0x08:
fallthrough // ^H: erase character
case 0x15:
fallthrough // ^U: erase line
case 0x17: // ^W: erase word
if removedstuff {
// No further action needed.
return
}
if t.q0 == 0 { // nothing to erase
return
}