10 "golang.org/x/text/encoding"
12 "github.com/zyedidia/micro/internal/config"
13 "github.com/zyedidia/micro/internal/util"
16 // The SerializedBuffer holds the types that get serialized when a buffer is saved
17 // These are used for the savecursor and saveundo options
18 type SerializedBuffer struct {
19 EventHandler *EventHandler
24 // Serialize serializes the buffer to config.ConfigDir/buffers
25 func (b *Buffer) Serialize() error {
26 if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
33 name := config.ConfigDir + "/buffers/" + util.EscapePath(b.AbsPath)
35 return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
36 err := gob.NewEncoder(file).Encode(SerializedBuffer{
38 b.GetActiveCursor().Loc,
45 func (b *Buffer) Unserialize() error {
46 // If either savecursor or saveundo is turned on, we need to load the serialized information
47 // from ~/.config/micro/buffers
51 file, err := os.Open(config.ConfigDir + "/buffers/" + util.EscapePath(b.AbsPath))
54 var buffer SerializedBuffer
55 decoder := gob.NewDecoder(file)
56 err = decoder.Decode(&buffer)
58 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.")
60 if b.Settings["savecursor"].(bool) {
61 b.StartCursor = buffer.Cursor
64 if b.Settings["saveundo"].(bool) {
65 // We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
66 if b.ModTime == buffer.ModTime {
67 b.EventHandler = buffer.EventHandler
68 b.EventHandler.cursors = b.cursors
69 b.EventHandler.buf = b.SharedBuffer