*** empty log message ***
[emacs.git] / lisp / elide-head.el
blob50c77684ae5dd52d1c1aeec613cf80385157b726
1 ;;; elid-head.el --- hide headers in files
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: outlines tools
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; Functionality for eliding boilerplate text (normally copyright
28 ;; notices) in file headers to avoid clutter when you know what it
29 ;; says.
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.
35 ;; You might add `elide-head' to appropriate major mode hooks or to
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.
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.
43 ;; Inspired by jwz's hide-copyleft.el, for which we don't have an
44 ;; assignment.
46 ;;; Code:
48 (defgroup elide-head nil
49 "Eliding copyright headers and the like in source files."
50 :version "21.1"
51 :prefix "elide-head"
52 :group 'tools)
54 (defcustom elide-head-headers-to-hide
55 '(("is free software; you can redistribute it" . ; GNU boilerplate
56 "Boston, MA 02111-1307, USA\\.")
57 ("The Regents of the University of California\\. All rights reserved\\." .
58 "SUCH DAMAGE\\.") ; BSD
59 ("Permission is hereby granted, free of charge" . ; X11
60 "authorization from the X Consortium\\."))
61 "Alist of regexps defining start end end of text to elide.
63 The cars of elements of the list are searched for in order. Text is
64 elided with an invisible overlay from the end of the line where the
65 first match is found to the end of the match for the corresponding
66 cdr."
67 :group 'elide-head
68 :type '(alist :key-type (string :tag "Start regexp")
69 :value-type (string :tag "End regexp")))
71 (defvar elide-head-overlay nil)
72 (make-variable-buffer-local 'elide-head-overlay)
74 ;;;###autoload
75 (defun elide-head (&optional arg)
76 "Hide header material in buffer according to `elide-head-headers-to-hide'.
78 The header is made invisible with an overlay. With a prefix arg, show
79 an elided material again.
81 This is suitable as an entry on `find-file-hooks' or appropriate mode hooks."
82 (interactive "P")
83 (if arg
84 (elide-head-show)
85 (save-excursion
86 (save-restriction
87 (let ((rest elide-head-headers-to-hide)
88 beg end)
89 (widen)
90 (goto-char (point-min))
91 (while rest
92 (save-excursion
93 (when (re-search-forward (caar rest) nil t)
94 (setq beg (point))
95 (when (re-search-forward (cdar rest) nil t)
96 (setq end (point-marker)
97 rest nil))))
98 (if rest (setq rest (cdr rest))))
99 (if (not (and beg end))
100 (if (interactive-p)
101 (error "No header found"))
102 (goto-char beg)
103 (end-of-line)
104 (if (overlayp elide-head-overlay)
105 (move-overlay elide-head-overlay (point-marker) end)
106 (setq elide-head-overlay (make-overlay (point-marker) end)))
107 (overlay-put elide-head-overlay 'invisible t)
108 (overlay-put elide-head-overlay 'intangible t)
109 (overlay-put elide-head-overlay 'after-string "...")))))))
111 (defun elide-head-show ()
112 "Show a header elided current buffer by \\[elide-head]."
113 (interactive)
114 (if (and (overlayp elide-head-overlay)
115 (overlay-buffer elide-head-overlay))
116 (delete-overlay elide-head-overlay)
117 (if (interactive-p)
118 (error "No header hidden"))))
120 (provide 'elide-head)
122 ;;; elide-head.el ends here