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-regexp
47 "\\(©\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
48 \\|[Cc]opyright\\s *:?\\s *©\\)\
49 \\s *\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
50 "What your copyright notice looks like.
51 The second \\( \\) construct must match the years."
55 (defcustom copyright-names-regexp
""
56 "Regexp matching the names which correspond to the user.
57 Only copyright lines where the name matches this regexp will be updated.
58 This allows you to avoid adding years to a copyright notice belonging to
59 someone else or to a group for which you do not work."
63 (defcustom copyright-years-regexp
64 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
65 "Match additional copyright notice years.
66 The second \\( \\) construct must match the years."
71 (defcustom copyright-query
'function
72 "If non-nil, ask user before changing copyright.
73 When this is `function', only ask when called non-interactively."
75 :type
'(choice (const :tag
"Do not ask")
76 (const :tag
"Ask unless interactive" function
)
77 (other :tag
"Ask" t
)))
80 ;; when modifying this, also modify the comment generated by autoinsert.el
81 (defconst copyright-current-gpl-version
"3"
82 "String representing the current version of the GPL or nil.")
84 (defvar copyright-update t
)
86 ;; This is a defvar rather than a defconst, because the year can
87 ;; change during the Emacs session.
88 (defvar copyright-current-year
(substring (current-time-string) -
4)
89 "String representing the current year.")
91 (defsubst copyright-limit
() ; re-search-forward BOUND
92 (and copyright-limit
(+ (point) copyright-limit
)))
94 (defun copyright-update-year (replace noquery
)
97 ;; (1) Need the extra \\( \\) around copyright-regexp because we
98 ;; goto (match-end 1) below. See note (2) below.
99 (re-search-forward (concat "\\(" copyright-regexp
100 "\\)\\([ \t]*\n\\)?.*\\(?:"
101 copyright-names-regexp
"\\)")
104 ;; In case the regexp is rejected. This is useful because
105 ;; copyright-update is typically called from before-save-hook where
106 ;; such an error is very inconvenient for the user.
107 (error (message "Can't update copyright: %s" err
) nil
))
108 (goto-char (match-end 1))
109 ;; If the years are continued onto multiple lines
110 ;; that are marked as comments, skip to the end of the years anyway.
111 (while (save-excursion
112 (and (eq (following-char) ?
,)
113 (progn (forward-char 1) t
)
114 (progn (skip-chars-forward " \t") (eolp))
118 (and (looking-at comment-start-skip
)
119 (goto-char (match-end 0))))
121 (looking-at copyright-years-regexp
))))
123 (re-search-forward comment-start-skip
)
124 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
125 ;; they are at note (1) above.
126 (re-search-forward (format "\\(%s\\)" copyright-years-regexp
)))
128 ;; Note that `current-time-string' isn't locale-sensitive.
129 (setq copyright-current-year
(substring (current-time-string) -
4))
130 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
131 (substring copyright-current-year -
2))
133 (y-or-n-p (if replace
134 (concat "Replace copyright year(s) by "
135 copyright-current-year
"? ")
136 (concat "Add " copyright-current-year
137 " to copyright? "))))
139 (replace-match copyright-current-year t t nil
3)
140 (let ((size (save-excursion (skip-chars-backward "0-9"))))
141 (if (and (eq (%
(- (string-to-number copyright-current-year
)
142 (string-to-number (buffer-substring
147 (or (eq (char-after (+ (point) size -
1)) ?-
)
148 (eq (char-after (+ (point) size -
2)) ?-
)))
149 ;; This is a range so just replace the end part.
151 ;; Insert a comma with the preferred number of spaces.
154 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
155 (line-beginning-position) t
)
158 ;; If people use the '91 '92 '93 scheme, do that as well.
159 (if (eq (char-after (+ (point) size -
3)) ?
')
161 ;; Finally insert the new year.
162 (insert (substring copyright-current-year size
))))))))
165 (defun copyright-update (&optional arg interactivep
)
166 "Update copyright notice at beginning of buffer to indicate the current year.
167 With prefix ARG, replace the years in the notice rather than adding
168 the current year after them. If necessary, and
169 `copyright-current-gpl-version' is set, any copying permissions
170 following the copyright are updated as well.
171 If non-nil, INTERACTIVEP tells the function to behave as when it's called
173 (interactive "*P\nd")
174 (when (or copyright-update interactivep
)
175 (let ((noquery (or (not copyright-query
)
176 (and (eq copyright-query
'function
) interactivep
))))
180 (goto-char (point-min))
181 (copyright-update-year arg noquery
)
182 (goto-char (point-min))
183 (and copyright-current-gpl-version
184 ;; match the GPL version comment in .el files, including the
185 ;; bilingual Esperanto one in two-column, and in texinfo.tex
187 "\\(the Free Software Foundation;\
188 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
189 version \\([0-9]+\\), or (at"
191 ;; Don't update if the file is already using a more recent
192 ;; version than the "current" one.
193 (< (string-to-number (match-string 3))
194 (string-to-number copyright-current-gpl-version
))
196 (y-or-n-p (format "Replace GPL version by %s? "
197 copyright-current-gpl-version
)))
200 ;; Esperanto bilingual comment in two-column.el
201 (replace-match copyright-current-gpl-version t t nil
2))
202 (replace-match copyright-current-gpl-version t t nil
3))))
203 (set (make-local-variable 'copyright-update
) nil
)))
204 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
209 (defun copyright-fix-years ()
210 "Convert 2 digit years to 4 digit years.
211 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
214 (goto-char (point-min))
215 (if (re-search-forward copyright-regexp
(copyright-limit) t
)
216 (let ((s (match-beginning 2))
217 (e (copy-marker (1+ (match-end 2))))
221 (while (re-search-forward "[0-9]+" e t
)
222 (set-marker p
(point))
223 (goto-char (match-beginning 0))
224 (let ((sep (char-before))
225 (year (string-to-number (match-string 0))))
227 (/= (char-syntax sep
) ?\s
)
231 (insert (if (>= year
50) "19" "20"))))
236 ;; Don't mess up whitespace after the years.
237 (skip-chars-backward " \t")
239 (narrow-to-region (point-min) (point))
240 (let ((fill-prefix " "))
241 (fill-region s last
))))
244 (copyright-update nil t
))
245 (message "No copyright message")))
248 (define-skeleton copyright
249 "Insert a copyright by $ORGANIZATION notice at cursor."
252 "Copyright (C) " `(substring (current-time-string) -
4) " by "
253 (or (getenv "ORGANIZATION")
255 '(if (and copyright-limit
(> (point) (+ (point-min) copyright-limit
)))
256 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
261 ;; For the copyright sign:
266 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
267 ;;; copyright.el ends here