From bf08642940abf4a3ae10630af67f5d9f43881d34 Mon Sep 17 00:00:00 2001 From: matthias314 Date: Thu, 12 Dec 2024 18:53:05 -0500 Subject: [PATCH] made `FindNext` and `FindPrevious` work with empty matches --- internal/action/actions.go | 27 +++++++++++++++++++++++++++ internal/buffer/buffer.go | 1 + 2 files changed, 28 insertions(+) diff --git a/internal/action/actions.go b/internal/action/actions.go index 0256865f9..68b7fa034 100644 --- a/internal/action/actions.go +++ b/internal/action/actions.go @@ -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() @@ -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() @@ -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() @@ -1213,11 +1216,23 @@ func (h *BufPane) FindNext() bool { InfoBar.Error(err) } if found { + if h.Buf.LastMatch[1] == match[0] && match[0] == match[1] { + // skip empty match right after previous match + 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() } @@ -1242,11 +1257,23 @@ func (h *BufPane) FindPrevious() bool { InfoBar.Error(err) } if found { + if h.Buf.LastMatch[0] == match[1] && match[0] == match[1] { + // skip empty match right after previous match + 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() } diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index c623fd586..58a6dfde3 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -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 }