1 ;;; type-break.el --- encourage rests from typing at appropriate intervals
3 ;; Copyright (C) 1994, 1995, 1997, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Noah Friedman
7 ;; Maintainer: Noah Friedman <friedman@splode.com>
8 ;; Keywords: extensions, timers
9 ;; Status: Works in GNU Emacs 19.25 or later, some versions of XEmacs
10 ;; Created: 1994-07-13
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; The docstring for the function `type-break-mode' summarizes most of the
30 ;; details of the interface.
32 ;; This package relies on the assumption that you live entirely in Emacs,
33 ;; as the author does. If that's not the case for you (e.g. you often
34 ;; suspend Emacs or work in other windows) then this won't help very much;
35 ;; it will depend on just how often you switch back to Emacs. At the very
36 ;; least, you will want to turn off the keystroke thresholds and rest
39 ;; If you prefer not to be queried about taking breaks, but instead just
40 ;; want to be reminded, do the following:
42 ;; (setq type-break-query-mode nil)
44 ;; Or call the command `type-break-query-mode' with a negative prefix
47 ;; If you find echo area messages annoying and would prefer to see messages
48 ;; in the mode line instead, do M-x type-break-mode-line-message-mode
49 ;; or set the variable of the same name to `t'.
51 ;; This program can truly cons up a storm because of all the calls to
52 ;; `current-time' (which always returns 3 fresh conses). I'm dismayed by
53 ;; this, but I think the health of my hands is far more important than a
54 ;; few pages of virtual memory.
56 ;; This program has no hope of working in Emacs 18.
58 ;; This package was inspired by Roland McGrath's hanoi-break.el.
59 ;; Several people contributed feedback and ideas, including
60 ;; Roland McGrath <roland@gnu.org>
61 ;; Kleanthes Koniaris <kgk@koniaris.com>
62 ;; Mark Ashton <mpashton@gnu.org>
63 ;; Matt Wilding <wilding@cli.com>
64 ;; Robert S. Boyer <boyer@cs.utexas.edu>
69 (defgroup type-break nil
70 "Encourage the user to take a rest from typing at suitable intervals."
75 (defcustom type-break-mode nil
76 "Toggle typing break mode.
77 See the docstring for the `type-break-mode' command for more information.
78 Setting this variable directly does not take effect;
79 use either \\[customize] or the function `type-break-mode'."
80 :set
(lambda (symbol value
)
81 (type-break-mode (if value
1 -
1)))
82 :initialize
'custom-initialize-default
88 (defcustom type-break-interval
(* 60 60)
89 "Number of seconds between scheduled typing breaks."
94 (defcustom type-break-good-rest-interval
(/ type-break-interval
6)
95 "Number of seconds of idle time considered to be an adequate typing rest.
97 When this variable is non-nil, Emacs checks the idle time between
98 keystrokes. If this idle time is long enough to be considered a \"good\"
99 rest from typing, then the next typing break is simply rescheduled for later.
101 If a break is interrupted before this much time elapses, the user will be
102 asked whether or not really to interrupt the break."
107 (defcustom type-break-good-break-interval nil
108 "Number of seconds considered to be an adequate explicit typing rest.
110 When this variable is non-nil, its value is considered to be a \"good\"
111 length (in seconds) for a break initiated by the command `type-break',
112 overriding `type-break-good-rest-interval'. This provides querying of
113 break interruptions when `type-break-good-rest-interval' is nil."
118 (defcustom type-break-keystroke-threshold
119 ;; Assuming typing speed is 35wpm (on the average, do you really
120 ;; type more than that in a minute? I spend a lot of time reading mail
121 ;; and simply studying code in buffers) and average word length is
122 ;; about 5 letters, default upper threshold to the average number of
123 ;; keystrokes one is likely to type in a break interval. That way if the
124 ;; user goes through a furious burst of typing activity, cause a typing
125 ;; break to be required sooner than originally scheduled.
126 ;; Conversely, the minimum threshold should be about a fifth of this.
129 (upper (* wpm avg-word-length
(/ type-break-interval
60)))
132 "Upper and lower bound on number of keystrokes for considering typing break.
133 This structure is a pair of numbers (MIN . MAX).
135 The first number is the minimum number of keystrokes that must have been
136 entered since the last typing break before considering another one, even if
137 the scheduled time has elapsed; the break is simply rescheduled until later
138 if the minimum threshold hasn't been reached. If this first value is nil,
139 then there is no minimum threshold; as soon as the scheduled time has
140 elapsed, the user will always be queried.
142 The second number is the maximum number of keystrokes that can be entered
143 before a typing break is requested immediately, pre-empting the originally
144 scheduled break. If this second value is nil, then no pre-emptive breaks
145 will occur; only scheduled ones will.
147 Keys with bucky bits (shift, control, meta, etc) are counted as only one
148 keystroke even though they really require multiple keys to generate them.
150 The command `type-break-guesstimate-keystroke-threshold' can be used to
151 guess a reasonably good pair of values for this variable."
155 (defcustom type-break-query-mode t
156 "Non-nil means ask whether or not to prompt user for breaks.
157 If so, call the function specified in the value of the variable
158 `type-break-query-function' to do the asking."
162 (defcustom type-break-query-function
'yes-or-no-p
163 "Function to use for making query for a typing break.
164 It should take a string as an argument, the prompt.
165 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
167 To avoid being queried at all, set `type-break-query-mode' to nil."
168 :type
'(radio function
169 (function-item yes-or-no-p
)
170 (function-item y-or-n-p
))
173 (defcustom type-break-query-interval
60
174 "Number of seconds between queries to take a break, if put off.
175 The user will continue to be prompted at this interval until he or she
176 finally submits to taking a typing break."
180 (defcustom type-break-time-warning-intervals
'(300 120 60 30)
181 "List of time intervals for warnings about upcoming typing break.
182 At each of the intervals (specified in seconds) away from a scheduled
183 typing break, print a warning in the echo area."
184 :type
'(repeat integer
)
187 (defcustom type-break-keystroke-warning-intervals
'(300 200 100 50)
188 "List of keystroke measurements for warnings about upcoming typing break.
189 At each of the intervals (specified in keystrokes) away from the upper
190 keystroke threshold, print a warning in the echo area.
191 If either this variable or the upper threshold is set, then no warnings
193 :type
'(repeat integer
)
196 (defcustom type-break-warning-repeat
40
197 "Number of keystrokes for which warnings should be repeated.
198 That is, for each of this many keystrokes the warning is redisplayed
199 in the echo area to make sure it's really seen."
203 (defcustom type-break-time-stamp-format
"[%H:%M] "
204 "Timestamp format used to prefix messages.
205 Format specifiers are as used by `format-time-string'."
209 (defcustom type-break-demo-functions
210 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi
)
211 "List of functions to consider running as demos during typing breaks.
212 When a typing break begins, one of these functions is selected randomly
213 to have Emacs do something interesting.
215 Any function in this list should start a demo which ceases as soon as a
217 :type
'(repeat function
)
220 (defcustom type-break-demo-boring-stats nil
221 "Show word per minute and keystroke figures in the Boring demo."
225 (defcustom type-break-terse-messages nil
226 "Use slightly terser messages."
230 (defcustom type-break-file-name
(convert-standard-filename "~/.type-break")
231 "Name of file used to save state across sessions.
232 If this is nil, no data will be saved across sessions."
236 (defvar type-break-post-command-hook
'(type-break-check)
237 "Hook run indirectly by `post-command-hook' for typing break functions.
238 This is not really intended to be set by the user, but it's probably
239 harmless to do so. Mainly it is used by various parts of the typing break
240 program to delay actions until after the user has completed some command.
241 It exists because `post-command-hook' itself is inaccessible while its
242 functions are being run, and some type-break--related functions want to
243 remove themselves after running.")
248 (defcustom type-break-mode-line-message-mode nil
249 "Non-nil means put type-break related messages in the mode line.
250 Otherwise, messages typically go in the echo area.
252 See also `type-break-mode-line-format' and its members."
256 (defvar type-break-mode-line-format
257 '(type-break-mode-line-message-mode
259 type-break-mode-line-break-message
260 type-break-mode-line-warning
))
261 "*Format of messages in the mode line concerning typing breaks.")
263 (defvar type-break-mode-line-break-message
264 '(type-break-mode-line-break-message-p
265 type-break-mode-line-break-string
))
267 (defvar type-break-mode-line-break-message-p nil
)
268 (defvar type-break-mode-line-break-string
" *** TAKE A TYPING BREAK NOW ***")
270 (defvar type-break-mode-line-warning
271 '(type-break-mode-line-break-message-p
273 (type-break-warning-countdown-string
276 type-break-warning-countdown-string
278 type-break-warning-countdown-string-type
281 (defvar type-break-warning-countdown-string nil
282 "If non-nil, this is a countdown for the next typing break.
284 This variable, in conjunction with `type-break-warning-countdown-string-type'
285 \(which indicates whether this value is a number of keystrokes or seconds)
286 is installed in `mode-line-format' to notify of imminent typing breaks.")
288 (defvar type-break-warning-countdown-string-type nil
289 "Indicates the unit type of `type-break-warning-countdown-string'.
290 It will be either \"seconds\" or \"keystrokes\".")
293 ;; These are internal variables. Do not set them yourself.
295 (defvar type-break-alarm-p nil
)
296 (defvar type-break-keystroke-count
0)
297 (defvar type-break-time-last-break nil
)
298 (defvar type-break-time-next-break nil
)
299 (defvar type-break-time-last-command
(current-time))
300 (defvar type-break-current-time-warning-interval nil
)
301 (defvar type-break-current-keystroke-warning-interval nil
)
302 (defvar type-break-time-warning-count
0)
303 (defvar type-break-keystroke-warning-count
0)
304 (defvar type-break-interval-start nil
)
308 (defun type-break-mode (&optional prefix
)
309 "Enable or disable typing-break mode.
310 This is a minor mode, but it is global to all buffers by default.
312 When this mode is enabled, the user is encouraged to take typing breaks at
313 appropriate intervals; either after a specified amount of time or when the
314 user has exceeded a keystroke threshold. When the time arrives, the user
315 is asked to take a break. If the user refuses at that time, Emacs will ask
316 again in a short period of time. The idea is to give the user enough time
317 to find a good breaking point in his or her work, but be sufficiently
318 annoying to discourage putting typing breaks off indefinitely.
320 A negative prefix argument disables this mode.
321 No argument or any non-negative argument enables it.
323 The user may enable or disable this mode by setting the variable of the
324 same name, though setting it in that way doesn't reschedule a break or
325 reset the keystroke counter.
327 If the mode was previously disabled and is enabled as a consequence of
328 calling this function, it schedules a break with `type-break-schedule' to
329 make sure one occurs (the user can call that command to reschedule the
330 break at any time). It also initializes the keystroke counter.
332 The variable `type-break-interval' specifies the number of seconds to
333 schedule between regular typing breaks. This variable doesn't directly
334 affect the time schedule; it simply provides a default for the
335 `type-break-schedule' command.
337 If set, the variable `type-break-good-rest-interval' specifies the minimum
338 amount of time which is considered a reasonable typing break. Whenever
339 that time has elapsed, typing breaks are automatically rescheduled for
340 later even if Emacs didn't prompt you to take one first. Also, if a break
341 is ended before this much time has elapsed, the user will be asked whether
342 or not to continue. A nil value for this variable prevents automatic
343 break rescheduling, making `type-break-interval' an upper bound on the time
344 between breaks. In this case breaks will be prompted for as usual before
345 the upper bound if the keystroke threshold is reached.
347 If `type-break-good-rest-interval' is nil and
348 `type-break-good-break-interval' is set, then confirmation is required to
349 interrupt a break before `type-break-good-break-interval' seconds
350 have passed. This provides for an upper bound on the time between breaks
351 together with confirmation of interruptions to these breaks.
353 The variable `type-break-keystroke-threshold' is used to determine the
354 thresholds at which typing breaks should be considered. You can use
355 the command `type-break-guesstimate-keystroke-threshold' to try to
356 approximate good values for this.
358 There are several variables that affect how or when warning messages about
359 imminent typing breaks are displayed. They include:
361 `type-break-mode-line-message-mode'
362 `type-break-time-warning-intervals'
363 `type-break-keystroke-warning-intervals'
364 `type-break-warning-repeat'
365 `type-break-warning-countdown-string'
366 `type-break-warning-countdown-string-type'
368 There are several variables that affect if, how, and when queries to begin
369 a typing break occur. They include:
371 `type-break-query-mode'
372 `type-break-query-function'
373 `type-break-query-interval'
375 The command `type-break-statistics' prints interesting things.
377 Finally, a file (named `type-break-file-name') is used to store information
378 across Emacs sessions. This provides recovery of the break status between
379 sessions and after a crash. Manual changes to the file may result in
382 (type-break-check-post-command-hook)
384 (let ((already-enabled type-break-mode
))
385 (setq type-break-mode
(>= (prefix-numeric-value prefix
) 0))
388 ((and already-enabled type-break-mode
)
389 (and (called-interactively-p 'interactive
)
390 (message "Type Break mode is already enabled")))
392 (when type-break-file-name
393 (with-current-buffer (find-file-noselect type-break-file-name
'nowarn
)
394 (setq buffer-save-without-query t
)))
396 (or global-mode-string
397 (setq global-mode-string
'("")))
398 (or (assq 'type-break-mode-line-message-mode
400 (setq minor-mode-alist
401 (cons type-break-mode-line-format
403 (type-break-keystroke-reset)
404 (type-break-mode-line-countdown-or-break nil
)
406 (setq type-break-time-last-break
407 (or (type-break-get-previous-time)
410 ;; schedule according to break time from session file
413 (if (and type-break-time-last-break
414 (< (setq diff
(type-break-time-difference
415 type-break-time-last-break
417 type-break-interval
))
418 ;; use the file's value
420 (setq type-break-keystroke-count
421 (type-break-get-previous-count))
422 ;; file the time, in case it was read from the auto-save file
423 (type-break-file-time type-break-interval-start
)
424 (setq type-break-interval-start type-break-time-last-break
)
425 (- type-break-interval diff
))
427 (setq type-break-interval-start
(current-time))
428 (type-break-file-time type-break-interval-start
)
429 type-break-interval
))
430 type-break-interval-start
433 (and (called-interactively-p 'interactive
)
434 (message "Type Break mode is enabled and set")))
436 (type-break-keystroke-reset)
437 (type-break-mode-line-countdown-or-break nil
)
438 (type-break-cancel-schedule)
440 (when type-break-file-name
441 (with-current-buffer (find-file-noselect type-break-file-name
443 (set-buffer-modified-p nil
)
446 (and (called-interactively-p 'interactive
)
447 (message "Type Break mode is disabled")))))
450 (defun type-break-mode-line-message-mode (&optional prefix
)
451 "Enable or disable warnings in the mode line about typing breaks.
453 A negative PREFIX argument disables this mode.
454 No argument or any non-negative argument enables it.
456 The user may also enable or disable this mode simply by setting the
457 variable of the same name.
459 Variables controlling the display of messages in the mode line include:
463 `type-break-mode-line-break-message'
464 `type-break-mode-line-warning'"
466 (setq type-break-mode-line-message-mode
467 (>= (prefix-numeric-value prefix
) 0))
468 (and (called-interactively-p 'interactive
)
469 (if type-break-mode-line-message-mode
470 (message "type-break-mode-line-message-mode is enabled")
471 (message "type-break-mode-line-message-mode is disabled")))
472 type-break-mode-line-message-mode
)
474 (defun type-break-query-mode (&optional prefix
)
475 "Enable or disable warnings in the mode line about typing breaks.
477 When enabled, the user is periodically queried about whether to take a
478 typing break at that moment. The function which does this query is
479 specified by the variable `type-break-query-function'.
481 A negative PREFIX argument disables this mode.
482 No argument or any non-negative argument enables it.
484 The user may also enable or disable this mode simply by setting the
485 variable of the same name."
487 (setq type-break-query-mode
488 (>= (prefix-numeric-value prefix
) 0))
489 (and (called-interactively-p 'interactive
)
490 (if type-break-query-mode
491 (message "type-break-query-mode is enabled")
492 (message "type-break-query-mode is disabled")))
493 type-break-query-mode
)
496 ;;; session file functions
498 (defvar type-break-auto-save-file-name
499 (let ((buffer-file-name type-break-file-name
))
500 (make-auto-save-file-name))
501 "Auto-save name of `type-break-file-name'.")
503 (defun type-break-file-time (&optional time
)
504 "File break time in `type-break-file-name', unless the file is locked."
505 (if (and type-break-file-name
506 (not (stringp (file-locked-p type-break-file-name
))))
507 (with-current-buffer (find-file-noselect type-break-file-name
509 (let ((inhibit-read-only t
))
511 (insert (format "%s\n\n" (or time type-break-interval-start
)))
512 ;; file saving is left to auto-save
515 (defun type-break-file-keystroke-count ()
516 "File keystroke count in `type-break-file-name', unless the file is locked."
517 (if (and type-break-file-name
518 (not (stringp (file-locked-p type-break-file-name
))))
519 ;; Prevent deactivation of the mark in some other buffer.
520 (let (deactivate-mark)
521 (with-current-buffer (find-file-noselect type-break-file-name
524 (let ((inhibit-read-only t
))
525 (goto-char (point-min))
527 (delete-region (point) (save-excursion (end-of-line) (point)))
528 (insert (format "%s" type-break-keystroke-count
))
529 ;; file saving is left to auto-save
533 "If TIME is in the format returned by `current-time' then
534 return TIME, else return nil."
537 (integerp (car time
))
538 (integerp (nth 1 time
))
539 (integerp (nth 2 time
))
542 (defun type-break-choose-file ()
543 "Return file to read from."
545 ((not type-break-file-name
)
547 ((and (file-exists-p type-break-auto-save-file-name
)
548 (file-readable-p type-break-auto-save-file-name
))
549 type-break-auto-save-file-name
)
550 ((and (file-exists-p type-break-file-name
)
551 (file-readable-p type-break-file-name
))
552 type-break-file-name
)
555 (defun type-break-get-previous-time ()
556 "Get previous break time from `type-break-file-name'.
557 Returns nil if the file is missing or if the time breaks with the
558 `current-time' format."
559 (let ((file (type-break-choose-file)))
561 (timep ;; returns expected format, else nil
562 (with-current-buffer (find-file-noselect file
'nowarn
)
565 (goto-char (point-min))
566 (read (current-buffer)))
568 (error "End of file in `%s'" file
))))))))
570 (defun type-break-get-previous-count ()
571 "Get previous keystroke count from `type-break-file-name'.
572 Return 0 if the file is missing or if the form read is not an
574 (let ((file (type-break-choose-file)))
579 (find-file-noselect file
'nowarn
)
582 (goto-char (point-min))
584 (read (current-buffer)))
586 (error "End of file in `%s'" file
)))))))
593 "Take a typing break.
595 During the break, a demo selected from the functions listed in
596 `type-break-demo-functions' is run.
598 After the typing break is finished, the next break is scheduled
599 as per the function `type-break-schedule'."
602 (type-break-cancel-schedule)
603 ;; remove any query scheduled during interactive invocation
604 (remove-hook 'type-break-post-command-hook
'type-break-do-query
)
606 (start-time (current-time)))
607 (setq type-break-time-last-break start-time
)
609 (save-window-excursion
611 (and (eq (selected-window) (minibuffer-window))
613 (delete-other-windows)
614 (scroll-right (window-width))
615 (unless type-break-terse-messages
616 (message "Press any key to resume from typing break."))
619 (let* ((len (length type-break-demo-functions
))
621 (fn (nth idx type-break-demo-functions
)))
626 (let ((good-interval (or type-break-good-rest-interval
627 type-break-good-break-interval
)))
630 (let ((break-secs (type-break-time-difference
631 start-time
(current-time))))
633 ((>= break-secs good-interval
)
635 ;; 60 seconds may be too much leeway if the break is only 3
636 ;; minutes to begin with. You can just say "no" to the query
637 ;; below if you're in that much of a hurry.
638 ;;((> 60 (abs (- break-secs good-interval)))
639 ;; (setq continue nil))
641 type-break-query-function
643 (if type-break-terse-messages
644 "%s%s remaining. Continue break? "
645 "%sYou really ought to rest %s more. Continue break? ")
646 (type-break-time-stamp)
647 (type-break-format-time (- good-interval
650 (setq continue nil
)))))
651 (t (setq continue nil
))))))
653 (type-break-keystroke-reset)
654 (type-break-file-time)
655 (type-break-mode-line-countdown-or-break nil
)
656 (type-break-schedule))
659 (defun type-break-schedule (&optional time start interval
)
660 "Schedule a typing break for TIME seconds from now.
661 If time is not specified it defaults to `type-break-interval'.
662 START and INTERVAL are used when recovering a break.
663 START is the start of the break (defaults to now).
664 INTERVAL is the full length of an interval (defaults to TIME)."
665 (interactive (list (and current-prefix-arg
666 (prefix-numeric-value current-prefix-arg
))))
667 (or time
(setq time type-break-interval
))
668 (type-break-check-post-command-hook)
669 (type-break-cancel-schedule)
670 (type-break-time-warning-schedule time
'reset
)
671 (type-break-run-at-time (max 1 time
) nil
'type-break-alarm
)
672 (setq type-break-time-next-break
673 (type-break-time-sum (or start
(current-time))
674 (or interval time
))))
676 (defun type-break-cancel-schedule ()
677 (type-break-cancel-time-warning-schedule)
678 (type-break-cancel-function-timers 'type-break-alarm
)
679 (setq type-break-alarm-p nil
)
680 (setq type-break-time-next-break nil
))
682 (defun type-break-time-warning-schedule (&optional time resetp
)
683 (let ((type-break-current-time-warning-interval nil
))
684 (type-break-cancel-time-warning-schedule))
685 (add-hook 'type-break-post-command-hook
'type-break-time-warning
'append
)
687 (type-break-time-warning-intervals
689 (setq type-break-current-time-warning-interval
690 type-break-time-warning-intervals
))
693 (setq time
(type-break-time-difference (current-time)
694 type-break-time-next-break
)))
696 (while (and type-break-current-time-warning-interval
697 (> (car type-break-current-time-warning-interval
) time
))
698 (setq type-break-current-time-warning-interval
699 (cdr type-break-current-time-warning-interval
)))
702 (type-break-current-time-warning-interval
703 (setq time
(- time
(car type-break-current-time-warning-interval
)))
704 (setq type-break-current-time-warning-interval
705 (cdr type-break-current-time-warning-interval
))
707 ;(let (type-break-current-time-warning-interval)
708 ; (type-break-cancel-time-warning-schedule))
709 (type-break-run-at-time (max 1 time
) nil
'type-break-time-warning-alarm
)
713 (setq type-break-warning-countdown-string nil
))
715 (setq type-break-warning-countdown-string
(number-to-string time
))
716 (setq type-break-warning-countdown-string-type
"seconds"))))))))
718 (defun type-break-cancel-time-warning-schedule ()
719 (type-break-cancel-function-timers 'type-break-time-warning-alarm
)
720 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
721 (setq type-break-current-time-warning-interval
722 type-break-time-warning-intervals
)
723 (setq type-break-time-warning-count
0) ; avoid warnings after break
724 (setq type-break-warning-countdown-string nil
))
726 (defun type-break-alarm ()
727 (type-break-check-post-command-hook)
728 (setq type-break-alarm-p t
)
729 (type-break-mode-line-countdown-or-break 'break
))
731 (defun type-break-time-warning-alarm ()
732 (type-break-check-post-command-hook)
733 (type-break-time-warning-schedule)
734 (setq type-break-time-warning-count type-break-warning-repeat
)
735 (type-break-time-warning)
736 (type-break-mode-line-countdown-or-break 'countdown
))
739 (defun type-break-run-tb-post-command-hook ()
741 (run-hooks 'type-break-post-command-hook
)))
743 (defun type-break-check ()
744 "Ask to take a typing break if appropriate.
745 This may be the case either because the scheduled time has come \(and the
746 minimum keystroke threshold has been reached\) or because the maximum
747 keystroke threshold has been exceeded."
748 (type-break-file-keystroke-count)
749 (let* ((min-threshold (car type-break-keystroke-threshold
))
750 (max-threshold (cdr type-break-keystroke-threshold
)))
751 (and type-break-good-rest-interval
753 (and (> (type-break-time-difference
754 type-break-time-last-command
(current-time))
755 type-break-good-rest-interval
)
757 (type-break-keystroke-reset)
758 (type-break-mode-line-countdown-or-break nil
)
759 (setq type-break-time-last-break
(current-time))
760 (type-break-schedule)))
761 (setq type-break-time-last-command
(current-time))))
763 (and type-break-keystroke-threshold
764 (let ((keys (this-command-keys)))
766 ;; Ignore mouse motion
768 (consp (aref keys
0))
769 (memq (car (aref keys
0)) '(mouse-movement))))
771 (setq type-break-keystroke-count
772 (+ type-break-keystroke-count
(length keys
)))))))
778 ((eq (selected-window) (minibuffer-window)))
780 (< type-break-keystroke-count min-threshold
))
781 (type-break-schedule))
783 ;; If keystroke count is within min-threshold of
784 ;; max-threshold, lower it to reduce the likelihood of an
785 ;; immediate subsequent query.
788 (< (- max-threshold type-break-keystroke-count
) min-threshold
)
790 (type-break-keystroke-reset)
791 (setq type-break-keystroke-count min-threshold
)))
792 (type-break-query))))
793 ((and type-break-keystroke-warning-intervals
795 (= type-break-keystroke-warning-count
0)
796 (type-break-check-keystroke-warning)))
798 (> type-break-keystroke-count max-threshold
)
799 (not (input-pending-p))
800 (not (eq (selected-window) (minibuffer-window))))
801 (type-break-keystroke-reset)
802 (setq type-break-keystroke-count
(or min-threshold
0))
803 (type-break-query)))))
805 ;; This should return t if warnings were enabled, nil otherwise.
806 (defun type-break-check-keystroke-warning ()
807 ;; This is safe because the caller should have checked that the cdr was
809 (let ((left (- (cdr type-break-keystroke-threshold
)
810 type-break-keystroke-count
)))
812 ((null (car type-break-current-keystroke-warning-interval
))
814 ((> left
(car type-break-current-keystroke-warning-interval
))
817 (while (and (car type-break-current-keystroke-warning-interval
)
818 (< left
(car type-break-current-keystroke-warning-interval
)))
819 (setq type-break-current-keystroke-warning-interval
820 (cdr type-break-current-keystroke-warning-interval
)))
821 (setq type-break-keystroke-warning-count type-break-warning-repeat
)
822 (add-hook 'type-break-post-command-hook
'type-break-keystroke-warning
)
823 (setq type-break-warning-countdown-string
(number-to-string left
))
824 (setq type-break-warning-countdown-string-type
"keystrokes")
825 (type-break-mode-line-countdown-or-break 'countdown
)
828 ;; Arrange for a break query to be made, when the user stops typing furiously.
829 (defun type-break-query ()
830 (add-hook 'type-break-post-command-hook
'type-break-do-query
))
832 (defun type-break-do-query ()
834 ((not type-break-query-mode
)
835 (type-break-noninteractive-query)
836 (type-break-schedule type-break-query-interval
)
837 (remove-hook 'type-break-post-command-hook
'type-break-do-query
))
841 ((let ((type-break-mode nil
)
842 ;; yes-or-no-p sets this-command to exit-minibuffer,
843 ;; which hoses undo or yank-pop (if you happened to be
844 ;; yanking just when the query occurred).
845 (this-command this-command
))
846 ;; Cancel schedule to prevent possibility of a second query
847 ;; from taking place before this one has even returned.
848 ;; The condition-case wrapper will reschedule on quit.
849 (type-break-cancel-schedule)
850 ;; Also prevent a second query when the break is interrupted.
851 (remove-hook 'type-break-post-command-hook
'type-break-do-query
)
852 (funcall type-break-query-function
854 (type-break-time-stamp)
855 (if type-break-terse-messages
857 "Take a break from typing now? "))))
860 (type-break-schedule type-break-query-interval
)))
862 (type-break-schedule type-break-query-interval
))))))
864 (defun type-break-noninteractive-query (&optional ignored-args
)
865 "Null query function which doesn't interrupt user and assumes `no'.
866 It prints a reminder in the echo area to take a break, but doesn't enforce
867 this or ask the user to start one right now."
869 (type-break-mode-line-message-mode)
872 (message "%sYou should take a typing break now. Do `M-x type-break'."
873 (type-break-time-stamp))
876 ;; return nil so query caller knows to reset reminder, as if user
877 ;; said "no" in response to yes-or-no-p.
880 (defun type-break-time-warning ()
882 ((and (car type-break-keystroke-threshold
)
883 (< type-break-keystroke-count
(car type-break-keystroke-threshold
))))
884 ((> type-break-time-warning-count
0)
885 (let ((timeleft (type-break-time-difference (current-time)
886 type-break-time-next-break
)))
887 (setq type-break-warning-countdown-string
(number-to-string timeleft
))
889 ((eq (selected-window) (minibuffer-window)))
890 ;; Do nothing if the command was just a prefix arg, since that will
891 ;; immediately be followed by some other interactive command.
892 ;; Otherwise, it is particularly annoying for the sit-for below to
893 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
894 ((memq this-command
'(digit-argument universal-argument
)))
895 ((not type-break-mode-line-message-mode
)
896 ;; Pause for a moment so any previous message can be seen.
898 (message "%sWarning: typing break due in %s."
899 (type-break-time-stamp)
900 (type-break-format-time timeleft
))
901 (setq type-break-time-warning-count
902 (1- type-break-time-warning-count
))))))
904 (remove-hook 'type-break-post-command-hook
'type-break-time-warning
)
905 (setq type-break-warning-countdown-string nil
))))
907 (defun type-break-keystroke-warning ()
909 ((> type-break-keystroke-warning-count
0)
910 (setq type-break-warning-countdown-string
911 (number-to-string (- (cdr type-break-keystroke-threshold
)
912 type-break-keystroke-count
)))
914 ((eq (selected-window) (minibuffer-window)))
915 ;; Do nothing if the command was just a prefix arg, since that will
916 ;; immediately be followed by some other interactive command.
917 ;; Otherwise, it is particularly annoying for the sit-for below to
918 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
919 ((memq this-command
'(digit-argument universal-argument
)))
920 ((not type-break-mode-line-message-mode
)
922 (message "%sWarning: typing break due in %s keystrokes."
923 (type-break-time-stamp)
924 (- (cdr type-break-keystroke-threshold
)
925 type-break-keystroke-count
))
926 (setq type-break-keystroke-warning-count
927 (1- type-break-keystroke-warning-count
)))))
929 (remove-hook 'type-break-post-command-hook
930 'type-break-keystroke-warning
)
931 (setq type-break-warning-countdown-string nil
))))
933 (defun type-break-mode-line-countdown-or-break (&optional type
)
935 ((not type-break-mode-line-message-mode
))
936 ((eq type
'countdown
)
937 ;(setq type-break-mode-line-break-message-p nil)
938 (add-hook 'type-break-post-command-hook
939 'type-break-force-mode-line-update
'append
))
942 (setq type-break-mode-line-break-message-p
943 (not type-break-mode-line-break-message-p
))
944 (remove-hook 'type-break-post-command-hook
945 'type-break-force-mode-line-update
))
947 (setq type-break-mode-line-break-message-p nil
)
948 (setq type-break-warning-countdown-string nil
)
949 (remove-hook 'type-break-post-command-hook
950 'type-break-force-mode-line-update
)))
951 (type-break-force-mode-line-update))
955 (defun type-break-statistics ()
956 "Print statistics about typing breaks in a temporary buffer.
957 This includes the last time a typing break was taken, when the next one is
958 scheduled, the keystroke thresholds and the current keystroke count, etc."
960 (with-output-to-temp-buffer "*Typing Break Statistics*"
961 (princ (format "Typing break statistics\n-----------------------\n
962 Typing break mode is currently %s.
963 Interactive query for breaks is %s.
964 Warnings of imminent typing breaks in mode line is %s.
966 Last typing break ended : %s
967 Next scheduled typing break : %s\n
968 Minimum keystroke threshold : %s
969 Maximum keystroke threshold : %s
970 Current keystroke count : %s"
971 (if type-break-mode
"enabled" "disabled")
972 (if type-break-query-mode
"enabled" "disabled")
973 (if type-break-mode-line-message-mode
"enabled" "disabled")
974 (if type-break-time-last-break
975 (current-time-string type-break-time-last-break
)
977 (if (and type-break-mode type-break-time-next-break
)
978 (format "%s\t(%s from now)"
979 (current-time-string type-break-time-next-break
)
980 (type-break-format-time
981 (type-break-time-difference
983 type-break-time-next-break
)))
985 (or (car type-break-keystroke-threshold
) "none")
986 (or (cdr type-break-keystroke-threshold
) "none")
987 type-break-keystroke-count
))))
990 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac
)
991 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
993 If called interactively, the user is prompted for their guess as to how
994 many words per minute they usually type. This value should not be your
995 maximum WPM, but your average. Of course, this is harder to gauge since it
996 can vary considerably depending on what you are doing. For example, one
997 tends to type less when debugging a program as opposed to writing
998 documentation. (Perhaps a separate program should be written to estimate
999 average typing speed.)
1001 From that, this command sets the values in `type-break-keystroke-threshold'
1002 based on a fairly simple algorithm involving assumptions about the average
1003 length of words (5). For the minimum threshold, it uses about a fifth of
1004 the computed maximum threshold.
1006 When called from Lisp programs, the optional args WORDLEN and FRAC can be
1007 used to override the default assumption about average word length and the
1008 fraction of the maximum threshold to which to set the minimum threshold.
1009 FRAC should be the inverse of the fractional value; for example, a value of
1010 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
1011 (interactive "NOn average, how many words per minute do you type? ")
1012 (let* ((upper (* wpm
(or wordlen
5) (/ type-break-interval
60)))
1013 (lower (/ upper
(or frac
5))))
1014 (or type-break-keystroke-threshold
1015 (setq type-break-keystroke-threshold
(cons nil nil
)))
1016 (setcar type-break-keystroke-threshold lower
)
1017 (setcdr type-break-keystroke-threshold upper
)
1018 (if (called-interactively-p 'interactive
)
1019 (message "min threshold: %d\tmax threshold: %d" lower upper
))
1020 type-break-keystroke-threshold
))
1025 ;; Compute the difference, in seconds, between a and b, two structures
1026 ;; similar to those returned by `current-time'.
1027 ;; Use addition rather than logand since that is more robust; the low 16
1028 ;; bits of the seconds might have been incremented, making it more than 16
1030 (defun type-break-time-difference (a b
)
1031 (+ (lsh (- (car b
) (car a
)) 16)
1032 (- (car (cdr b
)) (car (cdr a
)))))
1034 ;; Return (in a new list the same in structure to that returned by
1035 ;; `current-time') the sum of the arguments. Each argument may be a time
1036 ;; list or a single integer, a number of seconds.
1037 ;; This function keeps the high and low 16 bits of the seconds properly
1038 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
1039 ;; the result is passed to `current-time-string' it will toss some of the
1040 ;; "low" bits and format the time incorrectly.
1041 (defun type-break-time-sum (&rest tmlist
)
1047 (setq tem
(car tmlist
))
1048 (setq tmlist
(cdr tmlist
))
1051 (setq low
(+ low tem
)))
1053 (setq high
(+ high
(or (car tem
) 0)))
1054 (setq low
(+ low
(or (car (cdr tem
)) 0)))
1055 (setq micro
(+ micro
(or (car (cdr (cdr tem
))) 0))))))
1057 (and (>= micro
1000000)
1059 (setq tem
(/ micro
1000000))
1060 (setq low
(+ low tem
))
1061 (setq micro
(- micro
(* tem
1000000)))))
1063 (setq tem
(lsh low -
16))
1066 (setq low
(logand low
65535))
1067 (setq high
(+ high tem
))))
1069 (list high low micro
)))
1071 (defun type-break-time-stamp (&optional when
)
1072 (if (fboundp 'format-time-string
)
1073 (format-time-string type-break-time-stamp-format when
)
1074 ;; Emacs 19.28 and prior do not have format-time-string.
1075 ;; In that case, result is not customizable. Upgrade today!
1076 (format "[%s] " (substring (current-time-string when
) 11 16))))
1078 (defun type-break-format-time (secs)
1079 (let ((mins (/ secs
60)))
1081 ((= mins
1) (format "%d minute" mins
))
1082 ((> mins
0) (format "%d minutes" mins
))
1083 ((= secs
1) (format "%d second" secs
))
1084 (t (format "%d seconds" secs
)))))
1086 (defun type-break-keystroke-reset ()
1087 (setq type-break-interval-start
(current-time)) ; not a keystroke
1088 (setq type-break-keystroke-count
0)
1089 (setq type-break-keystroke-warning-count
0)
1090 (setq type-break-current-keystroke-warning-interval
1091 type-break-keystroke-warning-intervals
)
1092 (remove-hook 'type-break-post-command-hook
'type-break-keystroke-warning
))
1094 (defun type-break-force-mode-line-update (&optional all
)
1095 "Force the mode-line of the current buffer to be redisplayed.
1096 With optional non-nil ALL, force redisplay of all mode-lines."
1097 (and all
(with-current-buffer (other-buffer)))
1098 (set-buffer-modified-p (buffer-modified-p)))
1100 ;; If an exception occurs in Emacs while running the post command hook, the
1101 ;; value of that hook is clobbered. This is because the value of the
1102 ;; variable is temporarily set to nil while it's running to prevent
1103 ;; recursive application, but it also means an exception aborts the routine
1104 ;; of restoring it. This function is called from the timers to restore it,
1106 (defun type-break-check-post-command-hook ()
1107 (add-hook 'post-command-hook
'type-break-run-tb-post-command-hook
'append
))
1110 ;;; Timer wrapper functions
1112 ;; These shield type-break from variations in the interval timer packages
1113 ;; for different versions of Emacs.
1115 (defun type-break-run-at-time (time repeat function
)
1116 (condition-case nil
(or (require 'timer
) (require 'itimer
)) (error nil
))
1117 (run-at-time time repeat function
))
1119 (defvar timer-dont-exit
)
1120 (defun type-break-cancel-function-timers (function)
1121 (let ((timer-dont-exit t
))
1122 (cancel-function-timers function
)))
1127 (defun type-break-catch-up-event ()
1128 ;; If the last input event is a down-event, read and discard the
1129 ;; corresponding up-event too, to avoid triggering another prompt.
1130 (and (eventp last-input-event
)
1131 (memq 'down
(event-modifiers last-input-event
))
1134 ;; This is a wrapper around hanoi that calls it with an arg large enough to
1135 ;; make the largest discs possible that will fit in the window.
1136 ;; Also, clean up the *Hanoi* buffer after we're done.
1137 (defun type-break-demo-hanoi ()
1138 "Take a hanoiing typing break."
1139 (and (get-buffer "*Hanoi*")
1140 (kill-buffer "*Hanoi*"))
1143 (hanoi (/ (window-width) 8))
1144 ;; Wait for user to come back.
1146 (type-break-catch-up-event)
1147 (kill-buffer "*Hanoi*"))
1150 (type-break-catch-up-event)
1151 (and (get-buffer "*Hanoi*")
1152 (kill-buffer "*Hanoi*")))))
1154 ;; This is a wrapper around life that calls it with a `sleep' arg to make
1155 ;; it run a little more leisurely.
1156 ;; Also, clean up the *Life* buffer after we're done.
1157 (defun type-break-demo-life ()
1158 "Take a typing break and get a life."
1162 (and (get-buffer "*Life*")
1163 (kill-buffer "*Life*"))
1167 ;; wait for user to return
1169 (type-break-catch-up-event)
1170 (kill-buffer "*Life*"))
1172 (message "%s" (get 'life-extinct
'error-message
))
1176 (type-break-catch-up-event)
1177 (and (get-buffer "*Life*")
1178 (kill-buffer "*Life*")))))))
1180 ;; Boring demo, but doesn't use many cycles
1181 (defun type-break-demo-boring ()
1182 "Boring typing break demo."
1183 (let ((rmsg (if type-break-terse-messages
1185 "Press any key to resume from typing break"))
1186 (buffer-name "*Typing Break Buffer*")
1187 lines elapsed timeleft tmsg
)
1190 (switch-to-buffer (get-buffer-create buffer-name
))
1191 (buffer-disable-undo (current-buffer))
1192 (setq lines
(/ (window-body-height) 2))
1193 (unless type-break-terse-messages
(setq lines
(1- lines
)))
1194 (if type-break-demo-boring-stats
1195 (setq lines
(- lines
2)))
1196 (setq lines
(make-string lines ?\C-j
))
1197 (while (not (input-pending-p))
1199 (setq elapsed
(type-break-time-difference
1200 type-break-time-last-break
1202 (let ((good-interval (or type-break-good-rest-interval
1203 type-break-good-break-interval
)))
1206 (setq timeleft
(- good-interval elapsed
))
1209 (format (if type-break-terse-messages
1210 "Break remaining: %s"
1211 "You should rest for %s more")
1212 (type-break-format-time timeleft
)))
1214 (format (if type-break-terse-messages
1215 "Break complete (%s elapsed in total)"
1216 "Typing break has lasted %s")
1217 (type-break-format-time elapsed
)))))
1220 (format (if type-break-terse-messages
1221 "Break has lasted %s"
1222 "Typing break has lasted %s")
1223 (type-break-format-time elapsed
))))))
1225 (make-string (/ (- (window-width) (length tmsg
)) 2) ?\
)
1227 (if (> (length rmsg
) 0)
1229 (make-string (/ (- (window-width) (length rmsg
)) 2)
1232 (if type-break-demo-boring-stats
1236 (if type-break-terse-messages
1237 "Since last break: %s keystrokes\n"
1238 "Since your last break you've typed %s keystrokes\n")
1239 type-break-keystroke-count
))
1241 (make-string (/ (- (window-width) (length message
)) 2)
1243 (wpm (/ (/ (float type-break-keystroke-count
) 5)
1244 (/ (type-break-time-difference
1245 type-break-interval-start
1246 type-break-time-last-break
)
1248 (insert "\n\n" column-spaces message
)
1249 (if type-break-terse-messages
1250 (insert (format " %s%.2f wpm"
1254 (format "at an average of %.2f words per minute"
1257 (make-string (/ (- (window-width) (length message
)) 2)
1260 (goto-char (point-min))
1263 (type-break-catch-up-event)
1264 (kill-buffer buffer-name
))
1266 (and (get-buffer buffer-name
)
1267 (kill-buffer buffer-name
))))))
1270 (provide 'type-break
)
1273 (type-break-mode 1))
1275 ;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
1276 ;;; type-break.el ends here