]> git.lizzy.rs Git - micro.git/blobdiff - internal/buffer/buffer.go
Support csharp-script syntax. (#1425)
[micro.git] / internal / buffer / buffer.go
index 87bf8c0140530d3d3eb02a009ade481afde62789..fc74efe5b9b1cede591db6507b4fb94c6961c0a3 100644 (file)
@@ -5,10 +5,11 @@ import (
        "bytes"
        "crypto/md5"
        "errors"
+       "fmt"
        "io"
        "io/ioutil"
-       "log"
        "os"
+       "path"
        "path/filepath"
        "strconv"
        "strings"
@@ -60,6 +61,9 @@ var (
        BTRaw = BufType{4, false, true, false}
        // BTInfo is a buffer for inputting information
        BTInfo = BufType{5, false, true, false}
+       // BTStdout is a buffer that only writes to stdout
+       // when closed
+       BTStdout = BufType{6, false, true, true}
 
        // ErrFileTooLarge is returned when the file is too large to hash
        // (fastdirty is automatically enabled)
@@ -82,6 +86,8 @@ type SharedBuffer struct {
        // Name of the buffer on the status line
        name string
 
+       toStdout bool
+
        // Settings customized by the user
        Settings map[string]interface{}
 
@@ -128,7 +134,6 @@ func (b *SharedBuffer) insert(pos Loc, value []byte) {
        b.LineArray.insert(pos, value)
 
        inslines := bytes.Count(value, []byte{'\n'})
-       log.Println("insert", string(value), pos.Y, pos.Y+inslines)
        b.MarkModified(pos.Y, pos.Y+inslines)
 }
 func (b *SharedBuffer) remove(start, end Loc) []byte {
@@ -147,12 +152,10 @@ func (b *SharedBuffer) MarkModified(start, end int) {
                return
        }
 
-       log.Println("Modified", start, end, len(b.lines))
        start = util.Clamp(start, 0, len(b.lines))
        end = util.Clamp(end, 0, len(b.lines))
 
        l := -1
-       log.Println("Modified", start, end)
        for i := start; i <= end; i++ {
                l = util.Max(b.Highlighter.ReHighlightStates(b, i), l)
        }
@@ -281,15 +284,15 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
                        b.LineArray = NewLineArray(uint64(size), FFAuto, reader)
                }
                b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors)
+
+               // The last time this file was modified
+               b.UpdateModTime()
        }
 
        if b.Settings["readonly"].(bool) && b.Type == BTDefault {
                b.Type.Readonly = true
        }
 
-       // The last time this file was modified
-       b.UpdateModTime()
-
        switch b.Endings {
        case FFUnix:
                b.Settings["fileformat"] = "unix"
@@ -319,7 +322,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
        b.AddCursor(NewCursor(b, b.StartCursor))
        b.GetActiveCursor().Relocate()
 
-       if !b.Settings["fastdirty"].(bool) {
+       if !b.Settings["fastdirty"].(bool) && !found {
                if size > LargeFileThreshold {
                        // If the file is larger than LargeFileThreshold fastdirty needs to be on
                        b.Settings["fastdirty"] = true
@@ -358,18 +361,26 @@ func (b *Buffer) Fini() {
                b.Serialize()
        }
        b.RemoveBackup()
+
+       if b.Type == BTStdout {
+               fmt.Fprint(util.Stdout, string(b.Bytes()))
+       }
 }
 
 // GetName returns the name that should be displayed in the statusline
 // for this buffer
 func (b *Buffer) GetName() string {
-       if b.name == "" {
+       name := b.name
+       if name == "" {
                if b.Path == "" {
                        return "No name"
                }
-               return b.Path
+               name = b.Path
        }
-       return b.name
+       if b.Settings["basename"].(bool) {
+               return path.Base(name)
+       }
+       return name
 }
 
 //SetName changes the name for this buffer
@@ -537,7 +548,37 @@ func (b *Buffer) UpdateRules() {
                return
        }
        syntaxFile := ""
+       foundDef := false
        var header *highlight.Header
+       // search for the syntax file in the user's custom syntax files
+       for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
+               data, err := f.Data()
+               if err != nil {
+                       screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
+                       continue
+               }
+
+               header, err = highlight.MakeHeaderYaml(data)
+               file, err := highlight.ParseFile(data)
+               if err != nil {
+                       screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
+                       continue
+               }
+
+               if ((ft == "unknown" || ft == "") && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
+                       syndef, err := highlight.ParseDef(file, header)
+                       if err != nil {
+                               screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
+                               continue
+                       }
+                       b.SyntaxDef = syndef
+                       syntaxFile = f.Name()
+                       foundDef = true
+                       break
+               }
+       }
+
+       // search in the default syntax files
        for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) {
                data, err := f.Data()
                if err != nil {
@@ -562,34 +603,8 @@ func (b *Buffer) UpdateRules() {
                }
        }
 
-       if syntaxFile == "" {
-               // search for the syntax file in the user's custom syntax files
-               for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) {
-                       log.Println("real runtime file", f.Name())
-                       data, err := f.Data()
-                       if err != nil {
-                               screen.TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
-                               continue
-                       }
-
-                       header, err = highlight.MakeHeaderYaml(data)
-                       file, err := highlight.ParseFile(data)
-                       if err != nil {
-                               screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
-                               continue
-                       }
-
-                       if ((ft == "unknown" || ft == "") && highlight.MatchFiletype(header.FtDetect, b.Path, b.lines[0].data)) || header.FileType == ft {
-                               syndef, err := highlight.ParseDef(file, header)
-                               if err != nil {
-                                       screen.TermMessage("Error parsing syntax file " + f.Name() + ": " + err.Error())
-                                       continue
-                               }
-                               b.SyntaxDef = syndef
-                               break
-                       }
-               }
-       } else {
+       if syntaxFile != "" && !foundDef {
+               // we found a syntax file using a syntax header file
                for _, f := range config.ListRuntimeFiles(config.RTSyntax) {
                        if f.Name() == syntaxFile {
                                data, err := f.Data()
@@ -783,7 +798,6 @@ func (b *Buffer) ClearCursors() {
        b.UpdateCursors()
        b.curCursor = 0
        b.GetActiveCursor().ResetSelection()
-       log.Println("Cleared cursors:", len(b.cursors))
 }
 
 // MoveLinesUp moves the range of lines up one row