]> 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 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 yars 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 (not (string= (match-string 3) copyright-current-gpl-version))
188 (or noquery
189 (y-or-n-p (concat "Replace GPL version by "
190 copyright-current-gpl-version "? ")))
191 (progn
192 (if (match-end 2)
193 ;; Esperanto bilingual comment in two-column.el
194 (replace-match copyright-current-gpl-version t t nil 2))
195 (replace-match copyright-current-gpl-version t t nil 3))))
196 (set (make-local-variable 'copyright-update) nil)))
197 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
198 nil))
199
200
201 ;;;###autoload
202 (defun copyright-fix-years ()
203 "Convert 2 digit years to 4 digit years.
204 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
205 (interactive)
206 (widen)
207 (goto-char (point-min))
208 (if (re-search-forward copyright-regexp (copyright-limit) t)
209 (let ((s (match-beginning 2))
210 (e (copy-marker (1+ (match-end 2))))
211 (p (make-marker))
212 last)
213 (goto-char s)
214 (while (re-search-forward "[0-9]+" e t)
215 (set-marker p (point))
216 (goto-char (match-beginning 0))
217 (let ((sep (char-before))
218 (year (string-to-number (match-string 0))))
219 (when (and sep
220 (/= (char-syntax sep) ?\s)
221 (/= sep ?-))
222 (insert " "))
223 (when (< year 100)
224 (insert (if (>= year 50) "19" "20"))))
225 (goto-char p)
226 (setq last p))
227 (when last
228 (goto-char last)
229 ;; Don't mess up whitespace after the years.
230 (skip-chars-backward " \t")
231 (save-restriction
232 (narrow-to-region (point-min) (point))
233 (let ((fill-prefix " "))
234 (fill-region s last))))
235 (set-marker e nil)
236 (set-marker p nil)
237 (copyright-update nil t))
238 (message "No copyright message")))
239
240 ;;;###autoload
241 (define-skeleton copyright
242 "Insert a copyright by $ORGANIZATION notice at cursor."
243 "Company: "
244 comment-start
245 "Copyright (C) " `(substring (current-time-string) -4) " by "
246 (or (getenv "ORGANIZATION")
247 str)
248 '(if (and copyright-limit (> (point) (+ (point-min) copyright-limit)))
249 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
250 comment-end \n)
251
252 (provide 'copyright)
253
254 ;; For the copyright sign:
255 ;; Local Variables:
256 ;; coding: utf-8
257 ;; End:
258
259 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
260 ;;; copyright.el ends here