1

I'm trying to write a script using emacs --script and in it I am using start-process, because I want to use set-process-filter.

The following does nothing if run as a script:

#!/usr/bin/env -S emacs -Q --script # -*- mode: emacs-lisp; lexical-binding: t; -*-
 (let ((p (start-process "subcommand" nil "notify-send" "Hello!")))
   (set-process-sentinel p (lambda (process signal)
                             (start-process "notify" nil "notify-send" "Hello again!")))
   (set-process-filter p (lambda (process output)
                           (message output)))
   ;; (while t) 
   )

However, if I run that same elisp in my gui emacs I get to notifications (one for hello and one for hello again). I tried to solve this using a no-op loop (commented out), but that only allowed the first hello to be sent out. Basically, I want emacs to wait for it's subprocess to finish before exiting.

I'm not sure where to go from here. Any ideas on how I can get something like this working?

1 Answer 1

2

Basically, Emacs exits before the subprocess has exited. All you have to do is delay the exit until the subprocess has exited. Since Emacs does not have anything useful to do, you can have it sleep for a while. The following sleeps in a loop until the subprocess does not appear in the process list any longer:

#!/usr/bin/env -S emacs -Q --script # -*- mode: emacs-lisp; lexical-binding: t; -*-

 (let ((p (start-process "subcommand" nil "notify-send" "Hello!")))
   (if (memq p (process-list))
       (message "YES"))
   (set-process-sentinel p (lambda (process signal)
                             (start-process "notify" nil "notify-send" "Hello again!")))
   (set-process-filter p (lambda (process output)
                           (message output)))

   (while (memq p (process-list))
     (sleep-for 1))
   )

The loop you had, (while t), was keeping emacs busy and not allowing it to process the sentinel for the subprocess. The loop also never exited, so you have to kill emacs to make it stop, at which point emacs cannot process the sentinel for the subprocess any longer, so you lost the second message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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