]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
Add a provide statement.
[gnu-emacs] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 1998, 2001, 2003, 2004
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 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
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 (define-skeleton copyright
182 "Insert a copyright by $ORGANIZATION notice at cursor."
183 "Company: "
184 comment-start
185 "Copyright (C) " `(substring (current-time-string) -4) " by "
186 (or (getenv "ORGANIZATION")
187 str)
188 '(if (> (point) (+ (point-min) copyright-limit))
189 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
190 comment-end \n)
191
192 (provide 'copyright)
193
194 ;; For the copyright sign:
195 ;; Local Variables:
196 ;; coding: emacs-mule
197 ;; End:
198
199 ;;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
200 ;;; copyright.el ends here