elide-head.el 4.11 KB
Newer Older
1
;;; elide-head.el --- hide headers in files
Dave Love's avatar
Dave Love committed
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2006, 2007, 2008 Free Software Foundation, Inc.
Dave Love's avatar
Dave Love committed
5 6 7 8 9 10

;; Author: Dave Love <fx@gnu.org>
;; Keywords: outlines tools

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Dave Love's avatar
Dave Love committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Dave Love's avatar
Dave Love committed
15 16 17 18 19 20 21

;; 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
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Dave Love's avatar
Dave Love committed
23 24 25 26 27 28 29 30 31 32 33

;;; Commentary:

;; Functionality for eliding boilerplate text (normally copyright
;; notices) in file headers to avoid clutter when you know what it
;; says.
;;
;; `elide-head-headers-to-hide' controls what is elided by the command
;; `elide-head'.  A buffer-local invisible overlay manages the
;; elision.

Dave Love's avatar
Dave Love committed
34
;; You might add `elide-head' to appropriate major mode hooks or to
35
;; `find-file-hook'.  Please do not do this in site init files.  If
Dave Love's avatar
Dave Love committed
36 37
;; you do, information may be hidden from users who don't know it
;; already.
Dave Love's avatar
Dave Love committed
38

Dave Love's avatar
Dave Love committed
39 40 41
;; Note that `hs-minor-mode' will do a similar job by default, but
;; it's not selective about what leading commentary it hides.

Dave Love's avatar
Dave Love committed
42 43 44 45 46 47 48
;; Inspired by jwz's hide-copyleft.el, for which we don't have an
;; assignment.

;;; Code:

(defgroup elide-head nil
  "Eliding copyright headers and the like in source files."
Dave Love's avatar
Dave Love committed
49
  :version "21.1"
Dave Love's avatar
Dave Love committed
50 51 52 53 54
  :prefix "elide-head"
  :group 'tools)

(defcustom elide-head-headers-to-hide
  '(("is free software; you can redistribute it" . ; GNU boilerplate
55
     "Boston, MA 0211\\(1-1307\\|0-1301\\), USA\\.")
Dave Love's avatar
Dave Love committed
56
    ("The Regents of the University of California\\.  All rights reserved\\." .
57
     "SUCH DAMAGE\\.")				      ; BSD
Dave Love's avatar
Dave Love committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    ("Permission is hereby granted, free of charge" . ; X11
     "authorization from the X Consortium\\."))
  "Alist of regexps defining start end end of text to elide.

The cars of elements of the list are searched for in order.  Text is
elided with an invisible overlay from the end of the line where the
first match is found to the end of the match for the corresponding
cdr."
  :group 'elide-head
  :type '(alist :key-type  (string :tag "Start regexp")
		:value-type (string :tag "End regexp")))

(defvar elide-head-overlay nil)
(make-variable-buffer-local 'elide-head-overlay)

;;;###autoload
(defun elide-head (&optional arg)
  "Hide header material in buffer according to `elide-head-headers-to-hide'.

The header is made invisible with an overlay.  With a prefix arg, show
an elided material again.

80
This is suitable as an entry on `find-file-hook' or appropriate mode hooks."
Dave Love's avatar
Dave Love committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
  (interactive "P")
  (if arg
      (elide-head-show)
    (save-excursion
      (save-restriction
	(let ((rest elide-head-headers-to-hide)
	      beg end)
	  (widen)
	  (goto-char (point-min))
	  (while rest
	    (save-excursion
	      (when (re-search-forward (caar rest) nil t)
		(setq beg (point))
		(when (re-search-forward (cdar rest) nil t)
95
		  (setq end (point-marker)
Dave Love's avatar
Dave Love committed
96 97 98 99
			rest nil))))
	    (if rest (setq rest (cdr rest))))
	  (if (not (and beg end))
	      (if (interactive-p)
100
		  (message "No header found"))
Dave Love's avatar
Dave Love committed
101 102 103
	    (goto-char beg)
	    (end-of-line)
	    (if (overlayp elide-head-overlay)
104
		(move-overlay elide-head-overlay (point-marker) end)
105
	      (setq elide-head-overlay (make-overlay (point-marker) end)))
Dave Love's avatar
Dave Love committed
106
	    (overlay-put elide-head-overlay 'invisible t)
107
	    (overlay-put elide-head-overlay 'evaporate t)
Dave Love's avatar
Dave Love committed
108 109 110 111 112 113 114 115 116
	    (overlay-put elide-head-overlay 'after-string "...")))))))

(defun elide-head-show ()
  "Show a header elided current buffer by \\[elide-head]."
  (interactive)
  (if (and (overlayp elide-head-overlay)
	   (overlay-buffer elide-head-overlay))
      (delete-overlay elide-head-overlay)
    (if (interactive-p)
117
	(message "No header hidden"))))
Dave Love's avatar
Dave Love committed
118 119 120

(provide 'elide-head)

121
;; arch-tag: a00e6b5b-6aeb-45b1-b734-63e23df80928
Dave Love's avatar
Dave Love committed
122
;;; elide-head.el ends here