From 35eccf26d4be4a54a4d4f584f51765c69316f5b3 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 17 Apr 2016 09:24:14 -0400 Subject: [PATCH] Implement better search functionality (not finished) --- src/search.go | 29 +++++++++++++++++++++++++---- src/view.go | 2 ++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/search.go b/src/search.go index ec1fe783..469dfd88 100644 --- a/src/search.go +++ b/src/search.go @@ -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 } diff --git a/src/view.go b/src/view.go index 51a433d3..f15d0939 100644 --- a/src/view.go +++ b/src/view.go @@ -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