]> code.delx.au - gnu-emacs-elpa/blob - packages/timerfunctions/timerfunctions.el
Fix some quoting problems in doc strings
[gnu-emacs-elpa] / packages / timerfunctions / timerfunctions.el
1 ;;; timerfunctions.el --- Enhanced versions of some timer.el functions
2
3 ;; Copyright (C) 2000-2002, 2015 Free Software Foundation, Inc.
4
5 ;; Time-stamp: <2015-03-02 12:23:21 deego>
6 ;; Emacs Lisp Archive entry
7 ;; Filename: timerfunctions.el
8 ;; Author: Dave Goel <deego3@gmail.com>
9 ;; Version: 1.4.2
10 ;; Package-Requires: ((cl-lib "0.5"))
11 ;; Created: 2000/11/20
12 ;; Author's homepage: http://gnufans.net/~deego
13
14 ;; This file is NOT (yet) part of GNU Emacs.
15
16 ;; This is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; any later version.
20
21 ;; This is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28
29 ;; For latest version:
30 ;; Gives me a "Not found"!
31 ;;(defconst timerfunctions-home-page
32 ;; "http://gnufans.net/~deego/emacspub/timerfunctions")
33
34 ;;; Commentary:
35
36 ;; See also: midnight.el (part of Emacs), timer.el
37
38 ;;; Code:
39
40 (eval-when-compile (require 'cl-lib))
41
42 (defvar timerfunctions-version "1.4.2")
43
44
45 ;;; New features:
46 (defconst timerfunctions-new-features
47 "New since last posting: Changed the syntax of `tf-with-timeout' and
48 provided a `tf-with-timeout-check'.")
49
50 (defun timerfunctions-new-features ()
51 "Provides electric help from variable `timerfunctions-new-features'."
52 (interactive)
53 (with-electric-help
54 (lambda () (insert timerfunctions-new-features) nil) "*doc*"))
55
56
57 (defconst timerfunctions-introduction
58 "timerfunctions.el contains some “enhanced” versions of a few timer.el
59 functions. It is also used by vel.el, idledo.el etc.
60
61 Suppose you want Emacs to run an action every REDOSECS for
62 _as_long_as Emacs remains idle. `tf-run-with-idle-timer' allows that.
63
64 `tf-with-timeout' is a generalized with-timeout where you can inhibit
65 breaks within parts of the body that you want.
66
67 QUICKSTART:
68 Place this file somewhere in your load-path, and add the
69 following to your ~/.emacs: (load \"timerfunctions.el\")
70 "
71 )
72
73 ;;;###autoload
74 (defun timerfunctions-introduction ()
75 "Provides electric help from variable `timerfunctions-introduction'."
76 (interactive)
77 (with-electric-help
78 '(lambda () (insert timerfunctions-introduction) nil) "*doc*"))
79
80 ;;; Real Code:
81
82
83 ;;;###autoload
84 (defun tf-time-difference (timeplus timesub)
85 "Return the time in seconds elaspsed from TIMESUB to TIMEPLUS.
86
87 Conceptually: \(- TIMEPLUS TIMESUB \)."
88 (+ (* (expt 2 16) (- (car timeplus) (car timesub)))
89 (- (cadr timeplus) (cadr timesub)))
90 )
91
92
93 ;;;###autoload
94 (defun tf-run-with-idle-timer (secs repeat redosecs redorepeat includeruntime function &rest args)
95 "Similar to `run-with-idle-timer', except that provides more options.
96
97 Args are SECS, REPEAT, REDOSECS, REDOREPEAT, INCLUDERUNTIME,
98 FUNCTION and &rest ARGS.
99
100 Similar to `run-with-idle-timer', but provides more options.
101 Suppose you want Emacs to run an action every REDOSECS for as
102 long as Emacs remains idle. Emacs' `run-with-idle-timer' will
103 perform the action exactly once every time Emacs goes idle. This
104 funciton, on the other hand, will allow
105 you to keep performing an action as long as Emacs remains idle.
106
107 SECS is the number of seconds to wait once Emacs has first gone
108 idle. It can really be any expression whose at runtime yields a
109 number. Note that the way `run-with-idle-timer' is defined, SECS will
110 unfortunately be evalled immediately after you call this function, but
111 redosecs will be *every* time Emacs *remains* idle..yay..
112
113 If REDOREPEAT is non-nil, the action is repeated as long Emacs remains
114 idle. REDOSECS is the number of additional seconds (after the action
115 has been done) to wait if Emacs remains idle before performing the
116 action again. Again, redosecs does not have to be a number, it can be
117 any expression whose eval yields to a number...
118
119 If INCLUDERUNTIME is non-nil, REDOSECS is the number of
120 additional seconds to wait after the action has been invoked (not
121 finished).
122
123 If REPEAT is nonnil, the entire cycle is repeated every time Emacs
124 next goes idle.. (as in the default `run-with-idle-timer'."
125 (apply 'run-with-idle-timer
126 (eval secs) repeat 'tf-run-while-idle
127 redosecs redorepeat includeruntime
128 function args)
129 )
130
131
132 (defun tf-run-while-idle (redosecs redorepeat includeruntime function &rest args)
133 "A simplified version of `tf-run-with-idle-timer'.
134
135 Runs FUNCTION with ARGS and optionally repeats if emacs remains idle.
136 Probably is of no use unless used in programs.
137 If REDOREPEAT is non-nil, the function is repeated periodically every
138 REDOSECS as long as emacs remains idle. By default, emacs waits
139 REDOSECS *after* the function is done executing to repeat. If you want
140 the execution-time to count towards REDOSECS, make INCLUDERUNTIME
141 non-nil.
142 SECS and REDOSECS can be any expressions that eval at runtime to
143 numbers. In particular, of course, they can simply be numbers."
144 (if (not includeruntime)
145 (progn
146 (apply function args)
147 (if redorepeat
148 (while (sit-for (eval redosecs))
149 (apply function args))))
150 (progn
151 (let ((before-time (current-time)))
152 (apply function args)
153 (if redorepeat
154 (while (sit-for (-
155 (eval redosecs)
156 (tf-time-difference (current-time)
157 before-time)))
158 (setq before-time (current-time))
159 (apply function args))))))
160 )
161
162
163 ;;;====================================================
164 ;;;TESTS FOLLOW
165 (defun tf-test-display-time-internal ()
166 "A test function."
167 (interactive)
168 (let ((thisbuffer (buffer-name)))
169 (switch-to-buffer-other-window "*scratch*")
170 (goto-char (point-max))
171 (insert (concat "\n" (format "%S" (cadr (current-time)))))
172 (recenter)
173 (switch-to-buffer-other-window thisbuffer))
174 )
175
176
177 (defun tf-test-idle-timer ()
178 "A test function.
179
180 Run this and watch Play around with the options. If you run it,
181 you may have to exit your Emacs session to restore normal Emacs,
182 unless you clean things up carefully!"
183
184 (interactive)
185 (tf-run-with-idle-timer
186 1 t 3 t nil 'tf-test-display-time-internal)
187 )
188
189
190
191
192
193 (defun tf-test-timeout ()
194 "Bad count should be zero."
195 (interactive)
196 (let ((inhi nil) (goodcount 0) (badcount 0) (ctr 0) (a 1) (b 2)
197 (mytag nil)
198 (myvar nil)
199 )
200 (cl-loop
201 for ctr from 0 to 10 do
202 (message "ctr=%S" ctr)
203 (tf-with-timeout 'inhi 'mytah 'myvar
204 (0.3 nil)
205 (cl-loop for i from 0 to 100000 do
206 (message "ctr=%S, i=%S" ctr i)
207 (setq inhi t)
208 (setq a (random 100))
209 (sleep-for 0.1)
210 (setq b a)
211 (setq inhi nil)
212 (sleep-for 0.02)
213 ))
214 (if (equal b a) (cl-incf goodcount) (cl-incf badcount)))
215 (message "Goodcount: %S; badcount: %S" goodcount badcount)))
216
217
218
219 (defun tf-test-timeout-complex ()
220 "Should return a value of 20000 for a."
221
222 (interactive)
223 (let ((inhi t) (goodcount 0) (badcount 0) (ctr 0) (a 1) (b 2)
224 (mytag nil)
225 (myvar nil)
226 )
227 (setq a 0)
228 (message "ctr=%S" ctr)
229 (tf-with-timeout
230 'inhi 'mytag 'myvar
231 (0.1 nil)
232 (cl-loop for i from 0 to 10000 do
233 (message "first loop. ctr=%S, i=%S, " ctr i)
234 (cl-incf a))
235 (message "initial loop ends here.")
236 ;; no throw here because loop prohibited.
237 (tf-with-timeout-check 'inhi 'mytag 'myvar)
238 ;; this shouldn't help either
239 (sit-for 0.3)
240
241 (cl-loop for i from 0 to 10000 do
242 (message "second loop. i=%S" i)
243 (cl-incf a))
244 (message "second loop ends here.")
245 (setq inhi nil)
246 ;; this should throw.
247 (tf-with-timeout-check 'inhi 'mytag 'myvar)
248 ;; this should NOT be needed.
249 ;;(sit-for 0.2)
250 ;; this loop should never take place.
251 (cl-loop for i from 0 to 1000 do
252 (message "third loop, i=%S" i)
253 (cl-incf a))
254 (message "third loop ends here."))
255 (message "%S" a)
256 a))
257
258
259
260 (defvar tf-internal-var-recenter 1)
261
262 (defun tf-test-internal-recenter-toggle ()
263 "A test function."
264 (interactive)
265 (recenter 1)
266 (setq tf-internal-var-recenter (- 0 tf-internal-var-recenter)))
267
268 (defun tf-test-example-timer-recenter ()
269 "An example timer.
270 Changes the screen display every 3 seconds, thus ensuring that you
271 don't time out of ssh sessions."
272 (interactive)
273 (tf-run-with-idle-timer 3 t 3 t nil 'tf-test-internal-recenter-toggle))
274
275
276
277
278 (defun tf-todo-wait-until-idle (&optional secs)
279 "This function is not functional yet.
280
281 Waits until idle. Arguments are SECS.
282 Will help run processes in background. This function will NOT create
283 a timer. Will simply use `sit-for'."
284 (if (null secs)
285 (setq secs 1))
286 (while (not (sit-for secs))
287 (sit-for 1))
288 (message "tf-todo-wait-until-idle DONE WAITING!")
289 )
290
291
292 ;;;Tue Jan 23 17:38:44 2001
293 ;; FIXME: Use `with-demoted-errors' instead.
294 (defmacro tf-ignore-errors (&rest body)
295 "Ignore errors in BODY, but loudly."
296 (let ((err (make-symbol "err")))
297 `(condition-case ,err (progn ,@body)
298 (error (message "IGNORED ERROR: %s" (error-message-string ,err))))))
299
300
301 (defvar tf-with-timeout-repeat-sec 0.01
302 "Interval between checks for inhibitedness.
303
304 If the initial timeout fails because of inhibitedness, we shall
305 check every `tf-with-timeout-repeat-sec' seconds to see if we are
306 uninhibited, yet. This variable is customizable.")
307
308
309 (defun tf-with-timeout-handler-internal (tag timedoutvar inhibitp)
310 "Internal function.
311 Arguments are TAG, TIMEDOUTVAR, and INHIBITP."
312 (set timedoutvar t)
313 ;;(tf-with-timeout-check tag timedoutvar inhibitp)
314 ;; which is equivalent to:
315 (unless (eval inhibitp)
316 (tf-ignore-errors (throw tag 'timeout)))
317 )
318
319 (defun tf-with-timeout-check (inhibitp tag timedoutvar)
320 "Internal function.
321 Check whether timeout has actually reached.
322 We need this step because this function might be called by the
323 user as well. Arguments are INHIBITP, TAG and TIMEDOUTVAR."
324 (when (eval timedoutvar)
325 (unless (eval inhibitp)
326 (tf-ignore-errors (throw tag 'timeout)))))
327
328
329
330 (defvar tf-tag-tmpvar nil)
331
332 (defmacro tf-catch (tag &rest body)
333 "Catch a TAG in BODY."
334 `(let
335 ;; unquote the tag here..
336 ((,(cadr tag) 'tf-catch))
337 (catch ,tag
338 ,@body)))
339
340 (defmacro tf-throw (tag value)
341 "Throw a TAG with value VALUE."
342 `(when (eql (eval ,tag) 'tf-catch)
343 (throw ,tag value)))
344
345
346 ;;;###autoload
347 (defmacro tf-with-timeout (inhibitp timertag timedoutvar tlist &rest body)
348 "Like `with-timeout' but with support for unbreakable code.
349
350 Provides ability to inhibit timeout during parts of the body.
351 Note that most of the time, you may not need this functionality
352 at all unless you want to be very “clean” about things---you
353 could get by with the regular with-timeout and not using
354 sit-for's in the body. Or with the regular with-timeout and
355 using unwind-protect.
356
357
358 A judicious use of `unwind-protect' may seem to alleviate the
359 need for this function. This function, however, provides
360 additional flexibility in that the inhibitedness can be altered
361 at run-time depending on various conditions.
362
363
364 Run BODY, but if it doesn't finish in SECONDS seconds, give up.
365 If we give up, we run the TIMEOUT-FORMS which are contained in TLIST
366 and return the value of the last one.
367 The call should look like:
368 (tf-with-timeout quoted-expr (SECONDS TIMEOUT-FORMS...) BODY...)
369
370 The timeout is checked whenever Emacs waits for some kind of external
371 event \(such as keyboard input, input from subprocesses, or a certain time);
372 if the program loops without waiting in any way, the timeout will not
373 be detected. Furthermore:
374
375 During the execution of the body, we SHALL NOT time out when INHIBITP
376 evals to non-nil. Thus, for example, you might initially setq a
377 variable my-var as nil, supply inhibitp as `my-var', and then you may
378 setq my-var to t or nil within the body of tf-with-timeout to enable
379 or disable timeout. The best use of this functionality is to setq
380 inhibitp to t when during parts of loops where you do not want the
381 body broken within certain parts of the loop. (Of course, if that
382 part of the loop does not contain any sit-for's or read's then you
383 don't have to worry about this in the first place..)
384
385
386 Again, Do not forget to bind my-var to some value before attempting to use this
387 tf-with-timeout :)
388
389 Here's an example:
390
391
392 (let ((myinhibit t))
393 (tf-with-timeout \\='myinhibit \\='mytag \\='mytimedoutvar
394 (2 2)
395 (setq a nil)
396 (setq b nil)
397 (sit-for 4)
398 (setq a 4)
399 (setq myinhibit nil)
400 (sit-for 2)
401 (setq b 5)
402 ))
403
404
405 The above example requests a timeout within 2 seconds. However, the
406 timeout can takes place only when myinhibit is set to nil,
407 which becomes true after about 4 seconds. Thus, after the execution of the
408 body, a has the value 4, but b has the value nil.
409
410 See `tf-test-timeout' for another example.
411
412 Important Note: If the body of a loop tends to stay in a timeout
413 inhibited region for most of the time, then make sure that the timeout
414 enabled region atleast spans about 0.02 seconds.. thus, use (sleep-for
415 0.02) if needed.. this is because we check every 0.01 seconds if an
416 uninhibited timeout condition has been satisfied.
417
418 But perhaps you do not want to include (sleep-for 0.02) because that
419 wastes precious cpu time. Simple, don't include it, just after a long
420 inhibited body, you can include a timeout check within the body
421 instead of (sleep-for 0.02):
422 (tf-with-timeout-check \\='mytag \\='mytimedoutvar \\='myinhibitp)
423
424 Moreover, if that is the main check you rely on, you it perhaps makes
425 sense to increase the value of tf-with-timeout-repeat-sec, so that
426 your cpu cycles are not wasted every 0.01 sec. See the doc of that
427 variable for more.
428
429 TIMERTAG should be a quoted symbol, also we WILL set that symbol to t
430 during the execution of these forms.
431
432 TIMEDOUTVAR is the variable that times out."
433 (let ((seconds (car tlist))
434 (timeout-forms (cdr tlist)))
435 `(let (
436 ;;(with-timeout-tag (cons nil nil))
437 with-timeout-value with-timeout-timer)
438 (set ,timedoutvar nil)
439 (if (catch ,timertag
440 (progn
441 (setq with-timeout-timer
442 (run-with-timer ,seconds tf-with-timeout-repeat-sec
443 'tf-with-timeout-handler-internal
444 ,timertag ,timedoutvar
445 ,inhibitp))
446 (setq with-timeout-value (progn ,@body))
447 nil))
448 (progn ,@timeout-forms)
449 (cancel-timer with-timeout-timer)
450 with-timeout-value))))
451
452
453 (provide 'timerfunctions)
454 ;;; timerfunctions.el ends here