Subject: bug#59307: [PATCH] Cancel proced auto update timer if no proced buffers are open

From: Laurence Warne <[email-masked]>
Date: Sun, 20 Nov 2022 11:18:17 +0000

Hi, thanks for taking a look.  Apologies, you're right.

I've attached an updated patch, the problem before was that it was possible
for a timer to be cancelled when there still existed proced buffers, which
broke proced-toggle-auto-update.

To reproduce, apply the patch and then type
>
>   M-x proced-toggle-auto-update RET
>
> The result is that the Proced display is not updated every 5 sec as
> expected, and the value of proced-auto-update-timer is nil.
>

I've tested this with the new patch, and it works for me.

Thanks, Laurence
From fa8af3f90bc33d554b2d6a8c6c4c3866be77a2ff Mon Sep 17 00:00:00 2001
From: Laurence Warne <[email-masked]>
Date: Tue, 15 Nov 2022 18:48:40 +0000
Subject: [PATCH] Cancel proced auto update timer if no proced buffers are open

* lisp/proced.el (proced-auto-update-timer): Cancel timer if no proced
buffers are open.
---
 lisp/proced.el | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lisp/proced.el b/lisp/proced.el
index a774f2dd1e..91f7a944f9 100644
--- a/lisp/proced.el
+++ b/lisp/proced.el
@@ -740,12 +740,18 @@ proced
         "Type \\<proced-mode-map>\\[quit-window] to quit, \\[proced-help] for help")))))
 
 (defun proced-auto-update-timer ()
-  "Auto-update Proced buffers using `run-at-time'."
-  (dolist (buf (buffer-list))
-    (with-current-buffer buf
-      (if (and (eq major-mode 'proced-mode)
-               proced-auto-update-flag)
-          (proced-update t t)))))
+  "Auto-update Proced buffers using `run-at-time'.
+
+If there are no proced buffers, cancel the timer."
+  (unless (seq-filter (lambda (buf)
+                        (with-current-buffer buf
+                          (when (eq major-mode 'proced-mode)
+                            (when proced-auto-update-flag
+                              (proced-update t t))
+                            t)))
+                      (buffer-list))
+    (cancel-timer proced-auto-update-timer)
+    (setq proced-auto-update-timer nil)))
 
 (defun proced-toggle-auto-update (arg)
   "Change whether this Proced buffer is updated automatically.
-- 
2.30.2