Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / foldout.el
blob34e3c6da66b083a5ba634c3fa38cbe3a48aba815
1 ;;; foldout.el --- folding extensions for outline-mode and outline-minor-mode
3 ;; Copyright (C) 1994, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Kevin Broadey <KevinB@bartley.demon.co.uk>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Created: 27 Jan 1994
8 ;; Version: 1.10
9 ;; Keywords: folding, outlines
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
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 mode line 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 ;; recognizing special marker text in you file.
116 ;; c-outline.el (by me) provides outline-mode support to recognize `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.10 21-Mar-94
124 ;; foldout.el is now part of the GNU Emacs distribution!!
125 ;; Put in changes made by RMS to version 1.8 to keep the diffs to a minimum.
126 ;; bugfix: numeric arg to foldout-exit-fold wasn't working - looks like I don't
127 ;; know how to use the Common LISP `loop' macro after all, so use `while'
128 ;; instead.
130 ;; 1.9 15-Mar-94
131 ;; Didn't test that very well, did I? The change to foldout-zoom-subtree
132 ;; affected foldout-mouse-zoom: if the heading under the `level n' one clicked
133 ;; on was at `level n+2' then it didn't get exposed. Sorry about that!
135 ;; 1.8 15-Mar-94
136 ;; Changed meaning of prefix arg to foldout-zoom-subtree. arg > 0 now means
137 ;; "expose that many children" instead of just "expose children" so it is more
138 ;; like `show-children' (C-c C-i). Arg of C-u on its own only shows one level
139 ;; of children, though, so you can still zoom by doing C-u C-c C-z.
141 ;; I can't think of a good meaning for the value of a negative prefix. Any
142 ;; suggestions?
144 ;; Added advertisement for my c-outline.el package. Now you can have a folding
145 ;; editor for c-mode without any effort!
147 ;; 1.7 7-Mar-94
148 ;; I got fed up trying to work out how many blank lines there were outside the
149 ;; narrowed region when inside a fold. Now *all* newlines before the following
150 ;; heading are *in* the narrowed region. Thus, if the cursor is at point-max,
151 ;; the number of blank lines above it is the number you'll get above the next
152 ;; heading.
154 ;; Since all newlines are now inside the narrowed region, when exiting a fold
155 ;; add a newline at the end of the region if there isn't one so that the
156 ;; following heading doesn't accidentally get joined to the body text.
158 ;; Bugfix: `foldout-mouse-modifiers' should be `defvar', not `defconst'.
160 ;; Use "cond" instead of "case" so that lemacs-19.9 users can use the mouse.
162 ;; Improve "Commentary" entry on using the mouse.
164 ;; Add "Installation" keyword.
166 ;; 1.6 3-Mar-94
167 ;; Add mouse support functions foldout-mouse-zoom, foldout-mouse-show,
168 ;; foldout-mouse-hide-or-exit.
170 ;; 1.5 11-Feb-94
171 ;; Rename `foldout-enter-subtree' to `foldout-zoom-subtree' and change
172 ;; keystroke from C-g to C-z. This is more mnemonic and leaves C-g alone, as
173 ;; users expect this to cancel the current key sequence.
175 ;; Added better commentary at the request of RMS. Added stuff to comply with
176 ;; the lisp-mnt.el conventions. Added instructions on how best to load the
177 ;; package.
179 ;; 1.4 2-Feb-94
180 ;; Bugfix: end-of-fold marking was wrong:-
182 ;; End of narrowed region should be one character on from
183 ;; (outline-end-of-subtree) so it includes the end-of-line at the end of the
184 ;; last line of the subtree.
186 ;; End-of-fold marker should be outside the narrowed region so text inserted
187 ;; at the end of the region goes before the marker. Need to make a special
188 ;; case for end-of-buffer because it is impossible to set a marker that will
189 ;; follow eob. Bummer.
191 ;; 1.3 28-Jan-94
192 ;; Changed `foldout-zoom-subtree'. A zero arg now makes it expose the entire
193 ;; subtree on entering the fold. As before, < 0 shows only the body and > 0
194 ;; shows only the subheadings.
196 ;; 1.2 28-Jan-94
197 ;; Fixed a dumb bug - didn't make `foldout-mode-line-string' buffer-local :-(
199 ;; Changed `foldout-exit-fold' to use prefix arg to say how many folds to exit.
200 ;; Negative arg means exit but don't hide text. Zero arg means exit all folds.
202 ;; Added `foldout-inhibit-key-bindings' to inhibit key bindings.
204 ;; 1.1 27-Jan-94
205 ;; Released to the net. Inspired by a question in gnu.emacs.help from
206 ;; Jason D Lohn <jlohn@eng.umd.edu>.
208 ;;; Code:
210 (require 'outline)
212 (defvar foldout-fold-list nil
213 "List of start and end markers for the folds currently entered.
214 An end marker of nil means the fold ends after (point-max).")
215 (make-variable-buffer-local 'foldout-fold-list)
217 (defvar foldout-mode-line-string nil
218 "Mode line string announcing that we are in an outline fold.")
219 (make-variable-buffer-local 'foldout-mode-line-string)
221 ;; put our minor mode string immediately following outline-minor-mode's
222 (or (assq 'foldout-mode-line-string minor-mode-alist)
223 (let ((outl-entry (memq (assq 'outline-minor-mode minor-mode-alist)
224 minor-mode-alist))
225 (foldout-entry '((foldout-mode-line-string foldout-mode-line-string))))
227 ;; something's wrong with outline if we can't find it
228 (if (null outl-entry)
229 (error "Can't find outline-minor-mode in minor-mode-alist"))
231 ;; slip our fold announcement into the list
232 (setcdr outl-entry (nconc foldout-entry (cdr outl-entry)))
235 ;; outline-flag-region has different `flag' values in outline.el and
236 ;; noutline.el for hiding and showing text.
238 (defconst foldout-hide-flag
239 (if (featurep 'noutline) t ?\^M))
241 (defconst foldout-show-flag
242 (if (featurep 'noutline) nil ?\n))
245 (defun foldout-zoom-subtree (&optional exposure)
246 "Open the subtree under the current heading and narrow to it.
248 Normally the body and the immediate subheadings are exposed, but
249 optional arg EXPOSURE \(interactively with prefix arg) changes this:-
251 EXPOSURE > 0 exposes n levels of subheadings (c.f. show-children)
252 EXPOSURE < 0 exposes only the body
253 EXPOSURE = 0 exposes the entire subtree"
254 (interactive "P")
255 (save-excursion
256 (widen)
257 (outline-back-to-heading)
258 (let* ((exposure-value (prefix-numeric-value exposure))
259 (start (point))
260 (start-marker (point-marker))
261 (end (progn (outline-end-of-subtree)
262 (skip-chars-forward "\n\^M")
263 (point)))
264 ;; I need a marker that will follow the end of the region even when
265 ;; text is inserted right at the end. Text gets inserted *after*
266 ;; markers, so I need it at end+1. Unfortunately I can't set a
267 ;; marker at (point-max)+1, so I use nil to mean the region ends at
268 ;; (point-max).
269 (end-marker (if (eobp) nil (set-marker (make-marker) (1+ end))))
272 ;; narrow to this subtree
273 (narrow-to-region start end)
275 ;; show the body and/or subheadings for this heading
276 (goto-char start)
277 (cond
278 ((null exposure)
279 (outline-show-entry)
280 (outline-show-children))
281 ((< exposure-value 0)
282 (outline-show-entry))
283 ((consp exposure)
284 (outline-show-children))
285 ((> exposure-value 0)
286 (outline-show-children exposure-value))
288 (outline-show-subtree))
291 ;; save the location of the fold we are entering
292 (setq foldout-fold-list (cons (cons start-marker end-marker)
293 foldout-fold-list))
295 ;; update the mode line
296 (foldout-update-mode-line)
300 (defun foldout-exit-fold (&optional num-folds)
301 "Return to the ARG'th enclosing fold view. With ARG = 0 exit all folds.
303 Normally causes exited folds to be hidden, but with ARG < 0, -ARG folds are
304 exited and text is left visible."
305 (interactive "p")
306 (let ((hide-fold t) start-marker end-marker
307 beginning-of-heading end-of-subtree)
309 ;; check there are some folds to leave
310 (if (null foldout-fold-list)
311 (error "Not in a fold!"))
313 (cond
314 ;; catch a request to leave all folds
315 ((zerop num-folds)
316 (setq num-folds (length foldout-fold-list)))
318 ;; have we been told not to hide the fold?
319 ((< num-folds 0)
320 (setq hide-fold nil
321 num-folds (- num-folds)))
324 ;; limit the number of folds if we've been told to exit too many
325 (setq num-folds (min num-folds (length foldout-fold-list)))
327 ;; exit the folds
328 (widen)
329 (while (not (zerop num-folds))
330 ;; get the fold at the top of the stack
331 (setq start-marker (car (car foldout-fold-list))
332 end-marker (cdr (car foldout-fold-list))
333 foldout-fold-list (cdr foldout-fold-list)
334 num-folds (1- num-folds))
336 ;; Make sure there is a newline at the end of this fold,
337 ;; otherwise the following heading will get joined to the body
338 ;; text.
339 (if end-marker
340 (progn
341 (goto-char end-marker)
342 (forward-char -1)
343 (or (memq (preceding-char) '(?\n ?\^M))
344 (insert ?\n))))
346 ;; If this is the last fold to exit, hide the text unless we've
347 ;; been told not to. Note that at the moment point is at the
348 ;; beginning of the following heading if there is one.
350 ;; Also, make sure that the newline before the following heading
351 ;; is \n otherwise it will be hidden. If there is a newline
352 ;; before this one, make it visible too so we do the same as
353 ;; outline.el and leave a blank line before the heading.
354 (when (zerop num-folds)
355 (if end-marker
356 (setq beginning-of-heading (point)
357 end-of-subtree (progn (forward-char -1)
358 (if (memq (preceding-char)
359 '(?\n ?\^M))
360 (forward-char -1))
361 (point))))
362 ;; hide the subtree
363 (when hide-fold
364 (goto-char start-marker)
365 (outline-hide-subtree))
367 ;; make sure the next heading is exposed
368 (if end-marker
369 (outline-flag-region end-of-subtree beginning-of-heading
370 foldout-show-flag)))
372 ;; zap the markers so they don't slow down editing
373 (set-marker start-marker nil)
374 (if end-marker (set-marker end-marker nil)))
376 ;; narrow to the enclosing fold if there is one
377 (if foldout-fold-list
378 (progn
379 (setq start-marker (car (car foldout-fold-list))
380 end-marker (cdr (car foldout-fold-list)))
381 (narrow-to-region start-marker
382 (if end-marker
383 (1- (marker-position end-marker))
384 (point-max)))))
385 (recenter)
387 ;; update the mode line
388 (foldout-update-mode-line)))
391 (defun foldout-update-mode-line ()
392 "Set the mode line to indicate our fold depth."
393 (let ((depth (length foldout-fold-list)))
394 (setq foldout-mode-line-string
395 (cond
396 ;; if we're not in a fold, keep quiet
397 ((zerop depth)
398 nil)
399 ;; in outline-minor-mode we're after "Outl:xx" in the mode line
400 (outline-minor-mode
401 (format ":%d" depth))
402 ;; otherwise just announce the depth (I guess we're in outline-mode)
403 ((= depth 1)
404 " Inside 1 fold")
406 (format " Inside %d folds" depth))))))
409 (defun foldout-mouse-zoom (event)
410 "Zoom in on the heading clicked on.
412 How much is exposed by the zoom depends on the number of mouse clicks:-
414 1 expose body
415 2 expose subheadings
416 3 expose body and subheadings
417 4 expose entire subtree"
418 (interactive "@e")
420 ;; swallow intervening mouse events so we only get the final click-count.
421 (setq event (foldout-mouse-swallow-events event))
423 ;; go to the heading clicked on
424 (foldout-mouse-goto-heading event)
426 ;; zoom away
427 (foldout-zoom-subtree
428 (let ((nclicks (event-click-count event)))
429 (cond
430 ((= nclicks 1) -1) ; body only
431 ((= nclicks 2) '(1)) ; subheadings only
432 ((= nclicks 3) nil) ; body and subheadings
433 (t 0))))) ; entire subtree
435 (defun foldout-mouse-show (event)
436 "Show what is hidden under the heading clicked on.
438 What gets exposed depends on the number of mouse clicks:-
440 1 expose body
441 2 expose subheadings
442 3 expose body and subheadings
443 4 expose entire subtree"
444 (interactive "@e")
446 ;; swallow intervening mouse events so we only get the final click-count.
447 (setq event (foldout-mouse-swallow-events event))
449 ;; expose the text
450 (foldout-mouse-goto-heading event)
451 (let ((nclicks (event-click-count event)))
452 (cond
453 ((= nclicks 1) (outline-show-entry))
454 ((= nclicks 2) (outline-show-children))
455 ((= nclicks 3) (outline-show-entry) (outline-show-children))
456 (t (outline-show-subtree)))))
458 (defun foldout-mouse-hide-or-exit (event)
459 "Hide the subtree under the heading clicked on, or exit a fold.
461 What happens depends on the number of mouse clicks:-
463 1 hide subtree
464 2 exit fold and hide text
465 3 exit fold without hiding text
466 4 exit all folds and hide text"
467 (interactive "@e")
469 ;; swallow intervening mouse events so we only get the final click-count.
470 (setq event (foldout-mouse-swallow-events event))
472 ;; hide or exit
473 (let ((nclicks (event-click-count event)))
474 (if (= nclicks 1)
475 (progn
476 (foldout-mouse-goto-heading event)
477 (outline-hide-subtree))
478 (foldout-exit-fold
479 (cond
480 ((= nclicks 2) 1) ; exit and hide
481 ((= nclicks 3) -1) ; exit don't hide
482 (t 0)))))) ; exit all
485 (defun foldout-mouse-swallow-events (event)
486 "Swallow intervening mouse events so we only get the final click-count.
487 Signal an error if the final event isn't the same type as the first one."
488 (let ((initial-event-type (event-basic-type event)))
489 (while (null (sit-for (/ double-click-time 1000.0) 'nodisplay))
490 (setq event (read-event)))
491 (or (eq initial-event-type (event-basic-type event))
492 (error "")))
493 event)
495 (defun foldout-mouse-goto-heading (event)
496 "Go to the heading where the mouse event started. Signal an error
497 if the event didn't occur on a heading."
498 (goto-char (posn-point (event-start event)))
499 (or (outline-on-heading-p)
500 ;; outline.el sometimes treats beginning-of-buffer as a heading
501 ;; even though outline-on-heading returns nil.
502 (save-excursion (beginning-of-line) (bobp))
503 (error "Not a heading line")))
506 ;;; Keymaps:
508 (defvar foldout-inhibit-key-bindings nil
509 "Set non-nil before loading foldout to inhibit key bindings.")
511 (defvar foldout-mouse-modifiers '(meta control)
512 "List of modifier keys to apply to foldout's mouse events.
514 The default (meta control) makes foldout bind its functions to
515 M-C-down-mouse-{1,2,3}.
517 Valid modifiers are shift, control, meta, alt, hyper and super.")
519 (if foldout-inhibit-key-bindings
521 (define-key outline-mode-map "\C-c\C-z" 'foldout-zoom-subtree)
522 (define-key outline-mode-map "\C-c\C-x" 'foldout-exit-fold)
523 (let ((map (lookup-key outline-minor-mode-map outline-minor-mode-prefix)))
524 (unless map
525 (setq map (make-sparse-keymap))
526 (define-key outline-minor-mode-map outline-minor-mode-prefix map))
527 (define-key map "\C-z" 'foldout-zoom-subtree)
528 (define-key map "\C-x" 'foldout-exit-fold))
529 (let* ((modifiers (apply 'concat
530 (mapcar (function
531 (lambda (modifier)
532 (vector
533 (cond
534 ((eq modifier 'shift) ?S)
535 ((eq modifier 'control) ?C)
536 ((eq modifier 'meta) ?M)
537 ((eq modifier 'alt) ?A)
538 ((eq modifier 'hyper) ?H)
539 ((eq modifier 'super) ?s)
540 (t (error "invalid mouse modifier %s"
541 modifier)))
542 ?-)))
543 foldout-mouse-modifiers)))
544 (mouse-1 (vector (intern (concat modifiers "down-mouse-1"))))
545 (mouse-2 (vector (intern (concat modifiers "down-mouse-2"))))
546 (mouse-3 (vector (intern (concat modifiers "down-mouse-3")))))
548 (define-key outline-mode-map mouse-1 'foldout-mouse-zoom)
549 (define-key outline-mode-map mouse-2 'foldout-mouse-show)
550 (define-key outline-mode-map mouse-3 'foldout-mouse-hide-or-exit)
552 (define-key outline-minor-mode-map mouse-1 'foldout-mouse-zoom)
553 (define-key outline-minor-mode-map mouse-2 'foldout-mouse-show)
554 (define-key outline-minor-mode-map mouse-3 'foldout-mouse-hide-or-exit)
557 (provide 'foldout)
559 ;;; foldout.el ends here