Commit | Line | Data |
---|---|---|
a0aa2a49 SM |
1 | ;;; reveal.el --- Automatically reveal hidden text at point |
2 | ||
3 | ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc. | |
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 | ||
62 | ;; Actual code | |
63 | ||
64 | (defun reveal-post-command () | |
65 | ;; Refresh the spots that might have changed. | |
66 | ;; `Refreshing' here means to try and re-hide the corresponding text. | |
67 | ;; We don't refresh everything correctly: | |
68 | ;; - we only refresh spots in the current window. | |
69 | ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ? | |
70 | (with-local-quit | |
f6961018 | 71 | (condition-case err |
a0aa2a49 SM |
72 | (let* ((spots (cvs-partition |
73 | (lambda (x) | |
74 | ;; We refresh any spot in the current window as well | |
75 | ;; as any spots associated with a dead window or a window | |
76 | ;; which does not show this buffer any more. | |
77 | (or (eq (car x) (selected-window)) | |
78 | (not (window-live-p (car x))) | |
79 | (not (eq (window-buffer (car x)) | |
80 | (current-buffer))))) | |
81 | reveal-open-spots)) | |
82 | (old-ols (mapcar 'cdr (car spots))) | |
f6961018 | 83 | (repeat t)) |
a0aa2a49 SM |
84 | (setq reveal-open-spots (cdr spots)) |
85 | ;; Open new overlays. | |
86 | (while repeat | |
87 | (setq repeat nil) | |
88 | (dolist (ol (nconc (when (and reveal-around-mark mark-active) | |
89 | (overlays-at (mark))) | |
90 | (overlays-at (point)))) | |
91 | (push (cons (selected-window) ol) reveal-open-spots) | |
92 | (setq old-ols (delq ol old-ols)) | |
f6961018 SM |
93 | (let ((open (overlay-get ol 'reveal-toggle-invisible))) |
94 | (when (or open | |
95 | (let ((inv (overlay-get ol 'invisible))) | |
96 | (and inv (symbolp inv) | |
97 | (or (setq open (or (get inv 'reveal-toggle-invisible) | |
02ad7cbf | 98 | (overlay-get ol 'isearch-open-invisible-temporary))) |
f6961018 SM |
99 | (overlay-get ol 'isearch-open-invisible) |
100 | (and (consp buffer-invisibility-spec) | |
101 | (assq inv buffer-invisibility-spec))) | |
102 | (overlay-put ol 'reveal-invisible inv)))) | |
103 | (if (null open) | |
104 | (overlay-put ol 'invisible nil) | |
a0aa2a49 SM |
105 | ;; Use the provided opening function and repeat (since the |
106 | ;; opening function might have hidden a subpart around point). | |
107 | (setq repeat t) | |
108 | (condition-case err | |
f6961018 | 109 | (funcall open ol nil) |
836be5ce | 110 | (error (message "!!Reveal-show: %s !!" err) |
a604e2b4 SM |
111 | ;; Let's default to a meaningful behavior to avoid |
112 | ;; getting stuck in an infinite loop. | |
836be5ce | 113 | (setq repeat nil) |
a604e2b4 | 114 | (overlay-put ol 'invisible nil)))))))) |
a0aa2a49 SM |
115 | ;; Close old overlays. |
116 | (dolist (ol old-ols) | |
f6961018 | 117 | (when (and (eq (current-buffer) (overlay-buffer ol)) |
a0aa2a49 SM |
118 | (not (rassq ol reveal-open-spots))) |
119 | (if (and (>= (point) (save-excursion | |
120 | (goto-char (overlay-start ol)) | |
121 | (line-beginning-position 1))) | |
122 | (<= (point) (save-excursion | |
123 | (goto-char (overlay-end ol)) | |
124 | (line-beginning-position 2)))) | |
125 | ;; Still near the overlay: keep it open. | |
126 | (push (cons (selected-window) ol) reveal-open-spots) | |
127 | ;; Really close it. | |
f6961018 SM |
128 | (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv) |
129 | (if (or open | |
130 | (and (setq inv (overlay-get ol 'reveal-invisible)) | |
131 | (setq open (or (get inv 'reveal-toggle-invisible) | |
02ad7cbf | 132 | (overlay-get ol 'isearch-open-invisible-temporary))))) |
f6961018 SM |
133 | (condition-case err |
134 | (funcall open ol t) | |
836be5ce | 135 | (error (message "!!Reveal-hide: %s !!" err))) |
f6961018 | 136 | (overlay-put ol 'invisible inv))))))) |
836be5ce | 137 | (error (message "Reveal: %s" err))))) |
a0aa2a49 SM |
138 | |
139 | ;;;###autoload | |
140 | (define-minor-mode reveal-mode | |
141 | "Toggle Reveal mode on or off. | |
142 | Reveal mode renders invisible text around point visible again. | |
143 | ||
144 | Interactively, with no prefix argument, toggle the mode. | |
145 | With universal prefix ARG (or if ARG is nil) turn mode on. | |
146 | With zero or negative ARG turn mode off." | |
7b73ae06 | 147 | :lighter (global-reveal-mode nil " Reveal") |
a0aa2a49 SM |
148 | (if reveal-mode |
149 | (progn | |
150 | (set (make-local-variable 'search-invisible) t) | |
151 | (add-hook 'post-command-hook 'reveal-post-command nil t)) | |
152 | (kill-local-variable 'search-invisible) | |
153 | (remove-hook 'post-command-hook 'reveal-post-command t))) | |
154 | ||
155 | ;;;###autoload | |
156 | (define-minor-mode global-reveal-mode | |
157 | "Toggle Reveal mode in all buffers on or off. | |
158 | Reveal mode renders invisible text around point visible again. | |
159 | ||
160 | Interactively, with no prefix argument, toggle the mode. | |
161 | With universal prefix ARG (or if ARG is nil) turn mode on. | |
162 | With zero or negative ARG turn mode off." | |
710a1280 | 163 | :global t :group 'reveal |
a0aa2a49 SM |
164 | (setq-default reveal-mode global-reveal-mode) |
165 | (if global-reveal-mode | |
166 | (progn | |
167 | (setq search-invisible t) | |
168 | (add-hook 'post-command-hook 'reveal-post-command)) | |
169 | (setq search-invisible 'open) ;FIXME | |
170 | (remove-hook 'post-command-hook 'reveal-post-command))) | |
171 | ||
172 | (provide 'reveal) | |
710a1280 | 173 | |
ab5796a9 | 174 | ;;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8 |
a0aa2a49 | 175 | ;;; reveal.el ends here |