Merge from emacs-24; up to 2012-05-01T00:16:02Z!rgm@gnu.org
[bpt/emacs.git] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991-1995, 1998, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
6 ;; Keywords: maint, 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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Allows updating the copyright year and above mentioned GPL version manually
26 ;; or when saving a file.
27 ;; Do (add-hook 'before-save-hook 'copyright-update), or use
28 ;; M-x customize-variable RET before-save-hook RET.
29
30 ;;; Code:
31
32 (defgroup copyright nil
33 "Update the copyright notice in current buffer."
34 :group 'tools)
35
36 (defcustom copyright-limit 2000
37 "Don't try to update copyright beyond this position unless interactive.
38 A value of nil means to search whole buffer."
39 :group 'copyright
40 :type '(choice (integer :tag "Limit")
41 (const :tag "No limit")))
42
43 (defcustom copyright-at-end-flag nil
44 "Non-nil means to search backwards from the end of the buffer for copyright.
45 This is useful for ChangeLogs."
46 :group 'copyright
47 :type 'boolean
48 :version "23.1")
49 ;;;###autoload(put 'copyright-at-end-flag 'safe-local-variable 'booleanp)
50
51 (defcustom copyright-regexp
52 "\\\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
53 \\|[Cc]opyright\\s *:?\\s *©\\)\
54 \\s *\\(?:[^0-9\n]*\\s *\\)?\
55 \\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
56 "What your copyright notice looks like.
57 The second \\( \\) construct must match the years."
58 :group 'copyright
59 :type 'regexp)
60
61 (defcustom copyright-names-regexp ""
62 "Regexp matching the names which correspond to the user.
63 Only copyright lines where the name matches this regexp will be updated.
64 This allows you to avoid adding years to a copyright notice belonging to
65 someone else or to a group for which you do not work."
66 :group 'copyright
67 :type 'regexp)
68
69 ;; The worst that can happen is a malicious regexp that overflows in
70 ;; the regexp matcher, a minor nuisance. It's a pain to be always
71 ;; prompted if you want to put this in a dir-locals.el.
72 ;;;###autoload(put 'copyright-names-regexp 'safe-local-variable 'stringp)
73
74 (defcustom copyright-years-regexp
75 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
76 "Match additional copyright notice years.
77 The second \\( \\) construct must match the years."
78 :group 'copyright
79 :type 'regexp)
80
81 ;; See "Copyright Notices" in maintain.info.
82 ;; TODO? 'end only for ranges at the end, other for all ranges.
83 ;; Minimum limit on the size of a range?
84 (defcustom copyright-year-ranges nil
85 "Non-nil if individual consecutive years should be replaced with a range.
86 For example: 2005, 2006, 2007, 2008 might be replaced with 2005-2008.
87 If you use ranges, you should add an explanatory note in a README file.
88 The function `copyright-fix-years' respects this variable."
89 :group 'copyright
90 :type 'boolean
91 :version "24.1")
92
93 ;;;###autoload(put 'copyright-year-ranges 'safe-local-variable 'booleanp)
94
95 (defcustom copyright-query 'function
96 "If non-nil, ask user before changing copyright.
97 When this is `function', only ask when called non-interactively."
98 :group 'copyright
99 :type '(choice (const :tag "Do not ask")
100 (const :tag "Ask unless interactive" function)
101 (other :tag "Ask" t)))
102
103
104 ;; when modifying this, also modify the comment generated by autoinsert.el
105 (defconst copyright-current-gpl-version "3"
106 "String representing the current version of the GPL or nil.")
107
108 (defvar copyright-update t
109 "The function `copyright-update' sets this to nil after updating a buffer.")
110
111 ;; This is a defvar rather than a defconst, because the year can
112 ;; change during the Emacs session.
113 (defvar copyright-current-year (format-time-string "%Y")
114 "String representing the current year.")
115
116 (defsubst copyright-limit () ; re-search-forward BOUND
117 (and copyright-limit
118 (if copyright-at-end-flag
119 (- (point) copyright-limit)
120 (+ (point) copyright-limit))))
121
122 (defun copyright-re-search (regexp &optional bound noerror count)
123 "Re-search forward or backward depending on `copyright-at-end-flag'."
124 (if copyright-at-end-flag
125 (re-search-backward regexp bound noerror count)
126 (re-search-forward regexp bound noerror count)))
127
128 (defun copyright-start-point ()
129 "Return point-min or point-max, depending on `copyright-at-end-flag'."
130 (if copyright-at-end-flag
131 (point-max)
132 (point-min)))
133
134 (defun copyright-offset-too-large-p ()
135 "Return non-nil if point is too far from the edge of the buffer."
136 (when copyright-limit
137 (if copyright-at-end-flag
138 (< (point) (- (point-max) copyright-limit))
139 (> (point) (+ (point-min) copyright-limit)))))
140
141 (defun copyright-find-copyright ()
142 "Return non-nil if a copyright header suitable for updating is found.
143 The header must match `copyright-regexp' and `copyright-names-regexp', if set.
144 This function sets the match-data that `copyright-update-year' uses."
145 (widen)
146 (goto-char (copyright-start-point))
147 (condition-case err
148 ;; (1) Need the extra \\( \\) around copyright-regexp because we
149 ;; goto (match-end 1) below. See note (2) below.
150 (copyright-re-search (concat "\\(" copyright-regexp
151 "\\)\\([ \t]*\n\\)?.*\\(?:"
152 copyright-names-regexp "\\)")
153 (copyright-limit)
154 t)
155 ;; In case the regexp is rejected. This is useful because
156 ;; copyright-update is typically called from before-save-hook where
157 ;; such an error is very inconvenient for the user.
158 (error (message "Can't update copyright: %s" err) nil)))
159
160 (defun copyright-find-end ()
161 "Possibly adjust the search performed by `copyright-find-copyright'.
162 If the years continue onto multiple lines that are marked as comments,
163 skips to the end of all the years."
164 (while (save-excursion
165 (and (eq (following-char) ?,)
166 (progn (forward-char 1) t)
167 (progn (skip-chars-forward " \t") (eolp))
168 comment-start-skip
169 (save-match-data
170 (forward-line 1)
171 (and (looking-at comment-start-skip)
172 (goto-char (match-end 0))))
173 (looking-at-p copyright-years-regexp)))
174 (forward-line 1)
175 (re-search-forward comment-start-skip)
176 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
177 ;; they are at note (1) above.
178 (re-search-forward (format "\\(%s\\)" copyright-years-regexp))))
179
180 (defun copyright-update-year (replace noquery)
181 ;; This uses the match-data from copyright-find-copyright/end.
182 (goto-char (match-end 1))
183 (copyright-find-end)
184 (setq copyright-current-year (format-time-string "%Y"))
185 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
186 (substring copyright-current-year -2))
187 (if (or noquery
188 (save-window-excursion
189 (switch-to-buffer (current-buffer))
190 ;; Fixes some point-moving oddness (bug#2209).
191 (save-excursion
192 (y-or-n-p (if replace
193 (concat "Replace copyright year(s) by "
194 copyright-current-year "? ")
195 (concat "Add " copyright-current-year
196 " to copyright? "))))))
197 (if replace
198 (replace-match copyright-current-year t t nil 3)
199 (let ((size (save-excursion (skip-chars-backward "0-9"))))
200 (if (and (eq (% (- (string-to-number copyright-current-year)
201 (string-to-number (buffer-substring
202 (+ (point) size)
203 (point))))
204 100)
205 1)
206 (or (eq (char-after (+ (point) size -1)) ?-)
207 (eq (char-after (+ (point) size -2)) ?-)))
208 ;; This is a range so just replace the end part.
209 (delete-char size)
210 ;; Insert a comma with the preferred number of spaces.
211 (insert
212 (save-excursion
213 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
214 (line-beginning-position) t)
215 (match-string 1)
216 ", ")))
217 ;; If people use the '91 '92 '93 scheme, do that as well.
218 (if (eq (char-after (+ (point) size -3)) ?')
219 (insert ?')))
220 ;; Finally insert the new year.
221 (insert (substring copyright-current-year size)))))))
222
223 ;;;###autoload
224 (defun copyright-update (&optional arg interactivep)
225 "Update copyright notice to indicate the current year.
226 With prefix ARG, replace the years in the notice rather than adding
227 the current year after them. If necessary, and
228 `copyright-current-gpl-version' is set, any copying permissions
229 following the copyright are updated as well.
230 If non-nil, INTERACTIVEP tells the function to behave as when it's called
231 interactively."
232 (interactive "*P\nd")
233 (when (or copyright-update interactivep)
234 (let ((noquery (or (not copyright-query)
235 (and (eq copyright-query 'function) interactivep))))
236 (save-excursion
237 (save-restriction
238 ;; If names-regexp doesn't match, we should not mess with
239 ;; the years _or_ the GPL version.
240 ;; TODO there may be multiple copyrights we should update.
241 (when (copyright-find-copyright)
242 (copyright-update-year arg noquery)
243 (goto-char (copyright-start-point))
244 (and copyright-current-gpl-version
245 ;; Match the GPL version comment in .el files.
246 ;; This is sensitive to line-breaks. :(
247 (copyright-re-search
248 "the Free Software Foundation[,;\n].*either version \
249 \\([0-9]+\\)\\(?: of the License\\)?, or[ \n].*any later version"
250 (copyright-limit) t)
251 ;; Don't update if the file is already using a more recent
252 ;; version than the "current" one.
253 (< (string-to-number (match-string 1))
254 (string-to-number copyright-current-gpl-version))
255 (or noquery
256 (save-match-data
257 (goto-char (match-end 1))
258 (save-window-excursion
259 (switch-to-buffer (current-buffer))
260 (y-or-n-p
261 (format "Replace GPL version %s with version %s? "
262 (match-string-no-properties 1)
263 copyright-current-gpl-version)))))
264 (replace-match copyright-current-gpl-version t t nil 1))))
265 (set (make-local-variable 'copyright-update) nil)))
266 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
267 nil))
268
269
270 ;; FIXME heuristic should be within 50 years of present (cf calendar).
271 ;;;###autoload
272 (defun copyright-fix-years ()
273 "Convert 2 digit years to 4 digit years.
274 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx.
275 If `copyright-year-ranges' (which see) is non-nil, also
276 independently replaces consecutive years with a range."
277 (interactive)
278 ;; TODO there may be multiple copyrights we should fix.
279 (if (copyright-find-copyright)
280 (let ((s (match-beginning 3))
281 (p (make-marker))
282 ;; Not line-beg-pos, so we don't mess up leading whitespace.
283 (copystart (match-beginning 0))
284 e last sep year prev-year first-year range-start range-end)
285 ;; In case years are continued over multiple, commented lines.
286 (goto-char (match-end 1))
287 (copyright-find-end)
288 (setq e (copy-marker (1+ (match-end 3))))
289 (goto-char s)
290 (while (re-search-forward "[0-9]+" e t)
291 (set-marker p (point))
292 (goto-char (match-beginning 0))
293 (setq year (string-to-number (match-string 0)))
294 (and (setq sep (char-before))
295 (/= (char-syntax sep) ?\s)
296 (/= sep ?-)
297 (insert " "))
298 (when (< year 100)
299 (insert (if (>= year 50) "19" "20"))
300 (setq year (+ year (if (>= year 50) 1900 2000))))
301 (goto-char p)
302 (when copyright-year-ranges
303 ;; If the previous thing was a range, don't try to tack more on.
304 ;; Ie not 2000-2005 -> 2000-2005-2007
305 ;; TODO should merge into existing range if possible.
306 (if (eq sep ?-)
307 (setq prev-year nil
308 year nil)
309 (if (and prev-year (= year (1+ prev-year)))
310 (setq range-end (point))
311 (when (and first-year prev-year
312 (> prev-year first-year))
313 (goto-char range-end)
314 (delete-region range-start range-end)
315 (insert (format "-%d" prev-year))
316 (goto-char p))
317 (setq first-year year
318 range-start (point)))))
319 (setq prev-year year
320 last p))
321 (when last
322 (when (and copyright-year-ranges
323 first-year prev-year
324 (> prev-year first-year))
325 (goto-char range-end)
326 (delete-region range-start range-end)
327 (insert (format "-%d" prev-year)))
328 (goto-char last)
329 ;; Don't mess up whitespace after the years.
330 (skip-chars-backward " \t")
331 (save-restriction
332 (narrow-to-region copystart (point))
333 ;; This is clearly wrong, eg what about comment markers?
334 ;;; (let ((fill-prefix " "))
335 ;; TODO do not break copyright owner over lines.
336 (fill-region (point-min) (point-max))))
337 (set-marker e nil)
338 (set-marker p nil))
339 ;; Simply reformatting the years is not copyrightable, so it does
340 ;; not seem right to call this. Also it messes with ranges.
341 ;;; (copyright-update nil t))
342 (message "No copyright message")))
343
344 ;;;###autoload
345 (define-skeleton copyright
346 "Insert a copyright by $ORGANIZATION notice at cursor."
347 "Company: "
348 comment-start
349 "Copyright (C) " `(format-time-string "%Y") " by "
350 (or (getenv "ORGANIZATION")
351 str)
352 '(if (copyright-offset-too-large-p)
353 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
354 comment-end \n)
355
356 ;; TODO: recurse, exclude COPYING etc.
357 ;;;###autoload
358 (defun copyright-update-directory (directory match &optional fix)
359 "Update copyright notice for all files in DIRECTORY matching MATCH.
360 If FIX is non-nil, run `copyright-fix-years' instead."
361 (interactive "DDirectory: \nMFilenames matching (regexp): ")
362 (dolist (file (directory-files directory t match nil))
363 (unless (file-directory-p file)
364 (message "Updating file `%s'" file)
365 (find-file file)
366 (let ((inhibit-read-only t)
367 (enable-local-variables :safe)
368 copyright-query)
369 (if fix
370 (copyright-fix-years)
371 (copyright-update)))
372 (save-buffer)
373 (kill-buffer (current-buffer)))))
374
375 (provide 'copyright)
376
377 ;; For the copyright sign:
378 ;; Local Variables:
379 ;; coding: utf-8
380 ;; End:
381
382 ;;; copyright.el ends here