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..)
78 // Path to the file on disk
80 // Absolute path to the file on disk
82 // Name of the buffer on the status line
85 // Settings customized by the user
86 Settings map[string]interface{}
94 updateDiffTimer *time.Timer
98 diff map[int]DiffStatus
100 // counts the number of edits
101 // resets every backupTime edits
104 // ReloadDisabled allows the user to disable reloads if they
105 // are viewing a file that is constantly changing
109 // Whether or not suggestions can be autocompleted must be shared because
110 // it changes based on how the buffer has changed
113 // The Highlighter struct actually performs the highlighting
114 Highlighter *highlight.Highlighter
115 // SyntaxDef represents the syntax highlighting definition being used
116 // This stores the highlighting rules and filetype detection info
117 SyntaxDef *highlight.Def
119 ModifiedThisFrame bool
121 // Hash of the original buffer -- empty if fastdirty is on
122 origHash [md5.Size]byte
125 func (b *SharedBuffer) insert(pos Loc, value []byte) {
127 b.HasSuggestions = false
128 b.LineArray.insert(pos, value)
130 inslines := bytes.Count(value, []byte{'\n'})
131 b.MarkModified(pos.Y, pos.Y+inslines)
133 func (b *SharedBuffer) remove(start, end Loc) []byte {
135 b.HasSuggestions = false
136 defer b.MarkModified(start.Y, end.Y)
137 return b.LineArray.remove(start, end)
140 // MarkModified marks the buffer as modified for this frame
141 // and performs rehighlighting if syntax highlighting is enabled
142 func (b *SharedBuffer) MarkModified(start, end int) {
143 b.ModifiedThisFrame = true
145 if !b.Settings["syntax"].(bool) || b.SyntaxDef == nil {
149 start = util.Clamp(start, 0, len(b.lines))
150 end = util.Clamp(end, 0, len(b.lines))
153 for i := start; i <= end; i++ {
154 l = util.Max(b.Highlighter.ReHighlightStates(b, i), l)
156 b.Highlighter.HighlightMatches(b, start, l+1)
159 // DisableReload disables future reloads of this sharedbuffer
160 func (b *SharedBuffer) DisableReload() {
161 b.ReloadDisabled = true
173 // Buffer stores the main information about a currently open file including
174 // the actual text (in a LineArray), the undo/redo stack (in an EventHandler)
175 // all the cursors, the syntax highlighting info, the settings for the buffer
176 // and some misc info about modification time and path location.
177 // The syntax highlighting info must be stored with the buffer because the syntax
178 // highlighter attaches information to each line of the buffer for optimization
179 // purposes so it doesn't have to rehighlight everything on every update.
189 // NewBufferFromFile opens a new buffer using the given path
190 // It will also automatically handle `~`, and line/column with filename:l:c
191 // It will return an empty buffer if the path does not exist
192 // and an error if the file is a directory
193 func NewBufferFromFile(path string, btype BufType) (*Buffer, error) {
195 filename, cursorPos := util.GetPathAndCursorPosition(path)
196 filename, err = util.ReplaceHome(filename)
201 file, err := os.Open(filename)
202 fileInfo, _ := os.Stat(filename)
204 if err == nil && fileInfo.IsDir() {
205 return nil, errors.New("Error: " + filename + " is a directory and cannot be opened")
210 cursorLoc, cursorerr := ParseCursorLocation(cursorPos)
211 if cursorerr != nil {
212 cursorLoc = Loc{-1, -1}
217 // File does not exist -- create an empty buffer with that name
218 buf = NewBufferFromString("", filename, btype)
220 buf = NewBuffer(file, util.FSize(file), filename, cursorLoc, btype)
226 // NewBufferFromString creates a new buffer containing the given string
227 func NewBufferFromString(text, path string, btype BufType) *Buffer {
228 return NewBuffer(strings.NewReader(text), int64(len(text)), path, Loc{-1, -1}, btype)
231 // NewBuffer creates a new buffer from a given reader with a given path
232 // Ensure that ReadSettings and InitGlobalSettings have been called before creating
234 // Places the cursor at startcursor. If startcursor is -1, -1 places the
235 // cursor at an autodetected location (based on savecursor or :LINE:COL)
236 func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufType) *Buffer {
237 absPath, _ := filepath.Abs(path)
243 for _, buf := range OpenBuffers {
244 if buf.AbsPath == absPath && buf.Type != BTInfo {
246 b.SharedBuffer = buf.SharedBuffer
247 b.EventHandler = buf.EventHandler
253 b.SharedBuffer = new(SharedBuffer)
259 b.Settings = config.DefaultCommonSettings()
260 for k, v := range config.GlobalSettings {
261 if _, ok := config.DefaultGlobalOnlySettings[k]; !ok {
262 // make sure setting is not global-only
266 config.InitLocalSettings(b.Settings, path)
268 enc, err := htmlindex.Get(b.Settings["encoding"].(string))
271 b.Settings["encoding"] = "utf-8"
274 hasBackup := b.ApplyBackup(size)
277 reader := bufio.NewReader(transform.NewReader(r, enc.NewDecoder()))
278 b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
280 b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors)
282 // The last time this file was modified
286 if b.Settings["readonly"].(bool) && b.Type == BTDefault {
287 b.Type.Readonly = true
292 b.Settings["fileformat"] = "unix"
294 b.Settings["fileformat"] = "dos"
298 // init local settings again now that we know the filetype
299 config.InitLocalSettings(b.Settings, b.Path)
301 if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); os.IsNotExist(err) {
302 os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
305 if startcursor.X != -1 && startcursor.Y != -1 {
306 b.StartCursor = startcursor
308 if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
309 err := b.Unserialize()
311 screen.TermMessage(err)
316 b.AddCursor(NewCursor(b, b.StartCursor))
317 b.GetActiveCursor().Relocate()
319 if !b.Settings["fastdirty"].(bool) && !found {
320 if size > LargeFileThreshold {
321 // If the file is larger than LargeFileThreshold fastdirty needs to be on
322 b.Settings["fastdirty"] = true
324 calcHash(b, &b.origHash)
328 err := config.RunPluginFn("onBufferOpen", luar.New(ulua.L, b))
330 screen.TermMessage(err)
333 OpenBuffers = append(OpenBuffers, b)
338 // Close removes this buffer from the list of open buffers
339 func (b *Buffer) Close() {
340 for i, buf := range OpenBuffers {
343 copy(OpenBuffers[i:], OpenBuffers[i+1:])
344 OpenBuffers[len(OpenBuffers)-1] = nil
345 OpenBuffers = OpenBuffers[:len(OpenBuffers)-1]
351 // Fini should be called when a buffer is closed and performs
353 func (b *Buffer) Fini() {
360 // GetName returns the name that should be displayed in the statusline
362 func (b *Buffer) GetName() string {
370 if b.Settings["basename"].(bool) {
371 return path.Base(name)
376 //SetName changes the name for this buffer
377 func (b *Buffer) SetName(s string) {
381 // Insert inserts the given string of text at the start location
382 func (b *Buffer) Insert(start Loc, text string) {
383 if !b.Type.Readonly {
384 b.EventHandler.cursors = b.cursors
385 b.EventHandler.active = b.curCursor
386 b.EventHandler.Insert(start, text)
392 // Remove removes the characters between the start and end locations
393 func (b *Buffer) Remove(start, end Loc) {
394 if !b.Type.Readonly {
395 b.EventHandler.cursors = b.cursors
396 b.EventHandler.active = b.curCursor
397 b.EventHandler.Remove(start, end)
403 // FileType returns the buffer's filetype
404 func (b *Buffer) FileType() string {
405 return b.Settings["filetype"].(string)
408 // ExternallyModified returns whether the file being edited has
409 // been modified by some external process
410 func (b *Buffer) ExternallyModified() bool {
411 modTime, err := util.GetModTime(b.Path)
413 return modTime != b.ModTime
418 // UpdateModTime updates the modtime of this file
419 func (b *Buffer) UpdateModTime() (err error) {
420 b.ModTime, err = util.GetModTime(b.Path)
424 // ReOpen reloads the current buffer from disk
425 func (b *Buffer) ReOpen() error {
426 file, err := os.Open(b.Path)
431 enc, err := htmlindex.Get(b.Settings["encoding"].(string))
436 reader := bufio.NewReader(transform.NewReader(file, enc.NewDecoder()))
437 data, err := ioutil.ReadAll(reader)
443 b.EventHandler.ApplyDiff(txt)
445 err = b.UpdateModTime()
446 if !b.Settings["fastdirty"].(bool) {
447 calcHash(b, &b.origHash)
454 // RelocateCursors relocates all cursors (makes sure they are in the buffer)
455 func (b *Buffer) RelocateCursors() {
456 for _, c := range b.cursors {
461 // RuneAt returns the rune at a given location in the buffer
462 func (b *Buffer) RuneAt(loc Loc) rune {
463 line := b.LineBytes(loc.Y)
467 r, size := utf8.DecodeRune(line)
479 // Modified returns if this buffer has been modified since
481 func (b *Buffer) Modified() bool {
486 if b.Settings["fastdirty"].(bool) {
490 var buff [md5.Size]byte
493 return buff != b.origHash
496 // calcHash calculates md5 hash of all lines in the buffer
497 func calcHash(b *Buffer, out *[md5.Size]byte) error {
501 if len(b.lines) > 0 {
502 n, e := h.Write(b.lines[0].data)
508 for _, l := range b.lines[1:] {
509 n, e = h.Write([]byte{'\n'})
514 n, e = h.Write(l.data)
522 if size > LargeFileThreshold {
523 return ErrFileTooLarge
530 // UpdateRules updates the syntax rules and filetype for this buffer
531 // This is called when the colorscheme changes
532 func (b *Buffer) UpdateRules() {
536 ft := b.Settings["filetype"].(string)
541 var header *highlight.Header
542 for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
543 data, err := f.Data()
545 screen.TermMessage("Error loading syntax header file " + f.Name() + ": " + err.Error())
549 header, err = highlight.MakeHeader(data)
551 screen.TermMessage("Error reading syntax header file", f.Name(), err)
555 if ft == "unknown" || ft == "" {
556 if highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data) {
557 syntaxFile = f.Name()
560 } else if header.FileType == ft {
561 syntaxFile = f.Name()
566 if syntaxFile == "" {
567 // search for the syntax file in the user's custom syntax files
568 for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
569 data, err := f.Data()
571 screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
575 header, err = highlight.MakeHeaderYaml(data)
576 file, err := highlight.ParseFile(data)
578 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
582 if ((ft == "unknown" || ft == "") && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
583 syndef, err := highlight.ParseDef(file, header)
585 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
593 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
594 if f.Name() == syntaxFile {
595 data, err := f.Data()
597 screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
601 file, err := highlight.ParseFile(data)
603 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
607 syndef, err := highlight.ParseDef(file, header)
609 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
618 if b.SyntaxDef != nil && highlight.HasIncludes(b.SyntaxDef) {
619 includes := highlight.GetIncludes(b.SyntaxDef)
621 var files []*highlight.File
622 for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
623 data, err := f.Data()
625 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
628 header, err := highlight.MakeHeaderYaml(data)
630 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
634 for _, i := range includes {
635 if header.FileType == i {
636 file, err := highlight.ParseFile(data)
638 screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
641 files = append(files, file)
645 if len(files) >= len(includes) {
650 highlight.ResolveIncludes(b.SyntaxDef, files)
653 if b.Highlighter == nil || syntaxFile != "" {
654 if b.SyntaxDef != nil {
655 b.Settings["filetype"] = b.SyntaxDef.FileType
658 b.SyntaxDef = &highlight.EmptyDef
661 if b.SyntaxDef != nil {
662 b.Highlighter = highlight.NewHighlighter(b.SyntaxDef)
663 if b.Settings["syntax"].(bool) {
665 b.Highlighter.HighlightStates(b)
666 b.Highlighter.HighlightMatches(b, 0, b.End().Y)
673 // ClearMatches clears all of the syntax highlighting for the buffer
674 func (b *Buffer) ClearMatches() {
675 for i := range b.lines {
681 // IndentString returns this buffer's indent method (a tabstop or n spaces
682 // depending on the settings)
683 func (b *Buffer) IndentString(tabsize int) string {
684 if b.Settings["tabstospaces"].(bool) {
685 return util.Spaces(tabsize)
690 // SetCursors resets this buffer's cursors to a new list
691 func (b *Buffer) SetCursors(c []*Cursor) {
693 b.EventHandler.cursors = b.cursors
694 b.EventHandler.active = b.curCursor
697 // AddCursor adds a new cursor to the list
698 func (b *Buffer) AddCursor(c *Cursor) {
699 b.cursors = append(b.cursors, c)
700 b.EventHandler.cursors = b.cursors
701 b.EventHandler.active = b.curCursor
705 // SetCurCursor sets the current cursor
706 func (b *Buffer) SetCurCursor(n int) {
710 // GetActiveCursor returns the main cursor in this buffer
711 func (b *Buffer) GetActiveCursor() *Cursor {
712 return b.cursors[b.curCursor]
715 // GetCursor returns the nth cursor
716 func (b *Buffer) GetCursor(n int) *Cursor {
720 // GetCursors returns the list of cursors in this buffer
721 func (b *Buffer) GetCursors() []*Cursor {
725 // NumCursors returns the number of cursors
726 func (b *Buffer) NumCursors() int {
727 return len(b.cursors)
730 // MergeCursors merges any cursors that are at the same position
732 func (b *Buffer) MergeCursors() {
733 var cursors []*Cursor
734 for i := 0; i < len(b.cursors); i++ {
737 for j := 0; j < len(b.cursors); j++ {
739 if c2 != nil && i != j && c1.Loc == c2.Loc {
743 cursors = append(cursors, c1)
749 for i := range b.cursors {
753 if b.curCursor >= len(b.cursors) {
754 b.curCursor = len(b.cursors) - 1
756 b.EventHandler.cursors = b.cursors
757 b.EventHandler.active = b.curCursor
760 // UpdateCursors updates all the cursors indicies
761 func (b *Buffer) UpdateCursors() {
762 b.EventHandler.cursors = b.cursors
763 b.EventHandler.active = b.curCursor
764 for i, c := range b.cursors {
769 func (b *Buffer) RemoveCursor(i int) {
770 copy(b.cursors[i:], b.cursors[i+1:])
771 b.cursors[len(b.cursors)-1] = nil
772 b.cursors = b.cursors[:len(b.cursors)-1]
773 b.curCursor = util.Clamp(b.curCursor, 0, len(b.cursors)-1)
777 // ClearCursors removes all extra cursors
778 func (b *Buffer) ClearCursors() {
779 for i := 1; i < len(b.cursors); i++ {
782 b.cursors = b.cursors[:1]
785 b.GetActiveCursor().ResetSelection()
788 // MoveLinesUp moves the range of lines up one row
789 func (b *Buffer) MoveLinesUp(start int, end int) {
790 if start < 1 || start >= end || end > len(b.lines) {
793 l := string(b.LineBytes(start - 1))
794 if end == len(b.lines) {
797 utf8.RuneCount(b.lines[end-1].data),
814 // MoveLinesDown moves the range of lines down one row
815 func (b *Buffer) MoveLinesDown(start int, end int) {
816 if start < 0 || start >= end || end >= len(b.lines)-1 {
819 l := string(b.LineBytes(end))
831 var BracePairs = [][2]rune{
837 // FindMatchingBrace returns the location in the buffer of the matching bracket
838 // It is given a brace type containing the open and closing character, (for example
839 // '{' and '}') as well as the location to match from
840 // TODO: maybe can be more efficient with utf8 package
841 // returns the location of the matching brace
842 // if the boolean returned is true then the original matching brace is one character left
843 // of the starting location
844 func (b *Buffer) FindMatchingBrace(braceType [2]rune, start Loc) (Loc, bool, bool) {
845 curLine := []rune(string(b.LineBytes(start.Y)))
847 if start.X >= 0 && start.X < len(curLine) {
848 startChar = curLine[start.X]
851 if start.X-1 >= 0 && start.X-1 < len(curLine) {
852 leftChar = curLine[start.X-1]
855 if startChar == braceType[0] || leftChar == braceType[0] {
856 for y := start.Y; y < b.LinesNum(); y++ {
857 l := []rune(string(b.LineBytes(y)))
860 if startChar == braceType[0] {
866 for x := xInit; x < len(l); x++ {
868 if r == braceType[0] {
870 } else if r == braceType[1] {
873 if startChar == braceType[0] {
874 return Loc{x, y}, false, true
876 return Loc{x, y}, true, true
881 } else if startChar == braceType[1] || leftChar == braceType[1] {
882 for y := start.Y; y >= 0; y-- {
883 l := []rune(string(b.lines[y].data))
886 if leftChar == braceType[1] {
892 for x := xInit; x >= 0; x-- {
894 if r == braceType[0] {
897 if leftChar == braceType[1] {
898 return Loc{x, y}, true, true
900 return Loc{x, y}, false, true
902 } else if r == braceType[1] {
908 return start, true, false
911 // Retab changes all tabs to spaces or vice versa
912 func (b *Buffer) Retab() {
913 toSpaces := b.Settings["tabstospaces"].(bool)
914 tabsize := util.IntOpt(b.Settings["tabsize"])
917 for i := 0; i < b.LinesNum(); i++ {
920 ws := util.GetLeadingWhitespace(l)
923 ws = bytes.Replace(ws, []byte{'\t'}, bytes.Repeat([]byte{' '}, tabsize), -1)
925 ws = bytes.Replace(ws, bytes.Repeat([]byte{' '}, tabsize), []byte{'\t'}, -1)
929 l = bytes.TrimLeft(l, " \t")
930 b.lines[i].data = append(ws, l...)
938 // ParseCursorLocation turns a cursor location like 10:5 (LINE:COL)
940 func ParseCursorLocation(cursorPositions []string) (Loc, error) {
941 startpos := Loc{0, 0}
944 // if no positions are available exit early
945 if cursorPositions == nil {
946 return startpos, errors.New("No cursor positions were provided.")
949 startpos.Y, err = strconv.Atoi(cursorPositions[0])
952 if len(cursorPositions) > 1 {
953 startpos.X, err = strconv.Atoi(cursorPositions[1])
963 // Line returns the string representation of the given line number
964 func (b *Buffer) Line(i int) string {
965 return string(b.LineBytes(i))
968 func (b *Buffer) Write(bytes []byte) (n int, err error) {
969 b.EventHandler.InsertBytes(b.End(), bytes)
970 return len(bytes), nil
973 func (b *Buffer) updateDiffSync() {
975 defer b.diffLock.Unlock()
977 b.diff = make(map[int]DiffStatus)
979 if b.diffBase == nil {
984 baseRunes, bufferRunes, _ := differ.DiffLinesToRunes(string(b.diffBase), string(b.Bytes()))
985 diffs := differ.DiffMainRunes(baseRunes, bufferRunes, false)
988 for _, diff := range diffs {
989 lineCount := len([]rune(diff.Text))
995 var status DiffStatus
996 if b.diff[lineN] == DSDeletedAbove {
1001 for i := 0; i < lineCount; i++ {
1002 b.diff[lineN] = status
1005 case dmp.DiffDelete:
1006 b.diff[lineN] = DSDeletedAbove
1011 // UpdateDiff computes the diff between the diff base and the buffer content.
1012 // The update may be performed synchronously or asynchronously.
1013 // UpdateDiff calls the supplied callback when the update is complete.
1014 // The argument passed to the callback is set to true if and only if
1015 // the update was performed synchronously.
1016 // If an asynchronous update is already pending when UpdateDiff is called,
1017 // UpdateDiff does not schedule another update, in which case the callback
1019 func (b *Buffer) UpdateDiff(callback func(bool)) {
1020 if b.updateDiffTimer != nil {
1024 lineCount := b.LinesNum()
1025 if b.diffBaseLineCount > lineCount {
1026 lineCount = b.diffBaseLineCount
1029 if lineCount < 1000 {
1032 } else if lineCount < 30000 {
1033 b.updateDiffTimer = time.AfterFunc(500*time.Millisecond, func() {
1034 b.updateDiffTimer = nil
1039 // Don't compute diffs for very large files
1041 b.diff = make(map[int]DiffStatus)
1047 // SetDiffBase sets the text that is used as the base for diffing the buffer content
1048 func (b *Buffer) SetDiffBase(diffBase []byte) {
1049 b.diffBase = diffBase
1050 if diffBase == nil {
1051 b.diffBaseLineCount = 0
1053 b.diffBaseLineCount = strings.Count(string(diffBase), "\n")
1055 b.UpdateDiff(func(synchronous bool) {
1060 // DiffStatus returns the diff status for a line in the buffer
1061 func (b *Buffer) DiffStatus(lineN int) DiffStatus {
1063 defer b.diffLock.RUnlock()
1064 // Note that the zero value for DiffStatus is equal to DSUnchanged
1065 return b.diff[lineN]
1068 // WriteLog writes a string to the log buffer
1069 func WriteLog(s string) {
1070 LogBuf.EventHandler.Insert(LogBuf.End(), s)
1073 // GetLogBuf returns the log buffer
1074 func GetLogBuf() *Buffer {