]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/search.go
Use the new and updated version of tcell
[micro.git] / cmd / micro / search.go
index 77c368af8461d12e78a0ae24b7ee0f36048fb547..734c0712f9b200b04d3ccc567ad7e6c837b13df4 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,11 +76,12 @@ 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 err != nil {
@@ -86,10 +91,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
                }
 
@@ -106,9 +111,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)
        }