Skip to content

Commit e347c3d

Browse files
Fix expandfile windows (#492)
* Fix expand file for windows q0 within the found file selection test was broken. Also, try peeling off trailing words that aren't parts of a found filename. * Fix possible infinite loop on filename trimming --------- Co-authored-by: Paul Lalonde <[email protected]>
1 parent b54e04d commit e347c3d

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

Diff for: expandfile_win.go

+42-13
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,20 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
9595
if found == nil {
9696
found = filenameRE.FindForward(rb, 0, n, 1)
9797
}
98+
matchedone := false
9899
for i, pair := range found {
99100
// qq0 is zero of the range returned.
100101
// The match is thus at qq0+pair[0:1], and (q0-qq0) must fall in that range
101102
if q0-qq0 >= pair[0] && q0-qq0 <= pair[1] {
102103
q0, q1 = pair[0]+qq0, pair[1]+qq0
103104
found = found[i : i+1]
105+
matchedone = true
104106
break
105107
}
106108
}
109+
if !matchedone {
110+
return false
111+
}
107112
} else {
108113
n = q1 - q0
109114
if n == 0 {
@@ -147,18 +152,42 @@ func expandfile(t *Text, q0 int, q1 int, e *Expand) (success bool) {
147152
func(q int) rune { return t.ReadC(q) }, false)
148153
return true
149154
}
150-
s := filename
151-
if amin == q0 {
152-
return isFile(filename)
153-
}
154-
dname := t.DirName(s)
155-
// if it's already a window name, it's a file
156-
if lookfile(dname) != nil {
157-
return isFile(dname)
158-
}
159-
// if it's the name of a file, it's a file
160-
if ismtpt(dname) || !access(dname) {
161-
return false
155+
156+
// we iterate over the candidate filename, removing trailing words
157+
for {
158+
dname := t.DirName(filename)
159+
switch {
160+
case amin == q0:
161+
if isFile(filename) {
162+
return true
163+
}
164+
// if it's already a window name, it's a file
165+
case lookfile(dname) != nil:
166+
if isFile(dname) {
167+
return true
168+
}
169+
// if it's the name of a file, it's a file
170+
case ismtpt(dname) || !access(dname):
171+
return false
172+
}
173+
174+
if isFile(dname) {
175+
return true
176+
}
177+
var q1 int
178+
for q1 = e.q1; q1 >= e.q0; q1-- {
179+
if rb[q1-1] == '\\' || rb[q1-1] == '/' {
180+
// don't back up past a path separator
181+
return false
182+
}
183+
if rb[q1-1] == ' ' || rb[q1-1] == '\t' {
184+
filename = string(rb[e.q0:e.q1])
185+
e.q1 = q1
186+
break
187+
}
188+
}
189+
if q1 < e.q0 {
190+
return false
191+
}
162192
}
163-
return isFile(dname)
164193
}

0 commit comments

Comments
 (0)