1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 1993-1995, 1997, 2000-2018 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 <https://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 has outline invisible property.
392 If POS is nil, use `point' instead."
393 (eq (get-char-property (or pos (point)) 'invisible) 'outline))
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.
792 Note that this does not hide the lines preceding the first heading line."
794 (outline-hide-region-body (point-min) (point-max)))
796 (define-obsolete-function-alias
797 'hide-body 'outline-hide-body "25.1")
799 (defun outline-hide-region-body (start end)
800 "Hide all body lines between START and END, but not headings."
801 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
802 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
803 ;; and run the hook finally.
804 (let (outline-view-change-hook)
807 (narrow-to-region start end)
808 (goto-char (point-min))
809 (if (outline-on-heading-p)
810 (outline-end-of-heading)
811 (outline-next-preface))
813 (outline-flag-region (point)
814 (progn (outline-next-preface) (point)) t)
816 (forward-char (if (looking-at "\n\n") 2 1))
817 (outline-end-of-heading))))))
818 (run-hooks 'outline-view-change-hook))
820 (define-obsolete-function-alias
821 'hide-region-body 'outline-hide-region-body "25.1")
823 (defun outline-show-all ()
824 "Show all of the text in the buffer."
826 (outline-flag-region (point-min) (point-max) nil))
828 (define-obsolete-function-alias
829 'show-all 'outline-show-all "25.1")
831 (defun outline-hide-subtree ()
832 "Hide everything after this heading at deeper levels."
834 (outline-flag-subtree t))
836 (define-obsolete-function-alias
837 'hide-subtree 'outline-hide-subtree "25.1")
839 (defun outline-hide-leaves ()
840 "Hide the body after this heading and at deeper levels."
843 (outline-back-to-heading)
844 ;; Turned off to fix bug reported by Otto Maddox on 22 Nov 2005.
845 ;; (outline-end-of-heading)
846 (outline-hide-region-body
848 (progn (outline-end-of-subtree) (point)))))
850 (define-obsolete-function-alias
851 'hide-leaves 'outline-hide-leaves "25.1")
853 (defun outline-show-subtree ()
854 "Show everything after this heading at deeper levels."
856 (outline-flag-subtree nil))
858 (define-obsolete-function-alias
859 'show-subtree 'outline-show-subtree "25.1")
861 (defun outline-show-heading ()
862 "Show the current heading and move to its end."
863 (outline-flag-region (- (point)
865 (if (and outline-blank-line
866 (eq (char-before (1- (point))) ?\n))
868 (progn (outline-end-of-heading) (point))
871 (defun outline-hide-sublevels (levels)
872 "Hide everything but the top LEVELS levels of headers, in whole buffer.
873 This also unhides the top heading-less body, if any.
875 Interactively, the prefix argument supplies the value of LEVELS.
876 When invoked without a prefix argument, LEVELS defaults to the level
877 of the current heading, or to 1 if the current line is not a heading."
880 (current-prefix-arg (prefix-numeric-value current-prefix-arg))
881 ((save-excursion (beginning-of-line)
882 (looking-at outline-regexp))
883 (funcall outline-level))
886 (error "Must keep at least one level of headers"))
888 (let* (outline-view-change-hook
890 (goto-char (point-min))
891 ;; Skip the prelude, if any.
892 (unless (outline-on-heading-p t) (outline-next-heading))
895 (goto-char (point-max))
896 ;; Keep empty last line, if available.
897 (if (bolp) (1- (point)) (point)))))
899 (setq beg (prog1 end (setq end beg))))
900 ;; First hide everything.
901 (outline-flag-region beg end t)
902 ;; Then unhide the top level headers.
905 (if (<= (funcall outline-level) levels)
906 (outline-show-heading)))
908 ;; Finally unhide any trailing newline.
909 (goto-char (point-max))
910 (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
911 (outline-flag-region (1- (point)) (point) nil))))
912 (run-hooks 'outline-view-change-hook))
914 (define-obsolete-function-alias
915 'hide-sublevels 'outline-hide-sublevels "25.1")
917 (defun outline-hide-other ()
918 "Hide everything except current body and parent and top-level headings.
919 This also unhides the top heading-less body, if any."
921 (outline-hide-sublevels 1)
922 (let (outline-view-change-hook)
924 (outline-back-to-heading t)
926 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
928 (outline-flag-region (1- (point))
929 (save-excursion (forward-line 1) (point))
931 (run-hooks 'outline-view-change-hook))
933 (define-obsolete-function-alias
934 'hide-other 'outline-hide-other "25.1")
936 (defun outline-toggle-children ()
937 "Show or hide the current subtree depending on its current state."
940 (outline-back-to-heading)
941 (if (not (outline-invisible-p (line-end-position)))
942 (outline-hide-subtree)
943 (outline-show-children)
944 (outline-show-entry))))
946 (defun outline-flag-subtree (flag)
947 "Assign FLAG to the current subtree."
949 (outline-back-to-heading)
950 (outline-end-of-heading)
951 (outline-flag-region (point)
952 (progn (outline-end-of-subtree) (point))
955 (defun outline-end-of-subtree ()
956 "Move to the end of the current subtree."
957 (outline-back-to-heading)
959 (level (funcall outline-level)))
960 (while (and (not (eobp))
961 (or first (> (funcall outline-level) level)))
963 (outline-next-heading))
964 (if (and (bolp) (not (eolp)))
965 ;; We stopped at a nonempty line (the next heading).
967 ;; Go to end of line before heading
969 (if (and outline-blank-line (bolp))
970 ;; leave blank line before heading
971 (forward-char -1))))))
973 (defun outline-show-branches ()
974 "Show all subheadings of this heading, but not their bodies."
976 (outline-show-children 1000))
978 (define-obsolete-function-alias
979 'show-branches 'outline-show-branches "25.1")
981 (defun outline-show-children (&optional level)
982 "Show all direct subheadings of this heading.
983 Prefix arg LEVEL is how many levels below the current level should be shown.
984 Default is enough to cause the following heading to appear."
987 (if level (prefix-numeric-value level)
989 (outline-back-to-heading)
990 (let ((start-level (funcall outline-level)))
991 (outline-next-heading)
994 (max 1 (- (funcall outline-level) start-level)))))))
995 (let (outline-view-change-hook)
997 (outline-back-to-heading)
998 (setq level (+ level (funcall outline-level)))
1001 (if (<= (funcall outline-level) level)
1002 (outline-show-heading)))
1004 (progn (outline-end-of-subtree)
1005 (if (eobp) (point-max) (1+ (point)))))))
1006 (run-hooks 'outline-view-change-hook))
1008 (define-obsolete-function-alias
1009 'show-children 'outline-show-children "25.1")
1013 (defun outline-up-heading (arg &optional invisible-ok)
1014 "Move to the visible heading line of which the present line is a subheading.
1015 With argument, move up ARG levels.
1016 If INVISIBLE-OK is non-nil, also consider invisible lines."
1018 (and (eq this-command 'outline-up-heading)
1019 (or (eq last-command 'outline-up-heading) (push-mark)))
1020 (outline-back-to-heading invisible-ok)
1021 (let ((start-level (funcall outline-level)))
1022 (when (<= start-level 1)
1023 (error "Already at top level of the outline"))
1024 (while (and (> start-level 1) (> arg 0) (not (bobp)))
1025 (let ((level start-level))
1026 (while (not (or (< level start-level) (bobp)))
1028 (outline-previous-heading)
1029 (outline-previous-visible-heading 1))
1030 (setq level (funcall outline-level)))
1031 (setq start-level level))
1032 (setq arg (- arg 1))))
1033 (looking-at outline-regexp))
1035 (defun outline-forward-same-level (arg)
1036 "Move forward to the ARG'th subheading at same level as this one.
1037 Stop at the first and last subheadings of a superior heading."
1039 (outline-back-to-heading)
1041 (let ((point-to-move-to (save-excursion
1042 (outline-get-next-sibling))))
1043 (if point-to-move-to
1045 (goto-char point-to-move-to)
1046 (setq arg (1- arg)))
1049 (error "No following same-level heading"))))))
1051 (defun outline-get-next-sibling ()
1052 "Move to next heading of the same level, and return point.
1053 If there is no such heading, return nil."
1054 (let ((level (funcall outline-level)))
1055 (outline-next-visible-heading 1)
1056 (while (and (not (eobp)) (> (funcall outline-level) level))
1057 (outline-next-visible-heading 1))
1058 (if (or (eobp) (< (funcall outline-level) level))
1062 (defun outline-backward-same-level (arg)
1063 "Move backward to the ARG'th subheading at same level as this one.
1064 Stop at the first and last subheadings of a superior heading."
1066 (outline-back-to-heading)
1068 (let ((point-to-move-to (save-excursion
1069 (outline-get-last-sibling))))
1070 (if point-to-move-to
1072 (goto-char point-to-move-to)
1073 (setq arg (1- arg)))
1076 (error "No previous same-level heading"))))))
1078 (defun outline-get-last-sibling ()
1079 "Move to previous heading of the same level, and return point.
1080 If there is no such heading, return nil."
1081 (let ((opoint (point))
1082 (level (funcall outline-level)))
1083 (outline-previous-visible-heading 1)
1084 (when (and (/= (point) opoint) (outline-on-heading-p))
1085 (while (and (> (funcall outline-level) level)
1087 (outline-previous-visible-heading 1))
1088 (if (< (funcall outline-level) level)
1092 (defun outline-headers-as-kill (beg end)
1093 "Save the visible outline headers between BEG and END to the kill ring.
1095 Text shown between the headers isn't copied. Two newlines are
1096 inserted between saved headers. Yanking the result may be a
1097 convenient way to make a table of contents of the buffer."
1101 (narrow-to-region beg end)
1102 (goto-char (point-min))
1103 (let ((buffer (current-buffer)) start end)
1105 (let ((temp-buffer (current-buffer)))
1106 (with-current-buffer buffer
1107 ;; Boundary condition: starting on heading:
1108 (when (outline-on-heading-p)
1109 (outline-back-to-heading)
1111 end (progn (outline-end-of-heading) (point)))
1112 (with-current-buffer temp-buffer
1113 (insert-buffer-substring buffer start end)
1115 (while (outline-next-heading)
1116 (unless (outline-invisible-p)
1118 end (progn (outline-end-of-heading) (point)))
1119 (with-current-buffer temp-buffer
1120 (insert-buffer-substring buffer start end)
1121 (insert "\n\n"))))))
1122 (kill-new (buffer-string)))))))
1127 ;;; outline.el ends here