1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 1998, 2001, 2003, 2004
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 '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 "\\([�©Ž©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
50 \\|[Cc]opyright\\s *:?\\s *[�©Ž©]\\)\
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."
58 (defcustom copyright-query
'function
59 "*If non-nil, ask user before changing copyright.
60 When this is `function', only ask when called non-interactively."
62 :type
'(choice (const :tag
"Do not ask")
63 (const :tag
"Ask unless interactive" function
)
64 (other :tag
"Ask" t
)))
67 ;; when modifying this, also modify the comment generated by autoinsert.el
68 (defconst copyright-current-gpl-version
"2"
69 "String representing the current version of the GPL or nil.")
71 (defvar copyright-update t
)
73 ;; This is a defvar rather than a defconst, because the year can
74 ;; change during the Emacs session.
75 (defvar copyright-current-year
(substring (current-time-string) -
4)
76 "String representing the current year.")
78 (defun copyright-update-year (replace noquery
)
79 (when (re-search-forward copyright-regexp
(+ (point) copyright-limit
) t
)
80 ;; Note that `current-time-string' isn't locale-sensitive.
81 (setq copyright-current-year
(substring (current-time-string) -
4))
82 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
83 (substring copyright-current-year -
2))
86 (concat "Replace copyright year(s) by "
87 copyright-current-year
"? ")
88 (concat "Add " copyright-current-year
91 (replace-match copyright-current-year t t nil
1)
92 (let ((size (save-excursion (skip-chars-backward "0-9"))))
93 (if (and (eq (%
(- (string-to-number copyright-current-year
)
94 (string-to-number (buffer-substring
99 (or (eq (char-after (+ (point) size -
1)) ?-
)
100 (eq (char-after (+ (point) size -
2)) ?-
)))
101 ;; This is a range so just replace the end part.
103 ;; Detect if this is using the following shorthand:
104 ;; (C) 1993, 94, 95, 1998, 2000, 01, 02, 2003
106 ;; Check that the last year was 4-chars and same century.
108 (equal (buffer-substring (- (point) 4) (- (point) 2))
109 (substring copyright-current-year
0 2))
110 ;; Check that there are 2-char years as well.
112 (re-search-backward "[^0-9][0-9][0-9][^0-9]"
113 (line-beginning-position) t
))
114 ;; Make sure we don't remove the first century marker.
118 (concat (buffer-substring (point) (+ (point) 2))
120 (line-beginning-position) t
)))
121 ;; Remove the century marker of the last entry.
122 (delete-region (- (point) 4) (- (point) 2)))
123 ;; Insert a comma with the preferred number of spaces.
126 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
127 (line-beginning-position) t
)
130 ;; If people use the '91 '92 '93 scheme, do that as well.
131 (if (eq (char-after (+ (point) size -
3)) ?
')
133 ;; Finally insert the new year.
134 (insert (substring copyright-current-year size
))))))))
137 (defun copyright-update (&optional arg interactivep
)
138 "Update copyright notice at beginning of buffer to indicate the current year.
139 With prefix ARG, replace the years in the notice rather than adding
140 the current year after them. If necessary, and
141 `copyright-current-gpl-version' is set, any copying permissions
142 following the copyright are updated as well.
143 If non-nil, INTERACTIVEP tells the function to behave as when it's called
145 (interactive "*P\nd")
146 (when (or copyright-update interactivep
)
147 (let ((noquery (or (not copyright-query
)
148 (and (eq copyright-query
'function
) interactivep
))))
152 (goto-char (point-min))
153 (copyright-update-year arg noquery
)
154 (goto-char (point-min))
155 (and copyright-current-gpl-version
156 ;; match the GPL version comment in .el files, including the
157 ;; bilingual Esperanto one in two-column, and in texinfo.tex
158 (re-search-forward "\\(the Free Software Foundation;\
159 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
160 version \\([0-9]+\\), or (at"
161 (+ (point) copyright-limit
) t
)
162 (not (string= (match-string 3) copyright-current-gpl-version
))
164 (y-or-n-p (concat "Replace GPL version by "
165 copyright-current-gpl-version
"? ")))
168 ;; Esperanto bilingual comment in two-column.el
169 (replace-match copyright-current-gpl-version t t nil
2))
170 (replace-match copyright-current-gpl-version t t nil
3))))
171 (set (make-local-variable 'copyright-update
) nil
)))
172 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
177 (define-skeleton copyright
178 "Insert a copyright by $ORGANIZATION notice at cursor."
181 "Copyright (C) " `(substring (current-time-string) -
4) " by "
182 (or (getenv "ORGANIZATION")
184 '(if (> (point) (+ (point-min) copyright-limit
))
185 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
190 ;; For the copyright sign:
192 ;; coding: emacs-mule
195 ;;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
196 ;;; copyright.el ends here