]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
make undothresthold a setting (part 2)
[micro.git] / cmd / micro / buffer.go
index 5d4f9b742ba67519cfe10b6197ac8c0c58375296..df938296bcb7734a54222559ae0dba2ea002e915 100644 (file)
@@ -10,9 +10,14 @@ import (
 // It uses a rope to efficiently store the string and contains some
 // simple functions for saving and wrapper functions for modifying the rope
 type Buffer struct {
+       // The eventhandler for undo/redo
+       *EventHandler
+
        // Stores the text of the buffer
        r *rope.Rope
 
+       Cursor Cursor
+
        // Path to the file on disk
        Path string
        // Name of the buffer on the status line
@@ -43,6 +48,15 @@ func NewBuffer(txt, path string) *Buffer {
        b.Path = path
        b.Name = path
 
+       // Put the cursor at the first spot
+       b.Cursor = Cursor{
+               x:   0,
+               y:   0,
+               Buf: b,
+       }
+
+       b.EventHandler = NewEventHandler(b)
+
        b.Update()
        b.UpdateRules()
 
@@ -79,13 +93,13 @@ func (b *Buffer) SaveAs(filename string) error {
        data := []byte(b.String())
        err := ioutil.WriteFile(filename, data, 0644)
        if err == nil {
-               b.IsModified = true
+               b.IsModified = false
        }
        return err
 }
 
-// Insert a string into the rope
-func (b *Buffer) Insert(idx int, value string) {
+// This directly inserts value at idx, bypassing all undo/redo
+func (b *Buffer) insert(idx int, value string) {
        b.IsModified = true
        b.r = b.r.Insert(idx, value)
        b.Update()
@@ -93,7 +107,8 @@ func (b *Buffer) Insert(idx int, value string) {
 
 // Remove a slice of the rope from start to end (exclusive)
 // Returns the string that was removed
-func (b *Buffer) Remove(start, end int) string {
+// This directly removes from start to end from the buffer, bypassing all undo/redo
+func (b *Buffer) remove(start, end int) string {
        b.IsModified = true
        if start < 0 {
                start = 0
@@ -101,6 +116,9 @@ func (b *Buffer) Remove(start, end int) string {
        if end > b.Len() {
                end = b.Len()
        }
+       if start == end {
+               return ""
+       }
        removed := b.Substr(start, end)
        // The rope implenentation I am using wants indicies starting at 1 instead of 0
        start++
@@ -110,6 +128,7 @@ func (b *Buffer) Remove(start, end int) string {
        return removed
 }
 
+// Substr returns the substring of the rope from start to end
 func (b *Buffer) Substr(start, end int) string {
        return b.r.Substr(start+1, end-start).String()
 }