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