]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Don't blink on line movement
[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.1
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 (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 '(magit-status-mode magit-popup-mode)
153 "A list of major-modes where the beacon won't blink.
154 Whenever the current buffer satisfies `derived-mode-p' for
155 one of the major-modes on this list, the beacon will not
156 blink."
157 :type '(repeat symbol))
158
159 (defcustom beacon-dont-blink-commands '(recenter-top-bottom next-line previous-line
160 forward-line)
161 "A list of commands that should not make the beacon blink.
162 Use this for commands that scroll the window in very
163 predictable ways, when the blink would be more distracting
164 than helpful.."
165 :type '(repeat symbol))
166
167 \f
168 ;;; Internal variables
169 (defvar beacon--window-scrolled nil)
170 (defvar beacon--previous-place nil)
171 (defvar beacon--previous-mark-head nil)
172 (defvar beacon--previous-window nil)
173 (defvar beacon--previous-window-start 0)
174
175 (defun beacon--record-vars ()
176 (unless (window-minibuffer-p)
177 (setq beacon--previous-mark-head (car mark-ring))
178 (setq beacon--previous-place (point-marker))
179 (setq beacon--previous-window (selected-window))
180 (setq beacon--previous-window-start (window-start))))
181
182 \f
183 ;;; Overlays
184 (defvar beacon--ovs nil)
185
186 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
187 "Priotiy used on all of our overlays.")
188
189 (defun beacon--make-overlay (length &rest properties)
190 "Put an overlay at point with background COLOR."
191 (let ((ov (make-overlay (point) (+ length (point)))))
192 (overlay-put ov 'beacon t)
193 ;; Our overlay is very temporary, so we take the liberty of giving
194 ;; it a high priority.
195 (overlay-put ov 'priority beacon-overlay-priority)
196 (overlay-put ov 'window (selected-window))
197 (while properties
198 (overlay-put ov (pop properties) (pop properties)))
199 (push ov beacon--ovs)
200 ov))
201
202 (defun beacon--colored-overlay (color)
203 "Put an overlay at point with background COLOR."
204 (beacon--make-overlay 1 'face (list :background color)))
205
206 (defun beacon--ov-put-after-string (overlay colors)
207 "Add an after-string property to OVERLAY.
208 The property's value is a string of spaces with background
209 COLORS applied to each one.
210 If COLORS is nil, OVERLAY is deleted!"
211 (if (not colors)
212 (when (overlayp overlay)
213 (delete-overlay overlay))
214 (overlay-put overlay 'beacon-colors colors)
215 (overlay-put overlay 'after-string
216 (propertize
217 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
218 colors
219 "")
220 'cursor 1000))))
221
222 (defun beacon--after-string-overlay (colors)
223 "Put an overlay at point with an after-string property.
224 The property's value is a string of spaces with background
225 COLORS applied to each one."
226 ;; The after-string must not be longer than the remaining columns
227 ;; from point to right window-end else it will be wrapped around.
228 (let ((colors (seq-take colors (- (window-width) (current-column)))))
229 (beacon--ov-put-after-string (beacon--make-overlay 0) colors)))
230
231 (defun beacon--ov-at-point ()
232 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
233 (overlays-in (point) (point)))
234 (seq-filter (lambda (o) (overlay-get o 'beacon))
235 (overlays-at (point))))))
236
237 (defun beacon--vanish ()
238 "Turn off the beacon."
239 (when (timerp beacon--timer)
240 (cancel-timer beacon--timer))
241 (mapc #'delete-overlay beacon--ovs)
242 (setq beacon--ovs nil))
243
244 \f
245 ;;; Colors
246 (defun beacon--int-range (a b)
247 "Return a list of integers between A inclusive and B exclusive.
248 Only returns `beacon-size' elements."
249 (let ((d (/ (- b a) beacon-size))
250 (out (list a)))
251 (dotimes (_ (1- beacon-size))
252 (push (+ (car out) d) out))
253 (nreverse out)))
254
255 (defun beacon--color-range ()
256 "Return a list of background colors for the beacon."
257 (let* ((default-bg (or (background-color-at-point)
258 (face-background 'default)))
259 (bg (color-values (if (string-match "\\`unspecified-" default-bg)
260 (face-attribute 'beacon-fallback-background :background)
261 default-bg)))
262 (fg (cond
263 ((stringp beacon-color) (color-values beacon-color))
264 ((< (color-distance "black" bg)
265 (color-distance "white" bg))
266 (make-list 3 (* beacon-color 65535)))
267 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
268 (apply #'seq-mapn (lambda (r g b) (format "#%04x%04x%04x" r g b))
269 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
270 [0 1 2]))))
271
272 \f
273 ;;; Blinking
274 (defun beacon--shine ()
275 "Shine a beacon at point."
276 (let ((colors (beacon--color-range)))
277 (save-excursion
278 (while colors
279 (if (looking-at "$")
280 (progn
281 (beacon--after-string-overlay colors)
282 (setq colors nil))
283 (beacon--colored-overlay (pop colors))
284 (forward-char 1))))))
285
286 (defun beacon--dec ()
287 "Decrease the beacon brightness by one."
288 (pcase (beacon--ov-at-point)
289 (`nil (beacon--vanish))
290 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
291 (beacon--ov-put-after-string o (cdr c)))
292 (o
293 (delete-overlay o)
294 (save-excursion
295 (while (progn (forward-char 1)
296 (setq o (beacon--ov-at-point)))
297 (let ((colors (overlay-get o 'beacon-colors)))
298 (if (not colors)
299 (move-overlay o (1- (point)) (point))
300 (forward-char -1)
301 (beacon--colored-overlay (pop colors))
302 (beacon--ov-put-after-string o colors)
303 (forward-char 1))))))))
304
305 (defun beacon-blink ()
306 "Blink the beacon at the position of the cursor."
307 (interactive)
308 (beacon--vanish)
309 ;; Record vars here in case something is blinking outside the
310 ;; command loop.
311 (beacon--record-vars)
312 (unless (or (not beacon-mode)
313 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
314 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
315 (memq (or this-command last-command) beacon-dont-blink-commands))
316 (beacon--shine)
317 (setq beacon--timer
318 (run-at-time beacon-blink-delay
319 (/ beacon-blink-duration 1.0 beacon-size)
320 #'beacon--dec))))
321
322 \f
323 ;;; Movement detection
324 (defun beacon--movement-> (delta-y &optional delta-x)
325 "Return non-nil if latest vertical movement is > DELTA-Y.
326 If DELTA-Y is nil, return nil.
327 The same is true for DELTA-X and horizonta movement."
328 (and delta-y
329 (markerp beacon--previous-place)
330 (equal (marker-buffer beacon--previous-place)
331 (current-buffer))
332 ;; Quick check that prevents running the code below in very
333 ;; short movements (like typing).
334 (> (abs (- (point) beacon--previous-place))
335 delta-y)
336 ;; Col movement.
337 (or (and delta-x
338 (> (abs (- (current-column)
339 (save-excursion
340 (goto-char beacon--previous-place)
341 (current-column))))
342 delta-x))
343 ;; Check if the movement was >= DELTA lines by moving DELTA
344 ;; lines. `count-screen-lines' is too slow if the movement had
345 ;; thousands of lines.
346 (save-excursion
347 (let ((p (point)))
348 (goto-char (min beacon--previous-place p))
349 (vertical-motion delta-y)
350 (> (max p beacon--previous-place)
351 (line-beginning-position)))))))
352
353 (defun beacon--maybe-push-mark ()
354 "Push mark if it seems to be safe."
355 (when (and (not mark-active)
356 (beacon--movement-> beacon-push-mark))
357 (let ((head (car mark-ring)))
358 (when (and (eq beacon--previous-mark-head head)
359 (not (equal head beacon--previous-place)))
360 (push-mark beacon--previous-place 'silent)))))
361
362 (defun beacon--post-command ()
363 "Blink if point moved very far."
364 (cond
365 ((not (markerp beacon--previous-place))
366 (beacon--vanish))
367 ;; Blink for switching windows.
368 ((and beacon-blink-when-window-changes
369 (not (eq beacon--previous-window (selected-window))))
370 (beacon-blink))
371 ;; Blink for scrolling.
372 ((and beacon--window-scrolled
373 (equal beacon--window-scrolled (selected-window)))
374 (beacon-blink))
375 ;; Blink for movement
376 ((beacon--movement-> beacon-blink-when-point-moves-vertically
377 beacon-blink-when-point-moves-horizontally)
378 (beacon-blink))
379 ;; Even if we don't blink, vanish any previous beacon.
380 (t (beacon--vanish)))
381 (beacon--maybe-push-mark)
382 (setq beacon--window-scrolled nil))
383
384 (defun beacon--window-scroll-function (win start-pos)
385 "Blink the beacon or record that window has been scrolled.
386 If invoked during the command loop, record the current window so
387 that it may be blinked on post-command. This is because the
388 scrolled window might not be active, but we only know that at
389 `post-command-hook'.
390
391 If invoked outside the command loop, `post-command-hook' would be
392 unreliable, so just blink immediately."
393 (unless (or (and (equal beacon--previous-window-start start-pos)
394 (equal beacon--previous-window win))
395 (not beacon-blink-when-window-scrolls))
396 (if this-command
397 (setq beacon--window-scrolled win)
398 (setq beacon--window-scrolled nil)
399 (beacon-blink))))
400
401 (defun beacon--blink-on-focus ()
402 "Blink if `beacon-blink-when-focused' is non-nil"
403 (when beacon-blink-when-focused
404 (beacon-blink)))
405
406 \f
407 ;;; Minor-mode
408 (defcustom beacon-lighter
409 (cond
410 ;; ((char-displayable-p ?💡) " 💡")
411 ;; ((char-displayable-p ?Λ) " Λ")
412 (t " (*)"))
413 "Lighter string used on the mode-line."
414 :type 'string)
415
416 ;;;###autoload
417 (define-minor-mode beacon-mode
418 nil nil beacon-lighter nil
419 :global t
420 (if beacon-mode
421 (progn
422 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
423 (add-hook 'focus-in-hook #'beacon--blink-on-focus)
424 (add-hook 'post-command-hook #'beacon--post-command)
425 (add-hook 'pre-command-hook #'beacon--record-vars)
426 (add-hook 'pre-command-hook #'beacon--vanish))
427 (remove-hook 'focus-in-hook #'beacon--blink-on-focus)
428 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
429 (remove-hook 'post-command-hook #'beacon--post-command)
430 (remove-hook 'pre-command-hook #'beacon--record-vars)
431 (remove-hook 'pre-command-hook #'beacon--vanish)))
432
433 (provide 'beacon)
434 ;;; beacon.el ends here
435
436 ;; Local Variables:
437 ;; indent-tabs-mode: nil
438 ;; End: