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