Find next and prev

This commit is contained in:
Zachary Yedidia
2019-01-03 15:35:24 -05:00
parent 37a4cbfd98
commit 6cf09f9843
2 changed files with 49 additions and 2 deletions

View File

@@ -1,7 +1,6 @@
package action package action
import ( import (
"log"
"os" "os"
"strings" "strings"
"time" "time"
@@ -562,6 +561,7 @@ func (h *BufHandler) SaveAs() bool {
// Find opens a prompt and searches forward for the input // Find opens a prompt and searches forward for the input
func (h *BufHandler) Find() bool { func (h *BufHandler) Find() bool {
InfoBar.Prompt("Find: ", "", func(resp string) { InfoBar.Prompt("Find: ", "", func(resp string) {
// Event callback
match, found, _ := h.Buf.FindNext(resp, h.Cursor.Loc, true) match, found, _ := h.Buf.FindNext(resp, h.Cursor.Loc, true)
if found { if found {
h.Cursor.SetSelectionStart(match[0]) h.Cursor.SetSelectionStart(match[0])
@@ -569,10 +569,10 @@ func (h *BufHandler) Find() bool {
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
} else { } else {
log.Println("RESET")
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
} }
}, func(resp string, canceled bool) { }, func(resp string, canceled bool) {
// Finished callback
if !canceled { if !canceled {
match, found, err := h.Buf.FindNext(resp, h.Cursor.Loc, true) match, found, err := h.Buf.FindNext(resp, h.Cursor.Loc, true)
if err != nil { if err != nil {
@@ -584,8 +584,10 @@ func (h *BufHandler) Find() bool {
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0] h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1] h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.Cursor.Loc = h.Cursor.CurSelection[1] h.Cursor.Loc = h.Cursor.CurSelection[1]
h.lastSearch = resp
} else { } else {
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
InfoBar.Message("No matches found")
} }
} else { } else {
h.Cursor.ResetSelection() h.Cursor.ResetSelection()
@@ -597,11 +599,53 @@ func (h *BufHandler) Find() bool {
// FindNext searches forwards for the last used search term // FindNext searches forwards for the last used search term
func (h *BufHandler) FindNext() bool { func (h *BufHandler) FindNext() bool {
// If the cursor is at the start of a selection and we search we want
// to search from the end of the selection in the case that
// the selection is a search result in which case we wouldn't move at
// at all which would be bad
searchLoc := h.Cursor.Loc
if h.Cursor.HasSelection() {
searchLoc = h.Cursor.CurSelection[1]
}
match, found, err := h.Buf.FindNext(h.lastSearch, searchLoc, true)
if err != nil {
InfoBar.Error(err)
}
if found {
h.Cursor.SetSelectionStart(match[0])
h.Cursor.SetSelectionEnd(match[1])
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.Cursor.Loc = h.Cursor.CurSelection[1]
} else {
h.Cursor.ResetSelection()
}
return true return true
} }
// FindPrevious searches backwards for the last used search term // FindPrevious searches backwards for the last used search term
func (h *BufHandler) FindPrevious() bool { func (h *BufHandler) FindPrevious() bool {
// If the cursor is at the end of a selection and we search we want
// to search from the beginning of the selection in the case that
// the selection is a search result in which case we wouldn't move at
// at all which would be bad
searchLoc := h.Cursor.Loc
if h.Cursor.HasSelection() {
searchLoc = h.Cursor.CurSelection[0]
}
match, found, err := h.Buf.FindNext(h.lastSearch, searchLoc, false)
if err != nil {
InfoBar.Error(err)
}
if found {
h.Cursor.SetSelectionStart(match[0])
h.Cursor.SetSelectionEnd(match[1])
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.Cursor.Loc = h.Cursor.CurSelection[1]
} else {
h.Cursor.ResetSelection()
}
return true return true
} }

View File

@@ -87,6 +87,9 @@ type BufHandler struct {
doubleClick bool doubleClick bool
// Same here, just to keep track for mouse move events // Same here, just to keep track for mouse move events
tripleClick bool tripleClick bool
// Last search stores the last successful search for FindNext and FindPrev
lastSearch string
} }
func NewBufHandler(buf *buffer.Buffer, win display.Window) *BufHandler { func NewBufHandler(buf *buffer.Buffer, win display.Window) *BufHandler {