Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; elide-head.el --- hide headers in files |
af372af6 | 2 | |
aaef169d TTN |
3 | ;; Copyright (C) 1999, 2002, 2003, 2004, 2005, |
4 | ;; 2006 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 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
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 | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
af372af6 DL |
25 | |
26 | ;;; Commentary: | |
27 | ||
28 | ;; Functionality for eliding boilerplate text (normally copyright | |
29 | ;; notices) in file headers to avoid clutter when you know what it | |
30 | ;; says. | |
31 | ;; | |
32 | ;; `elide-head-headers-to-hide' controls what is elided by the command | |
33 | ;; `elide-head'. A buffer-local invisible overlay manages the | |
34 | ;; elision. | |
35 | ||
40afb967 | 36 | ;; You might add `elide-head' to appropriate major mode hooks or to |
1a4914f3 | 37 | ;; `find-file-hook'. Please do not do this in site init files. If |
573d0a7e DL |
38 | ;; you do, information may be hidden from users who don't know it |
39 | ;; already. | |
af372af6 | 40 | |
ba8b2c1a DL |
41 | ;; Note that `hs-minor-mode' will do a similar job by default, but |
42 | ;; it's not selective about what leading commentary it hides. | |
43 | ||
af372af6 DL |
44 | ;; Inspired by jwz's hide-copyleft.el, for which we don't have an |
45 | ;; assignment. | |
46 | ||
47 | ;;; Code: | |
48 | ||
49 | (defgroup elide-head nil | |
50 | "Eliding copyright headers and the like in source files." | |
c282ca4d | 51 | :version "21.1" |
af372af6 DL |
52 | :prefix "elide-head" |
53 | :group 'tools) | |
54 | ||
55 | (defcustom elide-head-headers-to-hide | |
56 | '(("is free software; you can redistribute it" . ; GNU boilerplate | |
0df0b0e9 | 57 | "Boston, MA 0211\\(1-1307\\|0-1301\\), USA\\.") |
af372af6 | 58 | ("The Regents of the University of California\\. All rights reserved\\." . |
0df0b0e9 | 59 | "SUCH DAMAGE\\.") ; BSD |
af372af6 DL |
60 | ("Permission is hereby granted, free of charge" . ; X11 |
61 | "authorization from the X Consortium\\.")) | |
62 | "Alist of regexps defining start end end of text to elide. | |
63 | ||
64 | The cars of elements of the list are searched for in order. Text is | |
65 | elided with an invisible overlay from the end of the line where the | |
66 | first match is found to the end of the match for the corresponding | |
67 | cdr." | |
68 | :group 'elide-head | |
69 | :type '(alist :key-type (string :tag "Start regexp") | |
70 | :value-type (string :tag "End regexp"))) | |
71 | ||
72 | (defvar elide-head-overlay nil) | |
73 | (make-variable-buffer-local 'elide-head-overlay) | |
74 | ||
75 | ;;;###autoload | |
76 | (defun elide-head (&optional arg) | |
77 | "Hide header material in buffer according to `elide-head-headers-to-hide'. | |
78 | ||
79 | The header is made invisible with an overlay. With a prefix arg, show | |
80 | an elided material again. | |
81 | ||
1a4914f3 | 82 | This is suitable as an entry on `find-file-hook' or appropriate mode hooks." |
af372af6 DL |
83 | (interactive "P") |
84 | (if arg | |
85 | (elide-head-show) | |
86 | (save-excursion | |
87 | (save-restriction | |
88 | (let ((rest elide-head-headers-to-hide) | |
89 | beg end) | |
90 | (widen) | |
91 | (goto-char (point-min)) | |
92 | (while rest | |
93 | (save-excursion | |
94 | (when (re-search-forward (caar rest) nil t) | |
95 | (setq beg (point)) | |
96 | (when (re-search-forward (cdar rest) nil t) | |
b88a9e9f | 97 | (setq end (point-marker) |
af372af6 DL |
98 | rest nil)))) |
99 | (if rest (setq rest (cdr rest)))) | |
100 | (if (not (and beg end)) | |
101 | (if (interactive-p) | |
636004b9 | 102 | (message "No header found")) |
af372af6 DL |
103 | (goto-char beg) |
104 | (end-of-line) | |
105 | (if (overlayp elide-head-overlay) | |
b88a9e9f | 106 | (move-overlay elide-head-overlay (point-marker) end) |
b74ce5c2 | 107 | (setq elide-head-overlay (make-overlay (point-marker) end))) |
af372af6 | 108 | (overlay-put elide-head-overlay 'invisible t) |
d30e6710 | 109 | (overlay-put elide-head-overlay 'evaporate t) |
af372af6 DL |
110 | (overlay-put elide-head-overlay 'after-string "..."))))))) |
111 | ||
112 | (defun elide-head-show () | |
113 | "Show a header elided current buffer by \\[elide-head]." | |
114 | (interactive) | |
115 | (if (and (overlayp elide-head-overlay) | |
116 | (overlay-buffer elide-head-overlay)) | |
117 | (delete-overlay elide-head-overlay) | |
118 | (if (interactive-p) | |
636004b9 | 119 | (message "No header hidden")))) |
af372af6 DL |
120 | |
121 | (provide 'elide-head) | |
122 | ||
ab5796a9 | 123 | ;;; arch-tag: a00e6b5b-6aeb-45b1-b734-63e23df80928 |
af372af6 | 124 | ;;; elide-head.el ends here |