Implement better search functionality (not finished)

This commit is contained in:
Zachary Yedidia
2016-04-17 09:24:14 -04:00
parent 147ed61f2f
commit 35eccf26d4
2 changed files with 27 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ import (
"strings"
)
var lastSearch string
// BeginSearch starts a search
func BeginSearch() {
searching = true
@@ -48,14 +50,33 @@ func HandleSearchEvent(event tcell.Event, v *View) {
return
}
str := strings.Join(v.buf.lines[v.cursor.y:], "\n")
charPos := ToCharPos(0, v.cursor.y, v.buf)
r, err := regexp.Compile(messenger.response)
Search(messenger.response, v)
return
}
// Search searches for a given regular expression inside a view
// It also moves the cursor and highlights the search result
func Search(searchStr string, v *View) {
lines := v.buf.lines[v.cursor.y:]
var charPos int
if v.cursor.HasSelection() {
x, y := FromCharPos(v.cursor.curSelection[1]-1, v.buf)
lines = v.buf.lines[y:]
lines[0] = lines[0][x:]
charPos = ToCharPos(x, y, v.buf)
} else {
lines[0] = lines[0][v.cursor.x:]
charPos = ToCharPos(v.cursor.x, v.cursor.y, v.buf)
}
str := strings.Join(lines, "\n")
r, err := regexp.Compile(searchStr)
if err != nil {
return
}
match := r.FindStringIndex(str)
if match == nil {
// FIXME
str = strings.Join(v.buf.lines[:v.cursor.y], "\n")
match = r.FindStringIndex(str)
charPos = 0
@@ -70,5 +91,5 @@ func HandleSearchEvent(event tcell.Event, v *View) {
if v.Relocate() {
v.matches = Match(v)
}
return
lastSearch = searchStr
}

View File

@@ -428,6 +428,8 @@ func (v *View) HandleEvent(event tcell.Event) {
v.Save()
case tcell.KeyCtrlF:
BeginSearch()
case tcell.KeyCtrlN:
Search(lastSearch, v)
case tcell.KeyCtrlZ:
v.eh.Undo()
// Rehighlight the entire buffer