Merge from emacs-23
[bpt/emacs.git] / lisp / elide-head.el
CommitLineData
e8af40ee 1;;; elide-head.el --- hide headers in files
af372af6 2
10bd156e 3;; Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5df4f04c 4;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
af372af6
DL
5
6;; Author: Dave Love <fx@gnu.org>
7;; Keywords: outlines tools
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
af372af6 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
af372af6
DL
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
af372af6
DL
23
24;;; Commentary:
25
26;; Functionality for eliding boilerplate text (normally copyright
27;; notices) in file headers to avoid clutter when you know what it
28;; says.
29;;
30;; `elide-head-headers-to-hide' controls what is elided by the command
31;; `elide-head'. A buffer-local invisible overlay manages the
32;; elision.
33
40afb967 34;; You might add `elide-head' to appropriate major mode hooks or to
1a4914f3 35;; `find-file-hook'. Please do not do this in site init files. If
573d0a7e
DL
36;; you do, information may be hidden from users who don't know it
37;; already.
af372af6 38
ba8b2c1a
DL
39;; Note that `hs-minor-mode' will do a similar job by default, but
40;; it's not selective about what leading commentary it hides.
41
af372af6
DL
42;; Inspired by jwz's hide-copyleft.el, for which we don't have an
43;; assignment.
44
45;;; Code:
46
47(defgroup elide-head nil
48 "Eliding copyright headers and the like in source files."
c282ca4d 49 :version "21.1"
af372af6
DL
50 :prefix "elide-head"
51 :group 'tools)
52
53(defcustom elide-head-headers-to-hide
10bd156e
GM
54 '(("is free software[:;] you can redistribute it" . ; GNU boilerplate
55 "\\(Boston, MA 0211\\(1-1307\\|0-1301\\), USA\\|\
56If not, see <http://www\\.gnu\\.org/licenses/>\\)\\.")
af372af6 57 ("The Regents of the University of California\\. All rights reserved\\." .
0df0b0e9 58 "SUCH DAMAGE\\.") ; BSD
af372af6
DL
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.
62
63The cars of elements of the list are searched for in order. Text is
64elided with an invisible overlay from the end of the line where the
65first match is found to the end of the match for the corresponding
66cdr."
67 :group 'elide-head
68 :type '(alist :key-type (string :tag "Start regexp")
69 :value-type (string :tag "End regexp")))
70
71(defvar elide-head-overlay nil)
72(make-variable-buffer-local 'elide-head-overlay)
73
74;;;###autoload
75(defun elide-head (&optional arg)
76 "Hide header material in buffer according to `elide-head-headers-to-hide'.
77
78The header is made invisible with an overlay. With a prefix arg, show
79an elided material again.
80
1a4914f3 81This is suitable as an entry on `find-file-hook' or appropriate mode hooks."
af372af6
DL
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)
b88a9e9f 96 (setq end (point-marker)
af372af6
DL
97 rest nil))))
98 (if rest (setq rest (cdr rest))))
99 (if (not (and beg end))
32226619 100 (if (called-interactively-p 'interactive)
636004b9 101 (message "No header found"))
af372af6
DL
102 (goto-char beg)
103 (end-of-line)
104 (if (overlayp elide-head-overlay)
b88a9e9f 105 (move-overlay elide-head-overlay (point-marker) end)
b74ce5c2 106 (setq elide-head-overlay (make-overlay (point-marker) end)))
af372af6 107 (overlay-put elide-head-overlay 'invisible t)
d30e6710 108 (overlay-put elide-head-overlay 'evaporate t)
af372af6
DL
109 (overlay-put elide-head-overlay 'after-string "...")))))))
110
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)
32226619 117 (if (called-interactively-p 'interactive)
636004b9 118 (message "No header hidden"))))
af372af6
DL
119
120(provide 'elide-head)
121
cbee283d 122;; arch-tag: a00e6b5b-6aeb-45b1-b734-63e23df80928
af372af6 123;;; elide-head.el ends here