]> code.delx.au - gnu-emacs-elpa/blob - beacon.el
Fix docs
[gnu-emacs-elpa] / beacon.el
1 ;;; beacon.el --- Highlight the cursor whenever it moves long distances -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Artur Malabarba
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; URL: https://github.com/Malabarba/beacon
7 ;; Keywords: convenience
8 ;; Version: 0.1
9 ;; Package-Requires: ((cl-lib "0.5"))
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 ;; (beacon-mode 1)
28
29 ;;; Code:
30
31 (require 'cl-lib)
32
33 (defgroup beacon nil
34 "Customization group for beacon."
35 :group 'emacs
36 :prefix "beacon-")
37
38 (defvar beacon--timer nil)
39
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)))
46
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)))
53
54 (defcustom beacon-blink-when-buffer-changes t
55 "Should the beacon blink when changing buffer?"
56 :type 'boolean)
57
58 (defcustom beacon-blink-when-window-scrolls t
59 "Should the beacon blink when the window scrolls?"
60 :type 'boolean)
61
62 (defcustom beacon-blink-duration 0.3
63 "Time, in seconds, that the blink should last."
64 :type 'number)
65
66 (defcustom beacon-blink-delay 0.3
67 "Time, in seconds, before starting to fade the beacon."
68 :type 'number)
69
70 (defcustom beacon-size 40
71 "Size of the beacon in characters."
72 :type 'number)
73
74 (defcustom beacon-brightness 0.4
75 "Brightness as a float between 0 and 1."
76 :type 'number)
77
78 \f
79 ;;; Overlays
80 (defvar beacon--ovs nil)
81
82 (defun beacon--colored-overlay (color)
83 "Put an overlay at point with background COLOR."
84 (let ((ov (make-overlay (point) (1+ (point)))))
85 (overlay-put ov 'face (list :background color))
86 (overlay-put ov 'beacon t)
87 (push ov beacon--ovs)))
88
89 (defun beacon--ov-at-point ()
90 (car (cl-member-if (lambda (o) (overlay-get o 'beacon))
91 (overlays-at (point)))))
92
93 (defun beacon--vanish ()
94 "Turn off the beacon."
95 (when (timerp beacon--timer)
96 (cancel-timer beacon--timer))
97 (mapc #'delete-overlay beacon--ovs)
98 (setq beacon--ovs nil))
99
100 \f
101 ;;; Colors
102 (defun beacon--int-range (a b)
103 "Return a list of integers between A inclusive and B exclusive.
104 Only returns `beacon-size' elements."
105 (let ((d (/ (- b a) beacon-size))
106 (out (list a)))
107 (dotimes (_ (1- beacon-size))
108 (push (+ (car out) d) out))
109 (nreverse out)))
110
111 (defun beacon--color-range ()
112 "Return a list of background colors for the beacon."
113 (let ((bg (color-values (face-attribute 'default :background))))
114 (apply #'cl-mapcar (lambda (r g b) (format "#%04x%04x%04x" r g b))
115 (if (< (color-distance "black" bg)
116 (color-distance "white" bg))
117 (mapcar (lambda (n) (butlast (beacon--int-range (* beacon-brightness 65535) n))) bg)
118 (mapcar (lambda (n) (cdr (beacon--int-range (* (- 1 beacon-brightness) 65535) n))) bg)))))
119
120 \f
121 ;;; Blinking
122 (defun beacon--shine ()
123 "Shine a beacon at point."
124 (let ((colors (beacon--color-range)))
125 (save-excursion
126 (while colors
127 (if (looking-at "$")
128 (progn
129 ;; (beacon--after-string)
130 (setq colors nil))
131 (beacon--colored-overlay (pop colors))
132 (forward-char 1))))))
133
134 (defun beacon--dec ()
135 "Decrease the beacon brightness by one."
136 (let ((o (beacon--ov-at-point)))
137 (if (not o)
138 (beacon--vanish)
139 (delete-overlay o)
140 (save-excursion
141 (while (progn (forward-char 1)
142 (setq o (beacon--ov-at-point)))
143 (move-overlay o (1- (point)) (point)))))))
144
145 (defun beacon-blink ()
146 "Blink the beacon at the position of the cursor."
147 (interactive)
148 (beacon--vanish)
149 (beacon--shine)
150 (setq beacon--timer
151 (run-at-time beacon-blink-delay
152 (/ beacon-blink-duration 1.0 beacon-size)
153 #'beacon--dec)))
154
155 \f
156 ;;; Movement detection
157 (defvar beacon--previous-place nil)
158 (defvar beacon--previous-window-start nil)
159 (defvar beacon--previous-mark-head nil)
160
161 (defun beacon--maybe-push-mark ()
162 "Push mark if it seems to be safe."
163 (when (and beacon-push-mark
164 (not mark-active))
165 (let ((head (car mark-ring)))
166 (when (and (eq beacon--previous-mark-head head)
167 (not (equal head beacon--previous-place)))
168 (push-mark beacon--previous-place)))))
169
170 (defun beacon--post-command ()
171 "Blink if point moved very far."
172 (cond
173 ((not (markerp beacon--previous-place))
174 (beacon--vanish))
175 ;; Blink because we changed buffer.
176 ((not (equal (marker-buffer beacon--previous-place)
177 (current-buffer)))
178 (when beacon-blink-when-buffer-changes
179 (unless (window-minibuffer-p)
180 (beacon-blink))))
181 ;; Blink for scrolling.
182 ((and beacon-blink-when-window-scrolls
183 (progn (redisplay)
184 (not (equal beacon--previous-window-start (window-start)))))
185 (beacon--maybe-push-mark)
186 (beacon-blink))
187 ;; Blink for movement
188 ((and beacon-blink-when-point-moves
189 (> (abs (- (point) beacon--previous-place))
190 beacon-blink-when-point-moves)
191 (> (count-screen-lines (min (point) beacon--previous-place)
192 (max (point) beacon--previous-place))))
193 (beacon--maybe-push-mark)
194 (beacon-blink))
195 ;; Even if we don't blink, vanish any previous beacon.
196 (t (beacon--vanish)))
197 (unless (window-minibuffer-p)
198 (setq beacon--previous-window-start (window-start))
199 (setq beacon--previous-mark-head (car mark-ring))
200 (setq beacon--previous-place (point-marker))))
201
202 \f
203 ;;; Minor-mode
204 (defcustom beacon-lighter (cond
205 ((char-displayable-p ?💡) "💡")
206 ((char-displayable-p ?Λ) "Λ")
207 (t "*"))
208 "Lighter string used on the mode-line."
209 :type 'string)
210
211 ;;;###autoload
212 (define-minor-mode beacon-mode
213 nil nil beacon-lighter nil
214 :global t
215 (if beacon-mode
216 (add-hook 'post-command-hook #'beacon--post-command)
217 (remove-hook 'post-command-hook #'beacon--post-command)))
218
219 (provide 'beacon)
220 ;;; beacon.el ends here