]> git.lizzy.rs Git - micro.git/commitdiff
Fix serialization
authorZachary Yedidia <zyedidia@gmail.com>
Sat, 15 Jun 2019 19:50:37 +0000 (15:50 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:10 +0000 (17:05 -0500)
internal/buffer/buffer.go
internal/buffer/save.go
internal/buffer/serialize.go
internal/info/infobuffer.go

index c1a2f72f4d711c569179ad8e7003df4257c0532d..543c5109c22813258b2b848238ced23ce977d562 100644 (file)
@@ -173,7 +173,7 @@ func NewBuffer(r io.Reader, size int64, path string, cursorPosition []string, bt
        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
@@ -245,6 +245,7 @@ func NewBuffer(r io.Reader, size int64, path string, cursorPosition []string, bt
 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]
@@ -253,6 +254,14 @@ func (b *Buffer) Close() {
        }
 }
 
+// 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 {
index 8597810d41be99abb55e01977db954e8f5409538..d876b338cb03efb18431472faa43d6460f2a24f4 100644 (file)
@@ -55,7 +55,7 @@ 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 {
+func (b *Buffer) SaveAs(filename string) (err error) {
        if b.Type.Scratch {
                return errors.New("Cannot save scratch buffer")
        }
@@ -82,6 +82,7 @@ func (b *Buffer) SaveAs(filename string) error {
        // 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
@@ -160,7 +161,7 @@ func (b *Buffer) SaveAs(filename string) error {
        absPath, _ := filepath.Abs(filename)
        b.AbsPath = absPath
        b.isModified = false
-       return b.Serialize()
+       return
 }
 
 // SaveWithSudo saves the buffer to the default path with sudo
index b559e764e4917ff559135eafe7121a3a8a37cfcb..ffd554b6a0a3b89d9edcab0113528b4bd9404531 100644 (file)
@@ -4,12 +4,14 @@ import (
        "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
@@ -20,25 +22,24 @@ type SerializedBuffer struct {
        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
        })
 }
@@ -46,12 +47,14 @@ func (b *Buffer) Serialize() error {
 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.")
@@ -63,9 +66,12 @@ func (b *Buffer) Unserialize() error {
                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)
                        }
                }
        }
index 75ed43eca6233ba1fcbe748213631fa5035dbc85..3e8de958b2332b64ed9a1a99d749d97741aae1a3 100644 (file)
@@ -39,7 +39,7 @@ func NewBuffer() *InfoBuf {
        ib := new(InfoBuf)
        ib.History = make(map[string][]string)
 
-       ib.Buffer = buffer.NewBufferFromString("", "infobar", buffer.BTInfo)
+       ib.Buffer = buffer.NewBufferFromString("", "", buffer.BTInfo)
        ib.LoadHistory()
 
        return ib