pulse.el 9.0 KB

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