Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More advanced syntax coloring in emacs for Python

I've been using emacs 24 (built from the git version). I've started to create a custom theme, but I've noticed that the theming abilities are rather limited, particularly for Python.

I want to be able to assign different colors to things like

  • 'Single quoted strings' vs. "Double quoted strings"
  • Highlighting of doctests. So if I have something like

    def myfunc(x):
        """
        This is the docstring.
    
        >>> # These are the examples
        >>> print myfunc(x)
        1
        """
        return 1
    

    I want the This is the docstring to be colored like the string, but I want the print myfunc() to be colored like Python code (or at least differently). For example, in vim, using whatever plugin I have installed, the doctests are colored brown but the strings are colored blue.

  • Highlighting docstrings differently than regular strings.

  • Coloring of string formatting characters (like "%s" or "%(newstyle_formatting)s").
  • Better treatment of r, u, or b preceding ' or ".

Any suggestions for one or all of these?

I might add more things here if I think of them.

like image 803
asmeurer Avatar asked Jan 17 '12 21:01

asmeurer


1 Answers

Take a look at the description of the variable font-lock-keywords. It is possible to add rules based on regexp:s, but also based on functions so you can write code to decide what should be colored and in which color.

Also, you can use font-lock-add-keywords to add fontification rules to major modes.

like image 66
Lindydancer Avatar answered Oct 17 '22 22:10

Lindydancer