From 44ab9c9e3cc677afe41513653665bce985539362 Mon Sep 17 00:00:00 2001 From: syl20bnr Date: Sat, 23 Nov 2013 23:04:26 -0500 Subject: [PATCH] Add spell check support to markdown files and text files --- init-package/init-auto-dictionary.el | 2 +- init-package/init-flyspell.el | 6 ++++ my-funcs.el | 54 ++++++++++++++++++++++++++++ my-keybindings.el | 4 +++ packages.el | 1 + 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 init-package/init-flyspell.el diff --git a/init-package/init-auto-dictionary.el b/init-package/init-auto-dictionary.el index 93132e688..07b8a5e16 100644 --- a/init-package/init-auto-dictionary.el +++ b/init-package/init-auto-dictionary.el @@ -1,4 +1,4 @@ (use-package auto-dictionary :defer t - :config + :init (add-hook 'flyspell-mode-hook '(lambda () (auto-dictionary-mode 1)))) diff --git a/init-package/init-flyspell.el b/init-package/init-flyspell.el new file mode 100644 index 000000000..2aa5398ad --- /dev/null +++ b/init-package/init-flyspell.el @@ -0,0 +1,6 @@ +(use-package flyspell + :defer t + :init + (progn + (add-hook 'markdown-mode-hook '(lambda () (flyspell-mode 1))) + (add-hook 'text-mode-hook '(lambda () (flyspell-mode 1))))) diff --git a/my-funcs.el b/my-funcs.el index 97d219513..9c0d05acf 100644 --- a/my-funcs.el +++ b/my-funcs.el @@ -220,4 +220,58 @@ argument takes the kindows rotate backwards." (delete-windows-on buf) (message "compilation ok.")))) +;; from https://gist.github.com/cofi/3013327 +(defun cofi/helm-flyspell-correct () + "Use helm for flyspell correction. +Adapted from `flyspell-correct-word-before-point'." + (interactive) + ;; use the correct dictionary + (flyspell-accept-buffer-local-defs) + (let ((cursor-location (point)) + (word (flyspell-get-word)) + (opoint (point))) + (if (consp word) + (let ((start (car (cdr word))) + (end (car (cdr (cdr word)))) + (word (car word)) + poss ispell-filter) + ;; now check spelling of word. + (ispell-send-string "%\n") ;put in verbose mode + (ispell-send-string (concat "^" word "\n")) + ;; wait until ispell has processed word + (while (progn + (accept-process-output ispell-process) + (not (string= "" (car ispell-filter))))) + ;; Remove leading empty element + (setq ispell-filter (cdr ispell-filter)) + ;; ispell process should return something after word is sent. + ;; Tag word as valid (i.e., skip) otherwise + (or ispell-filter + (setq ispell-filter '(*))) + (if (consp ispell-filter) + (setq poss (ispell-parse-output (car ispell-filter)))) + (cond + ((or (eq poss t) (stringp poss)) + ;; don't correct word + t) + ((null poss) + ;; ispell error + (error "Ispell: error in Ispell process")) + (t + ;; The word is incorrect, we have to propose a replacement. + (flyspell-do-correct (helm-comp-read "Correction: " + (append + (third poss) + '(("Save word" . save) + ("Accept (session)" . session) + ("Accept (buffer)" . buffer))) + :name (format "%s [%s]" word (or ispell-local-dictionary + ispell-dictionary + "Default")) + :must-match t + :alistp t) + + poss word cursor-location start end opoint))) + (ispell-pdict-save t))))) + (provide 'my-funcs) diff --git a/my-keybindings.el b/my-keybindings.el index 5d3f85575..be2d0ac13 100644 --- a/my-keybindings.el +++ b/my-keybindings.el @@ -193,6 +193,10 @@ ;; show ----------------------------------------------------------------------- (evil-leader/set-key "sk" 'helm-show-kill-ring) (evil-leader/set-key "sr" 'evil-show-registers) +;; spell check --------------------------------------------------------------- +(evil-leader/set-key "sc" 'cofi/helm-flyspell-correct) +(evil-leader/set-key "sd" 'adict-change-dictionary) +(evil-leader/set-key "sn" 'flyspell-goto-next-error) ;; toggle --------------------------------------------------------------------- (evil-leader/set-key "t8" 'toggle-fill-column-indicator) (evil-leader/set-key "ta" 'auto-complete-mode) diff --git a/packages.el b/packages.el index a9350ef8b..400490215 100644 --- a/packages.el +++ b/packages.el @@ -35,6 +35,7 @@ flx-ido flycheck flycheck-color-mode-line + flyspell fringe-helper ;; to be deleted ;; fuzzy