Installing Spellcheck in Emacs on Windows

1. Overview

Getting emacs on windows to work with spellcheck was weird, especially from a beginner to emacs and as someone who usually relies on spellcheck "just working".

This involved installing the utility itself, ensuring the dictionaries were install, then setting the LANG environment variable in the correct place.

2. Install Hunspell

Tried with Chocolately first, it did install but didn't seem to come with any dictionaries. Then moved on to installing through MSYS2. This was just as simple and providing a binary than when ran with the -D flag, showed the en_US dictionary.

3. Setting the LANG Environment Variable

After installing Hunspell, it kept returning the error Hunspell error (is $LANG unset?): Can't open affix or dictionary files for dictionary named "default".. Which I read as I needed to set a default dictionary file through the utility itself or through emacs. The correct thinking should have been, the $LANG unset? clues me that the variable LANG needs to be set, in order for the default dictionary to know what language I'm using.

As far as I can tell, all that is needed to get Hunspell working only in emacs is to M-x setenv RET LANG RET en_US then running something like flyspell-mode. (Assuming you have set the other variables emacs needs to use hunspell).

In order to do this on windows you would use the systems configuration which I did originally thinking the utility when called would use that variable (it doesn't seem like it). Here is how you do this encase you need the utility at the command line level within windows.

hunspell-install-00.png

hunspell-install-01.png

hunspell-install-02.png

4. Configuring in .emacs

Then to ensure you are setting up emacs to use hunspell we can add some configuration changes to our .emacs file.

;;;;;;;;;;;; spellcheck for windows 
;;;;;;;;;;;;
;; The emacs wiki for Aspsell [[https://www.emacswiki.org/emacs/AspellWindows]] mentioned needing to add the exec path
;; while the manual said to add the full path to the name [[https://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Spell-check.html]]
(setq ispell-program-name "C:/msys64/mingw64/bin/hunspell.exe")

;; Need to turn it on
(require 'ispell)

;; Need to invoke it, M-$ is the default will check the word or region
;; Ideally, I'd have it run for the entire buffer 
;; From the emacs manual M-x ispell-buffer will check and correct spelling in the buffer
;; something like this 
;; (global-set-key (kbd "<f8>") 'ispell-buffer)

;; Also likes like flyspell-mode will highlight all misspelled words in the buffer
;; and flyspell-prog-mode will only do comments and strings

;; need to set a dictionary as default - C:/msys64/mingw64/share/myspell/dicts/en (maybe??)
;; NO need to set the LANG environment variable so that hunspell KNOWS what to pick for default
(setenv "LANG" "en_US")
;; I'm assuming based on the above LANG that these could change the dictionary?
;; (setq ispell-dictionary "C:/msys64/mingw64/share/myspell/dicts/en_US") 
;; (setq ispell-local-dictionary "C:/msys64/mingw64/share/myspell/dicts/en_US") 

Date: 2023-01-08

Author: Russell Brinson

Validate