Skip to content

Instantly share code, notes, and snippets.

@kinjo kinjo/osx-say.el
Last active Aug 29, 2015

Embed
What would you like to do?
;;; osx-say.el --- Utilizing `say' command which Mac OSX has for Emacs
;; Copyright (C) 2014 Takumi Kinjo
;; Author: Takumi Kinjo <takumi.kinjo@gmail.com>
;; Keywords: local, hypermedia
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Utilizing `say' command which Mac OSX has for Emacs.
;; This library was forked from https://gist.github.com/ShingoFukuyama/7986889.
;;; Code:
(defgroup osx-say nil
"Option for osx-say."
:group 'osx-say)
(defcustom osx-say-speed 180
"Adjust speak speed."
:group 'osx-say :type 'integer)
(defcustom osx-say-voice "Alex"
"Kathy, Vicki, Victoria, Alex, Bruce and Fred are available."
:group 'osx-say
:type '(choice (const "Kathy")
(const "Vicki")
(const "Victoria")
(const "Alex")
(const "Bruce")
(const "Fred")))
(defconst osx-say-buffer "*osx say*")
;;;###autoload
(defun osx-say-stop ()
(interactive)
(when (get-buffer osx-say-buffer)
(kill-buffer osx-say-buffer)))
;;;###autoload
(defun osx-say (&optional $word $speed)
"Utilize `say' command that Mac OSX has."
(interactive)
(unless (executable-find "say")
(error (message "`say' command not found")))
(osx-say-stop)
(cond ($word $word)
(mark-active
(setq $word
(buffer-substring-no-properties
(region-beginning) (region-end))))
((setq $word (thing-at-point 'word)))
(t (setq $word (read-string "word: "))))
(mapc (lambda ($r)
(setq $word (replace-regexp-in-string (car $r) (cdr $r) $word)))
(list ;;'("'" . "\\\\'")
'("\"" . "\\\\\"")
'("?" . "\\\\?")
'("\n" . " ")
'("\(" . "\\\\(")
'("\)" . "\\\\)")
'("\\[" . "\\\\[")
'("\\]" . "\\\\]")
'("\;" . "\\\\;")
'("\&" . "\\\\&")
'("\|" . "\\\\|")))
(save-window-excursion
(start-process "OSX Say" osx-say-buffer
"say" "-v" osx-say-voice "-r"
(number-to-string (or $speed osx-say-speed)) $word)))
;;;###autoload
(defun osx-say-word ()
"Do osx-say with a word at point."
(interactive)
(osx-say (thing-at-point 'word)))
;;;###autoload
(defun osx-say-line ()
"Do osx-say with a line at point."
(interactive)
(osx-say
(buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
;;;###autoload
(define-minor-mode osx-say-minor-mode
"Toggle osx-say minor mode."
;; The initial value.
:init-value nil
;; The indicator for the mode line.
:lighter " say"
;; The minor mode bindings.
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c s w") 'osx-say-word)
(define-key map (kbd "C-c s l") 'osx-say-line)
map))
(provide 'osx-say)
;;; osx-say.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.