copyright.el 14 KB

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