Setting up Emacs spell checking on OS X

As I mentioned in yesterday’s post, I’m trying to improve my blogging workflow by using org2blog to draft my posts before pushing them to my WordPress blog. When I posted yesterday I had the basic workflow going, could edit posts in Emacs, save them, update drafts and push them to WordPress. The last piece that was missing was getting spell checking to work.

I’ve actually never spent much time thinking about spell checkers until I discovered that OS X doesn’t come with a spell checker that ispell recognises. A little research led me to Joel Kuiper’s blog post on spell checking in Emacs on Mac OS X. I decided to install Hunspell as it seemed to be modern, supported and able to do the job. Plus, it’s available via Homebrew which I’m already using to install other Unix software on my OS X machine. A quick

brew install hunspell

followed by me grabbing the German and English dictionaries from OpenOffice.org and I was in the spelling business. Just make sure that you unzip the .oxt files containing the dictionaries, copy/move the .dic and .aff files into either ~/Library/Spelling or /Library/Spelling (I prefer the first one) and run hunspell -D to check that it’s picking up the files. Oh, and don’t forget to set up soft links from default.dic and default.aff to your default language’s dictionary files.

Now I should be able run M-x flyspell-mode and we’re in business, correct? Only we weren’t, because my Emacs setup couldn’t find hunspell. Oops. Turns out I had neglected to add the Homebrew installation directory to the exec-path, with predictable results. The following code in my .emacs fixes that:

;; On OS X/Darwin, make sure we add the path to the homebrew installs
(when (string-equal system-type "darwin")
  (setq exec-path (append exec-path '("/usr/local/bin"))))

With the above in place and Emacs being able to find the hunspell executable, it was time to add the following code to my .emacs to ensure that ispell and flyspell use hunspell if it’s available:

(when (executable-find "hunspell")
  (setq-default ispell-program-name "hunspell")
  (setq ispell-really-hunspell t))

Now flyspell-mode is happily running using Hunspell at it’s time to crack open an adult beverage.