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" 'outline-show-children
)
66 (define-key map
"\C-s" 'outline-show-subtree
)
67 (define-key map
"\C-d" 'outline-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" 'outline-hide-body
)
72 (define-key map
"\C-a" 'outline-show-all
)
73 (define-key map
"\C-c" 'outline-hide-entry
)
74 (define-key map
"\C-e" 'outline-show-entry
)
75 (define-key map
"\C-l" 'outline-hide-leaves
)
76 (define-key map
"\C-k" 'outline-show-branches
)
77 (define-key map
"\C-q" 'outline-hide-sublevels
)
78 (define-key map
"\C-o" 'outline-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)))
88 (define-key map
[hide] (cons "Hide" (make-sparse-keymap "Hide")))
89 (define-key map [hide outline-hide-other]
90 '(menu-item "Hide Other" outline-hide-other
91 :help "Hide everything except current body and parent and top-level headings"))
92 (define-key map [hide outline-hide-sublevels]
93 '(menu-item "Hide Sublevels" outline-hide-sublevels
94 :help "Hide everything but the top LEVELS levels of headers, in whole buffer"))
95 (define-key map [hide outline-hide-subtree]
96 '(menu-item "Hide Subtree" outline-hide-subtree
97 :help "Hide everything after this heading at deeper levels"))
98 (define-key map [hide outline-hide-entry]
99 '(menu-item "Hide Entry" outline-hide-entry
100 :help "Hide the body directly following this heading"))
101 (define-key map [hide outline-hide-body]
102 '(menu-item "Hide Body" outline-hide-body
103 :help "Hide all body lines in buffer, leaving all headings visible"))
104 (define-key map [hide outline-hide-leaves]
105 '(menu-item "Hide Leaves" outline-hide-leaves
106 :help "Hide the body after this heading and at deeper levels"))
107 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
108 (define-key map [show outline-show-subtree]
109 '(menu-item "Show Subtree" outline-show-subtree
110 :help "Show everything after this heading at deeper levels"))
111 (define-key map [show outline-show-children]
112 '(menu-item "Show Children" outline-show-children
113 :help "Show all direct subheadings of this heading"))
114 (define-key map [show outline-show-branches]
115 '(menu-item "Show Branches" outline-show-branches
116 :help "Show all subheadings of this heading, but not their bodies"))
117 (define-key map [show outline-show-entry]
118 '(menu-item "Show Entry" outline-show-entry
119 :help "Show the body directly following this heading"))
120 (define-key map [show outline-show-all]
121 '(menu-item "Show All" outline-show-all
122 :help "Show all of the text in the buffer"))
123 (define-key map [headings]
124 (cons "Headings" (make-sparse-keymap "Headings")))
125 (define-key map [headings demote-subtree]
126 '(menu-item "Demote Subtree" outline-demote
127 :help "Demote headings lower down the tree"))
128 (define-key map [headings promote-subtree]
129 '(menu-item "Promote Subtree" outline-promote
130 :help "Promote headings higher up the tree"))
131 (define-key map [headings move-subtree-down]
132 '(menu-item "Move Subtree Down" outline-move-subtree-down
133 :help "Move the current subtree down past arg headlines of the same level"))
134 (define-key map [headings move-subtree-up]
135 '(menu-item "Move Subtree Up" outline-move-subtree-up
136 :help "Move the current subtree up past arg headlines of the same level"))
137 (define-key map [headings copy]
138 '(menu-item "Copy to Kill Ring" outline-headers-as-kill
140 :help "Save the visible outline headers in region at the start of the kill ring"))
141 (define-key map [headings outline-insert-heading]
142 '(menu-item "New Heading" outline-insert-heading
143 :help "Insert a new heading at same depth at point"))
144 (define-key map [headings outline-backward-same-level]
145 '(menu-item "Previous Same Level" outline-backward-same-level
146 :help "Move backward to the arg'th subheading at same level as this one."))
147 (define-key map [headings outline-forward-same-level]
148 '(menu-item "Next Same Level" outline-forward-same-level
149 :help "Move forward to the arg'th subheading at same level as this one"))
150 (define-key map [headings outline-previous-visible-heading]
151 '(menu-item "Previous" outline-previous-visible-heading
152 :help "Move to the previous heading line"))
153 (define-key map [headings outline-next-visible-heading]
154 '(menu-item "Next" outline-next-visible-heading
155 :help "Move to the next visible heading line"))
156 (define-key map [headings outline-up-heading]
157 '(menu-item "Up" outline-up-heading
158 :help "Move to the visible heading line of which the present line is a subheading"))
161 (defvar outline-minor-mode-menu-bar-map
162 (let ((map (make-sparse-keymap)))
163 (define-key map [outline]
165 (nconc (make-sparse-keymap "Outline")
166 ;; Remove extra separator
168 ;; Flatten the major mode's menus into a single menu.
172 ;; Add a separator between each
173 ;; part of the unified menu.
174 (cons '(--- "---") (cdr x))))
175 outline-mode-menu-bar-map))))))
178 (defvar outline-mode-map
179 (let ((map (make-sparse-keymap)))
180 (define-key map "\C-c" outline-mode-prefix-map)
181 (define-key map [menu-bar] outline-mode-menu-bar-map)
184 (defvar outline-font-lock-keywords
186 ;; Highlight headings according to the level.
187 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
188 0 '(outline-font-lock-face) nil t)))
189 "Additional expressions to highlight in Outline mode.")
192 '((t :inherit font-lock-function-name-face))
197 '((t :inherit font-lock-variable-name-face))
202 '((t :inherit font-lock-keyword-face))
207 '((t :inherit font-lock-comment-face))
212 '((t :inherit font-lock-type-face))
217 '((t :inherit font-lock-constant-face))
222 '((t :inherit font-lock-builtin-face))
227 '((t :inherit font-lock-string-face))
231 (defvar outline-font-lock-faces
232 [outline-1 outline-2 outline-3 outline-4
233 outline-5 outline-6 outline-7 outline-8])
235 (defun outline-font-lock-face ()
236 "Return one of `outline-font-lock-faces' for current level."
238 (goto-char (match-beginning 0))
239 (looking-at outline-regexp)
240 (aref outline-font-lock-faces
241 (% (1- (funcall outline-level))
242 (length outline-font-lock-faces)))))
244 (defvar outline-view-change-hook nil
245 "Normal hook to be run after outline visibility changes.")
247 (defvar outline-mode-hook nil
248 "This hook is run when outline mode starts.")
250 (defvar outline-blank-line nil
251 "Non-nil means to leave unhidden blank line before heading.")
254 (define-derived-mode outline-mode text-mode "Outline"
255 "Set major mode for editing outlines with selective display.
256 Headings are lines which start with asterisks: one for major headings,
257 two for subheadings, etc. Lines not starting with asterisks are body lines.
259 Body text or subheadings under a heading can be made temporarily
260 invisible, or visible again. Invisible lines are attached to the end
261 of the heading, so they move with it, if the line is killed and yanked
262 back. A heading with text hidden under it is marked with an ellipsis (...).
265 The commands `outline-hide-subtree', `outline-show-subtree',
266 `outline-show-children', `outline-hide-entry',
267 `outline-show-entry', `outline-hide-leaves', and `outline-show-branches'
268 are used when point is on a heading line.
270 The variable `outline-regexp' can be changed to control what is a heading.
271 A line is a heading if `outline-regexp' matches something at the
272 beginning of the line. The longer the match, the deeper the level.
274 Turning on outline mode calls the value of `text-mode-hook' and then of
275 `outline-mode-hook', if they are non-nil."
276 (make-local-variable 'line-move-ignore-invisible)
277 (setq line-move-ignore-invisible t)
278 ;; Cause use of ellipses for invisible text.
279 (add-to-invisibility-spec '(outline . t))
280 (set (make-local-variable 'paragraph-start)
281 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
282 ;; Inhibit auto-filling of header lines.
283 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
284 (set (make-local-variable 'paragraph-separate)
285 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
286 (set (make-local-variable 'font-lock-defaults)
287 '(outline-font-lock-keywords t nil nil backward-paragraph))
288 (setq imenu-generic-expression
289 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
290 (add-hook 'change-major-mode-hook 'outline-show-all nil t))
292 (defcustom outline-minor-mode-prefix "\C-c@"
293 "Prefix key to use for Outline commands in Outline minor mode.
294 The value of this variable is checked as part of loading Outline mode.
295 After that, changing the prefix key requires manipulating keymaps."
300 (define-minor-mode outline-minor-mode
301 "Toggle Outline minor mode.
302 With a prefix argument ARG, enable Outline minor mode if ARG is
303 positive, and disable it otherwise. If called from Lisp, enable
304 the mode if ARG is omitted or nil.
306 See the command `outline-mode' for more information on this mode."
307 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
308 (cons outline-minor-mode-prefix outline-mode-prefix-map))
310 (if outline-minor-mode
312 ;; Turn off this mode if we change major modes.
313 (add-hook 'change-major-mode-hook
314 (lambda () (outline-minor-mode -1))
316 (set (make-local-variable 'line-move-ignore-invisible) t)
317 ;; Cause use of ellipses for invisible text.
318 (add-to-invisibility-spec '(outline . t)))
319 (setq line-move-ignore-invisible nil)
320 ;; Cause use of ellipses for invisible text.
321 (remove-from-invisibility-spec '(outline . t))
322 ;; When turning off outline mode, get rid of any outline hiding.
325 (defvar outline-level 'outline-level
326 "Function of no args to compute a header's nesting level in an outline.
327 It can assume point is at the beginning of a header line and that the match
328 data reflects the `outline-regexp'.")
329 ;;;###autoload(put 'outline-level 'risky-local-variable t)
331 (defvar outline-heading-alist ()
332 "Alist associating a heading for every possible level.
333 Each entry is of the form (HEADING . LEVEL).
334 This alist is used two ways: to find the heading corresponding to
335 a given level and to find the level of a given heading.
336 If a mode or document needs several sets of outline headings (for example
337 numbered and unnumbered sections), list them set by set and sorted by level
338 within each set. For example in texinfo mode:
340 (setq outline-heading-alist
341 \\='((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
342 (\"@subsubsection\" . 5)
343 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
344 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
345 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
346 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
348 Instead of sorting the entries in each set, you can also separate the
350 (make-variable-buffer-local 'outline-heading-alist)
352 ;; This used to count columns rather than characters, but that made ^L
353 ;; appear to be at level 2 instead of 1. Columns would be better for
354 ;; tab handling, but the default regexp doesn't use tabs, and anyone
355 ;; who changes the regexp can also redefine the outline-level variable
357 (defun outline-level ()
358 "Return the depth to which a statement is nested in the outline.
359 Point must be at the beginning of a header line.
360 This is actually either the level specified in `outline-heading-alist'
361 or else the number of characters matched by `outline-regexp'."
362 (or (cdr (assoc (match-string 0) outline-heading-alist))
363 (- (match-end 0) (match-beginning 0))))
365 (defun outline-next-preface ()
366 "Skip forward to just before the next heading line.
367 If there's no following heading line, stop before the newline
368 at the end of the buffer."
369 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
371 (goto-char (match-beginning 0)))
372 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
375 (defun outline-next-heading ()
376 "Move to the next (possibly invisible) heading line."
378 ;; Make sure we don't match the heading we're at.
379 (if (and (bolp) (not (eobp))) (forward-char 1))
380 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
382 (goto-char (match-beginning 0))))
384 (defun outline-previous-heading ()
385 "Move to the previous (possibly invisible) heading line."
387 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
390 (defsubst outline-invisible-p (&optional pos)
391 "Non-nil if the character after POS is invisible.
392 If POS is nil, use `point' instead."
393 (get-char-property (or pos (point)) 'invisible))
395 (defun outline-back-to-heading (&optional invisible-ok)
396 "Move to previous heading line, or beg of this line if it's a heading.
397 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
399 (or (outline-on-heading-p invisible-ok)
403 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
405 (error "Before first heading"))
406 (setq found (and (or invisible-ok (not (outline-invisible-p)))
411 (defun outline-on-heading-p (&optional invisible-ok)
412 "Return t if point is on a (visible) heading line.
413 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
416 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
417 (looking-at outline-regexp))))
419 (defun outline-insert-heading ()
420 "Insert a new heading at same depth at point."
422 (let ((head (save-excursion
424 (outline-back-to-heading)
425 (error (outline-next-heading)))
427 (or (caar outline-heading-alist) "")
429 (unless (or (string-match "[ \t]\\'" head)
430 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
432 (setq head (concat head " ")))
433 (unless (bolp) (end-of-line) (newline))
436 (save-excursion (newline-and-indent)))
437 (run-hooks 'outline-insert-heading-hook)))
439 (defun outline-invent-heading (head up)
440 "Create a heading by using heading HEAD as a template.
441 When UP is non-nil, the created heading will be one level above.
442 Otherwise, it will be one level below."
444 ;; Let's try to invent one by repeating or deleting the last char.
445 (let ((new-head (if up (substring head 0 -1)
446 (concat head (substring head -1)))))
447 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
449 ;; Why bother checking that it is indeed higher/lower level ?
451 ;; Didn't work, so ask what to do.
452 (read-string (format-message "%s heading for `%s': "
453 (if up "Parent" "Demoted") head)
456 (defun outline-promote (&optional which)
457 "Promote headings higher up the tree.
458 If `transient-mark-mode' is on, and mark is active, promote headings in
459 the region (from a Lisp program, pass `region' for WHICH). Otherwise:
460 without prefix argument, promote current heading and all headings in the
461 subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
462 argument, promote just the current heading (from a Lisp program, pass
463 nil for WHICH, or do not pass any argument)."
465 (list (if (and transient-mark-mode mark-active) 'region
466 (outline-back-to-heading)
467 (if current-prefix-arg nil 'subtree))))
470 (outline-map-region 'outline-promote (region-beginning) (region-end)))
472 (outline-map-region 'outline-promote
474 (save-excursion (outline-get-next-sibling) (point))))
476 (outline-back-to-heading t)
477 (let* ((head (match-string-no-properties 0))
478 (level (save-match-data (funcall outline-level)))
479 (up-head (or (outline-head-from-level (1- level) head)
480 ;; Use the parent heading, if it is really
484 (outline-up-heading 1 t)
485 (and (= (1- level) (funcall outline-level))
486 (match-string-no-properties 0))))
487 ;; Bummer!! There is no lower level heading.
488 (outline-invent-heading head 'up))))
490 (unless (rassoc level outline-heading-alist)
491 (push (cons head level) outline-heading-alist))
493 (replace-match up-head nil t)))))
495 (defun outline-demote (&optional which)
496 "Demote headings lower down the tree.
497 If `transient-mark-mode' is on, and mark is active, demote headings in
498 the region (from a Lisp program, pass `region' for WHICH). Otherwise:
499 without prefix argument, demote current heading and all headings in the
500 subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
501 argument, demote just the current heading (from a Lisp program, pass
502 nil for WHICH, or do not pass any argument)."
504 (list (if (and transient-mark-mode mark-active) 'region
505 (outline-back-to-heading)
506 (if current-prefix-arg nil 'subtree))))
509 (outline-map-region 'outline-demote (region-beginning) (region-end)))
511 (outline-map-region 'outline-demote
513 (save-excursion (outline-get-next-sibling) (point))))
515 (let* ((head (match-string-no-properties 0))
516 (level (save-match-data (funcall outline-level)))
518 (or (outline-head-from-level (1+ level) head)
521 (while (and (progn (outline-next-heading) (not (eobp)))
522 (<= (funcall outline-level) level)))
524 ;; Try again from the beginning of the buffer.
525 (goto-char (point-min))
526 (while (and (progn (outline-next-heading) (not (eobp)))
527 (<= (funcall outline-level) level))))
529 (looking-at outline-regexp)
530 (match-string-no-properties 0))))
531 ;; Bummer!! There is no higher-level heading in the buffer.
532 (outline-invent-heading head nil))))
534 (unless (rassoc level outline-heading-alist)
535 (push (cons head level) outline-heading-alist))
536 (replace-match down-head nil t)))))
538 (defun outline-head-from-level (level head &optional alist)
539 "Get new heading with level LEVEL, closest to HEAD, from ALIST.
540 If there are no such entries, return nil.
541 ALIST defaults to `outline-heading-alist'.
542 Similar to (car (rassoc LEVEL ALIST)).
543 If there are several different entries with same new level, choose
544 the one with the smallest distance to the association of HEAD in the alist.
545 This makes it possible for promotion to work in modes with several
546 independent sets of headings (numbered, unnumbered, appendix...)"
547 (unless alist (setq alist outline-heading-alist))
548 (let ((l (rassoc level alist))
552 ;; If there's no HEAD after L, any other entry for LEVEL after L
553 ;; can't be much better than L.
554 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
555 ;; If there's no other entry for LEVEL, just keep L.
556 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
557 ;; Now we have L, L2, and H: see if L2 seems better than L.
558 ;; If H is after L2, L2 is better.
559 ((memq h (setq l2l (memq l2 (cdr ll))))
560 (outline-head-from-level level head l2l))
561 ;; Now we have H between L and L2.
562 ;; If there's a separator between L and H, prefer L2.
563 ((memq h (memq nil ll))
564 (outline-head-from-level level head l2l))
565 ;; If there's a separator between L2 and H, prefer L.
566 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
567 ;; No separator between L and L2, check the distance.
568 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
569 (outline-head-from-level level head l2l))
570 ;; If all else fails, just keep L.
573 (defun outline-map-region (fun beg end)
574 "Call FUN for every heading between BEG and END.
575 When FUN is called, point is at the beginning of the heading and
576 the match data is set appropriately."
578 (setq end (copy-marker end))
580 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
581 (goto-char (match-beginning 0))
584 (outline-next-heading)
589 ;; Vertical tree motion
591 (defun outline-move-subtree-up (&optional arg)
592 "Move the current subtree up past ARG headlines of the same level."
594 (outline-move-subtree-down (- arg)))
596 (defun outline-move-subtree-down (&optional arg)
597 "Move the current subtree down past ARG headlines of the same level."
599 (outline-back-to-heading)
600 (let* ((movfunc (if (> arg 0) 'outline-get-next-sibling
601 'outline-get-last-sibling))
602 ;; Find the end of the subtree to be moved as well as the point to
603 ;; move it to, adding a newline if necessary, to ensure these points
604 ;; are at bol on the line below the subtree.
605 (end-point-func (lambda ()
606 (outline-end-of-subtree)
607 (if (eq (char-after) ?\n) (forward-char 1)
608 (if (and (eobp) (not (bolp))) (insert "\n")))
611 (folded (save-match-data
612 (outline-end-of-heading)
613 (outline-invisible-p)))
614 (end (save-match-data
615 (funcall end-point-func)))
616 (ins-point (make-marker))
618 ;; Find insertion point, with error handling.
621 (or (funcall movfunc)
622 (progn (goto-char beg)
623 (user-error "Cannot move past superior level")))
626 ;; Moving forward - still need to move over subtree.
627 (funcall end-point-func))
628 (move-marker ins-point (point))
629 (insert (delete-and-extract-region beg end))
630 (goto-char ins-point)
631 (if folded (outline-hide-subtree))
632 (move-marker ins-point nil)))
634 (defun outline-end-of-heading ()
635 "Move to one char before the next `outline-heading-end-regexp'."
636 (if (re-search-forward outline-heading-end-regexp nil 'move)
639 (defun outline-next-visible-heading (arg)
640 "Move to the next visible heading line.
641 With ARG, repeats or can move backward if negative.
642 A heading line is one that starts with a `*' (or that
643 `outline-regexp' matches)."
648 (let (found-heading-p)
649 (while (and (not (bobp)) (< arg 0))
650 (while (and (not (bobp))
651 (setq found-heading-p
653 (concat "^\\(?:" outline-regexp "\\)")
655 (outline-invisible-p)))
657 (while (and (not (eobp)) (> arg 0))
658 (while (and (not (eobp))
659 (setq found-heading-p
661 (concat "^\\(?:" outline-regexp "\\)")
663 (outline-invisible-p (match-beginning 0))))
665 (if found-heading-p (beginning-of-line))))
667 (defun outline-previous-visible-heading (arg)
668 "Move to the previous heading line.
669 With ARG, repeats or can move forward if negative.
670 A heading line is one that starts with a `*' (or that
671 `outline-regexp' matches)."
673 (outline-next-visible-heading (- arg)))
675 (defun outline-mark-subtree ()
676 "Mark the current subtree in an outlined document.
677 This puts point at the start of the current subtree, and mark at the end."
680 (if (outline-on-heading-p)
681 ;; we are already looking at a heading
683 ;; else go back to previous heading
684 (outline-previous-visible-heading 1))
686 (outline-end-of-subtree)
687 (push-mark (point) nil t)
691 (defvar outline-isearch-open-invisible-function nil
692 "Function called if `isearch' finishes in an invisible overlay.
693 The function is called with the overlay as its only argument.
694 If nil, `outline-show-entry' is called to reveal the invisible text.")
696 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
697 (defun outline-flag-region (from to flag)
698 "Hide or show lines from FROM to TO, according to FLAG.
699 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
700 (remove-overlays from to 'invisible 'outline)
702 ;; We use `front-advance' here because the invisible text begins at the
703 ;; very end of the heading, before the newline, so text inserted at FROM
704 ;; belongs to the heading rather than to the entry.
705 (let ((o (make-overlay from to nil 'front-advance)))
706 (overlay-put o 'evaporate t)
707 (overlay-put o 'invisible 'outline)
708 (overlay-put o 'isearch-open-invisible
709 (or outline-isearch-open-invisible-function
710 'outline-isearch-open-invisible))))
711 ;; Seems only used by lazy-lock. I.e. obsolete.
712 (run-hooks 'outline-view-change-hook))
714 (defun outline-reveal-toggle-invisible (o hidep)
716 (goto-char (overlay-start o))
718 ;; When hiding the area again, we could just clean it up and let
719 ;; reveal do the rest, by simply doing:
720 ;; (remove-overlays (overlay-start o) (overlay-end o)
721 ;; 'invisible 'outline)
723 ;; That works fine as long as everything is in sync, but if the
724 ;; structure of the document is changed while revealing parts of it,
725 ;; the resulting behavior can be ugly. I.e. we need to make
726 ;; sure that we hide exactly a subtree.
728 (let ((end (overlay-end o)))
731 (outline-hide-subtree)
732 (outline-next-visible-heading 1)
733 (and (not (eobp)) (< (point) end))))))
735 ;; When revealing, we just need to reveal sublevels. If point is
736 ;; inside one of the sublevels, reveal will call us again.
737 ;; But we need to preserve the original overlay.
738 (let ((o1 (copy-overlay o)))
739 (overlay-put o 'invisible nil) ;Show (most of) the text.
742 (outline-show-children)
743 ;; Normally just the above is needed.
744 ;; But in odd cases, the above might fail to show anything.
745 ;; To avoid an infinite loop, we have to make sure that
746 ;; *something* gets shown.
747 (and (equal (overlay-start o) (overlay-start o1))
748 (< (point) (overlay-end o))
749 (= 0 (forward-line 1)))))
750 ;; If still nothing was shown, just kill the damn thing.
751 (when (equal (overlay-start o) (overlay-start o1))
752 ;; I've seen it happen at the end of buffer.
753 (delete-overlay o1))))))
755 ;; Function to be set as an outline-isearch-open-invisible' property
756 ;; to the overlay that makes the outline invisible (see
757 ;; `outline-flag-region').
758 (defun outline-isearch-open-invisible (_overlay)
759 ;; We rely on the fact that isearch places point on the matched text.
760 (outline-show-entry))
762 (defun outline-hide-entry ()
763 "Hide the body directly following this heading."
766 (outline-back-to-heading)
767 (outline-end-of-heading)
768 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
770 (define-obsolete-function-alias
771 'hide-entry 'outline-hide-entry "25.1")
773 (defun outline-show-entry ()
774 "Show the body directly following this heading.
775 Show the heading too, if it is currently invisible."
778 (outline-back-to-heading t)
779 (outline-flag-region (1- (point))
781 (outline-next-preface)
782 (if (= 1 (- (point-max) (point)))
787 (define-obsolete-function-alias
788 'show-entry 'outline-show-entry "25.1")
790 (defun outline-hide-body ()
791 "Hide all body lines in buffer, leaving all headings visible."
793 (outline-hide-region-body (point-min) (point-max)))
795 (define-obsolete-function-alias
796 'hide-body 'outline-hide-body "25.1")
798 (defun outline-hide-region-body (start end)
799 "Hide all body lines between START and END, but not headings."
800 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
801 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
802 ;; and run the hook finally.
803 (let (outline-view-change-hook)
806 (narrow-to-region start end)
807 (goto-char (point-min))
808 (if (outline-on-heading-p)
809 (outline-end-of-heading)
810 (outline-next-preface))
812 (outline-flag-region (point)
813 (progn (outline-next-preface) (point)) t)
815 (forward-char (if (looking-at "\n\n") 2 1))
816 (outline-end-of-heading))))))
817 (run-hooks 'outline-view-change-hook))
819 (define-obsolete-function-alias
820 'hide-region-body 'outline-hide-region-body "25.1")
822 (defun outline-show-all ()
823 "Show all of the text in the buffer."
825 (outline-flag-region (point-min) (point-max) nil))
827 (define-obsolete-function-alias
828 'show-all 'outline-show-all "25.1")
830 (defun outline-hide-subtree ()
831 "Hide everything after this heading at deeper levels."
833 (outline-flag-subtree t))
835 (define-obsolete-function-alias
836 'hide-subtree 'outline-hide-subtree "25.1")
838 (defun outline-hide-leaves ()
839 "Hide the body after this heading and at deeper levels."
842 (outline-back-to-heading)
843 ;; Turned off to fix bug reported by Otto Maddox on 22 Nov 2005.
844 ;; (outline-end-of-heading)
845 (outline-hide-region-body
847 (progn (outline-end-of-subtree) (point)))))
849 (define-obsolete-function-alias
850 'hide-leaves 'outline-hide-leaves "25.1")
852 (defun outline-show-subtree ()
853 "Show everything after this heading at deeper levels."
855 (outline-flag-subtree nil))
857 (define-obsolete-function-alias
858 'show-subtree 'outline-show-subtree "25.1")
860 (defun outline-show-heading ()
861 "Show the current heading and move to its end."
862 (outline-flag-region (- (point)
864 (if (and outline-blank-line
865 (eq (char-before (1- (point))) ?\n))
867 (progn (outline-end-of-heading) (point))
870 (defun outline-hide-sublevels (levels)
871 "Hide everything but the top LEVELS levels of headers, in whole buffer."
874 (current-prefix-arg (prefix-numeric-value current-prefix-arg))
875 ((save-excursion (beginning-of-line)
876 (looking-at outline-regexp))
877 (funcall outline-level))
880 (error "Must keep at least one level of headers"))
882 (let* (outline-view-change-hook
884 (goto-char (point-min))
885 ;; Skip the prelude, if any.
886 (unless (outline-on-heading-p t) (outline-next-heading))
889 (goto-char (point-max))
890 ;; Keep empty last line, if available.
891 (if (bolp) (1- (point)) (point)))))
893 (setq beg (prog1 end (setq end beg))))
894 ;; First hide everything.
895 (outline-flag-region beg end t)
896 ;; Then unhide the top level headers.
899 (if (<= (funcall outline-level) levels)
900 (outline-show-heading)))
902 ;; Finally unhide any trailing newline.
903 (goto-char (point-max))
904 (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
905 (outline-flag-region (1- (point)) (point) nil))))
906 (run-hooks 'outline-view-change-hook))
908 (define-obsolete-function-alias
909 'hide-sublevels 'outline-hide-sublevels "25.1")
911 (defun outline-hide-other ()
912 "Hide everything except current body and parent and top-level headings."
914 (outline-hide-sublevels 1)
915 (let (outline-view-change-hook)
917 (outline-back-to-heading t)
919 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
921 (outline-flag-region (1- (point))
922 (save-excursion (forward-line 1) (point))
924 (run-hooks 'outline-view-change-hook))
926 (define-obsolete-function-alias
927 'hide-other 'outline-hide-other "25.1")
929 (defun outline-toggle-children ()
930 "Show or hide the current subtree depending on its current state."
933 (outline-back-to-heading)
934 (if (not (outline-invisible-p (line-end-position)))
935 (outline-hide-subtree)
936 (outline-show-children)
937 (outline-show-entry))))
939 (defun outline-flag-subtree (flag)
940 "Assign FLAG to the current subtree."
942 (outline-back-to-heading)
943 (outline-end-of-heading)
944 (outline-flag-region (point)
945 (progn (outline-end-of-subtree) (point))
948 (defun outline-end-of-subtree ()
949 "Move to the end of the current subtree."
950 (outline-back-to-heading)
952 (level (funcall outline-level)))
953 (while (and (not (eobp))
954 (or first (> (funcall outline-level) level)))
956 (outline-next-heading))
957 (if (and (bolp) (not (eolp)))
958 ;; We stopped at a nonempty line (the next heading).
960 ;; Go to end of line before heading
962 (if (and outline-blank-line (bolp))
963 ;; leave blank line before heading
964 (forward-char -1))))))
966 (defun outline-show-branches ()
967 "Show all subheadings of this heading, but not their bodies."
969 (outline-show-children 1000))
971 (define-obsolete-function-alias
972 'show-branches 'outline-show-branches "25.1")
974 (defun outline-show-children (&optional level)
975 "Show all direct subheadings of this heading.
976 Prefix arg LEVEL is how many levels below the current level should be shown.
977 Default is enough to cause the following heading to appear."
980 (if level (prefix-numeric-value level)
982 (outline-back-to-heading)
983 (let ((start-level (funcall outline-level)))
984 (outline-next-heading)
987 (max 1 (- (funcall outline-level) start-level)))))))
988 (let (outline-view-change-hook)
990 (outline-back-to-heading)
991 (setq level (+ level (funcall outline-level)))
994 (if (<= (funcall outline-level) level)
995 (outline-show-heading)))
997 (progn (outline-end-of-subtree)
998 (if (eobp) (point-max) (1+ (point)))))))
999 (run-hooks 'outline-view-change-hook))
1001 (define-obsolete-function-alias
1002 'show-children 'outline-show-children "25.1")
1006 (defun outline-up-heading (arg &optional invisible-ok)
1007 "Move to the visible heading line of which the present line is a subheading.
1008 With argument, move up ARG levels.
1009 If INVISIBLE-OK is non-nil, also consider invisible lines."
1011 (and (eq this-command 'outline-up-heading)
1012 (or (eq last-command 'outline-up-heading) (push-mark)))
1013 (outline-back-to-heading invisible-ok)
1014 (let ((start-level (funcall outline-level)))
1015 (when (<= start-level 1)
1016 (error "Already at top level of the outline"))
1017 (while (and (> start-level 1) (> arg 0) (not (bobp)))
1018 (let ((level start-level))
1019 (while (not (or (< level start-level) (bobp)))
1021 (outline-previous-heading)
1022 (outline-previous-visible-heading 1))
1023 (setq level (funcall outline-level)))
1024 (setq start-level level))
1025 (setq arg (- arg 1))))
1026 (looking-at outline-regexp))
1028 (defun outline-forward-same-level (arg)
1029 "Move forward to the ARG'th subheading at same level as this one.
1030 Stop at the first and last subheadings of a superior heading."
1032 (outline-back-to-heading)
1034 (let ((point-to-move-to (save-excursion
1035 (outline-get-next-sibling))))
1036 (if point-to-move-to
1038 (goto-char point-to-move-to)
1039 (setq arg (1- arg)))
1042 (error "No following same-level heading"))))))
1044 (defun outline-get-next-sibling ()
1045 "Move to next heading of the same level, and return point.
1046 If there is no such heading, return nil."
1047 (let ((level (funcall outline-level)))
1048 (outline-next-visible-heading 1)
1049 (while (and (not (eobp)) (> (funcall outline-level) level))
1050 (outline-next-visible-heading 1))
1051 (if (or (eobp) (< (funcall outline-level) level))
1055 (defun outline-backward-same-level (arg)
1056 "Move backward to the ARG'th subheading at same level as this one.
1057 Stop at the first and last subheadings of a superior heading."
1059 (outline-back-to-heading)
1061 (let ((point-to-move-to (save-excursion
1062 (outline-get-last-sibling))))
1063 (if point-to-move-to
1065 (goto-char point-to-move-to)
1066 (setq arg (1- arg)))
1069 (error "No previous same-level heading"))))))
1071 (defun outline-get-last-sibling ()
1072 "Move to previous heading of the same level, and return point.
1073 If there is no such heading, return nil."
1074 (let ((opoint (point))
1075 (level (funcall outline-level)))
1076 (outline-previous-visible-heading 1)
1077 (when (and (/= (point) opoint) (outline-on-heading-p))
1078 (while (and (> (funcall outline-level) level)
1080 (outline-previous-visible-heading 1))
1081 (if (< (funcall outline-level) level)
1085 (defun outline-headers-as-kill (beg end)
1086 "Save the visible outline headers between BEG and END to the kill ring.
1088 Text shown between the headers isn't copied. Two newlines are
1089 inserted between saved headers. Yanking the result may be a
1090 convenient way to make a table of contents of the buffer."
1094 (narrow-to-region beg end)
1095 (goto-char (point-min))
1096 (let ((buffer (current-buffer))
1099 (with-current-buffer buffer
1100 ;; Boundary condition: starting on heading:
1101 (when (outline-on-heading-p)
1102 (outline-back-to-heading)
1104 end (progn (outline-end-of-heading)
1106 (insert-buffer-substring buffer start end)
1108 (let ((temp-buffer (current-buffer)))
1109 (with-current-buffer buffer
1110 (while (outline-next-heading)
1111 (unless (outline-invisible-p)
1113 end (progn (outline-end-of-heading) (point)))
1114 (with-current-buffer temp-buffer
1115 (insert-buffer-substring buffer start end)
1116 (insert "\n\n"))))))
1117 (kill-new (buffer-string)))))))
1122 ;;; outline.el ends here