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.11"))
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.
44 "Customization group for beacon."
48 (defvar beacon--timer nil)
50 (defcustom beacon-push-mark 35
51 "Should the mark be pushed before long movements?
52 If nil, `beacon' will not push the mark.
53 Otherwise this should be a number, and `beacon' will push the
54 mark whenever point moves more than that many lines."
55 :type '(choice integer (const nil)))
57 (defcustom beacon-blink-when-point-moves-vertically nil
58 "Should the beacon blink when moving a long distance vertically?
59 If nil, don't blink due to vertical movement.
60 If non-nil, this should be an integer, which is the minimum
61 movement distance (in lines) that triggers a beacon blink."
62 :type '(choice integer (const nil)))
64 (defcustom beacon-blink-when-point-moves-horizontally nil
65 "Should the beacon blink when moving a long distance horizontally?
66 If nil, don't blink due to horizontal movement.
67 If non-nil, this should be an integer, which is the minimum
68 movement distance (in columns) that triggers a beacon blink."
69 :type '(choice integer (const nil)))
71 (defcustom beacon-blink-when-buffer-changes t
72 "Should the beacon blink when changing buffer?"
75 (defcustom beacon-blink-when-window-scrolls t
76 "Should the beacon blink when the window scrolls?"
79 (defcustom beacon-blink-when-window-changes t
80 "Should the beacon blink when the window changes?"
83 (defcustom beacon-blink-when-focused nil
84 "Should the beacon blink when Emacs gains focus?
85 Note that, due to a limitation of `focus-in-hook', this might
86 trigger false positives on some systems."
88 :package-version '(beacon . "0.2"))
90 (defcustom beacon-blink-duration 0.3
91 "Time, in seconds, that the blink should last."
94 (defcustom beacon-blink-delay 0.3
95 "Time, in seconds, before starting to fade the beacon."
98 (defcustom beacon-size 40
99 "Size of the beacon in characters."
102 (defcustom beacon-color 0.5
103 "Color of the beacon.
104 This can be a string or a number.
106 If it is a number, the color is taken to be white or
107 black (depending on the current theme's background) and this
108 number is a float between 0 and 1 specifing the brightness.
110 If it is a string, it is a color name or specification,
112 :type '(choice number color))
114 (defface beacon-fallback-background
115 '((((class color) (background light)) (:background "black"))
116 (((class color) (background dark)) (:background "white")))
117 "Fallback beacon background color.
118 Used in cases where the color can't be determined by Emacs.
119 Only the background of this face is used.")
121 (defvar beacon-dont-blink-predicates nil
122 "A list of predicates that prevent the beacon blink.
123 These predicate functions are called in order, with no
124 arguments, before blinking the beacon. If any returns
125 non-nil, the beacon will not blink.
127 For instance, if you want to disable beacon on buffers where
128 `hl-line-mode' is on, you can do:
130 (add-hook \\='beacon-dont-blink-predicates
131 (lambda () (bound-and-true-p hl-line-mode)))")
133 (add-hook 'beacon-dont-blink-predicates #'window-minibuffer-p)
135 (defcustom beacon-dont-blink-major-modes '(magit-status-mode magit-popup-mode)
136 "A list of major-modes where the beacon won't blink.
137 Whenever the current buffer satisfies `derived-mode-p' for
138 one of the major-modes on this list, the beacon will not
140 :type '(repeat symbol))
142 (defcustom beacon-dont-blink-commands '(recenter-top-bottom)
143 "A list of commands that should not make the beacon blink.
144 Use this for commands that scroll the window in very
145 predictable ways, when the blink would be more distracting
147 :type '(repeat symbol))
150 ;;; Internal variables
151 (defvar beacon--window-scrolled nil)
152 (defvar beacon--previous-place nil)
153 (defvar beacon--previous-mark-head nil)
154 (defvar beacon--previous-window nil)
155 (defvar beacon--previous-window-start 0)
157 (defun beacon--record-vars ()
158 (unless (window-minibuffer-p)
159 (setq beacon--previous-mark-head (car mark-ring))
160 (setq beacon--previous-place (point-marker))
161 (setq beacon--previous-window (selected-window))
162 (setq beacon--previous-window-start (window-start))))
166 (defvar beacon--ovs nil)
168 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
169 "Priotiy used on all of our overlays.")
171 (defun beacon--make-overlay (length &rest properties)
172 "Put an overlay at point with background COLOR."
173 (let ((ov (make-overlay (point) (+ length (point)))))
174 (overlay-put ov 'beacon t)
175 ;; Our overlay is very temporary, so we take the liberty of giving
176 ;; it a high priority.
177 (overlay-put ov 'priority beacon-overlay-priority)
178 (overlay-put ov 'window (selected-window))
180 (overlay-put ov (pop properties) (pop properties)))
181 (push ov beacon--ovs)
184 (defun beacon--colored-overlay (color)
185 "Put an overlay at point with background COLOR."
186 (beacon--make-overlay 1 'face (list :background color)))
188 (defun beacon--ov-put-after-string (overlay colors)
189 "Add an after-string property to OVERLAY.
190 The property's value is a string of spaces with background
191 COLORS applied to each one.
192 If COLORS is nil, OVERLAY is deleted!"
194 (when (overlayp overlay)
195 (delete-overlay overlay))
196 (overlay-put overlay 'beacon-colors colors)
197 (overlay-put overlay 'after-string
199 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
204 (defun beacon--after-string-overlay (colors)
205 "Put an overlay at point with an after-string property.
206 The property's value is a string of spaces with background
207 COLORS applied to each one."
208 ;; The after-string must not be longer than the remaining columns
209 ;; from point to right window-end else it will be wrapped around.
210 (let ((colors (seq-take colors (- (window-width) (current-column)))))
211 (beacon--ov-put-after-string (beacon--make-overlay 0) colors)))
213 (defun beacon--ov-at-point ()
214 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
215 (overlays-in (point) (point)))
216 (seq-filter (lambda (o) (overlay-get o 'beacon))
217 (overlays-at (point))))))
219 (defun beacon--vanish ()
220 "Turn off the beacon."
221 (when (timerp beacon--timer)
222 (cancel-timer beacon--timer))
223 (mapc #'delete-overlay beacon--ovs)
224 (setq beacon--ovs nil))
228 (defun beacon--int-range (a b)
229 "Return a list of integers between A inclusive and B exclusive.
230 Only returns `beacon-size' elements."
231 (let ((d (/ (- b a) beacon-size))
233 (dotimes (_ (1- beacon-size))
234 (push (+ (car out) d) out))
237 (defun beacon--color-range ()
238 "Return a list of background colors for the beacon."
239 (let* ((default-bg (or (background-color-at-point)
240 (face-background 'default)))
241 (bg (color-values (if (string-match "\\`unspecified-" default-bg)
242 (face-attribute 'beacon-fallback-background :background)
245 ((stringp beacon-color) (color-values beacon-color))
246 ((< (color-distance "black" bg)
247 (color-distance "white" bg))
248 (make-list 3 (* beacon-color 65535)))
249 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
250 (apply #'seq-mapn (lambda (r g b) (format "#%04x%04x%04x" r g b))
251 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
256 (defun beacon--shine ()
257 "Shine a beacon at point."
258 (let ((colors (beacon--color-range)))
263 (beacon--after-string-overlay colors)
265 (beacon--colored-overlay (pop colors))
266 (forward-char 1))))))
268 (defun beacon--dec ()
269 "Decrease the beacon brightness by one."
270 (pcase (beacon--ov-at-point)
271 (`nil (beacon--vanish))
272 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
273 (beacon--ov-put-after-string o (cdr c)))
277 (while (progn (forward-char 1)
278 (setq o (beacon--ov-at-point)))
279 (let ((colors (overlay-get o 'beacon-colors)))
281 (move-overlay o (1- (point)) (point))
283 (beacon--colored-overlay (pop colors))
284 (beacon--ov-put-after-string o colors)
285 (forward-char 1))))))))
287 (defun beacon-blink ()
288 "Blink the beacon at the position of the cursor."
291 ;; Record vars here in case something is blinking outside the
293 (beacon--record-vars)
294 (unless (or (not beacon-mode)
295 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
296 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
297 (memq (or this-command last-command) beacon-dont-blink-commands))
300 (run-at-time beacon-blink-delay
301 (/ beacon-blink-duration 1.0 beacon-size)
305 ;;; Movement detection
306 (defun beacon--movement-> (delta-y &optional delta-x)
307 "Return non-nil if latest vertical movement is > DELTA-Y.
308 If DELTA-Y is nil, return nil.
309 The same is true for DELTA-X and horizonta movement."
311 (markerp beacon--previous-place)
312 (equal (marker-buffer beacon--previous-place)
314 ;; Quick check that prevents running the code below in very
315 ;; short movements (like typing).
316 (> (abs (- (point) beacon--previous-place))
320 (> (abs (- (current-column)
322 (goto-char beacon--previous-place)
325 ;; Check if the movement was >= DELTA lines by moving DELTA
326 ;; lines. `count-screen-lines' is too slow if the movement had
327 ;; thousands of lines.
330 (goto-char (min beacon--previous-place p))
331 (vertical-motion delta-y)
332 (> (max p beacon--previous-place)
333 (line-beginning-position)))))))
335 (defun beacon--maybe-push-mark ()
336 "Push mark if it seems to be safe."
337 (when (and (not mark-active)
338 (beacon--movement-> beacon-push-mark))
339 (let ((head (car mark-ring)))
340 (when (and (eq beacon--previous-mark-head head)
341 (not (equal head beacon--previous-place)))
342 (push-mark beacon--previous-place 'silent)))))
344 (defun beacon--post-command ()
345 "Blink if point moved very far."
347 ((not (markerp beacon--previous-place))
349 ;; Blink for switching windows.
350 ((and beacon-blink-when-window-changes
351 (not (eq beacon--previous-window (selected-window))))
353 ;; Blink for scrolling.
354 ((and beacon--window-scrolled
355 (equal beacon--window-scrolled (selected-window)))
357 ;; Blink for movement
358 ((beacon--movement-> beacon-blink-when-point-moves-vertically
359 beacon-blink-when-point-moves-horizontally)
361 ;; Even if we don't blink, vanish any previous beacon.
362 (t (beacon--vanish)))
363 (beacon--maybe-push-mark)
364 (setq beacon--window-scrolled nil))
366 (defun beacon--window-scroll-function (win start-pos)
367 "Blink the beacon or record that window has been scrolled.
368 If invoked during the command loop, record the current window so
369 that it may be blinked on post-command. This is because the
370 scrolled window might not be active, but we only know that at
373 If invoked outside the command loop, `post-command-hook' would be
374 unreliable, so just blink immediately."
375 (unless (or (and (equal beacon--previous-window-start start-pos)
376 (equal beacon--previous-window win))
377 (not beacon-blink-when-window-scrolls))
379 (setq beacon--window-scrolled win)
380 (setq beacon--window-scrolled nil)
383 (defun beacon--blink-on-focus ()
384 "Blink if `beacon-blink-when-focused' is non-nil"
385 (when beacon-blink-when-focused
390 (defcustom beacon-lighter
392 ;; ((char-displayable-p ?💡) " 💡")
393 ;; ((char-displayable-p ?Λ) " Λ")
395 "Lighter string used on the mode-line."
399 (define-minor-mode beacon-mode
400 nil nil beacon-lighter nil
404 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
405 (add-hook 'focus-in-hook #'beacon--blink-on-focus)
406 (add-hook 'post-command-hook #'beacon--post-command)
407 (add-hook 'pre-command-hook #'beacon--record-vars)
408 (add-hook 'pre-command-hook #'beacon--vanish))
409 (remove-hook 'focus-in-hook #'beacon--blink-on-focus)
410 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
411 (remove-hook 'post-command-hook #'beacon--post-command)
412 (remove-hook 'pre-command-hook #'beacon--record-vars)
413 (remove-hook 'pre-command-hook #'beacon--vanish)))
416 ;;; beacon.el ends here
419 ;; indent-tabs-mode: nil