1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 1998, 2001, 2003
4 ;; 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Allows updating the copyright year and above mentioned GPL version manually
29 ;; or when saving a file.
30 ;; Do (add-hook 'write-file-functions 'copyright-update).
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 ;; Would it be cleaner to specify Latin-1 coding for this file,
46 ;; and not use both unibyte and multibyte copyright symbol characters?
48 ;; The character classes include the unibyte (C) sign,
49 ;; the Latin-1 version, and the Latin-9 version.
50 (defcustom copyright-regexp
51 "\\([
\81©
\8e©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
52 \\|[Cc]opyright\\s *:?\\s *[
\81©
\8e©]\\)\
53 \\s *\\([1-9]\\([-0-9, ';\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
54 "*What your copyright notice looks like.
55 The second \\( \\) construct must match the years."
60 (defcustom copyright-query 'function
61 "*If non-nil, ask user before changing copyright.
62 When this is `function', only ask when called non-interactively."
64 :type '(choice (const :tag "Do not ask")
65 (const :tag "Ask unless interactive" function)
66 (other :tag "Ask" t)))
69 ;; when modifying this, also modify the comment generated by autoinsert.el
70 (defconst copyright-current-gpl-version "2"
71 "String representing the current version of the GPL or nil.")
73 (defvar copyright-update t)
75 ;; This is a defvar rather than a defconst, because the year can
76 ;; change during the Emacs session.
77 (defvar copyright-current-year (substring (current-time-string) -4)
78 "String representing the current year.")
80 (defun copyright-update-year (replace noquery)
81 (when (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
82 ;; Note that `current-time-string' isn't locale-sensitive.
83 (setq copyright-current-year (substring (current-time-string) -4))
84 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
85 (substring copyright-current-year -2))
88 (concat "Replace copyright year(s) by "
89 copyright-current-year "? ")
90 (concat "Add " copyright-current-year
93 (replace-match copyright-current-year t t nil 1)
94 (let ((size (save-excursion (skip-chars-backward "0-9"))))
95 (if (and (eq (% (- (string-to-number copyright-current-year)
96 (string-to-number (buffer-substring
101 (or (eq (char-after (+ (point) size -1)) ?-)
102 (eq (char-after (+ (point) size -2)) ?-)))
103 ;; This is a range so just replace the end part.
105 ;; Detect if this is using the following shorthand:
106 ;; (C) 1993, 94, 95, 1998, 2000, 01, 02, 2003
108 ;; Check that the last year was 4-chars and same century.
110 (equal (buffer-substring (- (point) 4) (- (point) 2))
111 (substring copyright-current-year 0 2))
112 ;; Check that there are 2-char years as well.
114 (re-search-backward "[^0-9][0-9][0-9][^0-9]"
115 (line-beginning-position) t))
116 ;; Make sure we don't remove the first century marker.
120 (concat (buffer-substring (point) (+ (point) 2))
122 (line-beginning-position) t)))
123 ;; Remove the century marker of the last entry.
124 (delete-region (- (point) 4) (- (point) 2)))
125 ;; Insert a comma with the preferred number of spaces.
128 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
129 (line-beginning-position) t)
132 ;; If people use the '91 '92 '93 scheme, do that as well.
133 (if (eq (char-after (+ (point) size -3)) ?')
135 ;; Finally insert the new year.
136 (insert (substring copyright-current-year size))))))))
139 (defun copyright-update (&optional arg interactivep)
140 "Update copyright notice at beginning of buffer to indicate the current year.
141 With prefix ARG, replace the years in the notice rather than adding
142 the current year after them. If necessary, and
143 `copyright-current-gpl-version' is set, any copying permissions
144 following the copyright are updated as well.
145 If non-nil, INTERACTIVEP tells the function to behave as when it's called
147 (interactive "*P\nd")
148 (when (or copyright-update interactivep)
149 (let ((noquery (or (not copyright-query)
150 (and (eq copyright-query 'function) interactivep))))
154 (goto-char (point-min))
155 (copyright-update-year arg noquery)
156 (goto-char (point-min))
157 (and copyright-current-gpl-version
158 ;; match the GPL version comment in .el files, including the
159 ;; bilingual Esperanto one in two-column, and in texinfo.tex
160 (re-search-forward "\\(the Free Software Foundation;\
161 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
162 version \\([0-9]+\\), or (at"
163 (+ (point) copyright-limit) t)
164 (not (string= (match-string 3) copyright-current-gpl-version))
166 (y-or-n-p (concat "Replace GPL version by "
167 copyright-current-gpl-version "? ")))
170 ;; Esperanto bilingual comment in two-column.el
171 (replace-match copyright-current-gpl-version t t nil 2))
172 (replace-match copyright-current-gpl-version t t nil 3))))
173 (set (make-local-variable 'copyright-update) nil)))
174 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
179 (define-skeleton copyright
180 "Insert a copyright by $ORGANIZATION notice at cursor."
183 "Copyright (C) " `(substring (current-time-string) -4) " by "
184 (or (getenv "ORGANIZATION")
186 '(if (> (point) (+ (point-min) copyright-limit))
187 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
192 ;; For the copyright sign:
194 ;; coding: emacs-mule
197 ;;; copyright.el ends here