]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
Merge pull request #507 from NicolaiSoeborg/master
[micro.git] / cmd / micro / buffer.go
index 5e4b0c44d9826e6becee3c16e7953180392200af..fdcc37ad2d43cf3f8e71773c43d749b174f48165 100644 (file)
@@ -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 {