etc/tutorials/TUTORIAL.es: Updated; synchronize with TUTORIAL.
[emacs.git] / lisp / type-break.el
blobee64fde1c67ab0c7dc43962b57f009a5e211dd4d
1 ;;; type-break.el --- encourage rests from typing at appropriate intervals
3 ;; Copyright (C) 1994-1995, 1997, 2000-2012 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman
6 ;; Maintainer: Noah Friedman <friedman@splode.com>
7 ;; Keywords: extensions, timers
8 ;; Created: 1994-07-13
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; The docstring for the function `type-break-mode' summarizes most of the
28 ;; details of the interface.
30 ;; This package relies on the assumption that you live entirely in Emacs,
31 ;; as the author does. If that's not the case for you (e.g. you often
32 ;; suspend Emacs or work in other windows) then this won't help very much;
33 ;; it will depend on just how often you switch back to Emacs. At the very
34 ;; least, you will want to turn off the keystroke thresholds and rest
35 ;; interval tracking.
37 ;; If you prefer not to be queried about taking breaks, but instead just
38 ;; want to be reminded, do the following:
40 ;; (setq type-break-query-mode nil)
42 ;; Or call the command `type-break-query-mode' with a negative prefix
43 ;; argument.
45 ;; If you find echo area messages annoying and would prefer to see messages
46 ;; in the mode line instead, do M-x type-break-mode-line-message-mode
47 ;; or set the variable of the same name to `t'.
49 ;; This program can truly cons up a storm because of all the calls to
50 ;; `current-time' (which always returns fresh conses). I'm dismayed by
51 ;; this, but I think the health of my hands is far more important than a
52 ;; few pages of virtual memory.
54 ;; This program has no hope of working in Emacs 18.
56 ;; This package was inspired by Roland McGrath's hanoi-break.el.
57 ;; Several people contributed feedback and ideas, including
58 ;; Roland McGrath <roland@gnu.org>
59 ;; Kleanthes Koniaris <kgk@koniaris.com>
60 ;; Mark Ashton <mpashton@gnu.org>
61 ;; Matt Wilding <wilding@cli.com>
62 ;; Robert S. Boyer <boyer@cs.utexas.edu>
64 ;;; Code:
67 (defgroup type-break nil
68 "Encourage the user to take a rest from typing at suitable intervals."
69 :prefix "type-break"
70 :group 'keyboard)
72 ;;;###autoload
73 (defcustom type-break-mode nil
74 "Toggle typing break mode.
75 See the docstring for the `type-break-mode' command for more information.
76 Setting this variable directly does not take effect;
77 use either \\[customize] or the function `type-break-mode'."
78 :set (lambda (_symbol value)
79 (type-break-mode (if value 1 -1)))
80 :initialize 'custom-initialize-default
81 :type 'boolean
82 :group 'type-break
83 :require 'type-break)
85 ;;;###autoload
86 (defcustom type-break-interval (* 60 60)
87 "Number of seconds between scheduled typing breaks."
88 :type 'integer
89 :group 'type-break)
91 ;;;###autoload
92 (defcustom type-break-good-rest-interval (/ type-break-interval 6)
93 "Number of seconds of idle time considered to be an adequate typing rest.
95 When this variable is non-nil, Emacs checks the idle time between
96 keystrokes. If this idle time is long enough to be considered a \"good\"
97 rest from typing, then the next typing break is simply rescheduled for later.
99 If a break is interrupted before this much time elapses, the user will be
100 asked whether or not really to interrupt the break."
101 :type 'integer
102 :group 'type-break)
104 ;;;###autoload
105 (defcustom type-break-good-break-interval nil
106 "Number of seconds considered to be an adequate explicit typing rest.
108 When this variable is non-nil, its value is considered to be a \"good\"
109 length (in seconds) for a break initiated by the command `type-break',
110 overriding `type-break-good-rest-interval'. This provides querying of
111 break interruptions when `type-break-good-rest-interval' is nil."
112 :type 'integer
113 :group 'type-break)
115 ;;;###autoload
116 (defcustom type-break-keystroke-threshold
117 ;; Assuming typing speed is 35wpm (on the average, do you really
118 ;; type more than that in a minute? I spend a lot of time reading mail
119 ;; and simply studying code in buffers) and average word length is
120 ;; about 5 letters, default upper threshold to the average number of
121 ;; keystrokes one is likely to type in a break interval. That way if the
122 ;; user goes through a furious burst of typing activity, cause a typing
123 ;; break to be required sooner than originally scheduled.
124 ;; Conversely, the minimum threshold should be about a fifth of this.
125 (let* ((wpm 35)
126 (avg-word-length 5)
127 (upper (* wpm avg-word-length (/ type-break-interval 60)))
128 (lower (/ upper 5)))
129 (cons lower upper))
130 "Upper and lower bound on number of keystrokes for considering typing break.
131 This structure is a pair of numbers (MIN . MAX).
133 The first number is the minimum number of keystrokes that must have been
134 entered since the last typing break before considering another one, even if
135 the scheduled time has elapsed; the break is simply rescheduled until later
136 if the minimum threshold hasn't been reached. If this first value is nil,
137 then there is no minimum threshold; as soon as the scheduled time has
138 elapsed, the user will always be queried.
140 The second number is the maximum number of keystrokes that can be entered
141 before a typing break is requested immediately, pre-empting the originally
142 scheduled break. If this second value is nil, then no pre-emptive breaks
143 will occur; only scheduled ones will.
145 Keys with bucky bits (shift, control, meta, etc) are counted as only one
146 keystroke even though they really require multiple keys to generate them.
148 The command `type-break-guesstimate-keystroke-threshold' can be used to
149 guess a reasonably good pair of values for this variable."
150 :type 'sexp
151 :group 'type-break)
153 (defcustom type-break-query-function 'yes-or-no-p
154 "Function to use for making query for a typing break.
155 It should take a string as an argument, the prompt.
156 Usually this should be set to `yes-or-no-p' or `y-or-n-p'.
158 To avoid being queried at all, set `type-break-query-mode' to nil."
159 :type '(radio function
160 (function-item yes-or-no-p)
161 (function-item y-or-n-p))
162 :group 'type-break)
164 (defcustom type-break-query-interval 60
165 "Number of seconds between queries to take a break, if put off.
166 The user will continue to be prompted at this interval until he or she
167 finally submits to taking a typing break."
168 :type 'integer
169 :group 'type-break)
171 (defcustom type-break-time-warning-intervals '(300 120 60 30)
172 "List of time intervals for warnings about upcoming typing break.
173 At each of the intervals (specified in seconds) away from a scheduled
174 typing break, print a warning in the echo area."
175 :type '(repeat integer)
176 :group 'type-break)
178 (defcustom type-break-keystroke-warning-intervals '(300 200 100 50)
179 "List of keystroke measurements for warnings about upcoming typing break.
180 At each of the intervals (specified in keystrokes) away from the upper
181 keystroke threshold, print a warning in the echo area.
182 If either this variable or the upper threshold is set, then no warnings
183 will occur."
184 :type '(repeat integer)
185 :group 'type-break)
187 (defcustom type-break-warning-repeat 40
188 "Number of keystrokes for which warnings should be repeated.
189 That is, for each of this many keystrokes the warning is redisplayed
190 in the echo area to make sure it's really seen."
191 :type 'integer
192 :group 'type-break)
194 (defcustom type-break-time-stamp-format "[%H:%M] "
195 "Timestamp format used to prefix messages.
196 Format specifiers are as used by `format-time-string'."
197 :type 'string
198 :group 'type-break)
200 (defcustom type-break-demo-functions
201 '(type-break-demo-boring type-break-demo-life type-break-demo-hanoi)
202 "List of functions to consider running as demos during typing breaks.
203 When a typing break begins, one of these functions is selected randomly
204 to have Emacs do something interesting.
206 Any function in this list should start a demo which ceases as soon as a
207 key is pressed."
208 :type '(repeat function)
209 :group 'type-break)
211 (defcustom type-break-demo-boring-stats nil
212 "Show word per minute and keystroke figures in the Boring demo."
213 :type 'boolean
214 :group 'type-break)
216 (defcustom type-break-terse-messages nil
217 "Use slightly terser messages."
218 :type 'boolean
219 :group 'type-break)
221 (defcustom type-break-file-name (convert-standard-filename "~/.type-break")
222 "Name of file used to save state across sessions.
223 If this is nil, no data will be saved across sessions."
224 :type 'file
225 :group 'type-break)
227 (defvar type-break-post-command-hook '(type-break-check)
228 "Hook run indirectly by `post-command-hook' for typing break functions.
229 This is not really intended to be set by the user, but it's probably
230 harmless to do so. Mainly it is used by various parts of the typing break
231 program to delay actions until after the user has completed some command.
232 It exists because `post-command-hook' itself is inaccessible while its
233 functions are being run, and some type-break--related functions want to
234 remove themselves after running.")
237 ;; Mode line frobs
239 (defvar type-break-mode-line-format
240 '(type-break-mode-line-message-mode
242 type-break-mode-line-break-message
243 type-break-mode-line-warning))
244 "*Format of messages in the mode line concerning typing breaks.")
246 (defvar type-break-mode-line-break-message
247 '(type-break-mode-line-break-message-p
248 type-break-mode-line-break-string))
250 (defvar type-break-mode-line-break-message-p nil)
251 (defvar type-break-mode-line-break-string " *** TAKE A TYPING BREAK NOW ***")
253 (defvar type-break-mode-line-warning
254 '(type-break-mode-line-break-message-p
255 ("")
256 (type-break-warning-countdown-string
257 (" *** "
258 "Break in "
259 type-break-warning-countdown-string
261 type-break-warning-countdown-string-type
262 "***"))))
264 (defvar type-break-warning-countdown-string nil
265 "If non-nil, this is a countdown for the next typing break.
267 This variable, in conjunction with `type-break-warning-countdown-string-type'
268 \(which indicates whether this value is a number of keystrokes or seconds)
269 is installed in `mode-line-format' to notify of imminent typing breaks.")
271 (defvar type-break-warning-countdown-string-type nil
272 "Indicates the unit type of `type-break-warning-countdown-string'.
273 It will be either \"seconds\" or \"keystrokes\".")
276 ;; These are internal variables. Do not set them yourself.
278 (defvar type-break-alarm-p nil)
279 (defvar type-break-keystroke-count 0)
280 (defvar type-break-time-last-break nil)
281 (defvar type-break-time-next-break nil)
282 (defvar type-break-time-last-command (current-time))
283 (defvar type-break-current-time-warning-interval nil)
284 (defvar type-break-current-keystroke-warning-interval nil)
285 (defvar type-break-time-warning-count 0)
286 (defvar type-break-keystroke-warning-count 0)
287 (defvar type-break-interval-start nil)
290 ;;;###autoload
291 (defun type-break-mode (&optional prefix)
292 "Enable or disable typing-break mode.
293 This is a minor mode, but it is global to all buffers by default.
295 When this mode is enabled, the user is encouraged to take typing breaks at
296 appropriate intervals; either after a specified amount of time or when the
297 user has exceeded a keystroke threshold. When the time arrives, the user
298 is asked to take a break. If the user refuses at that time, Emacs will ask
299 again in a short period of time. The idea is to give the user enough time
300 to find a good breaking point in his or her work, but be sufficiently
301 annoying to discourage putting typing breaks off indefinitely.
303 A negative prefix argument disables this mode.
304 No argument or any non-negative argument enables it.
306 The user may enable or disable this mode by setting the variable of the
307 same name, though setting it in that way doesn't reschedule a break or
308 reset the keystroke counter.
310 If the mode was previously disabled and is enabled as a consequence of
311 calling this function, it schedules a break with `type-break-schedule' to
312 make sure one occurs (the user can call that command to reschedule the
313 break at any time). It also initializes the keystroke counter.
315 The variable `type-break-interval' specifies the number of seconds to
316 schedule between regular typing breaks. This variable doesn't directly
317 affect the time schedule; it simply provides a default for the
318 `type-break-schedule' command.
320 If set, the variable `type-break-good-rest-interval' specifies the minimum
321 amount of time which is considered a reasonable typing break. Whenever
322 that time has elapsed, typing breaks are automatically rescheduled for
323 later even if Emacs didn't prompt you to take one first. Also, if a break
324 is ended before this much time has elapsed, the user will be asked whether
325 or not to continue. A nil value for this variable prevents automatic
326 break rescheduling, making `type-break-interval' an upper bound on the time
327 between breaks. In this case breaks will be prompted for as usual before
328 the upper bound if the keystroke threshold is reached.
330 If `type-break-good-rest-interval' is nil and
331 `type-break-good-break-interval' is set, then confirmation is required to
332 interrupt a break before `type-break-good-break-interval' seconds
333 have passed. This provides for an upper bound on the time between breaks
334 together with confirmation of interruptions to these breaks.
336 The variable `type-break-keystroke-threshold' is used to determine the
337 thresholds at which typing breaks should be considered. You can use
338 the command `type-break-guesstimate-keystroke-threshold' to try to
339 approximate good values for this.
341 There are several variables that affect how or when warning messages about
342 imminent typing breaks are displayed. They include:
344 `type-break-mode-line-message-mode'
345 `type-break-time-warning-intervals'
346 `type-break-keystroke-warning-intervals'
347 `type-break-warning-repeat'
348 `type-break-warning-countdown-string'
349 `type-break-warning-countdown-string-type'
351 There are several variables that affect if, how, and when queries to begin
352 a typing break occur. They include:
354 `type-break-query-mode'
355 `type-break-query-function'
356 `type-break-query-interval'
358 The command `type-break-statistics' prints interesting things.
360 Finally, a file (named `type-break-file-name') is used to store information
361 across Emacs sessions. This provides recovery of the break status between
362 sessions and after a crash. Manual changes to the file may result in
363 problems."
364 (interactive "P")
365 (type-break-check-post-command-hook)
367 (let ((already-enabled type-break-mode))
368 (setq type-break-mode (>= (prefix-numeric-value prefix) 0))
370 (cond
371 ((and already-enabled type-break-mode)
372 (and (called-interactively-p 'interactive)
373 (message "Type Break mode is already enabled")))
374 (type-break-mode
375 (when type-break-file-name
376 (with-current-buffer (find-file-noselect type-break-file-name 'nowarn)
377 (setq buffer-save-without-query t)))
379 (or global-mode-string
380 (setq global-mode-string '("")))
381 (or (assq 'type-break-mode-line-message-mode
382 minor-mode-alist)
383 (setq minor-mode-alist
384 (cons type-break-mode-line-format
385 minor-mode-alist)))
386 (type-break-keystroke-reset)
387 (type-break-mode-line-countdown-or-break nil)
389 (setq type-break-time-last-break
390 (or (type-break-get-previous-time)
391 (current-time)))
393 ;; schedule according to break time from session file
394 (type-break-schedule
395 (let (diff)
396 (if (and type-break-time-last-break
397 (< (setq diff (type-break-time-difference
398 type-break-time-last-break
399 (current-time)))
400 type-break-interval))
401 ;; use the file's value
402 (progn
403 (setq type-break-keystroke-count
404 (type-break-get-previous-count))
405 ;; file the time, in case it was read from the auto-save file
406 (type-break-file-time type-break-interval-start)
407 (setq type-break-interval-start type-break-time-last-break)
408 (- type-break-interval diff))
409 ;; schedule from now
410 (setq type-break-interval-start (current-time))
411 (type-break-file-time type-break-interval-start)
412 type-break-interval))
413 type-break-interval-start
414 type-break-interval)
416 (and (called-interactively-p 'interactive)
417 (message "Type Break mode is enabled and set")))
419 (type-break-keystroke-reset)
420 (type-break-mode-line-countdown-or-break nil)
421 (type-break-cancel-schedule)
422 (do-auto-save)
423 (when type-break-file-name
424 (with-current-buffer (find-file-noselect type-break-file-name
425 'nowarn)
426 (set-buffer-modified-p nil)
427 (unlock-buffer)
428 (kill-this-buffer)))
429 (and (called-interactively-p 'interactive)
430 (message "Type Break mode is disabled")))))
431 type-break-mode)
433 (define-minor-mode type-break-mode-line-message-mode
434 "Toggle warnings about typing breaks in the mode line.
435 With a prefix argument ARG, enable these warnings if ARG is
436 positive, and disable them otherwise. If called from Lisp,
437 enable them if ARG is omitted or nil.
439 The user may also enable or disable this mode simply by setting
440 the variable of the same name.
442 Variables controlling the display of messages in the mode line include:
444 `mode-line-format'
445 `global-mode-string'
446 `type-break-mode-line-break-message'
447 `type-break-mode-line-warning'"
448 :global t)
450 (define-minor-mode type-break-query-mode
451 "Toggle typing break queries.
452 With a prefix argument ARG, enable these queries if ARG is
453 positive, and disable them otherwise. If called from Lisp,
454 enable them if ARG is omitted or nil.
456 The user may also enable or disable this mode simply by setting
457 the variable of the same name."
458 :global t)
461 ;;; session file functions
463 (defvar type-break-auto-save-file-name
464 (let ((buffer-file-name type-break-file-name))
465 (make-auto-save-file-name))
466 "Auto-save name of `type-break-file-name'.")
468 (defun type-break-file-time (&optional time)
469 "File break time in `type-break-file-name', unless the file is locked."
470 (if (and type-break-file-name
471 (not (stringp (file-locked-p type-break-file-name))))
472 (with-current-buffer (find-file-noselect type-break-file-name
473 'nowarn)
474 (let ((inhibit-read-only t))
475 (erase-buffer)
476 (insert (format "%s\n\n" (or time type-break-interval-start)))
477 ;; file saving is left to auto-save
478 ))))
480 (defun type-break-file-keystroke-count ()
481 "File keystroke count in `type-break-file-name', unless the file is locked."
482 (if (and type-break-file-name
483 (not (stringp (file-locked-p type-break-file-name))))
484 ;; Prevent deactivation of the mark in some other buffer.
485 (let (deactivate-mark)
486 (with-current-buffer (find-file-noselect type-break-file-name
487 'nowarn)
488 (save-excursion
489 (let ((inhibit-read-only t))
490 (goto-char (point-min))
491 (forward-line)
492 (delete-region (point) (line-end-position))
493 (insert (format "%s" type-break-keystroke-count))
494 ;; file saving is left to auto-save
495 ))))))
497 (defun timep (time)
498 "If TIME is in the format returned by `current-time' then
499 return TIME, else return nil."
500 (condition-case nil
501 (and (float-time time) time)
502 (error nil)))
504 (defun type-break-choose-file ()
505 "Return file to read from."
506 (cond
507 ((not type-break-file-name)
508 nil)
509 ((and (file-exists-p type-break-auto-save-file-name)
510 (file-readable-p type-break-auto-save-file-name))
511 type-break-auto-save-file-name)
512 ((and (file-exists-p type-break-file-name)
513 (file-readable-p type-break-file-name))
514 type-break-file-name)
515 (t nil)))
517 (defun type-break-get-previous-time ()
518 "Get previous break time from `type-break-file-name'.
519 Returns nil if the file is missing or if the time breaks with the
520 `current-time' format."
521 (let ((file (type-break-choose-file)))
522 (if file
523 (timep ;; returns expected format, else nil
524 (with-current-buffer (find-file-noselect file 'nowarn)
525 (condition-case nil
526 (save-excursion
527 (goto-char (point-min))
528 (read (current-buffer)))
529 (end-of-file
530 (error "End of file in `%s'" file))))))))
532 (defun type-break-get-previous-count ()
533 "Get previous keystroke count from `type-break-file-name'.
534 Return 0 if the file is missing or if the form read is not an
535 integer."
536 (let ((file (type-break-choose-file)))
537 (if (and file
538 (integerp
539 (setq file
540 (with-current-buffer
541 (find-file-noselect file 'nowarn)
542 (condition-case nil
543 (save-excursion
544 (goto-char (point-min))
545 (forward-line 1)
546 (read (current-buffer)))
547 (end-of-file
548 (error "End of file in `%s'" file)))))))
549 file
550 0)))
553 ;;;###autoload
554 (defun type-break ()
555 "Take a typing break.
557 During the break, a demo selected from the functions listed in
558 `type-break-demo-functions' is run.
560 After the typing break is finished, the next break is scheduled
561 as per the function `type-break-schedule'."
562 (interactive)
563 (do-auto-save)
564 (type-break-cancel-schedule)
565 ;; remove any query scheduled during interactive invocation
566 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
567 (let ((continue t)
568 (start-time (current-time)))
569 (setq type-break-time-last-break start-time)
570 (while continue
571 (save-window-excursion
572 ;; Eat the screen.
573 (and (eq (selected-window) (minibuffer-window))
574 (other-window 1))
575 (delete-other-windows)
576 (scroll-right (window-width))
577 (unless type-break-terse-messages
578 (message "Press any key to resume from typing break."))
580 (random t)
581 (let* ((len (length type-break-demo-functions))
582 (idx (random len))
583 (fn (nth idx type-break-demo-functions)))
584 (condition-case ()
585 (funcall fn)
586 (error nil))))
588 (let ((good-interval (or type-break-good-rest-interval
589 type-break-good-break-interval)))
590 (cond
591 (good-interval
592 (let ((break-secs (type-break-time-difference
593 start-time (current-time))))
594 (cond
595 ((>= break-secs good-interval)
596 (setq continue nil))
597 ;; 60 seconds may be too much leeway if the break is only 3
598 ;; minutes to begin with. You can just say "no" to the query
599 ;; below if you're in that much of a hurry.
600 ;;((> 60 (abs (- break-secs good-interval)))
601 ;; (setq continue nil))
602 ((funcall
603 type-break-query-function
604 (format
605 (if type-break-terse-messages
606 "%s%s remaining. Continue break? "
607 "%sYou really ought to rest %s more. Continue break? ")
608 (type-break-time-stamp)
609 (type-break-format-time (- good-interval
610 break-secs)))))
612 (setq continue nil)))))
613 (t (setq continue nil))))))
615 (type-break-keystroke-reset)
616 (type-break-file-time)
617 (type-break-mode-line-countdown-or-break nil)
618 (type-break-schedule))
621 (defun type-break-schedule (&optional time start interval)
622 "Schedule a typing break for TIME seconds from now.
623 If time is not specified it defaults to `type-break-interval'.
624 START and INTERVAL are used when recovering a break.
625 START is the start of the break (defaults to now).
626 INTERVAL is the full length of an interval (defaults to TIME)."
627 (interactive (list (and current-prefix-arg
628 (prefix-numeric-value current-prefix-arg))))
629 (or time (setq time type-break-interval))
630 (type-break-check-post-command-hook)
631 (type-break-cancel-schedule)
632 (type-break-time-warning-schedule time 'reset)
633 (type-break-run-at-time (max 1 time) nil 'type-break-alarm)
634 (setq type-break-time-next-break
635 (type-break-time-sum (or start (current-time))
636 (or interval time))))
638 (defun type-break-cancel-schedule ()
639 (type-break-cancel-time-warning-schedule)
640 (type-break-cancel-function-timers 'type-break-alarm)
641 (setq type-break-alarm-p nil)
642 (setq type-break-time-next-break nil))
644 (defun type-break-time-warning-schedule (&optional time resetp)
645 (let ((type-break-current-time-warning-interval nil))
646 (type-break-cancel-time-warning-schedule))
647 (add-hook 'type-break-post-command-hook 'type-break-time-warning 'append)
648 (cond
649 (type-break-time-warning-intervals
650 (and resetp
651 (setq type-break-current-time-warning-interval
652 type-break-time-warning-intervals))
654 (or time
655 (setq time (type-break-time-difference (current-time)
656 type-break-time-next-break)))
658 (while (and type-break-current-time-warning-interval
659 (> (car type-break-current-time-warning-interval) time))
660 (setq type-break-current-time-warning-interval
661 (cdr type-break-current-time-warning-interval)))
663 (cond
664 (type-break-current-time-warning-interval
665 (setq time (- time (car type-break-current-time-warning-interval)))
666 (setq type-break-current-time-warning-interval
667 (cdr type-break-current-time-warning-interval))
669 ;(let (type-break-current-time-warning-interval)
670 ; (type-break-cancel-time-warning-schedule))
671 (type-break-run-at-time (max 1 time) nil 'type-break-time-warning-alarm)
673 (cond
674 (resetp
675 (setq type-break-warning-countdown-string nil))
677 (setq type-break-warning-countdown-string (number-to-string time))
678 (setq type-break-warning-countdown-string-type "seconds"))))))))
680 (defun type-break-cancel-time-warning-schedule ()
681 (type-break-cancel-function-timers 'type-break-time-warning-alarm)
682 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
683 (setq type-break-current-time-warning-interval
684 type-break-time-warning-intervals)
685 (setq type-break-time-warning-count 0) ; avoid warnings after break
686 (setq type-break-warning-countdown-string nil))
688 (defun type-break-alarm ()
689 (type-break-check-post-command-hook)
690 (setq type-break-alarm-p t)
691 (type-break-mode-line-countdown-or-break 'break))
693 (defun type-break-time-warning-alarm ()
694 (type-break-check-post-command-hook)
695 (type-break-time-warning-schedule)
696 (setq type-break-time-warning-count type-break-warning-repeat)
697 (type-break-time-warning)
698 (type-break-mode-line-countdown-or-break 'countdown))
701 (defun type-break-run-tb-post-command-hook ()
702 (and type-break-mode
703 (run-hooks 'type-break-post-command-hook)))
705 (defun type-break-check ()
706 "Ask to take a typing break if appropriate.
707 This may be the case either because the scheduled time has come \(and the
708 minimum keystroke threshold has been reached\) or because the maximum
709 keystroke threshold has been exceeded."
710 (type-break-file-keystroke-count)
711 (let* ((min-threshold (car type-break-keystroke-threshold))
712 (max-threshold (cdr type-break-keystroke-threshold)))
713 (and type-break-good-rest-interval
714 (progn
715 (and (> (type-break-time-difference
716 type-break-time-last-command (current-time))
717 type-break-good-rest-interval)
718 (progn
719 (type-break-keystroke-reset)
720 (type-break-mode-line-countdown-or-break nil)
721 (setq type-break-time-last-break (current-time))
722 (type-break-schedule)))
723 (setq type-break-time-last-command (current-time))))
725 (and type-break-keystroke-threshold
726 (let ((keys (this-command-keys)))
727 (cond
728 ;; Ignore mouse motion
729 ((and (vectorp keys)
730 (consp (aref keys 0))
731 (memq (car (aref keys 0)) '(mouse-movement))))
733 (setq type-break-keystroke-count
734 (+ type-break-keystroke-count (length keys)))))))
736 (cond
737 (type-break-alarm-p
738 (cond
739 ((input-pending-p))
740 ((eq (selected-window) (minibuffer-window)))
741 ((and min-threshold
742 (< type-break-keystroke-count min-threshold))
743 (type-break-schedule))
745 ;; If keystroke count is within min-threshold of
746 ;; max-threshold, lower it to reduce the likelihood of an
747 ;; immediate subsequent query.
748 (and max-threshold
749 min-threshold
750 (< (- max-threshold type-break-keystroke-count) min-threshold)
751 (progn
752 (type-break-keystroke-reset)
753 (setq type-break-keystroke-count min-threshold)))
754 (type-break-query))))
755 ((and type-break-keystroke-warning-intervals
756 max-threshold
757 (= type-break-keystroke-warning-count 0)
758 (type-break-check-keystroke-warning)))
759 ((and max-threshold
760 (> type-break-keystroke-count max-threshold)
761 (not (input-pending-p))
762 (not (eq (selected-window) (minibuffer-window))))
763 (type-break-keystroke-reset)
764 (setq type-break-keystroke-count (or min-threshold 0))
765 (type-break-query)))))
767 ;; This should return t if warnings were enabled, nil otherwise.
768 (defun type-break-check-keystroke-warning ()
769 ;; This is safe because the caller should have checked that the cdr was
770 ;; non-nil already.
771 (let ((left (- (cdr type-break-keystroke-threshold)
772 type-break-keystroke-count)))
773 (cond
774 ((null (car type-break-current-keystroke-warning-interval))
775 nil)
776 ((> left (car type-break-current-keystroke-warning-interval))
777 nil)
779 (while (and (car type-break-current-keystroke-warning-interval)
780 (< left (car type-break-current-keystroke-warning-interval)))
781 (setq type-break-current-keystroke-warning-interval
782 (cdr type-break-current-keystroke-warning-interval)))
783 (setq type-break-keystroke-warning-count type-break-warning-repeat)
784 (add-hook 'type-break-post-command-hook 'type-break-keystroke-warning)
785 (setq type-break-warning-countdown-string (number-to-string left))
786 (setq type-break-warning-countdown-string-type "keystrokes")
787 (type-break-mode-line-countdown-or-break 'countdown)
788 t))))
790 ;; Arrange for a break query to be made, when the user stops typing furiously.
791 (defun type-break-query ()
792 (add-hook 'type-break-post-command-hook 'type-break-do-query))
794 (defun type-break-do-query ()
795 (cond
796 ((not type-break-query-mode)
797 (type-break-noninteractive-query)
798 (type-break-schedule type-break-query-interval)
799 (remove-hook 'type-break-post-command-hook 'type-break-do-query))
800 ((sit-for 2)
801 (condition-case ()
802 (cond
803 ((let ((type-break-mode nil)
804 ;; yes-or-no-p sets this-command to exit-minibuffer,
805 ;; which hoses undo or yank-pop (if you happened to be
806 ;; yanking just when the query occurred).
807 (this-command this-command))
808 ;; Cancel schedule to prevent possibility of a second query
809 ;; from taking place before this one has even returned.
810 ;; The condition-case wrapper will reschedule on quit.
811 (type-break-cancel-schedule)
812 ;; Also prevent a second query when the break is interrupted.
813 (remove-hook 'type-break-post-command-hook 'type-break-do-query)
814 (funcall type-break-query-function
815 (format "%s%s"
816 (type-break-time-stamp)
817 (if type-break-terse-messages
818 "Break now? "
819 "Take a break from typing now? "))))
820 (type-break))
822 (type-break-schedule type-break-query-interval)))
823 (quit
824 (type-break-schedule type-break-query-interval))))))
826 (defun type-break-noninteractive-query (&optional _ignored-args)
827 "Null query function which doesn't interrupt user and assumes `no'.
828 It prints a reminder in the echo area to take a break, but doesn't enforce
829 this or ask the user to start one right now."
830 (cond
831 (type-break-mode-line-message-mode)
833 (beep t)
834 (message "%sYou should take a typing break now. Do `M-x type-break'."
835 (type-break-time-stamp))
836 (sit-for 1)
837 (beep t)
838 ;; return nil so query caller knows to reset reminder, as if user
839 ;; said "no" in response to yes-or-no-p.
840 nil)))
842 (defun type-break-time-warning ()
843 (cond
844 ((and (car type-break-keystroke-threshold)
845 (< type-break-keystroke-count (car type-break-keystroke-threshold))))
846 ((> type-break-time-warning-count 0)
847 (let ((timeleft (type-break-time-difference (current-time)
848 type-break-time-next-break)))
849 (setq type-break-warning-countdown-string (number-to-string timeleft))
850 (cond
851 ((eq (selected-window) (minibuffer-window)))
852 ;; Do nothing if the command was just a prefix arg, since that will
853 ;; immediately be followed by some other interactive command.
854 ;; Otherwise, it is particularly annoying for the sit-for below to
855 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
856 ((memq this-command '(digit-argument universal-argument)))
857 ((not type-break-mode-line-message-mode)
858 ;; Pause for a moment so any previous message can be seen.
859 (sit-for 2)
860 (message "%sWarning: typing break due in %s."
861 (type-break-time-stamp)
862 (type-break-format-time timeleft))
863 (setq type-break-time-warning-count
864 (1- type-break-time-warning-count))))))
866 (remove-hook 'type-break-post-command-hook 'type-break-time-warning)
867 (setq type-break-warning-countdown-string nil))))
869 (defun type-break-keystroke-warning ()
870 (cond
871 ((> type-break-keystroke-warning-count 0)
872 (setq type-break-warning-countdown-string
873 (number-to-string (- (cdr type-break-keystroke-threshold)
874 type-break-keystroke-count)))
875 (cond
876 ((eq (selected-window) (minibuffer-window)))
877 ;; Do nothing if the command was just a prefix arg, since that will
878 ;; immediately be followed by some other interactive command.
879 ;; Otherwise, it is particularly annoying for the sit-for below to
880 ;; delay redisplay when one types sequences like `C-u -1 C-l'.
881 ((memq this-command '(digit-argument universal-argument)))
882 ((not type-break-mode-line-message-mode)
883 (sit-for 2)
884 (message "%sWarning: typing break due in %s keystrokes."
885 (type-break-time-stamp)
886 (- (cdr type-break-keystroke-threshold)
887 type-break-keystroke-count))
888 (setq type-break-keystroke-warning-count
889 (1- type-break-keystroke-warning-count)))))
891 (remove-hook 'type-break-post-command-hook
892 'type-break-keystroke-warning)
893 (setq type-break-warning-countdown-string nil))))
895 (defun type-break-mode-line-countdown-or-break (&optional type)
896 (cond
897 ((not type-break-mode-line-message-mode))
898 ((eq type 'countdown)
899 ;(setq type-break-mode-line-break-message-p nil)
900 (add-hook 'type-break-post-command-hook
901 'type-break-force-mode-line-update 'append))
902 ((eq type 'break)
903 ;; Alternate
904 (setq type-break-mode-line-break-message-p
905 (not type-break-mode-line-break-message-p))
906 (remove-hook 'type-break-post-command-hook
907 'type-break-force-mode-line-update))
909 (setq type-break-mode-line-break-message-p nil)
910 (setq type-break-warning-countdown-string nil)
911 (remove-hook 'type-break-post-command-hook
912 'type-break-force-mode-line-update)))
913 (type-break-force-mode-line-update))
916 ;;;###autoload
917 (defun type-break-statistics ()
918 "Print statistics about typing breaks in a temporary buffer.
919 This includes the last time a typing break was taken, when the next one is
920 scheduled, the keystroke thresholds and the current keystroke count, etc."
921 (interactive)
922 (with-output-to-temp-buffer "*Typing Break Statistics*"
923 (princ (format "Typing break statistics\n-----------------------\n
924 Typing break mode is currently %s.
925 Interactive query for breaks is %s.
926 Warnings of imminent typing breaks in mode line is %s.
928 Last typing break ended : %s
929 Next scheduled typing break : %s\n
930 Minimum keystroke threshold : %s
931 Maximum keystroke threshold : %s
932 Current keystroke count : %s"
933 (if type-break-mode "enabled" "disabled")
934 (if type-break-query-mode "enabled" "disabled")
935 (if type-break-mode-line-message-mode "enabled" "disabled")
936 (if type-break-time-last-break
937 (current-time-string type-break-time-last-break)
938 "never")
939 (if (and type-break-mode type-break-time-next-break)
940 (format "%s\t(%s from now)"
941 (current-time-string type-break-time-next-break)
942 (type-break-format-time
943 (type-break-time-difference
944 (current-time)
945 type-break-time-next-break)))
946 "none scheduled")
947 (or (car type-break-keystroke-threshold) "none")
948 (or (cdr type-break-keystroke-threshold) "none")
949 type-break-keystroke-count))))
951 ;;;###autoload
952 (defun type-break-guesstimate-keystroke-threshold (wpm &optional wordlen frac)
953 "Guess values for the minimum/maximum keystroke threshold for typing breaks.
955 If called interactively, the user is prompted for their guess as to how
956 many words per minute they usually type. This value should not be your
957 maximum WPM, but your average. Of course, this is harder to gauge since it
958 can vary considerably depending on what you are doing. For example, one
959 tends to type less when debugging a program as opposed to writing
960 documentation. (Perhaps a separate program should be written to estimate
961 average typing speed.)
963 From that, this command sets the values in `type-break-keystroke-threshold'
964 based on a fairly simple algorithm involving assumptions about the average
965 length of words (5). For the minimum threshold, it uses about a fifth of
966 the computed maximum threshold.
968 When called from Lisp programs, the optional args WORDLEN and FRAC can be
969 used to override the default assumption about average word length and the
970 fraction of the maximum threshold to which to set the minimum threshold.
971 FRAC should be the inverse of the fractional value; for example, a value of
972 2 would mean to use one half, a value of 4 would mean to use one quarter, etc."
973 (interactive "NOn average, how many words per minute do you type? ")
974 (let* ((upper (* wpm (or wordlen 5) (/ type-break-interval 60)))
975 (lower (/ upper (or frac 5))))
976 (or type-break-keystroke-threshold
977 (setq type-break-keystroke-threshold (cons nil nil)))
978 (setcar type-break-keystroke-threshold lower)
979 (setcdr type-break-keystroke-threshold upper)
980 (if (called-interactively-p 'interactive)
981 (message "min threshold: %d\tmax threshold: %d" lower upper))
982 type-break-keystroke-threshold))
985 ;;; misc functions
987 ;; Compute the difference, in seconds, between a and b, two structures
988 ;; similar to those returned by `current-time'.
989 (defun type-break-time-difference (a b)
990 (round (float-time (time-subtract b a))))
992 ;; Return (in a new list the same in structure to that returned by
993 ;; `current-time') the sum of the arguments. Each argument may be a time
994 ;; list or a single integer, a number of seconds.
995 ;; This function keeps the high and low 16 bits of the seconds properly
996 ;; balanced so that the lower value never exceeds 16 bits. Otherwise, when
997 ;; the result is passed to `current-time-string' it will toss some of the
998 ;; "low" bits and format the time incorrectly.
999 (defun type-break-time-sum (&rest tmlist)
1000 (let ((sum '(0 0 0)))
1001 (dolist (tem tmlist sum)
1002 (setq sum (time-add sum (if (integerp tem)
1003 (list (floor tem 65536) (mod tem 65536))
1004 tem))))))
1006 (defun type-break-time-stamp (&optional when)
1007 (if (fboundp 'format-time-string)
1008 (format-time-string type-break-time-stamp-format when)
1009 ;; Emacs 19.28 and prior do not have format-time-string.
1010 ;; In that case, result is not customizable. Upgrade today!
1011 (format "[%s] " (substring (current-time-string when) 11 16))))
1013 (defun type-break-format-time (secs)
1014 (let ((mins (/ secs 60)))
1015 (cond
1016 ((= mins 1) (format "%d minute" mins))
1017 ((> mins 0) (format "%d minutes" mins))
1018 ((= secs 1) (format "%d second" secs))
1019 (t (format "%d seconds" secs)))))
1021 (defun type-break-keystroke-reset ()
1022 (setq type-break-interval-start (current-time)) ; not a keystroke
1023 (setq type-break-keystroke-count 0)
1024 (setq type-break-keystroke-warning-count 0)
1025 (setq type-break-current-keystroke-warning-interval
1026 type-break-keystroke-warning-intervals)
1027 (remove-hook 'type-break-post-command-hook 'type-break-keystroke-warning))
1029 (defun type-break-force-mode-line-update (&optional all)
1030 "Force the mode-line of the current buffer to be redisplayed.
1031 With optional non-nil ALL, force redisplay of all mode-lines."
1032 (and all (with-current-buffer (other-buffer)))
1033 (set-buffer-modified-p (buffer-modified-p)))
1035 ;; If an exception occurs in Emacs while running the post command hook, the
1036 ;; value of that hook is clobbered. This is because the value of the
1037 ;; variable is temporarily set to nil while it's running to prevent
1038 ;; recursive application, but it also means an exception aborts the routine
1039 ;; of restoring it. This function is called from the timers to restore it,
1040 ;; just in case.
1041 (defun type-break-check-post-command-hook ()
1042 (add-hook 'post-command-hook 'type-break-run-tb-post-command-hook 'append))
1045 ;;; Timer wrapper functions
1047 ;; These shield type-break from variations in the interval timer packages
1048 ;; for different versions of Emacs.
1050 (defun type-break-run-at-time (time repeat function)
1051 (condition-case nil (or (require 'timer) (require 'itimer)) (error nil))
1052 (run-at-time time repeat function))
1054 (defvar timer-dont-exit)
1055 (defun type-break-cancel-function-timers (function)
1056 (let ((timer-dont-exit t))
1057 (cancel-function-timers function)))
1060 ;;; Demo wrappers
1062 (defun type-break-catch-up-event ()
1063 ;; If the last input event is a down-event, read and discard the
1064 ;; corresponding up-event too, to avoid triggering another prompt.
1065 (and (eventp last-input-event)
1066 (memq 'down (event-modifiers last-input-event))
1067 (read-event)))
1069 ;; This is a wrapper around hanoi that calls it with an arg large enough to
1070 ;; make the largest discs possible that will fit in the window.
1071 ;; Also, clean up the *Hanoi* buffer after we're done.
1072 (defun type-break-demo-hanoi ()
1073 "Take a hanoiing typing break."
1074 (and (get-buffer "*Hanoi*")
1075 (kill-buffer "*Hanoi*"))
1076 (condition-case ()
1077 (progn
1078 (hanoi (/ (window-width) 8))
1079 ;; Wait for user to come back.
1080 (read-event)
1081 (type-break-catch-up-event)
1082 (kill-buffer "*Hanoi*"))
1083 (quit
1084 (read-event)
1085 (type-break-catch-up-event)
1086 (and (get-buffer "*Hanoi*")
1087 (kill-buffer "*Hanoi*")))))
1089 ;; This is a wrapper around life that calls it with a `sleep' arg to make
1090 ;; it run a little more leisurely.
1091 ;; Also, clean up the *Life* buffer after we're done.
1092 (defun type-break-demo-life ()
1093 "Take a typing break and get a life."
1094 (let ((continue t))
1095 (while continue
1096 (setq continue nil)
1097 (and (get-buffer "*Life*")
1098 (kill-buffer "*Life*"))
1099 (condition-case ()
1100 (progn
1101 (life 3)
1102 ;; wait for user to return
1103 (read-event)
1104 (type-break-catch-up-event)
1105 (kill-buffer "*Life*"))
1106 (life-extinct
1107 (message "%s" (get 'life-extinct 'error-message))
1108 ;; restart demo
1109 (setq continue t))
1110 (quit
1111 (type-break-catch-up-event)
1112 (and (get-buffer "*Life*")
1113 (kill-buffer "*Life*")))))))
1115 ;; Boring demo, but doesn't use many cycles
1116 (defun type-break-demo-boring ()
1117 "Boring typing break demo."
1118 (let ((rmsg (if type-break-terse-messages
1120 "Press any key to resume from typing break"))
1121 (buffer-name "*Typing Break Buffer*")
1122 lines elapsed timeleft tmsg)
1123 (condition-case ()
1124 (progn
1125 (switch-to-buffer (get-buffer-create buffer-name))
1126 (buffer-disable-undo (current-buffer))
1127 (setq lines (/ (window-body-height) 2))
1128 (unless type-break-terse-messages (setq lines (1- lines)))
1129 (if type-break-demo-boring-stats
1130 (setq lines (- lines 2)))
1131 (setq lines (make-string lines ?\C-j))
1132 (while (not (input-pending-p))
1133 (erase-buffer)
1134 (setq elapsed (type-break-time-difference
1135 type-break-time-last-break
1136 (current-time)))
1137 (let ((good-interval (or type-break-good-rest-interval
1138 type-break-good-break-interval)))
1139 (cond
1140 (good-interval
1141 (setq timeleft (- good-interval elapsed))
1142 (if (> timeleft 0)
1143 (setq tmsg
1144 (format (if type-break-terse-messages
1145 "Break remaining: %s"
1146 "You should rest for %s more")
1147 (type-break-format-time timeleft)))
1148 (setq tmsg
1149 (format (if type-break-terse-messages
1150 "Break complete (%s elapsed in total)"
1151 "Typing break has lasted %s")
1152 (type-break-format-time elapsed)))))
1154 (setq tmsg
1155 (format (if type-break-terse-messages
1156 "Break has lasted %s"
1157 "Typing break has lasted %s")
1158 (type-break-format-time elapsed))))))
1159 (insert lines
1160 (make-string (/ (- (window-width) (length tmsg)) 2) ?\ )
1161 tmsg)
1162 (if (> (length rmsg) 0)
1163 (insert "\n"
1164 (make-string (/ (- (window-width) (length rmsg)) 2)
1165 ?\ )
1166 rmsg))
1167 (if type-break-demo-boring-stats
1168 (let*
1169 ((message
1170 (format
1171 (if type-break-terse-messages
1172 "Since last break: %s keystrokes\n"
1173 "Since your last break you've typed %s keystrokes\n")
1174 type-break-keystroke-count))
1175 (column-spaces
1176 (make-string (/ (- (window-width) (length message)) 2)
1177 ?\ ))
1178 (wpm (/ (/ (float type-break-keystroke-count) 5)
1179 (/ (type-break-time-difference
1180 type-break-interval-start
1181 type-break-time-last-break)
1182 60.0))))
1183 (insert "\n\n" column-spaces message)
1184 (if type-break-terse-messages
1185 (insert (format " %s%.2f wpm"
1186 column-spaces
1187 wpm))
1188 (setq message
1189 (format "at an average of %.2f words per minute"
1190 wpm))
1191 (insert
1192 (make-string (/ (- (window-width) (length message)) 2)
1193 ?\ )
1194 message))))
1195 (goto-char (point-min))
1196 (sit-for 60))
1197 (read-event)
1198 (type-break-catch-up-event)
1199 (kill-buffer buffer-name))
1200 (quit
1201 (and (get-buffer buffer-name)
1202 (kill-buffer buffer-name))))))
1205 (provide 'type-break)
1207 (if type-break-mode
1208 (type-break-mode 1))
1210 ;;; type-break.el ends here