(mail-reply-buffer): Add defvar.
[emacs.git] / lisp / outline.el
blob714e7ec02ea6d8ad003a3c7a760eaf9b87f9ab4c
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 (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-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 ?
89 map))
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))
137 map))
139 (defvar outline-minor-mode-menu-bar-map
140 (let ((map (make-sparse-keymap)))
141 (define-key map [outline]
142 (cons "Outline"
143 (nconc (make-sparse-keymap "Outline")
144 ;; Remove extra separator
145 (cdr
146 ;; Flatten the major mode's menus into a single menu.
147 (apply 'append
148 (mapcar (lambda (x)
149 (if (consp x)
150 ;; Add a separator between each
151 ;; part of the unified menu.
152 (cons '(--- "---") (cdr x))))
153 outline-mode-menu-bar-map))))))
154 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)
161 map))
163 (defvar outline-font-lock-keywords
164 '(;;
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.")
170 (defface outline-1
171 '((t :inherit font-lock-function-name-face))
172 "Level 1."
173 :group 'outlines)
175 (defface outline-2
176 '((t :inherit font-lock-variable-name-face))
177 "Level 2."
178 :group 'outlines)
180 (defface outline-3
181 '((t :inherit font-lock-keyword-face))
182 "Level 3."
183 :group 'outlines)
185 (defface outline-4
186 '((t :inherit font-lock-builtin-face))
187 "Level 4."
188 :group 'outlines)
190 (defface outline-5
191 '((t :inherit font-lock-comment-face))
192 "Level 5."
193 :group 'outlines)
195 (defface outline-6
196 '((t :inherit font-lock-constant-face))
197 "Level 6."
198 :group 'outlines)
200 (defface outline-7
201 '((t :inherit font-lock-type-face))
202 "Level 7."
203 :group 'outlines)
205 (defface outline-8
206 '((t :inherit font-lock-string-face))
207 "Level 8."
208 :group 'outlines)
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 ()
218 ;; (save-excursion
219 ;; (outline-back-to-heading t)
220 ;; (let* ((count 0)
221 ;; (start-level (funcall outline-level))
222 ;; (level start-level)
223 ;; face-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))))
237 (save-excursion
238 (goto-char (match-beginning 0))
239 (looking-at outline-regexp)
240 (condition-case nil
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.")
253 ;;;###autoload
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."
314 :type 'string
315 :group 'outlines)
317 ;;;###autoload
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))
324 :group 'outlines
325 (if outline-minor-mode
326 (progn
327 ;; Turn off this mode if we change major modes.
328 (add-hook 'change-major-mode-hook
329 (lambda () (outline-minor-mode -1))
330 nil t)
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.
338 (show-all)))
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
363 sets with nil.")
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
370 ;; as appropriate.
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 "\\)")
384 nil 'move)
385 (goto-char (match-beginning 0)))
386 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
387 (forward-char -1)))
389 (defun outline-next-heading ()
390 "Move to the next (possibly invisible) heading line."
391 (interactive)
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 "\\)")
395 nil 'move)
396 (goto-char (match-beginning 0))))
398 (defun outline-previous-heading ()
399 "Move to the previous (possibly invisible) heading line."
400 (interactive)
401 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
402 nil 'move))
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."
415 (beginning-of-line)
416 (or (outline-on-heading-p invisible-ok)
417 (let (found)
418 (save-excursion
419 (while (not found)
420 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
421 nil t)
422 (error "before first heading"))
423 (setq found (and (or invisible-ok (not (outline-invisible-p)))
424 (point)))))
425 (goto-char found)
426 found)))
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."
431 (save-excursion
432 (beginning-of-line)
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."
438 (interactive)
439 (let ((head (save-excursion
440 (condition-case nil
441 (outline-back-to-heading)
442 (error (outline-next-heading)))
443 (if (eobp)
444 (or (caar outline-heading-alist) "")
445 (match-string 0)))))
446 (unless (or (string-match "[ \t]\\'" head)
447 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
448 (concat head " "))))
449 (setq head (concat head " ")))
450 (unless (bolp) (end-of-line) (newline))
451 (insert head)
452 (unless (eolp)
453 (save-excursion (newline-and-indent)))
454 (run-hooks 'outline-insert-heading-hook)))
456 (defun outline-invent-heading (head up)
457 (save-match-data
458 ;; Let's try to invent one by repeating or deleting the last char.
459 (let ((new-head (if up (substring head 0 -1)
460 (concat head (substring head -1)))))
461 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
462 new-head)
463 ;; Why bother checking that it is indeed higher/lower level ?
464 new-head
465 ;; Didn't work, so ask what to do.
466 (read-string (format "%s heading for `%s': "
467 (if up "Parent" "Demoted") head)
468 head nil nil t)))))
470 (defun outline-promote (&optional children)
471 "Promote headings higher up the tree.
472 If prefix argument CHILDREN is given, promote also all the children.
473 If the region is active in `transient-mark-mode', promote all headings
474 in the region."
475 (interactive
476 (list (if (and transient-mark-mode mark-active) 'region
477 (outline-back-to-heading)
478 (if current-prefix-arg nil 'subtree))))
479 (cond
480 ((eq children 'region)
481 (outline-map-region 'outline-promote (region-beginning) (region-end)))
482 (children
483 (outline-map-region 'outline-promote
484 (point)
485 (save-excursion (outline-get-next-sibling) (point))))
487 (outline-back-to-heading t)
488 (let* ((head (match-string-no-properties 0))
489 (level (save-match-data (funcall outline-level)))
490 (up-head (or (outline-head-from-level (1- level) head)
491 ;; Use the parent heading, if it is really
492 ;; one level less.
493 (save-excursion
494 (save-match-data
495 (outline-up-heading 1 t)
496 (and (= (1- level) (funcall outline-level))
497 (match-string-no-properties 0))))
498 ;; Bummer!! There is no lower level heading.
499 (outline-invent-heading head 'up))))
501 (unless (rassoc level outline-heading-alist)
502 (push (cons head level) outline-heading-alist))
504 (replace-match up-head nil t)))))
506 (defun outline-demote (&optional children)
507 "Demote headings lower down the tree.
508 If prefix argument CHILDREN is given, demote also all the children.
509 If the region is active in `transient-mark-mode', demote all headings
510 in the region."
511 (interactive
512 (list (if (and transient-mark-mode mark-active) 'region
513 (outline-back-to-heading)
514 (if current-prefix-arg nil 'subtree))))
515 (cond
516 ((eq children 'region)
517 (outline-map-region 'outline-demote (region-beginning) (region-end)))
518 (children
519 (outline-map-region 'outline-demote
520 (point)
521 (save-excursion (outline-get-next-sibling) (point))))
523 (let* ((head (match-string-no-properties 0))
524 (level (save-match-data (funcall outline-level)))
525 (down-head
526 (or (outline-head-from-level (1+ level) head)
527 (save-excursion
528 (save-match-data
529 (while (and (progn (outline-next-heading) (not (eobp)))
530 (<= (funcall outline-level) level)))
531 (when (eobp)
532 ;; Try again from the beginning of the buffer.
533 (goto-char (point-min))
534 (while (and (progn (outline-next-heading) (not (eobp)))
535 (<= (funcall outline-level) level))))
536 (unless (eobp)
537 (looking-at outline-regexp)
538 (match-string-no-properties 0))))
539 ;; Bummer!! There is no higher-level heading in the buffer.
540 (outline-invent-heading head nil))))
542 (unless (rassoc level outline-heading-alist)
543 (push (cons head level) outline-heading-alist))
544 (replace-match down-head nil t)))))
546 (defun outline-head-from-level (level head &optional alist)
547 "Get new heading with level LEVEL from ALIST.
548 If there are no such entries, return nil.
549 ALIST defaults to `outline-heading-alist'.
550 Similar to (car (rassoc LEVEL ALIST)).
551 If there are several different entries with same new level, choose
552 the one with the smallest distance to the assocation of HEAD in the alist.
553 This makes it possible for promotion to work in modes with several
554 independent sets of headings (numbered, unnumbered, appendix...)"
555 (unless alist (setq alist outline-heading-alist))
556 (let ((l (rassoc level alist))
557 ll h hl l2 l2l)
558 (cond
559 ((null l) nil)
560 ;; If there's no HEAD after L, any other entry for LEVEL after L
561 ;; can't be much better than L.
562 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
563 ;; If there's no other entry for LEVEL, just keep L.
564 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
565 ;; Now we have L, L2, and H: see if L2 seems better than L.
566 ;; If H is after L2, L2 is better.
567 ((memq h (setq l2l (memq l2 (cdr ll))))
568 (outline-head-from-level level head l2l))
569 ;; Now we have H between L and L2.
570 ;; If there's a separator between L and H, prefer L2.
571 ((memq h (memq nil ll))
572 (outline-head-from-level level head l2l))
573 ;; If there's a separator between L2 and H, prefer L.
574 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
575 ;; No separator between L and L2, check the distance.
576 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
577 (outline-head-from-level level head l2l))
578 ;; If all else fails, just keep L.
579 (t (car l)))))
581 (defun outline-map-region (fun beg end)
582 "Call FUN for every heading between BEG and END.
583 When FUN is called, point is at the beginning of the heading and
584 the match data is set appropriately."
585 (save-excursion
586 (setq end (copy-marker end))
587 (goto-char beg)
588 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
589 (goto-char (match-beginning 0))
590 (funcall fun)
591 (while (and (progn
592 (outline-next-heading)
593 (< (point) end))
594 (not (eobp)))
595 (funcall fun)))))
597 ;; Vertical tree motion
599 (defun outline-move-subtree-up (&optional arg)
600 "Move the currrent subtree up past ARG headlines of the same level."
601 (interactive "p")
602 (outline-move-subtree-down (- arg)))
604 (defun outline-move-subtree-down (&optional arg)
605 "Move the currrent subtree down past ARG headlines of the same level."
606 (interactive "p")
607 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
608 'outline-get-last-sibling))
609 (ins-point (make-marker))
610 (cnt (abs arg))
611 beg end folded)
612 ;; Select the tree
613 (outline-back-to-heading)
614 (setq beg (point))
615 (save-match-data
616 (save-excursion (outline-end-of-heading)
617 (setq folded (outline-invisible-p)))
618 (outline-end-of-subtree))
619 (if (= (char-after) ?\n) (forward-char 1))
620 (setq end (point))
621 ;; Find insertion point, with error handling
622 (goto-char beg)
623 (while (> cnt 0)
624 (or (funcall movfunc)
625 (progn (goto-char beg)
626 (error "Cannot move past superior level")))
627 (setq cnt (1- cnt)))
628 (if (> arg 0)
629 ;; Moving forward - still need to move over subtree
630 (progn (outline-end-of-subtree)
631 (if (= (char-after) ?\n) (forward-char 1))))
632 (move-marker ins-point (point))
633 (insert (delete-and-extract-region beg end))
634 (goto-char ins-point)
635 (if folded (hide-subtree))
636 (move-marker ins-point nil)))
638 (defun outline-end-of-heading ()
639 (if (re-search-forward outline-heading-end-regexp nil 'move)
640 (forward-char -1)))
642 (defun outline-next-visible-heading (arg)
643 "Move to the next visible heading line.
644 With argument, repeats or can move backward if negative.
645 A heading line is one that starts with a `*' (or that
646 `outline-regexp' matches)."
647 (interactive "p")
648 (if (< arg 0)
649 (beginning-of-line)
650 (end-of-line))
651 (while (and (not (bobp)) (< arg 0))
652 (while (and (not (bobp))
653 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
654 nil 'move)
655 (outline-invisible-p)))
656 (setq arg (1+ arg)))
657 (while (and (not (eobp)) (> arg 0))
658 (while (and (not (eobp))
659 (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
660 nil 'move)
661 (outline-invisible-p (match-beginning 0))))
662 (setq arg (1- arg)))
663 (beginning-of-line))
665 (defun outline-previous-visible-heading (arg)
666 "Move to the previous heading line.
667 With argument, repeats or can move forward if negative.
668 A heading line is one that starts with a `*' (or that
669 `outline-regexp' matches)."
670 (interactive "p")
671 (outline-next-visible-heading (- arg)))
673 (defun outline-mark-subtree ()
674 "Mark the current subtree in an outlined document.
675 This puts point at the start of the current subtree, and mark at the end."
676 (interactive)
677 (let ((beg))
678 (if (outline-on-heading-p)
679 ;; we are already looking at a heading
680 (beginning-of-line)
681 ;; else go back to previous heading
682 (outline-previous-visible-heading 1))
683 (setq beg (point))
684 (outline-end-of-subtree)
685 (push-mark (point))
686 (goto-char beg)))
689 (put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
690 (defun outline-flag-region (from to flag)
691 "Hide or show lines from FROM to TO, according to FLAG.
692 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
693 (remove-overlays from to 'invisible 'outline)
694 (when flag
695 (let ((o (make-overlay from to)))
696 (overlay-put o 'invisible 'outline)
697 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
698 ;; Seems only used by lazy-lock. I.e. obsolete.
699 (run-hooks 'outline-view-change-hook))
701 (defun outline-reveal-toggle-invisible (o hidep)
702 (save-excursion
703 (goto-char (overlay-start o))
704 (if hidep
705 ;; When hiding the area again, we could just clean it up and let
706 ;; reveal do the rest, by simply doing:
707 ;; (remove-overlays (overlay-start o) (overlay-end o)
708 ;; 'invisible 'outline)
710 ;; That works fine as long as everything is in sync, but if the
711 ;; structure of the document is changed while revealing parts of it,
712 ;; the resulting behavior can be ugly. I.e. we need to make
713 ;; sure that we hide exactly a subtree.
714 (progn
715 (let ((end (overlay-end o)))
716 (delete-overlay o)
717 (while (progn
718 (hide-subtree)
719 (outline-next-visible-heading 1)
720 (and (not (eobp)) (< (point) end))))))
722 ;; When revealing, we just need to reveal sublevels. If point is
723 ;; inside one of the sublevels, reveal will call us again.
724 ;; But we need to preserve the original overlay.
725 (let ((o1 (copy-overlay o)))
726 (overlay-put o 'invisible nil) ;Show (most of) the text.
727 (while (progn
728 (show-entry)
729 (show-children)
730 ;; Normally just the above is needed.
731 ;; But in odd cases, the above might fail to show anything.
732 ;; To avoid an infinite loop, we have to make sure that
733 ;; *something* gets shown.
734 (and (equal (overlay-start o) (overlay-start o1))
735 (< (point) (overlay-end o))
736 (= 0 (forward-line 1)))))
737 ;; If still nothing was shown, just kill the damn thing.
738 (when (equal (overlay-start o) (overlay-start o1))
739 ;; I've seen it happen at the end of buffer.
740 (delete-overlay o1))))))
742 ;; Function to be set as an outline-isearch-open-invisible' property
743 ;; to the overlay that makes the outline invisible (see
744 ;; `outline-flag-region').
745 (defun outline-isearch-open-invisible (overlay)
746 ;; We rely on the fact that isearch places point on the matched text.
747 (show-entry))
749 (defun hide-entry ()
750 "Hide the body directly following this heading."
751 (interactive)
752 (outline-back-to-heading)
753 (save-excursion
754 (outline-end-of-heading)
755 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
757 (defun show-entry ()
758 "Show the body directly following this heading.
759 Show the heading too, if it is currently invisible."
760 (interactive)
761 (save-excursion
762 (outline-back-to-heading t)
763 (outline-flag-region (1- (point))
764 (progn (outline-next-preface) (point)) nil)))
766 (defun hide-body ()
767 "Hide all body lines in buffer, leaving all headings visible."
768 (interactive)
769 (hide-region-body (point-min) (point-max)))
771 (defun hide-region-body (start end)
772 "Hide all body lines in the region, but not headings."
773 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
774 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
775 ;; and run the hook finally.
776 (let (outline-view-change-hook)
777 (save-excursion
778 (save-restriction
779 (narrow-to-region start end)
780 (goto-char (point-min))
781 (if (outline-on-heading-p)
782 (outline-end-of-heading)
783 (outline-next-preface))
784 (while (not (eobp))
785 (outline-flag-region (point)
786 (progn (outline-next-preface) (point)) t)
787 (unless (eobp)
788 (forward-char (if (looking-at "\n\n") 2 1))
789 (outline-end-of-heading))))))
790 (run-hooks 'outline-view-change-hook))
792 (defun show-all ()
793 "Show all of the text in the buffer."
794 (interactive)
795 (outline-flag-region (point-min) (point-max) nil))
797 (defun hide-subtree ()
798 "Hide everything after this heading at deeper levels."
799 (interactive)
800 (outline-flag-subtree t))
802 (defun hide-leaves ()
803 "Hide all body after this heading at deeper levels."
804 (interactive)
805 (outline-back-to-heading)
806 (save-excursion
807 (outline-end-of-heading)
808 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
810 (defun show-subtree ()
811 "Show everything after this heading at deeper levels."
812 (interactive)
813 (outline-flag-subtree nil))
815 (defun outline-show-heading ()
816 "Show the current heading and move to its end."
817 (outline-flag-region (- (point)
818 (if (bobp) 0
819 (if (and outline-blank-line
820 (eq (char-before (1- (point))) ?\n))
821 2 1)))
822 (progn (outline-end-of-heading) (point))
823 nil))
825 (defun hide-sublevels (levels)
826 "Hide everything but the top LEVELS levels of headers, in whole buffer."
827 (interactive "p")
828 (if (< levels 1)
829 (error "Must keep at least one level of headers"))
830 (let (outline-view-change-hook)
831 (save-excursion
832 (goto-char (point-min))
833 ;; Skip the prelude, if any.
834 (unless (outline-on-heading-p t) (outline-next-heading))
835 ;; First hide everything.
836 (outline-flag-region (point) (point-max) t)
837 ;; Then unhide the top level headers.
838 (outline-map-region
839 (lambda ()
840 (if (<= (funcall outline-level) levels)
841 (outline-show-heading)))
842 (point) (point-max))))
843 (run-hooks 'outline-view-change-hook))
845 (defun hide-other ()
846 "Hide everything except current body and parent and top-level headings."
847 (interactive)
848 (hide-sublevels 1)
849 (let (outline-view-change-hook)
850 (save-excursion
851 (outline-back-to-heading t)
852 (show-entry)
853 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
854 (error nil))
855 (outline-flag-region (1- (point))
856 (save-excursion (forward-line 1) (point))
857 nil))))
858 (run-hooks 'outline-view-change-hook))
860 (defun outline-toggle-children ()
861 "Show or hide the current subtree depending on its current state."
862 (interactive)
863 (outline-back-to-heading)
864 (if (not (outline-invisible-p (line-end-position)))
865 (hide-subtree)
866 (show-children)
867 (show-entry)))
869 (defun outline-flag-subtree (flag)
870 (save-excursion
871 (outline-back-to-heading)
872 (outline-end-of-heading)
873 (outline-flag-region (point)
874 (progn (outline-end-of-subtree) (point))
875 flag)))
877 (defun outline-end-of-subtree ()
878 (outline-back-to-heading)
879 (let ((first t)
880 (level (funcall outline-level)))
881 (while (and (not (eobp))
882 (or first (> (funcall outline-level) level)))
883 (setq first nil)
884 (outline-next-heading))
885 (if (bolp)
886 (progn
887 ;; Go to end of line before heading
888 (forward-char -1)
889 (if (and outline-blank-line (bolp))
890 ;; leave blank line before heading
891 (forward-char -1))))))
893 (defun show-branches ()
894 "Show all subheadings of this heading, but not their bodies."
895 (interactive)
896 (show-children 1000))
898 (defun show-children (&optional level)
899 "Show all direct subheadings of this heading.
900 Prefix arg LEVEL is how many levels below the current level should be shown.
901 Default is enough to cause the following heading to appear."
902 (interactive "P")
903 (setq level
904 (if level (prefix-numeric-value level)
905 (save-excursion
906 (outline-back-to-heading)
907 (let ((start-level (funcall outline-level)))
908 (outline-next-heading)
909 (if (eobp)
911 (max 1 (- (funcall outline-level) start-level)))))))
912 (let (outline-view-change-hook)
913 (save-excursion
914 (outline-back-to-heading)
915 (setq level (+ level (funcall outline-level)))
916 (outline-map-region
917 (lambda ()
918 (if (<= (funcall outline-level) level)
919 (outline-show-heading)))
920 (point)
921 (progn (outline-end-of-subtree)
922 (if (eobp) (point-max) (1+ (point)))))))
923 (run-hooks 'outline-view-change-hook))
927 (defun outline-up-heading (arg &optional invisible-ok)
928 "Move to the visible heading line of which the present line is a subheading.
929 With argument, move up ARG levels.
930 If INVISIBLE-OK is non-nil, also consider invisible lines."
931 (interactive "p")
932 (and (eq this-command 'outline-up-heading)
933 (or (eq last-command 'outline-up-heading) (push-mark)))
934 (outline-back-to-heading invisible-ok)
935 (let ((start-level (funcall outline-level)))
936 (if (eq start-level 1)
937 (error "Already at top level of the outline"))
938 (while (and (> start-level 1) (> arg 0) (not (bobp)))
939 (let ((level start-level))
940 (while (not (or (< level start-level) (bobp)))
941 (if invisible-ok
942 (outline-previous-heading)
943 (outline-previous-visible-heading 1))
944 (setq level (funcall outline-level)))
945 (setq start-level level))
946 (setq arg (- arg 1))))
947 (looking-at outline-regexp))
949 (defun outline-forward-same-level (arg)
950 "Move forward to the ARG'th subheading at same level as this one.
951 Stop at the first and last subheadings of a superior heading."
952 (interactive "p")
953 (outline-back-to-heading)
954 (while (> arg 0)
955 (let ((point-to-move-to (save-excursion
956 (outline-get-next-sibling))))
957 (if point-to-move-to
958 (progn
959 (goto-char point-to-move-to)
960 (setq arg (1- arg)))
961 (progn
962 (setq arg 0)
963 (error "No following same-level heading"))))))
965 (defun outline-get-next-sibling ()
966 "Move to next heading of the same level, and return point or nil if none."
967 (let ((level (funcall outline-level)))
968 (outline-next-visible-heading 1)
969 (while (and (not (eobp)) (> (funcall outline-level) level))
970 (outline-next-visible-heading 1))
971 (if (or (eobp) (< (funcall outline-level) level))
973 (point))))
975 (defun outline-backward-same-level (arg)
976 "Move backward to the ARG'th subheading at same level as this one.
977 Stop at the first and last subheadings of a superior heading."
978 (interactive "p")
979 (outline-back-to-heading)
980 (while (> arg 0)
981 (let ((point-to-move-to (save-excursion
982 (outline-get-last-sibling))))
983 (if point-to-move-to
984 (progn
985 (goto-char point-to-move-to)
986 (setq arg (1- arg)))
987 (progn
988 (setq arg 0)
989 (error "No previous same-level heading"))))))
991 (defun outline-get-last-sibling ()
992 "Move to previous heading of the same level, and return point or nil if none."
993 (let ((level (funcall outline-level)))
994 (outline-previous-visible-heading 1)
995 (while (and (> (funcall outline-level) level)
996 (not (bobp)))
997 (outline-previous-visible-heading 1))
998 (if (< (funcall outline-level) level)
1000 (point))))
1002 (defun outline-headers-as-kill (beg end)
1003 "Save the visible outline headers in region at the start of the kill ring.
1005 Text shown between the headers isn't copied. Two newlines are
1006 inserted between saved headers. Yanking the result may be a
1007 convenient way to make a table of contents of the buffer."
1008 (interactive "r")
1009 (save-excursion
1010 (save-restriction
1011 (narrow-to-region beg end)
1012 (goto-char (point-min))
1013 (let ((buffer (current-buffer))
1014 start end)
1015 (with-temp-buffer
1016 (with-current-buffer buffer
1017 ;; Boundary condition: starting on heading:
1018 (when (outline-on-heading-p)
1019 (outline-back-to-heading)
1020 (setq start (point)
1021 end (progn (outline-end-of-heading)
1022 (point)))
1023 (insert-buffer-substring buffer start end)
1024 (insert "\n\n")))
1025 (let ((temp-buffer (current-buffer)))
1026 (with-current-buffer buffer
1027 (while (outline-next-heading)
1028 (unless (outline-invisible-p)
1029 (setq start (point)
1030 end (progn (outline-end-of-heading) (point)))
1031 (with-current-buffer temp-buffer
1032 (insert-buffer-substring buffer start end)
1033 (insert "\n\n"))))))
1034 (kill-new (buffer-string)))))))
1036 (provide 'outline)
1037 (provide 'noutline)
1039 ;; arch-tag: 1724410e-7d4d-4f46-b801-49e18171e874
1040 ;;; outline.el ends here