-
Notifications
You must be signed in to change notification settings - Fork 25
/
look.go
326 lines (281 loc) · 5.88 KB
/
look.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
package main
import (
"image"
"os"
"path/filepath"
"github.com/as/event"
"github.com/as/srv/fs"
"github.com/as/text"
"github.com/as/text/action"
"github.com/as/text/find"
"github.com/as/ui/col"
"github.com/as/ui/tag"
)
type Named interface {
Plane
FileName() string
}
type Indexer interface {
Lookup(interface{}) Plane
}
func AbsOf(basedir, path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(basedir, path)
}
var resolver = &fs.Resolver{
Fs: newfsclient(),
}
type vis struct {
}
func (v *vis) Look(e event.Look) {
}
func (g *Grid) cwd() string {
s, _ := os.Getwd()
return s
}
// hiclick returns true if the nil address (q0,q1) intersects
// the highlighted selection (r0:r1)
func hiclick(r0, r1, q0, q1 int64) bool {
x := r0 == r1 && text.Region3(r0, q0-1, q1) == 0
return x
}
func (g *Grid) Look(e event.Look) {
if g.meta(g.Tag) {
return
}
ed := e.To[0]
t, _ := ed.(*tag.Tag)
p0, p1 := e.From.Dot() // pre-sweep
// e.Q0 and e.Q1 is post sweep
if e.Q0 == e.Q1 && !hiclick(e.Q0, e.Q1, p0, p1) {
// one click outside old selection
// expand the address
a1 := expandFile(d2a(e.Q0, e.Q1), e.From)
e.Q0, e.Q1 = a1.Dot()
} else if e.Q0 == e.Q1 {
// click inside old selection
// use the old selection
e.Q0, e.Q1 = p0, p1
} else {
// selection overlaps old selection
// we don't care about that
}
//fmt.Printf("name and dot %q %d %d\n", e.P, e.Q0, e.Q1)
e.P = e.P[e.Q0:e.Q1] //ed.Bytes()
name, addr := action.SplitPath(string(e.P))
if name == "" && addr == "" {
return
}
if matches(httpLink.Plumb(&Plumbmsg{Data: e.P})) {
return
}
if name == "" {
if t == nil {
return
}
if t.Window == nil {
return
}
if g.EditRun(addr, t.Window) {
ajump(ed, cursorNop)
}
return
}
if label := g.Lookup(name); label != nil {
// TODO(as): This fails to find labels like +Error
// and is overall useless
t, _ := label.(*tag.Tag)
if t == nil {
logf("nil tag")
return
}
if t.Window == nil || !g.EditRun(addr, t.Window) {
ajump(t, cursorNop)
return
} else if t.Window != nil {
ajump(t.Window, moveMouse)
return
}
ajump(t, moveMouse)
return
}
exists := false
info, existsRemote := resolver.Look(fs.Path{Root: g.cwd(), Tag: e.Name, Pred: name})
t, exists = g.Lookup(info.Path).(*tag.Tag)
if !exists && existsRemote {
t, _ = New(actCol, info.Dir, info.Path).(*tag.Tag)
getcmd(t)
}
if t != nil {
if g.EditRun(addr, t.Window) {
ajump(t.Window, moveMouse)
} else {
ajump(t, moveMouse)
}
return
}
if !exists && !existsRemote {
advance, _ := g.guru(e.Name, e.Q0, e.Q1)
if !advance {
return
}
}
//TODO(as): fix this so it doesn't compare hard coded coordinates
if e.To[0] == nil {
VisitAll(g, func(p Named) {
if p == nil {
return
}
lookliteral(p.(*tag.Tag).Window, e, cursorNop)
})
} else {
if e.To[0] != e.From {
lookliteraltag(e.To[0], e.Q0, e.Q1, e.P)
} else {
lookliteral(e.To[0], e, moveMouse)
}
}
}
func stub(g *Grid, p Plane) bool {
if p == nil {
return false
}
if p == g.Tag {
return true
}
l := g.Kids()
for i := range l {
c, _ := l[i].(*col.Col)
if c != nil && p == c.Tag {
return true
}
}
return false
}
func lookliteraltag(ed text.Editor, q0, q1 int64, what []byte) {
q0, q1 = ed.Dot()
s0, s1 := find.FindNext(ed, q0, q1, what)
ed.Select(s0, s1)
ajump(ed, cursorNop)
}
func lookliteral(ed text.Editor, e event.Look, mouseFunc func(image.Point)) {
// The behavior of look:
//
// Independent of the dot range, mark the given range as the starting point.
// Advance to the end of the starting point
// Search for the value repsesenting the range under the original starting point.
// If the found range is identical to the starting point, no result has been found
t0, t1 := ed.Dot()
q0, q1 := find.FindNext(ed, e.Q1, e.Q1, e.P)
//fmt.Printf("after q0,q1: %d,%d\n\n\t%q\n", e.Q0, e.Q1, e.P)
if q0 == e.Q0 && q1 == e.Q1 {
ed.Select(t0, t1)
return
}
ed.Select(q0, q1)
ajump(ed, mouseFunc)
}
func (g *Grid) meta(p interface{}) bool {
return p == g.Tag.Label
}
func VisitAll(root Plane, fn func(p Named)) {
type List interface {
Kids() []Plane
}
switch root := root.(type) {
case List:
for _, k := range root.Kids() {
VisitAll(k, fn)
}
case Named:
fn(root)
case Plane:
case interface{}:
panic("bad visitor")
}
}
func (g *Grid) FindName(name string) *tag.Tag {
for _, p := range g.List {
c, _ := p.(*Col)
if c == nil {
continue
}
t := c.FindName(name)
if t != nil {
return t
}
}
return nil
}
func (g *Grid) Lookup(pid interface{}) Plane {
for _, k := range g.Kids() {
if k, ok := k.(Indexer); ok {
tag := k.Lookup(pid)
if tag != nil {
return tag
}
}
}
return nil
}
/*
type Looker struct {
*tag.Tag // owning tag
*win.Win // source of the address below
Q0, Q1 int64
P []byte
err error
}
func (e *Looker) Err() error {
if e.err == nil {
if e.Win == nil {
e.err = ErrNoWin
}
if e.Tag == nil {
e.err = ErrNoTag
}
}
return e.err
}
func (l *Looker) FromTag() bool {
return l.Tag.Win == l.Win
}
func (e *Looker) SplitAddr() (name, addr string) {
e.Q0, e.Q1 = expand3(e.Win, e.Q0, e.Q1)
return action.SplitPath(string(e.Win.Bytes()[e.Q0:e.Q1]))
}
func (e *Looker) LookGrid(g *Grid) (error) {
panic("unfinished")
if e.Err() != nil {
return e.Err()
}
name, addr := e.SplitAddr()
if name == "" && addr == "" {
return nil
}
if name == "" {
if g.EditRun(addr, e.Tag.Window) {
ajump(e.Tag.Window, cursorNop)
}
return nil
}
// Existing window label?
if label := g.Lookup(name); label != nil {
t, _ := label.(*tag.Tag)
if t == nil{
logf("look d: tag is nil")
return
}
if g.EditRun(addr, t.Window) {
ajump(t.Window, moveMouse)
}
return
}
// A file on the filesystem
info, exists := resolver.look(pathinfo{tag: e.Name, name: name})
t, exists = g.Lookup(info.abspath).(*tag.Tag)
panic("unfinished")
}
*/