(vc-svn-registered): Catch all errors.
[bpt/emacs.git] / lisp / reveal.el
CommitLineData
a0aa2a49
SM
1;;; reveal.el --- Automatically reveal hidden text at point
2
0d30b337
TTN
3;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4;; 2005 Free Software Foundation, Inc.
a0aa2a49
SM
5
6;; Author: Stefan Monnier <monnier@cs.yale.edu>
7;; Keywords: outlines
8
041f4d74
PJ
9;; This file is part of GNU Emacs.
10
12b309f1 11;; GNU Emacs is free software; you can redistribute it and/or modify
a0aa2a49
SM
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)
14;; any later version.
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
22;; along with GNU Emacs; see the file COPYING. If not, write to
086add15
LK
23;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
a0aa2a49
SM
25
26;;; Commentary:
27
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.
31;;
32;; This is normally used in conjunction with `outline-minor-mode',
33;; `hs-minor-mode', `hide-ifdef-mode', ...
34;;
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.
43
44;;; Todo:
45
46;; - find other hysteresis features.
4acbd507
SM
47;; - don't hide after a scroll command
48;; - delay hiding by a couple seconds (i.e. hide in the background)
a0aa2a49
SM
49
50;;; Code:
51
a0aa2a49
SM
52(defgroup reveal nil
53 "Reveal hidden text on the fly."
54 :group 'editing)
55
56(defcustom reveal-around-mark t
57 "Reveal text around the mark, if active."
f5307782
JB
58 :type 'boolean
59 :group 'reveal)
a0aa2a49 60
4acbd507
SM
61(defvar reveal-open-spots nil
62 "List of spots in the buffer which are open.
63Each element has the form (WINDOW . OVERLAY).")
a0aa2a49
SM
64(make-variable-buffer-local 'reveal-open-spots)
65
40a45fac
SM
66(defvar reveal-last-tick nil)
67(make-variable-buffer-local 'reveal-last-tick)
68
a0aa2a49
SM
69;; Actual code
70
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) ?
77 (with-local-quit
f6961018 78 (condition-case err
4acbd507
SM
79 (let ((old-ols (delq nil
80 (mapcar
81 (lambda (x)
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
85 ;; buffer any more.
86 (if (or (eq (car x) (selected-window))
87 (not (window-live-p (car x)))
88 (not (eq (window-buffer (car x))
89 (current-buffer))))
90 (cdr x)))
91 reveal-open-spots)))
92 (repeat t))
a0aa2a49
SM
93 ;; Open new overlays.
94 (while repeat
95 (setq repeat nil)
96 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
97 (overlays-at (mark)))
98 (overlays-at (point))))
a0aa2a49 99 (setq old-ols (delq ol old-ols))
fc88c288
SM
100 (let ((inv (overlay-get ol 'invisible)) open)
101 (when (and inv
102 ;; There's an `invisible' property. Make sure it's
4acbd507
SM
103 ;; actually invisible, and ellipsised.
104 (and (consp buffer-invisibility-spec)
105 (cdr (assq inv buffer-invisibility-spec)))
106 (or (setq open
fc88c288
SM
107 (or (overlay-get ol 'reveal-toggle-invisible)
108 (and (symbolp inv)
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))
4acbd507 115 (push (cons (selected-window) ol) reveal-open-spots)
f6961018 116 (if (null open)
4acbd507
SM
117 (progn ;; (debug)
118 (overlay-put ol 'invisible nil))
a0aa2a49
SM
119 ;; Use the provided opening function and repeat (since the
120 ;; opening function might have hidden a subpart around point).
121 (setq repeat t)
122 (condition-case err
f6961018 123 (funcall open ol nil)
fc88c288
SM
124 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
125 open ol err)
a604e2b4
SM
126 ;; Let's default to a meaningful behavior to avoid
127 ;; getting stuck in an infinite loop.
836be5ce 128 (setq repeat nil)
a604e2b4 129 (overlay-put ol 'invisible nil))))))))
a0aa2a49 130 ;; Close old overlays.
40a45fac
SM
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).
4acbd507 139 nil
40a45fac
SM
140 ;; The last command was only a point motion or some such
141 ;; non-buffer-modifying command. Let's close whatever can be closed.
142 (dolist (ol old-ols)
4acbd507
SM
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
151 ;; original state.
152 (eq (current-buffer) (overlay-buffer ol)))
153 ;; Still near the overlay: keep it open.
154 nil
155 ;; Really close it.
156 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv)
157 (if (or open
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)))))
161 (condition-case err
162 (funcall open ol t)
163 (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
164 open ol err)))
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)))))))
836be5ce 170 (error (message "Reveal: %s" err)))))
a0aa2a49 171
ea78522d
SM
172(defvar reveal-mode-map
173 (let ((map (make-sparse-keymap)))
88418e61
KS
174 ;; Override the default move-beginning-of-line and move-end-of-line
175 ;; which skips valuable invisible text.
8d194e3e
SM
176 (define-key map [remap move-beginning-of-line] 'beginning-of-line)
177 (define-key map [remap move-end-of-line] 'end-of-line)
ea78522d
SM
178 map))
179
a0aa2a49
SM
180;;;###autoload
181(define-minor-mode reveal-mode
182 "Toggle Reveal mode on or off.
183Reveal mode renders invisible text around point visible again.
184
185Interactively, with no prefix argument, toggle the mode.
186With universal prefix ARG (or if ARG is nil) turn mode on.
187With zero or negative ARG turn mode off."
28a1f13b 188 :group 'reveal
7b73ae06 189 :lighter (global-reveal-mode nil " Reveal")
ea78522d 190 :keymap reveal-mode-map
a0aa2a49
SM
191 (if reveal-mode
192 (progn
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)))
197
198;;;###autoload
199(define-minor-mode global-reveal-mode
200 "Toggle Reveal mode in all buffers on or off.
201Reveal mode renders invisible text around point visible again.
202
203Interactively, with no prefix argument, toggle the mode.
204With universal prefix ARG (or if ARG is nil) turn mode on.
205With zero or negative ARG turn mode off."
710a1280 206 :global t :group 'reveal
a0aa2a49
SM
207 (setq-default reveal-mode global-reveal-mode)
208 (if global-reveal-mode
209 (progn
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)))
214
215(provide 'reveal)
710a1280 216
40a45fac 217;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
a0aa2a49 218;;; reveal.el ends here