reveal.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ;;; reveal.el --- Automatically reveal hidden text at point -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords: outlines
  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. ;; Reveal mode is a minor mode that makes sure that text around point
  18. ;; is always visible. When point enters a region of hidden text,
  19. ;; `reveal-mode' temporarily makes it visible.
  20. ;;
  21. ;; This is normally used in conjunction with `outline-minor-mode',
  22. ;; `hs-minor-mode', `hide-ifdef-mode', ...
  23. ;;
  24. ;; It only works with packages that hide text using overlays.
  25. ;; Packages can provide special support for it by placing
  26. ;; a function in the `reveal-toggle-invisible' property on the symbol
  27. ;; used as the value of the `invisible' overlay property.
  28. ;; The function is called right after revealing (or re-hiding) the
  29. ;; text with two arguments: the overlay and a boolean that's non-nil
  30. ;; if we have just revealed the text. When revealing, that function
  31. ;; may re-hide some of the text.
  32. ;;; Todo:
  33. ;; - find other hysteresis features.
  34. ;; - don't hide after a scroll command
  35. ;; - delay hiding by a couple seconds (i.e. hide in the background)
  36. ;;; Code:
  37. (defgroup reveal nil
  38. "Reveal hidden text on the fly."
  39. :group 'convenience)
  40. (defcustom reveal-around-mark t
  41. "Reveal text around the mark, if active."
  42. :type 'boolean
  43. :group 'reveal)
  44. (defvar reveal-open-spots nil
  45. "List of spots in the buffer which are open.
  46. Each element has the form (WINDOW . OVERLAY).")
  47. (make-variable-buffer-local 'reveal-open-spots)
  48. (defvar reveal-last-tick nil)
  49. (make-variable-buffer-local 'reveal-last-tick)
  50. ;; Actual code
  51. (defun reveal-post-command ()
  52. ;; Refresh the spots that might have changed.
  53. ;; `Refreshing' here means to try and re-hide the corresponding text.
  54. ;; We don't refresh everything correctly:
  55. ;; - we only refresh spots in the current window.
  56. ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
  57. (with-local-quit
  58. (condition-case err
  59. (let ((old-ols
  60. (delq nil
  61. (mapcar
  62. (lambda (x)
  63. ;; We refresh any spot in the current window as well
  64. ;; as any spots associated with a dead window or
  65. ;; a window which does not show this buffer any more.
  66. (cond
  67. ((eq (car x) (selected-window)) (cdr x))
  68. ((not (and (window-live-p (car x))
  69. (eq (window-buffer (car x)) (current-buffer))))
  70. ;; Adopt this since it's owned by a window that's
  71. ;; either not live or at least not showing this
  72. ;; buffer any more.
  73. (setcar x (selected-window))
  74. (cdr x))))
  75. reveal-open-spots))))
  76. (setq old-ols (reveal-open-new-overlays old-ols))
  77. (reveal-close-old-overlays old-ols))
  78. (error (message "Reveal: %s" err)))))
  79. (defun reveal-open-new-overlays (old-ols)
  80. (let ((repeat t))
  81. (while repeat
  82. (setq repeat nil)
  83. (dolist (ol (nconc (when (and reveal-around-mark mark-active)
  84. (overlays-at (mark)))
  85. (overlays-at (point))))
  86. (setq old-ols (delq ol old-ols))
  87. (when (overlay-start ol) ;Check it's still live.
  88. (let ((inv (overlay-get ol 'invisible)) open)
  89. (when (and inv
  90. ;; There's an `invisible' property. Make sure it's
  91. ;; actually invisible, and ellipsized.
  92. (and (consp buffer-invisibility-spec)
  93. (cdr (assq inv buffer-invisibility-spec)))
  94. (or (setq open
  95. (or (overlay-get ol 'reveal-toggle-invisible)
  96. (and (symbolp inv)
  97. (get inv 'reveal-toggle-invisible))
  98. (overlay-get ol 'isearch-open-invisible-temporary)))
  99. (overlay-get ol 'isearch-open-invisible)
  100. (and (consp buffer-invisibility-spec)
  101. (cdr (assq inv buffer-invisibility-spec))))
  102. (overlay-put ol 'reveal-invisible inv))
  103. (push (cons (selected-window) ol) reveal-open-spots)
  104. (if (null open)
  105. (overlay-put ol 'invisible nil)
  106. ;; Use the provided opening function and repeat (since the
  107. ;; opening function might have hidden a subpart around point
  108. ;; or moved/killed some of the overlays).
  109. (setq repeat t)
  110. (condition-case err
  111. (funcall open ol nil)
  112. (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
  113. open ol err)
  114. ;; Let's default to a meaningful behavior to avoid
  115. ;; getting stuck in an infinite loop.
  116. (setq repeat nil)
  117. (overlay-put ol 'invisible nil))))))))))
  118. old-ols)
  119. (defun reveal-close-old-overlays (old-ols)
  120. (if (not (eq reveal-last-tick
  121. (setq reveal-last-tick (buffer-modified-tick))))
  122. ;; The buffer was modified since last command: let's refrain from
  123. ;; closing any overlay because it tends to behave poorly when
  124. ;; inserting text at the end of an overlay (basically the overlay
  125. ;; should be rear-advance when it's open, but things like
  126. ;; outline-minor-mode make it non-rear-advance because it's
  127. ;; a better choice when it's closed).
  128. nil
  129. ;; The last command was only a point motion or some such
  130. ;; non-buffer-modifying command. Let's close whatever can be closed.
  131. (dolist (ol old-ols)
  132. (if (and (overlay-start ol) ;Check it's still live.
  133. (>= (point) (save-excursion
  134. (goto-char (overlay-start ol))
  135. (line-beginning-position 1)))
  136. (<= (point) (save-excursion
  137. (goto-char (overlay-end ol))
  138. (line-beginning-position 2)))
  139. ;; If the application has moved the overlay to some other
  140. ;; buffer, we'd better reset the buffer to its
  141. ;; original state.
  142. (eq (current-buffer) (overlay-buffer ol)))
  143. ;; Still near the overlay: keep it open.
  144. nil
  145. ;; Really close it.
  146. (let* ((inv (overlay-get ol 'reveal-invisible))
  147. (open (or (overlay-get ol 'reveal-toggle-invisible)
  148. (get inv 'reveal-toggle-invisible)
  149. (overlay-get ol 'isearch-open-invisible-temporary))))
  150. (if (and (overlay-start ol) ;Check it's still live.
  151. open)
  152. (condition-case err
  153. (funcall open ol t)
  154. (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
  155. open ol err)))
  156. (overlay-put ol 'invisible inv))
  157. ;; Remove the overlay from the list of open spots.
  158. (overlay-put ol 'reveal-invisible nil)
  159. (setq reveal-open-spots
  160. (delq (rassoc ol reveal-open-spots)
  161. reveal-open-spots)))))))
  162. (defvar reveal-mode-map
  163. (let ((map (make-sparse-keymap)))
  164. ;; Override the default move-beginning-of-line and move-end-of-line
  165. ;; which skips valuable invisible text.
  166. (define-key map [remap move-beginning-of-line] 'beginning-of-line)
  167. (define-key map [remap move-end-of-line] 'end-of-line)
  168. map))
  169. ;;;###autoload
  170. (define-minor-mode reveal-mode
  171. "Toggle uncloaking of invisible text near point (Reveal mode).
  172. With a prefix argument ARG, enable Reveal mode if ARG is
  173. positive, and disable it otherwise. If called from Lisp, enable
  174. Reveal mode if ARG is omitted or nil.
  175. Reveal mode is a buffer-local minor mode. When enabled, it
  176. reveals invisible text around point."
  177. :group 'reveal
  178. :lighter (global-reveal-mode nil " Reveal")
  179. :keymap reveal-mode-map
  180. (if reveal-mode
  181. (progn
  182. (set (make-local-variable 'search-invisible) t)
  183. (add-hook 'post-command-hook 'reveal-post-command nil t))
  184. (kill-local-variable 'search-invisible)
  185. (remove-hook 'post-command-hook 'reveal-post-command t)))
  186. ;;;###autoload
  187. (define-minor-mode global-reveal-mode
  188. "Toggle Reveal mode in all buffers (Global Reveal mode).
  189. Reveal mode renders invisible text around point visible again.
  190. With a prefix argument ARG, enable Global Reveal mode if ARG is
  191. positive, and disable it otherwise. If called from Lisp, enable
  192. the mode if ARG is omitted or nil."
  193. :global t :group 'reveal
  194. (setq-default reveal-mode global-reveal-mode)
  195. (if global-reveal-mode
  196. (progn
  197. (setq search-invisible t)
  198. (add-hook 'post-command-hook 'reveal-post-command))
  199. (setq search-invisible 'open) ;FIXME
  200. (remove-hook 'post-command-hook 'reveal-post-command)))
  201. (provide 'reveal)
  202. ;;; reveal.el ends here