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