+;;; cedit.el --- a speed reading mode
+
+;; Copyright (C) 2014 zk_phi
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program; if not, write to the Free Software
+;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+;; Author: zk_phi
+;; URL: http://hins11.yu-yake.com/
+;; Author: Ian Kelling <ian@iankelling.org>
+;; Version: 0.0.1
+
+;;; Commentary:
+
+;; Put this script into a "load-path"ed directory, and load it in your
+;; init file.
+;;
+;; (require 'spray)
+;;
+;; Then you may run spray with "M-x spray-mode". Binding some keys may
+;; also be useful.
+;;
+;; (global-set-key (kbd "<f6>") 'spray-mode)
+;;
+;; For more informations, see Readme.org.
+
+;;; Change Log:
+;; 0.0.0 test release
+;; 0.0.1 add spray-set-margins
+
+;;; Code:
+
(require 'face-remap)
-;; custom
+;; * customizable vars
+
(defvar spray-wpm 400 "words/min")
(defvar spray-height 400 "height of characters")
+(defvar spray-margin-top 1 "character margin at top of buffer. Characters are as big as spray text characters.")
+(defvar spray-margin-left 0 "character margin at left of buffer. Characters are as big as spray text characters.")
+
(defvar spray-mode-map
(let ((km (make-sparse-keymap)))
(define-key km (kbd "SPC") 'spray-start/stop)
(define-key km (kbd "h") 'spray-backward-word)
- (define-key km (kbd "<left>") 'spray-backward-word)
(define-key km (kbd "l") 'spray-forward-word)
+ (define-key km (kbd "<left>") 'spray-backward-word)
(define-key km (kbd "<right>") 'spray-forward-word)
- km))
+ (define-key km (kbd "f") 'spray-faster)
+ (define-key km (kbd "q") 'spray-quit)
+ (define-key km (kbd "<return>") 'spray-quit)
+ km)
+ "keymap for spray-mode buffers")
+
+;; * faces
(make-face 'spray-base-face)
(set-face-attribute 'spray-base-face nil
:background (face-background 'default)
- :foreground (face-foreground 'default))
+ :foreground (face-foreground 'default)
+ :slant 'normal)
(make-face 'spray-orp-face)
(set-face-attribute 'spray-orp-face nil
:foreground "red"
:overline (face-foreground 'default)
- :underline (face-foreground 'default))
+ :underline (face-foreground 'default)
+ :slant 'normal)
-;; internal variables
+;; * internal vars
+
+(defvar spray--margin-string ""
+ "Currently not used.")
(defvar spray--base-overlay nil)
(defvar spray--orp-overlay nil)
(defvar spray--running nil)
(defvar spray--saved-buffer-face nil)
(defvar spray--saved-restriction nil)
+;; * utility functions
+
+(defun spray-set-margins ()
+ "Setup spray--margin-string"
+ (setq spray--margin-string
+ (concat (make-string spray-margin-top 10) ;; 10 = ascii newline
+ (make-string spray-margin-left 32)))) ;; 32 = ascii space
+
+;; * the mode
+
+;;;###autoload
(define-minor-mode spray-mode
"spray mode"
:init nil
(overlay-put spray--orp-overlay 'priority 101)
(overlay-put spray--orp-overlay 'face 'spray-orp-face)
(add-hook 'pre-command-hook 'spray--pre-command-handler)
- (spray-start/stop 1))
+ (spray-start))
(t
(setq cursor-type spray--saved-cursor-type)
(if spray--saved-restriction
(delete-overlay spray--base-overlay)
(delete-overlay spray--orp-overlay)
(remove-hook 'pre-command-hook 'spray--pre-command-handler)
- (spray-start/stop -1))))
+ (spray-stop))))
+
+(defun spray-quit ()
+ "Exit spray mode."
+ (interactive)
+ (spray-mode -1))
(defun spray--pre-command-handler ()
- (unless (memq this-command '(spray-forward-word
- spray-backward-word spray-start/stop))
+ (unless (string-match "^spray-" (symbol-name this-command))
(spray-mode -1)))
(defun spray--word-at-point ()
(t 0))))
(move-overlay spray--orp-overlay (1- orp) orp)
(move-overlay spray--base-overlay beg end)
- (overlay-put spray--base-overlay
- 'before-string (make-string (- 5 (- orp beg)) ?\s))
+ (spray-set-margins)
+ (overlay-put spray--base-overlay 'before-string
+ (concat spray--margin-string
+ (make-string (- 5 (- orp beg)) ?\s)))
(narrow-to-region beg end)))
(defun spray--update ()
(skip-chars-forward "\s\t\n")
(spray--word-at-point)))))
-;; commands
+;; * interactive commands
-(defun spray-start/stop (&optional switch)
+(defun spray-start/stop ()
+ "Toggle pause/unpause spray."
(interactive)
- (cond ((and (memql switch '(nil 1))
- (not spray--running))
- (setq spray--running
- (run-with-timer 0 (/ 60.0 spray-wpm) 'spray--update)))
- ((memql switch '(nil -1))
- (cancel-timer spray--running)
- (setq spray--running nil))
- (t
- nil)))
+ (or (spray-stop) (spray-start)))
+
+(defun spray-stop ()
+ "Pause spray.
+Returns t if spray was unpaused."
+ (interactive)
+ (prog1 spray--running
+ (when spray--running
+ (cancel-timer spray--running)
+ (setq spray--running nil))))
+
+(defun spray-start ()
+ "Start / resume spray."
+ (interactive)
+ (setq spray--running
+ (run-with-timer 0 (/ 60.0 spray-wpm) 'spray--update)))
+
(defun spray-forward-word ()
(interactive)
- (when spray--running (spray-start/stop -1))
+ (spray-stop)
(widen)
(skip-chars-forward "\s\t\n")
(spray--word-at-point))
(defun spray-backward-word ()
(interactive)
- (when spray--running (spray-start/stop -1))
+ (spray-stop)
(widen)
(skip-chars-backward "^\s\t\n")
(skip-chars-backward "\s\t\n")
(spray--word-at-point))
+(defun spray-faster ()
+ "Increases speed.
+
+Increases the wpm (words per minute) parameter. See the variable
+`spray-wmp'."
+ (interactive)
+ (spray-inc-wpm 20))
+
+(defun spray-slower ()
+ "Decreases speed.
+
+Decreases the wpm (words per minute) parameter. See the variable
+`spray-wmp'."
+ (interactive)
+ (spray-inc-wpm -20))
+
+(defun spray-inc-wpm (delta)
+ (let ((was-running spray--running))
+ (spray-stop)
+ (when (< 10 (+ spray-wpm delta))
+ (setq spray-wpm (+ spray-wpm delta)))
+ (and was-running (spray-backward-word))
+ (message "spray wpm: %d" spray-wpm)
+ (when was-running
+ (spray-start))))
+
+;; * provide
+
(provide 'spray)
+
+;;; spray.el ends here