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