1 ;;; type-break.el --- encourage rests from typing at appropriate intervals
3 ;; Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman
6 ;; Maintainer: Noah Friedman <friedman@splode.com>
7 ;; Keywords: extensions, timers
8 ;; Status: Works in GNU Emacs 19.25 or later, some versions of XEmacs
11 ;; $Id: type-break.el,v 1.22 1999/04/27 19:00:42 fx Exp kwzh $
13 ;; This file is part of GNU Emacs.
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)
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.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
32 ;; The docstring for the function `type-break-mode' summarizes most of the
33 ;; details of the interface.
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
42 ;; If you prefer not to be queried about taking breaks, but instead just
43 ;; want to be reminded, do the following:
45 ;; (setq type-break-query-mode nil)
47 ;; Or call the command `type-break-query-mode' with a negative prefix
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'.
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.
59 ;; This program has no hope of working in Emacs 18.
61 ;; This package was inspired by Roland McGrath's hanoi-break.el.
62 ;; Several people contributed feedback and ideas, including
63 ;; Roland McGrath <roland@gnu.org>
64 ;; Kleanthes Koniaris <kgk@martigny.ai.mit.edu>
65 ;; Mark Ashton <mpashton@gnu.org>
66 ;; Matt Wilding <wilding@cli.com>
67 ;; Robert S. Boyer <boyer@cs.utexas.edu>
72 (defgroup type-break nil
73 "Encourage the user to take a rest from typing at suitable intervals."
78 (defcustom type-break-mode nil
79 "Toggle typing break mode.
80 See the docstring for the `type-break-mode' command for more information.
81 Setting this variable directly does not take effect;
82 use either \\[customize] or the function `type-break-mode'."
83 :set
(lambda (symbol value
)
84 (type-break-mode (if value
1 -
1)))
85 :initialize
'custom-initialize-default
91 (defcustom type-break-interval
(* 60 60)
92 "*Number of seconds between scheduled typing breaks."
97 (defcustom type-break-good-rest-interval
(/ type-break-interval
6)
98 "*Number of seconds of idle time considered to be an adequate typing rest.
100 When this variable is non-`nil', emacs checks the idle time between
101 keystrokes. If this idle time is long enough to be considered a \"good\"
102 rest from typing, then the next typing break is simply rescheduled for later.
104 If a break is interrupted before this much time elapses, the user will be
105 asked whether or not really to interrupt the break."
110 (defcustom type-break-keystroke-threshold
111 ;; Assuming typing speed is 35wpm (on the average, do you really
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
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.
118 ;; Conversely, the minimum threshold should be about a fifth of this.
121 (upper (* wpm avg-word-length
(/ type-break-interval
60)))
124 "*Upper and lower bound on number of keystrokes for considering typing break.
125 This structure is a pair of numbers (MIN . MAX).
127 The first number is the minimum number of keystrokes that must have been
128 entered since the last typing break before considering another one, even if
129 the scheduled time has elapsed; the break is simply rescheduled until later
130 if the minimum threshold hasn't been reached. If this first value is nil,
131 then there is no minimum threshold; as soon as the scheduled time has
132 elapsed, the user will always be queried.
134 The second number is the maximum number of keystrokes that can be entered
135 before a typing break is requested immediately, pre-empting the originally
136 scheduled break. If this second value is nil, then no pre-emptive breaks
137 will occur; only scheduled ones will.
139 Keys with bucky bits (shift, control, meta, etc) are counted as only one
140 keystroke even though they really require multiple keys to generate them.
142 The command `type-break-guesstimate-keystroke-threshold' can be used to
143 guess a reasonably good pair of values for this variable."
147 (defcustom type-break-query-mode t
148 "*Non-`nil' means ask whether or not to prompt user for breaks.
149 If so, call the function specified in the value of the variable
150 `type-break-query-function' to do the asking."
154 (defvar type-break-query-function
'yes-or-no-p
155 "Function to use for making query for a typing break.
156 It should take a string as an argument, the prompt.
157 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
159 To avoid being queried at all, set `type-break-query-mode' to `nil'.")
161 (defcustom type-break-query-interval
60
162 "*Number of seconds between queries to take a break, if put off.
163 The user will continue to be prompted at this interval until he or she
164 finally submits to taking a typing break."
168 (defcustom type-break-time-warning-intervals
'(300 120 60 30)
169 "*List of time intervals for warnings about upcoming typing break.
170 At each of the intervals (specified in seconds) away from a scheduled
171 typing break, print a warning in the echo area."
172 :type
'(repeat integer
)
175 (defcustom type-break-keystroke-warning-intervals
'(300 200 100 50)
176 "*List of keystroke measurements for warnings about upcoming typing break.
177 At each of the intervals (specified in keystrokes) away from the upper
178 keystroke threshold, print a warning in the echo area.
179 If either this variable or the upper threshold is set, then no warnings
181 :type
'(repeat integer
)
185 (defcustom type-break-warning-repeat
40
186 "*Number of keystrokes for which warnings should be repeated.
187 That is, for each of this many keystrokes the warning is redisplayed
188 in the echo area to make sure it's really seen."
192 (defcustom type-break-demo-functions
193 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi
)
194 "*List of functions to consider running as demos during typing breaks.
195 When a typing break begins, one of these functions is selected randomly
196 to have emacs do something interesting.
198 Any function in this list should start a demo which ceases as soon as a
200 :type
'(repeat function
)
203 (defvar type-break-post-command-hook
'(type-break-check)
204 "Hook run indirectly by post-command-hook for typing break functions.
205 This is not really intended to be set by the user, but it's probably
206 harmless to do so. Mainly it is used by various parts of the typing break
207 program to delay actions until after the user has completed some command.
208 It exists because `post-command-hook' itself is inaccessible while its
209 functions are being run, and some type-break--related functions want to
210 remove themselves after running.")
215 (defcustom type-break-mode-line-message-mode nil
216 "*Non-`nil' means put type-break related messages in the mode line.
217 Otherwise, messages typically go in the echo area.
219 See also `type-break-mode-line-format' and its members."
223 (defvar type-break-mode-line-format
224 '(type-break-mode-line-message-mode
226 type-break-mode-line-break-message
227 type-break-mode-line-warning
))
228 "*Format of messages in the mode line concerning typing breaks.")
230 (defvar type-break-mode-line-break-message
231 '(type-break-mode-line-break-message-p
232 type-break-mode-line-break-string
))
234 (defvar type-break-mode-line-break-message-p nil
)
235 (defvar type-break-mode-line-break-string
" *** TAKE A TYPING BREAK ***")
237 (defvar type-break-mode-line-warning
238 '(type-break-mode-line-break-message-p
240 (type-break-warning-countdown-string
242 type-break-warning-countdown-string
244 type-break-warning-countdown-string-type
247 (defvar type-break-warning-countdown-string nil
248 "If non-nil, this is a countdown for the next typing break.
250 This variable, in conjunction with `type-break-warning-countdown-string-type'
251 (which indicates whether this value is a number of keystrokes or seconds)
252 is installed in mode-line-format to notify of imminent typing breaks.")
254 (defvar type-break-warning-countdown-string-type nil
255 "Indicates the unit type of `type-break-warning-countdown-string'.
256 It will be either \"seconds\" or \"keystrokes\".")
259 ;; These are internal variables. Do not set them yourself.
261 (defvar type-break-alarm-p nil
)
262 (defvar type-break-keystroke-count
0)
263 (defvar type-break-time-last-break nil
)
264 (defvar type-break-time-next-break nil
)
265 (defvar type-break-time-last-command
(current-time))
266 (defvar type-break-current-time-warning-interval nil
)
267 (defvar type-break-current-keystroke-warning-interval nil
)
268 (defvar type-break-time-warning-count
0)
269 (defvar type-break-keystroke-warning-count
0)
271 ;; Constant indicating emacs variant.
272 ;; This can be one of `xemacs', `lucid', `epoch', `mule', etc.
273 (defconst type-break-emacs-variant
274 (let ((data (match-data))
276 ((fboundp 'nemacs-version
)
280 (alist '(("\\bXEmacs\\b" . xemacs
)
281 ("\\bLucid\\b" . lucid
)
282 ("^Nemacs\\b" . nemacs
)
283 ("^GNU Emacs 19" . standard19
)
284 ("^GNU Emacs 20" . standard19
)
285 ("^GNU Emacs 18" . emacs18
)))
289 ((string-match (car (car alist
)) version
)
290 (setq result
(cdr (car alist
)))
293 (setq alist
(cdr alist
)))))
294 (set-match-data data
)
295 (cond ((eq result
'lucid
)
296 (and (string= emacs-version
"19.8 Lucid")
297 (setq result
'lucid-19-8
)))
298 ((memq result
'(nemacs emacs18
))
300 "type-break not supported in this version of emacs.")))
305 (defun type-break-mode (&optional prefix
)
306 "Enable or disable typing-break mode.
307 This is a minor mode, but it is global to all buffers by default.
309 When this mode is enabled, the user is encouraged to take typing breaks at
310 appropriate intervals; either after a specified amount of time or when the
311 user has exceeded a keystroke threshold. When the time arrives, the user
312 is asked to take a break. If the user refuses at that time, emacs will ask
313 again in a short period of time. The idea is to give the user enough time
314 to find a good breaking point in his or her work, but be sufficiently
315 annoying to discourage putting typing breaks off indefinitely.
317 A negative prefix argument disables this mode.
318 No argument or any non-negative argument enables it.
320 The user may enable or disable this mode by setting the variable of the
321 same name, though setting it in that way doesn't reschedule a break or
322 reset the keystroke counter.
324 If the mode was previously disabled and is enabled as a consequence of
325 calling this function, it schedules a break with `type-break-schedule' to
326 make sure one occurs (the user can call that command to reschedule the
327 break at any time). It also initializes the keystroke counter.
329 The variable `type-break-interval' specifies the number of seconds to
330 schedule between regular typing breaks. This variable doesn't directly
331 affect the time schedule; it simply provides a default for the
332 `type-break-schedule' command.
334 If set, the variable `type-break-good-rest-interval' specifies the minimum
335 amount of time which is considered a reasonable typing break. Whenever
336 that time has elapsed, typing breaks are automatically rescheduled for
337 later even if emacs didn't prompt you to take one first. Also, if a break
338 is ended before this much time has elapsed, the user will be asked whether
341 The variable `type-break-keystroke-threshold' is used to determine the
342 thresholds at which typing breaks should be considered. You can use
343 the command `type-break-guesstimate-keystroke-threshold' to try to
344 approximate good values for this.
346 There are several variables that affect how or when warning messages about
347 imminent typing breaks are displayed. They include:
349 `type-break-mode-line-message-mode'
350 `type-break-time-warning-intervals'
351 `type-break-keystroke-warning-intervals'
352 `type-break-warning-repeat'
353 `type-break-warning-countdown-string'
354 `type-break-warning-countdown-string-type'
356 There are several variables that affect if, how, and when queries to begin
357 a typing break occur. They include:
359 `type-break-query-mode'
360 `type-break-query-function'
361 `type-break-query-interval'
363 Finally, the command `type-break-statistics' prints interesting things."
365 (type-break-check-post-command-hook)
367 (let ((already-enabled type-break-mode
))
368 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
371 ((and already-enabled type-break-mode
)
373 (message "Type Break mode is already enabled")))
375 (or global-mode-string
376 (setq global-mode-string
'("")))
377 (or (assq 'type-break-mode-line-message-mode
379 (setq minor-mode-alist
380 (cons type-break-mode-line-format
382 (type-break-keystroke-reset)
383 (type-break-mode-line-countdown-or-break nil
)
384 (type-break-schedule)
386 (message "Type Break mode is enabled and reset")))
388 (type-break-keystroke-reset)
389 (type-break-mode-line-countdown-or-break nil
)
390 (type-break-cancel-schedule)
392 (message "Type Break mode is disabled")))))
395 (defun type-break-mode-line-message-mode (&optional prefix
)
396 "Enable or disable warnings in the mode line about typing breaks.
398 A negative prefix argument disables this mode.
399 No argument or any non-negative argument enables it.
401 The user may also enable or disable this mode simply by setting the
402 variable of the same name.
404 Variables controlling the display of messages in the mode line include:
408 `type-break-mode-line-break-message'
409 `type-break-mode-line-warning'"
411 (setq type-break-mode-line-message-mode
412 (>= (prefix-numeric-value prefix
) 0))
414 (if type-break-mode-line-message-mode
415 (message "type-break-mode-line-message-mode is enabled")
416 (message "type-break-mode-line-message-mode is disabled")))
417 type-break-mode-line-message-mode
)
419 (defun type-break-query-mode (&optional prefix
)
420 "Enable or disable warnings in the mode line about typing breaks.
422 When enabled, the user is periodically queried about whether to take a
423 typing break at that moment. The function which does this query is
424 specified by the variable `type-break-query-function'.
426 A negative prefix argument disables this mode.
427 No argument or any non-negative argument enables it.
429 The user may also enable or disable this mode simply by setting the
430 variable of the same name."
432 (setq type-break-query-mode
433 (>= (prefix-numeric-value prefix
) 0))
435 (if type-break-query-mode
436 (message "type-break-query-mode is enabled")
437 (message "type-break-query-mode is disabled")))
438 type-break-query-mode
)
443 "Take a typing break.
445 During the break, a demo selected from the functions listed in
446 `type-break-demo-functions' is run.
448 After the typing break is finished, the next break is scheduled
449 as per the function `type-break-schedule'."
451 (type-break-cancel-schedule)
453 (start-time (current-time)))
454 (setq type-break-time-last-break start-time
)
456 (save-window-excursion
458 (and (eq (selected-window) (minibuffer-window))
460 (delete-other-windows)
461 (scroll-right (window-width))
462 (message "Press any key to resume from typing break.")
465 (let* ((len (length type-break-demo-functions
))
467 (fn (nth idx type-break-demo-functions
)))
473 (type-break-good-rest-interval
474 (let ((break-secs (type-break-time-difference
475 start-time
(current-time))))
477 ((>= break-secs type-break-good-rest-interval
)
479 ;; 60 seconds may be too much leeway if the break is only 3
480 ;; minutes to begin with. You can just say "no" to the query
481 ;; below if you're in that much of a hurry.
482 ;((> 60 (abs (- break-secs type-break-good-rest-interval)))
483 ; (setq continue nil))
485 type-break-query-function
486 (format "You really ought to rest %s more. Continue break? "
487 (type-break-format-time (- type-break-good-rest-interval
490 (setq continue nil
)))))
491 (t (setq continue nil
)))))
493 (type-break-keystroke-reset)
494 (type-break-mode-line-countdown-or-break nil
)
495 (type-break-schedule))
498 (defun type-break-schedule (&optional time
)
499 "Schedule a typing break for TIME seconds from now.
500 If time is not specified, default to `type-break-interval'."
501 (interactive (list (and current-prefix-arg
502 (prefix-numeric-value current-prefix-arg
))))
503 (or time
(setq time type-break-interval
))
504 (type-break-check-post-command-hook)
505 (type-break-cancel-schedule)
506 (type-break-time-warning-schedule time
'reset
)
507 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
508 (setq type-break-time-next-break
509 (type-break-time-sum (current-time) time
)))
511 (defun type-break-cancel-schedule ()
512 (type-break-cancel-time-warning-schedule)
513 (type-break-cancel-function-timers 'type-break-alarm
)
514 (setq type-break-alarm-p nil
)
515 (setq type-break-time-next-break nil
))
517 (defun type-break-time-warning-schedule (&optional time resetp
)
518 (let ((type-break-current-time-warning-interval nil
))
519 (type-break-cancel-time-warning-schedule))
520 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
522 (type-break-time-warning-intervals
524 (setq type-break-current-time-warning-interval
525 type-break-time-warning-intervals
))
528 (setq time
(type-break-time-difference (current-time)
529 type-break-time-next-break
)))
531 (while (and type-break-current-time-warning-interval
532 (> (car type-break-current-time-warning-interval
) time
))
533 (setq type-break-current-time-warning-interval
534 (cdr type-break-current-time-warning-interval
)))
537 (type-break-current-time-warning-interval
538 (setq time
(- time
(car type-break-current-time-warning-interval
)))
539 (setq type-break-current-time-warning-interval
540 (cdr type-break-current-time-warning-interval
))
542 ;(let (type-break-current-time-warning-interval)
543 ; (type-break-cancel-time-warning-schedule))
544 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
548 (setq type-break-warning-countdown-string nil
))
550 (setq type-break-warning-countdown-string
(number-to-string time
))
551 (setq type-break-warning-countdown-string-type
"seconds"))))))))
553 (defun type-break-cancel-time-warning-schedule ()
554 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
555 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
556 (setq type-break-current-time-warning-interval
557 type-break-time-warning-intervals
)
558 (setq type-break-warning-countdown-string nil
))
560 (defun type-break-alarm ()
561 (type-break-check-post-command-hook)
562 (setq type-break-alarm-p t
)
563 (type-break-mode-line-countdown-or-break 'break
))
565 (defun type-break-time-warning-alarm ()
566 (type-break-check-post-command-hook)
567 (type-break-time-warning-schedule)
568 (setq type-break-time-warning-count type-break-warning-repeat
)
569 (type-break-time-warning)
570 (type-break-mode-line-countdown-or-break 'countdown
))
573 (defun type-break-run-tb-post-command-hook ()
575 (run-hooks 'type-break-post-command-hook
)))
577 (defun type-break-check ()
578 "Ask to take a typing break if appropriate.
579 This may be the case either because the scheduled time has come \(and the
580 minimum keystroke threshold has been reached\) or because the maximum
581 keystroke threshold has been exceeded."
582 (let* ((min-threshold (car type-break-keystroke-threshold
))
583 (max-threshold (cdr type-break-keystroke-threshold
)))
584 (and type-break-good-rest-interval
586 (and (> (type-break-time-difference
587 type-break-time-last-command
(current-time))
588 type-break-good-rest-interval
)
590 (type-break-keystroke-reset)
591 (type-break-mode-line-countdown-or-break nil
)
592 (setq type-break-time-last-break
(current-time))
593 (type-break-schedule)))
594 (setq type-break-time-last-command
(current-time))))
596 (and type-break-keystroke-threshold
597 ;; next line is test for 20.2 that can be deleted
598 ;;(setq type-break-keystroke-count (1+ type-break-keystroke-count))
599 (let ((keys (this-command-keys)))
601 ;; Ignore mouse motion
603 (consp (aref keys
0))
604 (memq (car (aref keys
0)) '(mouse-movement))))
606 (setq type-break-keystroke-count
607 (+ type-break-keystroke-count
(length keys
)))))))
613 ((eq (selected-window) (minibuffer-window)))
615 (< type-break-keystroke-count min-threshold
))
616 (type-break-schedule))
618 ;; If keystroke count is within min-threshold of
619 ;; max-threshold, lower it to reduce the likelihood of an
620 ;; immediate subsequent query.
623 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
625 (type-break-keystroke-reset)
626 (setq type-break-keystroke-count min-threshold
)))
627 (type-break-query))))
628 ((and type-break-keystroke-warning-intervals
630 (= type-break-keystroke-warning-count
0)
631 (type-break-check-keystroke-warning)))
633 (> type-break-keystroke-count max-threshold
)
634 (not (input-pending-p))
635 (not (eq (selected-window) (minibuffer-window))))
636 (type-break-keystroke-reset)
637 (setq type-break-keystroke-count
(or min-threshold
0))
638 (type-break-query)))))
640 ;; This should return t if warnings were enabled, nil otherwise.
641 (defun type-break-check-keystroke-warning ()
642 ;; This is safe because the caller should have checked that the cdr was
644 (let ((left (- (cdr type-break-keystroke-threshold
)
645 type-break-keystroke-count
)))
647 ((null (car type-break-current-keystroke-warning-interval
))
649 ((> left
(car type-break-current-keystroke-warning-interval
))
652 (while (and (car type-break-current-keystroke-warning-interval
)
653 (< left
(car type-break-current-keystroke-warning-interval
)))
654 (setq type-break-current-keystroke-warning-interval
655 (cdr type-break-current-keystroke-warning-interval
)))
656 (setq type-break-keystroke-warning-count type-break-warning-repeat
)
657 (add-hook 'type-break-post-command-hook
'type-break-keystroke-warning
)
658 (setq type-break-warning-countdown-string
(number-to-string left
))
659 (setq type-break-warning-countdown-string-type
"keystrokes")
660 (type-break-mode-line-countdown-or-break 'countdown
)
663 ;; Arrange for a break query to be made, when the user stops typing furiously.
664 (defun type-break-query ()
665 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
667 (defun type-break-do-query ()
669 ((not type-break-query-mode
)
670 (type-break-noninteractive-query)
671 (type-break-schedule type-break-query-interval
)
672 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))
676 ((let ((type-break-mode nil
)
677 ;; yes-or-no-p sets this-command to exit-minibuffer,
678 ;; which hoses undo or yank-pop (if you happened to be
679 ;; yanking just when the query occurred).
680 (this-command this-command
))
681 (funcall type-break-query-function
682 "Take a break from typing now? "))
685 (type-break-schedule type-break-query-interval
)))
687 (type-break-schedule type-break-query-interval
)))
688 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))))
690 (defun type-break-noninteractive-query (&optional ignored-args
)
691 "Null query function which doesn't interrupt user and assumes `no'.
692 It prints a reminder in the echo area to take a break, but doesn't enforce
693 this or ask the user to start one right now."
695 (type-break-mode-line-message-mode)
698 (message "You should take a typing break now. Do `M-x type-break'.")
701 ;; return nil so query caller knows to reset reminder, as if user
702 ;; said "no" in response to yes-or-no-p.
705 (defun type-break-time-warning ()
707 ((and (car type-break-keystroke-threshold
)
708 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
709 ((> type-break-time-warning-count
0)
710 (let ((timeleft (type-break-time-difference (current-time)
711 type-break-time-next-break
)))
712 (setq type-break-warning-countdown-string
(number-to-string timeleft
))
714 ((eq (selected-window) (minibuffer-window)))
715 ;; Do nothing if the command was just a prefix arg, since that will
716 ;; immediately be followed by some other interactive command.
717 ;; Otherwise, it is particularly annoying for the sit-for below to
718 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
719 ((memq this-command
'(digit-argument universal-argument
)))
720 ((not type-break-mode-line-message-mode
)
721 ;; Pause for a moment so any previous message can be seen.
723 (message "Warning: typing break due in %s."
724 (type-break-format-time timeleft
))
725 (setq type-break-time-warning-count
726 (1- type-break-time-warning-count
))))))
728 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
729 (setq type-break-warning-countdown-string nil
))))
731 (defun type-break-keystroke-warning ()
733 ((> type-break-keystroke-warning-count
0)
734 (setq type-break-warning-countdown-string
735 (number-to-string (- (cdr type-break-keystroke-threshold
)
736 type-break-keystroke-count
)))
738 ((eq (selected-window) (minibuffer-window)))
739 ;; Do nothing if the command was just a prefix arg, since that will
740 ;; immediately be followed by some other interactive command.
741 ;; Otherwise, it is particularly annoying for the sit-for below to
742 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
743 ((memq this-command
'(digit-argument universal-argument
)))
744 ((not type-break-mode-line-message-mode
)
746 (message "Warning: typing break due in %s keystrokes."
747 (- (cdr type-break-keystroke-threshold
)
748 type-break-keystroke-count
))
749 (setq type-break-keystroke-warning-count
750 (1- type-break-keystroke-warning-count
)))))
752 (remove-hook 'type-break-post-command-hook
753 'type-break-keystroke-warning
)
754 (setq type-break-warning-countdown-string nil
))))
756 (defun type-break-mode-line-countdown-or-break (&optional type
)
758 ((not type-break-mode-line-message-mode
))
759 ((eq type
'countdown
)
760 ;(setq type-break-mode-line-break-message-p nil)
761 (add-hook 'type-break-post-command-hook
762 'type-break-force-mode-line-update
'append
))
765 (setq type-break-mode-line-break-message-p
766 (not type-break-mode-line-break-message-p
))
767 (remove-hook 'type-break-post-command-hook
768 'type-break-force-mode-line-update
))
770 (setq type-break-mode-line-break-message-p nil
)
771 (setq type-break-warning-countdown-string nil
)
772 (remove-hook 'type-break-post-command-hook
773 'type-break-force-mode-line-update
)))
774 (type-break-force-mode-line-update))
778 (defun type-break-statistics ()
779 "Print statistics about typing breaks in a temporary buffer.
780 This includes the last time a typing break was taken, when the next one is
781 scheduled, the keystroke thresholds and the current keystroke count, etc."
783 (with-output-to-temp-buffer "*Typing Break Statistics*"
784 (princ (format "Typing break statistics\n-----------------------\n
785 Typing break mode is currently %s.
786 Interactive query for breaks is %s.
787 Warnings of imminent typing breaks in mode line is %s.
789 Last typing break ended : %s
790 Next scheduled typing break : %s\n
791 Minimum keystroke threshold : %s
792 Maximum keystroke threshold : %s
793 Current keystroke count : %s"
794 (if type-break-mode
"enabled" "disabled")
795 (if type-break-query-mode
"enabled" "disabled")
796 (if type-break-mode-line-message-mode
"enabled" "disabled")
797 (if type-break-time-last-break
798 (current-time-string type-break-time-last-break
)
800 (if (and type-break-mode type-break-time-next-break
)
801 (format "%s\t(%s from now)"
802 (current-time-string type-break-time-next-break
)
803 (type-break-format-time
804 (type-break-time-difference
806 type-break-time-next-break
)))
808 (or (car type-break-keystroke-threshold
) "none")
809 (or (cdr type-break-keystroke-threshold
) "none")
810 type-break-keystroke-count
))))
813 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
814 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
816 If called interactively, the user is prompted for their guess as to how
817 many words per minute they usually type. This value should not be your
818 maximum WPM, but your average. Of course, this is harder to gauge since it
819 can vary considerably depending on what you are doing. For example, one
820 tends to type less when debugging a program as opposed to writing
821 documentation. (Perhaps a separate program should be written to estimate
822 average typing speed.)
824 From that, this command sets the values in `type-break-keystroke-threshold'
825 based on a fairly simple algorithm involving assumptions about the average
826 length of words (5). For the minimum threshold, it uses about a fifth of
827 the computed maximum threshold.
829 When called from lisp programs, the optional args WORDLEN and FRAC can be
830 used to override the default assumption about average word length and the
831 fraction of the maximum threshold to which to set the minimum threshold.
832 FRAC should be the inverse of the fractional value; for example, a value of
833 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
834 (interactive "NOn average, how many words per minute do you type? ")
835 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
836 (lower (/ upper
(or frac
5))))
837 (or type-break-keystroke-threshold
838 (setq type-break-keystroke-threshold
(cons nil nil
)))
839 (setcar type-break-keystroke-threshold lower
)
840 (setcdr type-break-keystroke-threshold upper
)
842 (message "min threshold: %d\tmax threshold: %d" lower upper
)
843 type-break-keystroke-threshold
)))
848 ;; Compute the difference, in seconds, between a and b, two structures
849 ;; similar to those returned by `current-time'.
850 ;; Use addition rather than logand since that is more robust; the low 16
851 ;; bits of the seconds might have been incremented, making it more than 16
853 (defun type-break-time-difference (a b
)
854 (+ (lsh (- (car b
) (car a
)) 16)
855 (- (car (cdr b
)) (car (cdr a
)))))
857 ;; Return (in a new list the same in structure to that returned by
858 ;; `current-time') the sum of the arguments. Each argument may be a time
859 ;; list or a single integer, a number of seconds.
860 ;; This function keeps the high and low 16 bits of the seconds properly
861 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
862 ;; the result is passed to `current-time-string' it will toss some of the
863 ;; "low" bits and format the time incorrectly.
864 (defun type-break-time-sum (&rest tmlist
)
870 (setq tem
(car tmlist
))
871 (setq tmlist
(cdr tmlist
))
874 (setq low
(+ low tem
)))
876 (setq high
(+ high
(or (car tem
) 0)))
877 (setq low
(+ low
(or (car (cdr tem
)) 0)))
878 (setq micro
(+ micro
(or (car (cdr (cdr tem
))) 0))))))
880 (and (>= micro
1000000)
882 (setq tem
(/ micro
1000000))
883 (setq low
(+ low tem
))
884 (setq micro
(- micro
(* tem
1000000)))))
886 (setq tem
(lsh low -
16))
889 (setq low
(logand low
65535))
890 (setq high
(+ high tem
))))
892 (list high low micro
)))
894 (defun type-break-format-time (secs)
895 (let ((mins (/ secs
60)))
897 ((= mins
1) (format "%d minute" mins
))
898 ((> mins
0) (format "%d minutes" mins
))
899 ((= secs
1) (format "%d second" secs
))
900 (t (format "%d seconds" secs
)))))
902 (defun type-break-keystroke-reset ()
903 (setq type-break-keystroke-count
0)
904 (setq type-break-keystroke-warning-count
0)
905 (setq type-break-current-keystroke-warning-interval
906 type-break-keystroke-warning-intervals
)
907 (remove-hook 'type-break-post-command-hook
'type-break-keystroke-warning
))
909 (defun type-break-force-mode-line-update (&optional all
)
910 "Force the mode-line of the current buffer to be redisplayed.
911 With optional non-nil ALL, force redisplay of all mode-lines."
912 (and all
(save-excursion (set-buffer (other-buffer))))
913 (set-buffer-modified-p (buffer-modified-p)))
915 ;; If an exception occurs in emacs while running the post command hook, the
916 ;; value of that hook is clobbered. This is because the value of the
917 ;; variable is temporarily set to nil while it's running to prevent
918 ;; recursive application, but it also means an exception aborts the routine
919 ;; of restoring it. This function is called from the timers to restore it,
921 (defun type-break-check-post-command-hook ()
922 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
925 ;;; Timer wrapper functions
927 ;;; These shield type-break from variations in the interval timer packages
928 ;;; for different versions of emacs.
930 (defun type-break-run-at-time (time repeat function
)
931 (cond ((eq type-break-emacs-variant
'standard19
)
933 (funcall 'run-at-time time repeat function
))
934 ((eq type-break-emacs-variant
'lucid-19-8
)
935 (let ((name (if (symbolp function
)
936 (symbol-name function
)
939 (funcall 'start-timer name function time repeat
)))
940 ((memq type-break-emacs-variant
'(xemacs lucid
))
941 (let ((name (if (symbolp function
)
942 (symbol-name function
)
945 (funcall 'start-itimer name function time repeat
)))))
947 (defun type-break-cancel-function-timers (function)
948 (cond ((eq type-break-emacs-variant
'standard19
)
949 (let ((timer-dont-exit t
))
950 (funcall 'cancel-function-timers function
)))
951 ((eq type-break-emacs-variant
'lucid-19-8
)
952 (let ((list timer-list
))
954 (and (eq (funcall 'timer-function
(car list
)) function
)
955 (funcall 'delete-timer
(car list
)))
956 (setq list
(cdr list
)))))
957 ((memq type-break-emacs-variant
'(xemacs lucid
))
958 (let ((list itimer-list
))
960 (and (eq (funcall 'itimer-function
(car list
)) function
)
961 (funcall 'delete-itimer
(car list
)))
962 (setq list
(cdr list
)))))))
967 ;; This is a wrapper around hanoi that calls it with an arg large enough to
968 ;; make the largest discs possible that will fit in the window.
969 ;; Also, clean up the *Hanoi* buffer after we're done.
970 (defun type-break-demo-hanoi ()
971 "Take a hanoiing typing break."
972 (and (get-buffer "*Hanoi*")
973 (kill-buffer "*Hanoi*"))
976 (hanoi (/ (window-width) 8))
977 ;; Wait for user to come back.
979 (kill-buffer "*Hanoi*"))
983 (and (get-buffer "*Hanoi*")
984 (kill-buffer "*Hanoi*")))))
986 ;; This is a wrapper around life that calls it with a `sleep' arg to make
987 ;; it run a little more leisurely.
988 ;; Also, clean up the *Life* buffer after we're done.
989 (defun type-break-demo-life ()
990 "Take a typing break and get a life."
994 (and (get-buffer "*Life*")
995 (kill-buffer "*Life*"))
999 ;; wait for user to return
1001 (kill-buffer "*Life*"))
1003 (message "%s" (get 'life-extinct
'error-message
))
1008 (and (get-buffer "*Life*")
1009 (kill-buffer "*Life*")))))))
1011 ;; Boring demo, but doesn't use many cycles
1012 (defun type-break-demo-boring ()
1013 "Boring typing break demo."
1014 (let ((rmsg "Press any key to resume from typing break")
1015 (buffer-name "*Typing Break Buffer*")
1017 elapsed timeleft tmsg
)
1020 (switch-to-buffer (get-buffer-create buffer-name
))
1021 (buffer-disable-undo (current-buffer))
1023 (setq line
(1+ (/ (window-height) 2)))
1024 (setq col
(/ (- (window-width) (length rmsg
)) 2))
1025 (insert (make-string line ?\C-j
)
1026 (make-string col ?\
)
1031 (while (not (input-pending-p))
1032 (delete-region pos
(progn
1036 (setq elapsed
(type-break-time-difference
1037 type-break-time-last-break
1040 (type-break-good-rest-interval
1041 (setq timeleft
(- type-break-good-rest-interval elapsed
))
1043 (setq tmsg
(format "You should rest for %s more"
1044 (type-break-format-time timeleft
)))
1045 (setq tmsg
(format "Typing break has lasted %s"
1046 (type-break-format-time elapsed
)))))
1048 (setq tmsg
(format "Typing break has lasted %s"
1049 (type-break-format-time elapsed
)))))
1050 (setq col
(/ (- (window-width) (length tmsg
)) 2))
1051 (insert (make-string col ?\
) tmsg
)
1052 (goto-char (point-min))
1055 (kill-buffer buffer-name
))
1057 (and (get-buffer buffer-name
)
1058 (kill-buffer buffer-name
))))))
1061 (provide 'type-break
)
1064 (type-break-mode 1))
1065 ;;; type-break.el ends here