22

How can I use multiple dictionaries with the Emacs spell checker? Specifically I want to use a British English dictionary and a medical English dictionary at the same time.

3
  • 1
    Are you using flyspell? Commented Apr 2, 2016 at 15:37
  • 1
    Yes, I'm using flyspell. Commented Apr 2, 2016 at 15:46
  • 2
    The solution can use flyspell but doesn't have to. That way the answers can be relevant to more people. Commented Apr 2, 2016 at 16:05

2 Answers 2

23

Hunspell can spell check with multiple dictionaries, and you can configure this to work with Emacs. This is how I do it on OS X 10.11, with Emacs 25.0. It will not work with older Emacsen.

Install Hunspell

brew install hunspell 

Download Hunspell dictionaries from LibreOffice and OpenMedSpel.

cd ~/Downloads/ curl http://extensions.libreoffice.org/extension-center/english-dictionaries/releases/2016.04.01/dict-en.oxt > dict-en.oxt unzip dict-en.oxt en_GB.aff en_GB.dic curl -L https://addons.mozilla.org/en-US/firefox/downloads/latest/6526/addon-6526-latest.xpi > openmedspel.xpi unzip openmedspel.xpi dictionaries/OpenMedSpel.{aff,dic} mv dictionaries/OpenMedSpel.dic en_US-med.dic mv dictionaries/OpenMedSpel.aff en_US-med.aff 

Put the dictionaries in ~/Library/Spelling/.

mv *.aff *.dic ~/Library/Spelling/ 

Add this to ~/.emacs/init.el:

 (with-eval-after-load "ispell" (setq ispell-program-name "hunspell") (setq ispell-dictionary "en_GB,en_US-med") ;; ispell-set-spellchecker-params has to be called ;; before ispell-hunspell-add-multi-dic will work (ispell-set-spellchecker-params) (ispell-hunspell-add-multi-dic "en_GB,en_US-med")) 
4
  • Note that the function ispell-hunspell-add-multi-dic seems to be not present in older versions of ispell.el, even the one in Emacs 24.5. I had to download the newest file at github.com/emacs-mirror/emacs/blob/master/lisp/textmodes/… and byte-compile it again to make it work. Commented May 8, 2016 at 7:06
  • I'm using Emacs 25.0. I added a note about it working only on Emacs >= 25.0. Commented May 12, 2016 at 13:10
  • I installed Emacs 25. However, strangely I met the following error: Symbol’s function definition is void: ispell-hunspell-add-multi-dic, though I'm sure in /usr/share/emacs/25.1.50/lisp/textmodes/ispell.elc, the function is present. Any idea how it might have happened? Thanks. Commented Jun 11, 2016 at 9:17
  • 1
    I realized it was my ispell.el in /usr/share/emacs/site-lisp/ shadowing the newer ispell.el in Emacs25... See unix.stackexchange.com/questions/28483/… for anybody who might be encountering the same issue. Commented Jun 11, 2016 at 9:25
4

Suppose you already downloaded en_US-med.dic and en_US-med.aff and installed hunspell

Step 1, run hunspell -D in shell, which will tell the directory where hunspell searches for dictionaries, copy en_US-med.dic and en_US-med.aff to that directory.

Step 2, insert below code into ~/.emacs,

 (setq ispell-program-name "hunspell") ;; you could set `ispell-dictionary` instead but `ispell-local-dictionary' has higher priority (setq ispell-local-dictionary "en_US") (setq ispell-local-dictionary-alist '(("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-d" "en_US,en_US-med") nil utf-8))) ;; new variable `ispell-hunspell-dictionary-alist' is defined in Emacs ;; If it's nil, Emacs tries to automatically set up the dictionaries. (when (boundp 'ispell-hunspell-dictionary-alist) (setq ispell-hunspell-dictionary-alist ispell-local-dictionary-alist)) 

We actually pass the option "-d en_US,en_US-med" to the hunspell CLI so it can use TWO dictionary "en_US" and "en_US-med" at the same time.

The "-d" options is documented in hunspell manual (man hunspell in shell)

Here is the quoted text from hunspell manual:

-d en_US,en_geo,en_med,de_DE,de_med en_US and de_DE are base dictionaries, they consist of aff and dic file pairs: en_US.aff, en_US.dic and de_DE.aff, de_DE.dic. En_geo, en_med, de_med are special dictionaries: dictionaries without affix file. Special dictionaries are optional extension of the base dictio‐ naries usually with special (medical, law etc.) terms. There is no naming convention for special dictionaries, only the ".dic" extension: dictionaries without affix file will be an extension of the preceding base dictionary (right order of the parameter list needs for good sug‐ gestions). First item of -d parameter list must be a base dictionary. 

Tested on Emacs 24.3, Debian 7 with the word "fibrochondritis".

Should work at Emacs 23+ on any OS.

Please note on Windows the easiest way to let hunspell know the search path is to setup environment variable DICPATH (it's documented in hunspell manual).

BTW, it's very possible the hunspell executable from Cygwin/MSYS2 only recognises path in UNIX format.

5
  • That method doesn't allow me to use multiple dictionaries. Commented May 14, 2016 at 7:04
  • The code I gave IS multi-dictionary. Commented May 14, 2016 at 12:05
  • I see, thanks for pointing that out – I didn't notice the -d option. Commented May 15, 2016 at 15:22
  • Well this doesn't seem to be working for some reason. When I use the -d option in the command line it works, but in Emacs, helm-flyspell seems to somehow only use the dictionary with the system language. Commented Jun 11, 2016 at 8:16
  • helm-flyspell might use aspell. Commented Jan 16, 2017 at 12:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.