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