]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
More documentation
[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: ((seq "1.9"))
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 ;; ┌────
28 ;; │ (beacon-mode 1)
29 ;; └────
30 ;;
31 ;; Whenever the window scrolls or the buffer changes a light will shine on
32 ;; top of your cursor so you know where it is.
33 ;;
34 ;; That’s it.
35 ;;
36 ;;
37 ;; 1 Customizations
38 ;; ════════════════
39 ;;
40 ;; • To customize the appearance of the beacon, configure `beacon-size'
41 ;; and `beacon-color'.
42 ;;
43 ;; • To customize how long it lasts, configure `beacon-blink-duration'
44 ;; and `beacon-blink-delay'.
45 ;;
46 ;; • To customize /when/ it is used at all, configure
47 ;; `beacon-blink-when-window-scrolls',
48 ;; `beacon-blink-when-buffer-changes', and
49 ;; `beacon-blink-when-point-moves'.
50
51 ;;; Code:
52
53 (require 'seq)
54
55 (defgroup beacon nil
56 "Customization group for beacon."
57 :group 'emacs
58 :prefix "beacon-")
59
60 (defvar beacon--timer nil)
61
62 (defcustom beacon-push-mark nil
63 "Should the mark be pushed before long movements?
64 If nil, `beacon' will not push the mark.
65 Otherwise this should be a number, and `beacon' will push the
66 mark whenever point moves more than that many lines."
67 :type '(choice integer (const nil)))
68
69 (defcustom beacon-blink-when-point-moves nil
70 "Should the beacon blink when moving a long distance?
71 If nil, don't blink due to plain movement.
72 If non-nil, this should be an integer, which is the minimum
73 movement distance (in lines) that triggers a beacon blink."
74 :type '(choice integer (const nil)))
75
76 (defcustom beacon-blink-when-buffer-changes t
77 "Should the beacon blink when changing buffer?"
78 :type 'boolean)
79
80 (defcustom beacon-blink-when-window-scrolls t
81 "Should the beacon blink when the window scrolls?"
82 :type 'boolean)
83
84 (defcustom beacon-blink-duration 0.3
85 "Time, in seconds, that the blink should last."
86 :type 'number)
87
88 (defcustom beacon-blink-delay 0.3
89 "Time, in seconds, before starting to fade the beacon."
90 :type 'number)
91
92 (defcustom beacon-size 40
93 "Size of the beacon in characters."
94 :type 'number)
95
96 (defcustom beacon-color 0.5
97 "Color of the beacon.
98 This can be a string or a number.
99
100 If it is a number, the color is taken to be white or
101 black (depending on the current theme's background) and this
102 number is a float between 0 and 1 specifing the brightness.
103
104 If it is a string, it is a color name or specification,
105 e.g. \"#666600\"."
106 :type '(choice number color))
107
108 \f
109 ;;; Overlays
110 (defvar beacon--ovs nil)
111
112 (defun beacon--colored-overlay (color)
113 "Put an overlay at point with background COLOR."
114 (let ((ov (make-overlay (point) (1+ (point)))))
115 (overlay-put ov 'face (list :background color))
116 (overlay-put ov 'beacon t)
117 (push ov beacon--ovs)))
118
119 (defun beacon--ov-put-after-string (overlay colors)
120 "Add an after-string property to OVERLAY.
121 The property's value is a string of spaces with background
122 COLORS applied to each one."
123 (if (not colors)
124 (delete-overlay overlay)
125 (overlay-put overlay 'beacon-colors colors)
126 (overlay-put overlay 'after-string
127 (propertize
128 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
129 colors
130 "")
131 'cursor 1000))))
132
133 (defun beacon--after-string-overlay (colors)
134 "Put an overlay at point with an after-string property.
135 The property's value is a string of spaces with background
136 COLORS applied to each one."
137 (let ((ov (make-overlay (point) (point)))
138 ;; The after-string must not be longer than the remaining columns from
139 ;; point to right window-end else it will be wrapped around (assuming
140 ;; truncate-lines is nil) introducing an ugly wrap-around for a
141 ;; fraction of a second.
142 (colors (seq-take colors (- (window-width) (current-column)))))
143 (beacon--ov-put-after-string ov colors)
144 (overlay-put ov 'beacon t)
145 (push ov beacon--ovs)))
146
147 (defun beacon--ov-at-point ()
148 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
149 (overlays-in (point) (point)))
150 (seq-filter (lambda (o) (overlay-get o 'beacon))
151 (overlays-at (point))))))
152
153 (defun beacon--vanish ()
154 "Turn off the beacon."
155 (when (timerp beacon--timer)
156 (cancel-timer beacon--timer))
157 (mapc #'delete-overlay beacon--ovs)
158 (setq beacon--ovs nil))
159
160 \f
161 ;;; Colors
162 (defun beacon--int-range (a b)
163 "Return a list of integers between A inclusive and B exclusive.
164 Only returns `beacon-size' elements."
165 (let ((d (/ (- b a) beacon-size))
166 (out (list a)))
167 (dotimes (_ (1- beacon-size))
168 (push (+ (car out) d) out))
169 (nreverse out)))
170
171 (defun beacon--color-range ()
172 "Return a list of background colors for the beacon."
173 (let* ((bg (color-values (face-attribute 'default :background)))
174 (fg (cond
175 ((stringp beacon-color) (color-values beacon-color))
176 ((< (color-distance "black" bg)
177 (color-distance "white" bg))
178 (make-list 3 (* beacon-color 65535)))
179 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
180 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
181 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
182 [0 1 2]))))
183
184 \f
185 ;;; Blinking
186 (defun beacon--shine ()
187 "Shine a beacon at point."
188 (let ((colors (beacon--color-range)))
189 (save-excursion
190 (while colors
191 (if (looking-at "$")
192 (progn
193 (beacon--after-string-overlay colors)
194 (setq colors nil))
195 (beacon--colored-overlay (pop colors))
196 (forward-char 1))))))
197
198 (defun beacon--dec ()
199 "Decrease the beacon brightness by one."
200 (pcase (beacon--ov-at-point)
201 (`nil (beacon--vanish))
202 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
203 (beacon--ov-put-after-string o (cdr c)))
204 (o
205 (delete-overlay o)
206 (save-excursion
207 (while (progn (forward-char 1)
208 (setq o (beacon--ov-at-point)))
209 (let ((colors (overlay-get o 'beacon-colors)))
210 (if (not colors)
211 (move-overlay o (1- (point)) (point))
212 (forward-char -1)
213 (beacon--colored-overlay (pop colors))
214 (beacon--ov-put-after-string o colors)
215 (forward-char 1))))))))
216
217 (defun beacon-blink ()
218 "Blink the beacon at the position of the cursor."
219 (interactive)
220 (beacon--vanish)
221 (beacon--shine)
222 (setq beacon--timer
223 (run-at-time beacon-blink-delay
224 (/ beacon-blink-duration 1.0 beacon-size)
225 #'beacon--dec)))
226
227 \f
228 ;;; Movement detection
229 (defvar beacon--window-scrolled nil)
230 (defvar beacon--previous-place nil)
231 (defvar beacon--previous-mark-head nil)
232
233 (defun beacon--movement-> (delta)
234 "Return non-nil if latest point movement is > DELTA.
235 If DELTA is nil, return nil."
236 (and delta
237 (markerp beacon--previous-place)
238 (equal (marker-buffer beacon--previous-place)
239 (current-buffer))
240 (> (abs (- (point) beacon--previous-place))
241 delta)
242 (> (count-screen-lines (min (point) beacon--previous-place)
243 (max (point) beacon--previous-place))
244 delta)))
245
246 (defun beacon--maybe-push-mark ()
247 "Push mark if it seems to be safe."
248 (when (and (not mark-active)
249 (beacon--movement-> beacon-push-mark))
250 (let ((head (car mark-ring)))
251 (when (and (eq beacon--previous-mark-head head)
252 (not (equal head beacon--previous-place)))
253 (push-mark beacon--previous-place)))))
254
255 (defun beacon--post-command ()
256 "Blink if point moved very far."
257 (cond
258 ((not (markerp beacon--previous-place))
259 (beacon--vanish))
260 ;; Blink because we changed buffer.
261 ((not (equal (marker-buffer beacon--previous-place)
262 (current-buffer)))
263 (when beacon-blink-when-buffer-changes
264 (unless (window-minibuffer-p)
265 (beacon-blink))))
266 ;; Blink for scrolling.
267 ((and beacon-blink-when-window-scrolls
268 beacon--window-scrolled
269 (equal beacon--window-scrolled (selected-window)))
270 (beacon-blink)
271 (setq beacon--window-scrolled nil))
272 ;; Blink for movement
273 ((beacon--movement-> beacon-blink-when-point-moves)
274 (beacon-blink))
275 ;; Even if we don't blink, vanish any previous beacon.
276 (t (beacon--vanish)))
277 (beacon--maybe-push-mark)
278 (unless (window-minibuffer-p)
279 (setq beacon--previous-mark-head (car mark-ring))
280 (setq beacon--previous-place (point-marker))))
281
282 (defun beacon--window-scroll-function (win _start-pos)
283 "Blink the beacon or record that window has been scrolled.
284 If invoked during the command loop, record the current window so
285 that it may be blinked on post-command. This is because the
286 scrolled window might not be active, but we only know that at
287 `post-command-hook'.
288
289 If invoked outside the command loop, `post-command-hook' would be
290 unreliable, so just blink immediately."
291 (if this-command
292 (setq beacon--window-scrolled win)
293 (beacon-blink)))
294
295 \f
296 ;;; Minor-mode
297 (defcustom beacon-lighter
298 (cond
299 ((char-displayable-p ?💡) " 💡")
300 ((char-displayable-p ?Λ) " Λ")
301 (t " *"))
302 "Lighter string used on the mode-line."
303 :type 'string)
304
305 ;;;###autoload
306 (define-minor-mode beacon-mode
307 nil nil beacon-lighter nil
308 :global t
309 (if beacon-mode
310 (progn
311 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
312 (add-hook 'post-command-hook #'beacon--post-command))
313 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
314 (remove-hook 'post-command-hook #'beacon--post-command)))
315
316 (provide 'beacon)
317 ;;; beacon.el ends here