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