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.
66 ;;; There used to be the following note here:
67 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
68 ;;; ;;; We don't want to produce defconsts and defvars that
69 ;;; ;;; make-docfile can grok, because then it would grok them twice,
70 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
71 ;;; ;;; once in loaddefs.el.
73 ;;; Counter-note: Yes, they should be marked in this way.
74 ;;; make-docfile only processes those files that are loaded into the
75 ;;; dumped Emacs, and those files should never have anything
76 ;;; autoloaded here. The above-feared problem only occurs with files
77 ;;; which have autoloaded entries *and* are processed by make-docfile;
78 ;;; there should be no such files.
80 (put 'autoload 'doc-string-elt 3)
81 (put 'defun 'doc-string-elt 3)
82 (put 'defvar 'doc-string-elt 3)
83 (put 'defconst 'doc-string-elt 3)
84 (put 'defmacro 'doc-string-elt 3)
86 (defun generate-file-autoloads (file)
87 "Insert at point a loaddefs autoload section for FILE.
88 autoloads are generated for defuns and defmacros in FILE
89 marked by `generate-autoload-regexp' (which see).
90 If FILE is being visited in a buffer, the contents of the buffer
92 (interactive "fGenerate autoloads for file: ")
93 (let ((outbuf (current-buffer))
94 (inbuf (find-file-noselect file))
96 (load-name (let ((name (file-name-nondirectory file)))
97 (if (string-match "\\.elc?$" name)
98 (substring name 0 (match-beginning 0))
101 (floating-output-format "%20e")
105 ;; If the autoload section we create here uses an absolute
106 ;; pathname for FILE in its header, and then Emacs is installed
107 ;; under a different path on another system,
108 ;; `update-autoloads-here' won't be able to find the files to be
109 ;; autoloaded. So, if FILE is in the same directory or a
110 ;; subdirectory of the current buffer's directory, we'll make it
111 ;; relative to the current buffer's directory.
112 (setq file (expand-file-name file))
113 (if (and (< (length default-directory) (length file))
114 (string= default-directory
115 (substring file 0 (length default-directory))))
117 (setq file (substring file (length default-directory)))))
119 (message "Generating autoloads for %s..." file)
125 (goto-char (point-min))
127 (skip-chars-forward " \t\n\f")
128 (cond ((looking-at (regexp-quote generate-autoload-cookie))
129 (search-forward generate-autoload-cookie)
130 (skip-chars-forward " \t")
133 ;; Read the next form and make an autoload.
134 (let* ((form (prog1 (read (current-buffer))
136 (autoload (make-autoload form load-name))
137 (doc-string-elt (get (car-safe form)
140 (setq autoloads-done (cons (nth 1 form)
142 (setq autoload form))
143 (if (and doc-string-elt
144 (stringp (nth doc-string-elt autoload)))
145 ;; We need to hack the printing because the
146 ;; doc-string must be printed specially for
147 ;; make-docfile (sigh).
148 (let* ((p (nthcdr (1- doc-string-elt) autoload))
152 (mapcar (function (lambda (elt)
156 (princ "\"\\\n" outbuf)
157 (princ (substring (prin1-to-string (car elt)) 1)
162 (princ (substring (prin1-to-string (cdr elt))
166 (print autoload outbuf)))
167 ;; Copy the rest of the line to the output.
168 (let ((begin (point)))
170 (princ (buffer-substring begin (point)) outbuf))))
172 ;; Don't read the comment.
176 (forward-line 1))))))
178 (setq output-end (point-marker)))
181 (insert generate-autoload-section-header)
182 (prin1 (list 'autoloads autoloads-done load-name file
183 (nth 5 (file-attributes file)))
186 (insert ";;; Generated autoloads from " file "\n")
187 (goto-char output-end)
188 (insert generate-autoload-section-trailer)))
189 (message "Generating autoloads for %s...done" file)))
191 (defconst generated-autoload-file "loaddefs.el"
192 "*File \\[update-file-autoloads] puts autoloads into.
193 A .el file can set this in its local variables section to make its
194 autoloads go somewhere else.")
197 (defun update-file-autoloads (file)
198 "Update the autoloads for FILE in `generated-autoload-file'
199 \(which FILE might bind in its local variables)."
200 (interactive "fUpdate autoloads for file: ")
201 (let ((load-name (let ((name (file-name-nondirectory file)))
202 (if (string-match "\\.elc?$" name)
203 (substring name 0 (match-beginning 0))
206 (existing-buffer (get-file-buffer file)))
208 ;; We want to get a value for generated-autoload-file from
209 ;; the local variables section if it's there.
210 (set-buffer (find-file-noselect file))
211 (set-buffer (find-file-noselect generated-autoload-file))
215 (goto-char (point-min))
216 (while (search-forward generate-autoload-section-header nil t)
217 (let ((form (condition-case ()
218 (read (current-buffer))
220 (if (string= (nth 2 form) load-name)
221 (let ((begin (match-beginning 0))
222 (last-time (nth 4 form))
223 (file-time (nth 5 (file-attributes file))))
224 (if (and (or (null existing-buffer)
225 (not (buffer-modified-p existing-buffer)))
226 (listp last-time) (= (length last-time) 2)
227 (or (> (car last-time) (car file-time))
228 (and (= (car last-time) (car file-time))
229 (>= (nth 1 last-time)
230 (nth 1 file-time)))))
231 (message "Autoload section for %s is up to date."
233 (search-forward generate-autoload-section-trailer)
234 (delete-region begin (point))
235 (generate-file-autoloads file))
239 ;; Have the user tell us where to put the section.
240 (save-window-excursion
241 (switch-to-buffer (current-buffer))
242 (with-output-to-temp-buffer "*Help*"
243 (princ (substitute-command-keys
245 Move point to where the autoload section
246 for %s should be inserted.
247 Then do \\[exit-recursive-edit]."
250 (generate-file-autoloads file)))
251 (if (and (null existing-buffer)
252 (setq existing-buffer (get-file-buffer file)))
253 (kill-buffer existing-buffer)))))
256 (defun update-autoloads-here ()
257 "Update the sections of the current buffer generated by
258 \\[update-file-autoloads]."
260 (let ((generated-autoload-file (buffer-file-name)))
262 (goto-char (point-min))
263 (while (search-forward generate-autoload-section-header nil t)
264 (let* ((form (condition-case ()
265 (read (current-buffer))
268 (if (and (stringp file)
269 (or (get-file-buffer file)
270 (file-exists-p file)))
272 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
273 file \"%s\") doesn't exist. Remove its autoload section? "
277 (read-file-name (format "Find \"%s\" load file: "
282 (let ((begin (match-beginning 0)))
283 (search-forward generate-autoload-section-trailer)
284 (delete-region begin (point))))
286 (generate-file-autoloads file)))))))
289 (defun update-directory-autoloads (dir)
290 "Run \\[update-file-autoloads] on each .el file in DIR."
291 (interactive "DUpdate autoloads for directory: ")
292 (mapcar 'update-file-autoloads
293 (directory-files dir nil "\\.el$")))
296 (defun batch-update-autoloads ()
297 "Update the autoloads for the files or directories on the command line.
298 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
299 on directories. Must be used only with -batch, and kills Emacs on completion.
300 Each file will be processed even if an error occurred previously.
301 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
302 (if (not noninteractive)
303 (error "batch-update-file-autoloads is to be used only with -batch"))
305 (args command-line-args-left))
308 (condition-case lossage
309 (if (file-directory-p (expand-file-name (car args)))
310 (update-directory-autoloads (car args))
311 (update-file-autoloads (car args)))
312 (error (progn (message ">>Error processing %s: %s"
315 (throw 'file nil)))))
316 (setq args (cdr args)))
317 (save-some-buffers t)
319 (kill-emacs (if lost 1 0))))
323 ;;; autoload.el ends here