(truncate-lines, write-file, print-buffer)
[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)
20e3210f 474 (let* ((head (match-string-no-properties 0))
36a77f37
JB
475 (level (save-match-data (funcall outline-level)))
476 (up-head (or (outline-head-from-level (1- level) head)
20e3210f
RS
477 ;; Use the parent heading, if it is really
478 ;; one level less.
36a77f37
JB
479 (save-excursion
480 (save-match-data
481 (outline-up-heading 1 t)
20e3210f
RS
482 (and (= (1- level) (funcall outline-level))
483 (match-string-no-properties 0))))
484 ;; Bummer!! There is no lower level heading.
485 ;; Let's try to invent one by deleting the last char.
486 (save-match-data
487 (let ((new-head (substring head 0 -1)))
488 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
489 new-head)
490 ;; Why bother checking that it is indeed lower level ?
491 new-head
492 ;; Didn't work, so ask what to do.
493 (read-string (format "Parent heading for `%s': "
494 head)
495 head nil nil t)))))))
48c9ce10 496
36a77f37
JB
497 (unless (rassoc level outline-heading-alist)
498 (push (cons head level) outline-heading-alist))
48c9ce10 499
36a77f37
JB
500 (replace-match up-head nil t)))))
501
502(defun outline-demote (&optional children)
503 "Demote headings lower down the tree.
504If prefix argument CHILDREN is given, demote also all the children.
505If the region is active in `transient-mark-mode', demote all headings
506in the region."
507 (interactive
508 (list (if (and transient-mark-mode mark-active) 'region
509 (outline-back-to-heading)
510 (if current-prefix-arg nil 'subtree))))
511 (cond
512 ((eq children 'region)
513 (outline-map-region 'outline-demote (region-beginning) (region-end)))
514 (children
515 (outline-map-region 'outline-demote
516 (point)
517 (save-excursion (outline-get-next-sibling) (point))))
518 (t
20e3210f 519 (let* ((head (match-string-no-properties 0))
36a77f37
JB
520 (level (save-match-data (funcall outline-level)))
521 (down-head
522 (or (outline-head-from-level (1+ level) head)
523 (save-excursion
524 (save-match-data
525 (while (and (progn (outline-next-heading) (not (eobp)))
526 (<= (funcall outline-level) level)))
527 (when (eobp)
528 ;; Try again from the beginning of the buffer.
529 (goto-char (point-min))
530 (while (and (progn (outline-next-heading) (not (eobp)))
531 (<= (funcall outline-level) level))))
532 (unless (eobp)
533 (looking-at outline-regexp)
20e3210f 534 (match-string-no-properties 0))))
36a77f37 535 (save-match-data
20e3210f
RS
536 ;; Bummer!! There is no higher-level heading in the buffer.
537 ;; Let's try to invent one by repeating the last char.
538 (let ((new-head (concat head (substring head -1))))
f56af8ca
SM
539 (if (string-match (concat "\\`\\(?:" outline-regexp "\\)")
540 new-head)
20e3210f 541 ;; Why bother checking that it is indeed higher level ?
36a77f37 542 new-head
20e3210f
RS
543 ;; Didn't work, so ask what to do.
544 (read-string (format "Demoted heading for `%s': "
545 head)
546 head nil nil t)))))))
36a77f37 547
20e3210f
RS
548 (unless (rassoc level outline-heading-alist)
549 (push (cons head level) outline-heading-alist))
550 (replace-match down-head nil t)))))
36a77f37
JB
551
552(defun outline-head-from-level (level head &optional alist)
553 "Get new heading with level LEVEL from ALIST.
554If there are no such entries, return nil.
555ALIST defaults to `outline-heading-alist'.
556Similar to (car (rassoc LEVEL ALIST)).
557If there are several different entries with same new level, choose
558the one with the smallest distance to the assocation of HEAD in the alist.
559This makes it possible for promotion to work in modes with several
560independent sets of headings (numbered, unnumbered, appendix...)"
561 (unless alist (setq alist outline-heading-alist))
562 (let ((l (rassoc level alist))
563 ll h hl l2 l2l)
564 (cond
565 ((null l) nil)
566 ;; If there's no HEAD after L, any other entry for LEVEL after L
567 ;; can't be much better than L.
568 ((null (setq h (assoc head (setq ll (memq l alist))))) (car l))
569 ;; If there's no other entry for LEVEL, just keep L.
570 ((null (setq l2 (rassoc level (cdr ll)))) (car l))
571 ;; Now we have L, L2, and H: see if L2 seems better than L.
572 ;; If H is after L2, L2 is better.
573 ((memq h (setq l2l (memq l2 (cdr ll))))
574 (outline-head-from-level level head l2l))
575 ;; Now we have H between L and L2.
576 ;; If there's a separator between L and H, prefer L2.
577 ((memq h (memq nil ll))
578 (outline-head-from-level level head l2l))
579 ;; If there's a separator between L2 and H, prefer L.
580 ((memq l2 (memq nil (setq hl (memq h ll)))) (car l))
581 ;; No separator between L and L2, check the distance.
582 ((< (* 2 (length hl)) (+ (length ll) (length l2l)))
583 (outline-head-from-level level head l2l))
584 ;; If all else fails, just keep L.
585 (t (car l)))))
586
587(defun outline-map-region (fun beg end)
588 "Call FUN for every heading between BEG and END.
589When FUN is called, point is at the beginning of the heading and
590the match data is set appropriately."
591 (save-excursion
592 (setq end (copy-marker end))
593 (goto-char beg)
594 (when (re-search-forward (concat "^\\(?:" outline-regexp "\\)") end t)
595 (goto-char (match-beginning 0))
596 (funcall fun)
597 (while (and (progn
598 (outline-next-heading)
599 (< (point) end))
600 (not (eobp)))
601 (funcall fun)))))
602
603;; Vertical tree motion
604
605(defun outline-move-subtree-up (&optional arg)
606 "Move the currrent subtree up past ARG headlines of the same level."
607 (interactive "p")
608 (outline-move-subtree-down (- arg)))
609
610(defun outline-move-subtree-down (&optional arg)
611 "Move the currrent subtree down past ARG headlines of the same level."
612 (interactive "p")
f56af8ca 613 (let ((re (concat "^\\(?:" outline-regexp "\\)"))
48c9ce10 614 (movfunc (if (> arg 0) 'outline-get-next-sibling
36a77f37
JB
615 'outline-get-last-sibling))
616 (ins-point (make-marker))
617 (cnt (abs arg))
618 beg end txt folded)
619 ;; Select the tree
620 (outline-back-to-heading)
621 (setq beg (point))
48c9ce10
JB
622 (save-match-data
623 (save-excursion (outline-end-of-heading)
36a77f37
JB
624 (setq folded (outline-invisible-p)))
625 (outline-end-of-subtree))
626 (if (= (char-after) ?\n) (forward-char 1))
627 (setq end (point))
628 ;; Find insertion point, with error handling
629 (goto-char beg)
630 (while (> cnt 0)
631 (or (funcall movfunc)
632 (progn (goto-char beg)
633 (error "Cannot move past superior level")))
634 (setq cnt (1- cnt)))
635 (if (> arg 0)
636 ;; Moving forward - still need to move over subtree
48c9ce10 637 (progn (outline-end-of-subtree)
36a77f37
JB
638 (if (= (char-after) ?\n) (forward-char 1))))
639 (move-marker ins-point (point))
640 (insert (delete-and-extract-region beg end))
641 (goto-char ins-point)
642 (if folded (hide-subtree))
643 (move-marker ins-point nil)))
644
645(defun outline-end-of-heading ()
646 (if (re-search-forward outline-heading-end-regexp nil 'move)
647 (forward-char -1)))
648
649(defun outline-next-visible-heading (arg)
650 "Move to the next visible heading line.
651With argument, repeats or can move backward if negative.
652A heading line is one that starts with a `*' (or that
653`outline-regexp' matches)."
654 (interactive "p")
655 (if (< arg 0)
656 (beginning-of-line)
657 (end-of-line))
658 (while (and (not (bobp)) (< arg 0))
659 (while (and (not (bobp))
660 (re-search-backward (concat "^\\(?:" outline-regexp "\\)")
661 nil 'move)
662 (outline-invisible-p)))
663 (setq arg (1+ arg)))
664 (while (and (not (eobp)) (> arg 0))
665 (while (and (not (eobp))
666 (re-search-forward (concat "^\\(?:" outline-regexp "\\)")
667 nil 'move)
668 (outline-invisible-p (match-beginning 0))))
669 (setq arg (1- arg)))
670 (beginning-of-line))
671
672(defun outline-previous-visible-heading (arg)
673 "Move to the previous heading line.
674With argument, repeats or can move forward if negative.
675A heading line is one that starts with a `*' (or that
676`outline-regexp' matches)."
677 (interactive "p")
678 (outline-next-visible-heading (- arg)))
679
680(defun outline-mark-subtree ()
681 "Mark the current subtree in an outlined document.
682This puts point at the start of the current subtree, and mark at the end."
683 (interactive)
684 (let ((beg))
685 (if (outline-on-heading-p)
686 ;; we are already looking at a heading
687 (beginning-of-line)
688 ;; else go back to previous heading
689 (outline-previous-visible-heading 1))
690 (setq beg (point))
691 (outline-end-of-subtree)
692 (push-mark (point))
693 (goto-char beg)))
694\f
695
696(put 'outline 'reveal-toggle-invisible 'outline-reveal-toggle-invisible)
697(defun outline-flag-region (from to flag)
698 "Hide or show lines from FROM to TO, according to FLAG.
699If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
700 (remove-overlays from to 'invisible 'outline)
701 (when flag
702 (let ((o (make-overlay from to)))
703 (overlay-put o 'invisible 'outline)
704 (overlay-put o 'isearch-open-invisible 'outline-isearch-open-invisible)))
705 ;; Seems only used by lazy-lock. I.e. obsolete.
706 (run-hooks 'outline-view-change-hook))
707
708(defun outline-reveal-toggle-invisible (o hidep)
709 (save-excursion
710 (goto-char (overlay-start o))
711 (if hidep
712 ;; When hiding the area again, we could just clean it up and let
713 ;; reveal do the rest, by simply doing:
714 ;; (remove-overlays (overlay-start o) (overlay-end o)
715 ;; 'invisible 'outline)
48c9ce10 716 ;;
36a77f37
JB
717 ;; That works fine as long as everything is in sync, but if the
718 ;; structure of the document is changed while revealing parts of it,
719 ;; the resulting behavior can be ugly. I.e. we need to make
720 ;; sure that we hide exactly a subtree.
721 (progn
722 (let ((end (overlay-end o)))
723 (delete-overlay o)
724 (while (progn
725 (hide-subtree)
726 (outline-next-visible-heading 1)
727 (and (not (eobp)) (< (point) end))))))
728
729 ;; When revealing, we just need to reveal sublevels. If point is
730 ;; inside one of the sublevels, reveal will call us again.
731 ;; But we need to preserve the original overlay.
732 (let ((o1 (copy-overlay o)))
733 (overlay-put o 'invisible nil) ;Show (most of) the text.
734 (while (progn
735 (show-entry)
736 (show-children)
737 ;; Normally just the above is needed.
738 ;; But in odd cases, the above might fail to show anything.
739 ;; To avoid an infinite loop, we have to make sure that
740 ;; *something* gets shown.
741 (and (equal (overlay-start o) (overlay-start o1))
742 (< (point) (overlay-end o))
743 (= 0 (forward-line 1)))))
744 ;; If still nothing was shown, just kill the damn thing.
745 (when (equal (overlay-start o) (overlay-start o1))
746 ;; I've seen it happen at the end of buffer.
747 (delete-overlay o1))))))
748
749;; Function to be set as an outline-isearch-open-invisible' property
750;; to the overlay that makes the outline invisible (see
751;; `outline-flag-region').
752(defun outline-isearch-open-invisible (overlay)
753 ;; We rely on the fact that isearch places point on the matched text.
754 (show-entry))
755\f
756(defun hide-entry ()
757 "Hide the body directly following this heading."
758 (interactive)
759 (outline-back-to-heading)
36a77f37 760 (save-excursion
e4619728 761 (outline-end-of-heading)
36a77f37
JB
762 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
763
764(defun show-entry ()
765 "Show the body directly following this heading.
766Show the heading too, if it is currently invisible."
767 (interactive)
768 (save-excursion
769 (outline-back-to-heading t)
770 (outline-flag-region (1- (point))
771 (progn (outline-next-preface) (point)) nil)))
772
773(defun hide-body ()
00958fa2 774 "Hide all body lines in buffer, leaving all headings visible."
36a77f37
JB
775 (interactive)
776 (hide-region-body (point-min) (point-max)))
777
778(defun hide-region-body (start end)
779 "Hide all body lines in the region, but not headings."
780 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
781 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
782 ;; and run the hook finally.
783 (let (outline-view-change-hook)
784 (save-excursion
785 (save-restriction
786 (narrow-to-region start end)
787 (goto-char (point-min))
788 (if (outline-on-heading-p)
00958fa2
EZ
789 (outline-end-of-heading)
790 (outline-next-preface))
36a77f37
JB
791 (while (not (eobp))
792 (outline-flag-region (point)
793 (progn (outline-next-preface) (point)) t)
794 (unless (eobp)
795 (forward-char (if (looking-at "\n\n") 2 1))
796 (outline-end-of-heading))))))
797 (run-hooks 'outline-view-change-hook))
798
799(defun show-all ()
800 "Show all of the text in the buffer."
801 (interactive)
802 (outline-flag-region (point-min) (point-max) nil))
803
804(defun hide-subtree ()
805 "Hide everything after this heading at deeper levels."
806 (interactive)
807 (outline-flag-subtree t))
808
809(defun hide-leaves ()
810 "Hide all body after this heading at deeper levels."
811 (interactive)
812 (outline-back-to-heading)
813 (save-excursion
814 (outline-end-of-heading)
815 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
816
817(defun show-subtree ()
818 "Show everything after this heading at deeper levels."
819 (interactive)
820 (outline-flag-subtree nil))
821
822(defun outline-show-heading ()
823 "Show the current heading and move to its end."
ca858c64
JL
824 (outline-flag-region (- (point)
825 (if (bobp) 0
826 (if (and outline-blank-line
827 (eq (char-before (1- (point))) ?\n))
828 2 1)))
36a77f37
JB
829 (progn (outline-end-of-heading) (point))
830 nil))
831
832(defun hide-sublevels (levels)
833 "Hide everything but the top LEVELS levels of headers, in whole buffer."
834 (interactive "p")
835 (if (< levels 1)
836 (error "Must keep at least one level of headers"))
837 (let (outline-view-change-hook)
838 (save-excursion
839 (goto-char (point-min))
840 ;; Skip the prelude, if any.
841 (unless (outline-on-heading-p t) (outline-next-heading))
842 ;; First hide everything.
843 (outline-flag-region (point) (point-max) t)
844 ;; Then unhide the top level headers.
845 (outline-map-region
846 (lambda ()
847 (if (<= (funcall outline-level) levels)
848 (outline-show-heading)))
849 (point) (point-max))))
850 (run-hooks 'outline-view-change-hook))
851
852(defun hide-other ()
853 "Hide everything except current body and parent and top-level headings."
854 (interactive)
855 (hide-sublevels 1)
856 (let (outline-view-change-hook)
857 (save-excursion
858 (outline-back-to-heading t)
859 (show-entry)
6e32922d 860 (while (condition-case nil (progn (outline-up-heading 1 t) (not (bobp)))
36a77f37
JB
861 (error nil))
862 (outline-flag-region (1- (point))
863 (save-excursion (forward-line 1) (point))
864 nil))))
865 (run-hooks 'outline-view-change-hook))
866
867(defun outline-toggle-children ()
868 "Show or hide the current subtree depending on its current state."
869 (interactive)
870 (outline-back-to-heading)
871 (if (not (outline-invisible-p (line-end-position)))
872 (hide-subtree)
873 (show-children)
874 (show-entry)))
875
876(defun outline-flag-subtree (flag)
877 (save-excursion
878 (outline-back-to-heading)
879 (outline-end-of-heading)
880 (outline-flag-region (point)
881 (progn (outline-end-of-subtree) (point))
882 flag)))
883
884(defun outline-end-of-subtree ()
885 (outline-back-to-heading)
886 (let ((opoint (point))
887 (first t)
888 (level (funcall outline-level)))
889 (while (and (not (eobp))
890 (or first (> (funcall outline-level) level)))
891 (setq first nil)
892 (outline-next-heading))
893 (if (bolp)
894 (progn
895 ;; Go to end of line before heading
ca858c64
JL
896 (forward-char -1)
897 (if (and outline-blank-line (bolp))
898 ;; leave blank line before heading
899 (forward-char -1))))))
36a77f37
JB
900\f
901(defun show-branches ()
902 "Show all subheadings of this heading, but not their bodies."
903 (interactive)
904 (show-children 1000))
905
906(defun show-children (&optional level)
907 "Show all direct subheadings of this heading.
908Prefix arg LEVEL is how many levels below the current level should be shown.
909Default is enough to cause the following heading to appear."
910 (interactive "P")
911 (setq level
912 (if level (prefix-numeric-value level)
913 (save-excursion
914 (outline-back-to-heading)
915 (let ((start-level (funcall outline-level)))
916 (outline-next-heading)
917 (if (eobp)
918 1
919 (max 1 (- (funcall outline-level) start-level)))))))
920 (let (outline-view-change-hook)
921 (save-excursion
922 (outline-back-to-heading)
923 (setq level (+ level (funcall outline-level)))
924 (outline-map-region
925 (lambda ()
926 (if (<= (funcall outline-level) level)
927 (outline-show-heading)))
928 (point)
929 (progn (outline-end-of-subtree)
930 (if (eobp) (point-max) (1+ (point)))))))
931 (run-hooks 'outline-view-change-hook))
932
933\f
934
935(defun outline-up-heading (arg &optional invisible-ok)
936 "Move to the visible heading line of which the present line is a subheading.
937With argument, move up ARG levels.
938If INVISIBLE-OK is non-nil, also consider invisible lines."
939 (interactive "p")
e4619728
JL
940 (and (eq this-command 'outline-up-heading)
941 (or (eq last-command 'outline-up-heading) (push-mark)))
36a77f37
JB
942 (outline-back-to-heading invisible-ok)
943 (let ((start-level (funcall outline-level)))
944 (if (eq start-level 1)
945 (error "Already at top level of the outline"))
946 (while (and (> start-level 1) (> arg 0) (not (bobp)))
947 (let ((level start-level))
948 (while (not (or (< level start-level) (bobp)))
949 (if invisible-ok
950 (outline-previous-heading)
951 (outline-previous-visible-heading 1))
952 (setq level (funcall outline-level)))
953 (setq start-level level))
954 (setq arg (- arg 1))))
955 (looking-at outline-regexp))
956
957(defun outline-forward-same-level (arg)
958 "Move forward to the ARG'th subheading at same level as this one.
959Stop at the first and last subheadings of a superior heading."
960 (interactive "p")
961 (outline-back-to-heading)
962 (while (> arg 0)
963 (let ((point-to-move-to (save-excursion
964 (outline-get-next-sibling))))
965 (if point-to-move-to
966 (progn
967 (goto-char point-to-move-to)
968 (setq arg (1- arg)))
969 (progn
970 (setq arg 0)
971 (error "No following same-level heading"))))))
972
973(defun outline-get-next-sibling ()
974 "Move to next heading of the same level, and return point or nil if none."
975 (let ((level (funcall outline-level)))
976 (outline-next-visible-heading 1)
977 (while (and (not (eobp)) (> (funcall outline-level) level))
978 (outline-next-visible-heading 1))
979 (if (or (eobp) (< (funcall outline-level) level))
980 nil
981 (point))))
982
983(defun outline-backward-same-level (arg)
984 "Move backward to the ARG'th subheading at same level as this one.
985Stop at the first and last subheadings of a superior heading."
986 (interactive "p")
987 (outline-back-to-heading)
988 (while (> arg 0)
989 (let ((point-to-move-to (save-excursion
990 (outline-get-last-sibling))))
991 (if point-to-move-to
992 (progn
993 (goto-char point-to-move-to)
994 (setq arg (1- arg)))
995 (progn
996 (setq arg 0)
997 (error "No previous same-level heading"))))))
998
999(defun outline-get-last-sibling ()
1000 "Move to previous heading of the same level, and return point or nil if none."
1001 (let ((level (funcall outline-level)))
1002 (outline-previous-visible-heading 1)
1003 (while (and (> (funcall outline-level) level)
1004 (not (bobp)))
1005 (outline-previous-visible-heading 1))
1006 (if (< (funcall outline-level) level)
1007 nil
1008 (point))))
1009\f
1010(defun outline-headers-as-kill (beg end)
1011 "Save the visible outline headers in region at the start of the kill ring.
1012
1013Text shown between the headers isn't copied. Two newlines are
1014inserted between saved headers. Yanking the result may be a
1015convenient way to make a table of contents of the buffer."
1016 (interactive "r")
1017 (save-excursion
1018 (save-restriction
1019 (narrow-to-region beg end)
1020 (goto-char (point-min))
1021 (let ((buffer (current-buffer))
1022 start end)
1023 (with-temp-buffer
1024 (with-current-buffer buffer
1025 ;; Boundary condition: starting on heading:
1026 (when (outline-on-heading-p)
1027 (outline-back-to-heading)
1028 (setq start (point)
1029 end (progn (outline-end-of-heading)
1030 (point)))
1031 (insert-buffer-substring buffer start end)
1032 (insert "\n\n")))
1033 (let ((temp-buffer (current-buffer)))
1034 (with-current-buffer buffer
1035 (while (outline-next-heading)
1036 (unless (outline-invisible-p)
1037 (setq start (point)
1038 end (progn (outline-end-of-heading) (point)))
1039 (with-current-buffer temp-buffer
1040 (insert-buffer-substring buffer start end)
1041 (insert "\n\n"))))))
1042 (kill-new (buffer-string)))))))
1043
1044(provide 'outline)
1045(provide 'noutline)
1046
ab5796a9 1047;;; arch-tag: 1724410e-7d4d-4f46-b801-49e18171e874
36a77f37 1048;;; outline.el ends here