Skip to content

Instantly share code, notes, and snippets.

@cofi
Created June 28, 2012 19:22
Show Gist options
  • Save cofi/3013327 to your computer and use it in GitHub Desktop.
Save cofi/3013327 to your computer and use it in GitHub Desktop.
Command using Helm to select spelling corrections
(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)))))
@jbranso
Copy link

jbranso commented Oct 31, 2014

I have no idea how this code works, but I copied it into my init.el, and it works perfectly. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment