Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Custom flyspell-insert-function that will read the word out loud
;; Read the inserted word out loud using the macOS speech synthesizer
;; when a misspelled word is corrected using the flyspell
;; `flyspell-auto-correct-word'-command.
;;
;; Note this only work on macOS. It should be fairly easy to change to
;; work with other command line interface speech synthesize systems.
;;
;; In a previous version this used to be a defadvice, but I've changed
;; it to implementing a custom flyspell insert function. This seems to
;; work a bit more reliably.
(defcustom mg/say-voice ""
"The voice to use, if nil use system voice"
:type '(choice
(const :tag "Default" "")
(const :tag "Magnus" "magnus")
(const :tag "Thomas" "thomas")
(const :tag "Samantha" "samantha")))
(defcustom mg/say-speech-rate 160
"The rate to use for the speech synthesizer"
:type 'integer)
(defun mg/say (word &optional output-buffer)
(let ((voice (format "--voice=%s" mg/say-voice))
(rate (format "--rate=%d" mg/say-speech-rate))
(topic (format "/%s" word)))
(start-process "speak" output-buffer "say" voice rate topic)))
(defun mg/say-and-insert (word)
(progn (mg/say word)
(insert word)))
;; (setq flyspell-insert-function (function mg/say-and-insert))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment