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 Free Software Foundation, Inc.
6 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: maint, tools
9 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
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.
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.
35 (defgroup copyright nil
36 "Update the copyright notice in current buffer."
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."
43 :type
'(choice (integer :tag
"Limit")
44 (const :tag
"No limit")))
46 (defcustom copyright-at-end-flag nil
47 "Non-nil means to search backwards from the end of the buffer for copyright.
48 This is useful for ChangeLogs."
53 (defcustom copyright-regexp
54 "\\(©\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
55 \\|[Cc]opyright\\s *:?\\s *©\\)\
56 \\s *\\([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
)
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.")
98 (defsubst copyright-limit
() ; re-search-forward BOUND
100 (if copyright-at-end-flag
101 (- (point) copyright-limit
)
102 (+ (point) copyright-limit
))))
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
)))
110 (defun copyright-start-point ()
111 "Return point-min or point-max, depending on `copyright-at-end-flag'."
112 (if copyright-at-end-flag
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
)))))
123 (defun copyright-update-year (replace noquery
)
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
"\\)")
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))
147 (and (looking-at comment-start-skip
)
148 (goto-char (match-end 0))))
149 (looking-at-p copyright-years-regexp
)))
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
)))
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))
161 (y-or-n-p (if replace
162 (concat "Replace copyright year(s) by "
163 copyright-current-year
"? ")
164 (concat "Add " copyright-current-year
165 " to copyright? "))))
167 (replace-match copyright-current-year t t nil
3)
168 (let ((size (save-excursion (skip-chars-backward "0-9"))))
169 (if (and (eq (%
(- (string-to-number copyright-current-year
)
170 (string-to-number (buffer-substring
175 (or (eq (char-after (+ (point) size -
1)) ?-
)
176 (eq (char-after (+ (point) size -
2)) ?-
)))
177 ;; This is a range so just replace the end part.
179 ;; Insert a comma with the preferred number of spaces.
182 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
183 (line-beginning-position) t
)
186 ;; If people use the '91 '92 '93 scheme, do that as well.
187 (if (eq (char-after (+ (point) size -
3)) ?
')
189 ;; Finally insert the new year.
190 (insert (substring copyright-current-year size
))))))))
193 (defun copyright-update (&optional arg interactivep
)
194 "Update copyright notice at beginning of buffer to indicate the current year.
195 With prefix ARG, replace the years in the notice rather than adding
196 the current year after them. If necessary, and
197 `copyright-current-gpl-version' is set, any copying permissions
198 following the copyright are updated as well.
199 If non-nil, INTERACTIVEP tells the function to behave as when it's called
201 (interactive "*P\nd")
202 (when (or copyright-update interactivep
)
203 (let ((noquery (or (not copyright-query
)
204 (and (eq copyright-query
'function
) interactivep
))))
208 (goto-char (copyright-start-point))
209 (copyright-update-year arg noquery
)
210 (goto-char (copyright-start-point))
211 (and copyright-current-gpl-version
212 ;; match the GPL version comment in .el files, including the
213 ;; bilingual Esperanto one in two-column, and in texinfo.tex
215 "\\(the Free Software Foundation;\
216 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
217 version \\([0-9]+\\), or (at"
219 ;; Don't update if the file is already using a more recent
220 ;; version than the "current" one.
221 (< (string-to-number (match-string 3))
222 (string-to-number copyright-current-gpl-version
))
224 (y-or-n-p (format "Replace GPL version by %s? "
225 copyright-current-gpl-version
)))
228 ;; Esperanto bilingual comment in two-column.el
229 (replace-match copyright-current-gpl-version t t nil
2))
230 (replace-match copyright-current-gpl-version t t nil
3))))
231 (set (make-local-variable 'copyright-update
) nil
)))
232 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
237 (defun copyright-fix-years ()
238 "Convert 2 digit years to 4 digit years.
239 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
242 (goto-char (copyright-start-point))
243 (if (copyright-re-search copyright-regexp
(copyright-limit) t
)
244 (let ((s (match-beginning 2))
245 (e (copy-marker (1+ (match-end 2))))
249 (while (re-search-forward "[0-9]+" e t
)
250 (set-marker p
(point))
251 (goto-char (match-beginning 0))
252 (let ((sep (char-before))
253 (year (string-to-number (match-string 0))))
255 (/= (char-syntax sep
) ?\s
)
259 (insert (if (>= year
50) "19" "20"))))
264 ;; Don't mess up whitespace after the years.
265 (skip-chars-backward " \t")
267 (narrow-to-region (copyright-start-point) (point))
268 (let ((fill-prefix " "))
269 (fill-region s last
))))
272 (copyright-update nil t
))
273 (message "No copyright message")))
276 (define-skeleton copyright
277 "Insert a copyright by $ORGANIZATION notice at cursor."
280 "Copyright (C) " `(substring (current-time-string) -
4) " by "
281 (or (getenv "ORGANIZATION")
283 '(if (copyright-offset-too-large-p)
284 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
287 (defun copyright-update-directory (directory match
)
288 "Update copyright notice for all files in DIRECTORY matching MATCH."
289 (interactive "DDirectory: \nMFilenames matching: ")
290 (dolist (file (directory-files directory t match nil
))
292 (let ((copyright-query nil
))
295 (kill-buffer (current-buffer))))
299 ;; For the copyright sign:
304 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
305 ;;; copyright.el ends here