(cl-emacs-type): Remove defvar.
[bpt/emacs.git] / lisp / elide-head.el
CommitLineData
af372af6
DL
1;;; elid-head.el --- hide headers in files
2
3;; Copyright (C) 1999 Free Software Foundation, Inc.
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: outlines tools
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
24
25;;; Commentary:
26
27;; Functionality for eliding boilerplate text (normally copyright
28;; notices) in file headers to avoid clutter when you know what it
29;; says.
30;;
31;; `elide-head-headers-to-hide' controls what is elided by the command
32;; `elide-head'. A buffer-local invisible overlay manages the
33;; elision.
34
40afb967 35;; You might add `elide-head' to appropriate major mode hooks or to
573d0a7e
DL
36;; `find-file-hooks'. Please do not do this in site init files. If
37;; you do, information may be hidden from users who don't know it
38;; already.
af372af6 39
ba8b2c1a
DL
40;; Note that `hs-minor-mode' will do a similar job by default, but
41;; it's not selective about what leading commentary it hides.
42
af372af6
DL
43;; Inspired by jwz's hide-copyleft.el, for which we don't have an
44;; assignment.
45
46;;; Code:
47
48(defgroup elide-head nil
49 "Eliding copyright headers and the like in source files."
50 :prefix "elide-head"
51 :group 'tools)
52
53(defcustom elide-head-headers-to-hide
54 '(("is free software; you can redistribute it" . ; GNU boilerplate
55 "Boston, MA 02111-1307, USA\\.")
56 ("The Regents of the University of California\\. All rights reserved\\." .
57 "SUCH DAMAGE\\.") ; BSD
58 ("Permission is hereby granted, free of charge" . ; X11
59 "authorization from the X Consortium\\."))
60 "Alist of regexps defining start end end of text to elide.
61
62The cars of elements of the list are searched for in order. Text is
63elided with an invisible overlay from the end of the line where the
64first match is found to the end of the match for the corresponding
65cdr."
66 :group 'elide-head
67 :type '(alist :key-type (string :tag "Start regexp")
68 :value-type (string :tag "End regexp")))
69
70(defvar elide-head-overlay nil)
71(make-variable-buffer-local 'elide-head-overlay)
72
73;;;###autoload
74(defun elide-head (&optional arg)
75 "Hide header material in buffer according to `elide-head-headers-to-hide'.
76
77The header is made invisible with an overlay. With a prefix arg, show
78an elided material again.
79
80This is suitable as an entry on `find-file-hooks' or appropriate mode hooks."
81 (interactive "P")
82 (if arg
83 (elide-head-show)
84 (save-excursion
85 (save-restriction
86 (let ((rest elide-head-headers-to-hide)
87 beg end)
88 (widen)
89 (goto-char (point-min))
90 (while rest
91 (save-excursion
92 (when (re-search-forward (caar rest) nil t)
93 (setq beg (point))
94 (when (re-search-forward (cdar rest) nil t)
95 (setq end (point)
96 rest nil))))
97 (if rest (setq rest (cdr rest))))
98 (if (not (and beg end))
99 (if (interactive-p)
100 (error "No header found"))
101 (goto-char beg)
102 (end-of-line)
103 (if (overlayp elide-head-overlay)
104 (move-overlay elide-head-overlay (point) end)
105 (setq elide-head-overlay (make-overlay (point) end)))
106 (overlay-put elide-head-overlay 'invisible t)
107 (overlay-put elide-head-overlay 'intangible t)
108 (overlay-put elide-head-overlay 'after-string "...")))))))
109
110(defun elide-head-show ()
111 "Show a header elided current buffer by \\[elide-head]."
112 (interactive)
113 (if (and (overlayp elide-head-overlay)
114 (overlay-buffer elide-head-overlay))
115 (delete-overlay elide-head-overlay)
116 (if (interactive-p)
117 (error "No header hidden"))))
118
119(provide 'elide-head)
120
121;;; elide-head.el ends here