* tutorial.el (help-with-tutorial): Use TUTORIAL.xx for the tutorial
[bpt/emacs.git] / lisp / reveal.el
CommitLineData
a0aa2a49
SM
1;;; reveal.el --- Automatically reveal hidden text at point
2
0d30b337 3;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
a0aa2a49 5
cc1eecfd 6;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
a0aa2a49
SM
7;; Keywords: outlines
8
041f4d74
PJ
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
a0aa2a49 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
a0aa2a49 15
7490a1e4 16;; GNU Emacs is distributed in the hope that it will be useful,
a0aa2a49
SM
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
a0aa2a49
SM
23
24;;; Commentary:
25
26;; Reveal mode is a minor mode that makes sure that text around point
27;; is always visible. When point enters a region of hidden text,
28;; `reveal-mode' temporarily makes it visible.
29;;
30;; This is normally used in conjunction with `outline-minor-mode',
31;; `hs-minor-mode', `hide-ifdef-mode', ...
32;;
33;; It only works with packages that hide text using overlays.
34;; Packages can provide special support for it by placing
35;; a function in the `reveal-toggle-invisible' property on the symbol
36;; used as the value of the `invisible' overlay property.
37;; The function is called right after revealing (or re-hiding) the
38;; text with two arguments: the overlay and a boolean that's non-nil
39;; if we have just revealed the text. When revealing, that function
40;; may re-hide some of the text.
41
42;;; Todo:
43
44;; - find other hysteresis features.
4acbd507
SM
45;; - don't hide after a scroll command
46;; - delay hiding by a couple seconds (i.e. hide in the background)
a0aa2a49
SM
47
48;;; Code:
49
a0aa2a49
SM
50(defgroup reveal nil
51 "Reveal hidden text on the fly."
52 :group 'editing)
53
54(defcustom reveal-around-mark t
55 "Reveal text around the mark, if active."
f5307782
JB
56 :type 'boolean
57 :group 'reveal)
a0aa2a49 58
4acbd507
SM
59(defvar reveal-open-spots nil
60 "List of spots in the buffer which are open.
61Each element has the form (WINDOW . OVERLAY).")
a0aa2a49
SM
62(make-variable-buffer-local 'reveal-open-spots)
63
40a45fac
SM
64(defvar reveal-last-tick nil)
65(make-variable-buffer-local 'reveal-last-tick)
66
a0aa2a49
SM
67;; Actual code
68
69(defun reveal-post-command ()
70 ;; Refresh the spots that might have changed.
71 ;; `Refreshing' here means to try and re-hide the corresponding text.
72 ;; We don't refresh everything correctly:
73 ;; - we only refresh spots in the current window.
74 ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
75 (with-local-quit
ffb5fc37
SM
76 (condition-case err
77 (let ((old-ols
78 (delq nil
79 (mapcar
80 (lambda (x)
81 ;; We refresh any spot in the current window as well
82 ;; as any spots associated with a dead window or
83 ;; a window which does not show this buffer any more.
84 (cond
85 ((eq (car x) (selected-window)) (cdr x))
23e0c1a3
SM
86 ((not (and (window-live-p (car x))
87 (eq (window-buffer (car x)) (current-buffer))))
ffb5fc37
SM
88 ;; Adopt this since it's owned by a window that's
89 ;; either not live or at least not showing this
90 ;; buffer any more.
91 (setcar x (selected-window))
92 (cdr x))))
93 reveal-open-spots))))
94 (setq old-ols (reveal-open-new-overlays old-ols))
95 (reveal-close-old-overlays old-ols))
96 (error (message "Reveal: %s" err)))))
97
98(defun reveal-open-new-overlays (old-ols)
99 (let ((repeat t))
100 (while repeat
101 (setq repeat nil)
102 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
103 (overlays-at (mark)))
104 (overlays-at (point))))
105 (setq old-ols (delq ol old-ols))
23e0c1a3
SM
106 (when (overlay-start ol) ;Check it's still live.
107 (let ((inv (overlay-get ol 'invisible)) open)
108 (when (and inv
109 ;; There's an `invisible' property. Make sure it's
110 ;; actually invisible, and ellipsised.
111 (and (consp buffer-invisibility-spec)
112 (cdr (assq inv buffer-invisibility-spec)))
113 (or (setq open
114 (or (overlay-get ol 'reveal-toggle-invisible)
115 (and (symbolp inv)
116 (get inv 'reveal-toggle-invisible))
117 (overlay-get ol 'isearch-open-invisible-temporary)))
118 (overlay-get ol 'isearch-open-invisible)
119 (and (consp buffer-invisibility-spec)
120 (cdr (assq inv buffer-invisibility-spec))))
121 (overlay-put ol 'reveal-invisible inv))
122 (push (cons (selected-window) ol) reveal-open-spots)
123 (if (null open)
124 (overlay-put ol 'invisible nil)
125 ;; Use the provided opening function and repeat (since the
126 ;; opening function might have hidden a subpart around point
127 ;; or moved/killed some of the overlays).
128 (setq repeat t)
129 (condition-case err
130 (funcall open ol nil)
131 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
132 open ol err)
133 ;; Let's default to a meaningful behavior to avoid
134 ;; getting stuck in an infinite loop.
135 (setq repeat nil)
136 (overlay-put ol 'invisible nil))))))))))
ffb5fc37
SM
137 old-ols)
138
139(defun reveal-close-old-overlays (old-ols)
140 (if (not (eq reveal-last-tick
141 (setq reveal-last-tick (buffer-modified-tick))))
142 ;; The buffer was modified since last command: let's refrain from
143 ;; closing any overlay because it tends to behave poorly when
144 ;; inserting text at the end of an overlay (basically the overlay
145 ;; should be rear-advance when it's open, but things like
146 ;; outline-minor-mode make it non-rear-advance because it's
147 ;; a better choice when it's closed).
148 nil
149 ;; The last command was only a point motion or some such
150 ;; non-buffer-modifying command. Let's close whatever can be closed.
151 (dolist (ol old-ols)
152 (if (and (overlay-start ol) ;Check it's still live.
153 (>= (point) (save-excursion
154 (goto-char (overlay-start ol))
155 (line-beginning-position 1)))
156 (<= (point) (save-excursion
157 (goto-char (overlay-end ol))
158 (line-beginning-position 2)))
159 ;; If the application has moved the overlay to some other
160 ;; buffer, we'd better reset the buffer to its
161 ;; original state.
162 (eq (current-buffer) (overlay-buffer ol)))
163 ;; Still near the overlay: keep it open.
164 nil
165 ;; Really close it.
166 (let* ((inv (overlay-get ol 'reveal-invisible))
167 (open (or (overlay-get ol 'reveal-toggle-invisible)
168 (get inv 'reveal-toggle-invisible)
169 (overlay-get ol 'isearch-open-invisible-temporary))))
170 (if (and (overlay-start ol) ;Check it's still live.
171 open)
172 (condition-case err
173 (funcall open ol t)
174 (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
175 open ol err)))
176 (overlay-put ol 'invisible inv))
177 ;; Remove the overlay from the list of open spots.
178 (overlay-put ol 'reveal-invisible nil)
179 (setq reveal-open-spots
180 (delq (rassoc ol reveal-open-spots)
181 reveal-open-spots)))))))
a0aa2a49 182
ea78522d
SM
183(defvar reveal-mode-map
184 (let ((map (make-sparse-keymap)))
88418e61
KS
185 ;; Override the default move-beginning-of-line and move-end-of-line
186 ;; which skips valuable invisible text.
8d194e3e
SM
187 (define-key map [remap move-beginning-of-line] 'beginning-of-line)
188 (define-key map [remap move-end-of-line] 'end-of-line)
ea78522d
SM
189 map))
190
a0aa2a49
SM
191;;;###autoload
192(define-minor-mode reveal-mode
193 "Toggle Reveal mode on or off.
194Reveal mode renders invisible text around point visible again.
195
196Interactively, with no prefix argument, toggle the mode.
197With universal prefix ARG (or if ARG is nil) turn mode on.
198With zero or negative ARG turn mode off."
28a1f13b 199 :group 'reveal
7b73ae06 200 :lighter (global-reveal-mode nil " Reveal")
ea78522d 201 :keymap reveal-mode-map
a0aa2a49
SM
202 (if reveal-mode
203 (progn
204 (set (make-local-variable 'search-invisible) t)
205 (add-hook 'post-command-hook 'reveal-post-command nil t))
206 (kill-local-variable 'search-invisible)
207 (remove-hook 'post-command-hook 'reveal-post-command t)))
208
209;;;###autoload
210(define-minor-mode global-reveal-mode
211 "Toggle Reveal mode in all buffers on or off.
212Reveal mode renders invisible text around point visible again.
213
214Interactively, with no prefix argument, toggle the mode.
215With universal prefix ARG (or if ARG is nil) turn mode on.
216With zero or negative ARG turn mode off."
710a1280 217 :global t :group 'reveal
a0aa2a49
SM
218 (setq-default reveal-mode global-reveal-mode)
219 (if global-reveal-mode
220 (progn
221 (setq search-invisible t)
222 (add-hook 'post-command-hook 'reveal-post-command))
223 (setq search-invisible 'open) ;FIXME
224 (remove-hook 'post-command-hook 'reveal-post-command)))
225
226(provide 'reveal)
710a1280 227
40a45fac 228;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
a0aa2a49 229;;; reveal.el ends here