8 // Buffer stores the text for files that are loaded into the text editor
9 // It uses a rope to efficiently store the string and contains some
10 // simple functions for saving and wrapper functions for modifying the rope
12 // Stores the text of the buffer
15 // Path to the file on disk
17 // Name of the buffer on the status line
20 // This is the text stored every time the buffer is saved to check if the buffer is modified
23 // Provide efficient and easy access to text and lines so the rope String does not
24 // need to be constantly recalculated
25 // These variables are updated in the update() function
29 // Syntax highlighting rules
31 // File type of the buffer
35 // NewBuffer creates a new buffer from `txt` with path and name `path`
36 func NewBuffer(txt, path string) *Buffer {
45 b.rules, b.filetype = GetRules(b)
50 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
51 func (b *Buffer) Update() {
53 b.lines = strings.Split(b.text, "\n")
56 // Save saves the buffer to its default path
57 func (b *Buffer) Save() error {
58 return b.SaveAs(b.path)
61 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
62 func (b *Buffer) SaveAs(filename string) error {
63 err := ioutil.WriteFile(filename, []byte(b.text), 0644)
70 // Insert a string into the rope
71 func (b *Buffer) Insert(idx int, value string) {
72 b.r.Insert(idx, value)
76 // Remove a slice of the rope from start to end (exclusive)
77 // Returns the string that was removed
78 func (b *Buffer) Remove(start, end int) string {
79 removed := b.text[start:end]
80 b.r.Remove(start, end)
85 // Len gives the length of the buffer
86 func (b *Buffer) Len() int {