Re: Adding missing copyright years

From:

Stefan Monnier

Subject:

Re: Adding missing copyright years

Date:

Sun, 08 May 2005 22:06:27 -0400

User-agent:

Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

>> > (add-hook 'before-save-hook 'copyright-update)
>>
>> This is not a problem since you are asked whether you want to update the
>> copyright.
> Indeed; I use it, and I don't think it's dangerous in the way Richard
> suggested (it always leaves the final decision to the user).
> However, I can see why some people would not like to use it in its
> current form -- it's generally quite stupid, and often mistakenly
> thinks the copyright may need updating when the current copyright line
> is actually up-to-date. So it tends to ask you whether you want to
> update the copyright far more often than it should (for example, it
> doesn't understand wrapped copyright lines, and gets confused if there
> are multiple copyrights).
I haven't seen the problem with wrapped lines, but as far as multiple
copyrights goes I use a the local hack below where I can specify a regexp
for the copyrights that I'm willing to change (e.g. I don't want my edits to
add a year to the copyright of someone else, right?).
I've then set copyright-names-regexp to "Monnier\\|Free Software".
This way when I edit a file with multiple copyrights, only the relevant line
is checked/updated. I've only written it recently (after the feature
freeze), which is why I haven't tried to install it.
Stefan
--- orig/lisp/emacs-lisp/copyright.el
+++ mod/lisp/emacs-lisp/copyright.el
@@ -54,6 +54,13 @@
:group 'copyright
:type 'regexp)
+(defcustom copyright-names-regexp ""
+ "Regexp matching the names which correspond to the user.
+Only copyright lines where the name matches this regexp will be updated.
+This allows you to avoid adding yars to a copyright notice belonging to
+someone else or to a group for which you do not work."
+ :type 'regexp)
+
(defcustom copyright-years-regexp
"\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
"*Match additional copyright notice years.
@@ -83,7 +90,17 @@
"String representing the current year.")
(defun copyright-update-year (replace noquery)
- (when (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
+ (when
+ (condition-case err
+ (re-search-forward (concat "\\(" copyright-regexp
+ "\\)\\([ \t]*\n\\)?.*\\(?:"
+ copyright-names-regexp "\\)")
+ (+ (point) copyright-limit) t)
+ ;; In case the regexp is rejected. This is useful because
+ ;; copyright-update is typically called from before-save-hook where
+ ;; such an error is very inconvenient for the user.
+ (error (message "Can't update copyright: %s" err) nil))
+ (goto-char (match-end 1))
;; If the years are continued onto multiple lined
;; that are marked as comments, skip to the end of the years anyway.
(while (save-excursion
@@ -94,7 +111,7 @@
(save-match-data
(forward-line 1)
(and (looking-at comment-start-skip)
- (goto-char (match-end 0))))
+ (goto-char (match-end 1))))
(save-match-data
(looking-at copyright-years-regexp))))
(forward-line 1)
@@ -103,7 +120,7 @@
;; Note that `current-time-string' isn't locale-sensitive.
(setq copyright-current-year (substring (current-time-string) -4))
- (unless (string= (buffer-substring (- (match-end 2) 2) (match-end 2))
+ (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
(substring copyright-current-year -2))
(if (or noquery
(y-or-n-p (if replace