]> git.lizzy.rs Git - micro.git/blob - cmd/micro/buffer.go
a9de2a7a3152dbc54ee0d0d91c6cced109f55855
[micro.git] / cmd / micro / buffer.go
1 package main
2
3 import (
4         "bufio"
5         "bytes"
6         "crypto/md5"
7         "encoding/gob"
8         "errors"
9         "io"
10         "io/ioutil"
11         "os"
12         "os/exec"
13         "os/signal"
14         "path/filepath"
15         "strconv"
16         "strings"
17         "time"
18         "unicode"
19         "unicode/utf8"
20
21         "github.com/zyedidia/micro/cmd/micro/highlight"
22 )
23
24 const LargeFileThreshold = 50000
25
26 var (
27         // 0 - no line type detected
28         // 1 - lf detected
29         // 2 - crlf detected
30         fileformat = 0
31 )
32
33 // Buffer stores the text for files that are loaded into the text editor
34 // It uses a rope to efficiently store the string and contains some
35 // simple functions for saving and wrapper functions for modifying the rope
36 type Buffer struct {
37         // The eventhandler for undo/redo
38         *EventHandler
39         // This stores all the text in the buffer as an array of lines
40         *LineArray
41
42         Cursor    Cursor
43         cursors   []*Cursor // for multiple cursors
44         curCursor int       // the current cursor
45
46         // Path to the file on disk
47         Path string
48         // Absolute path to the file on disk
49         AbsPath string
50         // Name of the buffer on the status line
51         name string
52
53         // Whether or not the buffer has been modified since it was opened
54         IsModified bool
55
56         // Stores the last modification time of the file the buffer is pointing to
57         ModTime time.Time
58
59         // NumLines is the number of lines in the buffer
60         NumLines int
61
62         syntaxDef   *highlight.Def
63         highlighter *highlight.Highlighter
64
65         // Hash of the original buffer -- empty if fastdirty is on
66         origHash [md5.Size]byte
67
68         // Buffer local settings
69         Settings map[string]interface{}
70 }
71
72 // The SerializedBuffer holds the types that get serialized when a buffer is saved
73 // These are used for the savecursor and saveundo options
74 type SerializedBuffer struct {
75         EventHandler *EventHandler
76         Cursor       Cursor
77         ModTime      time.Time
78 }
79
80 // NewBufferFromFile opens a new buffer using the given filepath
81 // It will also automatically handle `~`, and line/column with filename:l:c
82 // It will return an empty buffer if the filepath does not exist
83 // and an error if the file is a directory
84 func NewBufferFromFile(path string) (*Buffer, error) {
85         filename := GetPath(path)
86         filename = ReplaceHome(filename)
87         file, err := os.Open(filename)
88         fileInfo, _ := os.Stat(filename)
89
90         if err == nil && fileInfo.IsDir() {
91                 return nil, errors.New(filename + " is a directory")
92         }
93
94         defer file.Close()
95
96         var buf *Buffer
97         if err != nil {
98                 // File does not exist -- create an empty buffer with that name
99                 buf = NewBufferFromString("", path)
100         } else {
101                 buf = NewBuffer(file, FSize(file), path)
102         }
103
104         return buf, nil
105 }
106
107 // NewBufferFromString creates a new buffer containing the given
108 // string
109 func NewBufferFromString(text, path string) *Buffer {
110         return NewBuffer(strings.NewReader(text), int64(len(text)), path)
111 }
112
113 // NewBuffer creates a new buffer from a given reader with a given path
114 func NewBuffer(reader io.Reader, size int64, path string) *Buffer {
115         startpos := Loc{0, 0}
116         startposErr := true
117         if strings.Contains(path, ":") {
118                 var err error
119                 split := strings.Split(path, ":")
120                 path = split[0]
121                 startpos.Y, err = strconv.Atoi(split[1])
122                 if err != nil {
123                         messenger.Error("Error opening file: ", err)
124                 } else {
125                         startposErr = false
126                         if len(split) > 2 {
127                                 startpos.X, err = strconv.Atoi(split[2])
128                                 if err != nil {
129                                         messenger.Error("Error opening file: ", err)
130                                 }
131                         }
132                 }
133         }
134
135         if path != "" {
136                 for _, tab := range tabs {
137                         for _, view := range tab.Views {
138                                 if view.Buf.Path == path {
139                                         return view.Buf
140                                 }
141                         }
142                 }
143         }
144
145         b := new(Buffer)
146         b.LineArray = NewLineArray(size, reader)
147
148         b.Settings = DefaultLocalSettings()
149         for k, v := range globalSettings {
150                 if _, ok := b.Settings[k]; ok {
151                         b.Settings[k] = v
152                 }
153         }
154
155         if fileformat == 1 {
156                 b.Settings["fileformat"] = "unix"
157         } else if fileformat == 2 {
158                 b.Settings["fileformat"] = "dos"
159         }
160
161         absPath, _ := filepath.Abs(path)
162
163         b.Path = path
164         b.AbsPath = absPath
165
166         // The last time this file was modified
167         b.ModTime, _ = GetModTime(b.Path)
168
169         b.EventHandler = NewEventHandler(b)
170
171         b.Update()
172         b.UpdateRules()
173
174         if _, err := os.Stat(configDir + "/buffers/"); os.IsNotExist(err) {
175                 os.Mkdir(configDir+"/buffers/", os.ModePerm)
176         }
177
178         // Put the cursor at the first spot
179         cursorStartX := 0
180         cursorStartY := 0
181         // If -startpos LINE,COL was passed, use start position LINE,COL
182         if len(*flagStartPos) > 0 || !startposErr {
183                 positions := strings.Split(*flagStartPos, ",")
184                 if len(positions) == 2 || !startposErr {
185                         var lineNum, colNum int
186                         var errPos1, errPos2 error
187                         if !startposErr {
188                                 lineNum = startpos.Y
189                                 colNum = startpos.X
190                         } else {
191                                 lineNum, errPos1 = strconv.Atoi(positions[0])
192                                 colNum, errPos2 = strconv.Atoi(positions[1])
193                         }
194                         if errPos1 == nil && errPos2 == nil {
195                                 cursorStartX = colNum
196                                 cursorStartY = lineNum - 1
197                                 // Check to avoid line overflow
198                                 if cursorStartY > b.NumLines {
199                                         cursorStartY = b.NumLines - 1
200                                 } else if cursorStartY < 0 {
201                                         cursorStartY = 0
202                                 }
203                                 // Check to avoid column overflow
204                                 if cursorStartX > len(b.Line(cursorStartY)) {
205                                         cursorStartX = len(b.Line(cursorStartY))
206                                 } else if cursorStartX < 0 {
207                                         cursorStartX = 0
208                                 }
209                         }
210                 }
211         }
212         b.Cursor = Cursor{
213                 Loc: Loc{
214                         X: cursorStartX,
215                         Y: cursorStartY,
216                 },
217                 buf: b,
218         }
219
220         InitLocalSettings(b)
221
222         if startposErr && len(*flagStartPos) == 0 && (b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool)) {
223                 // If either savecursor or saveundo is turned on, we need to load the serialized information
224                 // from ~/.config/micro/buffers
225                 file, err := os.Open(configDir + "/buffers/" + EscapePath(b.AbsPath))
226                 defer file.Close()
227                 if err == nil {
228                         var buffer SerializedBuffer
229                         decoder := gob.NewDecoder(file)
230                         gob.Register(TextEvent{})
231                         err = decoder.Decode(&buffer)
232                         if err != nil {
233                                 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.")
234                         }
235                         if b.Settings["savecursor"].(bool) {
236                                 b.Cursor = buffer.Cursor
237                                 b.Cursor.buf = b
238                                 b.Cursor.Relocate()
239                         }
240
241                         if b.Settings["saveundo"].(bool) {
242                                 // We should only use last time's eventhandler if the file wasn't modified by someone else in the meantime
243                                 if b.ModTime == buffer.ModTime {
244                                         b.EventHandler = buffer.EventHandler
245                                         b.EventHandler.buf = b
246                                 }
247                         }
248                 }
249         }
250
251         if !b.Settings["fastdirty"].(bool) {
252                 if size > LargeFileThreshold {
253                         // If the file is larger than a megabyte fastdirty needs to be on
254                         b.Settings["fastdirty"] = true
255                 } else {
256                         calcHash(b, &b.origHash)
257                 }
258         }
259
260         b.cursors = []*Cursor{&b.Cursor}
261
262         return b
263 }
264
265 // GetName returns the name that should be displayed in the statusline
266 // for this buffer
267 func (b *Buffer) GetName() string {
268         if b.name == "" {
269                 if b.Path == "" {
270                         return "No name"
271                 }
272                 return b.Path
273         }
274         return b.name
275 }
276
277 // UpdateRules updates the syntax rules and filetype for this buffer
278 // This is called when the colorscheme changes
279 func (b *Buffer) UpdateRules() {
280         rehighlight := false
281         var files []*highlight.File
282         for _, f := range ListRuntimeFiles(RTSyntax) {
283                 data, err := f.Data()
284                 if err != nil {
285                         TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
286                 } else {
287                         file, err := highlight.ParseFile(data)
288                         if err != nil {
289                                 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
290                                 continue
291                         }
292                         ftdetect, err := highlight.ParseFtDetect(file)
293                         if err != nil {
294                                 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
295                                 continue
296                         }
297
298                         ft := b.Settings["filetype"].(string)
299                         if (ft == "Unknown" || ft == "") && !rehighlight {
300                                 if highlight.MatchFiletype(ftdetect, b.Path, b.lines[0].data) {
301                                         header := new(highlight.Header)
302                                         header.FileType = file.FileType
303                                         header.FtDetect = ftdetect
304                                         b.syntaxDef, err = highlight.ParseDef(file, header)
305                                         if err != nil {
306                                                 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
307                                                 continue
308                                         }
309                                         rehighlight = true
310                                 }
311                         } else {
312                                 if file.FileType == ft && !rehighlight {
313                                         header := new(highlight.Header)
314                                         header.FileType = file.FileType
315                                         header.FtDetect = ftdetect
316                                         b.syntaxDef, err = highlight.ParseDef(file, header)
317                                         if err != nil {
318                                                 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
319                                                 continue
320                                         }
321                                         rehighlight = true
322                                 }
323                         }
324                         files = append(files, file)
325                 }
326         }
327
328         if b.syntaxDef != nil {
329                 highlight.ResolveIncludes(b.syntaxDef, files)
330         }
331
332         if b.highlighter == nil || rehighlight {
333                 if b.syntaxDef != nil {
334                         b.Settings["filetype"] = b.syntaxDef.FileType
335                         b.highlighter = highlight.NewHighlighter(b.syntaxDef)
336                         if b.Settings["syntax"].(bool) {
337                                 b.highlighter.HighlightStates(b)
338                         }
339                 }
340         }
341 }
342
343 // FileType returns the buffer's filetype
344 func (b *Buffer) FileType() string {
345         return b.Settings["filetype"].(string)
346 }
347
348 // IndentString returns a string representing one level of indentation
349 func (b *Buffer) IndentString() string {
350         if b.Settings["tabstospaces"].(bool) {
351                 return Spaces(int(b.Settings["tabsize"].(float64)))
352         }
353         return "\t"
354 }
355
356 // CheckModTime makes sure that the file this buffer points to hasn't been updated
357 // by an external program since it was last read
358 // If it has, we ask the user if they would like to reload the file
359 func (b *Buffer) CheckModTime() {
360         modTime, ok := GetModTime(b.Path)
361         if ok {
362                 if modTime != b.ModTime {
363                         choice, canceled := messenger.YesNoPrompt("The file has changed since it was last read. Reload file? (y,n)")
364                         messenger.Reset()
365                         messenger.Clear()
366                         if !choice || canceled {
367                                 // Don't load new changes -- do nothing
368                                 b.ModTime, _ = GetModTime(b.Path)
369                         } else {
370                                 // Load new changes
371                                 b.ReOpen()
372                         }
373                 }
374         }
375 }
376
377 // ReOpen reloads the current buffer from disk
378 func (b *Buffer) ReOpen() {
379         data, err := ioutil.ReadFile(b.Path)
380         txt := string(data)
381
382         if err != nil {
383                 messenger.Error(err.Error())
384                 return
385         }
386         b.EventHandler.ApplyDiff(txt)
387
388         b.ModTime, _ = GetModTime(b.Path)
389         b.IsModified = false
390         b.Update()
391         b.Cursor.Relocate()
392 }
393
394 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
395 func (b *Buffer) Update() {
396         b.NumLines = len(b.lines)
397 }
398
399 // MergeCursors merges any cursors that are at the same position
400 // into one cursor
401 func (b *Buffer) MergeCursors() {
402         var cursors []*Cursor
403         for i := 0; i < len(b.cursors); i++ {
404                 c1 := b.cursors[i]
405                 if c1 != nil {
406                         for j := 0; j < len(b.cursors); j++ {
407                                 c2 := b.cursors[j]
408                                 if c2 != nil && i != j && c1.Loc == c2.Loc {
409                                         b.cursors[j] = nil
410                                 }
411                         }
412                         cursors = append(cursors, c1)
413                 }
414         }
415
416         b.cursors = cursors
417
418         for i := range b.cursors {
419                 b.cursors[i].Num = i
420         }
421
422         if b.curCursor >= len(b.cursors) {
423                 b.curCursor = len(b.cursors) - 1
424         }
425 }
426
427 // UpdateCursors updates all the cursors indicies
428 func (b *Buffer) UpdateCursors() {
429         for i, c := range b.cursors {
430                 c.Num = i
431         }
432 }
433
434 // Save saves the buffer to its default path
435 func (b *Buffer) Save() error {
436         return b.SaveAs(b.Path)
437 }
438
439 // SaveWithSudo saves the buffer to the default path with sudo
440 func (b *Buffer) SaveWithSudo() error {
441         return b.SaveAsWithSudo(b.Path)
442 }
443
444 // Serialize serializes the buffer to configDir/buffers
445 func (b *Buffer) Serialize() error {
446         if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
447                 return nil
448         }
449
450         name := configDir + "/buffers/" + EscapePath(b.AbsPath)
451
452         return overwriteFile(name, func(file io.Writer) error {
453                 return gob.NewEncoder(file).Encode(SerializedBuffer{
454                         b.EventHandler,
455                         b.Cursor,
456                         b.ModTime,
457                 })
458         })
459 }
460
461 func init() {
462         gob.Register(TextEvent{})
463         gob.Register(SerializedBuffer{})
464 }
465
466 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
467 func (b *Buffer) SaveAs(filename string) error {
468         b.UpdateRules()
469         if b.Settings["rmtrailingws"].(bool) {
470                 for i, l := range b.lines {
471                         pos := len(bytes.TrimRightFunc(l.data, unicode.IsSpace))
472
473                         if pos < len(l.data) {
474                                 b.deleteToEnd(Loc{pos, i})
475                         }
476                 }
477
478                 b.Cursor.Relocate()
479         }
480
481         if b.Settings["eofnewline"].(bool) {
482                 end := b.End()
483                 if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
484                         b.Insert(end, "\n")
485                 }
486         }
487
488         defer func() {
489                 b.ModTime, _ = GetModTime(filename)
490         }()
491
492         // Removes any tilde and replaces with the absolute path to home
493         absFilename := ReplaceHome(filename)
494
495         // Get the leading path to the file | "." is returned if there's no leading path provided
496         if dirname := filepath.Dir(absFilename); dirname != "." {
497                 // Check if the parent dirs don't exist
498                 if _, statErr := os.Stat(dirname); os.IsNotExist(statErr) {
499                         // Prompt to make sure they want to create the dirs that are missing
500                         if yes, canceled := messenger.YesNoPrompt("Parent folders \"" + dirname + "\" do not exist. Create them? (y,n)"); yes && !canceled {
501                                 // Create all leading dir(s) since they don't exist
502                                 if mkdirallErr := os.MkdirAll(dirname, os.ModePerm); mkdirallErr != nil {
503                                         // If there was an error creating the dirs
504                                         return mkdirallErr
505                                 }
506                         } else {
507                                 // If they canceled the creation of leading dirs
508                                 return errors.New("Save aborted")
509                         }
510                 }
511         }
512
513         var fileSize int
514
515         err := overwriteFile(absFilename, func(file io.Writer) (e error) {
516                 if len(b.lines) == 0 {
517                         return
518                 }
519
520                 // end of line
521                 var eol []byte
522
523                 if b.Settings["fileformat"] == "dos" {
524                         eol = []byte{'\r', '\n'}
525                 } else {
526                         eol = []byte{'\n'}
527                 }
528
529                 // write lines
530                 if fileSize, e = file.Write(b.lines[0].data); e != nil {
531                         return
532                 }
533
534                 for _, l := range b.lines[1:] {
535                         if _, e = file.Write(eol); e != nil {
536                                 return
537                         }
538
539                         if _, e = file.Write(l.data); e != nil {
540                                 return
541                         }
542
543                         fileSize += len(eol) + len(l.data)
544                 }
545
546                 return
547         })
548
549         if err != nil {
550                 return err
551         }
552
553         if !b.Settings["fastdirty"].(bool) {
554                 if fileSize > LargeFileThreshold {
555                         // For large files 'fastdirty' needs to be on
556                         b.Settings["fastdirty"] = true
557                 } else {
558                         calcHash(b, &b.origHash)
559                 }
560         }
561
562         b.Path = filename
563         b.IsModified = false
564         return b.Serialize()
565 }
566
567 // overwriteFile opens the given file for writing, truncating if one exists, and then calls
568 // the supplied function with the file as io.Writer object, also making sure the file is
569 // closed afterwards.
570 func overwriteFile(name string, fn func(io.Writer) error) (err error) {
571         var file *os.File
572
573         if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
574                 return
575         }
576
577         defer func() {
578                 if e := file.Close(); e != nil && err == nil {
579                         err = e
580                 }
581         }()
582
583         w := bufio.NewWriter(file)
584
585         if err = fn(w); err != nil {
586                 return
587         }
588
589         err = w.Flush()
590         return
591 }
592
593 // calcHash calculates md5 hash of all lines in the buffer
594 func calcHash(b *Buffer, out *[md5.Size]byte) {
595         h := md5.New()
596
597         if len(b.lines) > 0 {
598                 h.Write(b.lines[0].data)
599
600                 for _, l := range b.lines[1:] {
601                         h.Write([]byte{'\n'})
602                         h.Write(l.data)
603                 }
604         }
605
606         h.Sum((*out)[:0])
607 }
608
609 // SaveAsWithSudo is the same as SaveAs except it uses a neat trick
610 // with tee to use sudo so the user doesn't have to reopen micro with sudo
611 func (b *Buffer) SaveAsWithSudo(filename string) error {
612         b.UpdateRules()
613         b.Path = filename
614
615         // Shut down the screen because we're going to interact directly with the shell
616         screen.Fini()
617         screen = nil
618
619         // Set up everything for the command
620         cmd := exec.Command(globalSettings["sucmd"].(string), "tee", filename)
621         cmd.Stdin = bytes.NewBufferString(b.SaveString(b.Settings["fileformat"] == "dos"))
622
623         // This is a trap for Ctrl-C so that it doesn't kill micro
624         // Instead we trap Ctrl-C to kill the program we're running
625         c := make(chan os.Signal, 1)
626         signal.Notify(c, os.Interrupt)
627         go func() {
628                 for range c {
629                         cmd.Process.Kill()
630                 }
631         }()
632
633         // Start the command
634         cmd.Start()
635         err := cmd.Wait()
636
637         // Start the screen back up
638         InitScreen()
639         if err == nil {
640                 b.IsModified = false
641                 b.ModTime, _ = GetModTime(filename)
642                 b.Serialize()
643         }
644         return err
645 }
646
647 // Modified returns if this buffer has been modified since
648 // being opened
649 func (b *Buffer) Modified() bool {
650         if b.Settings["fastdirty"].(bool) {
651                 return b.IsModified
652         }
653
654         var buff [md5.Size]byte
655
656         calcHash(b, &buff)
657         return buff != b.origHash
658 }
659
660 func (b *Buffer) insert(pos Loc, value []byte) {
661         b.IsModified = true
662         b.LineArray.insert(pos, value)
663         b.Update()
664 }
665 func (b *Buffer) remove(start, end Loc) string {
666         b.IsModified = true
667         sub := b.LineArray.remove(start, end)
668         b.Update()
669         return sub
670 }
671 func (b *Buffer) deleteToEnd(start Loc) {
672         b.IsModified = true
673         b.LineArray.DeleteToEnd(start)
674         b.Update()
675 }
676
677 // Start returns the location of the first character in the buffer
678 func (b *Buffer) Start() Loc {
679         return Loc{0, 0}
680 }
681
682 // End returns the location of the last character in the buffer
683 func (b *Buffer) End() Loc {
684         return Loc{utf8.RuneCount(b.lines[b.NumLines-1].data), b.NumLines - 1}
685 }
686
687 // RuneAt returns the rune at a given location in the buffer
688 func (b *Buffer) RuneAt(loc Loc) rune {
689         line := b.LineRunes(loc.Y)
690         if len(line) > 0 {
691                 return line[loc.X]
692         }
693         return '\n'
694 }
695
696 // LineBytes returns a single line as an array of runes
697 func (b *Buffer) LineBytes(n int) []byte {
698         if n >= len(b.lines) {
699                 return []byte{}
700         }
701         return b.lines[n].data
702 }
703
704 // LineRunes returns a single line as an array of runes
705 func (b *Buffer) LineRunes(n int) []rune {
706         if n >= len(b.lines) {
707                 return []rune{}
708         }
709         return toRunes(b.lines[n].data)
710 }
711
712 // Line returns a single line
713 func (b *Buffer) Line(n int) string {
714         if n >= len(b.lines) {
715                 return ""
716         }
717         return string(b.lines[n].data)
718 }
719
720 // LinesNum returns the number of lines in the buffer
721 func (b *Buffer) LinesNum() int {
722         return len(b.lines)
723 }
724
725 // Lines returns an array of strings containing the lines from start to end
726 func (b *Buffer) Lines(start, end int) []string {
727         lines := b.lines[start:end]
728         var slice []string
729         for _, line := range lines {
730                 slice = append(slice, string(line.data))
731         }
732         return slice
733 }
734
735 // Len gives the length of the buffer
736 func (b *Buffer) Len() (n int) {
737         for _, l := range b.lines {
738                 n += utf8.RuneCount(l.data)
739         }
740
741         if len(b.lines) > 1 {
742                 n += len(b.lines) - 1 // account for newlines
743         }
744
745         return
746 }
747
748 // MoveLinesUp moves the range of lines up one row
749 func (b *Buffer) MoveLinesUp(start int, end int) {
750         // 0 < start < end <= len(b.lines)
751         if start < 1 || start >= end || end > len(b.lines) {
752                 return // what to do? FIXME
753         }
754         if end == len(b.lines) {
755                 b.Insert(
756                         Loc{
757                                 utf8.RuneCount(b.lines[end-1].data),
758                                 end - 1,
759                         },
760                         "\n"+b.Line(start-1),
761                 )
762         } else {
763                 b.Insert(
764                         Loc{0, end},
765                         b.Line(start-1)+"\n",
766                 )
767         }
768         b.Remove(
769                 Loc{0, start - 1},
770                 Loc{0, start},
771         )
772 }
773
774 // MoveLinesDown moves the range of lines down one row
775 func (b *Buffer) MoveLinesDown(start int, end int) {
776         // 0 <= start < end < len(b.lines)
777         // if end == len(b.lines), we can't do anything here because the
778         // last line is unaccessible, FIXME
779         if start < 0 || start >= end || end >= len(b.lines)-1 {
780                 return // what to do? FIXME
781         }
782         b.Insert(
783                 Loc{0, start},
784                 b.Line(end)+"\n",
785         )
786         end++
787         b.Remove(
788                 Loc{0, end},
789                 Loc{0, end + 1},
790         )
791 }
792
793 // ClearMatches clears all of the syntax highlighting for this buffer
794 func (b *Buffer) ClearMatches() {
795         for i := range b.lines {
796                 b.SetMatch(i, nil)
797                 b.SetState(i, nil)
798         }
799 }
800
801 func (b *Buffer) clearCursors() {
802         for i := 1; i < len(b.cursors); i++ {
803                 b.cursors[i] = nil
804         }
805         b.cursors = b.cursors[:1]
806         b.UpdateCursors()
807         b.Cursor.ResetSelection()
808 }
809
810 var bracePairs = [][2]rune{
811         {'(', ')'},
812         {'{', '}'},
813         {'[', ']'},
814 }
815
816 // FindMatchingBrace returns the location in the buffer of the matching bracket
817 // It is given a brace type containing the open and closing character, (for example
818 // '{' and '}') as well as the location to match from
819 func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) Loc {
820         curLine := b.LineRunes(start.Y)
821         startChar := curLine[start.X]
822         var i int
823         if startChar == braceType[0] {
824                 for y := start.Y; y < b.NumLines; y++ {
825                         l := b.LineRunes(y)
826                         xInit := 0
827                         if y == start.Y {
828                                 xInit = start.X
829                         }
830                         for x := xInit; x < len(l); x++ {
831                                 r := l[x]
832                                 if r == braceType[0] {
833                                         i++
834                                 } else if r == braceType[1] {
835                                         i--
836                                         if i == 0 {
837                                                 return Loc{x, y}
838                                         }
839                                 }
840                         }
841                 }
842         } else if startChar == braceType[1] {
843                 for y := start.Y; y >= 0; y-- {
844                         l := []rune(string(b.lines[y].data))
845                         xInit := len(l) - 1
846                         if y == start.Y {
847                                 xInit = start.X
848                         }
849                         for x := xInit; x >= 0; x-- {
850                                 r := l[x]
851                                 if r == braceType[0] {
852                                         i--
853                                         if i == 0 {
854                                                 return Loc{x, y}
855                                         }
856                                 } else if r == braceType[1] {
857                                         i++
858                                 }
859                         }
860                 }
861         }
862         return start
863 }