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