1 ;;; beacon.el --- Highlight the cursor whenever it moves long distances -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Artur Malabarba
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/beacon
7 ;; Keywords: convenience
9 ;; Package-Requires: ((cl-lib "0.5"))
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.
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.
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/>.
26 ;; This is a global minor-mode. Turn it on everywhere with
34 "Customization group for beacon."
38 (defvar beacon--timer nil)
40 (defcustom beacon-push-mark nil
41 "Should the mark be pushed before long movements?
42 If nil, `beacon' will not push the mark.
43 Otherwise this should be a number, and `beacon' will push the
44 mark whenever point moves more than that many lines."
45 :type '(choice integer (const nil)))
47 (defcustom beacon-blink-when-point-moves nil
48 "Should the beacon blink when moving a long distance?
49 If nil, don't blink due to plain movement.
50 If non-nil, this should be an integer, which is the minimum
51 movement distance (in lines) that triggers a beacon blink."
52 :type '(choice integer (const nil)))
54 (defcustom beacon-blink-when-buffer-changes t
55 "Should the beacon blink when changing buffer?"
58 (defcustom beacon-blink-when-window-scrolls t
59 "Should the beacon blink when the window scrolls?"
62 (defcustom beacon-blink-duration 0.3
63 "Time, in seconds, that the blink should last."
66 (defcustom beacon-blink-delay 0.3
67 "Time, in seconds, before starting to fade the beacon."
70 (defcustom beacon-size 40
71 "Size of the beacon in characters."
74 (defcustom beacon-color 0.5
76 This can be a string or a number.
78 If it is a number, the color is taken to be white or
79 black (depending on the current theme's background) and this
80 number is a float between 0 and 1 specifing the brightness.
82 If it is a string, it is a color name or specification,
84 :type '(choice number color))
88 (defvar beacon--ovs nil)
90 (defun beacon--colored-overlay (color)
91 "Put an overlay at point with background COLOR."
92 (let ((ov (make-overlay (point) (1+ (point)))))
93 (overlay-put ov 'face (list :background color))
94 (overlay-put ov 'beacon t)
95 (push ov beacon--ovs)))
97 (defun beacon--ov-at-point ()
98 (car (cl-member-if (lambda (o) (overlay-get o 'beacon))
99 (overlays-at (point)))))
101 (defun beacon--vanish ()
102 "Turn off the beacon."
103 (when (timerp beacon--timer)
104 (cancel-timer beacon--timer))
105 (mapc #'delete-overlay beacon--ovs)
106 (setq beacon--ovs nil))
110 (defun beacon--int-range (a b)
111 "Return a list of integers between A inclusive and B exclusive.
112 Only returns `beacon-size' elements."
113 (let ((d (/ (- b a) beacon-size))
115 (dotimes (_ (1- beacon-size))
116 (push (+ (car out) d) out))
119 (defun beacon--color-range ()
120 "Return a list of background colors for the beacon."
121 (let* ((bg (color-values (face-attribute 'default :background)))
123 ((stringp beacon-color) (color-values beacon-color))
124 ((< (color-distance "black" bg)
125 (color-distance "white" bg))
126 (make-list 3 (* beacon-color 65535)))
127 (t (make-list 3 (* (- 1 beacon-color) 65535))))))
128 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
129 (mapcar (lambda (n) (butlast (beacon--int-range (elt fg n) (elt bg n))))
134 (defun beacon--shine ()
135 "Shine a beacon at point."
136 (let ((colors (beacon--color-range)))
141 ;; (beacon--after-string)
143 (beacon--colored-overlay (pop colors))
144 (forward-char 1))))))
146 (defun beacon--dec ()
147 "Decrease the beacon brightness by one."
148 (let ((o (beacon--ov-at-point)))
153 (while (progn (forward-char 1)
154 (setq o (beacon--ov-at-point)))
155 (move-overlay o (1- (point)) (point)))))))
157 (defun beacon-blink ()
158 "Blink the beacon at the position of the cursor."
163 (run-at-time beacon-blink-delay
164 (/ beacon-blink-duration 1.0 beacon-size)
168 ;;; Movement detection
169 (defvar beacon--previous-place nil)
170 (defvar beacon--previous-window-start nil)
171 (defvar beacon--previous-mark-head nil)
173 (defun beacon--movement-> (delta)
174 "Return non-nil if latest point movement is > DELTA.
175 If DELTA is nil, return nil."
177 (equal (marker-buffer beacon--previous-place)
179 (> (abs (- (point) beacon--previous-place))
181 (> (count-screen-lines (min (point) beacon--previous-place)
182 (max (point) beacon--previous-place))
185 (defun beacon--maybe-push-mark ()
186 "Push mark if it seems to be safe."
187 (when (and (not mark-active)
188 (beacon--movement-> beacon-push-mark))
189 (let ((head (car mark-ring)))
190 (when (and (eq beacon--previous-mark-head head)
191 (not (equal head beacon--previous-place)))
192 (push-mark beacon--previous-place)))))
194 (defun beacon--post-command ()
195 "Blink if point moved very far."
197 ((not (markerp beacon--previous-place))
199 ;; Blink because we changed buffer.
200 ((not (equal (marker-buffer beacon--previous-place)
202 (when beacon-blink-when-buffer-changes
203 (unless (window-minibuffer-p)
205 ;; Blink for scrolling.
206 ((and beacon-blink-when-window-scrolls
208 (not (equal beacon--previous-window-start (window-start)))))
210 ;; Blink for movement
211 ((beacon--movement-> beacon-blink-when-point-moves)
213 ;; Even if we don't blink, vanish any previous beacon.
214 (t (beacon--vanish)))
215 (beacon--maybe-push-mark)
216 (unless (window-minibuffer-p)
217 (setq beacon--previous-window-start (window-start))
218 (setq beacon--previous-mark-head (car mark-ring))
219 (setq beacon--previous-place (point-marker))))
223 (defcustom beacon-lighter (cond
224 ((char-displayable-p ?💡) "💡")
225 ((char-displayable-p ?Λ) "Λ")
227 "Lighter string used on the mode-line."
231 (define-minor-mode beacon-mode
232 nil nil beacon-lighter nil
235 (add-hook 'post-command-hook #'beacon--post-command)
236 (remove-hook 'post-command-hook #'beacon--post-command)))
239 ;;; beacon.el ends here