20

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.

Divinenephron
  • 519
  • 3
  • 11

2 Answers2

21

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"))
Daanturo
  • 180
  • 8
Divinenephron
  • 519
  • 3
  • 11
  • 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 https://github.com/emacs-mirror/emacs/blob/master/lisp/textmodes/ispell.el and byte-compile it again to make it work. – xji May 08 '16 at 07:06
  • I'm using Emacs 25.0. I added a note about it working only on Emacs >= 25.0. – Divinenephron May 12 '16 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. – xji Jun 11 '16 at 09:17
  • 1
    I realized it was my `ispell.el` in `/usr/share/emacs/site-lisp/` shadowing the newer `ispell.el` in Emacs25... See http://unix.stackexchange.com/questions/28483/whats-the-difference-between-usr-share-emacs-site-lisp-directory-and-usr/289124#289124 for anybody who might be encountering the same issue. – xji Jun 11 '16 at 09: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.

Sean Allred
  • 6,861
  • 16
  • 85
chen bin
  • 4,781
  • 18
  • 36