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