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