From b13c6c4892a344094a98eb28e906cc1efcdb09c4 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Thu, 13 Oct 2016 14:59:57 -0400 Subject: [PATCH] Fix problem with regexes in search and replace Fixes #410 --- cmd/micro/command.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/micro/command.go b/cmd/micro/command.go index 712bb321..b81c0e44 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -367,7 +367,7 @@ func Replace(args []string) { search := string(args[0]) replace := string(args[1]) - regex, err := regexp.Compile(search) + regex, err := regexp.Compile("(?m)" + search) if err != nil { // There was an error with the user's regex messenger.Error(err.Error()) @@ -414,23 +414,28 @@ func Replace(args []string) { } } } else { - matches := regex.FindAllStringIndex(view.Buf.String(), -1) + bufStr := view.Buf.String() + matches := regex.FindAllStringIndex(bufStr, -1) if matches != nil && len(matches) > 0 { + prevMatchCount := runePos(matches[0][0], bufStr) + searchCount := runePos(matches[0][1], bufStr) - prevMatchCount adjust := 0 prevMatch := matches[0] from := FromCharPos(prevMatch[0], view.Buf) - to := from.Move(Count(search), view.Buf) - adjust += Count(replace) - Count(search) + to := from.Move(searchCount, view.Buf) + adjust += Count(replace) - searchCount view.Buf.Replace(from, to, replace) if len(matches) > 1 { for _, match := range matches[1:] { found++ - from = from.Move(match[0]-prevMatch[0]+adjust, view.Buf) - to := from.Move(Count(search), view.Buf) + matchCount := runePos(match[0], bufStr) + from = from.Move(matchCount-prevMatchCount+adjust, view.Buf) + to := from.Move(searchCount, view.Buf) // TermMessage(match[0], " ", prevMatch[0], " ", adjust, "\n", from, " ", to) view.Buf.Replace(from, to, replace) prevMatch = match - // adjust += Count(replace) - Count(search) + prevMatchCount = matchCount + // adjust += Count(replace) - searchCount } } }