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 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; The docstring for the function `type-break-mode' summarizes most of the
31 ;; details of the interface.
33 ;; This package relies on the assumption that you live entirely in emacs,
34 ;; as the author does. If that's not the case for you (e.g. you often
35 ;; suspend emacs or work in other windows) then this won't help very much;
36 ;; it will depend on just how often you switch back to emacs. At the very
37 ;; least, you will want to turn off the keystroke thresholds and rest
40 ;; If you prefer not to be queried about taking breaks, but instead just
41 ;; want to be reminded, do the following:
43 ;; (setq type-break-query-mode nil)
45 ;; Or call the command `type-break-query-mode' with a negative prefix
48 ;; If you find echo area messages annoying and would prefer to see messages
49 ;; in the mode line instead, do M-x type-break-mode-line-message-mode
50 ;; or set the variable of the same name to `t'.
52 ;; This program can truly cons up a storm because of all the calls to
53 ;; `current-time' (which always returns 3 fresh conses). I'm dismayed by
54 ;; this, but I think the health of my hands is far more important than a
55 ;; few pages of virtual memory.
57 ;; This program has no hope of working in Emacs 18.
59 ;; This package was inspired by Roland McGrath's hanoi-break.el.
60 ;; Several people contributed feedback and ideas, including
61 ;; Roland McGrath <roland@gnu.org>
62 ;; Kleanthes Koniaris <kgk@koniaris.com>
63 ;; Mark Ashton <mpashton@gnu.org>
64 ;; Matt Wilding <wilding@cli.com>
65 ;; Robert S. Boyer <boyer@cs.utexas.edu>
70 (defgroup type-break nil
71 "Encourage the user to take a rest from typing at suitable intervals."
76 (defcustom type-break-mode nil
77 "Toggle typing break mode.
78 See the docstring for the `type-break-mode' command for more information.
79 Setting this variable directly does not take effect;
80 use either \\[customize] or the function `type-break-mode'."
81 :set
(lambda (symbol value
)
82 (type-break-mode (if value
1 -
1)))
83 :initialize
'custom-initialize-default
89 (defcustom type-break-interval
(* 60 60)
90 "*Number of seconds between scheduled typing breaks."
95 (defcustom type-break-good-rest-interval
(/ type-break-interval
6)
96 "*Number of seconds of idle time considered to be an adequate typing rest.
98 When this variable is non-nil, emacs checks the idle time between
99 keystrokes. If this idle time is long enough to be considered a \"good\"
100 rest from typing, then the next typing break is simply rescheduled for later.
102 If a break is interrupted before this much time elapses, the user will be
103 asked whether or not really to interrupt the break."
108 (defcustom type-break-keystroke-threshold
109 ;; Assuming typing speed is 35wpm (on the average, do you really
110 ;; type more than that in a minute? I spend a lot of time reading mail
111 ;; and simply studying code in buffers) and average word length is
112 ;; about 5 letters, default upper threshold to the average number of
113 ;; keystrokes one is likely to type in a break interval. That way if the
114 ;; user goes through a furious burst of typing activity, cause a typing
115 ;; break to be required sooner than originally scheduled.
116 ;; Conversely, the minimum threshold should be about a fifth of this.
119 (upper (* wpm avg-word-length
(/ type-break-interval
60)))
122 "*Upper and lower bound on number of keystrokes for considering typing break.
123 This structure is a pair of numbers (MIN . MAX).
125 The first number is the minimum number of keystrokes that must have been
126 entered since the last typing break before considering another one, even if
127 the scheduled time has elapsed; the break is simply rescheduled until later
128 if the minimum threshold hasn't been reached. If this first value is nil,
129 then there is no minimum threshold; as soon as the scheduled time has
130 elapsed, the user will always be queried.
132 The second number is the maximum number of keystrokes that can be entered
133 before a typing break is requested immediately, pre-empting the originally
134 scheduled break. If this second value is nil, then no pre-emptive breaks
135 will occur; only scheduled ones will.
137 Keys with bucky bits (shift, control, meta, etc) are counted as only one
138 keystroke even though they really require multiple keys to generate them.
140 The command `type-break-guesstimate-keystroke-threshold' can be used to
141 guess a reasonably good pair of values for this variable."
145 (defcustom type-break-query-mode t
146 "*Non-nil means ask whether or not to prompt user for breaks.
147 If so, call the function specified in the value of the variable
148 `type-break-query-function' to do the asking."
152 (defcustom type-break-query-function
'yes-or-no-p
153 "*Function to use for making query for a typing break.
154 It should take a string as an argument, the prompt.
155 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
157 To avoid being queried at all, set `type-break-query-mode' to nil."
158 :type
'(radio function
159 (function-item yes-or-no-p
)
160 (function-item y-or-n-p
))
163 (defcustom type-break-query-interval
60
164 "*Number of seconds between queries to take a break, if put off.
165 The user will continue to be prompted at this interval until he or she
166 finally submits to taking a typing break."
170 (defcustom type-break-time-warning-intervals
'(300 120 60 30)
171 "*List of time intervals for warnings about upcoming typing break.
172 At each of the intervals (specified in seconds) away from a scheduled
173 typing break, print a warning in the echo area."
174 :type
'(repeat integer
)
177 (defcustom type-break-keystroke-warning-intervals
'(300 200 100 50)
178 "*List of keystroke measurements for warnings about upcoming typing break.
179 At each of the intervals (specified in keystrokes) away from the upper
180 keystroke threshold, print a warning in the echo area.
181 If either this variable or the upper threshold is set, then no warnings
183 :type
'(repeat integer
)
186 (defcustom type-break-warning-repeat
40
187 "*Number of keystrokes for which warnings should be repeated.
188 That is, for each of this many keystrokes the warning is redisplayed
189 in the echo area to make sure it's really seen."
193 (defcustom type-break-time-stamp-format
"[%H:%M] "
194 "*Timestamp format used to prefix messages.
195 Format specifiers are as used by `format-time-string'."
199 (defcustom type-break-demo-functions
200 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi
)
201 "*List of functions to consider running as demos during typing breaks.
202 When a typing break begins, one of these functions is selected randomly
203 to have emacs do something interesting.
205 Any function in this list should start a demo which ceases as soon as a
207 :type
'(repeat function
)
210 (defvar type-break-post-command-hook
'(type-break-check)
211 "Hook run indirectly by post-command-hook for typing break functions.
212 This is not really intended to be set by the user, but it's probably
213 harmless to do so. Mainly it is used by various parts of the typing break
214 program to delay actions until after the user has completed some command.
215 It exists because `post-command-hook' itself is inaccessible while its
216 functions are being run, and some type-break--related functions want to
217 remove themselves after running.")
222 (defcustom type-break-mode-line-message-mode nil
223 "*Non-nil means put type-break related messages in the mode line.
224 Otherwise, messages typically go in the echo area.
226 See also `type-break-mode-line-format' and its members."
230 (defvar type-break-mode-line-format
231 '(type-break-mode-line-message-mode
233 type-break-mode-line-break-message
234 type-break-mode-line-warning
))
235 "*Format of messages in the mode line concerning typing breaks.")
237 (defvar type-break-mode-line-break-message
238 '(type-break-mode-line-break-message-p
239 type-break-mode-line-break-string
))
241 (defvar type-break-mode-line-break-message-p nil
)
242 (defvar type-break-mode-line-break-string
" *** TAKE A TYPING BREAK NOW ***")
244 (defvar type-break-mode-line-warning
245 '(type-break-mode-line-break-message-p
247 (type-break-warning-countdown-string
250 type-break-warning-countdown-string
252 type-break-warning-countdown-string-type
255 (defvar type-break-warning-countdown-string nil
256 "If non-nil, this is a countdown for the next typing break.
258 This variable, in conjunction with `type-break-warning-countdown-string-type'
259 \(which indicates whether this value is a number of keystrokes or seconds)
260 is installed in mode-line-format to notify of imminent typing breaks.")
262 (defvar type-break-warning-countdown-string-type nil
263 "Indicates the unit type of `type-break-warning-countdown-string'.
264 It will be either \"seconds\" or \"keystrokes\".")
267 ;; These are internal variables. Do not set them yourself.
269 (defvar type-break-alarm-p nil
)
270 (defvar type-break-keystroke-count
0)
271 (defvar type-break-time-last-break nil
)
272 (defvar type-break-time-next-break nil
)
273 (defvar type-break-time-last-command
(current-time))
274 (defvar type-break-current-time-warning-interval nil
)
275 (defvar type-break-current-keystroke-warning-interval nil
)
276 (defvar type-break-time-warning-count
0)
277 (defvar type-break-keystroke-warning-count
0)
280 (defun type-break-mode (&optional prefix
)
281 "Enable or disable typing-break mode.
282 This is a minor mode, but it is global to all buffers by default.
284 When this mode is enabled, the user is encouraged to take typing breaks at
285 appropriate intervals; either after a specified amount of time or when the
286 user has exceeded a keystroke threshold. When the time arrives, the user
287 is asked to take a break. If the user refuses at that time, emacs will ask
288 again in a short period of time. The idea is to give the user enough time
289 to find a good breaking point in his or her work, but be sufficiently
290 annoying to discourage putting typing breaks off indefinitely.
292 A negative prefix argument disables this mode.
293 No argument or any non-negative argument enables it.
295 The user may enable or disable this mode by setting the variable of the
296 same name, though setting it in that way doesn't reschedule a break or
297 reset the keystroke counter.
299 If the mode was previously disabled and is enabled as a consequence of
300 calling this function, it schedules a break with `type-break-schedule' to
301 make sure one occurs (the user can call that command to reschedule the
302 break at any time). It also initializes the keystroke counter.
304 The variable `type-break-interval' specifies the number of seconds to
305 schedule between regular typing breaks. This variable doesn't directly
306 affect the time schedule; it simply provides a default for the
307 `type-break-schedule' command.
309 If set, the variable `type-break-good-rest-interval' specifies the minimum
310 amount of time which is considered a reasonable typing break. Whenever
311 that time has elapsed, typing breaks are automatically rescheduled for
312 later even if emacs didn't prompt you to take one first. Also, if a break
313 is ended before this much time has elapsed, the user will be asked whether
316 The variable `type-break-keystroke-threshold' is used to determine the
317 thresholds at which typing breaks should be considered. You can use
318 the command `type-break-guesstimate-keystroke-threshold' to try to
319 approximate good values for this.
321 There are several variables that affect how or when warning messages about
322 imminent typing breaks are displayed. They include:
324 `type-break-mode-line-message-mode'
325 `type-break-time-warning-intervals'
326 `type-break-keystroke-warning-intervals'
327 `type-break-warning-repeat'
328 `type-break-warning-countdown-string'
329 `type-break-warning-countdown-string-type'
331 There are several variables that affect if, how, and when queries to begin
332 a typing break occur. They include:
334 `type-break-query-mode'
335 `type-break-query-function'
336 `type-break-query-interval'
338 Finally, the command `type-break-statistics' prints interesting things."
340 (type-break-check-post-command-hook)
342 (let ((already-enabled type-break-mode
))
343 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
346 ((and already-enabled type-break-mode
)
348 (message "Type Break mode is already enabled")))
350 (or global-mode-string
351 (setq global-mode-string
'("")))
352 (or (assq 'type-break-mode-line-message-mode
354 (setq minor-mode-alist
355 (cons type-break-mode-line-format
357 (type-break-keystroke-reset)
358 (type-break-mode-line-countdown-or-break nil
)
359 (type-break-schedule)
361 (message "Type Break mode is enabled and reset")))
363 (type-break-keystroke-reset)
364 (type-break-mode-line-countdown-or-break nil
)
365 (type-break-cancel-schedule)
367 (message "Type Break mode is disabled")))))
370 (defun type-break-mode-line-message-mode (&optional prefix
)
371 "Enable or disable warnings in the mode line about typing breaks.
373 A negative prefix argument disables this mode.
374 No argument or any non-negative argument enables it.
376 The user may also enable or disable this mode simply by setting the
377 variable of the same name.
379 Variables controlling the display of messages in the mode line include:
383 `type-break-mode-line-break-message'
384 `type-break-mode-line-warning'"
386 (setq type-break-mode-line-message-mode
387 (>= (prefix-numeric-value prefix
) 0))
389 (if type-break-mode-line-message-mode
390 (message "type-break-mode-line-message-mode is enabled")
391 (message "type-break-mode-line-message-mode is disabled")))
392 type-break-mode-line-message-mode
)
394 (defun type-break-query-mode (&optional prefix
)
395 "Enable or disable warnings in the mode line about typing breaks.
397 When enabled, the user is periodically queried about whether to take a
398 typing break at that moment. The function which does this query is
399 specified by the variable `type-break-query-function'.
401 A negative prefix argument disables this mode.
402 No argument or any non-negative argument enables it.
404 The user may also enable or disable this mode simply by setting the
405 variable of the same name."
407 (setq type-break-query-mode
408 (>= (prefix-numeric-value prefix
) 0))
410 (if type-break-query-mode
411 (message "type-break-query-mode is enabled")
412 (message "type-break-query-mode is disabled")))
413 type-break-query-mode
)
418 "Take a typing break.
420 During the break, a demo selected from the functions listed in
421 `type-break-demo-functions' is run.
423 After the typing break is finished, the next break is scheduled
424 as per the function `type-break-schedule'."
427 (type-break-cancel-schedule)
429 (start-time (current-time)))
430 (setq type-break-time-last-break start-time
)
432 (save-window-excursion
434 (and (eq (selected-window) (minibuffer-window))
436 (delete-other-windows)
437 (scroll-right (window-width))
438 (message "Press any key to resume from typing break.")
441 (let* ((len (length type-break-demo-functions
))
443 (fn (nth idx type-break-demo-functions
)))
449 (type-break-good-rest-interval
450 (let ((break-secs (type-break-time-difference
451 start-time
(current-time))))
453 ((>= break-secs type-break-good-rest-interval
)
455 ;; 60 seconds may be too much leeway if the break is only 3
456 ;; minutes to begin with. You can just say "no" to the query
457 ;; below if you're in that much of a hurry.
458 ;((> 60 (abs (- break-secs type-break-good-rest-interval)))
459 ; (setq continue nil))
461 type-break-query-function
462 (format "%sYou really ought to rest %s more. Continue break? "
463 (type-break-time-stamp)
464 (type-break-format-time (- type-break-good-rest-interval
467 (setq continue nil
)))))
468 (t (setq continue nil
)))))
470 (type-break-keystroke-reset)
471 (type-break-mode-line-countdown-or-break nil
)
472 (type-break-schedule))
475 (defun type-break-schedule (&optional time
)
476 "Schedule a typing break for TIME seconds from now.
477 If time is not specified, default to `type-break-interval'."
478 (interactive (list (and current-prefix-arg
479 (prefix-numeric-value current-prefix-arg
))))
480 (or time
(setq time type-break-interval
))
481 (type-break-check-post-command-hook)
482 (type-break-cancel-schedule)
483 (type-break-time-warning-schedule time
'reset
)
484 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
485 (setq type-break-time-next-break
486 (type-break-time-sum (current-time) time
)))
488 (defun type-break-cancel-schedule ()
489 (type-break-cancel-time-warning-schedule)
490 (type-break-cancel-function-timers 'type-break-alarm
)
491 (setq type-break-alarm-p nil
)
492 (setq type-break-time-next-break nil
))
494 (defun type-break-time-warning-schedule (&optional time resetp
)
495 (let ((type-break-current-time-warning-interval nil
))
496 (type-break-cancel-time-warning-schedule))
497 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
499 (type-break-time-warning-intervals
501 (setq type-break-current-time-warning-interval
502 type-break-time-warning-intervals
))
505 (setq time
(type-break-time-difference (current-time)
506 type-break-time-next-break
)))
508 (while (and type-break-current-time-warning-interval
509 (> (car type-break-current-time-warning-interval
) time
))
510 (setq type-break-current-time-warning-interval
511 (cdr type-break-current-time-warning-interval
)))
514 (type-break-current-time-warning-interval
515 (setq time
(- time
(car type-break-current-time-warning-interval
)))
516 (setq type-break-current-time-warning-interval
517 (cdr type-break-current-time-warning-interval
))
519 ;(let (type-break-current-time-warning-interval)
520 ; (type-break-cancel-time-warning-schedule))
521 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
525 (setq type-break-warning-countdown-string nil
))
527 (setq type-break-warning-countdown-string
(number-to-string time
))
528 (setq type-break-warning-countdown-string-type
"seconds"))))))))
530 (defun type-break-cancel-time-warning-schedule ()
531 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
532 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
533 (setq type-break-current-time-warning-interval
534 type-break-time-warning-intervals
)
535 (setq type-break-warning-countdown-string nil
))
537 (defun type-break-alarm ()
538 (type-break-check-post-command-hook)
539 (setq type-break-alarm-p t
)
540 (type-break-mode-line-countdown-or-break 'break
))
542 (defun type-break-time-warning-alarm ()
543 (type-break-check-post-command-hook)
544 (type-break-time-warning-schedule)
545 (setq type-break-time-warning-count type-break-warning-repeat
)
546 (type-break-time-warning)
547 (type-break-mode-line-countdown-or-break 'countdown
))
550 (defun type-break-run-tb-post-command-hook ()
552 (run-hooks 'type-break-post-command-hook
)))
554 (defun type-break-check ()
555 "Ask to take a typing break if appropriate.
556 This may be the case either because the scheduled time has come \(and the
557 minimum keystroke threshold has been reached\) or because the maximum
558 keystroke threshold has been exceeded."
559 (let* ((min-threshold (car type-break-keystroke-threshold
))
560 (max-threshold (cdr type-break-keystroke-threshold
)))
561 (and type-break-good-rest-interval
563 (and (> (type-break-time-difference
564 type-break-time-last-command
(current-time))
565 type-break-good-rest-interval
)
567 (type-break-keystroke-reset)
568 (type-break-mode-line-countdown-or-break nil
)
569 (setq type-break-time-last-break
(current-time))
570 (type-break-schedule)))
571 (setq type-break-time-last-command
(current-time))))
573 (and type-break-keystroke-threshold
574 (let ((keys (this-command-keys)))
576 ;; Ignore mouse motion
578 (consp (aref keys
0))
579 (memq (car (aref keys
0)) '(mouse-movement))))
581 (setq type-break-keystroke-count
582 (+ type-break-keystroke-count
(length keys
)))))))
588 ((eq (selected-window) (minibuffer-window)))
590 (< type-break-keystroke-count min-threshold
))
591 (type-break-schedule))
593 ;; If keystroke count is within min-threshold of
594 ;; max-threshold, lower it to reduce the likelihood of an
595 ;; immediate subsequent query.
598 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
600 (type-break-keystroke-reset)
601 (setq type-break-keystroke-count min-threshold
)))
602 (type-break-query))))
603 ((and type-break-keystroke-warning-intervals
605 (= type-break-keystroke-warning-count
0)
606 (type-break-check-keystroke-warning)))
608 (> type-break-keystroke-count max-threshold
)
609 (not (input-pending-p))
610 (not (eq (selected-window) (minibuffer-window))))
611 (type-break-keystroke-reset)
612 (setq type-break-keystroke-count
(or min-threshold
0))
613 (type-break-query)))))
615 ;; This should return t if warnings were enabled, nil otherwise.
616 (defun type-break-check-keystroke-warning ()
617 ;; This is safe because the caller should have checked that the cdr was
619 (let ((left (- (cdr type-break-keystroke-threshold
)
620 type-break-keystroke-count
)))
622 ((null (car type-break-current-keystroke-warning-interval
))
624 ((> left
(car type-break-current-keystroke-warning-interval
))
627 (while (and (car type-break-current-keystroke-warning-interval
)
628 (< left
(car type-break-current-keystroke-warning-interval
)))
629 (setq type-break-current-keystroke-warning-interval
630 (cdr type-break-current-keystroke-warning-interval
)))
631 (setq type-break-keystroke-warning-count type-break-warning-repeat
)
632 (add-hook 'type-break-post-command-hook
'type-break-keystroke-warning
)
633 (setq type-break-warning-countdown-string
(number-to-string left
))
634 (setq type-break-warning-countdown-string-type
"keystrokes")
635 (type-break-mode-line-countdown-or-break 'countdown
)
638 ;; Arrange for a break query to be made, when the user stops typing furiously.
639 (defun type-break-query ()
640 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
642 (defun type-break-do-query ()
644 ((not type-break-query-mode
)
645 (type-break-noninteractive-query)
646 (type-break-schedule type-break-query-interval
)
647 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))
651 ((let ((type-break-mode nil
)
652 ;; yes-or-no-p sets this-command to exit-minibuffer,
653 ;; which hoses undo or yank-pop (if you happened to be
654 ;; yanking just when the query occurred).
655 (this-command this-command
))
656 ;; Cancel schedule to prevent possibility of a second query
657 ;; from taking place before this one has even returned.
658 ;; The condition-case wrapper will reschedule on quit.
659 (type-break-cancel-schedule)
660 (funcall type-break-query-function
662 (type-break-time-stamp)
663 "Take a break from typing now? ")))
666 (type-break-schedule type-break-query-interval
)))
668 (type-break-schedule type-break-query-interval
)))
669 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))))
671 (defun type-break-noninteractive-query (&optional ignored-args
)
672 "Null query function which doesn't interrupt user and assumes `no'.
673 It prints a reminder in the echo area to take a break, but doesn't enforce
674 this or ask the user to start one right now."
676 (type-break-mode-line-message-mode)
679 (message "%sYou should take a typing break now. Do `M-x type-break'."
680 (type-break-time-stamp))
683 ;; return nil so query caller knows to reset reminder, as if user
684 ;; said "no" in response to yes-or-no-p.
687 (defun type-break-time-warning ()
689 ((and (car type-break-keystroke-threshold
)
690 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
691 ((> type-break-time-warning-count
0)
692 (let ((timeleft (type-break-time-difference (current-time)
693 type-break-time-next-break
)))
694 (setq type-break-warning-countdown-string
(number-to-string timeleft
))
696 ((eq (selected-window) (minibuffer-window)))
697 ;; Do nothing if the command was just a prefix arg, since that will
698 ;; immediately be followed by some other interactive command.
699 ;; Otherwise, it is particularly annoying for the sit-for below to
700 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
701 ((memq this-command
'(digit-argument universal-argument
)))
702 ((not type-break-mode-line-message-mode
)
703 ;; Pause for a moment so any previous message can be seen.
705 (message "%sWarning: typing break due in %s."
706 (type-break-time-stamp)
707 (type-break-format-time timeleft
))
708 (setq type-break-time-warning-count
709 (1- type-break-time-warning-count
))))))
711 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
712 (setq type-break-warning-countdown-string nil
))))
714 (defun type-break-keystroke-warning ()
716 ((> type-break-keystroke-warning-count
0)
717 (setq type-break-warning-countdown-string
718 (number-to-string (- (cdr type-break-keystroke-threshold
)
719 type-break-keystroke-count
)))
721 ((eq (selected-window) (minibuffer-window)))
722 ;; Do nothing if the command was just a prefix arg, since that will
723 ;; immediately be followed by some other interactive command.
724 ;; Otherwise, it is particularly annoying for the sit-for below to
725 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
726 ((memq this-command
'(digit-argument universal-argument
)))
727 ((not type-break-mode-line-message-mode
)
729 (message "%sWarning: typing break due in %s keystrokes."
730 (type-break-time-stamp)
731 (- (cdr type-break-keystroke-threshold
)
732 type-break-keystroke-count
))
733 (setq type-break-keystroke-warning-count
734 (1- type-break-keystroke-warning-count
)))))
736 (remove-hook 'type-break-post-command-hook
737 'type-break-keystroke-warning
)
738 (setq type-break-warning-countdown-string nil
))))
740 (defun type-break-mode-line-countdown-or-break (&optional type
)
742 ((not type-break-mode-line-message-mode
))
743 ((eq type
'countdown
)
744 ;(setq type-break-mode-line-break-message-p nil)
745 (add-hook 'type-break-post-command-hook
746 'type-break-force-mode-line-update
'append
))
749 (setq type-break-mode-line-break-message-p
750 (not type-break-mode-line-break-message-p
))
751 (remove-hook 'type-break-post-command-hook
752 'type-break-force-mode-line-update
))
754 (setq type-break-mode-line-break-message-p nil
)
755 (setq type-break-warning-countdown-string nil
)
756 (remove-hook 'type-break-post-command-hook
757 'type-break-force-mode-line-update
)))
758 (type-break-force-mode-line-update))
762 (defun type-break-statistics ()
763 "Print statistics about typing breaks in a temporary buffer.
764 This includes the last time a typing break was taken, when the next one is
765 scheduled, the keystroke thresholds and the current keystroke count, etc."
767 (with-output-to-temp-buffer "*Typing Break Statistics*"
768 (princ (format "Typing break statistics\n-----------------------\n
769 Typing break mode is currently %s.
770 Interactive query for breaks is %s.
771 Warnings of imminent typing breaks in mode line is %s.
773 Last typing break ended : %s
774 Next scheduled typing break : %s\n
775 Minimum keystroke threshold : %s
776 Maximum keystroke threshold : %s
777 Current keystroke count : %s"
778 (if type-break-mode
"enabled" "disabled")
779 (if type-break-query-mode
"enabled" "disabled")
780 (if type-break-mode-line-message-mode
"enabled" "disabled")
781 (if type-break-time-last-break
782 (current-time-string type-break-time-last-break
)
784 (if (and type-break-mode type-break-time-next-break
)
785 (format "%s\t(%s from now)"
786 (current-time-string type-break-time-next-break
)
787 (type-break-format-time
788 (type-break-time-difference
790 type-break-time-next-break
)))
792 (or (car type-break-keystroke-threshold
) "none")
793 (or (cdr type-break-keystroke-threshold
) "none")
794 type-break-keystroke-count
))))
797 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
798 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
800 If called interactively, the user is prompted for their guess as to how
801 many words per minute they usually type. This value should not be your
802 maximum WPM, but your average. Of course, this is harder to gauge since it
803 can vary considerably depending on what you are doing. For example, one
804 tends to type less when debugging a program as opposed to writing
805 documentation. (Perhaps a separate program should be written to estimate
806 average typing speed.)
808 From that, this command sets the values in `type-break-keystroke-threshold'
809 based on a fairly simple algorithm involving assumptions about the average
810 length of words (5). For the minimum threshold, it uses about a fifth of
811 the computed maximum threshold.
813 When called from lisp programs, the optional args WORDLEN and FRAC can be
814 used to override the default assumption about average word length and the
815 fraction of the maximum threshold to which to set the minimum threshold.
816 FRAC should be the inverse of the fractional value; for example, a value of
817 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
818 (interactive "NOn average, how many words per minute do you type? ")
819 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
820 (lower (/ upper
(or frac
5))))
821 (or type-break-keystroke-threshold
822 (setq type-break-keystroke-threshold
(cons nil nil
)))
823 (setcar type-break-keystroke-threshold lower
)
824 (setcdr type-break-keystroke-threshold upper
)
826 (message "min threshold: %d\tmax threshold: %d" lower upper
)
827 type-break-keystroke-threshold
)))
832 ;; Compute the difference, in seconds, between a and b, two structures
833 ;; similar to those returned by `current-time'.
834 ;; Use addition rather than logand since that is more robust; the low 16
835 ;; bits of the seconds might have been incremented, making it more than 16
837 (defun type-break-time-difference (a b
)
838 (+ (lsh (- (car b
) (car a
)) 16)
839 (- (car (cdr b
)) (car (cdr a
)))))
841 ;; Return (in a new list the same in structure to that returned by
842 ;; `current-time') the sum of the arguments. Each argument may be a time
843 ;; list or a single integer, a number of seconds.
844 ;; This function keeps the high and low 16 bits of the seconds properly
845 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
846 ;; the result is passed to `current-time-string' it will toss some of the
847 ;; "low" bits and format the time incorrectly.
848 (defun type-break-time-sum (&rest tmlist
)
854 (setq tem
(car tmlist
))
855 (setq tmlist
(cdr tmlist
))
858 (setq low
(+ low tem
)))
860 (setq high
(+ high
(or (car tem
) 0)))
861 (setq low
(+ low
(or (car (cdr tem
)) 0)))
862 (setq micro
(+ micro
(or (car (cdr (cdr tem
))) 0))))))
864 (and (>= micro
1000000)
866 (setq tem
(/ micro
1000000))
867 (setq low
(+ low tem
))
868 (setq micro
(- micro
(* tem
1000000)))))
870 (setq tem
(lsh low -
16))
873 (setq low
(logand low
65535))
874 (setq high
(+ high tem
))))
876 (list high low micro
)))
878 (defun type-break-time-stamp (&optional when
)
879 (if (fboundp 'format-time-string
)
880 (format-time-string type-break-time-stamp-format when
)
881 ;; Emacs 19.28 and prior do not have format-time-string.
882 ;; In that case, result is not customizable. Upgrade today!
883 (format "[%s] " (substring (current-time-string when
) 11 16))))
885 (defun type-break-format-time (secs)
886 (let ((mins (/ secs
60)))
888 ((= mins
1) (format "%d minute" mins
))
889 ((> mins
0) (format "%d minutes" mins
))
890 ((= secs
1) (format "%d second" secs
))
891 (t (format "%d seconds" secs
)))))
893 (defun type-break-keystroke-reset ()
894 (setq type-break-keystroke-count
0)
895 (setq type-break-keystroke-warning-count
0)
896 (setq type-break-current-keystroke-warning-interval
897 type-break-keystroke-warning-intervals
)
898 (remove-hook 'type-break-post-command-hook
'type-break-keystroke-warning
))
900 (defun type-break-force-mode-line-update (&optional all
)
901 "Force the mode-line of the current buffer to be redisplayed.
902 With optional non-nil ALL, force redisplay of all mode-lines."
903 (and all
(save-excursion (set-buffer (other-buffer))))
904 (set-buffer-modified-p (buffer-modified-p)))
906 ;; If an exception occurs in emacs while running the post command hook, the
907 ;; value of that hook is clobbered. This is because the value of the
908 ;; variable is temporarily set to nil while it's running to prevent
909 ;; recursive application, but it also means an exception aborts the routine
910 ;; of restoring it. This function is called from the timers to restore it,
912 (defun type-break-check-post-command-hook ()
913 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
916 ;;; Timer wrapper functions
918 ;;; These shield type-break from variations in the interval timer packages
919 ;;; for different versions of emacs.
921 (defun type-break-run-at-time (time repeat function
)
922 (condition-case nil
(or (require 'timer
) (require 'itimer
)) (error nil
))
923 (cond ((fboundp 'run-at-time
)
924 (run-at-time time repeat function
))
925 ((fboundp 'start-timer
)
926 (let ((name (if (symbolp function
)
927 (symbol-name function
)
929 (start-timer name function time repeat
)))
930 ((fboundp 'start-itimer
)
931 (let ((name (if (symbolp function
)
932 (symbol-name function
)
934 (start-itimer name function time repeat
)))))
936 (defvar timer-dont-exit
)
937 (defun type-break-cancel-function-timers (function)
938 (cond ((fboundp 'cancel-function-timers
)
939 (let ((timer-dont-exit t
))
940 (cancel-function-timers function
)))
941 ((fboundp 'delete-timer
)
942 (let ((list timer-list
))
944 (and (eq (funcall 'timer-function
(car list
)) function
)
945 (delete-timer (car list
)))
946 (setq list
(cdr list
)))))
947 ((fboundp 'delete-itimer
)
949 (let ((list itimer-list
))
951 (and (eq (funcall 'itimer-function
(car list
)) function
)
952 (delete-itimer (car list
)))
953 (setq list
(cdr list
))))))))
958 ;; This is a wrapper around hanoi that calls it with an arg large enough to
959 ;; make the largest discs possible that will fit in the window.
960 ;; Also, clean up the *Hanoi* buffer after we're done.
961 (defun type-break-demo-hanoi ()
962 "Take a hanoiing typing break."
963 (and (get-buffer "*Hanoi*")
964 (kill-buffer "*Hanoi*"))
967 (hanoi (/ (window-width) 8))
968 ;; Wait for user to come back.
970 (kill-buffer "*Hanoi*"))
974 (and (get-buffer "*Hanoi*")
975 (kill-buffer "*Hanoi*")))))
977 ;; This is a wrapper around life that calls it with a `sleep' arg to make
978 ;; it run a little more leisurely.
979 ;; Also, clean up the *Life* buffer after we're done.
980 (defun type-break-demo-life ()
981 "Take a typing break and get a life."
985 (and (get-buffer "*Life*")
986 (kill-buffer "*Life*"))
990 ;; wait for user to return
992 (kill-buffer "*Life*"))
994 (message "%s" (get 'life-extinct
'error-message
))
999 (and (get-buffer "*Life*")
1000 (kill-buffer "*Life*")))))))
1002 ;; Boring demo, but doesn't use many cycles
1003 (defun type-break-demo-boring ()
1004 "Boring typing break demo."
1005 (let ((rmsg "Press any key to resume from typing break")
1006 (buffer-name "*Typing Break Buffer*")
1008 elapsed timeleft tmsg
)
1011 (switch-to-buffer (get-buffer-create buffer-name
))
1012 (buffer-disable-undo (current-buffer))
1014 (setq line
(1+ (/ (window-height) 2)))
1015 (setq col
(/ (- (window-width) (length rmsg
)) 2))
1016 (insert (make-string line ?\C-j
)
1017 (make-string col ?\
)
1022 (while (not (input-pending-p))
1023 (delete-region pos
(progn
1027 (setq elapsed
(type-break-time-difference
1028 type-break-time-last-break
1031 (type-break-good-rest-interval
1032 (setq timeleft
(- type-break-good-rest-interval elapsed
))
1034 (setq tmsg
(format "You should rest for %s more"
1035 (type-break-format-time timeleft
)))
1036 (setq tmsg
(format "Typing break has lasted %s"
1037 (type-break-format-time elapsed
)))))
1039 (setq tmsg
(format "Typing break has lasted %s"
1040 (type-break-format-time elapsed
)))))
1041 (setq col
(/ (- (window-width) (length tmsg
)) 2))
1042 (insert (make-string col ?\
) tmsg
)
1043 (goto-char (point-min))
1046 (kill-buffer buffer-name
))
1048 (and (get-buffer buffer-name
)
1049 (kill-buffer buffer-name
))))))
1052 (provide 'type-break
)
1055 (type-break-mode 1))
1057 ;;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
1058 ;;; type-break.el ends here