Commit | Line | Data |
---|---|---|
a0aa2a49 SM |
1 | ;;; reveal.el --- Automatically reveal hidden text at point |
2 | ||
fc88c288 | 3 | ;; Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc. |
a0aa2a49 SM |
4 | |
5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> | |
6 | ;; Keywords: outlines | |
7 | ||
041f4d74 PJ |
8 | ;; This file is part of GNU Emacs. |
9 | ||
12b309f1 | 10 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
a0aa2a49 SM |
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 | ||
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 | |
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 | ||
40a45fac SM |
62 | (defvar reveal-last-tick nil) |
63 | (make-variable-buffer-local 'reveal-last-tick) | |
64 | ||
a0aa2a49 SM |
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 | |
f6961018 | 74 | (condition-case err |
a0aa2a49 SM |
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))) | |
f6961018 | 86 | (repeat t)) |
a0aa2a49 SM |
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)) | |
fc88c288 SM |
96 | (let ((inv (overlay-get ol 'invisible)) open) |
97 | (when (and inv | |
98 | ;; There's an `invisible' property. Make sure it's | |
99 | ;; actually invisible. | |
100 | (or (not (listp buffer-invisibility-spec)) | |
101 | (memq inv buffer-invisibility-spec) | |
102 | (assq inv buffer-invisibility-spec)) | |
103 | (or (setq open | |
104 | (or (overlay-get ol 'reveal-toggle-invisible) | |
105 | (and (symbolp inv) | |
106 | (get inv 'reveal-toggle-invisible)) | |
107 | (overlay-get ol 'isearch-open-invisible-temporary))) | |
108 | (overlay-get ol 'isearch-open-invisible) | |
109 | (and (consp buffer-invisibility-spec) | |
110 | (cdr (assq inv buffer-invisibility-spec)))) | |
111 | (overlay-put ol 'reveal-invisible inv)) | |
f6961018 SM |
112 | (if (null open) |
113 | (overlay-put ol 'invisible nil) | |
a0aa2a49 SM |
114 | ;; Use the provided opening function and repeat (since the |
115 | ;; opening function might have hidden a subpart around point). | |
116 | (setq repeat t) | |
117 | (condition-case err | |
f6961018 | 118 | (funcall open ol nil) |
fc88c288 SM |
119 | (error (message "!!Reveal-show (funcall %s %s nil): %s !!" |
120 | open ol err) | |
a604e2b4 SM |
121 | ;; Let's default to a meaningful behavior to avoid |
122 | ;; getting stuck in an infinite loop. | |
836be5ce | 123 | (setq repeat nil) |
a604e2b4 | 124 | (overlay-put ol 'invisible nil)))))))) |
a0aa2a49 | 125 | ;; Close old overlays. |
40a45fac SM |
126 | (if (not (eq reveal-last-tick |
127 | (setq reveal-last-tick (buffer-modified-tick)))) | |
128 | ;; The buffer was modified since last command: let's refrain from | |
129 | ;; closing any overlay because it tends to behave poorly when | |
130 | ;; inserting text at the end of an overlay (basically the overlay | |
131 | ;; should be rear-advance when it's open, but things like | |
132 | ;; outline-minor-mode make it non-rear-advance because it's | |
133 | ;; a better choice when it's closed). | |
134 | (dolist (ol old-ols) | |
135 | (push (cons (selected-window) ol) reveal-open-spots)) | |
136 | ;; The last command was only a point motion or some such | |
137 | ;; non-buffer-modifying command. Let's close whatever can be closed. | |
138 | (dolist (ol old-ols) | |
139 | (when (and (eq (current-buffer) (overlay-buffer ol)) | |
140 | (not (rassq ol reveal-open-spots))) | |
141 | (if (and (>= (point) (save-excursion | |
142 | (goto-char (overlay-start ol)) | |
143 | (line-beginning-position 1))) | |
144 | (<= (point) (save-excursion | |
145 | (goto-char (overlay-end ol)) | |
146 | (line-beginning-position 2)))) | |
147 | ;; Still near the overlay: keep it open. | |
148 | (push (cons (selected-window) ol) reveal-open-spots) | |
149 | ;; Really close it. | |
150 | (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv) | |
151 | (if (or open | |
152 | (and (setq inv (overlay-get ol 'reveal-invisible)) | |
153 | (setq open (or (get inv 'reveal-toggle-invisible) | |
154 | (overlay-get ol 'isearch-open-invisible-temporary))))) | |
155 | (condition-case err | |
156 | (funcall open ol t) | |
fc88c288 SM |
157 | (error (message "!!Reveal-hide (funcall %s %s t): %s !!" |
158 | open ol err))) | |
40a45fac | 159 | (overlay-put ol 'invisible inv)))))))) |
836be5ce | 160 | (error (message "Reveal: %s" err))))) |
a0aa2a49 | 161 | |
ea78522d SM |
162 | (defvar reveal-mode-map |
163 | (let ((map (make-sparse-keymap))) | |
88418e61 KS |
164 | ;; Override the default move-beginning-of-line and move-end-of-line |
165 | ;; which skips valuable invisible text. | |
166 | (define-key map [?\C-a] 'beginning-of-line) | |
ea78522d SM |
167 | (define-key map [?\C-e] 'end-of-line) |
168 | map)) | |
169 | ||
a0aa2a49 SM |
170 | ;;;###autoload |
171 | (define-minor-mode reveal-mode | |
172 | "Toggle Reveal mode 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." | |
7b73ae06 | 178 | :lighter (global-reveal-mode nil " Reveal") |
ea78522d | 179 | :keymap reveal-mode-map |
a0aa2a49 SM |
180 | (if reveal-mode |
181 | (progn | |
182 | (set (make-local-variable 'search-invisible) t) | |
183 | (add-hook 'post-command-hook 'reveal-post-command nil t)) | |
184 | (kill-local-variable 'search-invisible) | |
185 | (remove-hook 'post-command-hook 'reveal-post-command t))) | |
186 | ||
187 | ;;;###autoload | |
188 | (define-minor-mode global-reveal-mode | |
189 | "Toggle Reveal mode in all buffers on or off. | |
190 | Reveal mode renders invisible text around point visible again. | |
191 | ||
192 | Interactively, with no prefix argument, toggle the mode. | |
193 | With universal prefix ARG (or if ARG is nil) turn mode on. | |
194 | With zero or negative ARG turn mode off." | |
710a1280 | 195 | :global t :group 'reveal |
a0aa2a49 SM |
196 | (setq-default reveal-mode global-reveal-mode) |
197 | (if global-reveal-mode | |
198 | (progn | |
199 | (setq search-invisible t) | |
200 | (add-hook 'post-command-hook 'reveal-post-command)) | |
201 | (setq search-invisible 'open) ;FIXME | |
202 | (remove-hook 'post-command-hook 'reveal-post-command))) | |
203 | ||
204 | (provide 'reveal) | |
710a1280 | 205 | |
40a45fac | 206 | ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8 |
a0aa2a49 | 207 | ;;; reveal.el ends here |