9 "github.com/zyedidia/tcell"
12 // FileTypeRules represents a complete set of syntax rules for a filetype
13 type FileTypeRules struct {
19 // SyntaxRule represents a regex to highlight in a certain style
20 type SyntaxRule struct {
25 // Whether this regex is a start=... end=... regex
27 // How to highlight it
31 var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
33 // These syntax files are pre installed and embedded in the resulting binary by go-bindata
34 var preInstalledSynFiles = []string{
126 // LoadSyntaxFiles loads the syntax files from the default directory (configDir)
127 func LoadSyntaxFiles() {
128 // Load the user's custom syntax files, if there are any
129 LoadSyntaxFilesFromDir(configDir + "/syntax")
131 // Load the pre-installed syntax files from inside the binary
132 for _, filetype := range preInstalledSynFiles {
133 data, err := Asset("runtime/syntax/" + filetype + ".micro")
135 TermMessage("Unable to load pre-installed syntax file " + filetype)
139 LoadSyntaxFile(string(data), filetype+".micro")
143 // LoadSyntaxFilesFromDir loads the syntax files from a specified directory
144 // To load the syntax files, we must fill the `syntaxFiles` map
145 // This involves finding the regex for syntax and if it exists, the regex
146 // for the header. Then we must get the text for the file and the filetype.
147 func LoadSyntaxFilesFromDir(dir string) {
148 colorscheme = make(Colorscheme)
152 defStyle = tcell.StyleDefault.
153 Foreground(tcell.ColorDefault).
154 Background(tcell.ColorDefault)
156 // There may be another default style defined in the colorscheme
157 // In that case we should use that one
158 if style, ok := colorscheme["default"]; ok {
162 screen.SetStyle(defStyle)
165 syntaxFiles = make(map[[2]*regexp.Regexp]FileTypeRules)
166 files, _ := ioutil.ReadDir(dir)
167 for _, f := range files {
168 if filepath.Ext(f.Name()) == ".micro" {
169 filename := dir + "/" + f.Name()
170 text, err := ioutil.ReadFile(filename)
173 TermMessage("Error loading syntax file " + filename + ": " + err.Error())
176 LoadSyntaxFile(string(text), filename)
181 // JoinRule takes a syntax rule (which can be multiple regular expressions)
182 // and joins it into one regular expression by ORing everything together
183 func JoinRule(rule string) string {
184 split := strings.Split(rule, `" "`)
185 joined := strings.Join(split, ")|(")
186 joined = "(" + joined + ")"
190 // LoadSyntaxFile simply gets the filetype of a the syntax file and the source for the
191 // file and creates FileTypeRules out of it. If this filetype is the one opened by the user
192 // the rules will be loaded and compiled later
193 // In this function we are only concerned with loading the syntax and header regexes
194 func LoadSyntaxFile(text, filename string) {
196 lines := strings.Split(string(text), "\n")
198 // Regex for parsing syntax statements
199 syntaxParser := regexp.MustCompile(`syntax "(.*?)"\s+"(.*)"+`)
200 // Regex for parsing header statements
201 headerParser := regexp.MustCompile(`header "(.*)"`)
203 // Is there a syntax definition in this file?
204 hasSyntax := syntaxParser.MatchString(text)
205 // Is there a header definition in this file?
206 hasHeader := headerParser.MatchString(text)
208 var syntaxRegex *regexp.Regexp
209 var headerRegex *regexp.Regexp
211 for lineNum, line := range lines {
212 if (hasSyntax == (syntaxRegex != nil)) && (hasHeader == (headerRegex != nil)) {
213 // We found what we we're supposed to find
217 if strings.TrimSpace(line) == "" ||
218 strings.TrimSpace(line)[0] == '#' {
223 if strings.HasPrefix(line, "syntax") {
225 syntaxMatches := syntaxParser.FindSubmatch([]byte(line))
226 if len(syntaxMatches) == 3 {
227 if syntaxRegex != nil {
228 TermError(filename, lineNum, "Syntax statement redeclaration")
231 filetype = string(syntaxMatches[1])
232 extensions := JoinRule(string(syntaxMatches[2]))
234 syntaxRegex, err = regexp.Compile(extensions)
236 TermError(filename, lineNum, err.Error())
240 TermError(filename, lineNum, "Syntax statement is not valid: "+line)
243 } else if strings.HasPrefix(line, "header") {
245 headerMatches := headerParser.FindSubmatch([]byte(line))
246 if len(headerMatches) == 2 {
247 header := JoinRule(string(headerMatches[1]))
249 headerRegex, err = regexp.Compile(header)
251 TermError(filename, lineNum, "Regex error: "+err.Error())
255 TermError(filename, lineNum, "Header statement is not valid: "+line)
260 if syntaxRegex != nil {
261 // Add the current rules to the syntaxFiles variable
262 regexes := [2]*regexp.Regexp{syntaxRegex, headerRegex}
263 syntaxFiles[regexes] = FileTypeRules{filetype, filename, text}
267 // LoadRulesFromFile loads just the syntax rules from a given file
268 // Only the necessary rules are loaded when the buffer is opened.
269 // If we load all the rules for every filetype when micro starts, there's a bit of lag
270 // A rule just explains how to color certain regular expressions
271 // Example: color comment "//.*"
272 // This would color all strings that match the regex "//.*" in the comment color defined
273 // by the colorscheme
274 func LoadRulesFromFile(text, filename string) []SyntaxRule {
275 lines := strings.Split(string(text), "\n")
277 // Regex for parsing standard syntax rules
278 ruleParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?"(.*)"`)
279 // Regex for parsing syntax rules with start="..." end="..."
280 ruleStartEndParser := regexp.MustCompile(`color (.*?)\s+(?:\((.*?)\)\s+)?start="(.*)"\s+end="(.*)"`)
282 var rules []SyntaxRule
283 for lineNum, line := range lines {
284 if strings.TrimSpace(line) == "" ||
285 strings.TrimSpace(line)[0] == '#' ||
286 strings.HasPrefix(line, "syntax") ||
287 strings.HasPrefix(line, "header") {
292 // Syntax rule, but it could be standard or start-end
293 if ruleParser.MatchString(line) {
294 // Standard syntax rule
296 submatch := ruleParser.FindSubmatch([]byte(line))
300 if len(submatch) == 4 {
301 // If len is 4 then the user specified some additional flags to use
302 color = string(submatch[1])
303 flags = string(submatch[2])
304 regexStr = "(?" + flags + ")" + JoinRule(string(submatch[3]))
305 } else if len(submatch) == 3 {
306 // If len is 3, no additional flags were given
307 color = string(submatch[1])
308 regexStr = JoinRule(string(submatch[2]))
310 // If len is not 3 or 4 there is a problem
311 TermError(filename, lineNum, "Invalid statement: "+line)
315 regex, err := regexp.Compile(regexStr)
317 TermError(filename, lineNum, err.Error())
322 // The user could give us a "color" that is really a part of the colorscheme
323 // in which case we should look that up in the colorscheme
324 // They can also just give us a straight up color
326 groups := strings.Split(color, ".")
329 for i, g := range groups {
334 if style, ok := colorscheme[curGroup]; ok {
338 } else if style, ok := colorscheme[color]; ok {
341 st = StringToStyle(color)
343 // Add the regex, flags, and style
344 // False because this is not start-end
345 rules = append(rules, SyntaxRule{regex, flags, false, st})
346 } else if ruleStartEndParser.MatchString(line) {
347 // Start-end syntax rule
348 submatch := ruleStartEndParser.FindSubmatch([]byte(line))
352 // Use m and s flags by default
354 if len(submatch) == 5 {
355 // If len is 5 the user provided some additional flags
356 color = string(submatch[1])
357 flags += string(submatch[2])
358 start = string(submatch[3])
359 end = string(submatch[4])
360 } else if len(submatch) == 4 {
361 // If len is 4 the user did not provide additional flags
362 color = string(submatch[1])
363 start = string(submatch[2])
364 end = string(submatch[3])
366 // If len is not 4 or 5 there is a problem
367 TermError(filename, lineNum, "Invalid statement: "+line)
372 regex, err := regexp.Compile("(?" + flags + ")" + "(" + start + ").*?(" + end + ")")
374 TermError(filename, lineNum, err.Error())
379 // The user could give us a "color" that is really a part of the colorscheme
380 // in which case we should look that up in the colorscheme
381 // They can also just give us a straight up color
383 if _, ok := colorscheme[color]; ok {
384 st = colorscheme[color]
386 st = StringToStyle(color)
388 // Add the regex, flags, and style
389 // True because this is start-end
390 rules = append(rules, SyntaxRule{regex, flags, true, st})
396 // FindFileType finds the filetype for the given buffer
397 func FindFileType(buf *Buffer) string {
398 for r := range syntaxFiles {
399 if r[0] != nil && r[0].MatchString(buf.Path) {
400 // The syntax statement matches the extension
401 return syntaxFiles[r].filetype
402 } else if r[1] != nil && r[1].MatchString(buf.Line(0)) {
403 // The header statement matches the first line
404 return syntaxFiles[r].filetype
410 // GetRules finds the syntax rules that should be used for the buffer
411 // and returns them. It also returns the filetype of the file
412 func GetRules(buf *Buffer) []SyntaxRule {
413 for r := range syntaxFiles {
414 if syntaxFiles[r].filetype == buf.FileType() {
415 return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename)
421 // SyntaxMatches is an alias to a map from character numbers to styles,
422 // so map[3] represents the style of the third character
423 type SyntaxMatches [][]tcell.Style
425 // Match takes a buffer and returns the syntax matches: a 2d array specifying how it should be syntax highlighted
426 // We match the rules from up `synLinesUp` lines and down `synLinesDown` lines
427 func Match(v *View) SyntaxMatches {
431 viewStart := v.Topline
432 viewEnd := v.Topline + v.height
433 if viewEnd > buf.NumLines {
434 viewEnd = buf.NumLines
437 lines := buf.Lines(viewStart, viewEnd)
438 matches := make(SyntaxMatches, len(lines))
440 for i, line := range lines {
441 matches[i] = make([]tcell.Style, len(line)+1)
442 for j := range matches[i] {
443 matches[i][j] = defStyle
447 // We don't actually check the entire buffer, just from synLinesUp to synLinesDown
448 totalStart := v.Topline - synLinesUp
449 totalEnd := v.Topline + v.height + synLinesDown
453 if totalEnd > buf.NumLines {
454 totalEnd = buf.NumLines
457 str := strings.Join(buf.Lines(totalStart, totalEnd), "\n")
458 startNum := ToCharPos(Loc{0, totalStart}, v.Buf)
460 for _, rule := range rules {
462 if indicies := rule.regex.FindAllStringIndex(str, -1); indicies != nil {
463 for _, value := range indicies {
464 value[0] = runePos(value[0], str) + startNum
465 value[1] = runePos(value[1], str) + startNum
466 startLoc := FromCharPos(value[0], buf)
467 endLoc := FromCharPos(value[1], buf)
468 for curLoc := startLoc; curLoc.LessThan(endLoc); curLoc = curLoc.Move(1, buf) {
469 if curLoc.Y < v.Topline {
472 colNum, lineNum := curLoc.X, curLoc.Y
473 if lineNum == -1 || colNum == -1 {
477 if lineNum >= 0 && lineNum < v.height {
478 matches[lineNum][colNum] = rule.style
484 for lineN, line := range lines {
485 if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil {
486 for _, value := range indicies {
487 start := runePos(value[0], line)
488 end := runePos(value[1], line)
489 for i := start; i < end; i++ {
490 matches[lineN][i] = rule.style