-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
180 lines (168 loc) · 4.11 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
package ziti
import (
termbox "github.com/nsf/termbox-go"
)
/* =============================== Find mode ================================ */
func (e *editor) runeAt(r, c int) rune {
return e.cb.rows[r].runes[c]
}
func (e *editor) searchForward(startr, startc int, stext string) (fr, rc int) {
if len(stext) == 0 {
return -1, -1
}
s := []rune(stext)
ss := 0
c := startc
r := startr
for r < e.cb.numrows {
for c < e.cb.rows[r].size {
rch := e.runeAt(r, c)
if ss < len(s) && s[ss] == rch {
ss++
} else {
if ss == len(s) {
//log.Println("found", r, c)
return r, c
}
if s[ss] != rch {
ss = 0
}
}
c++
}
c = 0
r++
ss = 0
}
return -1, -1
}
func reverse(s string) []rune {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return r
}
func (e *editor) searchBackwards(startr, startc int, stext string) (fr, fc int) {
if len(stext) == 0 {
return -1, -1
}
s := reverse(stext)
ss := 0
c := startc
r := startr
//log.Printf("before lop r %d c %d\n", r, c)
for r > -1 {
//rintf("r is %d\n", r)
for c >= 0 && c < e.cb.rows[r].size {
rch := e.runeAt(r, c)
if ss < len(s) && s[ss] == rch {
fr = r
fc = c
ss++
} else if s[ss] != rch {
ss = 0
}
if ss == len(s) {
//log.Println("found", r, c)
return fr, fc
}
c--
}
if r > 0 {
r--
c = e.cb.rows[r].size - 1
} else {
break
}
ss = 0
}
return -1, -1
}
func (e *editor) editorFind() {
query := ""
startrow := e.cb.point.ro + e.cb.point.r
startcol := e.cb.point.co + e.cb.point.c
lastLineMatch := startrow /* Last line where a match was found. -1 for none. */
lastColMatch := startcol // last column where a match was found
findDirection := 1 /* if 1 search next, if -1 search prev. */
/* Save the cursor position in order to restore it later. */
savedCx, savedCy := e.cb.point.c, e.cb.point.r
savedColoff, savedRowoff := e.cb.point.co, e.cb.point.ro
mesg := "Search: %s (Use Esc/Arrows/Enter)"
for {
e.editorSetStatusMessage(mesg, query)
e.editorRefreshScreen(false)
ev := <-e.events
if ev.Ch != 0 {
ch := ev.Ch
query = query + string(ch)
lastLineMatch, lastColMatch = startrow, startcol
findDirection = 1
}
if ev.Ch == 0 {
switch ev.Key {
case termbox.KeyTab:
query = query + string('\t')
lastLineMatch, lastColMatch = startrow, startcol
findDirection = 1
case termbox.KeySpace:
query = query + string(' ')
lastLineMatch, lastColMatch = startrow, startcol
findDirection = 1
case termbox.KeyEnter, termbox.KeyCtrlR:
e.editorSetStatusMessage("")
return
case termbox.KeyCtrlC:
e.editorSetStatusMessage("killed.")
return
case termbox.KeyBackspace2, termbox.KeyBackspace:
if len(query) > 0 {
query = query[:len(query)-1]
} else {
query = ""
}
lastLineMatch, lastColMatch = startrow, startcol
findDirection = -1
case termbox.KeyCtrlG, termbox.KeyEsc:
e.cb.point.c, e.cb.point.r = savedCx, savedCy
e.cb.point.co, e.cb.point.ro = savedColoff, savedRowoff
e.editorSetStatusMessage("")
return
case termbox.KeyArrowDown, termbox.KeyArrowRight:
findDirection = 1
case termbox.KeyArrowLeft, termbox.KeyArrowUp:
findDirection = -1
default:
e.editorSetStatusMessage(mesg, query)
e.editorRefreshScreen(false)
termbox.SetCursor(len(mesg)+len(query), e.screenrows+1)
findDirection = 0
}
}
if findDirection != 0 {
currentLine, matchOffset := -1, -1
if findDirection == 1 {
currentLine, matchOffset = e.searchForward(lastLineMatch, lastColMatch, query)
}
if findDirection == -1 {
currentLine, matchOffset = e.searchBackwards(lastLineMatch, lastColMatch, query)
}
//findDirection = 0
if currentLine != -1 {
lastLineMatch = currentLine
lastColMatch = matchOffset
e.cb.point.r = 0
e.cb.point.c = matchOffset
e.cb.point.ro = currentLine
e.cb.point.co = 0
/* Scroll horizontally as needed. */
if e.cb.point.c > e.screencols {
diff := e.cb.point.c - e.screencols
e.cb.point.c -= diff
e.cb.point.co += diff
}
}
}
}
}