Fix some search bugs

This commit is contained in:
Zachary Yedidia 2019-06-15 14:44:03 -04:00
parent 74ee256260
commit f5f4154d4c
4 changed files with 29 additions and 13 deletions

View file

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/go-errors/errors"
@ -69,7 +70,14 @@ func InitFlags() {
if *flagOptions {
// If -options was passed
for k, v := range config.DefaultGlobalSettings() {
var keys []string
m := config.DefaultGlobalSettings()
for k, _ := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := m[k]
fmt.Printf("-%s value\n", k)
fmt.Printf(" \tDefault value: '%v'\n", v)
}

View file

@ -599,21 +599,25 @@ func (h *BufPane) saveBufToFile(filename string) {
// Find opens a prompt and searches forward for the input
func (h *BufPane) Find() bool {
h.searchOrig = h.Cursor.Loc
InfoBar.Prompt("Find: ", "", "Find", func(resp string) {
// Event callback
match, found, _ := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.Cursor.Loc, true, true)
match, found, _ := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, true)
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.GotoLoc(match[1])
} else {
h.Cursor.GotoLoc(h.searchOrig)
h.Cursor.ResetSelection()
}
h.Relocate()
}, func(resp string, canceled bool) {
// Finished callback
if !canceled {
match, found, err := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.Cursor.Loc, true, true)
match, found, err := h.Buf.FindNext(resp, h.Buf.Start(), h.Buf.End(), h.searchOrig, true, true)
if err != nil {
InfoBar.Error(err)
}
@ -622,7 +626,7 @@ func (h *BufPane) Find() bool {
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]
h.Cursor.GotoLoc(h.Cursor.CurSelection[1])
h.lastSearch = resp
} else {
h.Cursor.ResetSelection()
@ -631,9 +635,10 @@ func (h *BufPane) Find() bool {
} else {
h.Cursor.ResetSelection()
}
h.Relocate()
})
return true
return false
}
// FindNext searches forwards for the last used search term

View file

@ -104,6 +104,9 @@ type BufPane struct {
multiWord bool
splitID uint64
// remember original location of a search in case the search is canceled
searchOrig buffer.Loc
}
func NewBufPane(buf *buffer.Buffer, win display.BWindow) *BufPane {

View file

@ -21,19 +21,19 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
if i == start.Y && start.Y == end.Y {
nchars := utf8.RuneCount(l)
start.X = util.Clamp(start.X, 0, nchars-1)
end.X = util.Clamp(end.X, 0, nchars-1)
start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == start.Y {
nchars := utf8.RuneCount(l)
start.X = util.Clamp(start.X, 0, nchars-1)
start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == end.Y {
nchars := utf8.RuneCount(l)
end.X = util.Clamp(end.X, 0, nchars-1)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
}
@ -62,19 +62,19 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
if i == start.Y && start.Y == end.Y {
nchars := utf8.RuneCount(l)
start.X = util.Clamp(start.X, 0, nchars-1)
end.X = util.Clamp(end.X, 0, nchars-1)
start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == start.Y {
nchars := utf8.RuneCount(l)
start.X = util.Clamp(start.X, 0, nchars-1)
start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == end.Y {
nchars := utf8.RuneCount(l)
end.X = util.Clamp(end.X, 0, nchars-1)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
}