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