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 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 (re-search-forward (concat "\\(" copyright-regexp
98 "\\)\\([ \t]*\n\\)?.*\\(?:"
99 copyright-names-regexp "\\)")
102 ;; In case the regexp is rejected. This is useful because
103 ;; copyright-update is typically called from before-save-hook where
104 ;; such an error is very inconvenient for the user.
105 (error (message "Can't update copyright: %s" err) nil))
106 (goto-char (match-end 1))
107 ;; If the years are continued onto multiple lined
108 ;; that are marked as comments, skip to the end of the years anyway.
109 (while (save-excursion
110 (and (eq (following-char) ?,)
111 (progn (forward-char 1) t)
112 (progn (skip-chars-forward " \t") (eolp))
116 (and (looking-at comment-start-skip)
117 (goto-char (match-end 0))))
119 (looking-at copyright-years-regexp))))
121 (re-search-forward comment-start-skip)
122 (re-search-forward copyright-years-regexp))
124 ;; Note that `current-time-string' isn't locale-sensitive.
125 (setq copyright-current-year (substring (current-time-string) -4))
126 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
127 (substring copyright-current-year -2))
129 (y-or-n-p (if replace
130 (concat "Replace copyright year(s) by "
131 copyright-current-year "? ")
132 (concat "Add " copyright-current-year
133 " to copyright? "))))
135 (replace-match copyright-current-year t t nil 2)
136 (let ((size (save-excursion (skip-chars-backward "0-9"))))
137 (if (and (eq (% (- (string-to-number copyright-current-year)
138 (string-to-number (buffer-substring
143 (or (eq (char-after (+ (point) size -1)) ?-)
144 (eq (char-after (+ (point) size -2)) ?-)))
145 ;; This is a range so just replace the end part.
147 ;; Insert a comma with the preferred number of spaces.
150 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
151 (line-beginning-position) t)
154 ;; If people use the '91 '92 '93 scheme, do that as well.
155 (if (eq (char-after (+ (point) size -3)) ?')
157 ;; Finally insert the new year.
158 (insert (substring copyright-current-year size))))))))
161 (defun copyright-update (&optional arg interactivep)
162 "Update copyright notice at beginning of buffer to indicate the current year.
163 With prefix ARG, replace the years in the notice rather than adding
164 the current year after them. If necessary, and
165 `copyright-current-gpl-version' is set, any copying permissions
166 following the copyright are updated as well.
167 If non-nil, INTERACTIVEP tells the function to behave as when it's called
169 (interactive "*P\nd")
170 (when (or copyright-update interactivep)
171 (let ((noquery (or (not copyright-query)
172 (and (eq copyright-query 'function) interactivep))))
176 (goto-char (point-min))
177 (copyright-update-year arg noquery)
178 (goto-char (point-min))
179 (and copyright-current-gpl-version
180 ;; match the GPL version comment in .el files, including the
181 ;; bilingual Esperanto one in two-column, and in texinfo.tex
183 "\\(the Free Software Foundation;\
184 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
185 version \\([0-9]+\\), or (at"
187 ;; Don't update if the file is already using a more recent
188 ;; version than the "current" one.
189 (< (string-to-number (match-string 3))
190 (string-to-number copyright-current-gpl-version))
192 (y-or-n-p (format "Replace GPL version by %s? "
193 copyright-current-gpl-version)))
196 ;; Esperanto bilingual comment in two-column.el
197 (replace-match copyright-current-gpl-version t t nil 2))
198 (replace-match copyright-current-gpl-version t t nil 3))))
199 (set (make-local-variable 'copyright-update) nil)))
200 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
205 (defun copyright-fix-years ()
206 "Convert 2 digit years to 4 digit years.
207 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
210 (goto-char (point-min))
211 (if (re-search-forward copyright-regexp (copyright-limit) t)
212 (let ((s (match-beginning 2))
213 (e (copy-marker (1+ (match-end 2))))
217 (while (re-search-forward "[0-9]+" e t)
218 (set-marker p (point))
219 (goto-char (match-beginning 0))
220 (let ((sep (char-before))
221 (year (string-to-number (match-string 0))))
223 (/= (char-syntax sep) ?\s)
227 (insert (if (>= year 50) "19" "20"))))
232 ;; Don't mess up whitespace after the years.
233 (skip-chars-backward " \t")
235 (narrow-to-region (point-min) (point))
236 (let ((fill-prefix " "))
237 (fill-region s last))))
240 (copyright-update nil t))
241 (message "No copyright message")))
244 (define-skeleton copyright
245 "Insert a copyright by $ORGANIZATION notice at cursor."
248 "Copyright (C) " `(substring (current-time-string) -4) " by "
249 (or (getenv "ORGANIZATION")
251 '(if (and copyright-limit (> (point) (+ (point-min) copyright-limit)))
252 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
257 ;; For the copyright sign:
262 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
263 ;;; copyright.el ends here