emacs-nano-tts-minor-mode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 134f9882a1bf6d6da46b710f4cf8fc18b01656d9
parent cda12a8338fead45f8c35b346a070d60b00dcc47
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sat,  8 Jul 2023 17:57:58 +0300

Repeated nano-tts-speak would add to the existing espeak stdin. Also, nano-tts-kill works.

Diffstat:
Mnano-tts.el | 62+++++++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 47 insertions(+), 15 deletions(-)

diff --git a/nano-tts.el b/nano-tts.el @@ -1,5 +1,6 @@ ;; -*- lexical-binding: t -*- + (defvar nano-tts--tts-process nil "The process of a running text-to-speech engine. @@ -7,30 +8,61 @@ nil when no text-to-speech engine is running.") +(defun nano-tts--get-region-as-string-to-speak () + (concat + (buffer-substring (region-beginning) + (region-end)) + ;; XXX: We add a newline because it seems espeak stdin is buffered + ;; on newlines(?). + "\n")) + + (defun nano-tts-speak () (interactive) - (unless nano-tts--tts-process - ;; We start the engine so that later we can send text to it. - (setq nano-tts--tts-process - (make-process - :name "espeak" - :command (list "espeak"))) - - (let* ((start (min (mark) (point))) - (end (max (mark) (point))) - (text-to-speak (buffer-substring start - end))) + (if nano-tts--tts-process + ;; If there is already a TTS process. + (let ((tts-process-status (process-status nano-tts--tts-process))) + (cond + ;; And if the process is running. + ((equal tts-process-status + 'run) + + ;; Send the existing process the text. + (process-send-string nano-tts--tts-process + (nano-tts--get-region-as-string-to-speak))) + ;; If the TTS process is not running. + (t + ;; Make the system aware that the TTS process had stopped. + (setq nano-tts--tts-process + nil)))) + + ;; If there is no running TTS process. + (progn + ;; Start the TTS process so that later we can send text to it. + (setq nano-tts--tts-process + (make-process + :name "espeak" + :command (list "espeak"))) + + ;; Send text to it. (process-send-string nano-tts--tts-process - (concat text-to-speak "\n"))))) + (nano-tts--get-region-as-string-to-speak))))) (defun nano-tts-kill () (interactive) - (kill-process nano-tts--tts-process) - (setq nano-tts--tts-process - nil)) + ;; If there is a TTS process object. + (if nano-tts--tts-process + (let ((old-tts-process nano-tts--tts-process)) + ;; Make the system aware that it is dead. + (setq nano-tts--tts-process + nil) + ;; Kill the TTS process. + (kill-process old-tts-process) + (message (format "Kill nano-tts process: %S" + old-tts-process))))) (define-minor-mode nano-tts