*** empty log message ***
[emacs.git] / lisp / emacs-lisp / autoload.el
blobc6916a3dd56a0d004828ea10a048e7513a492ed8
1 ;;; autoload.el --- maintain autoloads in loaddefs.el.
3 ;;; Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 ;;; Written by Roland McGrath.
5 ;;;
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)
9 ;;; any later version.
10 ;;;
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.
15 ;;;
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
19 ;;; 02139, USA.
20 ;;;
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)))
33 (setq doc (car form))
34 (if (stringp doc)
35 (setq form (cdr form))
36 (setq doc nil))
37 (list 'autoload (list 'quote name) file doc
38 (eq (car-safe (car form)) 'interactive) macrop))
39 nil)))
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).
46 This string is used:
48 ;;;###autoload
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 ;;;
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.
72 ;;;
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
91 are used."
92 (interactive "fGenerate autoloads for file: ")
93 (let ((outbuf (current-buffer))
94 (inbuf (find-file-noselect file))
95 (autoloads-done '())
96 (load-name (let ((name (file-name-nondirectory file)))
97 (if (string-match "\\.elc?$" name)
98 (substring name 0 (match-beginning 0))
99 name)))
100 (print-length nil)
101 (floating-output-format "%20e")
102 (done-any nil)
103 output-end)
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))))
116 (progn
117 (setq file (substring file (length default-directory)))))
119 (message "Generating autoloads for %s..." file)
120 (save-excursion
121 (set-buffer inbuf)
122 (save-excursion
123 (save-restriction
124 (widen)
125 (goto-char (point-min))
126 (while (not (eobp))
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")
131 (setq done-any t)
132 (if (eolp)
133 ;; Read the next form and make an autoload.
134 (let* ((form (prog1 (read (current-buffer))
135 (forward-line 1)))
136 (autoload (make-autoload form load-name))
137 (doc-string-elt (get (car-safe form)
138 'doc-string-elt)))
139 (if autoload
140 (setq autoloads-done (cons (nth 1 form)
141 autoloads-done))
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))
149 (elt (cdr p)))
150 (setcdr p nil)
151 (princ "\n(" outbuf)
152 (mapcar (function (lambda (elt)
153 (prin1 elt outbuf)
154 (princ " " outbuf)))
155 autoload)
156 (princ "\"\\\n" outbuf)
157 (princ (substring (prin1-to-string (car elt)) 1)
158 outbuf)
159 (if (null (cdr elt))
160 (princ ")" outbuf)
161 (princ " " outbuf)
162 (princ (substring (prin1-to-string (cdr elt))
164 outbuf))
165 (terpri outbuf))
166 (print autoload outbuf)))
167 ;; Copy the rest of the line to the output.
168 (let ((begin (point)))
169 (forward-line 1)
170 (princ (buffer-substring begin (point)) outbuf))))
171 ((looking-at ";")
172 ;; Don't read the comment.
173 (forward-line 1))
175 (forward-sexp 1)
176 (forward-line 1))))))
177 (set-buffer outbuf)
178 (setq output-end (point-marker)))
179 (if done-any
180 (progn
181 (insert generate-autoload-section-header)
182 (prin1 (list 'autoloads autoloads-done load-name file
183 (nth 5 (file-attributes file)))
184 outbuf)
185 (terpri outbuf)
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.")
196 ;;;###autoload
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))
204 name)))
205 (done nil)
206 (existing-buffer (get-file-buffer file)))
207 (save-excursion
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))
212 (save-excursion
213 (save-restriction
214 (widen)
215 (goto-char (point-min))
216 (while (search-forward generate-autoload-section-header nil t)
217 (let ((form (condition-case ()
218 (read (current-buffer))
219 (end-of-file nil))))
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."
232 file)
233 (search-forward generate-autoload-section-trailer)
234 (delete-region begin (point))
235 (generate-file-autoloads file))
236 (setq done t))))))
237 (if done
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
244 (format "\
245 Move point to where the autoload section
246 for %s should be inserted.
247 Then do \\[exit-recursive-edit]."
248 file))))
249 (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)))))
255 ;;;###autoload
256 (defun update-autoloads-here ()
257 "Update the sections of the current buffer generated by
258 \\[update-file-autoloads]."
259 (interactive)
260 (let ((generated-autoload-file (buffer-file-name)))
261 (save-excursion
262 (goto-char (point-min))
263 (while (search-forward generate-autoload-section-header nil t)
264 (let* ((form (condition-case ()
265 (read (current-buffer))
266 (end-of-file nil)))
267 (file (nth 3 form)))
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? "
274 (nth 2 form) file))
276 (condition-case ()
277 (read-file-name (format "Find \"%s\" load file: "
278 (nth 2 form))
279 nil nil t)
280 (quit nil)))))
281 (if file
282 (let ((begin (match-beginning 0)))
283 (search-forward generate-autoload-section-trailer)
284 (delete-region begin (point))))
285 (if (stringp file)
286 (generate-file-autoloads file)))))))
288 ;;;###autoload
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$")))
295 ;;;###autoload
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"))
304 (let ((lost nil)
305 (args command-line-args-left))
306 (while args
307 (catch 'file
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"
313 (car args) lossage)
314 (setq lost t)
315 (throw 'file nil)))))
316 (setq args (cdr args)))
317 (save-some-buffers t)
318 (message "Done")
319 (kill-emacs (if lost 1 0))))
321 (provide 'autoload)
323 ;;; autoload.el ends here