comparison lisp/utils/hide-copyleft.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 538048ae2ab8
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; -*- Mode:Emacs-Lisp -*-
2
3 ;;; "hide-copyleft.el" by Jamie Zawinski <jwz@lucid.com>, 19-jan-91.
4 ;;; Last modified 7-sep-91.
5 ;;;
6 ;;; I sometimes find it tiresome to have fifteen lines of copyright notice at
7 ;;; the beginning of each file. Meta-< does not take you to the beginning of
8 ;;; the code, it takes you a windowfull or two away, which can be tedious on
9 ;;; slow terminal lines.
10 ;;;
11 ;;; I know what the copyright notice says; so this code makes all but the first
12 ;;; line of it be invisible, by using Emacs's selective-display feature. The
13 ;;; text is still present and unmodified, but it is invisible.
14 ;;;
15 ;;; Elide the copyright notice with "Meta-X hide-copyleft-region". Make it
16 ;;; visible again with "Control-U Meta-X hide-copyleft-region". Or, if you're
17 ;;; sure you're not gonna get sued, you can do something like this in your
18 ;;; .emacs file:
19 ;;;
20 ;;; (autoload 'hide-copyleft-region "hide-copyleft" nil t)
21 ;;; (autoload 'unhide-copyleft-region "hide-copyleft" nil t)
22 ;;; (setq emacs-lisp-mode-hook 'hide-copyleft-region
23 ;;; c-mode-hook 'hide-copyleft-region)
24 ;;;
25 ;;; This code (obviously) has quite specific knowledge of the wording of the
26 ;;; various copyrights I've run across. Let me know if you find one on which
27 ;;; it fails.
28
29 (defvar copylefts-to-hide
30 ;; There are some extra backslashes in these strings to prevent this code
31 ;; from matching the definition of this list as the copyright notice!
32 '(;; GNU
33 ("free software\; you can redistribute it" .
34 "notice must be preserved on all")
35 ("free software\; you can redistribute it" .
36 "copy of the GNU General Public License.*\n?.*\n?.*\n?.*\n?02139,")
37 ("distributed in the hope that it will be useful\," .
38 "notice must be preserved on all")
39 ("free software\; you can redistribute it" .
40 "General Public License for more details\\.")
41 ;; X11
42 ("Permission to use\, copy, modify," .
43 "WITH THE USE OR PERFORMANCE")
44 ("Permission to use\, copy, modify," .
45 "without express or implied warranty")
46 ;; Motif
47 ("Copyright.*OPEN\ SOFTWARE FOUNDATION" .
48 "X Window System is a trademark of the")
49 ("THIS SOFTWARE\ IS FURNISHED UNDER A LICENSE" .
50 "X Window System is a trademark of the")
51 ;; UPenn
52 ("Permission to use\, copy, and distribute" .
53 " provided \"as is\" without")
54 ;; Evans & Sutherland, Solbourne.
55 ("Copyright 19[0-9][0-9] by " .
56 "OR PERFORMANCE OF THIS SOFTWARE\\.")
57 ;; TI Explorer
58 ("RESTRICTED RIGHTS LEGEND" . "All rights reserved\\.\\(\n;;; ?$\\)?")
59 ("^%%BeginDocumentation" . "^%%EndDocumentation")
60 )
61 "An alist of pairs of regexps which delimit copyright notices to hide.
62 The first one found is hidden, so order is significant.")
63
64
65 (defun hide-copyleft-region (&optional arg)
66 "Make the legal drivel at the front of this file invisible. Unhide it again
67 with C-u \\[hide-copyleft-region]."
68 (interactive "P")
69 (if arg
70 (unhide-copyleft-region)
71 (save-excursion
72 (save-restriction
73 (if selective-display (error "selective-display is already on."))
74 (catch 'Abort
75 (let ((mod-p (buffer-modified-p))
76 (buffer-read-only nil)
77 (rest copylefts-to-hide)
78 pair start end max)
79 (widen)
80 (goto-char (point-min))
81 (while (and rest (not pair))
82 (save-excursion
83 (and (re-search-forward (car (car rest)) nil t)
84 (setq start (point))
85 (re-search-forward (cdr (car rest)) nil t)
86 (setq end (point)
87 pair (car rest))))
88 (setq rest (cdr rest)))
89 (setq x pair)
90 (or pair
91 (if (interactive-p)
92 (error "Couldn't find a CopyLeft to hide.")
93 (throw 'Abort nil)))
94 (goto-char end)
95 (forward-line 1)
96 ;; If the last line of the notice closes a C comment, don't
97 ;; hide that line (to avoid confusion...)
98 (if (save-excursion (forward-char -3) (looking-at "\\*/"))
99 (forward-line -1))
100 (setq end (point))
101 (goto-char start)
102 (forward-line 1)
103 (while (< (point) end)
104 (delete-char -1)
105 (insert "\^M")
106 (forward-line 1))
107 (setq selective-display t)
108 (set-buffer-modified-p mod-p)))))))
109
110 (defun unhide-copyleft-region ()
111 "If the legal nonsense at the top of this file is elided, make it visible again."
112 (save-excursion
113 (save-restriction
114 (widen)
115 (goto-char (point-min))
116 (let ((mod-p (buffer-modified-p))
117 (buffer-read-only nil)
118 end)
119 (or (search-forward "\^M" nil t) (error "Nothing hidden here, dude."))
120 (end-of-line)
121 (setq end (point))
122 (beginning-of-line)
123 (while (search-forward "\^M" end t)
124 (delete-char -1)
125 (insert "\^J"))
126 (set-buffer-modified-p mod-p)
127 (setq selective-display nil)))))
128