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

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1999, 2001-2020 Free Software Foundation, Inc.
Dave Love's avatar
Dave Love committed
4 5 6 7 8 9

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

;; This file is part of GNU Emacs.

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

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

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

Dave Love's avatar
Dave Love committed
38 39 40
;; 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
41 42 43 44 45 46 47
;; 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
48
  :version "21.1"
Dave Love's avatar
Dave Love committed
49 50 51 52
  :prefix "elide-head"
  :group 'tools)

(defcustom elide-head-headers-to-hide
53 54
  '(("is free software[:;] you can redistribute it" . ; GNU boilerplate
     "\\(Boston, MA 0211\\(1-1307\\|0-1301\\), USA\\|\
55
If not, see <https?://www\\.gnu\\.org/licenses/>\\)\\.")
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
    ("Permission is hereby granted, free of charge" . ; X11
     "authorization from the X Consortium\\."))
60
  "Alist of regexps defining start and end of text to elide.
Dave Love's avatar
Dave Love committed
61 62 63 64 65 66

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
67 68
  :type '(alist :key-type  (regexp :tag "Start regexp")
		:value-type (regexp :tag "End regexp")))
Dave Love's avatar
Dave Love committed
69 70 71 72 73 74 75 76 77 78 79

(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
			rest nil))))
	    (if rest (setq rest (cdr rest))))
	  (if (not (and beg end))
99
	      (if (called-interactively-p 'interactive)
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
	    (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)
116
    (if (called-interactively-p 'interactive)
117
	(message "No header hidden"))))
Dave Love's avatar
Dave Love committed
118 119 120 121

(provide 'elide-head)

;;; elide-head.el ends here