Merge branch 'master' into comment-cache
[emacs.git] / lisp / cedet / pulse.el
blob913c183a7e6753c5c35e4d1864b2945774ce814f
1 ;;; pulse.el --- Pulsing Overlays
3 ;;; Copyright (C) 2007-2017 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6 ;; Version: 1.0
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Manage temporary pulsing of faces and overlays.
27 ;; This is a temporal decoration technique where something is to be
28 ;; highlighted briefly. This adds a gentle pulsing style to the text
29 ;; decorated this way.
31 ;; The following are useful entry points:
33 ;; `pulse' - Cause `pulse-highlight-face' to shift toward background color.
34 ;; Assumes you are using a version of Emacs that supports pulsing.
37 ;; `pulse-momentary-highlight-one-line' - Pulse a single line at POINT.
38 ;; `pulse-momentary-highlight-region' - Pulse a region.
39 ;; `pulse-momentary-highlight-overlay' - Pulse an overlay.
40 ;; These three functions will just blink the specified area if
41 ;; the version of Emacs you are using doesn't support pulsing.
43 ;; `pulse-line-hook-function' - A simple function that can be used in a
44 ;; hook that will pulse whatever line the cursor is on.
46 ;;; History:
48 ;; The original pulse code was written for semantic tag highlighting.
49 ;; It has been extracted, and adapted for general purpose pulsing.
51 ;; Pulse is a part of CEDET. http://cedet.sf.net
53 (defun pulse-available-p ()
54 "Return non-nil if pulsing is available on the current frame."
55 (condition-case nil
56 (let ((v (color-values (face-background 'default))))
57 (numberp (car-safe v)))
58 (error nil)))
60 (defcustom pulse-flag (pulse-available-p)
61 "Whether to use pulsing for momentary highlighting.
62 Pulsing involves a bright highlight that slowly shifts to the
63 background color.
65 If the value is nil, highlight with an unchanging color until a
66 key is pressed.
67 If the value is `never', do no coloring at all.
68 Any other value means to do the default pulsing behavior.
70 If `pulse-flag' is non-nil, but `pulse-available-p' is nil, then
71 this flag is ignored."
72 :group 'pulse
73 :type 'boolean)
75 (defface pulse-highlight-start-face
76 '((((class color) (background dark))
77 (:background "#AAAA33"))
78 (((class color) (background light))
79 (:background "#FFFFAA")))
80 "Face used at beginning of a highlight."
81 :group 'pulse)
83 (defface pulse-highlight-face
84 '((((class color) (background dark))
85 (:background "#AAAA33"))
86 (((class color) (background light))
87 (:background "#FFFFAA")))
88 "Face used during a pulse for display. *DO NOT CUSTOMIZE*
89 Face used for temporary highlighting of tags for effect."
90 :group 'pulse)
92 ;;; Code:
94 (defun pulse-int-to-hex (int &optional nb-digits)
95 "Convert integer argument INT to a #XXXXXXXXXXXX format hex string.
96 Each X in the output string is a hexadecimal digit.
97 NB-DIGITS is the number of hex digits. If INT is too large to be
98 represented with NB-DIGITS, then the result is truncated from the
99 left. So, for example, INT=256 and NB-DIGITS=2 returns \"00\", since
100 the hex equivalent of 256 decimal is 100, which is more than 2 digits.
102 This function was blindly copied from hexrgb.el by Drew Adams.
103 http://www.emacswiki.org/cgi-bin/wiki/hexrgb.el"
104 (setq nb-digits (or nb-digits 4))
105 (substring (format (concat "%0" (int-to-string nb-digits) "X") int) (- nb-digits)))
107 (defun pulse-color-values-to-hex (values)
108 "Convert list of rgb color VALUES to a hex string, #XXXXXXXXXXXX.
109 Each X in the string is a hexadecimal digit.
110 Input VALUES is as for the output of `x-color-values'.
112 This function was blindly copied from hexrgb.el by Drew Adams.
113 http://www.emacswiki.org/cgi-bin/wiki/hexrgb.el"
114 (concat "#"
115 (pulse-int-to-hex (nth 0 values) 4) ; red
116 (pulse-int-to-hex (nth 1 values) 4) ; green
117 (pulse-int-to-hex (nth 2 values) 4))) ; blue
119 (defcustom pulse-iterations 10
120 "Number of iterations in a pulse operation."
121 :group 'pulse
122 :type 'number)
123 (defcustom pulse-delay .03
124 "Delay between face lightening iterations."
125 :group 'pulse
126 :type 'number)
128 (defun pulse-lighten-highlight ()
129 "Lighten the face by 1/`pulse-iterations' toward the background color.
130 Return t if there is more drift to do, nil if completed."
131 (if (>= (get 'pulse-highlight-face :iteration) pulse-iterations)
133 (let* ((frame (color-values (face-background 'default)))
134 (pulse-background (face-background
135 (get 'pulse-highlight-face
136 :startface)
137 nil t)));; can be nil
138 (when pulse-background
139 (let* ((start (color-values pulse-background))
140 (frac (list (/ (- (nth 0 frame) (nth 0 start)) pulse-iterations)
141 (/ (- (nth 1 frame) (nth 1 start)) pulse-iterations)
142 (/ (- (nth 2 frame) (nth 2 start)) pulse-iterations)))
143 (it (get 'pulse-highlight-face :iteration))
145 (set-face-background 'pulse-highlight-face
146 (pulse-color-values-to-hex
147 (list
148 (+ (nth 0 start) (* (nth 0 frac) it))
149 (+ (nth 1 start) (* (nth 1 frac) it))
150 (+ (nth 2 start) (* (nth 2 frac) it)))))
151 (put 'pulse-highlight-face :iteration (1+ it))
152 (if (>= (1+ it) pulse-iterations)
154 t)))
157 (defun pulse-reset-face (&optional face)
158 "Reset the pulse highlighting FACE."
159 (set-face-background 'pulse-highlight-face
160 (if face
161 (face-background face nil t)
162 (face-background 'pulse-highlight-start-face)
164 (put 'pulse-highlight-face :startface (or face
165 'pulse-highlight-start-face))
166 (put 'pulse-highlight-face :iteration 0))
168 ;;; Convenience Functions
170 (defvar pulse-momentary-overlay nil
171 "The current pulsing overlay.")
173 (defvar pulse-momentary-timer nil
174 "The current pulsing timer.")
176 (defun pulse-momentary-highlight-overlay (o &optional face)
177 "Pulse the overlay O, unhighlighting before next command.
178 Optional argument FACE specifies the face to do the highlighting."
179 ;; We don't support simultaneous highlightings.
180 (pulse-momentary-unhighlight)
181 (overlay-put o 'original-face (overlay-get o 'face))
182 (setq pulse-momentary-overlay o)
183 (if (eq pulse-flag 'never)
185 (if (or (not pulse-flag) (not (pulse-available-p)))
186 ;; Provide a face... clear on next command
187 (progn
188 (overlay-put o 'face (or face 'pulse-highlight-start-face))
189 (add-hook 'pre-command-hook
190 'pulse-momentary-unhighlight))
191 ;; Pulse it.
192 (overlay-put o 'face 'pulse-highlight-face)
193 ;; The pulse function puts FACE onto 'pulse-highlight-face.
194 ;; Thus above we put our face on the overlay, but pulse
195 ;; with a reference face needed for the color.
196 (pulse-reset-face face)
197 (setq pulse-momentary-timer
198 (run-with-timer 0 pulse-delay #'pulse-tick
199 (time-add (current-time)
200 (* pulse-delay pulse-iterations)))))))
202 (defun pulse-tick (stop-time)
203 (if (time-less-p (current-time) stop-time)
204 (pulse-lighten-highlight)
205 (pulse-momentary-unhighlight)))
207 (defun pulse-momentary-unhighlight ()
208 "Unhighlight a line recently highlighted."
209 (when pulse-momentary-overlay
210 ;; clear the starting face
211 (let ((ol pulse-momentary-overlay))
212 (overlay-put ol 'face (overlay-get ol 'original-face))
213 (overlay-put ol 'original-face nil)
214 ;; Clear the overlay if it needs deleting.
215 (when (overlay-get ol 'pulse-delete) (delete-overlay ol)))
217 ;; Clear the variable.
218 (setq pulse-momentary-overlay nil)
220 ;; Reset the pulsing face.
221 (pulse-reset-face))
223 ;; Cancel the timer.
224 (when pulse-momentary-timer
225 (cancel-timer pulse-momentary-timer))
227 ;; Remove this hook.
228 (remove-hook 'pre-command-hook 'pulse-momentary-unhighlight))
230 ;;;###autoload
231 (defun pulse-momentary-highlight-one-line (point &optional face)
232 "Highlight the line around POINT, unhighlighting before next command.
233 Optional argument FACE specifies the face to do the highlighting."
234 (save-excursion
235 (goto-char point)
236 (let ((start (point-at-bol))
237 (end (save-excursion
238 (end-of-line)
239 (when (not (eobp))
240 (forward-char 1))
241 (point))))
242 (pulse-momentary-highlight-region start end face))))
244 ;;;###autoload
245 (defun pulse-momentary-highlight-region (start end &optional face)
246 "Highlight between START and END, unhighlighting before next command.
247 Optional argument FACE specifies the face to do the highlighting."
248 (let ((o (make-overlay start end)))
249 ;; Mark it for deletion
250 (overlay-put o 'pulse-delete t)
251 (pulse-momentary-highlight-overlay o face)))
253 ;;; Random integration with other tools
255 (defvar pulse-command-advice-flag nil)
257 (defun pulse-line-hook-function ()
258 "Function used in hooks to pulse the current line.
259 Only pulses the line if `pulse-command-advice-flag' is non-nil."
260 (when pulse-command-advice-flag
261 (pulse-momentary-highlight-one-line (point))))
263 (provide 'pulse)
265 ;;; pulse.el ends here