]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/search.go
Update docs and readme
[micro.git] / cmd / micro / search.go
index 227e3af9e370141dd09722803680436208eb5e1a..966184715e355505b9605bf9da0c6eccd974c15f 100644 (file)
@@ -15,10 +15,15 @@ var (
 
        // Is there currently a search in progress
        searching bool
+
+       // Stores the history for searching
+       searchHistory []string
 )
 
 // BeginSearch starts a search
 func BeginSearch() {
+       searchHistory = append(searchHistory, "")
+       messenger.historyNum = len(searchHistory) - 1
        searching = true
        messenger.hasPrompt = true
        messenger.Message("Find: ")
@@ -26,6 +31,7 @@ func BeginSearch() {
 
 // EndSearch stops the current search
 func EndSearch() {
+       searchHistory[len(searchHistory)-1] = messenger.response
        searching = false
        messenger.hasPrompt = false
        messenger.Clear()
@@ -48,7 +54,7 @@ func HandleSearchEvent(event tcell.Event, v *View) {
                }
        }
 
-       messenger.HandleEvent(event)
+       messenger.HandleEvent(event, searchHistory)
 
        if messenger.cursorx < 0 {
                // Done
@@ -78,13 +84,13 @@ func Search(searchStr string, v *View, down bool) {
        var charPos int
        text := v.Buf.String()
        if down {
-               str = text[searchStart:]
+               str = string([]rune(text)[searchStart:])
                charPos = searchStart
        } else {
-               str = text[:searchStart]
+               str = string([]rune(text)[:searchStart])
        }
        r, err := regexp.Compile(searchStr)
-       if settings["ignorecase"].(bool) {
+       if v.Buf.Settings["ignorecase"].(bool) {
                r, err = regexp.Compile("(?i)" + searchStr)
        }
        if err != nil {
@@ -106,6 +112,7 @@ func Search(searchStr string, v *View, down bool) {
                } else {
                        match = matches[0]
                }
+               str = text
        }
 
        if !down {
@@ -118,9 +125,9 @@ func Search(searchStr string, v *View, down bool) {
                return
        }
 
-       v.Cursor.curSelection[0] = charPos + match[0]
-       v.Cursor.curSelection[1] = charPos + match[1]
-       v.Cursor.x, v.Cursor.y = FromCharPos(charPos+match[1]-1, v.Buf)
+       v.Cursor.SetSelectionStart(FromCharPos(charPos+runePos(match[0], str), v.Buf))
+       v.Cursor.SetSelectionEnd(FromCharPos(charPos+runePos(match[1], str), v.Buf))
+       v.Cursor.Loc = v.Cursor.CurSelection[1]
        if v.Relocate() {
                v.matches = Match(v)
        }