]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/search.go
Fix crash
[micro.git] / cmd / micro / search.go
index d457ba1b1856335ae297c6ce3ca2fbbae876058c..227e3af9e370141dd09722803680436208eb5e1a 100644 (file)
@@ -1,8 +1,9 @@
 package main
 
 import (
-       "github.com/zyedidia/tcell"
        "regexp"
+
+       "github.com/zyedidia/tcell"
 )
 
 var (
@@ -29,6 +30,9 @@ func EndSearch() {
        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
@@ -53,7 +57,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,7 +76,7 @@ func Search(searchStr string, v *View, down bool) {
        }
        var str string
        var charPos int
-       text := v.buf.String()
+       text := v.Buf.String()
        if down {
                str = text[searchStart:]
                charPos = searchStart
@@ -80,6 +84,9 @@ func Search(searchStr string, v *View, down bool) {
                str = text[:searchStart]
        }
        r, err := regexp.Compile(searchStr)
+       if settings["ignorecase"].(bool) {
+               r, err = regexp.Compile("(?i)" + searchStr)
+       }
        if err != nil {
                return
        }
@@ -90,7 +97,7 @@ func Search(searchStr string, v *View, down bool) {
                matches = r.FindAllStringIndex(text, -1)
                charPos = 0
                if matches == nil {
-                       v.cursor.ResetSelection()
+                       v.Cursor.ResetSelection()
                        return
                }
 
@@ -107,9 +114,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] = charPos + match[0]
+       v.Cursor.curSelection[1] = charPos + match[1]
+       v.Cursor.x, v.Cursor.y = FromCharPos(charPos+match[1]-1, v.Buf)
        if v.Relocate() {
                v.matches = Match(v)
        }