# flyspell, emacs and aspell seem to diverge

I'm using the following configuration for aspell in my .emacs file

(setq ispell-program-name "/opt/local/bin/aspell")
(setq ispell-list-command "list")

and my ~/.aspell.conf contains the following line

trying this latex file:

\documentclass{article}
\begin{document}
\bibliographystyle{plainnat}
\end{document}

emacs flyspell mode shows me plainnat as an error.

When I invoke aspell from the command line with the following command

it shows no errors. Flyspell does not seem to use aspell or the aspell config. How can I change this in such a manner that I only have to maintain one config for my spell checking needs and not have an extra config for flyspell?

-
When I try your test case aspell --dont-tex-check-comments does show "plainnat" as a spelling error. This seems correct seeing that plainnat is not a TeX comment. To ignore plainnat properly the spell checker would need to be able to differentiate between e.g. \bibliographystyle and \emph and ignore the content of the former but not the latter. If it matters I am using International Ispell Version 3.1.20 (but really Aspell 0.60.7-20110707). –  N.N. Jun 28 '12 at 14:24
To clarify this a little more: aspell is able to ignore tex commands. This is what the "add-tex-command bibliographystyle p" line does. This line excludes the first parameter of bibliographystyle from the spellcheck. Which is does when I invoke aspell from the commandline but not during a flyspell run. –  Kungi Jul 3 '12 at 11:42

Flyspell uses a mode-specific function called flyspell-generic-check-word-predicate to identify areas to ignore when highlighting spelling errors. This is slightly more involved than normal customizations.

As I understand it, there are three steps. First, set the flyspell-mode-predicate property of LaTeX-mode to the name of your custom function. Second, define a function that will return t when point is on a word you want to ignore. Third, add a hook to LaTeX-mode to define flyspell-generic-check-word-predicate to use this new function whenever you enter LaTeX mode.

(The order doesn't really matter, so long as all three steps are done before you open a latex file.)

If you don't know elisp, that probably doesn't make much sense to you. Adding the following to your .emacs file and restarting Emacs should solve the problem. If you want to add additional regions to ignore, the part to play with is the (save-excursion ... ) form.

(put 'LaTeX-mode 'flyspell-mode-predicate 'auctex-mode-flyspell-verify)
(defun auctex-mode-flyspell-verify ()
"Function used for `flyspell-generic-check-word-predicate' in auctex mode."
(save-excursion
(forward-word -2)
(not (looking-at "bibliographystyle{"))))