home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / outline.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  13.7 KB  |  396 lines

  1. ;; Outline mode commands for Emacs
  2. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. ;; Jan '86, Some new features added by Peter Desnoyers and rewritten by RMS.
  21.   
  22. (defvar outline-regexp "[*\^l]+"
  23.   "*Regular expression to match the beginning of a heading.
  24. Any line whose beginning matches this regexp is considered to start a heading.
  25. The recommended way to set this is with a Local Variables: list
  26. in the file it applies to.  See also outline-heading-end-regexp.")
  27.   
  28. (defvar outline-heading-end-regexp "[\n^M]"
  29.   "*Regular expression to match the end of a heading line.
  30. You can assume that point is at the beginning of a heading
  31. when this regexp is searched for.  The heading ends at the end of the match.
  32. The recommended way to set this is with a Local Variables: list
  33. in the file it applies to.")
  34.  
  35. (defvar outline-mode-map nil "")
  36.  
  37. (if outline-mode-map
  38.     nil
  39.   (setq outline-mode-map (make-sparse-keymap))
  40.   (set-keymap-parent outline-mode-map text-mode-map)
  41.   (define-key outline-mode-map "\C-c\C-n" 'outline-next-visible-heading)
  42.   (define-key outline-mode-map "\C-c\C-p" 'outline-previous-visible-heading)
  43.   (define-key outline-mode-map "\C-c\C-i" 'show-children)
  44.   (define-key outline-mode-map "\C-c\C-s" 'show-subtree)
  45.   (define-key outline-mode-map "\C-c\C-h" 'hide-subtree)
  46.   (define-key outline-mode-map "\C-c\C-u" 'outline-up-heading)
  47.   (define-key outline-mode-map "\C-c\C-f" 'outline-forward-same-level)
  48.   (define-key outline-mode-map "\C-c\C-b" 'outline-backward-same-level))
  49.  
  50. (defvar outline-minor-mode nil
  51.   "Non-nil if using Outline mode as a minor mode of some other mode.")
  52. (setq minor-mode-alist (append minor-mode-alist
  53.                    (list '(outline-minor-mode " Outl"))))
  54.  
  55. (defun outline-mode ()
  56.   "Set major mode for editing outlines with selective display.
  57. Headings are lines which start with asterisks: one for major headings,
  58. two for subheadings, etc.  Lines not starting with asterisks are body lines. 
  59.  
  60. Body text or subheadings under a heading can be made temporarily
  61. invisible, or visible again.  Invisible lines are attached to the end 
  62. of the heading, so they move with it, if the line is killed and yanked
  63. back.  A heading with text hidden under it is marked with an ellipsis (...).
  64.  
  65. Commands:
  66. C-c C-n   outline-next-visible-heading      move by visible headings
  67. C-c C-p   outline-previous-visible-heading
  68. C-c C-f   outline-forward-same-level        similar but skip subheadings
  69. C-c C-b   outline-backward-same-level
  70. C-c C-u   outline-up-heading            move from subheading to heading
  71.  
  72. Meta-x hide-body    make all text invisible (not headings).
  73. Meta-x show-all        make everything in buffer visible.
  74.  
  75. The remaining commands are used when point is on a heading line.
  76. They apply to some of the body or subheadings of that heading.
  77. C-c C-h   hide-subtree    make body and subheadings invisible.
  78. C-c C-s   show-subtree    make body and subheadings visible.
  79. C-c C-i   show-children    make direct subheadings visible.
  80.          No effect on body, or subheadings 2 or more levels down.
  81.          With arg N, affects subheadings N levels down.
  82. M-x hide-entry       make immediately following body invisible.
  83. M-x show-entry       make it visible.
  84. M-x hide-leaves       make body under heading and under its subheadings invisible.
  85.              The subheadings remain visible.
  86. M-x show-branches  make all subheadings at all levels visible.
  87.  
  88. The variable outline-regexp can be changed to control what is a heading.
  89. A line is a heading if outline-regexp matches something at the
  90. beginning of the line.  The longer the match, the deeper the level.
  91.  
  92. Turning on outline mode calls the value of text-mode-hook and then of
  93. outline-mode-hook, if they are non-nil."
  94.   (interactive)
  95.   (kill-all-local-variables)
  96.   (setq selective-display t)
  97.   (use-local-map outline-mode-map)
  98.   (setq mode-name "Outline")
  99.   (setq major-mode 'outline-mode)
  100.   (define-abbrev-table 'text-mode-abbrev-table ())
  101.   (setq local-abbrev-table text-mode-abbrev-table)
  102.   (set-syntax-table text-mode-syntax-table)
  103.   (make-local-variable 'paragraph-start)
  104.   (setq paragraph-start (concat paragraph-start "\\|^\\("
  105.                 outline-regexp "\\)"))
  106.   ;; Inhibit auto-filling of header lines.
  107.   (make-local-variable 'auto-fill-inhibit-regexp)
  108.   (setq auto-fill-inhibit-regexp outline-regexp)
  109.   (make-local-variable 'paragraph-separate)
  110.   (setq paragraph-separate (concat paragraph-separate "\\|^\\("
  111.                    outline-regexp "\\)"))
  112.   (run-hooks 'text-mode-hook 'outline-mode-hook))
  113.  
  114. (defun outline-minor-mode (arg)
  115.   (interactive "P")
  116.   (setq outline-minor-mode
  117.     (if (null arg) (not outline-minor-mode)
  118.       (> (prefix-numeric-value arg) 0)))
  119.   (if outline-minor-mode
  120.       (progn
  121.     (setq selective-display t)
  122.     (make-local-variable 'outline-old-map)
  123.     (setq outline-old-map (current-local-map))
  124.     (let ((new-map (copy-keymap outline-old-map)))
  125.       (define-key new-map "\C-c"
  126.         (lookup-key outline-mode-map "\C-c"))
  127.       (use-local-map new-map))
  128.     (make-local-variable 'outline-regexp)
  129.     (setq outline-regexp "[ \t]*/\\*")
  130.     (make-local-variable 'outline-heading-end-regexp)
  131.     (setq outline-heading-end-regexp "\\*/[^\n\^M]*[\n\^M]")
  132.     (run-hooks 'outline-minor-mode-hook))
  133.     (progn
  134.       (setq selective-display nil)
  135.       (use-local-map outline-old-map))))
  136.  
  137. (defun outline-level ()
  138.   "Return the depth to which a statement is nested in the outline.
  139. Point must be at the beginning of a header line.
  140. This is actually the column-number of the end of what outline-regexp matches."
  141.   (save-excursion
  142.     (looking-at outline-regexp)
  143.     (save-excursion (goto-char (match-end 0)) (current-column))))
  144.  
  145. (defun outline-next-preface ()
  146.   "Skip forward to just before the next heading line."
  147.   (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
  148.              nil 'move)
  149.       (goto-char (match-beginning 0)))
  150.   (if (memq (preceding-char) '(?\n ?\^M))
  151.       (forward-char -1)))
  152.  
  153. (defun outline-next-heading ()
  154.   "Move to the next (possibly invisible) heading line."
  155.   (interactive)
  156.   (if (re-search-forward (concat "[\n\^M]\\(" outline-regexp "\\)")
  157.              nil 'move)
  158.       (goto-char (1+ (match-beginning 0)))))
  159.  
  160. (defun outline-back-to-heading ()
  161.   "Move to previous (possibly invisible) heading line,
  162. or to beginning of this line if it is a heading line."
  163.   (beginning-of-line)
  164.   (or (outline-on-heading-p)
  165.       (re-search-backward (concat "^\\(" outline-regexp "\\)") nil 'move)))
  166.  
  167. (defun outline-on-heading-p ()
  168.   "Return T if point is on a header line."
  169.   (save-excursion
  170.     (beginning-of-line)
  171.     (and (eq (preceding-char) ?\n)
  172.      (looking-at outline-regexp))))
  173.  
  174. (defun outline-end-of-heading ()
  175.   (if (re-search-forward outline-heading-end-regexp nil 'move)
  176.       (forward-char -1)))
  177.  
  178. (defun outline-next-visible-heading (arg)
  179.   "Move to the next visible heading line.
  180. With argument, repeats or can move backward if negative.
  181. A heading line is one that starts with a `*' (or that outline-regexp matches)."
  182.   (interactive "p")
  183.   (if (< arg 0)
  184.       (beginning-of-line)
  185.     (end-of-line))
  186.   (re-search-forward (concat "^\\(" outline-regexp "\\)") nil nil arg)
  187.   (beginning-of-line))
  188.  
  189. (defun outline-previous-visible-heading (arg)
  190.   "Move to the previous heading line.
  191. With argument, repeats or can move forward if negative.
  192. A heading line is one that starts with a `*' (or that outline-regexp matches)."
  193.   (interactive "p")
  194.   (outline-next-visible-heading (- arg)))
  195.  
  196. (defun outline-flag-region (from to flag)
  197.   "Hides or shows lines from FROM to TO, according to FLAG.
  198. If FLAG is `\\n' (newline character) then text is shown,
  199. while if FLAG is `\\^M' (control-M) the text is hidden."
  200.   (let ((modp (buffer-modified-p)))
  201.     (unwind-protect
  202.         (subst-char-in-region from to
  203.                   (if (= flag ?\n) ?\^M ?\n)
  204.                   flag)
  205.      (set-buffer-modified-p modp))))
  206.  
  207. (defun hide-entry ()
  208.   "Hide the body directly following this heading."
  209.   (interactive)
  210.   (outline-back-to-heading)
  211.   (outline-end-of-heading)
  212.   (save-excursion
  213.    (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\^M)))
  214.  
  215. (defun show-entry ()
  216.   "Show the body directly following this heading."
  217.   (interactive)
  218.   (save-excursion
  219.    (outline-flag-region (point) (progn (outline-next-preface) (point)) ?\n)))
  220.  
  221. (defun hide-body ()
  222.   "Hide all of buffer except headings."
  223.   (interactive)
  224.   (hide-region-body (point-min) (point-max)))
  225.  
  226. (defun hide-region-body (start end)
  227.   "Hide all body lines in the region, but not headings."
  228.   (save-excursion
  229.     (save-restriction
  230.       (narrow-to-region start end)
  231.       (goto-char (point-min))
  232.       (if (outline-on-heading-p)
  233.       (outline-end-of-heading))
  234.       (while (not (eobp))
  235.     (outline-flag-region (point)
  236.                  (progn (outline-next-preface) (point)) ?\^M)
  237.     (if (not (eobp))
  238.         (progn
  239.           (forward-char
  240.            (if (looking-at "[\n\^M][\n\^M]")
  241.            2 1))
  242.           (outline-end-of-heading)))))))
  243.  
  244. (defun show-all ()
  245.   "Show all of the text in the buffer."
  246.   (interactive)
  247.   (outline-flag-region (point-min) (point-max) ?\n))
  248.  
  249. (defun hide-subtree ()
  250.   "Hide everything after this heading at deeper levels."
  251.   (interactive)
  252.   (outline-flag-subtree ?\^M))
  253.  
  254. (defun hide-leaves ()
  255.   "Hide all body after this heading at deeper levels."
  256.   (interactive)
  257.   (outline-back-to-heading)
  258.   (outline-end-of-heading)
  259.   (hide-region-body (point) (progn (outline-end-of-subtree) (point))))
  260.  
  261. (defun show-subtree ()
  262.   "Show everything after this heading at deeper levels."
  263.   (interactive)
  264.   (outline-flag-subtree ?\n))
  265.  
  266. (defun outline-flag-subtree (flag)
  267.   (save-excursion
  268.     (outline-back-to-heading)
  269.     (outline-end-of-heading)
  270.     (outline-flag-region (point)
  271.               (progn (outline-end-of-subtree) (point))
  272.               flag)))
  273.  
  274. (defun outline-end-of-subtree ()
  275.   (outline-back-to-heading)
  276.   (let ((opoint (point))
  277.     (first t)
  278.     (level (outline-level)))
  279.     (while (and (not (eobp))
  280.         (or first (> (outline-level) level)))
  281.       (setq first nil)
  282.       (outline-next-heading))
  283.     (forward-char -1)
  284.     (if (memq (preceding-char) '(?\n ?\^M))
  285.     (forward-char -1))))
  286.  
  287. (defun show-branches ()
  288.   "Show all subheadings of this heading, but not their bodies."
  289.   (interactive)
  290.   (show-children 1000))
  291.  
  292. (defun show-children (&optional level)
  293.   "Show all direct subheadings of this heading.
  294. Prefix arg LEVEL is how many levels below the current level should be shown.
  295. Default is enough to cause the following heading to appear."
  296.   (interactive "P")
  297.   (setq level
  298.     (if level (prefix-numeric-value level)
  299.       (save-excursion
  300.         (beginning-of-line)
  301.         (let ((start-level (outline-level)))
  302.           (outline-next-heading)
  303.           (max 1 (- (outline-level) start-level))))))
  304.   (save-excursion
  305.    (save-restriction
  306.     (beginning-of-line)
  307.     (setq level (+ level (outline-level)))
  308.     (narrow-to-region (point)
  309.               (progn (outline-end-of-subtree) (1+ (point))))
  310.     (goto-char (point-min))
  311.     (while (and (not (eobp))
  312.         (progn
  313.          (outline-next-heading)
  314.          (not (eobp))))
  315.       (if (<= (outline-level) level)
  316.       (save-excursion
  317.         (outline-flag-region (save-excursion
  318.                    (forward-char -1)
  319.                    (if (memq (preceding-char) '(?\n ?\^M))
  320.                        (forward-char -1))
  321.                    (point))
  322.                  (progn (outline-end-of-heading) (point))
  323.                  ?\n)))))))
  324.  
  325. (defun outline-up-heading (arg)
  326.   "Move to the heading line of which the present line is a subheading.
  327. With argument, move up ARG levels."
  328.   (interactive "p")
  329.   (outline-back-to-heading)
  330.   (if (eq (outline-level) 1)
  331.       (error ""))
  332.     (while (and (> (outline-level) 1)
  333.         (> arg 0)
  334.         (not (bobp)))
  335.       (let ((present-level (outline-level)))
  336.     (while (not (< (outline-level) present-level))
  337.       (outline-previous-visible-heading 1))
  338.     (setq arg (- arg 1)))))
  339.  
  340. (defun outline-forward-same-level (arg)
  341.   "Move forward to the ARG'th subheading from here of the same level as the
  342. present one. It stops at the first and last subheadings of a superior heading."
  343.   (interactive "p")
  344.   (outline-back-to-heading)
  345.   (while (> arg 0)
  346.     (let ((point-to-move-to (save-excursion
  347.                   (outline-get-next-sibling))))  
  348.       (if point-to-move-to
  349.       (progn
  350.         (goto-char point-to-move-to)
  351.         (setq arg (1- arg)))
  352.     (progn
  353.       (setq arg 0)
  354.       (error ""))))))
  355.  
  356. (defun outline-get-next-sibling ()
  357.   "Position the point at the next heading of the same level, 
  358. and return that position or nil if it cannot be found."
  359.   (let ((level (outline-level)))
  360.     (outline-next-visible-heading 1)
  361.     (while (and (> (outline-level) level)
  362.         (not (eobp)))
  363.       (outline-next-visible-heading 1))
  364.     (if (< (outline-level) level)
  365.     nil
  366.       (point))))
  367.     
  368. (defun outline-backward-same-level (arg)
  369.   "Move backward to the ARG'th subheading from here of the same level as the
  370. present one. It stops at the first and last subheadings of a superior heading."
  371.   (interactive "p")
  372.   (outline-back-to-heading)
  373.   (while (> arg 0)
  374.     (let ((point-to-move-to (save-excursion
  375.                   (outline-get-last-sibling))))
  376.       (if point-to-move-to
  377.       (progn
  378.         (goto-char point-to-move-to)
  379.         (setq arg (1- arg)))
  380.     (progn
  381.       (setq arg 0)
  382.       (error ""))))))
  383.  
  384. (defun outline-get-last-sibling ()
  385.   "Position the point at the previous heading of the same level, 
  386. and return that position or nil if it cannot be found."
  387.   (let ((level (outline-level)))
  388.     (outline-previous-visible-heading 1)
  389.     (while (and (> (outline-level) level)
  390.         (not (bobp)))
  391.       (outline-previous-visible-heading 1))
  392.     (if (< (outline-level) level)
  393.     nil
  394.         (point))))
  395.  
  396.