]> code.delx.au - gnu-emacs/blob - lisp/textmodes/ooutline.el
(outline-minor-mode): Add autoload cookie.
[gnu-emacs] / lisp / textmodes / ooutline.el
1 ;;; outline.el --- outline mode commands for Emacs
2
3 ;; Copyright (C) 1986 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
22
23 ;;; Commentary:
24
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.
28
29 ;;; Code:
30
31 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
32
33 (defvar outline-regexp "[*\^l]+"
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.")
38
39 (defvar outline-heading-end-regexp "[\n\^M]"
40 "*Regular expression to match the end of a heading line.
41 You can assume that point is at the beginning of a heading when this
42 regexp is searched for. The heading ends at the end of the match.
43 The recommended way to set this is with a \"Local Variables:\" list
44 in the file it applies to.")
45
46 (defvar outline-mode-map nil "")
47
48 (if outline-mode-map
49 nil
50 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
51 (define-key outline-mode-map "\C-c\C-n" 'outline-next-visible-heading)
52 (define-key outline-mode-map "\C-c\C-p" 'outline-previous-visible-heading)
53 (define-key outline-mode-map "\C-c\C-i" 'show-children)
54 (define-key outline-mode-map "\C-c\C-s" 'show-subtree)
55 (define-key outline-mode-map "\C-c\C-h" 'hide-subtree)
56 (define-key outline-mode-map "\C-c\C-u" 'outline-up-heading)
57 (define-key outline-mode-map "\C-c\C-f" 'outline-forward-same-level)
58 (define-key outline-mode-map "\C-c\C-b" 'outline-backward-same-level))
59
60 (defvar outline-minor-mode nil
61 "Non-nil if using Outline mode as a minor mode of some other mode.")
62 (make-variable-buffer-local 'outline-minor-mode)
63 (put 'outline-minor-mode 'permanent-local t)
64 (setq minor-mode-alist (append minor-mode-alist
65 (list '(outline-minor-mode " Outl"))))
66
67 ;;;###autoload
68 (defun outline-mode ()
69 "Set major mode for editing outlines with selective display.
70 Headings are lines which start with asterisks: one for major headings,
71 two for subheadings, etc. Lines not starting with asterisks are body lines.
72
73 Body text or subheadings under a heading can be made temporarily
74 invisible, or visible again. Invisible lines are attached to the end
75 of the heading, so they move with it, if the line is killed and yanked
76 back. A heading with text hidden under it is marked with an ellipsis (...).
77
78 Commands:\\<outline-mode-map>
79 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
80 \\[outline-previous-visible-heading] outline-previous-visible-heading
81 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
82 \\[outline-backward-same-level] outline-backward-same-level
83 \\[outline-up-heading] outline-up-heading move from subheading to heading
84
85 M-x hide-body make all text invisible (not headings).
86 M-x show-all make everything in buffer visible.
87
88 The remaining commands are used when point is on a heading line.
89 They apply to some of the body or subheadings of that heading.
90 \\[hide-subtree] hide-subtree make body and subheadings invisible.
91 \\[show-subtree] show-subtree make body and subheadings visible.
92 \\[show-children] show-children make direct subheadings visible.
93 No effect on body, or subheadings 2 or more levels down.
94 With arg N, affects subheadings N levels down.
95 M-x hide-entry make immediately following body invisible.
96 M-x show-entry make it visible.
97 M-x hide-leaves make body under heading and under its subheadings invisible.
98 The subheadings remain visible.
99 M-x show-branches make all subheadings at all levels visible.
100
101 The variable `outline-regexp' can be changed to control what is a heading.
102 A line is a heading if `outline-regexp' matches something at the
103 beginning of the line. The longer the match, the deeper the level.
104
105 Turning on outline mode calls the value of `text-mode-hook' and then of
106 `outline-mode-hook', if they are non-nil."
107 (interactive)
108 (kill-all-local-variables)
109 (setq selective-display t)
110 (use-local-map outline-mode-map)
111 (setq mode-name "Outline")
112 (setq major-mode 'outline-mode)
113 (define-abbrev-table 'text-mode-abbrev-table ())
114 (setq local-abbrev-table text-mode-abbrev-table)
115 (set-syntax-table text-mode-syntax-table)
116 (make-local-variable 'paragraph-start)
117 (setq paragraph-start (concat paragraph-start "\\|^\\("
118 outline-regexp "\\)"))
119 ;; Inhibit auto-filling of header lines.
120 (make-local-variable 'auto-fill-inhibit-regexp)
121 (setq auto-fill-inhibit-regexp outline-regexp)
122 (make-local-variable 'paragraph-separate)
123 (setq paragraph-separate (concat paragraph-separate "\\|^\\("
124 outline-regexp "\\)"))
125 (run-hooks 'text-mode-hook 'outline-mode-hook))
126
127 (defvar outline-minor-mode-map nil)
128 (if outline-minor-mode-map
129 nil
130 (setq outline-minor-mode-map (make-sparse-keymap))
131 (define-key outline-minor-mode-map "\C-c"
132 (lookup-key outline-mode-map "\C-c")))
133
134 (or (assq 'outline-minor-mode minor-mode-map-alist)
135 (setq minor-mode-map-alist
136 (cons (cons 'outline-minor-mode outline-minor-mode-map)
137 minor-mode-map-alist)))
138
139 ;;;###autoload
140 (defun outline-minor-mode (&optional arg)
141 "Toggle Outline minor mode.
142 With arg, turn Outline minor mode on if arg is positive, off otherwise.
143 See the command `outline-mode' for more information on this mode."
144 (interactive "P")
145 (setq outline-minor-mode
146 (if (null arg) (not outline-minor-mode)
147 (> (prefix-numeric-value arg) 0)))
148 (if outline-minor-mode
149 (progn
150 (setq selective-display t)
151 (run-hooks 'outline-minor-mode-hook))
152 (setq selective-display nil)))
153 \f
154 (defun outline-level ()
155 "Return the depth to which a statement is nested in the outline.
156 Point must be at the beginning of a header line. This is actually
157 the column number of the end of what `outline-regexp matches'."
158 (save-excursion
159 (looking-at outline-regexp)
160 (save-excursion (goto-char (match-end 0)) (current-column))))
161
162 (defun outline-next-preface ()
163 "Skip forward to just before the next heading line."
164 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
165 nil 'move)
166 (goto-char (match-beginning 0)))
167 (if (memq (preceding-char) '(?\n ?\^M))
168 (forward-char -1)))
169
170 (defun outline-next-heading ()
171 "Move to the next (possibly invisible) heading line."
172 (interactive)
173 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
174 nil 'move)
175 (goto-char (1+ (match-beginning 0)))))
176
177 (defun outline-back-to-heading ()
178 "Move to previous (possibly invisible) heading line,
179 or to the beginning of this line if it is a heading line."
180 (beginning-of-line)
181 (or (outline-on-heading-p)
182 (re-search-backward (concat "^\\(" outline-regexp "\\)") nil 'move)))
183
184 (defun outline-on-heading-p ()
185 "Return T if point is on a header line."
186 (save-excursion
187 (beginning-of-line)
188 (and (eq (preceding-char) ?\n)
189 (looking-at outline-regexp))))
190
191 (defun outline-end-of-heading ()
192 (if (re-search-forward outline-heading-end-regexp nil 'move)
193 (forward-char -1)))
194
195 (defun outline-next-visible-heading (arg)
196 "Move to the next visible heading line.
197 With argument, repeats or can move backward if negative.
198 A heading line is one that starts with a `*' (or that
199 `outline-regexp' matches)."
200 (interactive "p")
201 (if (< arg 0)
202 (beginning-of-line)
203 (end-of-line))
204 (re-search-forward (concat "^\\(" outline-regexp "\\)") nil nil arg)
205 (beginning-of-line))
206
207 (defun outline-previous-visible-heading (arg)
208 "Move to the previous heading line.
209 With argument, repeats or can move forward if negative.
210 A heading line is one that starts with a `*' (or that
211 `outline-regexp' matches)."
212 (interactive "p")
213 (outline-next-visible-heading (- arg)))
214
215 (defun outline-flag-region (from to flag)
216 "Hides or shows lines from FROM to TO, according to FLAG.
217 If FLAG is `\\n' (newline character) then text is shown,
218 while if FLAG is `\\^M' (control-M) the text is hidden."
219 (let (buffer-read-only)
220 (subst-char-in-region from to
221 (if (= flag ?\n) ?\^M ?\n)
222 flag t)))
223 \f
224 (defun hide-entry ()
225 "Hide the body directly following this heading."
226 (interactive)
227 (outline-back-to-heading)
228 (outline-end-of-heading)
229 (save-excursion
230 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M)))
231
232 (defun show-entry ()
233 "Show the body directly following this heading."
234 (interactive)
235 (save-excursion
236 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\n)))
237
238 (defun hide-body ()
239 "Hide all of buffer except headings."
240 (interactive)
241 (hide-region-body (point-min) (point-max)))
242
243 (defun hide-region-body (start end)
244 "Hide all body lines in the region, but not headings."
245 (save-excursion
246 (save-restriction
247 (narrow-to-region start end)
248 (goto-char (point-min))
249 (if (outline-on-heading-p)
250 (outline-end-of-heading))
251 (while (not (eobp))
252 (outline-flag-region (point)
253 (progn (outline-next-preface) (point)) ?\^M)
254 (if (not (eobp))
255 (progn
256 (forward-char
257 (if (looking-at "[\n\^M][\n\^M]")
258 2 1))
259 (outline-end-of-heading)))))))
260
261 (defun show-all ()
262 "Show all of the text in the buffer."
263 (interactive)
264 (outline-flag-region (point-min) (point-max) ?\n))
265
266 (defun hide-subtree ()
267 "Hide everything after this heading at deeper levels."
268 (interactive)
269 (outline-flag-subtree ?\^M))
270
271 (defun hide-leaves ()
272 "Hide all body after this heading at deeper levels."
273 (interactive)
274 (outline-back-to-heading)
275 (outline-end-of-heading)
276 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
277
278 (defun show-subtree ()
279 "Show everything after this heading at deeper levels."
280 (interactive)
281 (outline-flag-subtree ?\n))
282
283 (defun outline-flag-subtree (flag)
284 (save-excursion
285 (outline-back-to-heading)
286 (outline-end-of-heading)
287 (outline-flag-region (point)
288 (progn (outline-end-of-subtree) (point))
289 flag)))
290
291 (defun outline-end-of-subtree ()
292 (outline-back-to-heading)
293 (let ((opoint (point))
294 (first t)
295 (level (outline-level)))
296 (while (and (not (eobp))
297 (or first (> (outline-level) level)))
298 (setq first nil)
299 (outline-next-heading))
300 (forward-char -1)
301 (if (memq (preceding-char) '(?\n ?\^M))
302 (forward-char -1))))
303 \f
304 (defun show-branches ()
305 "Show all subheadings of this heading, but not their bodies."
306 (interactive)
307 (show-children 1000))
308
309 (defun show-children (&optional level)
310 "Show all direct subheadings of this heading.
311 Prefix arg LEVEL is how many levels below the current level should be shown.
312 Default is enough to cause the following heading to appear."
313 (interactive "P")
314 (setq level
315 (if level (prefix-numeric-value level)
316 (save-excursion
317 (beginning-of-line)
318 (let ((start-level (outline-level)))
319 (outline-next-heading)
320 (max 1 (- (outline-level) start-level))))))
321 (save-excursion
322 (save-restriction
323 (beginning-of-line)
324 (setq level (+ level (outline-level)))
325 (narrow-to-region (point)
326 (progn (outline-end-of-subtree) (1+ (point))))
327 (goto-char (point-min))
328 (while (and (not (eobp))
329 (progn
330 (outline-next-heading)
331 (not (eobp))))
332 (if (<= (outline-level) level)
333 (save-excursion
334 (outline-flag-region (save-excursion
335 (forward-char -1)
336 (if (memq (preceding-char) '(?\n ?\^M))
337 (forward-char -1))
338 (point))
339 (progn (outline-end-of-heading) (point))
340 ?\n)))))))
341 \f
342 (defun outline-up-heading (arg)
343 "Move to the heading line of which the present line is a subheading.
344 With argument, move up ARG levels."
345 (interactive "p")
346 (outline-back-to-heading)
347 (if (eq (outline-level) 1)
348 (error ""))
349 (while (and (> (outline-level) 1)
350 (> arg 0)
351 (not (bobp)))
352 (let ((present-level (outline-level)))
353 (while (not (< (outline-level) present-level))
354 (outline-previous-visible-heading 1))
355 (setq arg (- arg 1)))))
356
357 (defun outline-forward-same-level (arg)
358 "Move forward to the ARG'th subheading from here of the same level as the
359 present one. It stops at the first and last subheadings of a superior heading."
360 (interactive "p")
361 (outline-back-to-heading)
362 (while (> arg 0)
363 (let ((point-to-move-to (save-excursion
364 (outline-get-next-sibling))))
365 (if point-to-move-to
366 (progn
367 (goto-char point-to-move-to)
368 (setq arg (1- arg)))
369 (progn
370 (setq arg 0)
371 (error ""))))))
372
373 (defun outline-get-next-sibling ()
374 "Position the point at the next heading of the same level,
375 and return that position or nil if it cannot be found."
376 (let ((level (outline-level)))
377 (outline-next-visible-heading 1)
378 (while (and (> (outline-level) level)
379 (not (eobp)))
380 (outline-next-visible-heading 1))
381 (if (< (outline-level) level)
382 nil
383 (point))))
384
385 (defun outline-backward-same-level (arg)
386 "Move backward to the ARG'th subheading from here of the same level as the
387 present one. It stops at the first and last subheadings of a superior heading."
388 (interactive "p")
389 (outline-back-to-heading)
390 (while (> arg 0)
391 (let ((point-to-move-to (save-excursion
392 (outline-get-last-sibling))))
393 (if point-to-move-to
394 (progn
395 (goto-char point-to-move-to)
396 (setq arg (1- arg)))
397 (progn
398 (setq arg 0)
399 (error ""))))))
400
401 (defun outline-get-last-sibling ()
402 "Position the point at the previous heading of the same level,
403 and return that position or nil if it cannot be found."
404 (let ((level (outline-level)))
405 (outline-previous-visible-heading 1)
406 (while (and (> (outline-level) level)
407 (not (bobp)))
408 (outline-previous-visible-heading 1))
409 (if (< (outline-level) level)
410 nil
411 (point))))
412
413 (provide 'outline)
414
415 ;;; outline.el ends here