-
Notifications
You must be signed in to change notification settings - Fork 23
/
plumb.go
209 lines (185 loc) · 4.58 KB
/
plumb.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
package actions
import (
"fmt"
"os"
"strconv"
"9fans.net/go/plan9"
plumblib "9fans.net/go/plumb"
"github.com/driusan/de/demodel"
)
// This boolean indicates whether plumbing is ready to be used. It should
// generally only be called by the main thread to flag that plumb messages
// are fine to use.
//
// Until the main thread does that, plumb will fail, assuming that it hasn't
// been properly initialized.
var PlumbingReady bool
func plumb(content []byte, buff *demodel.CharBuffer, v demodel.Viewport, click int) error {
if !PlumbingReady {
return fmt.Errorf("Plumbing unavailable")
}
fid, err := plumblib.Open("send", plan9.OWRITE)
if err != nil {
fmt.Printf("%v", err)
return err
}
wd, _ := os.Getwd()
m := plumblib.Message{
Src: "de",
Dst: "",
Dir: wd,
Type: "text",
Data: content,
}
if click != 0 {
m.Attr = &plumblib.Attribute{Name: "click", Value: strconv.Itoa(click)}
}
return m.Send(fid)
}
func PlumbExecuteOrFindNext(From, To demodel.Position, buff *demodel.CharBuffer, v demodel.Viewport) {
if buff == nil {
return
}
dot := demodel.Dot{}
i, err := From(*buff)
if err != nil {
return
}
dot.Start = i
i, err = To(*buff)
if err != nil {
return
}
dot.End = i + 1
word := string(buff.Buffer[dot.Start:dot.End])
// Don't bother trying if there's nothing listening yet.
if PlumbingReady {
// Create a new []byte for the plumber, because if nothing
// was selected we want to send a message with a click
// attribute, and if plumbing fails we don't want to have
// touched the word for the fallbacks.
var plumbword []byte
var click int
var pdot demodel.Dot
if dot.Start == dot.End-1 {
// Nothing was selected, to add a "click" attribute
if dot.Start < 100 {
click = int(dot.Start)
pdot.Start = 0
} else {
click = 100
pdot.Start = dot.Start - 100
}
if dot.End+100 < uint(len(buff.Buffer)) {
pdot.End = pdot.End + 100
} else {
pdot.End = uint(len(buff.Buffer))
}
plumbword = buff.Buffer[pdot.Start:pdot.End]
} else {
// Default to "word" (the selected text)
// with no click attribute
plumbword = []byte(word)
}
// If the message was successfully plumbed, we're done.
if err := plumb(plumbword, buff, v, click); err == nil {
return
}
}
// Try executing the command. If it works, we're done.
if err := RunOrExec(word, buff, v); err == nil {
return
}
// We couldn't plumb it, we couldn't execute it, so give up and search for
// the word
lenword := dot.End - dot.Start
for i := dot.End; i < uint(len(buff.Buffer))-lenword; i++ {
if string(buff.Buffer[i:i+lenword]) == word {
buff.Dot.Start = i
buff.Dot.End = i + lenword - 1
return
}
}
}
func PlumbOrFindNext(From, To demodel.Position, buff *demodel.CharBuffer, v demodel.Viewport) {
if buff == nil {
return
}
dot := demodel.Dot{}
i, err := From(*buff)
if err != nil {
return
}
dot.Start = i
i, err = To(*buff)
if err != nil {
return
}
dot.End = i + 1
var word string
// If nothing is selected, instead send 100 characters before and after
// and include a "click" attribute in the plumbing message.
var click int
if dot.Start == dot.End-1 {
if dot.Start < 100 {
click = int(dot.Start)
dot.Start = 0
} else {
click = 100
dot.Start -= 100
}
if dot.End+100 < uint(len(buff.Buffer)) {
dot.End += 100
} else {
dot.End = uint(len(buff.Buffer))
}
}
word = string(buff.Buffer[dot.Start:dot.End])
if PlumbingReady {
if err := plumb([]byte(word), buff, v, click); err == nil {
return
}
}
// the file doesn't exist, so find the next instance of word.
lenword := dot.End - dot.Start
for i := dot.End; i < uint(len(buff.Buffer))-lenword; i++ {
if string(buff.Buffer[i:i+lenword]) == word {
buff.Dot.Start = i
buff.Dot.End = i + lenword - 1
return
}
}
}
func TagPlumbOrFindNext(From, To demodel.Position, buff *demodel.CharBuffer, v demodel.Viewport) {
if buff == nil || buff.Tagline == nil {
return
}
dot := demodel.Dot{}
i, err := From(*buff)
if err != nil {
return
}
dot.Start = i
i, err = To(*buff)
if err != nil {
return
}
dot.End = i + 1
// find the word between From and To in the tagline
word := string(buff.Tagline.Buffer[dot.Start:dot.End])
if PlumbingReady {
if err := plumb([]byte(word), buff, v, 0); err == nil {
return
}
}
// the file doesn't exist, so find the next instance of word inside
// the *non-tag* buffer.
lenword := dot.End - dot.Start
for i := buff.Dot.End; i < uint(len(buff.Buffer))-lenword; i++ {
if string(buff.Buffer[i:i+lenword]) == word {
buff.Dot.Start = i
buff.Dot.End = i + lenword - 1
return
}
}
}