19 luar "layeh.com/gopher-luar"
21 dmp "github.com/sergi/go-diff/diffmatchpatch"
22 "github.com/zyedidia/micro/internal/config"
23 ulua "github.com/zyedidia/micro/internal/lua"
24 "github.com/zyedidia/micro/internal/screen"
25 "github.com/zyedidia/micro/internal/util"
26 "github.com/zyedidia/micro/pkg/highlight"
27 "golang.org/x/text/encoding/htmlindex"
28 "golang.org/x/text/encoding/unicode"
29 "golang.org/x/text/transform"
32 const backupTime = 8000
35 // OpenBuffers is a list of the currently open buffers
37 // LogBuf is a reference to the log buffer which can be opened with the
42 // The BufType defines what kind of buffer this is
45 Readonly bool // The buffer cannot be edited
46 Scratch bool // The buffer cannot be saved
47 Syntax bool // Syntax highlighting is enabled
51 // BTDefault is a default buffer
52 BTDefault = BufType{0, false, false, true}
53 // BTHelp is a help buffer
54 BTHelp = BufType{1, true, true, true}
55 // BTLog is a log buffer
56 BTLog = BufType{2, true, true, false}
57 // BTScratch is a buffer that cannot be saved (for scratch work)
58 BTScratch = BufType{3, false, true, false}
59 // BTRaw is is a buffer that shows raw terminal events
60 BTRaw = BufType{4, false, true, false}
61 // BTInfo is a buffer for inputting information
62 BTInfo = BufType{5, false, true, false}
64 // ErrFileTooLarge is returned when the file is too large to hash
65 // (fastdirty is automatically enabled)
66 ErrFileTooLarge = errors.New("File is too large to hash")
69 // SharedBuffer is a struct containing info that is shared among buffers
70 // that have the same file open
71 type SharedBuffer struct {
73 // Stores the last modification time of the file the buffer is pointing to
75 // Type of the buffer (e.g. help, raw, scratch etc..)
79 // Whether or not suggestions can be autocompleted must be shared because
80 // it changes based on how the buffer has changed
83 // Modifications is the list of modified regions for syntax highlighting
87 func (b *SharedBuffer) insert(pos Loc, value []byte) {
89 b.HasSuggestions = false
90 b.LineArray.insert(pos, value)
92 // b.Modifications is cleared every screen redraw so it's
93 // ok to append duplicates
94 b.Modifications = append(b.Modifications, Loc{pos.Y, pos.Y + bytes.Count(value, []byte{'\n'})})
96 func (b *SharedBuffer) remove(start, end Loc) []byte {
98 b.HasSuggestions = false
99 b.Modifications = append(b.Modifications, Loc{start.Y, start.Y})
100 return b.LineArray.remove(start, end)
112 // Buffer stores the main information about a currently open file including
113 // the actual text (in a LineArray), the undo/redo stack (in an EventHandler)
114 // all the cursors, the syntax highlighting info, the settings for the buffer
115 // and some misc info about modification time and path location.
116 // The syntax highlighting info must be stored with the buffer because the syntax
117 // highlighter attaches information to each line of the buffer for optimization
118 // purposes so it doesn't have to rehighlight everything on every update.
127 // Path to the file on disk
129 // Absolute path to the file on disk
131 // Name of the buffer on the status line
134 // SyntaxDef represents the syntax highlighting definition being used
135 // This stores the highlighting rules and filetype detection info
136 SyntaxDef *highlight.Def
137 // The Highlighter struct actually performs the highlighting
138 Highlighter *highlight.Highlighter
139 HighlightLock sync.Mutex
141 // Hash of the original buffer -- empty if fastdirty is on
142 origHash [md5.Size]byte
144 // Settings customized by the user
145 Settings map[string]interface{}
153 updateDiffTimer *time.Timer
155 diffBaseLineCount int
156 diffLock sync.RWMutex
157 diff map[int]DiffStatus
159 // counts the number of edits
160 // resets every backupTime edits
164 // NewBufferFromFile opens a new buffer using the given path
165 // It will also automatically handle `~`, and line/column with filename:l:c
166 // It will return an empty buffer if the path does not exist
167 // and an error if the file is a directory
168 func NewBufferFromFile(path string, btype BufType) (*Buffer, error) {
170 filename, cursorPos := util.GetPathAndCursorPosition(path)
171 filename, err = util.ReplaceHome(filename)
176 file, err := os.Open(filename)
177 fileInfo, _ := os.Stat(filename)
179 if err == nil && fileInfo.IsDir() {
180 return nil, errors.New("Error: " + filename + " is a directory and cannot be opened")
185 cursorLoc, cursorerr := ParseCursorLocation(cursorPos)
186 if cursorerr != nil {
187 cursorLoc = Loc{-1, -1}
192 // File does not exist -- create an empty buffer with that name
193 buf = NewBufferFromString("", filename, btype)
195 buf = NewBuffer(file, util.FSize(file), filename, cursorLoc, btype)
201 // NewBufferFromString creates a new buffer containing the given string
202 func NewBufferFromString(text, path string, btype BufType) *Buffer {
203 return NewBuffer(strings.NewReader(text), int64(len(text)), path, Loc{-1, -1}, btype)
206 // NewBuffer creates a new buffer from a given reader with a given path
207 // Ensure that ReadSettings and InitGlobalSettings have been called before creating
209 // Places the cursor at startcursor. If startcursor is -1, -1 places the
210 // cursor at an autodetected location (based on savecursor or :LINE:COL)
211 func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufType) *Buffer {
212 absPath, _ := filepath.Abs(path)
216 b.Settings = config.DefaultCommonSettings()
217 for k, v := range config.GlobalSettings {
218 if _, ok := b.Settings[k]; ok {
222 config.InitLocalSettings(b.Settings, path)
224 enc, err := htmlindex.Get(b.Settings["encoding"].(string))
227 b.Settings["encoding"] = "utf-8"
230 reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
234 for _, buf := range OpenBuffers {
235 if buf.AbsPath == absPath && buf.Type != BTInfo {
237 b.SharedBuffer = buf.SharedBuffer
238 b.EventHandler = buf.EventHandler
247 b.SharedBuffer = new(SharedBuffer)
250 hasBackup := b.ApplyBackup(size)
253 b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
255 b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors)
258 if b.Settings["readonly"].(bool) && b.Type == BTDefault {
259 b.Type.Readonly = true
262 // The last time this file was modified
267 b.Settings["fileformat"] = "unix"
269 b.Settings["fileformat"] = "dos"
273 config.InitLocalSettings(b.Settings, b.Path)
275 if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); os.IsNotExist(err) {
276 os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
279 if startcursor.X != -1 && startcursor.Y != -1 {
280 b.StartCursor = startcursor
282 if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
283 err := b.Unserialize()
285 screen.TermMessage(err)
290 b.AddCursor(NewCursor(b, b.StartCursor))
291 b.GetActiveCursor().Relocate()
293 if !b.Settings["fastdirty"].(bool) {
294 if size > LargeFileThreshold {
295 // If the file is larger than LargeFileThreshold fastdirty needs to be on
296 b.Settings["fastdirty"] = true
298 calcHash(b, &b.origHash)
302 err = config.RunPluginFn("onBufferOpen", luar.New(ulua.L, b))
304 screen.TermMessage(err)
307 b.Modifications = make([]Loc, 0, 10)
309 OpenBuffers = append(OpenBuffers, b)
314 // Close removes this buffer from the list of open buffers
315 func (b *Buffer) Close() {
316 for i, buf := range OpenBuffers {
319 copy(OpenBuffers[i:], OpenBuffers[i+1:])
320 OpenBuffers[len(OpenBuffers)-1] = nil
321 OpenBuffers = OpenBuffers[:len(OpenBuffers)-1]
327 // Fini should be called when a buffer is closed and performs
329 func (b *Buffer) Fini() {
336 // GetName returns the name that should be displayed in the statusline
338 func (b *Buffer) GetName() string {
348 //SetName changes the name for this buffer
349 func (b *Buffer) SetName(s string) {
353 // Insert inserts the given string of text at the start location
354 func (b *Buffer) Insert(start Loc, text string) {
355 if !b.Type.Readonly {
356 b.EventHandler.cursors = b.cursors
357 b.EventHandler.active = b.curCursor
358 b.EventHandler.Insert(start, text)
364 // Remove removes the characters between the start and end locations
365 func (b *Buffer) Remove(start, end Loc) {
366 if !b.Type.Readonly {
367 b.EventHandler.cursors = b.cursors
368 b.EventHandler.active = b.curCursor
369 b.EventHandler.Remove(start, end)
375 // ClearModifications clears the list of modified lines in this buffer
376 // The list of modified lines is used for syntax highlighting so that
377 // we can selectively highlight only the necessary lines
378 // This function should be called every time this buffer is drawn to
380 func (b *Buffer) ClearModifications() {
381 // clear slice without resetting the cap
382 b.Modifications = b.Modifications[:0]
385 // FileType returns the buffer's filetype
386 func (b *Buffer) FileType() string {
387 return b.Settings["filetype"].(string)
390 // ExternallyModified returns whether the file being edited has
391 // been modified by some external process
392 func (b *Buffer) ExternallyModified() bool {
393 modTime, err := util.GetModTime(b.Path)
395 return modTime != b.ModTime
400 // UpdateModTime updates the modtime of this file
401 func (b *Buffer) UpdateModTime() (err error) {
402 b.ModTime, err = util.GetModTime(b.Path)
406 // ReOpen reloads the current buffer from disk
407 func (b *Buffer) ReOpen() error {
408 file, err := os.Open(b.Path)
413 enc, err := htmlindex.Get(b.Settings["encoding"].(string))
418 reader := bufio.NewReader(transform.NewReader(file, enc.NewDecoder()))
419 data, err := ioutil.ReadAll(reader)
425 b.EventHandler.ApplyDiff(txt)
427 err = b.UpdateModTime()
433 // RelocateCursors relocates all cursors (makes sure they are in the buffer)
434 func (b *Buffer) RelocateCursors() {
435 for _, c := range b.cursors {
440 // RuneAt returns the rune at a given location in the buffer
441 func (b *Buffer) RuneAt(loc Loc) rune {
442 line := b.LineBytes(loc.Y)
446 r, size := utf8.DecodeRune(line)
458 // Modified returns if this buffer has been modified since
460 func (b *Buffer) Modified() bool {
465 if b.Settings["fastdirty"].(bool) {
469 var buff [md5.Size]byte
472 return buff != b.origHash
475 // calcHash calculates md5 hash of all lines in the buffer
476 func calcHash(b *Buffer, out *[md5.Size]byte) error {
480 if len(b.lines) > 0 {
481 n, e := h.Write(b.lines[0].data)
487 for _, l := range b.lines[1:] {
488 n, e = h.Write([]byte{'\n'})
493 n, e = h.Write(l.data)
501 if size > LargeFileThreshold {
502 return ErrFileTooLarge
509 // UpdateRules updates the syntax rules and filetype for this buffer
510 // This is called when the colorscheme changes
511 func (b *Buffer) UpdateRules() {
515 ft := b.Settings["filetype"].(string)
520 var header *highlight.Header
521 for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
522 data, err := f.Data()
524 screen.TermMessage("Error loading syntax header file " + f.Name() + ": " + err.Error())
528 header, err = highlight.MakeHeader(data)
530 screen.TermMessage("Error reading syntax header file", f.Name(), err)
534 if ft == "unknown" || ft == "" {
535 if highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data) {
536 syntaxFile = f.Name()
539 } else if header.FileType == ft {
540 syntaxFile = f.Name()
545 if syntaxFile == "" {
546 // search for the syntax file in the user's custom syntax files
547 for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
548 log.Println("real runtime file", f.Name())
549 data, err := f.Data()
551 screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
555 header, err = highlight.MakeHeaderYaml(data)
556 file, err := highlight.ParseFile(data)
558 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
562 if ((ft == "unknown" || ft == "") && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
563 syndef, err := highlight.ParseDef(file, header)
565 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
573 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
574 if f.Name() == syntaxFile {
575 data, err := f.Data()
577 screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
581 file, err := highlight.ParseFile(data)
583 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
587 syndef, err := highlight.ParseDef(file, header)
589 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
598 if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
599 includes := highlight.GetIncludes(b.SyntaxDef)
601 var files []*highlight.File
602 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
603 data, err := f.Data()
605 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
608 header, err := highlight.MakeHeaderYaml(data)
610 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
614 for _, i := range includes {
615 if header.FileType == i {
616 file, err := highlight.ParseFile(data)
618 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
621 files = append(files, file)
625 if len(files) >= len(includes) {
630 highlight.ResolveIncludes(b.SyntaxDef, files)
633 if b.Highlighter == nil || syntaxFile != "" {
634 if b.SyntaxDef != nil {
635 b.Settings["filetype"] = b.SyntaxDef.FileType
638 b.SyntaxDef = &highlight.EmptyDef
641 if b.SyntaxDef != nil {
642 b.Highlighter = highlight.NewHighlighter(b.SyntaxDef)
643 if b.Settings["syntax"].(bool) {
645 b.Highlighter.HighlightStates(b)
646 b.Highlighter.HighlightMatches(b, 0, b.End().Y)
647 screen.DrawChan <- true
653 // ClearMatches clears all of the syntax highlighting for the buffer
654 func (b *Buffer) ClearMatches() {
655 for i := range b.lines {
661 // IndentString returns this buffer's indent method (a tabstop or n spaces
662 // depending on the settings)
663 func (b *Buffer) IndentString(tabsize int) string {
664 if b.Settings["tabstospaces"].(bool) {
665 return util.Spaces(tabsize)
670 // SetCursors resets this buffer's cursors to a new list
671 func (b *Buffer) SetCursors(c []*Cursor) {
673 b.EventHandler.cursors = b.cursors
674 b.EventHandler.active = b.curCursor
677 // AddCursor adds a new cursor to the list
678 func (b *Buffer) AddCursor(c *Cursor) {
679 b.cursors = append(b.cursors, c)
680 b.EventHandler.cursors = b.cursors
681 b.EventHandler.active = b.curCursor
685 // SetCurCursor sets the current cursor
686 func (b *Buffer) SetCurCursor(n int) {
690 // GetActiveCursor returns the main cursor in this buffer
691 func (b *Buffer) GetActiveCursor() *Cursor {
692 return b.cursors[b.curCursor]
695 // GetCursor returns the nth cursor
696 func (b *Buffer) GetCursor(n int) *Cursor {
700 // GetCursors returns the list of cursors in this buffer
701 func (b *Buffer) GetCursors() []*Cursor {
705 // NumCursors returns the number of cursors
706 func (b *Buffer) NumCursors() int {
707 return len(b.cursors)
710 // MergeCursors merges any cursors that are at the same position
712 func (b *Buffer) MergeCursors() {
713 var cursors []*Cursor
714 for i := 0; i < len(b.cursors); i++ {
717 for j := 0; j < len(b.cursors); j++ {
719 if c2 != nil && i != j && c1.Loc == c2.Loc {
723 cursors = append(cursors, c1)
729 for i := range b.cursors {
733 if b.curCursor >= len(b.cursors) {
734 b.curCursor = len(b.cursors) - 1
736 b.EventHandler.cursors = b.cursors
737 b.EventHandler.active = b.curCursor
740 // UpdateCursors updates all the cursors indicies
741 func (b *Buffer) UpdateCursors() {
742 b.EventHandler.cursors = b.cursors
743 b.EventHandler.active = b.curCursor
744 for i, c := range b.cursors {
749 func (b *Buffer) RemoveCursor(i int) {
750 copy(b.cursors[i:], b.cursors[i+1:])
751 b.cursors[len(b.cursors)-1] = nil
752 b.cursors = b.cursors[:len(b.cursors)-1]
753 b.curCursor = util.Clamp(b.curCursor, 0, len(b.cursors)-1)
757 // ClearCursors removes all extra cursors
758 func (b *Buffer) ClearCursors() {
759 for i := 1; i < len(b.cursors); i++ {
762 b.cursors = b.cursors[:1]
765 b.GetActiveCursor().ResetSelection()
768 // MoveLinesUp moves the range of lines up one row
769 func (b *Buffer) MoveLinesUp(start int, end int) {
770 if start < 1 || start >= end || end > len(b.lines) {
773 l := string(b.LineBytes(start - 1))
774 if end == len(b.lines) {
777 utf8.RuneCount(b.lines[end-1].data),
794 // MoveLinesDown moves the range of lines down one row
795 func (b *Buffer) MoveLinesDown(start int, end int) {
796 if start < 0 || start >= end || end >= len(b.lines)-1 {
799 l := string(b.LineBytes(end))
811 var BracePairs = [][2]rune{
817 // FindMatchingBrace returns the location in the buffer of the matching bracket
818 // It is given a brace type containing the open and closing character, (for example
819 // '{' and '}') as well as the location to match from
820 // TODO: maybe can be more efficient with utf8 package
821 // returns the location of the matching brace
822 // if the boolean returned is true then the original matching brace is one character left
823 // of the starting location
824 func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) (Loc, bool) {
825 curLine := []rune(string(b.LineBytes(start.Y)))
827 if start.X >= 0 && start.X < len(curLine) {
828 startChar = curLine[start.X]
831 if start.X-1 >= 0 && start.X-1 < len(curLine) {
832 leftChar = curLine[start.X-1]
835 if startChar == braceType[0] || leftChar == braceType[0] {
836 for y := start.Y; y < b.LinesNum(); y++ {
837 l := []rune(string(b.LineBytes(y)))
840 if startChar == braceType[0] {
846 for x := xInit; x < len(l); x++ {
848 if r == braceType[0] {
850 } else if r == braceType[1] {
853 if startChar == braceType[0] {
854 return Loc{x, y}, false
856 return Loc{x, y}, true
861 } else if startChar == braceType[1] || leftChar == braceType[1] {
862 for y := start.Y; y >= 0; y-- {
863 l := []rune(string(b.lines[y].data))
866 if leftChar == braceType[1] {
872 for x := xInit; x >= 0; x-- {
874 if r == braceType[0] {
877 if leftChar == braceType[1] {
878 return Loc{x, y}, true
880 return Loc{x, y}, false
882 } else if r == braceType[1] {
891 // Retab changes all tabs to spaces or vice versa
892 func (b *Buffer) Retab() {
893 toSpaces := b.Settings["tabstospaces"].(bool)
894 tabsize := util.IntOpt(b.Settings["tabsize"])
897 for i := 0; i < b.LinesNum(); i++ {
900 ws := util.GetLeadingWhitespace(l)
903 ws = bytes.Replace(ws, []byte{'\t'}, bytes.Repeat([]byte{' '}, tabsize), -1)
905 ws = bytes.Replace(ws, bytes.Repeat([]byte{' '}, tabsize), []byte{'\t'}, -1)
909 l = bytes.TrimLeft(l, " \t")
910 b.lines[i].data = append(ws, l...)
911 b.Modifications = append(b.Modifications, Loc{i, i})
918 // ParseCursorLocation turns a cursor location like 10:5 (LINE:COL)
920 func ParseCursorLocation(cursorPositions []string) (Loc, error) {
921 startpos := Loc{0, 0}
924 // if no positions are available exit early
925 if cursorPositions == nil {
926 return startpos, errors.New("No cursor positions were provided.")
929 startpos.Y, err = strconv.Atoi(cursorPositions[0])
932 if len(cursorPositions) > 1 {
933 startpos.X, err = strconv.Atoi(cursorPositions[1])
943 // Line returns the string representation of the given line number
944 func (b *Buffer) Line(i int) string {
945 return string(b.LineBytes(i))
948 func (b *Buffer) Write(bytes []byte) (n int, err error) {
949 b.EventHandler.InsertBytes(b.End(), bytes)
950 return len(bytes), nil
953 func (b *Buffer) updateDiffSync() {
955 defer b.diffLock.Unlock()
957 b.diff = make(map[int]DiffStatus)
959 if b.diffBase == nil {
964 baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(b.Bytes()))
965 diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
968 for _, diff := range diffs {
969 lineCount := len([]rune(diff.Text))
975 var status DiffStatus
976 if b.diff[lineN] == DSDeletedAbove {
981 for i := 0; i < lineCount; i++ {
982 b.diff[lineN] = status
986 b.diff[lineN] = DSDeletedAbove
991 // UpdateDiff computes the diff between the diff base and the buffer content.
992 // The update may be performed synchronously or asynchronously.
993 // UpdateDiff calls the supplied callback when the update is complete.
994 // The argument passed to the callback is set to true if and only if
995 // the update was performed synchronously.
996 // If an asynchronous update is already pending when UpdateDiff is called,
997 // UpdateDiff does not schedule another update, in which case the callback
999 func (b *Buffer) UpdateDiff(callback func(bool)) {
1000 if b.updateDiffTimer != nil {
1004 lineCount := b.LinesNum()
1005 if b.diffBaseLineCount > lineCount {
1006 lineCount = b.diffBaseLineCount
1009 if lineCount < 1000 {
1012 } else if lineCount < 30000 {
1013 b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
1014 b.updateDiffTimer = nil
1019 // Don't compute diffs for very large files
1021 b.diff = make(map[int]DiffStatus)
1027 // SetDiffBase sets the text that is used as the base for diffing the buffer content
1028 func (b *Buffer) SetDiffBase(diffBase []byte) {
1029 b.diffBase = diffBase
1030 if diffBase == nil {
1031 b.diffBaseLineCount = 0
1033 b.diffBaseLineCount = strings.Count(string(diffBase), "\n")
1035 b.UpdateDiff(func(synchronous bool) {
1036 screen.DrawChan <- true
1040 // DiffStatus returns the diff status for a line in the buffer
1041 func (b *Buffer) DiffStatus(lineN int) DiffStatus {
1043 defer b.diffLock.RUnlock()
1044 // Note that the zero value for DiffStatus is equal to DSUnchanged
1045 return b.diff[lineN]
1048 // WriteLog writes a string to the log buffer
1049 func WriteLog(s string) {
1050 LogBuf.EventHandler.Insert(LogBuf.End(), s)
1053 // GetLogBuf returns the log buffer
1054 func GetLogBuf() *Buffer {