1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2003, 2004, 2005
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."
57 (defcustom copyright-years-regexp
58 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
59 "*Match additional copyright notice years.
60 The second \\( \\) construct must match the years."
65 (defcustom copyright-query
'function
66 "*If non-nil, ask user before changing copyright.
67 When this is `function', only ask when called non-interactively."
69 :type
'(choice (const :tag
"Do not ask")
70 (const :tag
"Ask unless interactive" function
)
71 (other :tag
"Ask" t
)))
74 ;; when modifying this, also modify the comment generated by autoinsert.el
75 (defconst copyright-current-gpl-version
"2"
76 "String representing the current version of the GPL or nil.")
78 (defvar copyright-update t
)
80 ;; This is a defvar rather than a defconst, because the year can
81 ;; change during the Emacs session.
82 (defvar copyright-current-year
(substring (current-time-string) -
4)
83 "String representing the current year.")
85 (defun copyright-update-year (replace noquery
)
86 (when (re-search-forward copyright-regexp
(+ (point) copyright-limit
) t
)
87 ;; If the years are continued onto multiple lined
88 ;; that are marked as comments, skip to the end of the years anyway.
89 (while (save-excursion
90 (and (eq (following-char) ?
,)
91 (progn (forward-char 1) t
)
92 (progn (skip-chars-forward " \t") (eolp))
96 (and (looking-at comment-start-skip
)
97 (goto-char (match-end 0))))
99 (looking-at copyright-years-regexp
))))
101 (re-search-forward comment-start-skip
)
102 (re-search-forward copyright-years-regexp
))
104 ;; Note that `current-time-string' isn't locale-sensitive.
105 (setq copyright-current-year
(substring (current-time-string) -
4))
106 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
107 (substring copyright-current-year -
2))
109 (y-or-n-p (if replace
110 (concat "Replace copyright year(s) by "
111 copyright-current-year
"? ")
112 (concat "Add " copyright-current-year
113 " to copyright? "))))
115 (replace-match copyright-current-year t t nil
1)
116 (let ((size (save-excursion (skip-chars-backward "0-9"))))
117 (if (and (eq (%
(- (string-to-number copyright-current-year
)
118 (string-to-number (buffer-substring
123 (or (eq (char-after (+ (point) size -
1)) ?-
)
124 (eq (char-after (+ (point) size -
2)) ?-
)))
125 ;; This is a range so just replace the end part.
127 ;; Insert a comma with the preferred number of spaces.
130 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
131 (line-beginning-position) t
)
134 ;; If people use the '91 '92 '93 scheme, do that as well.
135 (if (eq (char-after (+ (point) size -
3)) ?
')
137 ;; Finally insert the new year.
138 (insert (substring copyright-current-year size
))))))))
141 (defun copyright-update (&optional arg interactivep
)
142 "Update copyright notice at beginning of buffer to indicate the current year.
143 With prefix ARG, replace the years in the notice rather than adding
144 the current year after them. If necessary, and
145 `copyright-current-gpl-version' is set, any copying permissions
146 following the copyright are updated as well.
147 If non-nil, INTERACTIVEP tells the function to behave as when it's called
149 (interactive "*P\nd")
150 (when (or copyright-update interactivep
)
151 (let ((noquery (or (not copyright-query
)
152 (and (eq copyright-query
'function
) interactivep
))))
156 (goto-char (point-min))
157 (copyright-update-year arg noquery
)
158 (goto-char (point-min))
159 (and copyright-current-gpl-version
160 ;; match the GPL version comment in .el files, including the
161 ;; bilingual Esperanto one in two-column, and in texinfo.tex
162 (re-search-forward "\\(the Free Software Foundation;\
163 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
164 version \\([0-9]+\\), or (at"
165 (+ (point) copyright-limit
) t
)
166 (not (string= (match-string 3) copyright-current-gpl-version
))
168 (y-or-n-p (concat "Replace GPL version by "
169 copyright-current-gpl-version
"? ")))
172 ;; Esperanto bilingual comment in two-column.el
173 (replace-match copyright-current-gpl-version t t nil
2))
174 (replace-match copyright-current-gpl-version t t nil
3))))
175 (set (make-local-variable 'copyright-update
) nil
)))
176 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
181 (defun copyright-fix-years ()
182 "Convert 2 digit years to 4 digit years.
183 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
186 (goto-char (point-min))
187 (if (re-search-forward copyright-regexp
(+ (point) copyright-limit
) t
)
188 (let ((s (match-beginning 2)) (e (make-marker))
190 (set-marker e
(1+ (match-end 2)))
192 (while (and (< (point) (marker-position e
))
193 (re-search-forward "\\([^0-9]\\)\\([0-9]+\\)[^0-9]"
194 (marker-position e
) t
))
196 (sep (match-string 1))
197 (year (string-to-number (match-string 2))))
198 (goto-char (1+ (match-beginning 0)))
199 (unless (= (char-syntax (string-to-char sep
)) ?\s
)
202 (insert (if (>= year
50) "19" "20")))
207 (let ((fill-prefix " "))
208 (fill-region s last
))
211 (copyright-update nil t
))
212 (message "No copyright message")
213 (goto-char (point-min))))
216 (define-skeleton copyright
217 "Insert a copyright by $ORGANIZATION notice at cursor."
220 "Copyright (C) " `(substring (current-time-string) -
4) " by "
221 (or (getenv "ORGANIZATION")
223 '(if (> (point) (+ (point-min) copyright-limit
))
224 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
229 ;; For the copyright sign:
231 ;; coding: emacs-mule
234 ;;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
235 ;;; copyright.el ends here