Make font non-italicized by default
[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 :slant 'normal)
67
68 (make-face 'spray-orp-face)
69 (set-face-attribute 'spray-orp-face nil
70 :foreground "red"
71 :overline (face-foreground 'default)
72 :underline (face-foreground 'default)
73 :slant 'normal)
74
75 ;; * internal vars
76
77 (defvar spray--margin-string "")
78 (defvar spray--base-overlay nil)
79 (defvar spray--orp-overlay nil)
80 (defvar spray--running nil)
81 (defvar spray--delay 0)
82 (defvar spray--saved-cursor-type nil)
83 (defvar spray--saved-buffer-face nil)
84 (defvar spray--saved-restriction nil)
85
86 ;; * utility functions
87
88 (defun spray-set-margins (left above)
89 "add margins before/above the spray text. each arguments can be
90 an integer or a float value."
91 (setq spray--margin-string
92 (propertize " " 'display `((space-width ,left) (height ,(1+ above))))))
93
94 ;; * the mode
95
96 ;;;###autoload
97 (define-minor-mode spray-mode
98 "spray mode"
99 :init nil
100 :keymap spray-mode-map
101 (cond (spray-mode
102 (setq spray--base-overlay (make-overlay (point-min) (point-max))
103 spray--orp-overlay (make-overlay 0 0)
104 spray--saved-cursor-type cursor-type
105 spray--saved-restriction (and (buffer-narrowed-p)
106 (cons (point-min) (point-max)))
107 spray--saved-buffer-face (and (boundp 'buffer-face-mode)
108 buffer-face-mode
109 buffer-face-mode-face))
110 (setq cursor-type nil)
111 (let ((buffer-face-mode-face `(:height ,spray-height)))
112 (buffer-face-mode 1))
113 (overlay-put spray--base-overlay 'priority 100)
114 (overlay-put spray--base-overlay 'face 'spray-base-face)
115 (overlay-put spray--orp-overlay 'priority 101)
116 (overlay-put spray--orp-overlay 'face 'spray-orp-face)
117 (add-hook 'pre-command-hook 'spray--pre-command-handler)
118 (spray-start/stop 1))
119 (t
120 (setq cursor-type spray--saved-cursor-type)
121 (if spray--saved-restriction
122 (narrow-to-region (car spray--saved-restriction)
123 (cdr spray--saved-restriction))
124 (widen))
125 (buffer-face-mode -1)
126 (if spray--saved-buffer-face
127 (let ((buffer-face-mode-face spray--saved-buffer-face))
128 (buffer-face-mode 1)))
129 (delete-overlay spray--base-overlay)
130 (delete-overlay spray--orp-overlay)
131 (remove-hook 'pre-command-hook 'spray--pre-command-handler)
132 (spray-start/stop -1))))
133
134 (defun spray--pre-command-handler ()
135 (unless (string-match "^spray-" (symbol-name this-command))
136 (spray-mode -1)))
137
138 (defun spray--word-at-point ()
139 (skip-chars-backward "^\s\t\n")
140 (let* ((beg (point))
141 (len (skip-chars-forward "^\s\t\n"))
142 (end (point))
143 (orp (+ beg (cl-case len
144 ((1) 1)
145 ((2 3 4 5) 2)
146 ((6 7 8 9) 3)
147 ((10 11 12 13) 4)
148 (t 5)))))
149 (setq spray--delay (+ (if (> len 9) 1 0)
150 (if (looking-at "\n[\s\t\n]") 3 0)
151 (cl-case (char-before)
152 ((?. ?! ?\? ?\;) 3)
153 ((?, ?:) 1)
154 (t 0))))
155 (move-overlay spray--orp-overlay (1- orp) orp)
156 (move-overlay spray--base-overlay beg end)
157 (overlay-put spray--base-overlay 'before-string
158 (concat spray--margin-string
159 (make-string (- 5 (- orp beg)) ?\s)))
160 (narrow-to-region beg end)))
161
162 (defun spray--update ()
163 (cond ((not (zerop spray--delay))
164 (setq spray--delay (1- spray--delay))
165 (when (= spray--delay 2)
166 (narrow-to-region (point) (point))))
167 (t
168 (widen)
169 (if (eobp)
170 (spray-mode -1)
171 (skip-chars-forward "\s\t\n")
172 (spray--word-at-point)))))
173
174 ;; * interactive commands
175
176 (defun spray-start/stop (&optional switch)
177 (interactive)
178 (cond ((and (memql switch '(nil 1))
179 (not spray--running))
180 (setq spray--running
181 (run-with-timer 0 (/ 60.0 spray-wpm) 'spray--update)))
182 ((memql switch '(nil -1))
183 (cancel-timer spray--running)
184 (setq spray--running nil))
185 (t
186 nil)))
187
188 (defun spray-forward-word ()
189 (interactive)
190 (when spray--running (spray-start/stop -1))
191 (widen)
192 (skip-chars-forward "\s\t\n")
193 (spray--word-at-point))
194
195 (defun spray-backward-word ()
196 (interactive)
197 (when spray--running (spray-start/stop -1))
198 (widen)
199 (skip-chars-backward "^\s\t\n")
200 (skip-chars-backward "\s\t\n")
201 (spray--word-at-point))
202
203 ;; * provide
204
205 (provide 'spray)
206
207 ;;; spray.el ends here