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