]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
*** empty log message ***
[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, 98, 2001 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 A value of nil means to search whole buffer."
39 :group 'copyright
40 :type '(choice (integer :tag "Limit")
41 (const :tag "No limit")))
42
43 ;; Would it be cleaner to specify Latin-1 coding for this file,
44 ;; and not use both unibyte and multibyte copyright symbol characters?
45
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)
54
55
56 (defcustom copyright-query 'function
57 "*If non-nil, ask user before changing copyright.
58 When this is `function', only ask when called non-interactively."
59 :group 'copyright
60 :type '(choice (const :tag "Do not ask")
61 (const :tag "Ask unless interactive" function)
62 (other :tag "Ask" t)))
63
64
65 ;; when modifying this, also modify the comment generated by autoinsert.el
66 (defconst copyright-current-gpl-version "2"
67 "String representing the current version of the GPL or nil.")
68
69 (defvar copyright-update t)
70
71 ;; This is a defvar rather than a defconst, because the year can
72 ;; change during the Emacs session.
73 (defvar copyright-current-year (substring (current-time-string) -4)
74 "String representing the current year.")
75
76
77 ;;;###autoload
78 (defun copyright-update (&optional arg)
79 "Update copyright notice at beginning of buffer to indicate the current year.
80 With prefix ARG, replace the years in the notice rather than adding
81 the current year after them. If necessary, and
82 `copyright-current-gpl-version' is set, any copying permissions
83 following the copyright are updated as well."
84 (interactive "*P")
85 (if copyright-update
86 (save-excursion
87 (save-restriction
88 (widen)
89 (goto-char (point-min))
90 ;; Note that `current-time-string' isn't locale-sensitive.
91 (setq copyright-current-year (substring (current-time-string) -4))
92 (if (re-search-forward copyright-regexp copyright-limit t)
93 (if (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
94 (substring copyright-current-year -2))
95 ()
96 (if (or (not copyright-query)
97 (and (eq copyright-query 'function)
98 (eq this-command 'copyright-update))
99 (y-or-n-p (if arg
100 (concat "Replace copyright year(s) by "
101 copyright-current-year "? ")
102 (concat "Add " copyright-current-year
103 " to copyright? "))))
104 (if arg
105 (progn
106 (delete-region (match-beginning 1) (match-end 1))
107 (insert copyright-current-year))
108 (setq arg (save-excursion (skip-chars-backward "0-9")))
109 (if (and (eq (% (- (string-to-number
110 copyright-current-year)
111 (string-to-number (buffer-substring
112 (+ (point) arg)
113 (point))))
114 100)
115 1)
116 (or (eq (char-after (+ (point) arg -1)) ?-)
117 (eq (char-after (+ (point) arg -2)) ?-)))
118 (delete-char arg)
119 (insert ", ")
120 (if (eq (char-after (+ (point) arg -3)) ?')
121 (insert ?')))
122 (insert (substring copyright-current-year arg))))))
123 (goto-char (point-min))
124 (and copyright-current-gpl-version
125 ;; match the GPL version comment in .el files, including the
126 ;; bilingual Esperanto one in two-column, and in texinfo.tex
127 (re-search-forward "\\(the Free Software Foundation;\
128 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
129 version \\([0-9]+\\), or (at"
130 copyright-limit t)
131 (not (string= (match-string 3) copyright-current-gpl-version))
132 (or (not copyright-query)
133 (and (eq copyright-query 'function)
134 (eq this-command 'copyright-update))
135 (y-or-n-p (concat "Replace GPL version by "
136 copyright-current-gpl-version "? ")))
137 (progn
138 (if (match-end 2)
139 ;; Esperanto bilingual comment in two-column.el
140 (progn
141 (delete-region (match-beginning 2) (match-end 2))
142 (goto-char (match-beginning 2))
143 (insert copyright-current-gpl-version)))
144 (delete-region (match-beginning 3) (match-end 3))
145 (goto-char (match-beginning 3))
146 (insert copyright-current-gpl-version))))
147 (set (make-local-variable 'copyright-update) nil)))
148 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
149 nil)
150
151
152 ;;;###autoload
153 (define-skeleton copyright
154 "Insert a copyright by $ORGANIZATION notice at cursor."
155 "Company: "
156 comment-start
157 "Copyright (C) " `(substring (current-time-string) -4) " by "
158 (or (getenv "ORGANIZATION")
159 str)
160 '(if (> (point) copyright-limit)
161 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
162 comment-end \n)
163
164 (provide 'copyright)
165
166 ;; For the copyright sign:
167 ;; Local Variables:
168 ;; coding: utf-8
169 ;; End:
170
171 ;;; copyright.el ends here