]> git.lizzy.rs Git - micro.git/commitdiff
Add Search function to BufPane
authorZachary Yedidia <zyedidia@gmail.com>
Tue, 23 Jun 2020 22:47:42 +0000 (18:47 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Tue, 23 Jun 2020 22:47:42 +0000 (18:47 -0400)
internal/action/actions.go

index 9cff5de3180ec2b56bdf10c0092642e7101de845..184a57e0820feedc1bc84b66b508e25ceb91a106 100644 (file)
@@ -816,6 +816,30 @@ func (h *BufPane) FindLiteral() bool {
        return h.find(false)
 }
 
+// Search searches for a given string/regex in the buffer and selects the next
+// match if a match is found
+// This function affects lastSearch and lastSearchRegex (saved searches) for
+// use with FindNext and FindPrevious
+func (h *BufPane) Search(str string, useRegex bool, searchDown bool) error {
+       match, found, err := h.Buf.FindNext(str, h.Buf.Start(), h.Buf.End(), h.Cursor.Loc, searchDown, useRegex)
+       if err != nil {
+               return 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.GotoLoc(h.Cursor.CurSelection[1])
+               h.lastSearch = str
+               h.lastSearchRegex = useRegex
+               h.Relocate()
+       } else {
+               h.Cursor.ResetSelection()
+       }
+       return nil
+}
+
 func (h *BufPane) find(useRegex bool) bool {
        h.searchOrig = h.Cursor.Loc
        prompt := "Find: "