1 ;;; beacon.el --- Highlight the cursor whenever the window scrolls -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/beacon
7 ;; Keywords: convenience
9 ;; Package-Requires: ((seq "1.9"))
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.
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.
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/>.
26 ;; This is a global minor-mode. Turn it on everywhere with:
31 ;; Whenever the window scrolls a light will shine on top of your cursor so
32 ;; you know where it is.
36 ;; See the accompanying Readme.org for configuration details.
43 "Customization group for beacon."
47 (defvar beacon--timer nil)
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)))
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)))
63 (defcustom beacon-blink-when-buffer-changes t
64 "Should the beacon blink when changing buffer?"
67 (defcustom beacon-blink-when-window-scrolls t
68 "Should the beacon blink when the window scrolls?"
71 (defcustom beacon-blink-when-window-changes t
72 "Should the beacon blink when the window changes?"
75 (defcustom beacon-blink-duration 0.3
76 "Time, in seconds, that the blink should last."
79 (defcustom beacon-blink-delay 0.3
80 "Time, in seconds, before starting to fade the beacon."
83 (defcustom beacon-size 40
84 "Size of the beacon in characters."
87 (defcustom beacon-color 0.5
89 This can be a string or a number.
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.
95 If it is a string, it is a color name or specification,
97 :type '(choice number color))
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."
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)
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
114 :type '(repeat symbol))
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
121 :type '(repeat symbol))
125 (defvar beacon--ovs nil)
127 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
128 "Priotiy used on all of our overlays.")
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))
139 (overlay-put ov (pop properties) (pop properties)))
140 (push ov beacon--ovs)
143 (defun beacon--colored-overlay (color)
144 "Put an overlay at point with background COLOR."
145 (beacon--make-overlay 1 'face (list :background color)))
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!"
153 (when (overlayp overlay)
154 (delete-overlay overlay))
155 (overlay-put overlay 'beacon-colors colors)
156 (overlay-put overlay 'after-string
158 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
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)))
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))))))
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))
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))
192 (dotimes (_ (1- beacon-size))
193 (push (+ (car out) d) out))
196 (defun beacon--color-range ()
197 "Return a list of background colors for the beacon."
198 (let* ((bg (color-values (face-attribute 'default :background)))
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))))
211 (defun beacon--shine ()
212 "Shine a beacon at point."
213 (let ((colors (beacon--color-range)))
218 (beacon--after-string-overlay colors)
220 (beacon--colored-overlay (pop colors))
221 (forward-char 1))))))
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)))
232 (while (progn (forward-char 1)
233 (setq o (beacon--ov-at-point)))
234 (let ((colors (overlay-get o 'beacon-colors)))
236 (move-overlay o (1- (point)) (point))
238 (beacon--colored-overlay (pop colors))
239 (beacon--ov-put-after-string o colors)
240 (forward-char 1))))))))
242 (defun beacon-blink ()
243 "Blink the beacon at the position of the cursor."
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))
252 (run-at-time beacon-blink-delay
253 (/ beacon-blink-duration 1.0 beacon-size)
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)
263 (defun beacon--movement-> (delta)
264 "Return non-nil if latest point movement is > DELTA.
265 If DELTA is nil, return nil."
267 (markerp beacon--previous-place)
268 (equal (marker-buffer beacon--previous-place)
270 (> (abs (- (point) beacon--previous-place))
272 (> (count-screen-lines (min (point) beacon--previous-place)
273 (max (point) beacon--previous-place))
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)))))
285 (defun beacon--post-command ()
286 "Blink if point moved very far."
288 ((not (markerp beacon--previous-place))
290 ;; Blink for switching windows.
291 ((and beacon-blink-when-window-changes
292 (not (eq beacon--previous-window (selected-window))))
294 ;; Blink for scrolling.
295 ((and beacon-blink-when-window-scrolls
296 beacon--window-scrolled
297 (equal beacon--window-scrolled (selected-window)))
299 ;; Blink for movement
300 ((beacon--movement-> beacon-blink-when-point-moves)
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))))
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
318 If invoked outside the command loop, `post-command-hook' would be
319 unreliable, so just blink immediately."
321 (setq beacon--window-scrolled win)
322 (setq beacon--window-scrolled nil)
327 (defcustom beacon-lighter
329 ((char-displayable-p ?💡) " 💡")
330 ((char-displayable-p ?Λ) " Λ")
332 "Lighter string used on the mode-line."
336 (define-minor-mode beacon-mode
337 nil nil beacon-lighter nil
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)))
349 ;;; beacon.el ends here