(vmotion): Use minibuf_prompt_width despite window-start.
[emacs.git] / lisp / foldout.el
blob03a06bdc08648e74a9e5bb18035096350cdd653a
1 ;;; foldout.el --- Folding extensions for outline-mode and outline-minor-mode.
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
5 ;; Author: Kevin Broadey <KevinB@bartley.demon.co.uk>
6 ;; Created: 27 Jan 1994
7 ;; Version: foldout.el 1.9 dated 94/03/15 at 14:10:40
8 ;; Keywords: folding, outline
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;;; Commentary:
28 ;; This file provides folding editor extensions for outline-mode and
29 ;; outline-minor-mode buffers. What's a "folding editor"? Read on...
31 ;; Imagine you're in an outline-mode buffer and you've hidden all the text and
32 ;; subheadings under your level-1 headings. You now want to look at the stuff
33 ;; hidden under one of these headings. Normally you'd do C-c C-e (show-entry)
34 ;; to expose the body or C-c C-i to expose the child (level-2) headings.
36 ;; With foldout, you do C-c C-z (foldout-zoom-subtree). This exposes the body
37 ;; and child subheadings and narrows the buffer so that only the level-1
38 ;; heading, the body and the level-2 headings are visible. If you now want to
39 ;; look under one of the level-2 headings, position the cursor on it and do C-c
40 ;; C-z again. This exposes the level-2 body and its level-3 child subheadings
41 ;; and narrows the buffer again. You can keep on zooming in on successive
42 ;; subheadings as much as you like. A string in the modeline tells you how
43 ;; deep you've gone.
45 ;; When zooming in on a heading you might only want to see the child
46 ;; subheadings. You do this by specifying a numeric argument: C-u C-c C-z.
47 ;; You can specify the number of levels of children too (c.f. show-children):
48 ;; e.g. M-2 C-c C-z exposes two levels of child subheadings. Alternatively,
49 ;; you might only be interested in the body. You do this by specifying a
50 ;; negative argument: M-- C-c C-z. You can also cause the whole subtree to be
51 ;; expanded, similar to C-c C-s (show-subtree), by specifying a zero argument:
52 ;; M-0 C-c C-z.
54 ;; While you're zoomed in you can still use outline-mode's exposure and hiding
55 ;; functions. It won't upset foldout at all. Also, since the buffer is
56 ;; narrowed, "global" editing actions will only affect the stuff under the
57 ;; zoomed-in heading. This is useful for restricting changes to a particular
58 ;; chapter or section of your document.
60 ;; You unzoom (exit) a fold by doing C-c C-x (foldout-exit-fold). This hides
61 ;; all the text and subheadings under the top-level heading and returns you to
62 ;; the previous view of the buffer. Specifying a numeric argument exits that
63 ;; many folds. Specifying a zero argument exits *all* folds.
65 ;; You might want to exit a fold *without* hiding the text and subheadings.
66 ;; You do this by specifying a negative argument. For example, M--2 C-c C-x
67 ;; exits two folds and leaves the text and subheadings exposed.
69 ;; Foldout also provides mouse bindings for entering and exiting folds and for
70 ;; showing and hiding text. Hold down Meta and Control, then click a mouse
71 ;; button as follows:-
73 ;; mouse-1 (foldout-mouse-zoom) zooms in on the heading clicked on:-
75 ;; single click expose body
76 ;; double click expose subheadings
77 ;; triple click expose body and subheadings
78 ;; quad click expose entire subtree
80 ;; mouse-2 (foldout-mouse-show) exposes text under the heading clicked on:-
82 ;; single click expose body
83 ;; double click expose subheadings
84 ;; triple click expose body and subheadings
85 ;; quad click expose entire subtree
87 ;; mouse-3 (foldout-mouse-hide-or-exit) hides text under the heading clicked
88 ;; on or exits the fold:-
90 ;; single click hide subtree
91 ;; double click exit fold and hide text
92 ;; triple click exit fold without hiding text
93 ;; quad click exit all folds and hide text
95 ;; You can change the modifier keys used by setting `foldout-mouse-modifiers'.
97 ;;; Installation:
99 ;; To use foldout, put this in your .emacs:-
101 ;; (require 'foldout)
103 ;; If you don't want it loaded until you need it, try this instead:-
105 ;; (eval-after-load "outline" '(require 'foldout))
107 ;;; Advertisements:
109 ;; Get out-xtra.el by Per Abrahamsen <abraham@iesd.auc.dk> for more
110 ;; outline-mode goodies. In particular, `outline-hide-sublevels' makes
111 ;; setup a lot easier.
113 ;; folding.el by Jamie Lokier <u90jl@ecs.ox.ac.uk> supports folding by
114 ;; recognising special marker text in you file.
116 ;; c-outline.el (by me) provides outline-mode support to recognise `C'
117 ;; statements as outline headings, so with foldout you can have a folding `C'
118 ;; code editor without having to put in start- and end-of-fold markers. This
119 ;; is a real winner!
121 ;;; ChangeLog:
123 ;; 1.9 15-Mar-94
124 ;; Didn't test that very well, did I? The change to foldout-zoom-subtree
125 ;; affected foldout-mouse-zoom: if the heading under the `level n' one clicked
126 ;; on was at `level n+2' then it didn't get exposed. Sorry about that!
128 ;; 1.8 15-Mar-94
129 ;; Changed meaning of prefix arg to foldout-zoom-subtree. arg > 0 now means
130 ;; "expose that many children" instead of just "expose children" so it is more
131 ;; like `show-children' (C-c C-i). Arg of C-u on its own only shows one level
132 ;; of children, though, so you can still zoom by doing C-u C-c C-z.
134 ;; I can't think of a good meaning for the value of a negative prefix. Any
135 ;; suggestions?
137 ;; Added advertisement for my c-outline.el package. Now you can have a folding
138 ;; editor for c-mode without any effort!
140 ;; 1.7 7-Mar-94
141 ;; I got fed up trying to work out how many blank lines there were outside the
142 ;; narrowed region when inside a fold. Now *all* newlines before the following
143 ;; heading are *in* the narrowed region. Thus, if the cursor is at point-max,
144 ;; the number of blank lines above it is the number you'll get above the next
145 ;; heading.
147 ;; Since all newlines are now inside the narrowed region, when exiting a fold
148 ;; add a newline at the end of the region if there isn't one so that the
149 ;; following heading doesn't accidentally get joined to the body text.
151 ;; Bugfix: `foldout-mouse-modifiers' should be `defvar', not `defconst'.
153 ;; Use "cond" instead of "case" so that lemacs-19.9 users can use the mouse.
155 ;; Improve "Commentary" entry on using the mouse.
157 ;; Add "Installation" keyword.
159 ;; 1.6 3-Mar-94
160 ;; Add mouse support functions foldout-mouse-zoom, foldout-mouse-show,
161 ;; foldout-mouse-hide-or-exit.
163 ;; 1.5 11-Feb-94
164 ;; Rename `foldout-enter-subtree' to `foldout-zoom-subtree' and change
165 ;; keystroke from C-g to C-z. This is more mnemonic and leaves C-g alone, as
166 ;; users expect this to cancel the current key sequence.
168 ;; Added better commentary at the request of RMS. Added stuff to comply with
169 ;; the lisp-mnt.el conventions. Added instructions on how best to load the
170 ;; package.
172 ;; 1.4 2-Feb-94
173 ;; Bugfix: end-of-fold marking was wrong:-
175 ;; End of narrowed region should be one character on from
176 ;; (outline-end-of-subtree) so it includes the end-of-line at the end of the
177 ;; last line of the subtree.
179 ;; End-of-fold marker should be outside the narrowed region so text inserted
180 ;; at the end of the region goes before the marker. Need to make a special
181 ;; case for end-of-buffer because it is impossible to set a marker that will
182 ;; follow eob. Bummer.
184 ;; 1.3 28-Jan-94
185 ;; Changed `foldout-zoom-subtree'. A zero arg now makes it expose the entire
186 ;; subtree on entering the fold. As before, < 0 shows only the body and > 0
187 ;; shows only the subheadings.
189 ;; 1.2 28-Jan-94
190 ;; Fixed a dumb bug - didn't make `foldout-modeline-string' buffer-local :-(
192 ;; Changed `foldout-exit-fold' to use prefix arg to say how many folds to exit.
193 ;; Negative arg means exit but don't hide text. Zero arg means exit all folds.
195 ;; Added `foldout-inhibit-key-bindings' to inhibit key bindings.
197 ;; 1.1 27-Jan-94
198 ;; Released to the net. Inspired by a question in gnu.emacs.help from
199 ;; Jason D Lohn <jlohn@eng.umd.edu>.
201 ;;; Code:
203 (require 'outline)
205 ;; something has gone very wrong if outline-minor-mode isn't bound now.
206 (if (not (boundp 'outline-minor-mode))
207 (error "Can't find outline-minor-mode"))
209 (defconst foldout-fold-list nil
210 "List of start and end markers for the folds currently entered.
211 An end marker of NIL means the fold ends after (point-max).")
212 (make-variable-buffer-local 'foldout-fold-list)
214 (defconst foldout-modeline-string nil
215 "Modeline string announcing that we are in an outline fold.")
216 (make-variable-buffer-local 'foldout-modeline-string)
218 ;; put our minor mode string immediately following outline-minor-mode's
219 (or (assq 'foldout-modeline-string minor-mode-alist)
220 (let ((outl-entry (memq (assq 'outline-minor-mode minor-mode-alist)
221 minor-mode-alist))
222 (foldout-entry '((foldout-modeline-string foldout-modeline-string))))
224 ;; something's wrong with outline if we can't find it
225 (if (null outl-entry)
226 (error "Can't find outline-minor-mode in minor-mode-alist"))
228 ;; slip our fold announcement into the list
229 (setcdr outl-entry (nconc foldout-entry (cdr outl-entry)))
233 (defun foldout-zoom-subtree (&optional exposure)
234 "Open the subtree under the current heading and narrow to it.
236 Normally the body and the immediate subheadings are exposed, but
237 optional arg EXPOSURE \(interactively with prefix arg\) changes this:-
239 EXPOSURE > 0 exposes n levels of subheadings (c.f. show-children)
240 EXPOSURE < 0 exposes only the body
241 EXPOSURE = 0 exposes the entire subtree"
242 (interactive "P")
243 (save-excursion
244 (widen)
245 (outline-back-to-heading)
246 (let* ((exposure-value (prefix-numeric-value exposure))
247 (start (point))
248 (start-marker (point-marker))
249 (end (progn (outline-end-of-subtree)
250 (skip-chars-forward "\n\^M")
251 (point)))
252 ;; I need a marker that will follow the end of the region even when
253 ;; text is inserted right at the end. Text gets inserted *after*
254 ;; markers, so I need it at end+1. Unfortunately I can't set a
255 ;; marker at (point-max)+1, so I use NIL to mean the region ends at
256 ;; (point-max).
257 (end-marker (if (eobp) nil (set-marker (make-marker) (1+ end))))
260 ;; narrow to this subtree
261 (narrow-to-region start end)
263 ;; show the body and/or subheadings for this heading
264 (goto-char start)
265 (cond
266 ((null exposure)
267 (show-entry)
268 (show-children))
269 ((< exposure-value 0)
270 (show-entry))
271 ((consp exposure)
272 (show-children))
273 ((> exposure-value 0)
274 (show-children exposure-value))
276 (show-subtree))
279 ;; save the location of the fold we are entering
280 (setq foldout-fold-list (cons (cons start-marker end-marker)
281 foldout-fold-list))
283 ;; update the modeline
284 (foldout-update-modeline)
288 (defun foldout-exit-fold (&optional num-folds)
289 "Return to the ARG'th enclosing fold view. With ARG = 0 exit all folds.
291 Normally causes exited folds to be hidden, but with ARG < 0, -ARG folds are
292 exited and text is left visible."
293 (interactive "p")
294 (let (start-marker end-marker (hide-fold t))
296 ;; check there are some folds to leave
297 (if (null foldout-fold-list)
298 (error "Not in a fold!"))
300 (cond
301 ;; catch a request to leave all folds
302 ((zerop num-folds)
303 (setq num-folds (length foldout-fold-list)))
305 ;; have we been told not to hide the fold?
306 ((< num-folds 0)
307 (setq hide-fold nil
308 num-folds (- num-folds)))
311 ;; limit the number of folds if we've been told to exit too many
312 (setq num-folds (min num-folds (length foldout-fold-list)))
314 ;; exit the folds
315 (widen)
316 (loop
317 always (progn
318 ;; get the fold at the top of the stack
319 (setq start-marker (car (car foldout-fold-list))
320 end-marker (cdr (car foldout-fold-list))
321 foldout-fold-list (cdr foldout-fold-list)
322 num-folds (1- num-folds))
324 ;; Make sure there is a newline at the end of this fold,
325 ;; otherwise the following heading will get joined to the body
326 ;; text.
327 (if end-marker
328 (progn
329 (goto-char end-marker)
330 (forward-char -1)
331 (or (memq (preceding-char) '(?\n ?\^M))
332 (insert ?\n))))
334 ;; If this is the last fold to exit, hide the text unless we've
335 ;; been told not to. Note that at the moment point is at the
336 ;; beginning of the following heading if there is one.
338 ;; Also, make sure that the newline before the following heading
339 ;; is \n otherwise it will be hidden. If there is a newline
340 ;; before this one, make it visible too so we do the same as
341 ;; outline.el and leave a blank line before the heading.
342 (if (zerop num-folds)
343 (let ((beginning-of-heading (point))
344 (end-of-subtree (if end-marker
345 (progn
346 (forward-char -1)
347 (if (memq (preceding-char)
348 '(?\n ?\^M))
349 (forward-char -1))
350 (point))
351 (point-max))))
352 ;; hide the subtree
353 (if hide-fold
354 (outline-flag-region start-marker end-of-subtree ?\^M))
356 ;; make sure the next heading is exposed
357 (if end-marker
358 (outline-flag-region end-of-subtree
359 beginning-of-heading ?\n))
362 ;; zap the markers so they don't slow down editing
363 (set-marker start-marker nil)
364 (if end-marker (set-marker end-marker nil))
367 ;; have we exited enough folds?
368 until (zerop num-folds))
370 ;; narrow to the enclosing fold if there is one
371 (if foldout-fold-list
372 (progn
373 (setq start-marker (car (car foldout-fold-list))
374 end-marker (cdr (car foldout-fold-list)))
375 (narrow-to-region start-marker
376 (if end-marker
377 (1- (marker-position end-marker))
378 (point-max)))
380 (recenter)
382 ;; update the modeline
383 (foldout-update-modeline)
387 (defun foldout-update-modeline ()
388 "Set the modeline string to indicate our fold depth."
389 (let ((depth (length foldout-fold-list)))
390 (setq foldout-modeline-string
391 (cond
392 ;; if we're not in a fold, keep quiet
393 ((zerop depth)
394 nil)
395 ;; in outline-minor-mode we're after "Outl:xx" in the modeline
396 (outline-minor-mode
397 (format ":%d" depth))
398 ;; otherwise just announce the depth (I guess we're in outline-mode)
399 ((= depth 1)
400 " Inside 1 fold")
402 (format " Inside %d folds" depth))
403 ))))
406 (defun foldout-mouse-zoom (event)
407 "Zoom in on the heading clicked on.
409 How much is exposed by the zoom depends on the number of mouse clicks:-
411 1 expose body
412 2 expose subheadings
413 3 expose body and subheadings
414 4 expose entire subtree"
415 (interactive "@e")
417 ;; swallow intervening mouse events so we only get the final click-count.
418 (setq event (foldout-mouse-swallow-events event))
420 ;; go to the heading clicked on
421 (foldout-mouse-goto-heading event)
423 ;; zoom away
424 (foldout-zoom-subtree
425 (let ((nclicks (event-click-count event)))
426 (cond
427 ((= nclicks 1) -1) ; body only
428 ((= nclicks 2) '(1)) ; subheadings only
429 ((= nclicks 3) nil) ; body and subheadings
430 (t 0))))) ; entire subtree
432 (defun foldout-mouse-show (event)
433 "Show what is hidden under the heading clicked on.
435 What gets exposed depends on the number of mouse clicks:-
437 1 expose body
438 2 expose subheadings
439 3 expose body and subheadings
440 4 expose entire subtree"
441 (interactive "@e")
443 ;; swallow intervening mouse events so we only get the final click-count.
444 (setq event (foldout-mouse-swallow-events event))
446 ;; expose the text
447 (foldout-mouse-goto-heading event)
448 (let ((nclicks (event-click-count event)))
449 (cond
450 ((= nclicks 1) (show-entry))
451 ((= nclicks 2) (show-children))
452 ((= nclicks 3) (show-entry) (show-children))
453 (t (show-subtree)))))
455 (defun foldout-mouse-hide-or-exit (event)
456 "Hide the subtree under the heading clicked on, or exit a fold.
458 What happens depends on the number of mouse clicks:-
460 1 hide subtree
461 2 exit fold and hide text
462 3 exit fold without hiding text
463 4 exit all folds and hide text"
464 (interactive "@e")
466 ;; swallow intervening mouse events so we only get the final click-count.
467 (setq event (foldout-mouse-swallow-events event))
469 ;; hide or exit
470 (let ((nclicks (event-click-count event)))
471 (if (= nclicks 1)
472 (progn
473 (foldout-mouse-goto-heading event)
474 (hide-subtree))
475 (foldout-exit-fold
476 (cond
477 ((= nclicks 2) 1) ; exit and hide
478 ((= nclicks 3) -1) ; exit don't hide
479 (t 0)))))) ; exit all
482 (defun foldout-mouse-swallow-events (event)
483 "Swallow intervening mouse events so we only get the final click-count.
484 Signal an error if the final event isn't the same type as the first one."
485 (let ((initial-event-type (event-basic-type event)))
486 (while (null (sit-for 0 double-click-time 'nodisplay))
487 (setq event (read-event)))
488 (or (eq initial-event-type (event-basic-type event))
489 (error "")))
490 event)
492 (defun foldout-mouse-goto-heading (event)
493 "Go to the heading where the mouse event started. Signal an error
494 if the event didn't occur on a heading."
495 (goto-char (posn-point (event-start event)))
496 (or (outline-on-heading-p)
497 ;; outline.el sometimes treats beginning-of-buffer as a heading
498 ;; even though outline-on-heading returns nil.
499 (save-excursion (beginning-of-line) (bobp))
500 (error "Not a heading line")))
503 ;;; Keymaps:
505 (defvar foldout-inhibit-key-bindings nil
506 "Set non-NIL before loading foldout to inhibit key bindings.")
508 (defvar foldout-mouse-modifiers '(meta control)
509 "List of modifier keys to apply to foldout's mouse events.
511 The default (meta control) makes foldout bind its functions to
512 M-C-down-mouse-{1,2,3}.
514 Valid modifiers are shift, control, meta, alt, hyper and super.")
516 (if foldout-inhibit-key-bindings
518 (define-key outline-mode-map "\C-c\C-z" 'foldout-zoom-subtree)
519 (define-key outline-mode-map "\C-c\C-x" 'foldout-exit-fold)
520 (define-key outline-minor-mode-map
521 (concat outline-minor-mode-prefix "\C-z") 'foldout-zoom-subtree)
522 (define-key outline-minor-mode-map
523 (concat outline-minor-mode-prefix "\C-x") 'foldout-exit-fold)
525 (let* ((modifiers (apply 'concat
526 (mapcar (function
527 (lambda (modifier)
528 (vector
529 (cond
530 ((eq modifier 'shift) ?S)
531 ((eq modifier 'control) ?C)
532 ((eq modifier 'meta) ?M)
533 ((eq modifier 'alt) ?A)
534 ((eq modifier 'hyper) ?H)
535 ((eq modifier 'super) ?s)
536 (t (error "invalid mouse modifier %s"
537 modifier)))
538 ?-)))
539 foldout-mouse-modifiers)))
540 (mouse-1 (vector (intern (concat modifiers "down-mouse-1"))))
541 (mouse-2 (vector (intern (concat modifiers "down-mouse-2"))))
542 (mouse-3 (vector (intern (concat modifiers "down-mouse-3")))))
544 (define-key outline-mode-map mouse-1 'foldout-mouse-zoom)
545 (define-key outline-mode-map mouse-2 'foldout-mouse-show)
546 (define-key outline-mode-map mouse-3 'foldout-mouse-hide-or-exit)
548 (define-key outline-minor-mode-map mouse-1 'foldout-mouse-zoom)
549 (define-key outline-minor-mode-map mouse-2 'foldout-mouse-show)
550 (define-key outline-minor-mode-map mouse-3 'foldout-mouse-hide-or-exit)
553 (provide 'foldout)
555 ;;; foldout.el ends here