type-break.el 47 KB

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