*** empty log message ***
[emacs.git] / lisp / emacs-lisp / copyright.el
blobabed05cf9028d7e9bcad37a2ac8bef3feebe2df8
1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 98, 2001 Free Software Foundation, Inc.
5 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
6 ;; Keywords: maint, tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; Allows updating the copyright year and above mentioned GPL version manually
28 ;; or when saving a file. Do (add-hook 'write-file-hooks 'copyright-update).
30 ;;; Code:
32 (defgroup copyright nil
33 "Update the copyright notice in current buffer."
34 :group 'tools)
36 (defcustom copyright-limit 2000
37 "*Don't try to update copyright beyond this position unless interactive.
38 A value of nil means to search whole buffer."
39 :group 'copyright
40 :type '(choice (integer :tag "Limit")
41 (const :tag "No limit")))
43 ;; Would it be cleaner to specify Latin-1 coding for this file,
44 ;; and not use both unibyte and multibyte copyright symbol characters?
46 ;; The character classes include the unibyte (C) sign,
47 ;; the Latin-1 version, and the Latin-9 version.
48 (defcustom copyright-regexp
49 "\\([�©Ž©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
50 \\|[Cc]opyright\\s *:?\\s *[�©Ž©]\\)\
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)
58 (defcustom copyright-query 'function
59 "*If non-nil, ask user before changing copyright.
60 When this is `function', only ask when called non-interactively."
61 :group 'copyright
62 :type '(choice (const :tag "Do not ask")
63 (const :tag "Ask unless interactive" function)
64 (other :tag "Ask" t)))
67 ;; when modifying this, also modify the comment generated by autoinsert.el
68 (defconst copyright-current-gpl-version "2"
69 "String representing the current version of the GPL or nil.")
71 (defvar copyright-update t)
73 ;; This is a defvar rather than a defconst, because the year can
74 ;; change during the Emacs session.
75 (defvar copyright-current-year (substring (current-time-string) -4)
76 "String representing the current year.")
79 ;;;###autoload
80 (defun copyright-update (&optional arg)
81 "Update copyright notice at beginning of buffer to indicate the current year.
82 With prefix ARG, replace the years in the notice rather than adding
83 the current year after them. If necessary, and
84 `copyright-current-gpl-version' is set, any copying permissions
85 following the copyright are updated as well."
86 (interactive "*P")
87 (if copyright-update
88 (save-excursion
89 (save-restriction
90 (widen)
91 (goto-char (point-min))
92 ;; Note that `current-time-string' isn't locale-sensitive.
93 (setq copyright-current-year (substring (current-time-string) -4))
94 (if (re-search-forward copyright-regexp copyright-limit t)
95 (if (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
96 (substring copyright-current-year -2))
98 (if (or (not copyright-query)
99 (and (eq copyright-query 'function)
100 (eq this-command 'copyright-update))
101 (y-or-n-p (if arg
102 (concat "Replace copyright year(s) by "
103 copyright-current-year "? ")
104 (concat "Add " copyright-current-year
105 " to copyright? "))))
106 (if arg
107 (progn
108 (delete-region (match-beginning 1) (match-end 1))
109 (insert copyright-current-year))
110 (setq arg (save-excursion (skip-chars-backward "0-9")))
111 (if (and (eq (% (- (string-to-number
112 copyright-current-year)
113 (string-to-number (buffer-substring
114 (+ (point) arg)
115 (point))))
116 100)
118 (or (eq (char-after (+ (point) arg -1)) ?-)
119 (eq (char-after (+ (point) arg -2)) ?-)))
120 (delete-char arg)
121 (insert ", ")
122 (if (eq (char-after (+ (point) arg -3)) ?')
123 (insert ?')))
124 (insert (substring copyright-current-year arg))))))
125 (goto-char (point-min))
126 (and copyright-current-gpl-version
127 ;; match the GPL version comment in .el files, including the
128 ;; bilingual Esperanto one in two-column, and in texinfo.tex
129 (re-search-forward "\\(the Free Software Foundation;\
130 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
131 version \\([0-9]+\\), or (at"
132 copyright-limit t)
133 (not (string= (match-string 3) copyright-current-gpl-version))
134 (or (not copyright-query)
135 (and (eq copyright-query 'function)
136 (eq this-command 'copyright-update))
137 (y-or-n-p (concat "Replace GPL version by "
138 copyright-current-gpl-version "? ")))
139 (progn
140 (if (match-end 2)
141 ;; Esperanto bilingual comment in two-column.el
142 (progn
143 (delete-region (match-beginning 2) (match-end 2))
144 (goto-char (match-beginning 2))
145 (insert copyright-current-gpl-version)))
146 (delete-region (match-beginning 3) (match-end 3))
147 (goto-char (match-beginning 3))
148 (insert copyright-current-gpl-version))))
149 (set (make-local-variable 'copyright-update) nil)))
150 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
151 nil)
154 ;;;###autoload
155 (define-skeleton copyright
156 "Insert a copyright by $ORGANIZATION notice at cursor."
157 "Company: "
158 comment-start
159 "Copyright (C) " `(substring (current-time-string) -4) " by "
160 (or (getenv "ORGANIZATION")
161 str)
162 '(if (> (point) copyright-limit)
163 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
164 comment-end \n)
166 (provide 'copyright)
168 ;; For the copyright sign:
169 ;; Local Variables:
170 ;; coding: emacs-mule
171 ;; End:
173 ;;; copyright.el ends here