21 "github.com/zyedidia/micro/cmd/micro/highlight"
24 const LargeFileThreshold = 50000
27 // 0 - no line type detected
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
37 // The eventhandler for undo/redo
39 // This stores all the text in the buffer as an array of lines
43 cursors []*Cursor // for multiple cursors
44 curCursor int // the current cursor
46 // Path to the file on disk
48 // Absolute path to the file on disk
50 // Name of the buffer on the status line
53 // Whether or not the buffer has been modified since it was opened
56 // Stores the last modification time of the file the buffer is pointing to
59 // NumLines is the number of lines in the buffer
62 syntaxDef *highlight.Def
63 highlighter *highlight.Highlighter
65 // Hash of the original buffer -- empty if fastdirty is on
66 origHash [md5.Size]byte
68 // Buffer local settings
69 Settings map[string]interface{}
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
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)
90 if err == nil && fileInfo.IsDir() {
91 return nil, errors.New(filename + " is a directory")
98 // File does not exist -- create an empty buffer with that name
99 buf = NewBufferFromString("", path)
101 buf = NewBuffer(file, FSize(file), path)
107 // NewBufferFromString creates a new buffer containing the given
109 func NewBufferFromString(text, path string) *Buffer {
110 return NewBuffer(strings.NewReader(text), int64(len(text)), path)
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}
117 if strings.Contains(path, ":") {
119 split := strings.Split(path, ":")
121 startpos.Y, err = strconv.Atoi(split[1])
123 messenger.Error("Error opening file: ", err)
127 startpos.X, err = strconv.Atoi(split[2])
129 messenger.Error("Error opening file: ", err)
136 for _, tab := range tabs {
137 for _, view := range tab.Views {
138 if view.Buf.Path == path {
146 b.LineArray = NewLineArray(size, reader)
148 b.Settings = DefaultLocalSettings()
149 for k, v := range globalSettings {
150 if _, ok := b.Settings[k]; ok {
156 b.Settings["fileformat"] = "unix"
157 } else if fileformat == 2 {
158 b.Settings["fileformat"] = "dos"
161 absPath, _ := filepath.Abs(path)
166 // The last time this file was modified
167 b.ModTime, _ = GetModTime(b.Path)
169 b.EventHandler = NewEventHandler(b)
174 if _, err := os.Stat(configDir + "/buffers/"); os.IsNotExist(err) {
175 os.Mkdir(configDir+"/buffers/", os.ModePerm)
178 // Put the cursor at the first spot
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
191 lineNum, errPos1 = strconv.Atoi(positions[0])
192 colNum, errPos2 = strconv.Atoi(positions[1])
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 {
203 // Check to avoid column overflow
204 if cursorStartX > len(b.Line(cursorStartY)) {
205 cursorStartX = len(b.Line(cursorStartY))
206 } else if cursorStartX < 0 {
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))
228 var buffer SerializedBuffer
229 decoder := gob.NewDecoder(file)
230 gob.Register(TextEvent{})
231 err = decoder.Decode(&buffer)
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.")
235 if b.Settings["savecursor"].(bool) {
236 b.Cursor = buffer.Cursor
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
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
256 calcHash(b, &b.origHash)
260 b.cursors = []*Cursor{&b.Cursor}
265 // GetName returns the name that should be displayed in the statusline
267 func (b *Buffer) GetName() string {
277 // UpdateRules updates the syntax rules and filetype for this buffer
278 // This is called when the colorscheme changes
279 func (b *Buffer) UpdateRules() {
281 var files []*highlight.File
282 for _, f := range ListRuntimeFiles(RTSyntax) {
283 data, err := f.Data()
285 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
287 file, err := highlight.ParseFile(data)
289 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
292 ftdetect, err := highlight.ParseFtDetect(file)
294 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
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)
306 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
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)
318 TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
324 files = append(files, file)
328 if b.syntaxDef != nil {
329 highlight.ResolveIncludes(b.syntaxDef, files)
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)
343 // FileType returns the buffer's filetype
344 func (b *Buffer) FileType() string {
345 return b.Settings["filetype"].(string)
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)))
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)
362 if modTime != b.ModTime {
363 choice, canceled := messenger.YesNoPrompt("The file has changed since it was last read. Reload file? (y,n)")
366 if !choice || canceled {
367 // Don't load new changes -- do nothing
368 b.ModTime, _ = GetModTime(b.Path)
377 // ReOpen reloads the current buffer from disk
378 func (b *Buffer) ReOpen() {
379 data, err := ioutil.ReadFile(b.Path)
383 messenger.Error(err.Error())
386 b.EventHandler.ApplyDiff(txt)
388 b.ModTime, _ = GetModTime(b.Path)
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)
399 // MergeCursors merges any cursors that are at the same position
401 func (b *Buffer) MergeCursors() {
402 var cursors []*Cursor
403 for i := 0; i < len(b.cursors); i++ {
406 for j := 0; j < len(b.cursors); j++ {
408 if c2 != nil && i != j && c1.Loc == c2.Loc {
412 cursors = append(cursors, c1)
418 for i := range b.cursors {
422 if b.curCursor >= len(b.cursors) {
423 b.curCursor = len(b.cursors) - 1
427 // UpdateCursors updates all the cursors indicies
428 func (b *Buffer) UpdateCursors() {
429 for i, c := range b.cursors {
434 // Save saves the buffer to its default path
435 func (b *Buffer) Save() error {
436 return b.SaveAs(b.Path)
439 // SaveWithSudo saves the buffer to the default path with sudo
440 func (b *Buffer) SaveWithSudo() error {
441 return b.SaveAsWithSudo(b.Path)
444 // Serialize serializes the buffer to configDir/buffers
445 func (b *Buffer) Serialize() error {
446 if !b.Settings["savecursor"].(bool) && !b.Settings["saveundo"].(bool) {
450 name := configDir + "/buffers/" + EscapePath(b.AbsPath)
452 return overwriteFile(name, func(file io.Writer) error {
453 return gob.NewEncoder(file).Encode(SerializedBuffer{
462 gob.Register(TextEvent{})
463 gob.Register(SerializedBuffer{})
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 {
469 if b.Settings["rmtrailingws"].(bool) {
470 for i, l := range b.lines {
471 pos := len(bytes.TrimRightFunc(l.data, unicode.IsSpace))
473 if pos < len(l.data) {
474 b.deleteToEnd(Loc{pos, i})
481 if b.Settings["eofnewline"].(bool) {
483 if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
489 b.ModTime, _ = GetModTime(filename)
492 // Removes any tilde and replaces with the absolute path to home
493 absFilename := ReplaceHome(filename)
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
507 // If they canceled the creation of leading dirs
508 return errors.New("Save aborted")
515 err := overwriteFile(absFilename, func(file io.Writer) (e error) {
516 if len(b.lines) == 0 {
523 if b.Settings["fileformat"] == "dos" {
524 eol = []byte{'\r', '\n'}
530 if fileSize, e = file.Write(b.lines[0].data); e != nil {
534 for _, l := range b.lines[1:] {
535 if _, e = file.Write(eol); e != nil {
539 if _, e = file.Write(l.data); e != nil {
543 fileSize += len(eol) + len(l.data)
553 if !b.Settings["fastdirty"].(bool) {
554 if fileSize > LargeFileThreshold {
555 // For large files 'fastdirty' needs to be on
556 b.Settings["fastdirty"] = true
558 calcHash(b, &b.origHash)
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) {
573 if file, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
578 if e := file.Close(); e != nil && err == nil {
583 w := bufio.NewWriter(file)
585 if err = fn(w); err != nil {
593 // calcHash calculates md5 hash of all lines in the buffer
594 func calcHash(b *Buffer, out *[md5.Size]byte) {
597 if len(b.lines) > 0 {
598 h.Write(b.lines[0].data)
600 for _, l := range b.lines[1:] {
601 h.Write([]byte{'\n'})
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 {
615 // Shut down the screen because we're going to interact directly with the shell
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"))
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)
637 // Start the screen back up
641 b.ModTime, _ = GetModTime(filename)
647 // Modified returns if this buffer has been modified since
649 func (b *Buffer) Modified() bool {
650 if b.Settings["fastdirty"].(bool) {
654 var buff [md5.Size]byte
657 return buff != b.origHash
660 func (b *Buffer) insert(pos Loc, value []byte) {
662 b.LineArray.insert(pos, value)
665 func (b *Buffer) remove(start, end Loc) string {
667 sub := b.LineArray.remove(start, end)
671 func (b *Buffer) deleteToEnd(start Loc) {
673 b.LineArray.DeleteToEnd(start)
677 // Start returns the location of the first character in the buffer
678 func (b *Buffer) Start() Loc {
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}
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)
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) {
701 return b.lines[n].data
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) {
709 return toRunes(b.lines[n].data)
712 // Line returns a single line
713 func (b *Buffer) Line(n int) string {
714 if n >= len(b.lines) {
717 return string(b.lines[n].data)
720 // LinesNum returns the number of lines in the buffer
721 func (b *Buffer) LinesNum() int {
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]
729 for _, line := range lines {
730 slice = append(slice, string(line.data))
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)
741 if len(b.lines) > 1 {
742 n += len(b.lines) - 1 // account for newlines
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
754 if end == len(b.lines) {
757 utf8.RuneCount(b.lines[end-1].data),
760 "\n"+b.Line(start-1),
765 b.Line(start-1)+"\n",
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
793 // ClearMatches clears all of the syntax highlighting for this buffer
794 func (b *Buffer) ClearMatches() {
795 for i := range b.lines {
801 func (b *Buffer) clearCursors() {
802 for i := 1; i < len(b.cursors); i++ {
805 b.cursors = b.cursors[:1]
807 b.Cursor.ResetSelection()
810 var bracePairs = [][2]rune{
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]
823 if startChar == braceType[0] {
824 for y := start.Y; y < b.NumLines; y++ {
830 for x := xInit; x < len(l); x++ {
832 if r == braceType[0] {
834 } else if r == braceType[1] {
842 } else if startChar == braceType[1] {
843 for y := start.Y; y >= 0; y-- {
844 l := []rune(string(b.lines[y].data))
849 for x := xInit; x >= 0; x-- {
851 if r == braceType[0] {
856 } else if r == braceType[1] {