From 1e7c19e1aba6152dbc49173faa04d614fe2961c2 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Fri, 8 Oct 2021 11:20:53 +0200 Subject: [PATCH] Avoid creating timer if possible * timer.el (run-with-timer): Check if function can be called immediately (run-with-idle-timer): Check if function can be called immediately --- lisp/emacs-lisp/timer.el | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el index 1ef4931b7b..22029798fd 100644 --- a/lisp/emacs-lisp/timer.el +++ b/lisp/emacs-lisp/timer.el @@ -422,7 +422,9 @@ run-with-timer This function returns a timer object which you can use in `cancel-timer'." (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") - (apply 'run-at-time secs repeat function args)) + (if (and (numberp secs) (= secs 0) (null repeat)) + (apply function args) + (apply 'run-at-time secs repeat function args))) (defun add-timeout (secs function object &optional repeat) "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. @@ -448,13 +450,15 @@ run-with-idle-timer This function returns a timer object which you can use in `cancel-timer'." (interactive (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) - (y-or-n-p "Repeat each time Emacs is idle? ") - (intern (completing-read "Function: " obarray 'fboundp t)))) - (let ((timer (timer-create))) - (timer-set-function timer function args) - (timer-set-idle-time timer secs repeat) - (timer-activate-when-idle timer t) - timer)) + (y-or-n-p "Repeat each time Emacs is idle? ") + (intern (completing-read "Function: " obarray 'fboundp t)))) + (if (and (numberp secs) (= secs 0) (null repeat)) + (apply function args) + (let ((timer (timer-create))) + (timer-set-function timer function args) + (timer-set-idle-time timer secs repeat) + (timer-activate-when-idle timer t) + timer))) (defvar with-timeout-timers nil "List of all timers used by currently pending `with-timeout' calls.") -- 2.30.2