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