7

I currently use the following to enable flyspell in prog-mode:

(add-hook 'prog-mode-hook 'flyspell-prog-mode)

This checks spelling automatically in comments and strings. However, I would like to disable spell checking in strings.

How can I enable flyspell for comments but not strings?

Dan
  • 32,584
  • 6
  • 98
  • 168
Ben
  • 587
  • 4
  • 11

2 Answers2

10

Here's a quick trip down the flyspell source code rabbit hole. (Try C-h l flyspell to get to it.)

flyspell-prog-mode uses flyspell-generic-progmode-verify. That function uses text properities to decide whether or not to fire based on whether or not the text property of the previous character is a member of flyspell-prog-text-faces.

Now, the flyspell-prog-text-faces docstring tells me that, by default:

flyspell-prog-text-faces is a variable defined in flyspell.el. Its value is (font-lock-string-face font-lock-comment-face font-lock-doc-face)

Documentation:

Faces corresponding to text in programming-mode buffers.

So we can just remove font-lock-string-face from this list:

(setq flyspell-prog-text-faces
      (delq 'font-lock-string-face
            flyspell-prog-text-faces))

Et voila.

Dan
  • 32,584
  • 6
  • 98
  • 168
1

You can specify what flyspell-prog considers "text to be spelled" by customizing the flyspell-prog-text-faces variable:

;; original value: '(font-lock-string-face font-lock-comment-face font-lock-doc-face)
(setq flyspell-prog-text-faces '(font-lock-comment-face font-lock-doc-face)
Felipe Lema
  • 737
  • 3
  • 11