-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.go
75 lines (70 loc) · 1.49 KB
/
search.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
package kg
const (
fwdsearch = 1
revsearch = 2
)
func (e *Editor) search() {
e.Searchtext = e.getInput("Search: ")
found := e.CurrentBuffer.searchForward(e.CurrentBuffer.Point, e.Searchtext)
e.displaySearchResult(found, fwdsearch, "Search: ", e.Searchtext)
}
func (e *Editor) rsearch() {
e.Searchtext = e.getInput("R-Search: ")
found := e.CurrentBuffer.searchBackwards(e.CurrentBuffer.Point, e.Searchtext)
e.displaySearchResult(found, revsearch, "R-Search: ", e.Searchtext)
}
func (e *Editor) displaySearchResult(found int, dir int, prompt string, search string) {
if found != -1 {
e.CurrentBuffer.SetPoint(found)
e.Display(e.CurrentWindow, true)
} else {
e.msg("Failing %s%s", prompt, search)
e.displayMsg()
}
}
func (bp *Buffer) searchForward(startp int, stext string) int {
endpt := bp.TextSize - 1
if len(stext) == 0 {
return -1
}
for p := startp; p < endpt; p++ {
s := []rune(stext)
ss := 0
pp := 0
for pp = p; pp < endpt; pp++ {
rch, _ := bp.RuneAt(pp)
if ss < len(s) && s[ss] == rch {
ss++
} else {
break
}
}
if ss == len(s) {
return pp
}
}
return -1
}
func (bp *Buffer) searchBackwards(startp int, stext string) int {
endpt := bp.TextSize - 1
if len(stext) == 0 {
return startp
}
for p := startp; p >= 0; p-- {
s := []rune(stext)
ss := 0
pp := 0
for pp = p; pp < endpt; pp++ {
rch, _ := bp.RuneAt(pp)
if ss < len(s) && s[ss] == rch {
ss++
} else {
break
}
}
if ss == len(s) {
return pp
}
}
return -1
}