searchLoc = locs[0]
searchLoc.X += nrunes + locs[0].Diff(locs[1], h.Buf)
- end.Move(nrunes, h.Buf)
+ if end.Y == locs[1].Y {
+ end = end.Move(nrunes, h.Buf)
+ }
h.Cursor.Loc = searchLoc
nreplaced++
} else if !canceled && !yes {
)
func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
+ lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
+ if start.Y > b.LinesNum()-1 {
+ start.X = lastcn - 1
+ }
+ if end.Y > b.LinesNum()-1 {
+ end.X = lastcn
+ }
start.Y = util.Clamp(start.Y, 0, b.LinesNum()-1)
end.Y = util.Clamp(end.Y, 0, b.LinesNum()-1)
}
func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
+ lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
+ if start.Y > b.LinesNum()-1 {
+ start.X = lastcn - 1
+ }
+ if end.Y > b.LinesNum()-1 {
+ end.X = lastcn
+ }
start.Y = util.Clamp(start.Y, 0, b.LinesNum()-1)
end.Y = util.Clamp(end.Y, 0, b.LinesNum()-1)
// ReplaceRegex replaces all occurrences of 'search' with 'replace' in the given area
// and returns the number of replacements made and the number of runes
-// added or removed
+// added or removed on the last line of the range
func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []byte) (int, int) {
if start.GreaterThan(end) {
start, end = end, start
result = search.Expand(result, replace, in, submatches)
}
found++
- netrunes += util.CharacterCount(in) - util.CharacterCount(result)
+ if i == end.Y {
+ netrunes += util.CharacterCount(result) - util.CharacterCount(in)
+ }
return result
})