Add option for very accurate dirty flag

Set the `fastdirty` option flag to off if you really want accurate
reporting on whether the buffer is modified. This is more resource
intensive but it can be useful for people who don't mind.

Closes #787
Closes #467
This commit is contained in:
Zachary Yedidia
2017-09-17 23:33:18 -04:00
parent 19dc9d7bbc
commit fb980bb695
7 changed files with 65 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"crypto/md5"
"encoding/gob"
"io"
"io/ioutil"
@@ -57,6 +58,9 @@ type Buffer struct {
syntaxDef *highlight.Def
highlighter *highlight.Highlighter
// Hash of the original buffer -- empty if fastdirty is on
origHash [16]byte
// Buffer local settings
Settings map[string]interface{}
}
@@ -174,7 +178,7 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
}
if b.Settings["saveundo"].(bool) {
// We should only use last time's eventhandler if the file wasn't by someone else in the meantime
// We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
if b.ModTime == buffer.ModTime {
b.EventHandler = buffer.EventHandler
b.EventHandler.buf = b
@@ -188,6 +192,10 @@ func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
screen.EnableMouse()
}
if !b.Settings["fastdirty"].(bool) {
b.origHash = md5.Sum([]byte(b.String()))
}
b.cursors = []*Cursor{&b.Cursor}
return b
@@ -451,6 +459,13 @@ func (b *Buffer) SaveAsWithSudo(filename string) error {
return err
}
func (b *Buffer) Modified() bool {
if b.Settings["fastdirty"].(bool) {
return b.IsModified
}
return b.origHash != md5.Sum([]byte(b.String()))
}
func (b *Buffer) insert(pos Loc, value []byte) {
b.IsModified = true
b.LineArray.insert(pos, value)