micro/internal/buffer/serialize.go

77 lines
2.2 KiB
Go
Raw Normal View History

2018-08-28 02:53:08 +03:00
package buffer
import (
"encoding/gob"
"errors"
"io"
"os"
"path/filepath"
2018-08-30 03:53:40 +03:00
"time"
2018-08-28 02:53:08 +03:00
2019-06-15 22:50:37 +03:00
"golang.org/x/text/encoding"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/internal/util"
2018-08-28 02:53:08 +03:00
)
2018-08-30 03:53:40 +03:00
// The SerializedBuffer holds the types that get serialized when a buffer is saved
// These are used for the savecursor and saveundo options
type SerializedBuffer struct {
EventHandler *EventHandler
Cursor Loc
ModTime time.Time
}
2018-08-28 02:53:08 +03:00
// Serialize serializes the buffer to config.ConfigDir/buffers
func (b *Buffer) Serialize() error {
if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
return nil
}
2019-06-15 22:50:37 +03:00
if b.Path == "" {
return nil
}
2018-08-28 02:53:08 +03:00
name := config.ConfigDir + "/buffers/" + util.EscapePath(b.AbsPath)
2018-08-28 02:53:08 +03:00
2019-06-15 22:50:37 +03:00
return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
2018-08-30 03:53:40 +03:00
err := gob.NewEncoder(file).Encode(SerializedBuffer{
2018-08-28 02:53:08 +03:00
b.EventHandler,
b.GetActiveCursor().Loc,
2019-01-15 00:52:25 +03:00
b.ModTime,
2018-08-28 02:53:08 +03:00
})
2018-08-30 03:53:40 +03:00
return err
}, false)
2018-08-28 02:53:08 +03:00
}
2020-01-05 20:45:27 +03:00
// Unserialize loads the buffer info from config.ConfigDir/buffers
2018-08-28 02:53:08 +03:00
func (b *Buffer) Unserialize() error {
// If either savecursor or saveundo is turned on, we need to load the serialized information
// from ~/.config/micro/buffers
2019-06-15 22:50:37 +03:00
if b.Path == "" {
return nil
}
file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", util.EscapePath(b.AbsPath)))
2018-08-28 02:53:08 +03:00
defer file.Close()
if err == nil {
var buffer SerializedBuffer
decoder := gob.NewDecoder(file)
err = decoder.Decode(&buffer)
if err != nil {
2020-01-05 20:45:27 +03:00
return errors.New(err.Error() + "\nYou may want to remove the files in ~/.config/micro/buffers (these files\nstore the information for the 'saveundo' and 'savecursor' options) if\nthis problem persists.\nThis may be caused by upgrading to version 2.0, and removing the 'buffers'\ndirectory will reset the cursor and undo history and solve the problem.")
2018-08-28 02:53:08 +03:00
}
if b.Settings["savecursor"].(bool) {
b.StartCursor = buffer.Cursor
}
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
2019-01-15 00:52:25 +03:00
if b.ModTime == buffer.ModTime {
2018-08-28 02:53:08 +03:00
b.EventHandler = buffer.EventHandler
b.EventHandler.cursors = b.cursors
2019-01-15 00:52:25 +03:00
b.EventHandler.buf = b.SharedBuffer
2018-08-28 02:53:08 +03:00
}
}
}
2018-08-30 03:53:40 +03:00
return nil
2018-08-28 02:53:08 +03:00
}