Tweak previous copyright.el change.
[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, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
6
7 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: maint, tools
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Allows updating the copyright year and above mentioned GPL version manually
28 ;; or when saving a file.
29 ;; Do (add-hook 'before-save-hook 'copyright-update), or use
30 ;; M-x customize-variable RET before-save-hook RET.
31
32 ;;; Code:
33
34 (defgroup copyright nil
35 "Update the copyright notice in current buffer."
36 :group 'tools)
37
38 (defcustom copyright-limit 2000
39 "Don't try to update copyright beyond this position unless interactive.
40 A value of nil means to search whole buffer."
41 :group 'copyright
42 :type '(choice (integer :tag "Limit")
43 (const :tag "No limit")))
44
45 (defcustom copyright-at-end-flag nil
46 "Non-nil means to search backwards from the end of the buffer for copyright.
47 This is useful for ChangeLogs."
48 :group 'copyright
49 :type 'boolean
50 :version "23.1")
51
52 (defcustom copyright-regexp
53 "\\\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
54 \\|[Cc]opyright\\s *:?\\s *©\\)\
55 \\s *\\(?:[^0-9\n]*\\s *\\)?\
56 \\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
57 "What your copyright notice looks like.
58 The second \\( \\) construct must match the years."
59 :group 'copyright
60 :type 'regexp)
61
62 (defcustom copyright-names-regexp ""
63 "Regexp matching the names which correspond to the user.
64 Only copyright lines where the name matches this regexp will be updated.
65 This allows you to avoid adding years to a copyright notice belonging to
66 someone else or to a group for which you do not work."
67 :group 'copyright
68 :type 'regexp)
69
70 (defcustom copyright-years-regexp
71 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
72 "Match additional copyright notice years.
73 The second \\( \\) construct must match the years."
74 :group 'copyright
75 :type 'regexp)
76
77
78 (defcustom copyright-query 'function
79 "If non-nil, ask user before changing copyright.
80 When this is `function', only ask when called non-interactively."
81 :group 'copyright
82 :type '(choice (const :tag "Do not ask")
83 (const :tag "Ask unless interactive" function)
84 (other :tag "Ask" t)))
85
86
87 ;; when modifying this, also modify the comment generated by autoinsert.el
88 (defconst copyright-current-gpl-version "3"
89 "String representing the current version of the GPL or nil.")
90
91 (defvar copyright-update t
92 "The function `copyright-update' sets this to nil after updating a buffer.")
93
94 ;; This is a defvar rather than a defconst, because the year can
95 ;; change during the Emacs session.
96 (defvar copyright-current-year (substring (current-time-string) -4)
97 "String representing the current year.")
98
99 (defsubst copyright-limit () ; re-search-forward BOUND
100 (and copyright-limit
101 (if copyright-at-end-flag
102 (- (point) copyright-limit)
103 (+ (point) copyright-limit))))
104
105 (defun copyright-re-search (regexp &optional bound noerror count)
106 "Re-search forward or backward depending on `copyright-at-end-flag'."
107 (if copyright-at-end-flag
108 (re-search-backward regexp bound noerror count)
109 (re-search-forward regexp bound noerror count)))
110
111 (defun copyright-start-point ()
112 "Return point-min or point-max, depending on `copyright-at-end-flag'."
113 (if copyright-at-end-flag
114 (point-max)
115 (point-min)))
116
117 (defun copyright-offset-too-large-p ()
118 "Return non-nil if point is too far from the edge of the buffer."
119 (when copyright-limit
120 (if copyright-at-end-flag
121 (< (point) (- (point-max) copyright-limit))
122 (> (point) (+ (point-min) copyright-limit)))))
123
124 (defun copyright-find-copyright ()
125 "Return non-nil if a copyright header suitable for updating is found.
126 The header must match `copyright-regexp' and `copyright-names-regexp', if set.
127 This function sets the match-data that `copyright-update-year' uses."
128 (widen)
129 (goto-char (copyright-start-point))
130 (condition-case err
131 ;; (1) Need the extra \\( \\) around copyright-regexp because we
132 ;; goto (match-end 1) below. See note (2) below.
133 (copyright-re-search (concat "\\(" copyright-regexp
134 "\\)\\([ \t]*\n\\)?.*\\(?:"
135 copyright-names-regexp "\\)")
136 (copyright-limit)
137 t)
138 ;; In case the regexp is rejected. This is useful because
139 ;; copyright-update is typically called from before-save-hook where
140 ;; such an error is very inconvenient for the user.
141 (error (message "Can't update copyright: %s" err) nil)))
142
143 (defun copyright-update-year (replace noquery)
144 ;; This uses the match-data from copyright-find-copyright.
145 (goto-char (match-end 1))
146 ;; If the years are continued onto multiple lines
147 ;; that are marked as comments, skip to the end of the years anyway.
148 (while (save-excursion
149 (and (eq (following-char) ?,)
150 (progn (forward-char 1) t)
151 (progn (skip-chars-forward " \t") (eolp))
152 comment-start-skip
153 (save-match-data
154 (forward-line 1)
155 (and (looking-at comment-start-skip)
156 (goto-char (match-end 0))))
157 (looking-at-p copyright-years-regexp)))
158 (forward-line 1)
159 (re-search-forward comment-start-skip)
160 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
161 ;; they are at note (1) above.
162 (re-search-forward (format "\\(%s\\)" copyright-years-regexp)))
163
164 ;; Note that `current-time-string' isn't locale-sensitive.
165 (setq copyright-current-year (substring (current-time-string) -4))
166 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
167 (substring copyright-current-year -2))
168 (if (or noquery
169 (save-window-excursion
170 (switch-to-buffer (current-buffer))
171 ;; Fixes some point-moving oddness (bug#2209).
172 (save-excursion
173 (y-or-n-p (if replace
174 (concat "Replace copyright year(s) by "
175 copyright-current-year "? ")
176 (concat "Add " copyright-current-year
177 " to copyright? "))))))
178 (if replace
179 (replace-match copyright-current-year t t nil 3)
180 (let ((size (save-excursion (skip-chars-backward "0-9"))))
181 (if (and (eq (% (- (string-to-number copyright-current-year)
182 (string-to-number (buffer-substring
183 (+ (point) size)
184 (point))))
185 100)
186 1)
187 (or (eq (char-after (+ (point) size -1)) ?-)
188 (eq (char-after (+ (point) size -2)) ?-)))
189 ;; This is a range so just replace the end part.
190 (delete-char size)
191 ;; Insert a comma with the preferred number of spaces.
192 (insert
193 (save-excursion
194 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
195 (line-beginning-position) t)
196 (match-string 1)
197 ", ")))
198 ;; If people use the '91 '92 '93 scheme, do that as well.
199 (if (eq (char-after (+ (point) size -3)) ?')
200 (insert ?')))
201 ;; Finally insert the new year.
202 (insert (substring copyright-current-year size)))))))
203
204 ;;;###autoload
205 (defun copyright-update (&optional arg interactivep)
206 "Update copyright notice to indicate the current year.
207 With prefix ARG, replace the years in the notice rather than adding
208 the current year after them. If necessary, and
209 `copyright-current-gpl-version' is set, any copying permissions
210 following the copyright are updated as well.
211 If non-nil, INTERACTIVEP tells the function to behave as when it's called
212 interactively."
213 (interactive "*P\nd")
214 (when (or copyright-update interactivep)
215 (let ((noquery (or (not copyright-query)
216 (and (eq copyright-query 'function) interactivep))))
217 (save-excursion
218 (save-restriction
219 ;; If names-regexp doesn't match, we should not mess with
220 ;; the years _or_ the GPL version.
221 (when (copyright-find-copyright)
222 (copyright-update-year arg noquery)
223 (goto-char (copyright-start-point))
224 (and copyright-current-gpl-version
225 ;; Match the GPL version comment in .el files.
226 ;; This is sensitive to line-breaks. :(
227 (copyright-re-search
228 "the Free Software Foundation[,;\n].*either version \
229 \\([0-9]+\\)\\(?: of the License\\)?, or[ \n].*any later version"
230 (copyright-limit) t)
231 ;; Don't update if the file is already using a more recent
232 ;; version than the "current" one.
233 (< (string-to-number (match-string 1))
234 (string-to-number copyright-current-gpl-version))
235 (or noquery
236 (save-match-data
237 (goto-char (match-end 1))
238 (save-window-excursion
239 (switch-to-buffer (current-buffer))
240 (y-or-n-p
241 (format "Replace GPL version %s with version %s? "
242 (match-string-no-properties 1)
243 copyright-current-gpl-version)))))
244 (replace-match copyright-current-gpl-version t t nil 1))))
245 (set (make-local-variable 'copyright-update) nil)))
246 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
247 nil))
248
249
250 ;; FIXME should be within 50 years of present (cf calendar).
251 ;;;###autoload
252 (defun copyright-fix-years ()
253 "Convert 2 digit years to 4 digit years.
254 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
255 (interactive)
256 (if (copyright-find-copyright)
257 (let ((s (match-beginning 2))
258 (e (copy-marker (1+ (match-end 2))))
259 (p (make-marker))
260 last)
261 (goto-char s)
262 (while (re-search-forward "[0-9]+" e t)
263 (set-marker p (point))
264 (goto-char (match-beginning 0))
265 (let ((sep (char-before))
266 (year (string-to-number (match-string 0))))
267 (when (and sep
268 (/= (char-syntax sep) ?\s)
269 (/= sep ?-))
270 (insert " "))
271 (when (< year 100)
272 (insert (if (>= year 50) "19" "20"))))
273 (goto-char p)
274 (setq last p))
275 (when last
276 (goto-char last)
277 ;; Don't mess up whitespace after the years.
278 (skip-chars-backward " \t")
279 (save-restriction
280 (narrow-to-region (copyright-start-point) (point))
281 (let ((fill-prefix " "))
282 (fill-region s last))))
283 (set-marker e nil)
284 (set-marker p nil)
285 (copyright-update nil t))
286 (message "No copyright message")))
287
288 ;;;###autoload
289 (define-skeleton copyright
290 "Insert a copyright by $ORGANIZATION notice at cursor."
291 "Company: "
292 comment-start
293 "Copyright (C) " `(substring (current-time-string) -4) " by "
294 (or (getenv "ORGANIZATION")
295 str)
296 '(if (copyright-offset-too-large-p)
297 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
298 comment-end \n)
299
300 ;;;###autoload
301 (defun copyright-update-directory (directory match)
302 "Update copyright notice for all files in DIRECTORY matching MATCH."
303 (interactive "DDirectory: \nMFilenames matching (regexp): ")
304 (dolist (file (directory-files directory t match nil))
305 (message "Updating file `%s'" file)
306 (find-file file)
307 (let ((copyright-query nil))
308 (copyright-update))
309 (save-buffer)
310 (kill-buffer (current-buffer))))
311
312 (provide 'copyright)
313
314 ;; For the copyright sign:
315 ;; Local Variables:
316 ;; coding: utf-8
317 ;; End:
318
319 ;;; copyright.el ends here