Friday 27 December 2024


TIL: Elisp function `set-process-sentinel'
==========================================

While working on an Elisp program with Gijs [1], he used an Elisp
function called `set-process-sentinel', which I was unfamiliar with.
I was focused on other things, but made a mental note to dive into
the use of this function.

Doc string
----------
This is what the doc string of `set-process-sentinel' learns us:

   (set-process-sentinel PROCESS SENTINEL)

   Give PROCESS the sentinel SENTINEL; nil for default.
   The sentinel is called as a function when the process changes
   state. It gets two arguments: the process, and a string describing
   the change.

Exploring with IELM
-------------------
To understand more about this function I fired up IELM, the Elisp
REPL. This is a lovely way to experiment with Elisp.

With some browsing around on the internet I came to the following:

   (defun my-notifier (process event)
     "Notify when this function is called.
   Report the value of PROCESS and  EVENT."
     (message (format "%s had event: %s\n"
                      process
                      event)))

   (defun sentinel-play ()
     "Start a process, which runs `sleep' in the buffer `my-buffer'."
     (interactive)
     (start-process "my-process" "my-buffer" "sleep" "30")
     (set-process-sentinel (get-process "my-process")
                           #'my-notifier))

The function `sentinel-play' starts an asynchronous process (sleep)
and the sets a sentinel. When the asynchronous process changes state,
the sentinel calls the function `my-notifier'.

In this case, the function `my-notifier' messages us after 30 seconds:

   my-process had event: finished

Have fun with Elisp!

[1]: gopher://hillenius.net/1


Last edited: $Date: 2024/12/27 11:16:47 $