How do I change Emacs's font face for Python docstrings?
Asked Answered
T

2

8

I'm just starting to learn Python and use Emacs as my editor. Currently, Emacs uses the same color for normal strings (single quotes) and docstrings (triple quotes). I want docstrings to be a different color, so I used the 'Options->Customize Emacs' menu option to change 'font-lock-doc-face' to a new color and saved the changes. However, Emacs continues to keep docstrings the same color as normal strings. Changing the color of normal strings applies the change to docstrings as well.

It would seem that Emacs is treating docstrings and normal strings as the same. Is there a way to get Emacs to properly find and color Python docstrings separately from normal strings?

Edit: I'm using Emacs 23.1.1 (Kubuntu 10.10 package) with the default Python mode settings. I also use the color-theme package with the midnight theme.

Teador answered 26/2, 2011 at 22:14 Comment(3)
I don't know the answer, but just to clarify: docstrings and triple quoted strings are separate things (although triple quotes are often used for docstrings). A string immediately after a def or class line is a docstring. Triple quotes let you spread a string over several lines easily.Huihuie
That's good to know. I am new to Python and so thought that triple-quoted strings were implicitly docstrings. It would seem, then, that the problem is that Emacs cannot differentiate between a docstring as you describe and any other triple-quoted string in a Python file.Teador
See also #27317896Bandurria
D
2

Interesting. I was going to say that due to the way the emacs syntax table works, emacs thinks """ and ''' represent an empty string folowed by the start of a new string.

You can easily verify this in your copy of emacs by pasting the following code into a python buffer:

class MrsRobinson(object):
    '''
    What's that you say?
    '''
    pass

In emacs 23.1.1 [update: and apparently on stackoveflow!] this completely breaks the syntax highlighting for the rest of the file.

I always use """ specifically to avoid apostrophe problems in docstrings, so I didn't notice until right this minute that in emacs 23.2.1, this is somehow finally fixed...

(Yep, there's a new function called python-quote-syntax in python.el)

So: in your version of emacs, this is impossible because the strings aren't parsed correctly. If you upgrade to the latest emacs, you may be able to make it happen by modifying that function in python.el to treat them differently.

Dorettadorette answered 27/2, 2011 at 3:53 Comment(3)
I copied your little example into Emacs and it actually works correctly. That is, the entire doc string is highlighted as a string and 'pass' is properly highlighted as a keyword. I don't know, though, if there's a way to get Emacs to differentiate between a docstring and any other triple-quoted (''' or """) string that appears in the code. I imagine there must be some way to feed Emacs some regular expression telling it what docstrings look like, do you know how this can be done?Teador
Oh, I may be running the "other" python mode on the older emacs. Anyway, you still have to modify that same function. It does the syntax highlighting for all python strings. You just need to determine whether the string (regardless of how many quotes it uses) appeared immediately at the top of a file, class, or function (ignoring comments and whitespace). Maybe you could modify the lexer to set a flag when it hits a class or def, set another flag when it hits the : , unset both when it hits anything but a comment, and override the face if both are set and what it hit was a string.Dorettadorette
I took a look at the python.el file and that is indeed where I would need to change things to do what I want. It's certainly over my head at the moment, but I'll have to play around with at at some point. Thanks for getting me wwaayyy closer to figuring this out.Teador
M
1

I was able to accomplish this:

By doing M-x customize-face and enter: font-lock-doc-face.

The reason they are both the same color is because font-lock-doc-face inherits from font-lock-string-face:

I updated font-lock-string-face to font-lock-comment-face (as I prefer my doc-strings to have the same color as my comments). You can either do the same or select "Show all attributes" to choose your own

Murvyn answered 24/4, 2021 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.