*** empty log message ***
[emacs.git] / lisp / reveal.el
blob06f8940eddc0dcd4b07dcfc61809ee6e39d15f54
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>
7 ;; Keywords: outlines
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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.
44 ;;; Todo:
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)
50 ;;; Code:
52 (defgroup reveal nil
53 "Reveal hidden text on the fly."
54 :group 'editing)
56 (defcustom reveal-around-mark t
57 "Reveal text around the mark, if active."
58 :type 'boolean
59 :group 'reveal)
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)
69 ;; Actual code
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
78 (condition-case err
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))
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))))
99 (setq old-ols (delq ol old-ols))
100 (let ((inv (overlay-get ol 'invisible)) open)
101 (when (and inv
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)))
106 (or (setq open
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))
115 (push (cons (selected-window) ol) reveal-open-spots)
116 (if (null open)
117 (progn ;; (debug)
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).
121 (setq repeat t)
122 (condition-case err
123 (funcall open ol nil)
124 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
125 open ol err)
126 ;; Let's default to a meaningful behavior to avoid
127 ;; getting stuck in an infinite loop.
128 (setq repeat nil)
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.
142 (dolist (ol old-ols)
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.
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)))))))
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)
178 map))
180 ;;;###autoload
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."
188 :group 'reveal
189 :lighter (global-reveal-mode nil " Reveal")
190 :keymap reveal-mode-map
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)))
198 ;;;###autoload
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
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)))
215 (provide 'reveal)
217 ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
218 ;;; reveal.el ends here