comint.el: Don't let `insert' break an `input' field into two.
[emacs.git] / lisp / reveal.el
blobbf18602379c89b7b65ef453bb4a3d15923d8d347
1 ;;; reveal.el --- Automatically reveal hidden text at point -*- lexical-binding: t -*-
3 ;; Copyright (C) 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: outlines
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs 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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Reveal mode is a minor mode that makes sure that text around point
26 ;; is always visible. When point enters a region of hidden text,
27 ;; `reveal-mode' temporarily makes it visible.
29 ;; This is normally used in conjunction with `outline-minor-mode',
30 ;; `hs-minor-mode', `hide-ifdef-mode', ...
32 ;; It only works with packages that hide text using overlays.
33 ;; Packages can provide special support for it by placing
34 ;; a function in the `reveal-toggle-invisible' property on the symbol
35 ;; used as the value of the `invisible' overlay property.
36 ;; The function is called right after revealing (or re-hiding) the
37 ;; text with two arguments: the overlay and a boolean that's non-nil
38 ;; if we have just revealed the text. When revealing, that function
39 ;; may re-hide some of the text.
41 ;;; Todo:
43 ;; - find other hysteresis features.
44 ;; - don't hide after a scroll command
45 ;; - delay hiding by a couple seconds (i.e. hide in the background)
47 ;;; Code:
49 (defgroup reveal nil
50 "Reveal hidden text on the fly."
51 :group 'convenience)
53 (defcustom reveal-around-mark t
54 "Reveal text around the mark, if active."
55 :type 'boolean
56 :group 'reveal)
58 (defvar reveal-open-spots nil
59 "List of spots in the buffer which are open.
60 Each element has the form (WINDOW . OVERLAY).")
61 (make-variable-buffer-local 'reveal-open-spots)
63 (defvar reveal-last-tick nil)
64 (make-variable-buffer-local 'reveal-last-tick)
66 ;; Actual code
68 (defun reveal-post-command ()
69 ;; Refresh the spots that might have changed.
70 ;; `Refreshing' here means to try and re-hide the corresponding text.
71 ;; We don't refresh everything correctly:
72 ;; - we only refresh spots in the current window.
73 ;; FIXME: do we actually know that (current-buffer) = (window-buffer) ?
74 (with-local-quit
75 (condition-case err
76 (let ((old-ols
77 (delq nil
78 (mapcar
79 (lambda (x)
80 ;; We refresh any spot in the current window as well
81 ;; as any spots associated with a dead window or
82 ;; a window which does not show this buffer any more.
83 (cond
84 ((eq (car x) (selected-window)) (cdr x))
85 ((not (and (window-live-p (car x))
86 (eq (window-buffer (car x)) (current-buffer))))
87 ;; Adopt this since it's owned by a window that's
88 ;; either not live or at least not showing this
89 ;; buffer any more.
90 (setcar x (selected-window))
91 (cdr x))))
92 reveal-open-spots))))
93 (setq old-ols (reveal-open-new-overlays old-ols))
94 (reveal-close-old-overlays old-ols))
95 (error (message "Reveal: %s" err)))))
97 (defun reveal-open-new-overlays (old-ols)
98 (let ((repeat t))
99 (while repeat
100 (setq repeat nil)
101 (dolist (ol (nconc (when (and reveal-around-mark mark-active)
102 (overlays-at (mark)))
103 (overlays-at (point))))
104 (setq old-ols (delq ol old-ols))
105 (when (overlay-start ol) ;Check it's still live.
106 (let ((inv (overlay-get ol 'invisible)) open)
107 (when (and inv
108 ;; There's an `invisible' property. Make sure it's
109 ;; actually invisible, and ellipsised.
110 (and (consp buffer-invisibility-spec)
111 (cdr (assq inv buffer-invisibility-spec)))
112 (or (setq open
113 (or (overlay-get ol 'reveal-toggle-invisible)
114 (and (symbolp inv)
115 (get inv 'reveal-toggle-invisible))
116 (overlay-get ol 'isearch-open-invisible-temporary)))
117 (overlay-get ol 'isearch-open-invisible)
118 (and (consp buffer-invisibility-spec)
119 (cdr (assq inv buffer-invisibility-spec))))
120 (overlay-put ol 'reveal-invisible inv))
121 (push (cons (selected-window) ol) reveal-open-spots)
122 (if (null open)
123 (overlay-put ol 'invisible nil)
124 ;; Use the provided opening function and repeat (since the
125 ;; opening function might have hidden a subpart around point
126 ;; or moved/killed some of the overlays).
127 (setq repeat t)
128 (condition-case err
129 (funcall open ol nil)
130 (error (message "!!Reveal-show (funcall %s %s nil): %s !!"
131 open ol err)
132 ;; Let's default to a meaningful behavior to avoid
133 ;; getting stuck in an infinite loop.
134 (setq repeat nil)
135 (overlay-put ol 'invisible nil))))))))))
136 old-ols)
138 (defun reveal-close-old-overlays (old-ols)
139 (if (not (eq reveal-last-tick
140 (setq reveal-last-tick (buffer-modified-tick))))
141 ;; The buffer was modified since last command: let's refrain from
142 ;; closing any overlay because it tends to behave poorly when
143 ;; inserting text at the end of an overlay (basically the overlay
144 ;; should be rear-advance when it's open, but things like
145 ;; outline-minor-mode make it non-rear-advance because it's
146 ;; a better choice when it's closed).
148 ;; The last command was only a point motion or some such
149 ;; non-buffer-modifying command. Let's close whatever can be closed.
150 (dolist (ol old-ols)
151 (if (and (overlay-start ol) ;Check it's still live.
152 (>= (point) (save-excursion
153 (goto-char (overlay-start ol))
154 (line-beginning-position 1)))
155 (<= (point) (save-excursion
156 (goto-char (overlay-end ol))
157 (line-beginning-position 2)))
158 ;; If the application has moved the overlay to some other
159 ;; buffer, we'd better reset the buffer to its
160 ;; original state.
161 (eq (current-buffer) (overlay-buffer ol)))
162 ;; Still near the overlay: keep it open.
164 ;; Really close it.
165 (let* ((inv (overlay-get ol 'reveal-invisible))
166 (open (or (overlay-get ol 'reveal-toggle-invisible)
167 (get inv 'reveal-toggle-invisible)
168 (overlay-get ol 'isearch-open-invisible-temporary))))
169 (if (and (overlay-start ol) ;Check it's still live.
170 open)
171 (condition-case err
172 (funcall open ol t)
173 (error (message "!!Reveal-hide (funcall %s %s t): %s !!"
174 open ol err)))
175 (overlay-put ol 'invisible inv))
176 ;; Remove the overlay from the list of open spots.
177 (overlay-put ol 'reveal-invisible nil)
178 (setq reveal-open-spots
179 (delq (rassoc ol reveal-open-spots)
180 reveal-open-spots)))))))
182 (defvar reveal-mode-map
183 (let ((map (make-sparse-keymap)))
184 ;; Override the default move-beginning-of-line and move-end-of-line
185 ;; which skips valuable invisible text.
186 (define-key map [remap move-beginning-of-line] 'beginning-of-line)
187 (define-key map [remap move-end-of-line] 'end-of-line)
188 map))
190 ;;;###autoload
191 (define-minor-mode reveal-mode
192 "Toggle Reveal mode on or off.
193 Reveal mode renders invisible text around point visible again.
195 Interactively, with no prefix argument, toggle the mode.
196 With universal prefix ARG (or if ARG is nil) turn mode on.
197 With zero or negative ARG turn mode off."
198 :group 'reveal
199 :lighter (global-reveal-mode nil " Reveal")
200 :keymap reveal-mode-map
201 (if reveal-mode
202 (progn
203 (set (make-local-variable 'search-invisible) t)
204 (add-hook 'post-command-hook 'reveal-post-command nil t))
205 (kill-local-variable 'search-invisible)
206 (remove-hook 'post-command-hook 'reveal-post-command t)))
208 ;;;###autoload
209 (define-minor-mode global-reveal-mode
210 "Toggle Reveal mode in all buffers on or off.
211 Reveal mode renders invisible text around point visible again.
213 Interactively, with no prefix argument, toggle the mode.
214 With universal prefix ARG (or if ARG is nil) turn mode on.
215 With zero or negative ARG turn mode off."
216 :global t :group 'reveal
217 (setq-default reveal-mode global-reveal-mode)
218 (if global-reveal-mode
219 (progn
220 (setq search-invisible t)
221 (add-hook 'post-command-hook 'reveal-post-command))
222 (setq search-invisible 'open) ;FIXME
223 (remove-hook 'post-command-hook 'reveal-post-command)))
225 (provide 'reveal)
227 ;;; reveal.el ends here