]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
(pp-buffer): New fun created from the code in
[gnu-emacs] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 1998, 2001, 2003, 2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: maint, 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
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Allows updating the copyright year and above mentioned GPL version manually
29 ;; or when saving a file.
30 ;; Do (add-hook 'before-save-hook 'copyright-update), or use
31 ;; M-x customize-variable RET before-save-hook RET.
32
33 ;;; Code:
34
35 (defgroup copyright nil
36 "Update the copyright notice in current buffer."
37 :group 'tools)
38
39 (defcustom copyright-limit 2000
40 "*Don't try to update copyright beyond this position unless interactive.
41 A value of nil means to search whole buffer."
42 :group 'copyright
43 :type '(choice (integer :tag "Limit")
44 (const :tag "No limit")))
45
46 ;; The character classes have the Latin-1 version and the Latin-9
47 ;; version, which is probably enough.
48 (defcustom copyright-regexp
49 "\\([\81©\8e©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
50 \\|[Cc]opyright\\s *:?\\s *[\81©\8e©]\\)\
51 \\s *\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
52 "*What your copyright notice looks like.
53 The second \\( \\) construct must match the years."
54 :group 'copyright
55 :type 'regexp)
56
57
58 (defcustom copyright-query 'function
59 "*If non-nil, ask user before changing copyright.
60 When this is `function', only ask when called non-interactively."
61 :group 'copyright
62 :type '(choice (const :tag "Do not ask")
63 (const :tag "Ask unless interactive" function)
64 (other :tag "Ask" t)))
65
66
67 ;; when modifying this, also modify the comment generated by autoinsert.el
68 (defconst copyright-current-gpl-version "2"
69 "String representing the current version of the GPL or nil.")
70
71 (defvar copyright-update t)
72
73 ;; This is a defvar rather than a defconst, because the year can
74 ;; change during the Emacs session.
75 (defvar copyright-current-year (substring (current-time-string) -4)
76 "String representing the current year.")
77
78 (defun copyright-update-year (replace noquery)
79 (when (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
80 ;; Note that `current-time-string' isn't locale-sensitive.
81 (setq copyright-current-year (substring (current-time-string) -4))
82 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
83 (substring copyright-current-year -2))
84 (if (or noquery
85 (y-or-n-p (if replace
86 (concat "Replace copyright year(s) by "
87 copyright-current-year "? ")
88 (concat "Add " copyright-current-year
89 " to copyright? "))))
90 (if replace
91 (replace-match copyright-current-year t t nil 1)
92 (let ((size (save-excursion (skip-chars-backward "0-9"))))
93 (if (and (eq (% (- (string-to-number copyright-current-year)
94 (string-to-number (buffer-substring
95 (+ (point) size)
96 (point))))
97 100)
98 1)
99 (or (eq (char-after (+ (point) size -1)) ?-)
100 (eq (char-after (+ (point) size -2)) ?-)))
101 ;; This is a range so just replace the end part.
102 (delete-char size)
103 ;; Detect if this is using the following shorthand:
104 ;; (C) 1993, 94, 95, 1998, 2000, 01, 02, 2003
105 (if (and
106 ;; Check that the last year was 4-chars and same century.
107 (eq size -4)
108 (equal (buffer-substring (- (point) 4) (- (point) 2))
109 (substring copyright-current-year 0 2))
110 ;; Check that there are 2-char years as well.
111 (save-excursion
112 (re-search-backward "[^0-9][0-9][0-9][^0-9]"
113 (line-beginning-position) t))
114 ;; Make sure we don't remove the first century marker.
115 (save-excursion
116 (forward-char size)
117 (re-search-backward
118 (concat (buffer-substring (point) (+ (point) 2))
119 "[0-9][0-9]")
120 (line-beginning-position) t)))
121 ;; Remove the century marker of the last entry.
122 (delete-region (- (point) 4) (- (point) 2)))
123 ;; Insert a comma with the preferred number of spaces.
124 (insert
125 (save-excursion
126 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
127 (line-beginning-position) t)
128 (match-string 1)
129 ", ")))
130 ;; If people use the '91 '92 '93 scheme, do that as well.
131 (if (eq (char-after (+ (point) size -3)) ?')
132 (insert ?')))
133 ;; Finally insert the new year.
134 (insert (substring copyright-current-year size))))))))
135
136 ;;;###autoload
137 (defun copyright-update (&optional arg interactivep)
138 "Update copyright notice at beginning of buffer to indicate the current year.
139 With prefix ARG, replace the years in the notice rather than adding
140 the current year after them. If necessary, and
141 `copyright-current-gpl-version' is set, any copying permissions
142 following the copyright are updated as well.
143 If non-nil, INTERACTIVEP tells the function to behave as when it's called
144 interactively."
145 (interactive "*P\nd")
146 (when (or copyright-update interactivep)
147 (let ((noquery (or (not copyright-query)
148 (and (eq copyright-query 'function) interactivep))))
149 (save-excursion
150 (save-restriction
151 (widen)
152 (goto-char (point-min))
153 (copyright-update-year arg noquery)
154 (goto-char (point-min))
155 (and copyright-current-gpl-version
156 ;; match the GPL version comment in .el files, including the
157 ;; bilingual Esperanto one in two-column, and in texinfo.tex
158 (re-search-forward "\\(the Free Software Foundation;\
159 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
160 version \\([0-9]+\\), or (at"
161 (+ (point) copyright-limit) t)
162 (not (string= (match-string 3) copyright-current-gpl-version))
163 (or noquery
164 (y-or-n-p (concat "Replace GPL version by "
165 copyright-current-gpl-version "? ")))
166 (progn
167 (if (match-end 2)
168 ;; Esperanto bilingual comment in two-column.el
169 (replace-match copyright-current-gpl-version t t nil 2))
170 (replace-match copyright-current-gpl-version t t nil 3))))
171 (set (make-local-variable 'copyright-update) nil)))
172 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
173 nil))
174
175
176 ;;;###autoload
177 (define-skeleton copyright
178 "Insert a copyright by $ORGANIZATION notice at cursor."
179 "Company: "
180 comment-start
181 "Copyright (C) " `(substring (current-time-string) -4) " by "
182 (or (getenv "ORGANIZATION")
183 str)
184 '(if (> (point) (+ (point-min) copyright-limit))
185 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
186 comment-end \n)
187
188 (provide 'copyright)
189
190 ;; For the copyright sign:
191 ;; Local Variables:
192 ;; coding: emacs-mule
193 ;; End:
194
195 ;;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
196 ;;; copyright.el ends here