(texinfo-all-menus-update)
[bpt/emacs.git] / lisp / type-break.el
CommitLineData
be8d412c 1;;; type-break.el --- encourage rests from typing at appropriate intervals
458401b6 2
846f6dd9 3;; Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
99c0333b
NF
4
5;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
6;; Maintainer: friedman@prep.ai.mit.edu
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
NF
10
11;; $Id$
99c0333b
NF
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
b4dc9e6a
NF
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.
458401b6
NF
29
30;;; Commentary:
be8d412c 31
b4dc9e6a
NF
32;; The docstring for the function `type-break-mode' summarizes most of the
33;; details of the interface.
be8d412c 34
b4dc9e6a
NF
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.
be8d412c 41
846f6dd9
NF
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'.
b4dc9e6a
NF
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
846f6dd9
NF
59;; This program has no hope of working in Emacs 18.
60
b4dc9e6a
NF
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>
846f6dd9 67;; Robert S. Boyer <boyer@cs.utexas.edu>
be8d412c 68
458401b6
NF
69;;; Code:
70
71\f
622aca7c 72;;;###autoload
be8d412c 73(defvar type-break-mode nil
4cf64c15
NF
74 "*Non-`nil' means typing break mode is enabled.
75See 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.")
458401b6 80
be8d412c
NF
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
85When this variable is non-`nil', emacs checks the idle time between
cc669dd8 86keystrokes. If this idle time is long enough to be considered a \"good\"
be8d412c
NF
87rest from typing, then the next typing break is simply rescheduled for later.
88
cc669dd8
NF
89If a break is interrupted before this much time elapses, the user will be
90asked whether or not really to interrupt the break.")
be8d412c 91
458401b6 92;;;###autoload
4cf64c15 93(defvar type-break-keystroke-threshold
cc669dd8 94 ;; Assuming typing speed is 35wpm (on the average, do you really
f486195c
NF
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
4cf64c15
NF
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.
f486195c 101 ;; Conversely, the minimum threshold should be about a fifth of this.
cc669dd8 102 (let* ((wpm 35)
4cf64c15
NF
103 (avg-word-length 5)
104 (upper (* wpm avg-word-length (/ type-break-interval 60)))
f486195c 105 (lower (/ upper 5)))
4cf64c15
NF
106 (cons lower upper))
107 "*Upper and lower bound on number of keystrokes for considering typing break.
be8d412c 108This structure is a pair of numbers.
4cf64c15 109
be8d412c
NF
110The first number is the minimum number of keystrokes that must have been
111entered since the last typing break before considering another one, even if
112the scheduled time has elapsed; the break is simply rescheduled until later
113if the minimum threshold hasn't been reached. If this first value is nil,
114then there is no minimum threshold; as soon as the scheduled time has
115elapsed, the user will always be queried.
4cf64c15
NF
116
117The second number is the maximum number of keystrokes that can be entered
118before a typing break is requested immediately, pre-empting the originally
be8d412c
NF
119scheduled break. If this second value is nil, then no pre-emptive breaks
120will occur; only scheduled ones will.
4cf64c15
NF
121
122Keys with bucky bits (shift, control, meta, etc) are counted as only one
b4dc9e6a
NF
123keystroke even though they really require multiple keys to generate them.
124
125The command `type-break-guesstimate-keystroke-threshold' can be used to
126guess a reasonably good pair of values for this variable.")
127
846f6dd9
NF
128(defvar type-break-query-mode t
129 "*Non-`nil' means ask whether or not to prompt user for breaks.
130If so, call the function specified in the value of the variable
131`type-break-query-function' to do the asking.")
132
b4dc9e6a
NF
133(defvar type-break-query-function 'yes-or-no-p
134 "Function to use for making query for a typing break.
135It should take a string as an argument, the prompt.
136Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
137
846f6dd9 138To avoid being queried at all, set `type-break-query-mode' to `nil'.")
b4dc9e6a
NF
139
140(defvar type-break-query-interval 60
141 "*Number of seconds between queries to take a break, if put off.
142The user will continue to be prompted at this interval until he or she
143finally submits to taking a typing break.")
be8d412c 144
e7b20417
NF
145(defvar type-break-time-warning-intervals '(300 120 60 30)
146 "*List of time intervals for warnings about upcoming typing break.
147At each of the intervals (specified in seconds) away from a scheduled
148typing 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.
152At each of the intervals (specified in keystrokes) away from the upper
153keystroke threshold, print a warning in the echo area.
154If either this variable or the upper threshold is set, then no warnings
155Will occur.")
156
e7b20417
NF
157(defvar type-break-warning-repeat 40
158 "*Number of keystrokes for which warnings should be repeated.
159That is, for each of this many keystrokes the warning is redisplayed
160in the echo area to make sure it's really seen.")
161
cc669dd8 162(defvar type-break-demo-functions
e7b20417 163 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi)
cc669dd8 164 "*List of functions to consider running as demos during typing breaks.
458401b6
NF
165When a typing break begins, one of these functions is selected randomly
166to have emacs do something interesting.
622aca7c 167
cc669dd8 168Any function in this list should start a demo which ceases as soon as a
458401b6 169key is pressed.")
622aca7c 170
846f6dd9 171(defvar type-break-post-command-hook '(type-break-check)
b4dc9e6a
NF
172 "Hook run indirectly by post-command-hook for typing break functions.
173This is not really intended to be set by the user, but it's probably
174harmless to do so. Mainly it is used by various parts of the typing break
175program to delay actions until after the user has completed some command.
176It exists because `post-command-hook' itself is inaccessible while its
177functions are being run, and some type-break--related functions want to
178remove themselves after running.")
e7b20417 179
846f6dd9
NF
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.
185Otherwise, messages typically go in the echo area.
186
187See 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
216This variable, in conjunction with `type-break-warning-countdown-string-type'
217(which indicates whether this value is a number of keystrokes or seconds)
218is 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'.
222It will be either \"seconds\" or \"keystrokes\".")
223
224\f
4cf64c15 225;; These are internal variables. Do not set them yourself.
622aca7c 226
defa7346 227(defvar type-break-alarm-p nil)
be8d412c 228(defvar type-break-keystroke-count 0)
be8d412c
NF
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))
e7b20417
NF
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)
cc669dd8 236
846f6dd9
NF
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))
b4dc9e6a 267
cc669dd8 268\f
4cf64c15
NF
269;;;###autoload
270(defun type-break-mode (&optional prefix)
271 "Enable or disable typing-break mode.
272This is a minor mode, but it is global to all buffers by default.
273
274When this mode is enabled, the user is encouraged to take typing breaks at
275appropriate intervals; either after a specified amount of time or when the
276user has exceeded a keystroke threshold. When the time arrives, the user
277is asked to take a break. If the user refuses at that time, emacs will ask
278again in a short period of time. The idea is to give the user enough time
279to find a good breaking point in his or her work, but be sufficiently
280annoying to discourage putting typing breaks off indefinitely.
281
4cf64c15 282A negative prefix argument disables this mode.
cc669dd8 283No argument or any non-negative argument enables it.
4cf64c15
NF
284
285The user may enable or disable this mode by setting the variable of the
286same name, though setting it in that way doesn't reschedule a break or
287reset the keystroke counter.
288
be8d412c
NF
289If the mode was previously disabled and is enabled as a consequence of
290calling this function, it schedules a break with `type-break-schedule' to
291make sure one occurs (the user can call that command to reschedule the
292break at any time). It also initializes the keystroke counter.
4cf64c15
NF
293
294The variable `type-break-interval' specifies the number of seconds to
295schedule between regular typing breaks. This variable doesn't directly
296affect the time schedule; it simply provides a default for the
297`type-break-schedule' command.
298
cc669dd8
NF
299If set, the variable `type-break-good-rest-interval' specifies the minimum
300amount of time which is considered a reasonable typing break. Whenever
301that time has elapsed, typing breaks are automatically rescheduled for
302later even if emacs didn't prompt you to take one first. Also, if a break
303is ended before this much time has elapsed, the user will be asked whether
304or not to continue.
be8d412c
NF
305
306The variable `type-break-keystroke-threshold' is used to determine the
307thresholds at which typing breaks should be considered. You can use
b4dc9e6a 308the command `type-break-guesstimate-keystroke-threshold' to try to
be8d412c 309approximate good values for this.
4cf64c15 310
b4dc9e6a
NF
311There are several variables that affect how or when warning messages about
312imminent typing breaks are displayed. They include:
313
846f6dd9 314 type-break-mode-line-message-mode
b4dc9e6a
NF
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
846f6dd9
NF
321There are several variables that affect if, how, and when queries to begin
322a typing break occur. They include:
b4dc9e6a 323
846f6dd9 324 type-break-query-mode
b4dc9e6a
NF
325 type-break-query-function
326 type-break-query-interval
327
be8d412c 328Finally, the command `type-break-statistics' prints interesting things."
4cf64c15 329 (interactive "P")
846f6dd9 330 (type-break-check-post-command-hook)
4cf64c15 331
be8d412c 332 (let ((already-enabled type-break-mode))
cc669dd8 333 (setq type-break-mode (>= (prefix-numeric-value prefix) 0))
be8d412c
NF
334
335 (cond
336 ((and already-enabled type-break-mode)
337 (and (interactive-p)
846f6dd9 338 (message "type-break-mode is already enabled")))
be8d412c 339 (type-break-mode
846f6dd9
NF
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))))
e7b20417 347 (type-break-keystroke-reset)
846f6dd9 348 (type-break-mode-line-countdown-or-break nil)
be8d412c
NF
349 (type-break-schedule)
350 (and (interactive-p)
351 (message "type-break-mode is enabled and reset")))
846f6dd9
NF
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")))))
4cf64c15
NF
358 type-break-mode)
359
846f6dd9
NF
360(defun type-break-mode-line-message-mode (&optional prefix)
361 "Enable or disable warnings in the mode line about typing breaks.
362
363A negative prefix argument disables this mode.
364No argument or any non-negative argument enables it.
365
366The user may also enable or disable this mode simply by setting the
367variable of the same name.
368
369Variables 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
387When enabled, the user is periodically queried about whether to take a
388typing break at that moment. The function which does this query is
389specified by the variable `type-break-query-function'.
b4dc9e6a
NF
390
391A negative prefix argument disables this mode.
392No argument or any non-negative argument enables it.
393
394The user may also enable or disable this mode simply by setting the
395variable of the same name."
396 (interactive "P")
846f6dd9
NF
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)
b4dc9e6a 404
846f6dd9 405\f
622aca7c 406;;;###autoload
458401b6
NF
407(defun type-break ()
408 "Take a typing break.
409
4cf64c15 410During the break, a demo selected from the functions listed in
cc669dd8 411`type-break-demo-functions' is run.
458401b6 412
4cf64c15 413After the typing break is finished, the next break is scheduled
cc669dd8 414as per the function `type-break-schedule'."
622aca7c 415 (interactive)
e7b20417 416 (type-break-cancel-schedule)
cc669dd8
NF
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))))
be8d412c 436
cc669dd8
NF
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))
b4dc9e6a
NF
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))
e7b20417 449 ((funcall
cc669dd8
NF
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)))))
4cf64c15 457
e7b20417 458 (type-break-keystroke-reset)
846f6dd9 459 (type-break-mode-line-countdown-or-break nil)
be8d412c 460 (type-break-schedule))
622aca7c 461
458401b6 462\f
458401b6 463(defun type-break-schedule (&optional time)
defa7346
NF
464 "Schedule a typing break for TIME seconds from now.
465If time is not specified, default to `type-break-interval'."
466 (interactive (list (and current-prefix-arg
467 (prefix-numeric-value current-prefix-arg))))
458401b6 468 (or time (setq time type-break-interval))
846f6dd9 469 (type-break-check-post-command-hook)
4cf64c15 470 (type-break-cancel-schedule)
e7b20417 471 (type-break-time-warning-schedule time 'reset)
846f6dd9 472 (type-break-run-at-time (max 1 time) nil 'type-break-alarm)
defa7346
NF
473 (setq type-break-time-next-break
474 (type-break-time-sum (current-time) time)))
622aca7c 475
4cf64c15 476(defun type-break-cancel-schedule ()
e7b20417 477 (type-break-cancel-time-warning-schedule)
846f6dd9 478 (type-break-cancel-function-timers 'type-break-alarm)
be8d412c
NF
479 (setq type-break-alarm-p nil)
480 (setq type-break-time-next-break nil))
458401b6 481
e7b20417 482(defun type-break-time-warning-schedule (&optional time resetp)
b4dc9e6a 483 (let ((type-break-current-time-warning-interval nil))
e7b20417 484 (type-break-cancel-time-warning-schedule))
846f6dd9 485 (add-hook 'type-break-post-command-hook 'type-break-time-warning 'append)
e7b20417
NF
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
b4dc9e6a
NF
507 ;(let (type-break-current-time-warning-interval)
508 ; (type-break-cancel-time-warning-schedule))
846f6dd9 509 (type-break-run-at-time (max 1 time) nil 'type-break-time-warning-alarm)
b4dc9e6a
NF
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"))))))))
e7b20417
NF
517
518(defun type-break-cancel-time-warning-schedule ()
846f6dd9 519 (type-break-cancel-function-timers 'type-break-time-warning-alarm)
e7b20417
NF
520 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
521 (setq type-break-current-time-warning-interval
b4dc9e6a
NF
522 type-break-time-warning-intervals)
523 (setq type-break-warning-countdown-string nil))
e7b20417 524
4cf64c15 525(defun type-break-alarm ()
846f6dd9
NF
526 (type-break-check-post-command-hook)
527 (setq type-break-alarm-p t)
528 (type-break-mode-line-countdown-or-break 'break))
458401b6 529
e7b20417 530(defun type-break-time-warning-alarm ()
846f6dd9 531 (type-break-check-post-command-hook)
e7b20417
NF
532 (type-break-time-warning-schedule)
533 (setq type-break-time-warning-count type-break-warning-repeat)
846f6dd9
NF
534 (type-break-time-warning)
535 (type-break-mode-line-countdown-or-break 'countdown))
e7b20417
NF
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
458401b6 542(defun type-break-check ()
4cf64c15
NF
543 "Ask to take a typing break if appropriate.
544This may be the case either because the scheduled time has come \(and the
545minimum keystroke threshold has been reached\) or because the maximum
546keystroke threshold has been exceeded."
e7b20417
NF
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)
846f6dd9 556 (type-break-mode-line-countdown-or-break nil)
e7b20417
NF
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
b4dc9e6a
NF
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)))))))
e7b20417 571
e7b20417
NF
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
b4dc9e6a 582 ;; max-threshold, lower it to reduce the likelihood of an
e7b20417
NF
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.
846f6dd9 604(defun type-break-check-keystroke-warning ()
b4dc9e6a
NF
605 ;; This is safe because the caller should have checked that the cdr was
606 ;; non-nil already.
e7b20417
NF
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)
b4dc9e6a
NF
621 (setq type-break-warning-countdown-string (number-to-string left))
622 (setq type-break-warning-countdown-string-type "keystrokes")
846f6dd9 623 (type-break-mode-line-countdown-or-break 'countdown)
e7b20417 624 t))))
4cf64c15 625
b4dc9e6a 626;; Arrange for a break query to be made, when the user stops typing furiously.
4cf64c15 627(defun type-break-query ()
b4dc9e6a
NF
628 (add-hook 'type-break-post-command-hook 'type-break-do-query))
629
b4dc9e6a
NF
630(defun type-break-do-query ()
631 (cond
846f6dd9
NF
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)
b4dc9e6a
NF
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))))
458401b6 652
846f6dd9
NF
653(defun type-break-noninteractive-query (&optional ignored-args)
654 "Null query function which doesn't interrupt user and assumes `no'.
655It prints a reminder in the echo area to take a break, but doesn't enforce
656this 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
e7b20417
NF
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)
b4dc9e6a
NF
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.
846f6dd9
NF
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'.
b4dc9e6a 682 ((memq this-command '(digit-argument universal-argument)))
846f6dd9 683 ((not type-break-mode-line-message-mode)
b4dc9e6a
NF
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))))))
e7b20417 690 (t
b4dc9e6a
NF
691 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
692 (setq type-break-warning-countdown-string nil))))
e7b20417
NF
693
694(defun type-break-keystroke-warning ()
695 (cond
696 ((> type-break-keystroke-warning-count 0)
b4dc9e6a
NF
697 (setq type-break-warning-countdown-string
698 (number-to-string (- (cdr type-break-keystroke-threshold)
699 type-break-keystroke-count)))
e7b20417
NF
700 (cond
701 ((eq (selected-window) (minibuffer-window)))
846f6dd9
NF
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)
e7b20417
NF
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
b4dc9e6a
NF
716 'type-break-keystroke-warning)
717 (setq type-break-warning-countdown-string nil))))
458401b6 718
846f6dd9
NF
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
458401b6 739\f
be8d412c
NF
740;;;###autoload
741(defun type-break-statistics ()
742 "Print statistics about typing breaks in a temporary buffer.
743This includes the last time a typing break was taken, when the next one is
744scheduled, 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
b4dc9e6a 748Typing break mode is currently %s.
846f6dd9
NF
749Interactive query for breaks is %s.
750Warnings of imminent typing breaks in mode line is %s.
b4dc9e6a
NF
751
752Last typing break ended : %s
be8d412c
NF
753Next scheduled typing break : %s\n
754Minimum keystroke threshold : %s
755Maximum keystroke threshold : %s
756Current keystroke count : %s"
b4dc9e6a 757 (if type-break-mode "enabled" "disabled")
846f6dd9
NF
758 (if type-break-query-mode "enabled" "disabled")
759 (if type-break-mode-line-message-mode "enabled" "disabled")
be8d412c
NF
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)
6b62b567 764 (format "%s\t(%s from now)"
be8d412c 765 (current-time-string type-break-time-next-break)
e7b20417 766 (type-break-format-time
cc669dd8 767 (type-break-time-difference
e7b20417 768 (current-time)
cc669dd8 769 type-break-time-next-break)))
be8d412c
NF
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
b4dc9e6a 776(defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac)
be8d412c 777 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
b4dc9e6a 778
be8d412c 779If called interactively, the user is prompted for their guess as to how
b4dc9e6a
NF
780many words per minute they usually type. This value should not be your
781maximum WPM, but your average. Of course, this is harder to gauge since it
782can vary considerably depending on what you are doing. For example, one
846f6dd9 783tends to type less when debugging a program as opposed to writing
b4dc9e6a
NF
784documentation. (Perhaps a separate program should be written to estimate
785average typing speed.)
786
787From that, this command sets the values in `type-break-keystroke-threshold'
788based on a fairly simple algorithm involving assumptions about the average
789length of words (5). For the minimum threshold, it uses about a fifth of
790the computed maximum threshold.
be8d412c
NF
791
792When called from lisp programs, the optional args WORDLEN and FRAC can be
793used to override the default assumption about average word length and the
794fraction of the maximum threshold to which to set the minimum threshold.
795FRAC should be the inverse of the fractional value; for example, a value of
7962 would mean to use one half, a value of 4 would mean to use one quarter, etc."
b4dc9e6a 797 (interactive "NOn average, how many words per minute do you type? ")
be8d412c 798 (let* ((upper (* wpm (or wordlen 5) (/ type-break-interval 60)))
f486195c 799 (lower (/ upper (or frac 5))))
be8d412c
NF
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
e7b20417
NF
809;;; misc functions
810
811;; Compute the difference, in seconds, between a and b, two structures
812;; similar to those returned by `current-time'.
defa7346
NF
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.
846f6dd9 816(defun type-break-time-difference (a b)
e7b20417
NF
817 (+ (lsh (- (car b) (car a)) 16)
818 (- (car (cdr b)) (car (cdr a)))))
819
defa7346
NF
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
846f6dd9 826;; "low" bits and format the time incorrectly.
defa7346
NF
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
846f6dd9 857(defun type-break-format-time (secs)
e7b20417
NF
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
846f6dd9
NF
872(defun type-break-force-mode-line-update (&optional all)
873 "Force the mode-line of the current buffer to be redisplayed.
874With 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
e7b20417
NF
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
b4dc9e6a 966 (message "%s" (get 'life-extinct 'error-message))
e7b20417
NF
967 (sit-for 3)
968 ;; restart demo
969 (setq continue t))
970 (quit
971 (and (get-buffer "*Life*")
972 (kill-buffer "*Life*")))))))
973
defa7346 974;; Boring demo, but doesn't use many cycles
e7b20417
NF
975(defun type-break-demo-boring ()
976 "Boring typing break demo."
defa7346 977 (let ((rmsg "Press any key to resume from typing break")
e7b20417 978 (buffer-name "*Typing Break Buffer*")
defa7346
NF
979 line col pos
980 elapsed timeleft tmsg)
e7b20417
NF
981 (condition-case ()
982 (progn
983 (switch-to-buffer (get-buffer-create buffer-name))
984 (buffer-disable-undo (current-buffer))
985 (erase-buffer)
defa7346
NF
986 (setq line (1+ (/ (window-height) 2)))
987 (setq col (/ (- (window-width) (length rmsg)) 2))
e7b20417
NF
988 (insert (make-string line ?\C-j)
989 (make-string col ?\ )
defa7346
NF
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))
e7b20417
NF
1017 (read-char)
1018 (kill-buffer buffer-name))
1019 (quit
1020 (and (get-buffer buffer-name)
1021 (kill-buffer buffer-name))))))
1022
1023\f
458401b6
NF
1024(provide 'type-break)
1025
458401b6 1026;;; type-break.el ends here