-
Notifications
You must be signed in to change notification settings - Fork 1
/
acme.go
296 lines (251 loc) · 5.48 KB
/
acme.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
// Package acme extends the 9fans.net/go/acme
// package with additional functionality.
package acme
import (
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"9fans.net/go/acme"
)
var ctlre = regexp.MustCompile(`(.{11} )(.{11} )(.{11} )(.{11} )(.{11} )(.{11} )(.*?) (.*)`)
// Win is a wrapper for an acme window.
type Win struct {
*acme.Win
id int
}
// EvtHandler is an interface defining
// all possible event callback methods.
type EvtHandler interface {
// BodyInsert is invoked when text is being inserted to the body
BodyInsert(offset int)
// Del is invoked when a window is closed
Del()
// Invoked when a communication error happens
Err(error)
}
// GetWinID returns the window ID of the current window.
func GetWinID() (int, error) {
winid := os.Getenv("winid")
if winid == "" {
return -1, fmt.Errorf("$winid not set - not running inside acme?")
}
id, err := strconv.Atoi(winid)
if err != nil {
return -1, fmt.Errorf("invalid $winid %q", winid)
}
return id, nil
}
// GetWin returns a window handle for a given window ID.
func GetWin(id int) (*Win, error) {
win, err := acme.Open(id, nil)
if err != nil {
return nil, fmt.Errorf("cannot open acme window: %v", err)
}
ctl := &Win{}
ctl.Win = win
ctl.id = id
return ctl, nil
}
// New creates a new acme editor window.
func New() (*Win, error) {
ctl := &Win{}
win, err := acme.New()
if err != nil {
return nil, err
}
ctl.Win = win
buf, err := win.ReadAll("ctl")
if err != nil {
return nil, err
}
fields := ctlre.FindAllStringSubmatch(string(buf), -1)
ctl.id, err = strconv.Atoi(strings.TrimSpace(fields[0][1]))
if err != nil {
return nil, err
}
return ctl, err
}
type fileReadWriter struct {
*Win
file string
}
func (frw fileReadWriter) Write(p []byte) (n int, err error) {
return frw.Win.Write(frw.file, p)
}
func (frw fileReadWriter) Read(p []byte) (n int, err error) {
return frw.Win.Read(frw.file, p)
}
func (win *Win) FileReadWriter(file string) io.ReadWriter {
return fileReadWriter{win, file}
}
// LineAddress returns the line address
// at the current cursor position.
func (win *Win) LineAddress() (int, error) {
_, _, err := win.ReadAddr() // make sure address file is already open.
if err != nil {
return -1, fmt.Errorf("cannot read address: %v", err)
}
err = win.Ctl("addr=dot")
if err != nil {
return -1, fmt.Errorf("cannot set addr=dot: %v", err)
}
q0, _, err := win.ReadAddr()
if err != nil {
return -1, fmt.Errorf("cannot read address: %v", err)
}
body, err := win.ReadBody()
if err != nil {
return -1, fmt.Errorf("cannot read body: %v", err)
}
return 1 + nlcount(body, q0), nil
}
// ByteOffset returns the byte offset
// at the current cursor position.
func (win *Win) ByteOffset() (int, error) {
_, _, err := win.ReadAddr() // make sure address file is already open.
if err != nil {
return -1, fmt.Errorf("cannot read address: %v", err)
}
err = win.Ctl("addr=dot")
if err != nil {
return -1, fmt.Errorf("cannot set addr=dot: %v", err)
}
q0, _, err := win.ReadAddr()
if err != nil {
return -1, fmt.Errorf("cannot read address: %v", err)
}
body, err := win.ReadBody()
if err != nil {
return -1, fmt.Errorf("cannot read body: %v", err)
}
return runeOffset2ByteOffset(body, q0), nil
}
// Filename returns the file name.
func (win *Win) Filename() (string, error) {
tagb, err := win.ReadAll("tag")
if err != nil {
return "", fmt.Errorf("cannot read tag: %v", err)
}
tag := string(tagb)
i := strings.Index(tag, " ")
if i == -1 {
return "", fmt.Errorf("tag contains no spaces")
}
return tag[0:i], nil
}
// HandleEvt registers an event handler
// in a new goroutine.
func (win *Win) HandleEvt(h EvtHandler) {
go func() {
for evt := range win.Win.EventChan() {
switch evt.C2 {
case 'I':
err := win.Win.Ctl("addr=dot")
if err != nil {
h.Err(err)
return
}
q0, _, err := win.Win.ReadAddr()
if err != nil {
h.Err(err)
return
}
h.BodyInsert(q0)
case 'x', 'X':
if string(evt.Text) == "Del" {
win.Win.Ctl("delete")
h.Del()
}
}
win.Win.WriteEvent(evt)
}
}()
}
// WindowID returns the window ID.
func (win Win) WindowID() int {
return win.id
}
// ClearBody clears the body
// effectively deleting the complete text content.
func (win *Win) ClearBody() error {
err := win.Win.Addr(",")
if err != nil {
return err
}
_, err = win.Win.Write("data", nil)
if err != nil {
return err
}
err = win.Win.Ctl("clean")
if err != nil {
return err
}
return nil
}
// GotoAddr jumps to the given address.
func (win *Win) GotoAddr(addr string) error {
err := win.Win.Fprintf("addr", addr)
if err != nil {
return err
}
err = win.Win.Ctl("dot=addr")
if err != nil {
return err
}
err = win.Win.Ctl("show")
if err != nil {
return err
}
return nil
}
// ReadBody returns the text body content.
func (win Win) ReadBody() ([]byte, error) {
rwin, err := acme.Open(win.id, nil)
if err != nil {
return nil, err
}
defer rwin.CloseFiles()
var body []byte
buf := make([]byte, 8000)
for {
n, err := rwin.Read("body", buf)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
body = append(body, buf[0:n]...)
}
if err != nil {
return nil, err
}
return body, nil
}
func runeOffset2ByteOffset(b []byte, off int) int {
r := 0
for i := range string(b) {
if r == off {
return i
}
r++
}
return len(b)
}
func nlcount(b []byte, q0 int) int {
nl := 0
ri := 0
for _, r := range string(b) {
if ri == q0 {
return nl
}
if r == '\n' {
nl++
}
ri++
}
return nl
}