X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fbuffer.go;h=fdcc37ad2d43cf3f8e71773c43d749b174f48165;hb=d49e366413dc8956f6b62734a6331df6632672fe;hp=5e4b0c44d9826e6becee3c16e7953180392200af;hpb=32e82845052ee05b3f51df342888e7211ddd4e26;p=micro.git diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 5e4b0c44..fdcc37ad 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -9,6 +9,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "regexp" "strconv" "strings" "time" @@ -283,13 +284,25 @@ func (b *Buffer) SaveAs(filename string) error { b.UpdateRules() dir, _ := homedir.Dir() b.Path = strings.Replace(filename, "~", dir, 1) - str := b.String() + if b.Settings["rmtrailingws"].(bool) { + r, _ := regexp.Compile(`[ \t]+$`) + for lineNum, line := range b.Lines(0, b.NumLines) { + indices := r.FindStringIndex(line) + if indices == nil { + continue + } + startLoc := Loc{indices[0], lineNum} + b.deleteToEnd(startLoc) + } + b.Cursor.Relocate() + } if b.Settings["eofnewline"].(bool) { end := b.End() if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' { b.Insert(end, "\n") } } + str := b.String() data := []byte(str) err := ioutil.WriteFile(filename, data, 0644) if err == nil { @@ -362,6 +375,11 @@ func (b *Buffer) remove(start, end Loc) string { b.Update() return sub } +func (b *Buffer) deleteToEnd(start Loc) { + b.IsModified = true + b.LineArray.DeleteToEnd(start) + b.Update() +} // Start returns the location of the first character in the buffer func (b *Buffer) Start() Loc {