The Lone C++ Coder's Blog

The Lone C++ Coder's Blog

The continued diary of an experienced C++ programmer. Thoughts on C++ and other languages I play with, Emacs, functional, non functional and sometimes non-functioning programming.

Timo Geusch

3-Minute Read

I have a few more loose ends to tidy up before switching to the static version of the blog. One of the important tasks was to make sure I had a spell checker available. Back in the dim and distant past I had set up flyspell-mode with hunspell, but I wanted to check if there was something better available these days. Enter enchant, which acts as a front end to multiple, different spell checkers. I like that Emacs has included support for enchant since version 26, plus one of the backends enchant supports is AppleSpell. In other words, when running on macOS, flyspell can make use of the OS’s built in spell checker and dictionaries.

Instructions on how to actually set up enchant on macOS are a bit thin on the ground, so I decided that I’ll put together a quick write up.

Installation itself is simple if you are using homebrew. And if you’re not, why not? With homebrew, simple brew install enchant gets you there. Just be aware that the binary called enchant-2, not enchant. The actual setup is very easy. First, I ran enchant-lsmod-2 to get a list of supported backends. The homebrew build of enchant installs aspell automatically, plus I already had hunspell installed. As a result, the output looked like this:

mac-pro:$ enchant-lsmod-2
aspell (Aspell Provider)
AppleSpell (AppleSpell Provider)

Interestingly enough it didn’t list hunspell, even though I have it installed on this system. The next step was to create the configuration file for enchant. The man page has more info on how to customize it. I just stuck with a single default rule and put the config file into ~/.config/enchant. As per the man page, it has to be named enchant.ordering. My very simple file looks like this:

*:AppleSpell,hunspell,aspell

Enchant will pick the first available spell checker out of the list, which is AppleSpell if you’re running on macOS. At this point, I had a working enchant setup, but still needed to set it up for use with flyspell-mode.

To do that, I updated my .emacs file to ensure it would check if enchant was available. I also wanted to make sure that I provided a few fallback options in the order of priority that I wanted. As a result I ended up with this piece of code:

(cond ((executable-find "enchant-2")  (setq-default ispell-program-name "enchant-2"))
      ((executable-find "hunspell")   (progn (setq-default ispell-program-name "hunspell") (setq ispell-really-hunspell t)))
      ((executable-find "aspell")     (setq-default ispell-program-name "aspell")))

The code above checks for the presence of the three spell check backends I want to use, in descending order of priority. After re-evaluating my .emacs buffer, I checked that Emacs picked the right spell checker by executing (describe-variable 'ispell-program-name). This resulted in the following output:

ispell-program-name is a variable defined in ispell.el.
Its value is "enchant-2"
Original value was 
"/usr/local/bin/aspell"

  You can customize this variable.

Documentation:
Program invoked by M-$ and M-x ispell-region commands.

Once I was reassured that enchant was working, I wrote this blog post. In Emacs, of course, with enchant handling the spell checking.

Recent Posts

Categories

About

A developer's journey. Still trying to figure out this software thing after several decades.