comparison lisp/textmodes/outline.el @ 10950:d1990c517cc9

Initial revision
author Richard M. Stallman <rms@gnu.org>
date Fri, 10 Mar 1995 04:30:57 +0000
parents
children 356f658dbd65
comparison
equal deleted inserted replaced
10949:99c9f475a355 10950:d1990c517cc9
1 ;;; outline.el --- outline mode commands for Emacs
2 ;; Copyright (C) 1986, 1993, 1994 Free Software Foundation, Inc.
3
4 ;; Maintainer: FSF
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 ;;; Commentary:
23
24 ;; This package is a major mode for editing outline-format documents.
25 ;; An outline can be `abstracted' to show headers at any given level,
26 ;; with all stuff below hidden. See the Emacs manual for details.
27
28 ;;; Code:
29
30 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
31
32 (defvar outline-regexp nil
33 "*Regular expression to match the beginning of a heading.
34 Any line whose beginning matches this regexp is considered to start a heading.
35 The recommended way to set this is with a Local Variables: list
36 in the file it applies to. See also outline-heading-end-regexp.")
37
38 ;; Can't initialize this in the defvar above -- some major modes have
39 ;; already assigned a local value to it.
40 (or (default-value 'outline-regexp)
41 (setq-default outline-regexp "[*\^L]+"))
42
43 (defvar outline-heading-end-regexp "\n"
44 "*Regular expression to match the end of a heading line.
45 You can assume that point is at the beginning of a heading when this
46 regexp is searched for. The heading ends at the end of the match.
47 The recommended way to set this is with a `Local Variables:' list
48 in the file it applies to.")
49
50 (defvar outline-mode-prefix-map nil)
51
52 (if outline-mode-prefix-map
53 nil
54 (setq outline-mode-prefix-map (make-sparse-keymap))
55 (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
56 (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
57 (define-key outline-mode-prefix-map "\C-i" 'show-children)
58 (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
59 (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
60 (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
61 (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
62 (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
63 (define-key outline-mode-prefix-map "\C-t" 'hide-body)
64 (define-key outline-mode-prefix-map "\C-a" 'show-all)
65 (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
66 (define-key outline-mode-prefix-map "\C-e" 'show-entry)
67 (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
68 (define-key outline-mode-prefix-map "\C-k" 'show-branches)
69 (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
70 (define-key outline-mode-prefix-map "\C-o" 'hide-other))
71
72 (defvar outline-mode-menu-bar-map nil)
73 (if outline-mode-menu-bar-map
74 nil
75 (setq outline-mode-menu-bar-map (make-sparse-keymap))
76
77 (define-key outline-mode-menu-bar-map [hide]
78 (cons "Hide" (make-sparse-keymap "Hide")))
79
80 (define-key outline-mode-menu-bar-map [hide hide-other]
81 '("Hide Other" . hide-other))
82 (define-key outline-mode-menu-bar-map [hide hide-sublevels]
83 '("Hide Sublevels" . hide-sublevels))
84 (define-key outline-mode-menu-bar-map [hide hide-subtree]
85 '("Hide Subtree" . hide-subtree))
86 (define-key outline-mode-menu-bar-map [hide hide-entry]
87 '("Hide Entry" . hide-entry))
88 (define-key outline-mode-menu-bar-map [hide hide-body]
89 '("Hide Body" . hide-body))
90 (define-key outline-mode-menu-bar-map [hide hide-leaves]
91 '("Hide Leaves" . hide-leaves))
92
93 (define-key outline-mode-menu-bar-map [show]
94 (cons "Show" (make-sparse-keymap "Show")))
95
96 (define-key outline-mode-menu-bar-map [show show-subtree]
97 '("Show Subtree" . show-subtree))
98 (define-key outline-mode-menu-bar-map [show show-children]
99 '("Show Children" . show-children))
100 (define-key outline-mode-menu-bar-map [show show-branches]
101 '("Show Branches" . show-branches))
102 (define-key outline-mode-menu-bar-map [show show-entry]
103 '("Show Entry" . show-entry))
104 (define-key outline-mode-menu-bar-map [show show-all]
105 '("Show All" . show-all))
106
107 (define-key outline-mode-menu-bar-map [headings]
108 (cons "Headings" (make-sparse-keymap "Headings")))
109
110 (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
111 '("Previous Same Level" . outline-backward-same-level))
112 (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
113 '("Next Same Level" . outline-forward-same-level))
114 (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
115 '("Previous" . outline-previous-visible-heading))
116 (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
117 '("Next" . outline-next-visible-heading))
118 (define-key outline-mode-menu-bar-map [headings outline-up-heading]
119 '("Up" . outline-up-heading)))
120
121 (defvar outline-mode-map nil "")
122
123 (if outline-mode-map
124 nil
125 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
126 (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
127 (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
128
129 (defvar outline-minor-mode nil
130 "Non-nil if using Outline mode as a minor mode of some other mode.")
131 (make-variable-buffer-local 'outline-minor-mode)
132 (put 'outline-minor-mode 'permanent-local t)
133 (or (assq 'outline-minor-mode minor-mode-alist)
134 (setq minor-mode-alist (append minor-mode-alist
135 (list '(outline-minor-mode " Outl")))))
136
137 (defvar outline-font-lock-keywords
138 '(;; Highlight headings according to the level.
139 ("^\\(\\*+\\)[ \t]*\\(.+\\)?[ \t]*$"
140 (1 font-lock-string-face)
141 (2 (let ((len (- (match-end 1) (match-beginning 1))))
142 (or (cdr (assq len '((1 . font-lock-function-name-face)
143 (2 . font-lock-keyword-face)
144 (3 . font-lock-comment-face))))
145 font-lock-variable-name-face))
146 nil t))
147 ;; Highight citations of the form [1] and [Mar94].
148 ("\\[\\([A-Z][A-Za-z]+\\)*[0-9]+\\]" . font-lock-type-face))
149 "Additional expressions to highlight in Outline mode.")
150
151 ;;;###autoload
152 (defun outline-mode ()
153 "Set major mode for editing outlines with selective display.
154 Headings are lines which start with asterisks: one for major headings,
155 two for subheadings, etc. Lines not starting with asterisks are body lines.
156
157 Body text or subheadings under a heading can be made temporarily
158 invisible, or visible again. Invisible lines are attached to the end
159 of the heading, so they move with it, if the line is killed and yanked
160 back. A heading with text hidden under it is marked with an ellipsis (...).
161
162 Commands:\\<outline-mode-map>
163 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
164 \\[outline-previous-visible-heading] outline-previous-visible-heading
165 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
166 \\[outline-backward-same-level] outline-backward-same-level
167 \\[outline-up-heading] outline-up-heading move from subheading to heading
168
169 \\[hide-body] make all text invisible (not headings).
170 \\[show-all] make everything in buffer visible.
171
172 The remaining commands are used when point is on a heading line.
173 They apply to some of the body or subheadings of that heading.
174 \\[hide-subtree] hide-subtree make body and subheadings invisible.
175 \\[show-subtree] show-subtree make body and subheadings visible.
176 \\[show-children] show-children make direct subheadings visible.
177 No effect on body, or subheadings 2 or more levels down.
178 With arg N, affects subheadings N levels down.
179 \\[hide-entry] make immediately following body invisible.
180 \\[show-entry] make it visible.
181 \\[hide-leaves] make body under heading and under its subheadings invisible.
182 The subheadings remain visible.
183 \\[show-branches] make all subheadings at all levels visible.
184
185 The variable `outline-regexp' can be changed to control what is a heading.
186 A line is a heading if `outline-regexp' matches something at the
187 beginning of the line. The longer the match, the deeper the level.
188
189 Turning on outline mode calls the value of `text-mode-hook' and then of
190 `outline-mode-hook', if they are non-nil."
191 (interactive)
192 (kill-all-local-variables)
193 (use-local-map outline-mode-map)
194 (setq mode-name "Outline")
195 (setq major-mode 'outline-mode)
196 (define-abbrev-table 'text-mode-abbrev-table ())
197 (setq local-abbrev-table text-mode-abbrev-table)
198 (set-syntax-table text-mode-syntax-table)
199 (make-local-variable 'line-move-ignore-invisible)
200 (setq line-move-ignore-invisible t)
201 ;; Cause use of ellipses for invisible text.
202 (setq buffer-invisibility-spec '((t . t)))
203 (make-local-variable 'paragraph-start)
204 (setq paragraph-start (concat paragraph-start "\\|\\("
205 outline-regexp "\\)"))
206 ;; Inhibit auto-filling of header lines.
207 (make-local-variable 'auto-fill-inhibit-regexp)
208 (setq auto-fill-inhibit-regexp outline-regexp)
209 (make-local-variable 'paragraph-separate)
210 (setq paragraph-separate (concat paragraph-separate "\\|\\("
211 outline-regexp "\\)"))
212 (make-local-variable 'font-lock-defaults)
213 (setq font-lock-defaults '(outline-font-lock-keywords t))
214 (make-local-variable 'change-major-mode-hook)
215 (add-hook 'change-major-mode-hook 'show-all)
216 (run-hooks 'text-mode-hook 'outline-mode-hook))
217
218 (defvar outline-minor-mode-prefix "\C-c\C-o"
219 "*Prefix key to use for Outline commands in Outline minor mode.
220 The value of this variable is checked as part of loading Outline mode.
221 After that, changing the prefix key requires manipulating keymaps.")
222
223 (defvar outline-minor-mode-map nil)
224 (if outline-minor-mode-map
225 nil
226 (setq outline-minor-mode-map (make-sparse-keymap))
227 (define-key outline-minor-mode-map [menu-bar]
228 outline-mode-menu-bar-map)
229 (define-key outline-minor-mode-map outline-minor-mode-prefix
230 outline-mode-prefix-map))
231
232 (or (assq 'outline-minor-mode minor-mode-map-alist)
233 (setq minor-mode-map-alist
234 (cons (cons 'outline-minor-mode outline-minor-mode-map)
235 minor-mode-map-alist)))
236
237 ;;;###autoload
238 (defun outline-minor-mode (&optional arg)
239 "Toggle Outline minor mode.
240 With arg, turn Outline minor mode on if arg is positive, off otherwise.
241 See the command `outline-mode' for more information on this mode."
242 (interactive "P")
243 (setq outline-minor-mode
244 (if (null arg) (not outline-minor-mode)
245 (> (prefix-numeric-value arg) 0)))
246 (if outline-minor-mode
247 (progn
248 (make-local-variable 'line-move-ignore-invisible)
249 (setq line-move-ignore-invisible t)
250 ;; Cause use of ellipses for invisible text.
251 (setq buffer-invisibility-spec '((t . t))))
252 (run-hooks 'outline-minor-mode-hook)
253 (setq line-move-ignore-invisible nil)
254 ;; Cause use of ellipses for invisible text.
255 (setq buffer-invisibility-spec t))
256 ;; When turning off outline mode, get rid of any invisible props.
257 (or outline-minor-mode
258 (outline-flag-region (point-min) (point-max) nil))
259 (set-buffer-modified-p (buffer-modified-p)))
260
261 (defvar outline-level 'outline-level
262 "Function of no args to compute a header's nesting level in an outline.
263 It can assume point is at the beginning of a header line.")
264
265 ;; This used to count columns rather than characters, but that made ^L
266 ;; appear to be at level 2 instead of 1. Columns would be better for
267 ;; tab handling, but the default regexp doesn't use tabs, and anyone
268 ;; who changes the regexp can also redefine the outline-level variable
269 ;; as appropriate.
270 (defun outline-level ()
271 "Return the depth to which a statement is nested in the outline.
272 Point must be at the beginning of a header line. This is actually
273 the number of characters that `outline-regexp' matches."
274 (save-excursion
275 (looking-at outline-regexp)
276 (- (match-end 0) (match-beginning 0))))
277
278 (defun outline-next-preface ()
279 "Skip forward to just before the next heading line.
280 If there's no following heading line, stop before the newline
281 at the end of the buffer."
282 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
283 nil 'move)
284 (goto-char (match-beginning 0)))
285 (if (bolp)
286 (forward-char -1)))
287
288 (defun outline-next-heading ()
289 "Move to the next (possibly invisible) heading line."
290 (interactive)
291 (if (re-search-forward (concat "^\\(" outline-regexp "\\)")
292 nil 'move)
293 (goto-char (1+ (match-beginning 0)))))
294
295 (defsubst outline-visible ()
296 "Non-nil if the character after point is visible."
297 (not (get-char-property (point) 'invisible)))
298
299 (defun outline-back-to-heading ()
300 "Move to previous heading line, or beg of this line if it's a heading.
301 Only visible heading lines are considered."
302 (beginning-of-line)
303 (or (outline-on-heading-p)
304 (let (found)
305 (while (not found)
306 (setq found
307 (and (re-search-backward (concat "^\\(" outline-regexp "\\)")
308 nil t)
309 (outline-visible))))
310 found)
311 (error "before first heading")))
312
313 (defun outline-on-heading-p ()
314 "Return t if point is on a (visible) heading line."
315 (save-excursion
316 (beginning-of-line)
317 (and (bolp) (outline-visible)
318 (looking-at outline-regexp))))
319
320 (defun outline-end-of-heading ()
321 (if (re-search-forward outline-heading-end-regexp nil 'move)
322 (forward-char -1)))
323
324 (defun outline-next-visible-heading (arg)
325 "Move to the next visible heading line.
326 With argument, repeats or can move backward if negative.
327 A heading line is one that starts with a `*' (or that
328 `outline-regexp' matches)."
329 (interactive "p")
330 (if (< arg 0)
331 (beginning-of-line)
332 (end-of-line))
333 (let (found)
334 (or (while (and found (> arg 0))
335 (setq found nil)
336 (while (not found)
337 (setq found
338 (and (re-search-backward (concat "^\\(" outline-regexp "\\)")
339 nil t)
340 (outline-visible))))
341 (setq arg (1- arg)))
342 (error "")))
343 (beginning-of-line))
344
345 (defun outline-previous-visible-heading (arg)
346 "Move to the previous heading line.
347 With argument, repeats or can move forward if negative.
348 A heading line is one that starts with a `*' (or that
349 `outline-regexp' matches)."
350 (interactive "p")
351 (outline-next-visible-heading (- arg)))
352
353 (defun outline-flag-region (from to flag)
354 "Hides or shows lines from FROM to TO, according to FLAG.
355 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
356 (let ((inhibit-read-only t))
357 (save-excursion
358 (goto-char from)
359 (end-of-line)
360 (put-text-property (point) to 'invisible flag))))
361
362 (defun hide-entry ()
363 "Hide the body directly following this heading."
364 (interactive)
365 (outline-back-to-heading)
366 (outline-end-of-heading)
367 (save-excursion
368 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
369
370 (defun show-entry ()
371 "Show the body directly following this heading."
372 (interactive)
373 (save-excursion
374 (outline-flag-region (point) (progn (outline-next-preface) (point)) nil)))
375
376 (defun hide-body ()
377 "Hide all of buffer except headings."
378 (interactive)
379 (hide-region-body (point-min) (point-max)))
380
381 (defun hide-region-body (start end)
382 "Hide all body lines in the region, but not headings."
383 (save-excursion
384 (save-restriction
385 (narrow-to-region start end)
386 (goto-char (point-min))
387 (if (outline-on-heading-p)
388 (outline-end-of-heading))
389 (while (not (eobp))
390 (outline-flag-region (point)
391 (progn (outline-next-preface) (point)) t)
392 (if (not (eobp))
393 (progn
394 (forward-char
395 (if (looking-at "\n\n")
396 2 1))
397 (outline-end-of-heading)))))))
398
399 (defun show-all ()
400 "Show all of the text in the buffer."
401 (interactive)
402 (outline-flag-region (point-min) (point-max) nil))
403
404 (defun hide-subtree ()
405 "Hide everything after this heading at deeper levels."
406 (interactive)
407 (outline-flag-subtree t))
408
409 (defun hide-leaves ()
410 "Hide all body after this heading at deeper levels."
411 (interactive)
412 (outline-back-to-heading)
413 (outline-end-of-heading)
414 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
415
416 (defun show-subtree ()
417 "Show everything after this heading at deeper levels."
418 (interactive)
419 (outline-flag-subtree nil))
420
421 (defun hide-sublevels (levels)
422 "Hide everything but the top LEVELS levels of headers, in whole buffer."
423 (interactive "p")
424 (if (< levels 1)
425 (error "Must keep at least one level of headers"))
426 (setq levels (1- levels))
427 (save-excursion
428 (goto-char (point-min))
429 ;; Keep advancing to the next top-level heading.
430 (while (or (and (bobp) (outline-on-heading-p))
431 (outline-next-heading))
432 (let ((end (save-excursion (outline-end-of-subtree) (point))))
433 ;; Hide everything under that.
434 (outline-flag-region (point) end t)
435 ;; Show the first LEVELS levels under that.
436 (if (> levels 0)
437 (show-children levels))
438 ;; Move to the next, since we already found it.
439 (goto-char end)))))
440
441 (defun hide-other ()
442 "Hide everything except for the current body and the parent headings."
443 (interactive)
444 (hide-sublevels 1)
445 (let ((last (point))
446 (pos (point)))
447 (while (save-excursion
448 (and (end-of-line 0)
449 (not (outline-visible))))
450 (save-excursion
451 (beginning-of-line)
452 (if (eq last (point))
453 (progn
454 (outline-next-heading)
455 (outline-flag-region last (point) nil))
456 (show-children)
457 (setq last (point)))))))
458
459 (defun outline-flag-subtree (flag)
460 (save-excursion
461 (outline-back-to-heading)
462 (outline-end-of-heading)
463 (outline-flag-region (point)
464 (progn (outline-end-of-subtree) (point))
465 flag)))
466
467 (defun outline-end-of-subtree ()
468 (outline-back-to-heading)
469 (let ((opoint (point))
470 (first t)
471 (level (funcall outline-level)))
472 (while (and (not (eobp))
473 (or first (> (funcall outline-level) level)))
474 (setq first nil)
475 (outline-next-heading))
476 (if (bolp)
477 (progn
478 ;; Go to end of line before heading
479 (forward-char -1)
480 (if (bolp)
481 ;; leave blank line before heading
482 (forward-char -1))))))
483
484 (defun show-branches ()
485 "Show all subheadings of this heading, but not their bodies."
486 (interactive)
487 (show-children 1000))
488
489 (defun show-children (&optional level)
490 "Show all direct subheadings of this heading.
491 Prefix arg LEVEL is how many levels below the current level should be shown.
492 Default is enough to cause the following heading to appear."
493 (interactive "P")
494 (setq level
495 (if level (prefix-numeric-value level)
496 (save-excursion
497 (outline-back-to-heading)
498 (let ((start-level (funcall outline-level)))
499 (outline-next-heading)
500 (if (eobp)
501 1
502 (max 1 (- (funcall outline-level) start-level)))))))
503 (save-excursion
504 (save-restriction
505 (outline-back-to-heading)
506 (setq level (+ level (funcall outline-level)))
507 (narrow-to-region (point)
508 (progn (outline-end-of-subtree)
509 (if (eobp) (point-max) (1+ (point)))))
510 (goto-char (point-min))
511 (while (and (not (eobp))
512 (progn
513 (outline-next-heading)
514 (not (eobp))))
515 (if (<= (funcall outline-level) level)
516 (save-excursion
517 (outline-flag-region (save-excursion
518 (forward-char -1)
519 (if (bolp)
520 (forward-char -1))
521 (point))
522 (progn (outline-end-of-heading) (point))
523 nil)))))))
524
525 (defun outline-up-heading (arg)
526 "Move to the heading line of which the present line is a subheading.
527 With argument, move up ARG levels."
528 (interactive "p")
529 (outline-back-to-heading)
530 (if (eq (funcall outline-level) 1)
531 (error ""))
532 (while (and (> (funcall outline-level) 1)
533 (> arg 0)
534 (not (bobp)))
535 (let ((present-level (funcall outline-level)))
536 (while (not (< (funcall outline-level) present-level))
537 (outline-previous-visible-heading 1))
538 (setq arg (- arg 1)))))
539
540 (defun outline-forward-same-level (arg)
541 "Move forward to the ARG'th subheading at same level as this one.
542 Stop at the first and last subheadings of a superior heading."
543 (interactive "p")
544 (outline-back-to-heading)
545 (while (> arg 0)
546 (let ((point-to-move-to (save-excursion
547 (outline-get-next-sibling))))
548 (if point-to-move-to
549 (progn
550 (goto-char point-to-move-to)
551 (setq arg (1- arg)))
552 (progn
553 (setq arg 0)
554 (error ""))))))
555
556 (defun outline-get-next-sibling ()
557 "Move to next heading of the same level, and return point or nil if none."
558 (let ((level (funcall outline-level)))
559 (outline-next-visible-heading 1)
560 (while (and (> (funcall outline-level) level)
561 (not (eobp)))
562 (outline-next-visible-heading 1))
563 (if (< (funcall outline-level) level)
564 nil
565 (point))))
566
567 (defun outline-backward-same-level (arg)
568 "Move backward to the ARG'th subheading at same level as this one.
569 Stop at the first and last subheadings of a superior heading."
570 (interactive "p")
571 (outline-back-to-heading)
572 (while (> arg 0)
573 (let ((point-to-move-to (save-excursion
574 (outline-get-last-sibling))))
575 (if point-to-move-to
576 (progn
577 (goto-char point-to-move-to)
578 (setq arg (1- arg)))
579 (progn
580 (setq arg 0)
581 (error ""))))))
582
583 (defun outline-get-last-sibling ()
584 "Move to next heading of the same level, and return point or nil if none."
585 (let ((level (funcall outline-level)))
586 (outline-previous-visible-heading 1)
587 (while (and (> (funcall outline-level) level)
588 (not (bobp)))
589 (outline-previous-visible-heading 1))
590 (if (< (funcall outline-level) level)
591 nil
592 (point))))
593
594 (provide 'outline)
595
596 ;;; outline.el ends here