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