How can I make AUCTeX spell check in the language specified by babel and csquotes macros?

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}

• I do not use Emacs nor I type these days in any other language than English but I think this a very interesting question. Personally, I use ispell and a custom key binding to call it from nvi to do spell checking interactively (I like to confirm changes). Basically what you are asking is much more sophisticated script which will terminate ispell when Danish is encountered change default dictionary to Danish and continue spelling. I will ask my CS students to try to come up with something. – Predrag Punosevac Jan 14 '12 at 3:04
• @PredragPunosevac I would also like for it to work with fly spell because that what I currently use for spell checking. Would appreciate any lead on this issue. Make answering my question your students' home work! – N.N. Jan 14 '12 at 17:00
• I am not sure about the homework but a small project yes. Emacs is written in Lisp so they will have to hack Lisp code not shell script as I described for nvi but I think it should be duable. – Predrag Punosevac Jan 14 '12 at 17:09
• I think this is rather difficult. I could imagine to advise ispell-region accordingly, but, probably, the only way to go is to use a spell checker backend providing this – certainly useful – feature. – mhp Mar 28 '12 at 19:09
• @mhp Why so pessimistic? Given Emacs extensibility I think it is very possible. It is just a matter of coming up with an appropriate logic and implementing it. One way would be for flyspell to start spell checking in some default language then change dictionary every time there is some babel expression changing language. This seems doable as long as you can come up with the right lisp code to recognize the babel expressions, i.e. some regular expressions. – N.N. Mar 28 '12 at 19:25

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")


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

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

• I tried your installation instructions and it works for \foreignlanguage in my example but neither of the other macros. Perhaps you could include instructions on how to cover them as well. – N.N. Nov 10 '12 at 16:57