]> git.lizzy.rs Git - micro.git/blob - cmd/micro/buffer.go
Use a buffer for help screen
[micro.git] / cmd / micro / buffer.go
1 package main
2
3 import (
4         "github.com/vinzmay/go-rope"
5         "io/ioutil"
6         "strings"
7 )
8
9 // Buffer stores the text for files that are loaded into the text editor
10 // It uses a rope to efficiently store the string and contains some
11 // simple functions for saving and wrapper functions for modifying the rope
12 type Buffer struct {
13         // Stores the text of the buffer
14         r *rope.Rope
15
16         // Path to the file on disk
17         path string
18         // Name of the buffer on the status line
19         name string
20
21         // This is the text stored every time the buffer is saved to check if the buffer is modified
22         savedText     string
23         netInsertions int
24
25         // Provide efficient and easy access to text and lines so the rope String does not
26         // need to be constantly recalculated
27         // These variables are updated in the update() function
28         text  string
29         lines []string
30
31         // Syntax highlighting rules
32         rules []SyntaxRule
33         // The buffer's filetype
34         filetype string
35 }
36
37 // NewBuffer creates a new buffer from `txt` with path and name `path`
38 func NewBuffer(txt, path string) *Buffer {
39         b := new(Buffer)
40         if txt == "" {
41                 b.r = new(rope.Rope)
42         } else {
43                 b.r = rope.New(txt)
44         }
45         b.path = path
46         b.name = path
47         b.savedText = txt
48
49         b.Update()
50         b.UpdateRules()
51
52         return b
53 }
54
55 // UpdateRules updates the syntax rules and filetype for this buffer
56 // This is called when the colorscheme changes
57 func (b *Buffer) UpdateRules() {
58         b.rules, b.filetype = GetRules(b)
59 }
60
61 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
62 func (b *Buffer) Update() {
63         if b.r.Len() == 0 {
64                 b.text = ""
65         } else {
66                 b.text = b.r.String()
67         }
68         b.lines = strings.Split(b.text, "\n")
69 }
70
71 // Save saves the buffer to its default path
72 func (b *Buffer) Save() error {
73         return b.SaveAs(b.path)
74 }
75
76 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
77 func (b *Buffer) SaveAs(filename string) error {
78         b.UpdateRules()
79         err := ioutil.WriteFile(filename, []byte(b.text), 0644)
80         if err == nil {
81                 b.savedText = b.text
82                 b.netInsertions = 0
83         }
84         return err
85 }
86
87 // IsDirty returns whether or not the buffer has been modified compared to the one on disk
88 func (b *Buffer) IsDirty() bool {
89         if b.netInsertions == 0 {
90                 return b.savedText != b.text
91         }
92         return true
93 }
94
95 // Insert a string into the rope
96 func (b *Buffer) Insert(idx int, value string) {
97         b.netInsertions += len(value)
98         b.r = b.r.Insert(idx, value)
99         b.Update()
100 }
101
102 // Remove a slice of the rope from start to end (exclusive)
103 // Returns the string that was removed
104 func (b *Buffer) Remove(start, end int) string {
105         b.netInsertions -= end - start
106         if start < 0 {
107                 start = 0
108         }
109         if end > b.Len() {
110                 end = b.Len()
111         }
112         removed := b.text[start:end]
113         // The rope implenentation I am using wants indicies starting at 1 instead of 0
114         start++
115         end++
116         b.r = b.r.Delete(start, end-start)
117         b.Update()
118         return removed
119 }
120
121 // Len gives the length of the buffer
122 func (b *Buffer) Len() int {
123         return b.r.Len()
124 }