]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/highlighter.go
Add cd and pwd commands to change the working dir
[micro.git] / cmd / micro / highlighter.go
index d1d46a1664d8e70c49abe3e98a9ad525de1a5bb7..c71870e50f4ae7da7e653e3ddf1f100d21d399ef 100644 (file)
@@ -1,11 +1,10 @@
 package main
 
 import (
-       "github.com/zyedidia/tcell"
-       "io/ioutil"
-       "path/filepath"
        "regexp"
        "strings"
+
+       "github.com/zyedidia/tcell"
 )
 
 // FileTypeRules represents a complete set of syntax rules for a filetype
@@ -29,129 +28,16 @@ type SyntaxRule struct {
 
 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
 
-// These syntax files are pre installed and embedded in the resulting binary by go-bindata
-var preInstalledSynFiles = []string{
-       "Dockerfile",
-       "apacheconf",
-       "arduino",
-       "asciidoc",
-       "asm",
-       "awk",
-       "c",
-       "cmake",
-       "coffeescript",
-       "colortest",
-       "conf",
-       "conky",
-       "csharp",
-       "css",
-       "cython",
-       "d",
-       "dot",
-       "erb",
-       "fish",
-       "fortran",
-       "gentoo-ebuild",
-       "gentoo-etc-portage",
-       "git-commit",
-       "git-config",
-       "git-rebase-todo",
-       "glsl",
-       "go",
-       "groff",
-       "haml",
-       "haskell",
-       "html",
-       "ini",
-       "inputrc",
-       "java",
-       "javascript",
-       "json",
-       "keymap",
-       "kickstart",
-       "ledger",
-       "lisp",
-       "lua",
-       "makefile",
-       "man",
-       "markdown",
-       "mpdconf",
-       "nanorc",
-       "nginx",
-       "ocaml",
-       "patch",
-       "peg",
-       "perl",
-       "perl6",
-       "php",
-       "pkg-config",
-       "pkgbuild",
-       "po",
-       "pov",
-       "privoxy-action",
-       "privoxy-config",
-       "privoxy-filter",
-       "puppet",
-       "python",
-       "r",
-       "reST",
-       "rpmspec",
-       "ruby",
-       "rust",
-       "scala",
-       "sed",
-       "sh",
-       "sls",
-       "sql",
-       "swift",
-       "systemd",
-       "tcl",
-       "tex",
-       "vala",
-       "vi",
-       "xml",
-       "xresources",
-       "yaml",
-       "yum",
-       "zsh",
-}
-
 // LoadSyntaxFiles loads the syntax files from the default directory (configDir)
 func LoadSyntaxFiles() {
-       // Load the user's custom syntax files, if there are any
-       LoadSyntaxFilesFromDir(configDir + "/syntax")
-
-       // Load the pre-installed syntax files from inside the binary
-       for _, filetype := range preInstalledSynFiles {
-               data, err := Asset("runtime/syntax/" + filetype + ".micro")
-               if err != nil {
-                       TermMessage("Unable to load pre-installed syntax file " + filetype)
-                       continue
-               }
-
-               LoadSyntaxFile(string(data), filetype+".micro")
-       }
-}
-
-// LoadSyntaxFilesFromDir loads the syntax files from a specified directory
-// To load the syntax files, we must fill the `syntaxFiles` map
-// This involves finding the regex for syntax and if it exists, the regex
-// for the header. Then we must get the text for the file and the filetype.
-func LoadSyntaxFilesFromDir(dir string) {
        InitColorscheme()
-
        syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
-       files, _ := ioutil.ReadDir(dir)
-       for _, f := range files {
-               if filepath.Ext(f.Name()) == ".micro" {
-                       filename := dir + "/" + f.Name()
-                       text, err := ioutil.ReadFile(filename)
-
-                       if err != nil {
-                               TermMessage("Error loading syntax file " + filename + ": " + err.Error())
-                               return
-                       }
-                       LoadSyntaxFile(string(text), filename)
+       for _, f := range ListRuntimeFiles(RTSyntax) {
+               data, err := f.Data()
+               if err != nil {
+                       TermMessage("Error loading syntax file " + f.Name() + ": " + err.Error())
+               } else {
+                       LoadSyntaxFile(string(data), f.Name())
                }
        }
 }
@@ -301,8 +187,20 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule {
                        // in which case we should look that up in the colorscheme
                        // They can also just give us a straight up color
                        st := defStyle
-                       if _, ok := colorscheme[color]; ok {
-                               st = colorscheme[color]
+                       groups := strings.Split(color, ".")
+                       if len(groups) > 1 {
+                               curGroup := ""
+                               for i, g := range groups {
+                                       if i != 0 {
+                                               curGroup += "."
+                                       }
+                                       curGroup += g
+                                       if style, ok := colorscheme[curGroup]; ok {
+                                               st = style
+                                       }
+                               }
+                       } else if style, ok := colorscheme[color]; ok {
+                               st = style
                        } else {
                                st = StringToStyle(color)
                        }
@@ -359,19 +257,32 @@ func LoadRulesFromFile(text, filename string) []SyntaxRule {
        return rules
 }
 
+// FindFileType finds the filetype for the given buffer
+func FindFileType(buf *Buffer) string {
+       for r := range syntaxFiles {
+               if r[1] != nil && r[1].MatchString(buf.Line(0)) {
+                       // The header statement matches the first line
+                       return syntaxFiles[r].filetype
+               }
+       }
+       for r := range syntaxFiles {
+               if r[0] != nil && r[0].MatchString(buf.Path) {
+                       // The syntax statement matches the extension
+                       return syntaxFiles[r].filetype
+               }
+       }
+       return "Unknown"
+}
+
 // GetRules finds the syntax rules that should be used for the buffer
 // and returns them. It also returns the filetype of the file
-func GetRules(buf *Buffer) ([]SyntaxRule, string) {
+func GetRules(buf *Buffer) []SyntaxRule {
        for r := range syntaxFiles {
-               if r[0] != nil && r[0].MatchString(buf.Path) {
-                       // Check if the syntax statement matches the extension
-                       return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
-               } else if r[1] != nil && r[1].MatchString(buf.Lines[0]) {
-                       // Check if the header statement matches the first line
-                       return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
+               if syntaxFiles[r].filetype == buf.FileType() {
+                       return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename)
                }
        }
-       return nil, "Unknown"
+       return nil
 }
 
 // SyntaxMatches is an alias to a map from character numbers to styles,
@@ -390,11 +301,14 @@ func Match(v *View) SyntaxMatches {
                viewEnd = buf.NumLines
        }
 
-       lines := buf.Lines[viewStart:viewEnd]
+       lines := buf.Lines(viewStart, viewEnd)
        matches := make(SyntaxMatches, len(lines))
 
        for i, line := range lines {
                matches[i] = make([]tcell.Style, len(line)+1)
+               for j := range matches[i] {
+                       matches[i][j] = defStyle
+               }
        }
 
        // We don't actually check the entire buffer, just from synLinesUp to synLinesDown
@@ -407,10 +321,8 @@ func Match(v *View) SyntaxMatches {
                totalEnd = buf.NumLines
        }
 
-       str := strings.Join(buf.Lines[totalStart:totalEnd], "\n")
-       startNum := ToCharPos(0, totalStart, v.Buf)
-
-       toplineNum := ToCharPos(0, v.Topline, v.Buf)
+       str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
+       startNum := ToCharPos(Loc{0, totalStart}, v.Buf)
 
        for _, rule := range rules {
                if rule.startend {
@@ -418,11 +330,13 @@ func Match(v *View) SyntaxMatches {
                                for _, value := range indicies {
                                        value[0] = runePos(value[0], str) + startNum
                                        value[1] = runePos(value[1], str) + startNum
-                                       for i := value[0]; i < value[1]; i++ {
-                                               if i < toplineNum {
+                                       startLoc := FromCharPos(value[0], buf)
+                                       endLoc := FromCharPos(value[1], buf)
+                                       for curLoc := startLoc; curLoc.LessThan(endLoc); curLoc = curLoc.Move(1, buf) {
+                                               if curLoc.Y < v.Topline {
                                                        continue
                                                }
-                                               colNum, lineNum := FromCharPosStart(toplineNum, 0, v.Topline, i, buf)
+                                               colNum, lineNum := curLoc.X, curLoc.Y
                                                if lineNum == -1 || colNum == -1 {
                                                        continue
                                                }