1 ;;; spray.el --- a speed reading mode
3 ;; Copyright (C) 2014 Ian Kelling <ian@iankelling.org>
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ;; Maintainer: Ian Kelling <ian@iankelling.org>
19 ;; Author: Ian Kelling <ian@iankelling.org>
21 ;; Created: 18 Jun 2014
23 ;; URL: https://github.com/ian-kelling/spray
24 ;; Keywords: convenience
28 ;; For speed reading, or just more enjoyable reading. Narrows the buffer to show
29 ;; one word at a time. Adjust speed / pause as needed.
31 ;; Download from Melpa or put this script into a "load-path"ed directory, and
32 ;; load it in your init file:
36 ;; Then you may run spray with "M-x spray-mode". Binding some keys may
39 ;; (global-set-key (kbd "<f6>") 'spray-mode)
41 ;; In spray-mode buffers, following commands are available.
43 ;; - =spray-start/stop= (SPC) ::
44 ;; pause or resume spraying
46 ;; - =spray-backward-word= (h, <left>) ::
47 ;; pause and back to the last word
49 ;; - =spray-forward-word= (l, <right>) ::
50 ;; inverse of =spray-backward-word=
52 ;; - =spray-faster= (f) ::
55 ;; - =spray-slower= (s) ::
58 ;; - =spray-quit= (q, <return>) ::
61 ;; You may customize spray by modifying following items:
63 ;; - [Variable] spray-wpm
64 ;; - [Variable] spray-height
65 ;; - [Variable] spray-margin-top
66 ;; - [Variable] spray-margin-left
67 ;; - [Variable] spray-ramp
68 ;; - [Keymap] spray-mode-map
69 ;; - [Face] spray-base-face
70 ;; - [Face] spray-accent-face
72 ;; Readme.org from the package repository has some additional information:
74 ;; Algorithm specification.
75 ;; Comparison with similar projects.
79 ;; repeated words are indistinguishable, for example
80 ;; "going, going, gone" reads like going, gone, with a slight delay.
82 ;; sentences (like this) should trigger a pause for ( and )
87 ;; 0.0.1 add spray-set-margins
88 ;; 0.0.2 margin options, speed control, better quit
94 ;; * customizable vars
96 (defcustom spray-wpm 400
101 (defcustom spray-height 400
102 "Height of characters"
106 (defcustom spray-margin-top 1
107 "Character margin at top of buffer. Characters are as big as
108 spray text characters."
112 (defcustom spray-margin-left 1
113 "Character margin at left of buffer. Characters are as big as
114 spray text characters."
118 (defcustom spray-ramp 2
119 "Initial words before ramping up to full speed. Pauses for
120 this multiple of wpm on the first word,
121 decreasing by one for each subsequent word."
125 (defcustom spray-unsupported-minor-modes
126 '(buffer-face-mode smartparens-mode highlight-symbol-mode)
127 "Minor modes to toggle off when in spray mode."
129 :type '(list symbol))
134 (defface spray-base-face
135 '((t (:inherit default)))
136 "Face for non-accent characters."
139 (defface spray-accent-face
140 '((t (:foreground "red" :inherit spray-base-face)))
141 "Face for accent character."
147 (defvar spray-mode-map
148 (let ((km (make-sparse-keymap)))
149 (define-key km (kbd "SPC") 'spray-start/stop)
150 (define-key km (kbd "h") 'spray-backward-word)
151 (define-key km (kbd "l") 'spray-forward-word)
152 (define-key km (kbd "<left>") 'spray-backward-word)
153 (define-key km (kbd "<right>") 'spray-forward-word)
154 (define-key km (kbd "f") 'spray-faster)
155 (define-key km (kbd "s") 'spray-slower)
156 (define-key km (kbd "q") 'spray-quit)
157 (define-key km (kbd "<return>") 'spray-quit)
158 (define-key km [remap forward-char] 'spray-forward-word)
159 (define-key km [remap backward-char] 'spray-backward-word)
160 (define-key km [remap forward-word] 'spray-forward-word)
161 (define-key km [remap backward-word] 'spray-backward-word)
162 (define-key km [remap keyboard-quit] 'spray-quit)
164 "keymap for spray-mode buffers")
169 (defvar spray--margin-string "")
170 (defvar spray--base-overlay nil)
171 (defvar spray--accent-overlay nil)
172 (defvar spray--running nil)
173 (defvar spray--first-words 0)
174 (defvar spray--initial-delay 0)
175 (defvar spray--delay 0)
176 (defvar spray--saved-cursor-type nil)
177 (defvar spray--saved-restriction nil)
178 (defvar spray--saved-minor-modes nil)
180 ;; * utility functions
182 (defun spray-set-margins ()
183 "Setup spray--margin-string"
184 (setq spray--margin-string
185 (concat (make-string spray-margin-top 10) ;; 10 = ascii newline
186 (make-string spray-margin-left 32)))) ;; 32 = ascii space
191 (define-minor-mode spray-mode
194 :keymap spray-mode-map
196 (setq spray--base-overlay (make-overlay (point-min) (point-max))
197 spray--accent-overlay (make-overlay 0 0)
198 spray--saved-cursor-type cursor-type
199 spray--saved-restriction (and (buffer-narrowed-p)
200 (cons (point-min) (point-max))))
201 (dolist (mode spray-unsupported-minor-modes)
202 (when (and (boundp mode) (eval mode))
204 (push mode spray--saved-minor-modes)))
205 (setq cursor-type nil)
206 (let ((buffer-face-mode-face `(:height ,spray-height)))
207 (buffer-face-mode 1))
208 (overlay-put spray--base-overlay 'priority 100)
209 (overlay-put spray--base-overlay 'face 'spray-base-face)
210 (overlay-put spray--accent-overlay 'priority 101)
211 (overlay-put spray--accent-overlay 'face 'spray-accent-face)
215 (delete-overlay spray--accent-overlay)
216 (delete-overlay spray--base-overlay)
217 (buffer-face-mode -1)
218 (if spray--saved-restriction
219 (narrow-to-region (car spray--saved-restriction)
220 (cdr spray--saved-restriction))
222 (setq cursor-type spray--saved-cursor-type)
223 (dolist (mode spray--saved-minor-modes)
225 (setq spray--saved-minor-modes nil))))
232 (defun spray--word-at-point ()
233 (skip-chars-backward "^\s\t\n—")
235 (len (+ (skip-chars-forward "^\s\t\n—") (skip-chars-forward "—")))
237 (accent (+ beg (cl-case len
243 ;; this fairly obfuscated, using magic numbers to store state
244 ;; it would be nice to sometime patch this so it is more readable.
245 ;; for greater than 9 length, we display for twice as long
246 ;; for some punctuation, we display a blank
247 (setq spray--delay (+ (if (> len 9) 1 0)
248 (if (looking-at "\n[\s\t\n]") 3 0)
249 (cl-case (char-before)
253 (move-overlay spray--accent-overlay (1- accent) accent)
254 (move-overlay spray--base-overlay beg end)
256 (overlay-put spray--base-overlay 'before-string
257 (concat spray--margin-string
258 (make-string (- 5 (- accent beg)) ?\s)))
259 (narrow-to-region beg end)))
261 (defun spray--update ()
262 (cond ((not (zerop spray--initial-delay))
263 (setq spray--initial-delay (1- spray--initial-delay)))
264 ((not (zerop spray--delay))
265 (setq spray--delay (1- spray--delay))
266 (when (= spray--delay 2)
267 (narrow-to-region (point) (point))))
272 (when (not (zerop spray--first-words))
273 (setq spray--initial-delay spray--first-words)
274 (setq spray--first-words (1- spray--first-words)))
275 (skip-chars-forward "\s\t\n—")
276 (spray--word-at-point)))))
278 ;; * interactive commands
280 (defun spray-start/stop ()
281 "Toggle pause/unpause spray."
283 (or (spray-stop) (spray-start)))
287 Returns t if spray was unpaused."
289 (prog1 spray--running
291 (cancel-timer spray--running)
292 (setq spray--running nil))))
294 (defun spray-start ()
295 "Start / resume spray."
297 (setq spray--first-words spray-ramp)
299 (run-with-timer 0 (/ 60.0 spray-wpm) 'spray--update)))
301 (defun spray-forward-word ()
305 (skip-chars-forward "\s\t\n—")
306 (spray--word-at-point))
308 (defun spray-backward-word ()
312 (skip-chars-backward "^\s\t\n—")
313 (skip-chars-backward "\s\t\n—")
314 (spray--word-at-point))
316 (defun spray-faster ()
319 Increases the wpm (words per minute) parameter. See the variable
324 (defun spray-slower ()
327 Decreases the wpm (words per minute) parameter. See the variable
332 (defun spray-inc-wpm (delta)
333 (let ((was-running spray--running))
335 (when (< 10 (+ spray-wpm delta))
336 (setq spray-wpm (+ spray-wpm delta)))
337 (and was-running (spray-backward-word))
338 (message "spray wpm: %d" spray-wpm)
346 ;;; spray.el ends here