type-break.el 48 KB

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