1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
8 ;;; This program is free software; you can redistribute it and/or modify
9 ;;; it under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 2, or (at your option)
11 ;;; any later version.
13 ;;; This program is distributed in the hope that it will be useful,
14 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; A copy of the GNU General Public License can be obtained from this
19 ;;; program's author (send electronic mail to roland@ai.mit.edu) or from
20 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
26 (defun make-autoload (form file)
27 "Turn FORM, a defun or defmacro, into an autoload for source file FILE.
28 Returns nil if FORM is not a defun or defmacro."
29 (let ((car (car-safe form)))
30 (if (or (eq car 'defun) (eq car 'defmacro))
31 (let (name doc macrop)
32 (setq macrop (eq car 'defmacro))
33 (setq form (cdr form))
34 (setq name (car form))
35 ;; Ignore the arguments.
36 (setq form (cdr (cdr form)))
39 (setq form (cdr form))
41 (list 'autoload (list 'quote name) file doc
42 (eq (car-safe (car form)) 'interactive)
43 (if macrop (list 'quote 'macro) nil)))
46 (defconst generate-autoload-cookie ";;;###autoload"
47 "Magic comment that tells \\[update-file-autoloads]
48 to make the following form into an autoload. This string should be
49 meaningless to Lisp (e.g., a comment).
54 \(defun function-to-be-autoloaded () ...)
56 If this string appears alone on a line, the following form will be
57 read and an autoload made for it. If there is further text on the line,
58 that text will be copied verbatim to `generated-autoload-file'.")
60 (defconst generate-autoload-section-header "\f\n;;;### "
61 "String inserted before the form identifying
62 the section of autoloads for a file.")
64 (defconst generate-autoload-section-trailer "\n;;;***\n"
65 "String which indicates the end of the section of autoloads for a file.")
67 ;;; Forms which have doc-strings which should be printed specially.
68 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
69 ;;; the doc-string in FORM.
71 ;;; There used to be the following note here:
72 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
73 ;;; ;;; We don't want to produce defconsts and defvars that
74 ;;; ;;; make-docfile can grok, because then it would grok them twice,
75 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
76 ;;; ;;; once in loaddefs.el.
78 ;;; Counter-note: Yes, they should be marked in this way.
79 ;;; make-docfile only processes those files that are loaded into the
80 ;;; dumped Emacs, and those files should never have anything
81 ;;; autoloaded here. The above-feared problem only occurs with files
82 ;;; which have autoloaded entries *and* are processed by make-docfile;
83 ;;; there should be no such files.
85 (put 'autoload 'doc-string-elt 3)
86 (put 'defun 'doc-string-elt 3)
87 (put 'defvar 'doc-string-elt 3)
88 (put 'defconst 'doc-string-elt 3)
89 (put 'defmacro 'doc-string-elt 3)
91 (defun generate-file-autoloads (file)
92 "Insert at point a loaddefs autoload section for FILE.
93 autoloads are generated for defuns and defmacros in FILE
94 marked by `generate-autoload-regexp' (which see).
95 If FILE is being visited in a buffer, the contents of the buffer
97 (interactive "fGenerate autoloads for file: ")
98 (let ((outbuf (current-buffer))
99 (inbuf (find-file-noselect file))
101 (load-name (let ((name (file-name-nondirectory file)))
102 (if (string-match "\\.elc?$" name)
103 (substring name 0 (match-beginning 0))
106 (floating-output-format "%20e")
110 ;; If the autoload section we create here uses an absolute
111 ;; pathname for FILE in its header, and then Emacs is installed
112 ;; under a different path on another system,
113 ;; `update-autoloads-here' won't be able to find the files to be
114 ;; autoloaded. So, if FILE is in the same directory or a
115 ;; subdirectory of the current buffer's directory, we'll make it
116 ;; relative to the current buffer's directory.
117 (setq file (expand-file-name file))
118 (if (and (< (length default-directory) (length file))
119 (string= default-directory
120 (substring file 0 (length default-directory))))
122 (setq file (substring file (length default-directory)))))
124 (message "Generating autoloads for %s..." file)
130 (goto-char (point-min))
132 (skip-chars-forward " \t\n\f")
133 (cond ((looking-at (regexp-quote generate-autoload-cookie))
134 (search-forward generate-autoload-cookie)
135 (skip-chars-forward " \t")
138 ;; Read the next form and make an autoload.
139 (let* ((form (prog1 (read (current-buffer))
141 (autoload (make-autoload form load-name))
142 (doc-string-elt (get (car-safe form)
145 (setq autoloads-done (cons (nth 1 form)
147 (setq autoload form))
148 (if (and doc-string-elt
149 (stringp (nth doc-string-elt autoload)))
150 ;; We need to hack the printing because the
151 ;; doc-string must be printed specially for
152 ;; make-docfile (sigh).
153 (let* ((p (nthcdr (1- doc-string-elt) autoload))
157 (mapcar (function (lambda (elt)
161 (princ "\"\\\n" outbuf)
162 (princ (substring (prin1-to-string (car elt)) 1)
167 (princ (substring (prin1-to-string (cdr elt))
171 (print autoload outbuf)))
172 ;; Copy the rest of the line to the output.
173 (let ((begin (point)))
175 (princ (buffer-substring begin (point)) outbuf))))
177 ;; Don't read the comment.
181 (forward-line 1))))))
183 (setq output-end (point-marker)))
186 (insert generate-autoload-section-header)
187 (prin1 (list 'autoloads autoloads-done load-name file
188 (nth 5 (file-attributes file)))
191 (insert ";;; Generated autoloads from " file "\n")
192 (goto-char output-end)
193 (insert generate-autoload-section-trailer)))
194 (message "Generating autoloads for %s...done" file)))
196 (defconst generated-autoload-file "loaddefs.el"
197 "*File \\[update-file-autoloads] puts autoloads into.
198 A .el file can set this in its local variables section to make its
199 autoloads go somewhere else.")
202 (defun update-file-autoloads (file)
203 "Update the autoloads for FILE in `generated-autoload-file'
204 \(which FILE might bind in its local variables)."
205 (interactive "fUpdate autoloads for file: ")
206 (let ((load-name (let ((name (file-name-nondirectory file)))
207 (if (string-match "\\.elc?$" name)
208 (substring name 0 (match-beginning 0))
211 (existing-buffer (get-file-buffer file)))
213 ;; We want to get a value for generated-autoload-file from
214 ;; the local variables section if it's there.
215 (set-buffer (find-file-noselect file))
216 (set-buffer (find-file-noselect generated-autoload-file))
220 (goto-char (point-min))
221 (while (search-forward generate-autoload-section-header nil t)
222 (let ((form (condition-case ()
223 (read (current-buffer))
225 (if (string= (nth 2 form) load-name)
226 (let ((begin (match-beginning 0))
227 (last-time (nth 4 form))
228 (file-time (nth 5 (file-attributes file))))
229 (if (and (or (null existing-buffer)
230 (not (buffer-modified-p existing-buffer)))
231 (listp last-time) (= (length last-time) 2)
232 (or (> (car last-time) (car file-time))
233 (and (= (car last-time) (car file-time))
234 (>= (nth 1 last-time)
235 (nth 1 file-time)))))
236 (message "Autoload section for %s is up to date."
238 (search-forward generate-autoload-section-trailer)
239 (delete-region begin (point))
240 (generate-file-autoloads file))
244 ;; Have the user tell us where to put the section.
245 (save-window-excursion
246 (switch-to-buffer (current-buffer))
247 (with-output-to-temp-buffer "*Help*"
248 (princ (substitute-command-keys
250 Move point to where the autoload section
251 for %s should be inserted.
252 Then do \\[exit-recursive-edit]."
256 (generate-file-autoloads file)))
257 (if (and (null existing-buffer)
258 (setq existing-buffer (get-file-buffer file)))
259 (kill-buffer existing-buffer)))))
262 (defun update-autoloads-here ()
263 "Update the sections of the current buffer generated by
264 \\[update-file-autoloads]."
266 (let ((generated-autoload-file (buffer-file-name)))
268 (goto-char (point-min))
269 (while (search-forward generate-autoload-section-header nil t)
270 (let* ((form (condition-case ()
271 (read (current-buffer))
274 (if (and (stringp file)
275 (or (get-file-buffer file)
276 (file-exists-p file)))
278 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
279 file \"%s\") doesn't exist. Remove its autoload section? "
283 (read-file-name (format "Find \"%s\" load file: "
288 (let ((begin (match-beginning 0)))
289 (search-forward generate-autoload-section-trailer)
290 (delete-region begin (point))))
292 (generate-file-autoloads file)))))))
295 (defun update-directory-autoloads (dir)
296 "Run \\[update-file-autoloads] on each .el file in DIR."
297 (interactive "DUpdate autoloads for directory: ")
298 (mapcar 'update-file-autoloads
299 (directory-files dir nil "\\.el$")))
302 (defun batch-update-autoloads ()
303 "Update the autoloads for the files or directories on the command line.
304 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
305 on directories. Must be used only with -batch, and kills Emacs on completion.
306 Each file will be processed even if an error occurred previously.
307 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
308 (if (not noninteractive)
309 (error "batch-update-file-autoloads is to be used only with -batch"))
311 (args command-line-args-left))
314 (condition-case lossage
315 (if (file-directory-p (expand-file-name (car args)))
316 (update-directory-autoloads (car args))
317 (update-file-autoloads (car args)))
318 (error (progn (message ">>Error processing %s: %s"
321 (throw 'file nil)))))
322 (setq args (cdr args)))
323 (save-some-buffers t)
325 (kill-emacs (if lost 1 0))))
329 ;;; autoload.el ends here