1 ;;; type-break.el --- encourage rests from typing at appropriate intervals
3 ;; Copyright (C) 1994, 95, 97, 2000, 2004 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.29 2003/09/01 15:45:17 miles 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)
282 (defun type-break-mode (&optional prefix
)
283 "Enable or disable typing-break mode.
284 This is a minor mode, but it is global to all buffers by default.
286 When this mode is enabled, the user is encouraged to take typing breaks at
287 appropriate intervals; either after a specified amount of time or when the
288 user has exceeded a keystroke threshold. When the time arrives, the user
289 is asked to take a break. If the user refuses at that time, emacs will ask
290 again in a short period of time. The idea is to give the user enough time
291 to find a good breaking point in his or her work, but be sufficiently
292 annoying to discourage putting typing breaks off indefinitely.
294 A negative prefix argument disables this mode.
295 No argument or any non-negative argument enables it.
297 The user may enable or disable this mode by setting the variable of the
298 same name, though setting it in that way doesn't reschedule a break or
299 reset the keystroke counter.
301 If the mode was previously disabled and is enabled as a consequence of
302 calling this function, it schedules a break with `type-break-schedule' to
303 make sure one occurs (the user can call that command to reschedule the
304 break at any time). It also initializes the keystroke counter.
306 The variable `type-break-interval' specifies the number of seconds to
307 schedule between regular typing breaks. This variable doesn't directly
308 affect the time schedule; it simply provides a default for the
309 `type-break-schedule' command.
311 If set, the variable `type-break-good-rest-interval' specifies the minimum
312 amount of time which is considered a reasonable typing break. Whenever
313 that time has elapsed, typing breaks are automatically rescheduled for
314 later even if emacs didn't prompt you to take one first. Also, if a break
315 is ended before this much time has elapsed, the user will be asked whether
318 The variable `type-break-keystroke-threshold' is used to determine the
319 thresholds at which typing breaks should be considered. You can use
320 the command `type-break-guesstimate-keystroke-threshold' to try to
321 approximate good values for this.
323 There are several variables that affect how or when warning messages about
324 imminent typing breaks are displayed. They include:
326 `type-break-mode-line-message-mode'
327 `type-break-time-warning-intervals'
328 `type-break-keystroke-warning-intervals'
329 `type-break-warning-repeat'
330 `type-break-warning-countdown-string'
331 `type-break-warning-countdown-string-type'
333 There are several variables that affect if, how, and when queries to begin
334 a typing break occur. They include:
336 `type-break-query-mode'
337 `type-break-query-function'
338 `type-break-query-interval'
340 Finally, the command `type-break-statistics' prints interesting things."
342 (type-break-check-post-command-hook)
344 (let ((already-enabled type-break-mode
))
345 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
348 ((and already-enabled type-break-mode
)
350 (message "Type Break mode is already enabled")))
352 (or global-mode-string
353 (setq global-mode-string
'("")))
354 (or (assq 'type-break-mode-line-message-mode
356 (setq minor-mode-alist
357 (cons type-break-mode-line-format
359 (type-break-keystroke-reset)
360 (type-break-mode-line-countdown-or-break nil
)
361 (type-break-schedule)
363 (message "Type Break mode is enabled and reset")))
365 (type-break-keystroke-reset)
366 (type-break-mode-line-countdown-or-break nil
)
367 (type-break-cancel-schedule)
369 (message "Type Break mode is disabled")))))
372 (defun type-break-mode-line-message-mode (&optional prefix
)
373 "Enable or disable warnings in the mode line about typing breaks.
375 A negative prefix argument disables this mode.
376 No argument or any non-negative argument enables it.
378 The user may also enable or disable this mode simply by setting the
379 variable of the same name.
381 Variables controlling the display of messages in the mode line include:
385 `type-break-mode-line-break-message'
386 `type-break-mode-line-warning'"
388 (setq type-break-mode-line-message-mode
389 (>= (prefix-numeric-value prefix
) 0))
391 (if type-break-mode-line-message-mode
392 (message "type-break-mode-line-message-mode is enabled")
393 (message "type-break-mode-line-message-mode is disabled")))
394 type-break-mode-line-message-mode
)
396 (defun type-break-query-mode (&optional prefix
)
397 "Enable or disable warnings in the mode line about typing breaks.
399 When enabled, the user is periodically queried about whether to take a
400 typing break at that moment. The function which does this query is
401 specified by the variable `type-break-query-function'.
403 A negative prefix argument disables this mode.
404 No argument or any non-negative argument enables it.
406 The user may also enable or disable this mode simply by setting the
407 variable of the same name."
409 (setq type-break-query-mode
410 (>= (prefix-numeric-value prefix
) 0))
412 (if type-break-query-mode
413 (message "type-break-query-mode is enabled")
414 (message "type-break-query-mode is disabled")))
415 type-break-query-mode
)
420 "Take a typing break.
422 During the break, a demo selected from the functions listed in
423 `type-break-demo-functions' is run.
425 After the typing break is finished, the next break is scheduled
426 as per the function `type-break-schedule'."
429 (type-break-cancel-schedule)
431 (start-time (current-time)))
432 (setq type-break-time-last-break start-time
)
434 (save-window-excursion
436 (and (eq (selected-window) (minibuffer-window))
438 (delete-other-windows)
439 (scroll-right (window-width))
440 (message "Press any key to resume from typing break.")
443 (let* ((len (length type-break-demo-functions
))
445 (fn (nth idx type-break-demo-functions
)))
451 (type-break-good-rest-interval
452 (let ((break-secs (type-break-time-difference
453 start-time
(current-time))))
455 ((>= break-secs type-break-good-rest-interval
)
457 ;; 60 seconds may be too much leeway if the break is only 3
458 ;; minutes to begin with. You can just say "no" to the query
459 ;; below if you're in that much of a hurry.
460 ;((> 60 (abs (- break-secs type-break-good-rest-interval)))
461 ; (setq continue nil))
463 type-break-query-function
464 (format "%sYou really ought to rest %s more. Continue break? "
465 (type-break-time-stamp)
466 (type-break-format-time (- type-break-good-rest-interval
469 (setq continue nil
)))))
470 (t (setq continue nil
)))))
472 (type-break-keystroke-reset)
473 (type-break-mode-line-countdown-or-break nil
)
474 (type-break-schedule))
477 (defun type-break-schedule (&optional time
)
478 "Schedule a typing break for TIME seconds from now.
479 If time is not specified, default to `type-break-interval'."
480 (interactive (list (and current-prefix-arg
481 (prefix-numeric-value current-prefix-arg
))))
482 (or time
(setq time type-break-interval
))
483 (type-break-check-post-command-hook)
484 (type-break-cancel-schedule)
485 (type-break-time-warning-schedule time
'reset
)
486 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
487 (setq type-break-time-next-break
488 (type-break-time-sum (current-time) time
)))
490 (defun type-break-cancel-schedule ()
491 (type-break-cancel-time-warning-schedule)
492 (type-break-cancel-function-timers 'type-break-alarm
)
493 (setq type-break-alarm-p nil
)
494 (setq type-break-time-next-break nil
))
496 (defun type-break-time-warning-schedule (&optional time resetp
)
497 (let ((type-break-current-time-warning-interval nil
))
498 (type-break-cancel-time-warning-schedule))
499 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
501 (type-break-time-warning-intervals
503 (setq type-break-current-time-warning-interval
504 type-break-time-warning-intervals
))
507 (setq time
(type-break-time-difference (current-time)
508 type-break-time-next-break
)))
510 (while (and type-break-current-time-warning-interval
511 (> (car type-break-current-time-warning-interval
) time
))
512 (setq type-break-current-time-warning-interval
513 (cdr type-break-current-time-warning-interval
)))
516 (type-break-current-time-warning-interval
517 (setq time
(- time
(car type-break-current-time-warning-interval
)))
518 (setq type-break-current-time-warning-interval
519 (cdr type-break-current-time-warning-interval
))
521 ;(let (type-break-current-time-warning-interval)
522 ; (type-break-cancel-time-warning-schedule))
523 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
527 (setq type-break-warning-countdown-string nil
))
529 (setq type-break-warning-countdown-string
(number-to-string time
))
530 (setq type-break-warning-countdown-string-type
"seconds"))))))))
532 (defun type-break-cancel-time-warning-schedule ()
533 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
534 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
535 (setq type-break-current-time-warning-interval
536 type-break-time-warning-intervals
)
537 (setq type-break-warning-countdown-string nil
))
539 (defun type-break-alarm ()
540 (type-break-check-post-command-hook)
541 (setq type-break-alarm-p t
)
542 (type-break-mode-line-countdown-or-break 'break
))
544 (defun type-break-time-warning-alarm ()
545 (type-break-check-post-command-hook)
546 (type-break-time-warning-schedule)
547 (setq type-break-time-warning-count type-break-warning-repeat
)
548 (type-break-time-warning)
549 (type-break-mode-line-countdown-or-break 'countdown
))
552 (defun type-break-run-tb-post-command-hook ()
554 (run-hooks 'type-break-post-command-hook
)))
556 (defun type-break-check ()
557 "Ask to take a typing break if appropriate.
558 This may be the case either because the scheduled time has come \(and the
559 minimum keystroke threshold has been reached\) or because the maximum
560 keystroke threshold has been exceeded."
561 (let* ((min-threshold (car type-break-keystroke-threshold
))
562 (max-threshold (cdr type-break-keystroke-threshold
)))
563 (and type-break-good-rest-interval
565 (and (> (type-break-time-difference
566 type-break-time-last-command
(current-time))
567 type-break-good-rest-interval
)
569 (type-break-keystroke-reset)
570 (type-break-mode-line-countdown-or-break nil
)
571 (setq type-break-time-last-break
(current-time))
572 (type-break-schedule)))
573 (setq type-break-time-last-command
(current-time))))
575 (and type-break-keystroke-threshold
576 (let ((keys (this-command-keys)))
578 ;; Ignore mouse motion
580 (consp (aref keys
0))
581 (memq (car (aref keys
0)) '(mouse-movement))))
583 (setq type-break-keystroke-count
584 (+ type-break-keystroke-count
(length keys
)))))))
590 ((eq (selected-window) (minibuffer-window)))
592 (< type-break-keystroke-count min-threshold
))
593 (type-break-schedule))
595 ;; If keystroke count is within min-threshold of
596 ;; max-threshold, lower it to reduce the likelihood of an
597 ;; immediate subsequent query.
600 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
602 (type-break-keystroke-reset)
603 (setq type-break-keystroke-count min-threshold
)))
604 (type-break-query))))
605 ((and type-break-keystroke-warning-intervals
607 (= type-break-keystroke-warning-count
0)
608 (type-break-check-keystroke-warning)))
610 (> type-break-keystroke-count max-threshold
)
611 (not (input-pending-p))
612 (not (eq (selected-window) (minibuffer-window))))
613 (type-break-keystroke-reset)
614 (setq type-break-keystroke-count
(or min-threshold
0))
615 (type-break-query)))))
617 ;; This should return t if warnings were enabled, nil otherwise.
618 (defun type-break-check-keystroke-warning ()
619 ;; This is safe because the caller should have checked that the cdr was
621 (let ((left (- (cdr type-break-keystroke-threshold
)
622 type-break-keystroke-count
)))
624 ((null (car type-break-current-keystroke-warning-interval
))
626 ((> left
(car type-break-current-keystroke-warning-interval
))
629 (while (and (car type-break-current-keystroke-warning-interval
)
630 (< left
(car type-break-current-keystroke-warning-interval
)))
631 (setq type-break-current-keystroke-warning-interval
632 (cdr type-break-current-keystroke-warning-interval
)))
633 (setq type-break-keystroke-warning-count type-break-warning-repeat
)
634 (add-hook 'type-break-post-command-hook
'type-break-keystroke-warning
)
635 (setq type-break-warning-countdown-string
(number-to-string left
))
636 (setq type-break-warning-countdown-string-type
"keystrokes")
637 (type-break-mode-line-countdown-or-break 'countdown
)
640 ;; Arrange for a break query to be made, when the user stops typing furiously.
641 (defun type-break-query ()
642 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
644 (defun type-break-do-query ()
646 ((not type-break-query-mode
)
647 (type-break-noninteractive-query)
648 (type-break-schedule type-break-query-interval
)
649 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))
653 ((let ((type-break-mode nil
)
654 ;; yes-or-no-p sets this-command to exit-minibuffer,
655 ;; which hoses undo or yank-pop (if you happened to be
656 ;; yanking just when the query occurred).
657 (this-command this-command
))
658 ;; Cancel schedule to prevent possibility of a second query
659 ;; from taking place before this one has even returned.
660 ;; The condition-case wrapper will reschedule on quit.
661 (type-break-cancel-schedule)
662 (funcall type-break-query-function
664 (type-break-time-stamp)
665 "Take a break from typing now? ")))
668 (type-break-schedule type-break-query-interval
)))
670 (type-break-schedule type-break-query-interval
)))
671 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))))
673 (defun type-break-noninteractive-query (&optional ignored-args
)
674 "Null query function which doesn't interrupt user and assumes `no'.
675 It prints a reminder in the echo area to take a break, but doesn't enforce
676 this or ask the user to start one right now."
678 (type-break-mode-line-message-mode)
681 (message "%sYou should take a typing break now. Do `M-x type-break'."
682 (type-break-time-stamp))
685 ;; return nil so query caller knows to reset reminder, as if user
686 ;; said "no" in response to yes-or-no-p.
689 (defun type-break-time-warning ()
691 ((and (car type-break-keystroke-threshold
)
692 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
693 ((> type-break-time-warning-count
0)
694 (let ((timeleft (type-break-time-difference (current-time)
695 type-break-time-next-break
)))
696 (setq type-break-warning-countdown-string
(number-to-string timeleft
))
698 ((eq (selected-window) (minibuffer-window)))
699 ;; Do nothing if the command was just a prefix arg, since that will
700 ;; immediately be followed by some other interactive command.
701 ;; Otherwise, it is particularly annoying for the sit-for below to
702 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
703 ((memq this-command
'(digit-argument universal-argument
)))
704 ((not type-break-mode-line-message-mode
)
705 ;; Pause for a moment so any previous message can be seen.
707 (message "%sWarning: typing break due in %s."
708 (type-break-time-stamp)
709 (type-break-format-time timeleft
))
710 (setq type-break-time-warning-count
711 (1- type-break-time-warning-count
))))))
713 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
714 (setq type-break-warning-countdown-string nil
))))
716 (defun type-break-keystroke-warning ()
718 ((> type-break-keystroke-warning-count
0)
719 (setq type-break-warning-countdown-string
720 (number-to-string (- (cdr type-break-keystroke-threshold
)
721 type-break-keystroke-count
)))
723 ((eq (selected-window) (minibuffer-window)))
724 ;; Do nothing if the command was just a prefix arg, since that will
725 ;; immediately be followed by some other interactive command.
726 ;; Otherwise, it is particularly annoying for the sit-for below to
727 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
728 ((memq this-command
'(digit-argument universal-argument
)))
729 ((not type-break-mode-line-message-mode
)
731 (message "%sWarning: typing break due in %s keystrokes."
732 (type-break-time-stamp)
733 (- (cdr type-break-keystroke-threshold
)
734 type-break-keystroke-count
))
735 (setq type-break-keystroke-warning-count
736 (1- type-break-keystroke-warning-count
)))))
738 (remove-hook 'type-break-post-command-hook
739 'type-break-keystroke-warning
)
740 (setq type-break-warning-countdown-string nil
))))
742 (defun type-break-mode-line-countdown-or-break (&optional type
)
744 ((not type-break-mode-line-message-mode
))
745 ((eq type
'countdown
)
746 ;(setq type-break-mode-line-break-message-p nil)
747 (add-hook 'type-break-post-command-hook
748 'type-break-force-mode-line-update
'append
))
751 (setq type-break-mode-line-break-message-p
752 (not type-break-mode-line-break-message-p
))
753 (remove-hook 'type-break-post-command-hook
754 'type-break-force-mode-line-update
))
756 (setq type-break-mode-line-break-message-p nil
)
757 (setq type-break-warning-countdown-string nil
)
758 (remove-hook 'type-break-post-command-hook
759 'type-break-force-mode-line-update
)))
760 (type-break-force-mode-line-update))
764 (defun type-break-statistics ()
765 "Print statistics about typing breaks in a temporary buffer.
766 This includes the last time a typing break was taken, when the next one is
767 scheduled, the keystroke thresholds and the current keystroke count, etc."
769 (with-output-to-temp-buffer "*Typing Break Statistics*"
770 (princ (format "Typing break statistics\n-----------------------\n
771 Typing break mode is currently %s.
772 Interactive query for breaks is %s.
773 Warnings of imminent typing breaks in mode line is %s.
775 Last typing break ended : %s
776 Next scheduled typing break : %s\n
777 Minimum keystroke threshold : %s
778 Maximum keystroke threshold : %s
779 Current keystroke count : %s"
780 (if type-break-mode
"enabled" "disabled")
781 (if type-break-query-mode
"enabled" "disabled")
782 (if type-break-mode-line-message-mode
"enabled" "disabled")
783 (if type-break-time-last-break
784 (current-time-string type-break-time-last-break
)
786 (if (and type-break-mode type-break-time-next-break
)
787 (format "%s\t(%s from now)"
788 (current-time-string type-break-time-next-break
)
789 (type-break-format-time
790 (type-break-time-difference
792 type-break-time-next-break
)))
794 (or (car type-break-keystroke-threshold
) "none")
795 (or (cdr type-break-keystroke-threshold
) "none")
796 type-break-keystroke-count
))))
799 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
800 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
802 If called interactively, the user is prompted for their guess as to how
803 many words per minute they usually type. This value should not be your
804 maximum WPM, but your average. Of course, this is harder to gauge since it
805 can vary considerably depending on what you are doing. For example, one
806 tends to type less when debugging a program as opposed to writing
807 documentation. (Perhaps a separate program should be written to estimate
808 average typing speed.)
810 From that, this command sets the values in `type-break-keystroke-threshold'
811 based on a fairly simple algorithm involving assumptions about the average
812 length of words (5). For the minimum threshold, it uses about a fifth of
813 the computed maximum threshold.
815 When called from lisp programs, the optional args WORDLEN and FRAC can be
816 used to override the default assumption about average word length and the
817 fraction of the maximum threshold to which to set the minimum threshold.
818 FRAC should be the inverse of the fractional value; for example, a value of
819 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
820 (interactive "NOn average, how many words per minute do you type? ")
821 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
822 (lower (/ upper
(or frac
5))))
823 (or type-break-keystroke-threshold
824 (setq type-break-keystroke-threshold
(cons nil nil
)))
825 (setcar type-break-keystroke-threshold lower
)
826 (setcdr type-break-keystroke-threshold upper
)
828 (message "min threshold: %d\tmax threshold: %d" lower upper
)
829 type-break-keystroke-threshold
)))
834 ;; Compute the difference, in seconds, between a and b, two structures
835 ;; similar to those returned by `current-time'.
836 ;; Use addition rather than logand since that is more robust; the low 16
837 ;; bits of the seconds might have been incremented, making it more than 16
839 (defun type-break-time-difference (a b
)
840 (+ (lsh (- (car b
) (car a
)) 16)
841 (- (car (cdr b
)) (car (cdr a
)))))
843 ;; Return (in a new list the same in structure to that returned by
844 ;; `current-time') the sum of the arguments. Each argument may be a time
845 ;; list or a single integer, a number of seconds.
846 ;; This function keeps the high and low 16 bits of the seconds properly
847 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
848 ;; the result is passed to `current-time-string' it will toss some of the
849 ;; "low" bits and format the time incorrectly.
850 (defun type-break-time-sum (&rest tmlist
)
856 (setq tem
(car tmlist
))
857 (setq tmlist
(cdr tmlist
))
860 (setq low
(+ low tem
)))
862 (setq high
(+ high
(or (car tem
) 0)))
863 (setq low
(+ low
(or (car (cdr tem
)) 0)))
864 (setq micro
(+ micro
(or (car (cdr (cdr tem
))) 0))))))
866 (and (>= micro
1000000)
868 (setq tem
(/ micro
1000000))
869 (setq low
(+ low tem
))
870 (setq micro
(- micro
(* tem
1000000)))))
872 (setq tem
(lsh low -
16))
875 (setq low
(logand low
65535))
876 (setq high
(+ high tem
))))
878 (list high low micro
)))
880 (defun type-break-time-stamp (&optional when
)
881 (if (fboundp 'format-time-string
)
882 (format-time-string type-break-time-stamp-format when
)
883 ;; Emacs 19.28 and prior do not have format-time-string.
884 ;; In that case, result is not customizable. Upgrade today!
885 (format "[%s] " (substring (current-time-string when
) 11 16))))
887 (defun type-break-format-time (secs)
888 (let ((mins (/ secs
60)))
890 ((= mins
1) (format "%d minute" mins
))
891 ((> mins
0) (format "%d minutes" mins
))
892 ((= secs
1) (format "%d second" secs
))
893 (t (format "%d seconds" secs
)))))
895 (defun type-break-keystroke-reset ()
896 (setq type-break-keystroke-count
0)
897 (setq type-break-keystroke-warning-count
0)
898 (setq type-break-current-keystroke-warning-interval
899 type-break-keystroke-warning-intervals
)
900 (remove-hook 'type-break-post-command-hook
'type-break-keystroke-warning
))
902 (defun type-break-force-mode-line-update (&optional all
)
903 "Force the mode-line of the current buffer to be redisplayed.
904 With optional non-nil ALL, force redisplay of all mode-lines."
905 (and all
(save-excursion (set-buffer (other-buffer))))
906 (set-buffer-modified-p (buffer-modified-p)))
908 ;; If an exception occurs in emacs while running the post command hook, the
909 ;; value of that hook is clobbered. This is because the value of the
910 ;; variable is temporarily set to nil while it's running to prevent
911 ;; recursive application, but it also means an exception aborts the routine
912 ;; of restoring it. This function is called from the timers to restore it,
914 (defun type-break-check-post-command-hook ()
915 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
918 ;;; Timer wrapper functions
920 ;;; These shield type-break from variations in the interval timer packages
921 ;;; for different versions of emacs.
923 (defun type-break-run-at-time (time repeat function
)
924 (condition-case nil
(or (require 'timer
) (require 'itimer
)) (error nil
))
925 (cond ((fboundp 'run-at-time
)
926 (run-at-time time repeat function
))
927 ((fboundp 'start-timer
)
928 (let ((name (if (symbolp function
)
929 (symbol-name function
)
931 (start-timer name function time repeat
)))
932 ((fboundp 'start-itimer
)
933 (let ((name (if (symbolp function
)
934 (symbol-name function
)
936 (start-itimer name function time repeat
)))))
938 (defvar timer-dont-exit
)
939 (defun type-break-cancel-function-timers (function)
940 (cond ((fboundp 'cancel-function-timers
)
941 (let ((timer-dont-exit t
))
942 (cancel-function-timers function
)))
943 ((fboundp 'delete-timer
)
944 (let ((list timer-list
))
946 (and (eq (funcall 'timer-function
(car list
)) function
)
947 (delete-timer (car list
)))
948 (setq list
(cdr list
)))))
949 ((fboundp 'delete-itimer
)
951 (let ((list itimer-list
))
953 (and (eq (funcall 'itimer-function
(car list
)) function
)
954 (delete-itimer (car list
)))
955 (setq list
(cdr list
))))))))
960 ;; This is a wrapper around hanoi that calls it with an arg large enough to
961 ;; make the largest discs possible that will fit in the window.
962 ;; Also, clean up the *Hanoi* buffer after we're done.
963 (defun type-break-demo-hanoi ()
964 "Take a hanoiing typing break."
965 (and (get-buffer "*Hanoi*")
966 (kill-buffer "*Hanoi*"))
969 (hanoi (/ (window-width) 8))
970 ;; Wait for user to come back.
972 (kill-buffer "*Hanoi*"))
976 (and (get-buffer "*Hanoi*")
977 (kill-buffer "*Hanoi*")))))
979 ;; This is a wrapper around life that calls it with a `sleep' arg to make
980 ;; it run a little more leisurely.
981 ;; Also, clean up the *Life* buffer after we're done.
982 (defun type-break-demo-life ()
983 "Take a typing break and get a life."
987 (and (get-buffer "*Life*")
988 (kill-buffer "*Life*"))
992 ;; wait for user to return
994 (kill-buffer "*Life*"))
996 (message "%s" (get 'life-extinct
'error-message
))
1001 (and (get-buffer "*Life*")
1002 (kill-buffer "*Life*")))))))
1004 ;; Boring demo, but doesn't use many cycles
1005 (defun type-break-demo-boring ()
1006 "Boring typing break demo."
1007 (let ((rmsg "Press any key to resume from typing break")
1008 (buffer-name "*Typing Break Buffer*")
1010 elapsed timeleft tmsg
)
1013 (switch-to-buffer (get-buffer-create buffer-name
))
1014 (buffer-disable-undo (current-buffer))
1016 (setq line
(1+ (/ (window-height) 2)))
1017 (setq col
(/ (- (window-width) (length rmsg
)) 2))
1018 (insert (make-string line ?\C-j
)
1019 (make-string col ?\
)
1024 (while (not (input-pending-p))
1025 (delete-region pos
(progn
1029 (setq elapsed
(type-break-time-difference
1030 type-break-time-last-break
1033 (type-break-good-rest-interval
1034 (setq timeleft
(- type-break-good-rest-interval elapsed
))
1036 (setq tmsg
(format "You should rest for %s more"
1037 (type-break-format-time timeleft
)))
1038 (setq tmsg
(format "Typing break has lasted %s"
1039 (type-break-format-time elapsed
)))))
1041 (setq tmsg
(format "Typing break has lasted %s"
1042 (type-break-format-time elapsed
)))))
1043 (setq col
(/ (- (window-width) (length tmsg
)) 2))
1044 (insert (make-string col ?\
) tmsg
)
1045 (goto-char (point-min))
1048 (kill-buffer buffer-name
))
1050 (and (get-buffer buffer-name
)
1051 (kill-buffer buffer-name
))))))
1054 (provide 'type-break
)
1057 (type-break-mode 1))
1059 ;;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
1060 ;;; type-break.el ends here