-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
206 lines (186 loc) · 4.55 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
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
package main
import (
//"fmt"
"regexp"
"github.com/hanspr/tcell"
)
var (
// What was the last search
lastSearch string
// Where should we start the search down from (or up from)
searchStart Loc
// Is there currently a search in progress
searching bool
// Stores the history for searching
//searchHistory []string
)
// StartSearchMode starts the current search
func StartSearchMode() {
messenger.hasPrompt = false
searching = true
messenger.Message(Language.Translate("Find") + " :" + lastSearch + " " + Language.Translate("F5,Backspace (Previous) F6,Enter (Next)"))
}
// ExitSearch exits the search mode, reset active search phrase, and clear status bar
func ExitSearch(v *View) {
lastSearch = ""
searching = false
messenger.hasPrompt = false
messenger.Clear()
messenger.Reset()
v.Cursor.ResetSelection()
}
// HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
// to the current buffer. It searches down the buffer.
func HandleSearchEvent(event tcell.Event, v *View) {
if !searching {
return
}
switch e := event.(type) {
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyF5, tcell.KeyBackspace2:
v.FindPrevious(true)
case tcell.KeyF6, tcell.KeyEnter:
v.FindNext(true)
return
default:
loc := v.Cursor.CurSelection
ExitSearch(v)
v.Cursor.GotoLoc(loc[0])
if e.Rune() == 0 {
v.HandleEvent(event)
}
return
}
}
}
func searchDown(r *regexp.Regexp, v *View, start, end Loc) bool {
var startX int
if start.Y >= v.Buf.NumLines {
start.Y = v.Buf.NumLines - 1
}
if start.Y < 0 {
start.Y = 0
}
for i := start.Y; i <= end.Y; i++ {
if i == start.Y {
startX = start.X
} else {
startX = -1
}
l := string(v.Buf.lines[i].data)
match := r.FindAllStringIndex(l, -1)
if match != nil {
for j := 0; j < len(match); j++ {
X := runePos(match[j][0], l)
Y := runePos(match[j][1], l)
if X >= startX {
v.Cursor.SetSelectionStart(Loc{X, i})
v.Cursor.SetSelectionEnd(Loc{Y, i})
v.Cursor.OrigSelection[0] = v.Cursor.CurSelection[0]
v.Cursor.OrigSelection[1] = v.Cursor.CurSelection[1]
v.Cursor.Loc = v.Cursor.CurSelection[0]
if v.Cursor.Y >= v.Bottomline()-2 || v.Cursor.Y <= v.Topline+2 {
v.Center(false)
}
return true
}
}
}
}
return false
}
func searchUp(r *regexp.Regexp, v *View, start, end Loc) bool {
var startX int
if start.Y >= v.Buf.NumLines {
start.Y = v.Buf.NumLines - 1
}
if start.Y < 0 {
start.Y = 0
}
for i := start.Y; i >= end.Y; i-- {
if i == start.Y {
startX = v.Cursor.CurSelection[0].X
} else {
startX = 9999999
}
l := string(v.Buf.lines[i].data)
match := r.FindAllStringIndex(l, -1)
if match != nil {
for j := len(match) - 1; j >= 0; j-- {
X := runePos(match[j][0], l)
Y := runePos(match[j][1], l)
if X < startX {
v.Cursor.SetSelectionStart(Loc{X, i})
v.Cursor.SetSelectionEnd(Loc{Y, i})
v.Cursor.OrigSelection[0] = v.Cursor.CurSelection[0]
v.Cursor.OrigSelection[1] = v.Cursor.CurSelection[1]
v.Cursor.Loc = v.Cursor.CurSelection[0]
if v.Cursor.Y >= v.Bottomline()-2 || v.Cursor.Y <= v.Topline+2 {
v.Center(false)
}
return true
}
}
}
}
return false
}
// Search searches in the view for the given regex. The down bool
// specifies whether it should search down from the searchStart position
// or up from there
func Search(searchStr string, v *View, down bool) bool {
if searchStr == "" {
return false
}
r, err := regexp.Compile(searchStr)
if err != nil {
return false
}
var found bool
if down {
found = searchDown(r, v, searchStart, v.Buf.End())
if !found {
v.searchLoops++
found = searchDown(r, v, v.Buf.Start(), searchStart)
}
} else {
found = searchUp(r, v, searchStart, v.Buf.Start())
if !found {
v.searchLoops++
found = searchUp(r, v, v.Buf.End(), searchStart)
}
}
if !found {
} else {
v.Relocate()
}
return found
}
// Search dialogs preview of found text
const doff int = 20
// DialogSearch display search dialog box
func DialogSearch(searchStr string) string {
if searchStr == "" {
return ""
}
r, err := regexp.Compile(searchStr)
if err != nil {
return ""
}
v := CurView()
found := searchDown(r, v, v.searchSave, v.Buf.End())
if !found {
found = searchDown(r, v, v.Buf.Start(), v.searchSave)
}
if found {
xs := v.Cursor.CurSelection
line := CurView().Buf.LineRunes(xs[0].Y)
x1 := 0
if xs[0].X-doff >= 0 {
x1 = xs[0].X - doff
}
return string(line[x1:xs[0].X]) + "{f}" + v.Cursor.GetSelection() + "{/f}" + string(line[xs[1].X:])
}
return ""
}