I have a problem which is very similar to this one. I have some text that I'd like to listen to with macOS's say
utility. Unfortunately, Emacs locks up when I use shell-command-on-region
because it is not asynchronous. In other words, Emacs waits for the shell process to finish before it does anything else. This is highly inconvenient and would be resolved with an asynchronous version of shell-command-on-region
. Yet, it seems that no such function exists despite there being other asynchronous alternatives to shell-
commands. Does anyone know of a simple way to solve this problem?
1 Answer
Something in this spirit should do the trick. I tried it with espeak
under Ubuntu; I guess it would work with say
as well.
(defun my-read-words-on-region ()
"Send the region to `espeak'."
(interactive)
(start-process "espeak-process" "espeak-buffer" "espeak" "-v" "en-us")
(process-send-region "espeak-process" (region-beginning) (region-end)))
EDIT:
The previous function assume the process name is unique, which it may not be if you happen to call the function twice, so, following npostavs's suggestion, a better approach would be:
(defun my-read-words-on-region-2 ()
"Send the region to `espeak'."
(interactive)
(let ((proc (start-process "espeak-process" "espeak-buffer" "espeak" "-v" "en-us")))
(process-send-region proc (region-beginning) (region-end))))
-
I suggest
(let ((proc (start-process ...))) (process-send-region proc ...))
, otherwise you assume the process name is unique, which it may not be if you happen to call the function twice...– npostavsMar 12, 2018 at 23:15 -
-
Thanks! The second function seems to work fine and I'll probably bind it to a key for easy use. Of course, I replaced
espeak
withsay
and the arguments for it that I desired.– GDP2Mar 13, 2018 at 17:19
shell-command
. I'm looking for answers pertaining toshell-command-on-region
.