Merge from trunk
[emacs.git] / lisp / type-break.el
blob641215be8cf0fc341d76f822608063161ed56ba6
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/>.
27 ;;; Commentary:
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
37 ;; interval tracking.
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
45 ;; argument.
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>
66 ;;; Code:
69 (defgroup type-break nil
70 "Encourage the user to take a rest from typing at suitable intervals."
71 :prefix "type-break"
72 :group 'keyboard)
74 ;;;###autoload
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
83 :type 'boolean
84 :group 'type-break
85 :require 'type-break)
87 ;;;###autoload
88 (defcustom type-break-interval (* 60 60)
89 "Number of seconds between scheduled typing breaks."
90 :type 'integer
91 :group 'type-break)
93 ;;;###autoload
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."
103 :type 'integer
104 :group 'type-break)
106 ;;;###autoload
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."
114 :type 'integer
115 :group 'type-break)
117 ;;;###autoload
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.
127 (let* ((wpm 35)
128 (avg-word-length 5)
129 (upper (* wpm avg-word-length (/ type-break-interval 60)))
130 (lower (/ upper 5)))
131 (cons lower upper))
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."
152 :type 'sexp
153 :group 'type-break)
155 (defcustom type-break-query-function 'yes-or-no-p
156 "Function to use for making query for a typing break.
157 It should take a string as an argument, the prompt.
158 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
160 To avoid being queried at all, set `type-break-query-mode' to nil."
161 :type '(radio function
162 (function-item yes-or-no-p)
163 (function-item y-or-n-p))
164 :group 'type-break)
166 (defcustom type-break-query-interval 60
167 "Number of seconds between queries to take a break, if put off.
168 The user will continue to be prompted at this interval until he or she
169 finally submits to taking a typing break."
170 :type 'integer
171 :group 'type-break)
173 (defcustom type-break-time-warning-intervals '(300 120 60 30)
174 "List of time intervals for warnings about upcoming typing break.
175 At each of the intervals (specified in seconds) away from a scheduled
176 typing break, print a warning in the echo area."
177 :type '(repeat integer)
178 :group 'type-break)
180 (defcustom type-break-keystroke-warning-intervals '(300 200 100 50)
181 "List of keystroke measurements for warnings about upcoming typing break.
182 At each of the intervals (specified in keystrokes) away from the upper
183 keystroke threshold, print a warning in the echo area.
184 If either this variable or the upper threshold is set, then no warnings
185 will occur."
186 :type '(repeat integer)
187 :group 'type-break)
189 (defcustom type-break-warning-repeat 40
190 "Number of keystrokes for which warnings should be repeated.
191 That is, for each of this many keystrokes the warning is redisplayed
192 in the echo area to make sure it's really seen."
193 :type 'integer
194 :group 'type-break)
196 (defcustom type-break-time-stamp-format "[%H:%M] "
197 "Timestamp format used to prefix messages.
198 Format specifiers are as used by `format-time-string'."
199 :type 'string
200 :group 'type-break)
202 (defcustom type-break-demo-functions
203 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi)
204 "List of functions to consider running as demos during typing breaks.
205 When a typing break begins, one of these functions is selected randomly
206 to have Emacs do something interesting.
208 Any function in this list should start a demo which ceases as soon as a
209 key is pressed."
210 :type '(repeat function)
211 :group 'type-break)
213 (defcustom type-break-demo-boring-stats nil
214 "Show word per minute and keystroke figures in the Boring demo."
215 :type 'boolean
216 :group 'type-break)
218 (defcustom type-break-terse-messages nil
219 "Use slightly terser messages."
220 :type 'boolean
221 :group 'type-break)
223 (defcustom type-break-file-name (convert-standard-filename "~/.type-break")
224 "Name of file used to save state across sessions.
225 If this is nil, no data will be saved across sessions."
226 :type 'file
227 :group 'type-break)
229 (defvar type-break-post-command-hook '(type-break-check)
230 "Hook run indirectly by `post-command-hook' for typing break functions.
231 This is not really intended to be set by the user, but it's probably
232 harmless to do so. Mainly it is used by various parts of the typing break
233 program to delay actions until after the user has completed some command.
234 It exists because `post-command-hook' itself is inaccessible while its
235 functions are being run, and some type-break--related functions want to
236 remove themselves after running.")
239 ;; Mode line frobs
241 (defvar type-break-mode-line-format
242 '(type-break-mode-line-message-mode
244 type-break-mode-line-break-message
245 type-break-mode-line-warning))
246 "*Format of messages in the mode line concerning typing breaks.")
248 (defvar type-break-mode-line-break-message
249 '(type-break-mode-line-break-message-p
250 type-break-mode-line-break-string))
252 (defvar type-break-mode-line-break-message-p nil)
253 (defvar type-break-mode-line-break-string " *** TAKE A TYPING BREAK NOW ***")
255 (defvar type-break-mode-line-warning
256 '(type-break-mode-line-break-message-p
257 ("")
258 (type-break-warning-countdown-string
259 (" *** "
260 "Break in "
261 type-break-warning-countdown-string
263 type-break-warning-countdown-string-type
264 "***"))))
266 (defvar type-break-warning-countdown-string nil
267 "If non-nil, this is a countdown for the next typing break.
269 This variable, in conjunction with `type-break-warning-countdown-string-type'
270 \(which indicates whether this value is a number of keystrokes or seconds)
271 is installed in `mode-line-format' to notify of imminent typing breaks.")
273 (defvar type-break-warning-countdown-string-type nil
274 "Indicates the unit type of `type-break-warning-countdown-string'.
275 It will be either \"seconds\" or \"keystrokes\".")
278 ;; These are internal variables. Do not set them yourself.
280 (defvar type-break-alarm-p nil)
281 (defvar type-break-keystroke-count 0)
282 (defvar type-break-time-last-break nil)
283 (defvar type-break-time-next-break nil)
284 (defvar type-break-time-last-command (current-time))
285 (defvar type-break-current-time-warning-interval nil)
286 (defvar type-break-current-keystroke-warning-interval nil)
287 (defvar type-break-time-warning-count 0)
288 (defvar type-break-keystroke-warning-count 0)
289 (defvar type-break-interval-start nil)
292 ;;;###autoload
293 (defun type-break-mode (&optional prefix)
294 "Enable or disable typing-break mode.
295 This is a minor mode, but it is global to all buffers by default.
297 When this mode is enabled, the user is encouraged to take typing breaks at
298 appropriate intervals; either after a specified amount of time or when the
299 user has exceeded a keystroke threshold. When the time arrives, the user
300 is asked to take a break. If the user refuses at that time, Emacs will ask
301 again in a short period of time. The idea is to give the user enough time
302 to find a good breaking point in his or her work, but be sufficiently
303 annoying to discourage putting typing breaks off indefinitely.
305 A negative prefix argument disables this mode.
306 No argument or any non-negative argument enables it.
308 The user may enable or disable this mode by setting the variable of the
309 same name, though setting it in that way doesn't reschedule a break or
310 reset the keystroke counter.
312 If the mode was previously disabled and is enabled as a consequence of
313 calling this function, it schedules a break with `type-break-schedule' to
314 make sure one occurs (the user can call that command to reschedule the
315 break at any time). It also initializes the keystroke counter.
317 The variable `type-break-interval' specifies the number of seconds to
318 schedule between regular typing breaks. This variable doesn't directly
319 affect the time schedule; it simply provides a default for the
320 `type-break-schedule' command.
322 If set, the variable `type-break-good-rest-interval' specifies the minimum
323 amount of time which is considered a reasonable typing break. Whenever
324 that time has elapsed, typing breaks are automatically rescheduled for
325 later even if Emacs didn't prompt you to take one first. Also, if a break
326 is ended before this much time has elapsed, the user will be asked whether
327 or not to continue. A nil value for this variable prevents automatic
328 break rescheduling, making `type-break-interval' an upper bound on the time
329 between breaks. In this case breaks will be prompted for as usual before
330 the upper bound if the keystroke threshold is reached.
332 If `type-break-good-rest-interval' is nil and
333 `type-break-good-break-interval' is set, then confirmation is required to
334 interrupt a break before `type-break-good-break-interval' seconds
335 have passed. This provides for an upper bound on the time between breaks
336 together with confirmation of interruptions to these breaks.
338 The variable `type-break-keystroke-threshold' is used to determine the
339 thresholds at which typing breaks should be considered. You can use
340 the command `type-break-guesstimate-keystroke-threshold' to try to
341 approximate good values for this.
343 There are several variables that affect how or when warning messages about
344 imminent typing breaks are displayed. They include:
346 `type-break-mode-line-message-mode'
347 `type-break-time-warning-intervals'
348 `type-break-keystroke-warning-intervals'
349 `type-break-warning-repeat'
350 `type-break-warning-countdown-string'
351 `type-break-warning-countdown-string-type'
353 There are several variables that affect if, how, and when queries to begin
354 a typing break occur. They include:
356 `type-break-query-mode'
357 `type-break-query-function'
358 `type-break-query-interval'
360 The command `type-break-statistics' prints interesting things.
362 Finally, a file (named `type-break-file-name') is used to store information
363 across Emacs sessions. This provides recovery of the break status between
364 sessions and after a crash. Manual changes to the file may result in
365 problems."
366 (interactive "P")
367 (type-break-check-post-command-hook)
369 (let ((already-enabled type-break-mode))
370 (setq type-break-mode (>= (prefix-numeric-value prefix) 0))
372 (cond
373 ((and already-enabled type-break-mode)
374 (and (called-interactively-p 'interactive)
375 (message "Type Break mode is already enabled")))
376 (type-break-mode
377 (when type-break-file-name
378 (with-current-buffer (find-file-noselect type-break-file-name 'nowarn)
379 (setq buffer-save-without-query t)))
381 (or global-mode-string
382 (setq global-mode-string '("")))
383 (or (assq 'type-break-mode-line-message-mode
384 minor-mode-alist)
385 (setq minor-mode-alist
386 (cons type-break-mode-line-format
387 minor-mode-alist)))
388 (type-break-keystroke-reset)
389 (type-break-mode-line-countdown-or-break nil)
391 (setq type-break-time-last-break
392 (or (type-break-get-previous-time)
393 (current-time)))
395 ;; schedule according to break time from session file
396 (type-break-schedule
397 (let (diff)
398 (if (and type-break-time-last-break
399 (< (setq diff (type-break-time-difference
400 type-break-time-last-break
401 (current-time)))
402 type-break-interval))
403 ;; use the file's value
404 (progn
405 (setq type-break-keystroke-count
406 (type-break-get-previous-count))
407 ;; file the time, in case it was read from the auto-save file
408 (type-break-file-time type-break-interval-start)
409 (setq type-break-interval-start type-break-time-last-break)
410 (- type-break-interval diff))
411 ;; schedule from now
412 (setq type-break-interval-start (current-time))
413 (type-break-file-time type-break-interval-start)
414 type-break-interval))
415 type-break-interval-start
416 type-break-interval)
418 (and (called-interactively-p 'interactive)
419 (message "Type Break mode is enabled and set")))
421 (type-break-keystroke-reset)
422 (type-break-mode-line-countdown-or-break nil)
423 (type-break-cancel-schedule)
424 (do-auto-save)
425 (when type-break-file-name
426 (with-current-buffer (find-file-noselect type-break-file-name
427 'nowarn)
428 (set-buffer-modified-p nil)
429 (unlock-buffer)
430 (kill-this-buffer)))
431 (and (called-interactively-p 'interactive)
432 (message "Type Break mode is disabled")))))
433 type-break-mode)
435 (define-minor-mode type-break-mode-line-message-mode
436 "Enable or disable warnings in the mode line about typing breaks.
438 A negative PREFIX argument disables this mode.
439 No argument or any non-negative argument enables it.
441 The user may also enable or disable this mode simply by setting the
442 variable of the same name.
444 Variables controlling the display of messages in the mode line include:
446 `mode-line-format'
447 `global-mode-string'
448 `type-break-mode-line-break-message'
449 `type-break-mode-line-warning'"
450 :global t)
452 (define-minor-mode type-break-query-mode
453 "Enable or disable warnings in the mode line about typing breaks.
455 When enabled, the user is periodically queried about whether to take a
456 typing break at that moment. The function which does this query is
457 specified by the variable `type-break-query-function'.
459 A negative PREFIX argument disables this mode.
460 No argument or any non-negative argument enables it.
462 The user may also enable or disable this mode simply by setting the
463 variable of the same name."
464 :global t)
467 ;;; session file functions
469 (defvar type-break-auto-save-file-name
470 (let ((buffer-file-name type-break-file-name))
471 (make-auto-save-file-name))
472 "Auto-save name of `type-break-file-name'.")
474 (defun type-break-file-time (&optional time)
475 "File break time in `type-break-file-name', unless the file is locked."
476 (if (and type-break-file-name
477 (not (stringp (file-locked-p type-break-file-name))))
478 (with-current-buffer (find-file-noselect type-break-file-name
479 'nowarn)
480 (let ((inhibit-read-only t))
481 (erase-buffer)
482 (insert (format "%s\n\n" (or time type-break-interval-start)))
483 ;; file saving is left to auto-save
484 ))))
486 (defun type-break-file-keystroke-count ()
487 "File keystroke count in `type-break-file-name', unless the file is locked."
488 (if (and type-break-file-name
489 (not (stringp (file-locked-p type-break-file-name))))
490 ;; Prevent deactivation of the mark in some other buffer.
491 (let (deactivate-mark)
492 (with-current-buffer (find-file-noselect type-break-file-name
493 'nowarn)
494 (save-excursion
495 (let ((inhibit-read-only t))
496 (goto-char (point-min))
497 (forward-line)
498 (delete-region (point) (save-excursion (end-of-line) (point)))
499 (insert (format "%s" type-break-keystroke-count))
500 ;; file saving is left to auto-save
501 ))))))
503 (defun timep (time)
504 "If TIME is in the format returned by `current-time' then
505 return TIME, else return nil."
506 (and (listp time)
507 (eq (length time) 3)
508 (integerp (car time))
509 (integerp (nth 1 time))
510 (integerp (nth 2 time))
511 time))
513 (defun type-break-choose-file ()
514 "Return file to read from."
515 (cond
516 ((not type-break-file-name)
517 nil)
518 ((and (file-exists-p type-break-auto-save-file-name)
519 (file-readable-p type-break-auto-save-file-name))
520 type-break-auto-save-file-name)
521 ((and (file-exists-p type-break-file-name)
522 (file-readable-p type-break-file-name))
523 type-break-file-name)
524 (t nil)))
526 (defun type-break-get-previous-time ()
527 "Get previous break time from `type-break-file-name'.
528 Returns nil if the file is missing or if the time breaks with the
529 `current-time' format."
530 (let ((file (type-break-choose-file)))
531 (if file
532 (timep ;; returns expected format, else nil
533 (with-current-buffer (find-file-noselect file 'nowarn)
534 (condition-case nil
535 (save-excursion
536 (goto-char (point-min))
537 (read (current-buffer)))
538 (end-of-file
539 (error "End of file in `%s'" file))))))))
541 (defun type-break-get-previous-count ()
542 "Get previous keystroke count from `type-break-file-name'.
543 Return 0 if the file is missing or if the form read is not an
544 integer."
545 (let ((file (type-break-choose-file)))
546 (if (and file
547 (integerp
548 (setq file
549 (with-current-buffer
550 (find-file-noselect file 'nowarn)
551 (condition-case nil
552 (save-excursion
553 (goto-char (point-min))
554 (forward-line 1)
555 (read (current-buffer)))
556 (end-of-file
557 (error "End of file in `%s'" file)))))))
558 file
559 0)))
562 ;;;###autoload
563 (defun type-break ()
564 "Take a typing break.
566 During the break, a demo selected from the functions listed in
567 `type-break-demo-functions' is run.
569 After the typing break is finished, the next break is scheduled
570 as per the function `type-break-schedule'."
571 (interactive)
572 (do-auto-save)
573 (type-break-cancel-schedule)
574 ;; remove any query scheduled during interactive invocation
575 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
576 (let ((continue t)
577 (start-time (current-time)))
578 (setq type-break-time-last-break start-time)
579 (while continue
580 (save-window-excursion
581 ;; Eat the screen.
582 (and (eq (selected-window) (minibuffer-window))
583 (other-window 1))
584 (delete-other-windows)
585 (scroll-right (window-width))
586 (unless type-break-terse-messages
587 (message "Press any key to resume from typing break."))
589 (random t)
590 (let* ((len (length type-break-demo-functions))
591 (idx (random len))
592 (fn (nth idx type-break-demo-functions)))
593 (condition-case ()
594 (funcall fn)
595 (error nil))))
597 (let ((good-interval (or type-break-good-rest-interval
598 type-break-good-break-interval)))
599 (cond
600 (good-interval
601 (let ((break-secs (type-break-time-difference
602 start-time (current-time))))
603 (cond
604 ((>= break-secs good-interval)
605 (setq continue nil))
606 ;; 60 seconds may be too much leeway if the break is only 3
607 ;; minutes to begin with. You can just say "no" to the query
608 ;; below if you're in that much of a hurry.
609 ;;((> 60 (abs (- break-secs good-interval)))
610 ;; (setq continue nil))
611 ((funcall
612 type-break-query-function
613 (format
614 (if type-break-terse-messages
615 "%s%s remaining. Continue break? "
616 "%sYou really ought to rest %s more. Continue break? ")
617 (type-break-time-stamp)
618 (type-break-format-time (- good-interval
619 break-secs)))))
621 (setq continue nil)))))
622 (t (setq continue nil))))))
624 (type-break-keystroke-reset)
625 (type-break-file-time)
626 (type-break-mode-line-countdown-or-break nil)
627 (type-break-schedule))
630 (defun type-break-schedule (&optional time start interval)
631 "Schedule a typing break for TIME seconds from now.
632 If time is not specified it defaults to `type-break-interval'.
633 START and INTERVAL are used when recovering a break.
634 START is the start of the break (defaults to now).
635 INTERVAL is the full length of an interval (defaults to TIME)."
636 (interactive (list (and current-prefix-arg
637 (prefix-numeric-value current-prefix-arg))))
638 (or time (setq time type-break-interval))
639 (type-break-check-post-command-hook)
640 (type-break-cancel-schedule)
641 (type-break-time-warning-schedule time 'reset)
642 (type-break-run-at-time (max 1 time) nil 'type-break-alarm)
643 (setq type-break-time-next-break
644 (type-break-time-sum (or start (current-time))
645 (or interval time))))
647 (defun type-break-cancel-schedule ()
648 (type-break-cancel-time-warning-schedule)
649 (type-break-cancel-function-timers 'type-break-alarm)
650 (setq type-break-alarm-p nil)
651 (setq type-break-time-next-break nil))
653 (defun type-break-time-warning-schedule (&optional time resetp)
654 (let ((type-break-current-time-warning-interval nil))
655 (type-break-cancel-time-warning-schedule))
656 (add-hook 'type-break-post-command-hook 'type-break-time-warning 'append)
657 (cond
658 (type-break-time-warning-intervals
659 (and resetp
660 (setq type-break-current-time-warning-interval
661 type-break-time-warning-intervals))
663 (or time
664 (setq time (type-break-time-difference (current-time)
665 type-break-time-next-break)))
667 (while (and type-break-current-time-warning-interval
668 (> (car type-break-current-time-warning-interval) time))
669 (setq type-break-current-time-warning-interval
670 (cdr type-break-current-time-warning-interval)))
672 (cond
673 (type-break-current-time-warning-interval
674 (setq time (- time (car type-break-current-time-warning-interval)))
675 (setq type-break-current-time-warning-interval
676 (cdr type-break-current-time-warning-interval))
678 ;(let (type-break-current-time-warning-interval)
679 ; (type-break-cancel-time-warning-schedule))
680 (type-break-run-at-time (max 1 time) nil 'type-break-time-warning-alarm)
682 (cond
683 (resetp
684 (setq type-break-warning-countdown-string nil))
686 (setq type-break-warning-countdown-string (number-to-string time))
687 (setq type-break-warning-countdown-string-type "seconds"))))))))
689 (defun type-break-cancel-time-warning-schedule ()
690 (type-break-cancel-function-timers 'type-break-time-warning-alarm)
691 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
692 (setq type-break-current-time-warning-interval
693 type-break-time-warning-intervals)
694 (setq type-break-time-warning-count 0) ; avoid warnings after break
695 (setq type-break-warning-countdown-string nil))
697 (defun type-break-alarm ()
698 (type-break-check-post-command-hook)
699 (setq type-break-alarm-p t)
700 (type-break-mode-line-countdown-or-break 'break))
702 (defun type-break-time-warning-alarm ()
703 (type-break-check-post-command-hook)
704 (type-break-time-warning-schedule)
705 (setq type-break-time-warning-count type-break-warning-repeat)
706 (type-break-time-warning)
707 (type-break-mode-line-countdown-or-break 'countdown))
710 (defun type-break-run-tb-post-command-hook ()
711 (and type-break-mode
712 (run-hooks 'type-break-post-command-hook)))
714 (defun type-break-check ()
715 "Ask to take a typing break if appropriate.
716 This may be the case either because the scheduled time has come \(and the
717 minimum keystroke threshold has been reached\) or because the maximum
718 keystroke threshold has been exceeded."
719 (type-break-file-keystroke-count)
720 (let* ((min-threshold (car type-break-keystroke-threshold))
721 (max-threshold (cdr type-break-keystroke-threshold)))
722 (and type-break-good-rest-interval
723 (progn
724 (and (> (type-break-time-difference
725 type-break-time-last-command (current-time))
726 type-break-good-rest-interval)
727 (progn
728 (type-break-keystroke-reset)
729 (type-break-mode-line-countdown-or-break nil)
730 (setq type-break-time-last-break (current-time))
731 (type-break-schedule)))
732 (setq type-break-time-last-command (current-time))))
734 (and type-break-keystroke-threshold
735 (let ((keys (this-command-keys)))
736 (cond
737 ;; Ignore mouse motion
738 ((and (vectorp keys)
739 (consp (aref keys 0))
740 (memq (car (aref keys 0)) '(mouse-movement))))
742 (setq type-break-keystroke-count
743 (+ type-break-keystroke-count (length keys)))))))
745 (cond
746 (type-break-alarm-p
747 (cond
748 ((input-pending-p))
749 ((eq (selected-window) (minibuffer-window)))
750 ((and min-threshold
751 (< type-break-keystroke-count min-threshold))
752 (type-break-schedule))
754 ;; If keystroke count is within min-threshold of
755 ;; max-threshold, lower it to reduce the likelihood of an
756 ;; immediate subsequent query.
757 (and max-threshold
758 min-threshold
759 (< (- max-threshold type-break-keystroke-count) min-threshold)
760 (progn
761 (type-break-keystroke-reset)
762 (setq type-break-keystroke-count min-threshold)))
763 (type-break-query))))
764 ((and type-break-keystroke-warning-intervals
765 max-threshold
766 (= type-break-keystroke-warning-count 0)
767 (type-break-check-keystroke-warning)))
768 ((and max-threshold
769 (> type-break-keystroke-count max-threshold)
770 (not (input-pending-p))
771 (not (eq (selected-window) (minibuffer-window))))
772 (type-break-keystroke-reset)
773 (setq type-break-keystroke-count (or min-threshold 0))
774 (type-break-query)))))
776 ;; This should return t if warnings were enabled, nil otherwise.
777 (defun type-break-check-keystroke-warning ()
778 ;; This is safe because the caller should have checked that the cdr was
779 ;; non-nil already.
780 (let ((left (- (cdr type-break-keystroke-threshold)
781 type-break-keystroke-count)))
782 (cond
783 ((null (car type-break-current-keystroke-warning-interval))
784 nil)
785 ((> left (car type-break-current-keystroke-warning-interval))
786 nil)
788 (while (and (car type-break-current-keystroke-warning-interval)
789 (< left (car type-break-current-keystroke-warning-interval)))
790 (setq type-break-current-keystroke-warning-interval
791 (cdr type-break-current-keystroke-warning-interval)))
792 (setq type-break-keystroke-warning-count type-break-warning-repeat)
793 (add-hook 'type-break-post-command-hook 'type-break-keystroke-warning)
794 (setq type-break-warning-countdown-string (number-to-string left))
795 (setq type-break-warning-countdown-string-type "keystrokes")
796 (type-break-mode-line-countdown-or-break 'countdown)
797 t))))
799 ;; Arrange for a break query to be made, when the user stops typing furiously.
800 (defun type-break-query ()
801 (add-hook 'type-break-post-command-hook 'type-break-do-query))
803 (defun type-break-do-query ()
804 (cond
805 ((not type-break-query-mode)
806 (type-break-noninteractive-query)
807 (type-break-schedule type-break-query-interval)
808 (remove-hook 'type-break-post-command-hook 'type-break-do-query))
809 ((sit-for 2)
810 (condition-case ()
811 (cond
812 ((let ((type-break-mode nil)
813 ;; yes-or-no-p sets this-command to exit-minibuffer,
814 ;; which hoses undo or yank-pop (if you happened to be
815 ;; yanking just when the query occurred).
816 (this-command this-command))
817 ;; Cancel schedule to prevent possibility of a second query
818 ;; from taking place before this one has even returned.
819 ;; The condition-case wrapper will reschedule on quit.
820 (type-break-cancel-schedule)
821 ;; Also prevent a second query when the break is interrupted.
822 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
823 (funcall type-break-query-function
824 (format "%s%s"
825 (type-break-time-stamp)
826 (if type-break-terse-messages
827 "Break now? "
828 "Take a break from typing now? "))))
829 (type-break))
831 (type-break-schedule type-break-query-interval)))
832 (quit
833 (type-break-schedule type-break-query-interval))))))
835 (defun type-break-noninteractive-query (&optional ignored-args)
836 "Null query function which doesn't interrupt user and assumes `no'.
837 It prints a reminder in the echo area to take a break, but doesn't enforce
838 this or ask the user to start one right now."
839 (cond
840 (type-break-mode-line-message-mode)
842 (beep t)
843 (message "%sYou should take a typing break now. Do `M-x type-break'."
844 (type-break-time-stamp))
845 (sit-for 1)
846 (beep t)
847 ;; return nil so query caller knows to reset reminder, as if user
848 ;; said "no" in response to yes-or-no-p.
849 nil)))
851 (defun type-break-time-warning ()
852 (cond
853 ((and (car type-break-keystroke-threshold)
854 (< type-break-keystroke-count (car type-break-keystroke-threshold))))
855 ((> type-break-time-warning-count 0)
856 (let ((timeleft (type-break-time-difference (current-time)
857 type-break-time-next-break)))
858 (setq type-break-warning-countdown-string (number-to-string timeleft))
859 (cond
860 ((eq (selected-window) (minibuffer-window)))
861 ;; Do nothing if the command was just a prefix arg, since that will
862 ;; immediately be followed by some other interactive command.
863 ;; Otherwise, it is particularly annoying for the sit-for below to
864 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
865 ((memq this-command '(digit-argument universal-argument)))
866 ((not type-break-mode-line-message-mode)
867 ;; Pause for a moment so any previous message can be seen.
868 (sit-for 2)
869 (message "%sWarning: typing break due in %s."
870 (type-break-time-stamp)
871 (type-break-format-time timeleft))
872 (setq type-break-time-warning-count
873 (1- type-break-time-warning-count))))))
875 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
876 (setq type-break-warning-countdown-string nil))))
878 (defun type-break-keystroke-warning ()
879 (cond
880 ((> type-break-keystroke-warning-count 0)
881 (setq type-break-warning-countdown-string
882 (number-to-string (- (cdr type-break-keystroke-threshold)
883 type-break-keystroke-count)))
884 (cond
885 ((eq (selected-window) (minibuffer-window)))
886 ;; Do nothing if the command was just a prefix arg, since that will
887 ;; immediately be followed by some other interactive command.
888 ;; Otherwise, it is particularly annoying for the sit-for below to
889 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
890 ((memq this-command '(digit-argument universal-argument)))
891 ((not type-break-mode-line-message-mode)
892 (sit-for 2)
893 (message "%sWarning: typing break due in %s keystrokes."
894 (type-break-time-stamp)
895 (- (cdr type-break-keystroke-threshold)
896 type-break-keystroke-count))
897 (setq type-break-keystroke-warning-count
898 (1- type-break-keystroke-warning-count)))))
900 (remove-hook 'type-break-post-command-hook
901 'type-break-keystroke-warning)
902 (setq type-break-warning-countdown-string nil))))
904 (defun type-break-mode-line-countdown-or-break (&optional type)
905 (cond
906 ((not type-break-mode-line-message-mode))
907 ((eq type 'countdown)
908 ;(setq type-break-mode-line-break-message-p nil)
909 (add-hook 'type-break-post-command-hook
910 'type-break-force-mode-line-update 'append))
911 ((eq type 'break)
912 ;; Alternate
913 (setq type-break-mode-line-break-message-p
914 (not type-break-mode-line-break-message-p))
915 (remove-hook 'type-break-post-command-hook
916 'type-break-force-mode-line-update))
918 (setq type-break-mode-line-break-message-p nil)
919 (setq type-break-warning-countdown-string nil)
920 (remove-hook 'type-break-post-command-hook
921 'type-break-force-mode-line-update)))
922 (type-break-force-mode-line-update))
925 ;;;###autoload
926 (defun type-break-statistics ()
927 "Print statistics about typing breaks in a temporary buffer.
928 This includes the last time a typing break was taken, when the next one is
929 scheduled, the keystroke thresholds and the current keystroke count, etc."
930 (interactive)
931 (with-output-to-temp-buffer "*Typing Break Statistics*"
932 (princ (format "Typing break statistics\n-----------------------\n
933 Typing break mode is currently %s.
934 Interactive query for breaks is %s.
935 Warnings of imminent typing breaks in mode line is %s.
937 Last typing break ended : %s
938 Next scheduled typing break : %s\n
939 Minimum keystroke threshold : %s
940 Maximum keystroke threshold : %s
941 Current keystroke count : %s"
942 (if type-break-mode "enabled" "disabled")
943 (if type-break-query-mode "enabled" "disabled")
944 (if type-break-mode-line-message-mode "enabled" "disabled")
945 (if type-break-time-last-break
946 (current-time-string type-break-time-last-break)
947 "never")
948 (if (and type-break-mode type-break-time-next-break)
949 (format "%s\t(%s from now)"
950 (current-time-string type-break-time-next-break)
951 (type-break-format-time
952 (type-break-time-difference
953 (current-time)
954 type-break-time-next-break)))
955 "none scheduled")
956 (or (car type-break-keystroke-threshold) "none")
957 (or (cdr type-break-keystroke-threshold) "none")
958 type-break-keystroke-count))))
960 ;;;###autoload
961 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac)
962 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
964 If called interactively, the user is prompted for their guess as to how
965 many words per minute they usually type. This value should not be your
966 maximum WPM, but your average. Of course, this is harder to gauge since it
967 can vary considerably depending on what you are doing. For example, one
968 tends to type less when debugging a program as opposed to writing
969 documentation. (Perhaps a separate program should be written to estimate
970 average typing speed.)
972 From that, this command sets the values in `type-break-keystroke-threshold'
973 based on a fairly simple algorithm involving assumptions about the average
974 length of words (5). For the minimum threshold, it uses about a fifth of
975 the computed maximum threshold.
977 When called from Lisp programs, the optional args WORDLEN and FRAC can be
978 used to override the default assumption about average word length and the
979 fraction of the maximum threshold to which to set the minimum threshold.
980 FRAC should be the inverse of the fractional value; for example, a value of
981 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
982 (interactive "NOn average, how many words per minute do you type? ")
983 (let* ((upper (* wpm (or wordlen 5) (/ type-break-interval 60)))
984 (lower (/ upper (or frac 5))))
985 (or type-break-keystroke-threshold
986 (setq type-break-keystroke-threshold (cons nil nil)))
987 (setcar type-break-keystroke-threshold lower)
988 (setcdr type-break-keystroke-threshold upper)
989 (if (called-interactively-p 'interactive)
990 (message "min threshold: %d\tmax threshold: %d" lower upper))
991 type-break-keystroke-threshold))
994 ;;; misc functions
996 ;; Compute the difference, in seconds, between a and b, two structures
997 ;; similar to those returned by `current-time'.
998 ;; Use addition rather than logand since that is more robust; the low 16
999 ;; bits of the seconds might have been incremented, making it more than 16
1000 ;; bits wide.
1001 (defun type-break-time-difference (a b)
1002 (+ (lsh (- (car b) (car a)) 16)
1003 (- (car (cdr b)) (car (cdr a)))))
1005 ;; Return (in a new list the same in structure to that returned by
1006 ;; `current-time') the sum of the arguments. Each argument may be a time
1007 ;; list or a single integer, a number of seconds.
1008 ;; This function keeps the high and low 16 bits of the seconds properly
1009 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
1010 ;; the result is passed to `current-time-string' it will toss some of the
1011 ;; "low" bits and format the time incorrectly.
1012 (defun type-break-time-sum (&rest tmlist)
1013 (let ((high 0)
1014 (low 0)
1015 (micro 0)
1016 tem)
1017 (while tmlist
1018 (setq tem (car tmlist))
1019 (setq tmlist (cdr tmlist))
1020 (cond
1021 ((numberp tem)
1022 (setq low (+ low tem)))
1024 (setq high (+ high (or (car tem) 0)))
1025 (setq low (+ low (or (car (cdr tem)) 0)))
1026 (setq micro (+ micro (or (car (cdr (cdr tem))) 0))))))
1028 (and (>= micro 1000000)
1029 (progn
1030 (setq tem (/ micro 1000000))
1031 (setq low (+ low tem))
1032 (setq micro (- micro (* tem 1000000)))))
1034 (setq tem (lsh low -16))
1035 (and (> tem 0)
1036 (progn
1037 (setq low (logand low 65535))
1038 (setq high (+ high tem))))
1040 (list high low micro)))
1042 (defun type-break-time-stamp (&optional when)
1043 (if (fboundp 'format-time-string)
1044 (format-time-string type-break-time-stamp-format when)
1045 ;; Emacs 19.28 and prior do not have format-time-string.
1046 ;; In that case, result is not customizable. Upgrade today!
1047 (format "[%s] " (substring (current-time-string when) 11 16))))
1049 (defun type-break-format-time (secs)
1050 (let ((mins (/ secs 60)))
1051 (cond
1052 ((= mins 1) (format "%d minute" mins))
1053 ((> mins 0) (format "%d minutes" mins))
1054 ((= secs 1) (format "%d second" secs))
1055 (t (format "%d seconds" secs)))))
1057 (defun type-break-keystroke-reset ()
1058 (setq type-break-interval-start (current-time)) ; not a keystroke
1059 (setq type-break-keystroke-count 0)
1060 (setq type-break-keystroke-warning-count 0)
1061 (setq type-break-current-keystroke-warning-interval
1062 type-break-keystroke-warning-intervals)
1063 (remove-hook 'type-break-post-command-hook 'type-break-keystroke-warning))
1065 (defun type-break-force-mode-line-update (&optional all)
1066 "Force the mode-line of the current buffer to be redisplayed.
1067 With optional non-nil ALL, force redisplay of all mode-lines."
1068 (and all (with-current-buffer (other-buffer)))
1069 (set-buffer-modified-p (buffer-modified-p)))
1071 ;; If an exception occurs in Emacs while running the post command hook, the
1072 ;; value of that hook is clobbered. This is because the value of the
1073 ;; variable is temporarily set to nil while it's running to prevent
1074 ;; recursive application, but it also means an exception aborts the routine
1075 ;; of restoring it. This function is called from the timers to restore it,
1076 ;; just in case.
1077 (defun type-break-check-post-command-hook ()
1078 (add-hook 'post-command-hook 'type-break-run-tb-post-command-hook 'append))
1081 ;;; Timer wrapper functions
1083 ;; These shield type-break from variations in the interval timer packages
1084 ;; for different versions of Emacs.
1086 (defun type-break-run-at-time (time repeat function)
1087 (condition-case nil (or (require 'timer) (require 'itimer)) (error nil))
1088 (run-at-time time repeat function))
1090 (defvar timer-dont-exit)
1091 (defun type-break-cancel-function-timers (function)
1092 (let ((timer-dont-exit t))
1093 (cancel-function-timers function)))
1096 ;;; Demo wrappers
1098 (defun type-break-catch-up-event ()
1099 ;; If the last input event is a down-event, read and discard the
1100 ;; corresponding up-event too, to avoid triggering another prompt.
1101 (and (eventp last-input-event)
1102 (memq 'down (event-modifiers last-input-event))
1103 (read-event)))
1105 ;; This is a wrapper around hanoi that calls it with an arg large enough to
1106 ;; make the largest discs possible that will fit in the window.
1107 ;; Also, clean up the *Hanoi* buffer after we're done.
1108 (defun type-break-demo-hanoi ()
1109 "Take a hanoiing typing break."
1110 (and (get-buffer "*Hanoi*")
1111 (kill-buffer "*Hanoi*"))
1112 (condition-case ()
1113 (progn
1114 (hanoi (/ (window-width) 8))
1115 ;; Wait for user to come back.
1116 (read-event)
1117 (type-break-catch-up-event)
1118 (kill-buffer "*Hanoi*"))
1119 (quit
1120 (read-event)
1121 (type-break-catch-up-event)
1122 (and (get-buffer "*Hanoi*")
1123 (kill-buffer "*Hanoi*")))))
1125 ;; This is a wrapper around life that calls it with a `sleep' arg to make
1126 ;; it run a little more leisurely.
1127 ;; Also, clean up the *Life* buffer after we're done.
1128 (defun type-break-demo-life ()
1129 "Take a typing break and get a life."
1130 (let ((continue t))
1131 (while continue
1132 (setq continue nil)
1133 (and (get-buffer "*Life*")
1134 (kill-buffer "*Life*"))
1135 (condition-case ()
1136 (progn
1137 (life 3)
1138 ;; wait for user to return
1139 (read-event)
1140 (type-break-catch-up-event)
1141 (kill-buffer "*Life*"))
1142 (life-extinct
1143 (message "%s" (get 'life-extinct 'error-message))
1144 ;; restart demo
1145 (setq continue t))
1146 (quit
1147 (type-break-catch-up-event)
1148 (and (get-buffer "*Life*")
1149 (kill-buffer "*Life*")))))))
1151 ;; Boring demo, but doesn't use many cycles
1152 (defun type-break-demo-boring ()
1153 "Boring typing break demo."
1154 (let ((rmsg (if type-break-terse-messages
1156 "Press any key to resume from typing break"))
1157 (buffer-name "*Typing Break Buffer*")
1158 lines elapsed timeleft tmsg)
1159 (condition-case ()
1160 (progn
1161 (switch-to-buffer (get-buffer-create buffer-name))
1162 (buffer-disable-undo (current-buffer))
1163 (setq lines (/ (window-body-height) 2))
1164 (unless type-break-terse-messages (setq lines (1- lines)))
1165 (if type-break-demo-boring-stats
1166 (setq lines (- lines 2)))
1167 (setq lines (make-string lines ?\C-j))
1168 (while (not (input-pending-p))
1169 (erase-buffer)
1170 (setq elapsed (type-break-time-difference
1171 type-break-time-last-break
1172 (current-time)))
1173 (let ((good-interval (or type-break-good-rest-interval
1174 type-break-good-break-interval)))
1175 (cond
1176 (good-interval
1177 (setq timeleft (- good-interval elapsed))
1178 (if (> timeleft 0)
1179 (setq tmsg
1180 (format (if type-break-terse-messages
1181 "Break remaining: %s"
1182 "You should rest for %s more")
1183 (type-break-format-time timeleft)))
1184 (setq tmsg
1185 (format (if type-break-terse-messages
1186 "Break complete (%s elapsed in total)"
1187 "Typing break has lasted %s")
1188 (type-break-format-time elapsed)))))
1190 (setq tmsg
1191 (format (if type-break-terse-messages
1192 "Break has lasted %s"
1193 "Typing break has lasted %s")
1194 (type-break-format-time elapsed))))))
1195 (insert lines
1196 (make-string (/ (- (window-width) (length tmsg)) 2) ?\ )
1197 tmsg)
1198 (if (> (length rmsg) 0)
1199 (insert "\n"
1200 (make-string (/ (- (window-width) (length rmsg)) 2)
1201 ?\ )
1202 rmsg))
1203 (if type-break-demo-boring-stats
1204 (let*
1205 ((message
1206 (format
1207 (if type-break-terse-messages
1208 "Since last break: %s keystrokes\n"
1209 "Since your last break you've typed %s keystrokes\n")
1210 type-break-keystroke-count))
1211 (column-spaces
1212 (make-string (/ (- (window-width) (length message)) 2)
1213 ?\ ))
1214 (wpm (/ (/ (float type-break-keystroke-count) 5)
1215 (/ (type-break-time-difference
1216 type-break-interval-start
1217 type-break-time-last-break)
1218 60.0))))
1219 (insert "\n\n" column-spaces message)
1220 (if type-break-terse-messages
1221 (insert (format " %s%.2f wpm"
1222 column-spaces
1223 wpm))
1224 (setq message
1225 (format "at an average of %.2f words per minute"
1226 wpm))
1227 (insert
1228 (make-string (/ (- (window-width) (length message)) 2)
1229 ?\ )
1230 message))))
1231 (goto-char (point-min))
1232 (sit-for 60))
1233 (read-event)
1234 (type-break-catch-up-event)
1235 (kill-buffer buffer-name))
1236 (quit
1237 (and (get-buffer buffer-name)
1238 (kill-buffer buffer-name))))))
1241 (provide 'type-break)
1243 (if type-break-mode
1244 (type-break-mode 1))
1246 ;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
1247 ;;; type-break.el ends here