Skip to content

Instantly share code, notes, and snippets.

@ryseto
Created March 29, 2012 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryseto/2242924 to your computer and use it in GitHub Desktop.
Save ryseto/2242924 to your computer and use it in GitHub Desktop.
Run 'say' command on Emacs
;;; Convert text to audible speech by /usr/bin/say (Emacs + OSX)
(defun MyTeX-speech ()
"Convert text to audible speech by /usr/bin/say of OSX."
(interactive)
(let (b e s
(block-sep-str "^\*\\|^\\ *%\\|^\\ *\n\\|\\\\item\\|\\\\begin\\|\\\\end"))
(if (eq (process-status "speech") 'run)
(delete-process "speech")
(progn
(if mark-active
(setq b (mark) e (point))
(setq b (save-excursion
(progn
(re-search-backward block-sep-str (point-min) 1)
(point)))
e (save-excursion
(progn
(re-search-forward block-sep-str (point-max) 1)
(point)))))
(setq s (buffer-substring-no-properties b e)
s (replace-regexp-in-string "\\\\%" "percent" s)
s (replace-regexp-in-string "\\\\item" " " s)
s (replace-regexp-in-string "\\\\begin" " " s)
s (replace-regexp-in-string "\\\\end" " " s)
s (replace-regexp-in-string "%[^\n]*" "" s)
s (replace-regexp-in-string
"\\\\[a-zA-Z]+{\\|[$\\\\_^~(){}`*\n]" " " s)
s (replace-regexp-in-string "\\ +" " " s))
(message s)
(process-kill-without-query
(start-process-shell-command "speech" nil
"/usr/bin/say" (concat "\"" s "\"" )))))))
(define-key global-map [?\s-r] 'MyTeX-speech)
@ryseto
Copy link
Author

ryseto commented Apr 1, 2012

This function converts text in a marked region or a block sectioned by %s at the beginning of a line to audible speech. It works on Mac OS X (/usr/bin/say is required.)

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