]> 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 9bdc12506838b4e684748c4ec133cf67239ed3f1..fdcc37ad2d43cf3f8e71773c43d749b174f48165 100644 (file)
@@ -3,15 +3,19 @@ package main
 import (
        "bytes"
        "encoding/gob"
+       "io"
        "io/ioutil"
        "os"
        "os/exec"
        "os/signal"
        "path/filepath"
+       "regexp"
        "strconv"
        "strings"
        "time"
        "unicode/utf8"
+
+       "github.com/mitchellh/go-homedir"
 )
 
 // Buffer stores the text for files that are loaded into the text editor
@@ -55,8 +59,12 @@ type SerializedBuffer struct {
        ModTime      time.Time
 }
 
-// NewBuffer creates a new buffer from `txt` with path and name `path`
-func NewBuffer(txt []byte, path string) *Buffer {
+func NewBufferFromString(text, path string) *Buffer {
+       return NewBuffer(strings.NewReader(text), path)
+}
+
+// NewBuffer creates a new buffer from a given reader with a given path
+func NewBuffer(reader io.Reader, path string) *Buffer {
        if path != "" {
                for _, tab := range tabs {
                        for _, view := range tab.views {
@@ -68,7 +76,7 @@ func NewBuffer(txt []byte, path string) *Buffer {
        }
 
        b := new(Buffer)
-       b.LineArray = NewLineArray(txt)
+       b.LineArray = NewLineArray(reader)
 
        b.Settings = DefaultLocalSettings()
        for k, v := range globalSettings {
@@ -166,6 +174,9 @@ func NewBuffer(txt []byte, path string) *Buffer {
 
 func (b *Buffer) GetName() string {
        if b.name == "" {
+               if b.Path == "" {
+                       return "No name"
+               }
                return b.Path
        }
        return b.name
@@ -271,14 +282,27 @@ func (b *Buffer) Serialize() error {
 func (b *Buffer) SaveAs(filename string) error {
        b.FindFileType()
        b.UpdateRules()
-       b.Path = filename
-       str := b.String()
+       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' {
                        b.Insert(end, "\n")
                }
        }
+       str := b.String()
        data := []byte(str)
        err := ioutil.WriteFile(filename, data, 0644)
        if err == nil {
@@ -351,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 {