Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / admin / admin.el
blob6e05a883c2b66cb57f3d6bf7dc0b68fd602e1556
1 ;;; admin.el --- utilities for Emacs administration
3 ;; Copyright (C) 2001-2014 Free Software Foundation, Inc.
5 ;; 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.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;; add-release-logs Add ``Version X released'' change log entries.
23 ;; set-version Change Emacs version number in source tree.
24 ;; set-copyright Change Emacs short copyright string (eg as
25 ;; printed by --version) in source tree.
27 ;;; Code:
29 (defvar add-log-time-format) ; in add-log
31 ;; Does this information need to be in every ChangeLog, as opposed to
32 ;; just the top-level one? Only if you allow changes the same
33 ;; day as the release.
34 ;; http://lists.gnu.org/archive/html/emacs-devel/2013-03/msg00161.html
35 (defun add-release-logs (root version &optional date)
36 "Add \"Version VERSION released.\" change log entries in ROOT.
37 Root must be the root of an Emacs source tree.
38 Optional argument DATE is the release date, default today."
39 (interactive (list (read-directory-name "Emacs root directory: ")
40 (read-string "Version number: "
41 (format "%s.%s" emacs-major-version
42 emacs-minor-version))
43 (read-string "Release date: "
44 (progn (require 'add-log)
45 (let ((add-log-time-zone-rule t))
46 (funcall add-log-time-format))))))
47 (setq root (expand-file-name root))
48 (unless (file-exists-p (expand-file-name "src/emacs.c" root))
49 (user-error "%s doesn't seem to be the root of an Emacs source tree" root))
50 (require 'add-log)
51 (or date (setq date (let ((add-log-time-zone-rule t))
52 (funcall add-log-time-format))))
53 (let* ((logs (process-lines "find" root "-name" "ChangeLog"))
54 (entry (format "%s %s <%s>\n\n\t* Version %s released.\n\n"
55 date
56 (or add-log-full-name (user-full-name))
57 (or add-log-mailing-address user-mail-address)
58 version)))
59 (dolist (log logs)
60 (find-file log)
61 (goto-char (point-min))
62 (insert entry))))
64 (defun set-version-in-file (root file version rx)
65 "Subroutine of `set-version' and `set-copyright'."
66 (find-file (expand-file-name file root))
67 (goto-char (point-min))
68 (unless (re-search-forward rx nil :noerror)
69 (user-error "Version not found in %s" file))
70 (replace-match (format "%s" version) nil nil nil 1))
72 ;; TODO report the progress
73 (defun set-version (root version)
74 "Set Emacs version to VERSION in relevant files under ROOT.
75 Root must be the root of an Emacs source tree."
76 (interactive "DEmacs root directory: \nsVersion number: ")
77 (unless (file-exists-p (expand-file-name "src/emacs.c" root))
78 (user-error "%s doesn't seem to be the root of an Emacs source tree" root))
79 ;; There's also a "version 3" (standing for GPLv3) at the end of
80 ;; `README', but since `set-version-in-file' only replaces the first
81 ;; occurence, it won't be replaced.
82 (set-version-in-file root "README" version
83 (rx (and "version" (1+ space)
84 (submatch (1+ (in "0-9."))))))
85 (set-version-in-file root "configure.ac" version
86 (rx (and "AC_INIT" (1+ (not (in ?,)))
87 ?, (0+ space)
88 (submatch (1+ (in "0-9."))))))
89 (set-version-in-file root "doc/emacs/emacsver.texi" version
90 (rx (and "EMACSVER" (1+ space)
91 (submatch (1+ (in "0-9."))))))
92 (set-version-in-file root "doc/man/emacs.1" version
93 (rx (and ".TH EMACS" (1+ not-newline)
94 "GNU Emacs" (1+ space)
95 (submatch (1+ (in "0-9."))))))
96 (set-version-in-file root "nt/config.nt" version
97 (rx (and bol "#" (0+ blank) "define" (1+ blank)
98 "VERSION" (1+ blank) "\""
99 (submatch (1+ (in "0-9."))))))
100 (set-version-in-file root "msdos/sed2v2.inp" version
101 (rx (and bol "/^#undef " (1+ not-newline)
102 "define VERSION" (1+ space) "\""
103 (submatch (1+ (in "0-9."))))))
104 (set-version-in-file root "nt/makefile.w32-in" version
105 (rx (and "VERSION" (0+ space) "=" (0+ space)
106 (submatch (1+ (in "0-9."))))))
107 ;; nt/emacs.rc also contains the version number, but in an awkward
108 ;; format. It must contain four components, separated by commas, and
109 ;; in two places those commas are followed by space, in two other
110 ;; places they are not.
111 (let* ((version-components (append (split-string version "\\.")
112 '("0" "0")))
113 (comma-version
114 (concat (car version-components) ","
115 (cadr version-components) ","
116 (cadr (cdr version-components)) ","
117 (cadr (cdr (cdr version-components)))))
118 (comma-space-version
119 (concat (car version-components) ", "
120 (cadr version-components) ", "
121 (cadr (cdr version-components)) ", "
122 (cadr (cdr (cdr version-components))))))
123 (set-version-in-file root "nt/emacs.rc" comma-version
124 (rx (and "FILEVERSION" (1+ space)
125 (submatch (1+ (in "0-9,"))))))
126 (set-version-in-file root "nt/emacs.rc" comma-version
127 (rx (and "PRODUCTVERSION" (1+ space)
128 (submatch (1+ (in "0-9,"))))))
129 (set-version-in-file root "nt/emacs.rc" comma-space-version
130 (rx (and "\"FileVersion\"" (0+ space) ?, (0+ space)
131 ?\" (submatch (1+ (in "0-9, "))) "\\0\"")))
132 (set-version-in-file root "nt/emacs.rc" comma-space-version
133 (rx (and "\"ProductVersion\"" (0+ space) ?,
134 (0+ space) ?\" (submatch (1+ (in "0-9, ")))
135 "\\0\"")))
136 ;; Likewise for emacsclient.rc
137 (set-version-in-file root "nt/emacsclient.rc" comma-version
138 (rx (and "FILEVERSION" (1+ space)
139 (submatch (1+ (in "0-9,"))))))
140 (set-version-in-file root "nt/emacsclient.rc" comma-version
141 (rx (and "PRODUCTVERSION" (1+ space)
142 (submatch (1+ (in "0-9,"))))))
143 (set-version-in-file root "nt/emacsclient.rc" comma-space-version
144 (rx (and "\"FileVersion\"" (0+ space) ?, (0+ space)
145 ?\" (submatch (1+ (in "0-9, "))) "\\0\"")))
146 (set-version-in-file root "nt/emacsclient.rc" comma-space-version
147 (rx (and "\"ProductVersion\"" (0+ space) ?,
148 (0+ space) ?\" (submatch (1+ (in "0-9, ")))
149 "\\0\"")))
150 ;; Major version only.
151 (when (string-match "\\([0-9]\\{2,\\}\\)" version)
152 (setq version (match-string 1 version))
153 (set-version-in-file root "src/msdos.c" version
154 (rx (and "Vwindow_system_version" (1+ not-newline)
155 ?\( (submatch (1+ (in "0-9"))) ?\))))
156 (set-version-in-file root "etc/refcards/ru-refcard.tex" version
157 "\\\\newcommand{\\\\versionemacs}\\[0\\]\
158 {\\([0-9]\\{2,\\}\\)}.+%.+version of Emacs")
159 (set-version-in-file root "etc/refcards/emacsver.tex" version
160 "\\\\def\\\\versionemacs\
161 {\\([0-9]\\{2,\\}\\)}.+%.+version of Emacs"))))
164 ;; Note this makes some assumptions about form of short copyright.
165 ;; TODO report the progress
166 (defun set-copyright (root copyright)
167 "Set Emacs short copyright to COPYRIGHT in relevant files under ROOT.
168 Root must be the root of an Emacs source tree."
169 (interactive (list
170 (read-directory-name "Emacs root directory: " nil nil t)
171 (read-string
172 "Short copyright string: "
173 (format "Copyright (C) %s Free Software Foundation, Inc."
174 (format-time-string "%Y")))))
175 (unless (file-exists-p (expand-file-name "src/emacs.c" root))
176 (user-error "%s doesn't seem to be the root of an Emacs source tree" root))
177 (set-version-in-file root "configure.ac" copyright
178 (rx (and bol "copyright" (0+ (not (in ?\")))
179 ?\" (submatch (1+ (not (in ?\")))) ?\")))
180 (set-version-in-file root "msdos/sed2v2.inp" copyright
181 (rx (and bol "/^#undef " (1+ not-newline)
182 "define COPYRIGHT" (1+ space)
183 ?\" (submatch (1+ (not (in ?\")))) ?\")))
184 (set-version-in-file root "nt/config.nt" copyright
185 (rx (and bol "#" (0+ blank) "define" (1+ blank)
186 "COPYRIGHT" (1+ blank)
187 ?\" (submatch (1+ (not (in ?\")))) ?\")))
188 (set-version-in-file root "lib-src/rcs2log" copyright
189 (rx (and "Copyright" (0+ space) ?= (0+ space)
190 ?\' (submatch (1+ nonl)))))
191 (when (string-match "\\([0-9]\\{4\\}\\)" copyright)
192 (setq copyright (match-string 1 copyright))
193 (set-version-in-file root "etc/refcards/ru-refcard.tex" copyright
194 "\\\\newcommand{\\\\cyear}\\[0\\]\
195 {\\([0-9]\\{4\\}\\)}.+%.+copyright year")
196 (set-version-in-file root "etc/refcards/emacsver.tex" copyright
197 "\\\\def\\\\year\
198 {\\([0-9]\\{4\\}\\)}.+%.+copyright year")))
200 ;;; Various bits of magic for generating the web manuals
202 (defun manual-misc-manuals (root)
203 "Return doc/misc manuals as list of strings.
204 ROOT should be the root of an Emacs source tree."
205 ;; Similar to `make -C doc/misc echo-info', but works if unconfigured,
206 ;; and for INFO_TARGETS rather than INFO_INSTALL.
207 (with-temp-buffer
208 (insert-file-contents (expand-file-name "doc/misc/Makefile.in" root))
209 ;; Should really use expanded value of INFO_TARGETS.
210 (search-forward "INFO_COMMON = ")
211 (let ((start (point)))
212 (end-of-line)
213 (while (and (looking-back "\\\\")
214 (zerop (forward-line 1)))
215 (end-of-line))
216 (append (split-string (replace-regexp-in-string
217 "\\(\\\\\\|\\.info\\)" ""
218 (buffer-substring start (point))))
219 '("efaq-w32")))))
221 (defun make-manuals (root &optional type)
222 "Generate the web manuals for the Emacs webpage.
223 ROOT should be the root of an Emacs source tree.
224 Interactively with a prefix argument, prompt for TYPE.
225 Optional argument TYPE is type of output (nil means all)."
226 (interactive (let ((root (read-directory-name "Emacs root directory: "
227 source-directory nil t)))
228 (list root
229 (if current-prefix-arg
230 (completing-read
231 "Type: "
232 (append
233 '("misc" "pdf" "ps")
234 (let (res)
235 (dolist (i '("emacs" "elisp" "eintr") res)
236 (dolist (j '("" "-mono" "-node" "-ps" "-pdf"))
237 (push (concat i j) res))))
238 (manual-misc-manuals root)))))))
239 (let* ((dest (expand-file-name "manual" root))
240 (html-node-dir (expand-file-name "html_node" dest))
241 (html-mono-dir (expand-file-name "html_mono" dest))
242 (ps-dir (expand-file-name "ps" dest))
243 (pdf-dir (expand-file-name "pdf" dest))
244 (emacs (expand-file-name "doc/emacs/emacs.texi" root))
245 (elisp (expand-file-name "doc/lispref/elisp.texi" root))
246 (eintr (expand-file-name "doc/lispintro/emacs-lisp-intro.texi" root))
247 (misc (manual-misc-manuals root)))
248 ;; TODO this makes it non-continuable.
249 ;; Instead, delete the individual dest directory each time.
250 (when (file-directory-p dest)
251 (if (y-or-n-p (format "Directory %s exists, delete it first? " dest))
252 (delete-directory dest t)
253 (user-error "Aborted")))
254 (if (member type '(nil "emacs" "emacs-node"))
255 (manual-html-node emacs (expand-file-name "emacs" html-node-dir)))
256 (if (member type '(nil "emacs" "emacs-mono"))
257 (manual-html-mono emacs (expand-file-name "emacs.html" html-mono-dir)))
258 (if (member type '(nil "emacs" "emacs-pdf" "pdf"))
259 (manual-pdf emacs (expand-file-name "emacs.pdf" pdf-dir)))
260 (if (member type '(nil "emacs" "emacs-ps" "ps"))
261 (manual-ps emacs (expand-file-name "emacs.ps" ps-dir)))
262 (if (member type '(nil "elisp" "elisp-node"))
263 (manual-html-node elisp (expand-file-name "elisp" html-node-dir)))
264 (if (member type '(nil "elisp" "elisp-mono"))
265 (manual-html-mono elisp (expand-file-name "elisp.html" html-mono-dir)))
266 (if (member type '(nil "elisp" "elisp-pdf" "pdf"))
267 (manual-pdf elisp (expand-file-name "elisp.pdf" pdf-dir)))
268 (if (member type '(nil "elisp" "elisp-ps" "ps"))
269 (manual-ps elisp (expand-file-name "elisp.ps" ps-dir)))
270 (if (member type '(nil "eintr" "eintr-node"))
271 (manual-html-node eintr (expand-file-name "eintr" html-node-dir)))
272 (if (member type '(nil "eintr" "eintr-node"))
273 (manual-html-mono eintr (expand-file-name "eintr.html" html-mono-dir)))
274 (if (member type '(nil "eintr" "eintr-pdf" "pdf"))
275 (manual-pdf eintr (expand-file-name "eintr.pdf" pdf-dir)))
276 (if (member type '(nil "eintr" "eintr-ps" "ps"))
277 (manual-ps eintr (expand-file-name "eintr.ps" ps-dir)))
278 ;; Misc manuals
279 (dolist (manual misc)
280 (if (member type `(nil ,manual "misc"))
281 (manual-misc-html manual root html-node-dir html-mono-dir)))
282 (message "Manuals created in %s" dest)))
284 (defconst manual-doctype-string
285 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
286 \"http://www.w3.org/TR/html4/loose.dtd\">\n\n")
288 (defconst manual-meta-string
289 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
290 <link rev=\"made\" href=\"mailto:webmasters@gnu.org\">
291 <link rel=\"icon\" type=\"image/png\" href=\"/graphics/gnu-head-mini.png\">
292 <meta name=\"ICBM\" content=\"42.256233,-71.006581\">
293 <meta name=\"DC.title\" content=\"gnu.org\">\n\n")
295 (defconst manual-style-string "<style type=\"text/css\">
296 @import url('/software/emacs/manual.css');\n</style>\n")
298 (defun manual-misc-html (name root html-node-dir html-mono-dir)
299 ;; Hack to deal with the cases where .texi creates a different .info.
300 ;; Blech. TODO Why not just rename the .texi (or .info) files?
301 (let* ((texiname (cond ((equal name "ccmode") "cc-mode")
302 (t name)))
303 (texi (expand-file-name (format "doc/misc/%s.texi" texiname) root)))
304 (manual-html-node texi (expand-file-name name html-node-dir))
305 (manual-html-mono texi (expand-file-name (concat name ".html")
306 html-mono-dir))))
308 (defun manual-html-mono (texi-file dest)
309 "Run Makeinfo on TEXI-FILE, emitting mono HTML output to DEST.
310 This function also edits the HTML files so that they validate as
311 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
312 the @import directive."
313 (make-directory (or (file-name-directory dest) ".") t)
314 (call-process "makeinfo" nil nil nil
315 "-D" "WWW_GNU_ORG"
316 "-I" (expand-file-name "../emacs"
317 (file-name-directory texi-file))
318 "-I" (expand-file-name "../misc"
319 (file-name-directory texi-file))
320 "--html" "--no-split" texi-file "-o" dest)
321 (with-temp-buffer
322 (insert-file-contents dest)
323 (setq buffer-file-name dest)
324 (manual-html-fix-headers)
325 (manual-html-fix-index-1)
326 (manual-html-fix-index-2 t)
327 (manual-html-fix-node-div)
328 (goto-char (point-max))
329 (re-search-backward "</body>[\n \t]*</html>")
330 (insert "</div>\n\n")
331 (save-buffer)))
333 (defun manual-html-node (texi-file dir)
334 "Run Makeinfo on TEXI-FILE, emitting per-node HTML output to DIR.
335 This function also edits the HTML files so that they validate as
336 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
337 the @import directive."
338 (unless (file-exists-p texi-file)
339 (user-error "Manual file %s not found" texi-file))
340 (make-directory dir t)
341 (call-process "makeinfo" nil nil nil
342 "-D" "WWW_GNU_ORG"
343 "-I" (expand-file-name "../emacs"
344 (file-name-directory texi-file))
345 "-I" (expand-file-name "../misc"
346 (file-name-directory texi-file))
347 "--html" texi-file "-o" dir)
348 ;; Loop through the node files, fixing them up.
349 (dolist (f (directory-files dir nil "\\.html\\'"))
350 (let (opoint)
351 (with-temp-buffer
352 (insert-file-contents (expand-file-name f dir))
353 (setq buffer-file-name (expand-file-name f dir))
354 (if (looking-at "<meta http-equiv")
355 ;; Ignore those HTML files that are just redirects.
356 (set-buffer-modified-p nil)
357 (manual-html-fix-headers)
358 (if (equal f "index.html")
359 (let (copyright-text)
360 (manual-html-fix-index-1)
361 ;; Move copyright notice to the end.
362 (when (re-search-forward "[ \t]*<p>Copyright &copy;" nil t)
363 (setq opoint (match-beginning 0))
364 (re-search-forward "</blockquote>")
365 (setq copyright-text (buffer-substring opoint (point)))
366 (delete-region opoint (point)))
367 (manual-html-fix-index-2)
368 (if copyright-text
369 (insert copyright-text))
370 (insert "\n</div>\n"))
371 ;; For normal nodes, give the header div a blue bg.
372 (manual-html-fix-node-div))
373 (save-buffer))))))
375 (defun manual-pdf (texi-file dest)
376 "Run texi2pdf on TEXI-FILE, emitting PDF output to DEST."
377 (make-directory (or (file-name-directory dest) ".") t)
378 (let ((default-directory (file-name-directory texi-file)))
379 (call-process "texi2pdf" nil nil nil
380 "-I" "../emacs" "-I" "../misc"
381 texi-file "-o" dest)))
383 (defun manual-ps (texi-file dest)
384 "Generate a PostScript version of TEXI-FILE as DEST."
385 (make-directory (or (file-name-directory dest) ".") t)
386 (let ((dvi-dest (concat (file-name-sans-extension dest) ".dvi"))
387 (default-directory (file-name-directory texi-file)))
388 ;; FIXME: Use `texi2dvi --ps'? --xfq
389 (call-process "texi2dvi" nil nil nil
390 "-I" "../emacs" "-I" "../misc"
391 texi-file "-o" dvi-dest)
392 (call-process "dvips" nil nil nil dvi-dest "-o" dest)
393 (delete-file dvi-dest)
394 (call-process "gzip" nil nil nil dest)))
396 (defun manual-html-fix-headers ()
397 "Fix up HTML headers for the Emacs manual in the current buffer."
398 (let ((texi5 (search-forward "<!DOCTYPE" nil t))
399 opoint)
400 ;; Texinfo 5 supplies a DOCTYPE.
401 (or texi5
402 (insert manual-doctype-string))
403 (search-forward "<head>\n")
404 (insert manual-meta-string)
405 (search-forward "<meta")
406 (setq opoint (match-beginning 0))
407 (unless texi5
408 (search-forward "<!--")
409 (goto-char (match-beginning 0))
410 (delete-region opoint (point))
411 (search-forward "<meta http-equiv=\"Content-Style")
412 (setq opoint (match-beginning 0)))
413 (search-forward "</head>")
414 (goto-char (match-beginning 0))
415 (delete-region opoint (point))
416 (insert manual-style-string)))
418 (defun manual-html-fix-node-div ()
419 "Fix up HTML \"node\" divs in the current buffer."
420 (let (opoint div-end)
421 (while (search-forward "<div class=\"node\">" nil t)
422 (replace-match
423 "<div class=\"node\" style=\"background-color:#DDDDFF\">"
424 t t)
425 (setq opoint (point))
426 (re-search-forward "</div>")
427 (setq div-end (match-beginning 0))
428 (goto-char opoint)
429 (if (search-forward "<hr>" div-end 'move)
430 (replace-match "" t t)))))
432 (defun manual-html-fix-index-1 ()
433 (let (opoint)
434 (re-search-forward "<body.*>\n")
435 (setq opoint (match-end 0))
436 (search-forward "<h2 class=\"")
437 (goto-char (match-beginning 0))
438 (delete-region opoint (point))
439 (insert "<div id=\"content\" class=\"inner\">\n\n")))
441 (defun manual-html-fix-index-2 (&optional table-workaround)
442 "Replace the index list in the current buffer with a HTML table."
443 (let (done open-td tag desc)
444 ;; Convert the list that Makeinfo made into a table.
445 (or (search-forward "<ul class=\"menu\">" nil t)
446 (search-forward "<ul>"))
447 (replace-match "<table style=\"float:left\" width=\"100%\">")
448 (forward-line 1)
449 (while (not done)
450 (cond
451 ((or (looking-at "<li>\\(<a.+</a>\\):[ \t]+\\(.*\\)$")
452 (looking-at "<li>\\(<a.+</a>\\)$"))
453 (setq tag (match-string 1))
454 (setq desc (match-string 2))
455 (replace-match "" t t)
456 (when open-td
457 (save-excursion
458 (forward-char -1)
459 (skip-chars-backward " ")
460 (delete-region (point) (line-end-position))
461 (insert "</td>\n </tr>")))
462 (insert " <tr>\n ")
463 (if table-workaround
464 ;; This works around a Firefox bug in the mono file.
465 (insert "<td bgcolor=\"white\">")
466 (insert "<td>"))
467 (insert tag "</td>\n <td>" (or desc ""))
468 (setq open-td t))
469 ((eq (char-after) ?\n)
470 (delete-char 1)
471 ;; Negate the following `forward-line'.
472 (forward-line -1))
473 ((looking-at "<!-- ")
474 (search-forward "-->"))
475 ((looking-at "<p>[- ]*The Detailed Node Listing[- \n]*")
476 (replace-match " </td></tr></table>\n
477 <h3>Detailed Node Listing</h3>\n\n" t t)
478 (search-forward "<p>")
479 (search-forward "<p>" nil t)
480 (goto-char (match-beginning 0))
481 (skip-chars-backward "\n ")
482 (setq open-td nil)
483 (insert "</p>\n\n<table style=\"float:left\" width=\"100%\">"))
484 ((looking-at "</li></ul>")
485 (replace-match "" t t))
486 ((looking-at "<p>")
487 (replace-match "" t t)
488 (when open-td
489 (insert " </td></tr>")
490 (setq open-td nil))
491 (insert " <tr>
492 <th colspan=\"2\" align=\"left\" style=\"text-align:left\">")
493 (if (re-search-forward "</p>[ \t\n]*<ul class=\"menu\">" nil t)
494 (replace-match " </th></tr>")))
495 ((looking-at "[ \t]*</ul>[ \t]*$")
496 (replace-match
497 (if open-td
498 " </td></tr>\n</table>"
499 "</table>") t t)
500 (setq done t))
502 (if (eobp)
503 (error "Parse error in %s"
504 (file-name-nondirectory buffer-file-name)))
505 (unless open-td
506 (setq done t))))
507 (forward-line 1))))
510 ;; Stuff to check new `defcustom's got :version tags.
511 ;; Adapted from check-declare.el.
513 (defun cusver-find-files (root &optional old)
514 "Find .el files beneath directory ROOT that contain `defcustom's.
515 If optional OLD is non-nil, also include `defvar's."
516 (process-lines find-program root
517 "-name" "*.el"
518 "-exec" grep-program
519 "-l" "-E" (format "^[ \\t]*\\(def%s"
520 (if old "(custom|var)"
521 "custom"
523 "{}" "+"))
525 (defvar cusver-new-version (format "%s.%s" emacs-major-version
526 (1+ emacs-minor-version))
527 "Version number that new `defcustom's should have.")
529 (defun cusver-scan (file &optional old)
530 "Scan FILE for `defcustom' calls.
531 Return a list with elements of the form (VAR . VER),
532 This means that FILE contains a defcustom for variable VAR, with
533 a :version tag having value VER (may be nil).
534 If optional argument OLD is non-nil, also scan for `defvar's."
535 (let ((m (format "Scanning %s..." file))
536 (re (format "^[ \t]*\\((def%s\\)[ \t\n]"
537 (if old "\\(custom\\|var\\)" "\\(custom\\|group\\)")))
538 alist var ver form glist grp)
539 (message "%s" m)
540 (with-temp-buffer
541 (insert-file-contents file)
542 ;; FIXME we could theoretically be inside a string.
543 (while (re-search-forward re nil :noerror)
544 (goto-char (match-beginning 1))
545 (if (and (setq form (ignore-errors (read (current-buffer))))
546 (setq var (car-safe (cdr-safe form)))
547 ;; Exclude macros, eg (defcustom ,varname ...).
548 (symbolp var))
549 (progn
550 ;; FIXME It should be cus-test-apropos that does this.
551 (and (not old)
552 (equal "custom" (match-string 2))
553 (not (memq :type form))
554 (display-warning 'custom
555 (format "Missing type in: `%s'" form)))
556 (setq ver (car (cdr-safe (memq :version form))))
557 (if (equal "group" (match-string 2))
558 ;; Group :version could be old.
559 (if (equal ver cusver-new-version)
560 (setq glist (cons (cons var ver) glist)))
561 ;; If it specifies a group and the whole group has a
562 ;; version. use that.
563 (unless ver
564 (setq grp (car (cdr-safe (memq :group form))))
565 (and grp
566 (setq grp (car (cdr-safe grp))) ; (quote foo) -> foo
567 (setq ver (assq grp glist))))
568 (setq alist (cons (cons var ver) alist))))
569 (if form (message "Malformed defcustom: `%s'" form)))))
570 (message "%sdone" m)
571 alist))
573 (defun cusver-scan-cus-start (file)
574 "Scan cus-start.el and return an alist with elements (VAR . VER)."
575 (if (file-readable-p file)
576 (with-temp-buffer
577 (insert-file-contents file)
578 (when (search-forward "(let ((all '(" nil t)
579 (backward-char 1)
580 (let (var ver alist)
581 (dolist (elem (ignore-errors (read (current-buffer))))
582 (when (symbolp (setq var (car-safe elem)))
583 (or (stringp (setq ver (nth 3 elem)))
584 (setq ver nil))
585 (setq alist (cons (cons var ver) alist))))
586 alist)))))
588 (define-button-type 'cusver-xref 'action #'cusver-goto-xref)
590 (defun cusver-goto-xref (button)
591 "Jump to a Lisp file for the BUTTON at point."
592 (let ((file (button-get button 'file))
593 (var (button-get button 'var)))
594 (if (not (file-readable-p file))
595 (message "Cannot read `%s'" file)
596 (with-current-buffer (find-file-noselect file)
597 (goto-char (point-min))
598 (or (re-search-forward (format "^[ \t]*(defcustom[ \t]*%s" var) nil t)
599 (message "Unable to locate defcustom"))
600 (pop-to-buffer (current-buffer))))))
602 ;; You should probably at least do a grep over the old directory
603 ;; to check the results of this look sensible.
604 ;; TODO Check cus-start if something moved from C to Lisp.
605 ;; TODO Handle renamed things with aliases to the old names.
606 (defun cusver-check (newdir olddir version)
607 "Check that `defcustom's have :version tags where needed.
608 NEWDIR is the current lisp/ directory, OLDDIR is that from the
609 previous release, VERSION is the new version number. A
610 `defcustom' that is only in NEWDIR should have a :version tag.
611 We exclude cases where a `defvar' exists in OLDDIR, since just
612 converting a `defvar' to a `defcustom' does not require
613 a :version bump.
615 Note that a :version tag should also be added if the value of a defcustom
616 changes (in a non-trivial way). This function does not check for that."
617 (interactive (list (read-directory-name "New Lisp directory: " nil nil t)
618 (read-directory-name "Old Lisp directory: " nil nil t)
619 (number-to-string
620 (read-number "New version number: "
621 (string-to-number cusver-new-version)))))
622 (or (file-directory-p (setq newdir (expand-file-name newdir)))
623 (user-error "Directory `%s' not found" newdir))
624 (or (file-directory-p (setq olddir (expand-file-name olddir)))
625 (user-error "Directory `%s' not found" olddir))
626 (setq cusver-new-version version)
627 (let* ((newfiles (progn (message "Finding new files with `defcustom's...")
628 (cusver-find-files newdir)))
629 (oldfiles (progn (message "Finding old files with `defcustom's...")
630 (cusver-find-files olddir t)))
631 (newcus (progn (message "Reading new `defcustom's...")
632 (mapcar
633 (lambda (file)
634 (cons file (cusver-scan file))) newfiles)))
635 oldcus result thisfile file)
636 (message "Reading old `defcustom's...")
637 (dolist (file oldfiles)
638 (setq oldcus (append oldcus (cusver-scan file t))))
639 (setq oldcus (append oldcus (cusver-scan-cus-start
640 (expand-file-name "cus-start.el" olddir))))
641 ;; newcus has elements (FILE (VAR VER) ... ).
642 ;; oldcus just (VAR . VER).
643 (message "Checking for version tags...")
644 (dolist (new newcus)
645 (setq file (car new)
646 thisfile
647 (let (missing var)
648 (dolist (cons (cdr new))
649 (or (cdr cons)
650 (assq (setq var (car cons)) oldcus)
651 (push var missing)))
652 (if missing
653 (cons file missing))))
654 (if thisfile
655 (setq result (cons thisfile result))))
656 (message "Checking for version tags... done")
657 (if (not result)
658 (message "No missing :version tags")
659 (pop-to-buffer "*cusver*")
660 (erase-buffer)
661 (insert "These `defcustom's might be missing :version tags:\n\n")
662 (dolist (elem result)
663 (let* ((str (file-relative-name (car elem) newdir))
664 (strlen (length str)))
665 (dolist (var (cdr elem))
666 (insert (format "%s: %s\n" str var))
667 (make-text-button (+ (line-beginning-position 0) strlen 2)
668 (line-end-position 0)
669 'file (car elem)
670 'var var
671 'help-echo "Mouse-2: visit this definition"
672 :type 'cusver-xref)))))))
674 (provide 'admin)
676 ;;; admin.el ends here