(byte-compile-inline-expand):
[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 Free Software Foundation, Inc.
4
5 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
6 ;; Keywords: maint, tools
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
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).
29
30 ;;; Code:
31
32 (defgroup copyright nil
33 "Update the copyright notice in current buffer."
34 :group 'tools)
35
36 (defcustom copyright-limit 2000
37 "*Don't try to update copyright beyond this position unless interactive.
38 `nil' means to search whole buffer."
39 :group 'copyright
40 :type '(choice (integer :tag "Limit")
41 (const :tag "No limit")))
42
43 ;; Cleaner to specify Latin-1 coding for this file, and not use both
44 ;; unibyte and multibyte copyright symbol characters?
45 (defcustom copyright-regexp
46 "\\([\251\81©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *(C)\
47 \\|[Cc]opyright\\s *:?\\s *[\251\81©]\\)\
48 \\s *\\([1-9][-0-9, ']*[0-9]+\\)"
49 "*What your copyright notice looks like.
50 The second \\( \\) construct must match the years."
51 :group 'copyright
52 :type 'regexp)
53
54
55 (defcustom copyright-query 'function
56 "*If non-`nil', ask user before changing copyright.
57 When this is `function', only ask when called non-interactively."
58 :group 'copyright
59 :type '(choice (const :tag "Do not ask")
60 (const :tag "Ask unless interactive" function)
61 (other :tag "Ask" t)))
62
63
64 ;; when modifying this, also modify the comment generated by autoinsert.el
65 (defconst copyright-current-gpl-version "2"
66 "String representing the current version of the GPL or `nil'.")
67
68 (defvar copyright-update t)
69
70 ;; This is a defvar rather than a defconst, because the year can
71 ;; change during the Emacs session.
72 (defvar copyright-current-year "2001"
73 "String representing the current year.")
74
75
76 ;;;###autoload
77 (defun copyright-update (&optional arg)
78 "Update the copyright notice at the beginning of the buffer to indicate
79 the current year. If optional prefix ARG is given replace the years in the
80 notice rather than adding the current year after them. If necessary and
81 `copyright-current-gpl-version' is set, the copying permissions following the
82 copyright, if any, are updated as well."
83 (interactive "*P")
84 (if copyright-update
85 (save-excursion
86 (save-restriction
87 (widen)
88 (goto-char (point-min))
89 (setq copyright-current-year (substring (current-time-string) -4))
90 (if (re-search-forward copyright-regexp copyright-limit t)
91 (if (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
92 (substring copyright-current-year -2))
93 ()
94 (if (or (not copyright-query)
95 (and (eq copyright-query 'function)
96 (eq this-command 'copyright-update))
97 (y-or-n-p (if arg
98 (concat "Replace copyright year(s) by "
99 copyright-current-year "? ")
100 (concat "Add " copyright-current-year
101 " to copyright? "))))
102 (if arg
103 (progn
104 (delete-region (match-beginning 1) (match-end 1))
105 (insert copyright-current-year))
106 (setq arg (save-excursion (skip-chars-backward "0-9")))
107 (if (and (eq (% (- (string-to-number
108 copyright-current-year)
109 (string-to-number (buffer-substring
110 (+ (point) arg)
111 (point))))
112 100)
113 1)
114 (or (eq (char-after (+ (point) arg -1)) ?-)
115 (eq (char-after (+ (point) arg -2)) ?-)))
116 (delete-char arg)
117 (insert ", ")
118 (if (eq (char-after (+ (point) arg -3)) ?')
119 (insert ?')))
120 (insert (substring copyright-current-year arg))))))
121 (goto-char (point-min))
122 (and copyright-current-gpl-version
123 ;; match the GPL version comment in .el files, including the
124 ;; bilingual Esperanto one in two-column, and in texinfo.tex
125 (re-search-forward "\\(the Free Software Foundation; either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)version \\([0-9]+\\), or (at"
126 copyright-limit t)
127 (not (string= (buffer-substring (match-beginning 3) (match-end 3))
128 copyright-current-gpl-version))
129 (or (not copyright-query)
130 (and (eq copyright-query 'function)
131 (eq this-command 'copyright-update))
132 (y-or-n-p (concat "Replace GPL version by "
133 copyright-current-gpl-version "? ")))
134 (progn
135 (if (match-end 2)
136 ;; Esperanto bilingual comment in two-column.el
137 (progn
138 (delete-region (match-beginning 2) (match-end 2))
139 (goto-char (match-beginning 2))
140 (insert copyright-current-gpl-version)))
141 (delete-region (match-beginning 3) (match-end 3))
142 (goto-char (match-beginning 3))
143 (insert copyright-current-gpl-version))))
144 (set (make-local-variable 'copyright-update) nil)))
145 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
146 nil)
147
148
149 ;;;###autoload
150 (define-skeleton copyright
151 "Insert a copyright by $ORGANIZATION notice at cursor."
152 "Company: "
153 comment-start
154 "Copyright (C) " `(substring (current-time-string) -4) " by "
155 (or (getenv "ORGANIZATION")
156 str)
157 '(if (> (point) copyright-limit)
158 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
159 comment-end)
160
161 (provide 'copyright)
162
163 ;; For the copyright sign:
164 ;; Local Variables:
165 ;; coding: emacs-mule
166 ;; End:
167
168 ;; copyright.el ends here