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