1 ;;; outline.el --- outline mode commands for Emacs
2 ;; Copyright (C) 1986, 1993, 1994 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; This package is a major mode for editing outline-format documents.
26 ;; An outline can be `abstracted' to show headers at any given level,
27 ;; with all stuff below hidden. See the Emacs manual for details.
31 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
33 (defvar outline-regexp nil
34 "*Regular expression to match the beginning of a heading.
35 Any line whose beginning matches this regexp is considered to start a heading.
36 The recommended way to set this is with a Local Variables: list
37 in the file it applies to. See also outline-heading-end-regexp.")
39 ;; Can't initialize this in the defvar above -- some major modes have
40 ;; already assigned a local value to it.
41 (or (default-value 'outline-regexp)
42 (setq-default outline-regexp "[*\^L]+"))
44 (defvar outline-heading-end-regexp "[\n\^M]"
45 "*Regular expression to match the end of a heading line.
46 You can assume that point is at the beginning of a heading when this
47 regexp is searched for. The heading ends at the end of the match.
48 The recommended way to set this is with a \"Local Variables:\" list
49 in the file it applies to.")
51 (defvar outline-mode-prefix-map nil)
53 (if outline-mode-prefix-map
55 (setq outline-mode-prefix-map (make-sparse-keymap))
56 (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
57 (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
58 (define-key outline-mode-prefix-map "\C-i" 'show-children)
59 (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
60 (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
61 (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
62 (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
63 (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
64 (define-key outline-mode-prefix-map "\C-t" 'hide-body)
65 (define-key outline-mode-prefix-map "\C-a" 'show-all)
66 (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
67 (define-key outline-mode-prefix-map "\C-e" 'show-entry)
68 (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
69 (define-key outline-mode-prefix-map "\C-k" 'show-branches)
70 (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
71 (define-key outline-mode-prefix-map "\C-o" 'hide-other))
73 (defvar outline-mode-menu-bar-map nil)
74 (if outline-mode-menu-bar-map
76 (setq outline-mode-menu-bar-map (make-sparse-keymap))
78 (define-key outline-mode-menu-bar-map [hide]
79 (cons "Hide" (make-sparse-keymap "Hide")))
81 (define-key outline-mode-menu-bar-map [hide hide-other]
82 '("Hide Other" . hide-other))
83 (define-key outline-mode-menu-bar-map [hide hide-sublevels]
84 '("Hide Sublevels" . hide-sublevels))
85 (define-key outline-mode-menu-bar-map [hide hide-subtree]
86 '("Hide Subtree" . hide-subtree))
87 (define-key outline-mode-menu-bar-map [hide hide-entry]
88 '("Hide Entry" . hide-entry))
89 (define-key outline-mode-menu-bar-map [hide hide-body]
90 '("Hide Body" . hide-body))
91 (define-key outline-mode-menu-bar-map [hide hide-leaves]
92 '("Hide Leaves" . hide-leaves))
94 (define-key outline-mode-menu-bar-map [show]
95 (cons "Show" (make-sparse-keymap "Show")))
97 (define-key outline-mode-menu-bar-map [show show-subtree]
98 '("Show Subtree" . show-subtree))
99 (define-key outline-mode-menu-bar-map [show show-children]
100 '("Show Children" . show-children))
101 (define-key outline-mode-menu-bar-map [show show-branches]
102 '("Show Branches" . show-branches))
103 (define-key outline-mode-menu-bar-map [show show-entry]
104 '("Show Entry" . show-entry))
105 (define-key outline-mode-menu-bar-map [show show-all]
106 '("Show All" . show-all))
108 (define-key outline-mode-menu-bar-map [headings]
109 (cons "Headings" (make-sparse-keymap "Headings")))
111 (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
112 '("Previous Same Level" . outline-backward-same-level))
113 (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
114 '("Next Same Level" . outline-forward-same-level))
115 (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
116 '("Previous" . outline-previous-visible-heading))
117 (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
118 '("Next" . outline-next-visible-heading))
119 (define-key outline-mode-menu-bar-map [headings outline-up-heading]
120 '("Up" . outline-up-heading)))
122 (defvar outline-mode-map nil "")
126 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
127 (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
128 (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
130 (defvar outline-minor-mode nil
131 "Non-nil if using Outline mode as a minor mode of some other mode.")
132 (make-variable-buffer-local 'outline-minor-mode)
133 (put 'outline-minor-mode 'permanent-local t)
134 (or (assq 'outline-minor-mode minor-mode-alist)
135 (setq minor-mode-alist (append minor-mode-alist
136 (list '(outline-minor-mode " Outl")))))
138 (defvar outline-font-lock-keywords
139 '(;; Highlight headings according to the level.
140 ("^\\(\\*+\\)[ \t]*\\(.+\\)?[ \t]*$"
141 (1 font-lock-string-face)
142 (2 (let ((len (- (match-end 1) (match-beginning 1))))
143 (or (cdr (assq len '((1 . font-lock-function-name-face)
144 (2 . font-lock-keyword-face)
145 (3 . font-lock-comment-face))))
146 font-lock-variable-name-face))
148 ;; Highight citations of the form [1] and [Mar94].
149 ("\\[\\([A-Z][A-Za-z]+\\)*[0-9]+\\]" . font-lock-type-face))
150 "Additional expressions to highlight in Outline mode.")
153 (defun outline-mode ()
154 "Set major mode for editing outlines with selective display.
155 Headings are lines which start with asterisks: one for major headings,
156 two for subheadings, etc. Lines not starting with asterisks are body lines.
158 Body text or subheadings under a heading can be made temporarily
159 invisible, or visible again. Invisible lines are attached to the end
160 of the heading, so they move with it, if the line is killed and yanked
161 back. A heading with text hidden under it is marked with an ellipsis (...).
163 Commands:\\<outline-mode-map>
164 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
165 \\[outline-previous-visible-heading] outline-previous-visible-heading
166 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
167 \\[outline-backward-same-level] outline-backward-same-level
168 \\[outline-up-heading] outline-up-heading move from subheading to heading
170 \\[hide-body] make all text invisible (not headings).
171 \\[show-all] make everything in buffer visible.
173 The remaining commands are used when point is on a heading line.
174 They apply to some of the body or subheadings of that heading.
175 \\[hide-subtree] hide-subtree make body and subheadings invisible.
176 \\[show-subtree] show-subtree make body and subheadings visible.
177 \\[show-children] show-children make direct subheadings visible.
178 No effect on body, or subheadings 2 or more levels down.
179 With arg N, affects subheadings N levels down.
180 \\[hide-entry] make immediately following body invisible.
181 \\[show-entry] make it visible.
182 \\[hide-leaves] make body under heading and under its subheadings invisible.
183 The subheadings remain visible.
184 \\[show-branches] make all subheadings at all levels visible.
186 The variable `outline-regexp' can be changed to control what is a heading.
187 A line is a heading if `outline-regexp' matches something at the
188 beginning of the line. The longer the match, the deeper the level.
190 Turning on outline mode calls the value of `text-mode-hook' and then of
191 `outline-mode-hook', if they are non-nil."
193 (kill-all-local-variables)
194 (setq selective-display t)
195 (use-local-map outline-mode-map)
196 (setq mode-name "Outline")
197 (setq major-mode 'outline-mode)
198 (define-abbrev-table 'text-mode-abbrev-table ())
199 (setq local-abbrev-table text-mode-abbrev-table)
200 (set-syntax-table text-mode-syntax-table)
201 (make-local-variable 'paragraph-start)
202 (setq paragraph-start (concat paragraph-start "\\|\\("
203 outline-regexp "\\)"))
204 ;; Inhibit auto-filling of header lines.
205 (make-local-variable 'auto-fill-inhibit-regexp)
206 (setq auto-fill-inhibit-regexp outline-regexp)
207 (make-local-variable 'paragraph-separate)
208 (setq paragraph-separate (concat paragraph-separate "\\|\\("
209 outline-regexp "\\)"))
210 (make-local-variable 'font-lock-defaults)
211 (setq font-lock-defaults '(outline-font-lock-keywords t))
212 (make-local-variable 'change-major-mode-hook)
213 (add-hook 'change-major-mode-hook 'show-all)
214 (run-hooks 'text-mode-hook 'outline-mode-hook))
216 (defvar outline-minor-mode-prefix "\C-c\C-o"
217 "*Prefix key to use for Outline commands in Outline minor mode.
218 The value of this variable is checked as part of loading Outline mode.
219 After that, changing the prefix key requires manipulating keymaps.")
221 (defvar outline-minor-mode-map nil)
222 (if outline-minor-mode-map
224 (setq outline-minor-mode-map (make-sparse-keymap))
225 (define-key outline-minor-mode-map [menu-bar]
226 outline-mode-menu-bar-map)
227 (define-key outline-minor-mode-map outline-minor-mode-prefix
228 outline-mode-prefix-map))
230 (or (assq 'outline-minor-mode minor-mode-map-alist)
231 (setq minor-mode-map-alist
232 (cons (cons 'outline-minor-mode outline-minor-mode-map)
233 minor-mode-map-alist)))
236 (defun outline-minor-mode (&optional arg)
237 "Toggle Outline minor mode.
238 With arg, turn Outline minor mode on if arg is positive, off otherwise.
239 See the command `outline-mode' for more information on this mode."
241 (setq outline-minor-mode
242 (if (null arg) (not outline-minor-mode)
243 (> (prefix-numeric-value arg) 0)))
244 (if outline-minor-mode
246 (setq selective-display t)
247 (run-hooks 'outline-minor-mode-hook))
248 (setq selective-display nil))
249 ;; When turning off outline mode, get rid of any ^M's.
250 (or outline-minor-mode
251 (outline-flag-region (point-min) (point-max) ?\n))
252 (force-mode-line-update))
254 (defvar outline-level 'outline-level
255 "Function of no args to compute a header's nesting level in an outline.
256 It can assume point is at the beginning of a header line.")
258 ;; This used to count columns rather than characters, but that made ^L
259 ;; appear to be at level 2 instead of 1. Columns would be better for
260 ;; tab handling, but the default regexp doesn't use tabs, and anyone
261 ;; who changes the regexp can also redefine the outline-level variable
263 (defun outline-level ()
264 "Return the depth to which a statement is nested in the outline.
265 Point must be at the beginning of a header line. This is actually
266 the number of characters that `outline-regexp' matches."
268 (looking-at outline-regexp)
269 (- (match-end 0) (match-beginning 0))))
271 (defun outline-next-preface ()
272 "Skip forward to just before the next heading line.
273 If there's no following heading line, stop before the newline
274 at the end of the buffer."
275 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
277 (goto-char (match-beginning 0)))
278 (if (memq (preceding-char) '(?\n ?\^M))
281 (defun outline-next-heading ()
282 "Move to the next (possibly invisible) heading line."
284 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
286 (goto-char (1+ (match-beginning 0)))))
288 (defun outline-back-to-heading ()
289 "Move to previous heading line, or beg of this line if it's a heading.
290 Only visible heading lines are considered."
292 (or (outline-on-heading-p)
293 (re-search-backward (concat "^\\(" outline-regexp "\\)") nil t)
294 (error "before first heading")))
296 (defun outline-on-heading-p ()
297 "Return t if point is on a (visible) heading line."
301 (looking-at outline-regexp))))
303 (defun outline-end-of-heading ()
304 (if (re-search-forward outline-heading-end-regexp nil 'move)
307 (defun outline-next-visible-heading (arg)
308 "Move to the next visible heading line.
309 With argument, repeats or can move backward if negative.
310 A heading line is one that starts with a `*' (or that
311 `outline-regexp' matches)."
316 (or (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t arg)
320 (defun outline-previous-visible-heading (arg)
321 "Move to the previous heading line.
322 With argument, repeats or can move forward if negative.
323 A heading line is one that starts with a `*' (or that
324 `outline-regexp' matches)."
326 (outline-next-visible-heading (- arg)))
328 (defun outline-flag-region (from to flag)
329 "Hides or shows lines from FROM to TO, according to FLAG.
330 If FLAG is `\\n' (newline character) then text is shown,
331 while if FLAG is `\\^M' (control-M) the text is hidden."
332 (let (buffer-read-only)
333 (subst-char-in-region from to
334 (if (= flag ?\n) ?\^M ?\n)
338 "Hide the body directly following this heading."
340 (outline-back-to-heading)
341 (outline-end-of-heading)
343 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M)))
346 "Show the body directly following this heading."
349 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\n)))
352 "Hide all of buffer except headings."
354 (hide-region-body (point-min) (point-max)))
356 (defun hide-region-body (start end)
357 "Hide all body lines in the region, but not headings."
360 (narrow-to-region start end)
361 (goto-char (point-min))
362 (if (outline-on-heading-p)
363 (outline-end-of-heading))
365 (outline-flag-region (point)
366 (progn (outline-next-preface) (point)) ?\^M)
370 (if (looking-at "[\n\^M][\n\^M]")
372 (outline-end-of-heading)))))))
375 "Show all of the text in the buffer."
377 (outline-flag-region (point-min) (point-max) ?\n))
379 (defun hide-subtree ()
380 "Hide everything after this heading at deeper levels."
382 (outline-flag-subtree ?\^M))
384 (defun hide-leaves ()
385 "Hide all body after this heading at deeper levels."
387 (outline-back-to-heading)
388 (outline-end-of-heading)
389 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
391 (defun show-subtree ()
392 "Show everything after this heading at deeper levels."
394 (outline-flag-subtree ?\n))
396 (defun hide-sublevels (levels)
397 "Hide everything but the top LEVELS levels of headers, in whole buffer."
400 (error "Must keep at least one level of headers"))
401 (setq levels (1- levels))
403 (goto-char (point-min))
404 ;; Keep advancing to the next top-level heading.
405 (while (or (and (bobp) (outline-on-heading-p))
406 (outline-next-heading))
407 (let ((end (save-excursion (outline-end-of-subtree) (point))))
408 ;; Hide everything under that.
409 (outline-flag-region (point) end ?\^M)
410 ;; Show the first LEVELS levels under that.
412 (show-children levels))
413 ;; Move to the next, since we already found it.
417 "Hide everything except for the current body and the parent headings."
422 (while (save-excursion
423 (and (re-search-backward "[\n\r]" nil t)
424 (eq (following-char) ?\r)))
427 (if (eq last (point))
429 (outline-next-heading)
430 (outline-flag-region last (point) ?\n))
432 (setq last (point)))))))
434 (defun outline-flag-subtree (flag)
436 (outline-back-to-heading)
437 (outline-end-of-heading)
438 (outline-flag-region (point)
439 (progn (outline-end-of-subtree) (point))
442 (defun outline-end-of-subtree ()
443 (outline-back-to-heading)
444 (let ((opoint (point))
446 (level (funcall outline-level)))
447 (while (and (not (eobp))
448 (or first (> (funcall outline-level) level)))
450 (outline-next-heading))
451 (if (memq (preceding-char) '(?\n ?\^M))
453 ;; Go to end of line before heading
455 (if (memq (preceding-char) '(?\n ?\^M))
456 ;; leave blank line before heading
457 (forward-char -1))))))
459 (defun show-branches ()
460 "Show all subheadings of this heading, but not their bodies."
462 (show-children 1000))
464 (defun show-children (&optional level)
465 "Show all direct subheadings of this heading.
466 Prefix arg LEVEL is how many levels below the current level should be shown.
467 Default is enough to cause the following heading to appear."
470 (if level (prefix-numeric-value level)
472 (outline-back-to-heading)
473 (let ((start-level (funcall outline-level)))
474 (outline-next-heading)
477 (max 1 (- (funcall outline-level) start-level)))))))
480 (outline-back-to-heading)
481 (setq level (+ level (funcall outline-level)))
482 (narrow-to-region (point)
483 (progn (outline-end-of-subtree)
484 (if (eobp) (point-max) (1+ (point)))))
485 (goto-char (point-min))
486 (while (and (not (eobp))
488 (outline-next-heading)
490 (if (<= (funcall outline-level) level)
492 (outline-flag-region (save-excursion
494 (if (memq (preceding-char) '(?\n ?\^M))
497 (progn (outline-end-of-heading) (point))
500 (defun outline-up-heading (arg)
501 "Move to the heading line of which the present line is a subheading.
502 With argument, move up ARG levels."
504 (outline-back-to-heading)
505 (if (eq (funcall outline-level) 1)
507 (while (and (> (funcall outline-level) 1)
510 (let ((present-level (funcall outline-level)))
511 (while (not (< (funcall outline-level) present-level))
512 (outline-previous-visible-heading 1))
513 (setq arg (- arg 1)))))
515 (defun outline-forward-same-level (arg)
516 "Move forward to the ARG'th subheading at same level as this one.
517 Stop at the first and last subheadings of a superior heading."
519 (outline-back-to-heading)
521 (let ((point-to-move-to (save-excursion
522 (outline-get-next-sibling))))
525 (goto-char point-to-move-to)
531 (defun outline-get-next-sibling ()
532 "Move to next heading of the same level, and return point or nil if none."
533 (let ((level (funcall outline-level)))
534 (outline-next-visible-heading 1)
535 (while (and (> (funcall outline-level) level)
537 (outline-next-visible-heading 1))
538 (if (< (funcall outline-level) level)
542 (defun outline-backward-same-level (arg)
543 "Move backward to the ARG'th subheading at same level as this one.
544 Stop at the first and last subheadings of a superior heading."
546 (outline-back-to-heading)
548 (let ((point-to-move-to (save-excursion
549 (outline-get-last-sibling))))
552 (goto-char point-to-move-to)
558 (defun outline-get-last-sibling ()
559 "Move to next heading of the same level, and return point or nil if none."
560 (let ((level (funcall outline-level)))
561 (outline-previous-visible-heading 1)
562 (while (and (> (funcall outline-level) level)
564 (outline-previous-visible-heading 1))
565 (if (< (funcall outline-level) level)
571 ;;; outline.el ends here