]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Default `beacon-push-mark' to 35
[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.1.1
9 ;; Package-Requires: ((seq "1.9"))
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
42 (defgroup beacon nil
43 "Customization group for beacon."
44 :group 'emacs
45 :prefix "beacon-")
46
47 (defvar beacon--timer nil)
48
49 (defcustom beacon-push-mark 35
50 "Should the mark be pushed before long movements?
51 If nil, `beacon' will not push the mark.
52 Otherwise this should be a number, and `beacon' will push the
53 mark whenever point moves more than that many lines."
54 :type '(choice integer (const nil)))
55
56 (defcustom beacon-blink-when-point-moves nil
57 "Should the beacon blink when moving a long distance?
58 If nil, don't blink due to plain movement.
59 If non-nil, this should be an integer, which is the minimum
60 movement distance (in lines) that triggers a beacon blink."
61 :type '(choice integer (const nil)))
62
63 (defcustom beacon-blink-when-buffer-changes t
64 "Should the beacon blink when changing buffer?"
65 :type 'boolean)
66
67 (defcustom beacon-blink-when-window-scrolls t
68 "Should the beacon blink when the window scrolls?"
69 :type 'boolean)
70
71 (defcustom beacon-blink-when-window-changes t
72 "Should the beacon blink when the window changes?"
73 :type 'boolean)
74
75 (defcustom beacon-blink-duration 0.3
76 "Time, in seconds, that the blink should last."
77 :type 'number)
78
79 (defcustom beacon-blink-delay 0.3
80 "Time, in seconds, before starting to fade the beacon."
81 :type 'number)
82
83 (defcustom beacon-size 40
84 "Size of the beacon in characters."
85 :type 'number)
86
87 (defcustom beacon-color 0.5
88 "Color of the beacon.
89 This can be a string or a number.
90
91 If it is a number, the color is taken to be white or
92 black (depending on the current theme's background) and this
93 number is a float between 0 and 1 specifing the brightness.
94
95 If it is a string, it is a color name or specification,
96 e.g. \"#666600\"."
97 :type '(choice number color))
98
99 (defvar beacon-dont-blink-predicates nil
100 "A list of predicates that prevent the beacon blink.
101 These predicate functions are called in order, with no
102 arguments, before blinking the beacon. If any returns
103 non-nil, the beacon will not blink.
104
105 For instance, if you want to disable beacon on buffers where
106 `hl-line-mode' is on, you can do:
107
108 (add-hook \\='beacon-dont-blink-predicates
109 (lambda () (bound-and-true-p hl-line-mode)))")
110
111 (add-hook 'beacon-dont-blink-predicates #'window-minibuffer-p)
112
113 (defcustom beacon-dont-blink-major-modes '(magit-status-mode)
114 "A list of major-modes where the beacon won't blink.
115 Whenever the current buffer satisfies `derived-mode-p' for
116 one of the major-modes on this list, the beacon will not
117 blink."
118 :type '(repeat symbol))
119
120 (defcustom beacon-dont-blink-commands '(recenter-top-bottom)
121 "A list of commands that should not make the beacon blink.
122 Use this for commands that scroll the window in very
123 predictable ways, when the blink would be more distracting
124 than helpful.."
125 :type '(repeat symbol))
126
127 \f
128 ;;; Overlays
129 (defvar beacon--ovs nil)
130
131 (defconst beacon-overlay-priority (/ most-positive-fixnum 2)
132 "Priotiy used on all of our overlays.")
133
134 (defun beacon--make-overlay (length &rest properties)
135 "Put an overlay at point with background COLOR."
136 (let ((ov (make-overlay (point) (+ length (point)))))
137 (overlay-put ov 'beacon t)
138 ;; Our overlay is very temporary, so we take the liberty of giving
139 ;; it a high priority.
140 (overlay-put ov 'priority beacon-overlay-priority)
141 (overlay-put ov 'window (selected-window))
142 (while properties
143 (overlay-put ov (pop properties) (pop properties)))
144 (push ov beacon--ovs)
145 ov))
146
147 (defun beacon--colored-overlay (color)
148 "Put an overlay at point with background COLOR."
149 (beacon--make-overlay 1 'face (list :background color)))
150
151 (defun beacon--ov-put-after-string (overlay colors)
152 "Add an after-string property to OVERLAY.
153 The property's value is a string of spaces with background
154 COLORS applied to each one.
155 If COLORS is nil, OVERLAY is deleted!"
156 (if (not colors)
157 (when (overlayp overlay)
158 (delete-overlay overlay))
159 (overlay-put overlay 'beacon-colors colors)
160 (overlay-put overlay 'after-string
161 (propertize
162 (mapconcat (lambda (c) (propertize " " 'face (list :background c)))
163 colors
164 "")
165 'cursor 1000))))
166
167 (defun beacon--after-string-overlay (colors)
168 "Put an overlay at point with an after-string property.
169 The property's value is a string of spaces with background
170 COLORS applied to each one."
171 ;; The after-string must not be longer than the remaining columns
172 ;; from point to right window-end else it will be wrapped around.
173 (let ((colors (seq-take colors (- (window-width) (current-column)))))
174 (beacon--ov-put-after-string (beacon--make-overlay 0) colors)))
175
176 (defun beacon--ov-at-point ()
177 (car (or (seq-filter (lambda (o) (overlay-get o 'beacon))
178 (overlays-in (point) (point)))
179 (seq-filter (lambda (o) (overlay-get o 'beacon))
180 (overlays-at (point))))))
181
182 (defun beacon--vanish ()
183 "Turn off the beacon."
184 (when (timerp beacon--timer)
185 (cancel-timer beacon--timer))
186 (mapc #'delete-overlay beacon--ovs)
187 (setq beacon--ovs nil))
188
189 \f
190 ;;; Colors
191 (defun beacon--int-range (a b)
192 "Return a list of integers between A inclusive and B exclusive.
193 Only returns `beacon-size' elements."
194 (let ((d (/ (- b a) beacon-size))
195 (out (list a)))
196 (dotimes (_ (1- beacon-size))
197 (push (+ (car out) d) out))
198 (nreverse out)))
199
200 (defun beacon--color-range ()
201 "Return a list of background colors for the beacon."
202 (let* ((bg (color-values (face-attribute 'default :background)))
203 (fg (cond
204 ((stringp beacon-color) (color-values beacon-color))
205 ((< (color-distance "black" bg)
206 (color-distance "white" bg))
207 (make-list 3 (* beacon-color 65535)))
208 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
209 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
210 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
211 [0 1 2]))))
212
213 \f
214 ;;; Blinking
215 (defun beacon--shine ()
216 "Shine a beacon at point."
217 (let ((colors (beacon--color-range)))
218 (save-excursion
219 (while colors
220 (if (looking-at "$")
221 (progn
222 (beacon--after-string-overlay colors)
223 (setq colors nil))
224 (beacon--colored-overlay (pop colors))
225 (forward-char 1))))))
226
227 (defun beacon--dec ()
228 "Decrease the beacon brightness by one."
229 (pcase (beacon--ov-at-point)
230 (`nil (beacon--vanish))
231 ((and o (let c (overlay-get o 'beacon-colors)) (guard c))
232 (beacon--ov-put-after-string o (cdr c)))
233 (o
234 (delete-overlay o)
235 (save-excursion
236 (while (progn (forward-char 1)
237 (setq o (beacon--ov-at-point)))
238 (let ((colors (overlay-get o 'beacon-colors)))
239 (if (not colors)
240 (move-overlay o (1- (point)) (point))
241 (forward-char -1)
242 (beacon--colored-overlay (pop colors))
243 (beacon--ov-put-after-string o colors)
244 (forward-char 1))))))))
245
246 (defun beacon-blink ()
247 "Blink the beacon at the position of the cursor."
248 (interactive)
249 (beacon--vanish)
250 (unless (or (not beacon-mode)
251 (run-hook-with-args-until-success 'beacon-dont-blink-predicates)
252 (seq-find #'derived-mode-p beacon-dont-blink-major-modes)
253 (memq (or this-command last-command) beacon-dont-blink-commands))
254 (beacon--shine)
255 (setq beacon--timer
256 (run-at-time beacon-blink-delay
257 (/ beacon-blink-duration 1.0 beacon-size)
258 #'beacon--dec))))
259
260 \f
261 ;;; Movement detection
262 (defvar beacon--window-scrolled nil)
263 (defvar beacon--previous-place nil)
264 (defvar beacon--previous-mark-head nil)
265 (defvar beacon--previous-window nil)
266
267 (defun beacon--movement-> (delta)
268 "Return non-nil if latest point movement is > DELTA.
269 If DELTA is nil, return nil."
270 (and delta
271 (markerp beacon--previous-place)
272 (equal (marker-buffer beacon--previous-place)
273 (current-buffer))
274 (> (abs (- (point) beacon--previous-place))
275 delta)
276 (> (count-screen-lines (min (point) beacon--previous-place)
277 (max (point) beacon--previous-place))
278 delta)))
279
280 (defun beacon--maybe-push-mark ()
281 "Push mark if it seems to be safe."
282 (when (and (not mark-active)
283 (beacon--movement-> beacon-push-mark))
284 (let ((head (car mark-ring)))
285 (when (and (eq beacon--previous-mark-head head)
286 (not (equal head beacon--previous-place)))
287 (push-mark beacon--previous-place)))))
288
289 (defun beacon--post-command ()
290 "Blink if point moved very far."
291 (cond
292 ((not (markerp beacon--previous-place))
293 (beacon--vanish))
294 ;; Blink for switching windows.
295 ((and beacon-blink-when-window-changes
296 (not (eq beacon--previous-window (selected-window))))
297 (beacon-blink))
298 ;; Blink for scrolling.
299 ((and beacon-blink-when-window-scrolls
300 beacon--window-scrolled
301 (equal beacon--window-scrolled (selected-window)))
302 (beacon-blink))
303 ;; Blink for movement
304 ((beacon--movement-> beacon-blink-when-point-moves)
305 (beacon-blink))
306 ;; Even if we don't blink, vanish any previous beacon.
307 (t (beacon--vanish)))
308 (beacon--maybe-push-mark)
309 (setq beacon--window-scrolled nil)
310 (unless (window-minibuffer-p)
311 (setq beacon--previous-mark-head (car mark-ring))
312 (setq beacon--previous-place (point-marker))
313 (setq beacon--previous-window (selected-window))))
314
315 (defun beacon--window-scroll-function (win _start-pos)
316 "Blink the beacon or record that window has been scrolled.
317 If invoked during the command loop, record the current window so
318 that it may be blinked on post-command. This is because the
319 scrolled window might not be active, but we only know that at
320 `post-command-hook'.
321
322 If invoked outside the command loop, `post-command-hook' would be
323 unreliable, so just blink immediately."
324 (if this-command
325 (setq beacon--window-scrolled win)
326 (setq beacon--window-scrolled nil)
327 (beacon-blink)))
328
329 \f
330 ;;; Minor-mode
331 (defcustom beacon-lighter
332 (cond
333 ((char-displayable-p ?💡) " 💡")
334 ((char-displayable-p ?Λ) " Λ")
335 (t " *"))
336 "Lighter string used on the mode-line."
337 :type 'string)
338
339 ;;;###autoload
340 (define-minor-mode beacon-mode
341 nil nil beacon-lighter nil
342 :global t
343 (if beacon-mode
344 (progn
345 (add-hook 'window-scroll-functions #'beacon--window-scroll-function)
346 (add-hook 'post-command-hook #'beacon--post-command)
347 (add-hook 'pre-command-hook #'beacon--vanish))
348 (remove-hook 'window-scroll-functions #'beacon--window-scroll-function)
349 (remove-hook 'post-command-hook #'beacon--post-command)
350 (remove-hook 'pre-command-hook #'beacon--vanish)))
351
352 (provide 'beacon)
353 ;;; beacon.el ends here