]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Version 1.3.0
[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: 1.3.0
9 ;; Package-Requires: ((seq "2.14"))
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 (unless (fboundp 'seq-mapn)
43 ;; This is for people who are on outdated Emacs snapshots. Will be
44 ;; deleted in a couple of weeks.
45 (defun seq-mapn (function sequence &rest sequences)
46 "Like `seq-map' but FUNCTION is mapped over all SEQUENCES.
47 The arity of FUNCTION must match the number of SEQUENCES, and the
48 mapping stops on the shortest sequence.
49 Return a list of the results.
50
51 \(fn FUNCTION SEQUENCES...)"
52 (let ((result nil)
53 (sequences (seq-map (lambda (s) (seq-into s 'list))
54 (cons sequence sequences))))
55 (while (not (memq nil sequences))
56 (push (apply function (seq-map #'car sequences)) result)
57 (setq sequences (seq-map #'cdr sequences)))
58 (nreverse result))))
59
60 (defgroup beacon nil
61 "Customization group for beacon."
62 :group 'emacs
63 :prefix "beacon-")
64
65 (defvar beacon--timer nil)
66
67 (defcustom beacon-push-mark 35
68 "Should the mark be pushed before long movements?
69 If nil, `beacon' will not push the mark.
70 Otherwise this should be a number, and `beacon' will push the
71 mark whenever point moves more than that many lines."
72 :type '(choice integer (const nil)))
73
74 (defcustom beacon-blink-when-point-moves-vertically nil
75 "Should the beacon blink when moving a long distance vertically?
76 If nil, don't blink due to vertical movement.
77 If non-nil, this should be an integer, which is the minimum
78 movement distance (in lines) that triggers a beacon blink."
79 :type '(choice integer (const nil)))
80
81 (defcustom beacon-blink-when-point-moves-horizontally nil
82 "Should the beacon blink when moving a long distance horizontally?
83 If nil, don't blink due to horizontal movement.
84 If non-nil, this should be an integer, which is the minimum
85 movement distance (in columns) that triggers a beacon blink."
86 :type '(choice integer (const nil)))
87
88 (defcustom beacon-blink-when-buffer-changes t
89 "Should the beacon blink when changing buffer?"
90 :type 'boolean)
91
92 (defcustom beacon-blink-when-window-scrolls t
93 "Should the beacon blink when the window scrolls?"
94 :type 'boolean)
95
96 (defcustom beacon-blink-when-window-changes t
97 "Should the beacon blink when the window changes?"
98 :type 'boolean)
99
100 (defcustom beacon-blink-when-focused nil
101 "Should the beacon blink when Emacs gains focus?
102 Note that, due to a limitation of `focus-in-hook', this might
103 trigger false positives on some systems."
104 :type 'boolean
105 :package-version '(beacon . "0.2"))
106
107 (defcustom beacon-blink-duration 0.3
108 "Time, in seconds, that the blink should last."
109 :type 'number)
110
111 (defcustom beacon-blink-delay 0.3
112 "Time, in seconds, before starting to fade the beacon."
113 :type 'number)
114
115 (defcustom beacon-size 40
116 "Size of the beacon in characters."
117 :type 'number)
118
119 (defcustom beacon-color 0.5
120 "Color of the beacon.
121 This can be a string or a number.
122
123 If it is a number, the color is taken to be white or
124 black (depending on the current theme's background) and this
125 number is a float between 0 and 1 specifing the brightness.
126
127 If it is a string, it is a color name or specification,
128 e.g. \"#666600\"."
129 :type '(choice number color))
130
131 (defface beacon-fallback-background
132 '((((class color) (background light)) (:background "black"))
133 (((class color) (background dark)) (:background "white")))
134 "Fallback beacon background color.
135 Used in cases where the color can't be determined by Emacs.
136 Only the background of this face is used.")
137
138 (defvar beacon-dont-blink-predicates nil
139 "A list of predicates that prevent the beacon blink.
140 These predicate functions are called in order, with no
141 arguments, before blinking the beacon. If any returns
142 non-nil, the beacon will not blink.
143
144 For instance, if you want to disable beacon on buffers where
145 `hl-line-mode' is on, you can do:
146
147 (add-hook \\='beacon-dont-blink-predicates
148 (lambda () (bound-and-true-p hl-line-mode)))")
149
150 (add-hook 'beacon-dont-blink-predicates #'window-minibuffer-p)
151
152 (defcustom beacon-dont-blink-major-modes '(t magit-status-mode magit-popup-mode
153 inf-ruby-mode
154 gnus-summary-mode gnus-group-mode)
155 "A list of major-modes where the beacon won't blink.
156 Whenever the current buffer satisfies `derived-mode-p' for
157 one of the major-modes on this list, the beacon will not
158 blink."
159 :type '(repeat symbol))
160
161 (defcustom beacon-dont-blink-commands '(next-line previous-line
162 forward-line)
163 "A list of commands that should not make the beacon blink.
164 Use this for commands that scroll the window in very
165 predictable ways, when the blink would be more distracting
166 than helpful.."
167 :type '(repeat symbol))
168
169 (defcustom beacon-before-blink-hook nil
170 "Hook run immediately before blinking the beacon."
171 :type 'hook)
172
173 \f
174 ;;; Internal variables
175 (defvar beacon--window-scrolled nil)
176 (defvar beacon--previous-place nil)
177 (defvar beacon--previous-mark-head nil)
178 (defvar beacon--previous-window nil)
179 (defvar beacon--previous-window-start 0)
180
181 (defun beacon--record-vars ()
182 (unless (window-minibuffer-p)
183 (setq beacon--previous-mark-head (car mark-ring))
184 (setq beacon--previous-place (point-marker))
185 (setq beacon--previous-window (selected-window))
186 (setq beacon--previous-window-start (window-start))))
187
188 \f
189 ;;; Overlays
190 (defvar beacon--ovs nil)
191
192 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
193 "Priotiy used on all of our overlays.")
194
195 (defun beacon--make-overlay (length &rest properties)
196 "Put an overlay at point with background COLOR."
197 (let ((ov (make-overlay (point) (+ length (point)))))
198 (overlay-put ov 'beacon t)
199 ;; Our overlay is very temporary, so we take the liberty of giving
200 ;; it a high priority.
201 (overlay-put ov 'priority beacon-overlay-priority)
202 (overlay-put ov 'window (selected-window))
203 (while properties
204 (overlay-put ov (pop properties) (pop properties)))
205 (push ov beacon--ovs)
206 ov))
207
208 (defun beacon--colored-overlay (color)
209 "Put an overlay at point with background COLOR."
210 (beacon--make-overlay 1 'face (list :background color)))
211
212 (defun beacon--ov-put-after-string (overlay colors)
213 "Add an after-string property to OVERLAY.
214 The property's value is a string of spaces with background
215 COLORS applied to each one.
216 If COLORS is nil, OVERLAY is deleted!"
217 (if (not colors)
218 (when (overlayp overlay)
219 (delete-overlay overlay))
220 (overlay-put overlay 'beacon-colors colors)
221 (overlay-put overlay 'after-string
222 (propertize
223 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
224 colors
225 "")
226 'cursor 1000))))
227
228 (defun beacon--after-string-overlay (colors)
229 "Put an overlay at point with an after-string property.
230 The property's value is a string of spaces with background
231 COLORS applied to each one."
232 ;; The after-string must not be longer than the remaining columns
233 ;; from point to right window-end else it will be wrapped around.
234 (let ((colors (seq-take colors (- (window-width) (current-column)))))
235 (beacon--ov-put-after-string (beacon--make-overlay 0) colors)))
236
237 (defun beacon--ov-at-point ()
238 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
239 (overlays-in (point) (point)))
240 (seq-filter (lambda (o) (overlay-get o 'beacon))
241 (overlays-at (point))))))
242
243 (defun beacon--vanish (&rest _)
244 "Turn off the beacon."
245 (when (timerp beacon--timer)
246 (cancel-timer beacon--timer))
247 (mapc #'delete-overlay beacon--ovs)
248 (setq beacon--ovs nil))
249
250 \f
251 ;;; Colors
252 (defun beacon--int-range (a b)
253 "Return a list of integers between A inclusive and B exclusive.
254 Only returns `beacon-size' elements."
255 (let ((d (/ (- b a) beacon-size))
256 (out (list a)))
257 (dotimes (_ (1- beacon-size))
258 (push (+ (car out) d) out))
259 (nreverse out)))
260
261 (defun beacon--color-range ()
262 "Return a list of background colors for the beacon."
263 (let* ((default-bg (or (save-excursion
264 (unless (eobp)
265 (forward-line 1)
266 (unless (or (bobp) (not (bolp)))
267 (forward-char -1)))
268 (background-color-at-point))
269 (face-background 'default)))
270 (bg (color-values (if (or (not (stringp default-bg))
271 (string-match "\\`unspecified-" default-bg))
272 (face-attribute 'beacon-fallback-background :background)
273 default-bg)))
274 (fg (cond
275 ((stringp beacon-color) (color-values beacon-color))
276 ((and (stringp bg)
277 (< (color-distance "black" bg)
278 (color-distance "white" bg)))
279 (make-list 3 (* beacon-color 65535)))
280 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
281 (apply #'seq-mapn (lambda (r g b) (format "#%04x%04x%04x" r g b))
282 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
283 [0 1 2]))))
284
285 \f
286 ;;; Blinking
287 (defun beacon--shine ()
288 "Shine a beacon at point."
289 (let ((colors (beacon--color-range)))
290 (save-excursion
291 (while colors
292 (if (looking-at "$")
293 (progn
294 (beacon--after-string-overlay colors)
295 (setq colors nil))
296 (beacon--colored-overlay (pop colors))
297 (forward-char 1))))))
298
299 (defun beacon--dec ()
300 "Decrease the beacon brightness by one."
301 (pcase (beacon--ov-at-point)
302 (`nil (beacon--vanish))
303 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
304 (beacon--ov-put-after-string o (cdr c)))
305 (o
306 (delete-overlay o)
307 (save-excursion
308 (while (and (condition-case nil
309 (progn (forward-char 1) t)
310 (end-of-buffer nil))
311 (setq o (beacon--ov-at-point)))
312 (let ((colors (overlay-get o 'beacon-colors)))
313 (if (not colors)
314 (move-overlay o (1- (point)) (point))
315 (forward-char -1)
316 (beacon--colored-overlay (pop colors))
317 (beacon--ov-put-after-string o colors)
318 (forward-char 1))))))))
319
320 ;;;###autoload
321 (defun beacon-blink ()
322 "Blink the beacon at the position of the cursor.
323 Unlike `beacon-blink-automated', the beacon will blink
324 unconditionally (even if `beacon-mode' is disabled), and this can
325 be invoked as a user command or called from lisp code."
326 (interactive)
327 (beacon--vanish)
328 (run-hooks 'beacon-before-blink-hook)
329 (beacon--shine)
330 (setq beacon--timer
331 (run-at-time beacon-blink-delay
332 (/ beacon-blink-duration 1.0 beacon-size)
333 #'beacon--dec)))
334
335 (defun beacon-blink-automated ()
336 "If appropriate, blink the beacon at the position of the cursor.
337 Unlike `beacon-blink', the blinking is conditioned on a series of
338 variables: `beacon-mode', `beacon-dont-blink-commands',
339 `beacon-dont-blink-major-modes', and
340 `beacon-dont-blink-predicates'."
341 ;; Record vars here in case something is blinking outside the
342 ;; command loop.
343 (beacon--record-vars)
344 (unless (or (not beacon-mode)
345 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
346 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
347 (memq (or this-command last-command) beacon-dont-blink-commands))
348 (beacon-blink)))
349
350 \f
351 ;;; Movement detection
352 (defun beacon--movement-> (delta-y &optional delta-x)
353 "Return non-nil if latest vertical movement is > DELTA-Y.
354 If DELTA-Y is nil, return nil.
355 The same is true for DELTA-X and horizonta movement."
356 (and delta-y
357 (markerp beacon--previous-place)
358 (equal (marker-buffer beacon--previous-place)
359 (current-buffer))
360 ;; Quick check that prevents running the code below in very
361 ;; short movements (like typing).
362 (> (abs (- (point) beacon--previous-place))
363 delta-y)
364 ;; Col movement.
365 (or (and delta-x
366 (> (abs (- (current-column)
367 (save-excursion
368 (goto-char beacon--previous-place)
369 (current-column))))
370 delta-x))
371 ;; Check if the movement was >= DELTA lines by moving DELTA
372 ;; lines. `count-screen-lines' is too slow if the movement had
373 ;; thousands of lines.
374 (save-excursion
375 (let ((p (point)))
376 (goto-char (min beacon--previous-place p))
377 (vertical-motion delta-y)
378 (> (max p beacon--previous-place)
379 (line-beginning-position)))))))
380
381 (defun beacon--maybe-push-mark ()
382 "Push mark if it seems to be safe."
383 (when (and (not mark-active)
384 (beacon--movement-> beacon-push-mark))
385 (let ((head (car mark-ring)))
386 (when (and (eq beacon--previous-mark-head head)
387 (not (equal head beacon--previous-place)))
388 (push-mark beacon--previous-place 'silent)))))
389
390 (defun beacon--post-command ()
391 "Blink if point moved very far."
392 (cond
393 ;; Sanity check.
394 ((not (markerp beacon--previous-place)))
395 ;; Blink for switching buffers.
396 ((and beacon-blink-when-buffer-changes
397 (not (eq (marker-buffer beacon--previous-place)
398 (current-buffer))))
399 (beacon-blink-automated))
400 ;; Blink for switching windows.
401 ((and beacon-blink-when-window-changes
402 (not (eq beacon--previous-window (selected-window))))
403 (beacon-blink-automated))
404 ;; Blink for scrolling.
405 ((and beacon--window-scrolled
406 (equal beacon--window-scrolled (selected-window)))
407 (beacon-blink-automated))
408 ;; Blink for movement
409 ((beacon--movement-> beacon-blink-when-point-moves-vertically
410 beacon-blink-when-point-moves-horizontally)
411 (beacon-blink-automated)))
412 (beacon--maybe-push-mark)
413 (setq beacon--window-scrolled nil))
414
415 (defun beacon--window-scroll-function (win start-pos)
416 "Blink the beacon or record that window has been scrolled.
417 If invoked during the command loop, record the current window so
418 that it may be blinked on post-command. This is because the
419 scrolled window might not be active, but we only know that at
420 `post-command-hook'.
421
422 If invoked outside the command loop, `post-command-hook' would be
423 unreliable, so just blink immediately."
424 (unless (or (and (equal beacon--previous-window-start start-pos)
425 (equal beacon--previous-window win))
426 (not beacon-blink-when-window-scrolls))
427 (if this-command
428 (setq beacon--window-scrolled win)
429 (setq beacon--window-scrolled nil)
430 (beacon-blink-automated))))
431
432 (defun beacon--blink-on-focus ()
433 "Blink if `beacon-blink-when-focused' is non-nil"
434 (when beacon-blink-when-focused
435 (beacon-blink-automated)))
436
437 \f
438 ;;; Minor-mode
439 (defcustom beacon-lighter
440 (cond
441 ;; ((char-displayable-p ?💡) " 💡")
442 ;; ((char-displayable-p ?Λ) " Λ")
443 (t " (*)"))
444 "Lighter string used on the mode-line."
445 :type 'string)
446
447 ;;;###autoload
448 (define-minor-mode beacon-mode
449 nil nil beacon-lighter nil
450 :global t
451 (if beacon-mode
452 (progn
453 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
454 (add-hook 'focus-in-hook #'beacon--blink-on-focus)
455 (add-hook 'post-command-hook #'beacon--post-command)
456 (add-hook 'before-change-functions #'beacon--vanish)
457 (add-hook 'pre-command-hook #'beacon--record-vars)
458 (add-hook 'pre-command-hook #'beacon--vanish))
459 (remove-hook 'focus-in-hook #'beacon--blink-on-focus)
460 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
461 (remove-hook 'post-command-hook #'beacon--post-command)
462 (remove-hook 'before-change-functions #'beacon--vanish)
463 (remove-hook 'pre-command-hook #'beacon--record-vars)
464 (remove-hook 'pre-command-hook #'beacon--vanish)))
465
466 (provide 'beacon)
467 ;;; beacon.el ends here
468
469 ;; Local Variables:
470 ;; indent-tabs-mode: nil
471 ;; End: