]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Fix an error
[gnu-emacs-elpa] / beacon.el
1 ;;; beacon.el --- Highlight the cursor whenever it moves long distances -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Artur Malabarba
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/beacon
7 ;; Keywords: convenience
8 ;; Version: 0.1
9 ;; Package-Requires: ((cl-lib "0.5"))
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This is a global minor-mode. Turn it on everywhere with
27 ;; (beacon-mode 1)
28
29 ;;; Code:
30
31 (require 'cl-lib)
32
33 (defgroup beacon nil
34 "Customization group for beacon."
35 :group 'emacs
36 :prefix "beacon-")
37
38 (defvar beacon--timer nil)
39
40 (defcustom beacon-push-mark nil
41 "Should the mark be pushed before long movements?
42 If nil, `beacon' will not push the mark.
43 Otherwise this should be a number, and `beacon' will push the
44 mark whenever point moves more than that many lines."
45 :type '(choice integer (const nil)))
46
47 (defcustom beacon-blink-when-point-moves nil
48 "Should the beacon blink when moving a long distance?
49 If nil, don't blink due to plain movement.
50 If non-nil, this should be an integer, which is the minimum
51 movement distance (in lines) that triggers a beacon blink."
52 :type '(choice integer (const nil)))
53
54 (defcustom beacon-blink-when-buffer-changes t
55 "Should the beacon blink when changing buffer?"
56 :type 'boolean)
57
58 (defcustom beacon-blink-when-window-scrolls t
59 "Should the beacon blink when the window scrolls?"
60 :type 'boolean)
61
62 (defcustom beacon-blink-duration 0.3
63 "Time, in seconds, that the blink should last."
64 :type 'number)
65
66 (defcustom beacon-blink-delay 0.3
67 "Time, in seconds, before starting to fade the beacon."
68 :type 'number)
69
70 (defcustom beacon-size 40
71 "Size of the beacon in characters."
72 :type 'number)
73
74 (defcustom beacon-color 0.5
75 "Color of the beacon.
76 This can be a string or a number.
77
78 If it is a number, the color is taken to be white or
79 black (depending on the current theme's background) and this
80 number is a float between 0 and 1 specifing the brightness.
81
82 If it is a string, it is a color name or specification,
83 e.g. \"#666600\"."
84 :type '(choice number color))
85
86 \f
87 ;;; Overlays
88 (defvar beacon--ovs nil)
89
90 (defun beacon--colored-overlay (color)
91 "Put an overlay at point with background COLOR."
92 (let ((ov (make-overlay (point) (1+ (point)))))
93 (overlay-put ov 'face (list :background color))
94 (overlay-put ov 'beacon t)
95 (push ov beacon--ovs)))
96
97 (defun beacon--ov-put-after-string (overlay colors)
98 "Add an after-string property to OVERLAY.
99 The property's value is a string of spaces with background
100 COLORS applied to each one."
101 (overlay-put overlay 'beacon-colors colors)
102 (overlay-put overlay 'after-string
103 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
104 colors
105 "")))
106
107 (defun beacon--after-string-overlay (colors)
108 "Put an overlay at point with an after-string property.
109 The property's value is a string of spaces with background
110 COLORS applied to each one."
111 (let ((ov (make-overlay (point) (point))))
112 (beacon--ov-put-after-string ov colors)
113 (overlay-put ov 'beacon t)
114 (push ov beacon--ovs)))
115
116 (defun beacon--ov-at-point ()
117 (car (cl-member-if (lambda (o) (overlay-get o 'beacon))
118 (overlays-at (point)))))
119
120 (defun beacon--vanish ()
121 "Turn off the beacon."
122 (when (timerp beacon--timer)
123 (cancel-timer beacon--timer))
124 (mapc #'delete-overlay beacon--ovs)
125 (setq beacon--ovs nil))
126
127 \f
128 ;;; Colors
129 (defun beacon--int-range (a b)
130 "Return a list of integers between A inclusive and B exclusive.
131 Only returns `beacon-size' elements."
132 (let ((d (/ (- b a) beacon-size))
133 (out (list a)))
134 (dotimes (_ (1- beacon-size))
135 (push (+ (car out) d) out))
136 (nreverse out)))
137
138 (defun beacon--color-range ()
139 "Return a list of background colors for the beacon."
140 (let* ((bg (color-values (face-attribute 'default :background)))
141 (fg (cond
142 ((stringp beacon-color) (color-values beacon-color))
143 ((< (color-distance "black" bg)
144 (color-distance "white" bg))
145 (make-list 3 (* beacon-color 65535)))
146 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
147 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
148 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
149 [0 1 2]))))
150
151 \f
152 ;;; Blinking
153 (defun beacon--shine ()
154 "Shine a beacon at point."
155 (let ((colors (beacon--color-range)))
156 (save-excursion
157 (while colors
158 (if (looking-at "$")
159 (progn
160 (beacon--after-string-overlay colors)
161 (setq colors nil))
162 (beacon--colored-overlay (pop colors))
163 (forward-char 1))))))
164
165 (defun beacon--dec ()
166 "Decrease the beacon brightness by one."
167 (let ((o (beacon--ov-at-point)))
168 (if (not o)
169 (beacon--vanish)
170 (delete-overlay o)
171 (save-excursion
172 (while (progn (forward-char 1)
173 (setq o (beacon--ov-at-point)))
174 (let ((colors (overlay-get o 'beacon-colors)))
175 (if (not colors)
176 (move-overlay o (1- (point)) (point))
177 (forward-char -1)
178 (beacon--colored-overlay (pop colors))
179 (beacon--ov-put-after-string o colors))))))))
180
181 (defun beacon-blink ()
182 "Blink the beacon at the position of the cursor."
183 (interactive)
184 (beacon--vanish)
185 (beacon--shine)
186 (setq beacon--timer
187 (run-at-time beacon-blink-delay
188 (/ beacon-blink-duration 1.0 beacon-size)
189 #'beacon--dec)))
190
191 \f
192 ;;; Movement detection
193 (defvar beacon--previous-place nil)
194 (defvar beacon--previous-window-start nil)
195 (defvar beacon--previous-mark-head nil)
196
197 (defun beacon--movement-> (delta)
198 "Return non-nil if latest point movement is > DELTA.
199 If DELTA is nil, return nil."
200 (and delta
201 (markerp beacon--previous-place)
202 (equal (marker-buffer beacon--previous-place)
203 (current-buffer))
204 (> (abs (- (point) beacon--previous-place))
205 delta)
206 (> (count-screen-lines (min (point) beacon--previous-place)
207 (max (point) beacon--previous-place))
208 delta)))
209
210 (defun beacon--maybe-push-mark ()
211 "Push mark if it seems to be safe."
212 (when (and (not mark-active)
213 (beacon--movement-> beacon-push-mark))
214 (let ((head (car mark-ring)))
215 (when (and (eq beacon--previous-mark-head head)
216 (not (equal head beacon--previous-place)))
217 (push-mark beacon--previous-place)))))
218
219 (defun beacon--post-command ()
220 "Blink if point moved very far."
221 (cond
222 ((not (markerp beacon--previous-place))
223 (beacon--vanish))
224 ;; Blink because we changed buffer.
225 ((not (equal (marker-buffer beacon--previous-place)
226 (current-buffer)))
227 (when beacon-blink-when-buffer-changes
228 (unless (window-minibuffer-p)
229 (beacon-blink))))
230 ;; Blink for scrolling.
231 ((and beacon-blink-when-window-scrolls
232 (progn (redisplay)
233 (not (equal beacon--previous-window-start (window-start)))))
234 (beacon-blink))
235 ;; Blink for movement
236 ((beacon--movement-> beacon-blink-when-point-moves)
237 (beacon-blink))
238 ;; Even if we don't blink, vanish any previous beacon.
239 (t (beacon--vanish)))
240 (beacon--maybe-push-mark)
241 (unless (window-minibuffer-p)
242 (setq beacon--previous-window-start (window-start))
243 (setq beacon--previous-mark-head (car mark-ring))
244 (setq beacon--previous-place (point-marker))))
245
246 \f
247 ;;; Minor-mode
248 (defcustom beacon-lighter (cond
249 ((char-displayable-p ?💡) "💡")
250 ((char-displayable-p ?Λ) "Λ")
251 (t "*"))
252 "Lighter string used on the mode-line."
253 :type 'string)
254
255 ;;;###autoload
256 (define-minor-mode beacon-mode
257 nil nil beacon-lighter nil
258 :global t
259 (if beacon-mode
260 (add-hook 'post-command-hook #'beacon--post-command)
261 (remove-hook 'post-command-hook #'beacon--post-command)))
262
263 (provide 'beacon)
264 ;;; beacon.el ends here