]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
Merge from emacs--devo--0
[gnu-emacs] / 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, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 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 3, 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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 (defcustom copyright-regexp
47 "\\(©\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
48 \\|[Cc]opyright\\s *:?\\s *©\\)\
49 \\s *\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
50 "What your copyright notice looks like.
51 The second \\( \\) construct must match the years."
52 :group 'copyright
53 :type 'regexp)
54
55 (defcustom copyright-names-regexp ""
56 "Regexp matching the names which correspond to the user.
57 Only copyright lines where the name matches this regexp will be updated.
58 This allows you to avoid adding years to a copyright notice belonging to
59 someone else or to a group for which you do not work."
60 :group 'copyright
61 :type 'regexp)
62
63 (defcustom copyright-years-regexp
64 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
65 "Match additional copyright notice years.
66 The second \\( \\) construct must match the years."
67 :group 'copyright
68 :type 'regexp)
69
70
71 (defcustom copyright-query 'function
72 "If non-nil, ask user before changing copyright.
73 When this is `function', only ask when called non-interactively."
74 :group 'copyright
75 :type '(choice (const :tag "Do not ask")
76 (const :tag "Ask unless interactive" function)
77 (other :tag "Ask" t)))
78
79
80 ;; when modifying this, also modify the comment generated by autoinsert.el
81 (defconst copyright-current-gpl-version "3"
82 "String representing the current version of the GPL or nil.")
83
84 (defvar copyright-update t)
85
86 ;; This is a defvar rather than a defconst, because the year can
87 ;; change during the Emacs session.
88 (defvar copyright-current-year (substring (current-time-string) -4)
89 "String representing the current year.")
90
91 (defsubst copyright-limit () ; re-search-forward BOUND
92 (and copyright-limit (+ (point) copyright-limit)))
93
94 (defun copyright-update-year (replace noquery)
95 (when
96 (condition-case err
97 (re-search-forward (concat "\\(" copyright-regexp
98 "\\)\\([ \t]*\n\\)?.*\\(?:"
99 copyright-names-regexp "\\)")
100 (copyright-limit)
101 t)
102 ;; In case the regexp is rejected. This is useful because
103 ;; copyright-update is typically called from before-save-hook where
104 ;; such an error is very inconvenient for the user.
105 (error (message "Can't update copyright: %s" err) nil))
106 (goto-char (match-end 1))
107 ;; If the years are continued onto multiple lined
108 ;; that are marked as comments, skip to the end of the years anyway.
109 (while (save-excursion
110 (and (eq (following-char) ?,)
111 (progn (forward-char 1) t)
112 (progn (skip-chars-forward " \t") (eolp))
113 comment-start-skip
114 (save-match-data
115 (forward-line 1)
116 (and (looking-at comment-start-skip)
117 (goto-char (match-end 0))))
118 (save-match-data
119 (looking-at copyright-years-regexp))))
120 (forward-line 1)
121 (re-search-forward comment-start-skip)
122 (re-search-forward copyright-years-regexp))
123
124 ;; Note that `current-time-string' isn't locale-sensitive.
125 (setq copyright-current-year (substring (current-time-string) -4))
126 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
127 (substring copyright-current-year -2))
128 (if (or noquery
129 (y-or-n-p (if replace
130 (concat "Replace copyright year(s) by "
131 copyright-current-year "? ")
132 (concat "Add " copyright-current-year
133 " to copyright? "))))
134 (if replace
135 (replace-match copyright-current-year t t nil 2)
136 (let ((size (save-excursion (skip-chars-backward "0-9"))))
137 (if (and (eq (% (- (string-to-number copyright-current-year)
138 (string-to-number (buffer-substring
139 (+ (point) size)
140 (point))))
141 100)
142 1)
143 (or (eq (char-after (+ (point) size -1)) ?-)
144 (eq (char-after (+ (point) size -2)) ?-)))
145 ;; This is a range so just replace the end part.
146 (delete-char size)
147 ;; Insert a comma with the preferred number of spaces.
148 (insert
149 (save-excursion
150 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
151 (line-beginning-position) t)
152 (match-string 1)
153 ", ")))
154 ;; If people use the '91 '92 '93 scheme, do that as well.
155 (if (eq (char-after (+ (point) size -3)) ?')
156 (insert ?')))
157 ;; Finally insert the new year.
158 (insert (substring copyright-current-year size))))))))
159
160 ;;;###autoload
161 (defun copyright-update (&optional arg interactivep)
162 "Update copyright notice at beginning of buffer to indicate the current year.
163 With prefix ARG, replace the years in the notice rather than adding
164 the current year after them. If necessary, and
165 `copyright-current-gpl-version' is set, any copying permissions
166 following the copyright are updated as well.
167 If non-nil, INTERACTIVEP tells the function to behave as when it's called
168 interactively."
169 (interactive "*P\nd")
170 (when (or copyright-update interactivep)
171 (let ((noquery (or (not copyright-query)
172 (and (eq copyright-query 'function) interactivep))))
173 (save-excursion
174 (save-restriction
175 (widen)
176 (goto-char (point-min))
177 (copyright-update-year arg noquery)
178 (goto-char (point-min))
179 (and copyright-current-gpl-version
180 ;; match the GPL version comment in .el files, including the
181 ;; bilingual Esperanto one in two-column, and in texinfo.tex
182 (re-search-forward
183 "\\(the Free Software Foundation;\
184 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
185 version \\([0-9]+\\), or (at"
186 (copyright-limit) t)
187 ;; Don't update if the file is already using a more recent
188 ;; version than the "current" one.
189 (< (string-to-number (match-string 3))
190 (string-to-number copyright-current-gpl-version))
191 (or noquery
192 (y-or-n-p (format "Replace GPL version by %s? "
193 copyright-current-gpl-version)))
194 (progn
195 (if (match-end 2)
196 ;; Esperanto bilingual comment in two-column.el
197 (replace-match copyright-current-gpl-version t t nil 2))
198 (replace-match copyright-current-gpl-version t t nil 3))))
199 (set (make-local-variable 'copyright-update) nil)))
200 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
201 nil))
202
203
204 ;;;###autoload
205 (defun copyright-fix-years ()
206 "Convert 2 digit years to 4 digit years.
207 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
208 (interactive)
209 (widen)
210 (goto-char (point-min))
211 (if (re-search-forward copyright-regexp (copyright-limit) t)
212 (let ((s (match-beginning 2))
213 (e (copy-marker (1+ (match-end 2))))
214 (p (make-marker))
215 last)
216 (goto-char s)
217 (while (re-search-forward "[0-9]+" e t)
218 (set-marker p (point))
219 (goto-char (match-beginning 0))
220 (let ((sep (char-before))
221 (year (string-to-number (match-string 0))))
222 (when (and sep
223 (/= (char-syntax sep) ?\s)
224 (/= sep ?-))
225 (insert " "))
226 (when (< year 100)
227 (insert (if (>= year 50) "19" "20"))))
228 (goto-char p)
229 (setq last p))
230 (when last
231 (goto-char last)
232 ;; Don't mess up whitespace after the years.
233 (skip-chars-backward " \t")
234 (save-restriction
235 (narrow-to-region (point-min) (point))
236 (let ((fill-prefix " "))
237 (fill-region s last))))
238 (set-marker e nil)
239 (set-marker p nil)
240 (copyright-update nil t))
241 (message "No copyright message")))
242
243 ;;;###autoload
244 (define-skeleton copyright
245 "Insert a copyright by $ORGANIZATION notice at cursor."
246 "Company: "
247 comment-start
248 "Copyright (C) " `(substring (current-time-string) -4) " by "
249 (or (getenv "ORGANIZATION")
250 str)
251 '(if (and copyright-limit (> (point) (+ (point-min) copyright-limit)))
252 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
253 comment-end \n)
254
255 (provide 'copyright)
256
257 ;; For the copyright sign:
258 ;; Local Variables:
259 ;; coding: utf-8
260 ;; End:
261
262 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
263 ;;; copyright.el ends here