found := false
if len(path) > 0 {
for _, buf := range OpenBuffers {
- if buf.AbsPath == absPath {
+ if buf.AbsPath == absPath && buf.Type != BTInfo {
found = true
b.SharedBuffer = buf.SharedBuffer
b.EventHandler = buf.EventHandler
func (b *Buffer) Close() {
for i, buf := range OpenBuffers {
if b == buf {
+ b.Fini()
copy(OpenBuffers[i:], OpenBuffers[i+1:])
OpenBuffers[len(OpenBuffers)-1] = nil
OpenBuffers = OpenBuffers[:len(OpenBuffers)-1]
}
}
+// Fini should be called when a buffer is closed and performs
+// some cleanup
+func (b *Buffer) Fini() {
+ if !b.Modified() {
+ b.Serialize()
+ }
+}
+
// GetName returns the name that should be displayed in the statusline
// for this buffer
func (b *Buffer) GetName() string {
}
// SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
-func (b *Buffer) SaveAs(filename string) error {
+func (b *Buffer) SaveAs(filename string) (err error) {
if b.Type.Scratch {
return errors.New("Cannot save scratch buffer")
}
// Update the last time this file was updated after saving
defer func() {
b.ModTime, _ = GetModTime(filename)
+ err = b.Serialize()
}()
// Removes any tilde and replaces with the absolute path to home
absPath, _ := filepath.Abs(filename)
b.AbsPath = absPath
b.isModified = false
- return b.Serialize()
+ return
}
// SaveWithSudo saves the buffer to the default path with sudo
"encoding/gob"
"errors"
"io"
+ "log"
"os"
"time"
+ "golang.org/x/text/encoding"
+
"github.com/zyedidia/micro/internal/config"
. "github.com/zyedidia/micro/internal/util"
- "golang.org/x/text/encoding/unicode"
)
// The SerializedBuffer holds the types that get serialized when a buffer is saved
ModTime time.Time
}
-func init() {
- gob.Register(TextEvent{})
- gob.Register(SerializedBuffer{})
-}
-
// Serialize serializes the buffer to config.ConfigDir/buffers
func (b *Buffer) Serialize() error {
if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
return nil
}
+ if b.Path == "" {
+ return nil
+ }
name := config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath)
- return overwriteFile(name, unicode.UTF8, func(file io.Writer) error {
+ return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
err := gob.NewEncoder(file).Encode(SerializedBuffer{
b.EventHandler,
b.GetActiveCursor().Loc,
b.ModTime,
})
+ log.Println("save mod time", b.ModTime)
return err
})
}
func (b *Buffer) Unserialize() error {
// If either savecursor or saveundo is turned on, we need to load the serialized information
// from ~/.config/micro/buffers
+ if b.Path == "" {
+ return nil
+ }
file, err := os.Open(config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath))
defer file.Close()
if err == nil {
var buffer SerializedBuffer
decoder := gob.NewDecoder(file)
- gob.Register(TextEvent{})
err = decoder.Decode(&buffer)
if err != nil {
return errors.New(err.Error() + "\nYou may want to remove the files in ~/.config/micro/buffers (these files store the information for the 'saveundo' and 'savecursor' options) if this problem persists.")
if b.Settings["saveundo"].(bool) {
// 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 {
+ log.Println("good mod time")
b.EventHandler = buffer.EventHandler
b.EventHandler.cursors = b.cursors
b.EventHandler.buf = b.SharedBuffer
+ } else {
+ log.Println("bad mod time", b.ModTime, buffer.ModTime)
}
}
}