Mercurial > emacs
annotate lisp/textmodes/ooutline.el @ 3311:5d06123fc1df
* Makefile.in: (${archlibdir}): Use `(cd foo && pwd)` instead of
`(cd foo ; pwd)` to get the canonical name of a directory; cd
might fail, and have pwd print out the current directory.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sun, 30 May 1993 19:40:39 +0000 |
parents | 7c4226d1ea65 |
children | 898d7a33c038 |
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 | |
2900
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
39 (defvar outline-heading-end-regexp "[\n\^M]" |
234 | 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.") | |
2934
7c4226d1ea65
(outline-minor-mode): Make var permanent local in all buffers.
Richard M. Stallman <rms@gnu.org>
parents:
2900
diff
changeset
|
62 (make-variable-buffer-local 'outline-minor-mode) |
7c4226d1ea65
(outline-minor-mode): Make var permanent local in all buffers.
Richard M. Stallman <rms@gnu.org>
parents:
2900
diff
changeset
|
63 (put 'outline-minor-mode 'permanent-local t) |
234 | 64 (setq minor-mode-alist (append minor-mode-alist |
65 (list '(outline-minor-mode " Outl")))) | |
66 | |
258 | 67 ;;;###autoload |
234 | 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 | |
2900
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
127 (defvar outline-minor-mode-map nil) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
128 (if outline-minor-mode-map |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
129 nil |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
130 (setq outline-minor-mode-map (make-sparse-keymap)) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
131 (define-key outline-minor-mode-map "\C-c" |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
132 (lookup-key outline-mode-map "\C-c"))) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
133 |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
134 (or (assq 'outline-minor-mode minor-mode-map-alist) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
135 (setq minor-mode-map-alist |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
136 (cons (cons 'outline-minor-mode outline-minor-mode-map) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
137 minor-mode-map-alist))) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
138 |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
139 (defun outline-minor-mode (&optional arg) |
2934
7c4226d1ea65
(outline-minor-mode): Make var permanent local in all buffers.
Richard M. Stallman <rms@gnu.org>
parents:
2900
diff
changeset
|
140 "Toggle Outline minor mode. |
7c4226d1ea65
(outline-minor-mode): Make var permanent local in all buffers.
Richard M. Stallman <rms@gnu.org>
parents:
2900
diff
changeset
|
141 With arg, turn Outline minor mode on if arg is positive, off otherwise. |
7c4226d1ea65
(outline-minor-mode): Make var permanent local in all buffers.
Richard M. Stallman <rms@gnu.org>
parents:
2900
diff
changeset
|
142 See the command `outline-mode' for more information on this mode." |
234 | 143 (interactive "P") |
144 (setq outline-minor-mode | |
145 (if (null arg) (not outline-minor-mode) | |
146 (> (prefix-numeric-value arg) 0))) | |
147 (if outline-minor-mode | |
148 (progn | |
149 (setq selective-display t) | |
150 (run-hooks 'outline-minor-mode-hook)) | |
2900
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
151 (setq selective-display nil))) |
234 | 152 |
153 (defun outline-level () | |
154 "Return the depth to which a statement is nested in the outline. | |
155 Point must be at the beginning of a header line. This is actually | |
156 the column number of the end of what `outline-regexp matches'." | |
157 (save-excursion | |
158 (looking-at outline-regexp) | |
159 (save-excursion (goto-char (match-end 0)) (current-column)))) | |
160 | |
161 (defun outline-next-preface () | |
162 "Skip forward to just before the next heading line." | |
163 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)") | |
164 nil 'move) | |
165 (goto-char (match-beginning 0))) | |
166 (if (memq (preceding-char) '(?\n ?\^M)) | |
167 (forward-char -1))) | |
168 | |
169 (defun outline-next-heading () | |
170 "Move to the next (possibly invisible) heading line." | |
171 (interactive) | |
172 (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)") | |
173 nil 'move) | |
174 (goto-char (1+ (match-beginning 0))))) | |
175 | |
176 (defun outline-back-to-heading () | |
177 "Move to previous (possibly invisible) heading line, | |
178 or to the beginning of this line if it is a heading line." | |
179 (beginning-of-line) | |
180 (or (outline-on-heading-p) | |
181 (re-search-backward (concat "^\\(" outline-regexp "\\)") nil 'move))) | |
182 | |
183 (defun outline-on-heading-p () | |
184 "Return T if point is on a header line." | |
185 (save-excursion | |
186 (beginning-of-line) | |
187 (and (eq (preceding-char) ?\n) | |
188 (looking-at outline-regexp)))) | |
189 | |
190 (defun outline-end-of-heading () | |
191 (if (re-search-forward outline-heading-end-regexp nil 'move) | |
192 (forward-char -1))) | |
193 | |
194 (defun outline-next-visible-heading (arg) | |
195 "Move to the next visible heading line. | |
196 With argument, repeats or can move backward if negative. | |
197 A heading line is one that starts with a `*' (or that | |
198 `outline-regexp' matches)." | |
199 (interactive "p") | |
200 (if (< arg 0) | |
201 (beginning-of-line) | |
202 (end-of-line)) | |
203 (re-search-forward (concat "^\\(" outline-regexp "\\)") nil nil arg) | |
204 (beginning-of-line)) | |
205 | |
206 (defun outline-previous-visible-heading (arg) | |
207 "Move to the previous heading line. | |
208 With argument, repeats or can move forward if negative. | |
209 A heading line is one that starts with a `*' (or that | |
210 `outline-regexp' matches)." | |
211 (interactive "p") | |
212 (outline-next-visible-heading (- arg))) | |
213 | |
214 (defun outline-flag-region (from to flag) | |
215 "Hides or shows lines from FROM to TO, according to FLAG. | |
216 If FLAG is `\\n' (newline character) then text is shown, | |
217 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
|
218 (let (buffer-read-only |
2e4dd37cf7b2
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
659
diff
changeset
|
219 (modp (buffer-modified-p))) |
234 | 220 (unwind-protect |
221 (subst-char-in-region from to | |
222 (if (= flag ?\n) ?\^M ?\n) | |
223 flag) | |
224 (set-buffer-modified-p modp)))) | |
225 | |
226 (defun hide-entry () | |
227 "Hide the body directly following this heading." | |
228 (interactive) | |
229 (outline-back-to-heading) | |
230 (outline-end-of-heading) | |
231 (save-excursion | |
232 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M))) | |
233 | |
234 (defun show-entry () | |
235 "Show the body directly following this heading." | |
236 (interactive) | |
237 (save-excursion | |
238 (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\n))) | |
239 | |
240 (defun hide-body () | |
241 "Hide all of buffer except headings." | |
242 (interactive) | |
243 (hide-region-body (point-min) (point-max))) | |
244 | |
245 (defun hide-region-body (start end) | |
246 "Hide all body lines in the region, but not headings." | |
247 (save-excursion | |
248 (save-restriction | |
249 (narrow-to-region start end) | |
250 (goto-char (point-min)) | |
251 (if (outline-on-heading-p) | |
252 (outline-end-of-heading)) | |
253 (while (not (eobp)) | |
254 (outline-flag-region (point) | |
255 (progn (outline-next-preface) (point)) ?\^M) | |
256 (if (not (eobp)) | |
257 (progn | |
258 (forward-char | |
259 (if (looking-at "[\n\^M][\n\^M]") | |
260 2 1)) | |
261 (outline-end-of-heading))))))) | |
262 | |
263 (defun show-all () | |
264 "Show all of the text in the buffer." | |
265 (interactive) | |
266 (outline-flag-region (point-min) (point-max) ?\n)) | |
267 | |
268 (defun hide-subtree () | |
269 "Hide everything after this heading at deeper levels." | |
270 (interactive) | |
271 (outline-flag-subtree ?\^M)) | |
272 | |
273 (defun hide-leaves () | |
274 "Hide all body after this heading at deeper levels." | |
275 (interactive) | |
276 (outline-back-to-heading) | |
277 (outline-end-of-heading) | |
278 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))) | |
279 | |
280 (defun show-subtree () | |
281 "Show everything after this heading at deeper levels." | |
282 (interactive) | |
283 (outline-flag-subtree ?\n)) | |
284 | |
285 (defun outline-flag-subtree (flag) | |
286 (save-excursion | |
287 (outline-back-to-heading) | |
288 (outline-end-of-heading) | |
289 (outline-flag-region (point) | |
290 (progn (outline-end-of-subtree) (point)) | |
291 flag))) | |
292 | |
293 (defun outline-end-of-subtree () | |
294 (outline-back-to-heading) | |
295 (let ((opoint (point)) | |
296 (first t) | |
297 (level (outline-level))) | |
298 (while (and (not (eobp)) | |
299 (or first (> (outline-level) level))) | |
300 (setq first nil) | |
301 (outline-next-heading)) | |
302 (forward-char -1) | |
303 (if (memq (preceding-char) '(?\n ?\^M)) | |
304 (forward-char -1)))) | |
305 | |
306 (defun show-branches () | |
307 "Show all subheadings of this heading, but not their bodies." | |
308 (interactive) | |
309 (show-children 1000)) | |
310 | |
311 (defun show-children (&optional level) | |
312 "Show all direct subheadings of this heading. | |
313 Prefix arg LEVEL is how many levels below the current level should be shown. | |
314 Default is enough to cause the following heading to appear." | |
315 (interactive "P") | |
316 (setq level | |
317 (if level (prefix-numeric-value level) | |
318 (save-excursion | |
319 (beginning-of-line) | |
320 (let ((start-level (outline-level))) | |
321 (outline-next-heading) | |
322 (max 1 (- (outline-level) start-level)))))) | |
323 (save-excursion | |
324 (save-restriction | |
325 (beginning-of-line) | |
326 (setq level (+ level (outline-level))) | |
327 (narrow-to-region (point) | |
328 (progn (outline-end-of-subtree) (1+ (point)))) | |
329 (goto-char (point-min)) | |
330 (while (and (not (eobp)) | |
331 (progn | |
332 (outline-next-heading) | |
333 (not (eobp)))) | |
334 (if (<= (outline-level) level) | |
335 (save-excursion | |
336 (outline-flag-region (save-excursion | |
337 (forward-char -1) | |
338 (if (memq (preceding-char) '(?\n ?\^M)) | |
339 (forward-char -1)) | |
340 (point)) | |
341 (progn (outline-end-of-heading) (point)) | |
342 ?\n))))))) | |
343 | |
344 (defun outline-up-heading (arg) | |
345 "Move to the heading line of which the present line is a subheading. | |
346 With argument, move up ARG levels." | |
347 (interactive "p") | |
348 (outline-back-to-heading) | |
349 (if (eq (outline-level) 1) | |
350 (error "")) | |
351 (while (and (> (outline-level) 1) | |
352 (> arg 0) | |
353 (not (bobp))) | |
354 (let ((present-level (outline-level))) | |
355 (while (not (< (outline-level) present-level)) | |
356 (outline-previous-visible-heading 1)) | |
357 (setq arg (- arg 1))))) | |
358 | |
359 (defun outline-forward-same-level (arg) | |
360 "Move forward to the ARG'th subheading from here of the same level as the | |
361 present one. It stops at the first and last subheadings of a superior heading." | |
362 (interactive "p") | |
363 (outline-back-to-heading) | |
364 (while (> arg 0) | |
365 (let ((point-to-move-to (save-excursion | |
366 (outline-get-next-sibling)))) | |
367 (if point-to-move-to | |
368 (progn | |
369 (goto-char point-to-move-to) | |
370 (setq arg (1- arg))) | |
371 (progn | |
372 (setq arg 0) | |
373 (error "")))))) | |
374 | |
375 (defun outline-get-next-sibling () | |
376 "Position the point at the next heading of the same level, | |
377 and return that position or nil if it cannot be found." | |
378 (let ((level (outline-level))) | |
379 (outline-next-visible-heading 1) | |
380 (while (and (> (outline-level) level) | |
381 (not (eobp))) | |
382 (outline-next-visible-heading 1)) | |
383 (if (< (outline-level) level) | |
384 nil | |
385 (point)))) | |
386 | |
387 (defun outline-backward-same-level (arg) | |
388 "Move backward to the ARG'th subheading from here of the same level as the | |
389 present one. It stops at the first and last subheadings of a superior heading." | |
390 (interactive "p") | |
391 (outline-back-to-heading) | |
392 (while (> arg 0) | |
393 (let ((point-to-move-to (save-excursion | |
394 (outline-get-last-sibling)))) | |
395 (if point-to-move-to | |
396 (progn | |
397 (goto-char point-to-move-to) | |
398 (setq arg (1- arg))) | |
399 (progn | |
400 (setq arg 0) | |
401 (error "")))))) | |
402 | |
403 (defun outline-get-last-sibling () | |
404 "Position the point at the previous heading of the same level, | |
405 and return that position or nil if it cannot be found." | |
406 (let ((level (outline-level))) | |
407 (outline-previous-visible-heading 1) | |
408 (while (and (> (outline-level) level) | |
409 (not (bobp))) | |
410 (outline-previous-visible-heading 1)) | |
411 (if (< (outline-level) level) | |
412 nil | |
413 (point)))) | |
414 | |
2900
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
415 (provide 'outline) |
443ebd8f59d2
(outline-heading-end-regexp): Fix typo.
Richard M. Stallman <rms@gnu.org>
parents:
2308
diff
changeset
|
416 |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
417 ;;; outline.el ends here |