pulse.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; pulse.el --- Pulsing Overlays
  2. ;;; Copyright (C) 2007-2017 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."
  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. (pulse-background (face-background
  116. (get 'pulse-highlight-face
  117. :startface)
  118. nil t)));; can be nil
  119. (when pulse-background
  120. (let* ((start (color-values pulse-background))
  121. (frac (list (/ (- (nth 0 frame) (nth 0 start)) pulse-iterations)
  122. (/ (- (nth 1 frame) (nth 1 start)) pulse-iterations)
  123. (/ (- (nth 2 frame) (nth 2 start)) pulse-iterations)))
  124. (it (get 'pulse-highlight-face :iteration))
  125. )
  126. (set-face-background 'pulse-highlight-face
  127. (pulse-color-values-to-hex
  128. (list
  129. (+ (nth 0 start) (* (nth 0 frac) it))
  130. (+ (nth 1 start) (* (nth 1 frac) it))
  131. (+ (nth 2 start) (* (nth 2 frac) it)))))
  132. (put 'pulse-highlight-face :iteration (1+ it))
  133. (if (>= (1+ it) pulse-iterations)
  134. nil
  135. t)))
  136. )))
  137. (defun pulse-reset-face (&optional face)
  138. "Reset the pulse highlighting FACE."
  139. (set-face-background 'pulse-highlight-face
  140. (if face
  141. (face-background face nil t)
  142. (face-background 'pulse-highlight-start-face)
  143. ))
  144. (put 'pulse-highlight-face :startface (or face
  145. 'pulse-highlight-start-face))
  146. (put 'pulse-highlight-face :iteration 0))
  147. ;;; Convenience Functions
  148. ;;
  149. (defvar pulse-momentary-overlay nil
  150. "The current pulsing overlay.")
  151. (defvar pulse-momentary-timer nil
  152. "The current pulsing timer.")
  153. (defun pulse-momentary-highlight-overlay (o &optional face)
  154. "Pulse the overlay O, unhighlighting before next command.
  155. Optional argument FACE specifies the face to do the highlighting."
  156. ;; We don't support simultaneous highlightings.
  157. (pulse-momentary-unhighlight)
  158. (overlay-put o 'original-face (overlay-get o 'face))
  159. (setq pulse-momentary-overlay o)
  160. (if (eq pulse-flag 'never)
  161. nil
  162. (if (or (not pulse-flag) (not (pulse-available-p)))
  163. ;; Provide a face... clear on next command
  164. (progn
  165. (overlay-put o 'face (or face 'pulse-highlight-start-face))
  166. (add-hook 'pre-command-hook
  167. 'pulse-momentary-unhighlight))
  168. ;; Pulse it.
  169. (overlay-put o 'face 'pulse-highlight-face)
  170. ;; The pulse function puts FACE onto 'pulse-highlight-face.
  171. ;; Thus above we put our face on the overlay, but pulse
  172. ;; with a reference face needed for the color.
  173. (pulse-reset-face face)
  174. (setq pulse-momentary-timer
  175. (run-with-timer 0 pulse-delay #'pulse-tick
  176. (time-add (current-time)
  177. (* pulse-delay pulse-iterations)))))))
  178. (defun pulse-tick (stop-time)
  179. (if (time-less-p (current-time) stop-time)
  180. (pulse-lighten-highlight)
  181. (pulse-momentary-unhighlight)))
  182. (defun pulse-momentary-unhighlight ()
  183. "Unhighlight a line recently highlighted."
  184. (when pulse-momentary-overlay
  185. ;; clear the starting face
  186. (let ((ol pulse-momentary-overlay))
  187. (overlay-put ol 'face (overlay-get ol 'original-face))
  188. (overlay-put ol 'original-face nil)
  189. ;; Clear the overlay if it needs deleting.
  190. (when (overlay-get ol 'pulse-delete) (delete-overlay ol)))
  191. ;; Clear the variable.
  192. (setq pulse-momentary-overlay nil)
  193. ;; Reset the pulsing face.
  194. (pulse-reset-face))
  195. ;; Cancel the timer.
  196. (when pulse-momentary-timer
  197. (cancel-timer pulse-momentary-timer))
  198. ;; Remove this hook.
  199. (remove-hook 'pre-command-hook 'pulse-momentary-unhighlight))
  200. ;;;###autoload
  201. (defun pulse-momentary-highlight-one-line (point &optional face)
  202. "Highlight the line around POINT, unhighlighting before next command.
  203. Optional argument FACE specifies the face to do the highlighting."
  204. (save-excursion
  205. (goto-char point)
  206. (let ((start (point-at-bol))
  207. (end (save-excursion
  208. (end-of-line)
  209. (when (not (eobp))
  210. (forward-char 1))
  211. (point))))
  212. (pulse-momentary-highlight-region start end face))))
  213. ;;;###autoload
  214. (defun pulse-momentary-highlight-region (start end &optional face)
  215. "Highlight between START and END, unhighlighting before next command.
  216. Optional argument FACE specifies the face to do the highlighting."
  217. (let ((o (make-overlay start end)))
  218. ;; Mark it for deletion
  219. (overlay-put o 'pulse-delete t)
  220. (pulse-momentary-highlight-overlay o face)))
  221. ;;; Random integration with other tools
  222. (defvar pulse-command-advice-flag nil)
  223. (defun pulse-line-hook-function ()
  224. "Function used in hooks to pulse the current line.
  225. Only pulses the line if `pulse-command-advice-flag' is non-nil."
  226. (when pulse-command-advice-flag
  227. (pulse-momentary-highlight-one-line (point))))
  228. (provide 'pulse)
  229. ;;; pulse.el ends here