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