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