1 ;;; Maintain autoloads in loaddefs.el.
2 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3 ;;; Written by Roland McGrath.
5 ;;; This program is free software; you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation; either version 1, or (at your option)
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;; GNU General Public License for more details.
15 ;;; A copy of the GNU General Public License can be obtained from this
16 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
17 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
21 (defun make-autoload (form file
)
22 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
23 Returns nil if FORM is not a defun or defmacro."
24 (let ((car (car-safe form
)))
25 (if (or (eq car
'defun
) (eq car
'defmacro
))
26 (let (name doc macrop
)
27 (setq macrop
(eq car
'defmacro
))
28 (setq form
(cdr form
))
29 (setq name
(car form
))
30 ;; Ignore the arguments.
31 (setq form
(cdr (cdr form
)))
34 (setq form
(cdr form
))
36 (list 'autoload
(list 'quote name
) file doc
37 (eq (car-safe (car form
)) 'interactive
) macrop
))
40 (defconst generate-autoload-cookie
";;;###autoload"
41 "Magic comment that tells \\[update-file-autoloads]
42 to make the following form into an autoload. This string should be
43 meaningless to Lisp (e.g., a comment).
48 \(defun function-to-be-autoloaded () ...)
50 If this string appears alone on a line, the following form will be
51 read and an autoload made for it. If there is further text on the line,
52 that text will be copied verbatim to `generated-autoload-file'.")
54 (defconst generate-autoload-section-header
"\f\n;;;### "
55 "String inserted before the form identifying
56 the section of autoloads for a file.")
58 (defconst generate-autoload-section-trailer
"\n;;;***\n"
59 "String which indicates the end of the section of autoloads for a file.")
61 ;; Forms which have doc-strings which should be printed specially.
62 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
63 ;; the doc-string in FORM.
64 ;; Note: defconst and defvar should NOT be marked in this way.
65 ;; We don't want to produce defconsts and defvars that make-docfile can
66 ;; grok, because then it would grok them twice, once in foo.el (where they
67 ;; are given with ;;;###autoload) and once in loaddefs.el.
68 (put 'autoload
'doc-string-elt
3)
70 (defun generate-file-autoloads (file)
71 "Insert at point a loaddefs autoload section for FILE.
72 autoloads are generated for defuns and defmacros in FILE
73 marked by `generate-autoload-regexp' (which see).
74 If FILE is being visited in a buffer, the contents of the buffer
76 (interactive "fGenerate autoloads for file: ")
77 (let ((outbuf (current-buffer))
78 (inbuf (find-file-noselect file
))
80 (load-name (let ((name (file-name-nondirectory file
)))
81 (if (string-match "\\.elc?$" name
)
82 (substring name
0 (match-beginning 0))
85 (floating-output-format "%20e")
88 (message "Generating autoloads for %s..." file
)
94 (goto-char (point-min))
96 (skip-chars-forward " \t\n\f")
97 (cond ((looking-at (regexp-quote generate-autoload-cookie
))
98 (search-forward generate-autoload-cookie
)
99 (skip-chars-forward " \t")
102 ;; Read the next form and make an autoload.
103 (let* ((form (prog1 (read (current-buffer))
105 (autoload (make-autoload form load-name
))
106 (doc-string-elt (get (car-safe form
)
109 (setq autoloads-done
(cons (nth 1 form
)
111 (setq autoload form
))
112 (if (and doc-string-elt
113 (stringp (nth doc-string-elt autoload
)))
114 ;; We need to hack the printing because the
115 ;; doc-string must be printed specially for
116 ;; make-docfile (sigh).
117 (let* ((p (nthcdr (1- doc-string-elt
) autoload
))
121 (mapcar (function (lambda (elt)
125 (princ "\"\\\n" outbuf
)
126 (princ (substring (prin1-to-string (car elt
)) 1)
131 (princ (substring (prin1-to-string (cdr elt
))
135 (print autoload outbuf
)))
136 ;; Copy the rest of the line to the output.
137 (let ((begin (point)))
139 (princ (buffer-substring begin
(point)) outbuf
))))
141 ;; Don't read the comment.
145 (forward-line 1))))))
147 (setq output-end
(point-marker)))
150 (insert generate-autoload-section-header
)
151 (prin1 (list 'autoloads autoloads-done load-name file
152 (nth 5 (file-attributes file
)))
155 (insert ";;; Generated autoloads from " file
"\n")
156 (goto-char output-end
)
157 (insert generate-autoload-section-trailer
)))
158 (message "Generating autoloads for %s...done" file
)))
160 (defconst generated-autoload-file
"loaddefs.el"
161 "*File \\[update-file-autoloads] puts autoloads into.
162 A .el file can set this in its local variables section to make its
163 autoloads go somewhere else.")
166 (defun update-file-autoloads (file)
167 "Update the autoloads for FILE in `generated-autoload-file'
168 \(which FILE might bind in its local variables)."
169 (interactive "fUpdate autoloads for file: ")
170 (let ((load-name (let ((name (file-name-nondirectory file
)))
171 (if (string-match "\\.elc?$" name
)
172 (substring name
0 (match-beginning 0))
175 (existing-buffer (get-file-buffer file
)))
177 ;; We want to get a value for generated-autoload-file from
178 ;; the local variables section if it's there.
179 (set-buffer (find-file-noselect file
))
180 (set-buffer (find-file-noselect generated-autoload-file
))
184 (goto-char (point-min))
185 (while (search-forward generate-autoload-section-header nil t
)
186 (let ((form (condition-case ()
187 (read (current-buffer))
189 (if (string= (nth 2 form
) load-name
)
190 (let ((begin (match-beginning 0))
191 (last-time (nth 4 form
))
192 (file-time (nth 5 (file-attributes file
))))
193 (if (and (or (null existing-buffer
)
194 (not (buffer-modified-p existing-buffer
)))
195 (listp last-time
) (= (length last-time
) 2)
196 (or (> (car last-time
) (car file-time
))
197 (and (= (car last-time
) (car file-time
))
198 (>= (nth 1 last-time
)
199 (nth 1 file-time
)))))
200 (message "Autoload section for %s is up to date."
202 (search-forward generate-autoload-section-trailer
)
203 (delete-region begin
(point))
204 (generate-file-autoloads file
))
208 ;; Have the user tell us where to put the section.
209 (save-window-excursion
210 (switch-to-buffer (current-buffer))
211 (with-output-to-temp-buffer "*Help*"
212 (princ (substitute-command-keys
214 Move point to where the autoload section
215 for %s should be inserted.
216 Then do \\[exit-recursive-edit]."
219 (generate-file-autoloads file
)))
220 (if (and (null existing-buffer
)
221 (setq existing-buffer
(get-file-buffer file
)))
222 (kill-buffer existing-buffer
)))))
225 (defun update-autoloads-here ()
226 "Update the sections of the current buffer generated by
227 \\[update-file-autoloads]."
229 (let ((generated-autoload-file (buffer-file-name)))
231 (goto-char (point-min))
232 (while (search-forward generate-autoload-section-header nil t
)
233 (let* ((form (condition-case ()
234 (read (current-buffer))
237 (if (and (stringp file
)
238 (or (get-file-buffer file
)
239 (file-exists-p file
)))
241 (setq file
(if (y-or-n-p (format "Library \"%s\" (load \
242 file \"%s\") doesn't exist. Remove its autoload section? "
246 (read-file-name (format "Find \"%s\" load file: "
251 (let ((begin (match-beginning 0)))
252 (search-forward generate-autoload-section-trailer
)
253 (delete-region begin
(point))))
255 (generate-file-autoloads file
)))))))
258 (defun update-directory-autoloads (dir)
259 "Run \\[update-file-autoloads] on each .el file in DIR."
260 (interactive "DUpdate autoloads for directory: ")
261 (mapcar 'update-file-autoloads
262 (directory-files dir nil
"\\.el$")))
265 (defun batch-update-autoloads ()
266 "Update the autoloads for the files or directories on the command line.
267 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
268 on directories. Must be used only with -batch, and kills Emacs on completion.
269 Each file will be processed even if an error occurred previously.
270 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
271 (if (not noninteractive
)
272 (error "batch-update-file-autoloads is to be used only with -batch"))
274 (args command-line-args-left
))
277 (condition-case lossage
278 (if (file-directory-p (expand-file-name (car args
)))
279 (update-directory-autoloads (car args
))
280 (update-file-autoloads (car args
)))
281 (error (progn (message ">>Error processing %s: %s"
284 (throw 'file nil
)))))
285 (setq args
(cdr args
)))
286 (save-some-buffers t
)
288 (kill-emacs (if lost
1 0))))