comparison lisp/emacs-lisp/copyright.el @ 112429:6e50030da731

Some copyright.el updates. * lisp/emacs-lisp/copyright.el (copyright-find-copyright): New function, split out from copyright-update-year. (copyright-update): Don't mess with the GPL version if we don't own the copyright. Update license regexp, and remove no longer needed Esperanto stuff.
author Glenn Morris <rgm@gnu.org>
date Sat, 22 Jan 2011 14:09:09 -0800
parents ef719132ddfa
children 78c4c9559156
comparison
equal deleted inserted replaced
112428:5653bdbb0b32 112429:6e50030da731
1 ;;; copyright.el --- update the copyright notice in current buffer 1 ;;; copyright.el --- update the copyright notice in current buffer
2 2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 2003, 3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1998, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; Free Software Foundation, Inc.
5 6
6 ;; Author: Daniel Pfeiffer <occitan@esperanto.org> 7 ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: maint, tools 8 ;; Keywords: maint, tools
8 9
9 ;; This file is part of GNU Emacs. 10 ;; This file is part of GNU Emacs.
118 (when copyright-limit 119 (when copyright-limit
119 (if copyright-at-end-flag 120 (if copyright-at-end-flag
120 (< (point) (- (point-max) copyright-limit)) 121 (< (point) (- (point-max) copyright-limit))
121 (> (point) (+ (point-min) copyright-limit))))) 122 (> (point) (+ (point-min) copyright-limit)))))
122 123
124 (defun copyright-find-copyright ()
125 "Return non-nil if a copyright header suitable for updating is found.
126 The header must match `copyright-regexp' and `copyright-names-regexp', if set.
127 This function sets the match-data that `copyright-update-year' uses."
128 (condition-case err
129 ;; (1) Need the extra \\( \\) around copyright-regexp because we
130 ;; goto (match-end 1) below. See note (2) below.
131 (copyright-re-search (concat "\\(" copyright-regexp
132 "\\)\\([ \t]*\n\\)?.*\\(?:"
133 copyright-names-regexp "\\)")
134 (copyright-limit)
135 t)
136 ;; In case the regexp is rejected. This is useful because
137 ;; copyright-update is typically called from before-save-hook where
138 ;; such an error is very inconvenient for the user.
139 (error (message "Can't update copyright: %s" err) nil)))
140
123 (defun copyright-update-year (replace noquery) 141 (defun copyright-update-year (replace noquery)
124 (when 142 ;; This uses the match-data from copyright-find-copyright.
125 (condition-case err 143 (goto-char (match-end 1))
126 ;; (1) Need the extra \\( \\) around copyright-regexp because we 144 ;; If the years are continued onto multiple lines
127 ;; goto (match-end 1) below. See note (2) below. 145 ;; that are marked as comments, skip to the end of the years anyway.
128 (copyright-re-search (concat "\\(" copyright-regexp 146 (while (save-excursion
129 "\\)\\([ \t]*\n\\)?.*\\(?:" 147 (and (eq (following-char) ?,)
130 copyright-names-regexp "\\)") 148 (progn (forward-char 1) t)
131 (copyright-limit) 149 (progn (skip-chars-forward " \t") (eolp))
132 t) 150 comment-start-skip
133 ;; In case the regexp is rejected. This is useful because 151 (save-match-data
134 ;; copyright-update is typically called from before-save-hook where 152 (forward-line 1)
135 ;; such an error is very inconvenient for the user. 153 (and (looking-at comment-start-skip)
136 (error (message "Can't update copyright: %s" err) nil)) 154 (goto-char (match-end 0))))
137 (goto-char (match-end 1)) 155 (looking-at-p copyright-years-regexp)))
138 ;; If the years are continued onto multiple lines 156 (forward-line 1)
139 ;; that are marked as comments, skip to the end of the years anyway. 157 (re-search-forward comment-start-skip)
140 (while (save-excursion 158 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
141 (and (eq (following-char) ?,) 159 ;; they are at note (1) above.
142 (progn (forward-char 1) t) 160 (re-search-forward (format "\\(%s\\)" copyright-years-regexp)))
143 (progn (skip-chars-forward " \t") (eolp)) 161
144 comment-start-skip 162 ;; Note that `current-time-string' isn't locale-sensitive.
145 (save-match-data 163 (setq copyright-current-year (substring (current-time-string) -4))
146 (forward-line 1) 164 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
147 (and (looking-at comment-start-skip) 165 (substring copyright-current-year -2))
148 (goto-char (match-end 0)))) 166 (if (or noquery
149 (looking-at-p copyright-years-regexp))) 167 (save-window-excursion
150 (forward-line 1) 168 (switch-to-buffer (current-buffer))
151 (re-search-forward comment-start-skip) 169 ;; Fixes some point-moving oddness (bug#2209).
152 ;; (2) Need the extra \\( \\) so that the years are subexp 3, as 170 (save-excursion
153 ;; they are at note (1) above. 171 (y-or-n-p (if replace
154 (re-search-forward (format "\\(%s\\)" copyright-years-regexp))) 172 (concat "Replace copyright year(s) by "
155 173 copyright-current-year "? ")
156 ;; Note that `current-time-string' isn't locale-sensitive. 174 (concat "Add " copyright-current-year
157 (setq copyright-current-year (substring (current-time-string) -4)) 175 " to copyright? "))))))
158 (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3)) 176 (if replace
159 (substring copyright-current-year -2)) 177 (replace-match copyright-current-year t t nil 3)
160 (if (or noquery 178 (let ((size (save-excursion (skip-chars-backward "0-9"))))
161 (save-window-excursion 179 (if (and (eq (% (- (string-to-number copyright-current-year)
162 (switch-to-buffer (current-buffer)) 180 (string-to-number (buffer-substring
163 ;; Fixes some point-moving oddness (bug#2209). 181 (+ (point) size)
164 (save-excursion 182 (point))))
165 (y-or-n-p (if replace 183 100)
166 (concat "Replace copyright year(s) by " 184 1)
167 copyright-current-year "? ") 185 (or (eq (char-after (+ (point) size -1)) ?-)
168 (concat "Add " copyright-current-year 186 (eq (char-after (+ (point) size -2)) ?-)))
169 " to copyright? ")))))) 187 ;; This is a range so just replace the end part.
170 (if replace 188 (delete-char size)
171 (replace-match copyright-current-year t t nil 3) 189 ;; Insert a comma with the preferred number of spaces.
172 (let ((size (save-excursion (skip-chars-backward "0-9")))) 190 (insert
173 (if (and (eq (% (- (string-to-number copyright-current-year) 191 (save-excursion
174 (string-to-number (buffer-substring 192 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
175 (+ (point) size) 193 (line-beginning-position) t)
176 (point)))) 194 (match-string 1)
177 100) 195 ", ")))
178 1) 196 ;; If people use the '91 '92 '93 scheme, do that as well.
179 (or (eq (char-after (+ (point) size -1)) ?-) 197 (if (eq (char-after (+ (point) size -3)) ?')
180 (eq (char-after (+ (point) size -2)) ?-))) 198 (insert ?')))
181 ;; This is a range so just replace the end part. 199 ;; Finally insert the new year.
182 (delete-char size) 200 (insert (substring copyright-current-year size)))))))
183 ;; Insert a comma with the preferred number of spaces.
184 (insert
185 (save-excursion
186 (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
187 (line-beginning-position) t)
188 (match-string 1)
189 ", ")))
190 ;; If people use the '91 '92 '93 scheme, do that as well.
191 (if (eq (char-after (+ (point) size -3)) ?')
192 (insert ?')))
193 ;; Finally insert the new year.
194 (insert (substring copyright-current-year size))))))))
195 201
196 ;;;###autoload 202 ;;;###autoload
197 (defun copyright-update (&optional arg interactivep) 203 (defun copyright-update (&optional arg interactivep)
198 "Update copyright notice to indicate the current year. 204 "Update copyright notice to indicate the current year.
199 With prefix ARG, replace the years in the notice rather than adding 205 With prefix ARG, replace the years in the notice rather than adding
208 (and (eq copyright-query 'function) interactivep)))) 214 (and (eq copyright-query 'function) interactivep))))
209 (save-excursion 215 (save-excursion
210 (save-restriction 216 (save-restriction
211 (widen) 217 (widen)
212 (goto-char (copyright-start-point)) 218 (goto-char (copyright-start-point))
213 (copyright-update-year arg noquery) 219 ;; If names-regexp doesn't match, we should not mess with
214 (goto-char (copyright-start-point)) 220 ;; the years _or_ the GPL version.
215 (and copyright-current-gpl-version 221 (when (copyright-find-copyright)
216 ;; match the GPL version comment in .el files, including the 222 (copyright-update-year arg noquery)
217 ;; bilingual Esperanto one in two-column, and in texinfo.tex 223 (goto-char (copyright-start-point))
218 (copyright-re-search 224 (and copyright-current-gpl-version
219 "\\(the Free Software Foundation;\ 225 ;; Match the GPL version comment in .el files.
220 either \\|; a\\^u eldono \\([0-9]+\\)a, ? a\\^u (la\\^u via \\)\ 226 ;; This is sensitive to line-breaks. :(
221 version \\([0-9]+\\), or (at" 227 (copyright-re-search
222 (copyright-limit) t) 228 "the Free Software Foundation[,;\n].*either version \
223 ;; Don't update if the file is already using a more recent 229 \\([0-9]+\\)\\(?: of the License\\)?, or[ \n].*any later version"
224 ;; version than the "current" one. 230 (copyright-limit) t)
225 (< (string-to-number (match-string 3)) 231 ;; Don't update if the file is already using a more recent
226 (string-to-number copyright-current-gpl-version)) 232 ;; version than the "current" one.
227 (or noquery 233 (< (string-to-number (match-string 1))
228 (save-match-data 234 (string-to-number copyright-current-gpl-version))
229 (save-window-excursion 235 (or noquery
230 (switch-to-buffer (current-buffer)) 236 (save-match-data
231 (y-or-n-p (format "Replace GPL version by %s? " 237 (goto-char (match-end 1))
232 copyright-current-gpl-version))))) 238 (save-window-excursion
233 (progn 239 (switch-to-buffer (current-buffer))
234 (if (match-end 2) 240 (y-or-n-p
235 ;; Esperanto bilingual comment in two-column.el 241 (format "Replace GPL version %s with version %s? "
236 (replace-match copyright-current-gpl-version t t nil 2)) 242 (match-string-no-properties 1)
237 (replace-match copyright-current-gpl-version t t nil 3)))) 243 copyright-current-gpl-version)))))
244 (replace-match copyright-current-gpl-version t t nil 1))))
238 (set (make-local-variable 'copyright-update) nil))) 245 (set (make-local-variable 'copyright-update) nil)))
239 ;; If a write-file-hook returns non-nil, the file is presumed to be written. 246 ;; If a write-file-hook returns non-nil, the file is presumed to be written.
240 nil)) 247 nil))
241 248
242 249