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