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.

Community
  • 1
  • 1
Calaf
  • 10,113
  • 15
  • 57
  • 120

2 Answers2

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.

Andreas Röhler
  • 4,804
  • 14
  • 18
  • 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? – Calaf Dec 07 '14 at 02:40
  • @Calaf Needs download from https://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. – Andreas Röhler Dec 07 '14 at 08:50
  • 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." – Brian Burns Dec 07 '14 at 15:14
  • As of Emacs 25.1, Emacs' built-in `python.el` also uses `font-lock-doc-face` for docstrings. – dfan Oct 07 '16 at 17:49
0

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

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • 1
    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. – Calaf Dec 05 '14 at 14:09
  • 1
    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. – Tom Dec 05 '14 at 17:44
  • 1
    For me it says string face, so it seems the same face is used for docstrings. Emacs 24.1 – Tom Dec 05 '14 at 17:46
  • 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). – Stefan Dec 05 '14 at 19:13
  • I have a [patch](https://github.com/fgallina/python.el/pull/110) lying around for distinguishing between normal strings and docstrings, which has already been integrated in the alternative python-mode.el. – ryuslash Dec 07 '14 at 10:16
  • 1
    @ryuslash: Cool, I just installed it into Emacs's master branch. Thanks. – Stefan Dec 07 '14 at 16:25
  • @Stefan: awesome, thanks! I was still gathering up enough courage to send it to the mailing list. – ryuslash Dec 07 '14 at 19:39