-
Notifications
You must be signed in to change notification settings - Fork 0
/
win.go
443 lines (393 loc) · 10.9 KB
/
win.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
package nyne
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
"9fans.net/go/acme"
"9fans.net/go/draw"
p9client "9fans.net/go/plan9/client"
)
// Win represents the active Acme window
type Win struct {
ID int
File string
Lastpoint int
w *acme.Win
}
// NewWin constructs a Win object from acme window
func NewWin() (*Win, error) {
w, err := acme.New()
if err != nil {
return nil, err
}
return &Win{w: w}, nil
}
// OpenWin opens an acme window
func OpenWin(id int, file string) (*Win, error) {
w, err := acme.Open(id, nil)
if err != nil {
return nil, err
}
return &Win{
ID: id,
File: file,
w: w,
}, nil
}
// Windows returns all open acme windows
func Windows() (map[int]*Win, error) {
ws, err := acme.Windows()
if err != nil {
return nil, err
}
wins := make(map[int]*Win)
for _, wi := range ws {
w, err := acme.Open(wi.ID, nil)
if err != nil {
return nil, err
}
wins[wi.ID] = &Win{
ID: wi.ID,
File: wi.Name,
w: w,
}
}
return wins, nil
}
// FocusedWinID returns the $winid if present, otherwise it connects
// to the given addr to find the ID
//
// Derived from https://github.com/fhs/acme-lsp/blob/623cb39c2e31bddda0ad7c216c2f3c2fcfcf237f/cmd/L/main.go#L256
func FocusedWinID(addr string) (int, error) {
winid := os.Getenv("winid")
if winid == "" {
conn, err := net.Dial("unix", addr)
if err != nil {
return 0, fmt.Errorf("$winid is empty and could not dial acmefocused: %v", err)
}
defer conn.Close()
b, err := ioutil.ReadAll(conn)
if err != nil {
return 0, fmt.Errorf("$winid is empty and could not read acmefocused: %v", err)
}
winid = string(bytes.TrimSpace(b))
}
return strconv.Atoi(winid)
}
// FocusedWinAddr returns the address of the active window using acmefocused
func FocusedWinAddr() string {
return filepath.Join(p9client.Namespace(), "acmefocused")
}
// EventChan opens a channel to acme events
func (w *Win) EventChan(id int, filename string, stop <-chan struct{}) (<-chan Event, <-chan error) {
errs := make(chan error)
events := make(chan Event)
go func() {
ec := w.w.EventChan()
for {
select {
case e, ok := <-ec:
if !ok {
return
}
event, err := NewEvent(e, id, filename)
if err != nil {
errs <- err
continue
}
events <- event
case <-stop:
return
}
}
}()
return events, errs
}
// WriteEvent writes the acme event to the log
func (w *Win) WriteEvent(e Event) error {
raw, err := e.Log()
if err != nil {
return err
}
return w.w.WriteEvent(raw)
}
// Name sets the name for the win
func (w *Win) Name(format string, args ...interface{}) error {
return w.w.Name(format, args...)
}
// Close closes down the window with associated files
func (w *Win) Close() {
w.w.CloseFiles()
}
// Exec executes the given command in the window tag
func (w *Win) Exec(exec string, args ...string) error {
if w == nil || w.w == nil {
return fmt.Errorf("window handle lost")
}
tag, err := w.Tag()
if err != nil {
return fmt.Errorf("could not read tag: %w", err)
}
var before string
parts := strings.Split(string(tag), "|")
if len(parts) >= 2 {
before = parts[1]
}
cmd := fmt.Sprintf("%s %s", exec, strings.Join(args, " "))
if err := w.AppendTag(cmd); err != nil {
return fmt.Errorf("could not write tag: %w", err)
}
rc := utf8.RuneCount(tag)
nr := utf8.RuneCountInString(cmd)
// TODO: this is broken when using 9fans 0.0.4 due to
// this commit:
// https://github.com/9fans/go/pull/2/commits/ec429baff6830331d0884359449b303d4a87e33c
evt := Event{
Origin: Mouse,
Action: B2Tag,
SelBegin: rc,
SelEnd: rc + nr,
NumRunes: nr,
Text: Text(cmd),
Flag: HasChordedArg,
}
log, err := evt.Log()
if err != nil {
return fmt.Errorf("could not convert event to log: %w", err)
}
err = w.w.WriteEvent(log)
if err != nil {
return fmt.Errorf("could not write event: %w", err)
}
if err = w.ClearTag(); err != nil {
return fmt.Errorf("could not clear tag: %w", err)
}
if err := w.AppendTag(before); err != nil {
return fmt.Errorf("could not write tag: %w", err)
}
return nil
}
// Get is the equivalent to the Get interactive command with no
// arguments; accepts no arguments.
func (w *Win) Get() error {
return w.write("ctl", []byte("get"))
}
// Del is the equivalent to the Del interactive command.
func (w *Win) Del() error {
return w.write("ctl", []byte("del"))
}
// Put is the equivalent to the Put interactive command with no
// arguments; accepts no arguments.
func (w *Win) Put() error {
return w.write("ctl", []byte("put"))
}
// Dump sets the command string to recreate the window from a
// dump file.
func (w *Win) Dump(file string) error {
return w.write("ctl", []byte(fmt.Sprintf("dump %s", file)))
}
// Dumpdir sets the directory in which to run the command to recreate
// the window from a dump file.
func (w *Win) Dumpdir(dir string) error {
return w.write("ctl", []byte(fmt.Sprintf("dumpdir %s", dir)))
}
// Show guarantees at least some of the selected text is visible on
// the display.
func (w *Win) Show() error {
return w.write("ctl", []byte("show"))
}
// NoMark turns off automatic ‘marking’ of changes, so a set of
// related changes may be undone in a single Undo interactive command.
func (w *Win) NoMark() error {
return w.write("ctl", []byte("nomark"))
}
// DisableNoMark cancels nomark, returning the window to the usual state
// wherein each modification to the body must be undone individually.
func (w *Win) DisableNoMark() error {
return w.write("ctl", []byte("mark"))
}
// Clean marks the window clean as though it has just been written.
func (w *Win) Clean() error {
return w.write("ctl", []byte("clean"))
}
// Dirty marks the window dirty, the opposite of clean.
func (w *Win) Dirty() error {
return w.write("ctl", []byte("dirty"))
}
// Tag returns the tag contents
func (w *Win) Tag() ([]byte, error) {
if w == nil || w.w == nil {
return []byte{}, fmt.Errorf("window handle lost")
}
return w.w.ReadAll("tag")
}
// ClearTag removes all text in the tag after the vertical bar.
func (w *Win) ClearTag() error {
return w.write("ctl", []byte("cleartag"))
}
// AppendTag writes to the windows tag
func (w *Win) AppendTag(text string) error {
if w == nil || w.w == nil {
return fmt.Errorf("window handle lost")
}
return w.w.Fprintf("tag", "%s", text)
}
// Body returns the window body
func (w *Win) Body() ([]byte, error) {
if w == nil || w.w == nil {
return []byte{}, fmt.Errorf("window handle lost")
}
return w.w.ReadAll("body")
}
// ClearBody clears the text from the body
func (w *Win) ClearBody() error {
if err := w.SetAddr(","); err != nil {
return fmt.Errorf("could not set addr: %w", err)
}
if err := w.SetData(nil); err != nil {
return fmt.Errorf("could not set data: %w", err)
}
return nil
}
// AppendBody appends the given text to the body
func (w *Win) AppendBody(data []byte) error {
if w == nil || w.w == nil {
return fmt.Errorf("window handle lost")
}
return w.write("body", data)
}
// Char reads the character at q0
func (w *Win) Char(q0 int) (c byte, err error) {
var dat []byte
err = w.SetAddr("#%d;#%d", q0, q0+1)
if err != nil {
if err.Error() == "address out of range" {
c = 0
err = io.EOF
return
}
return
}
dat, err = w.Data(q0, q0+1)
if err != nil {
return
}
if len(dat) == 0 {
err = fmt.Errorf("no data")
return
}
c = dat[0]
return
}
// SetAddr takes an addr which may be written with any textual address
// in the format understood by button 3 but without the initial colon
func (w *Win) SetAddr(fmtstr string, args ...interface{}) error {
addr := fmtstr
if len(args) > 0 {
addr = fmt.Sprintf(fmtstr, args...)
}
return w.w.Addr(addr)
}
// Addr returns the current address of the window
//
// Derived from https://github.com/fhs/acme-lsp/blob/623cb39c2e31bddda0ad7c216c2f3c2fcfcf237f/internal/acme/acme.go#L366
func (w *Win) Addr() (q0, q1 int, err error) {
if w == nil || w.w == nil {
return 0, 0, fmt.Errorf("window handle lost")
}
buf, err := w.w.ReadAll("addr")
if err != nil {
return 0, 0, err
}
a := strings.Fields(string(buf))
if len(a) < 2 {
return 0, 0, errors.New("short read from acme addr")
}
q0, err0 := strconv.Atoi(a[0])
q1, err1 := strconv.Atoi(a[1])
if err0 != nil || err1 != nil {
return 0, 0, errors.New("invalid read from acme addr")
}
return q0, q1, nil
}
// CurrentAddr sets the addr to dot and reads the addr
func (w *Win) CurrentAddr() (q0, q1 int, err error) {
_, _, err = w.Addr() // open addr file
if err != nil {
return 0, 0, fmt.Errorf("read addr: %v", err)
}
err = w.AddrFromSelection()
if err != nil {
return 0, 0, fmt.Errorf("setting addr=dot: %v", err)
}
return w.Addr()
}
// AddrFromSelection sets the addr address to that of the user’s selected
// text in the window.
func (w *Win) AddrFromSelection() error {
return w.write("ctl", []byte("addr=dot"))
}
// SelectionFromAddr sets the user’s selected text in the window to the text
// addressed by the addr address.
func (w *Win) SelectionFromAddr() error {
return w.write("ctl", []byte("dot=addr"))
}
// LimitSearchToAddr restricts subsequent searches to the current addr
// address.
func (w *Win) LimitSearchToAddr() error {
return w.write("ctl", []byte("limit=addr"))
}
// SetData is used in conjunction with addr for random access to the
// contents of the body. The file offset is ignored when writing the
// data file; instead the location of the data to be read or written is
// determined by the state of the addr file. Text, which must contain only
// whole characters (no ‘partial runes’), written to data replaces the
// characters addressed by the addr file and sets the address to the null
// string at the end of the written text. A read from data returns as many
// whole characters as the read count will permit starting at the beginning
// of the addr address (the end of the address has no effect) and sets the
// address to the null string at the end of the returned characters.
func (w *Win) SetData(data []byte) error {
return w.write("data", data)
}
// Data reads the data in the body between q0 and q1. It is assumed
// that CurrentAddr() or similar has been called to properly set the addr
// and retrieve valid q0 and q1 points.
func (w *Win) Data(q0, q1 int) ([]byte, error) {
n := q1 - q0
if n <= 0 {
return nil, fmt.Errorf("invalid range %d,%d", q0, q1)
}
buf := make([]byte, n)
n2, err := w.w.Read("data", buf)
if err != nil {
return buf, err
}
if n2 != n {
return buf, fmt.Errorf("read %d bytes, expected %d", n2, n)
}
return buf, nil
}
// SetFont sets the font for the win
func (w *Win) SetFont(font string) error {
return w.w.Ctl("font %s", font)
}
// Font returns the font for the current win
func (w *Win) Font() (tab int, font *draw.Font, err error) {
return w.w.Font()
}
func (w *Win) write(file string, data []byte) error {
if w == nil || w.w == nil {
return fmt.Errorf("window handle lost")
}
_, err := w.w.Write(file, data)
return err
}