From 24e550570554b3ae89d9621d6602fd3f993a7d29 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Wed, 10 Dec 2025 19:37:33 +0200 Subject: [PATCH] Evaluate result on first call to throttled func in timeout.el * lisp/emacs-lisp/timeout.el (timeout--throttle-advice) (timeout-throttled-func): Replace 'prog1' with 'progn' to return the evaluated value of 'result' (bug#79979). This will capture the correct return value the first time a throttled function is called. Previously, it would return nil on the very first call. (timeout-debounce, timeout-throttle, timeout-throttled-func) (timeout-debounced-func): Add the autoload cookie. --- lisp/emacs-lisp/timeout.el | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lisp/emacs-lisp/timeout.el b/lisp/emacs-lisp/timeout.el index 2b90650f02a..7529d4bc0a4 100644 --- a/lisp/emacs-lisp/timeout.el +++ b/lisp/emacs-lisp/timeout.el @@ -82,7 +82,7 @@ This is intended for use as function advice." (result)) (lambda (orig-fn &rest args) "Throttle calls to this function." - (prog1 result + (progn (unless (and throttle-timer (timerp throttle-timer)) (setq result (apply orig-fn args)) (setq throttle-timer @@ -90,7 +90,8 @@ This is intended for use as function advice." (timeout--eval-value timeout-value) nil (lambda () (cancel-timer throttle-timer) - (setq throttle-timer nil))))))))) + (setq throttle-timer nil))))) + result)))) (defun timeout--debounce-advice (&optional delay default) "Return a function that debounces its argument function. @@ -122,6 +123,7 @@ This is intended for use as function advice." (apply orig-fn args)))) (current-buffer)))))))) +;;;###autoload (defun timeout-debounce (func &optional delay default) "Debounce FUNC by making it run DELAY seconds after it is called. @@ -143,6 +145,7 @@ returned." '((name . debounce) (depth . -99))))) +;;;###autoload (defun timeout-throttle (func &optional throttle) "Make FUNC run no more frequently than once every THROTTLE seconds. @@ -159,6 +162,7 @@ previous successful call is returned." '((name . throttle) (depth . -98))))) +;;;###autoload (defun timeout-throttled-func (func &optional throttle) "Return a throttled version of function FUNC. @@ -182,7 +186,7 @@ previous successful call is returned." "\n\nThrottle calls to this function")) (interactive (advice-eval-interactive-spec (cadr (interactive-form func)))) - (prog1 result + (progn (unless (and throttle-timer (timerp throttle-timer)) (setq result (apply func args)) (setq throttle-timer @@ -190,14 +194,15 @@ previous successful call is returned." (timeout--eval-value throttle-value) nil (lambda () (cancel-timer throttle-timer) - (setq throttle-timer nil))))))) + (setq throttle-timer nil))))) + result)) ;; NON-INTERACTIVE version (lambda (&rest args) (:documentation (concat (documentation func) "\n\nThrottle calls to this function")) - (prog1 result + (progn (unless (and throttle-timer (timerp throttle-timer)) (setq result (apply func args)) (setq throttle-timer @@ -205,8 +210,10 @@ previous successful call is returned." (timeout--eval-value throttle-value) nil (lambda () (cancel-timer throttle-timer) - (setq throttle-timer nil)))))))))) + (setq throttle-timer nil))))) + result))))) +;;;###autoload (defun timeout-debounced-func (func &optional delay default) "Return a debounced version of function FUNC.