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.23 2000/03/13 11:16:00 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."
79 (defcustom type-break-mode nil
80 "Toggle typing break mode.
81 See the docstring for the `type-break-mode' command for more information.
82 Setting this variable directly does not take effect;
83 use either \\[customize] or the function `type-break-mode'."
84 :set
(lambda (symbol value
)
85 (type-break-mode (if value
1 -
1)))
86 :initialize
'custom-initialize-default
92 (defcustom type-break-interval
(* 60 60)
93 "*Number of seconds between scheduled typing breaks."
98 (defcustom type-break-good-rest-interval
(/ type-break-interval
6)
99 "*Number of seconds of idle time considered to be an adequate typing rest.
101 When this variable is non-`nil', emacs checks the idle time between
102 keystrokes. If this idle time is long enough to be considered a \"good\"
103 rest from typing, then the next typing break is simply rescheduled for later.
105 If a break is interrupted before this much time elapses, the user will be
106 asked whether or not really to interrupt the break."
111 (defcustom type-break-keystroke-threshold
112 ;; Assuming typing speed is 35wpm (on the average, do you really
113 ;; type more than that in a minute? I spend a lot of time reading mail
114 ;; and simply studying code in buffers) and average word length is
115 ;; about 5 letters, default upper threshold to the average number of
116 ;; keystrokes one is likely to type in a break interval. That way if the
117 ;; user goes through a furious burst of typing activity, cause a typing
118 ;; break to be required sooner than originally scheduled.
119 ;; Conversely, the minimum threshold should be about a fifth of this.
122 (upper (* wpm avg-word-length
(/ type-break-interval
60)))
125 "*Upper and lower bound on number of keystrokes for considering typing break.
126 This structure is a pair of numbers (MIN . MAX).
128 The first number is the minimum number of keystrokes that must have been
129 entered since the last typing break before considering another one, even if
130 the scheduled time has elapsed; the break is simply rescheduled until later
131 if the minimum threshold hasn't been reached. If this first value is nil,
132 then there is no minimum threshold; as soon as the scheduled time has
133 elapsed, the user will always be queried.
135 The second number is the maximum number of keystrokes that can be entered
136 before a typing break is requested immediately, pre-empting the originally
137 scheduled break. If this second value is nil, then no pre-emptive breaks
138 will occur; only scheduled ones will.
140 Keys with bucky bits (shift, control, meta, etc) are counted as only one
141 keystroke even though they really require multiple keys to generate them.
143 The command `type-break-guesstimate-keystroke-threshold' can be used to
144 guess a reasonably good pair of values for this variable."
148 (defcustom type-break-query-mode t
149 "*Non-`nil' means ask whether or not to prompt user for breaks.
150 If so, call the function specified in the value of the variable
151 `type-break-query-function' to do the asking."
155 (defcustom type-break-query-function
'yes-or-no-p
156 "*Function to use for making query for a typing break.
157 It should take a string as an argument, the prompt.
158 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
160 To avoid being queried at all, set `type-break-query-mode' to `nil'."
161 :type
'(radio function
162 (function-item yes-or-no-p
)
163 (function-item y-or-n-p
))
166 (defcustom type-break-query-interval
60
167 "*Number of seconds between queries to take a break, if put off.
168 The user will continue to be prompted at this interval until he or she
169 finally submits to taking a typing break."
173 (defcustom type-break-time-warning-intervals
'(300 120 60 30)
174 "*List of time intervals for warnings about upcoming typing break.
175 At each of the intervals (specified in seconds) away from a scheduled
176 typing break, print a warning in the echo area."
177 :type
'(repeat integer
)
180 (defcustom type-break-keystroke-warning-intervals
'(300 200 100 50)
181 "*List of keystroke measurements for warnings about upcoming typing break.
182 At each of the intervals (specified in keystrokes) away from the upper
183 keystroke threshold, print a warning in the echo area.
184 If either this variable or the upper threshold is set, then no warnings
186 :type
'(repeat integer
)
189 (defcustom type-break-warning-repeat
40
190 "*Number of keystrokes for which warnings should be repeated.
191 That is, for each of this many keystrokes the warning is redisplayed
192 in the echo area to make sure it's really seen."
196 (defcustom type-break-time-stamp-format
"[%H:%M] "
197 "*Timestamp format used to prefix messages.
198 Format specifiers are as used by `format-time-string'."
202 (defcustom type-break-demo-functions
203 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi
)
204 "*List of functions to consider running as demos during typing breaks.
205 When a typing break begins, one of these functions is selected randomly
206 to have emacs do something interesting.
208 Any function in this list should start a demo which ceases as soon as a
210 :type
'(repeat function
)
213 (defvar type-break-post-command-hook
'(type-break-check)
214 "Hook run indirectly by post-command-hook for typing break functions.
215 This is not really intended to be set by the user, but it's probably
216 harmless to do so. Mainly it is used by various parts of the typing break
217 program to delay actions until after the user has completed some command.
218 It exists because `post-command-hook' itself is inaccessible while its
219 functions are being run, and some type-break--related functions want to
220 remove themselves after running.")
225 (defcustom type-break-mode-line-message-mode nil
226 "*Non-`nil' means put type-break related messages in the mode line.
227 Otherwise, messages typically go in the echo area.
229 See also `type-break-mode-line-format' and its members."
233 (defvar type-break-mode-line-format
234 '(type-break-mode-line-message-mode
236 type-break-mode-line-break-message
237 type-break-mode-line-warning
))
238 "*Format of messages in the mode line concerning typing breaks.")
240 (defvar type-break-mode-line-break-message
241 '(type-break-mode-line-break-message-p
242 type-break-mode-line-break-string
))
244 (defvar type-break-mode-line-break-message-p nil
)
245 (defvar type-break-mode-line-break-string
" *** TAKE A TYPING BREAK NOW ***")
247 (defvar type-break-mode-line-warning
248 '(type-break-mode-line-break-message-p
250 (type-break-warning-countdown-string
253 type-break-warning-countdown-string
255 type-break-warning-countdown-string-type
258 (defvar type-break-warning-countdown-string nil
259 "If non-nil, this is a countdown for the next typing break.
261 This variable, in conjunction with `type-break-warning-countdown-string-type'
262 (which indicates whether this value is a number of keystrokes or seconds)
263 is installed in mode-line-format to notify of imminent typing breaks.")
265 (defvar type-break-warning-countdown-string-type nil
266 "Indicates the unit type of `type-break-warning-countdown-string'.
267 It will be either \"seconds\" or \"keystrokes\".")
270 ;; These are internal variables. Do not set them yourself.
272 (defvar type-break-alarm-p nil
)
273 (defvar type-break-keystroke-count
0)
274 (defvar type-break-time-last-break nil
)
275 (defvar type-break-time-next-break nil
)
276 (defvar type-break-time-last-command
(current-time))
277 (defvar type-break-current-time-warning-interval nil
)
278 (defvar type-break-current-keystroke-warning-interval nil
)
279 (defvar type-break-time-warning-count
0)
280 (defvar type-break-keystroke-warning-count
0)
282 ;; Constant indicating emacs variant.
283 ;; This can be one of `xemacs', `lucid', `epoch', `mule', etc.
284 (defconst type-break-emacs-variant
285 (let ((data (match-data))
287 ((fboundp 'nemacs-version
)
291 (alist '(("\\bXEmacs\\b" . xemacs
)
292 ("\\bLucid\\b" . lucid
)
293 ("^Nemacs\\b" . nemacs
)
294 ("^GNU Emacs 19" . standard19
)
295 ("^GNU Emacs 20" . standard19
)
296 ("^GNU Emacs 18" . emacs18
)))
300 ((string-match (car (car alist
)) version
)
301 (setq result
(cdr (car alist
)))
304 (setq alist
(cdr alist
)))))
305 (set-match-data data
)
306 (cond ((eq result
'lucid
)
307 (and (string= emacs-version
"19.8 Lucid")
308 (setq result
'lucid-19-8
)))
309 ((memq result
'(nemacs emacs18
))
311 "type-break not supported in this version of emacs.")))
316 (defun type-break-mode (&optional prefix
)
317 "Enable or disable typing-break mode.
318 This is a minor mode, but it is global to all buffers by default.
320 When this mode is enabled, the user is encouraged to take typing breaks at
321 appropriate intervals; either after a specified amount of time or when the
322 user has exceeded a keystroke threshold. When the time arrives, the user
323 is asked to take a break. If the user refuses at that time, emacs will ask
324 again in a short period of time. The idea is to give the user enough time
325 to find a good breaking point in his or her work, but be sufficiently
326 annoying to discourage putting typing breaks off indefinitely.
328 A negative prefix argument disables this mode.
329 No argument or any non-negative argument enables it.
331 The user may enable or disable this mode by setting the variable of the
332 same name, though setting it in that way doesn't reschedule a break or
333 reset the keystroke counter.
335 If the mode was previously disabled and is enabled as a consequence of
336 calling this function, it schedules a break with `type-break-schedule' to
337 make sure one occurs (the user can call that command to reschedule the
338 break at any time). It also initializes the keystroke counter.
340 The variable `type-break-interval' specifies the number of seconds to
341 schedule between regular typing breaks. This variable doesn't directly
342 affect the time schedule; it simply provides a default for the
343 `type-break-schedule' command.
345 If set, the variable `type-break-good-rest-interval' specifies the minimum
346 amount of time which is considered a reasonable typing break. Whenever
347 that time has elapsed, typing breaks are automatically rescheduled for
348 later even if emacs didn't prompt you to take one first. Also, if a break
349 is ended before this much time has elapsed, the user will be asked whether
352 The variable `type-break-keystroke-threshold' is used to determine the
353 thresholds at which typing breaks should be considered. You can use
354 the command `type-break-guesstimate-keystroke-threshold' to try to
355 approximate good values for this.
357 There are several variables that affect how or when warning messages about
358 imminent typing breaks are displayed. They include:
360 `type-break-mode-line-message-mode'
361 `type-break-time-warning-intervals'
362 `type-break-keystroke-warning-intervals'
363 `type-break-warning-repeat'
364 `type-break-warning-countdown-string'
365 `type-break-warning-countdown-string-type'
367 There are several variables that affect if, how, and when queries to begin
368 a typing break occur. They include:
370 `type-break-query-mode'
371 `type-break-query-function'
372 `type-break-query-interval'
374 Finally, the command `type-break-statistics' prints interesting things."
376 (type-break-check-post-command-hook)
378 (let ((already-enabled type-break-mode
))
379 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
382 ((and already-enabled type-break-mode
)
384 (message "Type Break mode is already enabled")))
386 (or global-mode-string
387 (setq global-mode-string
'("")))
388 (or (assq 'type-break-mode-line-message-mode
390 (setq minor-mode-alist
391 (cons type-break-mode-line-format
393 (type-break-keystroke-reset)
394 (type-break-mode-line-countdown-or-break nil
)
395 (type-break-schedule)
397 (message "Type Break mode is enabled and reset")))
399 (type-break-keystroke-reset)
400 (type-break-mode-line-countdown-or-break nil
)
401 (type-break-cancel-schedule)
403 (message "Type Break mode is disabled")))))
406 (defun type-break-mode-line-message-mode (&optional prefix
)
407 "Enable or disable warnings in the mode line about typing breaks.
409 A negative prefix argument disables this mode.
410 No argument or any non-negative argument enables it.
412 The user may also enable or disable this mode simply by setting the
413 variable of the same name.
415 Variables controlling the display of messages in the mode line include:
419 `type-break-mode-line-break-message'
420 `type-break-mode-line-warning'"
422 (setq type-break-mode-line-message-mode
423 (>= (prefix-numeric-value prefix
) 0))
425 (if type-break-mode-line-message-mode
426 (message "type-break-mode-line-message-mode is enabled")
427 (message "type-break-mode-line-message-mode is disabled")))
428 type-break-mode-line-message-mode
)
430 (defun type-break-query-mode (&optional prefix
)
431 "Enable or disable warnings in the mode line about typing breaks.
433 When enabled, the user is periodically queried about whether to take a
434 typing break at that moment. The function which does this query is
435 specified by the variable `type-break-query-function'.
437 A negative prefix argument disables this mode.
438 No argument or any non-negative argument enables it.
440 The user may also enable or disable this mode simply by setting the
441 variable of the same name."
443 (setq type-break-query-mode
444 (>= (prefix-numeric-value prefix
) 0))
446 (if type-break-query-mode
447 (message "type-break-query-mode is enabled")
448 (message "type-break-query-mode is disabled")))
449 type-break-query-mode
)
454 "Take a typing break.
456 During the break, a demo selected from the functions listed in
457 `type-break-demo-functions' is run.
459 After the typing break is finished, the next break is scheduled
460 as per the function `type-break-schedule'."
463 (type-break-cancel-schedule)
465 (start-time (current-time)))
466 (setq type-break-time-last-break start-time
)
468 (save-window-excursion
470 (and (eq (selected-window) (minibuffer-window))
472 (delete-other-windows)
473 (scroll-right (window-width))
474 (message "Press any key to resume from typing break.")
477 (let* ((len (length type-break-demo-functions
))
479 (fn (nth idx type-break-demo-functions
)))
485 (type-break-good-rest-interval
486 (let ((break-secs (type-break-time-difference
487 start-time
(current-time))))
489 ((>= break-secs type-break-good-rest-interval
)
491 ;; 60 seconds may be too much leeway if the break is only 3
492 ;; minutes to begin with. You can just say "no" to the query
493 ;; below if you're in that much of a hurry.
494 ;((> 60 (abs (- break-secs type-break-good-rest-interval)))
495 ; (setq continue nil))
497 type-break-query-function
498 (format "%sYou really ought to rest %s more. Continue break? "
499 (type-break-time-stamp)
500 (type-break-format-time (- type-break-good-rest-interval
503 (setq continue nil
)))))
504 (t (setq continue nil
)))))
506 (type-break-keystroke-reset)
507 (type-break-mode-line-countdown-or-break nil
)
508 (type-break-schedule))
511 (defun type-break-schedule (&optional time
)
512 "Schedule a typing break for TIME seconds from now.
513 If time is not specified, default to `type-break-interval'."
514 (interactive (list (and current-prefix-arg
515 (prefix-numeric-value current-prefix-arg
))))
516 (or time
(setq time type-break-interval
))
517 (type-break-check-post-command-hook)
518 (type-break-cancel-schedule)
519 (type-break-time-warning-schedule time
'reset
)
520 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
521 (setq type-break-time-next-break
522 (type-break-time-sum (current-time) time
)))
524 (defun type-break-cancel-schedule ()
525 (type-break-cancel-time-warning-schedule)
526 (type-break-cancel-function-timers 'type-break-alarm
)
527 (setq type-break-alarm-p nil
)
528 (setq type-break-time-next-break nil
))
530 (defun type-break-time-warning-schedule (&optional time resetp
)
531 (let ((type-break-current-time-warning-interval nil
))
532 (type-break-cancel-time-warning-schedule))
533 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
535 (type-break-time-warning-intervals
537 (setq type-break-current-time-warning-interval
538 type-break-time-warning-intervals
))
541 (setq time
(type-break-time-difference (current-time)
542 type-break-time-next-break
)))
544 (while (and type-break-current-time-warning-interval
545 (> (car type-break-current-time-warning-interval
) time
))
546 (setq type-break-current-time-warning-interval
547 (cdr type-break-current-time-warning-interval
)))
550 (type-break-current-time-warning-interval
551 (setq time
(- time
(car type-break-current-time-warning-interval
)))
552 (setq type-break-current-time-warning-interval
553 (cdr type-break-current-time-warning-interval
))
555 ;(let (type-break-current-time-warning-interval)
556 ; (type-break-cancel-time-warning-schedule))
557 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
561 (setq type-break-warning-countdown-string nil
))
563 (setq type-break-warning-countdown-string
(number-to-string time
))
564 (setq type-break-warning-countdown-string-type
"seconds"))))))))
566 (defun type-break-cancel-time-warning-schedule ()
567 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
568 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
569 (setq type-break-current-time-warning-interval
570 type-break-time-warning-intervals
)
571 (setq type-break-warning-countdown-string nil
))
573 (defun type-break-alarm ()
574 (type-break-check-post-command-hook)
575 (setq type-break-alarm-p t
)
576 (type-break-mode-line-countdown-or-break 'break
))
578 (defun type-break-time-warning-alarm ()
579 (type-break-check-post-command-hook)
580 (type-break-time-warning-schedule)
581 (setq type-break-time-warning-count type-break-warning-repeat
)
582 (type-break-time-warning)
583 (type-break-mode-line-countdown-or-break 'countdown
))
586 (defun type-break-run-tb-post-command-hook ()
588 (run-hooks 'type-break-post-command-hook
)))
590 (defun type-break-check ()
591 "Ask to take a typing break if appropriate.
592 This may be the case either because the scheduled time has come \(and the
593 minimum keystroke threshold has been reached\) or because the maximum
594 keystroke threshold has been exceeded."
595 (let* ((min-threshold (car type-break-keystroke-threshold
))
596 (max-threshold (cdr type-break-keystroke-threshold
)))
597 (and type-break-good-rest-interval
599 (and (> (type-break-time-difference
600 type-break-time-last-command
(current-time))
601 type-break-good-rest-interval
)
603 (type-break-keystroke-reset)
604 (type-break-mode-line-countdown-or-break nil
)
605 (setq type-break-time-last-break
(current-time))
606 (type-break-schedule)))
607 (setq type-break-time-last-command
(current-time))))
609 (and type-break-keystroke-threshold
610 (let ((keys (this-command-keys)))
612 ;; Ignore mouse motion
614 (consp (aref keys
0))
615 (memq (car (aref keys
0)) '(mouse-movement))))
617 (setq type-break-keystroke-count
618 (+ type-break-keystroke-count
(length keys
)))))))
624 ((eq (selected-window) (minibuffer-window)))
626 (< type-break-keystroke-count min-threshold
))
627 (type-break-schedule))
629 ;; If keystroke count is within min-threshold of
630 ;; max-threshold, lower it to reduce the likelihood of an
631 ;; immediate subsequent query.
634 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
636 (type-break-keystroke-reset)
637 (setq type-break-keystroke-count min-threshold
)))
638 (type-break-query))))
639 ((and type-break-keystroke-warning-intervals
641 (= type-break-keystroke-warning-count
0)
642 (type-break-check-keystroke-warning)))
644 (> type-break-keystroke-count max-threshold
)
645 (not (input-pending-p))
646 (not (eq (selected-window) (minibuffer-window))))
647 (type-break-keystroke-reset)
648 (setq type-break-keystroke-count
(or min-threshold
0))
649 (type-break-query)))))
651 ;; This should return t if warnings were enabled, nil otherwise.
652 (defun type-break-check-keystroke-warning ()
653 ;; This is safe because the caller should have checked that the cdr was
655 (let ((left (- (cdr type-break-keystroke-threshold
)
656 type-break-keystroke-count
)))
658 ((null (car type-break-current-keystroke-warning-interval
))
660 ((> left
(car type-break-current-keystroke-warning-interval
))
663 (while (and (car type-break-current-keystroke-warning-interval
)
664 (< left
(car type-break-current-keystroke-warning-interval
)))
665 (setq type-break-current-keystroke-warning-interval
666 (cdr type-break-current-keystroke-warning-interval
)))
667 (setq type-break-keystroke-warning-count type-break-warning-repeat
)
668 (add-hook 'type-break-post-command-hook
'type-break-keystroke-warning
)
669 (setq type-break-warning-countdown-string
(number-to-string left
))
670 (setq type-break-warning-countdown-string-type
"keystrokes")
671 (type-break-mode-line-countdown-or-break 'countdown
)
674 ;; Arrange for a break query to be made, when the user stops typing furiously.
675 (defun type-break-query ()
676 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
678 (defun type-break-do-query ()
680 ((not type-break-query-mode
)
681 (type-break-noninteractive-query)
682 (type-break-schedule type-break-query-interval
)
683 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))
687 ((let ((type-break-mode nil
)
688 ;; yes-or-no-p sets this-command to exit-minibuffer,
689 ;; which hoses undo or yank-pop (if you happened to be
690 ;; yanking just when the query occurred).
691 (this-command this-command
))
692 ;; Cancel schedule to prevent possibility of a second query
693 ;; from taking place before this one has even returned.
694 ;; The condition-case wrapper will reschedule on quit.
695 (type-break-cancel-schedule)
696 (funcall type-break-query-function
698 (type-break-time-stamp)
699 "Take a break from typing now? ")))
702 (type-break-schedule type-break-query-interval
)))
704 (type-break-schedule type-break-query-interval
)))
705 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))))
707 (defun type-break-noninteractive-query (&optional ignored-args
)
708 "Null query function which doesn't interrupt user and assumes `no'.
709 It prints a reminder in the echo area to take a break, but doesn't enforce
710 this or ask the user to start one right now."
712 (type-break-mode-line-message-mode)
715 (message "%sYou should take a typing break now. Do `M-x type-break'."
716 (type-break-time-stamp))
719 ;; return nil so query caller knows to reset reminder, as if user
720 ;; said "no" in response to yes-or-no-p.
723 (defun type-break-time-warning ()
725 ((and (car type-break-keystroke-threshold
)
726 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
727 ((> type-break-time-warning-count
0)
728 (let ((timeleft (type-break-time-difference (current-time)
729 type-break-time-next-break
)))
730 (setq type-break-warning-countdown-string
(number-to-string timeleft
))
732 ((eq (selected-window) (minibuffer-window)))
733 ;; Do nothing if the command was just a prefix arg, since that will
734 ;; immediately be followed by some other interactive command.
735 ;; Otherwise, it is particularly annoying for the sit-for below to
736 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
737 ((memq this-command
'(digit-argument universal-argument
)))
738 ((not type-break-mode-line-message-mode
)
739 ;; Pause for a moment so any previous message can be seen.
741 (message "%sWarning: typing break due in %s."
742 (type-break-time-stamp)
743 (type-break-format-time timeleft
))
744 (setq type-break-time-warning-count
745 (1- type-break-time-warning-count
))))))
747 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
748 (setq type-break-warning-countdown-string nil
))))
750 (defun type-break-keystroke-warning ()
752 ((> type-break-keystroke-warning-count
0)
753 (setq type-break-warning-countdown-string
754 (number-to-string (- (cdr type-break-keystroke-threshold
)
755 type-break-keystroke-count
)))
757 ((eq (selected-window) (minibuffer-window)))
758 ;; Do nothing if the command was just a prefix arg, since that will
759 ;; immediately be followed by some other interactive command.
760 ;; Otherwise, it is particularly annoying for the sit-for below to
761 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
762 ((memq this-command
'(digit-argument universal-argument
)))
763 ((not type-break-mode-line-message-mode
)
765 (message "%sWarning: typing break due in %s keystrokes."
766 (type-break-time-stamp)
767 (- (cdr type-break-keystroke-threshold
)
768 type-break-keystroke-count
))
769 (setq type-break-keystroke-warning-count
770 (1- type-break-keystroke-warning-count
)))))
772 (remove-hook 'type-break-post-command-hook
773 'type-break-keystroke-warning
)
774 (setq type-break-warning-countdown-string nil
))))
776 (defun type-break-mode-line-countdown-or-break (&optional type
)
778 ((not type-break-mode-line-message-mode
))
779 ((eq type
'countdown
)
780 ;(setq type-break-mode-line-break-message-p nil)
781 (add-hook 'type-break-post-command-hook
782 'type-break-force-mode-line-update
'append
))
785 (setq type-break-mode-line-break-message-p
786 (not type-break-mode-line-break-message-p
))
787 (remove-hook 'type-break-post-command-hook
788 'type-break-force-mode-line-update
))
790 (setq type-break-mode-line-break-message-p nil
)
791 (setq type-break-warning-countdown-string nil
)
792 (remove-hook 'type-break-post-command-hook
793 'type-break-force-mode-line-update
)))
794 (type-break-force-mode-line-update))
798 (defun type-break-statistics ()
799 "Print statistics about typing breaks in a temporary buffer.
800 This includes the last time a typing break was taken, when the next one is
801 scheduled, the keystroke thresholds and the current keystroke count, etc."
803 (with-output-to-temp-buffer "*Typing Break Statistics*"
804 (princ (format "Typing break statistics\n-----------------------\n
805 Typing break mode is currently %s.
806 Interactive query for breaks is %s.
807 Warnings of imminent typing breaks in mode line is %s.
809 Last typing break ended : %s
810 Next scheduled typing break : %s\n
811 Minimum keystroke threshold : %s
812 Maximum keystroke threshold : %s
813 Current keystroke count : %s"
814 (if type-break-mode
"enabled" "disabled")
815 (if type-break-query-mode
"enabled" "disabled")
816 (if type-break-mode-line-message-mode
"enabled" "disabled")
817 (if type-break-time-last-break
818 (current-time-string type-break-time-last-break
)
820 (if (and type-break-mode type-break-time-next-break
)
821 (format "%s\t(%s from now)"
822 (current-time-string type-break-time-next-break
)
823 (type-break-format-time
824 (type-break-time-difference
826 type-break-time-next-break
)))
828 (or (car type-break-keystroke-threshold
) "none")
829 (or (cdr type-break-keystroke-threshold
) "none")
830 type-break-keystroke-count
))))
833 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
834 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
836 If called interactively, the user is prompted for their guess as to how
837 many words per minute they usually type. This value should not be your
838 maximum WPM, but your average. Of course, this is harder to gauge since it
839 can vary considerably depending on what you are doing. For example, one
840 tends to type less when debugging a program as opposed to writing
841 documentation. (Perhaps a separate program should be written to estimate
842 average typing speed.)
844 From that, this command sets the values in `type-break-keystroke-threshold'
845 based on a fairly simple algorithm involving assumptions about the average
846 length of words (5). For the minimum threshold, it uses about a fifth of
847 the computed maximum threshold.
849 When called from lisp programs, the optional args WORDLEN and FRAC can be
850 used to override the default assumption about average word length and the
851 fraction of the maximum threshold to which to set the minimum threshold.
852 FRAC should be the inverse of the fractional value; for example, a value of
853 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
854 (interactive "NOn average, how many words per minute do you type? ")
855 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
856 (lower (/ upper
(or frac
5))))
857 (or type-break-keystroke-threshold
858 (setq type-break-keystroke-threshold
(cons nil nil
)))
859 (setcar type-break-keystroke-threshold lower
)
860 (setcdr type-break-keystroke-threshold upper
)
862 (message "min threshold: %d\tmax threshold: %d" lower upper
)
863 type-break-keystroke-threshold
)))
868 ;; Compute the difference, in seconds, between a and b, two structures
869 ;; similar to those returned by `current-time'.
870 ;; Use addition rather than logand since that is more robust; the low 16
871 ;; bits of the seconds might have been incremented, making it more than 16
873 (defun type-break-time-difference (a b
)
874 (+ (lsh (- (car b
) (car a
)) 16)
875 (- (car (cdr b
)) (car (cdr a
)))))
877 ;; Return (in a new list the same in structure to that returned by
878 ;; `current-time') the sum of the arguments. Each argument may be a time
879 ;; list or a single integer, a number of seconds.
880 ;; This function keeps the high and low 16 bits of the seconds properly
881 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
882 ;; the result is passed to `current-time-string' it will toss some of the
883 ;; "low" bits and format the time incorrectly.
884 (defun type-break-time-sum (&rest tmlist
)
890 (setq tem
(car tmlist
))
891 (setq tmlist
(cdr tmlist
))
894 (setq low
(+ low tem
)))
896 (setq high
(+ high
(or (car tem
) 0)))
897 (setq low
(+ low
(or (car (cdr tem
)) 0)))
898 (setq micro
(+ micro
(or (car (cdr (cdr tem
))) 0))))))
900 (and (>= micro
1000000)
902 (setq tem
(/ micro
1000000))
903 (setq low
(+ low tem
))
904 (setq micro
(- micro
(* tem
1000000)))))
906 (setq tem
(lsh low -
16))
909 (setq low
(logand low
65535))
910 (setq high
(+ high tem
))))
912 (list high low micro
)))
914 (defun type-break-time-stamp (&optional when
)
915 (if (fboundp 'format-time-string
)
916 (format-time-string type-break-time-stamp-format when
)
917 ;; Emacs 19.28 and prior do not have format-time-string.
918 ;; In that case, result is not customizable. Upgrade today!
919 (format "[%s] " (substring (current-time-string when
) 11 16))))
921 (defun type-break-format-time (secs)
922 (let ((mins (/ secs
60)))
924 ((= mins
1) (format "%d minute" mins
))
925 ((> mins
0) (format "%d minutes" mins
))
926 ((= secs
1) (format "%d second" secs
))
927 (t (format "%d seconds" secs
)))))
929 (defun type-break-keystroke-reset ()
930 (setq type-break-keystroke-count
0)
931 (setq type-break-keystroke-warning-count
0)
932 (setq type-break-current-keystroke-warning-interval
933 type-break-keystroke-warning-intervals
)
934 (remove-hook 'type-break-post-command-hook
'type-break-keystroke-warning
))
936 (defun type-break-force-mode-line-update (&optional all
)
937 "Force the mode-line of the current buffer to be redisplayed.
938 With optional non-nil ALL, force redisplay of all mode-lines."
939 (and all
(save-excursion (set-buffer (other-buffer))))
940 (set-buffer-modified-p (buffer-modified-p)))
942 ;; If an exception occurs in emacs while running the post command hook, the
943 ;; value of that hook is clobbered. This is because the value of the
944 ;; variable is temporarily set to nil while it's running to prevent
945 ;; recursive application, but it also means an exception aborts the routine
946 ;; of restoring it. This function is called from the timers to restore it,
948 (defun type-break-check-post-command-hook ()
949 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
952 ;;; Timer wrapper functions
954 ;;; These shield type-break from variations in the interval timer packages
955 ;;; for different versions of emacs.
957 (defun type-break-run-at-time (time repeat function
)
958 (cond ((eq type-break-emacs-variant
'standard19
)
960 (funcall 'run-at-time time repeat function
))
961 ((eq type-break-emacs-variant
'lucid-19-8
)
962 (let ((name (if (symbolp function
)
963 (symbol-name function
)
966 (funcall 'start-timer name function time repeat
)))
967 ((memq type-break-emacs-variant
'(xemacs lucid
))
968 (let ((name (if (symbolp function
)
969 (symbol-name function
)
972 (funcall 'start-itimer name function time repeat
)))))
974 (defun type-break-cancel-function-timers (function)
975 (cond ((eq type-break-emacs-variant
'standard19
)
976 (let ((timer-dont-exit t
))
977 (funcall 'cancel-function-timers function
)))
978 ((eq type-break-emacs-variant
'lucid-19-8
)
979 (let ((list timer-list
))
981 (and (eq (funcall 'timer-function
(car list
)) function
)
982 (funcall 'delete-timer
(car list
)))
983 (setq list
(cdr list
)))))
984 ((memq type-break-emacs-variant
'(xemacs lucid
))
985 (let ((list itimer-list
))
987 (and (eq (funcall 'itimer-function
(car list
)) function
)
988 (funcall 'delete-itimer
(car list
)))
989 (setq list
(cdr list
)))))))
994 ;; This is a wrapper around hanoi that calls it with an arg large enough to
995 ;; make the largest discs possible that will fit in the window.
996 ;; Also, clean up the *Hanoi* buffer after we're done.
997 (defun type-break-demo-hanoi ()
998 "Take a hanoiing typing break."
999 (and (get-buffer "*Hanoi*")
1000 (kill-buffer "*Hanoi*"))
1003 (hanoi (/ (window-width) 8))
1004 ;; Wait for user to come back.
1006 (kill-buffer "*Hanoi*"))
1010 (and (get-buffer "*Hanoi*")
1011 (kill-buffer "*Hanoi*")))))
1013 ;; This is a wrapper around life that calls it with a `sleep' arg to make
1014 ;; it run a little more leisurely.
1015 ;; Also, clean up the *Life* buffer after we're done.
1016 (defun type-break-demo-life ()
1017 "Take a typing break and get a life."
1021 (and (get-buffer "*Life*")
1022 (kill-buffer "*Life*"))
1026 ;; wait for user to return
1028 (kill-buffer "*Life*"))
1030 (message "%s" (get 'life-extinct
'error-message
))
1035 (and (get-buffer "*Life*")
1036 (kill-buffer "*Life*")))))))
1038 ;; Boring demo, but doesn't use many cycles
1039 (defun type-break-demo-boring ()
1040 "Boring typing break demo."
1041 (let ((rmsg "Press any key to resume from typing break")
1042 (buffer-name "*Typing Break Buffer*")
1044 elapsed timeleft tmsg
)
1047 (switch-to-buffer (get-buffer-create buffer-name
))
1048 (buffer-disable-undo (current-buffer))
1050 (setq line
(1+ (/ (window-height) 2)))
1051 (setq col
(/ (- (window-width) (length rmsg
)) 2))
1052 (insert (make-string line ?\C-j
)
1053 (make-string col ?\
)
1058 (while (not (input-pending-p))
1059 (delete-region pos
(progn
1063 (setq elapsed
(type-break-time-difference
1064 type-break-time-last-break
1067 (type-break-good-rest-interval
1068 (setq timeleft
(- type-break-good-rest-interval elapsed
))
1070 (setq tmsg
(format "You should rest for %s more"
1071 (type-break-format-time timeleft
)))
1072 (setq tmsg
(format "Typing break has lasted %s"
1073 (type-break-format-time elapsed
)))))
1075 (setq tmsg
(format "Typing break has lasted %s"
1076 (type-break-format-time elapsed
)))))
1077 (setq col
(/ (- (window-width) (length tmsg
)) 2))
1078 (insert (make-string col ?\
) tmsg
)
1079 (goto-char (point-min))
1082 (kill-buffer buffer-name
))
1084 (and (get-buffer buffer-name
)
1085 (kill-buffer buffer-name
))))))
1088 (provide 'type-break
)
1091 (type-break-mode 1))
1093 ;;; type-break.el ends here