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