outline.el 26.2 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; outline.el --- outline mode commands for Emacs
Erik Naggum's avatar
Erik Naggum committed
2

3
;; Copyright (C) 1986, 93, 94, 95, 97, 2000 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5

;; Maintainer: FSF
Richard M. Stallman's avatar
Richard M. Stallman committed
6
;; Keywords: outlines
Richard M. Stallman's avatar
Richard M. Stallman committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26 27 28 29 30

;;; Commentary:

;; This package is a major mode for editing outline-format documents.
;; An outline can be `abstracted' to show headers at any given level,
;; with all stuff below hidden.  See the Emacs manual for details.

31 32 33 34
;;; Todo:

;; - subtree-terminators

Richard M. Stallman's avatar
Richard M. Stallman committed
35 36
;;; Code:

37 38 39 40 41 42
(defgroup outlines nil
  "Support for hierarchical outlining"
  :prefix "outline-"
  :group 'editing)

(defcustom outline-regexp nil
Richard M. Stallman's avatar
Richard M. Stallman committed
43 44 45
  "*Regular expression to match the beginning of a heading.
Any line whose beginning matches this regexp is considered to start a heading.
The recommended way to set this is with a Local Variables: list
Karl Heuer's avatar
Karl Heuer committed
46
in the file it applies to.  See also `outline-heading-end-regexp'."
47 48
  :type '(choice regexp (const nil))
  :group 'outlines)
Richard M. Stallman's avatar
Richard M. Stallman committed
49 50 51 52 53

;; Can't initialize this in the defvar above -- some major modes have
;; already assigned a local value to it.
(or (default-value 'outline-regexp)
    (setq-default outline-regexp "[*\^L]+"))
54

55
(defcustom outline-heading-end-regexp "\n"
Richard M. Stallman's avatar
Richard M. Stallman committed
56 57 58 59
  "*Regular expression to match the end of a heading line.
You can assume that point is at the beginning of a heading when this
regexp is searched for.  The heading ends at the end of the match.
The recommended way to set this is with a `Local Variables:' list
60 61 62
in the file it applies to."
  :type 'regexp
  :group 'outlines)
Richard M. Stallman's avatar
Richard M. Stallman committed
63 64 65 66 67 68

(defvar outline-mode-prefix-map nil)

(if outline-mode-prefix-map
    nil
  (setq outline-mode-prefix-map (make-sparse-keymap))
69
  (define-key outline-mode-prefix-map "@" 'outline-mark-subtree)
Richard M. Stallman's avatar
Richard M. Stallman committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  (define-key outline-mode-prefix-map "\C-n" 'outline-next-visible-heading)
  (define-key outline-mode-prefix-map "\C-p" 'outline-previous-visible-heading)
  (define-key outline-mode-prefix-map "\C-i" 'show-children)
  (define-key outline-mode-prefix-map "\C-s" 'show-subtree)
  (define-key outline-mode-prefix-map "\C-d" 'hide-subtree)
  (define-key outline-mode-prefix-map "\C-u" 'outline-up-heading)
  (define-key outline-mode-prefix-map "\C-f" 'outline-forward-same-level)
  (define-key outline-mode-prefix-map "\C-b" 'outline-backward-same-level)
  (define-key outline-mode-prefix-map "\C-t" 'hide-body)
  (define-key outline-mode-prefix-map "\C-a" 'show-all)
  (define-key outline-mode-prefix-map "\C-c" 'hide-entry)
  (define-key outline-mode-prefix-map "\C-e" 'show-entry)
  (define-key outline-mode-prefix-map "\C-l" 'hide-leaves)
  (define-key outline-mode-prefix-map "\C-k" 'show-branches)
  (define-key outline-mode-prefix-map "\C-q" 'hide-sublevels)
  (define-key outline-mode-prefix-map "\C-o" 'hide-other))

(defvar outline-mode-menu-bar-map nil)
(if outline-mode-menu-bar-map
    nil
  (setq outline-mode-menu-bar-map (make-sparse-keymap))

  (define-key outline-mode-menu-bar-map [hide]
    (cons "Hide" (make-sparse-keymap "Hide")))

  (define-key outline-mode-menu-bar-map [hide hide-other]
    '("Hide Other" . hide-other))
  (define-key outline-mode-menu-bar-map [hide hide-sublevels]
    '("Hide Sublevels" . hide-sublevels))
  (define-key outline-mode-menu-bar-map [hide hide-subtree]
    '("Hide Subtree" . hide-subtree))
  (define-key outline-mode-menu-bar-map [hide hide-entry]
    '("Hide Entry" . hide-entry))
  (define-key outline-mode-menu-bar-map [hide hide-body]
    '("Hide Body" . hide-body))
  (define-key outline-mode-menu-bar-map [hide hide-leaves]
    '("Hide Leaves" . hide-leaves))

  (define-key outline-mode-menu-bar-map [show]
    (cons "Show" (make-sparse-keymap "Show")))

  (define-key outline-mode-menu-bar-map [show show-subtree]
    '("Show Subtree" . show-subtree))
  (define-key outline-mode-menu-bar-map [show show-children]
    '("Show Children" . show-children))
  (define-key outline-mode-menu-bar-map [show show-branches]
    '("Show Branches" . show-branches))
  (define-key outline-mode-menu-bar-map [show show-entry]
    '("Show Entry" . show-entry))
  (define-key outline-mode-menu-bar-map [show show-all]
    '("Show All" . show-all))

  (define-key outline-mode-menu-bar-map [headings]
    (cons "Headings" (make-sparse-keymap "Headings")))

125 126 127
  (define-key outline-mode-menu-bar-map [headings copy]
    '(menu-item "Copy to kill ring" outline-headers-as-kill
		:enable mark-active))
Richard M. Stallman's avatar
Richard M. Stallman committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  (define-key outline-mode-menu-bar-map [headings outline-backward-same-level]
    '("Previous Same Level" . outline-backward-same-level))
  (define-key outline-mode-menu-bar-map [headings outline-forward-same-level]
    '("Next Same Level" . outline-forward-same-level))
  (define-key outline-mode-menu-bar-map [headings outline-previous-visible-heading]
    '("Previous" . outline-previous-visible-heading))
  (define-key outline-mode-menu-bar-map [headings outline-next-visible-heading]
    '("Next" . outline-next-visible-heading))
  (define-key outline-mode-menu-bar-map [headings outline-up-heading]
    '("Up" . outline-up-heading)))

(defvar outline-mode-map nil "")

(if outline-mode-map
    nil
  (setq outline-mode-map (nconc (make-sparse-keymap) text-mode-map))
  (define-key outline-mode-map "\C-c" outline-mode-prefix-map)
  (define-key outline-mode-map [menu-bar] outline-mode-menu-bar-map))

(defvar outline-font-lock-keywords
148 149 150 151 152 153 154 155 156
  '(;;
    ;; Highlight headings according to the level.
    (eval . (list (concat "^" outline-regexp ".+")
		  0 '(or (cdr (assq (outline-font-lock-level)
				    '((1 . font-lock-function-name-face)
				      (2 . font-lock-variable-name-face)
				      (3 . font-lock-keyword-face)
				      (4 . font-lock-builtin-face)
				      (5 . font-lock-comment-face)
157
				      (6 . font-lock-constant-face)
158 159 160 161
				      (7 . font-lock-type-face)
				      (8 . font-lock-string-face))))
			 font-lock-warning-face)
		  nil t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
162 163
  "Additional expressions to highlight in Outline mode.")

164 165 166
(defun outline-font-lock-level ()
  (let ((count 1))
    (save-excursion
167
      (outline-back-to-heading t)
168 169
      (condition-case nil
	  (while (not (bobp))
170
	    (outline-up-heading-all 1)
171 172 173 174
	    (setq count (1+ count)))
	(error)))
    count))

175 176 177
(defvar outline-view-change-hook nil
  "Normal hook to be run after outline visibility changes.")

178
;;;###autoload
179
(define-derived-mode outline-mode text-mode "Outline"
Richard M. Stallman's avatar
Richard M. Stallman committed
180 181
  "Set major mode for editing outlines with selective display.
Headings are lines which start with asterisks: one for major headings,
182
two for subheadings, etc.  Lines not starting with asterisks are body lines.
Richard M. Stallman's avatar
Richard M. Stallman committed
183 184

Body text or subheadings under a heading can be made temporarily
185
invisible, or visible again.  Invisible lines are attached to the end
Richard M. Stallman's avatar
Richard M. Stallman committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
of the heading, so they move with it, if the line is killed and yanked
back.  A heading with text hidden under it is marked with an ellipsis (...).

Commands:\\<outline-mode-map>
\\[outline-next-visible-heading]   outline-next-visible-heading      move by visible headings
\\[outline-previous-visible-heading]   outline-previous-visible-heading
\\[outline-forward-same-level]   outline-forward-same-level        similar but skip subheadings
\\[outline-backward-same-level]   outline-backward-same-level
\\[outline-up-heading]   outline-up-heading		    move from subheading to heading

\\[hide-body]	make all text invisible (not headings).
\\[show-all]	make everything in buffer visible.

The remaining commands are used when point is on a heading line.
They apply to some of the body or subheadings of that heading.
\\[hide-subtree]   hide-subtree	make body and subheadings invisible.
\\[show-subtree]   show-subtree	make body and subheadings visible.
\\[show-children]   show-children	make direct subheadings visible.
		 No effect on body, or subheadings 2 or more levels down.
		 With arg N, affects subheadings N levels down.
\\[hide-entry]	   make immediately following body invisible.
\\[show-entry]	   make it visible.
\\[hide-leaves]	   make body under heading and under its subheadings invisible.
		     The subheadings remain visible.
\\[show-branches]  make all subheadings at all levels visible.

The variable `outline-regexp' can be changed to control what is a heading.
A line is a heading if `outline-regexp' matches something at the
beginning of the line.  The longer the match, the deeper the level.

Turning on outline mode calls the value of `text-mode-hook' and then of
`outline-mode-hook', if they are non-nil."
  (make-local-variable 'line-move-ignore-invisible)
  (setq line-move-ignore-invisible t)
  ;; Cause use of ellipses for invisible text.
221
  (add-to-invisibility-spec '(outline . t))
222 223
  (set (make-local-variable 'paragraph-start)
       (concat paragraph-start "\\|\\(" outline-regexp "\\)"))
Richard M. Stallman's avatar
Richard M. Stallman committed
224
  ;; Inhibit auto-filling of header lines.
225 226 227 228 229
  (set (make-local-variable 'auto-fill-inhibit-regexp) outline-regexp)
  (set (make-local-variable 'paragraph-separate)
       (concat paragraph-separate "\\|\\(" outline-regexp "\\)"))
  (set (make-local-variable 'font-lock-defaults)
       '(outline-font-lock-keywords t))
230 231
  (setq imenu-generic-expression
	(list (list nil (concat outline-regexp ".*$") 0)))
232
  (add-hook 'change-major-mode-hook 'show-all nil t))
Richard M. Stallman's avatar
Richard M. Stallman committed
233

234
(defcustom outline-minor-mode-prefix "\C-c@"
Richard M. Stallman's avatar
Richard M. Stallman committed
235 236
  "*Prefix key to use for Outline commands in Outline minor mode.
The value of this variable is checked as part of loading Outline mode.
237 238 239
After that, changing the prefix key requires manipulating keymaps."
  :type 'string
  :group 'outlines)
Richard M. Stallman's avatar
Richard M. Stallman committed
240

241
;;;###autoload
242
(define-minor-mode outline-minor-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
243 244 245
  "Toggle Outline minor mode.
With arg, turn Outline minor mode on if arg is positive, off otherwise.
See the command `outline-mode' for more information on this mode."
246 247
  nil " Outl" (list (cons [menu-bar] outline-mode-menu-bar-map)
		    (cons outline-minor-mode-prefix outline-mode-prefix-map))
Richard M. Stallman's avatar
Richard M. Stallman committed
248 249
  (if outline-minor-mode
      (progn
250 251
	;; Turn off this mode if we change major modes.
	(add-hook 'change-major-mode-hook
252
		  (lambda () (outline-minor-mode -1))
253
		  nil t)
254
	(set (make-local-variable 'line-move-ignore-invisible) t)
Richard M. Stallman's avatar
Richard M. Stallman committed
255
	;; Cause use of ellipses for invisible text.
256
	(add-to-invisibility-spec '(outline . t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
257 258
    (setq line-move-ignore-invisible nil)
    ;; Cause use of ellipses for invisible text.
259
    (remove-from-invisibility-spec '(outline . t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
260
  ;; When turning off outline mode, get rid of any outline hiding.
Richard M. Stallman's avatar
Richard M. Stallman committed
261
  (or outline-minor-mode
262
      (show-all)))
Richard M. Stallman's avatar
Richard M. Stallman committed
263

264 265 266 267
(defcustom outline-level 'outline-level
  "*Function of no args to compute a header's nesting level in an outline.
It can assume point is at the beginning of a header line."
  :type 'function
268
  :group 'outlines)
Richard M. Stallman's avatar
Richard M. Stallman committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

;; This used to count columns rather than characters, but that made ^L
;; appear to be at level 2 instead of 1.  Columns would be better for
;; tab handling, but the default regexp doesn't use tabs, and anyone
;; who changes the regexp can also redefine the outline-level variable
;; as appropriate.
(defun outline-level ()
  "Return the depth to which a statement is nested in the outline.
Point must be at the beginning of a header line.  This is actually
the number of characters that `outline-regexp' matches."
  (save-excursion
    (looking-at outline-regexp)
    (- (match-end 0) (match-beginning 0))))

(defun outline-next-preface ()
  "Skip forward to just before the next heading line.
If there's no following heading line, stop before the newline
at the end of the buffer."
  (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
			 nil 'move)
      (goto-char (match-beginning 0)))
Karl Heuer's avatar
Karl Heuer committed
290
  (if (and (bolp) (not (bobp)))
Richard M. Stallman's avatar
Richard M. Stallman committed
291 292 293 294 295
      (forward-char -1)))

(defun outline-next-heading ()
  "Move to the next (possibly invisible) heading line."
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
296
  (if (re-search-forward (concat "\n\\(" outline-regexp "\\)")
Richard M. Stallman's avatar
Richard M. Stallman committed
297 298 299
			 nil 'move)
      (goto-char (1+ (match-beginning 0)))))

300 301 302 303 304 305
(defun outline-previous-heading ()
  "Move to the previous (possibly invisible) heading line."
  (interactive)
  (re-search-backward (concat "^\\(" outline-regexp "\\)")
		      nil 'move))

Richard M. Stallman's avatar
Richard M. Stallman committed
306 307 308 309
(defsubst outline-visible ()
  "Non-nil if the character after point is visible."
  (not (get-char-property (point) 'invisible)))

310
(defun outline-back-to-heading (&optional invisible-ok)
Richard M. Stallman's avatar
Richard M. Stallman committed
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."
Richard M. Stallman's avatar
Richard M. Stallman committed
313
  (beginning-of-line)
314
  (or (outline-on-heading-p invisible-ok)
Richard M. Stallman's avatar
Richard M. Stallman committed
315
      (let (found)
316 317 318 319 320
	(save-excursion
	  (while (not found)
	    (or (re-search-backward (concat "^\\(" outline-regexp "\\)")
				    nil t)
		(error "before first heading"))
321
	    (setq found (and (or invisible-ok (outline-visible)) (point)))))
322 323
	(goto-char found)
	found)))
Richard M. Stallman's avatar
Richard M. Stallman committed
324

325 326 327
(defun outline-on-heading-p (&optional invisible-ok)
  "Return t if point is on a (visible) heading line.
If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
Richard M. Stallman's avatar
Richard M. Stallman committed
328 329
  (save-excursion
    (beginning-of-line)
330
    (and (bolp) (or invisible-ok (outline-visible))
Richard M. Stallman's avatar
Richard M. Stallman committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	 (looking-at outline-regexp))))

(defun outline-end-of-heading ()
  (if (re-search-forward outline-heading-end-regexp nil 'move)
      (forward-char -1)))

(defun outline-next-visible-heading (arg)
  "Move to the next visible heading line.
With argument, repeats or can move backward if negative.
A heading line is one that starts with a `*' (or that
`outline-regexp' matches)."
  (interactive "p")
  (if (< arg 0)
      (beginning-of-line)
    (end-of-line))
346 347 348 349 350 351 352 353 354 355 356 357
  (while (and (not (bobp)) (< arg 0))
    (while (and (not (bobp))
		(re-search-backward (concat "^\\(" outline-regexp "\\)")
				    nil 'move)
		(not (outline-visible))))
    (setq arg (1+ arg)))
  (while (and (not (eobp)) (> arg 0))
    (while (and (not (eobp))
		(re-search-forward (concat "^\\(" outline-regexp "\\)")
				   nil 'move)
		(not (outline-visible))))
    (setq arg (1- arg)))
Richard M. Stallman's avatar
Richard M. Stallman committed
358 359 360 361 362 363 364 365 366 367
  (beginning-of-line))

(defun outline-previous-visible-heading (arg)
  "Move to the previous heading line.
With argument, repeats or can move forward if negative.
A heading line is one that starts with a `*' (or that
`outline-regexp' matches)."
  (interactive "p")
  (outline-next-visible-heading (- arg)))

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
(defun outline-mark-subtree ()
  "Mark the current subtree in an outlined document.
This puts point at the start of the current subtree, and mark at the end."
  (interactive)
  (let ((beg))
    (if (outline-on-heading-p)
	;; we are already looking at a heading
	(beginning-of-line)
      ;; else go back to previous heading
      (outline-previous-visible-heading 1))
    (setq beg (point))
    (outline-end-of-subtree)
    (push-mark (point))
    (goto-char beg)))

Richard M. Stallman's avatar
Richard M. Stallman committed
383 384 385 386 387 388 389
(defun outline-flag-region (from to flag)
  "Hides or shows lines from FROM to TO, according to FLAG.
If FLAG is nil then text is shown, while if FLAG is t the text is hidden."
  (let ((inhibit-read-only t))
    (save-excursion
      (goto-char from)
      (end-of-line)
390 391
      (outline-discard-overlays (point) to 'outline)
      (if flag
Dan Nicolaescu's avatar
Dan Nicolaescu committed
392 393 394 395
          (let ((o (make-overlay (point) to)))
            (overlay-put o 'invisible 'outline)
	    (overlay-put o 'isearch-open-invisible
			 'outline-isearch-open-invisible)))))
396
  (run-hooks 'outline-view-change-hook))
397

Dan Nicolaescu's avatar
Dan Nicolaescu committed
398 399 400 401 402 403 404 405 406 407

;; Function to be set as an outline-isearch-open-invisible' property
;; to the overlay that makes the outline invisible (see
;; `outline-flag-region').
(defun outline-isearch-open-invisible (overlay)
  (save-excursion
    (goto-char (overlay-start overlay))
    (show-entry)))


Richard M. Stallman's avatar
Richard M. Stallman committed
408
;; Exclude from the region BEG ... END all overlays
409
;; which have PROP as the value of the `invisible' property.
Richard M. Stallman's avatar
Richard M. Stallman committed
410 411
;; Exclude them by shrinking them to exclude BEG ... END,
;; or even by splitting them if necessary.
412
;; Overlays without such an `invisible' property are not touched.
413 414 415 416
(defun outline-discard-overlays (beg end prop)
  (if (< end beg)
      (setq beg (prog1 end (setq end beg))))
  (save-excursion
417 418 419
    (let ((overlays (overlays-in beg end))
	  o
	  o1)
420
      (while overlays
421
	(setq o (car overlays))
422
	(if (eq (overlay-get o 'invisible) prop)
423 424 425 426
	    ;; Either push this overlay outside beg...end
	    ;; or split it to exclude beg...end
	    ;; or delete it entirely (if it is contained in beg...end).
	    (if (< (overlay-start o) beg)
427
		(if (> (overlay-end o) end)
428
		    (progn
429 430 431 432 433 434 435
		      (setq o1 (outline-copy-overlay o))
		      (move-overlay o1 (overlay-start o1) beg)
		      (move-overlay o end (overlay-end o)))
		  (move-overlay o (overlay-start o) beg))
	      (if (> (overlay-end o) end)
		  (move-overlay o end (overlay-end o))
		(delete-overlay o))))
436
	(setq overlays (cdr overlays))))))
437

Richard M. Stallman's avatar
Richard M. Stallman committed
438
;; Make a copy of overlay O, with the same beginning, end and properties.
439
(defun outline-copy-overlay (o)
Richard M. Stallman's avatar
Richard M. Stallman committed
440 441
  (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
			  (overlay-buffer o)))
442 443 444 445 446
	(props (overlay-properties o)))
    (while props
      (overlay-put o1 (car props) (nth 1 props))
      (setq props (cdr (cdr props))))
    o1))
Richard M. Stallman's avatar
Richard M. Stallman committed
447 448 449 450 451 452 453 454 455 456

(defun hide-entry ()
  "Hide the body directly following this heading."
  (interactive)
  (outline-back-to-heading)
  (outline-end-of-heading)
  (save-excursion
   (outline-flag-region (point) (progn (outline-next-preface) (point)) t)))

(defun show-entry ()
457 458
  "Show the body directly following this heading.
Show the heading too, if it is currently invisible."
Richard M. Stallman's avatar
Richard M. Stallman committed
459 460
  (interactive)
  (save-excursion
461 462 463
    (outline-back-to-heading t)
    (outline-flag-region (1- (point))
			 (progn (outline-next-preface) (point)) nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
464 465 466 467 468 469 470 471

(defun hide-body ()
  "Hide all of buffer except headings."
  (interactive)
  (hide-region-body (point-min) (point-max)))

(defun hide-region-body (start end)
  "Hide all body lines in the region, but not headings."
Dave Love's avatar
Dave Love committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  ;; Nullify the hook to avoid repeated calls to `outline-flag-region'
  ;; wasting lots of time running `lazy-lock-fontify-after-outline'
  ;; and run the hook finally.
  (let (outline-view-change-hook)
    (save-excursion
      (save-restriction
	(narrow-to-region start end)
	(goto-char (point-min))
	(if (outline-on-heading-p)
	    (outline-end-of-heading))
	(while (not (eobp))
	  (outline-flag-region (point)
			       (progn (outline-next-preface) (point)) t)
	  (if (not (eobp))
	      (progn
		(forward-char
		 (if (looking-at "\n\n")
		     2 1))
		(outline-end-of-heading)))))))
  (run-hooks 'outline-view-change-hook))
Richard M. Stallman's avatar
Richard M. Stallman committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

(defun show-all ()
  "Show all of the text in the buffer."
  (interactive)
  (outline-flag-region (point-min) (point-max) nil))

(defun hide-subtree ()
  "Hide everything after this heading at deeper levels."
  (interactive)
  (outline-flag-subtree t))

(defun hide-leaves ()
  "Hide all body after this heading at deeper levels."
  (interactive)
  (outline-back-to-heading)
  (outline-end-of-heading)
  (hide-region-body (point) (progn (outline-end-of-subtree) (point))))

(defun show-subtree ()
  "Show everything after this heading at deeper levels."
  (interactive)
  (outline-flag-subtree nil))

(defun hide-sublevels (levels)
  "Hide everything but the top LEVELS levels of headers, in whole buffer."
  (interactive "p")
  (if (< levels 1)
      (error "Must keep at least one level of headers"))
  (setq levels (1- levels))
Dave Love's avatar
Dave Love committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  (let (outline-view-change-hook)
    (save-excursion
      (goto-char (point-min))
      ;; Keep advancing to the next top-level heading.
      (while (or (and (bobp) (outline-on-heading-p))
		 (outline-next-heading))
	(let ((end (save-excursion (outline-end-of-subtree) (point))))
	  ;; Hide everything under that.
	  (outline-flag-region (point) end t)
	  ;; Show the first LEVELS levels under that.
	  (if (> levels 0)
	      (show-children levels))
	  ;; Move to the next, since we already found it.
	  (goto-char end)))))
  (run-hooks 'outline-view-change-hook))
Richard M. Stallman's avatar
Richard M. Stallman committed
536 537

(defun hide-other ()
538
  "Hide everything except current body and parent and top-level headings."
Richard M. Stallman's avatar
Richard M. Stallman committed
539 540
  (interactive)
  (hide-sublevels 1)
Dave Love's avatar
Dave Love committed
541 542 543 544 545 546 547 548 549 550
  (let (outline-view-change-hook)
    (save-excursion
      (outline-back-to-heading t)
      (show-entry)
      (while (condition-case nil (progn (outline-up-heading 1) t)
	       (error nil))
	(outline-flag-region (1- (point))
			     (save-excursion (forward-line 1) (point))
			     nil))))
  (run-hooks 'outline-view-change-hook))
Richard M. Stallman's avatar
Richard M. Stallman committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595

(defun outline-flag-subtree (flag)
  (save-excursion
    (outline-back-to-heading)
    (outline-end-of-heading)
    (outline-flag-region (point)
			  (progn (outline-end-of-subtree) (point))
			  flag)))

(defun outline-end-of-subtree ()
  (outline-back-to-heading)
  (let ((opoint (point))
	(first t)
	(level (funcall outline-level)))
    (while (and (not (eobp))
		(or first (> (funcall outline-level) level)))
      (setq first nil)
      (outline-next-heading))
    (if (bolp)
	(progn
	  ;; Go to end of line before heading
	  (forward-char -1)
	  (if (bolp)
	      ;; leave blank line before heading
	      (forward-char -1))))))

(defun show-branches ()
  "Show all subheadings of this heading, but not their bodies."
  (interactive)
  (show-children 1000))

(defun show-children (&optional level)
  "Show all direct subheadings of this heading.
Prefix arg LEVEL is how many levels below the current level should be shown.
Default is enough to cause the following heading to appear."
  (interactive "P")
  (setq level
	(if level (prefix-numeric-value level)
	  (save-excursion
	    (outline-back-to-heading)
	    (let ((start-level (funcall outline-level)))
	      (outline-next-heading)
	      (if (eobp)
		  1
		(max 1 (- (funcall outline-level) start-level)))))))
Dave Love's avatar
Dave Love committed
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  (let (outline-view-change-hook)
    (save-excursion
      (save-restriction
	(outline-back-to-heading)
	(setq level (+ level (funcall outline-level)))
	(narrow-to-region (point)
			  (progn (outline-end-of-subtree)
				 (if (eobp) (point-max) (1+ (point)))))
	(goto-char (point-min))
	(while (and (not (eobp))
		    (progn
		      (outline-next-heading)
		      (not (eobp))))
	  (if (<= (funcall outline-level) level)
	      (save-excursion
		(outline-flag-region (save-excursion
				       (forward-char -1)
				       (if (bolp)
					   (forward-char -1))
				       (point))
				     (progn (outline-end-of-heading) (point))
				     nil)))))))
  (run-hooks 'outline-view-change-hook))
Richard M. Stallman's avatar
Richard M. Stallman committed
619

620
(defun outline-up-heading-all (arg)
621
  "Move to the heading line of which the present line is a subheading.
622 623 624 625 626 627 628 629 630 631 632
This function considers both visible and invisible heading lines.
With argument, move up ARG levels."
  (outline-back-to-heading t)
  (if (eq (funcall outline-level) 1)
      (error "Already at top level of the outline"))
  (while (and (> (funcall outline-level) 1)
	      (> arg 0)
	      (not (bobp)))
    (let ((present-level (funcall outline-level)))
      (while (and (not (< (funcall outline-level) present-level))
		  (not (bobp)))
633
	(outline-previous-heading))
634 635
      (setq arg (- arg 1)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
636
(defun outline-up-heading (arg)
637
  "Move to the visible heading line of which the present line is a subheading.
Richard M. Stallman's avatar
Richard M. Stallman committed
638 639 640 641
With argument, move up ARG levels."
  (interactive "p")
  (outline-back-to-heading)
  (if (eq (funcall outline-level) 1)
642
      (error "Already at top level of the outline"))
Richard M. Stallman's avatar
Richard M. Stallman committed
643 644 645 646
  (while (and (> (funcall outline-level) 1)
	      (> arg 0)
	      (not (bobp)))
    (let ((present-level (funcall outline-level)))
647 648
      (while (and (not (< (funcall outline-level) present-level))
		  (not (bobp)))
Richard M. Stallman's avatar
Richard M. Stallman committed
649 650 651 652 653 654 655 656 657 658
	(outline-previous-visible-heading 1))
      (setq arg (- arg 1)))))

(defun outline-forward-same-level (arg)
  "Move forward to the ARG'th subheading at same level as this one.
Stop at the first and last subheadings of a superior heading."
  (interactive "p")
  (outline-back-to-heading)
  (while (> arg 0)
    (let ((point-to-move-to (save-excursion
659
			      (outline-get-next-sibling))))
Richard M. Stallman's avatar
Richard M. Stallman committed
660 661 662 663 664 665
      (if point-to-move-to
	  (progn
	    (goto-char point-to-move-to)
	    (setq arg (1- arg)))
	(progn
	  (setq arg 0)
666
	  (error "No following same-level heading"))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
667 668 669 670 671 672 673 674 675 676 677

(defun outline-get-next-sibling ()
  "Move to next heading of the same level, and return point or nil if none."
  (let ((level (funcall outline-level)))
    (outline-next-visible-heading 1)
    (while (and (> (funcall outline-level) level)
		(not (eobp)))
      (outline-next-visible-heading 1))
    (if (< (funcall outline-level) level)
	nil
      (point))))
678

Richard M. Stallman's avatar
Richard M. Stallman committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692
(defun outline-backward-same-level (arg)
  "Move backward to the ARG'th subheading at same level as this one.
Stop at the first and last subheadings of a superior heading."
  (interactive "p")
  (outline-back-to-heading)
  (while (> arg 0)
    (let ((point-to-move-to (save-excursion
			      (outline-get-last-sibling))))
      (if point-to-move-to
	  (progn
	    (goto-char point-to-move-to)
	    (setq arg (1- arg)))
	(progn
	  (setq arg 0)
693
	  (error "No previous same-level heading"))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
694 695

(defun outline-get-last-sibling ()
696
  "Move to previous heading of the same level, and return point or nil if none."
Richard M. Stallman's avatar
Richard M. Stallman committed
697 698 699 700 701 702 703 704
  (let ((level (funcall outline-level)))
    (outline-previous-visible-heading 1)
    (while (and (> (funcall outline-level) level)
		(not (bobp)))
      (outline-previous-visible-heading 1))
    (if (< (funcall outline-level) level)
	nil
        (point))))
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738

(defun outline-headers-as-kill (beg end)
  "Save the visible outline headers in region at the start of the kill ring.

Text shown between the headers isn't copied.  Two newlines are
inserted between saved headers.  Yanking the result may be a
convenient way to make a table of contents of the buffer."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (let ((buffer (current-buffer))
	    start end)
	(with-temp-buffer
	  (with-current-buffer buffer
	    ;; Boundary condition: starting on heading:
	    (when (outline-on-heading-p)
	      (outline-back-to-heading)
	      (setq start (point)
		    end (progn (outline-end-of-heading)
			       (point)))
	      (insert-buffer-substring buffer start end)
	      (insert "\n\n")))
	  (let ((temp-buffer (current-buffer)))
	    (with-current-buffer buffer
	      (while (outline-next-heading)
		(when (outline-visible)
		  (setq start (point)
			end (progn (outline-end-of-heading) (point)))
		  (with-current-buffer temp-buffer
		    (insert-buffer-substring buffer start end)
		    (insert "\n\n"))))))
	  (kill-new (buffer-string)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
739 740

(provide 'outline)
741
(provide 'noutline)
Richard M. Stallman's avatar
Richard M. Stallman committed
742 743

;;; outline.el ends here