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