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 35
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-when-focused nil
76 "Should the beacon blink when Emacs gains focus?
77 Note that, due to a limitation of `focus-in-hook', this might
78 trigger false positives on some systems."
80 :package-version '(beacon . "0.2"))
82 (defcustom beacon-blink-duration 0.3
83 "Time, in seconds, that the blink should last."
86 (defcustom beacon-blink-delay 0.3
87 "Time, in seconds, before starting to fade the beacon."
90 (defcustom beacon-size 40
91 "Size of the beacon in characters."
94 (defcustom beacon-color 0.5
96 This can be a string or a number.
98 If it is a number, the color is taken to be white or
99 black (depending on the current theme's background) and this
100 number is a float between 0 and 1 specifing the brightness.
102 If it is a string, it is a color name or specification,
104 :type '(choice number color))
106 (defvar beacon-dont-blink-predicates nil
107 "A list of predicates that prevent the beacon blink.
108 These predicate functions are called in order, with no
109 arguments, before blinking the beacon. If any returns
110 non-nil, the beacon will not blink.
112 For instance, if you want to disable beacon on buffers where
113 `hl-line-mode' is on, you can do:
115 (add-hook \\='beacon-dont-blink-predicates
116 (lambda () (bound-and-true-p hl-line-mode)))")
118 (add-hook 'beacon-dont-blink-predicates #'window-minibuffer-p)
120 (defcustom beacon-dont-blink-major-modes '(magit-status-mode)
121 "A list of major-modes where the beacon won't blink.
122 Whenever the current buffer satisfies `derived-mode-p' for
123 one of the major-modes on this list, the beacon will not
125 :type '(repeat symbol))
127 (defcustom beacon-dont-blink-commands '(recenter-top-bottom)
128 "A list of commands that should not make the beacon blink.
129 Use this for commands that scroll the window in very
130 predictable ways, when the blink would be more distracting
132 :type '(repeat symbol))
136 (defvar beacon--ovs nil)
138 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
139 "Priotiy used on all of our overlays.")
141 (defun beacon--make-overlay (length &rest properties)
142 "Put an overlay at point with background COLOR."
143 (let ((ov (make-overlay (point) (+ length (point)))))
144 (overlay-put ov 'beacon t)
145 ;; Our overlay is very temporary, so we take the liberty of giving
146 ;; it a high priority.
147 (overlay-put ov 'priority beacon-overlay-priority)
148 (overlay-put ov 'window (selected-window))
150 (overlay-put ov (pop properties) (pop properties)))
151 (push ov beacon--ovs)
154 (defun beacon--colored-overlay (color)
155 "Put an overlay at point with background COLOR."
156 (beacon--make-overlay 1 'face (list :background color)))
158 (defun beacon--ov-put-after-string (overlay colors)
159 "Add an after-string property to OVERLAY.
160 The property's value is a string of spaces with background
161 COLORS applied to each one.
162 If COLORS is nil, OVERLAY is deleted!"
164 (when (overlayp overlay)
165 (delete-overlay overlay))
166 (overlay-put overlay 'beacon-colors colors)
167 (overlay-put overlay 'after-string
169 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
174 (defun beacon--after-string-overlay (colors)
175 "Put an overlay at point with an after-string property.
176 The property's value is a string of spaces with background
177 COLORS applied to each one."
178 ;; The after-string must not be longer than the remaining columns
179 ;; from point to right window-end else it will be wrapped around.
180 (let ((colors (seq-take colors (- (window-width) (current-column)))))
181 (beacon--ov-put-after-string (beacon--make-overlay 0) colors)))
183 (defun beacon--ov-at-point ()
184 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
185 (overlays-in (point) (point)))
186 (seq-filter (lambda (o) (overlay-get o 'beacon))
187 (overlays-at (point))))))
189 (defun beacon--vanish ()
190 "Turn off the beacon."
191 (when (timerp beacon--timer)
192 (cancel-timer beacon--timer))
193 (mapc #'delete-overlay beacon--ovs)
194 (setq beacon--ovs nil))
198 (defun beacon--int-range (a b)
199 "Return a list of integers between A inclusive and B exclusive.
200 Only returns `beacon-size' elements."
201 (let ((d (/ (- b a) beacon-size))
203 (dotimes (_ (1- beacon-size))
204 (push (+ (car out) d) out))
207 (defun beacon--color-range ()
208 "Return a list of background colors for the beacon."
209 (let* ((bg (color-values (face-attribute 'default :background)))
211 ((stringp beacon-color) (color-values beacon-color))
212 ((< (color-distance "black" bg)
213 (color-distance "white" bg))
214 (make-list 3 (* beacon-color 65535)))
215 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
216 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
217 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
222 (defun beacon--shine ()
223 "Shine a beacon at point."
224 (let ((colors (beacon--color-range)))
229 (beacon--after-string-overlay colors)
231 (beacon--colored-overlay (pop colors))
232 (forward-char 1))))))
234 (defun beacon--dec ()
235 "Decrease the beacon brightness by one."
236 (pcase (beacon--ov-at-point)
237 (`nil (beacon--vanish))
238 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
239 (beacon--ov-put-after-string o (cdr c)))
243 (while (progn (forward-char 1)
244 (setq o (beacon--ov-at-point)))
245 (let ((colors (overlay-get o 'beacon-colors)))
247 (move-overlay o (1- (point)) (point))
249 (beacon--colored-overlay (pop colors))
250 (beacon--ov-put-after-string o colors)
251 (forward-char 1))))))))
253 (defun beacon-blink ()
254 "Blink the beacon at the position of the cursor."
257 (unless (or (not beacon-mode)
258 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
259 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
260 (memq (or this-command last-command) beacon-dont-blink-commands))
263 (run-at-time beacon-blink-delay
264 (/ beacon-blink-duration 1.0 beacon-size)
268 ;;; Movement detection
269 (defvar beacon--window-scrolled nil)
270 (defvar beacon--previous-place nil)
271 (defvar beacon--previous-mark-head nil)
272 (defvar beacon--previous-window nil)
274 (defun beacon--movement-> (delta)
275 "Return non-nil if latest point movement is > DELTA.
276 If DELTA is nil, return nil."
278 (markerp beacon--previous-place)
279 (equal (marker-buffer beacon--previous-place)
281 ;; Quick check that prevents running the code below in very
282 ;; short movements (like typing).
283 (> (abs (- (point) beacon--previous-place))
285 ;; Check if the movement was >= DELTA lines by moving DELTA
286 ;; lines. `count-screen-lines' is too slow if the movement had
287 ;; thousands of lines.
290 (goto-char (min beacon--previous-place p))
291 (vertical-motion delta)
292 (> (max p beacon--previous-place)
293 (line-beginning-position))))))
295 (defun beacon--maybe-push-mark ()
296 "Push mark if it seems to be safe."
297 (when (and (not mark-active)
298 (beacon--movement-> beacon-push-mark))
299 (let ((head (car mark-ring)))
300 (when (and (eq beacon--previous-mark-head head)
301 (not (equal head beacon--previous-place)))
302 (push-mark beacon--previous-place)))))
304 (defun beacon--post-command ()
305 "Blink if point moved very far."
307 ((not (markerp beacon--previous-place))
309 ;; Blink for switching windows.
310 ((and beacon-blink-when-window-changes
311 (not (eq beacon--previous-window (selected-window))))
313 ;; Blink for scrolling.
314 ((and beacon-blink-when-window-scrolls
315 beacon--window-scrolled
316 (equal beacon--window-scrolled (selected-window)))
318 ;; Blink for movement
319 ((beacon--movement-> beacon-blink-when-point-moves)
321 ;; Even if we don't blink, vanish any previous beacon.
322 (t (beacon--vanish)))
323 (beacon--maybe-push-mark)
324 (setq beacon--window-scrolled nil)
325 (unless (window-minibuffer-p)
326 (setq beacon--previous-mark-head (car mark-ring))
327 (setq beacon--previous-place (point-marker))
328 (setq beacon--previous-window (selected-window))))
330 (defun beacon--window-scroll-function (win _start-pos)
331 "Blink the beacon or record that window has been scrolled.
332 If invoked during the command loop, record the current window so
333 that it may be blinked on post-command. This is because the
334 scrolled window might not be active, but we only know that at
337 If invoked outside the command loop, `post-command-hook' would be
338 unreliable, so just blink immediately."
340 (setq beacon--window-scrolled win)
341 (setq beacon--window-scrolled nil)
344 (defun beacon--blink-on-focus ()
345 "Blink if `beacon-blink-when-focused' is non-nil"
346 (when beacon-blink-when-focused
351 (defcustom beacon-lighter
353 ((char-displayable-p ?💡) " 💡")
354 ((char-displayable-p ?Λ) " Λ")
356 "Lighter string used on the mode-line."
360 (define-minor-mode beacon-mode
361 nil nil beacon-lighter nil
365 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
366 (add-hook 'focus-in-hook #'beacon--blink-on-focus)
367 (add-hook 'post-command-hook #'beacon--post-command)
368 (add-hook 'pre-command-hook #'beacon--vanish))
369 (remove-hook 'focus-in-hook #'beacon--blink-on-focus)
370 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
371 (remove-hook 'post-command-hook #'beacon--post-command)
372 (remove-hook 'pre-command-hook #'beacon--vanish)))
375 ;;; beacon.el ends here
378 ;; indent-tabs-mode: nil