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