diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 9ab2ae79..0879c0ec 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -1,6 +1,7 @@ package main import ( + "crypto/md5" "github.com/vinzmay/go-rope" "io/ioutil" "strings" @@ -19,7 +20,7 @@ type Buffer struct { name string // This is the text stored every time the buffer is saved to check if the buffer is modified - savedText string + savedText [16]byte netInsertions int dirtySinceLastCheck bool @@ -45,7 +46,7 @@ func NewBuffer(txt, path string) *Buffer { } b.path = path b.name = path - b.savedText = txt + b.savedText = md5.Sum([]byte(txt)) b.Update() b.UpdateRules() @@ -77,9 +78,10 @@ func (b *Buffer) Save() error { // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist func (b *Buffer) SaveAs(filename string) error { b.UpdateRules() - err := ioutil.WriteFile(filename, []byte(b.text), 0644) + data := []byte(b.text) + err := ioutil.WriteFile(filename, data, 0644) if err == nil { - b.savedText = b.text + b.savedText = md5.Sum(data) b.netInsertions = 0 } return err @@ -91,13 +93,17 @@ func (b *Buffer) IsDirty() bool { return false } if b.netInsertions == 0 { - isDirty := b.savedText != b.text + isDirty := b.savedText != md5.Sum([]byte(b.text)) b.dirtySinceLastCheck = isDirty return isDirty } return true } +func (b *Buffer) Lines() []string { + return strings.Split(b.text, "\n") +} + // Insert a string into the rope func (b *Buffer) Insert(idx int, value string) { b.dirtySinceLastCheck = true diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 4b8aa786..a05761fb 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -6,11 +6,11 @@ import ( "io/ioutil" "os" - "github.com/zyedidia/tcell" - "github.com/zyedidia/tcell/encoding" "github.com/go-errors/errors" "github.com/mattn/go-isatty" "github.com/mitchellh/go-homedir" + "github.com/zyedidia/tcell" + "github.com/zyedidia/tcell/encoding" ) const (