Working setup for hunspell in Emacs

前端 未结 3 1635
[愿得一人]
[愿得一人] 2021-02-08 04:46

Does anyone have a working setup for hunspell and Emacs? Simply setting ispell-program-name to hunspell doesn\'t work, the o

3条回答
  •  我在风中等你
    2021-02-08 05:34

    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.

提交回复
热议问题