]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
Move cursor with search selection
[micro.git] / cmd / micro / buffer.go
index 6b908f9ce0ed35c1483beb6f1ccfb7a79fac2e94..17abf53104da9ad2ea03540ab4c57d95b550485e 100644 (file)
@@ -18,6 +18,7 @@ import (
 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
@@ -27,6 +28,7 @@ type Buffer struct {
        // 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
@@ -41,6 +43,7 @@ type Buffer struct {
 }
 
 // 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
@@ -51,9 +54,16 @@ type SerializedBuffer struct {
 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)
@@ -75,6 +85,8 @@ func NewBuffer(txt []byte, path string) *Buffer {
        }
 
        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 {
@@ -83,7 +95,7 @@ func NewBuffer(txt []byte, path string) *Buffer {
                        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
@@ -177,7 +189,6 @@ func (b *Buffer) Serialize() error {
                                b.Cursor,
                                b.ModTime,
                        })
-                       // err = enc.Encode(b.Cursor)
                }
                file.Close()
                return err
@@ -269,7 +280,7 @@ func (b *Buffer) Start() Loc {
 
 // 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