10 "github.com/zyedidia/micro/cmd/micro/config"
11 . "github.com/zyedidia/micro/cmd/micro/util"
14 // The SerializedBuffer holds the types that get serialized when a buffer is saved
15 // These are used for the savecursor and saveundo options
16 type SerializedBuffer struct {
17 EventHandler *EventHandler
23 gob.Register(TextEvent{})
24 gob.Register(SerializedBuffer{})
27 // Serialize serializes the buffer to config.ConfigDir/buffers
28 func (b *Buffer) Serialize() error {
29 if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
33 name := config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath)
35 return overwriteFile(name, 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
48 file, err := os.Open(config.ConfigDir + "/buffers/" + EscapePath(b.AbsPath))
51 var buffer SerializedBuffer
52 decoder := gob.NewDecoder(file)
53 gob.Register(TextEvent{})
54 err = decoder.Decode(&buffer)
56 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.")
58 if b.Settings["savecursor"].(bool) {
59 b.StartCursor = buffer.Cursor
62 if b.Settings["saveundo"].(bool) {
63 // We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
64 if b.ModTime == buffer.ModTime {
65 b.EventHandler = buffer.EventHandler
66 b.EventHandler.buf = b