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 2, 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 ;; The character classes have the Latin-1 version and the Latin-9
47 ;; version, which is probably enough.
48 (defcustom copyright-regexp
49 "\\([\81©\8e©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
50 \\|[Cc]opyright\\s *:?\\s *[\81©\8e©]\\)\
51 \\s *\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
52 "What your copyright notice looks like.
53 The second \\( \\) construct must match the years."
57 (defcustom copyright-names-regexp
""
58 "Regexp matching the names which correspond to the user.
59 Only copyright lines where the name matches this regexp will be updated.
60 This allows you to avoid adding yars to a copyright notice belonging to
61 someone else or to a group for which you do not work."
64 (defcustom copyright-years-regexp
65 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
66 "Match additional copyright notice years.
67 The second \\( \\) construct must match the years."
72 (defcustom copyright-query
'function
73 "If non-nil, ask user before changing copyright.
74 When this is `function', only ask when called non-interactively."
76 :type
'(choice (const :tag
"Do not ask")
77 (const :tag
"Ask unless interactive" function
)
78 (other :tag
"Ask" t
)))
81 ;; when modifying this, also modify the comment generated by autoinsert.el
82 (defconst copyright-current-gpl-version
"3"
83 "String representing the current version of the GPL or nil.")
85 (defvar copyright-update t
)
87 ;; This is a defvar rather than a defconst, because the year can
88 ;; change during the Emacs session.
89 (defvar copyright-current-year
(substring (current-time-string) -
4)
90 "String representing the current year.")
92 (defun copyright-update-year (replace noquery
)
95 (re-search-forward (concat "\\(" copyright-regexp
96 "\\)\\([ \t]*\n\\)?.*\\(?:"
97 copyright-names-regexp
"\\)")
98 (+ (point) copyright-limit
) t
)
99 ;; In case the regexp is rejected. This is useful because
100 ;; copyright-update is typically called from before-save-hook where
101 ;; such an error is very inconvenient for the user.
102 (error (message "Can't update copyright: %s" err
) nil
))
103 (goto-char (match-end 1))
104 ;; If the years are continued onto multiple lined
105 ;; that are marked as comments, skip to the end of the years anyway.
106 (while (save-excursion
107 (and (eq (following-char) ?
,)
108 (progn (forward-char 1) t
)
109 (progn (skip-chars-forward " \t") (eolp))
113 (and (looking-at comment-start-skip
)
114 (goto-char (match-end 1))))
116 (looking-at copyright-years-regexp
))))
118 (re-search-forward comment-start-skip
)
119 (re-search-forward copyright-years-regexp
))
121 ;; Note that `current-time-string' isn't locale-sensitive.
122 (setq copyright-current-year
(substring (current-time-string) -
4))
123 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
124 (substring copyright-current-year -
2))
126 (y-or-n-p (if replace
127 (concat "Replace copyright year(s) by "
128 copyright-current-year
"? ")
129 (concat "Add " copyright-current-year
130 " to copyright? "))))
132 (replace-match copyright-current-year t t nil
2)
133 (let ((size (save-excursion (skip-chars-backward "0-9"))))
134 (if (and (eq (%
(- (string-to-number copyright-current-year
)
135 (string-to-number (buffer-substring
140 (or (eq (char-after (+ (point) size -
1)) ?-
)
141 (eq (char-after (+ (point) size -
2)) ?-
)))
142 ;; This is a range so just replace the end part.
144 ;; Insert a comma with the preferred number of spaces.
147 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
148 (line-beginning-position) t
)
151 ;; If people use the '91 '92 '93 scheme, do that as well.
152 (if (eq (char-after (+ (point) size -
3)) ?
')
154 ;; Finally insert the new year.
155 (insert (substring copyright-current-year size
))))))))
158 (defun copyright-update (&optional arg interactivep
)
159 "Update copyright notice at beginning of buffer to indicate the current year.
160 With prefix ARG, replace the years in the notice rather than adding
161 the current year after them. If necessary, and
162 `copyright-current-gpl-version' is set, any copying permissions
163 following the copyright are updated as well.
164 If non-nil, INTERACTIVEP tells the function to behave as when it's called
166 (interactive "*P\nd")
167 (when (or copyright-update interactivep
)
168 (let ((noquery (or (not copyright-query
)
169 (and (eq copyright-query
'function
) interactivep
))))
173 (goto-char (point-min))
174 (copyright-update-year arg noquery
)
175 (goto-char (point-min))
176 (and copyright-current-gpl-version
177 ;; match the GPL version comment in .el files, including the
178 ;; bilingual Esperanto one in two-column, and in texinfo.tex
179 (re-search-forward "\\(the Free Software Foundation;\
180 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
181 version \\([0-9]+\\), or (at"
182 (+ (point) copyright-limit
) t
)
183 (not (string= (match-string 3) copyright-current-gpl-version
))
185 (y-or-n-p (concat "Replace GPL version by "
186 copyright-current-gpl-version
"? ")))
189 ;; Esperanto bilingual comment in two-column.el
190 (replace-match copyright-current-gpl-version t t nil
2))
191 (replace-match copyright-current-gpl-version t t nil
3))))
192 (set (make-local-variable 'copyright-update
) nil
)))
193 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
198 (defun copyright-fix-years ()
199 "Convert 2 digit years to 4 digit years.
200 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
203 (goto-char (point-min))
204 (if (re-search-forward copyright-regexp
(+ (point) copyright-limit
) t
)
205 (let ((s (match-beginning 2))
206 (e (copy-marker (1+ (match-end 2))))
210 (while (re-search-forward "[0-9]+" e t
)
211 (set-marker p
(point))
212 (goto-char (match-beginning 0))
213 (let ((sep (char-before))
214 (year (string-to-number (match-string 0))))
216 (/= (char-syntax sep
) ?\s
)
220 (insert (if (>= year
50) "19" "20"))))
225 ;; Don't mess up whitespace after the years.
226 (skip-chars-backward " \t")
228 (narrow-to-region (point-min) (point))
229 (let ((fill-prefix " "))
230 (fill-region s last
))))
233 (copyright-update nil t
))
234 (message "No copyright message")))
237 (define-skeleton copyright
238 "Insert a copyright by $ORGANIZATION notice at cursor."
241 "Copyright (C) " `(substring (current-time-string) -
4) " by "
242 (or (getenv "ORGANIZATION")
244 '(if (> (point) (+ (point-min) copyright-limit
))
245 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
250 ;; For the copyright sign:
252 ;; coding: emacs-mule
255 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
256 ;;; copyright.el ends here