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 | ||
a0aa2a49 SM |
10 | ;; This file 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 | ;; This file 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 | ;; 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 | |
71 | (let* ((spots (cvs-partition | |
72 | (lambda (x) | |
73 | ;; We refresh any spot in the current window as well | |
74 | ;; as any spots associated with a dead window or a window | |
75 | ;; which does not show this buffer any more. | |
76 | (or (eq (car x) (selected-window)) | |
77 | (not (window-live-p (car x))) | |
78 | (not (eq (window-buffer (car x)) | |
79 | (current-buffer))))) | |
80 | reveal-open-spots)) | |
81 | (old-ols (mapcar 'cdr (car spots))) | |
82 | (repeat t) | |
83 | ;; `post-command-hook' binds it to t, but the user might want to | |
84 | ;; interrupt our work if we somehow get stuck in an infinite loop. | |
85 | (inhibit-quit nil) | |
86 | inv open) | |
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 | (when (setq inv (overlay-get ol 'invisible)) | |
97 | (when (or (overlay-get ol 'isearch-open-invisible) | |
98 | (and (consp buffer-invisibility-spec) | |
99 | (assq inv buffer-invisibility-spec))) | |
100 | (overlay-put ol 'reveal-invisible inv) | |
101 | (overlay-put ol 'invisible nil) | |
102 | (when (setq open (get inv 'reveal-toggle-invisible)) | |
103 | ;; Use the provided opening function and repeat (since the | |
104 | ;; opening function might have hidden a subpart around point). | |
105 | (setq repeat t) | |
106 | (condition-case err | |
107 | (funcall open ol t) | |
108 | (error (message "!!Reveal-show: %s !!" err)))))))) | |
109 | ;; Close old overlays. | |
110 | (dolist (ol old-ols) | |
111 | (when (and (setq inv (overlay-get ol 'reveal-invisible)) | |
112 | (eq (current-buffer) (overlay-buffer ol)) | |
113 | (not (rassq ol reveal-open-spots))) | |
114 | (if (and (>= (point) (save-excursion | |
115 | (goto-char (overlay-start ol)) | |
116 | (line-beginning-position 1))) | |
117 | (<= (point) (save-excursion | |
118 | (goto-char (overlay-end ol)) | |
119 | (line-beginning-position 2)))) | |
120 | ;; Still near the overlay: keep it open. | |
121 | (push (cons (selected-window) ol) reveal-open-spots) | |
122 | ;; Really close it. | |
123 | (overlay-put ol 'invisible inv) | |
124 | (when (setq open (get inv 'reveal-toggle-invisible)) | |
125 | (condition-case err | |
126 | (funcall open ol nil) | |
127 | (error (message "!!Reveal-hide: %s !!" err)))))))))) | |
128 | ||
129 | ;;;###autoload | |
130 | (define-minor-mode reveal-mode | |
131 | "Toggle Reveal mode on or off. | |
132 | Reveal mode renders invisible text around point visible again. | |
133 | ||
134 | Interactively, with no prefix argument, toggle the mode. | |
135 | With universal prefix ARG (or if ARG is nil) turn mode on. | |
136 | With zero or negative ARG turn mode off." | |
7b73ae06 | 137 | :lighter (global-reveal-mode nil " Reveal") |
a0aa2a49 SM |
138 | (if reveal-mode |
139 | (progn | |
140 | (set (make-local-variable 'search-invisible) t) | |
141 | (add-hook 'post-command-hook 'reveal-post-command nil t)) | |
142 | (kill-local-variable 'search-invisible) | |
143 | (remove-hook 'post-command-hook 'reveal-post-command t))) | |
144 | ||
145 | ;;;###autoload | |
146 | (define-minor-mode global-reveal-mode | |
147 | "Toggle Reveal mode in all buffers on or off. | |
148 | Reveal mode renders invisible text around point visible again. | |
149 | ||
150 | Interactively, with no prefix argument, toggle the mode. | |
151 | With universal prefix ARG (or if ARG is nil) turn mode on. | |
152 | With zero or negative ARG turn mode off." | |
153 | :global t | |
154 | (setq-default reveal-mode global-reveal-mode) | |
155 | (if global-reveal-mode | |
156 | (progn | |
157 | (setq search-invisible t) | |
158 | (add-hook 'post-command-hook 'reveal-post-command)) | |
159 | (setq search-invisible 'open) ;FIXME | |
160 | (remove-hook 'post-command-hook 'reveal-post-command))) | |
161 | ||
162 | (provide 'reveal) | |
163 | ;;; reveal.el ends here |