Better R support in pygments by monkey patching SLexer

I started using knitr with reStructuredText today and I found that the syntax highlighting with pygments (used by was not as nice as the output of pandoc. So I ended up doing some monkeypatching.

Try adding the following to

# SLexer is the lexer used for R
from pygments.lexers.math import SLexer
from pygments.token import Keyword, Name

# monkey patching SLexer ...

# add some builtin functions (TODO: add more)
   (r'(?<![A-Za-z0-9_-])(c|library)(?=\()', Name.Builtin))

# treat all names in front of a parenthesis as function names
   (r'[a-zA-Z][a-zA-Z_0-9]+(?=\s*\()', Name.Function))

# parameter names inside function calls/definitions
   (r'(?<=[\(,])\s*[a-z]+\s*(?==)', Name.Attribute)) 



Note: I assume you already added pygments’ to

Tags: , , ,

Leave a Reply

Your email address will not be published.