5

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?

GDP2
  • 1,340
  • 9
  • 25
  • Are the answers suggested [here](https://stackoverflow.com/questions/10766550/how-do-i-execute-a-shell-command-in-background) not satisfying? – Nsukami _ Mar 12 '18 at 20:03
  • @Nsukami_ That post deals specifically with `shell-command`. I'm looking for answers pertaining to `shell-command-on-region`. – GDP2 Mar 12 '18 at 21:37

1 Answers1

4

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))))
Basil
  • 12,019
  • 43
  • 69
Nsukami _
  • 6,341
  • 2
  • 22
  • 35
  • 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... – npostavs Mar 12 '18 at 23:15
  • @npostavs Thanks for the suggestion o/ – Nsukami _ Mar 12 '18 at 23:34
  • 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` with `say` and the arguments for it that I desired. – GDP2 Mar 13 '18 at 17:19