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