copyright.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. ;;; copyright.el --- update the copyright notice in current buffer
  2. ;; Copyright (C) 1991-1995, 1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
  4. ;; Keywords: maint, tools
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Allows updating the copyright year and above mentioned GPL version manually
  18. ;; or when saving a file.
  19. ;; Do (add-hook 'before-save-hook 'copyright-update), or use
  20. ;; M-x customize-variable RET before-save-hook RET.
  21. ;;; Code:
  22. (defgroup copyright nil
  23. "Update the copyright notice in current buffer."
  24. :group 'tools)
  25. (defcustom copyright-limit 2000
  26. "Don't try to update copyright beyond this position unless interactive.
  27. A value of nil means to search whole buffer."
  28. :group 'copyright
  29. :type '(choice (integer :tag "Limit")
  30. (const :tag "No limit")))
  31. (defcustom copyright-at-end-flag nil
  32. "Non-nil means to search backwards from the end of the buffer for copyright.
  33. This is useful for ChangeLogs."
  34. :group 'copyright
  35. :type 'boolean
  36. :version "23.1")
  37. ;;;###autoload(put 'copyright-at-end-flag 'safe-local-variable 'booleanp)
  38. (defcustom copyright-regexp
  39. "\\(©\\|@copyright{}\\|[Cc]opyright\\s *:?\\s *\\(?:(C)\\)?\
  40. \\|[Cc]opyright\\s *:?\\s *©\\)\
  41. \\s *\\(?:[^0-9\n]*\\s *\\)?\
  42. \\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
  43. "What your copyright notice looks like.
  44. The second \\( \\) construct must match the years."
  45. :group 'copyright
  46. :type 'regexp)
  47. (defcustom copyright-names-regexp ""
  48. "Regexp matching the names which correspond to the user.
  49. Only copyright lines where the name matches this regexp will be updated.
  50. This allows you to avoid adding years to a copyright notice belonging to
  51. someone else or to a group for which you do not work."
  52. :group 'copyright
  53. :type 'regexp)
  54. ;; The worst that can happen is a malicious regexp that overflows in
  55. ;; the regexp matcher, a minor nuisance. It's a pain to be always
  56. ;; prompted if you want to put this in a dir-locals.el.
  57. ;;;###autoload(put 'copyright-names-regexp 'safe-local-variable 'stringp)
  58. (defcustom copyright-years-regexp
  59. "\\(\\s *\\)\\([1-9]\\([-0-9, ';/*%#\n\t]\\|\\s<\\|\\s>\\)*[0-9]+\\)"
  60. "Match additional copyright notice years.
  61. The second \\( \\) construct must match the years."
  62. :group 'copyright
  63. :type 'regexp)
  64. ;; See "Copyright Notices" in maintain.info.
  65. ;; TODO? 'end only for ranges at the end, other for all ranges.
  66. ;; Minimum limit on the size of a range?
  67. (defcustom copyright-year-ranges nil
  68. "Non-nil if individual consecutive years should be replaced with a range.
  69. For example: 2005, 2006, 2007, 2008 might be replaced with 2005-2008.
  70. If you use ranges, you should add an explanatory note in a README file.
  71. The function `copyright-fix-years' respects this variable."
  72. :group 'copyright
  73. :type 'boolean
  74. :version "24.1")
  75. ;;;###autoload(put 'copyright-year-ranges 'safe-local-variable 'booleanp)
  76. (defcustom copyright-query 'function
  77. "If non-nil, ask user before changing copyright.
  78. When this is `function', only ask when called non-interactively."
  79. :group 'copyright
  80. :type '(choice (const :tag "Do not ask")
  81. (const :tag "Ask unless interactive" function)
  82. (other :tag "Ask" t)))
  83. ;; when modifying this, also modify the comment generated by autoinsert.el
  84. (defconst copyright-current-gpl-version "3"
  85. "String representing the current version of the GPL or nil.")
  86. (defvar copyright-update t
  87. "The function `copyright-update' sets this to nil after updating a buffer.")
  88. ;; This is a defvar rather than a defconst, because the year can
  89. ;; change during the Emacs session.
  90. (defvar copyright-current-year (substring (current-time-string) -4)
  91. "String representing the current year.")
  92. (defsubst copyright-limit () ; re-search-forward BOUND
  93. (and copyright-limit
  94. (if copyright-at-end-flag
  95. (- (point) copyright-limit)
  96. (+ (point) copyright-limit))))
  97. (defun copyright-re-search (regexp &optional bound noerror count)
  98. "Re-search forward or backward depending on `copyright-at-end-flag'."
  99. (if copyright-at-end-flag
  100. (re-search-backward regexp bound noerror count)
  101. (re-search-forward regexp bound noerror count)))
  102. (defun copyright-start-point ()
  103. "Return point-min or point-max, depending on `copyright-at-end-flag'."
  104. (if copyright-at-end-flag
  105. (point-max)
  106. (point-min)))
  107. (defun copyright-offset-too-large-p ()
  108. "Return non-nil if point is too far from the edge of the buffer."
  109. (when copyright-limit
  110. (if copyright-at-end-flag
  111. (< (point) (- (point-max) copyright-limit))
  112. (> (point) (+ (point-min) copyright-limit)))))
  113. (defun copyright-find-copyright ()
  114. "Return non-nil if a copyright header suitable for updating is found.
  115. The header must match `copyright-regexp' and `copyright-names-regexp', if set.
  116. This function sets the match-data that `copyright-update-year' uses."
  117. (widen)
  118. (goto-char (copyright-start-point))
  119. (condition-case err
  120. ;; (1) Need the extra \\( \\) around copyright-regexp because we
  121. ;; goto (match-end 1) below. See note (2) below.
  122. (copyright-re-search (concat "\\(" copyright-regexp
  123. "\\)\\([ \t]*\n\\)?.*\\(?:"
  124. copyright-names-regexp "\\)")
  125. (copyright-limit)
  126. t)
  127. ;; In case the regexp is rejected. This is useful because
  128. ;; copyright-update is typically called from before-save-hook where
  129. ;; such an error is very inconvenient for the user.
  130. (error (message "Can't update copyright: %s" err) nil)))
  131. (defun copyright-find-end ()
  132. "Possibly adjust the search performed by `copyright-find-copyright'.
  133. If the years continue onto multiple lines that are marked as comments,
  134. skips to the end of all the years."
  135. (while (save-excursion
  136. (and (eq (following-char) ?,)
  137. (progn (forward-char 1) t)
  138. (progn (skip-chars-forward " \t") (eolp))
  139. comment-start-skip
  140. (save-match-data
  141. (forward-line 1)
  142. (and (looking-at comment-start-skip)
  143. (goto-char (match-end 0))))
  144. (looking-at-p copyright-years-regexp)))
  145. (forward-line 1)
  146. (re-search-forward comment-start-skip)
  147. ;; (2) Need the extra \\( \\) so that the years are subexp 3, as
  148. ;; they are at note (1) above.
  149. (re-search-forward (format "\\(%s\\)" copyright-years-regexp))))
  150. (defun copyright-update-year (replace noquery)
  151. ;; This uses the match-data from copyright-find-copyright/end.
  152. (goto-char (match-end 1))
  153. (copyright-find-end)
  154. ;; Note that `current-time-string' isn't locale-sensitive.
  155. (setq copyright-current-year (substring (current-time-string) -4))
  156. (unless (string= (buffer-substring (- (match-end 3) 2) (match-end 3))
  157. (substring copyright-current-year -2))
  158. (if (or noquery
  159. (save-window-excursion
  160. (switch-to-buffer (current-buffer))
  161. ;; Fixes some point-moving oddness (bug#2209).
  162. (save-excursion
  163. (y-or-n-p (if replace
  164. (concat "Replace copyright year(s) by "
  165. copyright-current-year "? ")
  166. (concat "Add " copyright-current-year
  167. " to copyright? "))))))
  168. (if replace
  169. (replace-match copyright-current-year t t nil 3)
  170. (let ((size (save-excursion (skip-chars-backward "0-9"))))
  171. (if (and (eq (% (- (string-to-number copyright-current-year)
  172. (string-to-number (buffer-substring
  173. (+ (point) size)
  174. (point))))
  175. 100)
  176. 1)
  177. (or (eq (char-after (+ (point) size -1)) ?-)
  178. (eq (char-after (+ (point) size -2)) ?-)))
  179. ;; This is a range so just replace the end part.
  180. (delete-char size)
  181. ;; Insert a comma with the preferred number of spaces.
  182. (insert
  183. (save-excursion
  184. (if (re-search-backward "[0-9]\\( *, *\\)[0-9]"
  185. (line-beginning-position) t)
  186. (match-string 1)
  187. ", ")))
  188. ;; If people use the '91 '92 '93 scheme, do that as well.
  189. (if (eq (char-after (+ (point) size -3)) ?')
  190. (insert ?')))
  191. ;; Finally insert the new year.
  192. (insert (substring copyright-current-year size)))))))
  193. ;;;###autoload
  194. (defun copyright-update (&optional arg interactivep)
  195. "Update copyright notice to indicate the current year.
  196. With prefix ARG, replace the years in the notice rather than adding
  197. the current year after them. If necessary, and
  198. `copyright-current-gpl-version' is set, any copying permissions
  199. following the copyright are updated as well.
  200. If non-nil, INTERACTIVEP tells the function to behave as when it's called
  201. interactively."
  202. (interactive "*P\nd")
  203. (when (or copyright-update interactivep)
  204. (let ((noquery (or (not copyright-query)
  205. (and (eq copyright-query 'function) interactivep))))
  206. (save-excursion
  207. (save-restriction
  208. ;; If names-regexp doesn't match, we should not mess with
  209. ;; the years _or_ the GPL version.
  210. ;; TODO there may be multiple copyrights we should update.
  211. (when (copyright-find-copyright)
  212. (copyright-update-year arg noquery)
  213. (goto-char (copyright-start-point))
  214. (and copyright-current-gpl-version
  215. ;; Match the GPL version comment in .el files.
  216. ;; This is sensitive to line-breaks. :(
  217. (copyright-re-search
  218. "the Free Software Foundation[,;\n].*either version \
  219. \\([0-9]+\\)\\(?: of the License\\)?, or[ \n].*any later version"
  220. (copyright-limit) t)
  221. ;; Don't update if the file is already using a more recent
  222. ;; version than the "current" one.
  223. (< (string-to-number (match-string 1))
  224. (string-to-number copyright-current-gpl-version))
  225. (or noquery
  226. (save-match-data
  227. (goto-char (match-end 1))
  228. (save-window-excursion
  229. (switch-to-buffer (current-buffer))
  230. (y-or-n-p
  231. (format "Replace GPL version %s with version %s? "
  232. (match-string-no-properties 1)
  233. copyright-current-gpl-version)))))
  234. (replace-match copyright-current-gpl-version t t nil 1))))
  235. (set (make-local-variable 'copyright-update) nil)))
  236. ;; If a write-file-hook returns non-nil, the file is presumed to be written.
  237. nil))
  238. ;; FIXME heuristic should be within 50 years of present (cf calendar).
  239. ;;;###autoload
  240. (defun copyright-fix-years ()
  241. "Convert 2 digit years to 4 digit years.
  242. Uses heuristic: year >= 50 means 19xx, < 50 means 20xx.
  243. If `copyright-year-ranges' (which see) is non-nil, also
  244. independently replaces consecutive years with a range."
  245. (interactive)
  246. ;; TODO there may be multiple copyrights we should fix.
  247. (if (copyright-find-copyright)
  248. (let ((s (match-beginning 3))
  249. (p (make-marker))
  250. ;; Not line-beg-pos, so we don't mess up leading whitespace.
  251. (copystart (match-beginning 0))
  252. e last sep year prev-year first-year range-start range-end)
  253. ;; In case years are continued over multiple, commented lines.
  254. (goto-char (match-end 1))
  255. (copyright-find-end)
  256. (setq e (copy-marker (1+ (match-end 3))))
  257. (goto-char s)
  258. (while (re-search-forward "[0-9]+" e t)
  259. (set-marker p (point))
  260. (goto-char (match-beginning 0))
  261. (setq year (string-to-number (match-string 0)))
  262. (and (setq sep (char-before))
  263. (/= (char-syntax sep) ?\s)
  264. (/= sep ?-)
  265. (insert " "))
  266. (when (< year 100)
  267. (insert (if (>= year 50) "19" "20"))
  268. (setq year (+ year (if (>= year 50) 1900 2000))))
  269. (goto-char p)
  270. (when copyright-year-ranges
  271. ;; If the previous thing was a range, don't try to tack more on.
  272. ;; Ie not 2000-2005 -> 2000-2005-2007
  273. ;; TODO should merge into existing range if possible.
  274. (if (eq sep ?-)
  275. (setq prev-year nil
  276. year nil)
  277. (if (and prev-year (= year (1+ prev-year)))
  278. (setq range-end (point))
  279. (when (and first-year prev-year
  280. (> prev-year first-year))
  281. (goto-char range-end)
  282. (delete-region range-start range-end)
  283. (insert (format "-%d" prev-year))
  284. (goto-char p))
  285. (setq first-year year
  286. range-start (point)))))
  287. (setq prev-year year
  288. last p))
  289. (when last
  290. (when (and copyright-year-ranges
  291. first-year prev-year
  292. (> prev-year first-year))
  293. (goto-char range-end)
  294. (delete-region range-start range-end)
  295. (insert (format "-%d" prev-year)))
  296. (goto-char last)
  297. ;; Don't mess up whitespace after the years.
  298. (skip-chars-backward " \t")
  299. (save-restriction
  300. (narrow-to-region copystart (point))
  301. ;; This is clearly wrong, eg what about comment markers?
  302. ;;; (let ((fill-prefix " "))
  303. ;; TODO do not break copyright owner over lines.
  304. (fill-region (point-min) (point-max))))
  305. (set-marker e nil)
  306. (set-marker p nil))
  307. ;; Simply reformatting the years is not copyrightable, so it does
  308. ;; not seem right to call this. Also it messes with ranges.
  309. ;;; (copyright-update nil t))
  310. (message "No copyright message")))
  311. ;;;###autoload
  312. (define-skeleton copyright
  313. "Insert a copyright by $ORGANIZATION notice at cursor."
  314. "Company: "
  315. comment-start
  316. "Copyright (C) " `(substring (current-time-string) -4) " by "
  317. (or (getenv "ORGANIZATION")
  318. str)
  319. '(if (copyright-offset-too-large-p)
  320. (message "Copyright extends beyond `copyright-limit' and won't be updated automatically."))
  321. comment-end \n)
  322. ;; TODO: recurse, exclude COPYING etc.
  323. ;;;###autoload
  324. (defun copyright-update-directory (directory match &optional fix)
  325. "Update copyright notice for all files in DIRECTORY matching MATCH.
  326. If FIX is non-nil, run `copyright-fix-years' instead."
  327. (interactive "DDirectory: \nMFilenames matching (regexp): ")
  328. (dolist (file (directory-files directory t match nil))
  329. (unless (file-directory-p file)
  330. (message "Updating file `%s'" file)
  331. ;; FIXME we should not use find-file+save+kill.
  332. (let ((enable-local-variables :safe)
  333. (enable-local-eval nil))
  334. (find-file file))
  335. (let ((inhibit-read-only t))
  336. (if fix
  337. (copyright-fix-years)
  338. (copyright-update)))
  339. (save-buffer)
  340. (kill-buffer (current-buffer)))))
  341. (provide 'copyright)
  342. ;; For the copyright sign:
  343. ;; Local Variables:
  344. ;; coding: utf-8
  345. ;; End:
  346. ;;; copyright.el ends here