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