+;; This is a defvar rather than a defconst, because the year can
+;; change during the Emacs session.
+(defvar copyright-current-year (substring (current-time-string) -4)
+ "String representing the current year.")
+
+(defun copyright-update-year (replace noquery)
+ (when (re-search-forward copyright-regexp (+ (point) copyright-limit) t)
+ ;; If the years are continued onto multiple lined
+ ;; that are marked as comments, skip to the end of the years anyway.
+ (while (save-excursion
+ (and (eq (following-char) ?,)
+ (progn (forward-char 1) t)
+ (progn (skip-chars-forward " \t") (eolp))
+ comment-start-skip
+ (save-match-data
+ (forward-line 1)
+ (and (looking-at comment-start-skip)
+ (goto-char (match-end 0))))
+ (save-match-data
+ (looking-at copyright-years-regexp))))
+ (forward-line 1)
+ (re-search-forward comment-start-skip)
+ (re-search-forward copyright-years-regexp))
+
+ ;; 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))
+ (substring copyright-current-year -2))
+ (if (or noquery
+ (y-or-n-p (if replace
+ (concat "Replace copyright year(s) by "
+ copyright-current-year "? ")
+ (concat "Add " copyright-current-year
+ " to copyright? "))))
+ (if replace
+ (replace-match copyright-current-year t t nil 2)
+ (let ((size (save-excursion (skip-chars-backward "0-9"))))
+ (if (and (eq (% (- (string-to-number copyright-current-year)
+ (string-to-number (buffer-substring
+ (+ (point) size)
+ (point))))
+ 100)
+ 1)
+ (or (eq (char-after (+ (point) size -1)) ?-)
+ (eq (char-after (+ (point) size -2)) ?-)))
+ ;; This is a range so just replace the end part.
+ (delete-char size)
+ ;; Insert a comma with the preferred number of spaces.
+ (insert
+ (save-excursion
+ (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
+ (line-beginning-position) t)
+ (match-string 1)
+ ", ")))
+ ;; If people use the '91 '92 '93 scheme, do that as well.
+ (if (eq (char-after (+ (point) size -3)) ?')
+ (insert ?')))
+ ;; Finally insert the new year.
+ (insert (substring copyright-current-year size))))))))
+