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