I want to call a command asynchronously, and define a function that handles output from that command. I don't want to simply write output to a buffer.
According to the Elisp info (36.9.2 Process Filter Functions) I need to set a process filter function. I tried this:
(defun my-echo-filter (proc string)
(message "proc: %s string: %s" proc string))
(let ((my-proc (start-file-process "foo" nil "date")))
(set-process-filter my-proc #'my-echo-filter)) ; writes the time to *Messages*
However, this makes me nervous. I'm starting the process first, and only afterwards am I attaching the process filter.
If I do anything between creating the process and attaching the process filter, I can miss output.
(let ((my-proc (start-file-process "foo" nil "date")))
(sleep-for 2)
(set-process-filter my-proc #'my-echo-filter)) ; Does not write the time!
How do I create a process and ensure that my process filter doesn't miss anything?
start-file-process
andset-process-filter
when process output could arrive behind your back (as it were). You would have to callsit-for
oraccept-process-output
for that to happen.sleep-for
counts as idle. What about if garbage collection occurs?(elisp) Accepting Output
: "Output from asynchronous subprocesses normally arrives only while Emacs is waiting for some sort of external event, such as elapsed time or terminal input."