(strokes-unload-hook): Fix previous change.
[bpt/emacs.git] / lisp / type-break.el
CommitLineData
be8d412c 1;;; type-break.el --- encourage rests from typing at appropriate intervals
458401b6 2
2c8155f7 3;; Copyright (C) 1994, 95, 97, 2000, 2004 Free Software Foundation, Inc.
99c0333b 4
f6cafb3e
NF
5;; Author: Noah Friedman
6;; Maintainer: Noah Friedman <friedman@splode.com>
99c0333b 7;; Keywords: extensions, timers
846f6dd9 8;; Status: Works in GNU Emacs 19.25 or later, some versions of XEmacs
99c0333b 9;; Created: 1994-07-13
846f6dd9 10
99c0333b
NF
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 2, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b4dc9e6a
NF
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
458401b6
NF
27
28;;; Commentary:
be8d412c 29
b4dc9e6a
NF
30;; The docstring for the function `type-break-mode' summarizes most of the
31;; details of the interface.
be8d412c 32
27cd478d 33;; This package relies on the assumption that you live entirely in Emacs,
b4dc9e6a 34;; as the author does. If that's not the case for you (e.g. you often
27cd478d
EZ
35;; suspend Emacs or work in other windows) then this won't help very much;
36;; it will depend on just how often you switch back to Emacs. At the very
b4dc9e6a
NF
37;; least, you will want to turn off the keystroke thresholds and rest
38;; interval tracking.
be8d412c 39
846f6dd9
NF
40;; If you prefer not to be queried about taking breaks, but instead just
41;; want to be reminded, do the following:
42;;
43;; (setq type-break-query-mode nil)
44;;
45;; Or call the command `type-break-query-mode' with a negative prefix
46;; argument.
47
48;; If you find echo area messages annoying and would prefer to see messages
49;; in the mode line instead, do M-x type-break-mode-line-message-mode
50;; or set the variable of the same name to `t'.
b4dc9e6a
NF
51
52;; This program can truly cons up a storm because of all the calls to
53;; `current-time' (which always returns 3 fresh conses). I'm dismayed by
54;; this, but I think the health of my hands is far more important than a
55;; few pages of virtual memory.
56
846f6dd9
NF
57;; This program has no hope of working in Emacs 18.
58
b4dc9e6a
NF
59;; This package was inspired by Roland McGrath's hanoi-break.el.
60;; Several people contributed feedback and ideas, including
5762abec 61;; Roland McGrath <roland@gnu.org>
ad953485 62;; Kleanthes Koniaris <kgk@koniaris.com>
5762abec 63;; Mark Ashton <mpashton@gnu.org>
b4dc9e6a 64;; Matt Wilding <wilding@cli.com>
846f6dd9 65;; Robert S. Boyer <boyer@cs.utexas.edu>
be8d412c 66
458401b6
NF
67;;; Code:
68
69\f
104221a0
SE
70(defgroup type-break nil
71 "Encourage the user to take a rest from typing at suitable intervals."
72 :prefix "type-break"
73 :group 'keyboard)
74
622aca7c 75;;;###autoload
104221a0 76(defcustom type-break-mode nil
511a0719 77 "Toggle typing break mode.
104221a0 78See the docstring for the `type-break-mode' command for more information.
4de26885
DL
79Setting this variable directly does not take effect;
80use either \\[customize] or the function `type-break-mode'."
104221a0
SE
81 :set (lambda (symbol value)
82 (type-break-mode (if value 1 -1)))
e18b0c51 83 :initialize 'custom-initialize-default
104221a0
SE
84 :type 'boolean
85 :group 'type-break
e18b0c51 86 :require 'type-break)
4cf64c15
NF
87
88;;;###autoload
104221a0
SE
89(defcustom type-break-interval (* 60 60)
90 "*Number of seconds between scheduled typing breaks."
91 :type 'integer
92 :group 'type-break)
458401b6 93
be8d412c 94;;;###autoload
104221a0 95(defcustom type-break-good-rest-interval (/ type-break-interval 6)
be8d412c
NF
96 "*Number of seconds of idle time considered to be an adequate typing rest.
97
27cd478d 98When this variable is non-nil, Emacs checks the idle time between
cc669dd8 99keystrokes. If this idle time is long enough to be considered a \"good\"
be8d412c
NF
100rest from typing, then the next typing break is simply rescheduled for later.
101
cc669dd8 102If a break is interrupted before this much time elapses, the user will be
104221a0
SE
103asked whether or not really to interrupt the break."
104 :type 'integer
105 :group 'type-break)
be8d412c 106
27cd478d
EZ
107;;;###autoload
108(defcustom type-break-good-break-interval nil
109 "*Number of seconds considered to be an adequate explicit typing rest.
110
111When this variable is non-nil, its value is considered to be a \"good\"
112length (in seconds) for a break initiated by the command `type-break',
113overriding `type-break-good-rest-interval'. This provides querying of
114break interruptions when `type-break-good-rest-interval' is nil."
115 :type 'integer
116 :group 'type-break)
117
458401b6 118;;;###autoload
104221a0 119(defcustom type-break-keystroke-threshold
cc669dd8 120 ;; Assuming typing speed is 35wpm (on the average, do you really
f486195c
NF
121 ;; type more than that in a minute? I spend a lot of time reading mail
122 ;; and simply studying code in buffers) and average word length is
4cf64c15
NF
123 ;; about 5 letters, default upper threshold to the average number of
124 ;; keystrokes one is likely to type in a break interval. That way if the
125 ;; user goes through a furious burst of typing activity, cause a typing
126 ;; break to be required sooner than originally scheduled.
f486195c 127 ;; Conversely, the minimum threshold should be about a fifth of this.
cc669dd8 128 (let* ((wpm 35)
4cf64c15
NF
129 (avg-word-length 5)
130 (upper (* wpm avg-word-length (/ type-break-interval 60)))
f486195c 131 (lower (/ upper 5)))
4cf64c15
NF
132 (cons lower upper))
133 "*Upper and lower bound on number of keystrokes for considering typing break.
104221a0 134This structure is a pair of numbers (MIN . MAX).
4cf64c15 135
be8d412c
NF
136The first number is the minimum number of keystrokes that must have been
137entered since the last typing break before considering another one, even if
138the scheduled time has elapsed; the break is simply rescheduled until later
139if the minimum threshold hasn't been reached. If this first value is nil,
140then there is no minimum threshold; as soon as the scheduled time has
141elapsed, the user will always be queried.
4cf64c15
NF
142
143The second number is the maximum number of keystrokes that can be entered
144before a typing break is requested immediately, pre-empting the originally
be8d412c
NF
145scheduled break. If this second value is nil, then no pre-emptive breaks
146will occur; only scheduled ones will.
4cf64c15
NF
147
148Keys with bucky bits (shift, control, meta, etc) are counted as only one
b4dc9e6a
NF
149keystroke even though they really require multiple keys to generate them.
150
151The command `type-break-guesstimate-keystroke-threshold' can be used to
104221a0
SE
152guess a reasonably good pair of values for this variable."
153 :type 'sexp
154 :group 'type-break)
b4dc9e6a 155
104221a0 156(defcustom type-break-query-mode t
ee6258ff 157 "*Non-nil means ask whether or not to prompt user for breaks.
846f6dd9 158If so, call the function specified in the value of the variable
104221a0
SE
159`type-break-query-function' to do the asking."
160 :type 'boolean
161 :group 'type-break)
846f6dd9 162
ad953485
NF
163(defcustom type-break-query-function 'yes-or-no-p
164 "*Function to use for making query for a typing break.
b4dc9e6a
NF
165It should take a string as an argument, the prompt.
166Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
167
ee6258ff 168To avoid being queried at all, set `type-break-query-mode' to nil."
ad953485
NF
169 :type '(radio function
170 (function-item yes-or-no-p)
171 (function-item y-or-n-p))
172 :group 'type-break)
b4dc9e6a 173
104221a0 174(defcustom type-break-query-interval 60
b4dc9e6a
NF
175 "*Number of seconds between queries to take a break, if put off.
176The user will continue to be prompted at this interval until he or she
104221a0
SE
177finally submits to taking a typing break."
178 :type 'integer
179 :group 'type-break)
be8d412c 180
104221a0 181(defcustom type-break-time-warning-intervals '(300 120 60 30)
e7b20417
NF
182 "*List of time intervals for warnings about upcoming typing break.
183At each of the intervals (specified in seconds) away from a scheduled
104221a0
SE
184typing break, print a warning in the echo area."
185 :type '(repeat integer)
186 :group 'type-break)
e7b20417 187
104221a0 188(defcustom type-break-keystroke-warning-intervals '(300 200 100 50)
e7b20417
NF
189 "*List of keystroke measurements for warnings about upcoming typing break.
190At each of the intervals (specified in keystrokes) away from the upper
191keystroke threshold, print a warning in the echo area.
192If either this variable or the upper threshold is set, then no warnings
104221a0
SE
193will occur."
194 :type '(repeat integer)
195 :group 'type-break)
196
104221a0 197(defcustom type-break-warning-repeat 40
e7b20417
NF
198 "*Number of keystrokes for which warnings should be repeated.
199That is, for each of this many keystrokes the warning is redisplayed
104221a0
SE
200in the echo area to make sure it's really seen."
201 :type 'integer
202 :group 'type-break)
e7b20417 203
ad953485
NF
204(defcustom type-break-time-stamp-format "[%H:%M] "
205 "*Timestamp format used to prefix messages.
206Format specifiers are as used by `format-time-string'."
207 :type 'string
208 :group 'type-break)
209
104221a0 210(defcustom type-break-demo-functions
e7b20417 211 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi)
cc669dd8 212 "*List of functions to consider running as demos during typing breaks.
458401b6 213When a typing break begins, one of these functions is selected randomly
27cd478d 214to have Emacs do something interesting.
622aca7c 215
cc669dd8 216Any function in this list should start a demo which ceases as soon as a
104221a0
SE
217key is pressed."
218 :type '(repeat function)
219 :group 'type-break)
622aca7c 220
27cd478d
EZ
221(defcustom type-break-demo-boring-stats nil
222 "*Show word per minute and keystroke figures in the Boring demo."
223 :type 'boolean
224 :group 'type-break)
225
226(defcustom type-break-terse-messages nil
227 "*Use slightly terser messages."
228 :type 'boolean
229 :group 'type-break)
230
231(defcustom type-break-file-name (convert-standard-filename "~/.type-break")
232 "*Name of file used to save state across sessions."
233 :type 'file
234 :group 'type-break)
235
846f6dd9 236(defvar type-break-post-command-hook '(type-break-check)
27cd478d 237 "Hook run indirectly by `post-command-hook' for typing break functions.
b4dc9e6a
NF
238This is not really intended to be set by the user, but it's probably
239harmless to do so. Mainly it is used by various parts of the typing break
240program to delay actions until after the user has completed some command.
241It exists because `post-command-hook' itself is inaccessible while its
242functions are being run, and some type-break--related functions want to
243remove themselves after running.")
e7b20417 244
846f6dd9
NF
245\f
246;; Mode line frobs
247
104221a0 248(defcustom type-break-mode-line-message-mode nil
ee6258ff 249 "*Non-nil means put type-break related messages in the mode line.
846f6dd9
NF
250Otherwise, messages typically go in the echo area.
251
104221a0
SE
252See also `type-break-mode-line-format' and its members."
253 :type 'boolean
254 :group 'type-break)
846f6dd9
NF
255
256(defvar type-break-mode-line-format
257 '(type-break-mode-line-message-mode
258 (""
259 type-break-mode-line-break-message
260 type-break-mode-line-warning))
261 "*Format of messages in the mode line concerning typing breaks.")
262
263(defvar type-break-mode-line-break-message
264 '(type-break-mode-line-break-message-p
265 type-break-mode-line-break-string))
266
267(defvar type-break-mode-line-break-message-p nil)
ad953485 268(defvar type-break-mode-line-break-string " *** TAKE A TYPING BREAK NOW ***")
846f6dd9
NF
269
270(defvar type-break-mode-line-warning
271 '(type-break-mode-line-break-message-p
272 ("")
273 (type-break-warning-countdown-string
ad953485
NF
274 (" *** "
275 "Break in "
846f6dd9
NF
276 type-break-warning-countdown-string
277 " "
278 type-break-warning-countdown-string-type
279 "***"))))
280
281(defvar type-break-warning-countdown-string nil
282 "If non-nil, this is a countdown for the next typing break.
283
284This variable, in conjunction with `type-break-warning-countdown-string-type'
b688ed44 285\(which indicates whether this value is a number of keystrokes or seconds)
27cd478d 286is installed in `mode-line-format' to notify of imminent typing breaks.")
846f6dd9
NF
287
288(defvar type-break-warning-countdown-string-type nil
289 "Indicates the unit type of `type-break-warning-countdown-string'.
290It will be either \"seconds\" or \"keystrokes\".")
291
292\f
4cf64c15 293;; These are internal variables. Do not set them yourself.
622aca7c 294
defa7346 295(defvar type-break-alarm-p nil)
be8d412c 296(defvar type-break-keystroke-count 0)
be8d412c
NF
297(defvar type-break-time-last-break nil)
298(defvar type-break-time-next-break nil)
299(defvar type-break-time-last-command (current-time))
e7b20417
NF
300(defvar type-break-current-time-warning-interval nil)
301(defvar type-break-current-keystroke-warning-interval nil)
302(defvar type-break-time-warning-count 0)
303(defvar type-break-keystroke-warning-count 0)
27cd478d
EZ
304(defvar type-break-interval-start nil)
305
cc669dd8 306\f
4cf64c15
NF
307;;;###autoload
308(defun type-break-mode (&optional prefix)
309 "Enable or disable typing-break mode.
310This is a minor mode, but it is global to all buffers by default.
311
312When this mode is enabled, the user is encouraged to take typing breaks at
313appropriate intervals; either after a specified amount of time or when the
314user has exceeded a keystroke threshold. When the time arrives, the user
27cd478d 315is asked to take a break. If the user refuses at that time, Emacs will ask
4cf64c15
NF
316again in a short period of time. The idea is to give the user enough time
317to find a good breaking point in his or her work, but be sufficiently
318annoying to discourage putting typing breaks off indefinitely.
319
4cf64c15 320A negative prefix argument disables this mode.
cc669dd8 321No argument or any non-negative argument enables it.
4cf64c15
NF
322
323The user may enable or disable this mode by setting the variable of the
324same name, though setting it in that way doesn't reschedule a break or
325reset the keystroke counter.
326
be8d412c
NF
327If the mode was previously disabled and is enabled as a consequence of
328calling this function, it schedules a break with `type-break-schedule' to
329make sure one occurs (the user can call that command to reschedule the
330break at any time). It also initializes the keystroke counter.
4cf64c15
NF
331
332The variable `type-break-interval' specifies the number of seconds to
333schedule between regular typing breaks. This variable doesn't directly
334affect the time schedule; it simply provides a default for the
335`type-break-schedule' command.
336
cc669dd8
NF
337If set, the variable `type-break-good-rest-interval' specifies the minimum
338amount of time which is considered a reasonable typing break. Whenever
339that time has elapsed, typing breaks are automatically rescheduled for
27cd478d 340later even if Emacs didn't prompt you to take one first. Also, if a break
cc669dd8 341is ended before this much time has elapsed, the user will be asked whether
27cd478d
EZ
342or not to continue. A nil value for this variable prevents automatic
343break rescheduling, making `type-break-interval' an upper bound on the time
344between breaks. In this case breaks will be prompted for as usual before
345the upper bound if the keystroke threshold is reached.
346
347If `type-break-good-rest-interval' is nil and
348`type-break-good-break-interval' is set, then confirmation is required to
349interrupt a break before `type-break-good-break-interval' seconds
350have passed. This provides for an upper bound on the time between breaks
351together with confirmation of interruptions to these breaks.
be8d412c
NF
352
353The variable `type-break-keystroke-threshold' is used to determine the
354thresholds at which typing breaks should be considered. You can use
b4dc9e6a 355the command `type-break-guesstimate-keystroke-threshold' to try to
be8d412c 356approximate good values for this.
4cf64c15 357
b4dc9e6a
NF
358There are several variables that affect how or when warning messages about
359imminent typing breaks are displayed. They include:
360
2157be68
RS
361 `type-break-mode-line-message-mode'
362 `type-break-time-warning-intervals'
363 `type-break-keystroke-warning-intervals'
364 `type-break-warning-repeat'
365 `type-break-warning-countdown-string'
366 `type-break-warning-countdown-string-type'
b4dc9e6a 367
846f6dd9
NF
368There are several variables that affect if, how, and when queries to begin
369a typing break occur. They include:
b4dc9e6a 370
2157be68
RS
371 `type-break-query-mode'
372 `type-break-query-function'
373 `type-break-query-interval'
b4dc9e6a 374
27cd478d
EZ
375The command `type-break-statistics' prints interesting things.
376
377Finally, a file (named `type-break-file-name') is used to store information
378across Emacs sessions. This provides recovery of the break status between
379sessions and after a crash. Manual changes to the file may result in
380problems."
4cf64c15 381 (interactive "P")
846f6dd9 382 (type-break-check-post-command-hook)
4cf64c15 383
be8d412c 384 (let ((already-enabled type-break-mode))
cc669dd8 385 (setq type-break-mode (>= (prefix-numeric-value prefix) 0))
be8d412c
NF
386
387 (cond
388 ((and already-enabled type-break-mode)
389 (and (interactive-p)
2157be68 390 (message "Type Break mode is already enabled")))
be8d412c 391 (type-break-mode
846f6dd9
NF
392 (or global-mode-string
393 (setq global-mode-string '("")))
7cd3279a
RS
394 (or (assq 'type-break-mode-line-message-mode
395 minor-mode-alist)
396 (setq minor-mode-alist
397 (cons type-break-mode-line-format
398 minor-mode-alist)))
e7b20417 399 (type-break-keystroke-reset)
846f6dd9 400 (type-break-mode-line-countdown-or-break nil)
27cd478d
EZ
401
402 (if (boundp 'save-some-buffers-always)
403 (add-to-list 'save-some-buffers-always
404 (expand-file-name type-break-file-name)))
405
406 (setq type-break-time-last-break (type-break-get-previous-time))
407
408 ;; schedule according to break time from session file
409 (type-break-schedule
410 (let (diff)
411 (if (and type-break-time-last-break
412 (< (setq diff (type-break-time-difference
413 type-break-time-last-break
414 (current-time)))
415 type-break-interval))
416 ;; use the file's value
417 (progn
418 (setq type-break-keystroke-count
419 (type-break-get-previous-count))
420 ;; file the time, in case it was read from the auto-save file
421 (type-break-file-time type-break-interval-start)
422 (setq type-break-interval-start type-break-time-last-break)
423 (- type-break-interval diff))
424 ;; schedule from now
425 (setq type-break-interval-start (current-time))
426 (type-break-file-time type-break-interval-start)
427 type-break-interval))
428 type-break-interval-start
429 type-break-interval)
430
be8d412c 431 (and (interactive-p)
27cd478d 432 (message "Type Break mode is enabled and set")))
846f6dd9
NF
433 (t
434 (type-break-keystroke-reset)
435 (type-break-mode-line-countdown-or-break nil)
436 (type-break-cancel-schedule)
27cd478d
EZ
437 (do-auto-save)
438 (with-current-buffer (find-file-noselect type-break-file-name
439 'nowarn)
440 (set-buffer-modified-p nil)
441 (unlock-buffer)
442 (kill-this-buffer))
443 (if (boundp 'save-some-buffers-always)
444 (setq save-some-buffers-always
445 (remove (expand-file-name type-break-file-name)
446 save-some-buffers-always)))
846f6dd9 447 (and (interactive-p)
2157be68 448 (message "Type Break mode is disabled")))))
4cf64c15
NF
449 type-break-mode)
450
846f6dd9
NF
451(defun type-break-mode-line-message-mode (&optional prefix)
452 "Enable or disable warnings in the mode line about typing breaks.
453
27cd478d 454A negative PREFIX argument disables this mode.
846f6dd9
NF
455No argument or any non-negative argument enables it.
456
457The user may also enable or disable this mode simply by setting the
458variable of the same name.
459
460Variables controlling the display of messages in the mode line include:
461
2157be68
RS
462 `mode-line-format'
463 `global-mode-string'
464 `type-break-mode-line-break-message'
465 `type-break-mode-line-warning'"
846f6dd9
NF
466 (interactive "P")
467 (setq type-break-mode-line-message-mode
468 (>= (prefix-numeric-value prefix) 0))
469 (and (interactive-p)
470 (if type-break-mode-line-message-mode
471 (message "type-break-mode-line-message-mode is enabled")
472 (message "type-break-mode-line-message-mode is disabled")))
473 type-break-mode-line-message-mode)
474
475(defun type-break-query-mode (&optional prefix)
476 "Enable or disable warnings in the mode line about typing breaks.
477
478When enabled, the user is periodically queried about whether to take a
479typing break at that moment. The function which does this query is
480specified by the variable `type-break-query-function'.
b4dc9e6a 481
27cd478d 482A negative PREFIX argument disables this mode.
b4dc9e6a
NF
483No argument or any non-negative argument enables it.
484
485The user may also enable or disable this mode simply by setting the
486variable of the same name."
487 (interactive "P")
846f6dd9
NF
488 (setq type-break-query-mode
489 (>= (prefix-numeric-value prefix) 0))
490 (and (interactive-p)
491 (if type-break-query-mode
492 (message "type-break-query-mode is enabled")
493 (message "type-break-query-mode is disabled")))
494 type-break-query-mode)
b4dc9e6a 495
846f6dd9 496\f
27cd478d
EZ
497;;; session file functions
498
499(defvar type-break-auto-save-file-name
500 (let ((buffer-file-name type-break-file-name))
501 (make-auto-save-file-name))
502 "Auto-save name of `type-break-file-name'.")
503
504(defun type-break-file-time (&optional time)
505 "File break time in `type-break-file-name', unless the file is locked."
506 (if (not (stringp (file-locked-p type-break-file-name)))
507 (with-current-buffer (find-file-noselect type-break-file-name
508 'nowarn)
509 (let ((inhibit-read-only t))
510 (erase-buffer)
511 (insert (format "%s\n\n" (or time type-break-interval-start)))
512 ;; file saving is left to auto-save
513 ))))
514
515(defun type-break-file-keystroke-count ()
516 "File keystroke count in `type-break-file-name', unless the file is locked."
517 (if (not (stringp (file-locked-p type-break-file-name)))
518 (with-current-buffer (find-file-noselect type-break-file-name
519 'nowarn)
520 (save-excursion
521 (let ((inhibit-read-only t))
522 (goto-char (point-min))
523 (forward-line)
524 (delete-region (point) (save-excursion (end-of-line) (point)))
525 (insert (format "%s" type-break-keystroke-count))
526 ;; file saving is left to auto-save
527 )))))
528
529(defun timep (time)
530 "If TIME is in the format returned by `current-time' then
531return TIME, else return nil."
532 (and (listp time)
533 (eq (length time) 3)
534 (integerp (car time))
535 (integerp (nth 1 time))
536 (integerp (nth 2 time))
537 time))
538
539(defun type-break-choose-file ()
540 "Return file to read from."
541 (cond
542 ((and (file-exists-p type-break-auto-save-file-name)
543 (file-readable-p type-break-auto-save-file-name))
544 type-break-auto-save-file-name)
545 ((and (file-exists-p type-break-file-name)
546 (file-readable-p type-break-file-name))
547 type-break-file-name)
548 (t nil)))
549
550(defun type-break-get-previous-time ()
551 "Get previous break time from `type-break-file-name'.
552Returns nil if the file is missing or if the time breaks with the
553`current-time' format."
554 (let ((file (type-break-choose-file)))
555 (if file
556 (timep ;; returns expected format, else nil
557 (with-current-buffer (find-file-noselect file 'nowarn)
558 (save-excursion
559 (goto-char (point-min))
560 (read (current-buffer))))))))
561
562(defun type-break-get-previous-count ()
563 "Get previous keystroke count from `type-break-file-name'.
564Return 0 if the file is missing or if the form read is not an
565integer."
566 (let ((file (type-break-choose-file)))
567 (if (and file
568 (integerp
569 (setq file
570 (with-current-buffer
571 (find-file-noselect file 'nowarn)
572 (save-excursion
573 (goto-char (point-min))
574 (forward-line 1)
575 (read (current-buffer)))))))
576 file
577 0)))
578
579\f
622aca7c 580;;;###autoload
458401b6
NF
581(defun type-break ()
582 "Take a typing break.
583
4cf64c15 584During the break, a demo selected from the functions listed in
cc669dd8 585`type-break-demo-functions' is run.
458401b6 586
4cf64c15 587After the typing break is finished, the next break is scheduled
cc669dd8 588as per the function `type-break-schedule'."
622aca7c 589 (interactive)
ad953485 590 (do-auto-save)
e7b20417 591 (type-break-cancel-schedule)
27cd478d
EZ
592 ;; remove any query scheduled during interactive invocation
593 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
cc669dd8
NF
594 (let ((continue t)
595 (start-time (current-time)))
596 (setq type-break-time-last-break start-time)
597 (while continue
598 (save-window-excursion
599 ;; Eat the screen.
600 (and (eq (selected-window) (minibuffer-window))
601 (other-window 1))
602 (delete-other-windows)
603 (scroll-right (window-width))
27cd478d
EZ
604 (unless type-break-terse-messages
605 (message "Press any key to resume from typing break."))
cc669dd8
NF
606
607 (random t)
608 (let* ((len (length type-break-demo-functions))
609 (idx (random len))
610 (fn (nth idx type-break-demo-functions)))
611 (condition-case ()
612 (funcall fn)
613 (error nil))))
be8d412c 614
27cd478d
EZ
615 (let ((good-interval (or type-break-good-rest-interval
616 type-break-good-break-interval)))
617 (cond
618 (good-interval
619 (let ((break-secs (type-break-time-difference
620 start-time (current-time))))
621 (cond
622 ((>= break-secs good-interval)
623 (setq continue nil))
624 ;; 60 seconds may be too much leeway if the break is only 3
625 ;; minutes to begin with. You can just say "no" to the query
626 ;; below if you're in that much of a hurry.
627 ;;((> 60 (abs (- break-secs good-interval)))
628 ;; (setq continue nil))
629 ((funcall
630 type-break-query-function
631 (format
632 (if type-break-terse-messages
633 "%s%s remaining. Continue break? "
634 "%sYou really ought to rest %s more. Continue break? ")
635 (type-break-time-stamp)
636 (type-break-format-time (- good-interval
637 break-secs)))))
638 (t
639 (setq continue nil)))))
640 (t (setq continue nil))))))
4cf64c15 641
e7b20417 642 (type-break-keystroke-reset)
27cd478d 643 (type-break-file-time)
846f6dd9 644 (type-break-mode-line-countdown-or-break nil)
be8d412c 645 (type-break-schedule))
622aca7c 646
458401b6 647\f
27cd478d 648(defun type-break-schedule (&optional time start interval)
defa7346 649 "Schedule a typing break for TIME seconds from now.
27cd478d
EZ
650If time is not specified it defaults to `type-break-interval'.
651START and INTERVAL are used when recovering a break.
652START is the start of the break (defaults to now).
653INTERVAL is the full length of an interval (defaults to TIME)."
defa7346
NF
654 (interactive (list (and current-prefix-arg
655 (prefix-numeric-value current-prefix-arg))))
458401b6 656 (or time (setq time type-break-interval))
846f6dd9 657 (type-break-check-post-command-hook)
4cf64c15 658 (type-break-cancel-schedule)
e7b20417 659 (type-break-time-warning-schedule time 'reset)
846f6dd9 660 (type-break-run-at-time (max 1 time) nil 'type-break-alarm)
defa7346 661 (setq type-break-time-next-break
27cd478d
EZ
662 (type-break-time-sum (or start (current-time))
663 (or interval time))))
622aca7c 664
4cf64c15 665(defun type-break-cancel-schedule ()
e7b20417 666 (type-break-cancel-time-warning-schedule)
846f6dd9 667 (type-break-cancel-function-timers 'type-break-alarm)
be8d412c
NF
668 (setq type-break-alarm-p nil)
669 (setq type-break-time-next-break nil))
458401b6 670
e7b20417 671(defun type-break-time-warning-schedule (&optional time resetp)
b4dc9e6a 672 (let ((type-break-current-time-warning-interval nil))
e7b20417 673 (type-break-cancel-time-warning-schedule))
846f6dd9 674 (add-hook 'type-break-post-command-hook 'type-break-time-warning 'append)
e7b20417
NF
675 (cond
676 (type-break-time-warning-intervals
677 (and resetp
678 (setq type-break-current-time-warning-interval
679 type-break-time-warning-intervals))
680
681 (or time
682 (setq time (type-break-time-difference (current-time)
683 type-break-time-next-break)))
684
685 (while (and type-break-current-time-warning-interval
686 (> (car type-break-current-time-warning-interval) time))
687 (setq type-break-current-time-warning-interval
688 (cdr type-break-current-time-warning-interval)))
689
690 (cond
691 (type-break-current-time-warning-interval
692 (setq time (- time (car type-break-current-time-warning-interval)))
693 (setq type-break-current-time-warning-interval
694 (cdr type-break-current-time-warning-interval))
695
b4dc9e6a
NF
696 ;(let (type-break-current-time-warning-interval)
697 ; (type-break-cancel-time-warning-schedule))
846f6dd9 698 (type-break-run-at-time (max 1 time) nil 'type-break-time-warning-alarm)
b4dc9e6a
NF
699
700 (cond
701 (resetp
702 (setq type-break-warning-countdown-string nil))
703 (t
704 (setq type-break-warning-countdown-string (number-to-string time))
705 (setq type-break-warning-countdown-string-type "seconds"))))))))
e7b20417
NF
706
707(defun type-break-cancel-time-warning-schedule ()
846f6dd9 708 (type-break-cancel-function-timers 'type-break-time-warning-alarm)
e7b20417
NF
709 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
710 (setq type-break-current-time-warning-interval
b4dc9e6a 711 type-break-time-warning-intervals)
27cd478d 712 (setq type-break-time-warning-count 0) ; avoid warnings after break
b4dc9e6a 713 (setq type-break-warning-countdown-string nil))
e7b20417 714
4cf64c15 715(defun type-break-alarm ()
846f6dd9
NF
716 (type-break-check-post-command-hook)
717 (setq type-break-alarm-p t)
718 (type-break-mode-line-countdown-or-break 'break))
458401b6 719
e7b20417 720(defun type-break-time-warning-alarm ()
846f6dd9 721 (type-break-check-post-command-hook)
e7b20417
NF
722 (type-break-time-warning-schedule)
723 (setq type-break-time-warning-count type-break-warning-repeat)
846f6dd9
NF
724 (type-break-time-warning)
725 (type-break-mode-line-countdown-or-break 'countdown))
e7b20417
NF
726
727\f
728(defun type-break-run-tb-post-command-hook ()
729 (and type-break-mode
730 (run-hooks 'type-break-post-command-hook)))
731
458401b6 732(defun type-break-check ()
4cf64c15
NF
733 "Ask to take a typing break if appropriate.
734This may be the case either because the scheduled time has come \(and the
735minimum keystroke threshold has been reached\) or because the maximum
736keystroke threshold has been exceeded."
27cd478d 737 (type-break-file-keystroke-count)
e7b20417
NF
738 (let* ((min-threshold (car type-break-keystroke-threshold))
739 (max-threshold (cdr type-break-keystroke-threshold)))
740 (and type-break-good-rest-interval
741 (progn
742 (and (> (type-break-time-difference
743 type-break-time-last-command (current-time))
744 type-break-good-rest-interval)
745 (progn
746 (type-break-keystroke-reset)
846f6dd9 747 (type-break-mode-line-countdown-or-break nil)
e7b20417
NF
748 (setq type-break-time-last-break (current-time))
749 (type-break-schedule)))
750 (setq type-break-time-last-command (current-time))))
751
752 (and type-break-keystroke-threshold
b4dc9e6a
NF
753 (let ((keys (this-command-keys)))
754 (cond
755 ;; Ignore mouse motion
756 ((and (vectorp keys)
757 (consp (aref keys 0))
758 (memq (car (aref keys 0)) '(mouse-movement))))
759 (t
760 (setq type-break-keystroke-count
761 (+ type-break-keystroke-count (length keys)))))))
e7b20417 762
e7b20417
NF
763 (cond
764 (type-break-alarm-p
765 (cond
766 ((input-pending-p))
767 ((eq (selected-window) (minibuffer-window)))
768 ((and min-threshold
769 (< type-break-keystroke-count min-threshold))
770 (type-break-schedule))
771 (t
772 ;; If keystroke count is within min-threshold of
b4dc9e6a 773 ;; max-threshold, lower it to reduce the likelihood of an
e7b20417
NF
774 ;; immediate subsequent query.
775 (and max-threshold
776 min-threshold
777 (< (- max-threshold type-break-keystroke-count) min-threshold)
778 (progn
779 (type-break-keystroke-reset)
780 (setq type-break-keystroke-count min-threshold)))
781 (type-break-query))))
782 ((and type-break-keystroke-warning-intervals
783 max-threshold
784 (= type-break-keystroke-warning-count 0)
785 (type-break-check-keystroke-warning)))
786 ((and max-threshold
787 (> type-break-keystroke-count max-threshold)
788 (not (input-pending-p))
789 (not (eq (selected-window) (minibuffer-window))))
790 (type-break-keystroke-reset)
791 (setq type-break-keystroke-count (or min-threshold 0))
792 (type-break-query)))))
793
794;; This should return t if warnings were enabled, nil otherwise.
846f6dd9 795(defun type-break-check-keystroke-warning ()
b4dc9e6a
NF
796 ;; This is safe because the caller should have checked that the cdr was
797 ;; non-nil already.
e7b20417
NF
798 (let ((left (- (cdr type-break-keystroke-threshold)
799 type-break-keystroke-count)))
800 (cond
801 ((null (car type-break-current-keystroke-warning-interval))
802 nil)
803 ((> left (car type-break-current-keystroke-warning-interval))
804 nil)
805 (t
806 (while (and (car type-break-current-keystroke-warning-interval)
807 (< left (car type-break-current-keystroke-warning-interval)))
808 (setq type-break-current-keystroke-warning-interval
809 (cdr type-break-current-keystroke-warning-interval)))
810 (setq type-break-keystroke-warning-count type-break-warning-repeat)
811 (add-hook 'type-break-post-command-hook 'type-break-keystroke-warning)
b4dc9e6a
NF
812 (setq type-break-warning-countdown-string (number-to-string left))
813 (setq type-break-warning-countdown-string-type "keystrokes")
846f6dd9 814 (type-break-mode-line-countdown-or-break 'countdown)
e7b20417 815 t))))
4cf64c15 816
b4dc9e6a 817;; Arrange for a break query to be made, when the user stops typing furiously.
4cf64c15 818(defun type-break-query ()
b4dc9e6a
NF
819 (add-hook 'type-break-post-command-hook 'type-break-do-query))
820
b4dc9e6a
NF
821(defun type-break-do-query ()
822 (cond
846f6dd9
NF
823 ((not type-break-query-mode)
824 (type-break-noninteractive-query)
825 (type-break-schedule type-break-query-interval)
826 (remove-hook 'type-break-post-command-hook 'type-break-do-query))
827 ((sit-for 2)
b4dc9e6a
NF
828 (condition-case ()
829 (cond
830 ((let ((type-break-mode nil)
831 ;; yes-or-no-p sets this-command to exit-minibuffer,
832 ;; which hoses undo or yank-pop (if you happened to be
833 ;; yanking just when the query occurred).
834 (this-command this-command))
ad953485
NF
835 ;; Cancel schedule to prevent possibility of a second query
836 ;; from taking place before this one has even returned.
837 ;; The condition-case wrapper will reschedule on quit.
838 (type-break-cancel-schedule)
27cd478d
EZ
839 ;; Also prevent a second query when the break is interrupted.
840 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
b4dc9e6a 841 (funcall type-break-query-function
ad953485
NF
842 (format "%s%s"
843 (type-break-time-stamp)
27cd478d
EZ
844 (if type-break-terse-messages
845 "Break now? "
846 "Take a break from typing now? "))))
b4dc9e6a
NF
847 (type-break))
848 (t
849 (type-break-schedule type-break-query-interval)))
850 (quit
27cd478d 851 (type-break-schedule type-break-query-interval))))))
458401b6 852
846f6dd9
NF
853(defun type-break-noninteractive-query (&optional ignored-args)
854 "Null query function which doesn't interrupt user and assumes `no'.
855It prints a reminder in the echo area to take a break, but doesn't enforce
856this or ask the user to start one right now."
857 (cond
858 (type-break-mode-line-message-mode)
859 (t
860 (beep t)
ad953485
NF
861 (message "%sYou should take a typing break now. Do `M-x type-break'."
862 (type-break-time-stamp))
846f6dd9
NF
863 (sit-for 1)
864 (beep t)
865 ;; return nil so query caller knows to reset reminder, as if user
866 ;; said "no" in response to yes-or-no-p.
867 nil)))
868
e7b20417
NF
869(defun type-break-time-warning ()
870 (cond
871 ((and (car type-break-keystroke-threshold)
872 (< type-break-keystroke-count (car type-break-keystroke-threshold))))
873 ((> type-break-time-warning-count 0)
b4dc9e6a
NF
874 (let ((timeleft (type-break-time-difference (current-time)
875 type-break-time-next-break)))
876 (setq type-break-warning-countdown-string (number-to-string timeleft))
877 (cond
878 ((eq (selected-window) (minibuffer-window)))
879 ;; Do nothing if the command was just a prefix arg, since that will
880 ;; immediately be followed by some other interactive command.
846f6dd9
NF
881 ;; Otherwise, it is particularly annoying for the sit-for below to
882 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
b4dc9e6a 883 ((memq this-command '(digit-argument universal-argument)))
846f6dd9 884 ((not type-break-mode-line-message-mode)
b4dc9e6a
NF
885 ;; Pause for a moment so any previous message can be seen.
886 (sit-for 2)
ad953485
NF
887 (message "%sWarning: typing break due in %s."
888 (type-break-time-stamp)
b4dc9e6a
NF
889 (type-break-format-time timeleft))
890 (setq type-break-time-warning-count
891 (1- type-break-time-warning-count))))))
e7b20417 892 (t
b4dc9e6a
NF
893 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
894 (setq type-break-warning-countdown-string nil))))
e7b20417
NF
895
896(defun type-break-keystroke-warning ()
897 (cond
898 ((> type-break-keystroke-warning-count 0)
b4dc9e6a
NF
899 (setq type-break-warning-countdown-string
900 (number-to-string (- (cdr type-break-keystroke-threshold)
901 type-break-keystroke-count)))
e7b20417
NF
902 (cond
903 ((eq (selected-window) (minibuffer-window)))
846f6dd9
NF
904 ;; Do nothing if the command was just a prefix arg, since that will
905 ;; immediately be followed by some other interactive command.
906 ;; Otherwise, it is particularly annoying for the sit-for below to
907 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
908 ((memq this-command '(digit-argument universal-argument)))
909 ((not type-break-mode-line-message-mode)
e7b20417 910 (sit-for 2)
ad953485
NF
911 (message "%sWarning: typing break due in %s keystrokes."
912 (type-break-time-stamp)
e7b20417
NF
913 (- (cdr type-break-keystroke-threshold)
914 type-break-keystroke-count))
915 (setq type-break-keystroke-warning-count
916 (1- type-break-keystroke-warning-count)))))
917 (t
918 (remove-hook 'type-break-post-command-hook
b4dc9e6a
NF
919 'type-break-keystroke-warning)
920 (setq type-break-warning-countdown-string nil))))
458401b6 921
846f6dd9
NF
922(defun type-break-mode-line-countdown-or-break (&optional type)
923 (cond
924 ((not type-break-mode-line-message-mode))
925 ((eq type 'countdown)
926 ;(setq type-break-mode-line-break-message-p nil)
927 (add-hook 'type-break-post-command-hook
928 'type-break-force-mode-line-update 'append))
929 ((eq type 'break)
930 ;; Alternate
931 (setq type-break-mode-line-break-message-p
932 (not type-break-mode-line-break-message-p))
933 (remove-hook 'type-break-post-command-hook
934 'type-break-force-mode-line-update))
935 (t
936 (setq type-break-mode-line-break-message-p nil)
937 (setq type-break-warning-countdown-string nil)
938 (remove-hook 'type-break-post-command-hook
939 'type-break-force-mode-line-update)))
940 (type-break-force-mode-line-update))
941
458401b6 942\f
be8d412c
NF
943;;;###autoload
944(defun type-break-statistics ()
945 "Print statistics about typing breaks in a temporary buffer.
946This includes the last time a typing break was taken, when the next one is
947scheduled, the keystroke thresholds and the current keystroke count, etc."
948 (interactive)
949 (with-output-to-temp-buffer "*Typing Break Statistics*"
950 (princ (format "Typing break statistics\n-----------------------\n
b4dc9e6a 951Typing break mode is currently %s.
846f6dd9
NF
952Interactive query for breaks is %s.
953Warnings of imminent typing breaks in mode line is %s.
b4dc9e6a
NF
954
955Last typing break ended : %s
be8d412c
NF
956Next scheduled typing break : %s\n
957Minimum keystroke threshold : %s
958Maximum keystroke threshold : %s
959Current keystroke count : %s"
b4dc9e6a 960 (if type-break-mode "enabled" "disabled")
846f6dd9
NF
961 (if type-break-query-mode "enabled" "disabled")
962 (if type-break-mode-line-message-mode "enabled" "disabled")
be8d412c
NF
963 (if type-break-time-last-break
964 (current-time-string type-break-time-last-break)
965 "never")
966 (if (and type-break-mode type-break-time-next-break)
6b62b567 967 (format "%s\t(%s from now)"
be8d412c 968 (current-time-string type-break-time-next-break)
e7b20417 969 (type-break-format-time
cc669dd8 970 (type-break-time-difference
e7b20417 971 (current-time)
cc669dd8 972 type-break-time-next-break)))
be8d412c
NF
973 "none scheduled")
974 (or (car type-break-keystroke-threshold) "none")
975 (or (cdr type-break-keystroke-threshold) "none")
976 type-break-keystroke-count))))
977
978;;;###autoload
b4dc9e6a 979(defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac)
be8d412c 980 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
b4dc9e6a 981
be8d412c 982If called interactively, the user is prompted for their guess as to how
b4dc9e6a
NF
983many words per minute they usually type. This value should not be your
984maximum WPM, but your average. Of course, this is harder to gauge since it
985can vary considerably depending on what you are doing. For example, one
846f6dd9 986tends to type less when debugging a program as opposed to writing
b4dc9e6a
NF
987documentation. (Perhaps a separate program should be written to estimate
988average typing speed.)
989
990From that, this command sets the values in `type-break-keystroke-threshold'
991based on a fairly simple algorithm involving assumptions about the average
992length of words (5). For the minimum threshold, it uses about a fifth of
993the computed maximum threshold.
be8d412c 994
27cd478d 995When called from Lisp programs, the optional args WORDLEN and FRAC can be
be8d412c
NF
996used to override the default assumption about average word length and the
997fraction of the maximum threshold to which to set the minimum threshold.
998FRAC should be the inverse of the fractional value; for example, a value of
9992 would mean to use one half, a value of 4 would mean to use one quarter, etc."
b4dc9e6a 1000 (interactive "NOn average, how many words per minute do you type? ")
be8d412c 1001 (let* ((upper (* wpm (or wordlen 5) (/ type-break-interval 60)))
f486195c 1002 (lower (/ upper (or frac 5))))
be8d412c
NF
1003 (or type-break-keystroke-threshold
1004 (setq type-break-keystroke-threshold (cons nil nil)))
1005 (setcar type-break-keystroke-threshold lower)
1006 (setcdr type-break-keystroke-threshold upper)
1007 (if (interactive-p)
1008 (message "min threshold: %d\tmax threshold: %d" lower upper)
1009 type-break-keystroke-threshold)))
1010
1011\f
e7b20417
NF
1012;;; misc functions
1013
1014;; Compute the difference, in seconds, between a and b, two structures
1015;; similar to those returned by `current-time'.
defa7346
NF
1016;; Use addition rather than logand since that is more robust; the low 16
1017;; bits of the seconds might have been incremented, making it more than 16
1018;; bits wide.
846f6dd9 1019(defun type-break-time-difference (a b)
e7b20417
NF
1020 (+ (lsh (- (car b) (car a)) 16)
1021 (- (car (cdr b)) (car (cdr a)))))
1022
defa7346
NF
1023;; Return (in a new list the same in structure to that returned by
1024;; `current-time') the sum of the arguments. Each argument may be a time
1025;; list or a single integer, a number of seconds.
1026;; This function keeps the high and low 16 bits of the seconds properly
1027;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
1028;; the result is passed to `current-time-string' it will toss some of the
846f6dd9 1029;; "low" bits and format the time incorrectly.
defa7346
NF
1030(defun type-break-time-sum (&rest tmlist)
1031 (let ((high 0)
1032 (low 0)
1033 (micro 0)
1034 tem)
1035 (while tmlist
1036 (setq tem (car tmlist))
1037 (setq tmlist (cdr tmlist))
1038 (cond
1039 ((numberp tem)
1040 (setq low (+ low tem)))
1041 (t
1042 (setq high (+ high (or (car tem) 0)))
1043 (setq low (+ low (or (car (cdr tem)) 0)))
1044 (setq micro (+ micro (or (car (cdr (cdr tem))) 0))))))
1045
1046 (and (>= micro 1000000)
1047 (progn
1048 (setq tem (/ micro 1000000))
1049 (setq low (+ low tem))
1050 (setq micro (- micro (* tem 1000000)))))
1051
1052 (setq tem (lsh low -16))
1053 (and (> tem 0)
1054 (progn
1055 (setq low (logand low 65535))
1056 (setq high (+ high tem))))
1057
1058 (list high low micro)))
1059
ad953485
NF
1060(defun type-break-time-stamp (&optional when)
1061 (if (fboundp 'format-time-string)
1062 (format-time-string type-break-time-stamp-format when)
1063 ;; Emacs 19.28 and prior do not have format-time-string.
1064 ;; In that case, result is not customizable. Upgrade today!
1065 (format "[%s] " (substring (current-time-string when) 11 16))))
1066
846f6dd9 1067(defun type-break-format-time (secs)
e7b20417
NF
1068 (let ((mins (/ secs 60)))
1069 (cond
1070 ((= mins 1) (format "%d minute" mins))
1071 ((> mins 0) (format "%d minutes" mins))
1072 ((= secs 1) (format "%d second" secs))
1073 (t (format "%d seconds" secs)))))
1074
1075(defun type-break-keystroke-reset ()
27cd478d 1076 (setq type-break-interval-start (current-time)) ; not a keystroke
e7b20417
NF
1077 (setq type-break-keystroke-count 0)
1078 (setq type-break-keystroke-warning-count 0)
1079 (setq type-break-current-keystroke-warning-interval
1080 type-break-keystroke-warning-intervals)
1081 (remove-hook 'type-break-post-command-hook 'type-break-keystroke-warning))
1082
846f6dd9
NF
1083(defun type-break-force-mode-line-update (&optional all)
1084 "Force the mode-line of the current buffer to be redisplayed.
1085With optional non-nil ALL, force redisplay of all mode-lines."
1086 (and all (save-excursion (set-buffer (other-buffer))))
1087 (set-buffer-modified-p (buffer-modified-p)))
1088
27cd478d 1089;; If an exception occurs in Emacs while running the post command hook, the
846f6dd9
NF
1090;; value of that hook is clobbered. This is because the value of the
1091;; variable is temporarily set to nil while it's running to prevent
1092;; recursive application, but it also means an exception aborts the routine
1093;; of restoring it. This function is called from the timers to restore it,
1094;; just in case.
1095(defun type-break-check-post-command-hook ()
1096 (add-hook 'post-command-hook 'type-break-run-tb-post-command-hook 'append))
1097
1098\f
1099;;; Timer wrapper functions
1100;;;
1101;;; These shield type-break from variations in the interval timer packages
27cd478d 1102;;; for different versions of Emacs.
846f6dd9
NF
1103
1104(defun type-break-run-at-time (time repeat function)
2c8155f7
SM
1105 (condition-case nil (or (require 'timer) (require 'itimer)) (error nil))
1106 (cond ((fboundp 'run-at-time)
1107 (run-at-time time repeat function))
1108 ((fboundp 'start-timer)
846f6dd9
NF
1109 (let ((name (if (symbolp function)
1110 (symbol-name function)
1111 "type-break")))
2c8155f7
SM
1112 (start-timer name function time repeat)))
1113 ((fboundp 'start-itimer)
846f6dd9
NF
1114 (let ((name (if (symbolp function)
1115 (symbol-name function)
1116 "type-break")))
2c8155f7 1117 (start-itimer name function time repeat)))))
846f6dd9 1118
2c8155f7 1119(defvar timer-dont-exit)
846f6dd9 1120(defun type-break-cancel-function-timers (function)
2c8155f7 1121 (cond ((fboundp 'cancel-function-timers)
846f6dd9 1122 (let ((timer-dont-exit t))
2c8155f7
SM
1123 (cancel-function-timers function)))
1124 ((fboundp 'delete-timer)
846f6dd9
NF
1125 (let ((list timer-list))
1126 (while list
1127 (and (eq (funcall 'timer-function (car list)) function)
2c8155f7 1128 (delete-timer (car list)))
846f6dd9 1129 (setq list (cdr list)))))
2c8155f7 1130 ((fboundp 'delete-itimer)
2f3b1a8c
RS
1131 (with-no-warnings
1132 (let ((list itimer-list))
1133 (while list
1134 (and (eq (funcall 'itimer-function (car list)) function)
2c8155f7 1135 (delete-itimer (car list)))
2f3b1a8c 1136 (setq list (cdr list))))))))
846f6dd9 1137
e7b20417
NF
1138\f
1139;;; Demo wrappers
1140
1141;; This is a wrapper around hanoi that calls it with an arg large enough to
1142;; make the largest discs possible that will fit in the window.
1143;; Also, clean up the *Hanoi* buffer after we're done.
1144(defun type-break-demo-hanoi ()
1145 "Take a hanoiing typing break."
1146 (and (get-buffer "*Hanoi*")
1147 (kill-buffer "*Hanoi*"))
1148 (condition-case ()
1149 (progn
1150 (hanoi (/ (window-width) 8))
1151 ;; Wait for user to come back.
1152 (read-char)
1153 (kill-buffer "*Hanoi*"))
1154 (quit
1155 ;; eat char
1156 (read-char)
1157 (and (get-buffer "*Hanoi*")
1158 (kill-buffer "*Hanoi*")))))
1159
1160;; This is a wrapper around life that calls it with a `sleep' arg to make
1161;; it run a little more leisurely.
1162;; Also, clean up the *Life* buffer after we're done.
1163(defun type-break-demo-life ()
1164 "Take a typing break and get a life."
1165 (let ((continue t))
1166 (while continue
1167 (setq continue nil)
1168 (and (get-buffer "*Life*")
1169 (kill-buffer "*Life*"))
1170 (condition-case ()
1171 (progn
1172 (life 3)
1173 ;; wait for user to return
1174 (read-char)
1175 (kill-buffer "*Life*"))
1176 (life-extinct
b4dc9e6a 1177 (message "%s" (get 'life-extinct 'error-message))
e7b20417
NF
1178 (sit-for 3)
1179 ;; restart demo
1180 (setq continue t))
1181 (quit
1182 (and (get-buffer "*Life*")
1183 (kill-buffer "*Life*")))))))
1184
defa7346 1185;; Boring demo, but doesn't use many cycles
e7b20417
NF
1186(defun type-break-demo-boring ()
1187 "Boring typing break demo."
27cd478d
EZ
1188 (let ((rmsg (if type-break-terse-messages
1189 ""
1190 "Press any key to resume from typing break"))
e7b20417 1191 (buffer-name "*Typing Break Buffer*")
27cd478d 1192 lines elapsed timeleft tmsg)
e7b20417
NF
1193 (condition-case ()
1194 (progn
1195 (switch-to-buffer (get-buffer-create buffer-name))
1196 (buffer-disable-undo (current-buffer))
27cd478d
EZ
1197 (setq lines (/ (window-body-height) 2))
1198 (unless type-break-terse-messages (setq lines (1- lines)))
1199 (if type-break-demo-boring-stats
1200 (setq lines (- lines 2)))
1201 (setq lines (make-string lines ?\C-j))
defa7346 1202 (while (not (input-pending-p))
27cd478d 1203 (erase-buffer)
defa7346
NF
1204 (setq elapsed (type-break-time-difference
1205 type-break-time-last-break
1206 (current-time)))
27cd478d
EZ
1207 (let ((good-interval (or type-break-good-rest-interval
1208 type-break-good-break-interval)))
1209 (cond
1210 (good-interval
1211 (setq timeleft (- good-interval elapsed))
1212 (if (> timeleft 0)
1213 (setq tmsg
1214 (format (if type-break-terse-messages
1215 "Break remaining: %s"
1216 "You should rest for %s more")
1217 (type-break-format-time timeleft)))
1218 (setq tmsg
1219 (format (if type-break-terse-messages
1220 "Break complete (%s elapsed in total)"
1221 "Typing break has lasted %s")
1222 (type-break-format-time elapsed)))))
1223 (t
1224 (setq tmsg
1225 (format (if type-break-terse-messages
1226 "Break has lasted %s"
1227 "Typing break has lasted %s")
1228 (type-break-format-time elapsed))))))
1229 (insert lines
1230 (make-string (/ (- (window-width) (length tmsg)) 2) ?\ )
1231 tmsg)
1232 (if (> (length rmsg) 0)
1233 (insert "\n"
1234 (make-string (/ (- (window-width) (length rmsg)) 2)
1235 ?\ )
1236 rmsg))
1237 (if type-break-demo-boring-stats
1238 (let*
1239 ((message
1240 (format
1241 (if type-break-terse-messages
1242 "Since last break: %s keystrokes\n"
1243 "Since your last break you've typed %s keystrokes\n")
1244 type-break-keystroke-count))
1245 (column-spaces
1246 (make-string (/ (- (window-width) (length message)) 2)
1247 ?\ ))
1248 (wpm (/ (/ (float type-break-keystroke-count) 5)
1249 (/ (type-break-time-difference
1250 type-break-interval-start
1251 type-break-time-last-break)
1252 60.0))))
1253 (insert "\n\n" column-spaces message)
1254 (if type-break-terse-messages
1255 (insert (format " %s%.2f wpm"
1256 column-spaces
1257 wpm))
1258 (setq message
1259 (format "at an average of %.2f words per minute"
1260 wpm))
1261 (insert
1262 (make-string (/ (- (window-width) (length message)) 2)
1263 ?\ )
1264 message))))
defa7346
NF
1265 (goto-char (point-min))
1266 (sit-for 60))
e7b20417
NF
1267 (read-char)
1268 (kill-buffer buffer-name))
1269 (quit
1270 (and (get-buffer buffer-name)
1271 (kill-buffer buffer-name))))))
1272
1273\f
458401b6
NF
1274(provide 'type-break)
1275
104221a0
SE
1276(if type-break-mode
1277 (type-break-mode 1))
ad953485 1278
ab5796a9 1279;;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
458401b6 1280;;; type-break.el ends here