Refill some copyright headers.
[bpt/emacs.git] / lisp / outline.el
CommitLineData
36a77f37
JB
1;;; outline.el --- outline mode commands for Emacs
2
0d30b337 3;; Copyright (C) 1986, 1993, 1994, 1995, 1997, 2000, 2001, 2002,
e9bffc61
GM
4;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5;; Free Software Foundation, Inc.
36a77f37
JB
6
7;; Maintainer: FSF
8;; Keywords: outlines
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
36a77f37 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
36a77f37
JB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
36a77f37
JB
24
25;;; Commentary:
26
27;; This package is a major mode for editing outline-format documents.
28;; An outline can be `abstracted' to show headers at any given level,
29;; with all stuff below hidden. See the Emacs manual for details.
30
31;;; Todo:
32
33;; - subtree-terminators
34;; - better handle comments before function bodies (i.e. heading)
35;; - don't bother hiding whitespace
36
37;;; Code:
38
f63f8737
JB
39(defvar font-lock-warning-face)
40
41
36a77f37 42(defgroup outlines nil
7a5c8253 43 "Support for hierarchical outlining."
36a77f37 44 :prefix "outline-"
eba5b4dd 45 :group 'wp)
36a77f37
JB
46
47(defcustom outline-regexp "[*\^L]+"
1e0e9b38 48 "Regular expression to match the beginning of a heading.
36a77f37
JB
49Any line whose beginning matches this regexp is considered to start a heading.
50Note that Outline mode only checks this regexp at the start of a line,
51so the regexp need not (and usually does not) start with `^'.
52The recommended way to set this is with a Local Variables: list
53in the file it applies to. See also `outline-heading-end-regexp'."
54 :type '(choice regexp (const nil))
55 :group 'outlines)
631c8020 56;;;###autoload(put 'outline-regexp 'safe-local-variable 'string-or-null-p)
36a77f37
JB
57
58(defcustom outline-heading-end-regexp "\n"
1e0e9b38 59 "Regular expression to match the end of a heading line.
36a77f37
JB
60You can assume that point is at the beginning of a heading when this
61regexp is searched for. The heading ends at the end of the match.
62The recommended way to set this is with a `Local Variables:' list
63in the file it applies to."
64 :type 'regexp
65 :group 'outlines)
66
67(defvar outline-mode-prefix-map
68 (let ((map (make-sparse-keymap)))
69 (define-key map "@" 'outline-mark-subtree)
70 (define-key map "\C-n" 'outline-next-visible-heading)
71 (define-key map "\C-p" 'outline-previous-visible-heading)
72 (define-key map "\C-i" 'show-children)
73 (define-key map "\C-s" 'show-subtree)
74 (define-key map "\C-d" 'hide-subtree)
75 (define-key map "\C-u" 'outline-up-heading)
76 (define-key map "\C-f" 'outline-forward-same-level)
77 (define-key map "\C-b" 'outline-backward-same-level)
78 (define-key map "\C-t" 'hide-body)
79 (define-key map "\C-a" 'show-all)
80 (define-key map "\C-c" 'hide-entry)
81 (define-key map "\C-e" 'show-entry)
82 (define-key map "\C-l" 'hide-leaves)
83 (define-key map "\C-k" 'show-branches)
84 (define-key map "\C-q" 'hide-sublevels)
85 (define-key map "\C-o" 'hide-other)
86 (define-key map "\C-^" 'outline-move-subtree-up)
87 (define-key map "\C-v" 'outline-move-subtree-down)
88 (define-key map [(control ?<)] 'outline-promote)
89 (define-key map [(control ?>)] 'outline-demote)
90 (define-key map "\C-m" 'outline-insert-heading)
91 ;; Where to bind outline-cycle ?
92 map))
93
94(defvar outline-mode-menu-bar-map
95 (let ((map (make-sparse-keymap)))
96
97 (define-key map [hide] (cons "Hide" (make-sparse-keymap "Hide")))
98
7cd25617
DN
99 (define-key map [hide hide-other]
100 '(menu-item "Hide Other" hide-other
101 :help "Hide everything except current body and parent and top-level headings"))
102 (define-key map [hide hide-sublevels]
103 '(menu-item "Hide Sublevels" hide-sublevels
104 :help "Hide everything but the top LEVELS levels of headers, in whole buffer"))
105 (define-key map [hide hide-subtree]
106 '(menu-item "Hide Subtree" hide-subtree
799224fe 107 :help "Hide everything after this heading at deeper levels"))
7cd25617
DN
108 (define-key map [hide hide-entry]
109 '(menu-item "Hide Entry" hide-entry
799224fe 110 :help "Hide the body directly following this heading"))
7cd25617
DN
111 (define-key map [hide hide-body]
112 '(menu-item "Hide Body" hide-body
799224fe 113 :help "Hide all body lines in buffer, leaving all headings visible"))
7cd25617
DN
114 (define-key map [hide hide-leaves]
115 '(menu-item "Hide Leaves" hide-leaves
799224fe 116 :help "Hide the body after this heading and at deeper levels"))
36a77f37
JB
117
118 (define-key map [show] (cons "Show" (make-sparse-keymap "Show")))
119
7cd25617
DN
120 (define-key map [show show-subtree]
121 '(menu-item "Show Subtree" show-subtree
799224fe 122 :help "Show everything after this heading at deeper levels"))
7cd25617
DN
123 (define-key map [show show-children]
124 '(menu-item "Show Children" show-children
799224fe 125 :help "Show all direct subheadings of this heading"))
7cd25617
DN
126 (define-key map [show show-branches]
127 '(menu-item "Show Branches" show-branches
799224fe 128 :help "Show all subheadings of this heading, but not their bodies"))
7cd25617
DN
129 (define-key map [show show-entry]
130 '(menu-item "Show Entry" show-entry
799224fe 131 :help "Show the body directly following this heading"))
7cd25617
DN
132 (define-key map [show show-all]
133 '(menu-item "Show All" show-all
799224fe 134 :help "Show all of the text in the buffer"))
36a77f37
JB
135
136 (define-key map [headings]
137 (cons "Headings" (make-sparse-keymap "Headings")))
138
139 (define-key map [headings demote-subtree]
7cd25617 140 '(menu-item "Demote subtree" outline-demote
799224fe 141 :help "Demote headings lower down the tree"))
36a77f37 142 (define-key map [headings promote-subtree]
7cd25617 143 '(menu-item "Promote subtree" outline-promote
799224fe 144 :help "Promote headings higher up the tree"))
36a77f37 145 (define-key map [headings move-subtree-down]
7cd25617 146 '(menu-item "Move subtree down" outline-move-subtree-down
799224fe 147 :help "Move the currrent subtree down past arg headlines of the same level"))
36a77f37 148 (define-key map [headings move-subtree-up]
7cd25617 149 '(menu-item "Move subtree up" outline-move-subtree-up
799224fe 150 :help "Move the currrent subtree up past arg headlines of the same level"))
36a77f37
JB
151 (define-key map [headings copy]
152 '(menu-item "Copy to kill ring" outline-headers-as-kill
7cd25617 153 :enable mark-active
799224fe 154 :help "Save the visible outline headers in region at the start of the kill ring"))
36a77f37 155 (define-key map [headings outline-insert-heading]
7cd25617
DN
156
157 '(menu-item "New heading" outline-insert-heading
799224fe 158 :help "Insert a new heading at same depth at point"))
36a77f37 159 (define-key map [headings outline-backward-same-level]
7cd25617
DN
160
161 '(menu-item "Previous Same Level" outline-backward-same-level
799224fe 162 :help "Move backward to the arg'th subheading at same level as this one."))
36a77f37 163 (define-key map [headings outline-forward-same-level]
7cd25617
DN
164
165 '(menu-item "Next Same Level" outline-forward-same-level
799224fe 166 :help "Move forward to the arg'th subheading at same level as this one"))
36a77f37 167 (define-key map [headings outline-previous-visible-heading]
7cd25617
DN
168
169 '(menu-item "Previous" outline-previous-visible-heading
799224fe 170 :help "Move to the previous heading line"))
36a77f37 171 (define-key map [headings outline-next-visible-heading]
7cd25617
DN
172
173 '(menu-item "Next" outline-next-visible-heading
799224fe 174 :help "Move to the next visible heading line"))
36a77f37 175 (define-key map [headings outline-up-heading]
7cd25617
DN
176
177 '(menu-item "Up" outline-up-heading
799224fe 178 :help "Move to the visible heading line of which the present line is a subheading"))
36a77f37
JB
179 map))
180
181(defvar outline-minor-mode-menu-bar-map
182 (let ((map (make-sparse-keymap)))
183 (define-key map [outline]
184 (cons "Outline"
185 (nconc (make-sparse-keymap "Outline")
186 ;; Remove extra separator
187 (cdr
188 ;; Flatten the major mode's menus into a single menu.
189 (apply 'append
190 (mapcar (lambda (x)
191 (if (consp x)
192 ;; Add a separator between each
193 ;; part of the unified menu.
194 (cons '(--- "---") (cdr x))))
195 outline-mode-menu-bar-map))))))
196 map))
48c9ce10 197
36a77f37
JB
198
199(defvar outline-mode-map
200 (let ((map (make-sparse-keymap)))
201 (define-key map "\C-c" outline-mode-prefix-map)
202 (define-key map [menu-bar] outline-mode-menu-bar-map)
203 map))
204
205(defvar outline-font-lock-keywords
206 '(;;
207 ;; Highlight headings according to the level.
208 (eval . (list (concat "^\\(?:" outline-regexp "\\).+")
209 0 '(outline-font-lock-face) nil t)))
210 "Additional expressions to highlight in Outline mode.")
211
1f2503a5
LK
212(defface outline-1
213 '((t :inherit font-lock-function-name-face))
214 "Level 1."
215 :group 'outlines)
216
217(defface outline-2
218 '((t :inherit font-lock-variable-name-face))
219 "Level 2."
220 :group 'outlines)
221
222(defface outline-3
223 '((t :inherit font-lock-keyword-face))
224 "Level 3."
225 :group 'outlines)
226
227(defface outline-4
9c934431 228 '((t :inherit font-lock-comment-face))
1f2503a5
LK
229 "Level 4."
230 :group 'outlines)
231
232(defface outline-5
9c934431 233 '((t :inherit font-lock-type-face))
1f2503a5
LK
234 "Level 5."
235 :group 'outlines)
236
237(defface outline-6
238 '((t :inherit font-lock-constant-face))
239 "Level 6."
240 :group 'outlines)
241
242(defface outline-7
9c934431 243 '((t :inherit font-lock-builtin-face))
1f2503a5
LK
244 "Level 7."
245 :group 'outlines)
246
247(defface outline-8
248 '((t :inherit font-lock-string-face))
249 "Level 8."
250 :group 'outlines)
36a77f37
JB
251
252(defvar outline-font-lock-faces
253 [outline-1 outline-2 outline-3 outline-4
254 outline-5 outline-6 outline-7 outline-8])
255
550641d3
DP
256;; (defvar outline-font-lock-levels nil)
257;; (make-variable-buffer-local 'outline-font-lock-levels)
36a77f37
JB
258
259(defun outline-font-lock-face ()
260 ;; (save-excursion
261 ;; (outline-back-to-heading t)
262 ;; (let* ((count 0)
263 ;; (start-level (funcall outline-level))
264 ;; (level start-level)
265 ;; face-level)
266 ;; (while (not (setq face-level
267 ;; (if (or (bobp) (eq level 1)) 0
268 ;; (cdr (assq level outline-font-lock-levels)))))
269 ;; (outline-up-heading 1 t)
270 ;; (setq count (1+ count))
271 ;; (setq level (funcall outline-level)))
272 ;; ;; Remember for later.
273 ;; (unless (zerop count)
274 ;; (setq face-level (+ face-level count))
275 ;; (push (cons start-level face-level) outline-font-lock-levels))
276 ;; (condition-case nil
277 ;; (aref outline-font-lock-faces face-level)
278 ;; (error font-lock-warning-face))))
279 (save-excursion
280 (goto-char (match-beginning 0))
281 (looking-at outline-regexp)
550641d3 282 (aref outline-font-lock-faces (% (1- (funcall outline-level)) (length outline-font-lock-faces)))))
36a77f37
JB
283
284(defvar outline-view-change-hook nil
285 "Normal hook to be run after outline visibility changes.")
286
48c9ce10
JB
287(defvar outline-mode-hook nil
288 "*This hook is run when outline mode starts.")
289
ca858c64
JL
290(defvar outline-blank-line nil
291 "*Non-nil means to leave unhidden blank line before heading.")
292
36a77f37
JB
293;;;###autoload
294(define-derived-mode outline-mode text-mode "Outline"
295 "Set major mode for editing outlines with selective display.
296Headings are lines which start with asterisks: one for major headings,
297two for subheadings, etc. Lines not starting with asterisks are body lines.
298
299Body text or subheadings under a heading can be made temporarily
300invisible, or visible again. Invisible lines are attached to the end
301of the heading, so they move with it, if the line is killed and yanked
302back. A heading with text hidden under it is marked with an ellipsis (...).
303
304Commands:\\<outline-mode-map>
305\\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
306\\[outline-previous-visible-heading] outline-previous-visible-heading
307\\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
308\\[outline-backward-same-level] outline-backward-same-level
309\\[outline-up-heading] outline-up-heading move from subheading to heading
310
311\\[hide-body] make all text invisible (not headings).
312\\[show-all] make everything in buffer visible.
313\\[hide-sublevels] make only the first N levels of headers visible.
314
315The remaining commands are used when point is on a heading line.
316They apply to some of the body or subheadings of that heading.
317\\[hide-subtree] hide-subtree make body and subheadings invisible.
318\\[show-subtree] show-subtree make body and subheadings visible.
319\\[show-children] show-children make direct subheadings visible.
320 No effect on body, or subheadings 2 or more levels down.
321 With arg N, affects subheadings N levels down.
322\\[hide-entry] make immediately following body invisible.
323\\[show-entry] make it visible.
324\\[hide-leaves] make body under heading and under its subheadings invisible.
325 The subheadings remain visible.
326\\[show-branches] make all subheadings at all levels visible.
327
328The variable `outline-regexp' can be changed to control what is a heading.
329A line is a heading if `outline-regexp' matches something at the
330beginning of the line. The longer the match, the deeper the level.
331
332Turning on outline mode calls the value of `text-mode-hook' and then of
333`outline-mode-hook', if they are non-nil."
334 (make-local-variable 'line-move-ignore-invisible)
335 (setq line-move-ignore-invisible t)
336 ;; Cause use of ellipses for invisible text.
337 (add-to-invisibility-spec '(outline . t))
338 (set (make-local-variable 'paragraph-start)
339 (concat paragraph-start "\\|\\(?:" outline-regexp "\\)"))
340 ;; Inhibit auto-filling of header lines.
341 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
342 (set (make-local-variable 'paragraph-separate)
343 (concat paragraph-separate "\\|\\(?:" outline-regexp "\\)"))
344 (set (make-local-variable 'font-lock-defaults)
345 '(outline-font-lock-keywords t nil nil backward-paragraph))
346 (setq imenu-generic-expression
347 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
53526855 348 (add-hook 'change-major-mode-hook 'show-all nil t))
36a77f37
JB
349
350(defcustom outline-minor-mode-prefix "\C-c@"
9201cc28 351 "Prefix key to use for Outline commands in Outline minor mode.
36a77f37
JB
352The value of this variable is checked as part of loading Outline mode.
353After that, changing the prefix key requires manipulating keymaps."
354 :type 'string
355 :group 'outlines)
356
357;;;###autoload
358(define-minor-mode outline-minor-mode
359 "Toggle Outline minor mode.
360With arg, turn Outline minor mode on if arg is positive, off otherwise.
361See the command `outline-mode' for more information on this mode."
362 nil " Outl" (list (cons [menu-bar] outline-minor-mode-menu-bar-map)
363 (cons outline-minor-mode-prefix outline-mode-prefix-map))
364 :group 'outlines
365 (if outline-minor-mode
366 (progn
367 ;; Turn off this mode if we change major modes.
368 (add-hook 'change-major-mode-hook
369 (lambda () (outline-minor-mode -1))
370 nil t)
371 (set (make-local-variable 'line-move-ignore-invisible) t)
372 ;; Cause use of ellipses for invisible text.
373 (add-to-invisibility-spec '(outline . t)))
374 (setq line-move-ignore-invisible nil)
375 ;; Cause use of ellipses for invisible text.
376 (remove-from-invisibility-spec '(outline . t))
377 ;; When turning off outline mode, get rid of any outline hiding.
378 (show-all)))
379\f
380(defvar outline-level 'outline-level
381 "*Function of no args to compute a header's nesting level in an outline.
382It can assume point is at the beginning of a header line and that the match
383data reflects the `outline-regexp'.")
6dc3311d 384;;;###autoload(put 'outline-level 'risky-local-variable t)
36a77f37
JB
385
386(defvar outline-heading-alist ()
387 "Alist associating a heading for every possible level.
388Each entry is of the form (HEADING . LEVEL).
389This alist is used two ways: to find the heading corresponding to
390a given level and to find the level of a given heading.
391If a mode or document needs several sets of outline headings (for example
392numbered and unnumbered sections), list them set by set and sorted by level
393within each set. For example in texinfo mode:
394
395 (setq outline-heading-alist
396 '((\"@chapter\" . 2) (\"@section\" . 3) (\"@subsection\" . 4)
397 (\"@subsubsection\" . 5)
398 (\"@unnumbered\" . 2) (\"@unnumberedsec\" . 3)
399 (\"@unnumberedsubsec\" . 4) (\"@unnumberedsubsubsec\" . 5)
400 (\"@appendix\" . 2) (\"@appendixsec\" . 3)...
401 (\"@appendixsubsec\" . 4) (\"@appendixsubsubsec\" . 5) ..))
402
403Instead of sorting the entries in each set, you can also separate the
404sets with nil.")
405(make-variable-buffer-local 'outline-heading-alist)
406
407;; This used to count columns rather than characters, but that made ^L
408;; appear to be at level 2 instead of 1. Columns would be better for
409;; tab handling, but the default regexp doesn't use tabs, and anyone
410;; who changes the regexp can also redefine the outline-level variable
411;; as appropriate.
412(defun outline-level ()
413 "Return the depth to which a statement is nested in the outline.
414Point must be at the beginning of a header line.
415This is actually either the level specified in `outline-heading-alist'
416or else the number of characters matched by `outline-regexp'."
417 (or (cdr (assoc (match-string 0) outline-heading-alist))
418 (- (match-end 0) (match-beginning 0))))
419
420(defun outline-next-preface ()
421 "Skip forward to just before the next heading line.
422If there's no following heading line, stop before the newline
423at the end of the buffer."
424 (if (re-search-forward (concat "\n\\(?:" outline-regexp "\\)")
425 nil 'move)
426 (goto-char (match-beginning 0)))
ca858c64 427 (if (and (bolp) (or outline-blank-line (eobp)) (not (bobp)))
36a77f37
JB
428 (forward-char -1)))
429
430(defun outline-next-heading ()
431 "Move to the next (possibly invisible) heading line."
432 (interactive)
433 ;; Make sure we don't match the heading we're at.
434 (if (and (bolp) (not (eobp))) (forward-char 1))
435 (if (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
436 nil 'move)
437 (goto-char (match-beginning 0))))
438
439(defun outline-previous-heading ()
440 "Move to the previous (possibly invisible) heading line."
441 (interactive)
442 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
443 nil 'move))
444
445(defsubst outline-invisible-p (&optional pos)
446 "Non-nil if the character after point is invisible."
447 (get-char-property (or pos (point)) 'invisible))
448
36a77f37
JB
449(defun outline-back-to-heading (&optional invisible-ok)
450 "Move to previous heading line, or beg of this line if it's a heading.
451Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
452 (beginning-of-line)
453 (or (outline-on-heading-p invisible-ok)
454 (let (found)
455 (save-excursion
456 (while (not found)
457 (or (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
458 nil t)
459 (error "before first heading"))
460 (setq found (and (or invisible-ok (not (outline-invisible-p)))
461 (point)))))
462 (goto-char found)
463 found)))
464
465(defun outline-on-heading-p (&optional invisible-ok)
466 "Return t if point is on a (visible) heading line.
467If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
468 (save-excursion
469 (beginning-of-line)
470 (and (bolp) (or invisible-ok (not (outline-invisible-p)))
471 (looking-at outline-regexp))))
472
473(defun outline-insert-heading ()
474 "Insert a new heading at same depth at point."
475 (interactive)
476 (let ((head (save-excursion
477 (condition-case nil
478 (outline-back-to-heading)
479 (error (outline-next-heading)))
480 (if (eobp)
481 (or (caar outline-heading-alist) "")
482 (match-string 0)))))
483 (unless (or (string-match "[ \t]\\'" head)
f56af8ca
SM
484 (not (string-match (concat "\\`\\(?:" outline-regexp "\\)")
485 (concat head " "))))
36a77f37
JB
486 (setq head (concat head " ")))
487 (unless (bolp) (end-of-line) (newline))
488 (insert head)
489 (unless (eolp)
490 (save-excursion (newline-and-indent)))
491 (run-hooks 'outline-insert-heading-hook)))
492
797d92ed
SM
493(defun outline-invent-heading (head up)
494 (save-match-data
495 ;; Let's try to invent one by repeating or deleting the last char.
496 (let ((new-head (if up (substring head 0 -1)
497 (concat head (substring head -1)))))
498 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
499 new-head)
500 ;; Why bother checking that it is indeed higher/lower level ?
501 new-head
502 ;; Didn't work, so ask what to do.
503 (read-string (format "%s heading for `%s': "
504 (if up "Parent" "Demoted") head)
505 head nil nil t)))))
506
914f7ec3 507(defun outline-promote (&optional which)
36a77f37 508 "Promote headings higher up the tree.
914f7ec3
EZ
509If transient-mark-mode is on, and mark is active, promote headings in
510the region (from a Lisp program, pass `region' for WHICH). Otherwise:
511without prefix argument, promote current heading and all headings in the
512subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
513argument, promote just the current heading (from a Lisp program, pass
514nil for WHICH, or do not pass any argument)."
36a77f37
JB
515 (interactive
516 (list (if (and transient-mark-mode mark-active) 'region
517 (outline-back-to-heading)
518 (if current-prefix-arg nil 'subtree))))
519 (cond
914f7ec3 520 ((eq which 'region)
36a77f37 521 (outline-map-region 'outline-promote (region-beginning) (region-end)))
914f7ec3 522 (which
36a77f37
JB
523 (outline-map-region 'outline-promote
524 (point)
525 (save-excursion (outline-get-next-sibling) (point))))
526 (t
527 (outline-back-to-heading t)
20e3210f 528 (let* ((head (match-string-no-properties 0))
36a77f37
JB
529 (level (save-match-data (funcall outline-level)))
530 (up-head (or (outline-head-from-level (1- level) head)
20e3210f
RS
531 ;; Use the parent heading, if it is really
532 ;; one level less.
36a77f37
JB
533 (save-excursion
534 (save-match-data
535 (outline-up-heading 1 t)
20e3210f
RS
536 (and (= (1- level) (funcall outline-level))
537 (match-string-no-properties 0))))
797d92ed
SM
538 ;; Bummer!! There is no lower level heading.
539 (outline-invent-heading head 'up))))
48c9ce10 540
36a77f37
JB
541 (unless (rassoc level outline-heading-alist)
542 (push (cons head level) outline-heading-alist))
48c9ce10 543
36a77f37
JB
544 (replace-match up-head nil t)))))
545
914f7ec3 546(defun outline-demote (&optional which)
36a77f37 547 "Demote headings lower down the tree.
914f7ec3
EZ
548If transient-mark-mode is on, and mark is active, demote headings in
549the region (from a Lisp program, pass `region' for WHICH). Otherwise:
550without prefix argument, demote current heading and all headings in the
551subtree (from a Lisp program, pass `subtree' for WHICH); with prefix
552argument, demote just the current heading (from a Lisp program, pass
553nil for WHICH, or do not pass any argument)."
36a77f37
JB
554 (interactive
555 (list (if (and transient-mark-mode mark-active) 'region
556 (outline-back-to-heading)
557 (if current-prefix-arg nil 'subtree))))
558 (cond
914f7ec3 559 ((eq which 'region)
36a77f37 560 (outline-map-region 'outline-demote (region-beginning) (region-end)))
914f7ec3 561 (which
36a77f37
JB
562 (outline-map-region 'outline-demote
563 (point)
564 (save-excursion (outline-get-next-sibling) (point))))
565 (t
20e3210f 566 (let* ((head (match-string-no-properties 0))
36a77f37
JB
567 (level (save-match-data (funcall outline-level)))
568 (down-head
569 (or (outline-head-from-level (1+ level) head)
570 (save-excursion
571 (save-match-data
572 (while (and (progn (outline-next-heading) (not (eobp)))
573 (<= (funcall outline-level) level)))
574 (when (eobp)
575 ;; Try again from the beginning of the buffer.
576 (goto-char (point-min))
577 (while (and (progn (outline-next-heading) (not (eobp)))
578 (<= (funcall outline-level) level))))
579 (unless (eobp)
580 (looking-at outline-regexp)
20e3210f 581 (match-string-no-properties 0))))
797d92ed
SM
582 ;; Bummer!! There is no higher-level heading in the buffer.
583 (outline-invent-heading head nil))))
36a77f37 584
20e3210f
RS
585 (unless (rassoc level outline-heading-alist)
586 (push (cons head level) outline-heading-alist))
587 (replace-match down-head nil t)))))
36a77f37
JB
588
589(defun outline-head-from-level (level head &optional alist)
590 "Get new heading with level LEVEL from ALIST.
591If there are no such entries, return nil.
592ALIST defaults to `outline-heading-alist'.
593Similar to (car (rassoc LEVEL ALIST)).
594If there are several different entries with same new level, choose
fa5f7c5f 595the one with the smallest distance to the association of HEAD in the alist.
36a77f37
JB
596This makes it possible for promotion to work in modes with several
597independent sets of headings (numbered, unnumbered, appendix...)"
598 (unless alist (setq alist outline-heading-alist))
599 (let ((l (rassoc level alist))
600 ll h hl l2 l2l)
601 (cond
602 ((null l) nil)
603 ;; If there's no HEAD after L, any other entry for LEVEL after L
604 ;; can't be much better than L.
605 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
606 ;; If there's no other entry for LEVEL, just keep L.
607 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
608 ;; Now we have L, L2, and H: see if L2 seems better than L.
609 ;; If H is after L2, L2 is better.
610 ((memq h (setq l2l (memq l2 (cdr ll))))
611 (outline-head-from-level level head l2l))
612 ;; Now we have H between L and L2.
613 ;; If there's a separator between L and H, prefer L2.
614 ((memq h (memq nil ll))
615 (outline-head-from-level level head l2l))
616 ;; If there's a separator between L2 and H, prefer L.
617 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
618 ;; No separator between L and L2, check the distance.
619 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
620 (outline-head-from-level level head l2l))
621 ;; If all else fails, just keep L.
622 (t (car l)))))
623
624(defun outline-map-region (fun beg end)
625 "Call FUN for every heading between BEG and END.
626When FUN is called, point is at the beginning of the heading and
627the match data is set appropriately."
628 (save-excursion
629 (setq end (copy-marker end))
630 (goto-char beg)
631 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
632 (goto-char (match-beginning 0))
633 (funcall fun)
634 (while (and (progn
635 (outline-next-heading)
636 (< (point) end))
637 (not (eobp)))
638 (funcall fun)))))
639
640;; Vertical tree motion
641
642(defun outline-move-subtree-up (&optional arg)
643 "Move the currrent subtree up past ARG headlines of the same level."
644 (interactive "p")
645 (outline-move-subtree-down (- arg)))
646
647(defun outline-move-subtree-down (&optional arg)
648 "Move the currrent subtree down past ARG headlines of the same level."
649 (interactive "p")
797d92ed 650 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
36a77f37
JB
651 'outline-get-last-sibling))
652 (ins-point (make-marker))
653 (cnt (abs arg))
797d92ed 654 beg end folded)
36a77f37
JB
655 ;; Select the tree
656 (outline-back-to-heading)
657 (setq beg (point))
48c9ce10
JB
658 (save-match-data
659 (save-excursion (outline-end-of-heading)
36a77f37
JB
660 (setq folded (outline-invisible-p)))
661 (outline-end-of-subtree))
662 (if (= (char-after) ?\n) (forward-char 1))
663 (setq end (point))
664 ;; Find insertion point, with error handling
665 (goto-char beg)
666 (while (> cnt 0)
667 (or (funcall movfunc)
668 (progn (goto-char beg)
669 (error "Cannot move past superior level")))
670 (setq cnt (1- cnt)))
671 (if (> arg 0)
672 ;; Moving forward - still need to move over subtree
48c9ce10 673 (progn (outline-end-of-subtree)
36a77f37
JB
674 (if (= (char-after) ?\n) (forward-char 1))))
675 (move-marker ins-point (point))
676 (insert (delete-and-extract-region beg end))
677 (goto-char ins-point)
678 (if folded (hide-subtree))
679 (move-marker ins-point nil)))
680
681(defun outline-end-of-heading ()
682 (if (re-search-forward outline-heading-end-regexp nil 'move)
683 (forward-char -1)))
684
685(defun outline-next-visible-heading (arg)
686 "Move to the next visible heading line.
687With argument, repeats or can move backward if negative.
688A heading line is one that starts with a `*' (or that
689`outline-regexp' matches)."
690 (interactive "p")
691 (if (< arg 0)
692 (beginning-of-line)
693 (end-of-line))
cf27cc83
EZ
694 (let (found-heading-p)
695 (while (and (not (bobp)) (< arg 0))
696 (while (and (not (bobp))
697 (setq found-heading-p
698 (re-search-backward
699 (concat "^\\(?:" outline-regexp "\\)")
700 nil 'move))
701 (outline-invisible-p)))
702 (setq arg (1+ arg)))
703 (while (and (not (eobp)) (> arg 0))
704 (while (and (not (eobp))
705 (setq found-heading-p
706 (re-search-forward
707 (concat "^\\(?:" outline-regexp "\\)")
708 nil 'move))
709 (outline-invisible-p (match-beginning 0))))
710 (setq arg (1- arg)))
711 (if found-heading-p (beginning-of-line))))
36a77f37
JB
712
713(defun outline-previous-visible-heading (arg)
714 "Move to the previous heading line.
715With argument, repeats or can move forward if negative.
716A heading line is one that starts with a `*' (or that
717`outline-regexp' matches)."
718 (interactive "p")
719 (outline-next-visible-heading (- arg)))
720
721(defun outline-mark-subtree ()
722 "Mark the current subtree in an outlined document.
723This puts point at the start of the current subtree, and mark at the end."
724 (interactive)
725 (let ((beg))
726 (if (outline-on-heading-p)
727 ;; we are already looking at a heading
728 (beginning-of-line)
729 ;; else go back to previous heading
730 (outline-previous-visible-heading 1))
731 (setq beg (point))
732 (outline-end-of-subtree)
d35b8a28 733 (push-mark (point) nil t)
36a77f37
JB
734 (goto-char beg)))
735\f
736
203bef73
KS
737(defvar outline-isearch-open-invisible-function nil
738 "Function called if `isearch' finishes in an invisible overlay.
739The function is called with the overlay as its only argument.
740If nil, `show-entry' is called to reveal the invisible text.")
741
36a77f37
JB
742(put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
743(defun outline-flag-region (from to flag)
744 "Hide or show lines from FROM to TO, according to FLAG.
745If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
746 (remove-overlays from to 'invisible 'outline)
747 (when flag
b7e5bc1c
SM
748 ;; We use `front-advance' here because the invisible text begins at the
749 ;; very end of the heading, before the newline, so text inserted at FROM
750 ;; belongs to the heading rather than to the entry.
751 (let ((o (make-overlay from to nil 'front-advance)))
36a77f37 752 (overlay-put o 'invisible 'outline)
203bef73
KS
753 (overlay-put o 'isearch-open-invisible
754 (or outline-isearch-open-invisible-function
755 'outline-isearch-open-invisible))))
36a77f37
JB
756 ;; Seems only used by lazy-lock. I.e. obsolete.
757 (run-hooks 'outline-view-change-hook))
758
759(defun outline-reveal-toggle-invisible (o hidep)
760 (save-excursion
761 (goto-char (overlay-start o))
762 (if hidep
763 ;; When hiding the area again, we could just clean it up and let
764 ;; reveal do the rest, by simply doing:
765 ;; (remove-overlays (overlay-start o) (overlay-end o)
766 ;; 'invisible 'outline)
48c9ce10 767 ;;
36a77f37
JB
768 ;; That works fine as long as everything is in sync, but if the
769 ;; structure of the document is changed while revealing parts of it,
770 ;; the resulting behavior can be ugly. I.e. we need to make
771 ;; sure that we hide exactly a subtree.
772 (progn
773 (let ((end (overlay-end o)))
774 (delete-overlay o)
775 (while (progn
776 (hide-subtree)
777 (outline-next-visible-heading 1)
778 (and (not (eobp)) (< (point) end))))))
779
780 ;; When revealing, we just need to reveal sublevels. If point is
781 ;; inside one of the sublevels, reveal will call us again.
782 ;; But we need to preserve the original overlay.
783 (let ((o1 (copy-overlay o)))
784 (overlay-put o 'invisible nil) ;Show (most of) the text.
785 (while (progn
786 (show-entry)
787 (show-children)
788 ;; Normally just the above is needed.
789 ;; But in odd cases, the above might fail to show anything.
790 ;; To avoid an infinite loop, we have to make sure that
791 ;; *something* gets shown.
792 (and (equal (overlay-start o) (overlay-start o1))
793 (< (point) (overlay-end o))
794 (= 0 (forward-line 1)))))
795 ;; If still nothing was shown, just kill the damn thing.
796 (when (equal (overlay-start o) (overlay-start o1))
797 ;; I've seen it happen at the end of buffer.
798 (delete-overlay o1))))))
799
800;; Function to be set as an outline-isearch-open-invisible' property
801;; to the overlay that makes the outline invisible (see
802;; `outline-flag-region').
803(defun outline-isearch-open-invisible (overlay)
804 ;; We rely on the fact that isearch places point on the matched text.
805 (show-entry))
806\f
807(defun hide-entry ()
808 "Hide the body directly following this heading."
809 (interactive)
36a77f37 810 (save-excursion
c95502be 811 (outline-back-to-heading)
e4619728 812 (outline-end-of-heading)
36a77f37
JB
813 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
814
815(defun show-entry ()
816 "Show the body directly following this heading.
817Show the heading too, if it is currently invisible."
818 (interactive)
819 (save-excursion
820 (outline-back-to-heading t)
821 (outline-flag-region (1- (point))
822 (progn (outline-next-preface) (point)) nil)))
823
824(defun hide-body ()
00958fa2 825 "Hide all body lines in buffer, leaving all headings visible."
36a77f37
JB
826 (interactive)
827 (hide-region-body (point-min) (point-max)))
828
829(defun hide-region-body (start end)
830 "Hide all body lines in the region, but not headings."
831 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
832 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
833 ;; and run the hook finally.
834 (let (outline-view-change-hook)
835 (save-excursion
836 (save-restriction
837 (narrow-to-region start end)
838 (goto-char (point-min))
839 (if (outline-on-heading-p)
00958fa2
EZ
840 (outline-end-of-heading)
841 (outline-next-preface))
36a77f37
JB
842 (while (not (eobp))
843 (outline-flag-region (point)
844 (progn (outline-next-preface) (point)) t)
845 (unless (eobp)
846 (forward-char (if (looking-at "\n\n") 2 1))
847 (outline-end-of-heading))))))
848 (run-hooks 'outline-view-change-hook))
849
850(defun show-all ()
851 "Show all of the text in the buffer."
852 (interactive)
853 (outline-flag-region (point-min) (point-max) nil))
854
855(defun hide-subtree ()
856 "Hide everything after this heading at deeper levels."
857 (interactive)
858 (outline-flag-subtree t))
859
860(defun hide-leaves ()
d8b5b5d5 861 "Hide the body after this heading and at deeper levels."
36a77f37 862 (interactive)
36a77f37 863 (save-excursion
c95502be 864 (outline-back-to-heading)
4af23f81 865;; Turned off to fix bug reported by Otto Maddox on 22 Nov 2005.
d8b5b5d5 866;; (outline-end-of-heading)
36a77f37
JB
867 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
868
869(defun show-subtree ()
870 "Show everything after this heading at deeper levels."
871 (interactive)
872 (outline-flag-subtree nil))
873
874(defun outline-show-heading ()
875 "Show the current heading and move to its end."
ca858c64
JL
876 (outline-flag-region (- (point)
877 (if (bobp) 0
878 (if (and outline-blank-line
879 (eq (char-before (1- (point))) ?\n))
880 2 1)))
36a77f37
JB
881 (progn (outline-end-of-heading) (point))
882 nil))
883
884(defun hide-sublevels (levels)
885 "Hide everything but the top LEVELS levels of headers, in whole buffer."
1e0e9b38
SM
886 (interactive (list
887 (cond
888 (current-prefix-arg (prefix-numeric-value current-prefix-arg))
889 ((save-excursion (beginning-of-line)
890 (looking-at outline-regexp))
891 (funcall outline-level))
892 (t 1))))
36a77f37
JB
893 (if (< levels 1)
894 (error "Must keep at least one level of headers"))
8d683c8e
SM
895 (save-excursion
896 (let* (outline-view-change-hook
897 (beg (progn
898 (goto-char (point-min))
899 ;; Skip the prelude, if any.
900 (unless (outline-on-heading-p t) (outline-next-heading))
901 (point)))
902 (end (progn
903 (goto-char (point-max))
904 ;; Keep empty last line, if available.
905 (if (bolp) (1- (point)) (point)))))
3d0dd8ff
CY
906 (if (< end beg)
907 (setq beg (prog1 end (setq end beg))))
36a77f37 908 ;; First hide everything.
8d683c8e 909 (outline-flag-region beg end t)
36a77f37
JB
910 ;; Then unhide the top level headers.
911 (outline-map-region
912 (lambda ()
913 (if (<= (funcall outline-level) levels)
801ba3ba
SM
914 (outline-show-heading)))
915 beg end)
916 ;; Finally unhide any trailing newline.
917 (goto-char (point-max))
918 (if (and (bolp) (not (bobp)) (outline-invisible-p (1- (point))))
919 (outline-flag-region (1- (point)) (point) nil))))
36a77f37
JB
920 (run-hooks 'outline-view-change-hook))
921
922(defun hide-other ()
923 "Hide everything except current body and parent and top-level headings."
924 (interactive)
925 (hide-sublevels 1)
926 (let (outline-view-change-hook)
927 (save-excursion
928 (outline-back-to-heading t)
929 (show-entry)
6e32922d 930 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
36a77f37
JB
931 (error nil))
932 (outline-flag-region (1- (point))
933 (save-excursion (forward-line 1) (point))
934 nil))))
935 (run-hooks 'outline-view-change-hook))
936
937(defun outline-toggle-children ()
938 "Show or hide the current subtree depending on its current state."
939 (interactive)
c95502be
JL
940 (save-excursion
941 (outline-back-to-heading)
942 (if (not (outline-invisible-p (line-end-position)))
943 (hide-subtree)
944 (show-children)
945 (show-entry))))
36a77f37
JB
946
947(defun outline-flag-subtree (flag)
948 (save-excursion
949 (outline-back-to-heading)
950 (outline-end-of-heading)
951 (outline-flag-region (point)
952 (progn (outline-end-of-subtree) (point))
953 flag)))
954
955(defun outline-end-of-subtree ()
956 (outline-back-to-heading)
797d92ed 957 (let ((first t)
36a77f37
JB
958 (level (funcall outline-level)))
959 (while (and (not (eobp))
960 (or first (> (funcall outline-level) level)))
961 (setq first nil)
962 (outline-next-heading))
49bb7f01
RS
963 (if (and (bolp) (not (eolp)))
964 ;; We stopped at a nonempty line (the next heading).
36a77f37
JB
965 (progn
966 ;; Go to end of line before heading
ca858c64
JL
967 (forward-char -1)
968 (if (and outline-blank-line (bolp))
969 ;; leave blank line before heading
970 (forward-char -1))))))
36a77f37
JB
971\f
972(defun show-branches ()
973 "Show all subheadings of this heading, but not their bodies."
974 (interactive)
975 (show-children 1000))
976
977(defun show-children (&optional level)
978 "Show all direct subheadings of this heading.
979Prefix arg LEVEL is how many levels below the current level should be shown.
980Default is enough to cause the following heading to appear."
981 (interactive "P")
982 (setq level
983 (if level (prefix-numeric-value level)
984 (save-excursion
985 (outline-back-to-heading)
986 (let ((start-level (funcall outline-level)))
987 (outline-next-heading)
988 (if (eobp)
989 1
990 (max 1 (- (funcall outline-level) start-level)))))))
991 (let (outline-view-change-hook)
992 (save-excursion
993 (outline-back-to-heading)
994 (setq level (+ level (funcall outline-level)))
995 (outline-map-region
996 (lambda ()
997 (if (<= (funcall outline-level) level)
998 (outline-show-heading)))
999 (point)
1000 (progn (outline-end-of-subtree)
1001 (if (eobp) (point-max) (1+ (point)))))))
1002 (run-hooks 'outline-view-change-hook))
1003
1004\f
1005
1006(defun outline-up-heading (arg &optional invisible-ok)
1007 "Move to the visible heading line of which the present line is a subheading.
1008With argument, move up ARG levels.
1009If INVISIBLE-OK is non-nil, also consider invisible lines."
1010 (interactive "p")
e4619728
JL
1011 (and (eq this-command 'outline-up-heading)
1012 (or (eq last-command 'outline-up-heading) (push-mark)))
36a77f37
JB
1013 (outline-back-to-heading invisible-ok)
1014 (let ((start-level (funcall outline-level)))
d405bc15
MR
1015 (when (<= start-level 1)
1016 (error "Already at top level of the outline"))
36a77f37
JB
1017 (while (and (> start-level 1) (> arg 0) (not (bobp)))
1018 (let ((level start-level))
1019 (while (not (or (< level start-level) (bobp)))
1020 (if invisible-ok
1021 (outline-previous-heading)
1022 (outline-previous-visible-heading 1))
1023 (setq level (funcall outline-level)))
1024 (setq start-level level))
1025 (setq arg (- arg 1))))
1026 (looking-at outline-regexp))
1027
1028(defun outline-forward-same-level (arg)
1029 "Move forward to the ARG'th subheading at same level as this one.
1030Stop at the first and last subheadings of a superior heading."
1031 (interactive "p")
1032 (outline-back-to-heading)
1033 (while (> arg 0)
1034 (let ((point-to-move-to (save-excursion
1035 (outline-get-next-sibling))))
1036 (if point-to-move-to
1037 (progn
1038 (goto-char point-to-move-to)
1039 (setq arg (1- arg)))
1040 (progn
1041 (setq arg 0)
1042 (error "No following same-level heading"))))))
1043
1044(defun outline-get-next-sibling ()
29779b2d
CY
1045 "Move to next heading of the same level, and return point.
1046If there is no such heading, return nil."
36a77f37
JB
1047 (let ((level (funcall outline-level)))
1048 (outline-next-visible-heading 1)
1049 (while (and (not (eobp)) (> (funcall outline-level) level))
1050 (outline-next-visible-heading 1))
1051 (if (or (eobp) (< (funcall outline-level) level))
1052 nil
1053 (point))))
1054
1055(defun outline-backward-same-level (arg)
1056 "Move backward to the ARG'th subheading at same level as this one.
1057Stop at the first and last subheadings of a superior heading."
1058 (interactive "p")
1059 (outline-back-to-heading)
1060 (while (> arg 0)
1061 (let ((point-to-move-to (save-excursion
1062 (outline-get-last-sibling))))
1063 (if point-to-move-to
1064 (progn
1065 (goto-char point-to-move-to)
1066 (setq arg (1- arg)))
1067 (progn
1068 (setq arg 0)
1069 (error "No previous same-level heading"))))))
1070
1071(defun outline-get-last-sibling ()
29779b2d
CY
1072 "Move to previous heading of the same level, and return point.
1073If there is no such heading, return nil."
1074 (let ((opoint (point))
1075 (level (funcall outline-level)))
36a77f37 1076 (outline-previous-visible-heading 1)
29779b2d
CY
1077 (when (and (/= (point) opoint) (outline-on-heading-p))
1078 (while (and (> (funcall outline-level) level)
1079 (not (bobp)))
1080 (outline-previous-visible-heading 1))
1081 (if (< (funcall outline-level) level)
1082 nil
1083 (point)))))
36a77f37
JB
1084\f
1085(defun outline-headers-as-kill (beg end)
1086 "Save the visible outline headers in region at the start of the kill ring.
1087
1088Text shown between the headers isn't copied. Two newlines are
1089inserted between saved headers. Yanking the result may be a
1090convenient way to make a table of contents of the buffer."
1091 (interactive "r")
1092 (save-excursion
1093 (save-restriction
1094 (narrow-to-region beg end)
1095 (goto-char (point-min))
1096 (let ((buffer (current-buffer))
1097 start end)
1098 (with-temp-buffer
1099 (with-current-buffer buffer
1100 ;; Boundary condition: starting on heading:
1101 (when (outline-on-heading-p)
1102 (outline-back-to-heading)
1103 (setq start (point)
1104 end (progn (outline-end-of-heading)
1105 (point)))
1106 (insert-buffer-substring buffer start end)
1107 (insert "\n\n")))
1108 (let ((temp-buffer (current-buffer)))
1109 (with-current-buffer buffer
1110 (while (outline-next-heading)
1111 (unless (outline-invisible-p)
1112 (setq start (point)
1113 end (progn (outline-end-of-heading) (point)))
1114 (with-current-buffer temp-buffer
1115 (insert-buffer-substring buffer start end)
1116 (insert "\n\n"))))))
1117 (kill-new (buffer-string)))))))
1118
1119(provide 'outline)
1120(provide 'noutline)
1121
1122;;; outline.el ends here