From 701d0dfe3d6a508c08fd1d73f390fe0f2d57192e Mon Sep 17 00:00:00 2001 From: 10sr <8slashes+git@gmail.com> Date: Thu, 15 Dec 2016 16:54:30 +0900 Subject: [PATCH] Add rmtrailingws feature --- cmd/micro/buffer.go | 18 ++++++++++++++++++ cmd/micro/settings.go | 2 ++ 2 files changed, 20 insertions(+) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index c645862d..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,6 +284,18 @@ func (b *Buffer) SaveAs(filename string) error { b.UpdateRules() dir, _ := homedir.Dir() b.Path = strings.Replace(filename, "~", dir, 1) + 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' { @@ -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 { diff --git a/cmd/micro/settings.go b/cmd/micro/settings.go index 648858c3..b11a0614 100644 --- a/cmd/micro/settings.go +++ b/cmd/micro/settings.go @@ -181,6 +181,7 @@ func DefaultGlobalSettings() map[string]interface{} { "colorscheme": "default", "cursorline": true, "eofnewline": false, + "rmtrailingws": false, "ignorecase": false, "indentchar": " ", "infobar": true, @@ -212,6 +213,7 @@ func DefaultLocalSettings() map[string]interface{} { "colorcolumn": float64(0), "cursorline": true, "eofnewline": false, + "rmtrailingws": false, "filetype": "Unknown", "ignorecase": false, "indentchar": " ",