This repository has been archived by the owner on Feb 23, 2019. It is now read-only.
/
guru.go
150 lines (127 loc) 路 2.98 KB
/
guru.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
// Copyright (c) 2014 David R. Jenni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Guru is an experimental wrapper around oracle for use with editor buffers inside of Acme.
It executes oracle with the given arguments on the current cursor selection of the Acme buffer.
Usage:
Guru <mode> <args> ...
For information about <mode> and <args>, read the oracle help (oracle -help).
Guru assumes that the following change is applied, which adds a mechanism for editor buffers:
https://go-review.googlesource.com/#/c/10400/
*/
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"9fans.net/go/acme"
)
type file struct {
name string
body []byte
off0, off1 int // byte offsets of selection
}
func current() (*file, error) {
win, err := openWin()
if err != nil {
return nil, err
}
defer win.CloseFiles()
body, err := ioutil.ReadAll(&bodyReader{win})
if err != nil {
return nil, err
}
name, err := filename(win)
if err != nil {
return nil, err
}
off0, off1, err := selection(win, body)
if err != nil {
return nil, err
}
return &file{name, body, off0, off1}, nil
}
func openWin() (*acme.Win, error) {
id, err := strconv.Atoi(os.Getenv("winid"))
if err != nil {
return nil, err
}
return acme.Open(id, nil)
}
func filename(win *acme.Win) (string, error) {
b, err := win.ReadAll("tag")
if err != nil {
return "", err
}
tag := string(b)
i := strings.Index(tag, " ")
if i == -1 {
return "", errors.New("cannot get filename from tag")
}
return tag[0:i], nil
}
func selection(win *acme.Win, body []byte) (int, int, error) {
if _, _, err := win.ReadAddr(); err != nil {
return 0, 0, err
}
if err := win.Ctl("addr=dot"); err != nil {
return 0, 0, err
}
q0, q1, err := win.ReadAddr()
if err != nil {
return 0, 0, err
}
off0, err := byteOff(q0, bytes.NewReader(body))
if err != nil {
return 0, 0, err
}
off1, err := byteOff(q1, bytes.NewReader(body))
if err != nil {
return 0, 0, err
}
return off0, off1, nil
}
func byteOff(q int, r io.RuneReader) (off int, err error) {
for i := 0; i != q; i++ {
_, s, err := r.ReadRune()
if err != nil {
return 0, err
}
off += s
}
return
}
type bodyReader struct{ *acme.Win }
func (r bodyReader) Read(d []byte) (int, error) {
return r.Win.Read("body", d)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: Guru <mode> <args> ...\n")
os.Exit(1)
}
f, err := current()
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open window: %v\n", err)
os.Exit(1)
}
var args []string
args = append(args, "-pos", fmt.Sprintf("%s:#%d,#%d", f.name, f.off0, f.off1))
args = append(args, "-replaceset", f.name+",-")
args = append(args, os.Args[1:]...)
c := exec.Command("oracle", args...)
c.Stderr = os.Stderr
c.Stdout = os.Stderr
c.Stdin = bytes.NewReader(f.body)
if err = c.Run(); err != nil {
fmt.Fprintf(os.Stderr, "oracle error: %v\n", err)
os.Exit(1)
}
}