home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / outline.el < prev    next >
Encoding:
Text File  |  1994-05-09  |  19.2 KB  |  538 lines

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