*** empty log message ***
[emacs.git] / lisp / emacs-lisp / autoload.el
blobcdc5785c35ed8dc7a7d69949b161358469b3c7b8
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
4 ;; Last-Modified: 24 Jun 1992
6 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
7 ;;;
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.
12 ;;;
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.
17 ;;;
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
21 ;;; 02139, USA.
22 ;;;
24 ;;; Code:
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)))
37 (setq doc (car form))
38 (if (stringp doc)
39 (setq form (cdr form))
40 (setq doc nil))
41 (list 'autoload (list 'quote name) file doc
42 (eq (car-safe (car form)) 'interactive) macrop))
43 nil)))
45 (defconst generate-autoload-cookie ";;;###autoload"
46 "Magic comment that tells \\[update-file-autoloads]
47 to make the following form into an autoload. This string should be
48 meaningless to Lisp (e.g., a comment).
50 This string is used:
52 ;;;###autoload
53 \(defun function-to-be-autoloaded () ...)
55 If this string appears alone on a line, the following form will be
56 read and an autoload made for it. If there is further text on the line,
57 that text will be copied verbatim to `generated-autoload-file'.")
59 (defconst generate-autoload-section-header "\f\n;;;### "
60 "String inserted before the form identifying
61 the section of autoloads for a file.")
63 (defconst generate-autoload-section-trailer "\n;;;***\n"
64 "String which indicates the end of the section of autoloads for a file.")
66 ;;; Forms which have doc-strings which should be printed specially.
67 ;;; A doc-string-elt property of ELT says that (nth ELT FORM) is
68 ;;; the doc-string in FORM.
69 ;;;
70 ;;; There used to be the following note here:
71 ;;; ;;; Note: defconst and defvar should NOT be marked in this way.
72 ;;; ;;; We don't want to produce defconsts and defvars that
73 ;;; ;;; make-docfile can grok, because then it would grok them twice,
74 ;;; ;;; once in foo.el (where they are given with ;;;###autoload) and
75 ;;; ;;; once in loaddefs.el.
76 ;;;
77 ;;; Counter-note: Yes, they should be marked in this way.
78 ;;; make-docfile only processes those files that are loaded into the
79 ;;; dumped Emacs, and those files should never have anything
80 ;;; autoloaded here. The above-feared problem only occurs with files
81 ;;; which have autoloaded entries *and* are processed by make-docfile;
82 ;;; there should be no such files.
84 (put 'autoload 'doc-string-elt 3)
85 (put 'defun 'doc-string-elt 3)
86 (put 'defvar 'doc-string-elt 3)
87 (put 'defconst 'doc-string-elt 3)
88 (put 'defmacro 'doc-string-elt 3)
90 (defun generate-file-autoloads (file)
91 "Insert at point a loaddefs autoload section for FILE.
92 autoloads are generated for defuns and defmacros in FILE
93 marked by `generate-autoload-regexp' (which see).
94 If FILE is being visited in a buffer, the contents of the buffer
95 are used."
96 (interactive "fGenerate autoloads for file: ")
97 (let ((outbuf (current-buffer))
98 (inbuf (find-file-noselect file))
99 (autoloads-done '())
100 (load-name (let ((name (file-name-nondirectory file)))
101 (if (string-match "\\.elc?$" name)
102 (substring name 0 (match-beginning 0))
103 name)))
104 (print-length nil)
105 (floating-output-format "%20e")
106 (done-any nil)
107 output-end)
109 ;; If the autoload section we create here uses an absolute
110 ;; pathname for FILE in its header, and then Emacs is installed
111 ;; under a different path on another system,
112 ;; `update-autoloads-here' won't be able to find the files to be
113 ;; autoloaded. So, if FILE is in the same directory or a
114 ;; subdirectory of the current buffer's directory, we'll make it
115 ;; relative to the current buffer's directory.
116 (setq file (expand-file-name file))
117 (if (and (< (length default-directory) (length file))
118 (string= default-directory
119 (substring file 0 (length default-directory))))
120 (progn
121 (setq file (substring file (length default-directory)))))
123 (message "Generating autoloads for %s..." file)
124 (save-excursion
125 (set-buffer inbuf)
126 (save-excursion
127 (save-restriction
128 (widen)
129 (goto-char (point-min))
130 (while (not (eobp))
131 (skip-chars-forward " \t\n\f")
132 (cond ((looking-at (regexp-quote generate-autoload-cookie))
133 (search-forward generate-autoload-cookie)
134 (skip-chars-forward " \t")
135 (setq done-any t)
136 (if (eolp)
137 ;; Read the next form and make an autoload.
138 (let* ((form (prog1 (read (current-buffer))
139 (forward-line 1)))
140 (autoload (make-autoload form load-name))
141 (doc-string-elt (get (car-safe form)
142 'doc-string-elt)))
143 (if autoload
144 (setq autoloads-done (cons (nth 1 form)
145 autoloads-done))
146 (setq autoload form))
147 (if (and doc-string-elt
148 (stringp (nth doc-string-elt autoload)))
149 ;; We need to hack the printing because the
150 ;; doc-string must be printed specially for
151 ;; make-docfile (sigh).
152 (let* ((p (nthcdr (1- doc-string-elt) autoload))
153 (elt (cdr p)))
154 (setcdr p nil)
155 (princ "\n(" outbuf)
156 (mapcar (function (lambda (elt)
157 (prin1 elt outbuf)
158 (princ " " outbuf)))
159 autoload)
160 (princ "\"\\\n" outbuf)
161 (princ (substring (prin1-to-string (car elt)) 1)
162 outbuf)
163 (if (null (cdr elt))
164 (princ ")" outbuf)
165 (princ " " outbuf)
166 (princ (substring (prin1-to-string (cdr elt))
168 outbuf))
169 (terpri outbuf))
170 (print autoload outbuf)))
171 ;; Copy the rest of the line to the output.
172 (let ((begin (point)))
173 (forward-line 1)
174 (princ (buffer-substring begin (point)) outbuf))))
175 ((looking-at ";")
176 ;; Don't read the comment.
177 (forward-line 1))
179 (forward-sexp 1)
180 (forward-line 1))))))
181 (set-buffer outbuf)
182 (setq output-end (point-marker)))
183 (if done-any
184 (progn
185 (insert generate-autoload-section-header)
186 (prin1 (list 'autoloads autoloads-done load-name file
187 (nth 5 (file-attributes file)))
188 outbuf)
189 (terpri outbuf)
190 (insert ";;; Generated autoloads from " file "\n")
191 (goto-char output-end)
192 (insert generate-autoload-section-trailer)))
193 (message "Generating autoloads for %s...done" file)))
195 (defconst generated-autoload-file "loaddefs.el"
196 "*File \\[update-file-autoloads] puts autoloads into.
197 A .el file can set this in its local variables section to make its
198 autoloads go somewhere else.")
200 ;;;###autoload
201 (defun update-file-autoloads (file)
202 "Update the autoloads for FILE in `generated-autoload-file'
203 \(which FILE might bind in its local variables)."
204 (interactive "fUpdate autoloads for file: ")
205 (let ((load-name (let ((name (file-name-nondirectory file)))
206 (if (string-match "\\.elc?$" name)
207 (substring name 0 (match-beginning 0))
208 name)))
209 (done nil)
210 (existing-buffer (get-file-buffer file)))
211 (save-excursion
212 ;; We want to get a value for generated-autoload-file from
213 ;; the local variables section if it's there.
214 (set-buffer (find-file-noselect file))
215 (set-buffer (find-file-noselect generated-autoload-file))
216 (save-excursion
217 (save-restriction
218 (widen)
219 (goto-char (point-min))
220 (while (search-forward generate-autoload-section-header nil t)
221 (let ((form (condition-case ()
222 (read (current-buffer))
223 (end-of-file nil))))
224 (if (string= (nth 2 form) load-name)
225 (let ((begin (match-beginning 0))
226 (last-time (nth 4 form))
227 (file-time (nth 5 (file-attributes file))))
228 (if (and (or (null existing-buffer)
229 (not (buffer-modified-p existing-buffer)))
230 (listp last-time) (= (length last-time) 2)
231 (or (> (car last-time) (car file-time))
232 (and (= (car last-time) (car file-time))
233 (>= (nth 1 last-time)
234 (nth 1 file-time)))))
235 (message "Autoload section for %s is up to date."
236 file)
237 (search-forward generate-autoload-section-trailer)
238 (delete-region begin (point))
239 (generate-file-autoloads file))
240 (setq done t))))))
241 (if done
243 ;; Have the user tell us where to put the section.
244 (save-window-excursion
245 (switch-to-buffer (current-buffer))
246 (with-output-to-temp-buffer "*Help*"
247 (princ (substitute-command-keys
248 (format "\
249 Move point to where the autoload section
250 for %s should be inserted.
251 Then do \\[exit-recursive-edit]."
252 file))))
253 (recursive-edit))
254 (generate-file-autoloads file)))
255 (if (and (null existing-buffer)
256 (setq existing-buffer (get-file-buffer file)))
257 (kill-buffer existing-buffer)))))
259 ;;;###autoload
260 (defun update-autoloads-here ()
261 "Update the sections of the current buffer generated by
262 \\[update-file-autoloads]."
263 (interactive)
264 (let ((generated-autoload-file (buffer-file-name)))
265 (save-excursion
266 (goto-char (point-min))
267 (while (search-forward generate-autoload-section-header nil t)
268 (let* ((form (condition-case ()
269 (read (current-buffer))
270 (end-of-file nil)))
271 (file (nth 3 form)))
272 (if (and (stringp file)
273 (or (get-file-buffer file)
274 (file-exists-p file)))
276 (setq file (if (y-or-n-p (format "Library \"%s\" (load \
277 file \"%s\") doesn't exist. Remove its autoload section? "
278 (nth 2 form) file))
280 (condition-case ()
281 (read-file-name (format "Find \"%s\" load file: "
282 (nth 2 form))
283 nil nil t)
284 (quit nil)))))
285 (if file
286 (let ((begin (match-beginning 0)))
287 (search-forward generate-autoload-section-trailer)
288 (delete-region begin (point))))
289 (if (stringp file)
290 (generate-file-autoloads file)))))))
292 ;;;###autoload
293 (defun update-directory-autoloads (dir)
294 "Run \\[update-file-autoloads] on each .el file in DIR."
295 (interactive "DUpdate autoloads for directory: ")
296 (mapcar 'update-file-autoloads
297 (directory-files dir nil "\\.el$")))
299 ;;;###autoload
300 (defun batch-update-autoloads ()
301 "Update the autoloads for the files or directories on the command line.
302 Runs \\[update-file-autoloads] on files and \\[update-directory-autoloads]
303 on directories. Must be used only with -batch, and kills Emacs on completion.
304 Each file will be processed even if an error occurred previously.
305 For example, invoke \"emacs -batch -f batch-update-autoloads *.el\""
306 (if (not noninteractive)
307 (error "batch-update-file-autoloads is to be used only with -batch"))
308 (let ((lost nil)
309 (args command-line-args-left))
310 (while args
311 (catch 'file
312 (condition-case lossage
313 (if (file-directory-p (expand-file-name (car args)))
314 (update-directory-autoloads (car args))
315 (update-file-autoloads (car args)))
316 (error (progn (message ">>Error processing %s: %s"
317 (car args) lossage)
318 (setq lost t)
319 (throw 'file nil)))))
320 (setq args (cdr args)))
321 (save-some-buffers t)
322 (message "Done")
323 (kill-emacs (if lost 1 0))))
325 (provide 'autoload)
327 ;;; autoload.el ends here