[Tex/LaTex] How to make AUCTeX spell check in the language specified by babel and csquotes macros

auctexbabelcsquotesemacsspelling

It is possible to make AUCTeX load the spelling dictionary of the language which babel is loaded with. One further convenience would be for it to appropriately react to changes of language within a document. Such changes can be made with macros of babel and csquotes. So, is it possible to make AUCTeX spell check in the correct language according to the language macros of a document?

Here follows a test document. A solution would make AUCTeX automatically spell check the first paragraph in English and the other paragraphs should be spell checked according to the specified language.

\documentclass{article}

\usepackage[danish,english]{babel}
\usepackage{csquotes}

\begin{document}

In computing, a spell checker (or spell check) is an application
program that flags words in a document that may not be spelled
correctly.

\foreignquote{danish}{I computerterminologi er en stavekontrol en
design feature eller et software program designet til at tjekke
stavningen af ord i et dokument og som normalt kan komme med forslag
til stavningsforslag.}

\hyphenquote{danish}{I computerterminologi er en stavekontrol en
design feature eller et software program designet til at tjekke
stavningen af ord i et dokument og som normalt kan komme med forslag
til stavningsforslag.}

\foreignlanguage{danish}{I computerterminologi er en stavekontrol en
design feature eller et software program designet til at tjekke
stavningen af ord i et dokument og som normalt kan komme med forslag
til stavningsforslag.}

\begin{hyphenrules}{danish} I computerterminologi er en stavekontrol
en design feature eller et software program designet til at tjekke
stavningen af ord i et dokument og som normalt kan komme med forslag
til stavningsforslag.
\end{hyphenrules}

This sentence should be spell checked with an English dictionary.

\end{document}

Best Answer

Assuming you are using flyspell, yes it is possible!

The package flyspell-babel.el should do exactly what you want: read inline language changes for babel, and start a flyspell process for that language.

According to the documentation:

The parsing done by this package has its limits limited, and so it will not work with arbitrary LaTeX code. I hope that these restrictions will not in practice impinge on the typical usage of most people. The first language declaration is usually determined by the final language option passed to the babel \usepackage command, which takes effect after \begin{document}. Thereafter, you can switch the declared language with \selectlanguage statements, otherlanguage environments, and \foreignlanguage commands. You can also define your own language-switching commands, and register these with flyspell-babel.

Therefore, you may have to make your other language change commands, such as \foreignquote, known to flyspell-babel.

This package requires the package flyspell-multi.el (no known link at the moment) or ispell-multi.el to start several flyspell processes at the same time, to avoid slowdowns when scrolling through a file with language changes.

Installation instructions

Put the files flyspell-babel.el and flyspell-multi.el somewhere in the load-path and add the following lines to your .emacs:

(autoload 'flyspell-babel-setup "flyspell-babel")
(add-hook 'latex-mode-hook 'flyspell-babel-setup)

To add support for other commands, add the following lines to your .emacs:

(add-to-list 'flyspell-babel-command-alist ("hyphenquote" "hyphenquote"))
(add-to-list 'flyspell-babel-command-alist ("foreignquote" "foreignquote"))
(add-to-list 'flyspell-babel-environment-alist ("hyphenrules" "hyphenrules"))

All credit goes to Peter Heslin, the author of these two packages.