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