emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 6931f06 1/2: * packages/timerfunctions/timerfunctions.el:


From: Stefan Monnier
Subject: [elpa] master 6931f06 1/2: * packages/timerfunctions/timerfunctions.el: Use lexical-binding
Date: Tue, 3 Mar 2020 12:56:47 -0500 (EST)

branch: master
commit 6931f069f46b47399f74c4d6527e260089c099db
Author: Stefan Monnier <address@hidden>
Commit: Stefan Monnier <address@hidden>

    * packages/timerfunctions/timerfunctions.el: Use lexical-binding
    
    Move tests after definition of `tf-with-timeout` to fix the use of
    a macro before its definition.
    
    (tf--get-secs): New function.
    (tf-run-with-idle-timer): Allow `secs` and `redosecs` to be functions
    rather than expressions.
    (tf--run-while-idle): Rename from `tf-run-while-idle`.
    Adjust to new `redosecs` functionality.
    (tf--with-timeout-handler-internal): Use `symbol-value` rather than `eval`.
    (tf--internal-var-recenter): Rename from tf-internal-var-recenter.
---
 packages/timerfunctions/timerfunctions.el | 314 +++++++++++++++---------------
 1 file changed, 161 insertions(+), 153 deletions(-)

diff --git a/packages/timerfunctions/timerfunctions.el 
b/packages/timerfunctions/timerfunctions.el
index 20dc1df..cd9e4f4 100644
--- a/packages/timerfunctions/timerfunctions.el
+++ b/packages/timerfunctions/timerfunctions.el
@@ -1,13 +1,13 @@
-;;; timerfunctions.el --- Enhanced versions of some timer.el functions
+;;; timerfunctions.el --- Enhanced versions of some timer.el functions  -*- 
lexical-binding:t -*-
 
-;; Copyright (C) 2000-2002, 2015  Free Software Foundation, Inc.
+;; Copyright (C) 2000-2002, 2015-2016, 2020  Free Software Foundation, Inc.
 
 ;; Time-stamp: <2015-03-02 12:23:21 deego>
 ;; Emacs Lisp Archive entry
 ;; Filename: timerfunctions.el
 ;; Author: Dave Goel <address@hidden>
 ;; Version: 1.4.2
-;; Package-Requires: ((cl-lib "0.5"))
+;; Package-Requires: ((cl-lib "0.5") (emacs "24"))
 ;; Created: 2000/11/20
 ;; Author's homepage: http://gnufans.net/~deego
 
@@ -55,6 +55,7 @@ provided a `tf-with-timeout-check'.")
 
 
 (defconst timerfunctions-introduction
+  ;; FIXME: I think this belongs in the "Commentary:" instead.
   "timerfunctions.el contains some “enhanced” versions of a few timer.el
 functions.  It is also used by vel.el, idledo.el etc.
 
@@ -79,7 +80,8 @@ functions.  It is also used by vel.el, idledo.el etc.
 
 ;;; Real Code:
 
-
+;; FIXME: Why autoload?
+;; FIXME: Why not use time-subtract?
 ;;;###autoload
 (defun tf-time-difference (timeplus timesub)
   "Return the time in seconds elaspsed from TIMESUB to TIMEPLUS.
@@ -90,6 +92,12 @@ Conceptually:  \(- TIMEPLUS TIMESUB \)."
 )
 
 
+(defun tf--get-secs (secs)
+  (cond
+   ((functionp secs) (funcall secs))
+   ((numberp secs) secs)
+   (t (eval secs t)))) ;; Obsolete calling convention!
+
 ;;;###autoload
 (defun tf-run-with-idle-timer  (secs repeat redosecs redorepeat includeruntime 
function &rest args)
   "Similar to `run-with-idle-timer', except that provides more options.
@@ -101,58 +109,57 @@ Similar to `run-with-idle-timer', but provides more 
options.
 Suppose you want Emacs to run an action every REDOSECS for as
 long as Emacs remains idle.  Emacs' `run-with-idle-timer' will
 perform the action exactly once every time Emacs goes idle.  This
-funciton, on the other hand, will allow
+function, on the other hand, will allow
 you to keep performing an action as long as Emacs remains idle.
 
 SECS is the number of seconds to wait once Emacs has first gone
-idle. It can really be any expression whose at runtime yields a
-number.  Note that the way `run-with-idle-timer' is defined, SECS will
-unfortunately be evalled immediately after you call this function, but
-redosecs will be *every* time Emacs *remains* idle..yay..
+idle or a function that returns this number when called with no arguments.
+Note that the way `run-with-idle-timer' is defined, SECS will
+unfortunately be called immediately after you call this function, but
+REDOSECS will be *every* time Emacs *remains* idle..yay..
 
 If REDOREPEAT is non-nil, the action is repeated as long Emacs remains
 idle.  REDOSECS is the number of additional seconds (after the action
 has been done) to wait if Emacs remains idle before performing the
 action again.  Again, redosecs does not have to be a number, it can be
-any expression whose eval yields to a number...
+a function which yields such a number...
 
 If INCLUDERUNTIME is non-nil, REDOSECS is the number of
 additional seconds to wait after the action has been invoked (not
 finished).
 
 If REPEAT is nonnil, the entire cycle is repeated every time Emacs
-next goes idle.. (as in the default `run-with-idle-timer'."
-  (apply 'run-with-idle-timer
-        (eval secs) repeat 'tf-run-while-idle
+next goes idle.. (as in the default `run-with-idle-timer')."
+  (apply #'run-with-idle-timer
+        (tf--get-secs secs) repeat #'tf--run-while-idle
         redosecs redorepeat includeruntime
         function args)
   )
 
 
-(defun tf-run-while-idle (redosecs redorepeat includeruntime function &rest 
args)
+(defun tf--run-while-idle (redosecs redorepeat includeruntime function &rest 
args)
   "A simplified version of `tf-run-with-idle-timer'.
 
-Runs FUNCTION with ARGS and optionally repeats if emacs remains idle.
+Runs FUNCTION with ARGS and optionally repeats if Emacs remains idle.
 Probably is of no use unless used in programs.
  If REDOREPEAT is non-nil, the function is repeated periodically every
-REDOSECS as long as emacs remains idle. By default, emacs waits
+REDOSECS as long as Emacs remains idle. By default, Emacs waits
 REDOSECS *after* the function is done executing to repeat.  If you want
 the execution-time to count towards REDOSECS, make INCLUDERUNTIME
 non-nil.
-SECS and REDOSECS can be any expressions that eval at runtime to
-numbers. In particular, of course, they can simply be numbers."
+REDOSECS can be a number or a function that returns a number."
   (if (not includeruntime)
       (progn
        (apply function args)
        (if redorepeat
-           (while (sit-for (eval redosecs))
+           (while (sit-for (tf--get-secs redosecs))
              (apply function args))))
     (progn
       (let ((before-time (current-time)))
        (apply function args)
        (if redorepeat
            (while (sit-for (-
-                            (eval redosecs)
+                            (tf--get-secs redosecs)
                             (tf-time-difference (current-time)
                                                 before-time)))
              (setq before-time (current-time))
@@ -160,122 +167,10 @@ numbers. In particular, of course, they can simply be 
numbers."
   )
 
 
-;;;====================================================
-;;;TESTS FOLLOW
-(defun tf-test-display-time-internal ()
-  "A test function."
-  (interactive)
-  (let ((thisbuffer (buffer-name)))
-    (switch-to-buffer-other-window "*scratch*")
-    (goto-char (point-max))
-    (insert (concat "\n" (format "%S" (cadr (current-time)))))
-    (recenter)
-    (switch-to-buffer-other-window thisbuffer))
-)
-
-
-(defun tf-test-idle-timer ()
-  "A test function.
-
-Run this and watch Play around with the options.  If you run it,
-you may have to exit your Emacs session to restore normal Emacs,
-unless you clean things up carefully!"
-
-  (interactive)
-  (tf-run-with-idle-timer
-  1 t 3 t nil 'tf-test-display-time-internal)
-)
-
-
-
-
-
-(defun tf-test-timeout ()
-  "Bad count should be zero."
-  (interactive)
-  (let ((inhi nil) (goodcount 0) (badcount 0) (ctr 0) (a 1) (b 2)
-       (mytag nil)
-       (myvar nil)
-       )
-    (cl-loop
-     for ctr from 0 to 10 do
-     (message "ctr=%S" ctr)
-     (tf-with-timeout 'inhi 'mytah 'myvar
-      (0.3 nil)
-      (cl-loop for i from 0 to 100000 do
-           (message "ctr=%S, i=%S" ctr i)
-           (setq inhi t)
-           (setq a (random 100))
-           (sleep-for 0.1)
-           (setq b a)
-           (setq inhi nil)
-           (sleep-for 0.02)
-           ))
-     (if (equal b a) (cl-incf goodcount) (cl-incf badcount)))
-    (message "Goodcount: %S; badcount: %S" goodcount badcount)))
-
-
-
-(defun tf-test-timeout-complex ()
-  "Should return a value of 20000 for a."
-
-  (interactive)
-  (let ((inhi t) (goodcount 0) (badcount 0) (ctr 0) (a 1) (b 2)
-       (mytag nil)
-       (myvar nil)
-       )
-    (setq a 0)
-    (message "ctr=%S" ctr)
-    (tf-with-timeout
-     'inhi 'mytag 'myvar
-     (0.1 nil)
-     (cl-loop for i from 0 to 10000 do
-          (message "first loop. ctr=%S, i=%S, " ctr i)
-          (cl-incf a))
-     (message "initial loop ends here.")
-     ;; no throw here because loop prohibited.
-     (tf-with-timeout-check 'inhi 'mytag 'myvar)
-     ;; this shouldn't help either
-     (sit-for 0.3)
-
-     (cl-loop for i from 0 to 10000 do
-          (message "second loop.  i=%S" i)
-          (cl-incf a))
-     (message "second loop ends here.")
-     (setq inhi nil)
-     ;; this should throw.
-     (tf-with-timeout-check 'inhi 'mytag 'myvar)
-     ;; this should NOT be needed.
-     ;;(sit-for 0.2)
-     ;; this loop should never take place.
-     (cl-loop for i from 0 to 1000 do
-          (message "third loop, i=%S" i)
-          (cl-incf a))
-     (message "third loop ends here."))
-    (message "%S" a)
-    a))
-
-
-
-(defvar tf-internal-var-recenter 1)
-
-(defun tf-test-internal-recenter-toggle ()
-  "A test function."
-  (interactive)
-  (recenter 1)
-  (setq tf-internal-var-recenter (- 0 tf-internal-var-recenter)))
-
-(defun tf-test-example-timer-recenter ()
-  "An example timer.
-Changes the screen display every 3 seconds, thus ensuring that you
-don't time out of ssh sessions."
-  (interactive)
-  (tf-run-with-idle-timer 3 t 3 t nil 'tf-test-internal-recenter-toggle))
-
-
 
 
 (defun tf-todo-wait-until-idle (&optional secs)
+  ;; FIXME: This can only work if/when we add concurrency to Elisp, IIUC.
   "This function is not functional yet.
 
 Waits until idle.  Arguments are SECS.
@@ -306,45 +201,45 @@ check every `tf-with-timeout-repeat-sec' seconds to see 
if we are
 uninhibited, yet.  This variable is customizable.")
 
 
-(defun tf-with-timeout-handler-internal (tag timedoutvar inhibitp)
-  "Internal function.
-Arguments are TAG, TIMEDOUTVAR, and INHIBITP."
+(defun tf--with-timeout-handler-internal (tag timedoutvar inhibit-var)
+  "Internal function."
   (set timedoutvar t)
   ;;(tf-with-timeout-check tag timedoutvar inhibitp)
   ;; which is equivalent to:
-  (unless (eval inhibitp)
+  (unless (symbol-value inhibit-var)
     (tf-ignore-errors (throw tag 'timeout)))
   )
 
-(defun tf-with-timeout-check (inhibitp tag timedoutvar)
+(defun tf-with-timeout-check (inhibit-var tag timedoutvar)
   "Internal function.
 Check whether timeout has actually reached.
 We need this step because this function might be called by the
-user as well.  Arguments are INHIBITP, TAG and TIMEDOUTVAR."
-  (when (eval timedoutvar)
-    (unless (eval inhibitp)
+user as well."
+  (when (symbol-value timedoutvar)
+    (unless (symbol-value inhibit-var)
       (tf-ignore-errors (throw tag 'timeout)))))
 
 
 
 (defvar tf-tag-tmpvar nil)
 
-(defmacro tf-catch (tag &rest body)
+(defmacro tf-catch (tag &rest body)     ;FIXME: Unused.
   "Catch a TAG in BODY."
+  (declare (obsolete catch "1.4.3"))
   `(let
        ;; unquote the tag here..
        ((,(cadr tag) 'tf-catch))
      (catch ,tag
        ,@body)))
 
-(defmacro tf-throw (tag value)
+(defun tf-throw (tag value)             ;FIXME: Unused.
   "Throw a TAG with value VALUE."
-  `(when (eql (eval ,tag) 'tf-catch)
-     (throw ,tag value)))
-
+  (declare (obsolete throw "1.4.3"))
+  (when (eql (eval tag t) 'tf-catch)
+     (throw tag value)))
 
 ;;;###autoload
-(defmacro tf-with-timeout (inhibitp timertag timedoutvar tlist &rest body)
+(defmacro tf-with-timeout (inhibit-var timertag timedoutvar tlist &rest body)
   "Like `with-timeout' but with support for unbreakable code.
 
 Provides ability to inhibit timeout during parts of the body.
@@ -372,8 +267,8 @@ event \(such as keyboard input, input from subprocesses, or 
a certain time);
 if the program loops without waiting in any way, the timeout will not
 be detected.  Furthermore:
 
-During the execution of the body, we SHALL NOT time out when INHIBITP
-evals to non-nil.  Thus, for example, you might initially setq a
+During the execution of the body, we SHALL NOT time out when INHIBIT-VAR's
+content is non-nil.  Thus, for example, you might initially setq a
 variable my-var as nil, supply inhibitp as `my-var', and then you may
 setq my-var to t or nil within the body of tf-with-timeout to enable
 or disable timeout.  The best use of this functionality is to setq
@@ -422,7 +317,7 @@ instead of (sleep-for 0.02):
  (tf-with-timeout-check \\='mytag \\='mytimedoutvar \\='myinhibitp)
 
 Moreover, if that is the main check you rely on, you it perhaps makes
-sense to increase the value of tf-with-timeout-repeat-sec, so that
+sense to increase the value of `tf-with-timeout-repeat-sec', so that
 your cpu cycles are not wasted every 0.01 sec.  See the doc of that
 variable for more.
 
@@ -435,14 +330,14 @@ TIMEDOUTVAR is the variable that times out."
     `(let (
           ;;(with-timeout-tag (cons nil nil))
           with-timeout-value with-timeout-timer)
-       (set ,timedoutvar nil)
+       (set ,timedoutvar nil)          ;FIXME: Why do we need this?
        (if (catch ,timertag
             (progn
               (setq with-timeout-timer
                     (run-with-timer ,seconds tf-with-timeout-repeat-sec
-                                    'tf-with-timeout-handler-internal
+                                    #'tf--with-timeout-handler-internal
                                     ,timertag ,timedoutvar
-                                    ,inhibitp))
+                                    ,inhibit-var))
               (setq with-timeout-value (progn ,@body))
               nil))
           (progn ,@timeout-forms)
@@ -450,5 +345,118 @@ TIMEDOUTVAR is the variable that times out."
         with-timeout-value))))
 
 
+;;;====================================================
+;;;TESTS FOLLOW
+(defun tf-test-display-time-internal ()
+  "A test function."
+  (interactive)
+  (let ((thisbuffer (buffer-name)))
+    (switch-to-buffer-other-window "*scratch*")
+    (goto-char (point-max))
+    (insert (concat "\n" (format "%S" (cadr (current-time)))))
+    (recenter)
+    (switch-to-buffer-other-window thisbuffer))
+)
+
+
+(defun tf-test-idle-timer ()
+  "A test function.
+
+Run this and watch Play around with the options.  If you run it,
+you may have to exit your Emacs session to restore normal Emacs,
+unless you clean things up carefully!"
+
+  (interactive)
+  (tf-run-with-idle-timer
+  1 t 3 t nil #'tf-test-display-time-internal)
+)
+
+
+
+
+
+(defun tf-test-timeout ()
+  "Bad count should be zero."
+  (interactive)
+  (defvar tf--test-inhi) (defvar tf--test-myvar)
+  (let ((tf--test-inhi nil) (goodcount 0) (badcount 0) (a 1) (b 2)
+       (tf--test-myvar nil)
+       )
+    (cl-loop
+     for ctr from 0 to 10 do
+     (message "ctr=%S" ctr)
+     (tf-with-timeout 'tf--test-inhi 'mytag 'tf--test-myvar
+      (0.3 nil)
+      (cl-loop for i from 0 to 100000 do
+           (message "ctr=%S, i=%S" ctr i)
+           (setq tf--test-inhi t)
+           (setq a (random 100))
+           (sleep-for 0.1)
+           (setq b a)
+           (setq tf--test-inhi nil)
+           (sleep-for 0.02)
+           ))
+     (if (equal b a) (cl-incf goodcount) (cl-incf badcount)))
+    (message "Goodcount: %S; badcount: %S" goodcount badcount)))
+
+
+
+(defun tf-test-timeout-complex ()
+  "Should return a value of 20000 for a."
+
+  (interactive)
+  (defvar tf--test-inhi) (defvar tf--test-myvar)
+  (let ((tf--test-inhi t) (ctr 0) (a 1)
+       (tf--test-myvar nil)
+       )
+    (setq a 0)
+    (message "ctr=%S" ctr)
+    (tf-with-timeout
+     'tf--test-inhi 'mytag 'tf--test-myvar
+     (0.1 nil)
+     (cl-loop for i from 0 to 10000 do
+          (message "first loop. ctr=%S, i=%S, " ctr i)
+          (cl-incf a))
+     (message "initial loop ends here.")
+     ;; no throw here because loop prohibited.
+     (tf-with-timeout-check 'tf--test-inhi 'mytag 'tf--test-myvar)
+     ;; this shouldn't help either
+     (sit-for 0.3)
+
+     (cl-loop for i from 0 to 10000 do
+          (message "second loop.  i=%S" i)
+          (cl-incf a))
+     (message "second loop ends here.")
+     (setq tf--test-inhi nil)
+     ;; this should throw.
+     (tf-with-timeout-check 'tf--test-inhi 'mytag 'tf--test-myvar)
+     ;; this should NOT be needed.
+     ;;(sit-for 0.2)
+     ;; this loop should never take place.
+     (cl-loop for i from 0 to 1000 do
+          (message "third loop, i=%S" i)
+          (cl-incf a))
+     (message "third loop ends here."))
+    (message "%S" a)
+    a))
+
+
+
+(defvar tf--internal-var-recenter 1)
+
+(defun tf-test-internal-recenter-toggle ()
+  "A test function."
+  (interactive)
+  (recenter 1)
+  (setq tf--internal-var-recenter (- 0 tf--internal-var-recenter)))
+
+(defun tf-test-example-timer-recenter ()
+  "An example timer.
+Changes the screen display every 3 seconds, thus ensuring that you
+don't time out of ssh sessions."
+  (interactive)
+  (tf-run-with-idle-timer 3 t 3 t nil #'tf-test-internal-recenter-toggle))
+
+
 (provide 'timerfunctions)
 ;;; timerfunctions.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]