]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
(copyright-update-year): New function extracted from copyright-update.
[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
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 'write-file-functions 'copyright-update).
31
32 ;;; Code:
33
34 (defgroup copyright nil
35 "Update the copyright notice in current buffer."
36 :group 'tools)
37
38 (defcustom copyright-limit 2000
39 "*Don't try to update copyright beyond this position unless interactive.
40 A value of nil means to search whole buffer."
41 :group 'copyright
42 :type '(choice (integer :tag "Limit")
43 (const :tag "No limit")))
44
45 ;; Would it be cleaner to specify Latin-1 coding for this file,
46 ;; and not use both unibyte and multibyte copyright symbol characters?
47
48 ;; The character classes include the unibyte (C) sign,
49 ;; the Latin-1 version, and the Latin-9 version.
50 (defcustom copyright-regexp
51 "\\([\81©\8e©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
52 \\|[Cc]opyright\\s *:?\\s *[\81©\8e©]\\)\
53 \\s *\\([1-9]\\([-0-9, ';\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
54 "*What your copyright notice looks like.
55 The second \\( \\) construct must match the years."
56 :group 'copyright
57 :type 'regexp)
58
59
60 (defcustom copyright-query 'function
61 "*If non-nil, ask user before changing copyright.
62 When this is `function', only ask when called non-interactively."
63 :group 'copyright
64 :type '(choice (const :tag "Do not ask")
65 (const :tag "Ask unless interactive" function)
66 (other :tag "Ask" t)))
67
68
69 ;; when modifying this, also modify the comment generated by autoinsert.el
70 (defconst copyright-current-gpl-version "2"
71 "String representing the current version of the GPL or nil.")
72
73 (defvar copyright-update t)
74
75 ;; This is a defvar rather than a defconst, because the year can
76 ;; change during the Emacs session.
77 (defvar copyright-current-year (substring (current-time-string) -4)
78 "String representing the current year.")
79
80 (defun copyright-update-year (replace noquery)
81 (when (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
82 ;; Note that `current-time-string' isn't locale-sensitive.
83 (setq copyright-current-year (substring (current-time-string) -4))
84 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
85 (substring copyright-current-year -2))
86 (if (or noquery
87 (y-or-n-p (if replace
88 (concat "Replace copyright year(s) by "
89 copyright-current-year "? ")
90 (concat "Add " copyright-current-year
91 " to copyright? "))))
92 (if replace
93 (replace-match copyright-current-year t t nil 1)
94 (let ((size (save-excursion (skip-chars-backward "0-9"))))
95 (if (and (eq (% (- (string-to-number copyright-current-year)
96 (string-to-number (buffer-substring
97 (+ (point) size)
98 (point))))
99 100)
100 1)
101 (or (eq (char-after (+ (point) size -1)) ?-)
102 (eq (char-after (+ (point) size -2)) ?-)))
103 ;; This is a range so just replace the end part.
104 (delete-char size)
105 ;; Detect if this is using the following shorthand:
106 ;; (C) 1993, 94, 95, 1998, 2000, 01, 02, 2003
107 (if (and
108 ;; Check that the last year was 4-chars and same century.
109 (eq size -4)
110 (equal (buffer-substring (- (point) 4) (- (point) 2))
111 (substring copyright-current-year 0 2))
112 ;; Check that there are 2-char years as well.
113 (save-excursion
114 (re-search-backward "[^0-9][0-9][0-9][^0-9]"
115 (line-beginning-position) t))
116 ;; Make sure we don't remove the first century marker.
117 (save-excursion
118 (forward-char size)
119 (re-search-backward
120 (concat (buffer-substring (point) (+ (point) 2))
121 "[0-9][0-9]")
122 (line-beginning-position) t)))
123 ;; Remove the century marker of the last entry.
124 (delete-region (- (point) 4) (- (point) 2)))
125 ;; Insert a comma with the preferred number of spaces.
126 (insert
127 (save-excursion
128 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
129 (line-beginning-position) t)
130 (match-string 1)
131 ", ")))
132 ;; If people use the '91 '92 '93 scheme, do that as well.
133 (if (eq (char-after (+ (point) size -3)) ?')
134 (insert ?')))
135 ;; Finally insert the new year.
136 (insert (substring copyright-current-year size))))))))
137
138 ;;;###autoload
139 (defun copyright-update (&optional arg interactivep)
140 "Update copyright notice at beginning of buffer to indicate the current year.
141 With prefix ARG, replace the years in the notice rather than adding
142 the current year after them. If necessary, and
143 `copyright-current-gpl-version' is set, any copying permissions
144 following the copyright are updated as well.
145 If non-nil, INTERACTIVEP tells the function to behave as when it's called
146 interactively."
147 (interactive "*P\nd")
148 (when (or copyright-update interactivep)
149 (let ((noquery (or (not copyright-query)
150 (and (eq copyright-query 'function) interactivep))))
151 (save-excursion
152 (save-restriction
153 (widen)
154 (goto-char (point-min))
155 (copyright-update-year arg noquery)
156 (goto-char (point-min))
157 (and copyright-current-gpl-version
158 ;; match the GPL version comment in .el files, including the
159 ;; bilingual Esperanto one in two-column, and in texinfo.tex
160 (re-search-forward "\\(the Free Software Foundation;\
161 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
162 version \\([0-9]+\\), or (at"
163 (+ (point) copyright-limit) t)
164 (not (string= (match-string 3) copyright-current-gpl-version))
165 (or noquery
166 (y-or-n-p (concat "Replace GPL version by "
167 copyright-current-gpl-version "? ")))
168 (progn
169 (if (match-end 2)
170 ;; Esperanto bilingual comment in two-column.el
171 (replace-match copyright-current-gpl-version t t nil 2))
172 (replace-match copyright-current-gpl-version t t nil 3))))
173 (set (make-local-variable 'copyright-update) nil)))
174 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
175 nil))
176
177
178 ;;;###autoload
179 (define-skeleton copyright
180 "Insert a copyright by $ORGANIZATION notice at cursor."
181 "Company: "
182 comment-start
183 "Copyright (C) " `(substring (current-time-string) -4) " by "
184 (or (getenv "ORGANIZATION")
185 str)
186 '(if (> (point) (+ (point-min) copyright-limit))
187 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
188 comment-end \n)
189
190 (provide 'copyright)
191
192 ;; For the copyright sign:
193 ;; Local Variables:
194 ;; coding: emacs-mule
195 ;; End:
196
197 ;;; copyright.el ends here