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