]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/command.go
Merge pull request #1135 from whilei/gofmt-2018-Jun-17-00-39
[micro.git] / cmd / micro / command.go
index 3af89416a2ebabad93468a914a01dc2bba186125..976880d7a159922f9da08560c23e00eee19f03ad 100644 (file)
@@ -8,6 +8,7 @@ import (
        "runtime"
        "strconv"
        "strings"
+       "unicode/utf8"
 
        humanize "github.com/dustin/go-humanize"
        "github.com/zyedidia/micro/cmd/micro/shellwords"
@@ -245,7 +246,7 @@ func Raw(args []string) {
        curTab = len(tabs) - 1
        if len(tabs) == 2 {
                for _, t := range tabs {
-                       for _, v := range t.views {
+                       for _, v := range t.Views {
                                v.ToggleTabbar()
                        }
                }
@@ -261,7 +262,7 @@ func TabSwitch(args []string) {
 
                        found := false
                        for _, t := range tabs {
-                               v := t.views[t.CurView]
+                               v := t.Views[t.CurView]
                                if v.Buf.GetName() == args[0] {
                                        curTab = v.TabNum
                                        found = true
@@ -292,7 +293,7 @@ func Cd(args []string) {
                }
                wd, _ := os.Getwd()
                for _, tab := range tabs {
-                       for _, view := range tab.views {
+                       for _, view := range tab.Views {
                                if len(view.Buf.name) == 0 {
                                        continue
                                }
@@ -443,7 +444,7 @@ func NewTab(args []string) {
                curTab = len(tabs) - 1
                if len(tabs) == 2 {
                        for _, t := range tabs {
-                               for _, v := range t.views {
+                               for _, v := range t.Views {
                                        v.ToggleTabbar()
                                }
                        }
@@ -574,6 +575,7 @@ func Replace(args []string) {
        }
 
        replace := string(args[1])
+       replaceBytes := []byte(replace)
 
        regex, err := regexp.Compile("(?m)" + search)
        if err != nil {
@@ -587,23 +589,16 @@ func Replace(args []string) {
        found := 0
        replaceAll := func() {
                var deltas []Delta
-               deltaXOffset := Count(replace) - Count(search)
                for i := 0; i < view.Buf.LinesNum(); i++ {
-                       matches := regex.FindAllIndex(view.Buf.lines[i].data, -1)
-                       str := string(view.Buf.lines[i].data)
-
-                       if matches != nil {
-                               xOffset := 0
-                               for _, m := range matches {
-                                       from := Loc{runePos(m[0], str) + xOffset, i}
-                                       to := Loc{runePos(m[1], str) + xOffset, i}
+                       newText := regex.ReplaceAllFunc(view.Buf.lines[i].data, func(in []byte) []byte {
+                               found++
+                               return replaceBytes
+                       })
 
-                                       xOffset += deltaXOffset
+                       from := Loc{0, i}
+                       to := Loc{utf8.RuneCount(view.Buf.lines[i].data), i}
 
-                                       deltas = append(deltas, Delta{replace, from, to})
-                                       found++
-                               }
-                       }
+                       deltas = append(deltas, Delta{string(newText), from, to})
                }
                view.Buf.MultipleReplace(deltas)
        }