]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/search.go
Fix draw ordering
[micro.git] / cmd / micro / search.go
index 77c368af8461d12e78a0ae24b7ee0f36048fb547..ddf77aef660244b529816a18ce2ffc7200b6de63 100644 (file)
@@ -1,8 +1,9 @@
 package main
 
 import (
-       "github.com/zyedidia/tcell"
        "regexp"
+
+       "github.com/zyedidia/tcell"
 )
 
 var (
@@ -14,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: ")
@@ -25,10 +31,14 @@ func BeginSearch() {
 
 // EndSearch stops the current search
 func EndSearch() {
+       searchHistory[len(searchHistory)-1] = messenger.response
        searching = false
        messenger.hasPrompt = false
        messenger.Clear()
        messenger.Reset()
+       if lastSearch != "" {
+               messenger.Message("^P Previous ^N Next")
+       }
 }
 
 // HandleSearchEvent takes an event and a view and will do a real time match from the messenger's output
@@ -44,7 +54,7 @@ func HandleSearchEvent(event tcell.Event, v *View) {
                }
        }
 
-       messenger.HandleEvent(event)
+       messenger.HandleEvent(event, searchHistory)
 
        if messenger.cursorx < 0 {
                // Done
@@ -53,7 +63,7 @@ func HandleSearchEvent(event tcell.Event, v *View) {
        }
 
        if messenger.response == "" {
-               v.cursor.ResetSelection()
+               v.Cursor.ResetSelection()
                // We don't end the search though
                return
        }
@@ -72,13 +82,17 @@ func Search(searchStr string, v *View, down bool) {
        }
        var str string
        var charPos int
+       text := v.Buf.String()
        if down {
-               str = v.buf.text[searchStart:]
+               str = text[searchStart:]
                charPos = searchStart
        } else {
-               str = v.buf.text[:searchStart]
+               str = text[:searchStart]
        }
        r, err := regexp.Compile(searchStr)
+       if settings["ignorecase"].(bool) {
+               r, err = regexp.Compile("(?i)" + searchStr)
+       }
        if err != nil {
                return
        }
@@ -86,10 +100,10 @@ func Search(searchStr string, v *View, down bool) {
        var match []int
        if matches == nil {
                // Search the entire buffer now
-               matches = r.FindAllStringIndex(v.buf.text, -1)
+               matches = r.FindAllStringIndex(text, -1)
                charPos = 0
                if matches == nil {
-                       v.cursor.ResetSelection()
+                       v.Cursor.ResetSelection()
                        return
                }
 
@@ -98,6 +112,7 @@ func Search(searchStr string, v *View, down bool) {
                } else {
                        match = matches[0]
                }
+               str = text
        }
 
        if !down {
@@ -106,9 +121,13 @@ func Search(searchStr string, v *View, down bool) {
                match = matches[0]
        }
 
-       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)
+       if match[0] == match[1] {
+               return
+       }
+
+       v.Cursor.CurSelection[0] = FromCharPos(charPos+runePos(match[0], str), v.Buf)
+       v.Cursor.CurSelection[1] = FromCharPos(charPos+runePos(match[1], str), v.Buf)
+       v.Cursor.Loc = FromCharPos(charPos+match[1]-1, v.Buf)
        if v.Relocate() {
                v.matches = Match(v)
        }