]> git.lizzy.rs Git - micro.git/blob - internal/buffer/serialize.go
better top
[micro.git] / internal / buffer / serialize.go
1 package buffer
2
3 import (
4         "encoding/gob"
5         "errors"
6         "io"
7         "os"
8         "path/filepath"
9         "time"
10
11         "golang.org/x/text/encoding"
12
13         "github.com/zyedidia/micro/internal/config"
14         "github.com/zyedidia/micro/internal/util"
15 )
16
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
21         Cursor       Loc
22         ModTime      time.Time
23 }
24
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) {
28                 return nil
29         }
30         if b.Path == "" {
31                 return nil
32         }
33
34         name := config.ConfigDir + "/buffers/" + util.EscapePath(b.AbsPath)
35
36         return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
37                 err := gob.NewEncoder(file).Encode(SerializedBuffer{
38                         b.EventHandler,
39                         b.GetActiveCursor().Loc,
40                         b.ModTime,
41                 })
42                 return err
43         }, false)
44 }
45
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
50         if b.Path == "" {
51                 return nil
52         }
53         file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", util.EscapePath(b.AbsPath)))
54         defer file.Close()
55         if err == nil {
56                 var buffer SerializedBuffer
57                 decoder := gob.NewDecoder(file)
58                 err = decoder.Decode(&buffer)
59                 if err != nil {
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.")
61                 }
62                 if b.Settings["savecursor"].(bool) {
63                         b.StartCursor = buffer.Cursor
64                 }
65
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
72                         }
73                 }
74         }
75         return nil
76 }