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