Working setup for hunspell in Emacs
Asked Answered
C

3

12

Does anyone have a working setup for hunspell and Emacs? Simply setting ispell-program-name to hunspell doesn't work, the output (when using flyspell, for example) looks like below:

-> UTF-8 encoding error. Missing continuation byte in 0. character position: - 9631: word not found

(my files are usually encoded in UTF-8)

I've seen a few different setups, but they've all failed in one way or another. If the encoding works like it should it usually has problems finding the right dictionary.

Anyone with a working solution? It would be nice to be able to switch between two dictionaries (the default should be the swedish dictionary, and the secondary english), but having anything running would be even better.

Colubrine answered 18/10, 2010 at 16:20 Comment(0)
C
15

If you are getting that UTF-8 encoding error, then it means that the hunspell process is getting run with an argument specifying some other encoding. When I check my process list, for example, I see this child process to Emacs once it has started up:

/usr/bin/hunspell -a  -B -i iso-8859-1

The ispell-get-coding-system function is what decides which encoding to use, which it does by examining the big ispell-dictionary-alist variable that seems to list every language known to Emacs. The function normally grabs the last symbol off of the entry that matches the language you want to check. For some reason that I did not bother to figure out, this list has iso-8859-1 for English — instead of, you know, paying attention to the encoding in your actual buffer. I know, it seems to make no sense. But we carry on.

You would think that you could override this by setting your own value for the variable ispell-dictionary-alist and use utf-8 as the last of the eight parameters:

;; I could never get Emacs to pay attention to this
(setq ispell-dictionary-alist
  '((nil "[A-Za-z]" "[^A-Za-z]" "[']" t ("-d" "en_US") nil utf-8)))

But I could never get this setting to actually work, whether or not I did a (load-library "ispell") first in my .emacs, or whether I did it inside of one of those:

;; Did not work for me either.
(eval-after-load "ispell" '(progn ...))

Either way, if I started up a fresh Emacs and entered *scratch* and typed ispell-dictionary-alist and pressed Control-J, then the huge original list that ispell creates would come up. Every time.

So I decided to do an end-run around the entire problem of this huge list and simply rewrite the ispell-get-coding-system function to always return utf-8. Sure, this will bite me the next time that I open a file that is really in iso-8859-1, but I never do that anyway, right?

To implement this successfully in my .emacs file (well, ~/.emacs.d/init.el but that takes so much typing for a Stack Overflow answer) required this code:

;; It works!  It works!  After two hours of slogging, it works!
(if (file-exists-p "/usr/bin/hunspell")
    (progn
      (setq ispell-program-name "hunspell")
      (eval-after-load "ispell"
        '(progn (defun ispell-get-coding-system () 'utf-8)))))

I now have hunspell up and working like a champ! Unfortunately the whole reason I went through getting it working was in the hopes that its dictionary was vastly larger than aspell's but I see that it's highlighting some of the same words. Oh well, I'll try another approach. I basically want a spell checker that can be loaded up with the /usr/share/dict/american-english-huge dictionary that is available on Ubuntu, but aspell died in many ways when I tried to expand its horizons. Maybe I will be luckier with hunspell — we will see.

Cherin answered 6/11, 2010 at 2:30 Comment(4)
T-H-A-N-K-S. That is awesome. For using a language like swedish, hunspell is way better than i/aspell, and even if I have to watch out for files not encoded in utf8, this is still awesome. Thanks!Colubrine
In trying the approach you describe but I get an error. First I did sudo apt-get install hunspell then I inserted the code block in my .emacs (the last one with the if construct). When I try to spell check I get the following in *Messages*: Starting new Ispell process [english] ... Error enabling Flyspell mode: (Can't open affix or dictionary files for dictionary named "english". @(#) International Ispell Version 3.2.06 (but really Hunspell 1.2.14)). What is the problem?Urogenital
Helped me a lot, thanks! Although it took 2 hours to find this answer :)Lynsey
Simply setting ispell-dictionary-alist as one that failed for you did the job for me. Make sure ispell-dictionary is set to nil.Ethic
C
0

http://www.mail-archive.com/[email protected]/msg01709.html

Coracoid answered 19/10, 2010 at 6:30 Comment(1)
Tried it already. Did it again, hoping I'd missed something. This is my setup, I can change the dictionary (rw-ispell-change-dictionary) and it seems to find my dictionaries, but this is what I get when I try to use it :-(Colubrine
N
0

From https://passingcuriosity.com/2017/emacs-hunspell-and-dictionaries/

Add

;; Set $DICPATH to "$HOME/Library/Spelling" for hunspell.
(setenv
  "DICPATH"
  "/path/to/hunspell/dictionary")
;; Tell ispell-mode to use hunspell.
(setq
  ispell-program-name
  "hunspell")

into your ~/.emacs.

My dictionary files were at /usr/share/hunspell.

Nay answered 23/2, 2017 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.