(eval-defun): For defcustom, always set the value.
[emacs.git] / lisp / emacs-lisp / copyright.el
blob4f820810926fd049dc3346f8a0fb9df58d8081a2
1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
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 (defvar copyright-limit 2000
33 "*Don't try to update copyright beyond this position unless interactive.
34 `nil' means to search whole buffer.")
37 (defvar copyright-regexp
38 "\\(\251\\|[Cc]opyright\\s *:?\\s *(C)\
39 \\|[Cc]opyright\\s *:?\\s *\251\\)\
40 \\s *\\([1-9][-0-9, ']*[0-9]+\\) "
41 "*What your copyright notice looks like.
42 The second \\( \\) construct must match the years.")
45 (defvar copyright-query 'function
46 "*If non-`nil', ask user before changing copyright.
47 When this is `function', only ask when called non-interactively.")
50 (defconst copyright-current-year (substring (current-time-string) -4)
51 "String representing the current year.")
54 ;; when modifying this, also modify the comment generated by autoinsert.el
55 (defconst copyright-current-gpl-version "2"
56 "String representing the current version of the GPL or `nil'.")
58 (defvar copyright-update t)
60 ;;;###autoload
61 (defun copyright-update (&optional arg)
62 "Update the copyright notice at the beginning of the buffer to indicate
63 the current year. If optional prefix ARG is given replace the years in the
64 notice rather than adding the current year after them. If necessary and
65 `copyright-current-gpl-version' is set, the copying permissions following the
66 copyright, if any, are updated as well."
67 (interactive "*P")
68 (if copyright-update
69 (save-excursion
70 (save-restriction
71 (widen)
72 (goto-char (point-min))
73 (if (re-search-forward copyright-regexp copyright-limit t)
74 (if (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
75 (substring copyright-current-year -2))
77 (backward-char 1)
78 (if (or (not copyright-query)
79 (and (eq copyright-query 'function)
80 (eq this-command 'copyright-update))
81 (y-or-n-p (if arg
82 (concat "Replace copyright year(s) by "
83 copyright-current-year "? ")
84 (concat "Add " copyright-current-year
85 " to copyright? "))))
86 (if arg
87 (progn
88 (delete-region (match-beginning 1) (match-end 1))
89 (insert copyright-current-year))
90 (setq arg (save-excursion (skip-chars-backward "0-9")))
91 (if (and (eq (% (- (string-to-number
92 copyright-current-year)
93 (string-to-number (buffer-substring
94 (+ (point) arg)
95 (point))))
96 100)
98 (or (eq (char-after (+ (point) arg -1)) ?-)
99 (eq (char-after (+ (point) arg -2)) ?-)))
100 (delete-char arg)
101 (insert ", ")
102 (if (eq (char-after (+ (point) arg -3)) ?')
103 (insert ?')))
104 (insert (substring copyright-current-year arg))))))
105 (goto-char (point-min))
106 (and copyright-current-gpl-version
107 ;; match the GPL version comment in .el files, including the
108 ;; bilingual Esperanto one in two-column, and in texinfo.tex
109 (re-search-forward "\\(the Free Software Foundation; either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)version \\([0-9]+\\), or (at"
110 copyright-limit t)
111 (not (string= (buffer-substring (match-beginning 3) (match-end 3))
112 copyright-current-gpl-version))
113 (or (not copyright-query)
114 (and (eq copyright-query 'function)
115 (eq this-command 'copyright-update))
116 (y-or-n-p (concat "Replace GPL version by "
117 copyright-current-gpl-version "? ")))
118 (progn
119 (if (match-end 2)
120 ;; Esperanto bilingual comment in two-column.el
121 (progn
122 (delete-region (match-beginning 2) (match-end 2))
123 (goto-char (match-beginning 2))
124 (insert copyright-current-gpl-version)))
125 (delete-region (match-beginning 3) (match-end 3))
126 (goto-char (match-beginning 3))
127 (insert copyright-current-gpl-version))))
128 (set (make-local-variable 'copyright-update) nil)))
129 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
130 nil)
133 ;;;###autoload
134 (define-skeleton copyright
135 "Insert a copyright by $ORGANIZATION notice at cursor."
136 "Company: "
137 comment-start
138 "Copyright (C) " copyright-current-year " by "
139 (or (getenv "ORGANIZATION")
140 str)
141 '(if (> (point) copyright-limit)
142 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
143 comment-end)
145 ;; copyright.el ends here