]> code.delx.au - gnu-emacs/blob - lisp/textmodes/outline.el
(outline-discard-overlays):
[gnu-emacs] / lisp / textmodes / outline.el
1 ;;; outline.el --- outline mode commands for Emacs
2
3 ;; Copyright (C) 1986, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: outlines
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This package is a major mode for editing outline-format documents.
28 ;; An outline can be `abstracted' to show headers at any given level,
29 ;; with all stuff below hidden. See the Emacs manual for details.
30
31 ;;; Code:
32
33 ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
34
35 (defvar outline-regexp nil
36 "*Regular expression to match the beginning of a heading.
37 Any line whose beginning matches this regexp is considered to start a heading.
38 The recommended way to set this is with a Local Variables: list
39 in the file it applies to. See also outline-heading-end-regexp.")
40
41 ;; Can't initialize this in the defvar above -- some major modes have
42 ;; already assigned a local value to it.
43 (or (default-value 'outline-regexp)
44 (setq-default outline-regexp "[*\^L]+"))
45
46 (defvar outline-heading-end-regexp "\n"
47 "*Regular expression to match the end of a heading line.
48 You can assume that point is at the beginning of a heading when this
49 regexp is searched for. The heading ends at the end of the match.
50 The recommended way to set this is with a `Local Variables:' list
51 in the file it applies to.")
52
53 (defvar outline-mode-prefix-map nil)
54
55 (if outline-mode-prefix-map
56 nil
57 (setq outline-mode-prefix-map (make-sparse-keymap))
58 (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
59 (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
60 (define-key outline-mode-prefix-map "\C-i" 'show-children)
61 (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
62 (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
63 (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
64 (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
65 (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
66 (define-key outline-mode-prefix-map "\C-t" 'hide-body)
67 (define-key outline-mode-prefix-map "\C-a" 'show-all)
68 (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
69 (define-key outline-mode-prefix-map "\C-e" 'show-entry)
70 (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
71 (define-key outline-mode-prefix-map "\C-k" 'show-branches)
72 (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
73 (define-key outline-mode-prefix-map "\C-o" 'hide-other))
74
75 (defvar outline-mode-menu-bar-map nil)
76 (if outline-mode-menu-bar-map
77 nil
78 (setq outline-mode-menu-bar-map (make-sparse-keymap))
79
80 (define-key outline-mode-menu-bar-map [hide]
81 (cons "Hide" (make-sparse-keymap "Hide")))
82
83 (define-key outline-mode-menu-bar-map [hide hide-other]
84 '("Hide Other" . hide-other))
85 (define-key outline-mode-menu-bar-map [hide hide-sublevels]
86 '("Hide Sublevels" . hide-sublevels))
87 (define-key outline-mode-menu-bar-map [hide hide-subtree]
88 '("Hide Subtree" . hide-subtree))
89 (define-key outline-mode-menu-bar-map [hide hide-entry]
90 '("Hide Entry" . hide-entry))
91 (define-key outline-mode-menu-bar-map [hide hide-body]
92 '("Hide Body" . hide-body))
93 (define-key outline-mode-menu-bar-map [hide hide-leaves]
94 '("Hide Leaves" . hide-leaves))
95
96 (define-key outline-mode-menu-bar-map [show]
97 (cons "Show" (make-sparse-keymap "Show")))
98
99 (define-key outline-mode-menu-bar-map [show show-subtree]
100 '("Show Subtree" . show-subtree))
101 (define-key outline-mode-menu-bar-map [show show-children]
102 '("Show Children" . show-children))
103 (define-key outline-mode-menu-bar-map [show show-branches]
104 '("Show Branches" . show-branches))
105 (define-key outline-mode-menu-bar-map [show show-entry]
106 '("Show Entry" . show-entry))
107 (define-key outline-mode-menu-bar-map [show show-all]
108 '("Show All" . show-all))
109
110 (define-key outline-mode-menu-bar-map [headings]
111 (cons "Headings" (make-sparse-keymap "Headings")))
112
113 (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
114 '("Previous Same Level" . outline-backward-same-level))
115 (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
116 '("Next Same Level" . outline-forward-same-level))
117 (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
118 '("Previous" . outline-previous-visible-heading))
119 (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
120 '("Next" . outline-next-visible-heading))
121 (define-key outline-mode-menu-bar-map [headings outline-up-heading]
122 '("Up" . outline-up-heading)))
123
124 (defvar outline-mode-map nil "")
125
126 (if outline-mode-map
127 nil
128 (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
129 (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
130 (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))
131
132 (defvar outline-minor-mode nil
133 "Non-nil if using Outline mode as a minor mode of some other mode.")
134 (make-variable-buffer-local 'outline-minor-mode)
135 (or (assq 'outline-minor-mode minor-mode-alist)
136 (setq minor-mode-alist (append minor-mode-alist
137 (list '(outline-minor-mode " Outl")))))
138
139 (defvar outline-font-lock-keywords
140 '(;; Highlight headings according to the level.
141 ("^\\(\\*+\\)[ \t]*\\(.+\\)?[ \t]*$"
142 (1 font-lock-string-face)
143 (2 (let ((len (- (match-end 1) (match-beginning 1))))
144 (or (cdr (assq len '((1 . font-lock-function-name-face)
145 (2 . font-lock-keyword-face)
146 (3 . font-lock-comment-face))))
147 font-lock-variable-name-face))
148 nil t))
149 ;; Highlight citations of the form [1] and [Mar94].
150 ("\\[\\([A-Z][A-Za-z]+\\)*[0-9]+\\]" . font-lock-type-face))
151 "Additional expressions to highlight in Outline mode.")
152
153 (defvar outline-view-change-hook nil
154 "Normal hook to be run after outline visibility changes.")
155
156 ;;;autoload
157 (defun outline-mode ()
158 "Set major mode for editing outlines with selective display.
159 Headings are lines which start with asterisks: one for major headings,
160 two for subheadings, etc. Lines not starting with asterisks are body lines.
161
162 Body text or subheadings under a heading can be made temporarily
163 invisible, or visible again. Invisible lines are attached to the end
164 of the heading, so they move with it, if the line is killed and yanked
165 back. A heading with text hidden under it is marked with an ellipsis (...).
166
167 Commands:\\<outline-mode-map>
168 \\[outline-next-visible-heading] outline-next-visible-heading move by visible headings
169 \\[outline-previous-visible-heading] outline-previous-visible-heading
170 \\[outline-forward-same-level] outline-forward-same-level similar but skip subheadings
171 \\[outline-backward-same-level] outline-backward-same-level
172 \\[outline-up-heading] outline-up-heading move from subheading to heading
173
174 \\[hide-body] make all text invisible (not headings).
175 \\[show-all] make everything in buffer visible.
176
177 The remaining commands are used when point is on a heading line.
178 They apply to some of the body or subheadings of that heading.
179 \\[hide-subtree] hide-subtree make body and subheadings invisible.
180 \\[show-subtree] show-subtree make body and subheadings visible.
181 \\[show-children] show-children make direct subheadings visible.
182 No effect on body, or subheadings 2 or more levels down.
183 With arg N, affects subheadings N levels down.
184 \\[hide-entry] make immediately following body invisible.
185 \\[show-entry] make it visible.
186 \\[hide-leaves] make body under heading and under its subheadings invisible.
187 The subheadings remain visible.
188 \\[show-branches] make all subheadings at all levels visible.
189
190 The variable `outline-regexp' can be changed to control what is a heading.
191 A line is a heading if `outline-regexp' matches something at the
192 beginning of the line. The longer the match, the deeper the level.
193
194 Turning on outline mode calls the value of `text-mode-hook' and then of
195 `outline-mode-hook', if they are non-nil."
196 (interactive)
197 (kill-all-local-variables)
198 (use-local-map outline-mode-map)
199 (setq mode-name "Outline")
200 (setq major-mode 'outline-mode)
201 (define-abbrev-table 'text-mode-abbrev-table ())
202 (setq local-abbrev-table text-mode-abbrev-table)
203 (set-syntax-table text-mode-syntax-table)
204 (make-local-variable 'line-move-ignore-invisible)
205 (setq line-move-ignore-invisible t)
206 ;; Cause use of ellipses for invisible text.
207 (setq buffer-invisibility-spec '((t . t)))
208 (make-local-variable 'paragraph-start)
209 (setq paragraph-start (concat paragraph-start "\\|\\("
210 outline-regexp "\\)"))
211 ;; Inhibit auto-filling of header lines.
212 (make-local-variable 'auto-fill-inhibit-regexp)
213 (setq auto-fill-inhibit-regexp outline-regexp)
214 (make-local-variable 'paragraph-separate)
215 (setq paragraph-separate (concat paragraph-separate "\\|\\("
216 outline-regexp "\\)"))
217 (make-local-variable 'font-lock-defaults)
218 (setq font-lock-defaults '(outline-font-lock-keywords t))
219 (make-local-variable 'change-major-mode-hook)
220 (add-hook 'change-major-mode-hook 'show-all)
221 (run-hooks 'text-mode-hook 'outline-mode-hook))
222
223 (defvar outline-minor-mode-prefix "\C-c@"
224 "*Prefix key to use for Outline commands in Outline minor mode.
225 The value of this variable is checked as part of loading Outline mode.
226 After that, changing the prefix key requires manipulating keymaps.")
227
228 (defvar outline-minor-mode-map nil)
229 (if outline-minor-mode-map
230 nil
231 (setq outline-minor-mode-map (make-sparse-keymap))
232 (define-key outline-minor-mode-map [menu-bar]
233 outline-mode-menu-bar-map)
234 (define-key outline-minor-mode-map outline-minor-mode-prefix
235 outline-mode-prefix-map))
236
237 (or (assq 'outline-minor-mode minor-mode-map-alist)
238 (setq minor-mode-map-alist
239 (cons (cons 'outline-minor-mode outline-minor-mode-map)
240 minor-mode-map-alist)))
241
242 ;;;autoload
243 (defun outline-minor-mode (&optional arg)
244 "Toggle Outline minor mode.
245 With arg, turn Outline minor mode on if arg is positive, off otherwise.
246 See the command `outline-mode' for more information on this mode."
247 (interactive "P")
248 (setq outline-minor-mode
249 (if (null arg) (not outline-minor-mode)
250 (> (prefix-numeric-value arg) 0)))
251 (if outline-minor-mode
252 (progn
253 (make-local-hook 'change-major-mode-hook)
254 ;; Turn off this mode if we change major modes.
255 (add-hook 'change-major-mode-hook
256 '(lambda () (outline-minor-mode -1))
257 nil t)
258 (make-local-variable 'line-move-ignore-invisible)
259 (setq line-move-ignore-invisible t)
260 ;; Cause use of ellipses for invisible text.
261 (setq buffer-invisibility-spec '((t . t)))
262 (run-hooks 'outline-minor-mode-hook))
263 (setq line-move-ignore-invisible nil)
264 ;; Cause use of ellipses for invisible text.
265 (setq buffer-invisibility-spec t))
266 ;; When turning off outline mode, get rid of any outline hiding.
267 (or outline-minor-mode
268 (show-all))
269 (force-mode-line-update))
270 \f
271 (defvar outline-level 'outline-level
272 "Function of no args to compute a header's nesting level in an outline.
273 It can assume point is at the beginning of a header line.")
274
275 ;; This used to count columns rather than characters, but that made ^L
276 ;; appear to be at level 2 instead of 1. Columns would be better for
277 ;; tab handling, but the default regexp doesn't use tabs, and anyone
278 ;; who changes the regexp can also redefine the outline-level variable
279 ;; as appropriate.
280 (defun outline-level ()
281 "Return the depth to which a statement is nested in the outline.
282 Point must be at the beginning of a header line. This is actually
283 the number of characters that `outline-regexp' matches."
284 (save-excursion
285 (looking-at outline-regexp)
286 (- (match-end 0) (match-beginning 0))))
287
288 (defun outline-next-preface ()
289 "Skip forward to just before the next heading line.
290 If there's no following heading line, stop before the newline
291 at the end of the buffer."
292 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
293 nil 'move)
294 (goto-char (match-beginning 0)))
295 (if (bolp)
296 (forward-char -1)))
297
298 (defun outline-next-heading ()
299 "Move to the next (possibly invisible) heading line."
300 (interactive)
301 (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
302 nil 'move)
303 (goto-char (1+ (match-beginning 0)))))
304
305 (defsubst outline-visible ()
306 "Non-nil if the character after point is visible."
307 (not (get-char-property (point) 'invisible)))
308
309 (defun outline-back-to-heading ()
310 "Move to previous heading line, or beg of this line if it's a heading.
311 Only visible heading lines are considered."
312 (beginning-of-line)
313 (or (outline-on-heading-p)
314 (let (found)
315 (save-excursion
316 (while (not found)
317 (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
318 nil t)
319 (error "before first heading"))
320 (setq found (and (outline-visible) (point)))))
321 (goto-char found)
322 found)))
323
324 (defun outline-on-heading-p ()
325 "Return t if point is on a (visible) heading line."
326 (save-excursion
327 (beginning-of-line)
328 (and (bolp) (outline-visible)
329 (looking-at outline-regexp))))
330
331 (defun outline-end-of-heading ()
332 (if (re-search-forward outline-heading-end-regexp nil 'move)
333 (forward-char -1)))
334
335 (defun outline-next-visible-heading (arg)
336 "Move to the next visible heading line.
337 With argument, repeats or can move backward if negative.
338 A heading line is one that starts with a `*' (or that
339 `outline-regexp' matches)."
340 (interactive "p")
341 (if (< arg 0)
342 (beginning-of-line)
343 (end-of-line))
344 (while (and (not (bobp)) (< arg 0))
345 (while (and (not (bobp))
346 (re-search-backward (concat "^\\(" outline-regexp "\\)")
347 nil 'move)
348 (not (outline-visible))))
349 (setq arg (1+ arg)))
350 (while (and (not (eobp)) (> arg 0))
351 (while (and (not (eobp))
352 (re-search-forward (concat "^\\(" outline-regexp "\\)")
353 nil 'move)
354 (not (outline-visible))))
355 (setq arg (1- arg)))
356 (beginning-of-line))
357
358 (defun outline-previous-visible-heading (arg)
359 "Move to the previous heading line.
360 With argument, repeats or can move forward if negative.
361 A heading line is one that starts with a `*' (or that
362 `outline-regexp' matches)."
363 (interactive "p")
364 (outline-next-visible-heading (- arg)))
365
366 (defun outline-flag-region (from to flag)
367 "Hides or shows lines from FROM to TO, according to FLAG.
368 If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
369 (let ((inhibit-read-only t))
370 (save-excursion
371 (goto-char from)
372 (end-of-line)
373 (outline-discard-overlays (point) to 'outline)
374 (if flag
375 (let ((o (make-overlay (point) to)))
376 (overlay-put o 'invisible flag)
377 (overlay-put o 'outline t)))))
378 (run-hooks 'outline-view-change-hook))
379
380 ;; Exclude from the region BEG ... END all overlays
381 ;; with a non-nil PROP property.
382 ;; Exclude them by shrinking them to exclude BEG ... END,
383 ;; or even by splitting them if necessary.
384 ;; Overlays without a non-nil PROP property are not touched.
385 (defun outline-discard-overlays (beg end prop)
386 (if (< end beg)
387 (setq beg (prog1 end (setq end beg))))
388 (save-excursion
389 (let ((overlays (overlays-in beg end)))
390 (while overlays
391 (let ((o (car overlays)))
392 (if (overlay-get o prop)
393 ;; Either push this overlay outside beg...end
394 ;; or split it to exclude beg...end
395 ;; or delete it entirely (if it is contained in beg...end).
396 (if (< (overlay-start o) beg)
397 (if (> (overlay-end o) end)
398 (let ((o1 (outline-copy-overlay o)))
399 (move-overlay o1 (overlay-start o1) beg)
400 (move-overlay o (overlay-start o) beg)))
401 (if (> (overlay-end o) end)
402 (move-overlay o end (overlay-end o))
403 (delete-overlay o)))))
404 (setq overlays (cdr overlays))))))
405
406 ;; Make a copy of overlay O, with the same beginning, end and properties.
407 (defun outline-copy-overlay (o)
408 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
409 (overlay-buffer o)))
410 (props (overlay-properties o)))
411 (while props
412 (overlay-put o1 (car props) (nth 1 props))
413 (setq props (cdr (cdr props))))
414 o1))
415 \f
416 (defun hide-entry ()
417 "Hide the body directly following this heading."
418 (interactive)
419 (outline-back-to-heading)
420 (outline-end-of-heading)
421 (save-excursion
422 (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))
423
424 (defun show-entry ()
425 "Show the body directly following this heading."
426 (interactive)
427 (save-excursion
428 (outline-flag-region (point) (progn (outline-next-preface) (point)) nil)))
429
430 (defun hide-body ()
431 "Hide all of buffer except headings."
432 (interactive)
433 (hide-region-body (point-min) (point-max)))
434
435 (defun hide-region-body (start end)
436 "Hide all body lines in the region, but not headings."
437 (save-excursion
438 (save-restriction
439 (narrow-to-region start end)
440 (goto-char (point-min))
441 (if (outline-on-heading-p)
442 (outline-end-of-heading))
443 (while (not (eobp))
444 (outline-flag-region (point)
445 (progn (outline-next-preface) (point)) t)
446 (if (not (eobp))
447 (progn
448 (forward-char
449 (if (looking-at "\n\n")
450 2 1))
451 (outline-end-of-heading)))))))
452
453 (defun show-all ()
454 "Show all of the text in the buffer."
455 (interactive)
456 (outline-flag-region (point-min) (point-max) nil))
457
458 (defun hide-subtree ()
459 "Hide everything after this heading at deeper levels."
460 (interactive)
461 (outline-flag-subtree t))
462
463 (defun hide-leaves ()
464 "Hide all body after this heading at deeper levels."
465 (interactive)
466 (outline-back-to-heading)
467 (outline-end-of-heading)
468 (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
469
470 (defun show-subtree ()
471 "Show everything after this heading at deeper levels."
472 (interactive)
473 (outline-flag-subtree nil))
474
475 (defun hide-sublevels (levels)
476 "Hide everything but the top LEVELS levels of headers, in whole buffer."
477 (interactive "p")
478 (if (< levels 1)
479 (error "Must keep at least one level of headers"))
480 (setq levels (1- levels))
481 (save-excursion
482 (goto-char (point-min))
483 ;; Keep advancing to the next top-level heading.
484 (while (or (and (bobp) (outline-on-heading-p))
485 (outline-next-heading))
486 (let ((end (save-excursion (outline-end-of-subtree) (point))))
487 ;; Hide everything under that.
488 (outline-flag-region (point) end t)
489 ;; Show the first LEVELS levels under that.
490 (if (> levels 0)
491 (show-children levels))
492 ;; Move to the next, since we already found it.
493 (goto-char end)))))
494
495 (defun hide-other ()
496 "Hide everything except for the current body and the parent headings."
497 (interactive)
498 (hide-sublevels 1)
499 (let ((last (point))
500 (pos (point)))
501 (while (save-excursion
502 (and (end-of-line 0)
503 (not (outline-visible))))
504 (save-excursion
505 (beginning-of-line)
506 (if (eq last (point))
507 (progn
508 (outline-next-heading)
509 (outline-flag-region last (point) nil))
510 (show-children)
511 (setq last (point)))))))
512
513 (defun outline-flag-subtree (flag)
514 (save-excursion
515 (outline-back-to-heading)
516 (outline-end-of-heading)
517 (outline-flag-region (point)
518 (progn (outline-end-of-subtree) (point))
519 flag)))
520
521 (defun outline-end-of-subtree ()
522 (outline-back-to-heading)
523 (let ((opoint (point))
524 (first t)
525 (level (funcall outline-level)))
526 (while (and (not (eobp))
527 (or first (> (funcall outline-level) level)))
528 (setq first nil)
529 (outline-next-heading))
530 (if (bolp)
531 (progn
532 ;; Go to end of line before heading
533 (forward-char -1)
534 (if (bolp)
535 ;; leave blank line before heading
536 (forward-char -1))))))
537 \f
538 (defun show-branches ()
539 "Show all subheadings of this heading, but not their bodies."
540 (interactive)
541 (show-children 1000))
542
543 (defun show-children (&optional level)
544 "Show all direct subheadings of this heading.
545 Prefix arg LEVEL is how many levels below the current level should be shown.
546 Default is enough to cause the following heading to appear."
547 (interactive "P")
548 (setq level
549 (if level (prefix-numeric-value level)
550 (save-excursion
551 (outline-back-to-heading)
552 (let ((start-level (funcall outline-level)))
553 (outline-next-heading)
554 (if (eobp)
555 1
556 (max 1 (- (funcall outline-level) start-level)))))))
557 (save-excursion
558 (save-restriction
559 (outline-back-to-heading)
560 (setq level (+ level (funcall outline-level)))
561 (narrow-to-region (point)
562 (progn (outline-end-of-subtree)
563 (if (eobp) (point-max) (1+ (point)))))
564 (goto-char (point-min))
565 (while (and (not (eobp))
566 (progn
567 (outline-next-heading)
568 (not (eobp))))
569 (if (<= (funcall outline-level) level)
570 (save-excursion
571 (outline-flag-region (save-excursion
572 (forward-char -1)
573 (if (bolp)
574 (forward-char -1))
575 (point))
576 (progn (outline-end-of-heading) (point))
577 nil)))))))
578 \f
579 (defun outline-up-heading (arg)
580 "Move to the heading line of which the present line is a subheading.
581 With argument, move up ARG levels."
582 (interactive "p")
583 (outline-back-to-heading)
584 (if (eq (funcall outline-level) 1)
585 (error "Already at top level of the outline"))
586 (while (and (> (funcall outline-level) 1)
587 (> arg 0)
588 (not (bobp)))
589 (let ((present-level (funcall outline-level)))
590 (while (not (< (funcall outline-level) present-level))
591 (outline-previous-visible-heading 1))
592 (setq arg (- arg 1)))))
593
594 (defun outline-forward-same-level (arg)
595 "Move forward to the ARG'th subheading at same level as this one.
596 Stop at the first and last subheadings of a superior heading."
597 (interactive "p")
598 (outline-back-to-heading)
599 (while (> arg 0)
600 (let ((point-to-move-to (save-excursion
601 (outline-get-next-sibling))))
602 (if point-to-move-to
603 (progn
604 (goto-char point-to-move-to)
605 (setq arg (1- arg)))
606 (progn
607 (setq arg 0)
608 (error "No following same-level heading"))))))
609
610 (defun outline-get-next-sibling ()
611 "Move to next heading of the same level, and return point or nil if none."
612 (let ((level (funcall outline-level)))
613 (outline-next-visible-heading 1)
614 (while (and (> (funcall outline-level) level)
615 (not (eobp)))
616 (outline-next-visible-heading 1))
617 (if (< (funcall outline-level) level)
618 nil
619 (point))))
620
621 (defun outline-backward-same-level (arg)
622 "Move backward to the ARG'th subheading at same level as this one.
623 Stop at the first and last subheadings of a superior heading."
624 (interactive "p")
625 (outline-back-to-heading)
626 (while (> arg 0)
627 (let ((point-to-move-to (save-excursion
628 (outline-get-last-sibling))))
629 (if point-to-move-to
630 (progn
631 (goto-char point-to-move-to)
632 (setq arg (1- arg)))
633 (progn
634 (setq arg 0)
635 (error "No previous same-level heading"))))))
636
637 (defun outline-get-last-sibling ()
638 "Move to next heading of the same level, and return point or nil if none."
639 (let ((level (funcall outline-level)))
640 (outline-previous-visible-heading 1)
641 (while (and (> (funcall outline-level) level)
642 (not (bobp)))
643 (outline-previous-visible-heading 1))
644 (if (< (funcall outline-level) level)
645 nil
646 (point))))
647
648 (provide 'outline)
649 (provide 'noutline)
650
651 ;;; outline.el ends here