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