Merge from emacs--rel--22
[bpt/emacs.git] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (defcustom copyright-names-regexp ""
58 "Regexp matching the names which correspond to the user.
59 Only copyright lines where the name matches this regexp will be updated.
60 This allows you to avoid adding yars to a copyright notice belonging to
61 someone else or to a group for which you do not work."
62 :type 'regexp)
63
64 (defcustom copyright-years-regexp
65 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
66 "Match additional copyright notice years.
67 The second \\( \\) construct must match the years."
68 :group 'copyright
69 :type 'regexp)
70
71
72 (defcustom copyright-query 'function
73 "If non-nil, ask user before changing copyright.
74 When this is `function', only ask when called non-interactively."
75 :group 'copyright
76 :type '(choice (const :tag "Do not ask")
77 (const :tag "Ask unless interactive" function)
78 (other :tag "Ask" t)))
79
80
81 ;; when modifying this, also modify the comment generated by autoinsert.el
82 (defconst copyright-current-gpl-version "3"
83 "String representing the current version of the GPL or nil.")
84
85 (defvar copyright-update t)
86
87 ;; This is a defvar rather than a defconst, because the year can
88 ;; change during the Emacs session.
89 (defvar copyright-current-year (substring (current-time-string) -4)
90 "String representing the current year.")
91
92 (defun copyright-update-year (replace noquery)
93 (when
94 (condition-case err
95 (re-search-forward (concat "\\(" copyright-regexp
96 "\\)\\([ \t]*\n\\)?.*\\(?:"
97 copyright-names-regexp "\\)")
98 (+ (point) copyright-limit) t)
99 ;; In case the regexp is rejected. This is useful because
100 ;; copyright-update is typically called from before-save-hook where
101 ;; such an error is very inconvenient for the user.
102 (error (message "Can't update copyright: %s" err) nil))
103 (goto-char (match-end 1))
104 ;; If the years are continued onto multiple lined
105 ;; that are marked as comments, skip to the end of the years anyway.
106 (while (save-excursion
107 (and (eq (following-char) ?,)
108 (progn (forward-char 1) t)
109 (progn (skip-chars-forward " \t") (eolp))
110 comment-start-skip
111 (save-match-data
112 (forward-line 1)
113 (and (looking-at comment-start-skip)
114 (goto-char (match-end 1))))
115 (save-match-data
116 (looking-at copyright-years-regexp))))
117 (forward-line 1)
118 (re-search-forward comment-start-skip)
119 (re-search-forward copyright-years-regexp))
120
121 ;; Note that `current-time-string' isn't locale-sensitive.
122 (setq copyright-current-year (substring (current-time-string) -4))
123 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
124 (substring copyright-current-year -2))
125 (if (or noquery
126 (y-or-n-p (if replace
127 (concat "Replace copyright year(s) by "
128 copyright-current-year "? ")
129 (concat "Add " copyright-current-year
130 " to copyright? "))))
131 (if replace
132 (replace-match copyright-current-year t t nil 2)
133 (let ((size (save-excursion (skip-chars-backward "0-9"))))
134 (if (and (eq (% (- (string-to-number copyright-current-year)
135 (string-to-number (buffer-substring
136 (+ (point) size)
137 (point))))
138 100)
139 1)
140 (or (eq (char-after (+ (point) size -1)) ?-)
141 (eq (char-after (+ (point) size -2)) ?-)))
142 ;; This is a range so just replace the end part.
143 (delete-char size)
144 ;; Insert a comma with the preferred number of spaces.
145 (insert
146 (save-excursion
147 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
148 (line-beginning-position) t)
149 (match-string 1)
150 ", ")))
151 ;; If people use the '91 '92 '93 scheme, do that as well.
152 (if (eq (char-after (+ (point) size -3)) ?')
153 (insert ?')))
154 ;; Finally insert the new year.
155 (insert (substring copyright-current-year size))))))))
156
157 ;;;###autoload
158 (defun copyright-update (&optional arg interactivep)
159 "Update copyright notice at beginning of buffer to indicate the current year.
160 With prefix ARG, replace the years in the notice rather than adding
161 the current year after them. If necessary, and
162 `copyright-current-gpl-version' is set, any copying permissions
163 following the copyright are updated as well.
164 If non-nil, INTERACTIVEP tells the function to behave as when it's called
165 interactively."
166 (interactive "*P\nd")
167 (when (or copyright-update interactivep)
168 (let ((noquery (or (not copyright-query)
169 (and (eq copyright-query 'function) interactivep))))
170 (save-excursion
171 (save-restriction
172 (widen)
173 (goto-char (point-min))
174 (copyright-update-year arg noquery)
175 (goto-char (point-min))
176 (and copyright-current-gpl-version
177 ;; match the GPL version comment in .el files, including the
178 ;; bilingual Esperanto one in two-column, and in texinfo.tex
179 (re-search-forward "\\(the Free Software Foundation;\
180 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
181 version \\([0-9]+\\), or (at"
182 (+ (point) copyright-limit) t)
183 (not (string= (match-string 3) copyright-current-gpl-version))
184 (or noquery
185 (y-or-n-p (concat "Replace GPL version by "
186 copyright-current-gpl-version "? ")))
187 (progn
188 (if (match-end 2)
189 ;; Esperanto bilingual comment in two-column.el
190 (replace-match copyright-current-gpl-version t t nil 2))
191 (replace-match copyright-current-gpl-version t t nil 3))))
192 (set (make-local-variable 'copyright-update) nil)))
193 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
194 nil))
195
196
197 ;;;###autoload
198 (defun copyright-fix-years ()
199 "Convert 2 digit years to 4 digit years.
200 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
201 (interactive)
202 (widen)
203 (goto-char (point-min))
204 (if (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
205 (let ((s (match-beginning 2))
206 (e (copy-marker (1+ (match-end 2))))
207 (p (make-marker))
208 last)
209 (goto-char s)
210 (while (re-search-forward "[0-9]+" e t)
211 (set-marker p (point))
212 (goto-char (match-beginning 0))
213 (let ((sep (char-before))
214 (year (string-to-number (match-string 0))))
215 (when (and sep
216 (/= (char-syntax sep) ?\s)
217 (/= sep ?-))
218 (insert " "))
219 (when (< year 100)
220 (insert (if (>= year 50) "19" "20"))))
221 (goto-char p)
222 (setq last p))
223 (when last
224 (goto-char last)
225 ;; Don't mess up whitespace after the years.
226 (skip-chars-backward " \t")
227 (save-restriction
228 (narrow-to-region (point-min) (point))
229 (let ((fill-prefix " "))
230 (fill-region s last))))
231 (set-marker e nil)
232 (set-marker p nil)
233 (copyright-update nil t))
234 (message "No copyright message")))
235
236 ;;;###autoload
237 (define-skeleton copyright
238 "Insert a copyright by $ORGANIZATION notice at cursor."
239 "Company: "
240 comment-start
241 "Copyright (C) " `(substring (current-time-string) -4) " by "
242 (or (getenv "ORGANIZATION")
243 str)
244 '(if (> (point) (+ (point-min) copyright-limit))
245 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
246 comment-end \n)
247
248 (provide 'copyright)
249
250 ;; For the copyright sign:
251 ;; Local Variables:
252 ;; coding: emacs-mule
253 ;; End:
254
255 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
256 ;;; copyright.el ends here