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