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?

2 Answers 2

11

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.

0
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)
2
  • Looks like we were typing at the same time!
    – Dan
    Commented Mar 7, 2017 at 19:16
  • I take my hat off (and upvote you) as you were more educational 👍 Commented Mar 7, 2017 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.