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