// 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
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()
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()
// 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
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++
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()
}