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