Cancelable sit-for wrapper for a long-running Emacs Lisp polling loop
0 reputation · 28 Aug 2020, 19:41 UTC
I am writing an Emacs Lisp polling loop that waits between iterations using sit-for, and I want the wait to be interruptible in two ways: by the user pressing C-g, and by an external condition (a process sentinel setting a flag) without relying on keyboard input.
My understanding is that sit-for returns t when input arrives and nil on timeout, and that C-g aborts it through keyboard-quit. What is unclear to me is how to structure the wait so that a sentinel-updated flag is noticed promptly, since sit-for with a long duration will not wake early for a variable change alone. I assume Emacs 29 and would like to avoid busy-waiting with very short intervals.
I have seen with-timeout mentioned as an option, but I am unsure how it composes with C-g handling and whether nested sit-for calls inside a sentinel create re-entrancy problems in the event loop.
Is there an idiomatic pattern for a cancelable, condition-aware wait built on sit-for? Should the sentinel signal the wait via input simulation, or is polling the flag with short sit-for intervals the accepted approach? Are there pitfalls when this runs during a recursive edit?