1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 93, 94, 95, 97, 2000, 2001
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package is a major mode for editing outline-format documents.
29 ;; An outline can be `abstracted' to show headers at any given level,
30 ;; with all stuff below hidden. See the Emacs manual for details.
34 ;; - subtree-terminators
35 ;; - better handle comments before function bodies (i.e. heading)
36 ;; - don't bother hiding whitespace
40 (defgroup outlines nil
41 "Support for hierarchical outlining"
45 (defcustom outline-regexp
"[*\^L]+"
46 "*Regular expression to match the beginning of a heading.
47 Any line whose beginning matches this regexp is considered to start a heading.
48 Note that Outline mode only checks this regexp at the start of a line,
49 so the regexp need not (and usually does not) start with `^'.
50 The recommended way to set this is with a Local Variables: list
51 in the file it applies to. See also `outline-heading-end-regexp'."
52 :type
'(choice regexp
(const nil
))
55 (defcustom outline-heading-end-regexp
"\n"
56 "*Regular expression to match the end of a heading line.
57 You can assume that point is at the beginning of a heading when this
58 regexp is searched for. The heading ends at the end of the match.
59 The recommended way to set this is with a `Local Variables:' list
60 in the file it applies to."
64 (defvar outline-mode-prefix-map
65 (let ((map (make-sparse-keymap)))
66 (define-key map
"@" 'outline-mark-subtree
)
67 (define-key map
"\C-n" 'outline-next-visible-heading
)
68 (define-key map
"\C-p" 'outline-previous-visible-heading
)
69 (define-key map
"\C-i" 'show-children
)
70 (define-key map
"\C-s" 'show-subtree
)
71 (define-key map
"\C-d" 'hide-subtree
)
72 (define-key map
"\C-u" 'outline-up-heading
)
73 (define-key map
"\C-f" 'outline-forward-same-level
)
74 (define-key map
"\C-b" 'outline-backward-same-level
)
75 (define-key map
"\C-t" 'hide-body
)
76 (define-key map
"\C-a" 'show-all
)
77 (define-key map
"\C-c" 'hide-entry
)
78 (define-key map
"\C-e" 'show-entry
)
79 (define-key map
"\C-l" 'hide-leaves
)
80 (define-key map
"\C-k" 'show-branches
)
81 (define-key map
"\C-q" 'hide-sublevels
)
82 (define-key map
"\C-o" 'hide-other
)
83 (define-key map
"\C-^" 'outline-move-subtree-up
)
84 (define-key map
"\C-v" 'outline-move-subtree-down
)
85 (define-key map
[(control ?
<)] 'outline-promote
)
86 (define-key map
[(control ?
>)] 'outline-demote
)
87 (define-key map
"\C-m" 'outline-insert-heading
)
88 ;; Where to bind outline-cycle ?
91 (defvar outline-mode-menu-bar-map
92 (let ((map (make-sparse-keymap)))
94 (define-key map
[hide] (cons "Hide" (make-sparse-keymap "Hide")))
96 (define-key map [hide hide-other] '("Hide Other" . hide-other))
97 (define-key map [hide hide-sublevels] '("Hide Sublevels" . hide-sublevels))
98 (define-key map [hide hide-subtree] '("Hide Subtree" . hide-subtree))
99 (define-key map [hide hide-entry] '("Hide Entry" . hide-entry))
100 (define-key map [hide hide-body] '("Hide Body" . hide-body))
101 (define-key map [hide hide-leaves] '("Hide Leaves" . hide-leaves))
103 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
105 (define-key map [show show-subtree] '("Show Subtree" . show-subtree))
106 (define-key map [show show-children] '("Show Children" . show-children))
107 (define-key map [show show-branches] '("Show Branches" . show-branches))
108 (define-key map [show show-entry] '("Show Entry" . show-entry))
109 (define-key map [show show-all] '("Show All" . show-all))
111 (define-key map [headings]
112 (cons "Headings" (make-sparse-keymap "Headings")))
114 (define-key map [headings demote-subtree]
115 '(menu-item "Demote subtree" outline-demote))
116 (define-key map [headings promote-subtree]
117 '(menu-item "Promote subtree" outline-promote))
118 (define-key map [headings move-subtree-down]
119 '(menu-item "Move subtree down" outline-move-subtree-down))
120 (define-key map [headings move-subtree-up]
121 '(menu-item "Move subtree up" outline-move-subtree-up))
122 (define-key map [headings copy]
123 '(menu-item "Copy to kill ring" outline-headers-as-kill
124 :enable mark-active))
125 (define-key map [headings outline-insert-heading]
126 '("New heading" . outline-insert-heading))
127 (define-key map [headings outline-backward-same-level]
128 '("Previous Same Level" . outline-backward-same-level))
129 (define-key map [headings outline-forward-same-level]
130 '("Next Same Level" . outline-forward-same-level))
131 (define-key map [headings outline-previous-visible-heading]
132 '("Previous" . outline-previous-visible-heading))
133 (define-key map [headings outline-next-visible-heading]
134 '("Next" . outline-next-visible-heading))
135 (define-key map [headings outline-up-heading]
136 '("Up" . outline-up-heading))
139 (defvar outline-minor-mode-menu-bar-map
140 (let ((map (make-sparse-keymap)))
141 (define-key map [outline]
143 (nconc (make-sparse-keymap "Outline")
144 ;; Remove extra separator
146 ;; Flatten the major mode's menus into a single menu.
150 ;; Add a separator between each
151 ;; part of the unified menu.
152 (cons '(--- "---") (cdr x))))
153 outline-mode-menu-bar-map))))))
157 (defvar outline-mode-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\C-c" outline-mode-prefix-map)
160 (define-key map [menu-bar] outline-mode-menu-bar-map)
163 (defvar outline-font-lock-keywords
165 ;; Highlight headings according to the level.
166 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
167 0 '(outline-font-lock-face) nil t)))
168 "Additional expressions to highlight in Outline mode.")
170 (defface outline-1 '((t :inherit font-lock-function-name-face)) "Level 1.")
171 (defface outline-2 '((t :inherit font-lock-variable-name-face)) "Level 2.")
172 (defface outline-3 '((t :inherit font-lock-keyword-face)) "Level 3.")
173 (defface outline-4 '((t :inherit font-lock-builtin-face)) "Level 4.")
174 (defface outline-5 '((t :inherit font-lock-comment-face)) "Level 5.")
175 (defface outline-6 '((t :inherit font-lock-constant-face)) "Level 6.")
176 (defface outline-7 '((t :inherit font-lock-type-face)) "Level 7.")
177 (defface outline-8 '((t :inherit font-lock-string-face)) "Level 8.")
179 (defvar outline-font-lock-faces
180 [outline-1 outline-2 outline-3 outline-4
181 outline-5 outline-6 outline-7 outline-8])
183 (defvar outline-font-lock-levels nil)
184 (make-variable-buffer-local 'outline-font-lock-levels)
186 (defun outline-font-lock-face ()
188 ;; (outline-back-to-heading t)
190 ;; (start-level (funcall outline-level))
191 ;; (level start-level)
193 ;; (while (not (setq face-level
194 ;; (if (or (bobp) (eq level 1)) 0
195 ;; (cdr (assq level outline-font-lock-levels)))))
196 ;; (outline-up-heading 1 t)
197 ;; (setq count (1+ count))
198 ;; (setq level (funcall outline-level)))
199 ;; ;; Remember for later.
200 ;; (unless (zerop count)
201 ;; (setq face-level (+ face-level count))
202 ;; (push (cons start-level face-level) outline-font-lock-levels))
203 ;; (condition-case nil
204 ;; (aref outline-font-lock-faces face-level)
205 ;; (error font-lock-warning-face))))
207 (goto-char (match-beginning 0))
208 (looking-at outline-regexp)
210 (aref outline-font-lock-faces (1- (funcall outline-level)))
211 (error font-lock-warning-face))))
213 (defvar outline-view-change-hook nil
214 "Normal hook to be run after outline visibility changes.")
216 (defvar outline-mode-hook nil
217 "*This hook is run when outline mode starts.")
220 (define-derived-mode outline-mode text-mode "Outline"
221 "Set major mode for editing outlines with selective display.
222 Headings are lines which start with asterisks: one for major headings,
223 two for subheadings, etc. Lines not starting with asterisks are body lines.
225 Body text or subheadings under a heading can be made temporarily
226 invisible, or visible again. Invisible lines are attached to the end
227 of the heading, so they move with it, if the line is killed and yanked
228 back. A heading with text hidden under it is marked with an ellipsis (...).
230 Commands:\\<outline-mode-map>
231 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
232 \\[outline-previous-visible-heading] outline-previous-visible-heading
233 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
234 \\[outline-backward-same-level] outline-backward-same-level
235 \\[outline-up-heading] outline-up-heading move from subheading to heading
237 \\[hide-body] make all text invisible (not headings).
238 \\[show-all] make everything in buffer visible.
239 \\[hide-sublevels] make only the first N levels of headers visible.
241 The remaining commands are used when point is on a heading line.
242 They apply to some of the body or subheadings of that heading.
243 \\[hide-subtree] hide-subtree make body and subheadings invisible.
244 \\[show-subtree] show-subtree make body and subheadings visible.
245 \\[show-children] show-children make direct subheadings visible.
246 No effect on body, or subheadings 2 or more levels down.
247 With arg N, affects subheadings N levels down.
248 \\[hide-entry] make immediately following body invisible.
249 \\[show-entry] make it visible.
250 \\[hide-leaves] make body under heading and under its subheadings invisible.
251 The subheadings remain visible.
252 \\[show-branches] make all subheadings at all levels visible.
254 The variable `outline-regexp' can be changed to control what is a heading.
255 A line is a heading if `outline-regexp' matches something at the
256 beginning of the line. The longer the match, the deeper the level.
258 Turning on outline mode calls the value of `text-mode-hook' and then of
259 `outline-mode-hook', if they are non-nil."
260 (make-local-variable 'line-move-ignore-invisible)
261 (setq line-move-ignore-invisible t)
262 ;; Cause use of ellipses for invisible text.
263 (add-to-invisibility-spec '(outline . t))
264 (set (make-local-variable 'paragraph-start)
265 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
266 ;; Inhibit auto-filling of header lines.
267 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
268 (set (make-local-variable 'paragraph-separate)
269 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
270 (set (make-local-variable 'font-lock-defaults)
271 '(outline-font-lock-keywords t nil nil backward-paragraph))
272 (setq imenu-generic-expression
273 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
274 (add-hook 'change-major-mode-hook 'show-all nil t)
275 (run-hooks 'outline-mode-hook))
277 (defcustom outline-minor-mode-prefix "\C-c@"
278 "*Prefix key to use for Outline commands in Outline minor mode.
279 The value of this variable is checked as part of loading Outline mode.
280 After that, changing the prefix key requires manipulating keymaps."
285 (define-minor-mode outline-minor-mode
286 "Toggle Outline minor mode.
287 With arg, turn Outline minor mode on if arg is positive, off otherwise.
288 See the command `outline-mode' for more information on this mode."
289 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
290 (cons outline-minor-mode-prefix outline-mode-prefix-map))
292 (if outline-minor-mode
294 ;; Turn off this mode if we change major modes.
295 (add-hook 'change-major-mode-hook
296 (lambda () (outline-minor-mode -1))
298 (set (make-local-variable 'line-move-ignore-invisible) t)
299 ;; Cause use of ellipses for invisible text.
300 (add-to-invisibility-spec '(outline . t)))
301 (setq line-move-ignore-invisible nil)
302 ;; Cause use of ellipses for invisible text.
303 (remove-from-invisibility-spec '(outline . t))
304 ;; When turning off outline mode, get rid of any outline hiding.
307 (defvar outline-level 'outline-level
308 "*Function of no args to compute a header's nesting level in an outline.
309 It can assume point is at the beginning of a header line and that the match
310 data reflects the `outline-regexp'.")
312 (defvar outline-heading-alist ()
313 "Alist associating a heading for every possible level.
314 Each entry is of the form (HEADING . LEVEL).
315 This alist is used two ways: to find the heading corresponding to
316 a given level and to find the level of a given heading.
317 If a mode or document needs several sets of outline headings (for example
318 numbered and unnumbered sections), list them set by set and sorted by level
319 within each set. For example in texinfo mode:
321 (setq outline-heading-alist
322 '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
323 (\"@subsubsection\" . 5)
324 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
325 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
326 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
327 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
329 Instead of sorting the entries in each set, you can also separate the
331 (make-variable-buffer-local 'outline-heading-alist)
333 ;; This used to count columns rather than characters, but that made ^L
334 ;; appear to be at level 2 instead of 1. Columns would be better for
335 ;; tab handling, but the default regexp doesn't use tabs, and anyone
336 ;; who changes the regexp can also redefine the outline-level variable
338 (defun outline-level ()
339 "Return the depth to which a statement is nested in the outline.
340 Point must be at the beginning of a header line.
341 This is actually either the level specified in `outline-heading-alist'
342 or else the number of characters matched by `outline-regexp'."
343 (or (cdr (assoc (match-string 0) outline-heading-alist))
344 (- (match-end 0) (match-beginning 0))))
346 (defun outline-next-preface ()
347 "Skip forward to just before the next heading line.
348 If there's no following heading line, stop before the newline
349 at the end of the buffer."
350 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
352 (goto-char (match-beginning 0)))
353 (if (and (bolp) (not (bobp)))
356 (defun outline-next-heading ()
357 "Move to the next (possibly invisible) heading line."
359 ;; Make sure we don't match the heading we're at.
360 (if (and (bolp) (not (eobp))) (forward-char 1))
361 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
363 (goto-char (match-beginning 0))))
365 (defun outline-previous-heading ()
366 "Move to the previous (possibly invisible) heading line."
368 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
371 (defsubst outline-invisible-p (&optional pos)
372 "Non-nil if the character after point is invisible."
373 (get-char-property (or pos (point)) 'invisible))
375 (defun outline-visible ()
376 (not (outline-invisible-p)))
377 (make-obsolete 'outline-visible 'outline-invisible-p)
379 (defun outline-back-to-heading (&optional invisible-ok)
380 "Move to previous heading line, or beg of this line if it's a heading.
381 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
383 (or (outline-on-heading-p invisible-ok)
387 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
389 (error "before first heading"))
390 (setq found (and (or invisible-ok (not (outline-invisible-p)))
395 (defun outline-on-heading-p (&optional invisible-ok)
396 "Return t if point is on a (visible) heading line.
397 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
400 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
401 (looking-at outline-regexp))))
403 (defun outline-insert-heading ()
404 "Insert a new heading at same depth at point."
406 (let ((head (save-excursion
408 (outline-back-to-heading)
409 (error (outline-next-heading)))
411 (or (caar outline-heading-alist) "")
413 (unless (or (string-match "[ \t]\\'" head)
414 (not (string-match outline-regexp (concat head " "))))
415 (setq head (concat head " ")))
416 (unless (bolp) (end-of-line) (newline))
419 (save-excursion (newline-and-indent)))
420 (run-hooks 'outline-insert-heading-hook)))
422 (defun outline-promote (&optional children)
423 "Promote headings higher up the tree.
424 If prefix argument CHILDREN is given, promote also all the children.
425 If the region is active in `transient-mark-mode', promote all headings
428 (list (if (and transient-mark-mode mark-active) 'region
429 (outline-back-to-heading)
430 (if current-prefix-arg nil 'subtree))))
432 ((eq children 'region)
433 (outline-map-region 'outline-promote (region-beginning) (region-end)))
435 (outline-map-region 'outline-promote
437 (save-excursion (outline-get-next-sibling) (point))))
439 (outline-back-to-heading t)
440 (let* ((head (match-string 0))
441 (level (save-match-data (funcall outline-level)))
442 (up-head (or (outline-head-from-level (1- level) head)
445 (outline-up-heading 1 t)
446 (match-string 0))))))
448 (unless (rassoc level outline-heading-alist)
449 (push (cons head level) outline-heading-alist))
451 (replace-match up-head nil t)))))
453 (defun outline-demote (&optional children)
454 "Demote headings lower down the tree.
455 If prefix argument CHILDREN is given, demote also all the children.
456 If the region is active in `transient-mark-mode', demote all headings
459 (list (if (and transient-mark-mode mark-active) 'region
460 (outline-back-to-heading)
461 (if current-prefix-arg nil 'subtree))))
463 ((eq children 'region)
464 (outline-map-region 'outline-demote (region-beginning) (region-end)))
466 (outline-map-region 'outline-demote
468 (save-excursion (outline-get-next-sibling) (point))))
470 (let* ((head (match-string 0))
471 (level (save-match-data (funcall outline-level)))
473 (or (outline-head-from-level (1+ level) head)
476 (while (and (progn (outline-next-heading) (not (eobp)))
477 (<= (funcall outline-level) level)))
479 ;; Try again from the beginning of the buffer.
480 (goto-char (point-min))
481 (while (and (progn (outline-next-heading) (not (eobp)))
482 (<= (funcall outline-level) level))))
484 (looking-at outline-regexp)
487 ;; Bummer!! There is no lower heading in the buffer.
488 ;; Let's try to invent one by repeating the first char.
489 (let ((new-head (concat (substring head 0 1) head)))
490 (if (string-match (concat "\\`" outline-regexp) new-head)
491 ;; Why bother checking that it is indeed lower level ?
493 ;; Didn't work: keep it as is so it's still a heading.
496 (unless (rassoc level outline-heading-alist)
497 (push (cons head level) outline-heading-alist))
498 (replace-match down-head nil t)))))
500 (defun outline-head-from-level (level head &optional alist)
501 "Get new heading with level LEVEL from ALIST.
502 If there are no such entries, return nil.
503 ALIST defaults to `outline-heading-alist'.
504 Similar to (car (rassoc LEVEL ALIST)).
505 If there are several different entries with same new level, choose
506 the one with the smallest distance to the assocation of HEAD in the alist.
507 This makes it possible for promotion to work in modes with several
508 independent sets of headings (numbered, unnumbered, appendix...)"
509 (unless alist (setq alist outline-heading-alist))
510 (let ((l (rassoc level alist))
514 ;; If there's no HEAD after L, any other entry for LEVEL after L
515 ;; can't be much better than L.
516 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
517 ;; If there's no other entry for LEVEL, just keep L.
518 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
519 ;; Now we have L, L2, and H: see if L2 seems better than L.
520 ;; If H is after L2, L2 is better.
521 ((memq h (setq l2l (memq l2 (cdr ll))))
522 (outline-head-from-level level head l2l))
523 ;; Now we have H between L and L2.
524 ;; If there's a separator between L and H, prefer L2.
525 ((memq h (memq nil ll))
526 (outline-head-from-level level head l2l))
527 ;; If there's a separator between L2 and H, prefer L.
528 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
529 ;; No separator between L and L2, check the distance.
530 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
531 (outline-head-from-level level head l2l))
532 ;; If all else fails, just keep L.
535 (defun outline-map-region (fun beg end)
536 "Call FUN for every heading between BEG and END.
537 When FUN is called, point is at the beginning of the heading and
538 the match data is set appropriately."
540 (setq end (copy-marker end))
542 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
543 (goto-char (match-beginning 0))
546 (outline-next-heading)
551 ;; Vertical tree motion
553 (defun outline-move-subtree-up (&optional arg)
554 "Move the currrent subtree up past ARG headlines of the same level."
556 (outline-move-subtree-down (- arg)))
558 (defun outline-move-subtree-down (&optional arg)
559 "Move the currrent subtree down past ARG headlines of the same level."
561 (let ((re (concat "^" outline-regexp))
562 (movfunc (if (> arg 0) 'outline-get-next-sibling
563 'outline-get-last-sibling))
564 (ins-point (make-marker))
568 (outline-back-to-heading)
571 (save-excursion (outline-end-of-heading)
572 (setq folded (outline-invisible-p)))
573 (outline-end-of-subtree))
574 (if (= (char-after) ?\n) (forward-char 1))
576 ;; Find insertion point, with error handling
579 (or (funcall movfunc)
580 (progn (goto-char beg)
581 (error "Cannot move past superior level")))
584 ;; Moving forward - still need to move over subtree
585 (progn (outline-end-of-subtree)
586 (if (= (char-after) ?\n) (forward-char 1))))
587 (move-marker ins-point (point))
588 (insert (delete-and-extract-region beg end))
589 (goto-char ins-point)
590 (if folded (hide-subtree))
591 (move-marker ins-point nil)))
593 (defun outline-end-of-heading ()
594 (if (re-search-forward outline-heading-end-regexp nil 'move)
597 (defun outline-next-visible-heading (arg)
598 "Move to the next visible heading line.
599 With argument, repeats or can move backward if negative.
600 A heading line is one that starts with a `*' (or that
601 `outline-regexp' matches)."
606 (while (and (not (bobp)) (< arg 0))
607 (while (and (not (bobp))
608 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
610 (outline-invisible-p)))
612 (while (and (not (eobp)) (> arg 0))
613 (while (and (not (eobp))
614 (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
616 (outline-invisible-p (match-beginning 0))))
620 (defun outline-previous-visible-heading (arg)
621 "Move to the previous heading line.
622 With argument, repeats or can move forward if negative.
623 A heading line is one that starts with a `*' (or that
624 `outline-regexp' matches)."
626 (outline-next-visible-heading (- arg)))
628 (defun outline-mark-subtree ()
629 "Mark the current subtree in an outlined document.
630 This puts point at the start of the current subtree, and mark at the end."
633 (if (outline-on-heading-p)
634 ;; we are already looking at a heading
636 ;; else go back to previous heading
637 (outline-previous-visible-heading 1))
639 (outline-end-of-subtree)
644 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
645 (defun outline-flag-region (from to flag)
646 "Hide or show lines from FROM to TO, according to FLAG.
647 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
648 (remove-overlays from to 'invisible 'outline)
650 (let ((o (make-overlay from to)))
651 (overlay-put o 'invisible 'outline)
652 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
653 ;; Seems only used by lazy-lock. I.e. obsolete.
654 (run-hooks 'outline-view-change-hook))
656 (defun outline-reveal-toggle-invisible (o hidep)
658 (goto-char (overlay-start o))
660 ;; When hiding the area again, we could just clean it up and let
661 ;; reveal do the rest, by simply doing:
662 ;; (remove-overlays (overlay-start o) (overlay-end o)
663 ;; 'invisible 'outline)
665 ;; That works fine as long as everything is in sync, but if the
666 ;; structure of the document is changed while revealing parts of it,
667 ;; the resulting behavior can be ugly. I.e. we need to make
668 ;; sure that we hide exactly a subtree.
670 (let ((end (overlay-end o)))
674 (outline-next-visible-heading 1)
675 (and (not (eobp)) (< (point) end))))))
677 ;; When revealing, we just need to reveal sublevels. If point is
678 ;; inside one of the sublevels, reveal will call us again.
679 ;; But we need to preserve the original overlay.
680 (let ((o1 (copy-overlay o)))
681 (overlay-put o 'invisible nil) ;Show (most of) the text.
685 ;; Normally just the above is needed.
686 ;; But in odd cases, the above might fail to show anything.
687 ;; To avoid an infinite loop, we have to make sure that
688 ;; *something* gets shown.
689 (and (equal (overlay-start o) (overlay-start o1))
690 (< (point) (overlay-end o))
691 (= 0 (forward-line 1)))))
692 ;; If still nothing was shown, just kill the damn thing.
693 (when (equal (overlay-start o) (overlay-start o1))
694 ;; I've seen it happen at the end of buffer.
695 (delete-overlay o1))))))
697 ;; Function to be set as an outline-isearch-open-invisible' property
698 ;; to the overlay that makes the outline invisible (see
699 ;; `outline-flag-region').
700 (defun outline-isearch-open-invisible (overlay)
701 ;; We rely on the fact that isearch places point on the matched text.
705 "Hide the body directly following this heading."
707 (outline-back-to-heading)
708 (outline-end-of-heading)
710 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
713 "Show the body directly following this heading.
714 Show the heading too, if it is currently invisible."
717 (outline-back-to-heading t)
718 (outline-flag-region (1- (point))
719 (progn (outline-next-preface) (point)) nil)))
722 "Hide all of buffer except headings."
724 (hide-region-body (point-min) (point-max)))
726 (defun hide-region-body (start end)
727 "Hide all body lines in the region, but not headings."
728 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
729 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
730 ;; and run the hook finally.
731 (let (outline-view-change-hook)
734 (narrow-to-region start end)
735 (goto-char (point-min))
736 (if (outline-on-heading-p)
737 (outline-end-of-heading))
739 (outline-flag-region (point)
740 (progn (outline-next-preface) (point)) t)
742 (forward-char (if (looking-at "\n\n") 2 1))
743 (outline-end-of-heading))))))
744 (run-hooks 'outline-view-change-hook))
747 "Show all of the text in the buffer."
749 (outline-flag-region (point-min) (point-max) nil))
751 (defun hide-subtree ()
752 "Hide everything after this heading at deeper levels."
754 (outline-flag-subtree t))
756 (defun hide-leaves ()
757 "Hide all body after this heading at deeper levels."
759 (outline-back-to-heading)
761 (outline-end-of-heading)
762 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
764 (defun show-subtree ()
765 "Show everything after this heading at deeper levels."
767 (outline-flag-subtree nil))
769 (defun outline-show-heading ()
770 "Show the current heading and move to its end."
771 (outline-flag-region (- (point)
773 (if (eq (char-before (1- (point))) ?\n)
775 (progn (outline-end-of-heading) (point))
778 (defun hide-sublevels (levels)
779 "Hide everything but the top LEVELS levels of headers, in whole buffer."
782 (error "Must keep at least one level of headers"))
783 (let (outline-view-change-hook)
785 (goto-char (point-min))
786 ;; Skip the prelude, if any.
787 (unless (outline-on-heading-p t) (outline-next-heading))
788 ;; First hide everything.
789 (outline-flag-region (point) (point-max) t)
790 ;; Then unhide the top level headers.
793 (if (<= (funcall outline-level) levels)
794 (outline-show-heading)))
795 (point) (point-max))))
796 (run-hooks 'outline-view-change-hook))
799 "Hide everything except current body and parent and top-level headings."
802 (let (outline-view-change-hook)
804 (outline-back-to-heading t)
806 (while (condition-case nil (progn (outline-up-heading 1) (not (bobp)))
808 (outline-flag-region (1- (point))
809 (save-excursion (forward-line 1) (point))
811 (run-hooks 'outline-view-change-hook))
813 (defun outline-toggle-children ()
814 "Show or hide the current subtree depending on its current state."
816 (outline-back-to-heading)
817 (if (not (outline-invisible-p (line-end-position)))
822 (defun outline-flag-subtree (flag)
824 (outline-back-to-heading)
825 (outline-end-of-heading)
826 (outline-flag-region (point)
827 (progn (outline-end-of-subtree) (point))
830 (defun outline-end-of-subtree ()
831 (outline-back-to-heading)
832 (let ((opoint (point))
834 (level (funcall outline-level)))
835 (while (and (not (eobp))
836 (or first (> (funcall outline-level) level)))
838 (outline-next-heading))
841 ;; Go to end of line before heading
844 ;; leave blank line before heading
845 (forward-char -1))))))
847 (defun show-branches ()
848 "Show all subheadings of this heading, but not their bodies."
850 (show-children 1000))
852 (defun show-children (&optional level)
853 "Show all direct subheadings of this heading.
854 Prefix arg LEVEL is how many levels below the current level should be shown.
855 Default is enough to cause the following heading to appear."
858 (if level (prefix-numeric-value level)
860 (outline-back-to-heading)
861 (let ((start-level (funcall outline-level)))
862 (outline-next-heading)
865 (max 1 (- (funcall outline-level) start-level)))))))
866 (let (outline-view-change-hook)
868 (outline-back-to-heading)
869 (setq level (+ level (funcall outline-level)))
872 (if (<= (funcall outline-level) level)
873 (outline-show-heading)))
875 (progn (outline-end-of-subtree)
876 (if (eobp) (point-max) (1+ (point)))))))
877 (run-hooks 'outline-view-change-hook))
881 (defun outline-up-heading (arg &optional invisible-ok)
882 "Move to the visible heading line of which the present line is a subheading.
883 With argument, move up ARG levels.
884 If INVISIBLE-OK is non-nil, also consider invisible lines."
886 (outline-back-to-heading invisible-ok)
887 (let ((start-level (funcall outline-level)))
888 (if (eq start-level 1)
889 (error "Already at top level of the outline"))
890 (while (and (> start-level 1) (> arg 0) (not (bobp)))
891 (let ((level start-level))
892 (while (not (or (< level start-level) (bobp)))
894 (outline-previous-heading)
895 (outline-previous-visible-heading 1))
896 (setq level (funcall outline-level)))
897 (setq start-level level))
898 (setq arg (- arg 1))))
899 (looking-at outline-regexp))
901 (defun outline-forward-same-level (arg)
902 "Move forward to the ARG'th subheading at same level as this one.
903 Stop at the first and last subheadings of a superior heading."
905 (outline-back-to-heading)
907 (let ((point-to-move-to (save-excursion
908 (outline-get-next-sibling))))
911 (goto-char point-to-move-to)
915 (error "No following same-level heading"))))))
917 (defun outline-get-next-sibling ()
918 "Move to next heading of the same level, and return point or nil if none."
919 (let ((level (funcall outline-level)))
920 (outline-next-visible-heading 1)
921 (while (and (not (eobp)) (> (funcall outline-level) level))
922 (outline-next-visible-heading 1))
923 (if (or (eobp) (< (funcall outline-level) level))
927 (defun outline-backward-same-level (arg)
928 "Move backward to the ARG'th subheading at same level as this one.
929 Stop at the first and last subheadings of a superior heading."
931 (outline-back-to-heading)
933 (let ((point-to-move-to (save-excursion
934 (outline-get-last-sibling))))
937 (goto-char point-to-move-to)
941 (error "No previous same-level heading"))))))
943 (defun outline-get-last-sibling ()
944 "Move to previous heading of the same level, and return point or nil if none."
945 (let ((level (funcall outline-level)))
946 (outline-previous-visible-heading 1)
947 (while (and (> (funcall outline-level) level)
949 (outline-previous-visible-heading 1))
950 (if (< (funcall outline-level) level)
954 (defun outline-headers-as-kill (beg end)
955 "Save the visible outline headers in region at the start of the kill ring.
957 Text shown between the headers isn't copied. Two newlines are
958 inserted between saved headers. Yanking the result may be a
959 convenient way to make a table of contents of the buffer."
963 (narrow-to-region beg end)
964 (goto-char (point-min))
965 (let ((buffer (current-buffer))
968 (with-current-buffer buffer
969 ;; Boundary condition: starting on heading:
970 (when (outline-on-heading-p)
971 (outline-back-to-heading)
973 end (progn (outline-end-of-heading)
975 (insert-buffer-substring buffer start end)
977 (let ((temp-buffer (current-buffer)))
978 (with-current-buffer buffer
979 (while (outline-next-heading)
980 (unless (outline-invisible-p)
982 end (progn (outline-end-of-heading) (point)))
983 (with-current-buffer temp-buffer
984 (insert-buffer-substring buffer start end)
986 (kill-new (buffer-string)))))))
991 ;;; outline.el ends here