type Buffer struct {
// The eventhandler for undo/redo
*EventHandler
+ // This stores all the text in the buffer as an array of lines
*LineArray
Cursor Cursor
// Name of the buffer on the status line
Name string
+ // Whether or not the buffer has been modified since it was opened
IsModified bool
// Stores the last modification time of the file the buffer is pointing to
}
// The SerializedBuffer holds the types that get serialized when a buffer is saved
+// These are used for the savecursor and saveundo options
type SerializedBuffer struct {
EventHandler *EventHandler
Cursor Cursor
func NewBuffer(txt []byte, path string) *Buffer {
b := new(Buffer)
b.LineArray = NewLineArray(txt)
+
b.Path = path
b.Name = path
+ // If the file doesn't have a path to disk then we give it no name
+ if path == "" {
+ b.Name = "No name"
+ }
+
+ // The last time this file was modified
b.ModTime, _ = GetModTime(b.Path)
b.EventHandler = NewEventHandler(b)
}
if settings["savecursor"].(bool) || settings["saveundo"].(bool) {
+ // If either savecursor or saveundo is turned on, we need to load the serialized information
+ // from ~/.config/micro/buffers
absPath, _ := filepath.Abs(b.Path)
file, err := os.Open(configDir + "/buffers/" + EscapePath(absPath))
if err == nil {
gob.Register(TextEvent{})
err = decoder.Decode(&buffer)
if err != nil {
- TermMessage(err.Error())
+ TermMessage(err.Error(), "\n", "You 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.")
}
if settings["savecursor"].(bool) {
b.Cursor = buffer.Cursor
b.Cursor,
b.ModTime,
})
- // err = enc.Encode(b.Cursor)
}
file.Close()
return err
// End returns the location of the last character in the buffer
func (b *Buffer) End() Loc {
- return Loc{utf8.RuneCount(b.lines[len(b.lines)-1]), b.NumLines - 1}
+ return Loc{utf8.RuneCount(b.lines[b.NumLines-1]), b.NumLines - 1}
}
// Line returns a single line