flyspell: allow enabling either aspell or hunspell

* Previously, a user could not choose which spell checker to use.
  Instead, the first one found was used.
* This PR adds the flags `+aspell` and `+hunspell` which allow a user to
  choose which spellchcker to use (even if both are installed).
* Report an error if spell checking is enabled but no spell checker
  is found.
This commit is contained in:
UndeadKernel 2019-03-18 15:52:32 +01:00
parent 5e3c8c5f92
commit 76f98043f1

View file

@ -14,34 +14,45 @@ Since spellchecking can be slow in some buffers, this can be disabled with:
(after! ispell (after! ispell
(setq-default ispell-dictionary "english") (setq-default ispell-dictionary "english")
(cond ((executable-find "aspell") (let ((no-flags (and (not (featurep! +aspell)) (not (featurep! +hunspell)))))
(setq ispell-program-name "aspell" ;; Enable either aspell or hunspell.
ispell-extra-args '("--sug-mode=ultra" "--run-together")) ;; If no module flags are given, enable either aspell or hunspell if their
;; ... binary is found.
;; If one of the flags `+aspell' or `+hunspell' is given, try to only
;; ... enable that spell checker and not the other.
(cond
((and (or no-flags (featurep! +aspell))
(executable-find "aspell"))
(setq ispell-program-name "aspell"
ispell-extra-args '("--sug-mode=ultra" "--run-together"))
(setq-hook! 'text-mode-hook (setq-hook! 'text-mode-hook
ispell-extra-args (remove "--run-together" ispell-extra-args)) ispell-extra-args (remove "--run-together" ispell-extra-args))
(defun +flyspell*setup-ispell-extra-args (orig-fun &rest args) (defun +flyspell*setup-ispell-extra-args (orig-fun &rest args)
(let ((ispell-extra-args (remove "--run-together" ispell-extra-args))) (let ((ispell-extra-args (remove "--run-together" ispell-extra-args)))
(ispell-kill-ispell t) (ispell-kill-ispell t)
(apply orig-fun args) (apply orig-fun args)
(ispell-kill-ispell t))) (ispell-kill-ispell t)))
(advice-add #'ispell-word :around #'+flyspell*setup-ispell-extra-args) (advice-add #'ispell-word :around #'+flyspell*setup-ispell-extra-args)
(advice-add #'flyspell-auto-correct-word :around #'+flyspell*setup-ispell-extra-args)) (advice-add #'flyspell-auto-correct-word :around #'+flyspell*setup-ispell-extra-args))
((executable-find "hunspell") ((and (or no-flags (featurep! +hunspell))
(setq ispell-program-name "hunspell" (executable-find "hunspell"))
;; Don't use `ispell-cmd-args', it isn't respected with hunspell. (setq ispell-program-name "hunspell"
;; Hack ispell-local-dictionary-alist instead. ;; Don't use `ispell-cmd-args', it isn't respected with hunspell.
ispell-dictionary-alist ;; Hack ispell-local-dictionary-alist instead.
`((,ispell-local-dictionary ispell-dictionary-alist
"[[:alpha:]]" `((,ispell-local-dictionary
"[^[:alpha:]]" "[[:alpha:]]"
"[']" "[^[:alpha:]]"
nil "[']"
("-d" ,ispell-local-dictionary) nil
nil ("-d" ,ispell-local-dictionary)
utf-8))))) nil
utf-8))))
t (user-error "Spell checker not found. Either install `aspell' of `hunspell'")))
(add-to-list 'ispell-extra-args "--dont-tex-check-comments")) (add-to-list 'ispell-extra-args "--dont-tex-check-comments"))