Skip to content

Instantly share code, notes, and snippets.

@takumikinjo
Last active March 28, 2021 04:36
Show Gist options
  • Save takumikinjo/429fd2f0e506d45cab64 to your computer and use it in GitHub Desktop.
Save takumikinjo/429fd2f0e506d45cab64 to your computer and use it in GitHub Desktop.
;;; osx-say.el --- Utilizing `say' command which Mac OSX has for Emacs
;; Copyright (C) 2014 Takumi Kinjo
;; Author: Takumi Kinjo <[email protected]>
;; 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*")
(defun osx-say-stop ()
(interactive)
(when (get-buffer osx-say-buffer)
(kill-buffer osx-say-buffer)))
(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 t
;; 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 o") '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