How to distinguish Python strings and docstrings in an emacs buffer?
Asked Answered
S

2

2

If I have just

(set-face-foreground 'font-lock-comment-face "red")
(set-face-foreground 'font-lock-string-face "green")

in my .emacs, emacs uses the same font-lock for Python strings and for Python docstrings.

Python-comments-strings-and-docstrings

What should I add to my .emacs so that comments, strings, and docstrings are distinguished?

This answer suggests that it is possible to do so.

Sinuation answered 5/12, 2014 at 13:51 Comment(0)
C
2

Built-in python.el provides font-lock-doc-face via python-font-lock-syntactic-face-function.

python-mode.el uses font-lock-doc-face, given py-use-font-lock-doc-face-p is t. You can customize that variable.

Chorea answered 6/12, 2014 at 16:39 Comment(4)
I'm on Emacs 24.4 and I don't see where you're at. M-x customize-variable followed by py.. suggests that there are many variables beginning with python- and none beginning with py-. Even if you don't edit your .emacs directly, could you grep py and python out of your .emacs and tell us what you have there that makes docstring distinct from string?Sinuation
@Sinuation Needs download from launchpad.net/python-mode/+download. BTW commands of both modes should work in parallel - beside of menu and key-map, which will be taken by the last loaded.Meaghanmeagher
Just to clarify, from the site - "Please note that this is different than the python.el that comes by default in FSF Emacs. The origins of python-mode.el predates python.el by many years. Any known command should be available - with prefix `py-' here."Kohinoor
As of Emacs 25.1, Emacs' built-in python.el also uses font-lock-doc-face for docstrings.Audieaudience
C
0

The face used for doc strings should be font-lock-doc-face, so just customize it to look different.

Cindicindie answered 5/12, 2014 at 14:2 Comment(7)
Adding (set-face-foreground 'font-lock-doc-face "black") to the two lines above and restarting emacs then loading the Python buffer does not help. The docstring continues to be displayed in font-lock-string-face.Sinuation
If you put your cursor on the doc string and type M-x describe-face then it shows what face is used at that point.Dallman
For me it says string face, so it seems the same face is used for docstrings. Emacs 24.1Dallman
Oh, indeed, the """...""" syntax is not specific to docstrings, it can be used for any kind of strings, so Emacs can't just highlight it as a docstring. Maybe we should improve the highlighting to try and be more clever (look around to try and recognize those cases where it's indeed a docstring).Cindicindie
I have a patch lying around for distinguishing between normal strings and docstrings, which has already been integrated in the alternative python-mode.el.Etherify
@ryuslash: Cool, I just installed it into Emacs's master branch. Thanks.Cindicindie
@Stefan: awesome, thanks! I was still gathering up enough courage to send it to the mailing list.Etherify

© 2022 - 2024 — McMap. All rights reserved.