-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (117 loc) · 2.69 KB
/
main.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
// 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.
/*
Rename is a wrapper around gorename for use with Acme.
It renames the entity under the cursor.
Usage:
Rename <name>
Example:
Rename Foo
renames the entity under the cursor with 'Foo'.
gorename must be installed:
% go get golang.org/x/tools/cmd/gorename
*/
package main // import "github.com/aoeu/acme/Rename"
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"9fans.net/go/acme"
)
type bodyReader struct{ *acme.Win }
func (r bodyReader) Read(data []byte) (int, error) {
return r.Win.Read("body", data)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: agorn <name>\n")
os.Exit(1)
}
win, err := openWin()
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open window: %v\n", err)
os.Exit(1)
}
filename, off, err := selection(win)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot get selection: %v\n", err)
os.Exit(1)
}
c := exec.Command("gorename", "-offset", fmt.Sprintf("%s:#%d", filename, off), "-to", os.Args[1])
c.Stderr = os.Stderr
if err = c.Run(); err != nil {
fmt.Fprintf(os.Stderr, "rename failed: %v\n", err)
os.Exit(1)
}
if err := reloadShowAddr(win, off); err != nil {
fmt.Fprintf(os.Stderr, "cannot restore selection: %s\n", err)
os.Exit(1)
}
}
func openWin() (*acme.Win, error) {
id, err := strconv.Atoi(os.Getenv("winid"))
if err != nil {
return nil, err
}
return acme.Open(id, nil)
}
func selection(win *acme.Win) (filename string, off int, err error) {
filename, err = readFilename(win)
if err != nil {
return "", 0, err
}
q0, _, err := readAddr(win)
if err != nil {
return "", 0, err
}
off, err = byteOffset(bufio.NewReader(&bodyReader{win}), q0)
if err != nil {
return "", 0, err
}
return
}
func readFilename(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 "", fmt.Errorf("cannot get filename from tag")
}
return tag[0:i], nil
}
func readAddr(win *acme.Win) (q0, q1 int, err error) {
if _, _, err := win.ReadAddr(); err != nil {
return 0, 0, err
}
if err := win.Ctl("addr=dot"); err != nil {
return 0, 0, err
}
return win.ReadAddr()
}
func byteOffset(r io.RuneReader, off int) (bo int, err error) {
for i := 0; i != off; i++ {
_, s, err := r.ReadRune()
if err != nil {
return 0, err
}
bo += s
}
return
}
func reloadShowAddr(win *acme.Win, off int) error {
if err := win.Ctl("get"); err != nil {
return err
}
if err := win.Addr("#%d", off); err != nil {
return err
}
return win.Ctl("dot=addr\nshow")
}