Converted backquote to the new style.
[bpt/emacs.git] / lisp / textmodes / outline.el
1 ;;; outline.el --- outline mode commands for Emacs
2
3 ;; Copyright (C) 1986, 93, 94, 95, 97, 2000, 2001
4 ;; Free Software Foundation, Inc.
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
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
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
36 ;;; Code:
37
38 (defgroup outlines nil
39 "Support for hierarchical outlining"
40 :prefix "outline-"
41 :group 'editing)
42
43 (defcustom 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 :type '(choice regexp (const nil))
51 :group 'outlines)
52
53 (defcustom outline-heading-end-regexp "\n"
54 "*Regular expression to match the end of a heading line.
55 You can assume that point is at the beginning of a heading when this
56 regexp is searched for. The heading ends at the end of the match.
57 The recommended way to set this is with a `Local Variables:' list
58 in the file it applies to."
59 :type 'regexp
60 :group 'outlines)
61
62 (defvar outline-mode-prefix-map nil)
63
64 (if outline-mode-prefix-map
65 nil
66 (setq outline-mode-prefix-map (make-sparse-keymap))
67 (define-key outline-mode-prefix-map "@" 'outline-mark-subtree)
68 (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
69 (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
70 (define-key outline-mode-prefix-map "\C-i" 'show-children)
71 (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
72 (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
73 (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
74 (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
75 (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
76 (define-key outline-mode-prefix-map "\C-t" 'hide-body)
77 (define-key outline-mode-prefix-map "\C-a" 'show-all)
78 (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
79 (define-key outline-mode-prefix-map "\C-e" 'show-entry)
80 (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
81 (define-key outline-mode-prefix-map "\C-k" 'show-branches)
82 (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
83 (define-key outline-mode-prefix-map "\C-o" 'hide-other))
84
85 (defvar outline-mode-menu-bar-map nil)
86 (if outline-mode-menu-bar-map
87 nil
88 (setq outline-mode-menu-bar-map (make-sparse-keymap))
89
90 (define-key outline-mode-menu-bar-map [hide]
91 (cons "Hide" (make-sparse-keymap "Hide")))
92
93 (define-key outline-mode-menu-bar-map [hide hide-other]
94 '("Hide Other" . hide-other))
95 (define-key outline-mode-menu-bar-map [hide hide-sublevels]
96 '("Hide Sublevels" . hide-sublevels))
97 (define-key outline-mode-menu-bar-map [hide hide-subtree]
98 '("Hide Subtree" . hide-subtree))
99 (define-key outline-mode-menu-bar-map [hide hide-entry]
100 '("Hide Entry" . hide-entry))
101 (define-key outline-mode-menu-bar-map [hide hide-body]
102 '("Hide Body" . hide-body))
103 (define-key outline-mode-menu-bar-map [hide hide-leaves]
104 '("Hide Leaves" . hide-leaves))
105
106 (define-key outline-mode-menu-bar-map [show]
107 (cons "Show" (make-sparse-keymap "Show")))
108
109 (define-key outline-mode-menu-bar-map [show show-subtree]
110 '("Show Subtree" . show-subtree))
111 (define-key outline-mode-menu-bar-map [show show-children]
112 '("Show Children" . show-children))
113 (define-key outline-mode-menu-bar-map [show show-branches]
114 '("Show Branches" . show-branches))
115 (define-key outline-mode-menu-bar-map [show show-entry]
116 '("Show Entry" . show-entry))
117 (define-key outline-mode-menu-bar-map [show show-all]
118 '("Show All" . show-all))
119
120 (define-key outline-mode-menu-bar-map [headings]
121 (cons "Headings" (make-sparse-keymap "Headings")))
122
123 (define-key outline-mode-menu-bar-map [headings copy]
124 '(menu-item "Copy to kill ring" outline-headers-as-kill
125 :enable mark-active))
126 (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
127 '("Previous Same Level" . outline-backward-same-level))
128 (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
129 '("Next Same Level" . outline-forward-same-level))
130 (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
131 '("Previous" . outline-previous-visible-heading))
132 (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
133 '("Next" . outline-next-visible-heading))
134 (define-key outline-mode-menu-bar-map [headings outline-up-heading]
135 '("Up" . outline-up-heading)))
136
137 (defvar outline-mode-map nil "")
138
139 (if outline-mode-map
140 nil
141 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
142 (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
143 (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
144
145 (defvar outline-font-lock-keywords
146 '(;;
147 ;; Highlight headings according to the level.
148 (eval . (list (concat "^" outline-regexp ".+")
149 0 '(or (cdr (assq (outline-font-lock-level)
150 '((1 . font-lock-function-name-face)
151 (2 . font-lock-variable-name-face)
152 (3 . font-lock-keyword-face)
153 (4 . font-lock-builtin-face)
154 (5 . font-lock-comment-face)
155 (6 . font-lock-constant-face)
156 (7 . font-lock-type-face)
157 (8 . font-lock-string-face))))
158 font-lock-warning-face)
159 nil t)))
160 "Additional expressions to highlight in Outline mode.")
161
162 (defun outline-font-lock-level ()
163 (let ((count 1))
164 (save-excursion
165 (outline-back-to-heading t)
166 (while (and (not (bobp))
167 (not (eq (funcall outline-level) 1)))
168 (outline-up-heading-all 1)
169 (setq count (1+ count)))
170 count)))
171
172 (defvar outline-view-change-hook nil
173 "Normal hook to be run after outline visibility changes.")
174
175 ;;;###autoload
176 (define-derived-mode outline-mode text-mode "Outline"
177 "Set major mode for editing outlines with selective display.
178 Headings are lines which start with asterisks: one for major headings,
179 two for subheadings, etc. Lines not starting with asterisks are body lines.
180
181 Body text or subheadings under a heading can be made temporarily
182 invisible, or visible again. Invisible lines are attached to the end
183 of the heading, so they move with it, if the line is killed and yanked
184 back. A heading with text hidden under it is marked with an ellipsis (...).
185
186 Commands:\\<outline-mode-map>
187 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
188 \\[outline-previous-visible-heading] outline-previous-visible-heading
189 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
190 \\[outline-backward-same-level] outline-backward-same-level
191 \\[outline-up-heading] outline-up-heading move from subheading to heading
192
193 \\[hide-body] make all text invisible (not headings).
194 \\[show-all] make everything in buffer visible.
195
196 The remaining commands are used when point is on a heading line.
197 They apply to some of the body or subheadings of that heading.
198 \\[hide-subtree] hide-subtree make body and subheadings invisible.
199 \\[show-subtree] show-subtree make body and subheadings visible.
200 \\[show-children] show-children make direct subheadings visible.
201 No effect on body, or subheadings 2 or more levels down.
202 With arg N, affects subheadings N levels down.
203 \\[hide-entry] make immediately following body invisible.
204 \\[show-entry] make it visible.
205 \\[hide-leaves] make body under heading and under its subheadings invisible.
206 The subheadings remain visible.
207 \\[show-branches] make all subheadings at all levels visible.
208
209 The variable `outline-regexp' can be changed to control what is a heading.
210 A line is a heading if `outline-regexp' matches something at the
211 beginning of the line. The longer the match, the deeper the level.
212
213 Turning on outline mode calls the value of `text-mode-hook' and then of
214 `outline-mode-hook', if they are non-nil."
215 (make-local-variable 'line-move-ignore-invisible)
216 (setq line-move-ignore-invisible t)
217 ;; Cause use of ellipses for invisible text.
218 (add-to-invisibility-spec '(outline . t))
219 (set (make-local-variable 'paragraph-start)
220 (concat paragraph-start "\\|\\(" outline-regexp "\\)"))
221 ;; Inhibit auto-filling of header lines.
222 (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
223 (set (make-local-variable 'paragraph-separate)
224 (concat paragraph-separate "\\|\\(" outline-regexp "\\)"))
225 (set (make-local-variable 'font-lock-defaults)
226 '(outline-font-lock-keywords t nil nil backward-paragraph))
227 (setq imenu-generic-expression
228 (list (list nil (concat "^\\(?:" outline-regexp "\\).*$") 0)))
229 (add-hook 'change-major-mode-hook 'show-all nil t))
230
231 (defcustom outline-minor-mode-prefix "\C-c@"
232 "*Prefix key to use for Outline commands in Outline minor mode.
233 The value of this variable is checked as part of loading Outline mode.
234 After that, changing the prefix key requires manipulating keymaps."
235 :type 'string
236 :group 'outlines)
237
238 ;;;###autoload
239 (define-minor-mode outline-minor-mode
240 "Toggle Outline minor mode.
241 With arg, turn Outline minor mode on if arg is positive, off otherwise.
242 See the command `outline-mode' for more information on this mode."
243 nil " Outl" (list (cons [menu-bar] outline-mode-menu-bar-map)
244 (cons outline-minor-mode-prefix outline-mode-prefix-map))
245 (if outline-minor-mode
246 (progn
247 ;; Turn off this mode if we change major modes.
248 (add-hook 'change-major-mode-hook
249 (lambda () (outline-minor-mode -1))
250 nil t)
251 (set (make-local-variable 'line-move-ignore-invisible) t)
252 ;; Cause use of ellipses for invisible text.
253 (add-to-invisibility-spec '(outline . t)))
254 (setq line-move-ignore-invisible nil)
255 ;; Cause use of ellipses for invisible text.
256 (remove-from-invisibility-spec '(outline . t)))
257 ;; When turning off outline mode, get rid of any outline hiding.
258 (or outline-minor-mode
259 (show-all)))
260 \f
261 (defcustom outline-level 'outline-level
262 "*Function of no args to compute a header's nesting level in an outline.
263 It can assume point is at the beginning of a header line."
264 :type 'function
265 :group 'outlines)
266
267 ;; This used to count columns rather than characters, but that made ^L
268 ;; appear to be at level 2 instead of 1. Columns would be better for
269 ;; tab handling, but the default regexp doesn't use tabs, and anyone
270 ;; who changes the regexp can also redefine the outline-level variable
271 ;; as appropriate.
272 (defun outline-level ()
273 "Return the depth to which a statement is nested in the outline.
274 Point must be at the beginning of a header line. This is actually
275 the number of characters that `outline-regexp' matches."
276 (save-excursion
277 (looking-at outline-regexp)
278 (- (match-end 0) (match-beginning 0))))
279
280 (defun outline-next-preface ()
281 "Skip forward to just before the next heading line.
282 If there's no following heading line, stop before the newline
283 at the end of the buffer."
284 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
285 nil 'move)
286 (goto-char (match-beginning 0)))
287 (if (and (bolp) (not (bobp)))
288 (forward-char -1)))
289
290 (defun outline-next-heading ()
291 "Move to the next (possibly invisible) heading line."
292 (interactive)
293 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
294 nil 'move)
295 (goto-char (1+ (match-beginning 0)))))
296
297 (defun outline-previous-heading ()
298 "Move to the previous (possibly invisible) heading line."
299 (interactive)
300 (re-search-backward (concat "^\\(" outline-regexp "\\)")
301 nil 'move))
302
303 (defsubst outline-invisible-p ()
304 "Non-nil if the character after point is invisible."
305 (get-char-property (point) 'invisible))
306 (defun outline-visible ()
307 "Obsolete. Use `outline-invisible-p'."
308 (not (outline-invisible-p)))
309
310 (defun outline-back-to-heading (&optional invisible-ok)
311 "Move to previous heading line, or beg of this line if it's a heading.
312 Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
313 (beginning-of-line)
314 (or (outline-on-heading-p invisible-ok)
315 (let (found)
316 (save-excursion
317 (while (not found)
318 (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
319 nil t)
320 (error "before first heading"))
321 (setq found (and (or invisible-ok (outline-visible)) (point)))))
322 (goto-char found)
323 found)))
324
325 (defun outline-on-heading-p (&optional invisible-ok)
326 "Return t if point is on a (visible) heading line.
327 If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
328 (save-excursion
329 (beginning-of-line)
330 (and (bolp) (or invisible-ok (outline-visible))
331 (looking-at outline-regexp))))
332
333 (defun outline-end-of-heading ()
334 (if (re-search-forward outline-heading-end-regexp nil 'move)
335 (forward-char -1)))
336
337 (defun outline-next-visible-heading (arg)
338 "Move to the next visible heading line.
339 With argument, repeats or can move backward if negative.
340 A heading line is one that starts with a `*' (or that
341 `outline-regexp' matches)."
342 (interactive "p")
343 (if (< arg 0)
344 (beginning-of-line)
345 (end-of-line))
346 (while (and (not (bobp)) (< arg 0))
347 (while (and (not (bobp))
348 (re-search-backward (concat "^\\(" outline-regexp "\\)")
349 nil 'move)
350 (not (outline-visible))))
351 (setq arg (1+ arg)))
352 (while (and (not (eobp)) (> arg 0))
353 (while (and (not (eobp))
354 (re-search-forward (concat "^\\(" outline-regexp "\\)")
355 nil 'move)
356 (not (outline-visible))))
357 (setq arg (1- arg)))
358 (beginning-of-line))
359
360 (defun outline-previous-visible-heading (arg)
361 "Move to the previous heading line.
362 With argument, repeats or can move forward if negative.
363 A heading line is one that starts with a `*' (or that
364 `outline-regexp' matches)."
365 (interactive "p")
366 (outline-next-visible-heading (- arg)))
367
368 (defun outline-mark-subtree ()
369 "Mark the current subtree in an outlined document.
370 This puts point at the start of the current subtree, and mark at the end."
371 (interactive)
372 (let ((beg))
373 (if (outline-on-heading-p)
374 ;; we are already looking at a heading
375 (beginning-of-line)
376 ;; else go back to previous heading
377 (outline-previous-visible-heading 1))
378 (setq beg (point))
379 (outline-end-of-subtree)
380 (push-mark (point))
381 (goto-char beg)))
382 \f
383 (defun outline-flag-region (from to flag)
384 "Hides or shows lines from FROM to TO, according to FLAG.
385 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
386 (save-excursion
387 (goto-char from)
388 (end-of-line)
389 (outline-discard-overlays (point) to 'outline)
390 (if flag
391 (let ((o (make-overlay (point) to)))
392 (overlay-put o 'invisible 'outline)
393 (overlay-put o 'isearch-open-invisible
394 'outline-isearch-open-invisible))))
395 (run-hooks 'outline-view-change-hook))
396
397
398 ;; Function to be set as an outline-isearch-open-invisible' property
399 ;; to the overlay that makes the outline invisible (see
400 ;; `outline-flag-region').
401 (defun outline-isearch-open-invisible (overlay)
402 ;; We rely on the fact that isearch places point one the matched text.
403 (show-entry))
404
405
406 ;; Exclude from the region BEG ... END all overlays
407 ;; which have PROP as the value of the `invisible' property.
408 ;; Exclude them by shrinking them to exclude BEG ... END,
409 ;; or even by splitting them if necessary.
410 ;; Overlays without such an `invisible' property are not touched.
411 (defun outline-discard-overlays (beg end prop)
412 (if (< end beg)
413 (setq beg (prog1 end (setq end beg))))
414 (save-excursion
415 (dolist (o (overlays-in beg end))
416 (if (eq (overlay-get o 'invisible) prop)
417 ;; Either push this overlay outside beg...end
418 ;; or split it to exclude beg...end
419 ;; or delete it entirely (if it is contained in beg...end).
420 (if (< (overlay-start o) beg)
421 (if (> (overlay-end o) end)
422 (progn
423 (move-overlay (outline-copy-overlay o)
424 (overlay-start o) beg)
425 (move-overlay o end (overlay-end o)))
426 (move-overlay o (overlay-start o) beg))
427 (if (> (overlay-end o) end)
428 (move-overlay o end (overlay-end o))
429 (delete-overlay o)))))))
430
431 ;; Make a copy of overlay O, with the same beginning, end and properties.
432 (defun outline-copy-overlay (o)
433 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
434 (overlay-buffer o)))
435 (props (overlay-properties o)))
436 (while props
437 (overlay-put o1 (car props) (nth 1 props))
438 (setq props (cdr (cdr props))))
439 o1))
440 \f
441 (defun hide-entry ()
442 "Hide the body directly following this heading."
443 (interactive)
444 (outline-back-to-heading)
445 (outline-end-of-heading)
446 (save-excursion
447 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
448
449 (defun show-entry ()
450 "Show the body directly following this heading.
451 Show the heading too, if it is currently invisible."
452 (interactive)
453 (save-excursion
454 (outline-back-to-heading t)
455 (outline-flag-region (1- (point))
456 (progn (outline-next-preface) (point)) nil)))
457
458 (defun hide-body ()
459 "Hide all of buffer except headings."
460 (interactive)
461 (hide-region-body (point-min) (point-max)))
462
463 (defun hide-region-body (start end)
464 "Hide all body lines in the region, but not headings."
465 ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
466 ;; wasting lots of time running `lazy-lock-fontify-after-outline'
467 ;; and run the hook finally.
468 (let (outline-view-change-hook)
469 (save-excursion
470 (save-restriction
471 (narrow-to-region start end)
472 (goto-char (point-min))
473 (if (outline-on-heading-p)
474 (outline-end-of-heading))
475 (while (not (eobp))
476 (outline-flag-region (point)
477 (progn (outline-next-preface) (point)) t)
478 (unless (eobp)
479 (forward-char (if (looking-at "\n\n") 2 1))
480 (outline-end-of-heading))))))
481 (run-hooks 'outline-view-change-hook))
482
483 (defun show-all ()
484 "Show all of the text in the buffer."
485 (interactive)
486 (outline-flag-region (point-min) (point-max) nil))
487
488 (defun hide-subtree ()
489 "Hide everything after this heading at deeper levels."
490 (interactive)
491 (outline-flag-subtree t))
492
493 (defun hide-leaves ()
494 "Hide all body after this heading at deeper levels."
495 (interactive)
496 (outline-back-to-heading)
497 (save-excursion
498 (outline-end-of-heading)
499 (hide-region-body (point) (progn (outline-end-of-subtree) (point)))))
500
501 (defun show-subtree ()
502 "Show everything after this heading at deeper levels."
503 (interactive)
504 (outline-flag-subtree nil))
505
506 (defun hide-sublevels (levels)
507 "Hide everything but the top LEVELS levels of headers, in whole buffer."
508 (interactive "p")
509 (if (< levels 1)
510 (error "Must keep at least one level of headers"))
511 (setq levels (1- levels))
512 (let (outline-view-change-hook)
513 (save-excursion
514 (goto-char (point-min))
515 ;; Keep advancing to the next top-level heading.
516 (while (or (and (bobp) (outline-on-heading-p))
517 (outline-next-heading))
518 (let ((end (save-excursion (outline-end-of-subtree) (point))))
519 ;; Hide everything under that.
520 (outline-flag-region (point) end t)
521 ;; Show the first LEVELS levels under that.
522 (if (> levels 0)
523 (show-children levels))
524 ;; Move to the next, since we already found it.
525 (goto-char end)))))
526 (run-hooks 'outline-view-change-hook))
527
528 (defun hide-other ()
529 "Hide everything except current body and parent and top-level headings."
530 (interactive)
531 (hide-sublevels 1)
532 (let (outline-view-change-hook)
533 (save-excursion
534 (outline-back-to-heading t)
535 (show-entry)
536 (while (condition-case nil (progn (outline-up-heading 1) (not (bobp)))
537 (error nil))
538 (outline-flag-region (1- (point))
539 (save-excursion (forward-line 1) (point))
540 nil))))
541 (run-hooks 'outline-view-change-hook))
542
543 (defun outline-flag-subtree (flag)
544 (save-excursion
545 (outline-back-to-heading)
546 (outline-end-of-heading)
547 (outline-flag-region (point)
548 (progn (outline-end-of-subtree) (point))
549 flag)))
550
551 (defun outline-end-of-subtree ()
552 (outline-back-to-heading)
553 (let ((opoint (point))
554 (first t)
555 (level (funcall outline-level)))
556 (while (and (not (eobp))
557 (or first (> (funcall outline-level) level)))
558 (setq first nil)
559 (outline-next-heading))
560 (if (bolp)
561 (progn
562 ;; Go to end of line before heading
563 (forward-char -1)
564 (if (bolp)
565 ;; leave blank line before heading
566 (forward-char -1))))))
567 \f
568 (defun show-branches ()
569 "Show all subheadings of this heading, but not their bodies."
570 (interactive)
571 (show-children 1000))
572
573 (defun show-children (&optional level)
574 "Show all direct subheadings of this heading.
575 Prefix arg LEVEL is how many levels below the current level should be shown.
576 Default is enough to cause the following heading to appear."
577 (interactive "P")
578 (setq level
579 (if level (prefix-numeric-value level)
580 (save-excursion
581 (outline-back-to-heading)
582 (let ((start-level (funcall outline-level)))
583 (outline-next-heading)
584 (if (eobp)
585 1
586 (max 1 (- (funcall outline-level) start-level)))))))
587 (let (outline-view-change-hook)
588 (save-excursion
589 (save-restriction
590 (outline-back-to-heading)
591 (setq level (+ level (funcall outline-level)))
592 (narrow-to-region (point)
593 (progn (outline-end-of-subtree)
594 (if (eobp) (point-max) (1+ (point)))))
595 (goto-char (point-min))
596 (while (and (not (eobp))
597 (progn
598 (outline-next-heading)
599 (not (eobp))))
600 (if (<= (funcall outline-level) level)
601 (save-excursion
602 (outline-flag-region (save-excursion
603 (forward-char -1)
604 (if (bolp)
605 (forward-char -1))
606 (point))
607 (progn (outline-end-of-heading) (point))
608 nil)))))))
609 (run-hooks 'outline-view-change-hook))
610 \f
611 (defun outline-up-heading-all (arg)
612 "Move to the heading line of which the present line is a subheading.
613 This function considers both visible and invisible heading lines.
614 With argument, move up ARG levels."
615 (outline-back-to-heading t)
616 (if (eq (funcall outline-level) 1)
617 (error "Already at top level of the outline"))
618 (while (and (> (funcall outline-level) 1)
619 (> arg 0)
620 (not (bobp)))
621 (let ((present-level (funcall outline-level)))
622 (while (and (not (< (funcall outline-level) present-level))
623 (not (bobp)))
624 (outline-previous-heading))
625 (setq arg (- arg 1)))))
626
627 (defun outline-up-heading (arg)
628 "Move to the visible heading line of which the present line is a subheading.
629 With argument, move up ARG levels."
630 (interactive "p")
631 (outline-back-to-heading)
632 (if (eq (funcall outline-level) 1)
633 (error "Already at top level of the outline"))
634 (while (and (> (funcall outline-level) 1)
635 (> arg 0)
636 (not (bobp)))
637 (let ((present-level (funcall outline-level)))
638 (while (and (not (< (funcall outline-level) present-level))
639 (not (bobp)))
640 (outline-previous-visible-heading 1))
641 (setq arg (- arg 1)))))
642
643 (defun outline-forward-same-level (arg)
644 "Move forward to the ARG'th subheading at same level as this one.
645 Stop at the first and last subheadings of a superior heading."
646 (interactive "p")
647 (outline-back-to-heading)
648 (while (> arg 0)
649 (let ((point-to-move-to (save-excursion
650 (outline-get-next-sibling))))
651 (if point-to-move-to
652 (progn
653 (goto-char point-to-move-to)
654 (setq arg (1- arg)))
655 (progn
656 (setq arg 0)
657 (error "No following same-level heading"))))))
658
659 (defun outline-get-next-sibling ()
660 "Move to next heading of the same level, and return point or nil if none."
661 (let ((level (funcall outline-level)))
662 (outline-next-visible-heading 1)
663 (while (and (> (funcall outline-level) level)
664 (not (eobp)))
665 (outline-next-visible-heading 1))
666 (if (< (funcall outline-level) level)
667 nil
668 (point))))
669
670 (defun outline-backward-same-level (arg)
671 "Move backward to the ARG'th subheading at same level as this one.
672 Stop at the first and last subheadings of a superior heading."
673 (interactive "p")
674 (outline-back-to-heading)
675 (while (> arg 0)
676 (let ((point-to-move-to (save-excursion
677 (outline-get-last-sibling))))
678 (if point-to-move-to
679 (progn
680 (goto-char point-to-move-to)
681 (setq arg (1- arg)))
682 (progn
683 (setq arg 0)
684 (error "No previous same-level heading"))))))
685
686 (defun outline-get-last-sibling ()
687 "Move to previous heading of the same level, and return point or nil if none."
688 (let ((level (funcall outline-level)))
689 (outline-previous-visible-heading 1)
690 (while (and (> (funcall outline-level) level)
691 (not (bobp)))
692 (outline-previous-visible-heading 1))
693 (if (< (funcall outline-level) level)
694 nil
695 (point))))
696 \f
697 (defun outline-headers-as-kill (beg end)
698 "Save the visible outline headers in region at the start of the kill ring.
699
700 Text shown between the headers isn't copied. Two newlines are
701 inserted between saved headers. Yanking the result may be a
702 convenient way to make a table of contents of the buffer."
703 (interactive "r")
704 (save-excursion
705 (save-restriction
706 (narrow-to-region beg end)
707 (goto-char (point-min))
708 (let ((buffer (current-buffer))
709 start end)
710 (with-temp-buffer
711 (with-current-buffer buffer
712 ;; Boundary condition: starting on heading:
713 (when (outline-on-heading-p)
714 (outline-back-to-heading)
715 (setq start (point)
716 end (progn (outline-end-of-heading)
717 (point)))
718 (insert-buffer-substring buffer start end)
719 (insert "\n\n")))
720 (let ((temp-buffer (current-buffer)))
721 (with-current-buffer buffer
722 (while (outline-next-heading)
723 (when (outline-visible)
724 (setq start (point)
725 end (progn (outline-end-of-heading) (point)))
726 (with-current-buffer temp-buffer
727 (insert-buffer-substring buffer start end)
728 (insert "\n\n"))))))
729 (kill-new (buffer-string)))))))
730
731 (provide 'outline)
732 (provide 'noutline)
733
734 ;;; outline.el ends here