]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/copyright.el
Customized.
[gnu-emacs] / lisp / emacs-lisp / copyright.el
1 ;;; copyright.el --- update the copyright notice in current buffer
2
3 ;; Copyright (C) 1991, 1992-1995, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de
6 ;; fax (+49 69) 7588-2389
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. Do (add-hook 'write-file-hooks 'copyright-update).
30
31 ;;; Code:
32
33 (defgroup copyright nil
34 "Update the copyright notice in current buffer."
35 :group 'tools)
36
37 (defcustom copyright-limit 2000
38 "*Don't try to update copyright beyond this position unless interactive.
39 `nil' means to search whole buffer."
40 :group 'copyright
41 :type '(choice (integer :tag "Limit")
42 (const :tag "No limit")))
43
44
45 (defcustom copyright-regexp
46 "\\([\81©\251]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *(C)\
47 \\|[Cc]opyright\\s *:?\\s *[\81©\251]\\)\
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" t)
61 (const :tag "Ask unless interactive" function)))
62
63
64 (defconst copyright-current-year (substring (current-time-string) -4)
65 "String representing the current year.")
66
67
68 ;; when modifying this, also modify the comment generated by autoinsert.el
69 (defconst copyright-current-gpl-version "2"
70 "String representing the current version of the GPL or `nil'.")
71
72 (defvar copyright-update t)
73
74 ;;;###autoload
75 (defun copyright-update (&optional arg)
76 "Update the copyright notice at the beginning of the buffer to indicate
77 the current year. If optional prefix ARG is given replace the years in the
78 notice rather than adding the current year after them. If necessary and
79 `copyright-current-gpl-version' is set, the copying permissions following the
80 copyright, if any, are updated as well."
81 (interactive "*P")
82 (if copyright-update
83 (save-excursion
84 (save-restriction
85 (widen)
86 (goto-char (point-min))
87 (if (re-search-forward copyright-regexp copyright-limit t)
88 (if (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
89 (substring copyright-current-year -2))
90 ()
91 (backward-char 1)
92 (if (or (not copyright-query)
93 (and (eq copyright-query 'function)
94 (eq this-command 'copyright-update))
95 (y-or-n-p (if arg
96 (concat "Replace copyright year(s) by "
97 copyright-current-year "? ")
98 (concat "Add " copyright-current-year
99 " to copyright? "))))
100 (if arg
101 (progn
102 (delete-region (match-beginning 1) (match-end 1))
103 (insert copyright-current-year))
104 (setq arg (save-excursion (skip-chars-backward "0-9")))
105 (if (and (eq (% (- (string-to-number
106 copyright-current-year)
107 (string-to-number (buffer-substring
108 (+ (point) arg)
109 (point))))
110 100)
111 1)
112 (or (eq (char-after (+ (point) arg -1)) ?-)
113 (eq (char-after (+ (point) arg -2)) ?-)))
114 (delete-char arg)
115 (insert ", ")
116 (if (eq (char-after (+ (point) arg -3)) ?')
117 (insert ?')))
118 (insert (substring copyright-current-year arg))))))
119 (goto-char (point-min))
120 (and copyright-current-gpl-version
121 ;; match the GPL version comment in .el files, including the
122 ;; bilingual Esperanto one in two-column, and in texinfo.tex
123 (re-search-forward "\\(the Free Software Foundation; either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)version \\([0-9]+\\), or (at"
124 copyright-limit t)
125 (not (string= (buffer-substring (match-beginning 3) (match-end 3))
126 copyright-current-gpl-version))
127 (or (not copyright-query)
128 (and (eq copyright-query 'function)
129 (eq this-command 'copyright-update))
130 (y-or-n-p (concat "Replace GPL version by "
131 copyright-current-gpl-version "? ")))
132 (progn
133 (if (match-end 2)
134 ;; Esperanto bilingual comment in two-column.el
135 (progn
136 (delete-region (match-beginning 2) (match-end 2))
137 (goto-char (match-beginning 2))
138 (insert copyright-current-gpl-version)))
139 (delete-region (match-beginning 3) (match-end 3))
140 (goto-char (match-beginning 3))
141 (insert copyright-current-gpl-version))))
142 (set (make-local-variable 'copyright-update) nil)))
143 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
144 nil)
145
146
147 ;;;###autoload
148 (define-skeleton copyright
149 "Insert a copyright by $ORGANIZATION notice at cursor."
150 "Company: "
151 comment-start
152 "Copyright (C) " copyright-current-year " by "
153 (or (getenv "ORGANIZATION")
154 str)
155 '(if (> (point) copyright-limit)
156 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
157 comment-end)
158
159 (provide 'copyright)
160
161 ;; copyright.el ends here