Skip to content

Commit

Permalink
made FindNext and FindPrevious work with empty matches
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias314 committed Dec 13, 2024
1 parent 8cdf68b commit b8e5022
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ func (h *BufPane) Search(str string, useRegex bool, searchDown bool) error {
h.GotoLoc(h.Cursor.CurSelection[1])
h.Buf.LastSearch = str
h.Buf.LastSearchRegex = useRegex
h.Buf.LastMatch = match
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
} else {
h.Cursor.ResetSelection()
Expand All @@ -1127,6 +1128,7 @@ func (h *BufPane) find(useRegex bool) bool {
h.Cursor.OrigSelection[0] = h.Cursor.CurSelection[0]
h.Cursor.OrigSelection[1] = h.Cursor.CurSelection[1]
h.GotoLoc(match[1])
h.Buf.LastMatch = match
} else {
h.GotoLoc(h.searchOrig)
h.Cursor.ResetSelection()
Expand All @@ -1148,6 +1150,7 @@ func (h *BufPane) find(useRegex bool) bool {
h.GotoLoc(h.Cursor.CurSelection[1])
h.Buf.LastSearch = resp
h.Buf.LastSearchRegex = useRegex
h.Buf.LastMatch = match
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool)
} else {
h.Cursor.ResetSelection()
Expand Down Expand Up @@ -1213,11 +1216,22 @@ func (h *BufPane) FindNext() bool {
InfoBar.Error(err)
}
if found {
if h.Buf.LastMatch[1] == match[0] && match[0] == match[1] {
h.Cursor.ResetSelection()
h.Buf.LastMatch = [2]buffer.Loc{buffer.Loc{-1, -1}, buffer.Loc{-1, -1}}
if match[1] == h.Buf.End() {
h.GotoLoc(h.Buf.Start())
} else {
h.GotoLoc(match[1].Move(1, h.Buf))
}
return h.FindNext()
}
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.GotoLoc(h.Cursor.CurSelection[1])
h.Buf.LastMatch = match
} else {
h.Cursor.ResetSelection()
}
Expand All @@ -1242,11 +1256,22 @@ func (h *BufPane) FindPrevious() bool {
InfoBar.Error(err)
}
if found {
if h.Buf.LastMatch[0] == match[1] && match[0] == match[1] {
h.Cursor.ResetSelection()
h.Buf.LastMatch = [2]buffer.Loc{buffer.Loc{-1, -1}, buffer.Loc{-1, -1}}
if match[0] == h.Buf.Start() {
h.GotoLoc(h.Buf.End())
} else {
h.GotoLoc(match[0].Move(-1, h.Buf))
}
return h.FindPrevious()
}
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.GotoLoc(h.Cursor.CurSelection[1])
h.Buf.LastMatch = match
} else {
h.Cursor.ResetSelection()
}
Expand Down
1 change: 1 addition & 0 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type Buffer struct {
// Last search stores the last successful search
LastSearch string
LastSearchRegex bool
LastMatch [2]Loc
// HighlightSearch enables highlighting all instances of the last successful search
HighlightSearch bool
}
Expand Down

0 comments on commit b8e5022

Please sign in to comment.