]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Fix #34 background-color-at-point isn't 100% reliable
[gnu-emacs-elpa] / 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.5
9 ;; Package-Requires: ((seq "1.11"))
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 ;; See the accompanying Readme.org for configuration details.
37
38 ;;; Code:
39
40 (require 'seq)
41 (require 'faces)
42
43 (defgroup beacon nil
44 "Customization group for beacon."
45 :group 'emacs
46 :prefix "beacon-")
47
48 (defvar beacon--timer nil)
49
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)))
56
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)))
63
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)))
70
71 (defcustom beacon-blink-when-buffer-changes t
72 "Should the beacon blink when changing buffer?"
73 :type 'boolean)
74
75 (defcustom beacon-blink-when-window-scrolls t
76 "Should the beacon blink when the window scrolls?"
77 :type 'boolean)
78
79 (defcustom beacon-blink-when-window-changes t
80 "Should the beacon blink when the window changes?"
81 :type 'boolean)
82
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."
87 :type 'boolean
88 :package-version '(beacon . "0.2"))
89
90 (defcustom beacon-blink-duration 0.3
91 "Time, in seconds, that the blink should last."
92 :type 'number)
93
94 (defcustom beacon-blink-delay 0.3
95 "Time, in seconds, before starting to fade the beacon."
96 :type 'number)
97
98 (defcustom beacon-size 40
99 "Size of the beacon in characters."
100 :type 'number)
101
102 (defcustom beacon-color 0.5
103 "Color of the beacon.
104 This can be a string or a number.
105
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.
109
110 If it is a string, it is a color name or specification,
111 e.g. \"#666600\"."
112 :type '(choice number color))
113
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.")
120
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.
126
127 For instance, if you want to disable beacon on buffers where
128 `hl-line-mode' is on, you can do:
129
130 (add-hook \\='beacon-dont-blink-predicates
131 (lambda () (bound-and-true-p hl-line-mode)))")
132
133 (add-hook 'beacon-dont-blink-predicates #'window-minibuffer-p)
134
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
139 blink."
140 :type '(repeat symbol))
141
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
146 than helpful.."
147 :type '(repeat symbol))
148
149 \f
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)
156
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))))
163
164 \f
165 ;;; Overlays
166 (defvar beacon--ovs nil)
167
168 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
169 "Priotiy used on all of our overlays.")
170
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))
179 (while properties
180 (overlay-put ov (pop properties) (pop properties)))
181 (push ov beacon--ovs)
182 ov))
183
184 (defun beacon--colored-overlay (color)
185 "Put an overlay at point with background COLOR."
186 (beacon--make-overlay 1 'face (list :background color)))
187
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!"
193 (if (not colors)
194 (when (overlayp overlay)
195 (delete-overlay overlay))
196 (overlay-put overlay 'beacon-colors colors)
197 (overlay-put overlay 'after-string
198 (propertize
199 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
200 colors
201 "")
202 'cursor 1000))))
203
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)))
212
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))))))
218
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))
225
226 \f
227 ;;; Colors
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))
232 (out (list a)))
233 (dotimes (_ (1- beacon-size))
234 (push (+ (car out) d) out))
235 (nreverse out)))
236
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)
243 default-bg)))
244 (fg (cond
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))))
252 [0 1 2]))))
253
254 \f
255 ;;; Blinking
256 (defun beacon--shine ()
257 "Shine a beacon at point."
258 (let ((colors (beacon--color-range)))
259 (save-excursion
260 (while colors
261 (if (looking-at "$")
262 (progn
263 (beacon--after-string-overlay colors)
264 (setq colors nil))
265 (beacon--colored-overlay (pop colors))
266 (forward-char 1))))))
267
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)))
274 (o
275 (delete-overlay o)
276 (save-excursion
277 (while (progn (forward-char 1)
278 (setq o (beacon--ov-at-point)))
279 (let ((colors (overlay-get o 'beacon-colors)))
280 (if (not colors)
281 (move-overlay o (1- (point)) (point))
282 (forward-char -1)
283 (beacon--colored-overlay (pop colors))
284 (beacon--ov-put-after-string o colors)
285 (forward-char 1))))))))
286
287 (defun beacon-blink ()
288 "Blink the beacon at the position of the cursor."
289 (interactive)
290 (beacon--vanish)
291 ;; Record vars here in case something is blinking outside the
292 ;; command loop.
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))
298 (beacon--shine)
299 (setq beacon--timer
300 (run-at-time beacon-blink-delay
301 (/ beacon-blink-duration 1.0 beacon-size)
302 #'beacon--dec))))
303
304 \f
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."
310 (and delta-y
311 (markerp beacon--previous-place)
312 (equal (marker-buffer beacon--previous-place)
313 (current-buffer))
314 ;; Quick check that prevents running the code below in very
315 ;; short movements (like typing).
316 (> (abs (- (point) beacon--previous-place))
317 delta-y)
318 ;; Col movement.
319 (or (and delta-x
320 (> (abs (- (current-column)
321 (save-excursion
322 (goto-char beacon--previous-place)
323 (current-column))))
324 delta-x))
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.
328 (save-excursion
329 (let ((p (point)))
330 (goto-char (min beacon--previous-place p))
331 (vertical-motion delta-y)
332 (> (max p beacon--previous-place)
333 (line-beginning-position)))))))
334
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)))))
343
344 (defun beacon--post-command ()
345 "Blink if point moved very far."
346 (cond
347 ((not (markerp beacon--previous-place))
348 (beacon--vanish))
349 ;; Blink for switching windows.
350 ((and beacon-blink-when-window-changes
351 (not (eq beacon--previous-window (selected-window))))
352 (beacon-blink))
353 ;; Blink for scrolling.
354 ((and beacon--window-scrolled
355 (equal beacon--window-scrolled (selected-window)))
356 (beacon-blink))
357 ;; Blink for movement
358 ((beacon--movement-> beacon-blink-when-point-moves-vertically
359 beacon-blink-when-point-moves-horizontally)
360 (beacon-blink))
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))
365
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
371 `post-command-hook'.
372
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))
378 (if this-command
379 (setq beacon--window-scrolled win)
380 (setq beacon--window-scrolled nil)
381 (beacon-blink))))
382
383 (defun beacon--blink-on-focus ()
384 "Blink if `beacon-blink-when-focused' is non-nil"
385 (when beacon-blink-when-focused
386 (beacon-blink)))
387
388 \f
389 ;;; Minor-mode
390 (defcustom beacon-lighter
391 (cond
392 ;; ((char-displayable-p ?💡) " 💡")
393 ;; ((char-displayable-p ?Λ) " Λ")
394 (t " (*)"))
395 "Lighter string used on the mode-line."
396 :type 'string)
397
398 ;;;###autoload
399 (define-minor-mode beacon-mode
400 nil nil beacon-lighter nil
401 :global t
402 (if beacon-mode
403 (progn
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)))
414
415 (provide 'beacon)
416 ;;; beacon.el ends here
417
418 ;; Local Variables:
419 ;; indent-tabs-mode: nil
420 ;; End: