-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathexpandfile.go
204 lines (201 loc) · 4.17 KB
/
expandfile.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
//go:build !windows
package main
// PAL: If our q1==q0 selection is within ' chars, we check if it's a filename, otherwise fall
// back to our original selection code. Returns the quoted string.
func findquotedcontext(t *Text, q0 int) (qq0, qq1 int) {
qq0 = q0
qq1 = q0
foundquote := false
foundcolon := false
for qq0 >= 0 {
c := t.ReadC(qq0)
if c == ':' {
// We are looking for the case where the click
// happened in an address, in which case we aren't (yet)
// in a quoted context.
foundcolon = true
qq0--
continue
}
if foundcolon && !foundquote && c != '\'' {
qq0++
break
}
if c == '\'' {
if foundcolon {
foundquote = true
foundcolon = false // we no longer care about the colon
qq1 = qq0 // Make the search rightward start from within the quotes.
qq0--
continue // Keep marching leftwards;
}
foundquote = true
break
}
if !isfilec(c) && !isfilespace(c) {
return q0, q0 // No quote found leftward.
}
qq0--
}
if !foundquote {
return q0, q0
}
for qq1 < t.file.Nr() {
c := t.ReadC(qq1 - 1)
if c == '\'' {
break
}
if !isfilec(c) && !isfilespace(c) {
return q0, q0 // No quote found rightwards.
}
qq1++
}
return qq0, qq1
}
func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
amax := q1
if q1 == q0 {
// Check for being in a quoted string, find out if its a file.
qq0, qq1 := findquotedcontext(t, q0)
if qq0 != qq1 {
// Invariant: qq0 and qq1-1 are '
if expandfile(t, qq0+1, qq1-1, e) {
// We have a file. If we have a colon following our qq1+1 quote
// we have to get it and add it to Expand.
cq1 := qq1
c := t.ReadC(cq1)
if c != ':' { // We don't have any address information here. Just return e.
e.q0 = qq0
e.q1 = qq1
return true
}
cq1++
// collect the address
e.a0 = cq1
for cq1 < t.file.Nr() {
c := t.ReadC(cq1)
if !isaddrc(c) && !isregexc(c) && c != '\'' {
break
}
cq1++
}
e.a1 = cq1
q0 = qq0
q1 = cq1
e.q0 = q0
e.q1 = q1
return true
}
} else {
colon := int(-1)
// TODO(rjk): utf8 conversion work.
for q1 < t.file.Nr() {
c := t.ReadC(q1)
if !isfilec(c) {
break
}
if c == ':' {
colon = q1
break
}
q1++
}
for q0 > 0 {
c := t.ReadC(q0 - 1)
if !isfilec(c) && !isaddrc(c) && !isregexc(c) {
break
}
if colon < 0 && c == ':' {
colon = q0 - 1
}
q0--
}
// if it looks like it might begin file: , consume address chars after :
// otherwise terminate expansion at :
if colon >= 0 {
q1 = colon
if colon < t.file.Nr()-1 {
c := t.ReadC(colon + 1)
if isaddrc(c) {
q1 = colon + 1
for q1 < t.file.Nr() {
c := t.ReadC(q1)
if !isaddrc(c) {
break
}
q1++
}
}
}
}
if q1 > q0 {
if colon >= 0 { // stop at white space
for amax = colon + 1; amax < t.file.Nr(); amax++ {
c := t.ReadC(amax)
if c == ' ' || c == '\t' || c == '\n' {
break
}
}
} else {
amax = t.file.Nr()
}
}
}
}
amin := amax
e.q0 = q0
e.q1 = q1
n := q1 - q0
if n == 0 {
return false
}
// see if it's a file name
rb := make([]rune, n)
t.file.Read(q0, rb[:n])
// first, does it have bad chars?
nname := -1
for i, c := range rb {
if c == ':' && nname < 0 {
if q0+i+1 >= t.file.Nr() {
return false
}
if i != n-1 {
if cc := t.ReadC(q0 + i + 1); !isaddrc(cc) {
return false
}
}
amin = q0 + i
nname = i
}
}
if nname == -1 {
nname = n
}
for i := 0; i < nname; i++ {
if !isfilec(rb[i]) && rb[i] != ' ' {
return false
}
}
isFile := func(name string) bool {
e.name = name
e.at = t
e.a0 = amin + 1
_, _, e.a1 = address(true, nil, Range{-1, -1}, Range{0, 0}, e.a0, amax,
func(q int) rune { return t.ReadC(q) }, false)
return true
}
s := string(rb[:nname])
if amin == q0 {
return isFile(s)
}
dname := t.DirName(s)
// if it's already a window name, it's a file
if lookfile(dname) != nil {
return isFile(dname)
}
// if it's the name of a file, it's a file
if ismtpt(dname) || !access(dname) {
return false
}
return isFile(dname)
}