1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 1993-1995, 1997, 2000-2015 Free Software
6 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package is a major mode for editing outline-format documents.
27 ;; An outline can be `abstracted' to show headers at any given level,
28 ;; with all stuff below hidden. See the Emacs manual for details.
32 ;; - subtree-terminators
33 ;; - better handle comments before function bodies (i.e. heading)
34 ;; - don't bother hiding whitespace
38 (defgroup outlines nil
39 "Support for hierarchical outlining."
43 (defvar outline-regexp
"[*\^L]+"
44 "Regular expression to match the beginning of a heading.
45 Any line whose beginning matches this regexp is considered to start a heading.
46 Note that Outline mode only checks this regexp at the start of a line,
47 so the regexp need not (and usually does not) start with `^'.
48 The recommended way to set this is with a Local Variables: list
49 in the file it applies to. See also `outline-heading-end-regexp'.")
50 ;;;###autoload(put 'outline-regexp 'safe-local-variable 'stringp)
52 (defvar outline-heading-end-regexp
"\n"
53 "Regular expression to match the end of a heading line.
54 You can assume that point is at the beginning of a heading when this
55 regexp is searched for. The heading ends at the end of the match.
56 The recommended way to set this is with a `Local Variables:' list
57 in the file it applies to.")
58 ;;;###autoload(put 'outline-heading-end-regexp 'safe-local-variable 'stringp)
60 (defvar outline-mode-prefix-map
61 (let ((map (make-sparse-keymap)))
62 (define-key map
"@" 'outline-mark-subtree
)
63 (define-key map
"\C-n" 'outline-next-visible-heading
)
64 (define-key map
"\C-p" 'outline-previous-visible-heading
)
65 (define-key map
"\C-i" 'show-children
)
66 (define-key map
"\C-s" 'show-subtree
)
67 (define-key map
"\C-d" 'hide-subtree
)
68 (define-key map
"\C-u" 'outline-up-heading
)
69 (define-key map
"\C-f" 'outline-forward-same-level
)
70 (define-key map
"\C-b" 'outline-backward-same-level
)
71 (define-key map
"\C-t" 'hide-body
)
72 (define-key map
"\C-a" 'show-all
)
73 (define-key map
"\C-c" 'hide-entry
)
74 (define-key map
"\C-e" 'show-entry
)
75 (define-key map
"\C-l" 'hide-leaves
)
76 (define-key map
"\C-k" 'show-branches
)
77 (define-key map
"\C-q" 'hide-sublevels
)
78 (define-key map
"\C-o" 'hide-other
)
79 (define-key map
"\C-^" 'outline-move-subtree-up
)
80 (define-key map
"\C-v" 'outline-move-subtree-down
)
81 (define-key map
[(control ?
<)] 'outline-promote
)
82 (define-key map
[(control ?
>)] 'outline-demote
)
83 (define-key map
"\C-m" 'outline-insert-heading
)
86 (defvar outline-mode-menu-bar-map
87 (let ((map (make-sparse-keymap)))
89 (define-key map
[hide] (cons "Hide" (make-sparse-keymap "Hide")))
91 (define-key map [hide hide-other]
92 '(menu-item "Hide Other" hide-other
93 :help "Hide everything except current body and parent and top-level headings"))
94 (define-key map [hide hide-sublevels]
95 '(menu-item "Hide Sublevels" hide-sublevels
96 :help "Hide everything but the top LEVELS levels of headers, in whole buffer"))
97 (define-key map [hide hide-subtree]
98 '(menu-item "Hide Subtree" hide-subtree
99 :help "Hide everything after this heading at deeper levels"))
100 (define-key map [hide hide-entry]
101 '(menu-item "Hide Entry" hide-entry
102 :help "Hide the body directly following this heading"))
103 (define-key map [hide hide-body]
104 '(menu-item "Hide Body" hide-body
105 :help "Hide all body lines in buffer, leaving all headings visible"))
106 (define-key map [hide hide-leaves]
107 '(menu-item "Hide Leaves" hide-leaves
108 :help "Hide the body after this heading and at deeper levels"))
110 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
112 (define-key map [show show-subtree]
113 '(menu-item "Show Subtree" show-subtree
114 :help "Show everything after this heading at deeper levels"))
115 (define-key map [show show-children]
116 '(menu-item "Show Children" show-children
117 :help "Show all direct subheadings of this heading"))
118 (define-key map [show show-branches]
119 '(menu-item "Show Branches" show-branches
120 :help "Show all subheadings of this heading, but not their bodies"))
121 (define-key map [show show-entry]
122 '(menu-item "Show Entry" show-entry
123 :help "Show the body directly following this heading"))
124 (define-key map [show show-all]
125 '(menu-item "Show All" show-all
126 :help "Show all of the text in the buffer"))
128 (define-key map [headings]
129 (cons "Headings" (make-sparse-keymap "Headings")))
131 (define-key map [headings demote-subtree]
132 '(menu-item "Demote Subtree" outline-demote
133 :help "Demote headings lower down the tree"))
134 (define-key map [headings promote-subtree]
135 '(menu-item "Promote Subtree" outline-promote
136 :help "Promote headings higher up the tree"))
137 (define-key map [headings move-subtree-down]
138 '(menu-item "Move Subtree Down" outline-move-subtree-down
139 :help "Move the current subtree down past arg headlines of the same level"))
140 (define-key map [headings move-subtree-up]
141 '(menu-item "Move Subtree Up" outline-move-subtree-up
142 :help "Move the current subtree up past arg headlines of the same level"))
143 (define-key map [headings copy]
144 '(menu-item "Copy to Kill Ring" outline-headers-as-kill
146 :help "Save the visible outline headers in region at the start of the kill ring"))
147 (define-key map [headings outline-insert-heading]
148 '(menu-item "New Heading" outline-insert-heading
149 :help "Insert a new heading at same depth at point"))
150 (define-key map [headings outline-backward-same-level]
152 '(menu-item "Previous Same Level" outline-backward-same-level
153 :help "Move backward to the arg'th subheading at same level as this one."))
154 (define-key map [headings outline-forward-same-level]
156 '(menu-item "Next Same Level" outline-forward-same-level
157 :help "Move forward to the arg'th subheading at same level as this one"))
158 (define-key map [headings outline-previous-visible-heading]
160 '(menu-item "Previous" outline-previous-visible-heading
161 :help "Move to the previous heading line"))
162 (define-key map [headings outline-next-visible-heading]
164 '(menu-item "Next" outline-next-visible-heading
165 :help "Move to the next visible heading line"))
166 (define-key map [headings outline-up-heading]
168 '(menu-item "Up" outline-up-heading
169 :help "Move to the visible heading line of which the present line is a subheading"))
172 (defvar outline-minor-mode-menu-bar-map
173 (let ((map (make-sparse-keymap)))
174 (define-key map [outline]
176 (nconc (make-sparse-keymap "Outline")
177 ;; Remove extra separator
179 ;; Flatten the major mode's menus into a single menu.
183 ;; Add a separator between each
184 ;; part of the unified menu.
185 (cons '(--- "---") (cdr x))))
186 outline-mode-menu-bar-map))))))
189 (defvar outline-mode-map
190 (let ((map (make-sparse-keymap)))
191 (define-key map "\C-c" outline-mode-prefix-map)
192 (define-key map [menu-bar] outline-mode-menu-bar-map)
195 (defvar outline-font-lock-keywords
197 ;; Highlight headings according to the level.
198 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
199 0 '(outline-font-lock-face) nil t)))
200 "Additional expressions to highlight in Outline mode.")
203 '((t :inherit font-lock-function-name-face))
208 '((t :inherit font-lock-variable-name-face))
213 '((t :inherit font-lock-keyword-face))
218 '((t :inherit font-lock-comment-face))
223 '((t :inherit font-lock-type-face))
228 '((t :inherit font-lock-constant-face))
233 '((t :inherit font-lock-builtin-face))
238 '((t :inherit font-lock-string-face))
242 (defvar outline-font-lock-faces
243 [outline-1 outline-2 outline-3 outline-4
244 outline-5 outline-6 outline-7 outline-8])
246 (defun outline-font-lock-face ()
248 (goto-char (match-beginning 0))
249 (looking-at outline-regexp)
250 (aref outline-font-lock-faces
251 (% (1- (funcall outline-level))
252 (length outline-font-lock-faces)))))
254 (defvar outline-view-change-hook nil
255 "Normal hook to be run after outline visibility changes.")
257 (defvar outline-mode-hook nil
258 "This hook is run when outline mode starts.")
260 (defvar outline-blank-line nil
261 "Non-nil means to leave unhidden blank line before heading.")
264 (define-derived-mode outline-mode text-mode "Outline"
265 "Set major mode for editing outlines with selective display.
266 Headings are lines which start with asterisks: one for major headings,
267 two for subheadings, etc. Lines not starting with asterisks are body lines.
269 Body text or subheadings under a heading can be made temporarily
270 invisible, or visible again. Invisible lines are attached to the end
271 of the heading, so they move with it, if the line is killed and yanked
272 back. A heading with text hidden under it is marked with an ellipsis (...).
275 The commands `hide-subtree', `show-subtree', `show-children',
276 `hide-entry', `show-entry', `hide-leaves', and `show-branches'
277 are used when point is on a heading line.
279 The variable `outline-regexp' can be changed to control what is a heading.
280 A line is a heading if `outline-regexp' matches something at the
281 beginning of the line. The longer the match, the deeper the level.
283 Turning on outline mode calls the value of `text-mode-hook' and then of
284 `outline-mode-hook', if they are non-nil."
285 (make-local-variable 'line-move-ignore-invisible)
286 (setq line-move-ignore-invisible t)
287 ;; Cause use of ellipses for invisible text.
288 (add-to-invisibility-spec '(outline . t))
289 (set (make-local-variable 'paragraph-start)
290 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
291 ;; Inhibit auto-filling of header lines.
292 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
293 (set (make-local-variable 'paragraph-separate)
294 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
295 (set (make-local-variable 'font-lock-defaults)
296 '(outline-font-lock-keywords t nil nil backward-paragraph))
297 (setq imenu-generic-expression
298 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
299 (add-hook 'change-major-mode-hook 'show-all nil t))
301 (defcustom outline-minor-mode-prefix "\C-c@"
302 "Prefix key to use for Outline commands in Outline minor mode.
303 The value of this variable is checked as part of loading Outline mode.
304 After that, changing the prefix key requires manipulating keymaps."
309 (define-minor-mode outline-minor-mode
310 "Toggle Outline minor mode.
311 With a prefix argument ARG, enable Outline minor mode if ARG is
312 positive, and disable it otherwise. If called from Lisp, enable
313 the mode if ARG is omitted or nil.
315 See the command `outline-mode' for more information on this mode."
316 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
317 (cons outline-minor-mode-prefix outline-mode-prefix-map))
319 (if outline-minor-mode
321 ;; Turn off this mode if we change major modes.
322 (add-hook 'change-major-mode-hook
323 (lambda () (outline-minor-mode -1))
325 (set (make-local-variable 'line-move-ignore-invisible) t)
326 ;; Cause use of ellipses for invisible text.
327 (add-to-invisibility-spec '(outline . t)))
328 (setq line-move-ignore-invisible nil)
329 ;; Cause use of ellipses for invisible text.
330 (remove-from-invisibility-spec '(outline . t))
331 ;; When turning off outline mode, get rid of any outline hiding.
334 (defvar outline-level 'outline-level
335 "Function of no args to compute a header's nesting level in an outline.
336 It can assume point is at the beginning of a header line and that the match
337 data reflects the `outline-regexp'.")
338 ;;;###autoload(put 'outline-level 'risky-local-variable t)
340 (defvar outline-heading-alist ()
341 "Alist associating a heading for every possible level.
342 Each entry is of the form (HEADING . LEVEL).
343 This alist is used two ways: to find the heading corresponding to
344 a given level and to find the level of a given heading.
345 If a mode or document needs several sets of outline headings (for example
346 numbered and unnumbered sections), list them set by set and sorted by level
347 within each set. For example in texinfo mode:
349 (setq outline-heading-alist
350 '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
351 (\"@subsubsection\" . 5)
352 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
353 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
354 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
355 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
357 Instead of sorting the entries in each set, you can also separate the
359 (make-variable-buffer-local 'outline-heading-alist)
361 ;; This used to count columns rather than characters, but that made ^L
362 ;; appear to be at level 2 instead of 1. Columns would be better for
363 ;; tab handling, but the default regexp doesn't use tabs, and anyone
364 ;; who changes the regexp can also redefine the outline-level variable
366 (defun outline-level ()
367 "Return the depth to which a statement is nested in the outline.
368 Point must be at the beginning of a header line.
369 This is actually either the level specified in `outline-heading-alist'
370 or else the number of characters matched by `outline-regexp'."
371 (or (cdr (assoc (match-string 0) outline-heading-alist))
372 (- (match-end 0) (match-beginning 0))))
374 (defun outline-next-preface ()
375 "Skip forward to just before the next heading line.
376 If there's no following heading line, stop before the newline
377 at the end of the buffer."
378 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
380 (goto-char (match-beginning 0)))
381 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
384 (defun outline-next-heading ()
385 "Move to the next (possibly invisible) heading line."
387 ;; Make sure we don't match the heading we're at.
388 (if (and (bolp) (not (eobp))) (forward-char 1))
389 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
391 (goto-char (match-beginning 0))))
393 (defun outline-previous-heading ()
394 "Move to the previous (possibly invisible) heading line."
396 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
399 (defsubst outline-invisible-p (&optional pos)
400 "Non-nil if the character after point is invisible."
401 (get-char-property (or pos (point)) 'invisible))
403 (defun outline-back-to-heading (&optional invisible-ok)
404 "Move to previous heading line, or beg of this line if it's a heading.
405 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
407 (or (outline-on-heading-p invisible-ok)
411 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
413 (error "before first heading"))
414 (setq found (and (or invisible-ok (not (outline-invisible-p)))
419 (defun outline-on-heading-p (&optional invisible-ok)
420 "Return t if point is on a (visible) heading line.
421 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
424 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
425 (looking-at outline-regexp))))
427 (defun outline-insert-heading ()
428 "Insert a new heading at same depth at point."
430 (let ((head (save-excursion
432 (outline-back-to-heading)
433 (error (outline-next-heading)))
435 (or (caar outline-heading-alist) "")
437 (unless (or (string-match "[ \t]\\'" head)
438 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
440 (setq head (concat head " ")))
441 (unless (bolp) (end-of-line) (newline))
444 (save-excursion (newline-and-indent)))
445 (run-hooks 'outline-insert-heading-hook)))
447 (defun outline-invent-heading (head up)
449 ;; Let's try to invent one by repeating or deleting the last char.
450 (let ((new-head (if up (substring head 0 -1)
451 (concat head (substring head -1)))))
452 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
454 ;; Why bother checking that it is indeed higher/lower level ?
456 ;; Didn't work, so ask what to do.
457 (read-string (format "%s heading for `%s': "
458 (if up "Parent" "Demoted") head)
461 (defun outline-promote (&optional which)
462 "Promote headings higher up the tree.
463 If transient-mark-mode is on, and mark is active, promote headings in
464 the region (from a Lisp program, pass `region' for WHICH). Otherwise:
465 without prefix argument, promote current heading and all headings in the
466 subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
467 argument, promote just the current heading (from a Lisp program, pass
468 nil for WHICH, or do not pass any argument)."
470 (list (if (and transient-mark-mode mark-active) 'region
471 (outline-back-to-heading)
472 (if current-prefix-arg nil 'subtree))))
475 (outline-map-region 'outline-promote (region-beginning) (region-end)))
477 (outline-map-region 'outline-promote
479 (save-excursion (outline-get-next-sibling) (point))))
481 (outline-back-to-heading t)
482 (let* ((head (match-string-no-properties 0))
483 (level (save-match-data (funcall outline-level)))
484 (up-head (or (outline-head-from-level (1- level) head)
485 ;; Use the parent heading, if it is really
489 (outline-up-heading 1 t)
490 (and (= (1- level) (funcall outline-level))
491 (match-string-no-properties 0))))
492 ;; Bummer!! There is no lower level heading.
493 (outline-invent-heading head 'up))))
495 (unless (rassoc level outline-heading-alist)
496 (push (cons head level) outline-heading-alist))
498 (replace-match up-head nil t)))))
500 (defun outline-demote (&optional which)
501 "Demote headings lower down the tree.
502 If transient-mark-mode is on, and mark is active, demote headings in
503 the region (from a Lisp program, pass `region' for WHICH). Otherwise:
504 without prefix argument, demote current heading and all headings in the
505 subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
506 argument, demote just the current heading (from a Lisp program, pass
507 nil for WHICH, or do not pass any argument)."
509 (list (if (and transient-mark-mode mark-active) 'region
510 (outline-back-to-heading)
511 (if current-prefix-arg nil 'subtree))))
514 (outline-map-region 'outline-demote (region-beginning) (region-end)))
516 (outline-map-region 'outline-demote
518 (save-excursion (outline-get-next-sibling) (point))))
520 (let* ((head (match-string-no-properties 0))
521 (level (save-match-data (funcall outline-level)))
523 (or (outline-head-from-level (1+ level) head)
526 (while (and (progn (outline-next-heading) (not (eobp)))
527 (<= (funcall outline-level) level)))
529 ;; Try again from the beginning of the buffer.
530 (goto-char (point-min))
531 (while (and (progn (outline-next-heading) (not (eobp)))
532 (<= (funcall outline-level) level))))
534 (looking-at outline-regexp)
535 (match-string-no-properties 0))))
536 ;; Bummer!! There is no higher-level heading in the buffer.
537 (outline-invent-heading head nil))))
539 (unless (rassoc level outline-heading-alist)
540 (push (cons head level) outline-heading-alist))
541 (replace-match down-head nil t)))))
543 (defun outline-head-from-level (level head &optional alist)
544 "Get new heading with level LEVEL from ALIST.
545 If there are no such entries, return nil.
546 ALIST defaults to `outline-heading-alist'.
547 Similar to (car (rassoc LEVEL ALIST)).
548 If there are several different entries with same new level, choose
549 the one with the smallest distance to the association of HEAD in the alist.
550 This makes it possible for promotion to work in modes with several
551 independent sets of headings (numbered, unnumbered, appendix...)"
552 (unless alist (setq alist outline-heading-alist))
553 (let ((l (rassoc level alist))
557 ;; If there's no HEAD after L, any other entry for LEVEL after L
558 ;; can't be much better than L.
559 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
560 ;; If there's no other entry for LEVEL, just keep L.
561 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
562 ;; Now we have L, L2, and H: see if L2 seems better than L.
563 ;; If H is after L2, L2 is better.
564 ((memq h (setq l2l (memq l2 (cdr ll))))
565 (outline-head-from-level level head l2l))
566 ;; Now we have H between L and L2.
567 ;; If there's a separator between L and H, prefer L2.
568 ((memq h (memq nil ll))
569 (outline-head-from-level level head l2l))
570 ;; If there's a separator between L2 and H, prefer L.
571 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
572 ;; No separator between L and L2, check the distance.
573 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
574 (outline-head-from-level level head l2l))
575 ;; If all else fails, just keep L.
578 (defun outline-map-region (fun beg end)
579 "Call FUN for every heading between BEG and END.
580 When FUN is called, point is at the beginning of the heading and
581 the match data is set appropriately."
583 (setq end (copy-marker end))
585 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
586 (goto-char (match-beginning 0))
589 (outline-next-heading)
594 ;; Vertical tree motion
596 (defun outline-move-subtree-up (&optional arg)
597 "Move the current subtree up past ARG headlines of the same level."
599 (outline-move-subtree-down (- arg)))
601 (defun outline-move-subtree-down (&optional arg)
602 "Move the current subtree down past ARG headlines of the same level."
604 (outline-back-to-heading)
605 (let* ((movfunc (if (> arg 0) 'outline-get-next-sibling
606 'outline-get-last-sibling))
607 ;; Find the end of the subtree to be moved as well as the point to
608 ;; move it to, adding a newline if necessary, to ensure these points
609 ;; are at bol on the line below the subtree.
610 (end-point-func (lambda ()
611 (outline-end-of-subtree)
612 (if (eq (char-after) ?\n) (forward-char 1)
613 (if (and (eobp) (not (bolp))) (insert "\n")))
616 (folded (save-match-data
617 (outline-end-of-heading)
618 (outline-invisible-p)))
619 (end (save-match-data
620 (funcall end-point-func)))
621 (ins-point (make-marker))
623 ;; Find insertion point, with error handling.
626 (or (funcall movfunc)
627 (progn (goto-char beg)
628 (user-error "Cannot move past superior level")))
631 ;; Moving forward - still need to move over subtree.
632 (funcall end-point-func))
633 (move-marker ins-point (point))
634 (insert (delete-and-extract-region beg end))
635 (goto-char ins-point)
636 (if folded (hide-subtree))
637 (move-marker ins-point nil)))
639 (defun outline-end-of-heading ()
640 (if (re-search-forward outline-heading-end-regexp nil 'move)
643 (defun outline-next-visible-heading (arg)
644 "Move to the next visible heading line.
645 With argument, repeats or can move backward if negative.
646 A heading line is one that starts with a `*' (or that
647 `outline-regexp' matches)."
652 (let (found-heading-p)
653 (while (and (not (bobp)) (< arg 0))
654 (while (and (not (bobp))
655 (setq found-heading-p
657 (concat "^\\(?:" outline-regexp "\\)")
659 (outline-invisible-p)))
661 (while (and (not (eobp)) (> arg 0))
662 (while (and (not (eobp))
663 (setq found-heading-p
665 (concat "^\\(?:" outline-regexp "\\)")
667 (outline-invisible-p (match-beginning 0))))
669 (if found-heading-p (beginning-of-line))))
671 (defun outline-previous-visible-heading (arg)
672 "Move to the previous heading line.
673 With argument, repeats or can move forward if negative.
674 A heading line is one that starts with a `*' (or that
675 `outline-regexp' matches)."
677 (outline-next-visible-heading (- arg)))
679 (defun outline-mark-subtree ()
680 "Mark the current subtree in an outlined document.
681 This puts point at the start of the current subtree, and mark at the end."
684 (if (outline-on-heading-p)
685 ;; we are already looking at a heading
687 ;; else go back to previous heading
688 (outline-previous-visible-heading 1))
690 (outline-end-of-subtree)
691 (push-mark (point) nil t)
695 (defvar outline-isearch-open-invisible-function nil
696 "Function called if `isearch' finishes in an invisible overlay.
697 The function is called with the overlay as its only argument.
698 If nil, `show-entry' is called to reveal the invisible text.")
700 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
701 (defun outline-flag-region (from to flag)
702 "Hide or show lines from FROM to TO, according to FLAG.
703 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
704 (remove-overlays from to 'invisible 'outline)
706 ;; We use `front-advance' here because the invisible text begins at the
707 ;; very end of the heading, before the newline, so text inserted at FROM
708 ;; belongs to the heading rather than to the entry.
709 (let ((o (make-overlay from to nil 'front-advance)))
710 (overlay-put o 'evaporate t)
711 (overlay-put o 'invisible 'outline)
712 (overlay-put o 'isearch-open-invisible
713 (or outline-isearch-open-invisible-function
714 'outline-isearch-open-invisible))))
715 ;; Seems only used by lazy-lock. I.e. obsolete.
716 (run-hooks 'outline-view-change-hook))
718 (defun outline-reveal-toggle-invisible (o hidep)
720 (goto-char (overlay-start o))
722 ;; When hiding the area again, we could just clean it up and let
723 ;; reveal do the rest, by simply doing:
724 ;; (remove-overlays (overlay-start o) (overlay-end o)
725 ;; 'invisible 'outline)
727 ;; That works fine as long as everything is in sync, but if the
728 ;; structure of the document is changed while revealing parts of it,
729 ;; the resulting behavior can be ugly. I.e. we need to make
730 ;; sure that we hide exactly a subtree.
732 (let ((end (overlay-end o)))
736 (outline-next-visible-heading 1)
737 (and (not (eobp)) (< (point) end))))))
739 ;; When revealing, we just need to reveal sublevels. If point is
740 ;; inside one of the sublevels, reveal will call us again.
741 ;; But we need to preserve the original overlay.
742 (let ((o1 (copy-overlay o)))
743 (overlay-put o 'invisible nil) ;Show (most of) the text.
747 ;; Normally just the above is needed.
748 ;; But in odd cases, the above might fail to show anything.
749 ;; To avoid an infinite loop, we have to make sure that
750 ;; *something* gets shown.
751 (and (equal (overlay-start o) (overlay-start o1))
752 (< (point) (overlay-end o))
753 (= 0 (forward-line 1)))))
754 ;; If still nothing was shown, just kill the damn thing.
755 (when (equal (overlay-start o) (overlay-start o1))
756 ;; I've seen it happen at the end of buffer.
757 (delete-overlay o1))))))
759 ;; Function to be set as an outline-isearch-open-invisible' property
760 ;; to the overlay that makes the outline invisible (see
761 ;; `outline-flag-region').
762 (defun outline-isearch-open-invisible (_overlay)
763 ;; We rely on the fact that isearch places point on the matched text.
767 "Hide the body directly following this heading."
770 (outline-back-to-heading)
771 (outline-end-of-heading)
772 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
775 "Show the body directly following this heading.
776 Show the heading too, if it is currently invisible."
779 (outline-back-to-heading t)
780 (outline-flag-region (1- (point))
781 (progn (outline-next-preface) (point)) nil)))
784 "Hide all body lines in buffer, leaving all headings visible."
786 (hide-region-body (point-min) (point-max)))
788 (defun hide-region-body (start end)
789 "Hide all body lines in the region, but not headings."
790 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
791 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
792 ;; and run the hook finally.
793 (let (outline-view-change-hook)
796 (narrow-to-region start end)
797 (goto-char (point-min))
798 (if (outline-on-heading-p)
799 (outline-end-of-heading)
800 (outline-next-preface))
802 (outline-flag-region (point)
803 (progn (outline-next-preface) (point)) t)
805 (forward-char (if (looking-at "\n\n") 2 1))
806 (outline-end-of-heading))))))
807 (run-hooks 'outline-view-change-hook))
810 "Show all of the text in the buffer."
812 (outline-flag-region (point-min) (point-max) nil))
814 (defun hide-subtree ()
815 "Hide everything after this heading at deeper levels."
817 (outline-flag-subtree t))
819 (defun hide-leaves ()
820 "Hide the body after this heading and at deeper levels."
823 (outline-back-to-heading)
824 ;; Turned off to fix bug reported by Otto Maddox on 22 Nov 2005.
825 ;; (outline-end-of-heading)
826 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
828 (defun show-subtree ()
829 "Show everything after this heading at deeper levels."
831 (outline-flag-subtree nil))
833 (defun outline-show-heading ()
834 "Show the current heading and move to its end."
835 (outline-flag-region (- (point)
837 (if (and outline-blank-line
838 (eq (char-before (1- (point))) ?\n))
840 (progn (outline-end-of-heading) (point))
843 (defun hide-sublevels (levels)
844 "Hide everything but the top LEVELS levels of headers, in whole buffer."
847 (current-prefix-arg (prefix-numeric-value current-prefix-arg))
848 ((save-excursion (beginning-of-line)
849 (looking-at outline-regexp))
850 (funcall outline-level))
853 (error "Must keep at least one level of headers"))
855 (let* (outline-view-change-hook
857 (goto-char (point-min))
858 ;; Skip the prelude, if any.
859 (unless (outline-on-heading-p t) (outline-next-heading))
862 (goto-char (point-max))
863 ;; Keep empty last line, if available.
864 (if (bolp) (1- (point)) (point)))))
866 (setq beg (prog1 end (setq end beg))))
867 ;; First hide everything.
868 (outline-flag-region beg end t)
869 ;; Then unhide the top level headers.
872 (if (<= (funcall outline-level) levels)
873 (outline-show-heading)))
875 ;; Finally unhide any trailing newline.
876 (goto-char (point-max))
877 (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
878 (outline-flag-region (1- (point)) (point) nil))))
879 (run-hooks 'outline-view-change-hook))
882 "Hide everything except current body and parent and top-level headings."
885 (let (outline-view-change-hook)
887 (outline-back-to-heading t)
889 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
891 (outline-flag-region (1- (point))
892 (save-excursion (forward-line 1) (point))
894 (run-hooks 'outline-view-change-hook))
896 (defun outline-toggle-children ()
897 "Show or hide the current subtree depending on its current state."
900 (outline-back-to-heading)
901 (if (not (outline-invisible-p (line-end-position)))
906 (defun outline-flag-subtree (flag)
908 (outline-back-to-heading)
909 (outline-end-of-heading)
910 (outline-flag-region (point)
911 (progn (outline-end-of-subtree) (point))
914 (defun outline-end-of-subtree ()
915 (outline-back-to-heading)
917 (level (funcall outline-level)))
918 (while (and (not (eobp))
919 (or first (> (funcall outline-level) level)))
921 (outline-next-heading))
922 (if (and (bolp) (not (eolp)))
923 ;; We stopped at a nonempty line (the next heading).
925 ;; Go to end of line before heading
927 (if (and outline-blank-line (bolp))
928 ;; leave blank line before heading
929 (forward-char -1))))))
931 (defun show-branches ()
932 "Show all subheadings of this heading, but not their bodies."
934 (show-children 1000))
936 (defun show-children (&optional level)
937 "Show all direct subheadings of this heading.
938 Prefix arg LEVEL is how many levels below the current level should be shown.
939 Default is enough to cause the following heading to appear."
942 (if level (prefix-numeric-value level)
944 (outline-back-to-heading)
945 (let ((start-level (funcall outline-level)))
946 (outline-next-heading)
949 (max 1 (- (funcall outline-level) start-level)))))))
950 (let (outline-view-change-hook)
952 (outline-back-to-heading)
953 (setq level (+ level (funcall outline-level)))
956 (if (<= (funcall outline-level) level)
957 (outline-show-heading)))
959 (progn (outline-end-of-subtree)
960 (if (eobp) (point-max) (1+ (point)))))))
961 (run-hooks 'outline-view-change-hook))
965 (defun outline-up-heading (arg &optional invisible-ok)
966 "Move to the visible heading line of which the present line is a subheading.
967 With argument, move up ARG levels.
968 If INVISIBLE-OK is non-nil, also consider invisible lines."
970 (and (eq this-command 'outline-up-heading)
971 (or (eq last-command 'outline-up-heading) (push-mark)))
972 (outline-back-to-heading invisible-ok)
973 (let ((start-level (funcall outline-level)))
974 (when (<= start-level 1)
975 (error "Already at top level of the outline"))
976 (while (and (> start-level 1) (> arg 0) (not (bobp)))
977 (let ((level start-level))
978 (while (not (or (< level start-level) (bobp)))
980 (outline-previous-heading)
981 (outline-previous-visible-heading 1))
982 (setq level (funcall outline-level)))
983 (setq start-level level))
984 (setq arg (- arg 1))))
985 (looking-at outline-regexp))
987 (defun outline-forward-same-level (arg)
988 "Move forward to the ARG'th subheading at same level as this one.
989 Stop at the first and last subheadings of a superior heading."
991 (outline-back-to-heading)
993 (let ((point-to-move-to (save-excursion
994 (outline-get-next-sibling))))
997 (goto-char point-to-move-to)
1001 (error "No following same-level heading"))))))
1003 (defun outline-get-next-sibling ()
1004 "Move to next heading of the same level, and return point.
1005 If there is no such heading, return nil."
1006 (let ((level (funcall outline-level)))
1007 (outline-next-visible-heading 1)
1008 (while (and (not (eobp)) (> (funcall outline-level) level))
1009 (outline-next-visible-heading 1))
1010 (if (or (eobp) (< (funcall outline-level) level))
1014 (defun outline-backward-same-level (arg)
1015 "Move backward to the ARG'th subheading at same level as this one.
1016 Stop at the first and last subheadings of a superior heading."
1018 (outline-back-to-heading)
1020 (let ((point-to-move-to (save-excursion
1021 (outline-get-last-sibling))))
1022 (if point-to-move-to
1024 (goto-char point-to-move-to)
1025 (setq arg (1- arg)))
1028 (error "No previous same-level heading"))))))
1030 (defun outline-get-last-sibling ()
1031 "Move to previous heading of the same level, and return point.
1032 If there is no such heading, return nil."
1033 (let ((opoint (point))
1034 (level (funcall outline-level)))
1035 (outline-previous-visible-heading 1)
1036 (when (and (/= (point) opoint) (outline-on-heading-p))
1037 (while (and (> (funcall outline-level) level)
1039 (outline-previous-visible-heading 1))
1040 (if (< (funcall outline-level) level)
1044 (defun outline-headers-as-kill (beg end)
1045 "Save the visible outline headers in region at the start of the kill ring.
1047 Text shown between the headers isn't copied. Two newlines are
1048 inserted between saved headers. Yanking the result may be a
1049 convenient way to make a table of contents of the buffer."
1053 (narrow-to-region beg end)
1054 (goto-char (point-min))
1055 (let ((buffer (current-buffer))
1058 (with-current-buffer buffer
1059 ;; Boundary condition: starting on heading:
1060 (when (outline-on-heading-p)
1061 (outline-back-to-heading)
1063 end (progn (outline-end-of-heading)
1065 (insert-buffer-substring buffer start end)
1067 (let ((temp-buffer (current-buffer)))
1068 (with-current-buffer buffer
1069 (while (outline-next-heading)
1070 (unless (outline-invisible-p)
1072 end (progn (outline-end-of-heading) (point)))
1073 (with-current-buffer temp-buffer
1074 (insert-buffer-substring buffer start end)
1075 (insert "\n\n"))))))
1076 (kill-new (buffer-string)))))))
1081 ;;; outline.el ends here