reveal.el 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; reveal.el --- Automatically reveal hidden text at point -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2000-2017 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. (with-demoted-errors "Reveal: %s"
  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))
  70. (current-buffer))))
  71. ;; Adopt this since it's owned by a window that's
  72. ;; either not live or at least not showing this
  73. ;; buffer any more.
  74. (setcar x (selected-window))
  75. (cdr x))))
  76. reveal-open-spots))))
  77. (setq old-ols (reveal-open-new-overlays old-ols))
  78. (reveal-close-old-overlays old-ols)))))
  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 (or track-mouse ;Don't close in the middle of a click.
  121. (not (eq reveal-last-tick
  122. (setq reveal-last-tick (buffer-modified-tick)))))
  123. ;; The buffer was modified since last command: let's refrain from
  124. ;; closing any overlay because it tends to behave poorly when
  125. ;; inserting text at the end of an overlay (basically the overlay
  126. ;; should be rear-advance when it's open, but things like
  127. ;; outline-minor-mode make it non-rear-advance because it's
  128. ;; a better choice when it's closed).
  129. nil
  130. ;; The last command was only a point motion or some such
  131. ;; non-buffer-modifying command. Let's close whatever can be closed.
  132. (dolist (ol old-ols)
  133. (if (and (overlay-start ol) ;Check it's still live.
  134. (>= (point) (save-excursion
  135. (goto-char (overlay-start ol))
  136. (line-beginning-position 1)))
  137. (<= (point) (save-excursion
  138. (goto-char (overlay-end ol))
  139. (line-beginning-position 2)))
  140. ;; If the application has moved the overlay to some other
  141. ;; buffer, we'd better reset the buffer to its
  142. ;; original state.
  143. (eq (current-buffer) (overlay-buffer ol)))
  144. ;; Still near the overlay: keep it open.
  145. nil
  146. ;; Really close it.
  147. (let* ((inv (overlay-get ol 'reveal-invisible))
  148. (open (or (overlay-get ol 'reveal-toggle-invisible)
  149. (get inv 'reveal-toggle-invisible)
  150. (overlay-get ol 'isearch-open-invisible-temporary))))
  151. (if (and (overlay-start ol) ;Check it's still live.
  152. open)
  153. (condition-case err
  154. (funcall open ol t)
  155. (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
  156. open ol err)))
  157. (overlay-put ol 'invisible inv))
  158. ;; Remove the overlay from the list of open spots.
  159. (overlay-put ol 'reveal-invisible nil)
  160. (setq reveal-open-spots
  161. (delq (rassoc ol reveal-open-spots)
  162. reveal-open-spots)))))))
  163. (defvar reveal-mode-map
  164. (let ((map (make-sparse-keymap)))
  165. ;; Override the default move-beginning-of-line and move-end-of-line
  166. ;; which skips valuable invisible text.
  167. (define-key map [remap move-beginning-of-line] 'beginning-of-line)
  168. (define-key map [remap move-end-of-line] 'end-of-line)
  169. map))
  170. ;;;###autoload
  171. (define-minor-mode reveal-mode
  172. "Toggle uncloaking of invisible text near point (Reveal mode).
  173. With a prefix argument ARG, enable Reveal mode if ARG is
  174. positive, and disable it otherwise. If called from Lisp, enable
  175. Reveal mode if ARG is omitted or nil.
  176. Reveal mode is a buffer-local minor mode. When enabled, it
  177. reveals invisible text around point."
  178. :group 'reveal
  179. :lighter (global-reveal-mode nil " Reveal")
  180. :keymap reveal-mode-map
  181. (if reveal-mode
  182. (progn
  183. (set (make-local-variable 'search-invisible) t)
  184. (add-hook 'post-command-hook 'reveal-post-command nil t))
  185. (kill-local-variable 'search-invisible)
  186. (remove-hook 'post-command-hook 'reveal-post-command t)))
  187. ;;;###autoload
  188. (define-minor-mode global-reveal-mode
  189. "Toggle Reveal mode in all buffers (Global Reveal mode).
  190. Reveal mode renders invisible text around point visible again.
  191. With a prefix argument ARG, enable Global Reveal mode if ARG is
  192. positive, and disable it otherwise. If called from Lisp, enable
  193. the mode if ARG is omitted or nil."
  194. :global t :group 'reveal
  195. (setq-default reveal-mode global-reveal-mode)
  196. (if global-reveal-mode
  197. (progn
  198. (setq search-invisible t)
  199. (add-hook 'post-command-hook 'reveal-post-command))
  200. (setq search-invisible 'open) ;FIXME
  201. (remove-hook 'post-command-hook 'reveal-post-command)))
  202. (provide 'reveal)
  203. ;;; reveal.el ends here