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") (cl-lib "0.5"))
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 (face-attribute 'default :background))
240 (bg (color-values (if (string-match "\\`unspecified-" default-bg)
241 (face-attribute 'beacon-fallback-background :background)
244 ((stringp beacon-color) (color-values beacon-color))
245 ((< (color-distance "black" bg)
246 (color-distance "white" bg))
247 (make-list 3 (* beacon-color 65535)))
248 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
249 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
250 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
255 (defun beacon--shine ()
256 "Shine a beacon at point."
257 (let ((colors (beacon--color-range)))
262 (beacon--after-string-overlay colors)
264 (beacon--colored-overlay (pop colors))
265 (forward-char 1))))))
267 (defun beacon--dec ()
268 "Decrease the beacon brightness by one."
269 (pcase (beacon--ov-at-point)
270 (`nil (beacon--vanish))
271 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
272 (beacon--ov-put-after-string o (cdr c)))
276 (while (progn (forward-char 1)
277 (setq o (beacon--ov-at-point)))
278 (let ((colors (overlay-get o 'beacon-colors)))
280 (move-overlay o (1- (point)) (point))
282 (beacon--colored-overlay (pop colors))
283 (beacon--ov-put-after-string o colors)
284 (forward-char 1))))))))
286 (defun beacon-blink ()
287 "Blink the beacon at the position of the cursor."
290 ;; Record vars here in case something is blinking outside the
292 (beacon--record-vars)
293 (unless (or (not beacon-mode)
294 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
295 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
296 (memq (or this-command last-command) beacon-dont-blink-commands))
299 (run-at-time beacon-blink-delay
300 (/ beacon-blink-duration 1.0 beacon-size)
304 ;;; Movement detection
305 (defun beacon--movement-> (delta-y &optional delta-x)
306 "Return non-nil if latest vertical movement is > DELTA-Y.
307 If DELTA-Y is nil, return nil.
308 The same is true for DELTA-X and horizonta movement."
310 (markerp beacon--previous-place)
311 (equal (marker-buffer beacon--previous-place)
313 ;; Quick check that prevents running the code below in very
314 ;; short movements (like typing).
315 (> (abs (- (point) beacon--previous-place))
319 (> (abs (- (current-column)
321 (goto-char beacon--previous-place)
324 ;; Check if the movement was >= DELTA lines by moving DELTA
325 ;; lines. `count-screen-lines' is too slow if the movement had
326 ;; thousands of lines.
329 (goto-char (min beacon--previous-place p))
330 (vertical-motion delta-y)
331 (> (max p beacon--previous-place)
332 (line-beginning-position)))))))
334 (defun beacon--maybe-push-mark ()
335 "Push mark if it seems to be safe."
336 (when (and (not mark-active)
337 (beacon--movement-> beacon-push-mark))
338 (let ((head (car mark-ring)))
339 (when (and (eq beacon--previous-mark-head head)
340 (not (equal head beacon--previous-place)))
341 (push-mark beacon--previous-place)))))
343 (defun beacon--post-command ()
344 "Blink if point moved very far."
346 ((not (markerp beacon--previous-place))
348 ;; Blink for switching windows.
349 ((and beacon-blink-when-window-changes
350 (not (eq beacon--previous-window (selected-window))))
352 ;; Blink for scrolling.
353 ((and beacon--window-scrolled
354 (equal beacon--window-scrolled (selected-window)))
356 ;; Blink for movement
357 ((beacon--movement-> beacon-blink-when-point-moves-vertically
358 beacon-blink-when-point-moves-horizontally)
360 ;; Even if we don't blink, vanish any previous beacon.
361 (t (beacon--vanish)))
362 (beacon--maybe-push-mark)
363 (setq beacon--window-scrolled nil))
365 (defun beacon--window-scroll-function (win start-pos)
366 "Blink the beacon or record that window has been scrolled.
367 If invoked during the command loop, record the current window so
368 that it may be blinked on post-command. This is because the
369 scrolled window might not be active, but we only know that at
372 If invoked outside the command loop, `post-command-hook' would be
373 unreliable, so just blink immediately."
374 (unless (or (and (equal beacon--previous-window-start start-pos)
375 (equal beacon--previous-window win))
376 (not beacon-blink-when-window-scrolls))
378 (setq beacon--window-scrolled win)
379 (setq beacon--window-scrolled nil)
382 (defun beacon--blink-on-focus ()
383 "Blink if `beacon-blink-when-focused' is non-nil"
384 (when beacon-blink-when-focused
389 (defcustom beacon-lighter
391 ;; ((char-displayable-p ?💡) " 💡")
392 ;; ((char-displayable-p ?Λ) " Λ")
394 "Lighter string used on the mode-line."
398 (define-minor-mode beacon-mode
399 nil nil beacon-lighter nil
403 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
404 (add-hook 'focus-in-hook #'beacon--blink-on-focus)
405 (add-hook 'post-command-hook #'beacon--post-command)
406 (add-hook 'pre-command-hook #'beacon--record-vars)
407 (add-hook 'pre-command-hook #'beacon--vanish))
408 (remove-hook 'focus-in-hook #'beacon--blink-on-focus)
409 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
410 (remove-hook 'post-command-hook #'beacon--post-command)
411 (remove-hook 'pre-command-hook #'beacon--record-vars)
412 (remove-hook 'pre-command-hook #'beacon--vanish)))
415 ;;; beacon.el ends here
418 ;; indent-tabs-mode: nil