(Top): Update subnode menus.
[emacs.git] / lisp / outline.el
blobeed84bd61c7bccacd00460ce0542c4344b12221e
1 ;;; outline.el --- outline mode commands for Emacs
3 ;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (defvar font-lock-warning-face)
43 (defgroup outlines nil
44 "Support for hierarchical outlining."
45 :prefix "outline-"
46 :group 'editing)
48 (defcustom outline-regexp "[*\^L]+"
49 "*Regular expression to match the beginning of a heading.
50 Any line whose beginning matches this regexp is considered to start a heading.
51 Note that Outline mode only checks this regexp at the start of a line,
52 so the regexp need not (and usually does not) start with `^'.
53 The recommended way to set this is with a Local Variables: list
54 in the file it applies to. See also `outline-heading-end-regexp'."
55 :type '(choice regexp (const nil))
56 :group 'outlines)
58 (defcustom outline-heading-end-regexp "\n"
59 "*Regular expression to match the end of a heading line.
60 You can assume that point is at the beginning of a heading when this
61 regexp is searched for. The heading ends at the end of the match.
62 The recommended way to set this is with a `Local Variables:' list
63 in the file it applies to."
64 :type 'regexp
65 :group 'outlines)
67 (defvar outline-mode-prefix-map
68 (let ((map (make-sparse-keymap)))
69 (define-key map "@" 'outline-mark-subtree)
70 (define-key map "\C-n" 'outline-next-visible-heading)
71 (define-key map "\C-p" 'outline-previous-visible-heading)
72 (define-key map "\C-i" 'show-children)
73 (define-key map "\C-s" 'show-subtree)
74 (define-key map "\C-d" 'hide-subtree)
75 (define-key map "\C-u" 'outline-up-heading)
76 (define-key map "\C-f" 'outline-forward-same-level)
77 (define-key map "\C-b" 'outline-backward-same-level)
78 (define-key map "\C-t" 'hide-body)
79 (define-key map "\C-a" 'show-all)
80 (define-key map "\C-c" 'hide-entry)
81 (define-key map "\C-e" 'show-entry)
82 (define-key map "\C-l" 'hide-leaves)
83 (define-key map "\C-k" 'show-branches)
84 (define-key map "\C-q" 'hide-sublevels)
85 (define-key map "\C-o" 'hide-other)
86 (define-key map "\C-^" 'outline-move-subtree-up)
87 (define-key map "\C-v" 'outline-move-subtree-down)
88 (define-key map [(control ?<)] 'outline-promote)
89 (define-key map [(control ?>)] 'outline-demote)
90 (define-key map "\C-m" 'outline-insert-heading)
91 ;; Where to bind outline-cycle ?
92 map))
94 (defvar outline-mode-menu-bar-map
95 (let ((map (make-sparse-keymap)))
97 (define-key map [hide] (cons "Hide" (make-sparse-keymap "Hide")))
99 (define-key map [hide hide-other] '("Hide Other" . hide-other))
100 (define-key map [hide hide-sublevels] '("Hide Sublevels" . hide-sublevels))
101 (define-key map [hide hide-subtree] '("Hide Subtree" . hide-subtree))
102 (define-key map [hide hide-entry] '("Hide Entry" . hide-entry))
103 (define-key map [hide hide-body] '("Hide Body" . hide-body))
104 (define-key map [hide hide-leaves] '("Hide Leaves" . hide-leaves))
106 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
108 (define-key map [show show-subtree] '("Show Subtree" . show-subtree))
109 (define-key map [show show-children] '("Show Children" . show-children))
110 (define-key map [show show-branches] '("Show Branches" . show-branches))
111 (define-key map [show show-entry] '("Show Entry" . show-entry))
112 (define-key map [show show-all] '("Show All" . show-all))
114 (define-key map [headings]
115 (cons "Headings" (make-sparse-keymap "Headings")))
117 (define-key map [headings demote-subtree]
118 '(menu-item "Demote subtree" outline-demote))
119 (define-key map [headings promote-subtree]
120 '(menu-item "Promote subtree" outline-promote))
121 (define-key map [headings move-subtree-down]
122 '(menu-item "Move subtree down" outline-move-subtree-down))
123 (define-key map [headings move-subtree-up]
124 '(menu-item "Move subtree up" outline-move-subtree-up))
125 (define-key map [headings copy]
126 '(menu-item "Copy to kill ring" outline-headers-as-kill
127 :enable mark-active))
128 (define-key map [headings outline-insert-heading]
129 '("New heading" . outline-insert-heading))
130 (define-key map [headings outline-backward-same-level]
131 '("Previous Same Level" . outline-backward-same-level))
132 (define-key map [headings outline-forward-same-level]
133 '("Next Same Level" . outline-forward-same-level))
134 (define-key map [headings outline-previous-visible-heading]
135 '("Previous" . outline-previous-visible-heading))
136 (define-key map [headings outline-next-visible-heading]
137 '("Next" . outline-next-visible-heading))
138 (define-key map [headings outline-up-heading]
139 '("Up" . outline-up-heading))
140 map))
142 (defvar outline-minor-mode-menu-bar-map
143 (let ((map (make-sparse-keymap)))
144 (define-key map [outline]
145 (cons "Outline"
146 (nconc (make-sparse-keymap "Outline")
147 ;; Remove extra separator
148 (cdr
149 ;; Flatten the major mode's menus into a single menu.
150 (apply 'append
151 (mapcar (lambda (x)
152 (if (consp x)
153 ;; Add a separator between each
154 ;; part of the unified menu.
155 (cons '(--- "---") (cdr x))))
156 outline-mode-menu-bar-map))))))
157 map))
160 (defvar outline-mode-map
161 (let ((map (make-sparse-keymap)))
162 (define-key map "\C-c" outline-mode-prefix-map)
163 (define-key map [menu-bar] outline-mode-menu-bar-map)
164 map))
166 (defvar outline-font-lock-keywords
167 '(;;
168 ;; Highlight headings according to the level.
169 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
170 0 '(outline-font-lock-face) nil t)))
171 "Additional expressions to highlight in Outline mode.")
173 (defface outline-1
174 '((t :inherit font-lock-function-name-face))
175 "Level 1."
176 :group 'outlines)
178 (defface outline-2
179 '((t :inherit font-lock-variable-name-face))
180 "Level 2."
181 :group 'outlines)
183 (defface outline-3
184 '((t :inherit font-lock-keyword-face))
185 "Level 3."
186 :group 'outlines)
188 (defface outline-4
189 '((t :inherit font-lock-builtin-face))
190 "Level 4."
191 :group 'outlines)
193 (defface outline-5
194 '((t :inherit font-lock-comment-face))
195 "Level 5."
196 :group 'outlines)
198 (defface outline-6
199 '((t :inherit font-lock-constant-face))
200 "Level 6."
201 :group 'outlines)
203 (defface outline-7
204 '((t :inherit font-lock-type-face))
205 "Level 7."
206 :group 'outlines)
208 (defface outline-8
209 '((t :inherit font-lock-string-face))
210 "Level 8."
211 :group 'outlines)
213 (defvar outline-font-lock-faces
214 [outline-1 outline-2 outline-3 outline-4
215 outline-5 outline-6 outline-7 outline-8])
217 (defvar outline-font-lock-levels nil)
218 (make-variable-buffer-local 'outline-font-lock-levels)
220 (defun outline-font-lock-face ()
221 ;; (save-excursion
222 ;; (outline-back-to-heading t)
223 ;; (let* ((count 0)
224 ;; (start-level (funcall outline-level))
225 ;; (level start-level)
226 ;; face-level)
227 ;; (while (not (setq face-level
228 ;; (if (or (bobp) (eq level 1)) 0
229 ;; (cdr (assq level outline-font-lock-levels)))))
230 ;; (outline-up-heading 1 t)
231 ;; (setq count (1+ count))
232 ;; (setq level (funcall outline-level)))
233 ;; ;; Remember for later.
234 ;; (unless (zerop count)
235 ;; (setq face-level (+ face-level count))
236 ;; (push (cons start-level face-level) outline-font-lock-levels))
237 ;; (condition-case nil
238 ;; (aref outline-font-lock-faces face-level)
239 ;; (error font-lock-warning-face))))
240 (save-excursion
241 (goto-char (match-beginning 0))
242 (looking-at outline-regexp)
243 (condition-case nil
244 (aref outline-font-lock-faces (1- (funcall outline-level)))
245 (error font-lock-warning-face))))
247 (defvar outline-view-change-hook nil
248 "Normal hook to be run after outline visibility changes.")
250 (defvar outline-mode-hook nil
251 "*This hook is run when outline mode starts.")
253 (defvar outline-blank-line nil
254 "*Non-nil means to leave unhidden blank line before heading.")
256 ;;;###autoload
257 (define-derived-mode outline-mode text-mode "Outline"
258 "Set major mode for editing outlines with selective display.
259 Headings are lines which start with asterisks: one for major headings,
260 two for subheadings, etc. Lines not starting with asterisks are body lines.
262 Body text or subheadings under a heading can be made temporarily
263 invisible, or visible again. Invisible lines are attached to the end
264 of the heading, so they move with it, if the line is killed and yanked
265 back. A heading with text hidden under it is marked with an ellipsis (...).
267 Commands:\\<outline-mode-map>
268 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
269 \\[outline-previous-visible-heading] outline-previous-visible-heading
270 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
271 \\[outline-backward-same-level] outline-backward-same-level
272 \\[outline-up-heading] outline-up-heading move from subheading to heading
274 \\[hide-body] make all text invisible (not headings).
275 \\[show-all] make everything in buffer visible.
276 \\[hide-sublevels] make only the first N levels of headers visible.
278 The remaining commands are used when point is on a heading line.
279 They apply to some of the body or subheadings of that heading.
280 \\[hide-subtree] hide-subtree make body and subheadings invisible.
281 \\[show-subtree] show-subtree make body and subheadings visible.
282 \\[show-children] show-children make direct subheadings visible.
283 No effect on body, or subheadings 2 or more levels down.
284 With arg N, affects subheadings N levels down.
285 \\[hide-entry] make immediately following body invisible.
286 \\[show-entry] make it visible.
287 \\[hide-leaves] make body under heading and under its subheadings invisible.
288 The subheadings remain visible.
289 \\[show-branches] make all subheadings at all levels visible.
291 The variable `outline-regexp' can be changed to control what is a heading.
292 A line is a heading if `outline-regexp' matches something at the
293 beginning of the line. The longer the match, the deeper the level.
295 Turning on outline mode calls the value of `text-mode-hook' and then of
296 `outline-mode-hook', if they are non-nil."
297 (make-local-variable 'line-move-ignore-invisible)
298 (setq line-move-ignore-invisible t)
299 ;; Cause use of ellipses for invisible text.
300 (add-to-invisibility-spec '(outline . t))
301 (set (make-local-variable 'paragraph-start)
302 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
303 ;; Inhibit auto-filling of header lines.
304 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
305 (set (make-local-variable 'paragraph-separate)
306 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
307 (set (make-local-variable 'font-lock-defaults)
308 '(outline-font-lock-keywords t nil nil backward-paragraph))
309 (setq imenu-generic-expression
310 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
311 (add-hook 'change-major-mode-hook 'show-all nil t))
313 (defcustom outline-minor-mode-prefix "\C-c@"
314 "*Prefix key to use for Outline commands in Outline minor mode.
315 The value of this variable is checked as part of loading Outline mode.
316 After that, changing the prefix key requires manipulating keymaps."
317 :type 'string
318 :group 'outlines)
320 ;;;###autoload
321 (define-minor-mode outline-minor-mode
322 "Toggle Outline minor mode.
323 With arg, turn Outline minor mode on if arg is positive, off otherwise.
324 See the command `outline-mode' for more information on this mode."
325 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
326 (cons outline-minor-mode-prefix outline-mode-prefix-map))
327 :group 'outlines
328 (if outline-minor-mode
329 (progn
330 ;; Turn off this mode if we change major modes.
331 (add-hook 'change-major-mode-hook
332 (lambda () (outline-minor-mode -1))
333 nil t)
334 (set (make-local-variable 'line-move-ignore-invisible) t)
335 ;; Cause use of ellipses for invisible text.
336 (add-to-invisibility-spec '(outline . t)))
337 (setq line-move-ignore-invisible nil)
338 ;; Cause use of ellipses for invisible text.
339 (remove-from-invisibility-spec '(outline . t))
340 ;; When turning off outline mode, get rid of any outline hiding.
341 (show-all)))
343 (defvar outline-level 'outline-level
344 "*Function of no args to compute a header's nesting level in an outline.
345 It can assume point is at the beginning of a header line and that the match
346 data reflects the `outline-regexp'.")
348 (defvar outline-heading-alist ()
349 "Alist associating a heading for every possible level.
350 Each entry is of the form (HEADING . LEVEL).
351 This alist is used two ways: to find the heading corresponding to
352 a given level and to find the level of a given heading.
353 If a mode or document needs several sets of outline headings (for example
354 numbered and unnumbered sections), list them set by set and sorted by level
355 within each set. For example in texinfo mode:
357 (setq outline-heading-alist
358 '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
359 (\"@subsubsection\" . 5)
360 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
361 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
362 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
363 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
365 Instead of sorting the entries in each set, you can also separate the
366 sets with nil.")
367 (make-variable-buffer-local 'outline-heading-alist)
369 ;; This used to count columns rather than characters, but that made ^L
370 ;; appear to be at level 2 instead of 1. Columns would be better for
371 ;; tab handling, but the default regexp doesn't use tabs, and anyone
372 ;; who changes the regexp can also redefine the outline-level variable
373 ;; as appropriate.
374 (defun outline-level ()
375 "Return the depth to which a statement is nested in the outline.
376 Point must be at the beginning of a header line.
377 This is actually either the level specified in `outline-heading-alist'
378 or else the number of characters matched by `outline-regexp'."
379 (or (cdr (assoc (match-string 0) outline-heading-alist))
380 (- (match-end 0) (match-beginning 0))))
382 (defun outline-next-preface ()
383 "Skip forward to just before the next heading line.
384 If there's no following heading line, stop before the newline
385 at the end of the buffer."
386 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
387 nil 'move)
388 (goto-char (match-beginning 0)))
389 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
390 (forward-char -1)))
392 (defun outline-next-heading ()
393 "Move to the next (possibly invisible) heading line."
394 (interactive)
395 ;; Make sure we don't match the heading we're at.
396 (if (and (bolp) (not (eobp))) (forward-char 1))
397 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
398 nil 'move)
399 (goto-char (match-beginning 0))))
401 (defun outline-previous-heading ()
402 "Move to the previous (possibly invisible) heading line."
403 (interactive)
404 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
405 nil 'move))
407 (defsubst outline-invisible-p (&optional pos)
408 "Non-nil if the character after point is invisible."
409 (get-char-property (or pos (point)) 'invisible))
411 (defun outline-visible ()
412 (not (outline-invisible-p)))
413 (make-obsolete 'outline-visible 'outline-invisible-p)
415 (defun outline-back-to-heading (&optional invisible-ok)
416 "Move to previous heading line, or beg of this line if it's a heading.
417 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
418 (beginning-of-line)
419 (or (outline-on-heading-p invisible-ok)
420 (let (found)
421 (save-excursion
422 (while (not found)
423 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
424 nil t)
425 (error "before first heading"))
426 (setq found (and (or invisible-ok (not (outline-invisible-p)))
427 (point)))))
428 (goto-char found)
429 found)))
431 (defun outline-on-heading-p (&optional invisible-ok)
432 "Return t if point is on a (visible) heading line.
433 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
434 (save-excursion
435 (beginning-of-line)
436 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
437 (looking-at outline-regexp))))
439 (defun outline-insert-heading ()
440 "Insert a new heading at same depth at point."
441 (interactive)
442 (let ((head (save-excursion
443 (condition-case nil
444 (outline-back-to-heading)
445 (error (outline-next-heading)))
446 (if (eobp)
447 (or (caar outline-heading-alist) "")
448 (match-string 0)))))
449 (unless (or (string-match "[ \t]\\'" head)
450 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
451 (concat head " "))))
452 (setq head (concat head " ")))
453 (unless (bolp) (end-of-line) (newline))
454 (insert head)
455 (unless (eolp)
456 (save-excursion (newline-and-indent)))
457 (run-hooks 'outline-insert-heading-hook)))
459 (defun outline-invent-heading (head up)
460 (save-match-data
461 ;; Let's try to invent one by repeating or deleting the last char.
462 (let ((new-head (if up (substring head 0 -1)
463 (concat head (substring head -1)))))
464 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
465 new-head)
466 ;; Why bother checking that it is indeed higher/lower level ?
467 new-head
468 ;; Didn't work, so ask what to do.
469 (read-string (format "%s heading for `%s': "
470 (if up "Parent" "Demoted") head)
471 head nil nil t)))))
473 (defun outline-promote (&optional children)
474 "Promote headings higher up the tree.
475 If prefix argument CHILDREN is given, promote also all the children.
476 If the region is active in `transient-mark-mode', promote all headings
477 in the region."
478 (interactive
479 (list (if (and transient-mark-mode mark-active) 'region
480 (outline-back-to-heading)
481 (if current-prefix-arg nil 'subtree))))
482 (cond
483 ((eq children 'region)
484 (outline-map-region 'outline-promote (region-beginning) (region-end)))
485 (children
486 (outline-map-region 'outline-promote
487 (point)
488 (save-excursion (outline-get-next-sibling) (point))))
490 (outline-back-to-heading t)
491 (let* ((head (match-string-no-properties 0))
492 (level (save-match-data (funcall outline-level)))
493 (up-head (or (outline-head-from-level (1- level) head)
494 ;; Use the parent heading, if it is really
495 ;; one level less.
496 (save-excursion
497 (save-match-data
498 (outline-up-heading 1 t)
499 (and (= (1- level) (funcall outline-level))
500 (match-string-no-properties 0))))
501 ;; Bummer!! There is no lower level heading.
502 (outline-invent-heading head 'up))))
504 (unless (rassoc level outline-heading-alist)
505 (push (cons head level) outline-heading-alist))
507 (replace-match up-head nil t)))))
509 (defun outline-demote (&optional children)
510 "Demote headings lower down the tree.
511 If prefix argument CHILDREN is given, demote also all the children.
512 If the region is active in `transient-mark-mode', demote all headings
513 in the region."
514 (interactive
515 (list (if (and transient-mark-mode mark-active) 'region
516 (outline-back-to-heading)
517 (if current-prefix-arg nil 'subtree))))
518 (cond
519 ((eq children 'region)
520 (outline-map-region 'outline-demote (region-beginning) (region-end)))
521 (children
522 (outline-map-region 'outline-demote
523 (point)
524 (save-excursion (outline-get-next-sibling) (point))))
526 (let* ((head (match-string-no-properties 0))
527 (level (save-match-data (funcall outline-level)))
528 (down-head
529 (or (outline-head-from-level (1+ level) head)
530 (save-excursion
531 (save-match-data
532 (while (and (progn (outline-next-heading) (not (eobp)))
533 (<= (funcall outline-level) level)))
534 (when (eobp)
535 ;; Try again from the beginning of the buffer.
536 (goto-char (point-min))
537 (while (and (progn (outline-next-heading) (not (eobp)))
538 (<= (funcall outline-level) level))))
539 (unless (eobp)
540 (looking-at outline-regexp)
541 (match-string-no-properties 0))))
542 ;; Bummer!! There is no higher-level heading in the buffer.
543 (outline-invent-heading head nil))))
545 (unless (rassoc level outline-heading-alist)
546 (push (cons head level) outline-heading-alist))
547 (replace-match down-head nil t)))))
549 (defun outline-head-from-level (level head &optional alist)
550 "Get new heading with level LEVEL from ALIST.
551 If there are no such entries, return nil.
552 ALIST defaults to `outline-heading-alist'.
553 Similar to (car (rassoc LEVEL ALIST)).
554 If there are several different entries with same new level, choose
555 the one with the smallest distance to the assocation of HEAD in the alist.
556 This makes it possible for promotion to work in modes with several
557 independent sets of headings (numbered, unnumbered, appendix...)"
558 (unless alist (setq alist outline-heading-alist))
559 (let ((l (rassoc level alist))
560 ll h hl l2 l2l)
561 (cond
562 ((null l) nil)
563 ;; If there's no HEAD after L, any other entry for LEVEL after L
564 ;; can't be much better than L.
565 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
566 ;; If there's no other entry for LEVEL, just keep L.
567 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
568 ;; Now we have L, L2, and H: see if L2 seems better than L.
569 ;; If H is after L2, L2 is better.
570 ((memq h (setq l2l (memq l2 (cdr ll))))
571 (outline-head-from-level level head l2l))
572 ;; Now we have H between L and L2.
573 ;; If there's a separator between L and H, prefer L2.
574 ((memq h (memq nil ll))
575 (outline-head-from-level level head l2l))
576 ;; If there's a separator between L2 and H, prefer L.
577 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
578 ;; No separator between L and L2, check the distance.
579 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
580 (outline-head-from-level level head l2l))
581 ;; If all else fails, just keep L.
582 (t (car l)))))
584 (defun outline-map-region (fun beg end)
585 "Call FUN for every heading between BEG and END.
586 When FUN is called, point is at the beginning of the heading and
587 the match data is set appropriately."
588 (save-excursion
589 (setq end (copy-marker end))
590 (goto-char beg)
591 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
592 (goto-char (match-beginning 0))
593 (funcall fun)
594 (while (and (progn
595 (outline-next-heading)
596 (< (point) end))
597 (not (eobp)))
598 (funcall fun)))))
600 ;; Vertical tree motion
602 (defun outline-move-subtree-up (&optional arg)
603 "Move the currrent subtree up past ARG headlines of the same level."
604 (interactive "p")
605 (outline-move-subtree-down (- arg)))
607 (defun outline-move-subtree-down (&optional arg)
608 "Move the currrent subtree down past ARG headlines of the same level."
609 (interactive "p")
610 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
611 'outline-get-last-sibling))
612 (ins-point (make-marker))
613 (cnt (abs arg))
614 beg end folded)
615 ;; Select the tree
616 (outline-back-to-heading)
617 (setq beg (point))
618 (save-match-data
619 (save-excursion (outline-end-of-heading)
620 (setq folded (outline-invisible-p)))
621 (outline-end-of-subtree))
622 (if (= (char-after) ?\n) (forward-char 1))
623 (setq end (point))
624 ;; Find insertion point, with error handling
625 (goto-char beg)
626 (while (> cnt 0)
627 (or (funcall movfunc)
628 (progn (goto-char beg)
629 (error "Cannot move past superior level")))
630 (setq cnt (1- cnt)))
631 (if (> arg 0)
632 ;; Moving forward - still need to move over subtree
633 (progn (outline-end-of-subtree)
634 (if (= (char-after) ?\n) (forward-char 1))))
635 (move-marker ins-point (point))
636 (insert (delete-and-extract-region beg end))
637 (goto-char ins-point)
638 (if folded (hide-subtree))
639 (move-marker ins-point nil)))
641 (defun outline-end-of-heading ()
642 (if (re-search-forward outline-heading-end-regexp nil 'move)
643 (forward-char -1)))
645 (defun outline-next-visible-heading (arg)
646 "Move to the next visible heading line.
647 With argument, repeats or can move backward if negative.
648 A heading line is one that starts with a `*' (or that
649 `outline-regexp' matches)."
650 (interactive "p")
651 (if (< arg 0)
652 (beginning-of-line)
653 (end-of-line))
654 (while (and (not (bobp)) (< arg 0))
655 (while (and (not (bobp))
656 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
657 nil 'move)
658 (outline-invisible-p)))
659 (setq arg (1+ arg)))
660 (while (and (not (eobp)) (> arg 0))
661 (while (and (not (eobp))
662 (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
663 nil 'move)
664 (outline-invisible-p (match-beginning 0))))
665 (setq arg (1- arg)))
666 (beginning-of-line))
668 (defun outline-previous-visible-heading (arg)
669 "Move to the previous heading line.
670 With argument, repeats or can move forward if negative.
671 A heading line is one that starts with a `*' (or that
672 `outline-regexp' matches)."
673 (interactive "p")
674 (outline-next-visible-heading (- arg)))
676 (defun outline-mark-subtree ()
677 "Mark the current subtree in an outlined document.
678 This puts point at the start of the current subtree, and mark at the end."
679 (interactive)
680 (let ((beg))
681 (if (outline-on-heading-p)
682 ;; we are already looking at a heading
683 (beginning-of-line)
684 ;; else go back to previous heading
685 (outline-previous-visible-heading 1))
686 (setq beg (point))
687 (outline-end-of-subtree)
688 (push-mark (point) nil t)
689 (goto-char beg)))
692 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
693 (defun outline-flag-region (from to flag)
694 "Hide or show lines from FROM to TO, according to FLAG.
695 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
696 (remove-overlays from to 'invisible 'outline)
697 (when flag
698 (let ((o (make-overlay from to)))
699 (overlay-put o 'invisible 'outline)
700 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
701 ;; Seems only used by lazy-lock. I.e. obsolete.
702 (run-hooks 'outline-view-change-hook))
704 (defun outline-reveal-toggle-invisible (o hidep)
705 (save-excursion
706 (goto-char (overlay-start o))
707 (if hidep
708 ;; When hiding the area again, we could just clean it up and let
709 ;; reveal do the rest, by simply doing:
710 ;; (remove-overlays (overlay-start o) (overlay-end o)
711 ;; 'invisible 'outline)
713 ;; That works fine as long as everything is in sync, but if the
714 ;; structure of the document is changed while revealing parts of it,
715 ;; the resulting behavior can be ugly. I.e. we need to make
716 ;; sure that we hide exactly a subtree.
717 (progn
718 (let ((end (overlay-end o)))
719 (delete-overlay o)
720 (while (progn
721 (hide-subtree)
722 (outline-next-visible-heading 1)
723 (and (not (eobp)) (< (point) end))))))
725 ;; When revealing, we just need to reveal sublevels. If point is
726 ;; inside one of the sublevels, reveal will call us again.
727 ;; But we need to preserve the original overlay.
728 (let ((o1 (copy-overlay o)))
729 (overlay-put o 'invisible nil) ;Show (most of) the text.
730 (while (progn
731 (show-entry)
732 (show-children)
733 ;; Normally just the above is needed.
734 ;; But in odd cases, the above might fail to show anything.
735 ;; To avoid an infinite loop, we have to make sure that
736 ;; *something* gets shown.
737 (and (equal (overlay-start o) (overlay-start o1))
738 (< (point) (overlay-end o))
739 (= 0 (forward-line 1)))))
740 ;; If still nothing was shown, just kill the damn thing.
741 (when (equal (overlay-start o) (overlay-start o1))
742 ;; I've seen it happen at the end of buffer.
743 (delete-overlay o1))))))
745 ;; Function to be set as an outline-isearch-open-invisible' property
746 ;; to the overlay that makes the outline invisible (see
747 ;; `outline-flag-region').
748 (defun outline-isearch-open-invisible (overlay)
749 ;; We rely on the fact that isearch places point on the matched text.
750 (show-entry))
752 (defun hide-entry ()
753 "Hide the body directly following this heading."
754 (interactive)
755 (save-excursion
756 (outline-back-to-heading)
757 (outline-end-of-heading)
758 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
760 (defun show-entry ()
761 "Show the body directly following this heading.
762 Show the heading too, if it is currently invisible."
763 (interactive)
764 (save-excursion
765 (outline-back-to-heading t)
766 (outline-flag-region (1- (point))
767 (progn (outline-next-preface) (point)) nil)))
769 (defun hide-body ()
770 "Hide all body lines in buffer, leaving all headings visible."
771 (interactive)
772 (hide-region-body (point-min) (point-max)))
774 (defun hide-region-body (start end)
775 "Hide all body lines in the region, but not headings."
776 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
777 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
778 ;; and run the hook finally.
779 (let (outline-view-change-hook)
780 (save-excursion
781 (save-restriction
782 (narrow-to-region start end)
783 (goto-char (point-min))
784 (if (outline-on-heading-p)
785 (outline-end-of-heading)
786 (outline-next-preface))
787 (while (not (eobp))
788 (outline-flag-region (point)
789 (progn (outline-next-preface) (point)) t)
790 (unless (eobp)
791 (forward-char (if (looking-at "\n\n") 2 1))
792 (outline-end-of-heading))))))
793 (run-hooks 'outline-view-change-hook))
795 (defun show-all ()
796 "Show all of the text in the buffer."
797 (interactive)
798 (outline-flag-region (point-min) (point-max) nil))
800 (defun hide-subtree ()
801 "Hide everything after this heading at deeper levels."
802 (interactive)
803 (outline-flag-subtree t))
805 (defun hide-leaves ()
806 "Hide the body after this heading and at deeper levels."
807 (interactive)
808 (save-excursion
809 (outline-back-to-heading)
810 ;; Turned off to fix bug reported by Otto Maddox on 22 Nov 2005.
811 ;; (outline-end-of-heading)
812 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
814 (defun show-subtree ()
815 "Show everything after this heading at deeper levels."
816 (interactive)
817 (outline-flag-subtree nil))
819 (defun outline-show-heading ()
820 "Show the current heading and move to its end."
821 (outline-flag-region (- (point)
822 (if (bobp) 0
823 (if (and outline-blank-line
824 (eq (char-before (1- (point))) ?\n))
825 2 1)))
826 (progn (outline-end-of-heading) (point))
827 nil))
829 (defun hide-sublevels (levels)
830 "Hide everything but the top LEVELS levels of headers, in whole buffer."
831 (interactive "p")
832 (if (< levels 1)
833 (error "Must keep at least one level of headers"))
834 (let (outline-view-change-hook)
835 (save-excursion
836 (goto-char (point-min))
837 ;; Skip the prelude, if any.
838 (unless (outline-on-heading-p t) (outline-next-heading))
839 ;; First hide everything.
840 (outline-flag-region (point) (point-max) t)
841 ;; Then unhide the top level headers.
842 (outline-map-region
843 (lambda ()
844 (if (<= (funcall outline-level) levels)
845 (outline-show-heading)))
846 (point) (point-max))))
847 (run-hooks 'outline-view-change-hook))
849 (defun hide-other ()
850 "Hide everything except current body and parent and top-level headings."
851 (interactive)
852 (hide-sublevels 1)
853 (let (outline-view-change-hook)
854 (save-excursion
855 (outline-back-to-heading t)
856 (show-entry)
857 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
858 (error nil))
859 (outline-flag-region (1- (point))
860 (save-excursion (forward-line 1) (point))
861 nil))))
862 (run-hooks 'outline-view-change-hook))
864 (defun outline-toggle-children ()
865 "Show or hide the current subtree depending on its current state."
866 (interactive)
867 (save-excursion
868 (outline-back-to-heading)
869 (if (not (outline-invisible-p (line-end-position)))
870 (hide-subtree)
871 (show-children)
872 (show-entry))))
874 (defun outline-flag-subtree (flag)
875 (save-excursion
876 (outline-back-to-heading)
877 (outline-end-of-heading)
878 (outline-flag-region (point)
879 (progn (outline-end-of-subtree) (point))
880 flag)))
882 (defun outline-end-of-subtree ()
883 (outline-back-to-heading)
884 (let ((first t)
885 (level (funcall outline-level)))
886 (while (and (not (eobp))
887 (or first (> (funcall outline-level) level)))
888 (setq first nil)
889 (outline-next-heading))
890 (if (bolp)
891 (progn
892 ;; Go to end of line before heading
893 (forward-char -1)
894 (if (and outline-blank-line (bolp))
895 ;; leave blank line before heading
896 (forward-char -1))))))
898 (defun show-branches ()
899 "Show all subheadings of this heading, but not their bodies."
900 (interactive)
901 (show-children 1000))
903 (defun show-children (&optional level)
904 "Show all direct subheadings of this heading.
905 Prefix arg LEVEL is how many levels below the current level should be shown.
906 Default is enough to cause the following heading to appear."
907 (interactive "P")
908 (setq level
909 (if level (prefix-numeric-value level)
910 (save-excursion
911 (outline-back-to-heading)
912 (let ((start-level (funcall outline-level)))
913 (outline-next-heading)
914 (if (eobp)
916 (max 1 (- (funcall outline-level) start-level)))))))
917 (let (outline-view-change-hook)
918 (save-excursion
919 (outline-back-to-heading)
920 (setq level (+ level (funcall outline-level)))
921 (outline-map-region
922 (lambda ()
923 (if (<= (funcall outline-level) level)
924 (outline-show-heading)))
925 (point)
926 (progn (outline-end-of-subtree)
927 (if (eobp) (point-max) (1+ (point)))))))
928 (run-hooks 'outline-view-change-hook))
932 (defun outline-up-heading (arg &optional invisible-ok)
933 "Move to the visible heading line of which the present line is a subheading.
934 With argument, move up ARG levels.
935 If INVISIBLE-OK is non-nil, also consider invisible lines."
936 (interactive "p")
937 (and (eq this-command 'outline-up-heading)
938 (or (eq last-command 'outline-up-heading) (push-mark)))
939 (outline-back-to-heading invisible-ok)
940 (let ((start-level (funcall outline-level)))
941 (if (eq start-level 1)
942 (error "Already at top level of the outline"))
943 (while (and (> start-level 1) (> arg 0) (not (bobp)))
944 (let ((level start-level))
945 (while (not (or (< level start-level) (bobp)))
946 (if invisible-ok
947 (outline-previous-heading)
948 (outline-previous-visible-heading 1))
949 (setq level (funcall outline-level)))
950 (setq start-level level))
951 (setq arg (- arg 1))))
952 (looking-at outline-regexp))
954 (defun outline-forward-same-level (arg)
955 "Move forward to the ARG'th subheading at same level as this one.
956 Stop at the first and last subheadings of a superior heading."
957 (interactive "p")
958 (outline-back-to-heading)
959 (while (> arg 0)
960 (let ((point-to-move-to (save-excursion
961 (outline-get-next-sibling))))
962 (if point-to-move-to
963 (progn
964 (goto-char point-to-move-to)
965 (setq arg (1- arg)))
966 (progn
967 (setq arg 0)
968 (error "No following same-level heading"))))))
970 (defun outline-get-next-sibling ()
971 "Move to next heading of the same level, and return point or nil if none."
972 (let ((level (funcall outline-level)))
973 (outline-next-visible-heading 1)
974 (while (and (not (eobp)) (> (funcall outline-level) level))
975 (outline-next-visible-heading 1))
976 (if (or (eobp) (< (funcall outline-level) level))
978 (point))))
980 (defun outline-backward-same-level (arg)
981 "Move backward to the ARG'th subheading at same level as this one.
982 Stop at the first and last subheadings of a superior heading."
983 (interactive "p")
984 (outline-back-to-heading)
985 (while (> arg 0)
986 (let ((point-to-move-to (save-excursion
987 (outline-get-last-sibling))))
988 (if point-to-move-to
989 (progn
990 (goto-char point-to-move-to)
991 (setq arg (1- arg)))
992 (progn
993 (setq arg 0)
994 (error "No previous same-level heading"))))))
996 (defun outline-get-last-sibling ()
997 "Move to previous heading of the same level, and return point or nil if none."
998 (let ((level (funcall outline-level)))
999 (outline-previous-visible-heading 1)
1000 (while (and (> (funcall outline-level) level)
1001 (not (bobp)))
1002 (outline-previous-visible-heading 1))
1003 (if (< (funcall outline-level) level)
1005 (point))))
1007 (defun outline-headers-as-kill (beg end)
1008 "Save the visible outline headers in region at the start of the kill ring.
1010 Text shown between the headers isn't copied. Two newlines are
1011 inserted between saved headers. Yanking the result may be a
1012 convenient way to make a table of contents of the buffer."
1013 (interactive "r")
1014 (save-excursion
1015 (save-restriction
1016 (narrow-to-region beg end)
1017 (goto-char (point-min))
1018 (let ((buffer (current-buffer))
1019 start end)
1020 (with-temp-buffer
1021 (with-current-buffer buffer
1022 ;; Boundary condition: starting on heading:
1023 (when (outline-on-heading-p)
1024 (outline-back-to-heading)
1025 (setq start (point)
1026 end (progn (outline-end-of-heading)
1027 (point)))
1028 (insert-buffer-substring buffer start end)
1029 (insert "\n\n")))
1030 (let ((temp-buffer (current-buffer)))
1031 (with-current-buffer buffer
1032 (while (outline-next-heading)
1033 (unless (outline-invisible-p)
1034 (setq start (point)
1035 end (progn (outline-end-of-heading) (point)))
1036 (with-current-buffer temp-buffer
1037 (insert-buffer-substring buffer start end)
1038 (insert "\n\n"))))))
1039 (kill-new (buffer-string)))))))
1041 (provide 'outline)
1042 (provide 'noutline)
1044 ;; arch-tag: 1724410e-7d4d-4f46-b801-49e18171e874
1045 ;;; outline.el ends here