;; NO spell check for embedded snippets
(defadvice org-mode-flyspell-verify (after org-mode-flyspell-verify-hack activate)
(let* ((rlt ad-return-value)
(begin-regexp "^[ \t]*#\\+begin_\\(src\\|html\\|latex\\|example\\|quote\\)")
(end-regexp "^[ \t]*#\\+end_\\(src\\|html\\|latex\\|example\\|quote\\)")
(case-fold-search t)
b e)
(when ad-return-value
(save-excursion
(setq b (re-search-backward begin-regexp nil t))
(if b (setq e (re-search-forward end-regexp nil t))))
(if (and b e (< (point) e)) (setq rlt nil)))
(setq ad-return-value rlt)))
Please note ispell-skip-region-alist is NOT used by flyspell.
Test on Emacs24.3, 24.4, 24.5, 25.1 with builtin org-mode
Most users turn on flyspell by default. So above code is enough.
But I don't enable flyspell-mode in org file. I prefer manually M-x flyspell-buffer
again and again. So I need (flyspell-mode 1) (flyspell-mode -1)
in org-mode-hook. Yes, turn on then turn off flyspell-mode. The purpose is to make sure the org-mode's default predicate is loaded.
Please note flyspell-incorrect-hook
is not the good place for per major mode configuration because it's shared by all major modes. So the heavy weight configuration code in one major mode could impact other major modes if the per major mode code is placed in flyspell-incorrect-hook
.
flyspell
has already provided flyspell-generic-check-word-predicate
which is "Function providing per-mode customization over which words are flyspelled". It's buffer local variable so it safe to use out of box.
Screenshot of its definition from flyspell.el
:
Besides, the predicate is more flexible than the hook because the predicate is tested at first. So if the predicate claims a word is not a typo, the hook will not be executed. Here is the screenshot of flyspell-word
definition,
Actually, the default value of flyspell-generic-check-word-predicate
in org-mode
is org-mode-flyspell-verify
.
The reason I advice org-mode-flyspell-verify
is I'm lazy. I could change the value of the predicate to my own function which calls org-mode-flyspell-verify
instead,
(defun org-mode-hook-setup ()
(setq flyspell-generic-check-word-predicate
(lambda ()
(let* ((rlt (org-mode-flyspell-verify)))
;; do whatever on rlt below this line
(message "rlt=%s" rlt)
rlt))))
(add-hook 'org-mode-hook 'org-mode-hook-setup)
flyspell-generic-check-word-predicate
, if that hasn't already been invented. Here is a related thread for flyspell using theflyspell-generic-check-word-predicate
: superuser.com/a/345461/206164 ; and, here is a semi-related thread for ispell usingispell-skip-region-alist
, but not relating to flyspell: emacs.stackexchange.com/a/2103/2287ox
.