-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.go
151 lines (125 loc) · 2.75 KB
/
window.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
package core
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"unicode/utf8"
"github.com/mibk/syd/ui"
)
const EOF = utf8.MaxRune + 1
type Window struct {
col *Column
filename string
win ui.Updater
con Content
buf *UndoBuffer
tag *Text
body *Text
// used by Read and flush methods
insertbuf bytes.Buffer
y float64
next *Window
}
func (win *Window) SetFilename(filename string) {
win.filename = filename
win.tag.buf.Insert(0, filename)
win.col.ed.wins[filename] = win
}
func (win *Window) Dirty() bool {
return win.buf.Dirty()
}
func (win *Window) Y() float64 { return win.y }
func (win *Window) bottom() float64 {
if win.next == nil {
return 1
}
return win.next.y
}
func (win *Window) SetY(y float64) {
if y < 0 || y > 1 {
panic("y must be in the range 0..1")
}
win.y = y
}
func (win *Window) Tag() *Text { return win.tag }
func (win *Window) Body() *Text { return win.body }
func (win *Window) Close() error {
win.win.Update(ui.Delete)
win.col.removeWindow(win)
if ed := win.col.ed; ed.errWin == win {
ed.errWin = nil
}
if win.filename != "" {
delete(win.col.ed.wins, win.filename)
}
return win.con.Close()
}
func (win *Window) Write(b []byte) (n int, err error) {
return win.insertbuf.Write(b)
}
func (win *Window) flush() {
s := win.insertbuf.String()
win.insertbuf.Reset()
q := win.body.q0
win.body.Insert(s)
win.body.Select(q, q+int64(utf8.RuneCountInString(s)))
// TODO: Come up with a better solution?
win.buf.Commit()
}
func (win *Window) saveFile() {
if win.filename == "" {
win.readFilename()
}
dst, err := filepath.Abs(win.filename)
if err != nil {
panic(err)
}
dir, file := filepath.Split(dst)
f, err := ioutil.TempFile(dir, ".~"+file)
if err != nil {
panic(err)
}
r := io.NewSectionReader(win.buf, 0, win.buf.Size())
if _, err := io.Copy(f, r); err != nil {
panic(err)
}
f.Close()
if err := os.Rename(f.Name(), dst); err != nil {
panic(err)
}
}
func (win *Window) readFilename() {
var runes []rune
var p int64
for {
r := win.tag.readRuneAt(p)
if r == 0 || r == EOF {
break
}
runes = append(runes, r)
p++
}
if len(runes) == 0 {
return
}
win.filename = string(runes)
}
func (win *Window) findNextExactMatch(s string) {
rx := regexp.MustCompile(regexp.QuoteMeta(s))
body := win.body
buf := win.buf
for _, q := range []int64{body.q1, 0} {
r, off := buf.RuneReaderFrom(q)
if loc := rx.FindReaderIndex(r); loc != nil {
q0, q1 := buf.FindRange(off+int64(loc[0]), int64(loc[1]-loc[0]))
body.Select(q0, q1)
return
}
}
}
func (win *Window) editor() (ed *Editor) { return win.col.ed }
func (win *Window) column() (col *Column, ok bool) { return win.col, true }
func (win *Window) window() (w *Window, ok bool) { return win, true }