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