* admin/check-doc-strings: Replace `perl -w' with `use warnings;'.
[emacs.git] / admin / admin.el
blob58e5b0c830e2f9eebf60de7ec2e667e5f037bad6
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 ;; TODO report the progress
222 (defun make-manuals (root &optional type)
223 "Generate the web manuals for the Emacs webpage.
224 ROOT should be the root of an Emacs source tree.
225 Interactively with a prefix argument, prompt for TYPE.
226 Optional argument TYPE is type of output (nil means all)."
227 (interactive (let ((root (read-directory-name "Emacs root directory: "
228 source-directory nil t)))
229 (list root
230 (if current-prefix-arg
231 (completing-read
232 "Type: "
233 (append
234 '("misc" "pdf" "ps")
235 (let (res)
236 (dolist (i '("emacs" "elisp" "eintr") res)
237 (dolist (j '("" "-mono" "-node" "-ps" "-pdf"))
238 (push (concat i j) res))))
239 (manual-misc-manuals root)))))))
240 (let* ((dest (expand-file-name "manual" root))
241 (html-node-dir (expand-file-name "html_node" dest))
242 (html-mono-dir (expand-file-name "html_mono" dest))
243 (ps-dir (expand-file-name "ps" dest))
244 (pdf-dir (expand-file-name "pdf" dest))
245 (emacs (expand-file-name "doc/emacs/emacs.texi" root))
246 (elisp (expand-file-name "doc/lispref/elisp.texi" root))
247 (eintr (expand-file-name "doc/lispintro/emacs-lisp-intro.texi" root))
248 (misc (manual-misc-manuals root)))
249 ;; TODO this makes it non-continuable.
250 ;; Instead, delete the individual dest directory each time.
251 (when (file-directory-p dest)
252 (if (y-or-n-p (format "Directory %s exists, delete it first? " dest))
253 (delete-directory dest t)
254 (user-error "Aborted")))
255 (if (member type '(nil "emacs" "emacs-node"))
256 (manual-html-node emacs (expand-file-name "emacs" html-node-dir)))
257 (if (member type '(nil "emacs" "emacs-mono"))
258 (manual-html-mono emacs (expand-file-name "emacs.html" html-mono-dir)))
259 (if (member type '(nil "emacs" "emacs-pdf" "pdf"))
260 (manual-pdf emacs (expand-file-name "emacs.pdf" pdf-dir)))
261 (if (member type '(nil "emacs" "emacs-ps" "ps"))
262 (manual-ps emacs (expand-file-name "emacs.ps" ps-dir)))
263 (if (member type '(nil "elisp" "elisp-node"))
264 (manual-html-node elisp (expand-file-name "elisp" html-node-dir)))
265 (if (member type '(nil "elisp" "elisp-mono"))
266 (manual-html-mono elisp (expand-file-name "elisp.html" html-mono-dir)))
267 (if (member type '(nil "elisp" "elisp-pdf" "pdf"))
268 (manual-pdf elisp (expand-file-name "elisp.pdf" pdf-dir)))
269 (if (member type '(nil "elisp" "elisp-ps" "ps"))
270 (manual-ps elisp (expand-file-name "elisp.ps" ps-dir)))
271 (if (member type '(nil "eintr" "eintr-node"))
272 (manual-html-node eintr (expand-file-name "eintr" html-node-dir)))
273 (if (member type '(nil "eintr" "eintr-node"))
274 (manual-html-mono eintr (expand-file-name "eintr.html" html-mono-dir)))
275 (if (member type '(nil "eintr" "eintr-pdf" "pdf"))
276 (manual-pdf eintr (expand-file-name "eintr.pdf" pdf-dir)))
277 (if (member type '(nil "eintr" "eintr-ps" "ps"))
278 (manual-ps eintr (expand-file-name "eintr.ps" ps-dir)))
279 ;; Misc manuals
280 (dolist (manual misc)
281 (if (member type `(nil ,manual "misc"))
282 (manual-misc-html manual root html-node-dir html-mono-dir)))
283 (message "Manuals created in %s" dest)))
285 (defconst manual-doctype-string
286 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
287 \"http://www.w3.org/TR/html4/loose.dtd\">\n\n")
289 (defconst manual-meta-string
290 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
291 <link rev=\"made\" href=\"mailto:webmasters@gnu.org\">
292 <link rel=\"icon\" type=\"image/png\" href=\"/graphics/gnu-head-mini.png\">
293 <meta name=\"ICBM\" content=\"42.256233,-71.006581\">
294 <meta name=\"DC.title\" content=\"gnu.org\">\n\n")
296 (defconst manual-style-string "<style type=\"text/css\">
297 @import url('/software/emacs/manual.css');\n</style>\n")
299 (defun manual-misc-html (name root html-node-dir html-mono-dir)
300 ;; Hack to deal with the cases where .texi creates a different .info.
301 ;; Blech. TODO Why not just rename the .texi (or .info) files?
302 (let* ((texiname (cond ((equal name "ccmode") "cc-mode")
303 (t name)))
304 (texi (expand-file-name (format "doc/misc/%s.texi" texiname) root)))
305 (manual-html-node texi (expand-file-name name html-node-dir))
306 (manual-html-mono texi (expand-file-name (concat name ".html")
307 html-mono-dir))))
309 (defun manual-html-mono (texi-file dest)
310 "Run Makeinfo on TEXI-FILE, emitting mono HTML output to DEST.
311 This function also edits the HTML files so that they validate as
312 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
313 the @import directive."
314 (make-directory (or (file-name-directory dest) ".") t)
315 (call-process "makeinfo" nil nil nil
316 "-D" "WWW_GNU_ORG"
317 "-I" (expand-file-name "../emacs"
318 (file-name-directory texi-file))
319 "-I" (expand-file-name "../misc"
320 (file-name-directory texi-file))
321 "--html" "--no-split" texi-file "-o" dest)
322 (with-temp-buffer
323 (insert-file-contents dest)
324 (setq buffer-file-name dest)
325 (manual-html-fix-headers)
326 (manual-html-fix-index-1)
327 (manual-html-fix-index-2 t)
328 (manual-html-fix-node-div)
329 (goto-char (point-max))
330 (re-search-backward "</body>[\n \t]*</html>")
331 (insert "</div>\n\n")
332 (save-buffer)))
334 (defun manual-html-node (texi-file dir)
335 "Run Makeinfo on TEXI-FILE, emitting per-node HTML output to DIR.
336 This function also edits the HTML files so that they validate as
337 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
338 the @import directive."
339 (unless (file-exists-p texi-file)
340 (user-error "Manual file %s not found" texi-file))
341 (make-directory dir t)
342 (call-process "makeinfo" nil nil nil
343 "-D" "WWW_GNU_ORG"
344 "-I" (expand-file-name "../emacs"
345 (file-name-directory texi-file))
346 "-I" (expand-file-name "../misc"
347 (file-name-directory texi-file))
348 "--html" texi-file "-o" dir)
349 ;; Loop through the node files, fixing them up.
350 (dolist (f (directory-files dir nil "\\.html\\'"))
351 (let (opoint)
352 (with-temp-buffer
353 (insert-file-contents (expand-file-name f dir))
354 (setq buffer-file-name (expand-file-name f dir))
355 (if (looking-at "<meta http-equiv")
356 ;; Ignore those HTML files that are just redirects.
357 (set-buffer-modified-p nil)
358 (manual-html-fix-headers)
359 (if (equal f "index.html")
360 (let (copyright-text)
361 (manual-html-fix-index-1)
362 ;; Move copyright notice to the end.
363 (when (re-search-forward "[ \t]*<p>Copyright &copy;" nil t)
364 (setq opoint (match-beginning 0))
365 (re-search-forward "</blockquote>")
366 (setq copyright-text (buffer-substring opoint (point)))
367 (delete-region opoint (point)))
368 (manual-html-fix-index-2)
369 (if copyright-text
370 (insert copyright-text))
371 (insert "\n</div>\n"))
372 ;; For normal nodes, give the header div a blue bg.
373 (manual-html-fix-node-div))
374 (save-buffer))))))
376 (defun manual-pdf (texi-file dest)
377 "Run texi2pdf on TEXI-FILE, emitting PDF output to DEST."
378 (make-directory (or (file-name-directory dest) ".") t)
379 (let ((default-directory (file-name-directory texi-file)))
380 (call-process "texi2pdf" nil nil nil
381 "-I" "../emacs" "-I" "../misc"
382 texi-file "-o" dest)))
384 (defun manual-ps (texi-file dest)
385 "Generate a PostScript version of TEXI-FILE as DEST."
386 (make-directory (or (file-name-directory dest) ".") t)
387 (let ((dvi-dest (concat (file-name-sans-extension dest) ".dvi"))
388 (default-directory (file-name-directory texi-file)))
389 ;; FIXME: Use `texi2dvi --ps'? --xfq
390 (call-process "texi2dvi" nil nil nil
391 "-I" "../emacs" "-I" "../misc"
392 texi-file "-o" dvi-dest)
393 (call-process "dvips" nil nil nil dvi-dest "-o" dest)
394 (delete-file dvi-dest)
395 (call-process "gzip" nil nil nil dest)))
397 (defun manual-html-fix-headers ()
398 "Fix up HTML headers for the Emacs manual in the current buffer."
399 (let ((texi5 (search-forward "<!DOCTYPE" nil t))
400 opoint)
401 ;; Texinfo 5 supplies a DOCTYPE.
402 (or texi5
403 (insert manual-doctype-string))
404 (search-forward "<head>\n")
405 (insert manual-meta-string)
406 (search-forward "<meta")
407 (setq opoint (match-beginning 0))
408 (unless texi5
409 (search-forward "<!--")
410 (goto-char (match-beginning 0))
411 (delete-region opoint (point))
412 (search-forward "<meta http-equiv=\"Content-Style")
413 (setq opoint (match-beginning 0)))
414 (search-forward "</head>")
415 (goto-char (match-beginning 0))
416 (delete-region opoint (point))
417 (insert manual-style-string)))
419 (defun manual-html-fix-node-div ()
420 "Fix up HTML \"node\" divs in the current buffer."
421 (let (opoint div-end)
422 (while (search-forward "<div class=\"node\">" nil t)
423 (replace-match
424 "<div class=\"node\" style=\"background-color:#DDDDFF\">"
425 t t)
426 (setq opoint (point))
427 (re-search-forward "</div>")
428 (setq div-end (match-beginning 0))
429 (goto-char opoint)
430 (if (search-forward "<hr>" div-end 'move)
431 (replace-match "" t t)))))
433 (defun manual-html-fix-index-1 ()
434 (let (opoint)
435 (re-search-forward "<body.*>\n")
436 (setq opoint (match-end 0))
437 (search-forward "<h2 class=\"")
438 (goto-char (match-beginning 0))
439 (delete-region opoint (point))
440 (insert "<div id=\"content\" class=\"inner\">\n\n")))
442 (defun manual-html-fix-index-2 (&optional table-workaround)
443 "Replace the index list in the current buffer with a HTML table."
444 (let (done open-td tag desc)
445 ;; Convert the list that Makeinfo made into a table.
446 (or (search-forward "<ul class=\"menu\">" nil t)
447 (search-forward "<ul>"))
448 (replace-match "<table style=\"float:left\" width=\"100%\">")
449 (forward-line 1)
450 (while (not done)
451 (cond
452 ((or (looking-at "<li>\\(<a.+</a>\\):[ \t]+\\(.*\\)$")
453 (looking-at "<li>\\(<a.+</a>\\)$"))
454 (setq tag (match-string 1))
455 (setq desc (match-string 2))
456 (replace-match "" t t)
457 (when open-td
458 (save-excursion
459 (forward-char -1)
460 (skip-chars-backward " ")
461 (delete-region (point) (line-end-position))
462 (insert "</td>\n </tr>")))
463 (insert " <tr>\n ")
464 (if table-workaround
465 ;; This works around a Firefox bug in the mono file.
466 (insert "<td bgcolor=\"white\">")
467 (insert "<td>"))
468 (insert tag "</td>\n <td>" (or desc ""))
469 (setq open-td t))
470 ((eq (char-after) ?\n)
471 (delete-char 1)
472 ;; Negate the following `forward-line'.
473 (forward-line -1))
474 ((looking-at "<!-- ")
475 (search-forward "-->"))
476 ((looking-at "<p>[- ]*The Detailed Node Listing[- \n]*")
477 (replace-match " </td></tr></table>\n
478 <h3>Detailed Node Listing</h3>\n\n" t t)
479 (search-forward "<p>")
480 (search-forward "<p>" nil t)
481 (goto-char (match-beginning 0))
482 (skip-chars-backward "\n ")
483 (setq open-td nil)
484 (insert "</p>\n\n<table style=\"float:left\" width=\"100%\">"))
485 ((looking-at "</li></ul>")
486 (replace-match "" t t))
487 ((looking-at "<p>")
488 (replace-match "" t t)
489 (when open-td
490 (insert " </td></tr>")
491 (setq open-td nil))
492 (insert " <tr>
493 <th colspan=\"2\" align=\"left\" style=\"text-align:left\">")
494 (if (re-search-forward "</p>[ \t\n]*<ul class=\"menu\">" nil t)
495 (replace-match " </th></tr>")))
496 ((looking-at "[ \t]*</ul>[ \t]*$")
497 (replace-match
498 (if open-td
499 " </td></tr>\n</table>"
500 "</table>") t t)
501 (setq done t))
503 (if (eobp)
504 (error "Parse error in %s"
505 (file-name-nondirectory buffer-file-name)))
506 (unless open-td
507 (setq done t))))
508 (forward-line 1))))
511 ;; Stuff to check new `defcustom's got :version tags.
512 ;; Adapted from check-declare.el.
514 (defun cusver-find-files (root &optional old)
515 "Find .el files beneath directory ROOT that contain `defcustom's.
516 If optional OLD is non-nil, also include `defvar's."
517 (process-lines find-program root
518 "-name" "*.el"
519 "-exec" grep-program
520 "-l" "-E" (format "^[ \\t]*\\(def%s"
521 (if old "(custom|var)"
522 "custom"
524 "{}" "+"))
526 (defvar cusver-new-version (format "%s.%s" emacs-major-version
527 (1+ emacs-minor-version))
528 "Version number that new `defcustom's should have.")
530 (defun cusver-scan (file &optional old)
531 "Scan FILE for `defcustom' calls.
532 Return a list with elements of the form (VAR . VER),
533 This means that FILE contains a defcustom for variable VAR, with
534 a :version tag having value VER (may be nil).
535 If optional argument OLD is non-nil, also scan for `defvar's."
536 (let ((m (format "Scanning %s..." file))
537 (re (format "^[ \t]*\\((def%s\\)[ \t\n]"
538 (if old "\\(custom\\|var\\)" "\\(custom\\|group\\)")))
539 alist var ver form glist grp)
540 (message "%s" m)
541 (with-temp-buffer
542 (insert-file-contents file)
543 ;; FIXME we could theoretically be inside a string.
544 (while (re-search-forward re nil :noerror)
545 (goto-char (match-beginning 1))
546 (if (and (setq form (ignore-errors (read (current-buffer))))
547 (setq var (car-safe (cdr-safe form)))
548 ;; Exclude macros, eg (defcustom ,varname ...).
549 (symbolp var))
550 (progn
551 ;; FIXME It should be cus-test-apropos that does this.
552 (and (not old)
553 (equal "custom" (match-string 2))
554 (not (memq :type form))
555 (display-warning 'custom
556 (format "Missing type in: `%s'" form)))
557 (setq ver (car (cdr-safe (memq :version form))))
558 (if (equal "group" (match-string 2))
559 ;; Group :version could be old.
560 (if (equal ver cusver-new-version)
561 (setq glist (cons (cons var ver) glist)))
562 ;; If it specifies a group and the whole group has a
563 ;; version. use that.
564 (unless ver
565 (setq grp (car (cdr-safe (memq :group form))))
566 (and grp
567 (setq grp (car (cdr-safe grp))) ; (quote foo) -> foo
568 (setq ver (assq grp glist))))
569 (setq alist (cons (cons var ver) alist))))
570 (if form (message "Malformed defcustom: `%s'" form)))))
571 (message "%sdone" m)
572 alist))
574 (defun cusver-scan-cus-start (file)
575 "Scan cus-start.el and return an alist with elements (VAR . VER)."
576 (if (file-readable-p file)
577 (with-temp-buffer
578 (insert-file-contents file)
579 (when (search-forward "(let ((all '(" nil t)
580 (backward-char 1)
581 (let (var ver alist)
582 (dolist (elem (ignore-errors (read (current-buffer))))
583 (when (symbolp (setq var (car-safe elem)))
584 (or (stringp (setq ver (nth 3 elem)))
585 (setq ver nil))
586 (setq alist (cons (cons var ver) alist))))
587 alist)))))
589 (define-button-type 'cusver-xref 'action #'cusver-goto-xref)
591 (defun cusver-goto-xref (button)
592 "Jump to a Lisp file for the BUTTON at point."
593 (let ((file (button-get button 'file))
594 (var (button-get button 'var)))
595 (if (not (file-readable-p file))
596 (message "Cannot read `%s'" file)
597 (with-current-buffer (find-file-noselect file)
598 (goto-char (point-min))
599 (or (re-search-forward (format "^[ \t]*(defcustom[ \t]*%s" var) nil t)
600 (message "Unable to locate defcustom"))
601 (pop-to-buffer (current-buffer))))))
603 ;; You should probably at least do a grep over the old directory
604 ;; to check the results of this look sensible.
605 ;; TODO Check cus-start if something moved from C to Lisp.
606 ;; TODO Handle renamed things with aliases to the old names.
607 (defun cusver-check (newdir olddir version)
608 "Check that `defcustom's have :version tags where needed.
609 NEWDIR is the current lisp/ directory, OLDDIR is that from the
610 previous release, VERSION is the new version number. A
611 `defcustom' that is only in NEWDIR should have a :version tag.
612 We exclude cases where a `defvar' exists in OLDDIR, since just
613 converting a `defvar' to a `defcustom' does not require
614 a :version bump.
616 Note that a :version tag should also be added if the value of a defcustom
617 changes (in a non-trivial way). This function does not check for that."
618 (interactive (list (read-directory-name "New Lisp directory: " nil nil t)
619 (read-directory-name "Old Lisp directory: " nil nil t)
620 (number-to-string
621 (read-number "New version number: "
622 (string-to-number cusver-new-version)))))
623 (or (file-directory-p (setq newdir (expand-file-name newdir)))
624 (user-error "Directory `%s' not found" newdir))
625 (or (file-directory-p (setq olddir (expand-file-name olddir)))
626 (user-error "Directory `%s' not found" olddir))
627 (setq cusver-new-version version)
628 (let* ((newfiles (progn (message "Finding new files with `defcustom's...")
629 (cusver-find-files newdir)))
630 (oldfiles (progn (message "Finding old files with `defcustom's...")
631 (cusver-find-files olddir t)))
632 (newcus (progn (message "Reading new `defcustom's...")
633 (mapcar
634 (lambda (file)
635 (cons file (cusver-scan file))) newfiles)))
636 oldcus result thisfile file)
637 (message "Reading old `defcustom's...")
638 (dolist (file oldfiles)
639 (setq oldcus (append oldcus (cusver-scan file t))))
640 (setq oldcus (append oldcus (cusver-scan-cus-start
641 (expand-file-name "cus-start.el" olddir))))
642 ;; newcus has elements (FILE (VAR VER) ... ).
643 ;; oldcus just (VAR . VER).
644 (message "Checking for version tags...")
645 (dolist (new newcus)
646 (setq file (car new)
647 thisfile
648 (let (missing var)
649 (dolist (cons (cdr new))
650 (or (cdr cons)
651 (assq (setq var (car cons)) oldcus)
652 (push var missing)))
653 (if missing
654 (cons file missing))))
655 (if thisfile
656 (setq result (cons thisfile result))))
657 (message "Checking for version tags... done")
658 (if (not result)
659 (message "No missing :version tags")
660 (pop-to-buffer "*cusver*")
661 (erase-buffer)
662 (insert "These `defcustom's might be missing :version tags:\n\n")
663 (dolist (elem result)
664 (let* ((str (file-relative-name (car elem) newdir))
665 (strlen (length str)))
666 (dolist (var (cdr elem))
667 (insert (format "%s: %s\n" str var))
668 (make-text-button (+ (line-beginning-position 0) strlen 2)
669 (line-end-position 0)
670 'file (car elem)
671 'var var
672 'help-echo "Mouse-2: visit this definition"
673 :type 'cusver-xref)))))))
675 (provide 'admin)
677 ;;; admin.el ends here