41621
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
1 ;;; reveal.el --- Automatically reveal hidden text at point
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
2
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
3 ;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
4
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
6 ;; Keywords: outlines
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
7
|
42320
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
41621
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
10 ;; This file is free software; you can redistribute it and/or modify
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
13 ;; any later version.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
14
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
15 ;; This file is distributed in the hope that it will be useful,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
18 ;; GNU General Public License for more details.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
19
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
23 ;; Boston, MA 02111-1307, USA.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
24
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
25 ;;; Commentary:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
26
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
27 ;; Reveal mode is a minor mode that makes sure that text around point
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
28 ;; is always visible. When point enters a region of hidden text,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
29 ;; `reveal-mode' temporarily makes it visible.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
30 ;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
31 ;; This is normally used in conjunction with `outline-minor-mode',
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
32 ;; `hs-minor-mode', `hide-ifdef-mode', ...
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
33 ;;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
34 ;; It only works with packages that hide text using overlays.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
35 ;; Packages can provide special support for it by placing
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
36 ;; a function in the `reveal-toggle-invisible' property on the symbol
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
37 ;; used as the value of the `invisible' overlay property.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
38 ;; The function is called right after revealing (or re-hiding) the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
39 ;; text with two arguments: the overlay and a boolean that's non-nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
40 ;; if we have just revealed the text. When revealing, that function
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
41 ;; may re-hide some of the text.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
42
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
43 ;;; Todo:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
44
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
45 ;; - find other hysteresis features.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
46
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
47 ;;; Code:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
48
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
49 (require 'pcvs-util)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
50
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
51 (defgroup reveal nil
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
52 "Reveal hidden text on the fly."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
53 :group 'editing)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
54
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
55 (defcustom reveal-around-mark t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
56 "Reveal text around the mark, if active."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
57 :type 'boolean)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
58
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
59 (defvar reveal-open-spots nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
60 (make-variable-buffer-local 'reveal-open-spots)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
61
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
62 ;; Actual code
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
63
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
64 (defun reveal-post-command ()
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
65 ;; Refresh the spots that might have changed.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
66 ;; `Refreshing' here means to try and re-hide the corresponding text.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
67 ;; We don't refresh everything correctly:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
68 ;; - we only refresh spots in the current window.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
69 ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
70 (with-local-quit
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
71 (let* ((spots (cvs-partition
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
72 (lambda (x)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
73 ;; We refresh any spot in the current window as well
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
74 ;; as any spots associated with a dead window or a window
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
75 ;; which does not show this buffer any more.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
76 (or (eq (car x) (selected-window))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
77 (not (window-live-p (car x)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
78 (not (eq (window-buffer (car x))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
79 (current-buffer)))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
80 reveal-open-spots))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
81 (old-ols (mapcar 'cdr (car spots)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
82 (repeat t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
83 ;; `post-command-hook' binds it to t, but the user might want to
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
84 ;; interrupt our work if we somehow get stuck in an infinite loop.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
85 (inhibit-quit nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
86 inv open)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
87 (setq reveal-open-spots (cdr spots))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
88 ;; Open new overlays.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
89 (while repeat
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
90 (setq repeat nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
91 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
92 (overlays-at (mark)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
93 (overlays-at (point))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
94 (push (cons (selected-window) ol) reveal-open-spots)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
95 (setq old-ols (delq ol old-ols))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
96 (when (setq inv (overlay-get ol 'invisible))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
97 (when (or (overlay-get ol 'isearch-open-invisible)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
98 (and (consp buffer-invisibility-spec)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
99 (assq inv buffer-invisibility-spec)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
100 (overlay-put ol 'reveal-invisible inv)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
101 (overlay-put ol 'invisible nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
102 (when (setq open (get inv 'reveal-toggle-invisible))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
103 ;; Use the provided opening function and repeat (since the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
104 ;; opening function might have hidden a subpart around point).
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
105 (setq repeat t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
106 (condition-case err
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
107 (funcall open ol t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
108 (error (message "!!Reveal-show: %s !!" err))))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
109 ;; Close old overlays.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
110 (dolist (ol old-ols)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
111 (when (and (setq inv (overlay-get ol 'reveal-invisible))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
112 (eq (current-buffer) (overlay-buffer ol))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
113 (not (rassq ol reveal-open-spots)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
114 (if (and (>= (point) (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
115 (goto-char (overlay-start ol))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
116 (line-beginning-position 1)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
117 (<= (point) (save-excursion
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
118 (goto-char (overlay-end ol))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
119 (line-beginning-position 2))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
120 ;; Still near the overlay: keep it open.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
121 (push (cons (selected-window) ol) reveal-open-spots)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
122 ;; Really close it.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
123 (overlay-put ol 'invisible inv)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
124 (when (setq open (get inv 'reveal-toggle-invisible))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
125 (condition-case err
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
126 (funcall open ol nil)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
127 (error (message "!!Reveal-hide: %s !!" err))))))))))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
128
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
129 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
130 (define-minor-mode reveal-mode
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
131 "Toggle Reveal mode on or off.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
132 Reveal mode renders invisible text around point visible again.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
133
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
134 Interactively, with no prefix argument, toggle the mode.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
135 With universal prefix ARG (or if ARG is nil) turn mode on.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
136 With zero or negative ARG turn mode off."
|
41778
|
137 :lighter (global-reveal-mode nil " Reveal")
|
41621
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
138 (if reveal-mode
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
139 (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
140 (set (make-local-variable 'search-invisible) t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
141 (add-hook 'post-command-hook 'reveal-post-command nil t))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
142 (kill-local-variable 'search-invisible)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
143 (remove-hook 'post-command-hook 'reveal-post-command t)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
144
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
145 ;;;###autoload
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
146 (define-minor-mode global-reveal-mode
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
147 "Toggle Reveal mode in all buffers on or off.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
148 Reveal mode renders invisible text around point visible again.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
149
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
150 Interactively, with no prefix argument, toggle the mode.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
151 With universal prefix ARG (or if ARG is nil) turn mode on.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
152 With zero or negative ARG turn mode off."
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
153 :global t
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
154 (setq-default reveal-mode global-reveal-mode)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
155 (if global-reveal-mode
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
156 (progn
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
157 (setq search-invisible t)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
158 (add-hook 'post-command-hook 'reveal-post-command))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
159 (setq search-invisible 'open) ;FIXME
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
160 (remove-hook 'post-command-hook 'reveal-post-command)))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
161
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
162 (provide 'reveal)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
163 ;;; reveal.el ends here
|