Rename `autoselect-window' to `mouse-autoselect-window'.
[emacs.git] / lisp / textmodes / outline.el
blob941de59fbfb883aa86d305fac35d97525cc52564
1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 93, 94, 95, 97, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: outlines
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)
14 ;; 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; 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.
26 ;;; Commentary:
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.
32 ;;; Todo:
34 ;; - subtree-terminators
35 ;; - better handle comments before function bodies (i.e. heading)
36 ;; - don't bother hiding whitespace
38 ;;; Code:
40 (defgroup outlines nil
41 "Support for hierarchical outlining"
42 :prefix "outline-"
43 :group 'editing)
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))
53 :group 'outlines)
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."
61 :type 'regexp
62 :group 'outlines)
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-promote)
84 (define-key map "\C-v" 'outline-demote)
85 map))
87 (defvar outline-mode-menu-bar-map
88 (let ((map (make-sparse-keymap)))
90 (define-key map [hide] (cons "Hide" (make-sparse-keymap "Hide")))
92 (define-key map [hide hide-other] '("Hide Other" . hide-other))
93 (define-key map [hide hide-sublevels] '("Hide Sublevels" . hide-sublevels))
94 (define-key map [hide hide-subtree] '("Hide Subtree" . hide-subtree))
95 (define-key map [hide hide-entry] '("Hide Entry" . hide-entry))
96 (define-key map [hide hide-body] '("Hide Body" . hide-body))
97 (define-key map [hide hide-leaves] '("Hide Leaves" . hide-leaves))
99 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
101 (define-key map [show show-subtree] '("Show Subtree" . show-subtree))
102 (define-key map [show show-children] '("Show Children" . show-children))
103 (define-key map [show show-branches] '("Show Branches" . show-branches))
104 (define-key map [show show-entry] '("Show Entry" . show-entry))
105 (define-key map [show show-all] '("Show All" . show-all))
107 (define-key map [headings]
108 (cons "Headings" (make-sparse-keymap "Headings")))
110 (define-key map [headings copy]
111 '(menu-item "Copy to kill ring" outline-headers-as-kill
112 :enable mark-active))
113 (define-key map [headings outline-backward-same-level]
114 '("Previous Same Level" . outline-backward-same-level))
115 (define-key map [headings outline-forward-same-level]
116 '("Next Same Level" . outline-forward-same-level))
117 (define-key map [headings outline-previous-visible-heading]
118 '("Previous" . outline-previous-visible-heading))
119 (define-key map [headings outline-next-visible-heading]
120 '("Next" . outline-next-visible-heading))
121 (define-key map [headings outline-up-heading]
122 '("Up" . outline-up-heading))
123 map))
125 (defvar outline-minor-mode-menu-bar-map
126 (let ((map (make-sparse-keymap)))
127 (define-key map [outline]
128 (cons "Outline"
129 (nconc (make-sparse-keymap "Outline")
130 ;; Remove extra separator
131 (cdr
132 ;; Flatten the major mode's menus into a single menu.
133 (apply 'append
134 (mapcar (lambda (x)
135 (if (consp x)
136 ;; Add a separator between each
137 ;; part of the unified menu.
138 (cons '(--- "---") (cdr x))))
139 outline-mode-menu-bar-map))))))
140 map))
143 (defvar outline-mode-map
144 (let ((map (make-sparse-keymap)))
145 (define-key map "\C-c" outline-mode-prefix-map)
146 (define-key map [menu-bar] outline-mode-menu-bar-map)
147 map))
149 (defvar outline-font-lock-keywords
150 '(;;
151 ;; Highlight headings according to the level.
152 (eval . (list (concat "^" outline-regexp ".+")
153 0 '(or (cdr (assq (outline-font-lock-level)
154 ;; FIXME: this is silly!
155 '((1 . font-lock-function-name-face)
156 (2 . font-lock-variable-name-face)
157 (3 . font-lock-keyword-face)
158 (4 . font-lock-builtin-face)
159 (5 . font-lock-comment-face)
160 (6 . font-lock-constant-face)
161 (7 . font-lock-type-face)
162 (8 . font-lock-string-face))))
163 font-lock-warning-face)
164 nil t)))
165 "Additional expressions to highlight in Outline mode.")
167 (defun outline-font-lock-level ()
168 (let ((count 1))
169 (save-excursion
170 (outline-back-to-heading t)
171 (while (and (not (bobp))
172 (not (eq (funcall outline-level) 1)))
173 (outline-up-heading 1 t)
174 (setq count (1+ count)))
175 count)))
177 (defvar outline-view-change-hook nil
178 "Normal hook to be run after outline visibility changes.")
180 ;;;###autoload
181 (define-derived-mode outline-mode text-mode "Outline"
182 "Set major mode for editing outlines with selective display.
183 Headings are lines which start with asterisks: one for major headings,
184 two for subheadings, etc. Lines not starting with asterisks are body lines.
186 Body text or subheadings under a heading can be made temporarily
187 invisible, or visible again. Invisible lines are attached to the end
188 of the heading, so they move with it, if the line is killed and yanked
189 back. A heading with text hidden under it is marked with an ellipsis (...).
191 Commands:\\<outline-mode-map>
192 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
193 \\[outline-previous-visible-heading] outline-previous-visible-heading
194 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
195 \\[outline-backward-same-level] outline-backward-same-level
196 \\[outline-up-heading] outline-up-heading move from subheading to heading
198 \\[hide-body] make all text invisible (not headings).
199 \\[show-all] make everything in buffer visible.
201 The remaining commands are used when point is on a heading line.
202 They apply to some of the body or subheadings of that heading.
203 \\[hide-subtree] hide-subtree make body and subheadings invisible.
204 \\[show-subtree] show-subtree make body and subheadings visible.
205 \\[show-children] show-children make direct subheadings visible.
206 No effect on body, or subheadings 2 or more levels down.
207 With arg N, affects subheadings N levels down.
208 \\[hide-entry] make immediately following body invisible.
209 \\[show-entry] make it visible.
210 \\[hide-leaves] make body under heading and under its subheadings invisible.
211 The subheadings remain visible.
212 \\[show-branches] make all subheadings at all levels visible.
214 The variable `outline-regexp' can be changed to control what is a heading.
215 A line is a heading if `outline-regexp' matches something at the
216 beginning of the line. The longer the match, the deeper the level.
218 Turning on outline mode calls the value of `text-mode-hook' and then of
219 `outline-mode-hook', if they are non-nil."
220 (make-local-variable 'line-move-ignore-invisible)
221 (setq line-move-ignore-invisible t)
222 ;; Cause use of ellipses for invisible text.
223 (add-to-invisibility-spec '(outline . t))
224 (set (make-local-variable 'paragraph-start)
225 (concat paragraph-start "\\|\\(" outline-regexp "\\)"))
226 ;; Inhibit auto-filling of header lines.
227 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
228 (set (make-local-variable 'paragraph-separate)
229 (concat paragraph-separate "\\|\\(" outline-regexp "\\)"))
230 (set (make-local-variable 'font-lock-defaults)
231 '(outline-font-lock-keywords t nil nil backward-paragraph))
232 (setq imenu-generic-expression
233 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
234 (add-hook 'change-major-mode-hook 'show-all nil t))
236 (defcustom outline-minor-mode-prefix "\C-c@"
237 "*Prefix key to use for Outline commands in Outline minor mode.
238 The value of this variable is checked as part of loading Outline mode.
239 After that, changing the prefix key requires manipulating keymaps."
240 :type 'string
241 :group 'outlines)
243 ;;;###autoload
244 (define-minor-mode outline-minor-mode
245 "Toggle Outline minor mode.
246 With arg, turn Outline minor mode on if arg is positive, off otherwise.
247 See the command `outline-mode' for more information on this mode."
248 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
249 (cons outline-minor-mode-prefix outline-mode-prefix-map))
250 (if outline-minor-mode
251 (progn
252 ;; Turn off this mode if we change major modes.
253 (add-hook 'change-major-mode-hook
254 (lambda () (outline-minor-mode -1))
255 nil t)
256 (set (make-local-variable 'line-move-ignore-invisible) t)
257 ;; Cause use of ellipses for invisible text.
258 (add-to-invisibility-spec '(outline . t)))
259 (setq line-move-ignore-invisible nil)
260 ;; Cause use of ellipses for invisible text.
261 (remove-from-invisibility-spec '(outline . t))
262 ;; When turning off outline mode, get rid of any outline hiding.
263 (show-all)))
265 (defcustom outline-level 'outline-level
266 "*Function of no args to compute a header's nesting level in an outline.
267 It can assume point is at the beginning of a header line."
268 :type 'function
269 :group 'outlines)
271 (defvar outline-heading-alist ()
272 "Alist associating a heading for every possible level.
273 Each entry is of the form (HEADING . LEVEL).
274 This alist is used both to find the heading corresponding to
275 a given level and to find the level of a given heading.")
276 (make-variable-buffer-local 'outline-heading-alist)
278 ;; This used to count columns rather than characters, but that made ^L
279 ;; appear to be at level 2 instead of 1. Columns would be better for
280 ;; tab handling, but the default regexp doesn't use tabs, and anyone
281 ;; who changes the regexp can also redefine the outline-level variable
282 ;; as appropriate.
283 (defun outline-level ()
284 "Return the depth to which a statement is nested in the outline.
285 Point must be at the beginning of a header line.
286 This is actually either the level specified in `outline-heading-alist'
287 or else the number of characters matched by `outline-regexp'."
288 (save-excursion
289 (if (not (looking-at outline-regexp))
290 ;; This should never happen
291 1000
292 (or (cdr (assoc (match-string 0) outline-heading-alist))
293 (- (match-end 0) (match-beginning 0))))))
295 (defun outline-next-preface ()
296 "Skip forward to just before the next heading line.
297 If there's no following heading line, stop before the newline
298 at the end of the buffer."
299 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
300 nil 'move)
301 (goto-char (match-beginning 0)))
302 (if (and (bolp) (not (bobp)))
303 (forward-char -1)))
305 (defun outline-next-heading ()
306 "Move to the next (possibly invisible) heading line."
307 (interactive)
308 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
309 nil 'move)
310 (goto-char (1+ (match-beginning 0)))))
312 (defun outline-previous-heading ()
313 "Move to the previous (possibly invisible) heading line."
314 (interactive)
315 (re-search-backward (concat "^\\(" outline-regexp "\\)")
316 nil 'move))
318 (defsubst outline-invisible-p ()
319 "Non-nil if the character after point is invisible."
320 (get-char-property (point) 'invisible))
321 (defun outline-visible ()
322 "Obsolete. Use `outline-invisible-p'."
323 (not (outline-invisible-p)))
325 (defun outline-back-to-heading (&optional invisible-ok)
326 "Move to previous heading line, or beg of this line if it's a heading.
327 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
328 (beginning-of-line)
329 (or (outline-on-heading-p invisible-ok)
330 (let (found)
331 (save-excursion
332 (while (not found)
333 (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
334 nil t)
335 (error "before first heading"))
336 (setq found (and (or invisible-ok (not (outline-invisible-p)))
337 (point)))))
338 (goto-char found)
339 found)))
341 (defun outline-on-heading-p (&optional invisible-ok)
342 "Return t if point is on a (visible) heading line.
343 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
344 (save-excursion
345 (beginning-of-line)
346 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
347 (looking-at outline-regexp))))
349 (defun outline-insert-heading ()
350 "Insert a new heading at same depth at point."
351 (interactive)
352 (let ((head (save-excursion
353 (condition-case nil
354 (outline-back-to-heading)
355 (error (outline-next-heading)))
356 (if (eobp)
357 (or (caar outline-heading-alist) "")
358 (match-string 0)))))
359 (unless (or (string-match "[ \t]\\'" head)
360 (not (string-match outline-regexp (concat head " "))))
361 (setq head (concat head " ")))
362 (unless (bolp) (end-of-line) (newline))
363 (insert head)
364 (unless (eolp)
365 (save-excursion (newline-and-indent)))
366 (run-hooks 'outline-insert-heading-hook)))
368 (defun outline-promote (&optional children)
369 "Promote the current heading higher up the tree.
370 If prefix argument CHILDREN is given, promote also all the children."
371 (interactive "P")
372 (outline-back-to-heading)
373 (let* ((head (match-string 0))
374 (level (save-match-data (funcall outline-level)))
375 (up-head (or (car (rassoc (1- level) outline-heading-alist))
376 (save-excursion
377 (save-match-data
378 (outline-up-heading 1 t)
379 (match-string 0))))))
381 (unless (rassoc level outline-heading-alist)
382 (push (cons head level) outline-heading-alist))
384 (replace-match up-head nil t)
385 (when children
386 (outline-map-tree 'outline-promote level))))
388 (defun outline-demote (&optional children)
389 "Demote the current heading lower down the tree.
390 If prefix argument CHILDREN is given, demote also all the children."
391 (interactive "P")
392 (outline-back-to-heading)
393 (let* ((head (match-string 0))
394 (level (save-match-data (funcall outline-level)))
395 (down-head
396 (or (car (rassoc (1+ level) outline-heading-alist))
397 (save-excursion
398 (save-match-data
399 (while (and (not (eobp))
400 (progn
401 (outline-next-heading)
402 (<= (funcall outline-level) level))))
403 (when (eobp)
404 ;; Try again from the beginning of the buffer.
405 (goto-char (point-min))
406 (while (and (not (eobp))
407 (progn
408 (outline-next-heading)
409 (<= (funcall outline-level) level)))))
410 (unless (eobp) (match-string 0))))
411 (save-match-data
412 ;; Bummer!! There is no lower heading in the buffer.
413 ;; Let's try to invent one by repeating the first char.
414 (let ((new-head (concat (substring head 0 1) head)))
415 (if (string-match (concat "\\`" outline-regexp) new-head)
416 ;; Why bother checking that it is indeed of lower level ?
417 new-head
418 ;; Didn't work: keep it as is so it's still a heading.
419 head))))))
421 (unless (rassoc level outline-heading-alist)
422 (push (cons head level) outline-heading-alist))
424 (replace-match down-head nil t)
425 (when children
426 (outline-map-tree 'outline-demote level))))
428 (defun outline-map-tree (fun level)
429 "Call FUN for every heading underneath the current one."
430 (save-excursion
431 (while (and (progn
432 (outline-next-heading)
433 (> (funcall outline-level) level))
434 (not (eobp)))
435 (funcall fun))))
437 (defun outline-end-of-heading ()
438 (if (re-search-forward outline-heading-end-regexp nil 'move)
439 (forward-char -1)))
441 (defun outline-next-visible-heading (arg)
442 "Move to the next visible heading line.
443 With argument, repeats or can move backward if negative.
444 A heading line is one that starts with a `*' (or that
445 `outline-regexp' matches)."
446 (interactive "p")
447 (if (< arg 0)
448 (beginning-of-line)
449 (end-of-line))
450 (while (and (not (bobp)) (< arg 0))
451 (while (and (not (bobp))
452 (re-search-backward (concat "^\\(" outline-regexp "\\)")
453 nil 'move)
454 (outline-invisible-p)))
455 (setq arg (1+ arg)))
456 (while (and (not (eobp)) (> arg 0))
457 (while (and (not (eobp))
458 (re-search-forward (concat "^\\(" outline-regexp "\\)")
459 nil 'move)
460 (outline-invisible-p)))
461 (setq arg (1- arg)))
462 (beginning-of-line))
464 (defun outline-previous-visible-heading (arg)
465 "Move to the previous heading line.
466 With argument, repeats or can move forward if negative.
467 A heading line is one that starts with a `*' (or that
468 `outline-regexp' matches)."
469 (interactive "p")
470 (outline-next-visible-heading (- arg)))
472 (defun outline-mark-subtree ()
473 "Mark the current subtree in an outlined document.
474 This puts point at the start of the current subtree, and mark at the end."
475 (interactive)
476 (let ((beg))
477 (if (outline-on-heading-p)
478 ;; we are already looking at a heading
479 (beginning-of-line)
480 ;; else go back to previous heading
481 (outline-previous-visible-heading 1))
482 (setq beg (point))
483 (outline-end-of-subtree)
484 (push-mark (point))
485 (goto-char beg)))
488 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
489 (defun outline-flag-region (from to flag)
490 "Hide or show lines from FROM to TO, according to FLAG.
491 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
492 (remove-overlays from to 'invisible 'outline)
493 (when flag
494 (let ((o (make-overlay from to)))
495 (overlay-put o 'invisible 'outline)
496 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
497 ;; Seems only used by lazy-lock. I.e. obsolete.
498 (run-hooks 'outline-view-change-hook))
500 (defun outline-reveal-toggle-invisible (o revealp)
501 (save-excursion
502 (goto-char (overlay-start o))
503 (if (null revealp)
504 ;; When hiding the area again, we could just clean it up and let
505 ;; reveal do the rest, by simply doing:
506 ;; (remove-overlays (overlay-start o) (overlay-end o)
507 ;; 'invisible 'outline)
509 ;; That works fine as long as everything is in sync, but if the
510 ;; structure of the document is changed while revealing parts of it,
511 ;; the resulting behavior can be ugly. I.e. we need to make
512 ;; sure that we hide exactly a subtree.
513 (progn
514 (let ((end (overlay-end o)))
515 (delete-overlay o)
516 (while (progn
517 (hide-subtree)
518 (outline-next-visible-heading 1)
519 (and (not (eobp)) (< (point) end))))))
521 ;; When revealing, we just need to reveal sublevels. If point is
522 ;; inside one of the sublevels, reveal will call us again.
523 ;; But we need to preserve the original overlay.
524 (let ((o1 (copy-overlay o)))
525 (overlay-put o1 'invisible 'outline) ;We rehide some of the text.
526 (while (progn
527 (show-entry)
528 (show-children)
529 ;; Normally just the above is needed.
530 ;; But in odd cases, the above might fail to show anything.
531 ;; To avoid an infinite loop, we have to make sure that
532 ;; *something* gets shown.
533 (and (equal (overlay-start o) (overlay-start o1))
534 (< (point) (overlay-end o))
535 (= 0 (forward-line 1)))))
536 ;; If still nothing was shown, just kill the damn thing.
537 (when (equal (overlay-start o) (overlay-start o1))
538 ;; I've seen it happen at the end of buffer.
539 (delete-overlay o1))))))
541 ;; Function to be set as an outline-isearch-open-invisible' property
542 ;; to the overlay that makes the outline invisible (see
543 ;; `outline-flag-region').
544 (defun outline-isearch-open-invisible (overlay)
545 ;; We rely on the fact that isearch places point on the matched text.
546 (show-entry))
548 (defun hide-entry ()
549 "Hide the body directly following this heading."
550 (interactive)
551 (outline-back-to-heading)
552 (outline-end-of-heading)
553 (save-excursion
554 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
556 (defun show-entry ()
557 "Show the body directly following this heading.
558 Show the heading too, if it is currently invisible."
559 (interactive)
560 (save-excursion
561 (outline-back-to-heading t)
562 (outline-flag-region (1- (point))
563 (progn (outline-next-preface) (point)) nil)))
565 (defun hide-body ()
566 "Hide all of buffer except headings."
567 (interactive)
568 (hide-region-body (point-min) (point-max)))
570 (defun hide-region-body (start end)
571 "Hide all body lines in the region, but not headings."
572 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
573 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
574 ;; and run the hook finally.
575 (let (outline-view-change-hook)
576 (save-excursion
577 (save-restriction
578 (narrow-to-region start end)
579 (goto-char (point-min))
580 (if (outline-on-heading-p)
581 (outline-end-of-heading))
582 (while (not (eobp))
583 (outline-flag-region (point)
584 (progn (outline-next-preface) (point)) t)
585 (unless (eobp)
586 (forward-char (if (looking-at "\n\n") 2 1))
587 (outline-end-of-heading))))))
588 (run-hooks 'outline-view-change-hook))
590 (defun show-all ()
591 "Show all of the text in the buffer."
592 (interactive)
593 (outline-flag-region (point-min) (point-max) nil))
595 (defun hide-subtree ()
596 "Hide everything after this heading at deeper levels."
597 (interactive)
598 (outline-flag-subtree t))
600 (defun hide-leaves ()
601 "Hide all body after this heading at deeper levels."
602 (interactive)
603 (outline-back-to-heading)
604 (save-excursion
605 (outline-end-of-heading)
606 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
608 (defun show-subtree ()
609 "Show everything after this heading at deeper levels."
610 (interactive)
611 (outline-flag-subtree nil))
613 (defun hide-sublevels (levels)
614 "Hide everything but the top LEVELS levels of headers, in whole buffer."
615 (interactive "p")
616 (if (< levels 1)
617 (error "Must keep at least one level of headers"))
618 (setq levels (1- levels))
619 (let (outline-view-change-hook)
620 (save-excursion
621 (goto-char (point-min))
622 ;; Keep advancing to the next top-level heading.
623 (while (or (and (bobp) (outline-on-heading-p))
624 (outline-next-heading))
625 (let ((end (save-excursion (outline-end-of-subtree) (point))))
626 ;; Hide everything under that.
627 (outline-end-of-heading)
628 (outline-flag-region (point) end t)
629 ;; Show the first LEVELS levels under that.
630 (if (> levels 0)
631 (show-children levels))
632 ;; Move to the next, since we already found it.
633 (goto-char end)))))
634 (run-hooks 'outline-view-change-hook))
636 (defun hide-other ()
637 "Hide everything except current body and parent and top-level headings."
638 (interactive)
639 (hide-sublevels 1)
640 (let (outline-view-change-hook)
641 (save-excursion
642 (outline-back-to-heading t)
643 (show-entry)
644 (while (condition-case nil (progn (outline-up-heading 1) (not (bobp)))
645 (error nil))
646 (outline-flag-region (1- (point))
647 (save-excursion (forward-line 1) (point))
648 nil))))
649 (run-hooks 'outline-view-change-hook))
651 (defun outline-toggle-children ()
652 "Show or hide the current subtree depending on its current state."
653 (interactive)
654 (outline-back-to-heading)
655 (if (save-excursion
656 (end-of-line)
657 (not (outline-invisible-p)))
658 (hide-subtree)
659 (show-children)
660 (show-entry)))
662 (defun outline-flag-subtree (flag)
663 (save-excursion
664 (outline-back-to-heading)
665 (outline-end-of-heading)
666 (outline-flag-region (point)
667 (progn (outline-end-of-subtree) (point))
668 flag)))
670 (defun outline-end-of-subtree ()
671 (outline-back-to-heading)
672 (let ((opoint (point))
673 (first t)
674 (level (funcall outline-level)))
675 (while (and (not (eobp))
676 (or first (> (funcall outline-level) level)))
677 (setq first nil)
678 (outline-next-heading))
679 (if (bolp)
680 (progn
681 ;; Go to end of line before heading
682 (forward-char -1)
683 (if (bolp)
684 ;; leave blank line before heading
685 (forward-char -1))))))
687 (defun show-branches ()
688 "Show all subheadings of this heading, but not their bodies."
689 (interactive)
690 (show-children 1000))
692 (defun show-children (&optional level)
693 "Show all direct subheadings of this heading.
694 Prefix arg LEVEL is how many levels below the current level should be shown.
695 Default is enough to cause the following heading to appear."
696 (interactive "P")
697 (setq level
698 (if level (prefix-numeric-value level)
699 (save-excursion
700 (outline-back-to-heading)
701 (let ((start-level (funcall outline-level)))
702 (outline-next-heading)
703 (if (eobp)
705 (max 1 (- (funcall outline-level) start-level)))))))
706 (let (outline-view-change-hook)
707 (save-excursion
708 (save-restriction
709 (outline-back-to-heading)
710 (setq level (+ level (funcall outline-level)))
711 (narrow-to-region (point)
712 (progn (outline-end-of-subtree)
713 (if (eobp) (point-max) (1+ (point)))))
714 (goto-char (point-min))
715 (while (and (not (eobp))
716 (progn
717 (outline-next-heading)
718 (not (eobp))))
719 (if (<= (funcall outline-level) level)
720 (save-excursion
721 (outline-flag-region (save-excursion
722 (forward-char -1)
723 (if (bolp)
724 (forward-char -1))
725 (point))
726 (progn (outline-end-of-heading) (point))
727 nil)))))))
728 (run-hooks 'outline-view-change-hook))
732 (defun outline-up-heading (arg &optional invisible-ok)
733 "Move to the visible heading line of which the present line is a subheading.
734 With argument, move up ARG levels.
735 If INVISIBLE-OK is non-nil, also consider invisible lines."
736 (interactive "p")
737 (outline-back-to-heading invisible-ok)
738 (if (eq (funcall outline-level) 1)
739 (error "Already at top level of the outline"))
740 (while (and (> (funcall outline-level) 1)
741 (> arg 0)
742 (not (bobp)))
743 (let ((present-level (funcall outline-level)))
744 (while (and (not (< (funcall outline-level) present-level))
745 (not (bobp)))
746 (if invisible-ok
747 (outline-previous-heading)
748 (outline-previous-visible-heading 1)))
749 (setq arg (- arg 1)))))
751 (defun outline-forward-same-level (arg)
752 "Move forward to the ARG'th subheading at same level as this one.
753 Stop at the first and last subheadings of a superior heading."
754 (interactive "p")
755 (outline-back-to-heading)
756 (while (> arg 0)
757 (let ((point-to-move-to (save-excursion
758 (outline-get-next-sibling))))
759 (if point-to-move-to
760 (progn
761 (goto-char point-to-move-to)
762 (setq arg (1- arg)))
763 (progn
764 (setq arg 0)
765 (error "No following same-level heading"))))))
767 (defun outline-get-next-sibling ()
768 "Move to next heading of the same level, and return point or nil if none."
769 (let ((level (funcall outline-level)))
770 (outline-next-visible-heading 1)
771 (while (and (> (funcall outline-level) level)
772 (not (eobp)))
773 (outline-next-visible-heading 1))
774 (if (< (funcall outline-level) level)
776 (point))))
778 (defun outline-backward-same-level (arg)
779 "Move backward to the ARG'th subheading at same level as this one.
780 Stop at the first and last subheadings of a superior heading."
781 (interactive "p")
782 (outline-back-to-heading)
783 (while (> arg 0)
784 (let ((point-to-move-to (save-excursion
785 (outline-get-last-sibling))))
786 (if point-to-move-to
787 (progn
788 (goto-char point-to-move-to)
789 (setq arg (1- arg)))
790 (progn
791 (setq arg 0)
792 (error "No previous same-level heading"))))))
794 (defun outline-get-last-sibling ()
795 "Move to previous heading of the same level, and return point or nil if none."
796 (let ((level (funcall outline-level)))
797 (outline-previous-visible-heading 1)
798 (while (and (> (funcall outline-level) level)
799 (not (bobp)))
800 (outline-previous-visible-heading 1))
801 (if (< (funcall outline-level) level)
803 (point))))
805 (defun outline-headers-as-kill (beg end)
806 "Save the visible outline headers in region at the start of the kill ring.
808 Text shown between the headers isn't copied. Two newlines are
809 inserted between saved headers. Yanking the result may be a
810 convenient way to make a table of contents of the buffer."
811 (interactive "r")
812 (save-excursion
813 (save-restriction
814 (narrow-to-region beg end)
815 (goto-char (point-min))
816 (let ((buffer (current-buffer))
817 start end)
818 (with-temp-buffer
819 (with-current-buffer buffer
820 ;; Boundary condition: starting on heading:
821 (when (outline-on-heading-p)
822 (outline-back-to-heading)
823 (setq start (point)
824 end (progn (outline-end-of-heading)
825 (point)))
826 (insert-buffer-substring buffer start end)
827 (insert "\n\n")))
828 (let ((temp-buffer (current-buffer)))
829 (with-current-buffer buffer
830 (while (outline-next-heading)
831 (unless (outline-invisible-p)
832 (setq start (point)
833 end (progn (outline-end-of-heading) (point)))
834 (with-current-buffer temp-buffer
835 (insert-buffer-substring buffer start end)
836 (insert "\n\n"))))))
837 (kill-new (buffer-string)))))))
839 (provide 'outline)
840 (provide 'noutline)
842 ;;; outline.el ends here