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