-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathexpandfile.go
234 lines (228 loc) · 5.01 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
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
//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
}
// TODO(rjk): Should give up at a max filename length.
for qq1 < t.file.Nr() {
// TODO(rjk): why -1?
c := t.ReadC(qq1)
// c := t.ReadC(qq1 - 1)
if c == '\'' {
return qq0, qq1 + 1
}
if !isfilec(c) && !isfilespace(c) {
return q0, q0 // No quote found rightwards.
}
qq1++
}
return qq0, qq1
}
// isselectionquoted tests if a selection is a file name in quotes.
func isselectionquotedfilename(t *Text, q0, q1 int) (int, int) {
if c := t.ReadC(q0); c != '\'' {
// Definitely not a filename in single quotes as selection doesn't
// start with a single quote.
return q0, q0
}
qq0, qq1 := findquotedcontext(t, q0+1)
if qq0 == q0 && qq1 == q1 {
return qq0, qq1
}
return q0, q0
}
// quotedcontexthelper handles the situation where we have a file. If we
// have a colon following our qq1+1 quote we have to get it and add it to
// Expand.
func quotedcontexthelper(t *Text, e *Expand, qq0, qq1 int) bool {
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
e.q0 = qq0
e.q1 = cq1
return true
}
func expandfile(t *Text, q0 int, q1 int, e *Expand) 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) {
return quotedcontexthelper(t, e, qq0, qq1)
}
} 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()
}
}
}
} else {
// There is a selection. It might contain quotes wrapping a filename.
qq0, qq1 := isselectionquotedfilename(t, q0, q1)
if qq0 != qq1 {
// Invariant: qq0 and qq1-1 are '
if expandfile(t, qq0+1, qq1-1, e) {
return quotedcontexthelper(t, e, qq0, qq1)
}
}
}
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)
}