1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 93, 94, 95, 97, 2000 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This package is a major mode for editing outline-format documents.
28 ;; An outline can be `abstracted' to show headers at any given level,
29 ;; with all stuff below hidden. See the Emacs manual for details.
33 (defgroup outlines nil
34 "Support for hierarchical outlining"
38 (defcustom outline-regexp nil
39 "*Regular expression to match the beginning of a heading.
40 Any line whose beginning matches this regexp is considered to start a heading.
41 The recommended way to set this is with a Local Variables: list
42 in the file it applies to. See also `outline-heading-end-regexp'."
43 :type '(choice regexp (const nil))
46 ;; Can't initialize this in the defvar above -- some major modes have
47 ;; already assigned a local value to it.
48 (or (default-value 'outline-regexp)
49 (setq-default outline-regexp "[*\^L]+"))
51 (defcustom outline-heading-end-regexp "\n"
52 "*Regular expression to match the end of a heading line.
53 You can assume that point is at the beginning of a heading when this
54 regexp is searched for. The heading ends at the end of the match.
55 The recommended way to set this is with a `Local Variables:' list
56 in the file it applies to."
60 (defvar outline-mode-prefix-map nil)
62 (if outline-mode-prefix-map
64 (setq outline-mode-prefix-map (make-sparse-keymap))
65 (define-key outline-mode-prefix-map "@" 'outline-mark-subtree)
66 (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
67 (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
68 (define-key outline-mode-prefix-map "\C-i" 'show-children)
69 (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
70 (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
71 (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
72 (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
73 (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
74 (define-key outline-mode-prefix-map "\C-t" 'hide-body)
75 (define-key outline-mode-prefix-map "\C-a" 'show-all)
76 (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
77 (define-key outline-mode-prefix-map "\C-e" 'show-entry)
78 (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
79 (define-key outline-mode-prefix-map "\C-k" 'show-branches)
80 (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
81 (define-key outline-mode-prefix-map "\C-o" 'hide-other))
83 (defvar outline-mode-menu-bar-map nil)
84 (if outline-mode-menu-bar-map
86 (setq outline-mode-menu-bar-map (make-sparse-keymap))
88 (define-key outline-mode-menu-bar-map [hide]
89 (cons "Hide" (make-sparse-keymap "Hide")))
91 (define-key outline-mode-menu-bar-map [hide hide-other]
92 '("Hide Other" . hide-other))
93 (define-key outline-mode-menu-bar-map [hide hide-sublevels]
94 '("Hide Sublevels" . hide-sublevels))
95 (define-key outline-mode-menu-bar-map [hide hide-subtree]
96 '("Hide Subtree" . hide-subtree))
97 (define-key outline-mode-menu-bar-map [hide hide-entry]
98 '("Hide Entry" . hide-entry))
99 (define-key outline-mode-menu-bar-map [hide hide-body]
100 '("Hide Body" . hide-body))
101 (define-key outline-mode-menu-bar-map [hide hide-leaves]
102 '("Hide Leaves" . hide-leaves))
104 (define-key outline-mode-menu-bar-map [show]
105 (cons "Show" (make-sparse-keymap "Show")))
107 (define-key outline-mode-menu-bar-map [show show-subtree]
108 '("Show Subtree" . show-subtree))
109 (define-key outline-mode-menu-bar-map [show show-children]
110 '("Show Children" . show-children))
111 (define-key outline-mode-menu-bar-map [show show-branches]
112 '("Show Branches" . show-branches))
113 (define-key outline-mode-menu-bar-map [show show-entry]
114 '("Show Entry" . show-entry))
115 (define-key outline-mode-menu-bar-map [show show-all]
116 '("Show All" . show-all))
118 (define-key outline-mode-menu-bar-map [headings]
119 (cons "Headings" (make-sparse-keymap "Headings")))
121 (define-key outline-mode-menu-bar-map [headings copy]
122 '(menu-item "Copy to kill ring" outline-headers-as-kill
123 :enable mark-active))
124 (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
125 '("Previous Same Level" . outline-backward-same-level))
126 (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
127 '("Next Same Level" . outline-forward-same-level))
128 (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
129 '("Previous" . outline-previous-visible-heading))
130 (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
131 '("Next" . outline-next-visible-heading))
132 (define-key outline-mode-menu-bar-map [headings outline-up-heading]
133 '("Up" . outline-up-heading)))
135 (defvar outline-mode-map nil "")
139 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
140 (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
141 (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
143 (defcustom outline-minor-mode nil
144 "Non-nil if using Outline mode as a minor mode of some other mode."
147 (make-variable-buffer-local 'outline-minor-mode)
148 (or (assq 'outline-minor-mode minor-mode-alist)
149 (setq minor-mode-alist (append minor-mode-alist
150 (list '(outline-minor-mode " Outl")))))
152 (defvar outline-font-lock-keywords
154 ;; Highlight headings according to the level.
155 (eval . (list (concat "^" outline-regexp ".+")
156 0 '(or (cdr (assq (outline-font-lock-level)
157 '((1 . font-lock-function-name-face)
158 (2 . font-lock-variable-name-face)
159 (3 . font-lock-keyword-face)
160 (4 . font-lock-builtin-face)
161 (5 . font-lock-comment-face)
162 (6 . font-lock-constant-face)
163 (7 . font-lock-type-face)
164 (8 . font-lock-string-face))))
165 font-lock-warning-face)
167 "Additional expressions to highlight in Outline mode.")
169 (defun outline-font-lock-level ()
172 (outline-back-to-heading t)
175 (outline-up-heading-all 1)
176 (setq count (1+ count)))
180 (defvar outline-view-change-hook nil
181 "Normal hook to be run after outline visibility changes.")
184 (defun outline-mode ()
185 "Set major mode for editing outlines with selective display.
186 Headings are lines which start with asterisks: one for major headings,
187 two for subheadings, etc. Lines not starting with asterisks are body lines.
189 Body text or subheadings under a heading can be made temporarily
190 invisible, or visible again. Invisible lines are attached to the end
191 of the heading, so they move with it, if the line is killed and yanked
192 back. A heading with text hidden under it is marked with an ellipsis (...).
194 Commands:\\<outline-mode-map>
195 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
196 \\[outline-previous-visible-heading] outline-previous-visible-heading
197 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
198 \\[outline-backward-same-level] outline-backward-same-level
199 \\[outline-up-heading] outline-up-heading move from subheading to heading
201 \\[hide-body] make all text invisible (not headings).
202 \\[show-all] make everything in buffer visible.
204 The remaining commands are used when point is on a heading line.
205 They apply to some of the body or subheadings of that heading.
206 \\[hide-subtree] hide-subtree make body and subheadings invisible.
207 \\[show-subtree] show-subtree make body and subheadings visible.
208 \\[show-children] show-children make direct subheadings visible.
209 No effect on body, or subheadings 2 or more levels down.
210 With arg N, affects subheadings N levels down.
211 \\[hide-entry] make immediately following body invisible.
212 \\[show-entry] make it visible.
213 \\[hide-leaves] make body under heading and under its subheadings invisible.
214 The subheadings remain visible.
215 \\[show-branches] make all subheadings at all levels visible.
217 The variable `outline-regexp' can be changed to control what is a heading.
218 A line is a heading if `outline-regexp' matches something at the
219 beginning of the line. The longer the match, the deeper the level.
221 Turning on outline mode calls the value of `text-mode-hook' and then of
222 `outline-mode-hook', if they are non-nil."
224 (kill-all-local-variables)
225 (use-local-map outline-mode-map)
226 (setq mode-name "Outline")
227 (setq major-mode 'outline-mode)
228 (define-abbrev-table 'text-mode-abbrev-table ())
229 (setq local-abbrev-table text-mode-abbrev-table)
230 (set-syntax-table text-mode-syntax-table)
231 (make-local-variable 'line-move-ignore-invisible)
232 (setq line-move-ignore-invisible t)
233 ;; Cause use of ellipses for invisible text.
234 (add-to-invisibility-spec '(outline . t))
235 (make-local-variable 'paragraph-start)
236 (setq paragraph-start (concat paragraph-start "\\|\\("
237 outline-regexp "\\)"))
238 ;; Inhibit auto-filling of header lines.
239 (make-local-variable 'auto-fill-inhibit-regexp)
240 (setq auto-fill-inhibit-regexp outline-regexp)
241 (make-local-variable 'paragraph-separate)
242 (setq paragraph-separate (concat paragraph-separate "\\|\\("
243 outline-regexp "\\)"))
244 (make-local-variable 'font-lock-defaults)
245 (setq font-lock-defaults '(outline-font-lock-keywords t))
246 (make-local-variable 'change-major-mode-hook)
247 (setq imenu-generic-expression
248 (list (list nil (concat outline-regexp ".*$") 0)))
249 (add-hook 'change-major-mode-hook 'show-all)
250 (run-hooks 'text-mode-hook 'outline-mode-hook))
252 (defcustom outline-minor-mode-prefix "\C-c@"
253 "*Prefix key to use for Outline commands in Outline minor mode.
254 The value of this variable is checked as part of loading Outline mode.
255 After that, changing the prefix key requires manipulating keymaps."
259 (defvar outline-minor-mode-map nil)
260 (if outline-minor-mode-map
262 (setq outline-minor-mode-map (make-sparse-keymap))
263 (define-key outline-minor-mode-map [menu-bar]
264 outline-mode-menu-bar-map)
265 (define-key outline-minor-mode-map outline-minor-mode-prefix
266 outline-mode-prefix-map))
268 (or (assq 'outline-minor-mode minor-mode-map-alist)
269 (setq minor-mode-map-alist
270 (cons (cons 'outline-minor-mode outline-minor-mode-map)
271 minor-mode-map-alist)))
274 (defun outline-minor-mode (&optional arg)
275 "Toggle Outline minor mode.
276 With arg, turn Outline minor mode on if arg is positive, off otherwise.
277 See the command `outline-mode' for more information on this mode."
279 (setq outline-minor-mode
280 (if (null arg) (not outline-minor-mode)
281 (> (prefix-numeric-value arg) 0)))
282 (if outline-minor-mode
284 (make-local-hook 'change-major-mode-hook)
285 ;; Turn off this mode if we change major modes.
286 (add-hook 'change-major-mode-hook
287 '(lambda () (outline-minor-mode -1))
289 (make-local-variable 'line-move-ignore-invisible)
290 (setq line-move-ignore-invisible t)
291 ;; Cause use of ellipses for invisible text.
292 (add-to-invisibility-spec '(outline . t))
293 (run-hooks 'outline-minor-mode-hook))
294 (setq line-move-ignore-invisible nil)
295 ;; Cause use of ellipses for invisible text.
296 (remove-from-invisibility-spec '(outline . t)))
297 ;; When turning off outline mode, get rid of any outline hiding.
298 (or outline-minor-mode
300 (force-mode-line-update))
302 (defcustom outline-level 'outline-level
303 "*Function of no args to compute a header's nesting level in an outline.
304 It can assume point is at the beginning of a header line."
308 ;; This used to count columns rather than characters, but that made ^L
309 ;; appear to be at level 2 instead of 1. Columns would be better for
310 ;; tab handling, but the default regexp doesn't use tabs, and anyone
311 ;; who changes the regexp can also redefine the outline-level variable
313 (defun outline-level ()
314 "Return the depth to which a statement is nested in the outline.
315 Point must be at the beginning of a header line. This is actually
316 the number of characters that `outline-regexp' matches."
318 (looking-at outline-regexp)
319 (- (match-end 0) (match-beginning 0))))
321 (defun outline-next-preface ()
322 "Skip forward to just before the next heading line.
323 If there's no following heading line, stop before the newline
324 at the end of the buffer."
325 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
327 (goto-char (match-beginning 0)))
328 (if (and (bolp) (not (bobp)))
331 (defun outline-next-heading ()
332 "Move to the next (possibly invisible) heading line."
334 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
336 (goto-char (1+ (match-beginning 0)))))
338 (defun outline-previous-heading ()
339 "Move to the previous (possibly invisible) heading line."
341 (re-search-backward (concat "^\\(" outline-regexp "\\)")
344 (defsubst outline-visible ()
345 "Non-nil if the character after point is visible."
346 (not (get-char-property (point) 'invisible)))
348 (defun outline-back-to-heading (&optional invisible-ok)
349 "Move to previous heading line, or beg of this line if it's a heading.
350 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
352 (or (outline-on-heading-p invisible-ok)
356 (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
358 (error "before first heading"))
359 (setq found (and (or invisible-ok (outline-visible)) (point)))))
363 (defun outline-on-heading-p (&optional invisible-ok)
364 "Return t if point is on a (visible) heading line.
365 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
368 (and (bolp) (or invisible-ok (outline-visible))
369 (looking-at outline-regexp))))
371 (defun outline-end-of-heading ()
372 (if (re-search-forward outline-heading-end-regexp nil 'move)
375 (defun outline-next-visible-heading (arg)
376 "Move to the next visible heading line.
377 With argument, repeats or can move backward if negative.
378 A heading line is one that starts with a `*' (or that
379 `outline-regexp' matches)."
384 (while (and (not (bobp)) (< arg 0))
385 (while (and (not (bobp))
386 (re-search-backward (concat "^\\(" outline-regexp "\\)")
388 (not (outline-visible))))
390 (while (and (not (eobp)) (> arg 0))
391 (while (and (not (eobp))
392 (re-search-forward (concat "^\\(" outline-regexp "\\)")
394 (not (outline-visible))))
398 (defun outline-previous-visible-heading (arg)
399 "Move to the previous heading line.
400 With argument, repeats or can move forward if negative.
401 A heading line is one that starts with a `*' (or that
402 `outline-regexp' matches)."
404 (outline-next-visible-heading (- arg)))
406 (defun outline-mark-subtree ()
407 "Mark the current subtree in an outlined document.
408 This puts point at the start of the current subtree, and mark at the end."
411 (if (outline-on-heading-p)
412 ;; we are already looking at a heading
414 ;; else go back to previous heading
415 (outline-previous-visible-heading 1))
417 (outline-end-of-subtree)
421 (defun outline-flag-region (from to flag)
422 "Hides or shows lines from FROM to TO, according to FLAG.
423 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
424 (let ((inhibit-read-only t))
428 (outline-discard-overlays (point) to 'outline)
430 (let ((o (make-overlay (point) to)))
431 (overlay-put o 'invisible 'outline)
432 (overlay-put o 'isearch-open-invisible
433 'outline-isearch-open-invisible)))))
434 (run-hooks 'outline-view-change-hook))
437 ;; Function to be set as an outline-isearch-open-invisible' property
438 ;; to the overlay that makes the outline invisible (see
439 ;; `outline-flag-region').
440 (defun outline-isearch-open-invisible (overlay)
442 (goto-char (overlay-start overlay))
446 ;; Exclude from the region BEG ... END all overlays
447 ;; which have PROP as the value of the `invisible' property.
448 ;; Exclude them by shrinking them to exclude BEG ... END,
449 ;; or even by splitting them if necessary.
450 ;; Overlays without such an `invisible' property are not touched.
451 (defun outline-discard-overlays (beg end prop)
453 (setq beg (prog1 end (setq end beg))))
455 (let ((overlays (overlays-in beg end))
459 (setq o (car overlays))
460 (if (eq (overlay-get o 'invisible) prop)
461 ;; Either push this overlay outside beg...end
462 ;; or split it to exclude beg...end
463 ;; or delete it entirely (if it is contained in beg...end).
464 (if (< (overlay-start o) beg)
465 (if (> (overlay-end o) end)
467 (setq o1 (outline-copy-overlay o))
468 (move-overlay o1 (overlay-start o1) beg)
469 (move-overlay o end (overlay-end o)))
470 (move-overlay o (overlay-start o) beg))
471 (if (> (overlay-end o) end)
472 (move-overlay o end (overlay-end o))
473 (delete-overlay o))))
474 (setq overlays (cdr overlays))))))
476 ;; Make a copy of overlay O, with the same beginning, end and properties.
477 (defun outline-copy-overlay (o)
478 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
480 (props (overlay-properties o)))
482 (overlay-put o1 (car props) (nth 1 props))
483 (setq props (cdr (cdr props))))
487 "Hide the body directly following this heading."
489 (outline-back-to-heading)
490 (outline-end-of-heading)
492 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
495 "Show the body directly following this heading.
496 Show the heading too, if it is currently invisible."
499 (outline-back-to-heading t)
500 (outline-flag-region (1- (point))
501 (progn (outline-next-preface) (point)) nil)))
504 "Hide all of buffer except headings."
506 (hide-region-body (point-min) (point-max)))
508 (defun hide-region-body (start end)
509 "Hide all body lines in the region, but not headings."
510 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
511 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
512 ;; and run the hook finally.
513 (let (outline-view-change-hook)
516 (narrow-to-region start end)
517 (goto-char (point-min))
518 (if (outline-on-heading-p)
519 (outline-end-of-heading))
521 (outline-flag-region (point)
522 (progn (outline-next-preface) (point)) t)
526 (if (looking-at "\n\n")
528 (outline-end-of-heading)))))))
529 (run-hooks 'outline-view-change-hook))
532 "Show all of the text in the buffer."
534 (outline-flag-region (point-min) (point-max) nil))
536 (defun hide-subtree ()
537 "Hide everything after this heading at deeper levels."
539 (outline-flag-subtree t))
541 (defun hide-leaves ()
542 "Hide all body after this heading at deeper levels."
544 (outline-back-to-heading)
545 (outline-end-of-heading)
546 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
548 (defun show-subtree ()
549 "Show everything after this heading at deeper levels."
551 (outline-flag-subtree nil))
553 (defun hide-sublevels (levels)
554 "Hide everything but the top LEVELS levels of headers, in whole buffer."
557 (error "Must keep at least one level of headers"))
558 (setq levels (1- levels))
559 (let (outline-view-change-hook)
561 (goto-char (point-min))
562 ;; Keep advancing to the next top-level heading.
563 (while (or (and (bobp) (outline-on-heading-p))
564 (outline-next-heading))
565 (let ((end (save-excursion (outline-end-of-subtree) (point))))
566 ;; Hide everything under that.
567 (outline-flag-region (point) end t)
568 ;; Show the first LEVELS levels under that.
570 (show-children levels))
571 ;; Move to the next, since we already found it.
573 (run-hooks 'outline-view-change-hook))
576 "Hide everything except current body and parent and top-level headings."
579 (let (outline-view-change-hook)
581 (outline-back-to-heading t)
583 (while (condition-case nil (progn (outline-up-heading 1) t)
585 (outline-flag-region (1- (point))
586 (save-excursion (forward-line 1) (point))
588 (run-hooks 'outline-view-change-hook))
590 (defun outline-flag-subtree (flag)
592 (outline-back-to-heading)
593 (outline-end-of-heading)
594 (outline-flag-region (point)
595 (progn (outline-end-of-subtree) (point))
598 (defun outline-end-of-subtree ()
599 (outline-back-to-heading)
600 (let ((opoint (point))
602 (level (funcall outline-level)))
603 (while (and (not (eobp))
604 (or first (> (funcall outline-level) level)))
606 (outline-next-heading))
609 ;; Go to end of line before heading
612 ;; leave blank line before heading
613 (forward-char -1))))))
615 (defun show-branches ()
616 "Show all subheadings of this heading, but not their bodies."
618 (show-children 1000))
620 (defun show-children (&optional level)
621 "Show all direct subheadings of this heading.
622 Prefix arg LEVEL is how many levels below the current level should be shown.
623 Default is enough to cause the following heading to appear."
626 (if level (prefix-numeric-value level)
628 (outline-back-to-heading)
629 (let ((start-level (funcall outline-level)))
630 (outline-next-heading)
633 (max 1 (- (funcall outline-level) start-level)))))))
634 (let (outline-view-change-hook)
637 (outline-back-to-heading)
638 (setq level (+ level (funcall outline-level)))
639 (narrow-to-region (point)
640 (progn (outline-end-of-subtree)
641 (if (eobp) (point-max) (1+ (point)))))
642 (goto-char (point-min))
643 (while (and (not (eobp))
645 (outline-next-heading)
647 (if (<= (funcall outline-level) level)
649 (outline-flag-region (save-excursion
654 (progn (outline-end-of-heading) (point))
656 (run-hooks 'outline-view-change-hook))
658 (defun outline-up-heading-all (arg)
659 "Move to the heading line of which the present line is a subheading.
660 This function considers both visible and invisible heading lines.
661 With argument, move up ARG levels."
662 (outline-back-to-heading t)
663 (if (eq (funcall outline-level) 1)
664 (error "Already at top level of the outline"))
665 (while (and (> (funcall outline-level) 1)
668 (let ((present-level (funcall outline-level)))
669 (while (and (not (< (funcall outline-level) present-level))
671 (outline-previous-heading))
672 (setq arg (- arg 1)))))
674 (defun outline-up-heading (arg)
675 "Move to the visible heading line of which the present line is a subheading.
676 With argument, move up ARG levels."
678 (outline-back-to-heading)
679 (if (eq (funcall outline-level) 1)
680 (error "Already at top level of the outline"))
681 (while (and (> (funcall outline-level) 1)
684 (let ((present-level (funcall outline-level)))
685 (while (and (not (< (funcall outline-level) present-level))
687 (outline-previous-visible-heading 1))
688 (setq arg (- arg 1)))))
690 (defun outline-forward-same-level (arg)
691 "Move forward to the ARG'th subheading at same level as this one.
692 Stop at the first and last subheadings of a superior heading."
694 (outline-back-to-heading)
696 (let ((point-to-move-to (save-excursion
697 (outline-get-next-sibling))))
700 (goto-char point-to-move-to)
704 (error "No following same-level heading"))))))
706 (defun outline-get-next-sibling ()
707 "Move to next heading of the same level, and return point or nil if none."
708 (let ((level (funcall outline-level)))
709 (outline-next-visible-heading 1)
710 (while (and (> (funcall outline-level) level)
712 (outline-next-visible-heading 1))
713 (if (< (funcall outline-level) level)
717 (defun outline-backward-same-level (arg)
718 "Move backward to the ARG'th subheading at same level as this one.
719 Stop at the first and last subheadings of a superior heading."
721 (outline-back-to-heading)
723 (let ((point-to-move-to (save-excursion
724 (outline-get-last-sibling))))
727 (goto-char point-to-move-to)
731 (error "No previous same-level heading"))))))
733 (defun outline-get-last-sibling ()
734 "Move to previous heading of the same level, and return point or nil if none."
735 (let ((level (funcall outline-level)))
736 (outline-previous-visible-heading 1)
737 (while (and (> (funcall outline-level) level)
739 (outline-previous-visible-heading 1))
740 (if (< (funcall outline-level) level)
744 (defun outline-headers-as-kill (beg end)
745 "Save the visible outline headers in region at the start of the kill ring.
747 Text shown between the headers isn't copied. Two newlines are
748 inserted between saved headers. Yanking the result may be a
749 convenient way to make a table of contents of the buffer."
753 (narrow-to-region beg end)
754 (goto-char (point-min))
755 (let ((buffer (current-buffer))
758 (with-current-buffer buffer
759 ;; Boundary condition: starting on heading:
760 (when (outline-on-heading-p)
761 (outline-back-to-heading)
763 end (progn (outline-end-of-heading)
765 (insert-buffer-substring buffer start end)
767 (let ((temp-buffer (current-buffer)))
768 (with-current-buffer buffer
769 (while (outline-next-heading)
770 (when (outline-visible)
772 end (progn (outline-end-of-heading) (point)))
773 (with-current-buffer temp-buffer
774 (insert-buffer-substring buffer start end)
776 (kill-new (buffer-string)))))))
781 ;;; outline.el ends here