1 ;;; outline.el --- outline mode commands for Emacs
4 ;; Last-Modified: 10 Apr 1991
6 ;; Copyright (C) 1986 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
28 (defvar outline-regexp
"[*\^l]+"
29 "*Regular expression to match the beginning of a heading.
30 Any line whose beginning matches this regexp is considered to start a heading.
31 The recommended way to set this is with a Local Variables: list
32 in the file it applies to. See also outline-heading-end-regexp.")
34 (defvar outline-heading-end-regexp
"[\n^M]"
35 "*Regular expression to match the end of a heading line.
36 You can assume that point is at the beginning of a heading when this
37 regexp is searched for. The heading ends at the end of the match.
38 The recommended way to set this is with a \"Local Variables:\" list
39 in the file it applies to.")
41 (defvar outline-mode-map nil
"")
45 (setq outline-mode-map
(nconc (make-sparse-keymap) text-mode-map
))
46 (define-key outline-mode-map
"\C-c\C-n" 'outline-next-visible-heading
)
47 (define-key outline-mode-map
"\C-c\C-p" 'outline-previous-visible-heading
)
48 (define-key outline-mode-map
"\C-c\C-i" 'show-children
)
49 (define-key outline-mode-map
"\C-c\C-s" 'show-subtree
)
50 (define-key outline-mode-map
"\C-c\C-h" 'hide-subtree
)
51 (define-key outline-mode-map
"\C-c\C-u" 'outline-up-heading
)
52 (define-key outline-mode-map
"\C-c\C-f" 'outline-forward-same-level
)
53 (define-key outline-mode-map
"\C-c\C-b" 'outline-backward-same-level
))
55 (defvar outline-minor-mode nil
56 "Non-nil if using Outline mode as a minor mode of some other mode.")
57 (setq minor-mode-alist
(append minor-mode-alist
58 (list '(outline-minor-mode " Outl"))))
61 (defun outline-mode ()
62 "Set major mode for editing outlines with selective display.
63 Headings are lines which start with asterisks: one for major headings,
64 two for subheadings, etc. Lines not starting with asterisks are body lines.
66 Body text or subheadings under a heading can be made temporarily
67 invisible, or visible again. Invisible lines are attached to the end
68 of the heading, so they move with it, if the line is killed and yanked
69 back. A heading with text hidden under it is marked with an ellipsis (...).
71 Commands:\\<outline-mode-map>
72 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
73 \\[outline-previous-visible-heading] outline-previous-visible-heading
74 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
75 \\[outline-backward-same-level] outline-backward-same-level
76 \\[outline-up-heading] outline-up-heading move from subheading to heading
78 M-x hide-body make all text invisible (not headings).
79 M-x show-all make everything in buffer visible.
81 The remaining commands are used when point is on a heading line.
82 They apply to some of the body or subheadings of that heading.
83 \\[hide-subtree] hide-subtree make body and subheadings invisible.
84 \\[show-subtree] show-subtree make body and subheadings visible.
85 \\[show-children] show-children make direct subheadings visible.
86 No effect on body, or subheadings 2 or more levels down.
87 With arg N, affects subheadings N levels down.
88 M-x hide-entry make immediately following body invisible.
89 M-x show-entry make it visible.
90 M-x hide-leaves make body under heading and under its subheadings invisible.
91 The subheadings remain visible.
92 M-x show-branches make all subheadings at all levels visible.
94 The variable `outline-regexp' can be changed to control what is a heading.
95 A line is a heading if `outline-regexp' matches something at the
96 beginning of the line. The longer the match, the deeper the level.
98 Turning on outline mode calls the value of `text-mode-hook' and then of
99 `outline-mode-hook', if they are non-nil."
101 (kill-all-local-variables)
102 (setq selective-display t
)
103 (use-local-map outline-mode-map
)
104 (setq mode-name
"Outline")
105 (setq major-mode
'outline-mode
)
106 (define-abbrev-table 'text-mode-abbrev-table
())
107 (setq local-abbrev-table text-mode-abbrev-table
)
108 (set-syntax-table text-mode-syntax-table
)
109 (make-local-variable 'paragraph-start
)
110 (setq paragraph-start
(concat paragraph-start
"\\|^\\("
111 outline-regexp
"\\)"))
112 ;; Inhibit auto-filling of header lines.
113 (make-local-variable 'auto-fill-inhibit-regexp
)
114 (setq auto-fill-inhibit-regexp outline-regexp
)
115 (make-local-variable 'paragraph-separate
)
116 (setq paragraph-separate
(concat paragraph-separate
"\\|^\\("
117 outline-regexp
"\\)"))
118 (run-hooks 'text-mode-hook
'outline-mode-hook
))
120 (defun outline-minor-mode (arg)
122 (setq outline-minor-mode
123 (if (null arg
) (not outline-minor-mode
)
124 (> (prefix-numeric-value arg
) 0)))
125 (if outline-minor-mode
127 (setq selective-display t
)
128 (make-local-variable 'outline-old-map
)
129 (setq outline-old-map
(current-local-map))
130 (let ((new-map (copy-keymap outline-old-map
)))
131 (define-key new-map
"\C-c"
132 (lookup-key outline-mode-map
"\C-c"))
133 (use-local-map new-map
))
134 (make-local-variable 'outline-regexp
)
135 (setq outline-regexp
"[ \t]*/\\*")
136 (make-local-variable 'outline-heading-end-regexp
)
137 (setq outline-heading-end-regexp
"\\*/[^\n\^M]*[\n\^M]")
138 (run-hooks 'outline-minor-mode-hook
))
140 (setq selective-display nil
)
141 (use-local-map outline-old-map
))))
143 (defun outline-level ()
144 "Return the depth to which a statement is nested in the outline.
145 Point must be at the beginning of a header line. This is actually
146 the column number of the end of what `outline-regexp matches'."
148 (looking-at outline-regexp
)
149 (save-excursion (goto-char (match-end 0)) (current-column))))
151 (defun outline-next-preface ()
152 "Skip forward to just before the next heading line."
153 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp
"\\)")
155 (goto-char (match-beginning 0)))
156 (if (memq (preceding-char) '(?
\n ?\^M
))
159 (defun outline-next-heading ()
160 "Move to the next (possibly invisible) heading line."
162 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp
"\\)")
164 (goto-char (1+ (match-beginning 0)))))
166 (defun outline-back-to-heading ()
167 "Move to previous (possibly invisible) heading line,
168 or to the beginning of this line if it is a heading line."
170 (or (outline-on-heading-p)
171 (re-search-backward (concat "^\\(" outline-regexp
"\\)") nil
'move
)))
173 (defun outline-on-heading-p ()
174 "Return T if point is on a header line."
177 (and (eq (preceding-char) ?
\n)
178 (looking-at outline-regexp
))))
180 (defun outline-end-of-heading ()
181 (if (re-search-forward outline-heading-end-regexp nil
'move
)
184 (defun outline-next-visible-heading (arg)
185 "Move to the next visible heading line.
186 With argument, repeats or can move backward if negative.
187 A heading line is one that starts with a `*' (or that
188 `outline-regexp' matches)."
193 (re-search-forward (concat "^\\(" outline-regexp
"\\)") nil nil arg
)
196 (defun outline-previous-visible-heading (arg)
197 "Move to the previous heading line.
198 With argument, repeats or can move forward if negative.
199 A heading line is one that starts with a `*' (or that
200 `outline-regexp' matches)."
202 (outline-next-visible-heading (- arg
)))
204 (defun outline-flag-region (from to flag
)
205 "Hides or shows lines from FROM to TO, according to FLAG.
206 If FLAG is `\\n' (newline character) then text is shown,
207 while if FLAG is `\\^M' (control-M) the text is hidden."
208 (let (buffer-read-only
209 (modp (buffer-modified-p)))
211 (subst-char-in-region from to
212 (if (= flag ?
\n) ?\^M ?
\n)
214 (set-buffer-modified-p modp
))))
217 "Hide the body directly following this heading."
219 (outline-back-to-heading)
220 (outline-end-of-heading)
222 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M
)))
225 "Show the body directly following this heading."
228 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?
\n)))
231 "Hide all of buffer except headings."
233 (hide-region-body (point-min) (point-max)))
235 (defun hide-region-body (start end
)
236 "Hide all body lines in the region, but not headings."
239 (narrow-to-region start end
)
240 (goto-char (point-min))
241 (if (outline-on-heading-p)
242 (outline-end-of-heading))
244 (outline-flag-region (point)
245 (progn (outline-next-preface) (point)) ?\^M
)
249 (if (looking-at "[\n\^M][\n\^M]")
251 (outline-end-of-heading)))))))
254 "Show all of the text in the buffer."
256 (outline-flag-region (point-min) (point-max) ?
\n))
258 (defun hide-subtree ()
259 "Hide everything after this heading at deeper levels."
261 (outline-flag-subtree ?\^M
))
263 (defun hide-leaves ()
264 "Hide all body after this heading at deeper levels."
266 (outline-back-to-heading)
267 (outline-end-of-heading)
268 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
270 (defun show-subtree ()
271 "Show everything after this heading at deeper levels."
273 (outline-flag-subtree ?
\n))
275 (defun outline-flag-subtree (flag)
277 (outline-back-to-heading)
278 (outline-end-of-heading)
279 (outline-flag-region (point)
280 (progn (outline-end-of-subtree) (point))
283 (defun outline-end-of-subtree ()
284 (outline-back-to-heading)
285 (let ((opoint (point))
287 (level (outline-level)))
288 (while (and (not (eobp))
289 (or first
(> (outline-level) level
)))
291 (outline-next-heading))
293 (if (memq (preceding-char) '(?
\n ?\^M
))
296 (defun show-branches ()
297 "Show all subheadings of this heading, but not their bodies."
299 (show-children 1000))
301 (defun show-children (&optional level
)
302 "Show all direct subheadings of this heading.
303 Prefix arg LEVEL is how many levels below the current level should be shown.
304 Default is enough to cause the following heading to appear."
307 (if level
(prefix-numeric-value level
)
310 (let ((start-level (outline-level)))
311 (outline-next-heading)
312 (max 1 (- (outline-level) start-level
))))))
316 (setq level
(+ level
(outline-level)))
317 (narrow-to-region (point)
318 (progn (outline-end-of-subtree) (1+ (point))))
319 (goto-char (point-min))
320 (while (and (not (eobp))
322 (outline-next-heading)
324 (if (<= (outline-level) level
)
326 (outline-flag-region (save-excursion
328 (if (memq (preceding-char) '(?
\n ?\^M
))
331 (progn (outline-end-of-heading) (point))
334 (defun outline-up-heading (arg)
335 "Move to the heading line of which the present line is a subheading.
336 With argument, move up ARG levels."
338 (outline-back-to-heading)
339 (if (eq (outline-level) 1)
341 (while (and (> (outline-level) 1)
344 (let ((present-level (outline-level)))
345 (while (not (< (outline-level) present-level
))
346 (outline-previous-visible-heading 1))
347 (setq arg
(- arg
1)))))
349 (defun outline-forward-same-level (arg)
350 "Move forward to the ARG'th subheading from here of the same level as the
351 present one. It stops at the first and last subheadings of a superior heading."
353 (outline-back-to-heading)
355 (let ((point-to-move-to (save-excursion
356 (outline-get-next-sibling))))
359 (goto-char point-to-move-to
)
365 (defun outline-get-next-sibling ()
366 "Position the point at the next heading of the same level,
367 and return that position or nil if it cannot be found."
368 (let ((level (outline-level)))
369 (outline-next-visible-heading 1)
370 (while (and (> (outline-level) level
)
372 (outline-next-visible-heading 1))
373 (if (< (outline-level) level
)
377 (defun outline-backward-same-level (arg)
378 "Move backward to the ARG'th subheading from here of the same level as the
379 present one. It stops at the first and last subheadings of a superior heading."
381 (outline-back-to-heading)
383 (let ((point-to-move-to (save-excursion
384 (outline-get-last-sibling))))
387 (goto-char point-to-move-to
)
393 (defun outline-get-last-sibling ()
394 "Position the point at the previous heading of the same level,
395 and return that position or nil if it cannot be found."
396 (let ((level (outline-level)))
397 (outline-previous-visible-heading 1)
398 (while (and (> (outline-level) level
)
400 (outline-previous-visible-heading 1))
401 (if (< (outline-level) level
)
405 ;;; outline.el ends here