1 ;;; copyright.el --- update the copyright notice in current buffer
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.
7 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Keywords: maint, tools
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
34 (defgroup copyright nil
35 "Update the copyright notice in current buffer."
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."
42 :type
'(choice (integer :tag
"Limit")
43 (const :tag
"No limit")))
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."
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."
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."
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."
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."
82 :type
'(choice (const :tag
"Do not ask")
83 (const :tag
"Ask unless interactive" function
)
84 (other :tag
"Ask" t
)))
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.")
91 (defvar copyright-update t
92 "The function `copyright-update' sets this to nil after updating a buffer.")
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.")
99 (defsubst copyright-limit
() ; re-search-forward BOUND
101 (if copyright-at-end-flag
102 (- (point) copyright-limit
)
103 (+ (point) copyright-limit
))))
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
)))
111 (defun copyright-start-point ()
112 "Return point-min or point-max, depending on `copyright-at-end-flag'."
113 (if copyright-at-end-flag
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
)))))
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."
129 ;; (1) Need the extra \\( \\) around copyright-regexp because we
130 ;; goto (match-end 1) below. See note (2) below.
131 (copyright-re-search (concat "\\(" copyright-regexp
132 "\\)\\([ \t]*\n\\)?.*\\(?:"
133 copyright-names-regexp
"\\)")
136 ;; In case the regexp is rejected. This is useful because
137 ;; copyright-update is typically called from before-save-hook where
138 ;; such an error is very inconvenient for the user.
139 (error (message "Can't update copyright: %s" err
) nil
)))
141 (defun copyright-update-year (replace noquery
)
142 ;; This uses the match-data from copyright-find-copyright.
143 (goto-char (match-end 1))
144 ;; If the years are continued onto multiple lines
145 ;; that are marked as comments, skip to the end of the years anyway.
146 (while (save-excursion
147 (and (eq (following-char) ?
,)
148 (progn (forward-char 1) t
)
149 (progn (skip-chars-forward " \t") (eolp))
153 (and (looking-at comment-start-skip
)
154 (goto-char (match-end 0))))
155 (looking-at-p copyright-years-regexp
)))
157 (re-search-forward comment-start-skip
)
158 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
159 ;; they are at note (1) above.
160 (re-search-forward (format "\\(%s\\)" copyright-years-regexp
)))
162 ;; Note that `current-time-string' isn't locale-sensitive.
163 (setq copyright-current-year
(substring (current-time-string) -
4))
164 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
165 (substring copyright-current-year -
2))
167 (save-window-excursion
168 (switch-to-buffer (current-buffer))
169 ;; Fixes some point-moving oddness (bug#2209).
171 (y-or-n-p (if replace
172 (concat "Replace copyright year(s) by "
173 copyright-current-year
"? ")
174 (concat "Add " copyright-current-year
175 " to copyright? "))))))
177 (replace-match copyright-current-year t t nil
3)
178 (let ((size (save-excursion (skip-chars-backward "0-9"))))
179 (if (and (eq (%
(- (string-to-number copyright-current-year
)
180 (string-to-number (buffer-substring
185 (or (eq (char-after (+ (point) size -
1)) ?-
)
186 (eq (char-after (+ (point) size -
2)) ?-
)))
187 ;; This is a range so just replace the end part.
189 ;; Insert a comma with the preferred number of spaces.
192 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
193 (line-beginning-position) t
)
196 ;; If people use the '91 '92 '93 scheme, do that as well.
197 (if (eq (char-after (+ (point) size -
3)) ?
')
199 ;; Finally insert the new year.
200 (insert (substring copyright-current-year size
)))))))
203 (defun copyright-update (&optional arg interactivep
)
204 "Update copyright notice to indicate the current year.
205 With prefix ARG, replace the years in the notice rather than adding
206 the current year after them. If necessary, and
207 `copyright-current-gpl-version' is set, any copying permissions
208 following the copyright are updated as well.
209 If non-nil, INTERACTIVEP tells the function to behave as when it's called
211 (interactive "*P\nd")
212 (when (or copyright-update interactivep
)
213 (let ((noquery (or (not copyright-query
)
214 (and (eq copyright-query
'function
) interactivep
))))
218 (goto-char (copyright-start-point))
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. :(
228 "the Free Software Foundation[,;\n].*either version \
229 \\([0-9]+\\)\\(?: of the License\\)?, or[ \n].*any later version"
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
))
237 (goto-char (match-end 1))
238 (save-window-excursion
239 (switch-to-buffer (current-buffer))
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.
250 ;; FIXME should be within 50 years of present (cf calendar).
252 (defun copyright-fix-years ()
253 "Convert 2 digit years to 4 digit years.
254 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
257 (goto-char (copyright-start-point))
258 (if (copyright-find-copyright)
259 (let ((s (match-beginning 2))
260 (e (copy-marker (1+ (match-end 2))))
264 (while (re-search-forward "[0-9]+" e t
)
265 (set-marker p
(point))
266 (goto-char (match-beginning 0))
267 (let ((sep (char-before))
268 (year (string-to-number (match-string 0))))
270 (/= (char-syntax sep
) ?\s
)
274 (insert (if (>= year
50) "19" "20"))))
279 ;; Don't mess up whitespace after the years.
280 (skip-chars-backward " \t")
282 (narrow-to-region (copyright-start-point) (point))
283 (let ((fill-prefix " "))
284 (fill-region s last
))))
287 (copyright-update nil t
))
288 (message "No copyright message")))
291 (define-skeleton copyright
292 "Insert a copyright by $ORGANIZATION notice at cursor."
295 "Copyright (C) " `(substring (current-time-string) -
4) " by "
296 (or (getenv "ORGANIZATION")
298 '(if (copyright-offset-too-large-p)
299 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
303 (defun copyright-update-directory (directory match
)
304 "Update copyright notice for all files in DIRECTORY matching MATCH."
305 (interactive "DDirectory: \nMFilenames matching (regexp): ")
306 (dolist (file (directory-files directory t match nil
))
307 (message "Updating file `%s'" file
)
309 (let ((copyright-query nil
))
312 (kill-buffer (current-buffer))))
316 ;; For the copyright sign:
321 ;;; copyright.el ends here