LCOV - code coverage report
Current view: top level - lisp/emacs-lisp - timer.el (source / functions) Hit Total Coverage
Test: tramp-tests.info Lines: 136 215 63.3 %
Date: 2017-08-27 09:44:50 Functions: 22 33 66.7 %

          Line data    Source code
       1             : ;;; timer.el --- run a function with args at some time in future -*- lexical-binding: t -*-
       2             : 
       3             : ;; Copyright (C) 1996, 2001-2017 Free Software Foundation, Inc.
       4             : 
       5             : ;; Maintainer: emacs-devel@gnu.org
       6             : ;; Package: emacs
       7             : 
       8             : ;; This file is part of GNU Emacs.
       9             : 
      10             : ;; GNU Emacs is free software: you can redistribute it and/or modify
      11             : ;; it under the terms of the GNU General Public License as published by
      12             : ;; the Free Software Foundation, either version 3 of the License, or
      13             : ;; (at your option) any later version.
      14             : 
      15             : ;; GNU Emacs is distributed in the hope that it will be useful,
      16             : ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             : ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             : ;; GNU General Public License for more details.
      19             : 
      20             : ;; You should have received a copy of the GNU General Public License
      21             : ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
      22             : 
      23             : ;;; Commentary:
      24             : 
      25             : ;; This package gives you the capability to run Emacs Lisp commands at
      26             : ;; specified times in the future, either as one-shots or periodically.
      27             : 
      28             : ;;; Code:
      29             : 
      30             : (eval-when-compile (require 'cl-lib))
      31             : 
      32             : (cl-defstruct (timer
      33             :                (:constructor nil)
      34             :                (:copier nil)
      35             :                (:constructor timer-create ())
      36             :                (:type vector)
      37             :                (:conc-name timer--))
      38             :   ;; nil if the timer is active (waiting to be triggered),
      39             :   ;; non-nil if it is inactive ("already triggered", in theory).
      40             :   (triggered t)
      41             :   ;; Time of next trigger: for normal timers, absolute time, for idle timers,
      42             :   ;; time relative to idle-start.
      43             :   high-seconds low-seconds usecs
      44             :   ;; For normal timers, time between repetitions, or nil.  For idle timers,
      45             :   ;; non-nil iff repeated.
      46             :   repeat-delay
      47             :   function args                         ;What to do when triggered.
      48             :   idle-delay                            ;If non-nil, this is an idle-timer.
      49             :   psecs)
      50             : 
      51             : (defun timerp (object)
      52             :   "Return t if OBJECT is a timer."
      53       64458 :   (and (vectorp object) (= (length object) 9)))
      54             : 
      55             : (defsubst timer--check (timer)
      56       45722 :   (or (timerp timer) (signal 'wrong-type-argument (list #'timerp timer))))
      57             : 
      58             : (defun timer--time-setter (timer time)
      59       15247 :   (timer--check timer)
      60       30494 :   (setf (timer--high-seconds timer) (pop time))
      61       15247 :   (let ((low time) (usecs 0) (psecs 0))
      62       15247 :     (when (consp time)
      63       30494 :       (setq low (pop time))
      64       15247 :       (when time
      65       30280 :         (setq usecs (pop time))
      66       15140 :         (when time
      67       15247 :           (setq psecs (car time)))))
      68       15247 :     (setf (timer--low-seconds timer) low)
      69       15247 :     (setf (timer--usecs timer) usecs)
      70       15247 :     (setf (timer--psecs timer) psecs)
      71       15247 :     time))
      72             : 
      73             : ;; Pseudo field `time'.
      74             : (defun timer--time (timer)
      75             :   (declare (gv-setter timer--time-setter))
      76       18761 :   (list (timer--high-seconds timer)
      77       18761 :         (timer--low-seconds timer)
      78       18761 :         (timer--usecs timer)
      79       18761 :         (timer--psecs timer)))
      80             : 
      81             : (defun timer-set-time (timer time &optional delta)
      82             :   "Set the trigger time of TIMER to TIME.
      83             : TIME must be in the internal format returned by, e.g., `current-time'.
      84             : If optional third argument DELTA is a positive number, make the timer
      85             : fire repeatedly that many seconds apart."
      86       15125 :   (setf (timer--time timer) time)
      87       15125 :   (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta))
      88       15125 :   timer)
      89             : 
      90             : (defun timer-set-idle-time (timer secs &optional repeat)
      91             :   ;; FIXME: Merge with timer-set-time.
      92             :   "Set the trigger idle time of TIMER to SECS.
      93             : SECS may be an integer, floating point number, or the internal
      94             : time format returned by, e.g., `current-idle-time'.
      95             : If optional third argument REPEAT is non-nil, make the timer
      96             : fire each time Emacs is idle for that many seconds."
      97         107 :   (setf (timer--time timer) (if (consp secs) secs (seconds-to-time secs)))
      98         107 :   (setf (timer--repeat-delay timer) repeat)
      99         107 :   timer)
     100             : 
     101             : (defun timer-next-integral-multiple-of-time (time secs)
     102             :   "Yield the next value after TIME that is an integral multiple of SECS.
     103             : More precisely, the next value, after TIME, that is an integral multiple
     104             : of SECS seconds since the epoch.  SECS may be a fraction."
     105           0 :   (let* ((trillion 1e12)
     106           0 :          (time-sec (+ (nth 1 time)
     107           0 :                       (* 65536.0 (nth 0 time))))
     108           0 :          (delta-sec (mod (- time-sec) secs))
     109           0 :          (next-sec (+ time-sec (ffloor delta-sec)))
     110           0 :          (next-sec-psec (ffloor (* trillion (mod delta-sec 1))))
     111           0 :          (sub-time-psec (+ (or (nth 3 time) 0)
     112           0 :                            (* 1e6 (nth 2 time))))
     113           0 :          (psec-diff (- sub-time-psec next-sec-psec)))
     114           0 :     (if (and (<= next-sec time-sec) (< 0 psec-diff))
     115           0 :         (setq next-sec-psec (+ sub-time-psec
     116           0 :                                (mod (- psec-diff) (* trillion secs)))))
     117           0 :     (setq next-sec (+ next-sec (floor next-sec-psec trillion)))
     118           0 :     (setq next-sec-psec (mod next-sec-psec trillion))
     119           0 :     (list (floor next-sec 65536)
     120           0 :           (floor (mod next-sec 65536))
     121           0 :           (floor next-sec-psec 1000000)
     122           0 :           (floor (mod next-sec-psec 1000000)))))
     123             : 
     124             : (defun timer-relative-time (time secs &optional usecs psecs)
     125             :   "Advance TIME by SECS seconds and optionally USECS microseconds
     126             : and PSECS picoseconds.  SECS may be either an integer or a
     127             : floating point number."
     128       15140 :   (let ((delta secs))
     129       15140 :     (if (or usecs psecs)
     130       15140 :         (setq delta (time-add delta (list 0 0 (or usecs 0) (or psecs 0)))))
     131       15140 :     (time-add time delta)))
     132             : 
     133             : (defun timer--time-less-p (t1 t2)
     134             :   "Say whether time value T1 is less than time value T2."
     135        1742 :   (time-less-p (timer--time t1) (timer--time t2)))
     136             : 
     137             : (defun timer-inc-time (timer secs &optional usecs psecs)
     138             :   "Increment the time set in TIMER by SECS seconds, USECS microseconds,
     139             : and PSECS picoseconds.  SECS may be a fraction.  If USECS or PSECS are
     140             : omitted, they are treated as zero."
     141          15 :   (setf (timer--time timer)
     142          15 :         (timer-relative-time (timer--time timer) secs usecs psecs)))
     143             : 
     144             : (defun timer-set-time-with-usecs (timer time usecs &optional delta)
     145             :   "Set the trigger time of TIMER to TIME plus USECS.
     146             : TIME must be in the internal format returned by, e.g., `current-time'.
     147             : The microsecond count from TIME is ignored, and USECS is used instead.
     148             : If optional fourth argument DELTA is a positive number, make the timer
     149             : fire repeatedly that many seconds apart."
     150             :   (declare (obsolete "use `timer-set-time' and `timer-inc-time' instead."
     151             :                      "22.1"))
     152           0 :   (setf (timer--time timer) time)
     153           0 :   (setf (timer--usecs timer) usecs)
     154           0 :   (setf (timer--psecs timer) 0)
     155           0 :   (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta))
     156           0 :   timer)
     157             : 
     158             : (defun timer-set-function (timer function &optional args)
     159             :   "Make TIMER call FUNCTION with optional ARGS when triggering."
     160       15232 :   (timer--check timer)
     161       15232 :   (setf (timer--function timer) function)
     162       15232 :   (setf (timer--args timer) args)
     163       15232 :   timer)
     164             : 
     165             : (defun timer--activate (timer &optional triggered-p reuse-cell idle)
     166       15244 :   (if (and (timerp timer)
     167       15244 :            (integerp (timer--high-seconds timer))
     168       15244 :            (integerp (timer--low-seconds timer))
     169       15244 :            (integerp (timer--usecs timer))
     170       15244 :            (integerp (timer--psecs timer))
     171       15244 :            (timer--function timer))
     172       15244 :       (let ((timers (if idle timer-idle-list timer-list))
     173             :             last)
     174             :         ;; Skip all timers to trigger before the new one.
     175       15810 :         (while (and timers (timer--time-less-p (car timers) timer))
     176         566 :           (setq last timers
     177       15244 :                 timers (cdr timers)))
     178       15244 :         (if reuse-cell
     179          12 :             (progn
     180          12 :               (setcar reuse-cell timer)
     181          12 :               (setcdr reuse-cell timers))
     182       15244 :           (setq reuse-cell (cons timer timers)))
     183             :         ;; Insert new timer after last which possibly means in front of queue.
     184       15244 :         (setf (cond (last (cdr last))
     185       14688 :                     (idle timer-idle-list)
     186       29162 :                     (t    timer-list))
     187       15244 :               reuse-cell)
     188       15244 :         (setf (timer--triggered timer) triggered-p)
     189       15244 :         (setf (timer--idle-delay timer) idle)
     190       15244 :         nil)
     191       15244 :     (error "Invalid or uninitialized timer")))
     192             : 
     193             : (defun timer-activate (timer &optional triggered-p reuse-cell)
     194             :   "Insert TIMER into `timer-list'.
     195             : If TRIGGERED-P is t, make TIMER inactive (put it on the list, but
     196             : mark it as already triggered).  To remove it, use `cancel-timer'.
     197             : 
     198             : REUSE-CELL, if non-nil, is a cons cell to reuse when inserting
     199             : TIMER into `timer-list' (usually a cell removed from that list by
     200             : `cancel-timer-internal'; using this reduces consing for repeat
     201             : timers).  If nil, allocate a new cell."
     202       15137 :   (timer--activate timer triggered-p reuse-cell nil))
     203             : 
     204             : (defun timer-activate-when-idle (timer &optional dont-wait reuse-cell)
     205             :   "Insert TIMER into `timer-idle-list'.
     206             : This arranges to activate TIMER whenever Emacs is next idle.
     207             : If optional argument DONT-WAIT is non-nil, set TIMER to activate
     208             : immediately \(see below), or at the right time, if Emacs is
     209             : already idle.
     210             : 
     211             : REUSE-CELL, if non-nil, is a cons cell to reuse when inserting
     212             : TIMER into `timer-idle-list' (usually a cell removed from that
     213             : list by `cancel-timer-internal'; using this reduces consing for
     214             : repeat timers).  If nil, allocate a new cell.
     215             : 
     216             : Using non-nil DONT-WAIT is not recommended when activating an
     217             : idle timer from an idle timer handler, if the timer being
     218             : activated has an idleness time that is smaller or equal to
     219             : the time of the current timer.  That's because the activated
     220             : timer will fire right away."
     221         107 :   (timer--activate timer (not dont-wait) reuse-cell 'idle))
     222             : 
     223             : (defalias 'disable-timeout 'cancel-timer)
     224             : 
     225             : (defun cancel-timer (timer)
     226             :   "Remove TIMER from the list of active timers."
     227       15230 :   (timer--check timer)
     228       15230 :   (setq timer-list (delq timer timer-list))
     229       15230 :   (setq timer-idle-list (delq timer timer-idle-list))
     230             :   nil)
     231             : 
     232             : (defun cancel-timer-internal (timer)
     233             :   "Remove TIMER from the list of active timers or idle timers.
     234             : Only to be used in this file.  It returns the cons cell
     235             : that was removed from the timer list."
     236          13 :   (let ((cell1 (memq timer timer-list))
     237          13 :         (cell2 (memq timer timer-idle-list)))
     238          13 :     (if cell1
     239          13 :         (setq timer-list (delq timer timer-list)))
     240          13 :     (if cell2
     241          13 :         (setq timer-idle-list (delq timer timer-idle-list)))
     242          13 :     (or cell1 cell2)))
     243             : 
     244             : (defun cancel-function-timers (function)
     245             :   "Cancel all timers which would run FUNCTION.
     246             : This affects ordinary timers such as are scheduled by `run-at-time',
     247             : and idle timers such as are scheduled by `run-with-idle-timer'."
     248             :   (interactive "aCancel timers of function: ")
     249           0 :   (dolist (timer timer-list)
     250           0 :     (if (eq (timer--function timer) function)
     251           0 :         (setq timer-list (delq timer timer-list))))
     252           0 :   (dolist (timer timer-idle-list)
     253           0 :     (if (eq (timer--function timer) function)
     254           0 :         (setq timer-idle-list (delq timer timer-idle-list)))))
     255             : 
     256             : ;; Record the last few events, for debugging.
     257             : (defvar timer-event-last nil
     258             :   "Last timer that was run.")
     259             : (defvar timer-event-last-1 nil
     260             :   "Next-to-last timer that was run.")
     261             : (defvar timer-event-last-2 nil
     262             :   "Third-to-last timer that was run.")
     263             : 
     264             : (defcustom timer-max-repeats 10
     265             :   "Maximum number of times to repeat a timer, if many repeats are delayed.
     266             : Timer invocations can be delayed because Emacs is suspended or busy,
     267             : or because the system's time changes.  If such an occurrence makes it
     268             : appear that many invocations are overdue, this variable controls
     269             : how many will really happen."
     270             :   :type 'integer
     271             :   :group 'internal)
     272             : 
     273             : (defun timer-until (timer time)
     274             :   "Calculate number of seconds from when TIMER will run, until TIME.
     275             : TIMER is a timer, and stands for the time when its next repeat is scheduled.
     276             : TIME is a time-list."
     277          15 :   (- (float-time time) (float-time (timer--time timer))))
     278             : 
     279             : (defun timer-event-handler (timer)
     280             :   "Call the handler for the timer TIMER.
     281             : This function is called, by name, directly by the C code."
     282          13 :   (setq timer-event-last-2 timer-event-last-1)
     283          13 :   (setq timer-event-last-1 timer-event-last)
     284          13 :   (setq timer-event-last timer)
     285          13 :   (let ((inhibit-quit t))
     286          13 :     (timer--check timer)
     287          13 :     (let ((retrigger nil)
     288             :           (cell
     289             :            ;; Delete from queue.  Record the cons cell that was used.
     290          13 :            (cancel-timer-internal timer)))
     291             :       ;; If `cell' is nil, it means the timer was already canceled, so we
     292             :       ;; shouldn't be running it at all.  This can happen for example with the
     293             :       ;; following scenario (bug#17392):
     294             :       ;; - we run timers, starting with A (and remembering the rest as (B C)).
     295             :       ;; - A runs and a does a sit-for.
     296             :       ;; - during sit-for we run timer D which cancels timer B.
     297             :       ;; - timer A finally finishes, so we move on to timers B and C.
     298          13 :       (when cell
     299             :         ;; Re-schedule if requested.
     300          13 :         (if (timer--repeat-delay timer)
     301          12 :             (if (timer--idle-delay timer)
     302           0 :                 (timer-activate-when-idle timer nil cell)
     303          12 :               (timer-inc-time timer (timer--repeat-delay timer) 0)
     304             :               ;; If real time has jumped forward,
     305             :               ;; perhaps because Emacs was suspended for a long time,
     306             :               ;; limit how many times things get repeated.
     307          12 :               (if (and (numberp timer-max-repeats)
     308          12 :                        (< 0 (timer-until timer nil)))
     309           3 :                   (let ((repeats (/ (timer-until timer nil)
     310           3 :                                     (timer--repeat-delay timer))))
     311           3 :                     (if (> repeats timer-max-repeats)
     312           3 :                         (timer-inc-time timer (* (timer--repeat-delay timer)
     313          12 :                                                  repeats)))))
     314             :               ;; Place it back on the timer-list before running
     315             :               ;; timer--function, so it can cancel-timer itself.
     316          12 :               (timer-activate timer t cell)
     317          13 :               (setq retrigger t)))
     318             :         ;; Run handler.
     319          13 :         (condition-case-unless-debug err
     320             :             ;; Timer functions should not change the current buffer.
     321             :             ;; If they do, all kinds of nasty surprises can happen,
     322             :             ;; and it can be hellish to track down their source.
     323          13 :             (save-current-buffer
     324          13 :               (apply (timer--function timer) (timer--args timer)))
     325           0 :           (error (message "Error running timer%s: %S"
     326           0 :                           (if (symbolp (timer--function timer))
     327           0 :                               (format-message " `%s'" (timer--function timer))
     328           0 :                             "")
     329          13 :                           err)))
     330          13 :         (when (and retrigger
     331             :                    ;; If the timer's been canceled, don't "retrigger" it
     332             :                    ;; since it might still be in the copy of timer-list kept
     333             :                    ;; by keyboard.c:timer_check (bug#14156).
     334          13 :                    (memq timer timer-list))
     335          13 :           (setf (timer--triggered timer) nil))))))
     336             : 
     337             : ;; This function is incompatible with the one in levents.el.
     338             : (defun timeout-event-p (event)
     339             :   "Non-nil if EVENT is a timeout event."
     340           0 :   (and (listp event) (eq (car event) 'timer-event)))
     341             : 
     342             : 
     343             : (declare-function diary-entry-time "diary-lib" (s))
     344             : 
     345             : (defun run-at-time (time repeat function &rest args)
     346             :   "Perform an action at time TIME.
     347             : Repeat the action every REPEAT seconds, if REPEAT is non-nil.
     348             : REPEAT may be an integer or floating point number.
     349             : TIME should be one of:
     350             : - a string giving today's time like \"11:23pm\"
     351             :   (the acceptable formats are HHMM, H:MM, HH:MM, HHam, HHAM,
     352             :   HHpm, HHPM, HH:MMam, HH:MMAM, HH:MMpm, or HH:MMPM;
     353             :   a period `.' can be used instead of a colon `:' to separate
     354             :   the hour and minute parts);
     355             : - a string giving a relative time like \"90\" or \"2 hours 35 minutes\"
     356             :   (the acceptable forms are a number of seconds without units
     357             :   or some combination of values using units in `timer-duration-words');
     358             : - nil, meaning now;
     359             : - a number of seconds from now;
     360             : - a value from `encode-time';
     361             : - or t (with non-nil REPEAT) meaning the next integral
     362             :   multiple of REPEAT.
     363             : 
     364             : The action is to call FUNCTION with arguments ARGS.
     365             : 
     366             : This function returns a timer object which you can use in
     367             : `cancel-timer'."
     368             :   (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
     369             : 
     370       15125 :   (or (null repeat)
     371           1 :       (and (numberp repeat) (< 0 repeat))
     372       15125 :       (error "Invalid repetition interval"))
     373             : 
     374             :   ;; Special case: nil means "now" and is useful when repeating.
     375       15125 :   (if (null time)
     376       15125 :       (setq time (current-time)))
     377             : 
     378             :   ;; Special case: t means the next integral multiple of REPEAT.
     379       15125 :   (if (and (eq time t) repeat)
     380       15125 :       (setq time (timer-next-integral-multiple-of-time (current-time) repeat)))
     381             : 
     382             :   ;; Handle numbers as relative times in seconds.
     383       15125 :   (if (numberp time)
     384       15125 :       (setq time (timer-relative-time nil time)))
     385             : 
     386             :   ;; Handle relative times like "2 hours 35 minutes"
     387       15125 :   (if (stringp time)
     388           0 :       (let ((secs (timer-duration time)))
     389           0 :         (if secs
     390       15125 :             (setq time (timer-relative-time nil secs)))))
     391             : 
     392             :   ;; Handle "11:23pm" and the like.  Interpret it as meaning today
     393             :   ;; which admittedly is rather stupid if we have passed that time
     394             :   ;; already.  (Though only Emacs hackers hack Emacs at that time.)
     395       15125 :   (if (stringp time)
     396           0 :       (progn
     397           0 :         (require 'diary-lib)
     398           0 :         (let ((hhmm (diary-entry-time time))
     399           0 :               (now (decode-time)))
     400           0 :           (if (>= hhmm 0)
     401           0 :               (setq time
     402           0 :                     (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now)
     403       15125 :                                  (nth 4 now) (nth 5 now) (nth 8 now)))))))
     404             : 
     405       15125 :   (or (consp time)
     406       15125 :       (error "Invalid time format"))
     407             : 
     408       15125 :   (let ((timer (timer-create)))
     409       15125 :     (timer-set-time timer time repeat)
     410       15125 :     (timer-set-function timer function args)
     411       15125 :     (timer-activate timer)
     412       15125 :     timer))
     413             : 
     414             : (defun run-with-timer (secs repeat function &rest args)
     415             :   "Perform an action after a delay of SECS seconds.
     416             : Repeat the action every REPEAT seconds, if REPEAT is non-nil.
     417             : SECS and REPEAT may be integers or floating point numbers.
     418             : The action is to call FUNCTION with arguments ARGS.
     419             : 
     420             : This function returns a timer object which you can use in `cancel-timer'."
     421             :   (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
     422       15122 :   (apply 'run-at-time secs repeat function args))
     423             : 
     424             : (defun add-timeout (secs function object &optional repeat)
     425             :   "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT.
     426             : If REPEAT is non-nil, repeat the timer every REPEAT seconds.
     427             : 
     428             : This function returns a timer object which you can use in `cancel-timer'.
     429             : This function is for compatibility; see also `run-with-timer'."
     430           0 :   (run-with-timer secs repeat function object))
     431             : 
     432             : (defun run-with-idle-timer (secs repeat function &rest args)
     433             :   "Perform an action the next time Emacs is idle for SECS seconds.
     434             : The action is to call FUNCTION with arguments ARGS.
     435             : SECS may be an integer, a floating point number, or the internal
     436             : time format returned by, e.g., `current-idle-time'.
     437             : If Emacs is currently idle, and has been idle for N seconds (N < SECS),
     438             : then it will call FUNCTION in SECS - N seconds from now.  Using
     439             : SECS <= N is not recommended if this function is invoked from an idle
     440             : timer, because FUNCTION will then be called immediately.
     441             : 
     442             : If REPEAT is non-nil, do the action each time Emacs has been idle for
     443             : exactly SECS seconds (that is, only once for each time Emacs becomes idle).
     444             : 
     445             : This function returns a timer object which you can use in `cancel-timer'."
     446             :   (interactive
     447           0 :    (list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
     448           0 :          (y-or-n-p "Repeat each time Emacs is idle? ")
     449           0 :          (intern (completing-read "Function: " obarray 'fboundp t))))
     450         107 :   (let ((timer (timer-create)))
     451         107 :     (timer-set-function timer function args)
     452         107 :     (timer-set-idle-time timer secs repeat)
     453         107 :     (timer-activate-when-idle timer t)
     454         107 :     timer))
     455             : 
     456             : (defvar with-timeout-timers nil
     457             :   "List of all timers used by currently pending `with-timeout' calls.")
     458             : 
     459             : (defmacro with-timeout (list &rest body)
     460             :   "Run BODY, but if it doesn't finish in SECONDS seconds, give up.
     461             : If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
     462             : The timeout is checked whenever Emacs waits for some kind of external
     463             : event (such as keyboard input, input from subprocesses, or a certain time);
     464             : if the program loops without waiting in any way, the timeout will not
     465             : be detected.
     466             : \n(fn (SECONDS TIMEOUT-FORMS...) BODY)"
     467             :   (declare (indent 1) (debug ((form body) body)))
     468          22 :   (let ((seconds (car list))
     469          22 :         (timeout-forms (cdr list))
     470          22 :         (timeout (make-symbol "timeout")))
     471          22 :     `(let ((-with-timeout-value-
     472          22 :             (catch ',timeout
     473             :               (let* ((-with-timeout-timer-
     474          22 :                       (run-with-timer ,seconds nil
     475          22 :                                       (lambda () (throw ',timeout ',timeout))))
     476             :                      (with-timeout-timers
     477             :                          (cons -with-timeout-timer- with-timeout-timers)))
     478             :                 (unwind-protect
     479          22 :                     (progn ,@body)
     480             :                   (cancel-timer -with-timeout-timer-))))))
     481             :        ;; It is tempting to avoid the `if' altogether and instead run
     482             :        ;; timeout-forms in the timer, just before throwing `timeout'.
     483             :        ;; But that would mean that timeout-forms are run in the deeper
     484             :        ;; dynamic context of the timer, with inhibit-quit set etc...
     485          22 :        (if (eq -with-timeout-value- ',timeout)
     486          22 :            (progn ,@timeout-forms)
     487          22 :          -with-timeout-value-))))
     488             : 
     489             : (defun with-timeout-suspend ()
     490             :   "Stop the clock for `with-timeout'.  Used by debuggers.
     491             : The idea is that the time you spend in the debugger should not
     492             : count against these timeouts.
     493             : 
     494             : The value is a list that the debugger can pass to `with-timeout-unsuspend'
     495             : when it exits, to make these timers start counting again."
     496           0 :   (mapcar (lambda (timer)
     497           0 :             (cancel-timer timer)
     498           0 :             (list timer (time-subtract (timer--time timer) nil)))
     499           0 :           with-timeout-timers))
     500             : 
     501             : (defun with-timeout-unsuspend (timer-spec-list)
     502             :   "Restart the clock for `with-timeout'.
     503             : The argument should be a value previously returned by `with-timeout-suspend'."
     504           0 :   (dolist (elt timer-spec-list)
     505           0 :     (let ((timer (car elt))
     506           0 :           (delay (cadr elt)))
     507           0 :       (timer-set-time timer (time-add nil delay))
     508           0 :       (timer-activate timer))))
     509             : 
     510             : (defun y-or-n-p-with-timeout (prompt seconds default-value)
     511             :   "Like (y-or-n-p PROMPT), with a timeout.
     512             : If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
     513           0 :   (with-timeout (seconds default-value)
     514           0 :     (y-or-n-p prompt)))
     515             : 
     516             : (defconst timer-duration-words
     517             :   (list (cons "microsec" 0.000001)
     518             :         (cons "microsecond" 0.000001)
     519             :         (cons "millisec" 0.001)
     520             :         (cons "millisecond" 0.001)
     521             :         (cons "sec" 1)
     522             :         (cons "second" 1)
     523             :         (cons "min" 60)
     524             :         (cons "minute" 60)
     525             :         (cons "hour" (* 60 60))
     526             :         (cons "day" (* 24 60 60))
     527             :         (cons "week" (* 7 24 60 60))
     528             :         (cons "fortnight" (* 14 24 60 60))
     529             :         (cons "month" (* 30 24 60 60))          ; Approximation
     530             :         (cons "year" (* 365.25 24 60 60)) ; Approximation
     531             :         )
     532             :   "Alist mapping temporal words to durations in seconds.")
     533             : 
     534             : (defun timer-duration (string)
     535             :   "Return number of seconds specified by STRING, or nil if parsing fails."
     536           0 :   (let ((secs 0)
     537             :         (start 0)
     538             :         (case-fold-search t))
     539           0 :     (while (string-match
     540             :             "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*"
     541           0 :             string start)
     542           0 :       (let ((count (if (match-beginning 1)
     543           0 :                        (string-to-number (match-string 1 string))
     544           0 :                      1))
     545           0 :             (itemsize (cdr (assoc (match-string 2 string)
     546           0 :                                   timer-duration-words))))
     547           0 :         (if itemsize
     548           0 :             (setq start (match-end 0)
     549           0 :                   secs (+ secs (* count itemsize)))
     550           0 :           (setq secs nil
     551           0 :                 start (length string)))))
     552           0 :     (if (= start (length string))
     553           0 :         secs
     554           0 :       (if (string-match-p "\\`[0-9.]+\\'" string)
     555           0 :           (string-to-number string)))))
     556             : 
     557             : (defun internal-timer-start-idle ()
     558             :   "Mark all idle-time timers as once again candidates for running."
     559           0 :   (dolist (timer timer-idle-list)
     560           0 :     (if (timerp timer) ;; FIXME: Why test?
     561           0 :         (setf (timer--triggered timer) nil))))
     562             : 
     563             : (provide 'timer)
     564             : 
     565             : ;;; timer.el ends here

Generated by: LCOV version 1.12