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
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 {
}
b := new(Buffer)
- b.LineArray = NewLineArray(txt)
+ b.LineArray = NewLineArray(reader)
b.Settings = DefaultLocalSettings()
for k, v := range globalSettings {
func (b *Buffer) GetName() string {
if b.name == "" {
+ if b.Path == "" {
+ return "No name"
+ }
return b.Path
}
return b.name
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 {
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 {