complete-computing-environment/flyspell_mode.org

2.7 KiB

Spell-checking with Flyspell Mode

I use flyspell-correct for spell-checking my buffers.

(use-package flyspell-correct
  :after flyspell
  :bind
  (:map flyspell-mode-map
        ("C-;" . flyspell-correct-wrapper))
  (:map evil-normal-state-map
        ("z=" . flyspell-correct-wrapper)))

(use-package flyspell
  :ensure nil
  :init
  (add-hook 'text-mode-hook 'flyspell-mode)
  :config
  (setq ispell-program-name (executable-find "hunspell")))
{config, pkgs,...}:
{
  home.packages = [
    pkgs.hunspell
    pkgs.hunspellDicts.en-us-large
  ];
}

I have cce/append-hunspell-current modified from EmacsWiki which I can call to add the current word at point to my personal dictionary.

(eval-when-compile (require 'cl))

(defun cce/append-hunspell-word (new-word)
  (let ((file-name (substitute-in-file-name "$HOME/.hunspell_en_US"))
        (read-words (lambda (file-name)
                      (let ((all-lines (with-temp-buffer
                                         (insert-file-contents file-name)
                                         (split-string (buffer-string) "\n" t))))
                        (if (null all-lines)
                            ""
                          (split-string (mapconcat 'identity all-lines "\n")
                                        nil 
                                        t))))))
    (when (file-readable-p file-name)
      (let* ((cur-words (apply read-words (list file-name)))
             (all-words (cons new-word cur-words))
             (words (delq nil (remove-duplicates all-words :test 'string=))))
        (with-temp-file file-name     
          (insert (mapconcat 'identity (sort words #'string<) "\n")))))
    (unless (file-readable-p file-name)
      (with-temp-file file-name
        (insert new-word "\n")))) 
  (ispell-kill-ispell t) ; restart ispell
  (flyspell-mode-off)
  (flyspell-mode-on))

(defun cce/append-hunspell-current ()
  "Add current word to aspell dictionary"
  (interactive)
  (cce/append-hunspell-word (thing-at-point 'word t)))

(evil-define-key 'normal 'flyspell-mode-map (kbd "zi") #'cce/append-hunspell-current)