1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 2000, 2001, 2004
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This package is a major mode for editing outline-format documents.
29 ;; An outline can be `abstracted' to show headers at any given level,
30 ;; with all stuff below hidden. See the Emacs manual for details.
34 ;; - subtree-terminators
35 ;; - better handle comments before function bodies (i.e. heading)
36 ;; - don't bother hiding whitespace
40 (defgroup outlines nil
41 "Support for hierarchical outlining."
45 (defcustom outline-regexp
"[*\^L]+"
46 "*Regular expression to match the beginning of a heading.
47 Any line whose beginning matches this regexp is considered to start a heading.
48 Note that Outline mode only checks this regexp at the start of a line,
49 so the regexp need not (and usually does not) start with `^'.
50 The recommended way to set this is with a Local Variables: list
51 in the file it applies to. See also `outline-heading-end-regexp'."
52 :type
'(choice regexp
(const nil
))
55 (defcustom outline-heading-end-regexp
"\n"
56 "*Regular expression to match the end of a heading line.
57 You can assume that point is at the beginning of a heading when this
58 regexp is searched for. The heading ends at the end of the match.
59 The recommended way to set this is with a `Local Variables:' list
60 in the file it applies to."
64 (defvar outline-mode-prefix-map
65 (let ((map (make-sparse-keymap)))
66 (define-key map
"@" 'outline-mark-subtree
)
67 (define-key map
"\C-n" 'outline-next-visible-heading
)
68 (define-key map
"\C-p" 'outline-previous-visible-heading
)
69 (define-key map
"\C-i" 'show-children
)
70 (define-key map
"\C-s" 'show-subtree
)
71 (define-key map
"\C-d" 'hide-subtree
)
72 (define-key map
"\C-u" 'outline-up-heading
)
73 (define-key map
"\C-f" 'outline-forward-same-level
)
74 (define-key map
"\C-b" 'outline-backward-same-level
)
75 (define-key map
"\C-t" 'hide-body
)
76 (define-key map
"\C-a" 'show-all
)
77 (define-key map
"\C-c" 'hide-entry
)
78 (define-key map
"\C-e" 'show-entry
)
79 (define-key map
"\C-l" 'hide-leaves
)
80 (define-key map
"\C-k" 'show-branches
)
81 (define-key map
"\C-q" 'hide-sublevels
)
82 (define-key map
"\C-o" 'hide-other
)
83 (define-key map
"\C-^" 'outline-move-subtree-up
)
84 (define-key map
"\C-v" 'outline-move-subtree-down
)
85 (define-key map
[(control ?
<)] 'outline-promote
)
86 (define-key map
[(control ?
>)] 'outline-demote
)
87 (define-key map
"\C-m" 'outline-insert-heading
)
88 ;; Where to bind outline-cycle ?
91 (defvar outline-mode-menu-bar-map
92 (let ((map (make-sparse-keymap)))
94 (define-key map
[hide] (cons "Hide" (make-sparse-keymap "Hide")))
96 (define-key map [hide hide-other] '("Hide Other" . hide-other))
97 (define-key map [hide hide-sublevels] '("Hide Sublevels" . hide-sublevels))
98 (define-key map [hide hide-subtree] '("Hide Subtree" . hide-subtree))
99 (define-key map [hide hide-entry] '("Hide Entry" . hide-entry))
100 (define-key map [hide hide-body] '("Hide Body" . hide-body))
101 (define-key map [hide hide-leaves] '("Hide Leaves" . hide-leaves))
103 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
105 (define-key map [show show-subtree] '("Show Subtree" . show-subtree))
106 (define-key map [show show-children] '("Show Children" . show-children))
107 (define-key map [show show-branches] '("Show Branches" . show-branches))
108 (define-key map [show show-entry] '("Show Entry" . show-entry))
109 (define-key map [show show-all] '("Show All" . show-all))
111 (define-key map [headings]
112 (cons "Headings" (make-sparse-keymap "Headings")))
114 (define-key map [headings demote-subtree]
115 '(menu-item "Demote subtree" outline-demote))
116 (define-key map [headings promote-subtree]
117 '(menu-item "Promote subtree" outline-promote))
118 (define-key map [headings move-subtree-down]
119 '(menu-item "Move subtree down" outline-move-subtree-down))
120 (define-key map [headings move-subtree-up]
121 '(menu-item "Move subtree up" outline-move-subtree-up))
122 (define-key map [headings copy]
123 '(menu-item "Copy to kill ring" outline-headers-as-kill
124 :enable mark-active))
125 (define-key map [headings outline-insert-heading]
126 '("New heading" . outline-insert-heading))
127 (define-key map [headings outline-backward-same-level]
128 '("Previous Same Level" . outline-backward-same-level))
129 (define-key map [headings outline-forward-same-level]
130 '("Next Same Level" . outline-forward-same-level))
131 (define-key map [headings outline-previous-visible-heading]
132 '("Previous" . outline-previous-visible-heading))
133 (define-key map [headings outline-next-visible-heading]
134 '("Next" . outline-next-visible-heading))
135 (define-key map [headings outline-up-heading]
136 '("Up" . outline-up-heading))
139 (defvar outline-minor-mode-menu-bar-map
140 (let ((map (make-sparse-keymap)))
141 (define-key map [outline]
143 (nconc (make-sparse-keymap "Outline")
144 ;; Remove extra separator
146 ;; Flatten the major mode's menus into a single menu.
150 ;; Add a separator between each
151 ;; part of the unified menu.
152 (cons '(--- "---") (cdr x))))
153 outline-mode-menu-bar-map))))))
157 (defvar outline-mode-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\C-c" outline-mode-prefix-map)
160 (define-key map [menu-bar] outline-mode-menu-bar-map)
163 (defvar outline-font-lock-keywords
165 ;; Highlight headings according to the level.
166 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
167 0 '(outline-font-lock-face) nil t)))
168 "Additional expressions to highlight in Outline mode.")
171 '((t :inherit font-lock-function-name-face))
176 '((t :inherit font-lock-variable-name-face))
181 '((t :inherit font-lock-keyword-face))
186 '((t :inherit font-lock-builtin-face))
191 '((t :inherit font-lock-comment-face))
196 '((t :inherit font-lock-constant-face))
201 '((t :inherit font-lock-type-face))
206 '((t :inherit font-lock-string-face))
210 (defvar outline-font-lock-faces
211 [outline-1 outline-2 outline-3 outline-4
212 outline-5 outline-6 outline-7 outline-8])
214 (defvar outline-font-lock-levels nil)
215 (make-variable-buffer-local 'outline-font-lock-levels)
217 (defun outline-font-lock-face ()
219 ;; (outline-back-to-heading t)
221 ;; (start-level (funcall outline-level))
222 ;; (level start-level)
224 ;; (while (not (setq face-level
225 ;; (if (or (bobp) (eq level 1)) 0
226 ;; (cdr (assq level outline-font-lock-levels)))))
227 ;; (outline-up-heading 1 t)
228 ;; (setq count (1+ count))
229 ;; (setq level (funcall outline-level)))
230 ;; ;; Remember for later.
231 ;; (unless (zerop count)
232 ;; (setq face-level (+ face-level count))
233 ;; (push (cons start-level face-level) outline-font-lock-levels))
234 ;; (condition-case nil
235 ;; (aref outline-font-lock-faces face-level)
236 ;; (error font-lock-warning-face))))
238 (goto-char (match-beginning 0))
239 (looking-at outline-regexp)
241 (aref outline-font-lock-faces (1- (funcall outline-level)))
242 (error font-lock-warning-face))))
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 (...).
264 Commands:\\<outline-mode-map>
265 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
266 \\[outline-previous-visible-heading] outline-previous-visible-heading
267 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
268 \\[outline-backward-same-level] outline-backward-same-level
269 \\[outline-up-heading] outline-up-heading move from subheading to heading
271 \\[hide-body] make all text invisible (not headings).
272 \\[show-all] make everything in buffer visible.
273 \\[hide-sublevels] make only the first N levels of headers visible.
275 The remaining commands are used when point is on a heading line.
276 They apply to some of the body or subheadings of that heading.
277 \\[hide-subtree] hide-subtree make body and subheadings invisible.
278 \\[show-subtree] show-subtree make body and subheadings visible.
279 \\[show-children] show-children make direct subheadings visible.
280 No effect on body, or subheadings 2 or more levels down.
281 With arg N, affects subheadings N levels down.
282 \\[hide-entry] make immediately following body invisible.
283 \\[show-entry] make it visible.
284 \\[hide-leaves] make body under heading and under its subheadings invisible.
285 The subheadings remain visible.
286 \\[show-branches] make all subheadings at all levels visible.
288 The variable `outline-regexp' can be changed to control what is a heading.
289 A line is a heading if `outline-regexp' matches something at the
290 beginning of the line. The longer the match, the deeper the level.
292 Turning on outline mode calls the value of `text-mode-hook' and then of
293 `outline-mode-hook', if they are non-nil."
294 (make-local-variable 'line-move-ignore-invisible)
295 (setq line-move-ignore-invisible t)
296 ;; Cause use of ellipses for invisible text.
297 (add-to-invisibility-spec '(outline . t))
298 (set (make-local-variable 'paragraph-start)
299 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
300 ;; Inhibit auto-filling of header lines.
301 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
302 (set (make-local-variable 'paragraph-separate)
303 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
304 (set (make-local-variable 'font-lock-defaults)
305 '(outline-font-lock-keywords t nil nil backward-paragraph))
306 (setq imenu-generic-expression
307 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
308 (add-hook 'change-major-mode-hook 'show-all nil t))
310 (defcustom outline-minor-mode-prefix "\C-c@"
311 "*Prefix key to use for Outline commands in Outline minor mode.
312 The value of this variable is checked as part of loading Outline mode.
313 After that, changing the prefix key requires manipulating keymaps."
318 (define-minor-mode outline-minor-mode
319 "Toggle Outline minor mode.
320 With arg, turn Outline minor mode on if arg is positive, off otherwise.
321 See the command `outline-mode' for more information on this mode."
322 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
323 (cons outline-minor-mode-prefix outline-mode-prefix-map))
325 (if outline-minor-mode
327 ;; Turn off this mode if we change major modes.
328 (add-hook 'change-major-mode-hook
329 (lambda () (outline-minor-mode -1))
331 (set (make-local-variable 'line-move-ignore-invisible) t)
332 ;; Cause use of ellipses for invisible text.
333 (add-to-invisibility-spec '(outline . t)))
334 (setq line-move-ignore-invisible nil)
335 ;; Cause use of ellipses for invisible text.
336 (remove-from-invisibility-spec '(outline . t))
337 ;; When turning off outline mode, get rid of any outline hiding.
340 (defvar outline-level 'outline-level
341 "*Function of no args to compute a header's nesting level in an outline.
342 It can assume point is at the beginning of a header line and that the match
343 data reflects the `outline-regexp'.")
345 (defvar outline-heading-alist ()
346 "Alist associating a heading for every possible level.
347 Each entry is of the form (HEADING . LEVEL).
348 This alist is used two ways: to find the heading corresponding to
349 a given level and to find the level of a given heading.
350 If a mode or document needs several sets of outline headings (for example
351 numbered and unnumbered sections), list them set by set and sorted by level
352 within each set. For example in texinfo mode:
354 (setq outline-heading-alist
355 '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
356 (\"@subsubsection\" . 5)
357 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
358 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
359 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
360 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
362 Instead of sorting the entries in each set, you can also separate the
364 (make-variable-buffer-local 'outline-heading-alist)
366 ;; This used to count columns rather than characters, but that made ^L
367 ;; appear to be at level 2 instead of 1. Columns would be better for
368 ;; tab handling, but the default regexp doesn't use tabs, and anyone
369 ;; who changes the regexp can also redefine the outline-level variable
371 (defun outline-level ()
372 "Return the depth to which a statement is nested in the outline.
373 Point must be at the beginning of a header line.
374 This is actually either the level specified in `outline-heading-alist'
375 or else the number of characters matched by `outline-regexp'."
376 (or (cdr (assoc (match-string 0) outline-heading-alist))
377 (- (match-end 0) (match-beginning 0))))
379 (defun outline-next-preface ()
380 "Skip forward to just before the next heading line.
381 If there's no following heading line, stop before the newline
382 at the end of the buffer."
383 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
385 (goto-char (match-beginning 0)))
386 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
389 (defun outline-next-heading ()
390 "Move to the next (possibly invisible) heading line."
392 ;; Make sure we don't match the heading we're at.
393 (if (and (bolp) (not (eobp))) (forward-char 1))
394 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
396 (goto-char (match-beginning 0))))
398 (defun outline-previous-heading ()
399 "Move to the previous (possibly invisible) heading line."
401 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
404 (defsubst outline-invisible-p (&optional pos)
405 "Non-nil if the character after point is invisible."
406 (get-char-property (or pos (point)) 'invisible))
408 (defun outline-visible ()
409 (not (outline-invisible-p)))
410 (make-obsolete 'outline-visible 'outline-invisible-p)
412 (defun outline-back-to-heading (&optional invisible-ok)
413 "Move to previous heading line, or beg of this line if it's a heading.
414 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
416 (or (outline-on-heading-p invisible-ok)
420 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
422 (error "before first heading"))
423 (setq found (and (or invisible-ok (not (outline-invisible-p)))
428 (defun outline-on-heading-p (&optional invisible-ok)
429 "Return t if point is on a (visible) heading line.
430 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
433 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
434 (looking-at outline-regexp))))
436 (defun outline-insert-heading ()
437 "Insert a new heading at same depth at point."
439 (let ((head (save-excursion
441 (outline-back-to-heading)
442 (error (outline-next-heading)))
444 (or (caar outline-heading-alist) "")
446 (unless (or (string-match "[ \t]\\'" head)
447 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
449 (setq head (concat head " ")))
450 (unless (bolp) (end-of-line) (newline))
453 (save-excursion (newline-and-indent)))
454 (run-hooks 'outline-insert-heading-hook)))
456 (defun outline-promote (&optional children)
457 "Promote headings higher up the tree.
458 If prefix argument CHILDREN is given, promote also all the children.
459 If the region is active in `transient-mark-mode', promote all headings
462 (list (if (and transient-mark-mode mark-active) 'region
463 (outline-back-to-heading)
464 (if current-prefix-arg nil 'subtree))))
466 ((eq children 'region)
467 (outline-map-region 'outline-promote (region-beginning) (region-end)))
469 (outline-map-region 'outline-promote
471 (save-excursion (outline-get-next-sibling) (point))))
473 (outline-back-to-heading t)
474 (let* ((head (match-string 0))
475 (level (save-match-data (funcall outline-level)))
476 (up-head (or (outline-head-from-level (1- level) head)
479 (outline-up-heading 1 t)
480 (match-string 0))))))
482 (unless (rassoc level outline-heading-alist)
483 (push (cons head level) outline-heading-alist))
485 (replace-match up-head nil t)))))
487 (defun outline-demote (&optional children)
488 "Demote headings lower down the tree.
489 If prefix argument CHILDREN is given, demote also all the children.
490 If the region is active in `transient-mark-mode', demote all headings
493 (list (if (and transient-mark-mode mark-active) 'region
494 (outline-back-to-heading)
495 (if current-prefix-arg nil 'subtree))))
497 ((eq children 'region)
498 (outline-map-region 'outline-demote (region-beginning) (region-end)))
500 (outline-map-region 'outline-demote
502 (save-excursion (outline-get-next-sibling) (point))))
504 (let* ((head (match-string 0))
505 (level (save-match-data (funcall outline-level)))
507 (or (outline-head-from-level (1+ level) head)
510 (while (and (progn (outline-next-heading) (not (eobp)))
511 (<= (funcall outline-level) level)))
513 ;; Try again from the beginning of the buffer.
514 (goto-char (point-min))
515 (while (and (progn (outline-next-heading) (not (eobp)))
516 (<= (funcall outline-level) level))))
518 (looking-at outline-regexp)
521 ;; Bummer!! There is no lower heading in the buffer.
522 ;; Let's try to invent one by repeating the first char.
523 (let ((new-head (concat (substring head 0 1) head)))
524 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
526 ;; Why bother checking that it is indeed lower level ?
528 ;; Didn't work: keep it as is so it's still a heading.
531 (unless (rassoc level outline-heading-alist)
532 (push (cons head level) outline-heading-alist))
533 (replace-match down-head nil t)))))
535 (defun outline-head-from-level (level head &optional alist)
536 "Get new heading with level LEVEL from ALIST.
537 If there are no such entries, return nil.
538 ALIST defaults to `outline-heading-alist'.
539 Similar to (car (rassoc LEVEL ALIST)).
540 If there are several different entries with same new level, choose
541 the one with the smallest distance to the assocation of HEAD in the alist.
542 This makes it possible for promotion to work in modes with several
543 independent sets of headings (numbered, unnumbered, appendix...)"
544 (unless alist (setq alist outline-heading-alist))
545 (let ((l (rassoc level alist))
549 ;; If there's no HEAD after L, any other entry for LEVEL after L
550 ;; can't be much better than L.
551 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
552 ;; If there's no other entry for LEVEL, just keep L.
553 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
554 ;; Now we have L, L2, and H: see if L2 seems better than L.
555 ;; If H is after L2, L2 is better.
556 ((memq h (setq l2l (memq l2 (cdr ll))))
557 (outline-head-from-level level head l2l))
558 ;; Now we have H between L and L2.
559 ;; If there's a separator between L and H, prefer L2.
560 ((memq h (memq nil ll))
561 (outline-head-from-level level head l2l))
562 ;; If there's a separator between L2 and H, prefer L.
563 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
564 ;; No separator between L and L2, check the distance.
565 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
566 (outline-head-from-level level head l2l))
567 ;; If all else fails, just keep L.
570 (defun outline-map-region (fun beg end)
571 "Call FUN for every heading between BEG and END.
572 When FUN is called, point is at the beginning of the heading and
573 the match data is set appropriately."
575 (setq end (copy-marker end))
577 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
578 (goto-char (match-beginning 0))
581 (outline-next-heading)
586 ;; Vertical tree motion
588 (defun outline-move-subtree-up (&optional arg)
589 "Move the currrent subtree up past ARG headlines of the same level."
591 (outline-move-subtree-down (- arg)))
593 (defun outline-move-subtree-down (&optional arg)
594 "Move the currrent subtree down past ARG headlines of the same level."
596 (let ((re (concat "^\\(?:" outline-regexp "\\)"))
597 (movfunc (if (> arg 0) 'outline-get-next-sibling
598 'outline-get-last-sibling))
599 (ins-point (make-marker))
603 (outline-back-to-heading)
606 (save-excursion (outline-end-of-heading)
607 (setq folded (outline-invisible-p)))
608 (outline-end-of-subtree))
609 (if (= (char-after) ?\n) (forward-char 1))
611 ;; Find insertion point, with error handling
614 (or (funcall movfunc)
615 (progn (goto-char beg)
616 (error "Cannot move past superior level")))
619 ;; Moving forward - still need to move over subtree
620 (progn (outline-end-of-subtree)
621 (if (= (char-after) ?\n) (forward-char 1))))
622 (move-marker ins-point (point))
623 (insert (delete-and-extract-region beg end))
624 (goto-char ins-point)
625 (if folded (hide-subtree))
626 (move-marker ins-point nil)))
628 (defun outline-end-of-heading ()
629 (if (re-search-forward outline-heading-end-regexp nil 'move)
632 (defun outline-next-visible-heading (arg)
633 "Move to the next visible heading line.
634 With argument, repeats or can move backward if negative.
635 A heading line is one that starts with a `*' (or that
636 `outline-regexp' matches)."
641 (while (and (not (bobp)) (< arg 0))
642 (while (and (not (bobp))
643 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
645 (outline-invisible-p)))
647 (while (and (not (eobp)) (> arg 0))
648 (while (and (not (eobp))
649 (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
651 (outline-invisible-p (match-beginning 0))))
655 (defun outline-previous-visible-heading (arg)
656 "Move to the previous heading line.
657 With argument, repeats or can move forward if negative.
658 A heading line is one that starts with a `*' (or that
659 `outline-regexp' matches)."
661 (outline-next-visible-heading (- arg)))
663 (defun outline-mark-subtree ()
664 "Mark the current subtree in an outlined document.
665 This puts point at the start of the current subtree, and mark at the end."
668 (if (outline-on-heading-p)
669 ;; we are already looking at a heading
671 ;; else go back to previous heading
672 (outline-previous-visible-heading 1))
674 (outline-end-of-subtree)
679 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
680 (defun outline-flag-region (from to flag)
681 "Hide or show lines from FROM to TO, according to FLAG.
682 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
683 (remove-overlays from to 'invisible 'outline)
685 (let ((o (make-overlay from to)))
686 (overlay-put o 'invisible 'outline)
687 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
688 ;; Seems only used by lazy-lock. I.e. obsolete.
689 (run-hooks 'outline-view-change-hook))
691 (defun outline-reveal-toggle-invisible (o hidep)
693 (goto-char (overlay-start o))
695 ;; When hiding the area again, we could just clean it up and let
696 ;; reveal do the rest, by simply doing:
697 ;; (remove-overlays (overlay-start o) (overlay-end o)
698 ;; 'invisible 'outline)
700 ;; That works fine as long as everything is in sync, but if the
701 ;; structure of the document is changed while revealing parts of it,
702 ;; the resulting behavior can be ugly. I.e. we need to make
703 ;; sure that we hide exactly a subtree.
705 (let ((end (overlay-end o)))
709 (outline-next-visible-heading 1)
710 (and (not (eobp)) (< (point) end))))))
712 ;; When revealing, we just need to reveal sublevels. If point is
713 ;; inside one of the sublevels, reveal will call us again.
714 ;; But we need to preserve the original overlay.
715 (let ((o1 (copy-overlay o)))
716 (overlay-put o 'invisible nil) ;Show (most of) the text.
720 ;; Normally just the above is needed.
721 ;; But in odd cases, the above might fail to show anything.
722 ;; To avoid an infinite loop, we have to make sure that
723 ;; *something* gets shown.
724 (and (equal (overlay-start o) (overlay-start o1))
725 (< (point) (overlay-end o))
726 (= 0 (forward-line 1)))))
727 ;; If still nothing was shown, just kill the damn thing.
728 (when (equal (overlay-start o) (overlay-start o1))
729 ;; I've seen it happen at the end of buffer.
730 (delete-overlay o1))))))
732 ;; Function to be set as an outline-isearch-open-invisible' property
733 ;; to the overlay that makes the outline invisible (see
734 ;; `outline-flag-region').
735 (defun outline-isearch-open-invisible (overlay)
736 ;; We rely on the fact that isearch places point on the matched text.
740 "Hide the body directly following this heading."
742 (outline-back-to-heading)
744 (outline-end-of-heading)
745 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
748 "Show the body directly following this heading.
749 Show the heading too, if it is currently invisible."
752 (outline-back-to-heading t)
753 (outline-flag-region (1- (point))
754 (progn (outline-next-preface) (point)) nil)))
757 "Hide all body lines in buffer, leaving all headings visible."
759 (hide-region-body (point-min) (point-max)))
761 (defun hide-region-body (start end)
762 "Hide all body lines in the region, but not headings."
763 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
764 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
765 ;; and run the hook finally.
766 (let (outline-view-change-hook)
769 (narrow-to-region start end)
770 (goto-char (point-min))
771 (if (outline-on-heading-p)
772 (outline-end-of-heading)
773 (outline-next-preface))
775 (outline-flag-region (point)
776 (progn (outline-next-preface) (point)) t)
778 (forward-char (if (looking-at "\n\n") 2 1))
779 (outline-end-of-heading))))))
780 (run-hooks 'outline-view-change-hook))
783 "Show all of the text in the buffer."
785 (outline-flag-region (point-min) (point-max) nil))
787 (defun hide-subtree ()
788 "Hide everything after this heading at deeper levels."
790 (outline-flag-subtree t))
792 (defun hide-leaves ()
793 "Hide all body after this heading at deeper levels."
795 (outline-back-to-heading)
797 (outline-end-of-heading)
798 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
800 (defun show-subtree ()
801 "Show everything after this heading at deeper levels."
803 (outline-flag-subtree nil))
805 (defun outline-show-heading ()
806 "Show the current heading and move to its end."
807 (outline-flag-region (- (point)
809 (if (and outline-blank-line
810 (eq (char-before (1- (point))) ?\n))
812 (progn (outline-end-of-heading) (point))
815 (defun hide-sublevels (levels)
816 "Hide everything but the top LEVELS levels of headers, in whole buffer."
819 (error "Must keep at least one level of headers"))
820 (let (outline-view-change-hook)
822 (goto-char (point-min))
823 ;; Skip the prelude, if any.
824 (unless (outline-on-heading-p t) (outline-next-heading))
825 ;; First hide everything.
826 (outline-flag-region (point) (point-max) t)
827 ;; Then unhide the top level headers.
830 (if (<= (funcall outline-level) levels)
831 (outline-show-heading)))
832 (point) (point-max))))
833 (run-hooks 'outline-view-change-hook))
836 "Hide everything except current body and parent and top-level headings."
839 (let (outline-view-change-hook)
841 (outline-back-to-heading t)
843 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
845 (outline-flag-region (1- (point))
846 (save-excursion (forward-line 1) (point))
848 (run-hooks 'outline-view-change-hook))
850 (defun outline-toggle-children ()
851 "Show or hide the current subtree depending on its current state."
853 (outline-back-to-heading)
854 (if (not (outline-invisible-p (line-end-position)))
859 (defun outline-flag-subtree (flag)
861 (outline-back-to-heading)
862 (outline-end-of-heading)
863 (outline-flag-region (point)
864 (progn (outline-end-of-subtree) (point))
867 (defun outline-end-of-subtree ()
868 (outline-back-to-heading)
869 (let ((opoint (point))
871 (level (funcall outline-level)))
872 (while (and (not (eobp))
873 (or first (> (funcall outline-level) level)))
875 (outline-next-heading))
878 ;; Go to end of line before heading
880 (if (and outline-blank-line (bolp))
881 ;; leave blank line before heading
882 (forward-char -1))))))
884 (defun show-branches ()
885 "Show all subheadings of this heading, but not their bodies."
887 (show-children 1000))
889 (defun show-children (&optional level)
890 "Show all direct subheadings of this heading.
891 Prefix arg LEVEL is how many levels below the current level should be shown.
892 Default is enough to cause the following heading to appear."
895 (if level (prefix-numeric-value level)
897 (outline-back-to-heading)
898 (let ((start-level (funcall outline-level)))
899 (outline-next-heading)
902 (max 1 (- (funcall outline-level) start-level)))))))
903 (let (outline-view-change-hook)
905 (outline-back-to-heading)
906 (setq level (+ level (funcall outline-level)))
909 (if (<= (funcall outline-level) level)
910 (outline-show-heading)))
912 (progn (outline-end-of-subtree)
913 (if (eobp) (point-max) (1+ (point)))))))
914 (run-hooks 'outline-view-change-hook))
918 (defun outline-up-heading (arg &optional invisible-ok)
919 "Move to the visible heading line of which the present line is a subheading.
920 With argument, move up ARG levels.
921 If INVISIBLE-OK is non-nil, also consider invisible lines."
923 (and (eq this-command 'outline-up-heading)
924 (or (eq last-command 'outline-up-heading) (push-mark)))
925 (outline-back-to-heading invisible-ok)
926 (let ((start-level (funcall outline-level)))
927 (if (eq start-level 1)
928 (error "Already at top level of the outline"))
929 (while (and (> start-level 1) (> arg 0) (not (bobp)))
930 (let ((level start-level))
931 (while (not (or (< level start-level) (bobp)))
933 (outline-previous-heading)
934 (outline-previous-visible-heading 1))
935 (setq level (funcall outline-level)))
936 (setq start-level level))
937 (setq arg (- arg 1))))
938 (looking-at outline-regexp))
940 (defun outline-forward-same-level (arg)
941 "Move forward to the ARG'th subheading at same level as this one.
942 Stop at the first and last subheadings of a superior heading."
944 (outline-back-to-heading)
946 (let ((point-to-move-to (save-excursion
947 (outline-get-next-sibling))))
950 (goto-char point-to-move-to)
954 (error "No following same-level heading"))))))
956 (defun outline-get-next-sibling ()
957 "Move to next heading of the same level, and return point or nil if none."
958 (let ((level (funcall outline-level)))
959 (outline-next-visible-heading 1)
960 (while (and (not (eobp)) (> (funcall outline-level) level))
961 (outline-next-visible-heading 1))
962 (if (or (eobp) (< (funcall outline-level) level))
966 (defun outline-backward-same-level (arg)
967 "Move backward to the ARG'th subheading at same level as this one.
968 Stop at the first and last subheadings of a superior heading."
970 (outline-back-to-heading)
972 (let ((point-to-move-to (save-excursion
973 (outline-get-last-sibling))))
976 (goto-char point-to-move-to)
980 (error "No previous same-level heading"))))))
982 (defun outline-get-last-sibling ()
983 "Move to previous heading of the same level, and return point or nil if none."
984 (let ((level (funcall outline-level)))
985 (outline-previous-visible-heading 1)
986 (while (and (> (funcall outline-level) level)
988 (outline-previous-visible-heading 1))
989 (if (< (funcall outline-level) level)
993 (defun outline-headers-as-kill (beg end)
994 "Save the visible outline headers in region at the start of the kill ring.
996 Text shown between the headers isn't copied. Two newlines are
997 inserted between saved headers. Yanking the result may be a
998 convenient way to make a table of contents of the buffer."
1002 (narrow-to-region beg end)
1003 (goto-char (point-min))
1004 (let ((buffer (current-buffer))
1007 (with-current-buffer buffer
1008 ;; Boundary condition: starting on heading:
1009 (when (outline-on-heading-p)
1010 (outline-back-to-heading)
1012 end (progn (outline-end-of-heading)
1014 (insert-buffer-substring buffer start end)
1016 (let ((temp-buffer (current-buffer)))
1017 (with-current-buffer buffer
1018 (while (outline-next-heading)
1019 (unless (outline-invisible-p)
1021 end (progn (outline-end-of-heading) (point)))
1022 (with-current-buffer temp-buffer
1023 (insert-buffer-substring buffer start end)
1024 (insert "\n\n"))))))
1025 (kill-new (buffer-string)))))))
1030 ;;; arch-tag: 1724410e-7d4d-4f46-b801-49e18171e874
1031 ;;; outline.el ends here