]> code.delx.au - gnu-emacs/blob - admin/admin.el
Merge from emacs-24
[gnu-emacs] / admin / admin.el
1 ;;; admin.el --- utilities for Emacs administration
2
3 ;; Copyright (C) 2001-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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
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.
16
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/>.
19
20 ;;; Commentary:
21
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.
26
27 ;;; Code:
28
29 (defvar add-log-time-format) ; in add-log
30
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 (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))))
63
64 (defun set-version-in-file (root file version rx)
65 (find-file (expand-file-name file root))
66 (goto-char (point-min))
67 (unless (re-search-forward rx nil t)
68 (error "Version not found in %s" file))
69 (replace-match (format "%s" version) nil nil nil 1))
70
71 (defun set-version (root version)
72 "Set Emacs version to VERSION in relevant files under ROOT.
73 Root must be the root of an Emacs source tree."
74 (interactive "DEmacs root directory: \nsVersion number: ")
75 (unless (file-exists-p (expand-file-name "src/emacs.c" root))
76 (error "%s doesn't seem to be the root of an Emacs source tree" root))
77 (set-version-in-file root "README" version
78 (rx (and "version" (1+ space)
79 (submatch (1+ (in "0-9."))))))
80 (set-version-in-file root "configure.ac" version
81 (rx (and "AC_INIT" (1+ (not (in ?,)))
82 ?, (0+ space)
83 (submatch (1+ (in "0-9."))))))
84 (set-version-in-file root "doc/emacs/emacsver.texi" version
85 (rx (and "EMACSVER" (1+ space)
86 (submatch (1+ (in "0-9."))))))
87 (set-version-in-file root "doc/man/emacs.1" version
88 (rx (and ".TH EMACS" (1+ not-newline)
89 "GNU Emacs" (1+ space)
90 (submatch (1+ (in "0-9."))))))
91 (set-version-in-file root "nt/config.nt" version
92 (rx (and bol "#" (0+ blank) "define" (1+ blank)
93 "VERSION" (1+ blank) "\""
94 (submatch (1+ (in "0-9."))))))
95 (set-version-in-file root "msdos/sed2v2.inp" version
96 (rx (and bol "/^#undef " (1+ not-newline)
97 "define VERSION" (1+ space) "\""
98 (submatch (1+ (in "0-9."))))))
99 (set-version-in-file root "nt/makefile.w32-in" version
100 (rx (and "VERSION" (0+ space) "=" (0+ space)
101 (submatch (1+ (in "0-9."))))))
102 ;; nt/emacs.rc also contains the version number, but in an awkward
103 ;; format. It must contain four components, separated by commas, and
104 ;; in two places those commas are followed by space, in two other
105 ;; places they are not.
106 (let* ((version-components (append (split-string version "\\.")
107 '("0" "0")))
108 (comma-version
109 (concat (car version-components) ","
110 (cadr version-components) ","
111 (cadr (cdr version-components)) ","
112 (cadr (cdr (cdr version-components)))))
113 (comma-space-version
114 (concat (car version-components) ", "
115 (cadr version-components) ", "
116 (cadr (cdr version-components)) ", "
117 (cadr (cdr (cdr version-components))))))
118 (set-version-in-file root "nt/emacs.rc" comma-version
119 (rx (and "FILEVERSION" (1+ space)
120 (submatch (1+ (in "0-9,"))))))
121 (set-version-in-file root "nt/emacs.rc" comma-version
122 (rx (and "PRODUCTVERSION" (1+ space)
123 (submatch (1+ (in "0-9,"))))))
124 (set-version-in-file root "nt/emacs.rc" comma-space-version
125 (rx (and "\"FileVersion\"" (0+ space) ?, (0+ space)
126 ?\" (submatch (1+ (in "0-9, "))) "\\0\"")))
127 (set-version-in-file root "nt/emacs.rc" comma-space-version
128 (rx (and "\"ProductVersion\"" (0+ space) ?,
129 (0+ space) ?\" (submatch (1+ (in "0-9, ")))
130 "\\0\"")))
131 ;; Likewise for emacsclient.rc
132 (set-version-in-file root "nt/emacsclient.rc" comma-version
133 (rx (and "FILEVERSION" (1+ space)
134 (submatch (1+ (in "0-9,"))))))
135 (set-version-in-file root "nt/emacsclient.rc" comma-version
136 (rx (and "PRODUCTVERSION" (1+ space)
137 (submatch (1+ (in "0-9,"))))))
138 (set-version-in-file root "nt/emacsclient.rc" comma-space-version
139 (rx (and "\"FileVersion\"" (0+ space) ?, (0+ space)
140 ?\" (submatch (1+ (in "0-9, "))) "\\0\"")))
141 (set-version-in-file root "nt/emacsclient.rc" comma-space-version
142 (rx (and "\"ProductVersion\"" (0+ space) ?,
143 (0+ space) ?\" (submatch (1+ (in "0-9, ")))
144 "\\0\"")))
145 ;; Major version only.
146 (when (string-match "\\([0-9]\\{2,\\}\\)" version)
147 (setq version (match-string 1 version))
148 (set-version-in-file root "src/msdos.c" version
149 (rx (and "Vwindow_system_version" (1+ not-newline)
150 ?\( (submatch (1+ (in "0-9"))) ?\))))
151 (set-version-in-file root "etc/refcards/ru-refcard.tex" version
152 "\\\\newcommand{\\\\versionemacs}\\[0\\]\
153 {\\([0-9]\\{2,\\}\\)}.+%.+version of Emacs")
154 (set-version-in-file root "etc/refcards/emacsver.tex" version
155 "\\\\def\\\\versionemacs\
156 {\\([0-9]\\{2,\\}\\)}.+%.+version of Emacs"))))
157
158
159 ;; Note this makes some assumptions about form of short copyright.
160 (defun set-copyright (root copyright)
161 "Set Emacs short copyright to COPYRIGHT in relevant files under ROOT.
162 Root must be the root of an Emacs source tree."
163 (interactive (list
164 (read-directory-name "Emacs root directory: " nil nil t)
165 (read-string
166 "Short copyright string: "
167 (format "Copyright (C) %s Free Software Foundation, Inc."
168 (format-time-string "%Y")))))
169 (unless (file-exists-p (expand-file-name "src/emacs.c" root))
170 (error "%s doesn't seem to be the root of an Emacs source tree" root))
171 (set-version-in-file root "configure.ac" copyright
172 (rx (and bol "copyright" (0+ (not (in ?\")))
173 ?\" (submatch (1+ (not (in ?\")))) ?\")))
174 (set-version-in-file root "msdos/sed2v2.inp" copyright
175 (rx (and bol "/^#undef " (1+ not-newline)
176 "define COPYRIGHT" (1+ space)
177 ?\" (submatch (1+ (not (in ?\")))) ?\")))
178 (set-version-in-file root "nt/config.nt" copyright
179 (rx (and bol "#" (0+ blank) "define" (1+ blank)
180 "COPYRIGHT" (1+ blank)
181 ?\" (submatch (1+ (not (in ?\")))) ?\")))
182 (set-version-in-file root "lib-src/rcs2log" copyright
183 (rx (and "Copyright" (0+ space) ?= (0+ space)
184 ?\' (submatch (1+ nonl)))))
185 (when (string-match "\\([0-9]\\{4\\}\\)" copyright)
186 (setq copyright (match-string 1 copyright))
187 (set-version-in-file root "etc/refcards/ru-refcard.tex" copyright
188 "\\\\newcommand{\\\\cyear}\\[0\\]\
189 {\\([0-9]\\{4\\}\\)}.+%.+copyright year")
190 (set-version-in-file root "etc/refcards/emacsver.tex" copyright
191 "\\\\def\\\\year\
192 {\\([0-9]\\{4\\}\\)}.+%.+copyright year")))
193
194 ;;; Various bits of magic for generating the web manuals
195
196 (defun make-manuals (root)
197 "Generate the web manuals for the Emacs webpage."
198 (interactive "DEmacs root directory: ")
199 (let* ((dest (expand-file-name "manual" root))
200 (html-node-dir (expand-file-name "html_node" dest))
201 (html-mono-dir (expand-file-name "html_mono" dest))
202 (txt-dir (expand-file-name "text" dest))
203 (dvi-dir (expand-file-name "dvi" dest))
204 (ps-dir (expand-file-name "ps" dest)))
205 (when (file-directory-p dest)
206 (if (y-or-n-p (format "Directory %s exists, delete it first?" dest))
207 (delete-directory dest t)
208 (error "Aborted")))
209 (make-directory dest)
210 (make-directory html-node-dir)
211 (make-directory html-mono-dir)
212 (make-directory txt-dir)
213 (make-directory dvi-dir)
214 (make-directory ps-dir)
215 ;; Emacs manual
216 (let ((texi (expand-file-name "doc/emacs/emacs.texi" root)))
217 (manual-html-node texi (expand-file-name "emacs" html-node-dir))
218 (manual-html-mono texi (expand-file-name "emacs.html" html-mono-dir))
219 (manual-txt texi (expand-file-name "emacs.txt" txt-dir))
220 (manual-pdf texi (expand-file-name "emacs.pdf" dest))
221 (manual-dvi texi (expand-file-name "emacs.dvi" dvi-dir)
222 (expand-file-name "emacs.ps" ps-dir)))
223 ;; Lisp manual
224 (let ((texi (expand-file-name "doc/lispref/elisp.texi" root)))
225 (manual-html-node texi (expand-file-name "elisp" html-node-dir))
226 (manual-html-mono texi (expand-file-name "elisp.html" html-mono-dir))
227 (manual-txt texi (expand-file-name "elisp.txt" txt-dir))
228 (manual-pdf texi (expand-file-name "elisp.pdf" dest))
229 (manual-dvi texi (expand-file-name "elisp.dvi" dvi-dir)
230 (expand-file-name "elisp.ps" ps-dir)))
231 (let ((texi (expand-file-name "doc/lispintro/emacs-lisp-intro.texi" root))
232 (dest (expand-file-name "emacs-lisp-intro" dest))
233 dest2 dest3)
234 ;; Mimic the atypical directory layout used for emacs-lisp-intro.
235 (make-directory dest)
236 (make-directory (setq dest2 (expand-file-name "html_node" dest)))
237 (manual-html-node texi dest2)
238 (make-directory (setq dest2 (expand-file-name "html_mono" dest)))
239 (manual-html-mono texi (expand-file-name "emacs-lisp-intro.html" dest2))
240 (make-directory (setq dest2 (expand-file-name "txt" dest)))
241 (manual-txt texi (expand-file-name "emacs-lisp-intro.txt" dest2))
242 (manual-pdf texi (expand-file-name "emacs-lisp-intro.pdf" dest))
243 (make-directory (setq dest2 (expand-file-name "dvi" dest)))
244 (make-directory (setq dest3 (expand-file-name "ps" dest)))
245 (manual-dvi texi (expand-file-name "emacs-lisp-intro.dvi" dest2)
246 (expand-file-name "emacs-lisp-intro.ps" dest3)))
247 ;; Misc manuals
248 (let ((manuals '("ada-mode" "auth" "autotype" "bovine" "calc" "cc-mode"
249 "cl" "dbus" "dired-x" "ebrowse" "ede" "ediff"
250 "edt" "eieio" "emacs-gnutls" "emacs-mime" "epa" "erc" "ert"
251 "eshell" "eudc" "faq" "flymake" "forms"
252 "gnus" "htmlfontify" "idlwave" "info"
253 "mairix-el" "message" "mh-e" "newsticker"
254 "nxml-mode" "org" "pcl-cvs" "pgg" "rcirc"
255 "reftex" "remember" "sasl" "sc" "semantic"
256 "ses" "sieve" "smtpmail" "speedbar" "srecode" "tramp"
257 "url" "vip" "viper" "widget" "wisent" "woman")))
258 (dolist (manual manuals)
259 (manual-misc-html manual root html-node-dir html-mono-dir)))
260 (message "Manuals created in %s" dest)))
261
262 (defconst manual-doctype-string
263 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
264 \"http://www.w3.org/TR/html4/loose.dtd\">\n\n")
265
266 (defconst manual-meta-string
267 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
268 <link rev=\"made\" href=\"mailto:webmasters@gnu.org\">
269 <link rel=\"icon\" type=\"image/png\" href=\"/graphics/gnu-head-mini.png\">
270 <meta name=\"ICBM\" content=\"42.256233,-71.006581\">
271 <meta name=\"DC.title\" content=\"gnu.org\">\n\n")
272
273 (defconst manual-style-string "<style type=\"text/css\">
274 @import url('/style.css');\n</style>\n")
275
276 (defun manual-misc-html (name root html-node-dir html-mono-dir)
277 (let ((texi (expand-file-name (format "doc/misc/%s.texi" name) root)))
278 (manual-html-node texi (expand-file-name name html-node-dir))
279 (manual-html-mono texi (expand-file-name (concat name ".html")
280 html-mono-dir))))
281
282 (defun manual-html-mono (texi-file dest)
283 "Run Makeinfo on TEXI-FILE, emitting mono HTML output to DEST.
284 This function also edits the HTML files so that they validate as
285 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
286 the @import directive."
287 (call-process "makeinfo" nil nil nil
288 "-D" "WWW_GNU_ORG"
289 "-I" (expand-file-name "../emacs"
290 (file-name-directory texi-file))
291 "-I" (expand-file-name "../misc"
292 (file-name-directory texi-file))
293 "--html" "--no-split" texi-file "-o" dest)
294 (with-temp-buffer
295 (insert-file-contents dest)
296 (setq buffer-file-name dest)
297 (manual-html-fix-headers)
298 (manual-html-fix-index-1)
299 (manual-html-fix-index-2 t)
300 (manual-html-fix-node-div)
301 (goto-char (point-max))
302 (re-search-backward "</body>[\n \t]*</html>")
303 (insert "</div>\n\n")
304 (save-buffer)))
305
306 (defun manual-html-node (texi-file dir)
307 "Run Makeinfo on TEXI-FILE, emitting per-node HTML output to DIR.
308 This function also edits the HTML files so that they validate as
309 HTML 4.01 Transitional, and pulls in the gnu.org stylesheet using
310 the @import directive."
311 (unless (file-exists-p texi-file)
312 (error "Manual file %s not found" texi-file))
313 (call-process "makeinfo" nil nil nil
314 "-D" "WWW_GNU_ORG"
315 "-I" (expand-file-name "../emacs"
316 (file-name-directory texi-file))
317 "-I" (expand-file-name "../misc"
318 (file-name-directory texi-file))
319 "--html" texi-file "-o" dir)
320 ;; Loop through the node files, fixing them up.
321 (dolist (f (directory-files dir nil "\\.html\\'"))
322 (let (opoint)
323 (with-temp-buffer
324 (insert-file-contents (expand-file-name f dir))
325 (setq buffer-file-name (expand-file-name f dir))
326 (if (looking-at "<meta http-equiv")
327 ;; Ignore those HTML files that are just redirects.
328 (set-buffer-modified-p nil)
329 (manual-html-fix-headers)
330 (if (equal f "index.html")
331 (let (copyright-text)
332 (manual-html-fix-index-1)
333 ;; Move copyright notice to the end.
334 (when (re-search-forward "[ \t]*<p>Copyright &copy;" nil t)
335 (setq opoint (match-beginning 0))
336 (re-search-forward "</blockquote>")
337 (setq copyright-text (buffer-substring opoint (point)))
338 (delete-region opoint (point)))
339 (manual-html-fix-index-2)
340 (if copyright-text
341 (insert copyright-text))
342 (insert "\n</div>\n"))
343 ;; For normal nodes, give the header div a blue bg.
344 (manual-html-fix-node-div))
345 (save-buffer))))))
346
347 (defun manual-txt (texi-file dest)
348 "Run Makeinfo on TEXI-FILE, emitting plaintext output to DEST."
349 (call-process "makeinfo" nil nil nil
350 "-I" (expand-file-name "../emacs"
351 (file-name-directory texi-file))
352 "-I" (expand-file-name "../misc"
353 (file-name-directory texi-file))
354 "--plaintext" "--no-split" texi-file "-o" dest)
355 (shell-command (concat "gzip -c " dest " > " (concat dest ".gz"))))
356
357 (defun manual-pdf (texi-file dest)
358 "Run texi2pdf on TEXI-FILE, emitting plaintext output to DEST."
359 (call-process "texi2pdf" nil nil nil
360 "-I" (expand-file-name "../emacs"
361 (file-name-directory texi-file))
362 "-I" (expand-file-name "../misc"
363 (file-name-directory texi-file))
364 texi-file "-o" dest))
365
366 (defun manual-dvi (texi-file dest ps-dest)
367 "Run texi2dvi on TEXI-FILE, emitting dvi output to DEST.
368 Also generate PostScript output in PS-DEST."
369 (call-process "texi2dvi" nil nil nil
370 "-I" (expand-file-name "../emacs"
371 (file-name-directory texi-file))
372 "-I" (expand-file-name "../misc"
373 (file-name-directory texi-file))
374 texi-file "-o" dest)
375 (call-process "dvips" nil nil nil dest "-o" ps-dest)
376 (call-process "gzip" nil nil nil dest)
377 (call-process "gzip" nil nil nil ps-dest))
378
379 (defun manual-html-fix-headers ()
380 "Fix up HTML headers for the Emacs manual in the current buffer."
381 (let (opoint)
382 (insert manual-doctype-string)
383 (search-forward "<head>\n")
384 (insert manual-meta-string)
385 (search-forward "<meta")
386 (setq opoint (match-beginning 0))
387 (re-search-forward "<!--")
388 (goto-char (match-beginning 0))
389 (delete-region opoint (point))
390 (insert manual-style-string)
391 (search-forward "<meta http-equiv=\"Content-Style")
392 (setq opoint (match-beginning 0))
393 (search-forward "</head>")
394 (delete-region opoint (match-beginning 0))))
395
396 (defun manual-html-fix-node-div ()
397 "Fix up HTML \"node\" divs in the current buffer."
398 (let (opoint div-end)
399 (while (search-forward "<div class=\"node\">" nil t)
400 (replace-match
401 "<div class=\"node\" style=\"background-color:#DDDDFF\">"
402 t t)
403 (setq opoint (point))
404 (re-search-forward "</div>")
405 (setq div-end (match-beginning 0))
406 (goto-char opoint)
407 (if (search-forward "<hr>" div-end 'move)
408 (replace-match "" t t)))))
409
410 (defun manual-html-fix-index-1 ()
411 (let (opoint)
412 (re-search-forward "<body>\n")
413 (setq opoint (match-end 0))
414 (search-forward "<h2 class=\"")
415 (goto-char (match-beginning 0))
416 (delete-region opoint (point))
417 (insert "<div id=\"content\" class=\"inner\">\n\n")))
418
419 (defun manual-html-fix-index-2 (&optional table-workaround)
420 "Replace the index list in the current buffer with a HTML table."
421 (let (done open-td tag desc)
422 ;; Convert the list that Makeinfo made into a table.
423 (or (search-forward "<ul class=\"menu\">" nil t)
424 (search-forward "<ul>"))
425 (replace-match "<table style=\"float:left\" width=\"100%\">")
426 (forward-line 1)
427 (while (not done)
428 (cond
429 ((or (looking-at "<li>\\(<a.+</a>\\):[ \t]+\\(.*\\)$")
430 (looking-at "<li>\\(<a.+</a>\\)$"))
431 (setq tag (match-string 1))
432 (setq desc (match-string 2))
433 (replace-match "" t t)
434 (when open-td
435 (save-excursion
436 (forward-char -1)
437 (skip-chars-backward " ")
438 (delete-region (point) (line-end-position))
439 (insert "</td>\n </tr>")))
440 (insert " <tr>\n ")
441 (if table-workaround
442 ;; This works around a Firefox bug in the mono file.
443 (insert "<td bgcolor=\"white\">")
444 (insert "<td>"))
445 (insert tag "</td>\n <td>" (or desc ""))
446 (setq open-td t))
447 ((eq (char-after) ?\n)
448 (delete-char 1)
449 ;; Negate the following `forward-line'.
450 (forward-line -1))
451 ((looking-at "<!-- ")
452 (search-forward "-->"))
453 ((looking-at "<p>[- ]*The Detailed Node Listing[- \n]*")
454 (replace-match " </td></tr></table>\n
455 <h3>Detailed Node Listing</h3>\n\n" t t)
456 (search-forward "<p>")
457 (search-forward "<p>" nil t)
458 (goto-char (match-beginning 0))
459 (skip-chars-backward "\n ")
460 (setq open-td nil)
461 (insert "</p>\n\n<table style=\"float:left\" width=\"100%\">"))
462 ((looking-at "</li></ul>")
463 (replace-match "" t t))
464 ((looking-at "<p>")
465 (replace-match "" t t)
466 (when open-td
467 (insert " </td></tr>")
468 (setq open-td nil))
469 (insert " <tr>
470 <th colspan=\"2\" align=\"left\" style=\"text-align:left\">")
471 (if (re-search-forward "</p>[ \t\n]*<ul class=\"menu\">" nil t)
472 (replace-match " </th></tr>")))
473 ((looking-at "[ \t]*</ul>[ \t]*$")
474 (replace-match
475 (if open-td
476 " </td></tr>\n</table>"
477 "</table>") t t)
478 (setq done t))
479 (t
480 (if (eobp)
481 (error "Parse error in %s" f)) ; f is bound in manual-html-node
482 (unless open-td
483 (setq done t))))
484 (forward-line 1))))
485
486 \f
487 ;; Stuff to check new defcustoms got :version tags.
488 ;; Adapted from check-declare.el.
489
490 (defun cusver-find-files (root &optional old)
491 "Find .el files beneath directory ROOT that contain defcustoms.
492 If optional OLD is non-nil, also include defvars."
493 (process-lines find-program root
494 "-name" "*.el"
495 "-exec" grep-program
496 "-l" "-E" (format "^[ \\t]*\\(def%s"
497 (if old "(custom|var)"
498 "custom"
499 ))
500 "{}" "+"))
501
502 (defvar cusver-new-version (format "%s.%s" emacs-major-version
503 (1+ emacs-minor-version))
504 "Version number that new defcustoms should have.")
505
506 (defun cusver-scan (file &optional old)
507 "Scan FILE for `defcustom' calls.
508 Return a list with elements of the form (VAR . VER),
509 This means that FILE contains a defcustom for variable VAR, with
510 a :version tag having value VER (may be nil).
511 If optional argument OLD is non-nil, also scan for defvars."
512 (let ((m (format "Scanning %s..." file))
513 (re (format "^[ \t]*\\((def%s\\)[ \t\n]"
514 (if old "\\(custom\\|var\\)" "\\(custom\\|group\\)")))
515 alist var ver form glist grp)
516 (message "%s" m)
517 (with-temp-buffer
518 (insert-file-contents file)
519 ;; FIXME we could theoretically be inside a string.
520 (while (re-search-forward re nil t)
521 (goto-char (match-beginning 1))
522 (if (and (setq form (ignore-errors (read (current-buffer))))
523 (setq var (car-safe (cdr-safe form)))
524 ;; Exclude macros, eg (defcustom ,varname ...).
525 (symbolp var))
526 (progn
527 (setq ver (car (cdr-safe (memq :version form))))
528 (if (equal "group" (match-string 2))
529 ;; Group :version could be old.
530 (if (equal ver cusver-new-version)
531 (setq glist (cons (cons var ver) glist)))
532 ;; If it specifies a group and the whole group has a
533 ;; version. use that.
534 (unless ver
535 (setq grp (car (cdr-safe (memq :group form))))
536 (and grp
537 (setq grp (car (cdr-safe grp))) ; (quote foo) -> foo
538 (setq ver (assq grp glist))))
539 (setq alist (cons (cons var ver) alist))))
540 (if form (message "Malformed defcustom: `%s'" form)))))
541 (message "%sdone" m)
542 alist))
543
544 (defun cusver-scan-cus-start (file)
545 "Scan cus-start.el and return an alist with elements (VAR . VER)."
546 (if (file-readable-p file)
547 (with-temp-buffer
548 (insert-file-contents file)
549 (when (search-forward "(let ((all '(" nil t)
550 (backward-char 1)
551 (let (var ver alist)
552 (dolist (elem (ignore-errors (read (current-buffer))))
553 (when (symbolp (setq var (car-safe elem)))
554 (or (stringp (setq ver (nth 3 elem)))
555 (setq ver nil))
556 (setq alist (cons (cons var ver) alist))))
557 alist)))))
558
559 (define-button-type 'cusver-xref 'action #'cusver-goto-xref)
560
561 (defun cusver-goto-xref (button)
562 "Jump to a lisp file for the BUTTON at point."
563 (let ((file (button-get button 'file))
564 (var (button-get button 'var)))
565 (if (not (file-readable-p file))
566 (message "Cannot read `%s'" file)
567 (with-current-buffer (find-file-noselect file)
568 (goto-char (point-min))
569 (or (re-search-forward (format "^[ \t]*(defcustom[ \t]*%s" var) nil t)
570 (message "Unable to locate defcustom"))
571 (pop-to-buffer (current-buffer))))))
572
573 ;; You should probably at least do a grep over the old directory
574 ;; to check the results of this look sensible.
575 ;; TODO Check cus-start if something moved from C to Lisp.
576 ;; TODO Handle renamed things with aliases to the old names.
577 (defun cusver-check (newdir olddir version)
578 "Check that defcustoms have :version tags where needed.
579 NEWDIR is the current lisp/ directory, OLDDIR is that from the previous
580 release. A defcustom that is only in NEWDIR should have a :version
581 tag. We exclude cases where a defvar exists in OLDDIR, since
582 just converting a defvar to a defcustom does not require a :version bump.
583
584 Note that a :version tag should also be added if the value of a defcustom
585 changes (in a non-trivial way). This function does not check for that."
586 (interactive (list (read-directory-name "New Lisp directory: ")
587 (read-directory-name "Old Lisp directory: ")
588 (number-to-string
589 (read-number "New version number: "
590 (string-to-number cusver-new-version)))))
591 (or (file-directory-p (setq newdir (expand-file-name newdir)))
592 (error "Directory `%s' not found" newdir))
593 (or (file-directory-p (setq olddir (expand-file-name olddir)))
594 (error "Directory `%s' not found" olddir))
595 (setq cusver-new-version version)
596 (let* ((newfiles (progn (message "Finding new files with defcustoms...")
597 (cusver-find-files newdir)))
598 (oldfiles (progn (message "Finding old files with defcustoms...")
599 (cusver-find-files olddir t)))
600 (newcus (progn (message "Reading new defcustoms...")
601 (mapcar
602 (lambda (file)
603 (cons file (cusver-scan file))) newfiles)))
604 oldcus result thisfile file)
605 (message "Reading old defcustoms...")
606 (dolist (file oldfiles)
607 (setq oldcus (append oldcus (cusver-scan file t))))
608 (setq oldcus (append oldcus (cusver-scan-cus-start
609 (expand-file-name "cus-start.el" olddir))))
610 ;; newcus has elements (FILE (VAR VER) ... ).
611 ;; oldcus just (VAR . VER).
612 (message "Checking for version tags...")
613 (dolist (new newcus)
614 (setq file (car new)
615 thisfile
616 (let (missing var)
617 (dolist (cons (cdr new))
618 (or (cdr cons)
619 (assq (setq var (car cons)) oldcus)
620 (push var missing)))
621 (if missing
622 (cons file missing))))
623 (if thisfile
624 (setq result (cons thisfile result))))
625 (message "Checking for version tags... done")
626 (if (not result)
627 (message "No missing :version tags")
628 (pop-to-buffer "*cusver*")
629 (erase-buffer)
630 (insert "These defcustoms might be missing :version tags:\n\n")
631 (dolist (elem result)
632 (let* ((str (file-relative-name (car elem) newdir))
633 (strlen (length str)))
634 (dolist (var (cdr elem))
635 (insert (format "%s: %s\n" str var))
636 (make-text-button (+ (line-beginning-position 0) strlen 2)
637 (line-end-position 0)
638 'file (car elem)
639 'var var
640 'help-echo "Mouse-2: visit this definition"
641 :type 'cusver-xref)))))))
642
643 (provide 'admin)
644
645 ;;; admin.el ends here