(FORWARD_SIGNAL_TO_MAIN_THREAD): New define.
[emacs.git] / lisp / emacs-lisp / copyright.el
blob96c846d912a81646b85ba13fd01b57d5f6ece4b8
1 ;;; copyright.el --- update the copyright notice in current buffer
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: maint, tools
9 ;; This file is part of GNU Emacs.
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 3, or (at your option)
14 ;; any later version.
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.
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Allows updating the copyright year and above mentioned GPL version manually
29 ;; or when saving a file.
30 ;; Do (add-hook 'before-save-hook 'copyright-update), or use
31 ;; M-x customize-variable RET before-save-hook RET.
33 ;;; Code:
35 (defgroup copyright nil
36 "Update the copyright notice in current buffer."
37 :group 'tools)
39 (defcustom copyright-limit 2000
40 "Don't try to update copyright beyond this position unless interactive.
41 A value of nil means to search whole buffer."
42 :group 'copyright
43 :type '(choice (integer :tag "Limit")
44 (const :tag "No limit")))
46 ;; The character classes have the Latin-1 version and the Latin-9
47 ;; version, which is probably enough.
48 (defcustom copyright-regexp
49 "\\([�©Ž©]\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
50 \\|[Cc]opyright\\s *:?\\s *[�©Ž©]\\)\
51 \\s *\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
52 "What your copyright notice looks like.
53 The second \\( \\) construct must match the years."
54 :group 'copyright
55 :type 'regexp)
57 (defcustom copyright-years-regexp
58 "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
59 "Match additional copyright notice years.
60 The second \\( \\) construct must match the years."
61 :group 'copyright
62 :type 'regexp)
65 (defcustom copyright-query 'function
66 "If non-nil, ask user before changing copyright.
67 When this is `function', only ask when called non-interactively."
68 :group 'copyright
69 :type '(choice (const :tag "Do not ask")
70 (const :tag "Ask unless interactive" function)
71 (other :tag "Ask" t)))
74 ;; when modifying this, also modify the comment generated by autoinsert.el
75 (defconst copyright-current-gpl-version "3"
76 "String representing the current version of the GPL or nil.")
78 (defvar copyright-update t)
80 ;; This is a defvar rather than a defconst, because the year can
81 ;; change during the Emacs session.
82 (defvar copyright-current-year (substring (current-time-string) -4)
83 "String representing the current year.")
85 (defun copyright-update-year (replace noquery)
86 (when (re-search-forward copyright-regexp
87 (if copyright-limit (+ (point) copyright-limit)) t)
88 ;; If the years are continued onto multiple lined
89 ;; that are marked as comments, skip to the end of the years anyway.
90 (while (save-excursion
91 (and (eq (following-char) ?,)
92 (progn (forward-char 1) t)
93 (progn (skip-chars-forward " \t") (eolp))
94 comment-start-skip
95 (save-match-data
96 (forward-line 1)
97 (and (looking-at comment-start-skip)
98 (goto-char (match-end 0))))
99 (save-match-data
100 (looking-at copyright-years-regexp))))
101 (forward-line 1)
102 (re-search-forward comment-start-skip)
103 (re-search-forward copyright-years-regexp))
105 ;; Note that `current-time-string' isn't locale-sensitive.
106 (setq copyright-current-year (substring (current-time-string) -4))
107 (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
108 (substring copyright-current-year -2))
109 (if (or noquery
110 (y-or-n-p (if replace
111 (concat "Replace copyright year(s) by "
112 copyright-current-year "? ")
113 (concat "Add " copyright-current-year
114 " to copyright? "))))
115 (if replace
116 (replace-match copyright-current-year t t nil 2)
117 (let ((size (save-excursion (skip-chars-backward "0-9"))))
118 (if (and (eq (% (- (string-to-number copyright-current-year)
119 (string-to-number (buffer-substring
120 (+ (point) size)
121 (point))))
122 100)
124 (or (eq (char-after (+ (point) size -1)) ?-)
125 (eq (char-after (+ (point) size -2)) ?-)))
126 ;; This is a range so just replace the end part.
127 (delete-char size)
128 ;; Insert a comma with the preferred number of spaces.
129 (insert
130 (save-excursion
131 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
132 (line-beginning-position) t)
133 (match-string 1)
134 ", ")))
135 ;; If people use the '91 '92 '93 scheme, do that as well.
136 (if (eq (char-after (+ (point) size -3)) ?')
137 (insert ?')))
138 ;; Finally insert the new year.
139 (insert (substring copyright-current-year size))))))))
141 ;;;###autoload
142 (defun copyright-update (&optional arg interactivep)
143 "Update copyright notice at beginning of buffer to indicate the current year.
144 With prefix ARG, replace the years in the notice rather than adding
145 the current year after them. If necessary, and
146 `copyright-current-gpl-version' is set, any copying permissions
147 following the copyright are updated as well.
148 If non-nil, INTERACTIVEP tells the function to behave as when it's called
149 interactively."
150 (interactive "*P\nd")
151 (when (or copyright-update interactivep)
152 (let ((noquery (or (not copyright-query)
153 (and (eq copyright-query 'function) interactivep))))
154 (save-excursion
155 (save-restriction
156 (widen)
157 (goto-char (point-min))
158 (copyright-update-year arg noquery)
159 (goto-char (point-min))
160 (and copyright-current-gpl-version
161 ;; match the GPL version comment in .el files, including the
162 ;; bilingual Esperanto one in two-column, and in texinfo.tex
163 (re-search-forward
164 "\\(the Free Software Foundation;\
165 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\
166 version \\([0-9]+\\), or (at"
167 (if copyright-limit (+ (point) copyright-limit)) t)
168 (not (string= (match-string 3) copyright-current-gpl-version))
169 (or noquery
170 (y-or-n-p (concat "Replace GPL version by "
171 copyright-current-gpl-version "? ")))
172 (progn
173 (if (match-end 2)
174 ;; Esperanto bilingual comment in two-column.el
175 (replace-match copyright-current-gpl-version t t nil 2))
176 (replace-match copyright-current-gpl-version t t nil 3))))
177 (set (make-local-variable 'copyright-update) nil)))
178 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
179 nil))
182 ;;;###autoload
183 (defun copyright-fix-years ()
184 "Convert 2 digit years to 4 digit years.
185 Uses heuristic: year >= 50 means 19xx, < 50 means 20xx."
186 (interactive)
187 (widen)
188 (goto-char (point-min))
189 (if (re-search-forward copyright-regexp
190 (if copyright-limit (+ (point) copyright-limit)) t)
191 (let ((s (match-beginning 2))
192 (e (copy-marker (1+ (match-end 2))))
193 (p (make-marker))
194 last)
195 (goto-char s)
196 (while (re-search-forward "[0-9]+" e t)
197 (set-marker p (point))
198 (goto-char (match-beginning 0))
199 (let ((sep (char-before))
200 (year (string-to-number (match-string 0))))
201 (when (and sep
202 (/= (char-syntax sep) ?\s)
203 (/= sep ?-))
204 (insert " "))
205 (when (< year 100)
206 (insert (if (>= year 50) "19" "20"))))
207 (goto-char p)
208 (setq last p))
209 (when last
210 (goto-char last)
211 ;; Don't mess up whitespace after the years.
212 (skip-chars-backward " \t")
213 (save-restriction
214 (narrow-to-region (point-min) (point))
215 (let ((fill-prefix " "))
216 (fill-region s last))))
217 (set-marker e nil)
218 (set-marker p nil)
219 (copyright-update nil t))
220 (message "No copyright message")))
222 ;;;###autoload
223 (define-skeleton copyright
224 "Insert a copyright by $ORGANIZATION notice at cursor."
225 "Company: "
226 comment-start
227 "Copyright (C) " `(substring (current-time-string) -4) " by "
228 (or (getenv "ORGANIZATION")
229 str)
230 '(if (and copyright-limit (> (point) (+ (point-min) copyright-limit)))
231 (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
232 comment-end \n)
234 (provide 'copyright)
236 ;; For the copyright sign:
237 ;; Local Variables:
238 ;; coding: emacs-mule
239 ;; End:
241 ;; arch-tag: b4991afb-b6b1-4590-bebe-e076d9d4aee8
242 ;;; copyright.el ends here