]> code.delx.au - gnu-emacs/blob - lisp/reveal.el
(reveal-last-tick): New var.
[gnu-emacs] / lisp / reveal.el
1 ;;; reveal.el --- Automatically reveal hidden text at point
2
3 ;; Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: outlines
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
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.
30 ;;
31 ;; This is normally used in conjunction with `outline-minor-mode',
32 ;; `hs-minor-mode', `hide-ifdef-mode', ...
33 ;;
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.
42
43 ;;; Todo:
44
45 ;; - find other hysteresis features.
46
47 ;;; Code:
48
49 (require 'pcvs-util)
50
51 (defgroup reveal nil
52 "Reveal hidden text on the fly."
53 :group 'editing)
54
55 (defcustom reveal-around-mark t
56 "Reveal text around the mark, if active."
57 :type 'boolean)
58
59 (defvar reveal-open-spots nil)
60 (make-variable-buffer-local 'reveal-open-spots)
61
62 (defvar reveal-last-tick nil)
63 (make-variable-buffer-local 'reveal-last-tick)
64
65 ;; Actual code
66
67 (defun reveal-post-command ()
68 ;; Refresh the spots that might have changed.
69 ;; `Refreshing' here means to try and re-hide the corresponding text.
70 ;; We don't refresh everything correctly:
71 ;; - we only refresh spots in the current window.
72 ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
73 (with-local-quit
74 (condition-case err
75 (let* ((spots (cvs-partition
76 (lambda (x)
77 ;; We refresh any spot in the current window as well
78 ;; as any spots associated with a dead window or a window
79 ;; which does not show this buffer any more.
80 (or (eq (car x) (selected-window))
81 (not (window-live-p (car x)))
82 (not (eq (window-buffer (car x))
83 (current-buffer)))))
84 reveal-open-spots))
85 (old-ols (mapcar 'cdr (car spots)))
86 (repeat t))
87 (setq reveal-open-spots (cdr spots))
88 ;; Open new overlays.
89 (while repeat
90 (setq repeat nil)
91 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
92 (overlays-at (mark)))
93 (overlays-at (point))))
94 (push (cons (selected-window) ol) reveal-open-spots)
95 (setq old-ols (delq ol old-ols))
96 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv)
97 (when (or open
98 (and (setq inv (overlay-get ol 'invisible))
99 (symbolp inv)
100 (or (setq open (or (get inv 'reveal-toggle-invisible)
101 (overlay-get ol 'isearch-open-invisible-temporary)))
102 (overlay-get ol 'isearch-open-invisible)
103 (and (consp buffer-invisibility-spec)
104 (assq inv buffer-invisibility-spec)))
105 (overlay-put ol 'reveal-invisible inv)))
106 (if (null open)
107 (overlay-put ol 'invisible nil)
108 ;; Use the provided opening function and repeat (since the
109 ;; opening function might have hidden a subpart around point).
110 (setq repeat t)
111 (condition-case err
112 (funcall open ol nil)
113 (error (message "!!Reveal-show: %s !!" err)
114 ;; Let's default to a meaningful behavior to avoid
115 ;; getting stuck in an infinite loop.
116 (setq repeat nil)
117 (overlay-put ol 'invisible nil))))))))
118 ;; Close old overlays.
119 (if (not (eq reveal-last-tick
120 (setq reveal-last-tick (buffer-modified-tick))))
121 ;; The buffer was modified since last command: let's refrain from
122 ;; closing any overlay because it tends to behave poorly when
123 ;; inserting text at the end of an overlay (basically the overlay
124 ;; should be rear-advance when it's open, but things like
125 ;; outline-minor-mode make it non-rear-advance because it's
126 ;; a better choice when it's closed).
127 (dolist (ol old-ols)
128 (push (cons (selected-window) ol) reveal-open-spots))
129 ;; The last command was only a point motion or some such
130 ;; non-buffer-modifying command. Let's close whatever can be closed.
131 (dolist (ol old-ols)
132 (when (and (eq (current-buffer) (overlay-buffer ol))
133 (not (rassq ol reveal-open-spots)))
134 (if (and (>= (point) (save-excursion
135 (goto-char (overlay-start ol))
136 (line-beginning-position 1)))
137 (<= (point) (save-excursion
138 (goto-char (overlay-end ol))
139 (line-beginning-position 2))))
140 ;; Still near the overlay: keep it open.
141 (push (cons (selected-window) ol) reveal-open-spots)
142 ;; Really close it.
143 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv)
144 (if (or open
145 (and (setq inv (overlay-get ol 'reveal-invisible))
146 (setq open (or (get inv 'reveal-toggle-invisible)
147 (overlay-get ol 'isearch-open-invisible-temporary)))))
148 (condition-case err
149 (funcall open ol t)
150 (error (message "!!Reveal-hide: %s !!" err)))
151 (overlay-put ol 'invisible inv))))))))
152 (error (message "Reveal: %s" err)))))
153
154 ;;;###autoload
155 (define-minor-mode reveal-mode
156 "Toggle Reveal mode on or off.
157 Reveal mode renders invisible text around point visible again.
158
159 Interactively, with no prefix argument, toggle the mode.
160 With universal prefix ARG (or if ARG is nil) turn mode on.
161 With zero or negative ARG turn mode off."
162 :lighter (global-reveal-mode nil " Reveal")
163 (if reveal-mode
164 (progn
165 (set (make-local-variable 'search-invisible) t)
166 (add-hook 'post-command-hook 'reveal-post-command nil t))
167 (kill-local-variable 'search-invisible)
168 (remove-hook 'post-command-hook 'reveal-post-command t)))
169
170 ;;;###autoload
171 (define-minor-mode global-reveal-mode
172 "Toggle Reveal mode in all buffers on or off.
173 Reveal mode renders invisible text around point visible again.
174
175 Interactively, with no prefix argument, toggle the mode.
176 With universal prefix ARG (or if ARG is nil) turn mode on.
177 With zero or negative ARG turn mode off."
178 :global t :group 'reveal
179 (setq-default reveal-mode global-reveal-mode)
180 (if global-reveal-mode
181 (progn
182 (setq search-invisible t)
183 (add-hook 'post-command-hook 'reveal-post-command))
184 (setq search-invisible 'open) ;FIXME
185 (remove-hook 'post-command-hook 'reveal-post-command)))
186
187 (provide 'reveal)
188
189 ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8
190 ;;; reveal.el ends here