1 ;;; reveal.el --- Automatically reveal hidden text at point
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Reveal mode is a minor mode that makes sure that text around point
29 ;; is always visible. When point enters a region of hidden text,
30 ;; `reveal-mode' temporarily makes it visible.
32 ;; This is normally used in conjunction with `outline-minor-mode',
33 ;; `hs-minor-mode', `hide-ifdef-mode', ...
35 ;; It only works with packages that hide text using overlays.
36 ;; Packages can provide special support for it by placing
37 ;; a function in the `reveal-toggle-invisible' property on the symbol
38 ;; used as the value of the `invisible' overlay property.
39 ;; The function is called right after revealing (or re-hiding) the
40 ;; text with two arguments: the overlay and a boolean that's non-nil
41 ;; if we have just revealed the text. When revealing, that function
42 ;; may re-hide some of the text.
46 ;; - find other hysteresis features.
47 ;; - don't hide after a scroll command
48 ;; - delay hiding by a couple seconds (i.e. hide in the background)
53 "Reveal hidden text on the fly."
56 (defcustom reveal-around-mark t
57 "Reveal text around the mark, if active."
61 (defvar reveal-open-spots nil
62 "List of spots in the buffer which are open.
63 Each element has the form (WINDOW . OVERLAY).")
64 (make-variable-buffer-local 'reveal-open-spots
)
66 (defvar reveal-last-tick nil
)
67 (make-variable-buffer-local 'reveal-last-tick
)
71 (defun reveal-post-command ()
72 ;; Refresh the spots that might have changed.
73 ;; `Refreshing' here means to try and re-hide the corresponding text.
74 ;; We don't refresh everything correctly:
75 ;; - we only refresh spots in the current window.
76 ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
79 (let ((old-ols (delq nil
82 ;; We refresh any spot in the current window as
83 ;; well as any spots associated with a dead
84 ;; window or a window which does not show this
86 (if (or (eq (car x
) (selected-window))
87 (not (window-live-p (car x
)))
88 (not (eq (window-buffer (car x
))
96 (dolist (ol (nconc (when (and reveal-around-mark mark-active
)
98 (overlays-at (point))))
99 (setq old-ols
(delq ol old-ols
))
100 (let ((inv (overlay-get ol
'invisible
)) open
)
102 ;; There's an `invisible' property. Make sure it's
103 ;; actually invisible, and ellipsised.
104 (and (consp buffer-invisibility-spec
)
105 (cdr (assq inv buffer-invisibility-spec
)))
107 (or (overlay-get ol
'reveal-toggle-invisible
)
109 (get inv
'reveal-toggle-invisible
))
110 (overlay-get ol
'isearch-open-invisible-temporary
)))
111 (overlay-get ol
'isearch-open-invisible
)
112 (and (consp buffer-invisibility-spec
)
113 (cdr (assq inv buffer-invisibility-spec
))))
114 (overlay-put ol
'reveal-invisible inv
))
115 (push (cons (selected-window) ol
) reveal-open-spots
)
118 (overlay-put ol
'invisible nil
))
119 ;; Use the provided opening function and repeat (since the
120 ;; opening function might have hidden a subpart around point).
123 (funcall open ol nil
)
124 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
126 ;; Let's default to a meaningful behavior to avoid
127 ;; getting stuck in an infinite loop.
129 (overlay-put ol
'invisible nil
))))))))
130 ;; Close old overlays.
131 (if (not (eq reveal-last-tick
132 (setq reveal-last-tick
(buffer-modified-tick))))
133 ;; The buffer was modified since last command: let's refrain from
134 ;; closing any overlay because it tends to behave poorly when
135 ;; inserting text at the end of an overlay (basically the overlay
136 ;; should be rear-advance when it's open, but things like
137 ;; outline-minor-mode make it non-rear-advance because it's
138 ;; a better choice when it's closed).
140 ;; The last command was only a point motion or some such
141 ;; non-buffer-modifying command. Let's close whatever can be closed.
143 (if (and (>= (point) (save-excursion
144 (goto-char (overlay-start ol
))
145 (line-beginning-position 1)))
146 (<= (point) (save-excursion
147 (goto-char (overlay-end ol
))
148 (line-beginning-position 2)))
149 ;; If the application has moved the overlay to some other
150 ;; buffer, we'd better reset the buffer to its
152 (eq (current-buffer) (overlay-buffer ol
)))
153 ;; Still near the overlay: keep it open.
156 (let ((open (overlay-get ol
'reveal-toggle-invisible
)) inv
)
158 (and (setq inv
(overlay-get ol
'reveal-invisible
))
159 (setq open
(or (get inv
'reveal-toggle-invisible
)
160 (overlay-get ol
'isearch-open-invisible-temporary
)))))
163 (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
165 (overlay-put ol
'invisible inv
))
166 ;; Remove the olverlay from the list of open spots.
167 (setq reveal-open-spots
168 (delq (rassoc ol reveal-open-spots
)
169 reveal-open-spots
)))))))
170 (error (message "Reveal: %s" err
)))))
172 (defvar reveal-mode-map
173 (let ((map (make-sparse-keymap)))
174 ;; Override the default move-beginning-of-line and move-end-of-line
175 ;; which skips valuable invisible text.
176 (define-key map
[remap move-beginning-of-line
] 'beginning-of-line
)
177 (define-key map
[remap move-end-of-line
] 'end-of-line
)
181 (define-minor-mode reveal-mode
182 "Toggle Reveal mode on or off.
183 Reveal mode renders invisible text around point visible again.
185 Interactively, with no prefix argument, toggle the mode.
186 With universal prefix ARG (or if ARG is nil) turn mode on.
187 With zero or negative ARG turn mode off."
189 :lighter
(global-reveal-mode nil
" Reveal")
190 :keymap reveal-mode-map
193 (set (make-local-variable 'search-invisible
) t
)
194 (add-hook 'post-command-hook
'reveal-post-command nil t
))
195 (kill-local-variable 'search-invisible
)
196 (remove-hook 'post-command-hook
'reveal-post-command t
)))
199 (define-minor-mode global-reveal-mode
200 "Toggle Reveal mode in all buffers on or off.
201 Reveal mode renders invisible text around point visible again.
203 Interactively, with no prefix argument, toggle the mode.
204 With universal prefix ARG (or if ARG is nil) turn mode on.
205 With zero or negative ARG turn mode off."
206 :global t
:group
'reveal
207 (setq-default reveal-mode global-reveal-mode
)
208 (if global-reveal-mode
210 (setq search-invisible t
)
211 (add-hook 'post-command-hook
'reveal-post-command
))
212 (setq search-invisible
'open
) ;FIXME
213 (remove-hook 'post-command-hook
'reveal-post-command
)))
217 ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
218 ;;; reveal.el ends here