(copyright-fix-years): Match properly if the first year is 2-digit.
[bpt/emacs.git] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: maint, tools
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.
32
33 ;;; Code:
34
35 (defgroup copyright nil
36 "Update the copyright notice in current buffer."
37 :group 'tools)
38
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."
42 :group 'copyright
43 :type '(choice (integer :tag "Limit")
44 (const :tag "No limit")))
45
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."
54 :group 'copyright
55 :type 'regexp)
56
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."
61 :group 'copyright
62 :type 'regexp)
63
64
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."
68 :group 'copyright
69 :type '(choice (const :tag "Do not ask")
70 (const :tag "Ask unless interactive" function)
71 (other :tag "Ask" t)))
72
73
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.")
77
78 (defvar copyright-update t)
79
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.")
84
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))
93 comment-start-skip
94 (save-match-data
95 (forward-line 1)
96 (and (looking-at comment-start-skip)
97 (goto-char (match-end 0))))
98 (save-match-data
99 (looking-at copyright-years-regexp))))
100 (forward-line 1)
101 (re-search-forward comment-start-skip)
102 (re-search-forward copyright-years-regexp))
103
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))
108 (if (or noquery
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? "))))
114 (if replace
115 (replace-match copyright-current-year t t nil 2)
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
119 (+ (point) size)
120 (point))))
121 100)
122 1)
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.
126 (delete-char size)
127 ;; Insert a comma with the preferred number of spaces.
128 (insert
129 (save-excursion
130 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
131 (line-beginning-position) t)
132 (match-string 1)
133 ", ")))
134 ;; If people use the '91 '92 '93 scheme, do that as well.
135 (if (eq (char-after (+ (point) size -3)) ?')
136 (insert ?')))
137 ;; Finally insert the new year.
138 (insert (substring copyright-current-year size))))))))
139
140 ;;;###autoload
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
148 interactively."
149 (interactive "*P\nd")
150 (when (or copyright-update interactivep)
151 (let ((noquery (or (not copyright-query)
152 (and (eq copyright-query 'function) interactivep))))
153 (save-excursion
154 (save-restriction
155 (widen)
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))
167 (or noquery
168 (y-or-n-p (concat "Replace GPL version by "
169 copyright-current-gpl-version "? ")))
170 (progn
171 (if (match-end 2)
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.
177 nil))
178
179
180 ;;;###autoload
181 (defun copyright-fix-years ()
182 "Convert 2 digit years to 4 digit years.
183 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
184 (interactive)
185 (widen)
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))
189 last)
190 (set-marker e (1+ (match-end 2)))
191 (goto-char s)
192 ;; Back up one character so that our search can match the first year.
193 (backward-char 1)
194 (while (and (< (point) (marker-position e))
195 (re-search-forward "\\([^0-9]\\)\\([0-9]+\\)[^0-9]"
196 (marker-position e) t))
197 (let ((p (point))
198 (sep (match-string 1))
199 (year (string-to-number (match-string 2))))
200 (goto-char (1+ (match-beginning 0)))
201 (unless (= (char-syntax (string-to-char sep)) ?\s)
202 (insert " "))
203 (if (< year 100)
204 (insert (if (>= year 50) "19" "20")))
205 (goto-char p)
206 (setq last p)))
207 (when last
208 (goto-char last)
209 ;; Don't mess up whitespace after the years.
210 (skip-chars-backward " \t")
211 (save-restriction
212 (narrow-to-region (point-min) (point))
213 (let ((fill-prefix " "))
214 (fill-region s last)))
215 )
216 (set-marker e nil)
217 (copyright-update nil t))
218 (message "No copyright message")
219 (goto-char (point-min))))
220
221 ;;;###autoload
222 (define-skeleton copyright
223 "Insert a copyright by $ORGANIZATION notice at cursor."
224 "Company: "
225 comment-start
226 "Copyright (C) " `(substring (current-time-string) -4) " by "
227 (or (getenv "ORGANIZATION")
228 str)
229 '(if (> (point) (+ (point-min) copyright-limit))
230 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
231 comment-end \n)
232
233 (provide 'copyright)
234
235 ;; For the copyright sign:
236 ;; Local Variables:
237 ;; coding: emacs-mule
238 ;; End:
239
240 ;;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
241 ;;; copyright.el ends here