aab9f8e1988a0e9ada1e137e70f5542ef6a97106
[spray] / spray.el
1 ;;; cedit.el --- a speed reading mode
2
3 ;; Copyright (C) 2014 zk_phi
4
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 2 of the License, or
8 ;; (at your option) any later version.
9 ;;
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.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;; Author: zk_phi
20 ;; URL: http://hins11.yu-yake.com/
21 ;; Version: 0.0.1
22
23 ;;; Commentary:
24
25 ;; Put this script into a "load-path"ed directory, and load it in your
26 ;; init file.
27 ;;
28 ;; (require 'spray)
29 ;;
30 ;; Then you may run spray with "M-x spray-mode". Binding some keys may
31 ;; also be useful.
32 ;;
33 ;; (global-set-key (kbd "<f6>") 'spray-mode)
34 ;;
35 ;; For more informations, see Readme.org.
36
37 ;;; Change Log:
38 ;; 0.0.0 test release
39 ;; 0.0.1 add spray-set-margins
40
41 ;;; Code:
42
43 (require 'face-remap)
44
45 ;; * customizable vars
46
47 (defvar spray-wpm 400 "words/min")
48 (defvar spray-height 400 "height of characters")
49
50 (defvar spray-mode-map
51 (let ((km (make-sparse-keymap)))
52 (define-key km (kbd "SPC") 'spray-start/stop)
53 (define-key km (kbd "h") 'spray-backward-word)
54 (define-key km (kbd "l") 'spray-forward-word)
55 (define-key km (kbd "<left>") 'spray-backward-word)
56 (define-key km (kbd "<right>") 'spray-forward-word)
57 km)
58 "keymap for spray-mode buffers")
59
60 ;; * faces
61
62 (make-face 'spray-base-face)
63 (set-face-attribute 'spray-base-face nil
64 :background (face-background 'default)
65 :foreground (face-foreground 'default))
66
67 (make-face 'spray-orp-face)
68 (set-face-attribute 'spray-orp-face nil
69 :foreground "red"
70 :overline (face-foreground 'default)
71 :underline (face-foreground 'default))
72
73 ;; * internal vars
74
75 (defvar spray--margin-string "")
76 (defvar spray--base-overlay nil)
77 (defvar spray--orp-overlay nil)
78 (defvar spray--running nil)
79 (defvar spray--delay 0)
80 (defvar spray--saved-cursor-type nil)
81 (defvar spray--saved-buffer-face nil)
82 (defvar spray--saved-restriction nil)
83
84 ;; * utility functions
85
86 (defun spray-set-margins (left above)
87 "add margins before/above the spray text. each arguments can be
88 an integer or a float value."
89 (setq spray--margin-string
90 (propertize " " 'display `((space-width ,left) (height ,(1+ above))))))
91
92 ;; * the mode
93
94 ;;;###autoload
95 (define-minor-mode spray-mode
96 "spray mode"
97 :init nil
98 :keymap spray-mode-map
99 (cond (spray-mode
100 (setq spray--base-overlay (make-overlay (point-min) (point-max))
101 spray--orp-overlay (make-overlay 0 0)
102 spray--saved-cursor-type cursor-type
103 spray--saved-restriction (and (buffer-narrowed-p)
104 (cons (point-min) (point-max)))
105 spray--saved-buffer-face (and (boundp 'buffer-face-mode)
106 buffer-face-mode
107 buffer-face-mode-face))
108 (setq cursor-type nil)
109 (let ((buffer-face-mode-face `(:height ,spray-height)))
110 (buffer-face-mode 1))
111 (overlay-put spray--base-overlay 'priority 100)
112 (overlay-put spray--base-overlay 'face 'spray-base-face)
113 (overlay-put spray--orp-overlay 'priority 101)
114 (overlay-put spray--orp-overlay 'face 'spray-orp-face)
115 (add-hook 'pre-command-hook 'spray--pre-command-handler)
116 (spray-start/stop 1))
117 (t
118 (setq cursor-type spray--saved-cursor-type)
119 (if spray--saved-restriction
120 (narrow-to-region (car spray--saved-restriction)
121 (cdr spray--saved-restriction))
122 (widen))
123 (buffer-face-mode -1)
124 (if spray--saved-buffer-face
125 (let ((buffer-face-mode-face spray--saved-buffer-face))
126 (buffer-face-mode 1)))
127 (delete-overlay spray--base-overlay)
128 (delete-overlay spray--orp-overlay)
129 (remove-hook 'pre-command-hook 'spray--pre-command-handler)
130 (spray-start/stop -1))))
131
132 (defun spray--pre-command-handler ()
133 (unless (string-match "^spray-" (symbol-name this-command))
134 (spray-mode -1)))
135
136 (defun spray--word-at-point ()
137 (skip-chars-backward "^\s\t\n")
138 (let* ((beg (point))
139 (len (skip-chars-forward "^\s\t\n"))
140 (end (point))
141 (orp (+ beg (cl-case len
142 ((1) 1)
143 ((2 3 4 5) 2)
144 ((6 7 8 9) 3)
145 ((10 11 12 13) 4)
146 (t 5)))))
147 (setq spray--delay (+ (if (> len 9) 1 0)
148 (if (looking-at "\n[\s\t\n]") 3 0)
149 (cl-case (char-before)
150 ((?. ?! ?\? ?\;) 3)
151 ((?, ?:) 1)
152 (t 0))))
153 (move-overlay spray--orp-overlay (1- orp) orp)
154 (move-overlay spray--base-overlay beg end)
155 (overlay-put spray--base-overlay 'before-string
156 (concat spray--margin-string
157 (make-string (- 5 (- orp beg)) ?\s)))
158 (narrow-to-region beg end)))
159
160 (defun spray--update ()
161 (cond ((not (zerop spray--delay))
162 (setq spray--delay (1- spray--delay))
163 (when (= spray--delay 2)
164 (narrow-to-region (point) (point))))
165 (t
166 (widen)
167 (if (eobp)
168 (spray-mode -1)
169 (skip-chars-forward "\s\t\n")
170 (spray--word-at-point)))))
171
172 ;; * interactive commands
173
174 (defun spray-start/stop (&optional switch)
175 (interactive)
176 (cond ((and (memql switch '(nil 1))
177 (not spray--running))
178 (setq spray--running
179 (run-with-timer 0 (/ 60.0 spray-wpm) 'spray--update)))
180 ((memql switch '(nil -1))
181 (cancel-timer spray--running)
182 (setq spray--running nil))
183 (t
184 nil)))
185
186 (defun spray-forward-word ()
187 (interactive)
188 (when spray--running (spray-start/stop -1))
189 (widen)
190 (skip-chars-forward "\s\t\n")
191 (spray--word-at-point))
192
193 (defun spray-backward-word ()
194 (interactive)
195 (when spray--running (spray-start/stop -1))
196 (widen)
197 (skip-chars-backward "^\s\t\n")
198 (skip-chars-backward "\s\t\n")
199 (spray--word-at-point))
200
201 ;; * provide
202
203 (provide 'spray)
204
205 ;;; spray.el ends here