1 ;;; type-break.el --- encourage rests from typing at appropriate intervals
3 ;; Copyright (C) 1994, 95, 97, 2000 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.24 2000/07/24 00:49:09 friedman Exp $
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@koniaris.com>
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 (defcustom 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'."
160 :type
'(radio function
161 (function-item yes-or-no-p
)
162 (function-item y-or-n-p
))
165 (defcustom type-break-query-interval
60
166 "*Number of seconds between queries to take a break, if put off.
167 The user will continue to be prompted at this interval until he or she
168 finally submits to taking a typing break."
172 (defcustom type-break-time-warning-intervals
'(300 120 60 30)
173 "*List of time intervals for warnings about upcoming typing break.
174 At each of the intervals (specified in seconds) away from a scheduled
175 typing break, print a warning in the echo area."
176 :type
'(repeat integer
)
179 (defcustom type-break-keystroke-warning-intervals
'(300 200 100 50)
180 "*List of keystroke measurements for warnings about upcoming typing break.
181 At each of the intervals (specified in keystrokes) away from the upper
182 keystroke threshold, print a warning in the echo area.
183 If either this variable or the upper threshold is set, then no warnings
185 :type
'(repeat integer
)
188 (defcustom type-break-warning-repeat
40
189 "*Number of keystrokes for which warnings should be repeated.
190 That is, for each of this many keystrokes the warning is redisplayed
191 in the echo area to make sure it's really seen."
195 (defcustom type-break-time-stamp-format
"[%H:%M] "
196 "*Timestamp format used to prefix messages.
197 Format specifiers are as used by `format-time-string'."
201 (defcustom type-break-demo-functions
202 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi
)
203 "*List of functions to consider running as demos during typing breaks.
204 When a typing break begins, one of these functions is selected randomly
205 to have emacs do something interesting.
207 Any function in this list should start a demo which ceases as soon as a
209 :type
'(repeat function
)
212 (defvar type-break-post-command-hook
'(type-break-check)
213 "Hook run indirectly by post-command-hook for typing break functions.
214 This is not really intended to be set by the user, but it's probably
215 harmless to do so. Mainly it is used by various parts of the typing break
216 program to delay actions until after the user has completed some command.
217 It exists because `post-command-hook' itself is inaccessible while its
218 functions are being run, and some type-break--related functions want to
219 remove themselves after running.")
224 (defcustom type-break-mode-line-message-mode nil
225 "*Non-`nil' means put type-break related messages in the mode line.
226 Otherwise, messages typically go in the echo area.
228 See also `type-break-mode-line-format' and its members."
232 (defvar type-break-mode-line-format
233 '(type-break-mode-line-message-mode
235 type-break-mode-line-break-message
236 type-break-mode-line-warning
))
237 "*Format of messages in the mode line concerning typing breaks.")
239 (defvar type-break-mode-line-break-message
240 '(type-break-mode-line-break-message-p
241 type-break-mode-line-break-string
))
243 (defvar type-break-mode-line-break-message-p nil
)
244 (defvar type-break-mode-line-break-string
" *** TAKE A TYPING BREAK NOW ***")
246 (defvar type-break-mode-line-warning
247 '(type-break-mode-line-break-message-p
249 (type-break-warning-countdown-string
252 type-break-warning-countdown-string
254 type-break-warning-countdown-string-type
257 (defvar type-break-warning-countdown-string nil
258 "If non-nil, this is a countdown for the next typing break.
260 This variable, in conjunction with `type-break-warning-countdown-string-type'
261 (which indicates whether this value is a number of keystrokes or seconds)
262 is installed in mode-line-format to notify of imminent typing breaks.")
264 (defvar type-break-warning-countdown-string-type nil
265 "Indicates the unit type of `type-break-warning-countdown-string'.
266 It will be either \"seconds\" or \"keystrokes\".")
269 ;; These are internal variables. Do not set them yourself.
271 (defvar type-break-alarm-p nil
)
272 (defvar type-break-keystroke-count
0)
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))
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)
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))
286 ((fboundp 'nemacs-version
)
290 (alist '(("\\bXEmacs\\b" . xemacs
)
291 ("\\bLucid\\b" . lucid
)
292 ("^Nemacs\\b" . nemacs
)
293 ("^GNU Emacs 19" . standard19
)
294 ("^GNU Emacs 20" . standard19
)
295 ("^GNU Emacs 18" . emacs18
)))
299 ((string-match (car (car alist
)) version
)
300 (setq result
(cdr (car alist
)))
303 (setq alist
(cdr alist
)))))
304 (set-match-data data
)
305 (cond ((eq result
'lucid
)
306 (and (string= emacs-version
"19.8 Lucid")
307 (setq result
'lucid-19-8
)))
308 ((memq result
'(nemacs emacs18
))
310 "type-break not supported in this version of emacs.")))
315 (defun type-break-mode (&optional prefix
)
316 "Enable or disable typing-break mode.
317 This is a minor mode, but it is global to all buffers by default.
319 When this mode is enabled, the user is encouraged to take typing breaks at
320 appropriate intervals; either after a specified amount of time or when the
321 user has exceeded a keystroke threshold. When the time arrives, the user
322 is asked to take a break. If the user refuses at that time, emacs will ask
323 again in a short period of time. The idea is to give the user enough time
324 to find a good breaking point in his or her work, but be sufficiently
325 annoying to discourage putting typing breaks off indefinitely.
327 A negative prefix argument disables this mode.
328 No argument or any non-negative argument enables it.
330 The user may enable or disable this mode by setting the variable of the
331 same name, though setting it in that way doesn't reschedule a break or
332 reset the keystroke counter.
334 If the mode was previously disabled and is enabled as a consequence of
335 calling this function, it schedules a break with `type-break-schedule' to
336 make sure one occurs (the user can call that command to reschedule the
337 break at any time). It also initializes the keystroke counter.
339 The variable `type-break-interval' specifies the number of seconds to
340 schedule between regular typing breaks. This variable doesn't directly
341 affect the time schedule; it simply provides a default for the
342 `type-break-schedule' command.
344 If set, the variable `type-break-good-rest-interval' specifies the minimum
345 amount of time which is considered a reasonable typing break. Whenever
346 that time has elapsed, typing breaks are automatically rescheduled for
347 later even if emacs didn't prompt you to take one first. Also, if a break
348 is ended before this much time has elapsed, the user will be asked whether
351 The variable `type-break-keystroke-threshold' is used to determine the
352 thresholds at which typing breaks should be considered. You can use
353 the command `type-break-guesstimate-keystroke-threshold' to try to
354 approximate good values for this.
356 There are several variables that affect how or when warning messages about
357 imminent typing breaks are displayed. They include:
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'
366 There are several variables that affect if, how, and when queries to begin
367 a typing break occur. They include:
369 `type-break-query-mode'
370 `type-break-query-function'
371 `type-break-query-interval'
373 Finally, the command `type-break-statistics' prints interesting things."
375 (type-break-check-post-command-hook)
377 (let ((already-enabled type-break-mode
))
378 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
381 ((and already-enabled type-break-mode
)
383 (message "Type Break mode is already enabled")))
385 (or global-mode-string
386 (setq global-mode-string
'("")))
387 (or (assq 'type-break-mode-line-message-mode
389 (setq minor-mode-alist
390 (cons type-break-mode-line-format
392 (type-break-keystroke-reset)
393 (type-break-mode-line-countdown-or-break nil
)
394 (type-break-schedule)
396 (message "Type Break mode is enabled and reset")))
398 (type-break-keystroke-reset)
399 (type-break-mode-line-countdown-or-break nil
)
400 (type-break-cancel-schedule)
402 (message "Type Break mode is disabled")))))
405 (defun type-break-mode-line-message-mode (&optional prefix
)
406 "Enable or disable warnings in the mode line about typing breaks.
408 A negative prefix argument disables this mode.
409 No argument or any non-negative argument enables it.
411 The user may also enable or disable this mode simply by setting the
412 variable of the same name.
414 Variables controlling the display of messages in the mode line include:
418 `type-break-mode-line-break-message'
419 `type-break-mode-line-warning'"
421 (setq type-break-mode-line-message-mode
422 (>= (prefix-numeric-value prefix
) 0))
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
)
429 (defun type-break-query-mode (&optional prefix
)
430 "Enable or disable warnings in the mode line about typing breaks.
432 When enabled, the user is periodically queried about whether to take a
433 typing break at that moment. The function which does this query is
434 specified by the variable `type-break-query-function'.
436 A negative prefix argument disables this mode.
437 No argument or any non-negative argument enables it.
439 The user may also enable or disable this mode simply by setting the
440 variable of the same name."
442 (setq type-break-query-mode
443 (>= (prefix-numeric-value prefix
) 0))
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
)
453 "Take a typing break.
455 During the break, a demo selected from the functions listed in
456 `type-break-demo-functions' is run.
458 After the typing break is finished, the next break is scheduled
459 as per the function `type-break-schedule'."
462 (type-break-cancel-schedule)
464 (start-time (current-time)))
465 (setq type-break-time-last-break start-time
)
467 (save-window-excursion
469 (and (eq (selected-window) (minibuffer-window))
471 (delete-other-windows)
472 (scroll-right (window-width))
473 (message "Press any key to resume from typing break.")
476 (let* ((len (length type-break-demo-functions
))
478 (fn (nth idx type-break-demo-functions
)))
484 (type-break-good-rest-interval
485 (let ((break-secs (type-break-time-difference
486 start-time
(current-time))))
488 ((>= break-secs type-break-good-rest-interval
)
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))
496 type-break-query-function
497 (format "%sYou really ought to rest %s more. Continue break? "
498 (type-break-time-stamp)
499 (type-break-format-time (- type-break-good-rest-interval
502 (setq continue nil
)))))
503 (t (setq continue nil
)))))
505 (type-break-keystroke-reset)
506 (type-break-mode-line-countdown-or-break nil
)
507 (type-break-schedule))
510 (defun type-break-schedule (&optional time
)
511 "Schedule a typing break for TIME seconds from now.
512 If time is not specified, default to `type-break-interval'."
513 (interactive (list (and current-prefix-arg
514 (prefix-numeric-value current-prefix-arg
))))
515 (or time
(setq time type-break-interval
))
516 (type-break-check-post-command-hook)
517 (type-break-cancel-schedule)
518 (type-break-time-warning-schedule time
'reset
)
519 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
520 (setq type-break-time-next-break
521 (type-break-time-sum (current-time) time
)))
523 (defun type-break-cancel-schedule ()
524 (type-break-cancel-time-warning-schedule)
525 (type-break-cancel-function-timers 'type-break-alarm
)
526 (setq type-break-alarm-p nil
)
527 (setq type-break-time-next-break nil
))
529 (defun type-break-time-warning-schedule (&optional time resetp
)
530 (let ((type-break-current-time-warning-interval nil
))
531 (type-break-cancel-time-warning-schedule))
532 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
534 (type-break-time-warning-intervals
536 (setq type-break-current-time-warning-interval
537 type-break-time-warning-intervals
))
540 (setq time
(type-break-time-difference (current-time)
541 type-break-time-next-break
)))
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
)))
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
))
554 ;(let (type-break-current-time-warning-interval)
555 ; (type-break-cancel-time-warning-schedule))
556 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
560 (setq type-break-warning-countdown-string nil
))
562 (setq type-break-warning-countdown-string
(number-to-string time
))
563 (setq type-break-warning-countdown-string-type
"seconds"))))))))
565 (defun type-break-cancel-time-warning-schedule ()
566 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
567 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
568 (setq type-break-current-time-warning-interval
569 type-break-time-warning-intervals
)
570 (setq type-break-warning-countdown-string nil
))
572 (defun type-break-alarm ()
573 (type-break-check-post-command-hook)
574 (setq type-break-alarm-p t
)
575 (type-break-mode-line-countdown-or-break 'break
))
577 (defun type-break-time-warning-alarm ()
578 (type-break-check-post-command-hook)
579 (type-break-time-warning-schedule)
580 (setq type-break-time-warning-count type-break-warning-repeat
)
581 (type-break-time-warning)
582 (type-break-mode-line-countdown-or-break 'countdown
))
585 (defun type-break-run-tb-post-command-hook ()
587 (run-hooks 'type-break-post-command-hook
)))
589 (defun type-break-check ()
590 "Ask to take a typing break if appropriate.
591 This may be the case either because the scheduled time has come \(and the
592 minimum keystroke threshold has been reached\) or because the maximum
593 keystroke threshold has been exceeded."
594 (let* ((min-threshold (car type-break-keystroke-threshold
))
595 (max-threshold (cdr type-break-keystroke-threshold
)))
596 (and type-break-good-rest-interval
598 (and (> (type-break-time-difference
599 type-break-time-last-command
(current-time))
600 type-break-good-rest-interval
)
602 (type-break-keystroke-reset)
603 (type-break-mode-line-countdown-or-break nil
)
604 (setq type-break-time-last-break
(current-time))
605 (type-break-schedule)))
606 (setq type-break-time-last-command
(current-time))))
608 (and type-break-keystroke-threshold
609 (let ((keys (this-command-keys)))
611 ;; Ignore mouse motion
613 (consp (aref keys
0))
614 (memq (car (aref keys
0)) '(mouse-movement))))
616 (setq type-break-keystroke-count
617 (+ type-break-keystroke-count
(length keys
)))))))
623 ((eq (selected-window) (minibuffer-window)))
625 (< type-break-keystroke-count min-threshold
))
626 (type-break-schedule))
628 ;; If keystroke count is within min-threshold of
629 ;; max-threshold, lower it to reduce the likelihood of an
630 ;; immediate subsequent query.
633 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
635 (type-break-keystroke-reset)
636 (setq type-break-keystroke-count min-threshold
)))
637 (type-break-query))))
638 ((and type-break-keystroke-warning-intervals
640 (= type-break-keystroke-warning-count
0)
641 (type-break-check-keystroke-warning)))
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)))))
650 ;; This should return t if warnings were enabled, nil otherwise.
651 (defun type-break-check-keystroke-warning ()
652 ;; This is safe because the caller should have checked that the cdr was
654 (let ((left (- (cdr type-break-keystroke-threshold
)
655 type-break-keystroke-count
)))
657 ((null (car type-break-current-keystroke-warning-interval
))
659 ((> left
(car type-break-current-keystroke-warning-interval
))
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
)
668 (setq type-break-warning-countdown-string
(number-to-string left
))
669 (setq type-break-warning-countdown-string-type
"keystrokes")
670 (type-break-mode-line-countdown-or-break 'countdown
)
673 ;; Arrange for a break query to be made, when the user stops typing furiously.
674 (defun type-break-query ()
675 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
677 (defun type-break-do-query ()
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
))
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
))
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)
695 (funcall type-break-query-function
697 (type-break-time-stamp)
698 "Take a break from typing now? ")))
701 (type-break-schedule type-break-query-interval
)))
703 (type-break-schedule type-break-query-interval
)))
704 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))))
706 (defun type-break-noninteractive-query (&optional ignored-args
)
707 "Null query function which doesn't interrupt user and assumes `no'.
708 It prints a reminder in the echo area to take a break, but doesn't enforce
709 this or ask the user to start one right now."
711 (type-break-mode-line-message-mode)
714 (message "%sYou should take a typing break now. Do `M-x type-break'."
715 (type-break-time-stamp))
718 ;; return nil so query caller knows to reset reminder, as if user
719 ;; said "no" in response to yes-or-no-p.
722 (defun type-break-time-warning ()
724 ((and (car type-break-keystroke-threshold
)
725 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
726 ((> type-break-time-warning-count
0)
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
))
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.
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'.
736 ((memq this-command
'(digit-argument universal-argument
)))
737 ((not type-break-mode-line-message-mode
)
738 ;; Pause for a moment so any previous message can be seen.
740 (message "%sWarning: typing break due in %s."
741 (type-break-time-stamp)
742 (type-break-format-time timeleft
))
743 (setq type-break-time-warning-count
744 (1- type-break-time-warning-count
))))))
746 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
747 (setq type-break-warning-countdown-string nil
))))
749 (defun type-break-keystroke-warning ()
751 ((> type-break-keystroke-warning-count
0)
752 (setq type-break-warning-countdown-string
753 (number-to-string (- (cdr type-break-keystroke-threshold
)
754 type-break-keystroke-count
)))
756 ((eq (selected-window) (minibuffer-window)))
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
)
764 (message "%sWarning: typing break due in %s keystrokes."
765 (type-break-time-stamp)
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
)))))
771 (remove-hook 'type-break-post-command-hook
772 'type-break-keystroke-warning
)
773 (setq type-break-warning-countdown-string nil
))))
775 (defun type-break-mode-line-countdown-or-break (&optional type
)
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
))
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
))
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))
797 (defun type-break-statistics ()
798 "Print statistics about typing breaks in a temporary buffer.
799 This includes the last time a typing break was taken, when the next one is
800 scheduled, the keystroke thresholds and the current keystroke count, etc."
802 (with-output-to-temp-buffer "*Typing Break Statistics*"
803 (princ (format "Typing break statistics\n-----------------------\n
804 Typing break mode is currently %s.
805 Interactive query for breaks is %s.
806 Warnings of imminent typing breaks in mode line is %s.
808 Last typing break ended : %s
809 Next scheduled typing break : %s\n
810 Minimum keystroke threshold : %s
811 Maximum keystroke threshold : %s
812 Current keystroke count : %s"
813 (if type-break-mode
"enabled" "disabled")
814 (if type-break-query-mode
"enabled" "disabled")
815 (if type-break-mode-line-message-mode
"enabled" "disabled")
816 (if type-break-time-last-break
817 (current-time-string type-break-time-last-break
)
819 (if (and type-break-mode type-break-time-next-break
)
820 (format "%s\t(%s from now)"
821 (current-time-string type-break-time-next-break
)
822 (type-break-format-time
823 (type-break-time-difference
825 type-break-time-next-break
)))
827 (or (car type-break-keystroke-threshold
) "none")
828 (or (cdr type-break-keystroke-threshold
) "none")
829 type-break-keystroke-count
))))
832 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
833 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
835 If called interactively, the user is prompted for their guess as to how
836 many words per minute they usually type. This value should not be your
837 maximum WPM, but your average. Of course, this is harder to gauge since it
838 can vary considerably depending on what you are doing. For example, one
839 tends to type less when debugging a program as opposed to writing
840 documentation. (Perhaps a separate program should be written to estimate
841 average typing speed.)
843 From that, this command sets the values in `type-break-keystroke-threshold'
844 based on a fairly simple algorithm involving assumptions about the average
845 length of words (5). For the minimum threshold, it uses about a fifth of
846 the computed maximum threshold.
848 When called from lisp programs, the optional args WORDLEN and FRAC can be
849 used to override the default assumption about average word length and the
850 fraction of the maximum threshold to which to set the minimum threshold.
851 FRAC should be the inverse of the fractional value; for example, a value of
852 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
853 (interactive "NOn average, how many words per minute do you type? ")
854 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
855 (lower (/ upper
(or frac
5))))
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
)
861 (message "min threshold: %d\tmax threshold: %d" lower upper
)
862 type-break-keystroke-threshold
)))
867 ;; Compute the difference, in seconds, between a and b, two structures
868 ;; similar to those returned by `current-time'.
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
872 (defun type-break-time-difference (a b
)
873 (+ (lsh (- (car b
) (car a
)) 16)
874 (- (car (cdr b
)) (car (cdr a
)))))
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
882 ;; "low" bits and format the time incorrectly.
883 (defun type-break-time-sum (&rest tmlist
)
889 (setq tem
(car tmlist
))
890 (setq tmlist
(cdr tmlist
))
893 (setq low
(+ low tem
)))
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))))))
899 (and (>= micro
1000000)
901 (setq tem
(/ micro
1000000))
902 (setq low
(+ low tem
))
903 (setq micro
(- micro
(* tem
1000000)))))
905 (setq tem
(lsh low -
16))
908 (setq low
(logand low
65535))
909 (setq high
(+ high tem
))))
911 (list high low micro
)))
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))))
920 (defun type-break-format-time (secs)
921 (let ((mins (/ secs
60)))
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
)))))
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
))
935 (defun type-break-force-mode-line-update (&optional all
)
936 "Force the mode-line of the current buffer to be redisplayed.
937 With 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)))
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,
947 (defun type-break-check-post-command-hook ()
948 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
951 ;;; Timer wrapper functions
953 ;;; These shield type-break from variations in the interval timer packages
954 ;;; for different versions of emacs.
956 (defun type-break-run-at-time (time repeat function
)
957 (cond ((eq type-break-emacs-variant
'standard19
)
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
)
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
)
971 (funcall 'start-itimer name function time repeat
)))))
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
))
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
))
986 (and (eq (funcall 'itimer-function
(car list
)) function
)
987 (funcall 'delete-itimer
(car list
)))
988 (setq list
(cdr list
)))))))
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*"))
1002 (hanoi (/ (window-width) 8))
1003 ;; Wait for user to come back.
1005 (kill-buffer "*Hanoi*"))
1009 (and (get-buffer "*Hanoi*")
1010 (kill-buffer "*Hanoi*")))))
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."
1020 (and (get-buffer "*Life*")
1021 (kill-buffer "*Life*"))
1025 ;; wait for user to return
1027 (kill-buffer "*Life*"))
1029 (message "%s" (get 'life-extinct
'error-message
))
1034 (and (get-buffer "*Life*")
1035 (kill-buffer "*Life*")))))))
1037 ;; Boring demo, but doesn't use many cycles
1038 (defun type-break-demo-boring ()
1039 "Boring typing break demo."
1040 (let ((rmsg "Press any key to resume from typing break")
1041 (buffer-name "*Typing Break Buffer*")
1043 elapsed timeleft tmsg
)
1046 (switch-to-buffer (get-buffer-create buffer-name
))
1047 (buffer-disable-undo (current-buffer))
1049 (setq line
(1+ (/ (window-height) 2)))
1050 (setq col
(/ (- (window-width) (length rmsg
)) 2))
1051 (insert (make-string line ?\C-j
)
1052 (make-string col ?\
)
1057 (while (not (input-pending-p))
1058 (delete-region pos
(progn
1062 (setq elapsed
(type-break-time-difference
1063 type-break-time-last-break
1066 (type-break-good-rest-interval
1067 (setq timeleft
(- type-break-good-rest-interval elapsed
))
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
)))))
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))
1081 (kill-buffer buffer-name
))
1083 (and (get-buffer buffer-name
)
1084 (kill-buffer buffer-name
))))))
1087 (provide 'type-break
)
1090 (type-break-mode 1))
1092 ;;; type-break.el ends here