2

I can't get aspell to build, so I'm trying hunspell. Got hunspell built. Set up .emacs so that emacs can (and does) find the executable, like so:

;;; Spell checking using hunspell
(setq ispell-dictionary-alist
  '((nil "[A-Za-z]" "[^A-Za-z]" "[']" t
     ("-d" "en_US" "-i" "utf-8") nil utf-8)
    ("american"
     "[A-Za-z]" "[^A-Za-z]" "[']" nil
     ("-d" "en_US") nil utf-8)
    ("english"
     "[A-Za-z]" "[^A-Za-z]" "[']" nil
     ("-d" "en_GB") nil utf-8)
    ("british"
     "[A-Za-z]" "[^A-Za-z]" "[']" nil
     ("-d" "en_GB") nil utf-8)
    ("norsk"
     "[A-Za-zÉÆØÅéæøå]" "[^A-Za-zÉÆØÅéæøå]" "[\"]" nil
     ("-d" "nb_NO") "~list" utf-8)))
(eval-after-load "ispell"
  (progn
    (setq ispell-dictionary "english"
          ispell-extra-args '("-a" "-i" "utf-8")
          ispell-silently-savep t)))

(setq ispell-dictionary "en_US")
(setq ispell-program-name "/usr/local/bin/hunspell")

and this in my .bash_profile

export DICTIONARY=en_US
export DICPATH=/Users/myname/Applications/en_US

also tried

export DICTIONARY=en_US
export DICPATH=/Users/gpajer/Applications/

(there's a directory ~/Applications/en_US in which the dictionary files reside)

But ispell-buffer returns something like

Can't open affix or dictionary flies for dictionary named "english".
@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.2)
@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.2)

Is hunspell not finding the dictionary? Is there a special place I should put the dictionary? or how do I tell emacs/hunspell where to look for the dictionary?

garyp
  • 967
  • 7
  • 36
  • I initially had trouble with aspell also, but I eventually got it. Here is a link to my notes regarding what source was used and how to install it on OSX -- I still use OSX 10.6.8 though: http://stackoverflow.com/a/20013202/2112489 – lawlist Aug 20 '14 at 21:59
  • You have two (2) *different* definitions for `ispell-dictionary`. – lawlist Aug 20 '14 at 22:03
  • @lawlist Yes, but that's not the issue. That's a left-over artifact of trying ten million permutations of ideas. – garyp Aug 20 '14 at 22:39
  • @lawlist THanks, but your aspell solution didn't work. See [my earlier question](http://stackoverflow.com/q/25395685/619754) on my aspell problem. Got the same error with your solution. Aspell or hunspell ... I don't care which! I just want a spell checker. – garyp Aug 20 '14 at 22:46
  • How about a pre-built binary for OSX? http://people.ict.usc.edu/~leuski/cocoaspell/ See also the wiki on that build: http://www.emacswiki.org/emacs/CocoAspell – lawlist Aug 20 '14 at 22:56
  • @lawlist I'm trying to do this also. I don't see any notes at the link you provide above though. – incandescentman Jun 17 '15 at 02:31
  • Is there a page with detailed instructions on how to get hunspell working with Emacs on OSX Yosemite? – incandescentman Jun 17 '15 at 02:31
  • 1
    @Peter Salazar -- hmmm . . . I don't have a specific recollection of why I deleted my post -- perhaps someone gave me a demerit without placing a comment and I figured . . . :) I'll undelete it now. – lawlist Jun 17 '15 at 02:35
  • This looks promising. http://stackoverflow.com/questions/3961119/working-setup-for-hunspell-in-emacs – incandescentman Jun 17 '15 at 02:45

2 Answers2

2

Upgrade to Emacs 24.4, either by installing a recent pretest or by building Emacs trunk. Prebuild binaries for pretests and nightly builds of Emacs trunk are available from Emacs for Mac OS X, in the “Pretests” and “Nightlies” sections respectively.

Emacs 24.4 considerably improves support for Hunspell, and is now able to use Hunspell automatically with only little further customization. Notably, Emacs can now discover available Hunspell dictionaries, and fills ispell-dictionary-alist automatically. Essentially, you just need the following to tell Emacs to use hunspell:

(setq ispell-program-name (executable-find "hunspell"))

You need to explicitly install these dictionaries for Hunspell, though, depending on how you installed Hunspell. Normally, you just need to put the corresponding *.aff and *.dic files into ~/Library/Spelling. Obtaining dictionaries is a little more difficult, though. The best way probably is to download the corresponding LibreOffice extensions and extract the *.dic and *.aff files from the OXT files, which are essentially just ZIP files. At least, that's what I do. There may be better sources of dictionaries.

Besides language-specific dictioniaries, you also need to have a “default” dictionary for Emacs. This dictionary needs to be named default, literally. Creating it is easy enough, though. Just create symlinks to the dictionaries of your preferred language:

$ cd ~/Library/Spelling
$ ln -s en_GB.aff default.aff
$ ln -s en_GB.dic default.dic

That's all I needed to get Hunspell up and working on my system.

  • 1
    I've been trying for weeks to get *any* spellchecker working with a new Mac and emacs. I have hunspell, Emacs 24.4.50, and did what you suggest. Results: Starting new Ispell process nil with default dictionary... ispell-check-version: Wrong type argument: stringp, nil – garyp Sep 02 '14 at 16:25
  • Something is wrong with my Emacs. Variables set in .emacs are not being honored. ispell-program-name is nil after startup no matter how I set it in .emacs: your suggestion, setq ispell-program-name, or via the menu-driven customization... always nil. If I set the variable by hand after Emacs has started, all is well, until next time I start Emacs. – garyp Sep 02 '14 at 16:41
  • Argh ... It's not as simple as that. Even when set by hand it sometimes works, and sometimes doesn't. I'll have to dig some more. – garyp Sep 02 '14 at 17:01
  • @garyp That error means that you have no `default` dictionary. Did you add the symbols for `default.{aff,dic}` as explained in the answer? –  Sep 02 '14 at 17:35
  • @garyp If the variable is `nil` even though you set it in your `init.el` as described in this answer, `executable-find` returns `nil`, which indicates that it did not find `hunspell` in `exec-path`. Did you install `hunspell` via Homebrew? If so, is `/usr/local/bin` in your `exec-path`? Do you have a proper `$PATH` in your Emacs? –  Sep 02 '14 at 17:37
  • Yes, I made those links. How does hunspell know where to look for the dictionaries? /usr/local/bin is not in my exec-path, but what I neglected to mention is that I used the whole path when setting the variable: (setq ispell-program-name "/usr/local/bin/hunspell"). I thought I opened a new question on setting variables ... I don't see it. – garyp Sep 03 '14 at 01:16
  • @garyp there must be something else interfering. I actually use customize groups almost exclusively and my `ispell-program-name` is therefore set to string literal. In my case, like yours, it is `"/usr/local/bin/hunspell"`. But apart from customize groups and absolute path everything works for me. I also have starter-kit installed, but it shouldn’t matter in this case. – F4-Z4 Apr 19 '15 at 11:33
  • Thanks for the comment regarding default dictionaries, I just moved laptops and was receiving the `Can't open affix or dictionary files for dictionary named "default".` error endlessly and could find nothing that mentioned this step in any offical documentation. – ChrisChinchilla Jul 13 '17 at 10:25
  • Download dictionary https://wiki.openoffice.org/wiki/Dictionaries – WierdGreenCat Feb 12 '22 at 11:29
0

Here is my working Emacs 24.4 / OS X 10.9 Hunspell related setup, hope it helps.

First, get the path working:

(cond
 ((eq system-type 'darwin)
  ;; Sane path (OSX doesn't've much on the path when launching not from shell:
  (setq path "/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/opt/local/bin")
  (setenv "PATH" path)
  ;; Emacs 24.4 seems to need this...:
  (mapc (lambda (p) (push p exec-path)) '("/usr/local/bin" "/usr/texbin" "/opt/local/bin"))))

Then, point to Hunspell and set it up:

(setq-default ispell-program-name (executable-find "hunspell"))
(setq ispell-dictionary "american"
  ispell-extra-args '() ;; TeX mode "-t"
  ispell-silently-savep t
  )

(add-hook 'ispell-initialize-spellchecker-hook
          (lambda ()
            (setq ispell-base-dicts-override-alist
                  '((nil ; default
                     "[A-Za-z]" "[^A-Za-z]" "[']" t
                     ("-d" "en_US" "-i" "utf-8") nil utf-8)
                    ("american" ; Yankee English
                     "[A-Za-z]" "[^A-Za-z]" "[']" t
                     ("-d" "en_US" "-i" "utf-8") nil utf-8)
                    ("british" ; British English
                     "[A-Za-z]" "[^A-Za-z]" "[']" t
                     ("-d" "en_GB" "-i" "utf-8") nil utf-8)))))
Svarog
  • 285
  • 1
  • 7