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