(Top): Delete duplicate words.
[emacs.git] / lisp / reveal.el
blob402fa7c4f499ba41a62dd45d7f18b889476c19ca
1 ;;; reveal.el --- Automatically reveal hidden text at point
3 ;; Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: outlines
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; Reveal mode is a minor mode that makes sure that text around point
28 ;; is always visible. When point enters a region of hidden text,
29 ;; `reveal-mode' temporarily makes it visible.
31 ;; This is normally used in conjunction with `outline-minor-mode',
32 ;; `hs-minor-mode', `hide-ifdef-mode', ...
34 ;; It only works with packages that hide text using overlays.
35 ;; Packages can provide special support for it by placing
36 ;; a function in the `reveal-toggle-invisible' property on the symbol
37 ;; used as the value of the `invisible' overlay property.
38 ;; The function is called right after revealing (or re-hiding) the
39 ;; text with two arguments: the overlay and a boolean that's non-nil
40 ;; if we have just revealed the text. When revealing, that function
41 ;; may re-hide some of the text.
43 ;;; Todo:
45 ;; - find other hysteresis features.
47 ;;; Code:
49 (require 'pcvs-util)
51 (defgroup reveal nil
52 "Reveal hidden text on the fly."
53 :group 'editing)
55 (defcustom reveal-around-mark t
56 "Reveal text around the mark, if active."
57 :type 'boolean
58 :group 'reveal)
60 (defvar reveal-open-spots nil)
61 (make-variable-buffer-local 'reveal-open-spots)
63 (defvar reveal-last-tick nil)
64 (make-variable-buffer-local 'reveal-last-tick)
66 ;; Actual code
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
75 (condition-case err
76 (let* ((spots (cvs-partition
77 (lambda (x)
78 ;; We refresh any spot in the current window as well
79 ;; as any spots associated with a dead window or a window
80 ;; which does not show this buffer any more.
81 (or (eq (car x) (selected-window))
82 (not (window-live-p (car x)))
83 (not (eq (window-buffer (car x))
84 (current-buffer)))))
85 reveal-open-spots))
86 (old-ols (mapcar 'cdr (car spots)))
87 (repeat t))
88 (setq reveal-open-spots (cdr spots))
89 ;; Open new overlays.
90 (while repeat
91 (setq repeat nil)
92 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
93 (overlays-at (mark)))
94 (overlays-at (point))))
95 (push (cons (selected-window) ol) reveal-open-spots)
96 (setq old-ols (delq ol old-ols))
97 (let ((inv (overlay-get ol 'invisible)) open)
98 (when (and inv
99 ;; There's an `invisible' property. Make sure it's
100 ;; actually invisible.
101 (or (not (listp buffer-invisibility-spec))
102 (memq inv buffer-invisibility-spec)
103 (assq inv buffer-invisibility-spec))
104 (or (setq open
105 (or (overlay-get ol 'reveal-toggle-invisible)
106 (and (symbolp inv)
107 (get inv 'reveal-toggle-invisible))
108 (overlay-get ol 'isearch-open-invisible-temporary)))
109 (overlay-get ol 'isearch-open-invisible)
110 (and (consp buffer-invisibility-spec)
111 (cdr (assq inv buffer-invisibility-spec))))
112 (overlay-put ol 'reveal-invisible inv))
113 (if (null open)
114 (overlay-put ol 'invisible nil)
115 ;; Use the provided opening function and repeat (since the
116 ;; opening function might have hidden a subpart around point).
117 (setq repeat t)
118 (condition-case err
119 (funcall open ol nil)
120 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
121 open ol err)
122 ;; Let's default to a meaningful behavior to avoid
123 ;; getting stuck in an infinite loop.
124 (setq repeat nil)
125 (overlay-put ol 'invisible nil))))))))
126 ;; Close old overlays.
127 (if (not (eq reveal-last-tick
128 (setq reveal-last-tick (buffer-modified-tick))))
129 ;; The buffer was modified since last command: let's refrain from
130 ;; closing any overlay because it tends to behave poorly when
131 ;; inserting text at the end of an overlay (basically the overlay
132 ;; should be rear-advance when it's open, but things like
133 ;; outline-minor-mode make it non-rear-advance because it's
134 ;; a better choice when it's closed).
135 (dolist (ol old-ols)
136 (push (cons (selected-window) ol) reveal-open-spots))
137 ;; The last command was only a point motion or some such
138 ;; non-buffer-modifying command. Let's close whatever can be closed.
139 (dolist (ol old-ols)
140 (when (and (eq (current-buffer) (overlay-buffer ol))
141 (not (rassq ol reveal-open-spots)))
142 (if (and (>= (point) (save-excursion
143 (goto-char (overlay-start ol))
144 (line-beginning-position 1)))
145 (<= (point) (save-excursion
146 (goto-char (overlay-end ol))
147 (line-beginning-position 2))))
148 ;; Still near the overlay: keep it open.
149 (push (cons (selected-window) ol) reveal-open-spots)
150 ;; Really close it.
151 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv)
152 (if (or open
153 (and (setq inv (overlay-get ol 'reveal-invisible))
154 (setq open (or (get inv 'reveal-toggle-invisible)
155 (overlay-get ol 'isearch-open-invisible-temporary)))))
156 (condition-case err
157 (funcall open ol t)
158 (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
159 open ol err)))
160 (overlay-put ol 'invisible inv))))))))
161 (error (message "Reveal: %s" err)))))
163 (defvar reveal-mode-map
164 (let ((map (make-sparse-keymap)))
165 ;; Override the default move-beginning-of-line and move-end-of-line
166 ;; which skips valuable invisible text.
167 (define-key map [remap move-beginning-of-line] 'beginning-of-line)
168 (define-key map [remap move-end-of-line] 'end-of-line)
169 map))
171 ;;;###autoload
172 (define-minor-mode reveal-mode
173 "Toggle Reveal mode on or off.
174 Reveal mode renders invisible text around point visible again.
176 Interactively, with no prefix argument, toggle the mode.
177 With universal prefix ARG (or if ARG is nil) turn mode on.
178 With zero or negative ARG turn mode off."
179 :group 'reveal
180 :lighter (global-reveal-mode nil " Reveal")
181 :keymap reveal-mode-map
182 (if reveal-mode
183 (progn
184 (set (make-local-variable 'search-invisible) t)
185 (add-hook 'post-command-hook 'reveal-post-command nil t))
186 (kill-local-variable 'search-invisible)
187 (remove-hook 'post-command-hook 'reveal-post-command t)))
189 ;;;###autoload
190 (define-minor-mode global-reveal-mode
191 "Toggle Reveal mode in all buffers on or off.
192 Reveal mode renders invisible text around point visible again.
194 Interactively, with no prefix argument, toggle the mode.
195 With universal prefix ARG (or if ARG is nil) turn mode on.
196 With zero or negative ARG turn mode off."
197 :global t :group 'reveal
198 (setq-default reveal-mode global-reveal-mode)
199 (if global-reveal-mode
200 (progn
201 (setq search-invisible t)
202 (add-hook 'post-command-hook 'reveal-post-command))
203 (setq search-invisible 'open) ;FIXME
204 (remove-hook 'post-command-hook 'reveal-post-command)))
206 (provide 'reveal)
208 ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
209 ;;; reveal.el ends here