| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 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. |
|---|
| 41 |
A value of nil means to search whole buffer." |
|---|
| 42 |
:group 'copyright |
|---|
| 43 |
:type '(choice (integer :tag "Limit") |
|---|
| 44 |
(const :tag "No limit"))) |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 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) |
|---|
| 56 |
|
|---|
| 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) |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 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))) |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
(defconst copyright-current-gpl-version "3" |
|---|
| 76 |
"String representing the current version of the GPL or nil.") |
|---|
| 77 |
|
|---|
| 78 |
(defvar copyright-update t) |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 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 |
|---|
| 87 |
(if copyright-limit (+ (point) copyright-limit)) t) |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 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)) |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 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) |
|---|
| 123 |
1) |
|---|
| 124 |
(or (eq (char-after (+ (point) size -1)) ?-) |
|---|
| 125 |
(eq (char-after (+ (point) size -2)) ?-))) |
|---|
| 126 |
|
|---|
| 127 |
(delete-char size) |
|---|
| 128 |
|
|---|
| 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 |
|
|---|
| 136 |
(if (eq (char-after (+ (point) size -3)) ?') |
|---|
| 137 |
(insert ?'))) |
|---|
| 138 |
|
|---|
| 139 |
(insert (substring copyright-current-year size)))))))) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 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 |
|
|---|
| 162 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 179 |
nil)) |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 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 |
|
|---|
| 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"))) |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 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) |
|---|
| 233 |
|
|---|
| 234 |
(provide 'copyright) |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|