Mercurial > emacs
annotate lisp/reveal.el @ 58448:f3a76c43ac80
(Fset_window_scroll_bars): Don't use XINT if it isn't int.
(decode_next_window_args, window_loop): Don't use XFASTINT blindly.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Tue, 23 Nov 2004 05:05:34 +0000 |
parents | 180297c18beb |
children | f3c5c717aa02 f2ebccfa87d4 |
rev | line source |
---|---|
41621 | 1 ;;; reveal.el --- Automatically reveal hidden text at point |
2 | |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc. |
41621 | 4 |
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu> | |
6 ;; Keywords: outlines | |
7 | |
42320 | 8 ;; This file is part of GNU Emacs. |
9 | |
45340 | 10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
41621 | 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 | |
45341 | 15 ;; GNU Emacs is distributed in the hope that it will be useful, |
41621 | 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 | |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
62 (defvar reveal-last-tick nil) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
63 (make-variable-buffer-local 'reveal-last-tick) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
64 |
41621 | 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 | |
45428
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
74 (condition-case err |
41621 | 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))) | |
45428
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
86 (repeat t)) |
41621 | 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)) | |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
96 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv) |
45428
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
97 (when (or open |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
98 (and (setq inv (overlay-get ol 'invisible)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
99 (symbolp inv) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
100 (or (setq open (or (get inv 'reveal-toggle-invisible) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
101 (overlay-get ol 'isearch-open-invisible-temporary))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
102 (overlay-get ol 'isearch-open-invisible) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
103 (and (consp buffer-invisibility-spec) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
104 (assq inv buffer-invisibility-spec))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
105 (overlay-put ol 'reveal-invisible inv))) |
45428
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
106 (if (null open) |
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
107 (overlay-put ol 'invisible nil) |
41621 | 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 | |
45428
ac7e0cc03aed
(reveal-post-command): Reverse the semantics of
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45341
diff
changeset
|
112 (funcall open ol nil) |
50384
09ec24acd6a5
(reveal-post-command): Remove buggy debugging code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50374
diff
changeset
|
113 (error (message "!!Reveal-show: %s !!" err) |
50374
73660951a233
(reveal-post-command): Better error handling and debugging.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
48399
diff
changeset
|
114 ;; Let's default to a meaningful behavior to avoid |
73660951a233
(reveal-post-command): Better error handling and debugging.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
48399
diff
changeset
|
115 ;; getting stuck in an infinite loop. |
50384
09ec24acd6a5
(reveal-post-command): Remove buggy debugging code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50374
diff
changeset
|
116 (setq repeat nil) |
50374
73660951a233
(reveal-post-command): Better error handling and debugging.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
48399
diff
changeset
|
117 (overlay-put ol 'invisible nil)))))))) |
41621 | 118 ;; Close old overlays. |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
119 (if (not (eq reveal-last-tick |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
120 (setq reveal-last-tick (buffer-modified-tick)))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
121 ;; The buffer was modified since last command: let's refrain from |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
122 ;; closing any overlay because it tends to behave poorly when |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
123 ;; inserting text at the end of an overlay (basically the overlay |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
124 ;; should be rear-advance when it's open, but things like |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
125 ;; outline-minor-mode make it non-rear-advance because it's |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
126 ;; a better choice when it's closed). |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
127 (dolist (ol old-ols) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
128 (push (cons (selected-window) ol) reveal-open-spots)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
129 ;; The last command was only a point motion or some such |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
130 ;; non-buffer-modifying command. Let's close whatever can be closed. |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
131 (dolist (ol old-ols) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
132 (when (and (eq (current-buffer) (overlay-buffer ol)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
133 (not (rassq ol reveal-open-spots))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
134 (if (and (>= (point) (save-excursion |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
135 (goto-char (overlay-start ol)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
136 (line-beginning-position 1))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
137 (<= (point) (save-excursion |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
138 (goto-char (overlay-end ol)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
139 (line-beginning-position 2)))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
140 ;; Still near the overlay: keep it open. |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
141 (push (cons (selected-window) ol) reveal-open-spots) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
142 ;; Really close it. |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
143 (let ((open (overlay-get ol 'reveal-toggle-invisible)) inv) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
144 (if (or open |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
145 (and (setq inv (overlay-get ol 'reveal-invisible)) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
146 (setq open (or (get inv 'reveal-toggle-invisible) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
147 (overlay-get ol 'isearch-open-invisible-temporary))))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
148 (condition-case err |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
149 (funcall open ol t) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
150 (error (message "!!Reveal-hide: %s !!" err))) |
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
151 (overlay-put ol 'invisible inv)))))))) |
50384
09ec24acd6a5
(reveal-post-command): Remove buggy debugging code.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50374
diff
changeset
|
152 (error (message "Reveal: %s" err))))) |
41621 | 153 |
58435
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
154 (defvar reveal-mode-map |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
155 (let ((map (make-sparse-keymap))) |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
156 ;; Override the default move-end-of-line which skips valuable |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
157 ;; invisible text. |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
158 (define-key map [?\C-e] 'end-of-line) |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
159 map)) |
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
160 |
41621 | 161 ;;;###autoload |
162 (define-minor-mode reveal-mode | |
163 "Toggle Reveal mode on or off. | |
164 Reveal mode renders invisible text around point visible again. | |
165 | |
166 Interactively, with no prefix argument, toggle the mode. | |
167 With universal prefix ARG (or if ARG is nil) turn mode on. | |
168 With zero or negative ARG turn mode off." | |
41778
19e76eadbf86
(reveal-mode): Fix reveal-mode's lighter.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
41621
diff
changeset
|
169 :lighter (global-reveal-mode nil " Reveal") |
58435
180297c18beb
(reveal-mode-map): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
57816
diff
changeset
|
170 :keymap reveal-mode-map |
41621 | 171 (if reveal-mode |
172 (progn | |
173 (set (make-local-variable 'search-invisible) t) | |
174 (add-hook 'post-command-hook 'reveal-post-command nil t)) | |
175 (kill-local-variable 'search-invisible) | |
176 (remove-hook 'post-command-hook 'reveal-post-command t))) | |
177 | |
178 ;;;###autoload | |
179 (define-minor-mode global-reveal-mode | |
180 "Toggle Reveal mode in all buffers on or off. | |
181 Reveal mode renders invisible text around point visible again. | |
182 | |
183 Interactively, with no prefix argument, toggle the mode. | |
184 With universal prefix ARG (or if ARG is nil) turn mode on. | |
185 With zero or negative ARG turn mode off." | |
48399
59fc5f077731
(global-reveal-mode): Add group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
45428
diff
changeset
|
186 :global t :group 'reveal |
41621 | 187 (setq-default reveal-mode global-reveal-mode) |
188 (if global-reveal-mode | |
189 (progn | |
190 (setq search-invisible t) | |
191 (add-hook 'post-command-hook 'reveal-post-command)) | |
192 (setq search-invisible 'open) ;FIXME | |
193 (remove-hook 'post-command-hook 'reveal-post-command))) | |
194 | |
195 (provide 'reveal) | |
48399
59fc5f077731
(global-reveal-mode): Add group.
Markus Rost <rost@math.uni-bielefeld.de>
parents:
45428
diff
changeset
|
196 |
57816
af6e1f8dd9e0
(reveal-last-tick): New var.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
52401
diff
changeset
|
197 ;; arch-tag: 96ba0242-2274-4ed7-8e10-26bc0707b4d8 |
41621 | 198 ;;; reveal.el ends here |