Commit | Line | Data |
---|---|---|
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 DL |
35 | ;; You might add `elide-head' to appropriate major mode hooks or to |
36 | ;; `find-file-hooks'. Please do this in site init files. If you do, | |
37 | ;; information may be hidden from users who don't know it already. | |
af372af6 DL |
38 | |
39 | ;; Inspired by jwz's hide-copyleft.el, for which we don't have an | |
40 | ;; assignment. | |
41 | ||
42 | ;;; Code: | |
43 | ||
44 | (defgroup elide-head nil | |
45 | "Eliding copyright headers and the like in source files." | |
46 | :prefix "elide-head" | |
47 | :group 'tools) | |
48 | ||
49 | (defcustom elide-head-headers-to-hide | |
50 | '(("is free software; you can redistribute it" . ; GNU boilerplate | |
51 | "Boston, MA 02111-1307, USA\\.") | |
52 | ("The Regents of the University of California\\. All rights reserved\\." . | |
53 | "SUCH DAMAGE\\.") ; BSD | |
54 | ("Permission is hereby granted, free of charge" . ; X11 | |
55 | "authorization from the X Consortium\\.")) | |
56 | "Alist of regexps defining start end end of text to elide. | |
57 | ||
58 | The cars of elements of the list are searched for in order. Text is | |
59 | elided with an invisible overlay from the end of the line where the | |
60 | first match is found to the end of the match for the corresponding | |
61 | cdr." | |
62 | :group 'elide-head | |
63 | :type '(alist :key-type (string :tag "Start regexp") | |
64 | :value-type (string :tag "End regexp"))) | |
65 | ||
66 | (defvar elide-head-overlay nil) | |
67 | (make-variable-buffer-local 'elide-head-overlay) | |
68 | ||
69 | ;;;###autoload | |
70 | (defun elide-head (&optional arg) | |
71 | "Hide header material in buffer according to `elide-head-headers-to-hide'. | |
72 | ||
73 | The header is made invisible with an overlay. With a prefix arg, show | |
74 | an elided material again. | |
75 | ||
76 | This is suitable as an entry on `find-file-hooks' or appropriate mode hooks." | |
77 | (interactive "P") | |
78 | (if arg | |
79 | (elide-head-show) | |
80 | (save-excursion | |
81 | (save-restriction | |
82 | (let ((rest elide-head-headers-to-hide) | |
83 | beg end) | |
84 | (widen) | |
85 | (goto-char (point-min)) | |
86 | (while rest | |
87 | (save-excursion | |
88 | (when (re-search-forward (caar rest) nil t) | |
89 | (setq beg (point)) | |
90 | (when (re-search-forward (cdar rest) nil t) | |
91 | (setq end (point) | |
92 | rest nil)))) | |
93 | (if rest (setq rest (cdr rest)))) | |
94 | (if (not (and beg end)) | |
95 | (if (interactive-p) | |
96 | (error "No header found")) | |
97 | (goto-char beg) | |
98 | (end-of-line) | |
99 | (if (overlayp elide-head-overlay) | |
100 | (move-overlay elide-head-overlay (point) end) | |
101 | (setq elide-head-overlay (make-overlay (point) end))) | |
102 | (overlay-put elide-head-overlay 'invisible t) | |
103 | (overlay-put elide-head-overlay 'intangible t) | |
104 | (overlay-put elide-head-overlay 'after-string "..."))))))) | |
105 | ||
106 | (defun elide-head-show () | |
107 | "Show a header elided current buffer by \\[elide-head]." | |
108 | (interactive) | |
109 | (if (and (overlayp elide-head-overlay) | |
110 | (overlay-buffer elide-head-overlay)) | |
111 | (delete-overlay elide-head-overlay) | |
112 | (if (interactive-p) | |
113 | (error "No header hidden")))) | |
114 | ||
115 | (provide 'elide-head) | |
116 | ||
117 | ;;; elide-head.el ends here |