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?

  • Are the answers suggested here 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
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))))
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.